Skip to content

Commit 316ee11

Browse files
raphael-nutanixmstsirkin
authored andcommitted
libvhost-user: Add vu_rem_mem_reg input validation
Today if multiple FDs are sent from the VMM to the backend in a VHOST_USER_REM_MEM_REG message, one FD will be unmapped and the remaining FDs will be leaked. Therefore if multiple FDs are sent we report an error and fail the operation, closing all FDs in the message. Likewise in case the VMM sends a message with a size less than that of a memory region descriptor, we add a check to gracefully report an error and fail the operation rather than crashing. Signed-off-by: Raphael Norwitz <[email protected]> Message-Id: <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]> Reviewed-by: David Hildenbrand <[email protected]>
1 parent 408ca92 commit 316ee11

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

subprojects/libvhost-user/libvhost-user.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,21 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
801801
VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
802802
VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
803803

804+
if (vmsg->fd_num != 1) {
805+
vmsg_close_fds(vmsg);
806+
vu_panic(dev, "VHOST_USER_REM_MEM_REG received %d fds - only 1 fd "
807+
"should be sent for this message type", vmsg->fd_num);
808+
return false;
809+
}
810+
811+
if (vmsg->size < VHOST_USER_MEM_REG_SIZE) {
812+
close(vmsg->fds[0]);
813+
vu_panic(dev, "VHOST_USER_REM_MEM_REG requires a message size of at "
814+
"least %d bytes and only %d bytes were received",
815+
VHOST_USER_MEM_REG_SIZE, vmsg->size);
816+
return false;
817+
}
818+
804819
DPRINT("Removing region:\n");
805820
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
806821
msg_region->guest_phys_addr);

subprojects/libvhost-user/libvhost-user.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ typedef struct VhostUserMemoryRegion {
129129
uint64_t mmap_offset;
130130
} VhostUserMemoryRegion;
131131

132+
#define VHOST_USER_MEM_REG_SIZE (sizeof(VhostUserMemoryRegion))
133+
132134
typedef struct VhostUserMemory {
133135
uint32_t nregions;
134136
uint32_t padding;

0 commit comments

Comments
 (0)