Skip to content

openvmm: memory-expansion range math overflows near the top of the GPA space #1364

Closed

Description

@tangruize

canonical_memory_expansion_ranges adds before it subtracts, so when the RAM size is at least 2^64 - 4 GiB (and below the capacity), a debug build panics with "attempt to add with overflow" although the GPA it computes fits. A release build wraps and computes the correct value, so only debug builds are affected. No real VM has such a size, but a crafted snapshot manifest can declare one: opening it reaches the helper through the shape check validate_machine_contract_shape. Building a microVM's memory-expansion contract calls the helper too.

Reproduction

canonical_memory_expansion_ranges(2^64 - 2^32, 2^64 - 2^31) should return one range at GPA 2^64 - 3 GiB; in a debug build it panics at the gpa_start computation. The test memory_expansion_near_gpa_limit_does_not_overflow that fix.patch adds fails at the pin and passes with the fix.

Cause

For the high-RAM capacity range (gpa_start 4 GiB, file_offset 3 GiB), gpa_start + logical_start overflows before the subtraction brings the value back into range:

// openvmm/openvmm_helpers/src/snapshot/microvm.rs
for capacity_range in canonical_microvm_memory_ranges(memory_capacity)? {
    let logical_start = capacity_range.file_offset.max(memory_size);
    // ...
    ranges.push(SnapshotMemoryExpansionRange {
        gpa_start: capacity_range.gpa_start + logical_start - capacity_range.file_offset,
        length: logical_end - logical_start,
    });

Fix

fix.patch computes gpa_start + (logical_start - file_offset). The subtraction cannot underflow, because logical_start is at least file_offset. The sum is at most the range's GPA end, which canonical_microvm_memory_ranges has already checked fits in a u64. The test checks the resulting range, and that validate_machine_contract_shape accepts the manifest built from it.

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/openvmm/openvmm_helpers/src/snapshot/microvm.rs b/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    index 0664d1a..2266d60 100644
    --- a/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    +++ b/openvmm/openvmm_helpers/src/snapshot/microvm.rs
    @@ -911,7 +911,7 @@ fn canonical_memory_expansion_ranges(
                 continue;
             }
             ranges.push(SnapshotMemoryExpansionRange {
    -            gpa_start: capacity_range.gpa_start + logical_start - capacity_range.file_offset,
    +            gpa_start: capacity_range.gpa_start + (logical_start - capacity_range.file_offset),
                 length: logical_end - logical_start,
             });
         }
    @@ -4065,6 +4065,26 @@ fn expandable_memory_manifest(base_memory: u64, capacity: u64) -> SnapshotManife
             manifest
         }
     
    +    #[test]
    +    fn memory_expansion_near_gpa_limit_does_not_overflow() {
    +        let memory_size = u64::MAX - (1 << 32) + 1;
    +        let capacity = u64::MAX - (1 << 31) + 1;
    +        assert_eq!(
    +            canonical_memory_expansion_ranges(memory_size, capacity).unwrap(),
    +            [SnapshotMemoryExpansionRange {
    +                gpa_start: memory_size + (1 << 30),
    +                length: capacity - memory_size,
    +            }]
    +        );
    +        let manifest = expandable_memory_manifest(memory_size, capacity);
    +        validate_machine_contract_shape(
    +            manifest.machine_contract.as_ref().unwrap(),
    +            memory_size,
    +            manifest.vp_count,
    +        )
    +        .unwrap();
    +    }
    +
         #[test]
         fn restore_memory_targets_select_canonical_prefixes() {
             const MB: u64 = 1024 * 1024;
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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions