All of lore.kernel.org
 help / color / mirror / Atom feed
From: Janis Edvarts Lacis <janislacis06@gmail.com>
To: netdev@vger.kernel.org
Cc: willemdebruijn.kernel@gmail.com, jasowangio@gmail.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, peterpenkov96@gmail.com,
	maheshb@google.com, Janis Edvarts Lacis <janislacis06@gmail.com>
Subject: [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode
Date: Sun, 29 Nov 2026 19:13:37 +0200	[thread overview]
Message-ID: <20261129171337.815175-1-janislacis06@gmail.com> (raw)

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


             reply	other threads:[~2026-11-29 17:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 17:13 Janis Edvarts Lacis [this message]
2026-11-30 17:15 ` [PATCH net] tun: reject iterators that are not user backed in NAPI frags mode netdev-bot+sashiko

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=20261129171337.815175-1-janislacis06@gmail.com \
    --to=janislacis06@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=maheshb@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peterpenkov96@gmail.com \
    --cc=willemdebruijn.kernel@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.