vsock: preserve receive timeout across partial reads
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest2 patches 0 repliesNew
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | vsock: preserve receive timeout across partial readsnet/ | +7 | 0 replies | |
| 2/2 | vsock/test: add stream MSG_WAITALL receive timeout testtools/ | +112 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH net 0/2] 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