* [PATCH net v3 1/4] net: tun_vnet: validate hdr_len after skipping header padding
2026-11-19 14:42 [PATCH net v3 0/4] tun, tap: bound vnet header and skb copy lengths Junyoung Jang
@ 2026-11-19 14:42 ` Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 2/4] tap: clamp zero-copy copylen to remaining payload length Junyoung Jang
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-19 14:42 UTC (permalink / raw)
To: Willem de Bruijn, Jason Wang, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Junyoung Jang, Simon Horman, Shuah Khan, Gwanhyun Lee, netdev,
linux-kernel, linux-kselftest
__tun_vnet_hdr_get() checks hdr_len against iov_iter_count(from) before
advancing past the configured virtio header padding. With a 512-byte
header and a 14-byte Ethernet frame, hdr_len=256 passes because the
iterator still includes header padding. After the advance, only
14 bytes remain. tun_get_user() and tap_get_user() can then reserve a
linear skb area larger than the frame and transmit uncopied bytes.
Advance past the padding before comparing hdr_len with the remaining
payload. Both callers return immediately on error and discard their
iterator, so the cursor position on -EINVAL is not observable by them.
The problem was identified during review of the tap zero-copy fix and
reproduced with vhost-net in an x86-64 QEMU guest: a 14-byte input
produced a 256-byte frame on both tun and tap. After the change, both
drivers reject that input with -EINVAL. The new tun and tap selftests
pass on the patched kernel.
Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Fixes: 2506251e81d1 ("tun: Decouple vnet handling")
Fixes: 6a53fc5a8770 ("tap: Use tun's vnet-related code")
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
drivers/net/tun_vnet.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/net/tun_vnet.h b/drivers/net/tun_vnet.h
index f4c652b1fa44..0b9622891681 100644
--- a/drivers/net/tun_vnet.h
+++ b/drivers/net/tun_vnet.h
@@ -138,11 +138,10 @@ static inline int __tun_vnet_hdr_get(int sz, unsigned int flags,
hdr->hdr_len = cpu_to_tun_vnet16(flags, hdr_len);
}
+ iov_iter_advance(from, sz - parsed_size);
if (hdr_len > iov_iter_count(from))
return -EINVAL;
- iov_iter_advance(from, sz - parsed_size);
-
return hdr_len;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net v3 2/4] tap: clamp zero-copy copylen to remaining payload length
2026-11-19 14:42 [PATCH net v3 0/4] tun, tap: bound vnet header and skb copy lengths Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 1/4] net: tun_vnet: validate hdr_len after skipping header padding Junyoung Jang
@ 2026-11-19 14:42 ` Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 3/4] tun: " Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 4/4] selftests: net: check padded vnet hdr_len in tun and tap Junyoung Jang
3 siblings, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-19 14:42 UTC (permalink / raw)
To: Willem de Bruijn, Jason Wang, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Junyoung Jang, Simon Horman, Shuah Khan, Gwanhyun Lee, netdev,
linux-kernel, linux-kselftest
tap_get_user() chooses GOODCOPY_LEN when the virtio hdr_len is zero,
without limiting it to the payload remaining after the virtio header.
A 246-byte padded header and a 14-byte Ethernet frame give vhost-net a
260-byte TX descriptor, above its 256-byte zero-copy threshold. The
128-byte skb head then contains only 14 copied bytes; the rest can be
transmitted as stale kernel heap contents.
The original macvtap zero-copy support introduced the copylen choice.
Commit bab632d69ee4 ("vhost: vhost TX zero-copy support") connected
the in-tree vhost-net caller, so this case predates the v6.15 header
validation regression.
Clamp copylen to the remaining payload before allocating the skb. This
also covers cases where the entire frame fits in the skb's linear area.
An AI-assisted code review found this issue. An x86-64 QEMU/vhost-net
reproducer observed a 128-byte frame from 14 input bytes, including a
114-byte uncopied tail. The patched kernel delivered 14 bytes in the
same test and in 400 repeated short-frame sends.
Fixes: 97bc3633bec7 ("macvtap: macvtapTX zero-copy support")
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
drivers/net/tap.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 2f19fb07bb2d..f25c58d6e986 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -608,6 +608,8 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
struct iov_iter i;
copylen = clamp(hdr_len ?: GOODCOPY_LEN, ETH_HLEN, good_linear);
+ /* The remaining payload may be shorter than copylen. */
+ copylen = min_t(unsigned long, copylen, len);
linear = copylen;
i = *from;
iov_iter_advance(&i, copylen);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net v3 3/4] tun: clamp zero-copy copylen to remaining payload length
2026-11-19 14:42 [PATCH net v3 0/4] tun, tap: bound vnet header and skb copy lengths Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 1/4] net: tun_vnet: validate hdr_len after skipping header padding Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 2/4] tap: clamp zero-copy copylen to remaining payload length Junyoung Jang
@ 2026-11-19 14:42 ` Junyoung Jang
2026-11-19 14:42 ` [PATCH net v3 4/4] selftests: net: check padded vnet hdr_len in tun and tap Junyoung Jang
3 siblings, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-19 14:42 UTC (permalink / raw)
To: Willem de Bruijn, Jason Wang, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Junyoung Jang, Simon Horman, Shuah Khan, Gwanhyun Lee, netdev,
linux-kernel, linux-kselftest
tun_get_user() also derives copylen from hdr_len or GOODCOPY_LEN
without bounding it to len after removing the virtio header. A
padded header can make a short frame cross vhost-net's zero-copy
threshold. tun_alloc_skb() then reserves more linear packet data than
zerocopy_sg_from_iter() copies, and the uncopied tail can be delivered
to the network stack.
Limit copylen to len before the NAPI fragments and skb allocation
branches. The requested copy length then cannot exceed the supplied
payload. The normal skb-building path is unchanged.
This matching tun issue was noted during review of the tap zero-copy
fix. An AI coding assistant helped prepare the /dev/net/tun
reproducer. In x86-64 QEMU/vhost-net, a 14-byte input produced a
128-byte frame with a nonzero uncopied tail. The patched kernel
delivered 14 bytes, completed 300 repeated short sends, and still
selected zero-copy for a later 400-byte frame.
Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Fixes: 0690899b4d45 ("tun: experimental zero copy tx support")
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
drivers/net/tun.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index dcec7311e601..76486cde21e4 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -1850,6 +1850,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
* The rest of the buffer is mapped from userspace.
*/
copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
+ copylen = min_t(size_t, copylen, len);
linear = copylen;
iov_iter_advance(&i, copylen);
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH net v3 4/4] selftests: net: check padded vnet hdr_len in tun and tap
2026-11-19 14:42 [PATCH net v3 0/4] tun, tap: bound vnet header and skb copy lengths Junyoung Jang
` (2 preceding siblings ...)
2026-11-19 14:42 ` [PATCH net v3 3/4] tun: " Junyoung Jang
@ 2026-11-19 14:42 ` Junyoung Jang
3 siblings, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-19 14:42 UTC (permalink / raw)
To: Willem de Bruijn, Jason Wang, Andrew Lunn, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Junyoung Jang, Simon Horman, Shuah Khan, Gwanhyun Lee, netdev,
linux-kernel, linux-kselftest
Add tun and macvtap write tests with a 512-byte virtio header and a
14-byte Ethernet frame. A hdr_len of 256 must fail with -EINVAL
because it exceeds the post-padding payload. A zero hdr_len must
continue to accept the same valid frame.
These tests exercise the common header parser through both drivers.
On the unpatched kernel, each oversized hdr_len test accepts the
packet instead of rejecting it.
In x86-64 QEMU, the patched kernel passed all 105 tun and five tap
selftests, including the new cases.
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
tools/testing/selftests/net/tap.c | 29 +++++++++++++++++++++++++++++
tools/testing/selftests/net/tun.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)
diff --git a/tools/testing/selftests/net/tap.c b/tools/testing/selftests/net/tap.c
index a0c9418132c8..bf5d36cf73c9 100644
--- a/tools/testing/selftests/net/tap.c
+++ b/tools/testing/selftests/net/tap.c
@@ -425,4 +425,33 @@ TEST_F(tap, test_packet_crash_tap_invalid_eth_proto)
ASSERT_EQ(errno, EINVAL);
}
+TEST_F(tap, padded_hdr_len_exceeds_payload)
+{
+ const int hdr_size = 512;
+ uint8_t pkt[512 + ETH_HLEN] = { 0 };
+ struct virtio_net_hdr *vh = (void *)pkt;
+ ssize_t ret;
+
+ ASSERT_EQ(ioctl(self->fd, TUNSETVNETHDRSZ, &hdr_size), 0);
+ vh->hdr_len = 256;
+ build_eth(pkt + hdr_size, ETH_P_IP);
+
+ ret = write(self->fd, pkt, sizeof(pkt));
+ ASSERT_EQ(ret, -1);
+ ASSERT_EQ(errno, EINVAL);
+}
+
+TEST_F(tap, padded_hdr_len_zero)
+{
+ const int hdr_size = 512;
+ uint8_t pkt[512 + ETH_HLEN] = { 0 };
+ ssize_t ret;
+
+ ASSERT_EQ(ioctl(self->fd, TUNSETVNETHDRSZ, &hdr_size), 0);
+ build_eth(pkt + hdr_size, ETH_P_IP);
+
+ ret = write(self->fd, pkt, sizeof(pkt));
+ ASSERT_EQ(ret, sizeof(pkt));
+}
+
TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 34de2580e32e..525fd588d916 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -683,6 +683,37 @@ TEST_F(tun_vnet_gso, vlan_tcpv4_gso_no_csum_64k)
ASSERT_EQ(ret, len);
}
+TEST_F(tun_vnet_gso, padded_hdr_len_exceeds_payload)
+{
+ const int hdr_size = 512;
+ uint8_t pkt[512 + ETH_HLEN] = { 0 };
+ struct virtio_net_hdr *vh = (void *)pkt;
+ ssize_t ret;
+
+ ASSERT_EQ(ioctl(self->fd, TUNSETVNETHDRSZ, &hdr_size), 0);
+ vh->hdr_len = 256;
+ build_eth(pkt + hdr_size, ETH_P_IP, param_hwaddr_outer_src,
+ param_hwaddr_outer_dst);
+
+ ret = write(self->fd, pkt, sizeof(pkt));
+ ASSERT_EQ(ret, -1);
+ ASSERT_EQ(errno, EINVAL);
+}
+
+TEST_F(tun_vnet_gso, padded_hdr_len_zero)
+{
+ const int hdr_size = 512;
+ uint8_t pkt[512 + ETH_HLEN] = { 0 };
+ ssize_t ret;
+
+ ASSERT_EQ(ioctl(self->fd, TUNSETVNETHDRSZ, &hdr_size), 0);
+ build_eth(pkt + hdr_size, ETH_P_IP, param_hwaddr_outer_src,
+ param_hwaddr_outer_dst);
+
+ ret = write(self->fd, pkt, sizeof(pkt));
+ ASSERT_EQ(ret, sizeof(pkt));
+}
+
FIXTURE(tun_vnet_udptnl)
{
char ifname[IFNAMSIZ];
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread