Skip to content

Commit b58c536

Browse files
committed
fix(memory): validate memory region state before resuming
When the region state is invalid or corrupted (like when generated by the fuzzer), it is possible that a DRAM slot is unplugged, leading to segfaults when accessing guest memory (ie from vmgenid device). To avoid these crashes, validate the region state and allow the DRAM region (not hot-pluggable) to only contain one plugged slot. Signed-off-by: Riccardo Mancini <[email protected]>
1 parent 32e882e commit b58c536

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/vmm/src/vstate/memory.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub enum MemoryError {
5555
OffsetTooLarge,
5656
/// Cannot retrieve snapshot file metadata: {0}
5757
FileMetadata(std::io::Error),
58-
/// Memory region is not aligned
59-
Unaligned,
58+
/// Memory region state is invalid: {0}
59+
InvalidRegionState(&'static str),
6060
/// Error protecting memory slot: {0}
6161
Mprotect(std::io::Error),
6262
}
@@ -231,9 +231,23 @@ impl GuestRegionMmapExt {
231231
slot_from: u32,
232232
) -> Result<Self, MemoryError> {
233233
let slot_cnt = state.plugged.len();
234-
let slot_size = u64_to_usize(region.len())
235-
.checked_div(slot_cnt)
236-
.ok_or(MemoryError::Unaligned)?;
234+
let slot_size = u64_to_usize(region.len()).checked_div(slot_cnt).ok_or(
235+
MemoryError::InvalidRegionState("memory region should be aligned to the slot size"),
236+
)?;
237+
238+
// validate the region state to avoid spurious crashes when resuming from an invalid state
239+
if state.region_type == GuestRegionType::Dram {
240+
if slot_cnt != 1 {
241+
return Err(MemoryError::InvalidRegionState(
242+
"DRAM region should contain only one slot",
243+
));
244+
}
245+
if !state.plugged[0] {
246+
return Err(MemoryError::InvalidRegionState(
247+
"DRAM region should be plugged",
248+
));
249+
}
250+
}
237251

238252
Ok(GuestRegionMmapExt {
239253
inner: region,

0 commit comments

Comments
 (0)