Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net/sched: sch_frag: guard against same-CPU re-entrancy
@ 2026-11-28 10:33 Jamal Hadi Salim
  0 siblings, 0 replies; only message in thread
From: Jamal Hadi Salim @ 2026-11-28 10:33 UTC (permalink / raw)
  To: netdev
  Cc: Jamal Hadi Salim, Jiri Pirko, David S . Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
	linux-rt-devel, wenxu, stable, Victor Nogueira, Sashiko

This is a followup of commit 975b5b067f52 ("ipv6: sr: restore network
header before routing and forwarding") as reported by Sashiko. That change
fixed the negative L2 length that could reach sch_fragment(); this patch
addresses a separate pre-existing bug in the same fragment path.

The issue:
sch_fragment() holds the per-CPU local_lock_nested_bh across
ip_do_fragment()/ip6_fragment(). Their sch_frag_xmit() callback calls
data->xmit(skb), which may re-enter TC egress (cascaded act_mirred
redirect to a qdisc that calls sch_frag_xmit_hook() again on the same
CPU). Re-entering re-acquires the per-CPU bh_lock:

- on PREEMPT_RT this is a real spin_lock, so the same-CPU recursion
  self-deadlocks;
- on non-RT the inner call overwrites the shared per-CPU sch_frag_data
  (dst, cb, xmit, l2_data), so when the outer fragment loop resumes it
  uses the inner call's stale state.

With the lockdep detector built in, the recursion reports itself as:

 ============================================
 WARNING: possible recursive locking detected
 7.3.0-rc7+ #44 Not tainted
 --------------------------------------------
 python3/20084 is trying to acquire lock:
 ffff8e80bd7b44d0 (bh_lock#2){+.-.}-{3:3}, at: sch_fragment+0x288/0x560

                but task is already holding lock:
 ffff8e80bd7b44d0 (bh_lock#2){+.-.}-{3:3}, at: sch_fragment+0x288/0x560

  Possible unsafe locking scenario:

        CPU0
        ----
   lock(bh_lock#2);
   lock(bh_lock#2);

                 *** DEADLOCK ***

 Call Trace:
  <IRQ>
  sch_fragment+0x291/0x560
  sch_frag_xmit_hook+0x74/0x90
  tcf_dev_queue_xmit+0x1f/0x30
  tcf_mirred_to_dev+0x2da/0x560
  ...
  sch_frag_xmit+0x169/0x260
  ip6_fragment+0x8dc/0xa70
  sch_fragment+0x33f/0x560
  sch_frag_xmit_hook+0x74/0x90

The Fix:
Prevent same-CPU re-entrancy by recording which task owns the per-CPU
fragment state. A deeper call must not re-use the outer call's per-CPU
state, and it must not transmit the skb: sch_frag_xmit_hook() has
already determined that the skb exceeds its recorded fragment MRU. Drop
it and return an error instead.

Owner identity, not nesting depth: on PREEMPT_RT a second task that
reaches sch_fragment() on the same CPU waits for the real spin_lock and
then works on its own state, so only the task that already owns the
state is the recursive one. A depth counter would refuse that contender
too, and reporting -ELOOP for it would be wrong.

Conditions to recreate the bug:
  # Minimum privilege: CAP_NET_ADMIN in a user-owned network namespace,
  #   e.g. via `unshare -Urn`; nothing outside that namespace is required.
  # Build with CONFIG_DEBUG_LOCK_ALLOC=y for the recursive-lock splat;
  # on PREEMPT_RT the same recursion is a hard self-deadlock instead.
  ip link add vetha type veth peer name vethb
  ip link add vethc type veth peer name vethd
  ip link add vethe type veth peer name vethf
  for i in a b c d e f; do ip link set veth$i up; done

  # outer: reassemble the fragments on ingress, then redirect to vethc
  tc qdisc add dev vetha clsact
  tc filter add dev vetha ingress matchall action ct zone 1 \
      action mirred egress redirect dev vethc

  # inner: the refragmented egress re-enters sch_fragment() on the same
  # CPU while the outer call still holds the per-CPU lock
  tc qdisc add dev vethc clsact
  tc filter add dev vethc egress matchall action ct zone 2 \
      action mirred egress redirect dev vethe

  # then send a multi-fragment IPv6/UDP datagram out vethb (the zone-1
  #   ct reassembly sets cb.mru so the redirect enters sch_fragment()).

Fixes: 20d677d389e7 ("net/sched: Use nested-BH locking for sch_frag_data_storage")
Reported-by: Sashiko (gemini) <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260827092345.2301937-1-fouriezhang@tencent.com
Link: https://lore.kernel.org/netdev/20260827092345.2301937-1-fouriezhang@tencent.com/
Cc: stable@vger.kernel.org
Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
---
 net/sched/sch_frag.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/net/sched/sch_frag.c b/net/sched/sch_frag.c
index 75ee52750919..980f1490b027 100644
--- a/net/sched/sch_frag.c
+++ b/net/sched/sch_frag.c
@@ -17,6 +17,7 @@ struct sch_frag_data {
 	unsigned int l2_len;
 	u8 l2_data[VLAN_ETH_HLEN];
 	int (*xmit)(struct sk_buff *skb);
+	struct task_struct *owner;
 	local_lock_t bh_lock;
 };
 
@@ -98,10 +99,19 @@ static int sch_fragment(struct net *net, struct sk_buff *skb,
 
 	if (skb_protocol(skb, true) == htons(ETH_P_IP)) {
 		struct rtable sch_frag_rt = { 0 };
+		struct sch_frag_data *data;
 		unsigned long orig_dst;
 
+		data = this_cpu_ptr(&sch_frag_data_storage);
+		if (READ_ONCE(data->owner) == current) {
+			kfree_skb(skb);
+			return -ELOOP;
+		}
+
 		local_lock_nested_bh(&sch_frag_data_storage.bh_lock);
+		data = this_cpu_ptr(&sch_frag_data_storage);
 		sch_frag_prepare_frag(skb, xmit);
+		WRITE_ONCE(data->owner, current);
 		dst_init(&sch_frag_rt.dst, &sch_frag_dst_ops, NULL,
 			 DST_OBSOLETE_NONE, DST_NOCOUNT);
 		sch_frag_rt.dst.dev = skb->dev;
@@ -111,14 +121,24 @@ static int sch_fragment(struct net *net, struct sk_buff *skb,
 		IPCB(skb)->frag_max_size = mru;
 
 		ret = ip_do_fragment(net, skb->sk, skb, sch_frag_xmit);
+		WRITE_ONCE(data->owner, NULL);
 		local_unlock_nested_bh(&sch_frag_data_storage.bh_lock);
 		refdst_drop(orig_dst);
 	} else if (skb_protocol(skb, true) == htons(ETH_P_IPV6)) {
-		unsigned long orig_dst;
 		struct rt6_info sch_frag_rt;
+		struct sch_frag_data *data;
+		unsigned long orig_dst;
+
+		data = this_cpu_ptr(&sch_frag_data_storage);
+		if (READ_ONCE(data->owner) == current) {
+			kfree_skb(skb);
+			return -ELOOP;
+		}
 
 		local_lock_nested_bh(&sch_frag_data_storage.bh_lock);
+		data = this_cpu_ptr(&sch_frag_data_storage);
 		sch_frag_prepare_frag(skb, xmit);
+		WRITE_ONCE(data->owner, current);
 		memset(&sch_frag_rt, 0, sizeof(sch_frag_rt));
 		dst_init(&sch_frag_rt.dst, &sch_frag_dst_ops, NULL,
 			 DST_OBSOLETE_NONE, DST_NOCOUNT);
@@ -129,6 +149,7 @@ static int sch_fragment(struct net *net, struct sk_buff *skb,
 		IP6CB(skb)->frag_max_size = mru;
 
 		ret = ip6_fragment(net, skb->sk, skb, sch_frag_xmit);
+		WRITE_ONCE(data->owner, NULL);
 		local_unlock_nested_bh(&sch_frag_data_storage.bh_lock);
 		refdst_drop(orig_dst);
 	} else {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-28 10:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 10:33 [PATCH net] net/sched: sch_frag: guard against same-CPU re-entrancy Jamal Hadi Salim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox