Re: [PATCH net 1/3] net: Extend bpf_net_context lifetime to cover qdisc enqueue
From: Jamal Hadi Salim <jhs@mojatatu.com>
Date: 2026-06-29 10:47:25
Also in:
bpf
- On Mon, Jun 29, 2026 at 6:29 AM Sebastian Andrzej Siewior [off-list ref] wrote:
On 2026-06-26 12:51:54 [-0400], Jamal Hadi Salim wrote:quoted
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?
There are 2 separate filters. IIUC, you are thinking of the first one which is the clsact egress classifier (which runs in sch_handle_egress()) - its redirect would indeed return NULL and skip qdisc enqueue. The second one is the qevent redirect whch happens in tcf_qevent_handle() during qdisc enqueue (block 10 in the reproducer).
quoted
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…
It's just pseudo code for a bpf prog that redirects (so you can create probably a few liner bpf prog). Take a look at patch 3 which uses a prebuilt action-ebpf binary with the action-redirect section (added by patch 3 to action.c). If it's still not clear, I can craft one and send it to you. cheers, jamal
quoted
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