[PATCH net v3 4/4] selftests: net: check padded vnet hdr_len in tun and tap
From: Junyoung Jang
Date: Thu Nov 19 2026 - 09:43:54 EST
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@xxxxxxxxx>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@xxxxxxxxx>
Signed-off-by: Junyoung Jang <graypanda.inzag@xxxxxxxxx>
---
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