Skip to content

Commit b684b88

Browse files
committed
Do not rely on GuestMemory to be well-behaved
GuestMemory is a safe trait, so it is permissible for GuestMemory::get_host_address() to return a garbage pointer that is obviously unsafe to use. Instead, use GuestMemory::get_slice(), which returns a VolatileSlice that always points to valid memory. See rust-vmm/vm-memory#332 for details. Signed-off-by: Demi Marie Obenour <[email protected]>
1 parent b60a717 commit b684b88

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

vfio-ioctls/src/vfio_device.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,21 @@ impl VfioContainer {
354354
/// Panics if the length of one of the regions overflows `usize`.
355355
pub unsafe fn vfio_map_guest_memory<M: GuestMemory>(&self, mem: &M) -> Result<()> {
356356
mem.iter().try_for_each(|region| {
357-
let host_addr = region
358-
.get_host_address(MemoryRegionAddress(0))
357+
let len = region.len().try_into().unwrap();
358+
let slice = region
359+
.get_slice(MemoryRegionAddress(0), len)
359360
.map_err(|_| VfioError::GetHostAddress)?;
360-
// SAFETY: GuestMemory guarantees the requirements
361+
assert_eq!(slice.len(), len);
362+
// FIXME: This crate (vfio-ioctls) is badly designed, because
363+
// it does not own the buffers that it maps into the kernel.
364+
// A proper design would own the buffers and unmap them on `Drop`.
365+
// To compose with other libraries that also need to own buffers,
366+
// this needs some sort of API that refers to a region of address
367+
// space and has hooks that are called when the address space is
368+
// unmapped.
369+
let host_addr = slice.ptr_guard_mut();
370+
let host_addr = host_addr.as_ptr();
371+
// SAFETY: VolatileSlice guarantees the requirements
361372
// are upheld.
362373
unsafe {
363374
self.vfio_dma_map(

0 commit comments

Comments
 (0)