Skip to contentSkip to conversation

vsock/virtio: fix zerocopy completion for multi-skb sends

PATCH Applied Alexander Martyniuk 3 participants 3 messages Jul 16 – Jul 19

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vsock/virtio: fix zerocopy completion for multi-skb sends

From: Stefano Garzarella <[email protected]>

commit ae38d9179190a956e2a87a69ef1dd6f451b51c4d upstream.

When a large message is fragmented into multiple skbs, the zerocopy uarg is only allocated and attached to the last skb in the loop. Non-final skbs carry pinned user pages with no completion tracking, so the kernel has no way to notify userspace when those pages are safe to reuse. If the loop breaks early the uarg is never allocated at all, leaking pinned pages with no completion notification.

Fix this by following the approach used by TCP: allocate the zerocopy uarg (if not provided by the caller) before the send loop and attach it to every skb via skb_zcopy_set(), which takes a reference per skb. Each skb's completion properly decrements the refcount, and the notification only fires after the last skb is freed.
On failure, if no data was sent, the uarg is cleanly aborted via net_zcopy_put_abort().

This issue was initially discovered by sashiko while reviewing commit 1cb36e252211 ("vsock/virtio: fix MSG_ZEROCOPY pinned-pages accounting") but was pre-existing.

---
Backport fix for CVE-2026-53365

 net/vmw_vsock/virtio_transport_common.c | 78 +++++++++++--------------
 1 file changed, 34 insertions(+), 44 deletions(-)
net/vmw_vsock/virtio_transport_common.c +34 -44
index 95170c7be758..aeb205e84bd3 100644
@@ -72,35 +72,6 @@ static bool virtio_transport_can_zcopy(const struct virtio_transport *t_ops,
 	return true;
 }
 
-static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk,
-					   struct sk_buff *skb,
-					   struct msghdr *msg,
-					   bool zerocopy)
-{
-	struct ubuf_info *uarg;
-
-	if (msg->msg_ubuf) {
-		uarg = msg->msg_ubuf;
-		net_zcopy_get(uarg);
-	} else {
-		struct iov_iter *iter = &msg->msg_iter;
-		struct ubuf_info_msgzc *uarg_zc;
-
-		uarg = msg_zerocopy_realloc(sk_vsock(vsk),
-					    iter->count,
-					    NULL);
-		if (!uarg)
-			return -1;
-
-		uarg_zc = uarg_to_msgzc(uarg);
-		uarg_zc->zerocopy = zerocopy ? 1 : 0;
-	}
-
-	skb_zcopy_init(skb, uarg);
-
-	return 0;
-}
-
 static int virtio_transport_fill_skb(struct sk_buff *skb,
 				     struct virtio_vsock_pkt_info *info,
 				     size_t len,
@@ -321,8 +292,10 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 	u32 src_cid, src_port, dst_cid, dst_port;
 	const struct virtio_transport *t_ops;
 	struct virtio_vsock_sock *vvs;
+	struct ubuf_info *uarg = NULL;
 	u32 pkt_len = info->pkt_len;
 	bool can_zcopy = false;
+	bool have_uref = false;
 	u32 rest_len;
 	int ret;
 
@@ -364,6 +337,25 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 		if (can_zcopy)
 			max_skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE,
 					    (MAX_SKB_FRAGS * PAGE_SIZE));
+
+		if (info->msg->msg_flags & MSG_ZEROCOPY &&
+		    info->op == VIRTIO_VSOCK_OP_RW) {
+			uarg = info->msg->msg_ubuf;
+
+			if (!uarg) {
+				uarg = msg_zerocopy_realloc(sk_vsock(vsk),
+							    pkt_len, NULL);
+				if (!uarg) {
+					virtio_transport_put_credit(vvs, pkt_len);
+					return -ENOMEM;
+				}
+
+				if (!can_zcopy)
+					uarg_to_msgzc(uarg)->zerocopy = 0;
+
+				have_uref = true;
+			}
+		}
 	}
 
 	rest_len = pkt_len;
@@ -382,21 +374,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 			break;
 		}
 
-		/* We process buffer part by part, allocating skb on
-		 * each iteration. If this is last skb for this buffer
-		 * and MSG_ZEROCOPY mode is in use - we must allocate
-		 * completion for the current syscall.
-		 */
-		if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY &&
-		    skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) {
-			if (virtio_transport_init_zcopy_skb(vsk, skb,
-							    info->msg,
-							    can_zcopy)) {
-				kfree_skb(skb);
-				ret = -ENOMEM;
-				break;
-			}
-		}
+		skb_zcopy_set(skb, uarg, NULL);
 
 		virtio_transport_inc_tx_pkt(vvs, skb);
 
@@ -420,6 +398,18 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 
 	virtio_transport_put_credit(vvs, rest_len);
 
+	/* msg_zerocopy_realloc() initializes the ubuf_info refcnt to 1.
+	 * skb_zcopy_set() increases it for each skb, so we can drop that
+	 * initial reference to keep it balanced.
+	 */
+	if (have_uref) {
+		if (rest_len == pkt_len)
+			/* No data sent, abort the notification. */
+			net_zcopy_put_abort(uarg, true);
+		else
+			net_zcopy_put(uarg);
+	}
+
 	/* Return number of bytes, if any data has been sent. */
 	if (rest_len != pkt_len)
 		ret = pkt_len - rest_len;
Signature · 1 line
2.43.0
#1 lore raw
Thank 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:

Pre-existing issues:
- [Critical] Failure during zerocopy skb allocation advances the user's iov_iter without reverting it, causing subsequent loop iterations to skip bytes and silently corrupt the message stream.
- [High] MSG_ZEROCOPY violates its API contract by generating multiple completion IDs for a single sendmsg() syscall when the message is fragmented by the socket's wait loop, which can cause Use-After-Free in userspace. --

--- Patch [1]: [PATCH 6.12] vsock/virtio: fix zerocopy completion for multi-skb sends --- commit 60570416b19b3acf053340573a6882df58212cc4
Author: Stefano Garzarella <[email protected]>

vsock/virtio: fix zerocopy completion for multi-skb sends

This commit fixes an issue where non-final skbs in a fragmented message do not carry zerocopy completion tracking. It resolves this by pre-allocating the zerocopy uarg before the send loop and attaching it to every skb via skb_zcopy_set. Notification only fires after the last skb is freed.

Quoted 4 lines
net/vmw_vsock/virtio_transport_common.c +0 -0
index 9acc13ab3f822..62d753e3df358 100644

[ ... ]

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, · line 355

Quoted 12 lines
net/vmw_vsock/virtio_transport_common.c +8 -0
@@ -372,6 +345,25 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 		if (can_zcopy)
 			max_skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE,
 					    (MAX_SKB_FRAGS * PAGE_SIZE));
+
+		if (info->msg->msg_flags & MSG_ZEROCOPY &&
+		    info->op == VIRTIO_VSOCK_OP_RW) {
+			uarg = info->msg->msg_ubuf;
+
+			if (!uarg) {
+				uarg = msg_zerocopy_realloc(sk_vsock(vsk),
+							    pkt_len, NULL);

[Severity: High]
This is a pre-existing issue, but does MSG_ZEROCOPY generate multiple completion IDs for a single sendmsg() syscall here?

If the peer lacks sufficient credit, stream_enqueue() in af_vsock.c can return a short write, causing vsock_connectible_sendmsg() to loop and wait. In the next iteration, info->msg->msg_ubuf remains NULL because it was never updated with the previously allocated uarg.

When virtio_transport_send_pkt_info() is called again, does it allocate a new uarg with msg_zerocopy_realloc(..., NULL) and increment the socket's sk_zckey? This would return multiple completion IDs for one syscall, which might cause userspace to desynchronize its tracking and prematurely free buffers.

Quoted 15 lines
net/vmw_vsock/virtio_transport_common.c +11 -0
+				if (!uarg) {
+					virtio_transport_put_credit(vvs, pkt_len);
+					return -ENOMEM;
+				}
+
+				if (!can_zcopy)
+					uarg_to_msgzc(uarg)->zerocopy = 0;
+
+				have_uref = true;
+			}
+		}
 	}
 
 	rest_len = pkt_len;

net/vmw_vsock/virtio_transport_common.c static int virtio_transport_send_pkt_info(struct vsock_sock *vsk, · line 383

Quoted 2 lines
net/vmw_vsock/virtio_transport_common.c +0 -0
@@ -390,21 +382,7 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
 			break;
 		}

[Severity: Critical]
This isn't a bug introduced by this patch, but what happens to the user's iov_iter if virtio_transport_alloc_skb() (called just above this snippet) fails and the loop breaks?

If virtio_transport_alloc_skb() fails because __zerocopy_sg_from_iter() hits an error after successfully pinning some pages, the iter has already been advanced. Since rest_len != pkt_len, virtio_transport_send_pkt_info() returns the partial byte count without reverting msg_iter.

Will the next iteration of vsock_connectible_sendmsg() read from the advanced iter, permanently skipping the user bytes that were pinned in the failed skb and silently corrupting the stream?

Quoted 19 lines
net/vmw_vsock/virtio_transport_common.c +1 -15
-		/* We process buffer part by part, allocating skb on
-		 * each iteration. If this is last skb for this buffer
-		 * and MSG_ZEROCOPY mode is in use - we must allocate
-		 * completion for the current syscall.
-		 */
-		if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY &&
-		    skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) {
-			if (virtio_transport_init_zcopy_skb(vsk, skb,
-							    info->msg,
-							    can_zcopy)) {
-				kfree_skb(skb);
-				ret = -ENOMEM;
-				break;
-			}
-		}
+		skb_zcopy_set(skb, uarg, NULL);
 
 		virtio_transport_inc_tx_pkt(vvs, skb);
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Alexander Martyniuk
Quoted 5 lines

When a large message is fragmented into multiple skbs, the zerocopy uarg is only allocated and attached to the last skb in the loop. Non-final skbs carry pinned user pages with no completion tracking, so the kernel has no way to notify userspace when those pages are safe to reuse.

Queued for 6.12, thanks.

Signature · 2 lines
Thanks,
Sasha