Netdev List
 help / color / mirror / Atom feed
* [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode
@ 2026-11-29 17:13 Janis Edvarts Lacis
  2026-11-30 17:15 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Janis Edvarts Lacis @ 2026-11-29 17:13 UTC (permalink / raw)
  To: netdev
  Cc: willemdebruijn.kernel, jasowangio, andrew+netdev, davem, edumazet,
	kuba, pabeni, peterpenkov96, maheshb, Janis Edvarts Lacis

The fragment allocation loop in tun_napi_alloc_frags() assumes that
the underlying iterator is of type ITER_IOVEC. However, an io_uring
fixed-buffer write to /dev/net/tun uses an ITER_BVEC iterator, causing
the loop to interpret bio_vec entries as iovec entries.

This can result in an SKB where the sum of the fragment sizes does
not match skb->data_len. This was reproduced using an io_uring
fixed-buffer write to a TAP device, see the reproducer code below.

Reject non user-backed iterators in tun_napi_alloc_frags() with
-EOPNOTSUPP.

The bug was discovered by looking through patchwork and seeing Willem's
response to Sashiko's review. Tested on an x86-64 QEMU guest with 4 KiB pages.

Fixes: 90e33d459407 ("tun: enable napi_gro_frags() for TUN/TAP driver")
Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Link: https://lore.kernel.org/netdev/willemdebruijn.kernel.3587ccd7685e2@gmail.com/
Signed-off-by: Janis Edvarts Lacis <janislacis06@gmail.com>
---

For the reproduction, the following code was added right before the
tun_napi_alloc_frags() successful return:
{
	size_t frag_bytes = 0;
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
    		frag_bytes += skb_frag_size(&skb_shinfo(skb)->frags[i]);
	printk("bvec=%d len=%u data_len=%u frag_bytes=%zu\n",
        	iov_iter_is_bvec(it), skb->len, skb->data_len,
        	frag_bytes);
}

The output in dmesg was:
[   31.723287] bvec=1 len=5042 data_len=946 frag_bytes=4096

The reproducer program prints 'written: 5042'

Compile the reproducer with :
    cc poc.c -o poc -luring

======BEGIN poc.c======
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/uio.h>

#include <liburing.h>
#include <linux/if.h>
#include <linux/if_tun.h>

// assumes that one page is 4kb
#define PACKET_PAYLOAD_SIZE 5000
#define UDP_HDR_SIZE 8

struct packet_iphdr {
    uint32_t source_addr;
    uint32_t dest_addr;
    uint8_t zeros;
    uint8_t prot;
    uint16_t length;
};

static void die(const char *s)
{
	perror(s);
	exit(EXIT_FAILURE);
}

int setup_tap(char *dev)
{
        int fd = open("/dev/net/tun", O_RDWR);
        if (fd < 0)
            return -1;

        struct ifreq ifr = {0};
        ifr.ifr_flags = IFF_TAP | IFF_NAPI | IFF_NAPI_FRAGS | IFF_NO_PI;

        if (*dev)
            strcpy(ifr.ifr_name, dev);

        int err = ioctl(fd, TUNSETIFF, (void *) &ifr);
        if (err < 0) {
            close(fd);
            return err;
        }

        strcpy(dev, ifr.ifr_name);

        int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (s < 0)
	        die("socket");

        memset(&ifr, 0, sizeof(ifr));
        strcpy(ifr.ifr_name, dev);
	ifr.ifr_mtu = 9000;

	if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
		die("SIOCSIFMTU");

	if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
		die("SIOCGIFFLAGS");

	ifr.ifr_flags |= IFF_UP;

	if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
		die("SIOCSIFFLAGS");

	close(s);

    return fd;
}

static void put_be16(uint8_t *p, uint16_t value)
{
	p[0] = value >> 8;
	p[1] = value & 0xff;
}

static uint16_t ipv4_checksum(const uint8_t *ip)
{
	uint32_t sum = 0;

	for (size_t i = 0; i < 20; i += 2)
		sum += ((uint16_t)ip[i] << 8) | ip[i + 1];

	while (sum >> 16)
		sum = (sum & 0xffff) + (sum >> 16);

	return (uint16_t)~sum;
}

static ssize_t build_udp_packet(uint8_t *frame)
{
	enum { ETH_LEN = 14, IP_LEN = 20, UDP_LEN = 8, ETH_MIN = 60 };
	const uint8_t src_mac[6] = {0}, dst_mac[6] = {0};
	size_t udp_len, ip_len, frame_len;
	struct sockaddr_in src_ip, dst_ip;
	uint16_t src_port = 12345;
	uint16_t dst_port = 12345;
	uint8_t *ip, *udp;

        if (inet_pton(AF_INET, "192.0.2.1", &src_ip.sin_addr) != 1) {
            perror("inet_pton(src)");
            return -1;
        }

        if (inet_pton(AF_INET, "192.0.2.2", &dst_ip.sin_addr) != 1) {
            perror("inet_pton(dst)");
            return -1;
        }

	udp_len = UDP_LEN + PACKET_PAYLOAD_SIZE ;
	ip_len = IP_LEN + udp_len;
	frame_len = ETH_LEN + ip_len;
	if (frame_len < ETH_MIN)
		frame_len = ETH_MIN;

	memset(frame, 0, frame_len);
	memcpy(frame, dst_mac, 6);
	memcpy(frame + 6, src_mac, 6);
	put_be16(frame + 12, 0x0800);

	ip = frame + ETH_LEN;
	ip[0] = 0x45;
	put_be16(ip + 2, ip_len);
	ip[8] = 64;
	ip[9] = IPPROTO_UDP;
	memcpy(ip + 12, &src_ip.sin_addr, 4);
	memcpy(ip + 16, &dst_ip.sin_addr, 4);
	put_be16(ip + 10, ipv4_checksum(ip));

	udp = ip + IP_LEN;
	put_be16(udp, src_port);
	put_be16(udp + 2, dst_port);
	put_be16(udp + 4, udp_len);

	return (ssize_t)frame_len;
}

int main()
{
    char dev_name[IFNAMSIZ] = "tun0";
    struct io_uring_sqe *sqe;
    struct io_uring_cqe *cqe;
    struct io_uring ring;
    uint8_t *packet;
    struct iovec iov;
    int packet_size;
    int tap_fd;

    tap_fd = setup_tap(dev_name);
    if (tap_fd < 0)
        die("setup_tap");

    if (posix_memalign((void **)&packet, 4096, 8192)) {
		fprintf(stderr, "posix_memalign failed\n");
		return EXIT_FAILURE;
	}

    packet_size = build_udp_packet(packet);
    if (packet_size < 0)
        die("build_udp_packet");

    io_uring_queue_init(1, &ring, 0);
    sqe = io_uring_get_sqe(&ring);

	iov = (struct iovec){
		.iov_base = packet,
		.iov_len = 8192,
	};
    io_uring_register_buffers(&ring, &iov, 1);
    io_uring_prep_write_fixed(sqe, tap_fd, packet, packet_size, 0, 0);
    io_uring_sqe_set_data(sqe, dev_name);  // use dev_name, does not matter
    io_uring_submit(&ring);

    // get response
    if (io_uring_wait_cqe(&ring, &cqe) < 0)
        die("io_uring_wait_cqe");
    if (cqe->res < 0) {
        errno = -cqe->res;
        perror("io_uring_wait_cqe()->res");
        die("");
    }

    printf("written: %d\n", cqe->res);

    io_uring_cqe_seen(&ring, cqe);
    return 0;
}
======END poc.c========
---
 drivers/net/tun.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index ae05b1e58aba..864945f2fac2 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1502,6 +1502,9 @@ static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
 	int err;
 	int i;
 
+	if (!user_backed_iter(it))
+		return ERR_PTR(-EOPNOTSUPP);
+
 	if (it->nr_segs > MAX_SKB_FRAGS + 1 ||
 	    len > (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN))
 		return ERR_PTR(-EMSGSIZE);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode
  2026-11-29 17:13 [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode Janis Edvarts Lacis
@ 2026-11-30 17:15 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-30 17:15 UTC (permalink / raw)
  To: janislacis06
  Cc: netdev, willemdebruijn.kernel, jasowangio, andrew+netdev, davem,
	edumazet, kuba, pabeni, peterpenkov96, maheshb

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

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

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 new early return `if (!user_backed_iter(it)) return
  ERR_PTR(-EOPNOTSUPP);` in tun_napi_alloc_frags() runs before…
- [Low] The commit message understates what the bug does.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same 'skb
  lengths do not match the fragment layout' bug stays reachable with a…

--

tun: reject iterators that are not user backed in NAPI frags mode

This commit adds a user_backed_iter() check at the top of
tun_napi_alloc_frags(). Iterators that are not user backed, such as the
ITER_BVEC used by io_uring fixed-buffer writes, are now rejected with
-EOPNOTSUPP. Before this, the fragment loop read bio_vec entries as if
they were iovec entries.

Link: https://lore.kernel.org/netdev/willemdebruijn.kernel.3587ccd7685e2@gmail.com/

> This can result in an SKB where the sum of the fragment sizes does
> not match skb->data_len. This was reproduced using an io_uring
> fixed-buffer write to a TAP device, see the reproducer code below.

[Severity: Low]
The code change itself is fine, but does this description understate
what the bug does?

In tun_napi_alloc_frags(), bvec[i] is read as a struct iovec. On 64-bit
little-endian, iov_len overlays bv_len | (bv_offset << 32), so fragsz is
4096 when bv_offset is 0:

    const struct iovec *iov = iter_iov(it) + i;
    size_t fragsz = iov->iov_len;
    ...
    frag = netdev_alloc_frag(fragsz);

netdev_alloc_frag() does not zero the memory. Later, tun_get_user() calls:

    err = skb_copy_datagram_from_iter(skb, 0, from, len);

This fills only data_len bytes of the frags and returns 0 once len is
used up. The skb is then passed to napi_gro_frags().

With the reproducer output (len=5042, data_len=946, frag_bytes=4096),
3150 bytes of stale page-frag memory sit inside a frag labelled 4096
bytes.

Could anything that walks frags by skb_frag_size() read or transmit that
memory? Two examples are an SG-capable NIC driver after bridging or
forwarding, and skb_segment().

If so, could the commit message mention the possible kernel memory
disclosure, so stable and security triage can weigh it properly?

> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index ae05b1e58abaa..864945f2fac20 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -1502,6 +1502,9 @@ static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
>  	int err;
>  	int i;
>  
> +	if (!user_backed_iter(it))
> +		return ERR_PTR(-EOPNOTSUPP);
> +

[Severity: Medium]
Can this early return leak a recycled skb cached in tfile->napi.skb?

The return happens before napi_get_frags(), and before the free label
that calls napi_free_frags().

tfile->napi.skb can still hold an skb from an earlier write. That
happens when napi_gro_frags() returns GRO_MERGED_FREE:

net/core/gro.c:napi_frags_finish() {
    ...
    case GRO_MERGED_FREE:
        if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
            napi_skb_free_stolen_head(skb);
        else
            napi_reuse_skb(napi, skb);
    ...
}

napi_reuse_skb() ends with napi->skb = skb, and tun_get_user() then
just unlocks napi_mutex.

Now suppose the next write is an io_uring IORING_OP_WRITE_FIXED
(ITER_BVEC) to the same IFF_NAPI_FRAGS device. tun_get_user() jumps to
drop with an ERR_PTR skb:

drivers/net/tun.c:tun_get_user() {
    ...
free_skb:
    drop_reason = drop_reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
    if (!IS_ERR_OR_NULL(skb))
        kfree_skb_reason(skb, drop_reason);

unlock_frags:
    if (frags) {
        tfile->napi.skb = NULL;
        mutex_unlock(&tfile->napi_mutex);
    }
    ...
}

The ERR_PTR skb is not freed. Then tfile->napi.skb = NULL drops the
only reference to the recycled sk_buff and its head buffer.
napi_free_frags() at NAPI teardown cannot reclaim it either, because
napi->skb is already NULL.

A write that GRO merges followed by a fixed-buffer write would then leak
one skb per pair, with no limit. Before this patch, the bvec write
reused the cached skb instead.

The existing -EMSGSIZE early return just below has the same flaw.
However, this check adds an easy way to trigger it for a whole class of
iterators.

Would it be better to do the user_backed_iter() check in tun_get_user()
before taking napi_mutex? Another option would be to clear
tfile->napi.skb in unlock_frags only when it was actually used or freed.

>  	if (it->nr_segs > MAX_SKB_FRAGS + 1 ||
>  	    len > (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN))
>  		return ERR_PTR(-EMSGSIZE);

[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Can
the same mismatch between skb lengths and frag layout still be reached
with a user-backed ITER_IOVEC iterator? The new check lets those
through.

When tun_get_user() is called with msg_control set (the vhost-net
zerocopy TX path, TUN_MSG_UBUF), zerocopy is true and copylen stays
short:

    copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
    ...
    if (frags) {
        mutex_lock(&tfile->napi_mutex);
        skb = tun_napi_alloc_frags(tfile, copylen, from);
        ...
        zerocopy = false;
    ...
    err = skb_copy_datagram_from_iter(skb, 0, from, len);

Inside tun_napi_alloc_frags(), linear is the length of the first
segment, which the guest controls:

    linear = iov_iter_single_seg_count(it);
    err = __skb_grow(skb, linear);
    ...
    skb->len = len;
    skb->data_len = len - linear;
    skb->truesize += skb->data_len;

Suppose the first segment is longer than copylen, for example 1500 vs
128. Then skb->data_len wraps to roughly 0xFFFFFAA4 and truesize wraps
as well, while skb->len is 128.

Would the following eth_type_trans()->skb_pull_inline()->__skb_pull()
then see skb->len < skb->data_len and hit BUG()?

If the first segment is shorter than copylen, the frag sizes exceed
data_len instead. That is the symptom this commit describes.

It looks like the cause here is the length argument passed in, not the
iterator type.

Reaching this needs an IFF_NAPI_FRAGS tap used as a vhost-net backend
with experimental_zcopytx=1. Once that is set up, the guest's TX
descriptor layout decides whether it triggers.

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 17:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 17:13 [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode Janis Edvarts Lacis
2026-11-30 17:15 ` netdev-bot+sashiko

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