Skip to content

Commit 8b222bb

Browse files
ManciukicShadowCurse
authored andcommitted
fix(memory): validate memory state in snapshot_state_sanity_check
Add validation about the memory regions stored in the memory state: - we need at least 1 region - we need at least 1 DRAM region - all DRAM regions need to have a single plugged slot This issue was caught by the fuzzer snapshot harness. Signed-off-by: Riccardo Mancini <mancio@amazon.com>
1 parent 2d1256c commit 8b222bb

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/vmm/src/persist.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ use crate::vmm_config::instance_info::InstanceInfo;
3636
use crate::vmm_config::machine_config::{HugePageConfig, MachineConfigError, MachineConfigUpdate};
3737
use crate::vmm_config::snapshot::{CreateSnapshotParams, LoadSnapshotParams, MemBackendType};
3838
use crate::vstate::kvm::KvmState;
39-
use crate::vstate::memory;
40-
use crate::vstate::memory::{GuestMemoryState, GuestRegionMmap, MemoryError};
39+
use crate::vstate::memory::{
40+
self, GuestMemoryState, GuestRegionMmap, GuestRegionType, MemoryError,
41+
};
4142
use crate::vstate::vcpu::{VcpuSendEventError, VcpuState};
4243
use crate::vstate::vm::{VmError, VmState};
4344
use crate::{EventManager, Vmm, vstate};
@@ -268,19 +269,48 @@ pub fn validate_cpu_manufacturer_id(microvm_state: &MicrovmState) {
268269
pub enum SnapShotStateSanityCheckError {
269270
/// No memory region defined.
270271
NoMemory,
272+
/// No DRAM memory region defined.
273+
NoDramMemory,
274+
/// DRAM memory has more than a single slot.
275+
DramMemoryTooManySlots,
276+
/// DRAM memory is unplugged.
277+
DramMemoryUnplugged,
271278
}
272279

273280
/// Performs sanity checks against the state file and returns specific errors.
274281
pub fn snapshot_state_sanity_check(
275282
microvm_state: &MicrovmState,
276283
) -> Result<(), SnapShotStateSanityCheckError> {
277-
// Check if the snapshot contains at least 1 mem region.
284+
// Check that the snapshot contains at least 1 mem region, that at least one is Dram,
285+
// and that Dram region contains a single plugged slot.
278286
// Upper bound check will be done when creating guest memory by comparing against
279287
// KVM max supported value kvm_context.max_memslots().
280-
if microvm_state.vm_state.memory.regions.is_empty() {
288+
let regions = &microvm_state.vm_state.memory.regions;
289+
290+
if regions.is_empty() {
281291
return Err(SnapShotStateSanityCheckError::NoMemory);
282292
}
283293

294+
if !regions
295+
.iter()
296+
.any(|r| r.region_type == GuestRegionType::Dram)
297+
{
298+
return Err(SnapShotStateSanityCheckError::NoDramMemory);
299+
}
300+
301+
for dram_region in regions
302+
.iter()
303+
.filter(|r| r.region_type == GuestRegionType::Dram)
304+
{
305+
if dram_region.plugged.len() != 1 {
306+
return Err(SnapShotStateSanityCheckError::DramMemoryTooManySlots);
307+
}
308+
309+
if !dram_region.plugged[0] {
310+
return Err(SnapShotStateSanityCheckError::DramMemoryUnplugged);
311+
}
312+
}
313+
284314
#[cfg(target_arch = "x86_64")]
285315
validate_cpu_vendor(microvm_state);
286316
#[cfg(target_arch = "aarch64")]

0 commit comments

Comments
 (0)