Skip to contentSkip to conversation

vsock: preserve receive timeout across partial reads

PATCH New Ren Wei 1 participant 3 messages Nov 27

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
author
#2 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