[PATCH] libvhost-user: fail SET_LOG_BASE if mmap() of the log fails

Kevin Nguyen via B4 Relay posted 1 patch 21 hours ago
subprojects/libvhost-user/libvhost-user.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] libvhost-user: fail SET_LOG_BASE if mmap() of the log fails
Posted by Kevin Nguyen via B4 Relay 21 hours ago
From: Kevin Nguyen <kevinnguyen4221@gmail.com>

If mmap() of the dirty log fd fails, vu_set_log_base_exec() only
prints an error, then unmaps the previous log, stores MAP_FAILED in
dev->log_table and replies as if the request succeeded. The next
request completed with VHOST_F_LOG_ALL set writes through
(uint8_t *)-1 and the backend crashes with SIGSEGV.

Leave the existing log untouched and call vu_panic() instead, without
sending a reply. Setting log_table to NULL would avoid the crash but
silently stop dirty logging, so a migration could complete with
missing pages. The SET_LOG_BASE reply has no status field, so dropping
the connection is the only way to tell the frontend that logging
failed.

Also save errno before close(fd), which may clobber it.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4793
Signed-off-by: Kevin Nguyen <kevinnguyen4221@gmail.com>
---
 subprojects/libvhost-user/libvhost-user.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvhost-user/libvhost-user.c
index df21581033..f47612f9e7 100644
--- a/subprojects/libvhost-user/libvhost-user.c
+++ b/subprojects/libvhost-user/libvhost-user.c
@@ -1159,6 +1159,7 @@ vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
     int fd;
     uint64_t log_mmap_size, log_mmap_offset;
     void *rc;
+    int err;
 
     if (vmsg->fd_num != 1 ||
         vmsg->size != sizeof(vmsg->payload.log)) {
@@ -1174,9 +1175,11 @@ vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg)
 
     rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
               log_mmap_offset);
+    err = errno;
     close(fd);
     if (rc == MAP_FAILED) {
-        perror("log mmap error");
+        vu_panic(dev, "log mmap error: %s", strerror(err));
+        return false;
     }
 
     if (dev->log_table) {

---
base-commit: 8f66cb79044c370e3813c56ceb7a75a5ca2d0a0e
change-id: 20261121-libvhost-user-log-mmap-6f24f5fc1aab

Best regards,
--  
Kevin Nguyen <kevinnguyen4221@gmail.com>