libvhost-user: vhost-user-blk backend crashes after SET_LOG_BASE if mmap() of the dirty log fails
Host environment
-
Operating system: Ubuntu 22.04.5 LTS
-
OS/kernel version: Linux 5.15.0-198-generic
-
Architecture: x86_64
-
QEMU flavor: qemu-storage-daemon (
vhost-user-blkexport) -
QEMU version: 11.1.50, git master 56afd82b. Two builds:
--enable-asan --target-list=x86_64-softmmu --disable-docs --disable-werror, and the same without--enable-asan -
QEMU command line:
qemu-storage-daemon \ --blockdev null-co,node-name=disk0,size=67108864,read-zeroes=on \ --export vhost-user-blk,id=exp0,node-name=disk0,addr.type=unix,addr.path=/tmp/vhost-user-blk.sockA small vhost-user frontend that is included in the reproducer connects to the socket in place of QEMU.
Emulated/Virtualized environment
- Operating system: none (no guest). The frontend in the reproducer stands in for QEMU's vhost-user-blk frontend and writes the virtio-blk request into its own memory (see "Steps to reproduce").
- OS/kernel version: n/a (no guest)
- Architecture: n/a (no guest)
Description of problem
If mmap() of the dirty log passed with VHOST_USER_SET_LOG_BASE fails, libvhost-user stores MAP_FAILED as the log table and replies as if the call had succeeded. The next request that completes while VHOST_F_LOG_ALL is set marks its pages dirty through (uint8_t *)-1, and qemu-storage-daemon is killed by SIGSEGV. The daemon prints only:
log mmap error: No such deviceThe ASan build reports (full report under "Additional information"):
==3847267==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000001f (pc 0x5586c80c9ec6 bp 0x515000001c68 sp 0x7f2f55409db0 T0)
==3847267==The signal is caused by a WRITE memory access.
#0 0x5586c80c9ec6 in vu_log_page ../subprojects/libvhost-user/libvhost-user.c:725
#1 0x5586c80c9ec6 in vu_log_write ../subprojects/libvhost-user/libvhost-user.c:742
#3 0x5586c80caec0 in vu_log_queue_fill ../subprojects/libvhost-user/libvhost-user.c:3235
#6 0x5586c80cdfe8 in vu_queue_push ../subprojects/libvhost-user/libvhost-user.c:3300
#7 0x5586c7d6e249 in vu_blk_req_complete ../block/export/vhost-user-blk-server.c:48
SUMMARY: AddressSanitizer: SEGV ../subprojects/libvhost-user/libvhost-user.c:725 in vu_log_pageCode path (master 56afd82b, subprojects/libvhost-user/libvhost-user.c):
vu_set_log_base_exec()maps the log fd (:1174-1175). If that fails, it only callsperror()(:1177-1179). It then unmaps the previous log, if any, stores the result (MAP_FAILED) indev->log_table(:1181-1185), and sends the normal reply (:1187-1190).vu_log_write()skips logging only whendev->log_tableis NULL (:733-736).MAP_FAILEDis(void *)-1, so logging goes ahead.- When a request completes,
vu_queue_push()->vu_queue_fill()->vu_log_queue_fill()logs each buffer the device wrote (:3235), andvu_log_page()writes to&log_table[page / 8](:725), which is just above address 0. In the reproducer the data buffer is at guest address 0x104000, so the write goes to(uint8_t *)-1 + 32= 0x1f.
How it is reached: libvhost-user advertises VHOST_USER_PROTOCOL_F_LOG_SHMFD by default (:1791; the reproducer prints the negotiated bits). QEMU therefore sends SET_LOG_BASE with a log fd whenever it starts dirty logging for migration. If the backend cannot map that fd, every completed request crashes it from then on. The reproducer forces the failure by passing an fd that cannot be mapped (an eventfd). With the memfd that QEMU sends, the failure would have to come from the backend side (for example, ENOMEM); I did not try that. The frontend cannot detect the failure, because the reply carries no status and vhost_user_set_log_base() in QEMU only checks the reply's request type (hw/virtio/vhost-user.c:565-577). If a log was already mapped (for example, when QEMU resizes the log), that working log is unmapped and replaced by MAP_FAILED.
I am filing this as a normal bug rather than a security issue. The trigger needs control of the vhost-user frontend, which is normally QEMU itself, or an mmap() failure inside the backend. A guest cannot choose the log fd or its size, and docs/system/security.rst does not treat the boundary between QEMU and a vhost-user backend as a security boundary.
Results with the reproducer below (master 56afd82b):
| Build | Runs | Result |
|---|---|---|
--enable-asan |
3 | ASan SEGV in vu_log_page (libvhost-user.c:725), daemon exits with status 1, in 3 of 3 |
| without ASan | 3 | daemon killed by SIGSEGV (exit status 139) in 3 of 3 |
Steps to reproduce
- Build qemu-storage-daemon from the commit above.
- Run the script below as an ordinary user:
QSD=./build/storage-daemon/qemu-storage-daemon ./repro.sh. It needs gcc, but no root, no guest, and no kernel modules. - The script exports a null-co disk on a vhost-user-blk socket, then builds and runs a small frontend. The frontend negotiates features as QEMU does, starts one virtqueue, enables dirty logging with
SET_FEATURES(VHOST_F_LOG_ALL) andSET_LOG_BASE(passing an eventfd as the log fd), and submits one READ. The script then reports whether the daemon died.
The result is deterministic: the first request completed after the failed SET_LOG_BASE crashes the daemon.
repro.sh
#!/bin/bash
# Reproducer: qemu-storage-daemon (vhost-user-blk export) crashes when the
# backend cannot mmap() the dirty log passed with VHOST_USER_SET_LOG_BASE.
#
# Needs: gcc and a qemu-storage-daemon build. No root and no guest.
# Usage: QSD=./build/storage-daemon/qemu-storage-daemon ./repro209.sh
#
# The small vhost-user client below acts as the frontend, in the same order
# QEMU uses for a vhost-user-blk device: it negotiates features, starts one
# virtqueue, and then does what QEMU does when migration starts (SET_FEATURES
# with VHOST_F_LOG_ALL, then SET_LOG_BASE). It passes an eventfd as the log
# fd, so the backend's mmap() fails. It then submits one READ.
set -u
QSD=${QSD:-./build/storage-daemon/qemu-storage-daemon}
WORK=$(mktemp -d)
SOCK=$WORK/vhost-user-blk.sock
echo "[*] work dir: $WORK"
cat > "$WORK/vhu_logbase.c" <<'EOF'
/* Minimal vhost-user frontend: start one virtqueue, turn on dirty logging
* with a log fd the backend cannot mmap(), then complete one READ. */
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/virtio_blk.h>
#include <linux/virtio_ring.h>
enum {
GET_FEATURES = 1, SET_FEATURES = 2, SET_OWNER = 3, SET_MEM_TABLE = 5,
SET_LOG_BASE = 6, SET_VRING_NUM = 8, SET_VRING_ADDR = 9,
SET_VRING_BASE = 10, SET_VRING_KICK = 12, SET_VRING_CALL = 13,
GET_PROTOCOL_FEATURES = 15, SET_PROTOCOL_FEATURES = 16,
SET_VRING_ENABLE = 18,
};
#define F_LOG_ALL 26
#define F_PROTOCOL_FEATURES 30
#define F_VERSION_1 32
#define PF_LOG_SHMFD 1
#define GPA 0x100000ULL /* one 1 MiB RAM region; GPA == frontend VA */
#define RAMSIZE 0x100000ULL
struct hdr {
uint32_t request, flags, size;
};
static int sock;
static void send_msg(uint32_t request, const void *payload, uint32_t size,
int fd)
{
char buf[256];
struct hdr h = { request, 1 /* version */, size };
struct iovec iov = { buf, sizeof(h) + size };
char cbuf[CMSG_SPACE(sizeof(int))] = { 0 };
struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
memcpy(buf, &h, sizeof(h));
if (size) {
memcpy(buf + sizeof(h), payload, size);
}
if (fd >= 0) {
struct cmsghdr *c;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
c = CMSG_FIRSTHDR(&msg);
c->cmsg_level = SOL_SOCKET;
c->cmsg_type = SCM_RIGHTS;
c->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(c), &fd, sizeof(int));
}
if (sendmsg(sock, &msg, 0) < 0) {
perror("sendmsg");
exit(1);
}
}
static void send_u64(uint32_t request, uint64_t v, int fd)
{
send_msg(request, &v, sizeof(v), fd);
}
static uint64_t recv_u64(void)
{
struct hdr h;
uint64_t v;
if (recv(sock, &h, sizeof(h), MSG_WAITALL) != sizeof(h) ||
h.size != sizeof(v) ||
recv(sock, &v, sizeof(v), MSG_WAITALL) != sizeof(v)) {
fprintf(stderr, "bad reply\n");
exit(1);
}
return v;
}
int main(int argc, char **argv)
{
struct sockaddr_un addr = { .sun_family = AF_UNIX };
if (argc != 2) {
fprintf(stderr, "usage: %s SOCKET\n", argv[0]);
return 2;
}
strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path) - 1);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr))) {
perror("connect");
return 1;
}
/* Feature negotiation, as QEMU's vhost-user frontend does it. */
send_msg(GET_FEATURES, NULL, 0, -1);
uint64_t features = recv_u64();
send_msg(GET_PROTOCOL_FEATURES, NULL, 0, -1);
uint64_t pfeatures = recv_u64();
printf("[fe] backend offers VHOST_F_LOG_ALL: %s, "
"VHOST_USER_PROTOCOL_F_LOG_SHMFD: %s\n",
features & (1ULL << F_LOG_ALL) ? "yes" : "no",
pfeatures & (1ULL << PF_LOG_SHMFD) ? "yes" : "no");
send_u64(SET_PROTOCOL_FEATURES, 1ULL << PF_LOG_SHMFD, -1);
send_msg(SET_OWNER, NULL, 0, -1);
uint64_t base = (1ULL << F_VERSION_1) | (1ULL << F_PROTOCOL_FEATURES);
send_u64(SET_FEATURES, base, -1);
/* Guest RAM and one virtqueue: desc @0, avail @0x1000, used @0x2000. */
int memfd = memfd_create("guest-ram", 0);
if (memfd < 0 || ftruncate(memfd, RAMSIZE)) {
perror("memfd");
return 1;
}
char *ram = mmap(NULL, RAMSIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
memfd, 0);
if (ram == MAP_FAILED) {
perror("mmap");
return 1;
}
struct { uint32_t nregions, padding; uint64_t gpa, size, uaddr, offset; }
mem = { 1, 0, GPA, RAMSIZE, GPA, 0 };
send_msg(SET_MEM_TABLE, &mem, sizeof(mem), memfd);
struct { uint32_t index, num; } state = { 0, 16 };
send_msg(SET_VRING_NUM, &state, sizeof(state), -1);
state.num = 0;
send_msg(SET_VRING_BASE, &state, sizeof(state), -1);
struct { uint32_t index, flags; uint64_t desc, used, avail, log; }
vaddr = { 0, 0, GPA, GPA + 0x2000, GPA + 0x1000, 0 };
send_msg(SET_VRING_ADDR, &vaddr, sizeof(vaddr), -1);
int kick = eventfd(0, 0);
send_u64(SET_VRING_CALL, 0, eventfd(0, 0));
send_u64(SET_VRING_KICK, 0, kick);
state.num = 1;
send_msg(SET_VRING_ENABLE, &state, sizeof(state), -1);
/* What QEMU does when migration starts, except that the log fd is an
* eventfd instead of a memfd, so the backend's mmap() fails. */
send_u64(SET_FEATURES, base | (1ULL << F_LOG_ALL), -1);
struct { uint64_t size, offset; } log = { 4096, 0 };
send_msg(SET_LOG_BASE, &log, sizeof(log), eventfd(0, 0));
recv_u64();
printf("[fe] SET_LOG_BASE with an unmappable log fd: backend replied\n");
/* One READ: header, 4 KiB data buffer, status byte. */
struct vring_desc *desc = (void *)ram;
struct vring_avail *avail = (void *)(ram + 0x1000);
struct virtio_blk_outhdr *req = (void *)(ram + 0x3000);
req->type = VIRTIO_BLK_T_IN;
req->sector = 0;
desc[0] = (struct vring_desc){ GPA + 0x3000, sizeof(*req), VRING_DESC_F_NEXT, 1 };
desc[1] = (struct vring_desc){ GPA + 0x4000, 4096,
VRING_DESC_F_NEXT | VRING_DESC_F_WRITE, 2 };
desc[2] = (struct vring_desc){ GPA + 0x5000, 1, VRING_DESC_F_WRITE, 0 };
avail->ring[0] = 0;
__sync_synchronize();
avail->idx = 1;
uint64_t one = 1;
if (write(kick, &one, sizeof(one)) != sizeof(one)) {
perror("kick");
return 1;
}
printf("[fe] READ submitted\n");
sleep(1);
char c;
printf("[fe] connection %s\n",
recv(sock, &c, 1, MSG_DONTWAIT) == 0 ? "closed by the backend"
: "still open");
return 0;
}
EOF
gcc -O1 -Wall -o "$WORK/vhu_logbase" "$WORK/vhu_logbase.c" || exit 1
ASAN_OPTIONS=detect_leaks=0 "$QSD" \
--blockdev null-co,node-name=disk0,size=67108864,read-zeroes=on \
--export vhost-user-blk,id=exp0,node-name=disk0,addr.type=unix,addr.path="$SOCK" \
> "$WORK/qsd.log" 2>&1 &
QSD_PID=$!
trap 'kill $QSD_PID 2>/dev/null' EXIT
for _ in $(seq 50); do [ -S "$SOCK" ] && break; sleep 0.1; done
[ -S "$SOCK" ] || { echo "no $SOCK"; cat "$WORK/qsd.log"; exit 1; }
"$WORK/vhu_logbase" "$SOCK"
sleep 1
echo "==== result ===="
if kill -0 "$QSD_PID" 2>/dev/null; then
echo "=> qemu-storage-daemon still running (not reproduced)"
else
wait "$QSD_PID"
echo "qemu-storage-daemon exit status: $? (139 = SIGSEGV; ASan builds exit with 1)"
grep -h -E "log mmap error|ERROR: AddressSanitizer|SUMMARY" "$WORK/qsd.log"
grep -q "log mmap error" "$WORK/qsd.log" && echo "=> REPRODUCED"
fi
echo "[*] logs in $WORK"Additional information
-
Script output from one run (build without ASan):
[*] work dir: /tmp/tmp.PoSP244mXP [fe] backend offers VHOST_F_LOG_ALL: yes, VHOST_USER_PROTOCOL_F_LOG_SHMFD: yes [fe] SET_LOG_BASE with an unmappable log fd: backend replied [fe] READ submitted [fe] connection closed by the backend /home/cc/workspace/vmm-review/repro/qemu/reports/repro_report.sh: line 211: 3847394 Segmentation fault (core dumped) ASAN_OPTIONS=detect_leaks=0 "$QSD" --blockdev null-co,node-name=disk0,size=67108864,read-zeroes=on --export vhost-user-blk,id=exp0,node-name=disk0,addr.type=unix,addr.path="$SOCK" > "$WORK/qsd.log" 2>&1 ==== result ==== qemu-storage-daemon exit status: 139 (139 = SIGSEGV; ASan builds exit with 1) log mmap error: No such device => REPRODUCED [*] logs in /tmp/tmp.PoSP244mXP -
Full ASan report (ASan build, same reproducer):
ASan report
==3847267==WARNING: ASan doesn't fully support makecontext/swapcontext functions and may produce false positives in some cases! log mmap error: No such device AddressSanitizer:DEADLYSIGNAL ================================================================= ==3847267==ERROR: AddressSanitizer: SEGV on unknown address 0x00000000001f (pc 0x5586c80c9ec6 bp 0x515000001c68 sp 0x7f2f55409db0 T0) ==3847267==The signal is caused by a WRITE memory access. ==3847267==Hint: address points to the zero page. #0 0x5586c80c9ec6 in vu_log_page ../subprojects/libvhost-user/libvhost-user.c:725 #1 0x5586c80c9ec6 in vu_log_write ../subprojects/libvhost-user/libvhost-user.c:742 #2 0x5586c80c9ec6 in vu_log_write ../subprojects/libvhost-user/libvhost-user.c:729 #3 0x5586c80caec0 in vu_log_queue_fill ../subprojects/libvhost-user/libvhost-user.c:3235 #4 0x5586c80cde53 in vu_queue_fill ../subprojects/libvhost-user/libvhost-user.c:3255 #5 0x5586c80cde53 in vu_queue_fill ../subprojects/libvhost-user/libvhost-user.c:3245 #6 0x5586c80cdfe8 in vu_queue_push ../subprojects/libvhost-user/libvhost-user.c:3300 #7 0x5586c7d6e249 in vu_blk_req_complete ../block/export/vhost-user-blk-server.c:48 #8 0x5586c7d6e249 in vu_blk_virtio_process_req ../block/export/vhost-user-blk-server.c:79 #9 0x5586c8092dcb in coroutine_trampoline ../util/coroutine-ucontext.c:175 #10 0x7f2f58d0c12f (/lib/x86_64-linux-gnu/libc.so.6+0x5a12f) AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV ../subprojects/libvhost-user/libvhost-user.c:725 in vu_log_page ==3847267==ABORTING -
A possible direction, not a patch: map the new log into a temporary variable first. If
mmap()fails, leave the oldlog_tableandlog_sizeuntouched and drop the connection withvu_panic(). Unmap the old log and install the new one only after a successfulmmap(). Settinglog_tableto NULL and replying normally would avoid the crash, but dirty logging would then stop silently, and a migration could finish with pages missing. Because the reply has no status field, closing the connection is the only way the frontend learns that logging failed.