tap: clamp zero-copy copylen to remaining payload length
Subsystems (3)
PATCH v2 1/1 tap: clamp zero-copy copylen to remaining payload length
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.
---
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/[email protected]/
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(+)
drivers/net/tap.c +2 -0
@@ -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
Willem de Bruijn
maintainerQuoted 14 lines · Junyoung Jang
tap_get_user()derives the linear copy length for zero-copy TX fromhdr_lenorGOODCOPY_LENwithout bounding it to the payload remaining after the virtio header.tap_alloc_skb()exposes that length in the skb, butzerocopy_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_lenmakes copylen 128. With a 512-byte virtio header, a guest-suppliedhdr_lenof 256 also passes__tun_vnet_hdr_get(): it checkshdr_lenbefore skipping the padding. That makes copylen 256 for the same 14-byte frame. Larger padded headers can raise copylen as far asgood_linear.
Quoted 4 lines
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.
Quoted 68 lines
Co-developed-by Gwanhyun Lee <[email protected]>Signed-off-by Gwanhyun Lee <[email protected]>Signed-off-by Junyoung Jang <[email protected]>---
Changes in v2:
- Replace the Fixes placeholder and add the co-developer trailers.
- Document and reproduce the guesthdr_len>GOODCOPY_LENpath.
- Shorten the code comment and keep the C reproducer separate.
- Link to v1: https://lore.kernel.org/netdev/[email protected]/Tested on Linux 7.4.0-rc2 (
067c5c24cef2) in x86-64 QEMU/KVM with two vCPUs,CONFIG_MACVTAP=y,CONFIG_VHOST_NET=y, andCONFIG_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_hdr0received_len=128 expected_user_frame=14 [POC]case_a_hdr0tail_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_hdr256received_len=256 expected_user_frame=14 [POC]case_b_hdr256tail_bytes=242 tail_nonzero=82 [POC] control received_len=14 expected_user_frame=14 [POC] used_idx=2 trigger=256 control=14 POC_EXIT=0With this patch applied, using the same inputs:
[POC] case=A
[POC]case_a_hdr0received_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_hdr256received_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=0Disclosure: 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(+)drivers/net/tap.c +2
-0@@ -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
Quoted 7 lines
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 beforeiov_iter_advance(from, sz - sizeof(*hdr))Commit
6a53fc5a8770("tap: Use tun's vnet-related code", v6.15) then brought that bug intodrivers/net/tap.c
Thanks for the review and tracking down those commits.
Mixing the two examples in my v2 commit message made the timeline confusing:
- The second case (sz=512, hdr_len=256 bypassing __tun_vnet_hdr_get()) is indeed the v6.15 regression from 2506251e81d1 and 6a53fc5a8770.
- The first case (246-byte padded vnet_hdr + 14-byte frame with hdr_len=0) predates v6.15. Because vhost-net checks the 260-byte total descriptor length against VHOST_GOODCOPY_LEN (256) before the vnet_hdr is stripped, this short frame still enters the zero-copy path. With len=14 after subtracting vnet_hdr_sz, hdr_len=0 passed the old "hdr_len > len" check, and GOODCOPY_LEN (128) was chosen as copylen. Keeping Fixes: 97bc3633bec7 on tap (and 0690899b4d45 on tun, which has the same unbounded GOODCOPY_LEN issue) ensures pre-v6.15 stable trees pick up the fix as well.
Quoted 2 lines
We still should also include this fix (or skip zero-copy when copylen >= len)
I checked both options. Clamping copylen keeps skb_zcopy_init() intact even with zero frags and completes cleanly via skb_zcopy_clear(), whereas falling back to zerocopy = false invokes uarg->callback(NULL, uarg, false) and increments vhost's tx_zcopy_err counter on valid short frames. So keeping the copylen clamp in both tap and tun looks cleaner.
Quoted 4 lines
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.
I will take on the full work and send out a v3 series shortly, split by Fixes target:
1/4 net: tun_vnet: validate hdr_len after skipping header padding 2/4 tap: clamp zero-copy copylen to remaining payload length 3/4 tun: clamp zero-copy copylen to remaining payload length 4/4 selftests: net: check padded vnet hdr_len in tun and tap
Thanks,
Junyoung
Quoted 120 lines · Willem de Bruijn
Quoted 14 lines · Junyoung Jang
tap_get_user()derives the linear copy length for zero-copy TX fromhdr_lenorGOODCOPY_LENwithout bounding it to the payload remaining after the virtio header.tap_alloc_skb()exposes that length in the skb, butzerocopy_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_lenmakes copylen 128. With a 512-byte virtio header, a guest-suppliedhdr_lenof 256 also passes__tun_vnet_hdr_get(): it checkshdr_lenbefore skipping the padding. That makes copylen 256 for the same 14-byte frame. Larger padded headers can raise copylen as far asgood_linear.Quoted 4 lines
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_szfirst, and rejectedhdr_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 beforeiov_iter_advance(from, sz - sizeof(*hdr))Commit
6a53fc5a8770("tap: Use tun's vnet-related code", v6.15) then brought that bug intodrivers/net/tap.cWe 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_hdrsall this time. But that is perhaps too late too fix. Certainly for net and stable.Quoted 68 lines
Co-developed-by Gwanhyun Lee <[email protected]>Signed-off-by Gwanhyun Lee <[email protected]>Signed-off-by Junyoung Jang <[email protected]>---
Changes in v2:
- Replace the Fixes placeholder and add the co-developer trailers.
- Document and reproduce the guesthdr_len>GOODCOPY_LENpath.
- Shorten the code comment and keep the C reproducer separate.
- Link to v1: https://lore.kernel.org/netdev/[email protected]/Tested on Linux 7.4.0-rc2 (
067c5c24cef2) in x86-64 QEMU/KVM with two vCPUs,CONFIG_MACVTAP=y,CONFIG_VHOST_NET=y, andCONFIG_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_hdr0received_len=128 expected_user_frame=14 [POC]case_a_hdr0tail_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_hdr256received_len=256 expected_user_frame=14 [POC]case_b_hdr256tail_bytes=242 tail_nonzero=82 [POC] control received_len=14 expected_user_frame=14 [POC] used_idx=2 trigger=256 control=14 POC_EXIT=0With this patch applied, using the same inputs:
[POC] case=A
[POC]case_a_hdr0received_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_hdr256received_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=0Disclosure: 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(+)drivers/net/tap.c +2
-0@@ -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
[email protected]
AI reviewThank 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 (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] Incomplete fix: tun.c has the same stale-heap leak and the patch leaves it alone.
- [Low] The commit message understates the impact.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the if (!tap) branch of tap_get_user() (drivers/net/tap.c:646-650) leaks the…
--
tap: clamp zero-copy copylen to remaining payload length
This patch clamps the zero-copy copylen in tap_get_user() to the payload left after the virtio header. Without the clamp, tap_alloc_skb() sizes the linear area from hdr_len or GOODCOPY_LEN. zerocopy_sg_from_iter() only fills the payload bytes that exist, so stale heap contents can be transmitted.
Quoted 6 lines
For example, a 246-byte padded virtio header and a 14-byte Ethernet frame leave only 14 payload bytes, but a zero
hdr_lenmakes copylen 128. With a 512-byte virtio header, a guest-suppliedhdr_lenof 256 also passes__tun_vnet_hdr_get(): it checkshdr_lenbefore skipping the padding. That makes copylen 256 for the same 14-byte frame. Larger padded headers can raise copylen as far asgood_linear.
[Severity: Low]
Does this description understate the exposure? Both examples use padded vnet headers set up by the host. That reads as if an unusual vnet_hdr_sz is needed.
tap_get_user() calls tun_vnet_hdr_get() with features=0, so parsed_size is sizeof(struct virtio_net_hdr), which is 10. __tun_vnet_hdr_get() then does:
drivers/net/tun_vnet.h:__tun_vnet_hdr_get() {
...
if (hdr_len > iov_iter_count(from))
return -EINVAL;
iov_iter_advance(from, sz - parsed_size);
...
}At that check, the count still includes the sz - 10 padding bytes. With the common 12-byte header (VERSION_1 / MRG_RXBUF), a guest can set hdr_len = payload + 2. Before this patch that made copylen = payload + 2, so 2 uninitialized bytes went out with each packet. With the 20-byte hash header the overshoot is 10 bytes.
The new min_t() already handles these cases, so the code is fine. Could the commit message say that the default header sizes are affected too? Stable and security triage will rely on it.
drivers/net/tap.c static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, · line 612
Quoted 10 lines
drivers/net/tap.c +2
-0@@ -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);
[Severity: High]
Does tun_get_user() in drivers/net/tun.c need the same clamp? Its zero-copy branch computes copylen the same way, and nothing bounds it by len:
drivers/net/tun.c:tun_get_user() {
...
if (msg_control) {
struct iov_iter i = *from;
...
copylen = min(hdr_len ? hdr_len : GOODCOPY_LEN, good_linear);
linear = copylen;
iov_iter_advance(&i, copylen);
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
zerocopy = true;
}
...
}When zerocopy is set, tun_can_build_skb() returns false and copylen is not reset to len. From there the path matches tap:
tun_get_user()
tun_alloc_skb(tfile, align, copylen, linear, noblock)
skb_put(skb, linear)
zerocopy_sg_from_iter()
copy = min_t(int, skb_headlen(skb), iov_iter_count(from))
Nothing writes the tail of the linear head, and it goes out through the host stack and any bridged link.
tun_get_user() also calls __tun_vnet_hdr_get(), which the commit message blames for case B. A tun device with IFF_VNET_HDR always has SOCK_ZEROCOPY set. With experimental_zcopytx=1, vhost_net uses zero-copy for guest TX of 256 bytes or more. Either of these looks like it reaches the leak:
vnet_hdr_sz >= 242, hdr_len = 0 and a short frame (copylen = 128) any vnet_hdr_sz > parsed_size, hdr_len = payload + (sz - parsed_size)
Would it make sense to fix this in __tun_vnet_hdr_get(), or in both callers? Otherwise the tun path stays exposed after a stable backport of this patch.
drivers/net/tap.c static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, · line 615
Quoted 3 lines
drivers/net/tap.c +0
-0linear = copylen; i = *from;iov_iter_advance (&i, copylen);
[Severity: High]
This isn't a bug introduced by this patch, but tap_get_user() returns early further down when q->tap is NULL:
drivers/net/tap.c:tap_get_user() {
...
rcu_read_lock();
tap = rcu_dereference(q->tap);
if (!tap) {
kfree_skb(skb);
rcu_read_unlock();
return total_len;
}
...
}This return comes before skb_zcopy_init(skb, msg_control) and before uarg->ops->complete(). Can this leak the vhost zerocopy ubuf_info?
By this point, handle_tx_zerocopy() in drivers/vhost/net.c has already marked the head as in progress and taken a reference on ubufs:
vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS; ... atomic_inc(&ubufs->refcount);
Because sendmsg returned success, vhost waits for vhost_zerocopy_complete() to finish the job, and that callback never runs. The heads[] slot stays VHOST_DMA_IN_PROGRESS, which blocks vhost_zerocopy_signal_used(). The ubufs refcount never reaches zero.
Later, vhost_net_flush() (from release, set_backend or reset_owner) waits with no timeout:
drivers/vhost/net.c:vhost_net_ubuf_put_and_wait() {
vhost_net_ubuf_put(ubufs);
wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
}q->tap becomes NULL in tap_del_queues() when the macvtap/ipvtap link is deleted while the queue fd is still attached to vhost-net:
RCU_INIT_POINTER(q->tap, NULL);
Quoted 1 line
From then on, every zero-copy guest TX takes this path. The process
releasing /dev/vhost-net can then hang in D state. tun does not have this problem because tun_sendmsg() returns -EBADFD once the device is gone.
This looks like it dates back to commit 4f61f133f354 ("net: tap: NULL pointer derefence in dev_parse_header_protocol when skb->dev is null").
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118120102.3717000-1-graypanda.inzag%40gmail.com