Skip to contentSkip to conversation

tun, tap: bound vnet header and skb copy lengths

PATCH In review Junyoung Jang 3 participants 10 messages Nov 19 – Nov 23

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover tun, tap: bound vnet header and skb copy lengths

There are two independent length mismatches in tun and tap TX paths. Since v6.15, __tun_vnet_hdr_get() has compared hdr_len with an iterator count that still includes configured virtio header padding. A 512-byte header, hdr_len of 256, and a 14-byte Ethernet frame pass that check even though only 14 payload bytes remain after the padding is skipped. Commit 2506251e81d1 ("tun: Decouple vnet handling") introduced this ordering for tun. Commit 6a53fc5a8770 ("tap: Use tun's vnet-related code") brought the common parser into tap.

An older zero-copy issue also affects both drivers when hdr_len is zero. With a 246-byte padded header and a 14-byte frame, the full 260-byte vhost descriptor crosses its 256-byte zero-copy threshold. The drivers choose GOODCOPY_LEN (128) without limiting it to the remaining frame, allowing an skb to carry uncopied bytes. Commit 97bc3633bec7 ("macvtap: macvtapTX zero-copy support") added the tap calculation. Commit 0690899b4d45 ("tun: experimental zero copy tx support") added the matching tun path.

Patch 1 validates hdr_len after skipping padding. Patches 2 and 3 bound the zero-copy skb head to the remaining payload in tap and tun. Patch 4 adds direct-write regression tests for both drivers.

Clamping keeps valid short frames on the zero-copy completion path. A normal-copy fallback would complete the vhost ubuf_info with failure and increase tx_zcopy_err. Without packet-socket listeners, 401 short frames completed without raising that counter.

Changes since v2:
- Separate the v6.15 header validation regression from the older GOODCOPY_LEN issue and fix the shared parser.
- Add the matching tun zero-copy fix and tun/macvtap selftests.
- Retain the tap copylen clamp, with its original macvtap Fixes tag.

An AI coding assistant assisted with analysis, code and reproducer preparation. In x86-64 QEMU/vhost-net, both drivers now deliver only 14 bytes for the short-frame case and reject the oversized hdr_len case with -EINVAL. The patched kernel passed all 105 tun and five tap selftests.

---
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

Junyoung Jang (4):
net: tun_vnet: validate hdr_len after skipping header padding
tap: clamp zero-copy copylen to remaining payload length
tun: clamp zero-copy copylen to remaining payload length
selftests: net: check padded vnet hdr_len in tun and tap

 drivers/net/tap.c                 |  2 ++
 drivers/net/tun.c                 |  1 +
 drivers/net/tun_vnet.h            |  3 +--
 tools/testing/selftests/net/tap.c | 29 +++++++++++++++++++++++++++++
 tools/testing/selftests/net/tun.c | 31 +++++++++++++++++++++++++++++++
 5 files changed, 64 insertions(+), 2 deletions(-)

base-commit: d48723711bae9b8cdc56719c448ba42fef28cbee

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/4 net: tun_vnet: validate hdr_len after skipping header padding

__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.

Fixes 2506251e81d1 ("tun: Decouple vnet handling")
Fixes 6a53fc5a8770 ("tap: Use tun's vnet-related code")
---
 drivers/net/tun_vnet.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/tun_vnet.h +1 -2
index f4c652b1fa44..0b9622891681 100644
@@ -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;
 }
 
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH net v3 1/4] net: tun_vnet: validate hdr_len after skipping header padding

Quoted 11 lines · Junyoung Jang

__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.

Quoted 6 lines

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.

If respinning, please state explicitly that this restores existing pre 6.15 behavior.

Quoted 6 lines
Fixes 2506251e81d1 ("tun: Decouple vnet handling")
Fixes 6a53fc5a8770 ("tap: Use tun's vnet-related code")
Quoted 24 lines
---
 drivers/net/tun_vnet.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/tun_vnet.h +1 -2
index f4c652b1fa44..0b9622891681 100644
@@ -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;
 }
 
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH net v3 1/4] net: tun_vnet: validate hdr_len after skipping header padding

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

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

Critical: 0 · High: 1 · Medium: 0 · 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).

- [High] At this commit, the new check in __tun_vnet_hdr_get() (drivers/net/tun_vnet.h:141-143) only limits a non-zero hdr_len to the…
- [Low] The commit message says "The new tun and tap selftests pass on the patched kernel", but this commit only changes…

--

net: tun_vnet: validate hdr_len after skipping header padding

This patch moves the iov_iter_advance() past the virtio header padding in __tun_vnet_hdr_get() so it happens before the hdr_len check. hdr_len is then compared against the payload that is really left. With that, a hdr_len larger than the frame is rejected with -EINVAL in tun_get_user() and tap_get_user().

Quoted 5 lines

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.

[Severity: Low]
This commit only changes drivers/net/tun_vnet.h. Which selftests does "The new tun and tap selftests" refer to?

They appear to be added by a later patch in this series, "selftests: net:
check padded vnet hdr_len in tun and tap". That patch adds padded_hdr_len_exceeds_payload and padded_hdr_len_zero to tools/testing/selftests/net/tun.c and tools/testing/selftests/net/tap.c.

Pointing at tests elsewhere in the same series is common. Could the message name that selftest patch, though, so stable backporters know to take it as well?

drivers/net/tun_vnet.h static inline int __tun_vnet_hdr_get(int sz, unsigned int flags, · line 143

Quoted 11 lines
drivers/net/tun_vnet.h +1 -0
index f4c652b1fa44d..0b96228916814 100644
@@ -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;

[Severity: High]
Does this fully close the uninitialized bytes leak that the commit message describes? The check only limits a non-zero hdr_len, so hdr_len == 0 always passes.

On the vhost-net zerocopy path, tun_get_user() then falls back to GOODCOPY_LEN and does not cap it by the remaining payload len:

drivers/net/tun.c:tun_get_user() {
    ...
	if (msg_control) {
		...
		copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
		linear = copylen;
    ...
}

tap_get_user() does the same:

drivers/net/tap.c:tap_get_user() {
    ...
		copylen = clamp(hdr_len ?: GOODCOPY_LEN, ETH_HLEN, good_linear);
		linear = copylen;
    ...
}

Take the same 512-byte vnet_hdr_sz and a 14-byte frame. The handle_tx_zerocopy() threshold (len >= VHOST_GOODCOPY_LEN) includes the vnet header, so this frame still takes the zerocopy path. After that:

tun_get_user() / tap_get_user()
tun_alloc_skb() / tap_alloc_skb()

    skb_put(skb, linear);                 /* linear == 128 */
  zerocopy_sg_from_iter()
    int copy = min_t(int, skb_headlen(skb), iov_iter_count(from)); /* 14 */

Can that leave 114 bytes of uninitialized heap in the transmitted frame? That is the same result this patch sets out to fix.

The trigger needs vhost-net with experimental_zcopytx=1, SOCK_ZEROCOPY on tap, and a large vnet_hdr_sz set by the host. With those in place, the guest controls hdr_len == 0 and the frame length.

Two later patches in this series appear to close the gap: "tap: clamp zero-copy copylen to remaining payload length" and "tun: clamp zero-copy copylen to remaining payload length". They clamp copylen to len with min_t() in tap_get_user() and tun_get_user(). Could that dependency be noted here, or those changes folded into this patch, so this fix is not backported without them?

Quoted 5 lines
drivers/net/tun_vnet.h +0 -2
-	iov_iter_advance(from, sz - parsed_size);
-
 	return hdr_len;
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119144241.360238-1-graypanda.inzag%40gmail.com
#4 lore raw

PATCH v3 2/4 tap: clamp zero-copy copylen to remaining payload length

In reply to Junyoung Jang

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")
---
 drivers/net/tap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/tap.c +2 -0
index 2f19fb07bb2d..f25c58d6e986 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 2/4
#5 lore raw

Re: [PATCH net v3 2/4] tap: clamp zero-copy copylen to remaining payload length

Quoted 12 lines · Junyoung Jang

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.

Quoted 7 lines

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.

If respinning:

tun/tap's vnet_hdr_sz must be at least ~129. That is far larger than any header vhost would negotiate, so it only happens when the VMM sets vnet_hdr_sz inconsistently with the vhost features. The guest controls the packet contents but not vnet_hdr_sz, so a guest can't trigger this on its own against a sane VMM.

Quoted 4 lines
Fixes 97bc3633bec7 ("macvtap: macvtapTX zero-copy support")
#6 lore raw

PATCH v3 3/4 tun: clamp zero-copy copylen to remaining payload length

In reply to Junyoung Jang

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.

Fixes 0690899b4d45 ("tun: experimental zero copy tx support")
---
 drivers/net/tun.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/tun.c +1 -0
index dcec7311e601..76486cde21e4 100644
@@ -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)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 3/4
#7 lore raw

Re: [PATCH net v3 3/4] tun: clamp zero-copy copylen to remaining payload length

Quoted 18 lines · Junyoung Jang

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.

Quoted 5 lines
Fixes 0690899b4d45 ("tun: experimental zero copy tx support")

Thanks for converting the original patch into this more complete series.

#8 lore raw

PATCH v3 4/4 selftests: net: check padded vnet hdr_len in tun and tap

In reply to Junyoung Jang

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.

---
 tools/testing/selftests/net/tap.c | 29 +++++++++++++++++++++++++++++
 tools/testing/selftests/net/tun.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
tools/testing/selftests/net/tap.c +29 -0
index a0c9418132c8..bf5d36cf73c9 100644
@@ -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
tools/testing/selftests/net/tun.c +31 -0
index 34de2580e32e..525fd588d916 100644
@@ -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];
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 4/4
#9 lore raw

Re: [PATCH net v3 4/4] selftests: net: check padded vnet hdr_len in tun and tap

Quoted 9 lines · Junyoung Jang

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.

Quoted 6 lines

In x86-64 QEMU, the patched kernel passed all 105 tun and five tap selftests, including the new cases.