From 2c54c30435633c8f94c3dc7a45a763acce548be4 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 6 Aug 2021 15:13:08 +0200 Subject: [PATCH] virtio-devices: vhost_user: common: Fix memory access It was incorrect to call Vec::from_raw_parts() on the address pointing to the shared memory log region since Vec is a Rust specific structure that doesn't directly translate into bytes. That's why we use the same function from std::slice in order to create a proper slice out of the memory region, which is then copied into a Vec. Signed-off-by: Sebastien Boeuf --- virtio-devices/src/vhost_user/vu_common_ctrl.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/virtio-devices/src/vhost_user/vu_common_ctrl.rs b/virtio-devices/src/vhost_user/vu_common_ctrl.rs index 469b98250..4acd8a98d 100644 --- a/virtio-devices/src/vhost_user/vu_common_ctrl.rs +++ b/virtio-devices/src/vhost_user/vu_common_ctrl.rs @@ -457,8 +457,7 @@ impl VhostUserHandle { // Make sure we hold onto the region to prevent the mapping from being // released. - let old_region = self.shm_log.take(); - self.shm_log = Some(Arc::new(region)); + let old_region = self.shm_log.replace(Arc::new(region)); // Send the shm_log fd over to the backend let log = VhostUserDirtyLogRegion { @@ -537,12 +536,14 @@ impl VhostUserHandle { // region. The previous region is returned and processed to create the // bitmap representing the dirty pages. if let Some(region) = self.update_log_base(last_ram_addr)? { - // Cast the pointer to u64 - let ptr = region.as_ptr() as *mut u64; // Be careful with the size, as it was based on u8, meaning we must // divide it by 8. let len = region.size() / 8; - let bitmap = unsafe { Vec::from_raw_parts(ptr, len, len) }; + let bitmap = unsafe { + // Cast the pointer to u64 + let ptr = region.as_ptr() as *const u64; + std::slice::from_raw_parts(ptr, len).to_vec() + }; Ok(MemoryRangeTable::from_bitmap(bitmap, 0)) } else { Err(Error::MissingShmLogRegion)