Skip to content

openvmm: an unaligned or reversed decoded snapshot RAM range panics microVM load #1373

Closed

Description

@tangruize

A VM worker restoring a microVM snapshot panics at load with "assertion failed: range.end & (PAGE_SIZE - 1) == 0" when its launch message carries a snapshot or restore RAM range that is not 4-KiB-aligned, or ends before it starts, and that range merges with its neighbour into another invalid range. The assertion fires in release builds too, and panic = 'abort' ends the worker's process. Malformed ranges that merge into a valid range are not caught at all, and reach the RAM backing as they are. Only a launch message that OpenVMM's controller did not build carries such a range: the controller builds every snapshot and restore range with MemoryRange::new, and only MemoryRange's Protobuf decoding skips that constructor's checks.

Reproduction

Decode a MemoryRange with start 0x1000 and end 0x1800 (Protobuf fields 1 and 2), and put it after MemoryRange::new(0..0x1000) in snapshot_memory_ranges of an otherwise valid microVM configuration that has a RAM capacity. validate_machine_config should refuse the configuration; it accepts it. Load then merges the two into MemoryRange::new(0..0x1800), which panics on the unaligned end.

Cause

validate_machine_config looks at the snapshot ranges only to require a RAM capacity for them, never at their bounds:

// openvmm/openvmm_defs/src/microvm.rs
if let Some(memory_capacity) = config.microvm.memory_capacity {
    // ...
} else {
    anyhow::ensure!(
        config.microvm.snapshot_memory_ranges.is_empty()
            && config.microvm.restore_memory_ranges.is_empty(),
        "microVM snapshot RAM ranges require a RAM capacity"
    );
}

At load, add_snapshot_restore_backing merges adjacent ranges in coalesce_adjacent_ranges with MemoryRange::new, which asserts alignment and ordering:

// openvmm/openvmm_core/src/worker/dispatch/microvm.rs
if range.start() == previous.end() {
    *previous = MemoryRange::new(previous.start()..range.end());

Fix

fix.patch adds validate_snapshot_memory_ranges, which validate_machine_config calls after the RAM-capacity check. It requires MemoryRange::try_new(start..end) to succeed for every snapshot range and every restore range, and otherwise fails with "microVM snapshot RAM ranges must be page-aligned ranges", so the worker fails to start instead of aborting at load. Checking only in coalesce_adjacent_ranges would still let malformed ranges that merge into a valid range reach the RAM backing. Every range built with MemoryRange::new validates as before. The test decoded_snapshot_memory_ranges_are_validated decodes an unaligned end, an unaligned start and a reversed range into both lists and expects each to be refused, and checks that valid ranges pass. It adds test_with_tracing as a dev-dependency of openvmm_defs.

Related: #1372.

Activity

  1. tangruize commented on Nov 8, 2026

    @tangruize
    ContributorAuthor

    Proposed patch

    Patch against OpenVMM 3cf58a5234ed824435825c9f87187f5129219724. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/Cargo.lock b/Cargo.lock
    index 962a90d..8ae438e 100644
    --- a/Cargo.lock
    +++ b/Cargo.lock
    @@ -6419,6 +6419,7 @@ dependencies = [
      "serial_uart_resources",
      "smbios_defs",
      "state_unit",
    + "test_with_tracing",
      "thiserror 2.0.16",
      "tpm_resources",
      "unix_socket",
    diff --git a/openvmm/openvmm_defs/Cargo.toml b/openvmm/openvmm_defs/Cargo.toml
    index d0b278a..ad094f3 100644
    --- a/openvmm/openvmm_defs/Cargo.toml
    +++ b/openvmm/openvmm_defs/Cargo.toml
    @@ -43,5 +43,8 @@ thiserror.workspace = true
     virt_whp.workspace = true
     vmbus_proxy.workspace = true
     
    +[dev-dependencies]
    +test_with_tracing.workspace = true
    +
     [lints]
     workspace = true
    diff --git a/openvmm/openvmm_defs/src/microvm.rs b/openvmm/openvmm_defs/src/microvm.rs
    index bb8a07e..0911f3c 100644
    --- a/openvmm/openvmm_defs/src/microvm.rs
    +++ b/openvmm/openvmm_defs/src/microvm.rs
    @@ -1899,6 +1899,19 @@ fn validate_machine_load_mode(
         }
     }
     
    +fn validate_snapshot_memory_ranges(config: &MicrovmConfig) -> anyhow::Result<()> {
    +    // Mesh decoding does not enforce MemoryRange's constructor invariants.
    +    anyhow::ensure!(
    +        config
    +            .snapshot_memory_ranges
    +            .iter()
    +            .chain(&config.restore_memory_ranges)
    +            .all(|range| MemoryRange::try_new(range.start()..range.end()).is_ok()),
    +        "microVM snapshot RAM ranges must be page-aligned ranges"
    +    );
    +    Ok(())
    +}
    +
     /// Validates the microVM machine contract. Standard-machine configurations are unchanged.
     pub fn validate_machine_config(config: &Config, hypervisor_id: Option<&str>) -> anyhow::Result<()> {
         let MachineProfile::Microvm = config.machine_profile else {
    @@ -1982,6 +1995,7 @@ pub fn validate_machine_config(config: &Config, hypervisor_id: Option<&str>) ->
                 "microVM snapshot RAM ranges require a RAM capacity"
             );
         }
    +    validate_snapshot_memory_ranges(&config.microvm)?;
         anyhow::ensure!(
             !config.hypervisor.with_hv
                 && config.hypervisor.with_vtl2.is_none()
    @@ -2125,6 +2139,39 @@ pub fn validate_machine_config(config: &Config, hypervisor_id: Option<&str>) ->
     mod tests {
         use super::*;
         use std::fs::File;
    +    use test_with_tracing::test;
    +
    +    #[test]
    +    fn decoded_snapshot_memory_ranges_are_validated() {
    +        #[derive(mesh::payload::Protobuf)]
    +        struct EncodedRange {
    +            #[mesh(1)]
    +            start: u64,
    +            #[mesh(2)]
    +            end: u64,
    +        }
    +
    +        for (start, end) in [(0x1000, 0x1800), (0x1800, 0x2000), (0x2000, 0x1000)] {
    +            let range: MemoryRange =
    +                mesh::payload::decode(&mesh::payload::encode(EncodedRange { start, end })).unwrap();
    +            for restore in [false, true] {
    +                let mut config = MicrovmConfig::default();
    +                if restore {
    +                    config.restore_memory_ranges = vec![range];
    +                } else {
    +                    config.snapshot_memory_ranges = vec![MemoryRange::new(0..0x1000), range];
    +                }
    +                assert!(validate_snapshot_memory_ranges(&config).is_err());
    +            }
    +        }
    +
    +        let config = MicrovmConfig {
    +            snapshot_memory_ranges: vec![MemoryRange::new(0..0x1000)],
    +            restore_memory_ranges: vec![MemoryRange::new(0x1000..0x2000)],
    +            ..Default::default()
    +        };
    +        validate_snapshot_memory_ranges(&config).unwrap();
    +    }
     
         fn linux_load_mode(
             boot_mode: LinuxDirectBootMode,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions