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 Link: https://lore.kernel.org/netdev/willemdebruijn.kernel.3587ccd7685e2@gmail.com/ Signed-off-by: Janis Edvarts Lacis --- 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // 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