Netdev List
 help / color / mirror / Atom feed
From: Jamal Hadi Salim <jhs@mojatatu.com>
To: netdev@vger.kernel.org
Cc: Jamal Hadi Salim <jhs@mojatatu.com>,
	Jiri Pirko <jiri@resnulli.us>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-rt-devel@lists.linux.dev, wenxu <wenxu@ucloud.cn>,
	stable@vger.kernel.org, Victor Nogueira <victor@mojatatu.com>,
	Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH net] net/sched: sch_frag: guard against same-CPU re-entrancy
Date: Sat, 28 Nov 2026 05:33:33 -0500	[thread overview]
Message-ID: <QDISC-R0QC.v1.r0.20261125082538-1@mojatatu.com> (raw)

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


                 reply	other threads:[~2026-11-28 10:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=QDISC-R0QC.v1.r0.20261125082538-1@mojatatu.com \
    --to=jhs@mojatatu.com \
    --cc=bigeasy@linutronix.de \
    --cc=clrkwllms@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=victor@mojatatu.com \
    --cc=wenxu@ucloud.cn \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox