Skip to contentSkip to conversation

netdevsim: nsim_start_peer_tx_queue() indexes the peer's TX queues with an RX queue index

BUG Kyuwon Cho 3 participants 3 messages Nov 19 – Nov 23

Subsystems (3)
lore
Share & tools
lore

Kyuwon Cho

author
#0 lore raw

Hi Jakub,

While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that netdevsim can read past the peer's TX queue array when it wakes the peer's queue from NAPI poll:

  BUG: KASAN: slab-out-of-bounds in nsim_poll (include/linux/netdevice.h:3834 drivers/net/netdevsim/netdev.c:60 drivers/net/netdevsim/netdev.c:409 drivers/net/netdevsim/netdev.c:418)
  Read of size 8 at addr ff110000140a0fd0 by task test/118
  Call Trace:
   <IRQ>
   nsim_poll (include/linux/netdevice.h:3834 drivers/net/netdevsim/netdev.c:60 drivers/net/netdevsim/netdev.c:409 drivers/net/netdevsim/netdev.c:418)
   __napi_poll.constprop.0 (net/core/dev.c:7997)
   net_rx_action (net/core/dev.c:8060 net/core/dev.c:8217)
   handle_softirqs (kernel/softirq.c:669)
   __irq_exit_rcu (kernel/softirq.c:703 kernel/softirq.c:542 kernel/softirq.c:786)
   irq_exit_rcu (kernel/softirq.c:809)
   sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1062)
   </IRQ>

nsim_start_peer_tx_queue() uses the index of the RX queue being polled as an index into the peer's TX queues:

	idx = rq->napi.index;
	...
	peer_dev = peer_ns->netdev;
	if (dev->real_num_tx_queues != peer_dev->num_rx_queues)
		goto out;

	txq = netdev_get_tx_queue(peer_dev, idx);

The guard compares this device's TX queue count with the peer's RX queue count, not idx with the peer's TX queue count. The peer is read again at poll time, so an RX queue filled by one peer can be polled after the device was linked to a peer with fewer TX queues.

The reproducer runs as root and uses only sysfs, ethtool and packet
sockets: netdevsim devices X and R with 8 queues, P with 1 queue, R reduced to 1 combined channel (so R's real_num_tx_queues equals P's num_rx_queues and the guard passes), a packet socket flooding X with many flows so that R's RX queues 0..7 get traffic, and a thread that keeps relinking R between X and P. On an x86_64 QEMU guest with 2 vCPUs and KASAN (gcc 13.3), KASAN reports the read in our runs.

The code comes from ff2ac4df58ad ("netdevsim: implement peer queue flow control"). Checked against v7.4-rc3 (aebb72744764); nsim_start_peer_tx_queue() is the same in net and net-next.

A patch is attached (git format-patch against v7.4-rc3; it applies to net and net-next):

0001-netdevsim-bound-the-peer-TX-queue-index-in-nsim_star.patch

It skips the wake-up when the index is not a TX queue of the peer. It was written with LLM assistance; we reviewed and tested it. With it, the reproducer no longer triggers the report.

The reproducer (repro.c), the kernel config and the patch are attached.

Thanks,
Kyuwon Cho

// Reproducer for the suspected sibling of 08f566e8f83b in netdevsim:
// nsim_start_peer_tx_queue() calls netdev_get_tx_queue(peer, rq->napi.index) // with no check against peer->num_tx_queues.
//
// Setup (all netdevsim, init netns, syscalls/sysfs/ioctl only):

//   X, R: bus device 10, 2 ports, 8 queues each (num_tx = num_rx = 8)
//   P:    bus device 11, 1 port,  1 queue       (num_tx = num_rx = 1)
//   R: ethtool -L combined 1  -> R->real_num_tx = 1 (== P->num_rx, passes the guard)

// Sender: X floods frames with many flows, so skb tx mapping m spreads over 0..7.
// nsim_forward_skb() puts them on R's rq[m % R->num_rx] = rq[m].
// Toggler: link/unlink R between X and P. The rq poll (armed 5us after a forward)
// reads R->peer at poll time. If the poll sees P, idx = rq->napi.index
// (1..7) is used as an index into P->_tx[], which has only one entry.
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/ethtool.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>

#define RUN_SECS 60

static int nsfd, ctl;
static unsigned int ifX, ifR, ifP;
static atomic_int stop;
static atomic_long sends, toggles, wr_ok, wr_fail;

static int wr(const char *path, const char *s)

{
	int fd = open(path, O_WRONLY);
	int r;

	if (fd < 0)
		return -1;
	r = write(fd, s, strlen(s)) < 0 ? -1 : 0;
	close(fd);
	return r;
}

static int list_netdevs(int busid, char names[][IFNAMSIZ], int want)

{
	char path[128];
	struct dirent *de;
	int n = 0;
	DIR *d;

	snprintf(path, sizeof(path), "/sys/bus/netdevsim/devices/netdevsim%d/net", busid);
	d = opendir(path);
	if (!d) {
		perror(path);
		return 0;
	}
	while ((de = readdir(d)) && n < want) {
		if (de->d_name[0] == '.')
			continue;
		snprintf(names[n], IFNAMSIZ, "%s", de->d_name);
		n++;
	}
	closedir(d);
	return n;
}

static void set_up(const char *name)

{
	struct ifreq ifr;

	memset(&ifr, 0, sizeof(ifr));
	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
	if (ioctl(ctl, SIOCGIFFLAGS, &ifr) < 0)
		perror("SIOCGIFFLAGS");
	ifr.ifr_flags |= IFF_UP;
	if (ioctl(ctl, SIOCSIFFLAGS, &ifr) < 0)
		perror("SIOCSIFFLAGS");
}

static void set_channels(const char *name, unsigned int n)

{
	struct ethtool_channels ch;
	struct ifreq ifr;

	memset(&ch, 0, sizeof(ch));
	memset(&ifr, 0, sizeof(ifr));
	ch.cmd = ETHTOOL_SCHANNELS;
	ch.combined_count = n;
	snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
	ifr.ifr_data = (void *)&ch;
	if (ioctl(ctl, SIOCETHTOOL, &ifr) < 0)
		perror("SCHANNELS");
	else
		printf("set %s combined=%u\n", name, n);
}

static uint32_t rng_state = 0x9e3779b9;
static uint32_t rnd(void)

{
	rng_state ^= rng_state << 13;
	rng_state ^= rng_state >> 17;
	rng_state ^= rng_state << 5;
	return rng_state;
}

static void *sender(void *arg)

{
	unsigned char dst[ETH_ALEN] = { 0x02, 0, 0, 0, 0, 0x02 };
	unsigned char src[ETH_ALEN] = { 0x02, 0, 0, 0, 0, 0x01 };
	struct sockaddr_ll sll;
	unsigned char f[60];
	int fd;

	(void)arg;
	fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
	if (fd < 0) {
		perror("socket(AF_PACKET)");
		return NULL;
	}
	memset(&sll, 0, sizeof(sll));
	sll.sll_family = AF_PACKET;
	sll.sll_protocol = htons(ETH_P_IP);
	sll.sll_ifindex = ifX;
	sll.sll_halen = ETH_ALEN;
	memcpy(sll.sll_addr, dst, ETH_ALEN);

	while (!atomic_load(&stop)) {
		for (int b = 0; b < 32; b++) {
			struct iphdr *ip = (struct iphdr *)(f + 14);
			struct udphdr *u = (struct udphdr *)(f + 34);

			memset(f, 0, sizeof(f));
			memcpy(f, dst, ETH_ALEN);
			memcpy(f + 6, src, ETH_ALEN);
			f[12] = 0x08;
			f[13] = 0x00;
			ip->version = 4;
			ip->ihl = 5;
			ip->tot_len = htons(46);
			ip->ttl = 64;
			ip->protocol = IPPROTO_UDP;
			ip->saddr = htonl(0x0a000001);
			ip->daddr = htonl(0x0a000002);
			u->source = htons(1024 + (rnd() % 60000));
			u->dest = htons(9);
			u->len = htons(26);
			sendto(fd, f, sizeof(f), 0, (struct sockaddr *)&sll, sizeof(sll));
			atomic_fetch_add(&sends, 1);
		}
	}
	close(fd);
	return NULL;
}

static void *toggler(void *arg)

{
	char unlink_r[64], link_pr[64], link_xr[64];
	int ok;

	(void)arg;
	snprintf(unlink_r, sizeof(unlink_r), "%d:%u", nsfd, ifR);
	snprintf(link_pr, sizeof(link_pr), "%d:%u %d:%u", nsfd, ifP, nsfd, ifR);
	snprintf(link_xr, sizeof(link_xr), "%d:%u %d:%u", nsfd, ifX, nsfd, ifR);

	while (!atomic_load(&stop)) {
		wr("/sys/bus/netdevsim/unlink_device", unlink_r);
		ok = wr("/sys/bus/netdevsim/link_device", link_pr);
		atomic_fetch_add(ok == 0 ? &wr_ok : &wr_fail, 1);
		wr("/sys/bus/netdevsim/unlink_device", unlink_r);
		ok = wr("/sys/bus/netdevsim/link_device", link_xr);
		atomic_fetch_add(ok == 0 ? &wr_ok : &wr_fail, 1);
		atomic_fetch_add(&toggles, 1);
	}
	return NULL;
}

int main(void)

{
	char x[2][IFNAMSIZ], p[1][IFNAMSIZ];
	char r_name[IFNAMSIZ], x_name[IFNAMSIZ], p_name[IFNAMSIZ];
	pthread_t ts, tc;
	int n;

	setvbuf(stdout, NULL, _IONBF, 0);
	mount("sysfs", "/sys", "sysfs", 0, NULL);
	ctl = socket(AF_INET, SOCK_DGRAM, 0);
	nsfd = open("/proc/self/ns/net", O_RDONLY);

	if (wr("/sys/bus/netdevsim/new_device", "10 2 8") < 0)
		perror("new_device 10");
	if (wr("/sys/bus/netdevsim/new_device", "11 1 1") < 0)
		perror("new_device 11");

	n = list_netdevs(10, x, 2);
	if (n < 2) {
		printf("need 2 netdevs on bus 10, got %d\n", n);
		return 1;
	}
	if (list_netdevs(11, p, 1) < 1) {
		printf("need 1 netdev on bus 11\n");
		return 1;
	}
	snprintf(x_name, sizeof(x_name), "%s", x[0]);
	snprintf(r_name, sizeof(r_name), "%s", x[1]);
	snprintf(p_name, sizeof(p_name), "%s", p[0]);
	ifX = if_nametoindex(x_name);
	ifR = if_nametoindex(r_name);
	ifP = if_nametoindex(p_name);
	printf("X=%s(%u) R=%s(%u) P=%s(%u)\n", x_name, ifX, r_name, ifR, p_name, ifP);

	set_up(x_name);
	set_up(r_name);
	set_up(p_name);
	set_channels(r_name, 1);

	{
		char link_xr[64];

		snprintf(link_xr, sizeof(link_xr), "%d:%u %d:%u", nsfd, ifX, nsfd, ifR);
		if (wr("/sys/bus/netdevsim/link_device", link_xr) < 0)
			perror("link X-R");
		else
			printf("linked X-R\n");
	}

	pthread_create(&ts, NULL, sender, NULL);
	pthread_create(&tc, NULL, toggler, NULL);
	sleep(RUN_SECS);
	atomic_store(&stop, 1);
	pthread_join(ts, NULL);
	pthread_join(tc, NULL);
	printf("done: sends=%ld toggles=%ld link_ok=%ld link_fail=%ld\n",
	       atomic_load(&sends), atomic_load(&toggles),
	       atomic_load(&wr_ok), atomic_load(&wr_fail));
	return 0;
}
Quoted 1 line

From 0a9629d8c7c93ef16dd000ab4cf04e9792044987 Mon Sep 17 00:00:00 2001

From: Kyuwon Cho <[email protected]>
Date: Thu, 19 Nov 2026 11:36:52 +0900
Subject: [PATCH net] netdevsim: bound the peer TX queue index in
nsim_start_peer_tx_queue()

nsim_start_peer_tx_queue() wakes the peer's TX queue with the index of the RX queue being polled, and only compares this device's TX queue count with the peer's RX queue count. When the device is relinked to a peer with fewer TX queues while its RX queues still hold traffic from the old peer, the index is past the peer's TX queue array:

  BUG: KASAN: slab-out-of-bounds in nsim_poll (include/linux/netdevice.h:3834 drivers/net/netdevsim/netdev.c:60)
  Read of size 8
  Call Trace:
   <IRQ>
   nsim_poll (drivers/net/netdevsim/netdev.c:409)
   __napi_poll.constprop.0 (net/core/dev.c:7997)
   net_rx_action (net/core/dev.c:8217)

Skip the wake-up when the index is not a TX queue of the peer.

Fixes ff2ac4df58ad ("netdevsim: implement peer queue flow control")
---
 drivers/net/netdevsim/netdev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/netdevsim/netdev.c +2 -1
index eebc02ccc4a9..6e287ae692a8 100644
@@ -53,7 +53,8 @@ static void nsim_start_peer_tx_queue(struct net_device *dev, struct nsim_rq *rq)
 
 	/* TX device */
 	peer_dev = peer_ns->netdev;
-	if (dev->real_num_tx_queues != peer_dev->num_rx_queues)
+	if (dev->real_num_tx_queues != peer_dev->num_rx_queues ||
+	    idx >= peer_dev->real_num_tx_queues)
 		goto out;
 
 	txq = netdev_get_tx_queue(peer_dev, idx);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 4 lines · On Wed, 18 Nov 2026 19:05:00 -0800 (PST) Kyuwon Cho wrote:

A patch is attached (git format-patch against v7.4-rc3; it applies to net and net-next):

0001-netdevsim-bound-the-peer-TX-queue-index-in-nsim_star.patch

This is not how patches are submitted upstream. You can put the explanation slop in the cover letter.

#2 lore raw
In reply to Kyuwon Cho
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The commit message for 7bdc53e966c0 is the raw multipart MIME email.

--

netdevsim: nsim_start_peer_tx_queue() indexes the peer's TX queues with an RX queue index

This commit adds a check to nsim_start_peer_tx_queue() in drivers/net/netdevsim/netdev.c. The peer's TX queue is now woken only when rq->napi.index is below peer_dev->real_num_tx_queues.

This fixes a KASAN slab-out-of-bounds read in nsim_poll(). The read happens when a device is relinked to a peer that has fewer TX queues while its RX queues still hold traffic from the old peer.

Quoted 7 lines

netdevsim: nsim_start_peer_tx_queue() indexes the peer's TX queues with an RX queue index

--===============8293946977515997899==
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hi Jakub,

[Severity: Medium]
It looks like the cover email was applied here instead of the attached patch. The subject is the title of the bug report. The attached format-patch has its own subject:

netdevsim: bound the peer TX queue index in nsim_start_peer_tx_queue()

The changelog body is the raw multipart MIME message. It includes the MIME boundary lines, the per-part Content-Type, Content-Transfer-Encoding and Content-Disposition headers, and the full repro.c source. It also includes the base64-encoded config.gz attachment:

Quoted 6 lines

--===============8293946977515997899==
Content-Type: application/gzip
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="config.gz"
MIME-Version: 1.0

[ ... ]

The real changelog and its trailers come at the very end, inside the embedded attachment, after a nested format-patch header:

Quoted 5 lines
Quoted 1 line
Quoted 1 line

From 0a9629d8c7c93ef16dd000ab4cf04e9792044987 Mon Sep 17 00:00:00 2001

From: Kyuwon Cho <[email protected]>
Date: Thu, 19 Nov 2026 11:36:52 +0900
Subject: [PATCH net] netdevsim: bound the peer TX queue index in
nsim_start_peer_tx_queue()

[ ... ]

Quoted 2 lines
Fixes ff2ac4df58ad ("netdevsim: implement peer queue flow control")

Could this be re-applied from 0001-netdevsim-bound-the-peer-TX-queue-index-in-nsim_star.patch? Then the subject and body would be the patch's own changelog, with the Fixes: and
Signed-off-by: tags kept. The reproducer and kernel config could stay in the mail thread and be referenced with a Link: tag.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/179505710275.3511681.13434978152402269948%40gmail.com