Skip to contentSkip to conversation

vsock: preserve receive timeout across partial reads

PATCH In review Ren Wei 5 participants 10 messages Nov 27 – Dec 1

Subsystems (4)
lore
Share & tools
lore

Ren Wei

author
#0 lore raw

cover vsock: preserve receive timeout across partial reads

From: Ruide Cao <[email protected]>

Hi Linux kernel maintainers,

We found and validated an issue in net/vmw_vsock/af_vsock.c. The bug is reachable by an unprivileged local user who can connect to an AF_VSOCK SOCK_STREAM service (the reproducer used VMADDR_CID_LOCAL). We've tested it, and it should not affect any other functionality.

This bug is tracked at: https://bugtracker.nebusec.ai/f/13843

We will provide detailed information about the bug in this email.

---- details below ----

Bug details:

__vsock_stream_recvmsg() takes the SO_RCVTIMEO value once and passes it by value to vsock_connectible_wait_data(). The helper updates only its local copy from schedule_timeout() and does not return the remaining time. After a partial dequeue below the MSG_WAITALL or SO_RCVLOWAT target, the stream receive loop calls the helper again with the original full timeout.

A peer that drip-feeds data just inside each interval can therefore keep the receive blocked far beyond the configured timeout. This is a receive-timeout contract violation, not a memory-safety bug.

The first patch changes vsock_connectible_wait_data() to take the timeout by pointer so the remaining budget is preserved across partial reads. The seqpacket call site is updated the same way; it only waits once. The second patch adds a tools/testing/vsock regression for SOCK_STREAM recv(MSG_WAITALL) against a drip-feeding peer. The selftest keeps the data socket open until both sides confirm.

Reproducer:

As an unprivileged local user, create a connected AF_VSOCK SOCK_STREAM pair on VMADDR_CID_LOCAL. On the receiving end, set SO_RCVTIMEO to 1 second and issue recv(MSG_WAITALL) for 6 bytes. Have the peer send one byte every 0.8 seconds. On an unfixed kernel the recv returns 6 bytes after about 4.8 seconds instead of failing or returning partial data near the 1 second timeout.

We ran the PoC in a 2 GB RAM x86 QEMU environment.

------BEGIN PoC------

#define _GNU_SOURCE
#include <errno.h>
#include <linux/vm_sockets.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#define TOTAL_BYTES 6
#define SEND_INTERVAL_USEC 800000
#define RCV_TIMEOUT_SEC 1

struct test_ctx {
	int listener_fd;
	unsigned int port;
};

static void fatal(const char *what)

{
	perror(what);
	exit(1);
}

static double elapsed_seconds(const struct timespec *start,
const struct timespec *end)

{
	time_t sec = end->tv_sec - start->tv_sec;
	long nsec = end->tv_nsec - start->tv_nsec;

	if (nsec < 0) {
		sec--;
		nsec += 1000000000L;
	}

	return (double)sec + (double)nsec / 1000000000.0;
}

static void *sender_thread(void *arg)

{
	struct test_ctx *ctx = arg;
	struct sockaddr_vm peer = {
		.svm_family = AF_VSOCK,
	};
	socklen_t peer_len = sizeof(peer);
	int conn_fd;
	char ch = 'A';
	int i;

	conn_fd = accept(ctx->listener_fd, (struct sockaddr *)&peer, &peer_len);
	if (conn_fd < 0)
		fatal("accept");

	fprintf(stdout,
		"sender: accepted peer cid=%u port=%u, sending %d bytes every %.3f s\n",
		peer.svm_cid, peer.svm_port, TOTAL_BYTES,
		(double)SEND_INTERVAL_USEC / 1000000.0);
	fflush(stdout);

	for (i = 0; i < TOTAL_BYTES; i++) {
		ssize_t n;

		usleep(SEND_INTERVAL_USEC);
		n = send(conn_fd, &ch, 1, 0);
		if (n != 1)
			fatal("send");
		fprintf(stdout, "sender: sent byte %d at +%.3f s\n", i + 1,
			(double)(i + 1) * (double)SEND_INTERVAL_USEC / 1000000.0);
		fflush(stdout);
		ch++;
	}

	close(conn_fd);
	return NULL;
}

int main(void)

{
	struct test_ctx ctx = {
		.listener_fd = -1,
	};
	struct sockaddr_vm addr = {
		.svm_family = AF_VSOCK,
		.svm_cid = VMADDR_CID_LOCAL,
		.svm_port = VMADDR_PORT_ANY,
	};
	struct sockaddr_vm bound = {
		.svm_family = AF_VSOCK,
	};
	struct timeval timeout = {
		.tv_sec = RCV_TIMEOUT_SEC,
		.tv_usec = 0,
	};
	struct timespec start;
	struct timespec end;
	socklen_t bound_len = sizeof(bound);
	pthread_t tid;
	char buf[TOTAL_BYTES];
	int client_fd;
	ssize_t n;
	double secs;
	int i;

	ctx.listener_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
	if (ctx.listener_fd < 0)
		fatal("socket(listener)");

	if (bind(ctx.listener_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		fatal("bind(listener)");

	if (getsockname(ctx.listener_fd, (struct sockaddr *)&bound, &bound_len) < 0)
		fatal("getsockname(listener)");

	ctx.port = bound.svm_port;

	if (listen(ctx.listener_fd, 1) < 0)
		fatal("listen");

	fprintf(stdout, "listener: cid=%u port=%u\n", VMADDR_CID_LOCAL, ctx.port);
	fflush(stdout);

	if (pthread_create(&tid, NULL, sender_thread, &ctx) != 0)
		fatal("pthread_create");

	client_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
	if (client_fd < 0)
		fatal("socket(client)");

	if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
		       sizeof(timeout)) < 0)
		fatal("setsockopt(SO_RCVTIMEO)");

	addr.svm_port = ctx.port;
	if (connect(client_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
		fatal("connect");

	memset(buf, 0, sizeof(buf));
	if (clock_gettime(CLOCK_MONOTONIC, &start) != 0)
		fatal("clock_gettime(start)");

	n = recv(client_fd, buf, sizeof(buf), MSG_WAITALL);

	if (clock_gettime(CLOCK_MONOTONIC, &end) != 0)
		fatal("clock_gettime(end)");

	secs = elapsed_seconds(&start, &end);

	fprintf(stdout,
		"client: recv(MSG_WAITALL, %d) returned %zd after %.3f s (timeout=%d s)\n",
		TOTAL_BYTES, n, secs, RCV_TIMEOUT_SEC);
	if (n >= 0) {
		fprintf(stdout, "client: bytes received:");
		for (i = 0; i < n; i++)
			fprintf(stdout, " %c", buf[i]);
		fprintf(stdout, "\n");
	} else {
		fprintf(stdout, "client: errno=%d (%s)\n", errno, strerror(errno));
	}

	if (n == TOTAL_BYTES && secs > 4.0) {
		fprintf(stdout,
			"RESULT: bug observed. Finite SO_RCVTIMEO did not cap total wait time.\n");
	} else if (n > 0 && secs < 2.0) {
		fprintf(stdout,
			"RESULT: behavior looks fixed. Partial data returned near the original timeout.\n");
	} else {
		fprintf(stdout,
			"RESULT: inconclusive. Re-check timing, transport availability, or system load.\n");
	}

	fflush(stdout);

	close(client_fd);
	pthread_join(tid, NULL);
	close(ctx.listener_fd);
	return 0;
}

------END PoC--------

----BEGIN crash log----

Unprivileged user (test_user, CapEff=0), VMADDR_CID_LOCAL:

listener: cid=1 port=3381025544
sender: accepted peer cid=1 port=3381025545, sending 6 bytes every 0.800 s
sender: sent byte 1 at +0.800 s
sender: sent byte 2 at +1.600 s
sender: sent byte 3 at +2.400 s
sender: sent byte 4 at +3.200 s
sender: sent byte 5 at +4.000 s
sender: sent byte 6 at +4.800 s
client: recv(MSG_WAITALL, 6) returned 6 after 4.816 s (timeout=1 s)
client: bytes received: A B C D E F
RESULT: bug observed. Finite SO_RCVTIMEO did not cap total wait time.

-----END crash log-----

Best regards,
Ruide Cao

Ruide Cao (2):
vsock: preserve receive timeout across partial reads vsock/test: add stream MSG_WAITALL receive timeout test

 net/vmw_vsock/af_vsock.c         |  14 ++--
 tools/testing/vsock/vsock_test.c | 112 +++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+), 7 deletions(-)

base-commit: cc7fe6e741730fef549e484379281736d32746f0

Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 1/2 vsock: preserve receive timeout across partial reads

From: Ruide Cao <[email protected]>

__vsock_stream_recvmsg() passes its receive timeout by value to vsock_connectible_wait_data(). schedule_timeout() therefore updates only the helper's local copy. After a partial dequeue, the stream receive loop calls the helper again with the original timeout, allowing a peer to restart SO_RCVTIMEO with periodic short writes.

This was found with LLM assistance on the Vega platform and reproduced with the platform PoC in QEMU as an unprivileged user on VMADDR_CID_LOCAL. recv(MSG_WAITALL, 6) with SO_RCVTIMEO=1s returned 6 bytes after ~4.8s while the peer sent one byte every 0.8s:

client: recv(MSG_WAITALL, 6) returned 6 after 4.816 s (timeout=1 s)
RESULT: bug observed. Finite SO_RCVTIMEO did not cap total wait time.

Pass the timeout by reference so the remaining time is retained across partial reads. This makes the receive wait budget cumulative without changing the existing blocking behavior. The seqpacket call site is updated the same way; it only waits once, so the change is mechanical.

Fixes b3f7fd54881b ("af_vsock: separate wait data loop")
---
 net/vmw_vsock/af_vsock.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
net/vmw_vsock/af_vsock.c +7 -7
index 1d7d53542724..99542ac080b4 100644
@@ -2406,7 +2406,7 @@ static int vsock_connectible_sendmsg(struct socket *sock, struct msghdr *msg,
 
 static int vsock_connectible_wait_data(struct sock *sk,
 				       struct wait_queue_entry *wait,
-				       long timeout,
+				       long *timeout,
 				       struct vsock_transport_recv_notify_data *recv_data,
 				       size_t target)
 {
@@ -2432,7 +2432,7 @@ static int vsock_connectible_wait_data(struct sock *sk,
 		}
 
 		/* Don't wait for non-blocking sockets. */
-		if (timeout == 0) {
+		if (*timeout == 0) {
 			err = -EAGAIN;
 			break;
 		}
@@ -2444,13 +2444,13 @@ static int vsock_connectible_wait_data(struct sock *sk,
 		}
 
 		release_sock(sk);
-		timeout = schedule_timeout(timeout);
+		*timeout = schedule_timeout(*timeout);
 		lock_sock(sk);
 
 		if (signal_pending(current)) {
-			err = sock_intr_errno(timeout);
+			err = sock_intr_errno(*timeout);
 			break;
-		} else if (timeout == 0) {
+		} else if (*timeout == 0) {
 			err = -EAGAIN;
 			break;
 		}
@@ -2509,7 +2509,7 @@ static int __vsock_stream_recvmsg(struct sock *sk, struct msghdr *msg,
 	while (1) {
 		ssize_t read;
 
-		err = vsock_connectible_wait_data(sk, &wait, timeout,
+		err = vsock_connectible_wait_data(sk, &wait, &timeout,
 						  &recv_data, target);
 		if (err <= 0)
 			break;
@@ -2565,7 +2565,7 @@ static int __vsock_seqpacket_recvmsg(struct sock *sk, struct msghdr *msg,
 
 	timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
 
-	err = vsock_connectible_wait_data(sk, &wait, timeout, NULL, 0);
+	err = vsock_connectible_wait_data(sk, &wait, &timeout, NULL, 0);
 	if (err <= 0)
 		goto out;
 
Signature · 1 line
2.47.3
#2 lore raw

LGTM. this just restores what b3f7fd54881b dropped when it moved the schedule_timeout() assignment into a helper taking timeout by value.

maintainer Reviewed-by on v1 1/2
#3 lore raw
In reply to Ren Wei
Quoted 21 lines · Ren Wei

From: Ruide Cao <[email protected]>

__vsock_stream_recvmsg() passes its receive timeout by value to vsock_connectible_wait_data(). schedule_timeout() therefore updates only the helper's local copy. After a partial dequeue, the stream receive loop calls the helper again with the original timeout, allowing a peer to restart SO_RCVTIMEO with periodic short writes.

This was found with LLM assistance on the Vega platform and reproduced with the platform PoC in QEMU as an unprivileged user on VMADDR_CID_LOCAL. recv(MSG_WAITALL, 6) with SO_RCVTIMEO=1s returned 6 bytes after ~4.8s while the peer sent one byte every 0.8s:

client: recv(MSG_WAITALL, 6) returned 6 after 4.816 s (timeout=1 s)
RESULT: bug observed. Finite SO_RCVTIMEO did not cap total wait time.

Pass the timeout by reference so the remaining time is retained across partial reads. This makes the receive wait budget cumulative without changing the existing blocking behavior. The seqpacket call site is updated the same way; it only waits once, so the change is mechanical.

Quoted 8 lines
Fixes b3f7fd54881b ("af_vsock: separate wait data loop")
---
 net/vmw_vsock/af_vsock.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

LGTM, thanks!

author
#4 lore raw

PATCH 2/2 vsock/test: add stream MSG_WAITALL receive timeout test

In reply to Ren Wei

From: Ruide Cao <[email protected]>

test_seqpacket_timeout covers SOCK_SEQPACKET with SO_RCVTIMEO, but there is no SOCK_STREAM test for a cumulative receive timeout across partial reads.

Have the peer drip-feed one byte every 0.8s while the client issues recv(MSG_WAITALL) with a 1s SO_RCVTIMEO. Without the timeout being preserved, the wait budget is restarted after each byte and the recv completes after several intervals. With the fix, the call must return around the original timeout, either with EAGAIN or with a partial read shorter than the MSG_WAITALL request.

Keep the data socket open until both sides confirm: the client writes WAITDONE after recv returns, the server finishes sending all six bytes and acknowledges, and only then is the socket closed. Failure paths complete the same handshake so the server does not hang.

---
 tools/testing/vsock/vsock_test.c | 112 +++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
tools/testing/vsock/vsock_test.c +112 -0
index 10b9074fb87f..868792f7729c 100644
@@ -662,6 +662,113 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)
 	close(fd);
 }
 
+#define STREAM_RCVTIMEO_DRIP_BYTES	6
+#define STREAM_RCVTIMEO_DRIP_USEC	800000 /* 0.8s, inside the 1s timeout */
+
+/* Keep the data socket open until both sides confirm. The client
+ * writes WAITDONE when recv() returns, but must not close until the
+ * server has finished drip-feeding all 6 bytes and acknowledged;
+ * otherwise later sends hit EPIPE. Failure paths complete the same
+ * handshake so the server is not left in control_expectln("WAITDONE").
+ */
+static void test_stream_timeout_waitall_done(int fd)
+{
+	control_writeln("WAITDONE");
+	control_expectln("SRVDONE");
+	close(fd);
+}
+
+static void test_stream_timeout_waitall_client(const struct test_opts *opts)
+{
+	char buf[STREAM_RCVTIMEO_DRIP_BYTES];
+	time_t read_enter_ns;
+	struct timeval tv;
+	time_t read_ns;
+	int failed = 0;
+	ssize_t n;
+	int fd;
+
+	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
+	if (fd < 0) {
+		perror("connect");
+		exit(EXIT_FAILURE);
+	}
+
+	tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
+	tv.tv_usec = 0;
+
+	setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
+				 "setsockopt(SO_RCVTIMEO)");
+
+	/* Enter recv() before the peer starts drip-feeding, so the first
+	 * byte cannot already be queued.
+	 */
+	control_writeln("CLNSENT");
+
+	read_enter_ns = current_nsec();
+	n = recv(fd, buf, sizeof(buf), MSG_WAITALL);
+	read_ns = current_nsec() - read_enter_ns;
+
+	/* Unfixed kernels restart the full SO_RCVTIMEO after each partial
+	 * read, so this recv() returns 6 bytes after ~4.8s. A cumulative
+	 * 1s timeout returns much sooner; use 2s as the cap so the two
+	 * outcomes cannot be confused.
+	 */
+	if (read_ns >= 2ULL * NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) exceeded SO_RCVTIMEO, %lu ns\n",
+			read_ns);
+		failed = 1;
+	}
+
+	if (n == sizeof(buf)) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) completed under drip-feed\n");
+		failed = 1;
+	}
+
+	if (n < 0 && errno != EAGAIN) {
+		perror("EAGAIN expected");
+		failed = 1;
+	}
+
+	if (n == 0) {
+		fprintf(stderr, "unexpected EOF on recv(MSG_WAITALL)\n");
+		failed = 1;
+	}
+
+	test_stream_timeout_waitall_done(fd);
+	if (failed)
+		exit(EXIT_FAILURE);
+}
+
+static void test_stream_timeout_waitall_server(const struct test_opts *opts)
+{
+	int fd;
+	int i;
+
+	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
+	if (fd < 0) {
+		perror("accept");
+		exit(EXIT_FAILURE);
+	}
+
+	control_expectln("CLNSENT");
+
+	/* Drip-feed all 6 bytes even after the client times out, so an
+	 * unfixed kernel still collects a full 6-byte recv and fails.
+	 * The client keeps the data socket open until SRVDONE.
+	 */
+	for (i = 0; i < STREAM_RCVTIMEO_DRIP_BYTES; i++) {
+		timeout_usleep(STREAM_RCVTIMEO_DRIP_USEC);
+		send_byte(fd, 1, 0);
+	}
+
+	control_expectln("WAITDONE");
+	control_writeln("SRVDONE");
+	close(fd);
+}
+
 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
 {
 	unsigned long long sock_buf_size;
@@ -2739,6 +2846,11 @@ static struct test_case test_cases[] = {
 		.run_client = test_seqpacket_timeout_client,
 		.run_server = test_seqpacket_timeout_server,
 	},
+	{
+		.name = "SOCK_STREAM MSG_WAITALL timeout",
+		.run_client = test_stream_timeout_waitall_client,
+		.run_server = test_stream_timeout_waitall_server,
+	},
 	{
 		.name = "SOCK_SEQPACKET invalid receive buffer",
 		.run_client = test_seqpacket_invalid_rec_buffer_client,
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH net 2/2] vsock/test: add stream MSG_WAITALL receive timeout test

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] vsock/test: inserting test in test_cases[] shifts IDs and breaks vmtest.sh --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] vsock/test: inserting test in test_cases[] shifts IDs and breaks vmtest.sh
--

commit 3fdae506954ede6076a29bc4c547728169cd474c
Author: Ruide Cao <[email protected]>

vsock/test: add stream MSG_WAITALL receive timeout test

This commit adds a SOCK_STREAM receive timeout test that verifies recv(MSG_WAITALL) enforces a cumulative SO_RCVTIMEO timeout across partial drip-fed reads.

Quoted 4 lines
tools/testing/vsock/vsock_test.c +0 -0
index 10b9074fb87f7..868792f7729cf 100644

[ ... ]

tools/testing/vsock/vsock_test.c static struct test_case test_cases[] = { · line 2856

Quoted 12 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2739,6 +2846,11 @@ static struct test_case test_cases[] = {
 		.run_client = test_seqpacket_timeout_client,
 		.run_server = test_seqpacket_timeout_server,
 	},
+	{
+		.name = "SOCK_STREAM MSG_WAITALL timeout",
+		.run_client = test_stream_timeout_waitall_client,
+		.run_server = test_stream_timeout_waitall_server,
+	},
 	{
 		.name = "SOCK_SEQPACKET invalid receive buffer",
 		.run_client = test_seqpacket_invalid_rec_buffer_client,

[Severity: Medium]
Does inserting this entry in the middle of test_cases[] shift the numeric test IDs and break tools/testing/selftests/vsock/vmtest.sh?

test_ns_same_local_host_connect_to_local_vm_ok() in vmtest.sh passes --skip=29 to skip "SOCK_STREAM transport release use-after-free":

tools/testing/selftests/vsock/vmtest.sh:test_ns_same_local_host_connect_to_local_vm_ok() {
...
# virtio-vsock (G2H) does not support local namespaces, so skip test 29 # (SOCK_STREAM transport release use-after-free) which requires a
# local listener in the same namespace.
host_vsock_test "${ns}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" --skip=29
...
}

Inserting the new test here shifts that test from index 29 to 30, so --skip=29 skips "SOCK_STREAM MSG_ZEROCOPY leak completion skb" instead and runs the incompatible test. Appending the new entry at the end of test_cases[] keeps the existing test IDs stable.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH net 2/2] vsock/test: add stream MSG_WAITALL receive timeout test

In reply to Ren Wei
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3

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

- [Medium] The new entry "SOCK_STREAM MSG_WAITALL timeout" is inserted in the middle of test_cases[] in tools/testing/vsock/vsock_test.c (lines…
- [Low] test_stream_timeout_waitall_client() (tools/testing/vsock/vsock_test.c:717-738) only checks that the…
- [Low] In test_stream_timeout_waitall_client() (tools/testing/vsock/vsock_test.c:709-717), read_ns = current_nsec() -…
- [Low] In test_stream_timeout_waitall_client() (tools/testing/vsock/vsock_test.c:709-731), errno from recv() is not…

--

vsock/test: add stream MSG_WAITALL receive timeout test

This adds a SOCK_STREAM test to vsock_test. The client calls recv(MSG_WAITALL) with a 1s SO_RCVTIMEO while the peer sends one byte every 0.8s, and the test checks that the timeout is cumulative. The client expects recv() to return in under 2s, either with EAGAIN or with a partial read.

Quoted 5 lines
tools/testing/vsock/vsock_test.c +0 -0
index 10b9074fb87f7..868792f7729cf 100644
@@ -662,6 +662,113 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)

[ ... ]

Quoted 6 lines
tools/testing/vsock/vsock_test.c +6 -0
+static void test_stream_timeout_waitall_client(const struct test_opts *opts)
+{
+	char buf[STREAM_RCVTIMEO_DRIP_BYTES];
+	time_t read_enter_ns;
+	struct timeval tv;
+	time_t read_ns;

[ ... ]

tools/testing/vsock/vsock_test.c static void test_seqpacket_timeout_server(const struct test_opts *opts) · line 717

Quoted 10 lines
tools/testing/vsock/vsock_test.c +10 -0
+	read_enter_ns = current_nsec();
+	n = recv(fd, buf, sizeof(buf), MSG_WAITALL);
+	read_ns = current_nsec() - read_enter_ns;
+
+	/* Unfixed kernels restart the full SO_RCVTIMEO after each partial
+	 * read, so this recv() returns 6 bytes after ~4.8s. A cumulative
+	 * 1s timeout returns much sooner; use 2s as the cap so the two
+	 * outcomes cannot be confused.
+	 */
+	if (read_ns >= 2ULL * NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC) {

[Severity: Low]
The commit message says:

With the fix, the call must return around the original timeout, either with EAGAIN or with a partial read shorter than the MSG_WAITALL request.

This is the only timing check, and it only sets an upper bound. Would a kernel that ignores MSG_WAITALL and returns 1 byte at about 0.8s also pass?

The same question applies to a kernel that sets the remaining timeout to 0 after the first partial dequeue, and to one that returns EAGAIN at once without waiting. In both cases this is the bookkeeping in question:

net/vmw_vsock/af_vsock.c:vsock_connectible_wait_data() {
    ...
	*timeout = schedule_timeout(*timeout);
    ...
}

With the companion fix ("vsock: preserve receive timeout across partial reads"), recv() should return 1 byte after about 1s.

Should the test also check for a lower bound near RCVTIMEO_TIMEOUT_SEC, and that recv() returned the single byte?

[Severity: Low]
read_ns is a signed time_t. The 2ULL makes the right-hand side unsigned long long, so read_ns is converted to unsigned for this comparison.

current_nsec() reads the wall clock:

	if (clock_gettime(CLOCK_REALTIME, &ts)) {

Suppose CLOCK_REALTIME steps backwards while recv() is blocked, for example on an NTP step or a guest clock resync after a VM pause. read_ns would then be negative and compare as roughly 1.8e19.

Can the test then print "recv(MSG_WAITALL) exceeded SO_RCVTIMEO" and fail even though recv() returned on time?

test_seqpacket_timeout_client() compares against an int constant, so a backwards step there makes the check pass instead of fail.

Would a signed comparison, or measuring with CLOCK_MONOTONIC, avoid this?

Quoted 4 lines
tools/testing/vsock/vsock_test.c +4 -0
+		fprintf(stderr,
+			"recv(MSG_WAITALL) exceeded SO_RCVTIMEO, %lu ns\n",
+			read_ns);
+		failed = 1;

tools/testing/vsock/vsock_test.c static void test_seqpacket_timeout_server(const struct test_opts *opts) · line 731

Quoted 10 lines
tools/testing/vsock/vsock_test.c +10 -0
+	}
+
+	if (n == sizeof(buf)) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) completed under drip-feed\n");
+		failed = 1;
+	}
+
+	if (n < 0 && errno != EAGAIN) {
+		perror("EAGAIN expected");

[Severity: Low]
errno is not saved right after recv(). It is checked only after current_nsec() runs and, if the elapsed-time check fired, after fprintf(). Both calls are allowed to change errno even when they succeed.

Could this check and perror() then report an errno that did not come from recv()?

This only affects the diagnostic messages, not the pass/fail result. test_seqpacket_timeout_client() checks errno right after read(). Would it be cleaner to save errno in a local variable right after recv() here?

Quoted 2 lines
tools/testing/vsock/vsock_test.c +2 -0
+		failed = 1;
+	}

[ ... ]

tools/testing/vsock/vsock_test.c static struct test_case test_cases[] = { · line 2853

Quoted 9 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2739,6 +2846,11 @@ static struct test_case test_cases[] = {
 		.run_client = test_seqpacket_timeout_client,
 		.run_server = test_seqpacket_timeout_server,
 	},
+	{
+		.name = "SOCK_STREAM MSG_WAITALL timeout",
+		.run_client = test_stream_timeout_waitall_client,
+		.run_server = test_stream_timeout_waitall_server,
+	},

[Severity: Medium]
The new entry goes into the middle of test_cases[], right after "SOCK_SEQPACKET timeout", so every later test's ID goes up by one. vsock_test selects tests only by array index: parse_test_id() feeds skip_test() and pick_test().

test_ns_same_local_host_connect_to_local_vm_ok() in tools/testing/selftests/vsock/vmtest.sh hard-codes one of these IDs:

# Skip test 29 (transport release use-after-free): This test attempts # binding both G2H and H2G CIDs. Because virtio-vsock (G2H) doesn't # support local namespaces the test will fail when
# transport_g2h->stream_allow() returns false. This edge case only # happens for vsock_test in client mode on the host in a local
# namespace. This is a false positive.
host_vsock_test "${ns}" "127.0.0.1" "${VSOCK_CID}" "${TEST_HOST_PORT}" --skip=29

After this patch, index 29 is "SOCK_STREAM MSG_ZEROCOPY leak completion skb". "SOCK_STREAM transport release use-after-free" is now index 30.

Does this make that vmtest.sh case skip the zerocopy completion skb leak test? It would also run the use-after-free test, which gives the false failure the skip was added to avoid.

External scripts that pass --skip or --pick IDs would be off by one for every test after index 8. So would host and guest binaries built from different trees.

Could the new test be appended at the end of test_cases[] instead, or could vmtest.sh be updated in the same patch?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795587333.git.caoruide123%40gmail.com
#7 lore raw

Re: [PATCH net 2/2] vsock/test: add stream MSG_WAITALL receive timeout test

In reply to Ren Wei

tools/testing/vsock/vsock_test.c static void test_seqpacket_timeout_server(const struct test_opts *opts) · line 717

Quoted 1 line
tools/testing/vsock/vsock_test.c +1 -0
+	if (read_ns >= 2ULL * NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC) {

What if the sender gets delayed?

800ms is the requested sleep, not a guarantee that the sender gets scheduled in 800ms.

Say the first byte arrives at 0.8s and the second gets delayed by 300ms. The broken kernel times out at 1.8s with one byte read, and the test happily passes :(

#8 lore raw

Re: [PATCH net 2/2] vsock/test: add stream MSG_WAITALL receive timeout test

In reply to Ren Wei
Quoted 32 lines · Ren Wei

From: Ruide Cao <[email protected]>

test_seqpacket_timeout covers SOCK_SEQPACKET with SO_RCVTIMEO, but there is no SOCK_STREAM test for a cumulative receive timeout across partial reads.

Have the peer drip-feed one byte every 0.8s while the client issues recv(MSG_WAITALL) with a 1s SO_RCVTIMEO. Without the timeout being preserved, the wait budget is restarted after each byte and the recv completes after several intervals. With the fix, the call must return around the original timeout, either with EAGAIN or with a partial read shorter than the MSG_WAITALL request.

Keep the data socket open until both sides confirm: the client writes WAITDONE after recv returns, the server finishes sending all six bytes and acknowledges, and only then is the socket closed. Failure paths complete the same handshake so the server does not hang.

---
 tools/testing/vsock/vsock_test.c | 112 +++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
tools/testing/vsock/vsock_test.c +0 -0
index 10b9074fb87f..868792f7729c 100644
@@ -662,6 +662,113 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)
 	close(fd);
 }
 

tools/testing/vsock/vsock_test.c static void test_seqpacket_timeout_server(const struct test_opts *opts) · line 674

Quoted 10 lines
tools/testing/vsock/vsock_test.c +10 -0
+#define STREAM_RCVTIMEO_DRIP_BYTES	6
+#define STREAM_RCVTIMEO_DRIP_USEC	800000 /* 0.8s, inside the 1s timeout */
+
+/* Keep the data socket open until both sides confirm. The client
+ * writes WAITDONE when recv() returns, but must not close until the
+ * server has finished drip-feeding all 6 bytes and acknowledged;
+ * otherwise later sends hit EPIPE. Failure paths complete the same
+ * handshake so the server is not left in control_expectln("WAITDONE").
+ */
+static void test_stream_timeout_waitall_done(int fd)

why we need this function? Can we inline those since this is called in a single point.

tools/testing/vsock/vsock_test.c static void test_seqpacket_timeout_server(const struct test_opts *opts) · line 676

Quoted 2 lines
tools/testing/vsock/vsock_test.c +2 -0
+{
+	control_writeln("WAITDONE");

Why we need this sync point (i.e. WAITDONE)?

Quoted 99 lines
tools/testing/vsock/vsock_test.c +95 -0
+	control_expectln("SRVDONE");
+	close(fd);
+}
+
+static void test_stream_timeout_waitall_client(const struct test_opts *opts)
+{
+	char buf[STREAM_RCVTIMEO_DRIP_BYTES];
+	time_t read_enter_ns;
+	struct timeval tv;
+	time_t read_ns;
+	int failed = 0;
+	ssize_t n;
+	int fd;
+
+	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
+	if (fd < 0) {
+		perror("connect");
+		exit(EXIT_FAILURE);
+	}
+
+	tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
+	tv.tv_usec = 0;
+
+	setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
+				 "setsockopt(SO_RCVTIMEO)");
+
+	/* Enter recv() before the peer starts drip-feeding, so the first
+	 * byte cannot already be queued.
+	 */
+	control_writeln("CLNSENT");
+
+	read_enter_ns = current_nsec();
+	n = recv(fd, buf, sizeof(buf), MSG_WAITALL);
+	read_ns = current_nsec() - read_enter_ns;
+
+	/* Unfixed kernels restart the full SO_RCVTIMEO after each partial
+	 * read, so this recv() returns 6 bytes after ~4.8s. A cumulative
+	 * 1s timeout returns much sooner; use 2s as the cap so the two
+	 * outcomes cannot be confused.
+	 */
+	if (read_ns >= 2ULL * NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) exceeded SO_RCVTIMEO, %lu ns\n",
+			read_ns);
+		failed = 1;
+	}
+
+	if (n == sizeof(buf)) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) completed under drip-feed\n");
+		failed = 1;
+	}
+
+	if (n < 0 && errno != EAGAIN) {
+		perror("EAGAIN expected");
+		failed = 1;
+	}
+
+	if (n == 0) {
+		fprintf(stderr, "unexpected EOF on recv(MSG_WAITALL)\n");
+		failed = 1;
+	}
+
+	test_stream_timeout_waitall_done(fd);
+	if (failed)
+		exit(EXIT_FAILURE);
+}
+
+static void test_stream_timeout_waitall_server(const struct test_opts *opts)
+{
+	int fd;
+	int i;
+
+	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
+	if (fd < 0) {
+		perror("accept");
+		exit(EXIT_FAILURE);
+	}
+
+	control_expectln("CLNSENT");
+
+	/* Drip-feed all 6 bytes even after the client times out, so an
+	 * unfixed kernel still collects a full 6-byte recv and fails.
+	 * The client keeps the data socket open until SRVDONE.
+	 */
+	for (i = 0; i < STREAM_RCVTIMEO_DRIP_BYTES; i++) {
+		timeout_usleep(STREAM_RCVTIMEO_DRIP_USEC);
+		send_byte(fd, 1, 0);
+	}
+
+	control_expectln("WAITDONE");
+	control_writeln("SRVDONE");
+	close(fd);
+}
+
 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
 {
 	unsigned long long sock_buf_size;

tools/testing/vsock/vsock_test.c static struct test_case test_cases[] = { · line 2853

Quoted 8 lines
tools/testing/vsock/vsock_test.c +5 -0
@@ -2739,6 +2846,11 @@ static struct test_case test_cases[] = {
 		.run_client = test_seqpacket_timeout_client,
 		.run_server = test_seqpacket_timeout_server,
 	},
+	{
+		.name = "SOCK_STREAM MSG_WAITALL timeout",
+		.run_client = test_stream_timeout_waitall_client,
+		.run_server = test_stream_timeout_waitall_server,
+	},

Please add new test at the bottom, we used id to skip them, so we should not put tests in the middle (I need to add a comment about that in the code).

Thanks,
Stefano

Quoted 6 lines
tools/testing/vsock/vsock_test.c +0 -0
 	{
 		.name = "SOCK_SEQPACKET invalid receive buffer",
 		.run_client = test_seqpacket_invalid_rec_buffer_client,
Signature · 1 line
2.47.3
#9 lore raw
In reply to Ren Wei
Sashiko has finished reviewing this 2-patch series:

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("vsock/test: add stream MSG_WAITALL receive timeout test"): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]