Skip to content

Commit 8dd7b14

Browse files
syntacticallyliuw
authored andcommitted
get_gpa_access_state: avoid returning dangling reference
Previously, VmFd::get_gpa_access_state returned a dangling reference (in the form of a raw pointer) to a vector that was allocated and freed during the function call. This commit modifies it to instead return the vector directly, since no other information is required by its only consumer (VmFd::get_dirty_log). Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com>
1 parent 2859281 commit 8dd7b14

File tree

1 file changed

+6
-8
lines changed
  • mshv-ioctls/src/ioctls

1 file changed

+6
-8
lines changed

mshv-ioctls/src/ioctls/vm.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl VmFd {
644644
base_pfn: u64,
645645
nr_pfns: u32,
646646
flags: u64,
647-
) -> Result<mshv_get_gpa_pages_access_state> {
647+
) -> Result<Vec<hv_gpa_page_access_state>> {
648648
let mut states: Vec<hv_gpa_page_access_state> =
649649
vec![hv_gpa_page_access_state { as_uint8: 0 }; nr_pfns as usize];
650650
let mut gpa_pages_access_state: mshv_get_gpa_pages_access_state =
@@ -664,7 +664,7 @@ impl VmFd {
664664
)
665665
};
666666
if ret == 0 {
667-
Ok(gpa_pages_access_state)
667+
Ok(states)
668668
} else {
669669
Err(errno::Error::last().into())
670670
}
@@ -707,11 +707,7 @@ impl VmFd {
707707
current_size = cmp::min(PAGE_ACCESS_STATES_BATCH_SIZE, remaining);
708708
let page_states =
709709
self.get_gpa_access_state(base_pfn + processed as u64, current_size, flags)?;
710-
// SAFETY: we're sure states and count meet the requirements for from_raw_parts
711-
let slices: &[hv_gpa_page_access_state] = unsafe {
712-
std::slice::from_raw_parts(page_states.states, page_states.count as usize)
713-
};
714-
for item in slices.iter() {
710+
for item in page_states.iter() {
715711
let bits = &mut bitmap[bitmap_index];
716712
mask = 1 << bit_index;
717713
// SAFETY: access union field
@@ -723,7 +719,9 @@ impl VmFd {
723719
bitmap_index = processed / 64;
724720
bit_index = processed % 64;
725721
}
726-
remaining -= page_states.count;
722+
// There is no risk of overflow on this cast, since
723+
// page_states.len() is at most PAGE_ACCESS_STATES_BATCH_SIZE
724+
remaining -= page_states.len() as u32;
727725
}
728726
Ok(bitmap)
729727
}

0 commit comments

Comments
 (0)