Fix broken TC_ACT_REDIRECT from qdiscs
Subsystems (8)
Daniel Borkmann
maintainercover Fix broken TC_ACT_REDIRECT from qdiscs
This is an alternative fix to [0] in order to not uglify __dev_queue_xmit() with sprinkled ifdefs given this can be simplified and isolated through a simple test into the BPF redirect helper itself.
I've also added a proper BPF selftest, so there is no need to check-in a binary BPF object into selftests given we do have BPF infra for all of this.
[0] https://lore.kernel.org/netdev/[email protected]/
[1] https://lore.kernel.org/netdev/[email protected]/
Daniel Borkmann (2):
bpf: Reject redirect helpers without a bpf_net_context selftests/bpf: Add test for redirect from qdisc qevent block
Jamal Hadi Salim (1):
net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
include/net/pkt_cls.h | 14 ++- net/core/filter.c | 17 ++- 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/bpf/config | 1 + .../selftests/bpf/prog_tests/tc_qevent.c | 113 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_qevent.c | 23 ++++ 19 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_qevent.c create mode 100644 tools/testing/selftests/bpf/progs/test_tc_qevent.c
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH 1/3 bpf: Reject redirect helpers without a bpf_net_context
The bpf_redirect*() helpers and skb_do_redirect() obtain the per-task bpf_redirect_info via bpf_net_ctx_get_ri(), which dereferences the current->bpf_net_context unconditionally. That context is established on the paths that run tc BPF such as sch_handle_{ingress,egress}(), *except* for the case where {cls,act}_bpf was attached to a proper qdisc. A program running from there reaches the NULL deref in two ways:
* It calls bpf_redirect() directly, which dereferences the context at the top of the helper:
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
* It simply returns TC_ACT_REDIRECT without helper call: tcf_qevent_handle() then dispatches to skb_do_redirect(), which dereferences the context
Rather than extending bpf_net_context management into the qdisc path, make the redirect helpers refuse to operate when no context exists, and have tcf_qevent_handle() drop a TC_ACT_REDIRECT verdict instead of calling skb_do_redirect(). Previous behaviour was a crash, so nothing regresses by not supporting it.
--- net/core/filter.c | 17 +++++++++++------ net/sched/cls_api.c | 6 ++---- 2 files changed, 13 insertions(+), 10 deletions(-)
net/core/filter.c +11 -6
@@ -2552,11 +2552,13 @@ int skb_do_redirect(struct sk_buff *skb) BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags) { -struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); +struct bpf_redirect_info *ri; -if (unlikely (flags & (~(BPF_F_INGRESS ) |BPF_F_REDIRECT_INTERNAL ))) +if (unlikely (!bpf_net_ctx_get () || + (flags & (~(BPF_F_INGRESS ) |BPF_F_REDIRECT_INTERNAL ))))return TC_ACT_SHOT ; + ri =bpf_net_ctx_get_ri (); ri->flags = flags; ri->tgt_index = ifindex;
@@ -2573,11 +2575,12 @@ static const struct bpf_func_proto bpf_redirect_proto = { BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags) { -struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); +struct bpf_redirect_info *ri; -if (unlikely (flags)) +if (unlikely (!bpf_net_ctx_get () || flags))return TC_ACT_SHOT ; + ri =bpf_net_ctx_get_ri (); ri->flags =BPF_F_PEER ; ri->tgt_index = ifindex;
@@ -2595,11 +2598,13 @@ static const struct bpf_func_proto bpf_redirect_peer_proto = { BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params, int, plen, u64, flags) { -struct bpf_redirect_info *ri =bpf_net_ctx_get_ri (); +struct bpf_redirect_info *ri; -if (unlikely ((plen && plen <sizeof (*params)) || flags)) +if (unlikely ((plen && plen <sizeof (*params)) || + !bpf_net_ctx_get () || flags))return TC_ACT_SHOT ; + ri =bpf_net_ctx_get_ri (); ri->flags =BPF_F_NEIGH | (plen ?BPF_F_NEXTHOP :0 ); ri->tgt_index = ifindex;
net/sched/cls_api.c +2 -4
@@ -4034,6 +4034,8 @@ 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)) { +case TC_ACT_REDIRECT : + fallthrough;case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);
@@ -4045,10 +4047,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 ;
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH 2/3 net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
In reply to Daniel BorkmannFrom: Jamal Hadi Salim <[email protected]>
When 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(). 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. 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.
--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)
include/net/pkt_cls.h +13 -1
@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#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); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. +*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + 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 -3
@@ -4033,9 +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)) { -case TC_ACT_REDIRECT : - fallthrough; +switch (tcf_classify_qdisc (skb, fl, &cl_res, false)) {case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);
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.43.0
Jamal Hadi Salim
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Quoted 74 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)include/net/pkt_cls.h +4
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#endif +static inline int tcf_classify_qdisc (struct sk_buff *skb, +const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode) +{
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 173
Quoted 10 lines
include/net/pkt_cls.h +9
-0+int ret =tcf_classify (skb,NULL , tp, res, compat_mode); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. +*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}
Why did you remove the warning?
A lesser issue is that you introduced a space above #endif
cheers,
jamal
Quoted 189 lines
include/net/pkt_cls.h +0
-0static inline unsigned long __cls_set_class (unsigned long *clp,unsigned long cl)net/sched/cls_api.c +1
-3@@ -4033,9 +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)) { -case TC_ACT_REDIRECT : - fallthrough; +switch (tcf_classify_qdisc (skb, fl, &cl_res, false)) {case TC_ACT_SHOT :qdisc_qstats_drop (sch);__qdisc_drop (skb, to_free);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
-2@@ -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) { --2.43.0
Daniel Borkmann
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Quoted 78 lines · Jamal Hadi Salim
Quoted 77 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)include/net/pkt_cls.h +8
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#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); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead.
Quoted 8 lines
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 172
Quoted 5 lines
include/net/pkt_cls.h +5
-0+*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}Why did you remove the warning?
A lesser issue is that you introduced a space above #endif
I don't think we need to put out an extra warn to spam the log, this can be easily traced either via bpftrace or qdisc stats etc; plus the guidance to eBPF with clsact is also obsolete. Given noone has run into this the last 10y, I don't think it really matters tbh.
Thanks,
Daniel
Jamal Hadi Salim
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Quoted 89 lines · Daniel Borkmann
Quoted 78 lines · Jamal Hadi Salim
Quoted 77 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)include/net/pkt_cls.h +8
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#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); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. Quoted 8 lines
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 172
Quoted 5 lines
include/net/pkt_cls.h +5
-0+*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}Why did you remove the warning?
A lesser issue is that you introduced a space above #endif
Quoted 4 lines
I don't think we need to put out an extra warn to spam the log, this can be easily traced either via bpftrace or qdisc stats etc; plus the guidance to eBPF with clsact is also obsolete. Given noone has run into this the last 10y, I don't think it really matters tbh.
It's a bit entitled for you to change someone else's patch to fit your philosophy without a mention.
It's only one message per qdisc. But fine, let's keep it that way. Sashiko is hallucinating on TC_ACT_CONSUMED for patch 1. Let's ignore it.
cheers,
jamal
Quoted 2 lines
Thanks,
Daniel
Paolo Abeni
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Hi,
Quoted 95 lines · Jamal Hadi Salim
Quoted 89 lines · Daniel Borkmann
Quoted 78 lines · Jamal Hadi Salim
Quoted 77 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)include/net/pkt_cls.h +8
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#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); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. Quoted 8 lines
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 172
Quoted 5 lines
include/net/pkt_cls.h +5
-0+*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}Why did you remove the warning?
A lesser issue is that you introduced a space above #endifQuoted 4 lines
I don't think we need to put out an extra warn to spam the log, this can be easily traced either via bpftrace or qdisc stats etc; plus the guidance to eBPF with clsact is also obsolete. Given noone has run into this the last 10y, I don't think it really matters tbh.
Quoted 4 lines
It's a bit entitled for you to change someone else's patch to fit your philosophy without a mention.
It's only one message per qdisc. But fine, let's keep it that way. Sashiko is hallucinating onTC_ACT_CONSUMEDfor patch 1. Let's ignore it.
@Jamal: I read the above as if you are ok with the series in the current form as a whole. Please LMK.
FTR, I have a slight preference towards the extra warn message in case of misconfiguration, but big deal either way.
/P
Jamal Hadi Salim
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Quoted 99 lines · Paolo Abeni
Hi,
Quoted 95 lines · Jamal Hadi Salim
Quoted 93 lines · Daniel Borkmann
Quoted 78 lines · Jamal Hadi Salim
Quoted 77 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>--- include/net/pkt_cls.h | 14 +++++++++++++- net/sched/cls_api.c | 4 +--- 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(+), 17 deletions(-)include/net/pkt_cls.h +8
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#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); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. Quoted 8 lines
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 172
Quoted 5 lines
include/net/pkt_cls.h +5
-0+*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}Why did you remove the warning?
A lesser issue is that you introduced a space above #endifI don't think we need to put out an extra warn to spam the log, this can be easily traced either via bpftrace or qdisc stats etc; plus the guidance to eBPF with clsact is also obsolete. Given noone has run into this the last 10y, I don't think it really matters tbh.
Quoted 8 lines
Quoted 4 lines
It's a bit entitled for you to change someone else's patch to fit your philosophy without a mention.
It's only one message per qdisc. But fine, let's keep it that way. Sashiko is hallucinating onTC_ACT_CONSUMEDfor patch 1. Let's ignore it.@Jamal: I read the above as if you are ok with the series in the current form as a whole. Please LMK.
I am fine with series.
cheers,
jamal
Quoted 5 lines
FTR, I have a slight preference towards the extra warn message in case of misconfiguration, but big deal either way.
/P
[email protected]
AI reviewRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
In reply to Daniel BorkmannThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Missing handling of TC_ACT_CONSUMED in qdisc filter chains leads to Use-After-Free. --
commit 84137b5068cebac45a2a901c08e1d2f5e6d71111
Author: Jamal Hadi Salim <[email protected]>
net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
When a TC filter attached to a qdisc filter chain returns TC_ACT_REDIRECT, the redirect was silently lost and the packet fell through to the switch. This patch adds a tcf_classify_qdisc wrapper to convert TC_ACT_REDIRECT to TC_ACT_SHOT, preventing the packet from silently continuing.
Quoted 3 lines
include/net/pkt_cls.h +0
-0
[ ... ]
Quoted 9 lines
include/net/pkt_cls.h +3
-1@@ -156,8 +156,20 @@ static inline int tcf_classify(struct sk_buff *skb, {return TC_ACT_UNSPEC ; } -#endif +static inline int tcf_classify_qdisc (struct sk_buff *skb, +const struct tcf_proto *tp, +struct tcf_result *res,bool compat_mode)
include/net/pkt_cls.h static inline int tcf_classify(struct sk_buff *skb, · line 172
Quoted 10 lines
include/net/pkt_cls.h +10
-0+{ +int ret =tcf_classify (skb,NULL , tp, res, compat_mode); + +/* TC_ACT_REDIRECT from qdisc filter chains is not supported. +* Use BPF via tcx or mirred redirect instead. +*/ +if (unlikely (ret ==TC_ACT_REDIRECT )) + ret =TC_ACT_SHOT ; +return ret; +}
[Severity: Critical]
This is a pre-existing issue, but missing handling of TC_ACT_CONSUMED in this helper can lead to a use-after-free.
The tcf_classify_qdisc() wrapper intentionally intercepts TC_ACT_REDIRECT to prevent it from falling through to the qdiscs. However, it fails to intercept TC_ACT_CONSUMED, which suffers from the exact same fall-through vulnerability.
If a user configures a qdisc filter with an action that returns TC_ACT_CONSUMED (such as act_ct queueing an IPv4 fragment for defrag), the action takes ownership of the skb.
Because TC_ACT_CONSUMED bypasses the switch statements in qdisc classify functions like cake_classify(), the qdisc treats it as a successful match and enqueues the already consumed skb into its internal queues.
Should TC_ACT_CONSUMED also be intercepted and handled here in tcf_classify_qdisc() to prevent this use-after-free scenario?
Quoted 3 lines
net/sched/sch_cake.c +0
-0
[ ... ]
net/sched/sch_cake.c static u32 cake_classify(struct Qdisc *sch, struct cake_tin_data **t, · line 1736
Quoted 9 lines
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
[Severity: Critical]
This is a pre-existing issue, but TC_ACT_CONSUMED bypasses the switch statement that immediately follows this result check.
When tcf_classify_qdisc() returns TC_ACT_CONSUMED, it satisfies the result >= 0 check but falls through the switch handling TC_ACT_STOLEN, TC_ACT_QUEUED, and TC_ACT_TRAP. The qdisc then treats the consumed packet as a successful match and enqueues it.
Could this be resolved by either adding TC_ACT_CONSUMED to the switch cases here and in other qdiscs, or by having tcf_classify_qdisc() intercept it alongside TC_ACT_REDIRECT?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Daniel Borkmann
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
Quoted 9 lines · On 7/1/26 2:33 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Missing handling ofTC_ACT_CONSUMEDin qdisc filter chains leads to Use-After-Free. --commit
84137b5068cebac45a2a901c08e1d2f5e6d71111
Author: Jamal Hadi Salim <[email protected]>
Quoted 6 lines
net/sched: Handle
TC_ACT_REDIRECTfrom qdisc filter chainsWhen a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT, the redirect was silently lost and the packet fell through to the switch. This patch adds atcf_classify_qdiscwrapper to convertTC_ACT_REDIRECTtoTC_ACT_SHOT, preventing the packet from silently continuing.
Looks like this needs a follow-up as its likely not just a simple || addition to handle this case in tc.
Paolo Abeni
maintainerRe: [PATCH net 2/3] net/sched: Handle TC_ACT_REDIRECT from qdisc filter chains
In reply to Daniel BorkmannQuoted 36 lines · Daniel Borkmann
From: Jamal Hadi Salim <[email protected]>
When a TC filter attached to a qdisc filter chain returns
TC_ACT_REDIRECT(ex: via an eBPF program callingbpf_redirect()or anact_bpfaction), the redirect was silently lost i.e no qdisc classify function handledTC_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 commit27b29f63058d("bpf: addbpf_redirect()helper"). We got lucky for a long time becausebpf_net_contextwas a per-CPU variable that was always available.commit
401cb7dae813("net: Referencebpf_redirect_infoviatask_structonPREEMPT_RT.") turnedbpf_net_contextinto atask_structmember that is only set up by explicit callers. Without a caller setting it up,bpf_redirect()itself crashes with a NULL pointer dereference inbpf_net_ctx_get_ri(). However, even withbpf_net_contextavailable,TC_ACT_REDIRECTfrom qdisc filter chains cannot be honored without addingskb_do_redirect()calls to every qdisc classify function, which would require changes acrossnet/sched/. Isolate it to ebpf core where it belongs.Instead, add a
tcf_classify_qdisc()inline helper inpkt_cls.h, as a wrapper aroundtcf_classify()for use by qdisc classify functions andtcf_qevent_handle(). When the classify verdict isTC_ACT_REDIRECT, the wrapper converts it toTC_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. 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 calltcf_classify()directly and is unaffected:TC_ACT_REDIRECTis returned as-is and handled by sch_handle_egress/ingress() callingskb_do_redirect()as before.
Quoted 5 lines
Fixes 27b29f63058d ("bpf: add bpf_redirect() helper")Fixes 401cb7dae813 ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.")Tested-by Victor Nogueira <[email protected]>Signed-off-by Jamal Hadi Salim <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
I'm sorry, this does not apply cleanly anymore.
Could you please rebase and repost?
Thanks!
Paolo
Daniel Borkmann
maintainerPATCH 3/3 selftests/bpf: Add test for redirect from qdisc qevent block
In reply to Daniel BorkmannAdd a regression test for the NULL current->bpf_net_context deref hit when a BPF classifier attached to a qdisc qevent block asks for a redirect. The classifier runs from tcf_qevent_handle() on the qdisc enqueue path, outside any bpf_net_context.
# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t qevent [...]
+ /etc/rcS.d/S50-startup
./test_progs -t qevent
#496/1 tc_qevent/redirect_verdict:OK #496/2 tc_qevent/redirect_helper:OK #496 tc_qevent:OK
Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/tc_qevent.c | 113 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_qevent.c | 23 ++++ 3 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_qevent.c create mode 100644 tools/testing/selftests/bpf/progs/test_tc_qevent.c
tools/testing/selftests/bpf/config +1 -0
@@ -82,6 +82,7 @@ CONFIG_NET_SCH_BPF=y CONFIG_NET_SCH_FQ=y CONFIG_NET_SCH_INGRESS=y CONFIG_NET_SCH_HTB=y +CONFIG_NET_SCH_RED=y CONFIG_NET_SCHED=y CONFIG_NETDEVSIM=y CONFIG_NETFILTER=y
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +113 -0
113 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_tc_qevent.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +int redirect_ifindex =1 ; +__u64 verdict_calls =0 ; +__u64 helper_calls =0 ; + +SEC("tc") +int qevent_redirect_verdict (struct __sk_buff *skb) +{ +__sync_fetch_and_add (&verdict_calls,1 ); +return TCX_REDIRECT ; +} + +SEC("tc") +int qevent_redirect_helper (struct __sk_buff *skb) +{ +__sync_fetch_and_add (&helper_calls,1 ); +return bpf_redirect (redirect_ifindex,0 ); +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net 3/3] selftests/bpf: Add test for redirect from qdisc qevent block
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The test is flaky due to a race condition with asynchronous ARP resolution. - [Low] Semantically…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The test is flaky due to a race condition with asynchronous ARP resolution.
- [Low] Semantically incorrect use of the tcx-specific constant TCX_REDIRECT in a legacy cls_bpf program. --
commit fc8f69a4140a0efbcb7d3933eb46154533713ffc
Author: Daniel Borkmann <[email protected]>
selftests/bpf: Add test for redirect from qdisc qevent block
This commit adds a regression test for a NULL current->bpf_net_context dereference. It verifies the behavior when a BPF classifier attached to a qdisc qevent block asks for a redirect.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +0
-0@@ -0,0 +1,113 @@
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +2
-0+static void blast_udp (void ) +{
[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +10
-0+/* +* Push far more than the RED queue can hold. Once qavg crosses qth_min +* every further packet hits the congestion_drop / early_drop qevent. +*/ +for (i =0 ; i <50000 ; i++) +sendto (fd, buf,sizeof (buf),MSG_DONTWAIT , + (struct sockaddr *)&dst,sizeof (dst)); + +close (fd); +}
[ ... ]
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +2
-0+static void run_qevent_redirect (struct bpf_program *prog,__u64 *counter) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/tc_qevent.c line 86
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/tc_qevent.c new file +8
-0+if (system ("tc filter add block 10 bpf da object-pinned " +PIN_PATH " 2>/dev/null" )) { +test__skip (); + goto close_ns; + } + +blast_udp (); +ASSERT_GT (*counter,0 ,"qevent classifier ran" );
[Severity: Medium]
Since static ARP entries are not configured, could this lead to a flaky test due to a race condition with asynchronous ARP resolution?
When blast_udp() loops through sendto(), the first packets might trigger an ARP request while the rest get dropped or queued by the neighbor layer before reaching the RED qdisc.
Because ASSERT_GT() is checked immediately after blast_udp(), is it possible the check evaluates before the network stack finishes processing the ARP reply, leaving the qdisc with no packets and causing the test to fail?
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_tc_qevent.c new file +0
-0@@ -0,0 +1,23 @@
[ ... ]
tools/testing/selftests/bpf/progs/test_tc_qevent.c line 14
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_tc_qevent.c new file +6
-0+SEC("tc") +int qevent_redirect_verdict (struct __sk_buff *skb) +{ +__sync_fetch_and_add (&verdict_calls,1 ); +return TCX_REDIRECT ; +}
[Severity: Low]
Is it correct to return the TCX_REDIRECT constant here?
Since this program is attached using the legacy cls_bpf via tc filter add bpf da in run_qevent_redirect(), shouldn't it return standard TC_ACT_* return codes like TC_ACT_REDIRECT?
They both evaluate to 7 so it functions correctly, but mixing the newer TCX framework constants in a legacy program might cause API confusion.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Daniel Borkmann
maintainerRe: [PATCH net 3/3] selftests/bpf: Add test for redirect from qdisc qevent block
Quoted 2 lines · On 7/1/26 2:33 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The test is flaky due to a race condition with asynchronous ARP resolution.
This is a false-positive; the only way counter == 0 is if ARP never resolves across all sends which is extremely unlikely. If it ever becomes an issue, we can add a static entry.
Quoted 1 line
- [Low] Semantically incorrect use of the tcx-specific constant
TCX_REDIRECTin a legacycls_bpfprogram.
Intentional since this is defined via vmlinux header and they're both the same return value.
Quoted 8 lines · Daniel Borkmann
This is an alternative fix to [0] in order to not uglify
__dev_queue_xmit()with sprinkled ifdefs given this can be simplified and isolated through a simple test into the BPF redirect helper itself.I've also added a proper BPF selftest, so there is no need to check-in a binary BPF object into selftests given we do have BPF infra for all of this.
1/3 makes sense. Assuming we wouldn't have this per-task memory assignment, wouldn't then the state from one redirect leak into another?
For what it's worth:
Sebastian
Daniel Borkmann
maintainerQuoted 6 lines · Sebastian Andrzej Siewior
Quoted 5 lines · Daniel Borkmann
This is an alternative fix to [0] in order to not uglify
__dev_queue_xmit()with sprinkled ifdefs given this can be simplified and isolated through a simple test into the BPF redirect helper itself.
Quoted 6 lines
Quoted 3 lines
I've also added a proper BPF selftest, so there is no need to check-in a binary BPF object into selftests given we do have BPF infra for all of this.
1/3 makes sense. Assuming we wouldn't have this per-task memory assignment, wouldn't then the state from one redirect leak into another?
For the normal/functional case out of tcx / cls_bpf via clsact not given skb_do_redirect() is called right after return. (For the full qdisc case it was never working in the first place.)
Quoted 4 lines
For what it's worth:
Reviewed-by Sebastian Andrzej Siewior <[email protected]>Sebastian