The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH net v2] tap: clamp zero-copy copylen to remaining payload length
@ 2026-11-18 12:01 Junyoung Jang
  2026-11-19  4:37 ` Willem de Bruijn
  0 siblings, 1 reply; 2+ messages in thread
From: Junyoung Jang @ 2026-11-18 12:01 UTC (permalink / raw)
  To: Willem de Bruijn, Jason Wang, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Junyoung Jang, Gwanhyun Lee, netdev, linux-kernel

tap_get_user() derives the linear copy length for zero-copy TX from
hdr_len or GOODCOPY_LEN without bounding it to the payload remaining
after the virtio header. tap_alloc_skb() exposes that length in the skb,
but zerocopy_sg_from_iter() copies only the available payload bytes.
The unfilled tail is transmitted on the underlying link and can expose
stale kernel heap contents.

For example, a 246-byte padded virtio header and a 14-byte Ethernet
frame leave only 14 payload bytes, but a zero hdr_len makes copylen 128.
With a 512-byte virtio header, a guest-supplied hdr_len of 256 also
passes __tun_vnet_hdr_get(): it checks hdr_len before skipping the
padding. That makes copylen 256 for the same 14-byte frame.
Larger padded headers can raise copylen as far as good_linear.

Clamp copylen to the remaining payload length before allocating the
skb. The minimum Ethernet frame size has already been checked.

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>
---
Changes in v2:
- Replace the Fixes placeholder and add the co-developer trailers.
- Document and reproduce the guest hdr_len > GOODCOPY_LEN path.
- Shorten the code comment and keep the C reproducer separate.
- Link to v1: https://lore.kernel.org/netdev/20261113164903.785381-1-graypanda.inzag@gmail.com/

Tested on Linux 7.4.0-rc2 (067c5c24cef2) in x86-64 QEMU/KVM with
two vCPUs, CONFIG_MACVTAP=y, CONFIG_VHOST_NET=y, and CONFIG_VETH=y.
The guest used vhost_net.experimental_zcopytx=1 and init_on_alloc=0.
Each case ran in a fresh VM with TUNSETVNETLE enabled.

Before this patch, with a 14-byte Ethernet payload:

  [POC] case=A
  [POC] case_a_hdr0 received_len=128 expected_user_frame=14
  [POC] case_a_hdr0 tail_bytes=114 tail_nonzero=98
  [POC] control received_len=14 expected_user_frame=14
  [POC] used_idx=2 trigger=128 control=14
  POC_EXIT=0

  [POC] case=B
  [POC] case_b_hdr256 received_len=256 expected_user_frame=14
  [POC] case_b_hdr256 tail_bytes=242 tail_nonzero=82
  [POC] control received_len=14 expected_user_frame=14
  [POC] used_idx=2 trigger=256 control=14
  POC_EXIT=0

With this patch applied, using the same inputs:

  [POC] case=A
  [POC] case_a_hdr0 received_len=14 expected_user_frame=14
  [POC] control received_len=14 expected_user_frame=14
  [POC] used_idx=2 trigger=14 control=14
  POC_EXIT=0

  [POC] case=B
  [POC] case_b_hdr256 received_len=14 expected_user_frame=14
  [POC] control received_len=14 expected_user_frame=14
  [POC] used_idx=2 trigger=14 control=14
  POC_EXIT=0

Disclosure: An in-house LLM-based analysis tool developed by the authors
assisted in surfacing this bug candidate and drafting the initial notes.
All root-cause analysis, patch code, and QEMU/KVM reproducer results
were manually reviewed and independently verified by the authors.

 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);

base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net v2] tap: clamp zero-copy copylen to remaining payload length
  2026-11-18 12:01 [PATCH net v2] tap: clamp zero-copy copylen to remaining payload length Junyoung Jang
@ 2026-11-19  4:37 ` Willem de Bruijn
  0 siblings, 0 replies; 2+ messages in thread
From: Willem de Bruijn @ 2026-11-19  4:37 UTC (permalink / raw)
  To: Junyoung Jang, Willem de Bruijn, Jason Wang, Andrew Lunn,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Junyoung Jang, Gwanhyun Lee, netdev, linux-kernel

Junyoung Jang wrote:
> tap_get_user() derives the linear copy length for zero-copy TX from
> hdr_len or GOODCOPY_LEN without bounding it to the payload remaining
> after the virtio header. tap_alloc_skb() exposes that length in the skb,
> but zerocopy_sg_from_iter() copies only the available payload bytes.
> The unfilled tail is transmitted on the underlying link and can expose
> stale kernel heap contents.
> 
> For example, a 246-byte padded virtio header and a 14-byte Ethernet
> frame leave only 14 payload bytes, but a zero hdr_len makes copylen 128.
> With a 512-byte virtio header, a guest-supplied hdr_len of 256 also
> passes __tun_vnet_hdr_get(): it checks hdr_len before skipping the
> padding. That makes copylen 256 for the same 14-byte frame.
> Larger padded headers can raise copylen as far as good_linear.
> 
> Clamp copylen to the remaining payload length before allocating the
> skb. The minimum Ethernet frame size has already been checked.
> 
> Fixes: 97bc3633bec7 ("macvtap: macvtapTX zero-copy support")

Thanks for the fix.

This appears to apply to both tun and tap. But to have been introduced
later

Both macvtap/tap and tun originally subtraced len -= vnet_hdr_sz first,
and rejected hdr_len > len with -EINVAL. We should reinstate that for
both.

Commit 2506251e81d1 ("tun: Decouple vnet handling", v6.15) introduced
the premature check, replacing if (hdr_len > len) with if
(hdr_len > iov_iter_count(from)) and crucially before
iov_iter_advance(from, sz - sizeof(*hdr))

Commit 6a53fc5a8770 ("tap: Use tun's vnet-related code", v6.15) then
brought that bug into drivers/net/tap.c

We still should also include this fix (or skip zero-copy when
copylen >= len)

So there is more work to protect all paths. This on its own fixes
one issue. It should probably be respun to fix the Fixes tag. Let
me know if you want to take on the other work and send a series.
This can certainly also stand on its own. 

Side-note: it is also somewhat surprising that we have allowed such
massively padded vnet_hdrs all this time. But that is perhaps too late
too fix. Certainly for net and stable.

> 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>
> ---
> Changes in v2:
> - Replace the Fixes placeholder and add the co-developer trailers.
> - Document and reproduce the guest hdr_len > GOODCOPY_LEN path.
> - Shorten the code comment and keep the C reproducer separate.
> - Link to v1: https://lore.kernel.org/netdev/20261113164903.785381-1-graypanda.inzag@gmail.com/
> 
> Tested on Linux 7.4.0-rc2 (067c5c24cef2) in x86-64 QEMU/KVM with
> two vCPUs, CONFIG_MACVTAP=y, CONFIG_VHOST_NET=y, and CONFIG_VETH=y.
> The guest used vhost_net.experimental_zcopytx=1 and init_on_alloc=0.
> Each case ran in a fresh VM with TUNSETVNETLE enabled.
> 
> Before this patch, with a 14-byte Ethernet payload:
> 
>   [POC] case=A
>   [POC] case_a_hdr0 received_len=128 expected_user_frame=14
>   [POC] case_a_hdr0 tail_bytes=114 tail_nonzero=98
>   [POC] control received_len=14 expected_user_frame=14
>   [POC] used_idx=2 trigger=128 control=14
>   POC_EXIT=0
> 
>   [POC] case=B
>   [POC] case_b_hdr256 received_len=256 expected_user_frame=14
>   [POC] case_b_hdr256 tail_bytes=242 tail_nonzero=82
>   [POC] control received_len=14 expected_user_frame=14
>   [POC] used_idx=2 trigger=256 control=14
>   POC_EXIT=0
> 
> With this patch applied, using the same inputs:
> 
>   [POC] case=A
>   [POC] case_a_hdr0 received_len=14 expected_user_frame=14
>   [POC] control received_len=14 expected_user_frame=14
>   [POC] used_idx=2 trigger=14 control=14
>   POC_EXIT=0
> 
>   [POC] case=B
>   [POC] case_b_hdr256 received_len=14 expected_user_frame=14
>   [POC] control received_len=14 expected_user_frame=14
>   [POC] used_idx=2 trigger=14 control=14
>   POC_EXIT=0
> 
> Disclosure: An in-house LLM-based analysis tool developed by the authors
> assisted in surfacing this bug candidate and drafting the initial notes.
> All root-cause analysis, patch code, and QEMU/KVM reproducer results
> were manually reviewed and independently verified by the authors.
> 
>  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);
> 
> base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19  4:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 12:01 [PATCH net v2] tap: clamp zero-copy copylen to remaining payload length Junyoung Jang
2026-11-19  4:37 ` Willem de Bruijn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox