Re: [PATCH net 1/3] net: Extend bpf_net_context lifetime to cover qdisc enqueue
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date: 2026-06-29 10:29:20
Also in:
bpf
On 2026-06-26 12:51:54 [-0400], Jamal Hadi Salim wrote:
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. And as a matter of fact the same assumption is made in the code outside of tc. 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. The setup is placed outside the egress_needed_key static branch because qevents are independent of clsact/NF egress hooks and that key may stay disabled when only a qevent-bearing qdisc is configured. Unfortunately this adds a small unconditional penalty to the code path _per packet_ only guarded by CONFIG_NET_XGRESS (two writes and one read for bpf_net_ctx_set, plus one write for bpf_net_ctx_clear).
I fail to understand this but you and sashiko have an understanding... If there is TC_ACT_REDIRECT returned by tc_run(), then the skb is NULL and as such uppon return from sch_handle_egress() the control flow goes to the out label. As a fix you move the bpf_net_ctx assigned to before CONFIG_NET_EGRESS and clear it on exit. What do I miss here?
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 (see the accompanying tdc test):
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 qdisc add dev eth0 clsact
tc filter add block 10 pref 1 bpf obj redirect.ostupid question: how do I get this redirect.o? Just a simply thing to reproduce this…
tc filter add dev eth0 egress protocol ip prio 1 matchall \
action gact pass
traffic through eth0 triggers red_enqueue() -> tcf_qevent_handle() and,
on a redirect verdict, a NULL deref in skb_do_redirect().Sebastian