misc: tdx: make tdx_init_memory_region() unsafe

It takes a pointer to a userspace address that it accesses, so it should
be marked unsafe.  This was missed earlier.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour
2025-06-27 22:08:26 -04:00
committed by Rob Bradford
parent 021f450cdb
commit 969a3b57a3
3 changed files with 22 additions and 8 deletions
+4 -2
View File
@@ -1020,11 +1020,13 @@ impl vm::Vm for KvmVm {
.map_err(vm::HypervisorVmError::FinalizeTdx) .map_err(vm::HypervisorVmError::FinalizeTdx)
} }
///
/// Initialize memory regions for the TDX VM /// Initialize memory regions for the TDX VM
/// ///
/// # Safety
///
/// `host_address` must be valid for `size` bytes
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
fn tdx_init_memory_region( unsafe fn tdx_init_memory_region(
&self, &self,
host_address: *mut u8, host_address: *mut u8,
guest_address: u64, guest_address: u64,
+5 -1
View File
@@ -401,7 +401,11 @@ pub trait Vm: Send + Sync + Any {
} }
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
/// Initialize a TDX memory region for this VM /// 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, &self,
_host_address: *mut u8, _host_address: *mut u8,
_guest_address: u64, _guest_address: u64,
+13 -5
View File
@@ -2249,15 +2249,23 @@ impl Vm {
let mem = guest_memory.memory(); let mem = guest_memory.memory();
for section in sections { for section in sections {
self.vm let size = section.size.try_into().unwrap();
.tdx_init_memory_region( // SAFETY: get_host_address_range does proper bounds checking
mem.get_host_address(GuestAddress(section.address)).unwrap(), unsafe {
self.vm.tdx_init_memory_region(
virtio_devices::get_host_address_range(
&*mem,
GuestAddress(section.address),
size,
)
.unwrap(),
section.address, section.address,
section.size.try_into().unwrap(), size,
/* TDVF_SECTION_ATTRIBUTES_EXTENDMR */ /* TDVF_SECTION_ATTRIBUTES_EXTENDMR */
section.attributes == 1, section.attributes == 1,
) )
.map_err(Error::InitializeTdxMemoryRegion)?; }
.map_err(Error::InitializeTdxMemoryRegion)?;
} }
Ok(()) Ok(())