Fix broken TC_ACT_REDIRECT
Subsystems (9)
Jamal Hadi Salim
maintainerv2 cover Fix broken TC_ACT_REDIRECT
When sashiko-gemini[1] reviewed commit a8a02897f2b4 ("net/sched: cls_api: Handle TC_ACT_CONSUMED in tcf_qevent_handle") it
correctly pointed out the following:
"
This is a pre-existing issue, but does executing a redirect via a qevent filter cause a NULL pointer dereference?
When tcf_qevent_handle() processes a TC_ACT_REDIRECT, it calls skb_do_redirect(). This eventually calls bpf_net_ctx_get_ri() which dereferences the task bpf_net_context:
include/linux/filter.h:bpf_net_ctx_get_ri() {
...
struct bpf_net_context *bpf_net_ctx = bpf_net_ctx_get();
if (!(bpf_net_ctx->ri.kern_flags & BPF_RI_F_RI_INIT)) {
...
}Since qevents are evaluated during enqueue, which runs within __dev_queue_xmit() after sch_handle_egress() has already executed and cleared the bpf_net_context pointer, will this dereference a NULL pointer? "
That issue is fixed in patch 1. See the commit log for details.
Upon further investigation it turns out that TC_ACT_REDIRECT being returned from the egress qdiscs never actually worked. When an action returns that code we would silently loose it and the packet will never be redirected. After all those years, if nobody complained, my gut feel is it was never used by anyone with serious need for it.
Patch 2 fixes it by 1) putting a warning out when someone does and 2) asking the core to drop the packet. At least this would help whoever is misconfiguring to diagnose the issue much faster.
I had initially attempted to "fix" this and make it work, but unfortunately it's a bit ugly so i left i didnt think it was worth fixing
Apologies for the shotgun Cc - its what get_maintainer.pl told me to use.
[1] https://sashiko.dev/#/patchset/20260620130749.226642-1-jhs%40mojatatu.com
---
Changes since v1 (address 3 sashiko comments):
1)Patch 1: Address pre-existing issue to cover asynchronous qdisc enqueue
operations in particular if bpf_redirect() is invoked from an attached
ebpf program (the helper invokes bpf_net_ctx_get_ri())
https://sashiko.dev/#/patchset/20260620130749.226642-1-jhs%40mojatatu.com
2)Patch 2: Explain in the commit message that it is actually design intent to
remove TC_ACT_REDIRECT from tcf_qevent_handle().
https://sashiko.dev/#/patchset/[email protected]?part=2
3) Patch 3: be explicit with $EBPFDIR
https://sashiko.dev/#/patchset/[email protected]?part=3
--- net/core/dev.c | 31 +++++++++++++++---- include/net/pkt_cls.h | 13 +++++++ net/sched/cls_api.c | 6 +--- net/sched/sch_cake.c | 2 +- net/sched/sch_drr.c | 2 +- net/sched/sch_dualpi2.c | 2 +- net/sched/sch_ets.c | 2 +- net/sched/sch_fq_codel.c | 2 +- net/sched/sch_fq_pie.c | 2 +- net/sched/sch_hfsc.c | 2 +- net/sched/sch_htb.c | 2 +- net/sched/sch_multiq.c | 2 +- net/sched/sch_prio.c | 2 +- net/sched/sch_qfq.c | 2 +- net/sched/sch_sfb.c | 2 +- net/sched/sch_sfq.c | 2 +- tools/testing/selftests/tc-testing/action-ebpf | Bin 856 -> 9072 bytes tools/testing/selftests/tc-testing/action.c | 5 +++ .../tc-testing/tc-tests/infra/qdiscs.json | 32 ++++++++++++++ 19 files changed, 87 insertions(+), 26 deletions(-)
Jamal Hadi Salim
maintainerPATCH v2 1/3 net: Extend bpf_net_context lifetime to cover qdisc enqueue
The bpf_net_context used by sch_handle_egress() is stack-allocated and torn down in that function returned. By the time tcf_qevent_handle() runs current->bpf_net_context is NULL.
When a filter attached to a qevent block (e.g. RED's early_drop or mark qevents, which always use shared blocks) returns TC_ACT_REDIRECT, tcf_qevent_handle() calls skb_do_redirect(), which in turn calls bpf helper bpf_net_ctx_get_ri(). That helper unconditionally dereferences current->bpf_net_context resulting in a NULL pointer dereference.
Note: The same holds for actions that invoke BPF redirect helpers (e.g. act_bpf running a program that calls bpf_redirect()) during qevent classification itself.
Fix:
Move the bpf_net_context lifecycle out of sch_handle_egress() into __dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside the egress_needed_key static branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path _per packet_ only guarded by CONFIG_NET_XGRESS (two writes and one read).
As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, since qdisc_run() -> netem_dequeue() -> qdisc_enqueue( RED child) can trigger qevent classification asynchronously from softirq context.
This keeps all bpf_net_context management in net/core/dev.c i.e the existing boundary between tc core and BPF without requiring any net/sched/ code to know about BPF plumbing.
Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qevent early_drop block 10
tc filter add block 10 pref 1 bpf obj redirect.o
traffic through eth0 triggers red_enqueue() -> tcf_qevent_handle() and, on a redirect verdict, a NULL deref in skb_do_redirect().
--- net/core/dev.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
net/core/dev.c +23 -7
@@ -4527,14 +4527,11 @@ sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) {struct bpf_mprog_entry *entry =rcu_dereference_bh (dev->tcx_egress);enum skb_drop_reason drop_reason =SKB_DROP_REASON_TC_EGRESS ; -struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;int sch_ret;if (!entry)return skb; - bpf_net_ctx =bpf_net_ctx_set (&__bpf_net_ctx); -/* qdisc_skb_cb(skb)->pkt_len & tcx_set_ingress() was * already set by the caller. */
@@ -4550,12 +4547,10 @@ sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)/* No need to push/pop skb's mac_header here on egress! */ skb_do_redirect (skb); *ret =NET_XMIT_SUCCESS ; -bpf_net_ctx_clear (bpf_net_ctx);return NULL ;case TC_ACT_SHOT :kfree_skb_reason (skb, drop_reason); *ret =NET_XMIT_DROP ; -bpf_net_ctx_clear (bpf_net_ctx);return NULL ;/* used by tc_run */ case TC_ACT_STOLEN :
@@ -4565,10 +4560,8 @@ sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) fallthrough;case TC_ACT_CONSUMED : *ret =NET_XMIT_SUCCESS ; -bpf_net_ctx_clear (bpf_net_ctx);return NULL ; } -bpf_net_ctx_clear (bpf_net_ctx);return skb; }
@@ -4767,6 +4760,9 @@ struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,*/ int __dev_queue_xmit (struct sk_buff *skb,struct net_device *sb_dev) { +#ifdef CONFIG_NET_XGRESS +struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx =NULL ; +#endif struct net_device *dev = skb->dev;struct netdev_queue *txq =NULL ;enum skb_drop_reason reason;
@@ -4795,6 +4791,9 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)skb_update_prio (skb);tcx_set_ingress (skb, false); +#ifdef CONFIG_NET_XGRESS + bpf_net_ctx =bpf_net_ctx_set (&__bpf_net_ctx); +#endif #ifdef CONFIG_NET_EGRESS if (static_branch_unlikely (&egress_needed_key)) {if (nf_hook_egress_active ()) {
@@ -4898,12 +4897,18 @@ int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) reason =SKB_DROP_REASON_RECURSION_LIMIT ; drop: +#ifdef CONFIG_NET_XGRESS +bpf_net_ctx_clear (bpf_net_ctx ); +#endif rcu_read_unlock_bh ();dev_core_stats_tx_dropped_inc (dev);kfree_skb_list_reason (skb, reason);return rc; out: +#ifdef CONFIG_NET_XGRESS +bpf_net_ctx_clear (bpf_net_ctx ); +#endif rcu_read_unlock_bh ();return rc; }
@@ -5815,6 +5820,9 @@ static __latent_entropy void net_tx_action(void)if (sd->output_queue) {struct Qdisc *head; +#ifdef CONFIG_NET_XGRESS +struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; +#endif local_irq_disable (); head = sd->output_queue;
@@ -5824,6 +5832,10 @@ static __latent_entropy void net_tx_action(void)rcu_read_lock (); +#ifdef CONFIG_NET_XGRESS + bpf_net_ctx =bpf_net_ctx_set (&__bpf_net_ctx); +#endif +while (head) {spinlock_t *root_lock =NULL ;struct sk_buff *to_free;
@@ -5860,6 +5872,10 @@ static __latent_entropy void net_tx_action(void)tcf_kfree_skb_list (to_free, q,NULL ,qdisc_dev (q)); } +#ifdef CONFIG_NET_XGRESS +bpf_net_ctx_clear (bpf_net_ctx); +#endif +rcu_read_unlock (); }
Signature · 1 line
2.54.0
Daniel Borkmann
maintainerRe: [PATCH net 1/3 v2] net: Extend bpf_net_context lifetime to cover qdisc enqueue
Hi Jamal,
Quoted 38 lines · Jamal Hadi Salim
The
bpf_net_contextused bysch_handle_egress()is stack-allocated and torn down in that function returned. By the timetcf_qevent_handle()runscurrent->bpf_net_contextis NULL.When a filter attached to a qevent block (e.g. RED's
early_dropor mark qevents, which always use shared blocks) returnsTC_ACT_REDIRECT,tcf_qevent_handle()callsskb_do_redirect(), which in turn calls bpf helperbpf_net_ctx_get_ri(). That helper unconditionally dereferencescurrent->bpf_net_contextresulting in a NULL pointer dereference.Note: The same holds for actions that invoke BPF redirect helpers (e.g.
act_bpfrunning a program that callsbpf_redirect()) during qevent classification itself.Fix:
Move thebpf_net_contextlifecycle out ofsch_handle_egress()into__dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside theegress_needed_keystatic branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path_perpacket_only guarded byCONFIG_NET_XGRESS(two writes and one read).As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, sinceqdisc_run()->netem_dequeue()->qdisc_enqueue( RED child)can trigger qevent classification asynchronously from softirq context.This keeps all
bpf_net_contextmanagement innet/core/dev.ci.e the existing boundary between tc core and BPF without requiring anynet/sched/code to know about BPF plumbing.Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qeventearly_dropblock 10
tc filter add block 10 pref 1 bpf obj redirect.o
Quoted 6 lines
traffic through eth0 triggers
red_enqueue()->tcf_qevent_handle()and, on a redirect verdict, a NULL deref inskb_do_redirect().Fixes 3625750f05ec ("net: sched: Introduce helpers for qevent blocks")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>
Could we simplify patch 1 & 2 by just moving the bpf_net_ctx_set() and bpf_net_ctx_clear() into a tcf_classify_qdisc() wrapper where we don't end up having to touch the core TX code?
Untested diff :
include/net/pkt_cls.h +9 -0
@@ -93,6 +93,8 @@ int tcf_classify(struct sk_buff *skb,const struct tcf_block *block, const struct tcf_proto *tp, struct tcf_result *res, bool compat_mode); +int tcf_classify_qdisc (struct sk_buff *skb,const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode); static inline bool tc_cls_stats_dump(struct tcf_proto *tp, struct tcf_walker *arg,
@@ -157,6 +159,13 @@ static inline int tcf_classify(struct sk_buff *skb,return TC_ACT_UNSPEC ; } +static inline int tcf_classify_qdisc (struct sk_buff *skb, +const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{ +return tcf_classify (skb,NULL , tp, res, compat_mode); +} +#endif static inline unsigned long
net/sched/cls_api.c +20 -5
@@ -23,6 +23,7 @@#include <linux/jhash.h> #include <linux/rculist.h> #include <linux/rhashtable.h> +#include <linux/filter.h> #include <net/net_namespace.h> #include <net/sock.h> #include <net/netlink.h>
@@ -1884,6 +1885,24 @@ int tcf_classify(struct sk_buff *skb, }EXPORT_SYMBOL (tcf_classify); +int tcf_classify_qdisc (struct sk_buff *skb,const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{ +struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; +int ret; + + bpf_net_ctx =bpf_net_ctx_set (&__bpf_net_ctx); + ret =tcf_classify (skb,NULL , tp, res, compat_mode); +bpf_net_ctx_clear (bpf_net_ctx); + +if (unlikely (ret ==TC_ACT_REDIRECT )) { +pr_warn_once ("TC_ACT_REDIRECT from qdisc filter chains is not supported\n" ); + ret =TC_ACT_SHOT ; + } +return ret; +} +EXPORT_SYMBOL (tcf_classify_qdisc); +struct tcf_chain_info {struct tcf_proto __rcu **pprev;struct tcf_proto __rcu *next;
@@ -4033,7 +4052,7 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru fl =rcu_dereference_bh (qe->filter_chain); -switch (tcf_classify (skb,NULL , fl, &cl_res, false)) { +switch (tcf_classify_qdisc (skb, fl, &cl_res, false)) {case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);
@@ -4045,10 +4064,6 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru__qdisc_drop (skb, to_free); *ret = __NET_XMIT_STOLEN;return NULL ; -case TC_ACT_REDIRECT : -skb_do_redirect (skb); - *ret = __NET_XMIT_STOLEN; -return NULL ;case TC_ACT_CONSUMED : *ret = __NET_XMIT_STOLEN;return NULL ;
net/sched/sch_cake.c +1 -1
@@ -1730,7 +1730,7 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, goto hash; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT
net/sched/sch_drr.c +1 -1
@@ -312,7 +312,7 @@ static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_dualpi2.c +1 -1
@@ -364,7 +364,7 @@ static int dualpi2_skb_classify(struct dualpi2_sched_data *q,return NET_XMIT_SUCCESS ; } - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_ets.c +1 -1
@@ -391,7 +391,7 @@ static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_fq_codel.c +1 -1
@@ -91,7 +91,7 @@ static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_codel_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_fq_pie.c +1 -1
@@ -96,7 +96,7 @@ static unsigned int fq_pie_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_pie_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_hfsc.c +1 -1
@@ -1143,7 +1143,7 @@ hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; head = &q->root; tcf =rcu_dereference_bh (q->root.filter_list); -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :
net/sched/sch_htb.c +1 -1
@@ -243,7 +243,7 @@ static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch, } *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :
net/sched/sch_multiq.c +1 -1
@@ -36,7 +36,7 @@ multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)int err; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_prio.c +1 -1
@@ -39,7 +39,7 @@ prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_qfq.c +1 -1
@@ -709,7 +709,7 @@ static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_sfb.c +1 -1
@@ -260,7 +260,7 @@ static bool sfb_classify(struct sk_buff *skb, struct tcf_proto *fl,struct tcf_result res;int result; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_sfq.c +1 -1
@@ -171,7 +171,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,return sfq_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
Thanks,
Daniel
Jamal Hadi Salim
maintainerRe: [PATCH net 1/3 v2] net: Extend bpf_net_context lifetime to cover qdisc enqueue
Quoted 45 lines · Daniel Borkmann
Hi Jamal,
Quoted 41 lines · Jamal Hadi Salim
The
bpf_net_contextused bysch_handle_egress()is stack-allocated and torn down in that function returned. By the timetcf_qevent_handle()runscurrent->bpf_net_contextis NULL.When a filter attached to a qevent block (e.g. RED's
early_dropor mark qevents, which always use shared blocks) returnsTC_ACT_REDIRECT,tcf_qevent_handle()callsskb_do_redirect(), which in turn calls bpf helperbpf_net_ctx_get_ri(). That helper unconditionally dereferencescurrent->bpf_net_contextresulting in a NULL pointer dereference.Note: The same holds for actions that invoke BPF redirect helpers (e.g.
act_bpfrunning a program that callsbpf_redirect()) during qevent classification itself.Fix:
Move thebpf_net_contextlifecycle out ofsch_handle_egress()into__dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside theegress_needed_keystatic branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path_perpacket_only guarded byCONFIG_NET_XGRESS(two writes and one read).As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, sinceqdisc_run()->netem_dequeue()->qdisc_enqueue( RED child)can trigger qevent classification asynchronously from softirq context.This keeps all
bpf_net_contextmanagement innet/core/dev.ci.e the existing boundary between tc core and BPF without requiring anynet/sched/code to know about BPF plumbing.Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qeventearly_dropblock 10
tc filter add block 10 pref 1 bpf obj redirect.otraffic through eth0 triggers
red_enqueue()->tcf_qevent_handle()and, on a redirect verdict, a NULL deref inskb_do_redirect().
Quoted 9 lines
Quoted 3 lines
Fixes 3625750f05ec ("net: sched: Introduce helpers for qevent blocks")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Could we simplify patch 1 & 2 by just moving the
bpf_net_ctx_set()andbpf_net_ctx_clear()into atcf_classify_qdisc()wrapper where we don't end up having to touch the core TX code?Untested diff :
This is bpf plumbing which doesnt belong in tc really. You already moved most ebpf/clsact stuff into dev.c - let's just keep it there.
As a side note: calling a hierachy of N qdiscs we would incur N set/clear cycles for N levels — and worse, qdiscs like HTB and HFSC iterate filters while loop calling tcf_classify_qdisc() per iteration, so each filter chain traversal does set/clear per proto.
cheers,
jamal
Quoted 256 lines
include/net/pkt_cls.h +9
-0@@ -93,6 +93,8 @@ int tcf_classify(struct sk_buff *skb,const struct tcf_block *block, const struct tcf_proto *tp, struct tcf_result *res, bool compat_mode); +int tcf_classify_qdisc (struct sk_buff *skb,const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode); static inline bool tc_cls_stats_dump(struct tcf_proto *tp, struct tcf_walker *arg,@@ -157,6 +159,13 @@ static inline int tcf_classify(struct sk_buff *skb,return TC_ACT_UNSPEC ; } +static inline int tcf_classify_qdisc (struct sk_buff *skb, +const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{ +return tcf_classify (skb,NULL , tp, res, compat_mode); +} +#endif static inline unsigned longnet/sched/cls_api.c +20
-5@@ -23,6 +23,7 @@#include <linux/jhash.h> #include <linux/rculist.h> #include <linux/rhashtable.h> +#include <linux/filter.h> #include <net/net_namespace.h> #include <net/sock.h> #include <net/netlink.h> @@ -1884,6 +1885,24 @@ int tcf_classify(struct sk_buff *skb, }EXPORT_SYMBOL (tcf_classify); +int tcf_classify_qdisc (struct sk_buff *skb,const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{ +struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; +int ret; + + bpf_net_ctx =bpf_net_ctx_set (&__bpf_net_ctx); + ret =tcf_classify (skb,NULL , tp, res, compat_mode); +bpf_net_ctx_clear (bpf_net_ctx); + +if (unlikely (ret ==TC_ACT_REDIRECT )) { +pr_warn_once ("TC_ACT_REDIRECT from qdisc filter chains is not supported\n" ); + ret =TC_ACT_SHOT ; + } +return ret; +} +EXPORT_SYMBOL (tcf_classify_qdisc); +struct tcf_chain_info {struct tcf_proto __rcu **pprev;struct tcf_proto __rcu *next;@@ -4033,7 +4052,7 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru fl =rcu_dereference_bh (qe->filter_chain); -switch (tcf_classify (skb,NULL , fl, &cl_res, false)) { +switch (tcf_classify_qdisc (skb, fl, &cl_res, false)) {case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);@@ -4045,10 +4064,6 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru__qdisc_drop (skb, to_free); *ret = __NET_XMIT_STOLEN;return NULL ; -case TC_ACT_REDIRECT : -skb_do_redirect (skb); - *ret = __NET_XMIT_STOLEN; -return NULL ;case TC_ACT_CONSUMED : *ret = __NET_XMIT_STOLEN;return NULL ;net/sched/sch_cake.c +1
-1@@ -1730,7 +1730,7 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, goto hash; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT net/sched/sch_drr.c +1
-1@@ -312,7 +312,7 @@ static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_dualpi2.c +1
-1@@ -364,7 +364,7 @@ static int dualpi2_skb_classify(struct dualpi2_sched_data *q,return NET_XMIT_SUCCESS ; } - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_ets.c +1
-1@@ -391,7 +391,7 @@ static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :net/sched/sch_fq_codel.c +1
-1@@ -91,7 +91,7 @@ static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_codel_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_fq_pie.c +1
-1@@ -96,7 +96,7 @@ static unsigned int fq_pie_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_pie_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_hfsc.c +1
-1@@ -1143,7 +1143,7 @@ hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; head = &q->root; tcf =rcu_dereference_bh (q->root.filter_list); -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :net/sched/sch_htb.c +1
-1@@ -243,7 +243,7 @@ static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch, } *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :net/sched/sch_multiq.c +1
-1@@ -36,7 +36,7 @@ multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)int err; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :net/sched/sch_prio.c +1
-1@@ -39,7 +39,7 @@ prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :net/sched/sch_qfq.c +1
-1@@ -709,7 +709,7 @@ static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_sfb.c +1
-1@@ -260,7 +260,7 @@ static bool sfb_classify(struct sk_buff *skb, struct tcf_proto *fl,struct tcf_result res;int result; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {net/sched/sch_sfq.c +1
-1@@ -171,7 +171,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,return sfq_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH net 1/3 v2] net: Extend bpf_net_context lifetime to cover qdisc enqueue
Quoted 52 lines · Jamal Hadi Salim
Quoted 42 lines · Daniel Borkmann
Quoted 41 lines · Jamal Hadi Salim
The
bpf_net_contextused bysch_handle_egress()is stack-allocated and torn down in that function returned. By the timetcf_qevent_handle()runscurrent->bpf_net_contextis NULL.When a filter attached to a qevent block (e.g. RED's
early_dropor mark qevents, which always use shared blocks) returnsTC_ACT_REDIRECT,tcf_qevent_handle()callsskb_do_redirect(), which in turn calls bpf helperbpf_net_ctx_get_ri(). That helper unconditionally dereferencescurrent->bpf_net_contextresulting in a NULL pointer dereference.Note: The same holds for actions that invoke BPF redirect helpers (e.g.
act_bpfrunning a program that callsbpf_redirect()) during qevent classification itself.Fix:
Move thebpf_net_contextlifecycle out ofsch_handle_egress()into__dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside theegress_needed_keystatic branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path_perpacket_only guarded byCONFIG_NET_XGRESS(two writes and one read).As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, sinceqdisc_run()->netem_dequeue()->qdisc_enqueue( RED child)can trigger qevent classification asynchronously from softirq context.This keeps all
bpf_net_contextmanagement innet/core/dev.ci.e the existing boundary between tc core and BPF without requiring anynet/sched/code to know about BPF plumbing.Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qeventearly_dropblock 10
tc filter add block 10 pref 1 bpf obj redirect.otraffic through eth0 triggers
red_enqueue()->tcf_qevent_handle()and, on a redirect verdict, a NULL deref inskb_do_redirect().Quoted 8 lines
Quoted 3 lines
Fixes 3625750f05ec ("net: sched: Introduce helpers for qevent blocks")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Could we simplify patch 1 & 2 by just moving the
bpf_net_ctx_set()andbpf_net_ctx_clear()into atcf_classify_qdisc()wrapper where we don't end up having to touch the core TX code?Untested diff :
Quoted 7 lines
This is bpf plumbing which doesnt belong in tc really. You already moved most ebpf/clsact stuff into
dev.c- let's just keep it there.As a side note: calling a hierachy of N qdiscs we would incur N set/clear cycles for N levels — and worse, qdiscs like HTB and HFSC iterate filters while loop calling
tcf_classify_qdisc()per iteration, so each filter chain traversal does set/clear per proto.
I'm just saying that this is a lot simpler and taken out of the core fast path. Also, I think you forgot to Cc Sebastian on the whole v2 given the bpf_net_ctx_{set,clear} dance. Imho, having them via tcf_classify_qdisc or something similar would be the much better choice compared to sprinkling ifdefs since you want to block the TC_ACT_REDIRECT from classifiers attached to qdiscs.
Daniel Borkmann
maintainerRe: [PATCH net 1/3 v2] net: Extend bpf_net_context lifetime to cover qdisc enqueue
Quoted 60 lines · Daniel Borkmann
Quoted 52 lines · Jamal Hadi Salim
Quoted 42 lines · Daniel Borkmann
Quoted 41 lines · Jamal Hadi Salim
The
bpf_net_contextused bysch_handle_egress()is stack-allocated and torn down in that function returned. By the timetcf_qevent_handle()runscurrent->bpf_net_contextis NULL.When a filter attached to a qevent block (e.g. RED's
early_dropor mark qevents, which always use shared blocks) returnsTC_ACT_REDIRECT,tcf_qevent_handle()callsskb_do_redirect(), which in turn calls bpf helper bpf_net_ctx_get_ri(). That helper unconditionally dereferencescurrent->bpf_net_contextresulting in a NULL pointer dereference.Note: The same holds for actions that invoke BPF redirect helpers (e.g.
act_bpfrunning a program that callsbpf_redirect()) during qevent classification itself.Fix:
Move thebpf_net_contextlifecycle out ofsch_handle_egress()into__dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside theegress_needed_keystatic branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path_perpacket_only guarded byCONFIG_NET_XGRESS(two writes and one read).As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, sinceqdisc_run()->netem_dequeue()->qdisc_enqueue( RED child)can trigger qevent classification asynchronously from softirq context.This keeps all
bpf_net_contextmanagement innet/core/dev.ci.e the existing boundary between tc core and BPF without requiring anynet/sched/code to know about BPF plumbing.Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qeventearly_dropblock 10
tc filter add block 10 pref 1 bpf obj redirect.otraffic through eth0 triggers
red_enqueue()->tcf_qevent_handle()and, on a redirect verdict, a NULL deref inskb_do_redirect().Quoted 8 lines
Quoted 3 lines
Fixes 3625750f05ec ("net: sched: Introduce helpers for qevent blocks")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Could we simplify patch 1 & 2 by just moving the
bpf_net_ctx_set()andbpf_net_ctx_clear()into atcf_classify_qdisc()wrapper where we don't end up having to touch the core TX code?Untested diff :
Quoted 7 lines
This is bpf plumbing which doesnt belong in tc really. You already moved most ebpf/clsact stuff into
dev.c- let's just keep it there.As a side note: calling a hierachy of N qdiscs we would incur N set/clear cycles for N levels — and worse, qdiscs like HTB and HFSC iterate filters while loop calling
tcf_classify_qdisc()per iteration, so each filter chain traversal does set/clear per proto.
Quoted 6 lines
I'm just saying that this is a lot simpler and taken out of the core fast path. Also, I think you forgot to Cc Sebastian on the whole v2 given the bpf_net_ctx_{set,clear} dance. Imho, having them via
tcf_classify_qdiscor something similar would be the much better choice compared to sprinkling ifdefs since you want to block theTC_ACT_REDIRECTfrom classifiers attached to qdiscs.
Q @ Sebastian, we could probably also do sth like this, wdyt between the two options :
net/core/filter.c +3 -3
@@ -2554,7 +2554,7 @@ BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) {struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); -if (unlikely (flags & (~(BPF_F_INGRESS ) |BPF_F_REDIRECT_INTERNAL ))) +if (unlikely (!ri || (flags & (~(BPF_F_INGRESS ) |BPF_F_REDIRECT_INTERNAL ))))return TC_ACT_SHOT ; ri->flags = flags;
@@ -2575,7 +2575,7 @@ BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags) {struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); -if (unlikely (flags)) +if (unlikely (!ri || flags))return TC_ACT_SHOT ; ri->flags =BPF_F_PEER ;
@@ -2597,7 +2597,7 @@ BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params, {struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); -if (unlikely ((plen && plen <sizeof (*params)) || flags)) +if (unlikely ((plen && plen <sizeof (*params)) || !ri || flags))return TC_ACT_SHOT ; ri->flags =BPF_F_NEIGH | (plen ?BPF_F_NEXTHOP :0 );
Jamal Hadi Salim
maintainerRe: [PATCH net 1/3 v2] net: Extend bpf_net_context lifetime to cover qdisc enqueue
In reply to Daniel BorkmannQuoted 61 lines · Daniel Borkmann
Quoted 52 lines · Jamal Hadi Salim
Quoted 42 lines · Daniel Borkmann
Quoted 41 lines · Jamal Hadi Salim
The
bpf_net_contextused bysch_handle_egress()is stack-allocated and torn down in that function returned. By the timetcf_qevent_handle()runscurrent->bpf_net_contextis NULL.When a filter attached to a qevent block (e.g. RED's
early_dropor mark qevents, which always use shared blocks) returnsTC_ACT_REDIRECT,tcf_qevent_handle()callsskb_do_redirect(), which in turn calls bpf helperbpf_net_ctx_get_ri(). That helper unconditionally dereferencescurrent->bpf_net_contextresulting in a NULL pointer dereference.Note: The same holds for actions that invoke BPF redirect helpers (e.g.
act_bpfrunning a program that callsbpf_redirect()) during qevent classification itself.Fix:
Move thebpf_net_contextlifecycle out ofsch_handle_egress()into__dev_queue_xmit(), so that it spans both the egress TC fast path and the qdisc enqueue.
Note: The call is placed outside theegress_needed_keystatic branch to cover the case where clsact static key is disabled. Unfortunately this adds a small unconditional penalty to the code path_perpacket_only guarded byCONFIG_NET_XGRESS(two writes and one read).As pointed by sashiko [1]:
The same context must also be set up in net_tx_action()'s qdisc drain path, sinceqdisc_run()->netem_dequeue()->qdisc_enqueue( RED child)can trigger qevent classification asynchronously from softirq context.This keeps all
bpf_net_contextmanagement innet/core/dev.ci.e the existing boundary between tc core and BPF without requiring anynet/sched/code to know about BPF plumbing.Reproducer:
tc qdisc add dev eth0 root handle 1: red limit 1MB min 10KB max 20KB \
avpkt 1000 burst 100 qeventearly_dropblock 10
tc filter add block 10 pref 1 bpf obj redirect.otraffic through eth0 triggers
red_enqueue()->tcf_qevent_handle()and, on a redirect verdict, a NULL deref inskb_do_redirect().Quoted 8 lines
Quoted 3 lines
Fixes 3625750f05ec ("net: sched: Introduce helpers for qevent blocks")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Could we simplify patch 1 & 2 by just moving the
bpf_net_ctx_set()andbpf_net_ctx_clear()into atcf_classify_qdisc()wrapper where we don't end up having to touch the core TX code?Untested diff :
Quoted 7 lines
This is bpf plumbing which doesnt belong in tc really. You already moved most ebpf/clsact stuff into
dev.c- let's just keep it there.As a side note: calling a hierachy of N qdiscs we would incur N set/clear cycles for N levels — and worse, qdiscs like HTB and HFSC iterate filters while loop calling
tcf_classify_qdisc()per iteration, so each filter chain traversal does set/clear per proto.
Quoted 6 lines
I'm just saying that this is a lot simpler and taken out of the core fast path. Also, I think you forgot to Cc Sebastian on the whole v2 given the bpf_net_ctx_{set,clear} dance. Imho, having them via
tcf_classify_qdiscor something similar would be the much better choice compared to sprinkling ifdefs since you want to block theTC_ACT_REDIRECTfrom classifiers attached to qdiscs.
And you are clearly not listening to what i said.
Something similar would be fine - but stop using tc as your dumping ground.
cheers,
jamal
Jamal Hadi Salim
maintainerPATCH v2 2/3 net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
In reply to Jamal Hadi SalimWhen a TC filter attached to a qdisc filter chain returns TC_ACT_REDIRECT (ex: via an eBPF program calling bpf_redirect() or an act_bpf action), the redirect was silently lost i.e no qdisc classify function handled TC_ACT_REDIRECT, so the packet fell through the switch and was enqueued normally instead of being redirected.
This has been broken since bpf_redirect() was introduced for TC in
commit 27b29f63058d ("bpf: add bpf_redirect() helper"). We got lucky
for a long time because bpf_net_context was a per-CPU variable that
was always available.
commit 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.") turned bpf_net_context into a task_struct member that is only set up by explicit callers. Without a caller setting it up, bpf_redirect() itself crashes with a NULL pointer dereference in bpf_net_ctx_get_ri().
The NULL deref is fixed separately by extending the bpf_net_context lifetime to cover qdisc enqueue. However, even with bpf_net_context available, TC_ACT_REDIRECT from qdisc filter chains cannot be honored without adding skb_do_redirect() calls to every qdisc classify function, which would require changes across net/sched/. Isolate it to ebpf core where it belongs.
Instead, add a tcf_classify_qdisc() inline helper in pkt_cls.h, as a wrapper around tcf_classify() for use by qdisc classify functions and tcf_qevent_handle(). When the classify verdict is TC_ACT_REDIRECT, the wrapper converts it to TC_ACT_SHOT, dropping the packet rather than letting it continue silently. Dropping is preferred over letting the packet through because the user immediately sees packet loss and, with the help of the rate-limited warning in the log ("use eBPF with clsact or mirred redirect instead"), can quickly identify and fix the misconfiguration. Silently passing the packet through would hide the problem and leave the user wondering why their redirect is not working.
The clsact fast path, tc_run() continues to call tcf_classify() directly and is unaffected: TC_ACT_REDIRECT is returned as-is and handled by sch_handle_egress/ingress() calling skb_do_redirect() as before.
Also (to emphasize again to Sashiko):
Remove the TC_ACT_REDIRECT case from tcf_qevent_handle() as well. skb_do_redirect() belongs in the BPF plumbing layer (net/core/), not in net/sched/. The case was never consistent with the rest of the qdisc classification infrastructure, where no classify function handles TC_ACT_REDIRECT. It appears to have been a cut-and-paste artifact from the qevent introduction rather than a deliberate design decision.
--- include/net/pkt_cls.h | 13 +++++++++++++ net/sched/cls_api.c | 6 +----- net/sched/sch_cake.c | 2 +- net/sched/sch_drr.c | 2 +- net/sched/sch_dualpi2.c | 2 +- net/sched/sch_ets.c | 2 +- net/sched/sch_fq_codel.c | 2 +- net/sched/sch_fq_pie.c | 2 +- net/sched/sch_hfsc.c | 2 +- net/sched/sch_htb.c | 2 +- net/sched/sch_multiq.c | 2 +- net/sched/sch_prio.c | 2 +- net/sched/sch_qfq.c | 2 +- net/sched/sch_sfb.c | 2 +- net/sched/sch_sfq.c | 2 +- 15 files changed, 27 insertions(+), 18 deletions(-)
include/net/pkt_cls.h +13 -0
@@ -159,6 +159,19 @@ static inline int tcf_classify(struct sk_buff *skb,#endif +static inline int tcf_classify_qdisc (struct sk_buff *skb, +const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{ +int ret =tcf_classify (skb,NULL , tp, res, compat_mode); + +if (unlikely (ret ==TC_ACT_REDIRECT )) { +pr_warn_once ("TC_ACT_REDIRECT from qdisc filter chains is not supported; use eBPF with clsact or mirred redirect instead\n" ); + ret =TC_ACT_SHOT ; + } +return ret; +} +static inline unsigned long __cls_set_class (unsigned long *clp,unsigned long cl) {
net/sched/cls_api.c +1 -5
@@ -4033,7 +4033,7 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru fl =rcu_dereference_bh (qe->filter_chain); -switch (tcf_classify (skb,NULL , fl, &cl_res, false)) { +switch (tcf_classify_qdisc (skb, fl, &cl_res, false)) {case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);
@@ -4045,10 +4045,6 @@ struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, stru__qdisc_drop (skb, to_free); *ret = __NET_XMIT_STOLEN;return NULL ; -case TC_ACT_REDIRECT : -skb_do_redirect (skb); - *ret = __NET_XMIT_STOLEN; -return NULL ;case TC_ACT_CONSUMED : *ret = __NET_XMIT_STOLEN;return NULL ;
net/sched/sch_cake.c +1 -1
@@ -1730,7 +1730,7 @@ static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, goto hash; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT
net/sched/sch_drr.c +1 -1
@@ -312,7 +312,7 @@ static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_dualpi2.c +1 -1
@@ -364,7 +364,7 @@ static int dualpi2_skb_classify(struct dualpi2_sched_data *q,return NET_XMIT_SUCCESS ; } - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_ets.c +1 -1
@@ -391,7 +391,7 @@ static struct ets_class *ets_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_fq_codel.c +1 -1
@@ -91,7 +91,7 @@ static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_codel_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_fq_pie.c +1 -1
@@ -96,7 +96,7 @@ static unsigned int fq_pie_classify(struct sk_buff *skb, struct Qdisc *sch,return fq_pie_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , filter, &res, false); + result =tcf_classify_qdisc (skb, filter, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_hfsc.c +1 -1
@@ -1143,7 +1143,7 @@ hfsc_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; head = &q->root; tcf =rcu_dereference_bh (q->root.filter_list); -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :
net/sched/sch_htb.c +1 -1
@@ -243,7 +243,7 @@ static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch, } *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; -while (tcf && (result =tcf_classify (skb,NULL , tcf, &res, false)) >=0 ) { +while (tcf && (result =tcf_classify_qdisc (skb, tcf, &res, false)) >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {case TC_ACT_QUEUED :
net/sched/sch_multiq.c +1 -1
@@ -36,7 +36,7 @@ multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)int err; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_prio.c +1 -1
@@ -39,7 +39,7 @@ prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr) *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;if (TC_H_MAJ (skb->priority) != sch->handle) { fl =rcu_dereference_bh (q->filter_list); - err =tcf_classify (skb,NULL , fl, &res, false); + err =tcf_classify_qdisc (skb, fl, &res, false);#ifdef CONFIG_NET_CLS_ACT switch (err) {case TC_ACT_STOLEN :
net/sched/sch_qfq.c +1 -1
@@ -709,7 +709,7 @@ static struct qfq_class *qfq_classify(struct sk_buff *skb, struct Qdisc *sch, *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; fl =rcu_dereference_bh (q->filter_list); - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_sfb.c +1 -1
@@ -260,7 +260,7 @@ static bool sfb_classify(struct sk_buff *skb, struct tcf_proto *fl,struct tcf_result res;int result; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
net/sched/sch_sfq.c +1 -1
@@ -171,7 +171,7 @@ static unsigned int sfq_classify(struct sk_buff *skb, struct Qdisc *sch,return sfq_hash (q, skb) +1 ; *qerr =NET_XMIT_SUCCESS | __NET_XMIT_BYPASS; - result =tcf_classify (skb,NULL , fl, &res, false); + result =tcf_classify_qdisc (skb, fl, &res, false);if (result >=0 ) {#ifdef CONFIG_NET_CLS_ACT switch (result) {
Signature · 1 line
2.54.0
Jamal Hadi Salim
maintainerPATCH v2 3/3 selftests/tc-testing: Verify bpf redirect on RED block with preceding clsact (egress) classifier
In reply to Jamal Hadi SalimFrom: Victor Nogueira <[email protected]>
The bpf_net_context used by sch_handle_egress() is stack-allocated and torn down in that function. By the time tcf_qevent_handle() runs current->bpf_net_context is NULL.
When a filter attached to a qevent block (e.g. RED's early_drop or mark qevents, which always uses shared blocks) returns TC_ACT_REDIRECT, tcf_qevent_handle() calls skb_do_redirect(), which in turn calls bpf helper bpf_net_ctx_get_ri(). That helper unconditionally dereferences current->bpf_net_context resulting in a NULL pointer dereference.
Add a test case that reproduces this scenario by attaching a filter to clsact (egress) and a bpf filter to a block attached to RED. Use TBF as red's parent, so that a traffic burst builds backlog and RED early-drops triggers the block filter.
--- .../testing/selftests/tc-testing/action-ebpf | Bin 856 -> 9072 bytes tools/testing/selftests/tc-testing/action.c | 5 +++ .../tc-testing/tc-tests/infra/qdiscs.json | 32 ++++++++++++++++++ 3 files changed, 37 insertions(+)
tools/testing/selftests/tc-testing/action-ebpf binary +0 -0
tools/testing/selftests/tc-testing/action.c +5 -0
@@ -20,4 +20,9 @@ __attribute__((section("action-ko"),used)) int action_ko(struct __sk_buff *s)return TC_ACT_OK ; } +__attribute__((section ("action-redirect" ), used))int action_redirect (struct __sk_buff *s) +{ +return TC_ACT_REDIRECT ; +} + char _license[] __attribute__((section("license"),used)) = "GPL";
tools/testing/selftests/tc-testing/tc-tests/infra/qdiscs.json +32 -0
@@ -1540,5 +1540,37 @@ "$TC qdisc del dev $DUMMY root", "$IP addr del 10.10.10.10/24 dev $DUMMY || true" ] + }, + { + "id": "fb8d", + "name": "Verify bpf redirect on RED block with preceding clsact (egress) classifier", + "category": [ + "qdisc", + "red", + "qevent", + "clsact" + ], + "plugins": { + "requires": "nsPlugin" + }, + "setup": [ + "$IP addr add 10.10.10.1/24 dev $DUMMY", + "$IP neigh add 10.10.10.2 lladdr 02:00:00:00:00:01 dev $DUMMY nud permanent", + "$TC qdisc add dev $DUMMY handle 1: root tbf rate 1Mbit burst 10K limit 1M", + "$TC qdisc add dev $DUMMY parent 1:1 handle 11: red limit 1M avpkt 1400 probability 1 burst 38 harddrop min 30000 max 30001 qevent early_drop block 10", + "$TC qdisc add dev $DUMMY clsact", + "$TC filter add dev $DUMMY egress protocol ip prio 1 matchall action gact pass", + "$TC filter add block 10 protocol ip prio 1 matchall action bpf obj $EBPFDIR/action-ebpf sec action-redirect" + ], + "cmdUnderTest": "bash -c 'data=$(head -c 1400 /dev/zero | tr \"\\0\" \"x\"); exec 3>/dev/udp/10.10.10.2/12345; for i in $(seq 1 8000); do printf \"%s\" \"$data\" >&3; done; exit 0'", + "expExitCode": "0", + "verifyCmd": "$TC -s filter show block 10", + "matchPattern": "Sent [1-9][0-9]* bytes [1-9][0-9]* pkt", + "matchCount": "1", + "teardown": [ + "$TC qdisc del dev $DUMMY clsact", + "$TC qdisc del dev $DUMMY handle 1: root", + "$IP addr del 10.10.10.1/24 dev $DUMMY" + ] } ]
Signature · 1 line
2.54.0