vsock/virtio: fix msg_iter desync on transmission failure
Subsystems (10)
v3 cover vsock/virtio: fix msg_iter desync on transmission failure
This series fixes a msg_iter desync issue in the virtio vsock transport that can lead to warnings and eventual -ENOMEM under specific failure scenarios (e.g. partial GUP failure during MSG_ZEROCOPY transmission).
To fix this, we need to restore the msg_iter state on transmission failure. However, since virtio vsock transport can be built as a module, we first need to export iov_iter_restore.
Patch 1 exports iov_iter_restore.
Patch 2 implements the msg_iter restoration in virtio vsock.
Changes in v3:
- Use EXPORT_SYMBOL_GPL (Jens)
Changes in v2:
- Use iov_iter_savestate()/iov_iter_restore() (Stefano)
- Use a single restore point (Stefano)
- Reverse xmas tree (Stefano)
- Added comments in the code (Stefano)
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Octavian Purdila (2):
iov_iter: export iov_iter_restore vsock/virtio: restore msg_iter on transmission failure
lib/iov_iter.c | 1 + net/vmw_vsock/virtio_transport_common.c | 13 +++++++++++++ 2 files changed, 14 insertions(+)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
PATCH v3 1/2 iov_iter: export iov_iter_restore
Export iov_iter_restore so that it can be used by modules.
This is needed by the virtio vsock transport (which can be built as a module) to restore the msg_iter state when transmission fails.
--- lib/iov_iter.c | 1 + 1 file changed, 1 insertion(+)
lib/iov_iter.c +1 -0
@@ -1491,6 +1491,7 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) i->__iov -= state->nr_segs - i->nr_segs; i->nr_segs = state->nr_segs; } +EXPORT_SYMBOL_GPL (iov_iter_restore);/* * Extract a list of contiguous pages from an ITER_FOLIOQ iterator. This does
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
[email protected]
AI reviewRe: [PATCH net v3 1/2] iov_iter: export iov_iter_restore
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Exporting iov_iter_restore exposes a pre-existing logic error where restoring an ITER_KVEC iterator falsely triggers a WARN_ON_ONCE. --
commit b7b5854a768fd37654ad61552651a21df8d14c8f
Author: Octavian Purdila <[email protected]>
iov_iter: export iov_iter_restore
Export iov_iter_restore so that it can be used by modules. This is needed by the virtio vsock transport to restore the msg_iter state when transmission fails.
lib/iov_iter.c void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) · line 1494
Quoted 9 lines
lib/iov_iter.c +1
-0@@ -1491,6 +1491,7 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) i->__iov -= state->nr_segs - i->nr_segs; i->nr_segs = state->nr_segs; } +EXPORT_SYMBOL_GPL (iov_iter_restore);
[Severity: High]
This isn't a bug introduced by this patch, but exporting this function to modules exposes a pre-existing issue when restoring an ITER_KVEC iterator.
Looking at iov_iter_restore() in lib/iov_iter.c:
void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state)
{
if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) &&
!iter_is_ubuf(i)) && !iov_iter_is_kvec(i))
return;Because !iov_iter_is_kvec(i) is placed outside the WARN_ON_ONCE() parentheses, restoring an ITER_KVEC evaluates to WARN_ON_ONCE(true) && false. This safely skips the early return, but still incorrectly triggers the warning, which can lead to a panic on systems with panic_on_warn enabled.
Since ITER_KVEC is often used for kernel-originated network and storage data, does exporting this to modules like virtio vsock create a risk of falsely triggering warnings or a denial of service condition?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian Brauner
maintainerRe: [PATCH net v3 1/2] iov_iter: export iov_iter_restore
In reply to Octavian PurdilaQuoted 13 lines
Export
iov_iter_restoreso that it can be used by modules.This is needed by the virtio vsock transport (which can be built as a module) to restore the
msg_iterstate when transmission fails.Signed-off-by Octavian Purdila <[email protected]>lib/iov_iter.c +0
-0
lib/iov_iter.c void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) · line 1494
Quoted 4 lines
lib/iov_iter.c +1
-0@@ -1491,6 +1491,7 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) i->__iov -= state->nr_segs - i->nr_segs; i->nr_segs = state->nr_segs; } +EXPORT_SYMBOL_GPL (iov_iter_restore);
At least only export it for the module that really needs it. For example, see:
EXPORT_SYMBOL_FOR_MODULES(__kernel_write, "autofs4");
Signature · 1 line
Christian Brauner <[email protected]>
Michael S. Tsirkin
maintainerRe: [PATCH net v3 1/2] iov_iter: export iov_iter_restore
Quoted 21 lines · Christian Brauner
Quoted 13 lines
Export
iov_iter_restoreso that it can be used by modules.This is needed by the virtio vsock transport (which can be built as a module) to restore the
msg_iterstate when transmission fails.Signed-off-by Octavian Purdila <[email protected]>lib/iov_iter.c +0
-0lib/iov_iter.c void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) · line 1494
Quoted 4 lines
lib/iov_iter.c +1
-0@@ -1491,6 +1491,7 @@ void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) i->__iov -= state->nr_segs - i->nr_segs; i->nr_segs = state->nr_segs; } +EXPORT_SYMBOL_GPL (iov_iter_restore);At least only export it for the module that really needs it. For example, see:
Quoted 4 lines
EXPORT_SYMBOL_FOR_MODULES(__kernel_write, "autofs4");Signature · 1 line
Christian Brauner <[email protected]>
Octavian, you wanna try?
PATCH v3 2/2 vsock/virtio: restore msg_iter on transmission failure
In reply to Octavian PurdilaWhen transmission fails in virtio_transport_send_pkt_info, the msg_iter might have been partially advanced. If we don't restore it, the next attempt to send data will use an incorrect iterator state, leading to desync and warnings like "send_pkt() returns 0, but X expected".
Specifically, this can happen in the following scenario, triggered by the syzkaller repro:
1. A write-only VMA (PROT_WRITE only) is partially populated by a prior TUN write that failed with -EIO but still faulted in some pages).
2. A vsock sendmmsg call with MSG_ZEROCOPY requests transmission of a buffer from this VMA.
3. The first packet (64KB) is sent successfully because the pages are populated.
4. The second packet allocation fails because GUP fast pins the first page but GUP slow fails on the next unpopulated page due to PROT_WRITE-only permissions.
5. The iterator is advanced by the partially successful GUP (68KB total
advanced: 64KB from first packet + 4KB from second), but the send loop breaks and only reports 64KB sent. This creates a 4KB desync.
6. The next retry starts with a non-zero iov_offset, disabling zerocopy and falling back to copy mode.
7. In copy mode, the transmission succeeds for the next packets but exhausts the iterator early because of the desync.
8. The final retry sees an empty iterator but zerocopy is re-enabled (offset resets). It attempts to send the remaining bytes with zerocopy but pins 0 pages, creating an empty packet.
9. The transport sends the empty packet, triggering the warning because the returned bytes (header only) do not match the expected payload size.
10. The loop continues to spin, allocating ubuf_info each time, eventually exhausting sysctl_optmem_max and returning -ENOMEM to userspace.
Restore msg_iter to its original state before the packet allocation and transmission attempt if they fail.
--- net/vmw_vsock/virtio_transport_common.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
net/vmw_vsock/virtio_transport_common.c +13 -0
@@ -295,6 +295,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,u32 max_skb_len =VIRTIO_VSOCK_MAX_PKT_BUF_SIZE ;u32 src_cid, src_port, dst_cid, dst_port;const struct virtio_transport *t_ops; +struct iov_iter_state msg_iter_state;struct virtio_vsock_sock *vvs;struct ubuf_info *uarg =NULL ;u32 pkt_len = info->pkt_len;
@@ -368,8 +369,17 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,struct sk_buff *skb;size_t skb_len; +/* Save iterator state in case allocation or transmission fails +* so we can restore it and retry. +*/ +if (info->msg) +iov_iter_save_state (&info->msg->msg_iter, &msg_iter_state); + skb_len =min (max_skb_len, rest_len); +/* Note: virtio_transport_alloc_skb() can advance info->msg->msg_iter +* even if it fails (e.g. partial GUP success). +*/ skb = virtio_transport_alloc_skb(info, skb_len, can_zcopy, uarg, src_cid, src_port,
@@ -399,6 +409,9 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,break ; }while (rest_len); +if (info->msg && ret <0 ) +iov_iter_restore (&info->msg->msg_iter, &msg_iter_state); +virtio_transport_put_credit (vvs, rest_len);/* msg_zerocopy_realloc() initializes the ubuf_info refcnt to 1.
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog