Skip to content

tun, tap: bound vnet header and skb copy lengths

Junyoung Jang linux-kselftest lkml netdev 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v3 Latest4 patches 4 repliesIn review

Changes in v3 from the cover letter

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

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4net: tun_vnet: validate hdr_len after skipping header paddingdrivers/net/tun_vnet.h+1 -21 reply
2/4tap: clamp zero-copy copylen to remaining payload lengthdrivers/net/tap.c+2 -01 reply
3/4tun: clamp zero-copy copylen to remaining payload lengthdrivers/net/tun.c+1 -01 reply
4/4selftests: net: check padded vnet hdr_len in tun and taptools/testing/selftests/net/tap.c tools/testing/selftests/net/tun.c+60 -01 reply

Reviews trailers per patch

All 4 patches have a Reviewed-by or Acked-by.

PatchWillem de Bruijn
1/4R-b
2/4R-b
3/4R-b
4/4R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH net v3 0/4] 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