Skip to content

Commit 8d19371

Browse files
raphael-nutanixmstsirkin
authored andcommitted
Stop vhost-user sending uninitialized mmap_offsets
Prior to this change, the vhost_user_fill_msg_region function filled out all elements of the VhostUserMemoryRegion struct except the mmap_offset. This function is often called on uninitialized structs, which are then copied into VHOST_USER_SET_MEM_TABLE and VHOST_USER_ADD/REM_MEM_REG messages. In some cases, where the mmap_offset was not needed, it was left uninitialized, causing QEMU to send the backend uninitialized data, which Coverity flagged as a series of issues. This change augments the vhost_user_fill_msg_region API, adding a mmap_offset paramenter, forcing the caller to initialize mmap_offset. Fixes: ece9909 Fixes: f1aeb14 Reported-by: Coverity (CIDs 1429802, 1429803 and 1429804) Suggested-by: Peter Maydell <[email protected]> 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: Peter Maydell <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]>
1 parent 56172c4 commit 8d19371

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

hw/virtio/vhost-user.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,14 @@ static MemoryRegion *vhost_user_get_mr_data(uint64_t addr, ram_addr_t *offset,
460460
}
461461

462462
static void vhost_user_fill_msg_region(VhostUserMemoryRegion *dst,
463-
struct vhost_memory_region *src)
463+
struct vhost_memory_region *src,
464+
uint64_t mmap_offset)
464465
{
465466
assert(src != NULL && dst != NULL);
466467
dst->userspace_addr = src->userspace_addr;
467468
dst->memory_size = src->memory_size;
468469
dst->guest_phys_addr = src->guest_phys_addr;
470+
dst->mmap_offset = mmap_offset;
469471
}
470472

471473
static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u,
@@ -500,9 +502,8 @@ static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u,
500502
error_report("Failed preparing vhost-user memory table msg");
501503
return -1;
502504
}
503-
vhost_user_fill_msg_region(&region_buffer, reg);
505+
vhost_user_fill_msg_region(&region_buffer, reg, offset);
504506
msg->payload.memory.regions[*fd_num] = region_buffer;
505-
msg->payload.memory.regions[*fd_num].mmap_offset = offset;
506507
fds[(*fd_num)++] = fd;
507508
} else if (track_ramblocks) {
508509
u->region_rb_offset[i] = 0;
@@ -649,7 +650,7 @@ static int send_remove_regions(struct vhost_dev *dev,
649650

650651
if (fd > 0) {
651652
msg->hdr.request = VHOST_USER_REM_MEM_REG;
652-
vhost_user_fill_msg_region(&region_buffer, shadow_reg);
653+
vhost_user_fill_msg_region(&region_buffer, shadow_reg, 0);
653654
msg->payload.mem_reg.region = region_buffer;
654655

655656
if (vhost_user_write(dev, msg, &fd, 1) < 0) {
@@ -709,9 +710,8 @@ static int send_add_regions(struct vhost_dev *dev,
709710
u->region_rb[reg_idx] = mr->ram_block;
710711
}
711712
msg->hdr.request = VHOST_USER_ADD_MEM_REG;
712-
vhost_user_fill_msg_region(&region_buffer, reg);
713+
vhost_user_fill_msg_region(&region_buffer, reg, offset);
713714
msg->payload.mem_reg.region = region_buffer;
714-
msg->payload.mem_reg.region.mmap_offset = offset;
715715

716716
if (vhost_user_write(dev, msg, &fd, 1) < 0) {
717717
return -1;

0 commit comments

Comments
 (0)