Netdev List
 help / color / mirror / Atom feed
From: SeungJong Ha <engineer.jjhama@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	Breno Leitao <leitao@debian.org>
Cc: netdev@vger.kernel.org, SeungJong Ha <engineer.jjhama@gmail.com>
Subject: [PATCH net] netdevsim: fix use-after-free of skb in nsim_napi_rx()
Date: Sun, 06 Dec 2026 20:40:11 +0900	[thread overview]
Message-ID: <20261206-b4-netdevsim-rx-uaf-v1-1-d314e4a9b303@gmail.com> (raw)

nsim_napi_rx() reads the skb's queue_mapping after skb_queue_tail()
has queued it on the peer's RX queue, where the peer's NAPI poll may
already have dequeued and freed it on another CPU.

With AF_PACKET senders on one device, threaded NAPI on its peer and a
2 ms udelay() after skb_queue_tail() to widen the window, KASAN
reports it within seconds:

  BUG: KASAN: slab-use-after-free in nsim_start_xmit+0x693/0x6c0
  Read of size 2 at addr ff1100010124a57c by task python3/200
  Call Trace:
   nsim_start_xmit+0x693/0x6c0
   __dev_direct_xmit+0x2d1/0x420
   packet_xmit+0x195/0x2c0
  Freed by task 190:
   kmem_cache_free+0xf8/0x390
   __netif_receive_skb_core+0x2155/0x2e30
   ...
   gro_receive_skb+0x4f2/0xb10
   nsim_poll+0xe2/0x420

Read queue_mapping before queueing the skb.

Found with a prototype static analysis tool I am developing. The
analysis, the reproducer and the fix were written with Claude Code
(Claude Opus 5.5).

Fixes: ff2ac4df58ad ("netdevsim: implement peer queue flow control")
Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
I found this with a prototype static analysis tool I am developing. It
checks drivers against ownership contracts of the networking core, and
flagged the skb read after skb_queue_tail() hands the skb to NAPI.

The analysis, the reproducer and the fix were written with Claude Code
(Claude Opus 5.5). I reviewed every line of the patch and the commit
message, and re-ran the reproducer myself.

Tested in a QEMU guest with KASAN on net (6902a2bb708c). With a 2 ms
udelay() added before the read to widen the window, the report shows
within 10 s without this patch, and not once in 600 s with it. Without
the delay it hit once in two 30-minute runs on v7.3. The netdevsim
peer.sh selftest passes with this patch.
---
 drivers/net/netdevsim/netdev.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index eebc02ccc4a9..b68b108a1fc7 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -85,6 +85,8 @@ static void nsim_stop_tx_queue(struct net_device *tx_dev,
 static int nsim_napi_rx(struct net_device *tx_dev, struct net_device *rx_dev,
 			struct nsim_rq *rq, struct sk_buff *skb)
 {
+	u16 idx = skb_get_queue_mapping(skb);
+
 	if (skb_queue_len(&rq->skb_queue) > NSIM_RING_SIZE) {
 		dev_kfree_skb_any(skb);
 		return NET_RX_DROP;
@@ -94,8 +96,7 @@ static int nsim_napi_rx(struct net_device *tx_dev, struct net_device *rx_dev,
 
 	/* Stop the peer TX queue avoiding dropping packets later */
 	if (skb_queue_len(&rq->skb_queue) >= NSIM_RING_SIZE)
-		nsim_stop_tx_queue(tx_dev, rx_dev, rq,
-				   skb_get_queue_mapping(skb));
+		nsim_stop_tx_queue(tx_dev, rx_dev, rq, idx);
 
 	return NET_RX_SUCCESS;
 }

---
base-commit: 6902a2bb708c4075d3de6ec18ab6a5864c4c1aa2
change-id: 20261206-b4-netdevsim-rx-uaf-5b5c8829282f

Best regards,
--  
SeungJong Ha <engineer.jjhama@gmail.com>


                 reply	other threads:[~2026-12-06 11:40 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=20261206-b4-netdevsim-rx-uaf-v1-1-d314e4a9b303@gmail.com \
    --to=engineer.jjhama@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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