diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index cb21c28e2..e005834b9 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -1020,11 +1020,13 @@ impl vm::Vm for KvmVm { .map_err(vm::HypervisorVmError::FinalizeTdx) } - /// /// Initialize memory regions for the TDX VM /// + /// # Safety + /// + /// `host_address` must be valid for `size` bytes #[cfg(feature = "tdx")] - fn tdx_init_memory_region( + unsafe fn tdx_init_memory_region( &self, host_address: *mut u8, guest_address: u64, diff --git a/hypervisor/src/vm.rs b/hypervisor/src/vm.rs index 5e1cf7ea8..3b43c453a 100644 --- a/hypervisor/src/vm.rs +++ b/hypervisor/src/vm.rs @@ -401,7 +401,11 @@ pub trait Vm: Send + Sync + Any { } #[cfg(feature = "tdx")] /// Initialize a TDX memory region for this VM - fn tdx_init_memory_region( + /// + /// # Safety + /// + /// `_host_address` must be valid for `_size` bytes + unsafe fn tdx_init_memory_region( &self, _host_address: *mut u8, _guest_address: u64, diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 24d859b14..b5bb4a70a 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -2249,15 +2249,23 @@ impl Vm { let mem = guest_memory.memory(); for section in sections { - self.vm - .tdx_init_memory_region( - mem.get_host_address(GuestAddress(section.address)).unwrap(), + let size = section.size.try_into().unwrap(); + // SAFETY: get_host_address_range does proper bounds checking + unsafe { + self.vm.tdx_init_memory_region( + virtio_devices::get_host_address_range( + &*mem, + GuestAddress(section.address), + size, + ) + .unwrap(), section.address, - section.size.try_into().unwrap(), + size, /* TDVF_SECTION_ATTRIBUTES_EXTENDMR */ section.attributes == 1, ) - .map_err(Error::InitializeTdxMemoryRegion)?; + } + .map_err(Error::InitializeTdxMemoryRegion)?; } Ok(())