Skip to content

openvmm: restoring a short or malformed XSAVE area panics the VM worker on KVM #1381

Closed

Description

@tangruize

On KVM, restoring a snapshot whose saved XSAVE area is shorter than its legacy area and header (72 words), or whose header selects a component that ends past the area, panics the VM worker, in debug and release builds. A read never returns such an area, so only a crafted or corrupted snapshot file reaches this. MSHV and WHP pass the compact area to the hypervisor unchanged and are not affected.

Reproduction

restore_all a KVM VP saved state whose xsave element has 64 words (the legacy area only), or 72 words whose header's XCOMP_BV and XSTATE_BV both select AVX, on a host with AVX. Expected: the restore is refused. Actual: set_xsave panics, on an unwrap or on an out-of-range slice.

Cause

KvmVpStateAccess::set_xsave (vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs) converts the saved compact area with Xsave::write_standard (vmm_core/virt/src/x86/vp.rs). It unwraps the reads of the legacy area (in fxsave) and of the header, and slices both buffers at each component that the area's own header selects, without checking either bound:

// vmm_core/virt/src/x86/vp.rs
pub fn write_standard(&self, data: &mut [u8], caps: &X86PartitionCapabilities) {
    // ...
    for (i, feature) in caps.xsave.variable_features(header.xcomp_bv) {
        // ...
        if header.xstate_bv & (1 << i) != 0 {
            data[offset..offset + len].copy_from_slice(&self.data.as_bytes()[cur..cur + len]);
        }
// ...
fn xsave_header(&self) -> &XsaveHeader {
    XsaveHeader::ref_from_prefix(&self.data.as_bytes()[XSAVE_LEGACY_LEN..])
        .unwrap()

Fix

fix.patch adds Xsave::fits_standard(len, caps), which holds exactly when write_standard can write the area into a buffer of len bytes: the area and the buffer hold the legacy area and the header, and each component that both header masks select lies within the area at its compact place and within the buffer at its standard offset. set_xsave checks it before it writes anything and returns KvmError::InvalidState otherwise, so such a restore fails with restore_all's StateError for the XSAVE element instead of panicking. Every area a read returns is accepted and written as before. The test fits_standard_is_what_write_standard_can_write (virt) covers three crafted areas, each bound exactly at its edge, components selected by only one of the two header masks, and 2000 random layouts, headers and lengths, and checks fits_standard against write_standard's own panics. The patch has no KVM-backed test.

Related: #1169, #1380.

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/vmm_core/virt/src/x86/vp.rs b/vmm_core/virt/src/x86/vp.rs
    index a73d343..c7c5f6c 100644
    --- a/vmm_core/virt/src/x86/vp.rs
    +++ b/vmm_core/virt/src/x86/vp.rs
    @@ -873,6 +873,40 @@ fn xsave_header(&self) -> &XsaveHeader {
                 .unwrap()
                 .0 // TODO: zerocopy: ref-from-prefix: use-rest-of-range (https://github.com/microsoft/openvmm/issues/759)
         }
    +
    +    /// Returns whether [`Self::write_standard`] can write this area into a
    +    /// standard-format buffer of `len` bytes with `caps`' layout: the area
    +    /// holds the legacy area and the header, the buffer holds them too, and
    +    /// each component that the area's XCOMP_BV and XSTATE_BV both select lies
    +    /// within the area at its compact place and within the buffer at its
    +    /// standard offset. (`write_standard` panics on any other area.)
    +    pub fn fits_standard(&self, len: usize, caps: &X86PartitionCapabilities) -> bool {
    +        if self.data.len() < XSAVE_VARIABLE_OFFSET / 8 || len < XSAVE_VARIABLE_OFFSET {
    +            return false;
    +        }
    +        let xstate_bv = self.data[XSAVE_LEGACY_LEN / 8];
    +        let xcomp_bv = self.data[XSAVE_LEGACY_LEN / 8 + 1];
    +        let words = self.data.len() as u64;
    +        let mut cur = XSAVE_VARIABLE_OFFSET as u64;
    +        for i in 2..63 {
    +            if xcomp_bv & (1 << i) != 0 {
    +                let feature = &caps.xsave.feature_info[i];
    +                if feature.align {
    +                    cur = (cur + 63) & !63;
    +                }
    +                // The component's end, in whole words, past the area's, or
    +                // its standard end past the buffer's.
    +                if xstate_bv & (1 << i) != 0
    +                    && ((cur + feature.len as u64 + 7) / 8 > words
    +                        || feature.offset as u64 + feature.len as u64 > len as u64)
    +                {
    +                    return false;
    +                }
    +                cur += feature.len as u64;
    +            }
    +        }
    +        true
    +    }
     }
     
     impl Debug for Xsave {
    @@ -2099,4 +2133,223 @@ fn clear_tsc_keeps_the_other_elements() {
                 }
             );
         }
    +
    +    /// Capabilities of a one-VP partition whose CPUID is all zero but for
    +    /// the x2APIC bit its topology needs.
    +    fn caps_with() -> X86PartitionCapabilities {
    +        let topology = vm_topology::processor::TopologyBuilder::new_x86()
    +            .build(1)
    +            .unwrap();
    +        let x2apic = topology.apic_mode() != vm_topology::processor::x86::ApicMode::XApic;
    +        X86PartitionCapabilities::from_cpuid(&topology, &mut |leaf, _| match leaf {
    +            0 => [1, 0, 0, 0],
    +            1 => [0, 0, if x2apic { 1 << 21 } else { 0 }, 0],
    +            _ => [0; 4],
    +        })
    +        .unwrap()
    +    }
    +
    +    /// Whether `write_standard` writes `area` into a buffer of `len` bytes
    +    /// with `caps`' layout without panicking.
    +    fn writes_standard(area: &Xsave, len: usize, caps: &X86PartitionCapabilities) -> bool {
    +        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    +            area.write_standard(&mut vec![0; len], caps)
    +        }))
    +        .is_ok()
    +    }
    +
    +    /// Capabilities whose XSAVE layout has the components `components`, each
    +    /// `(index, standard offset, length, aligned)`, with the compact length
    +    /// of all of them (as `from_cpuid` computes it).
    +    fn caps_with_xsave(components: &[(usize, u32, u32, bool)]) -> X86PartitionCapabilities {
    +        let mut caps = caps_with();
    +        caps.xsave.features = 3;
    +        caps.xsave.supervisor_features = 0;
    +        caps.xsave.feature_info = [Default::default(); 63];
    +        for &(i, offset, len, align) in components {
    +            caps.xsave.features |= 1 << i;
    +            caps.xsave.feature_info[i] = crate::x86::XsaveFeature { offset, len, align };
    +        }
    +        caps.xsave.compact_len = caps.xsave.compact_len_for(!0);
    +        caps.xsave.standard_len = caps.xsave.standard_len_for(!0);
    +        caps
    +    }
    +
    +    /// An area of `words` words, its XSTATE_BV `xstate` and XCOMP_BV `xcomp`
    +    /// (when it holds the header), every other word distinct.
    +    fn xsave_area(words: usize, xstate: u64, xcomp: u64) -> Xsave {
    +        let mut data: Vec<u64> = (0..words as u64).map(|w| w * 0x0101_0101 + 7).collect();
    +        if words >= 66 {
    +            data[64] = xstate;
    +            data[65] = xcomp;
    +        }
    +        Xsave { data }
    +    }
    +
    +    /// `Xsave::fits_standard`, the check KVM's set_xsave makes before
    +    /// it writes an area out, holds exactly for the areas `write_standard`
    +    /// writes without panicking. Cases: three crafted areas (no words, the
    +    /// legacy area only, the legacy area and the header with XSTATE_BV
    +    /// holding a component); an area `from_standard` reads, which fits; each
    +    /// bound at its edge (an area ending at a selected component's end, and a
    +    /// word short of it, for a component placed after a 64-byte alignment;
    +    /// a selected component's standard end at the buffer's end, and a byte
    +    /// past it; a buffer of 576 bytes and of 575); a component that only one
    +    /// of XCOMP_BV and XSTATE_BV selects, which neither reads; and areas,
    +    /// headers and layouts drawn at random.
    +    #[test]
    +    fn fits_standard_is_what_write_standard_can_write() {
    +        // Components 2 (256 bytes, standard offset 576), 3 (8 bytes, 960), 5
    +        // (64 bytes, 1088, aligned to 64) and 9 (8 bytes, 2688): compact
    +        // places 576, 832, 896 (840 aligned) and 960; compact length 968.
    +        let caps = caps_with_xsave(&[
    +            (2, 576, 256, false),
    +            (3, 960, 8, false),
    +            (5, 1088, 64, true),
    +            (9, 2688, 8, false),
    +        ]);
    +        assert_eq!(caps.xsave.compact_len, 968);
    +        let xcomp = XCOMP_COMPRESSED | caps.xsave.features;
    +        let cases: Vec<(&str, Xsave, usize, bool)> = vec![
    +            ("no words", xsave_area(0, 0, 0), 4096, false),
    +            ("the legacy area only", xsave_area(64, 0, 0), 4096, false),
    +            (
    +                "the legacy area and the header, XSTATE_BV holding component 2",
    +                xsave_area(72, 1 << 2, xcomp),
    +                4096,
    +                false,
    +            ),
    +            (
    +                "the legacy area and the header, nothing selected",
    +                xsave_area(72, 3, xcomp),
    +                4096,
    +                true,
    +            ),
    +            (
    +                "component 5 ending at the area's end (960 bytes)",
    +                xsave_area(120, 1 << 5, xcomp),
    +                4096,
    +                true,
    +            ),
    +            (
    +                "component 5 a word past the area's end",
    +                xsave_area(119, 1 << 5, xcomp),
    +                4096,
    +                false,
    +            ),
    +            (
    +                "component 9's standard end at the buffer's end",
    +                xsave_area(121, 1 << 9, xcomp),
    +                2696,
    +                true,
    +            ),
    +            (
    +                "component 9's standard end a byte past the buffer's end",
    +                xsave_area(121, 1 << 9, xcomp),
    +                2695,
    +                false,
    +            ),
    +            (
    +                "a buffer of 576 bytes, nothing selected",
    +                xsave_area(121, 3, xcomp),
    +                576,
    +                true,
    +            ),
    +            (
    +                "a buffer of 575 bytes",
    +                xsave_area(121, 3, xcomp),
    +                575,
    +                false,
    +            ),
    +            (
    +                "component 9 in XCOMP_BV only, past the area",
    +                xsave_area(72, 0, xcomp),
    +                4096,
    +                true,
    +            ),
    +            (
    +                "component 9 in XSTATE_BV only, past the area",
    +                xsave_area(72, 1 << 9, XCOMP_COMPRESSED | (1 << 2)),
    +                4096,
    +                true,
    +            ),
    +        ];
    +        for (name, area, len, fits) in &cases {
    +            assert_eq!(
    +                area.fits_standard(*len, &caps),
    +                *fits,
    +                "{name}: fits_standard"
    +            );
    +            assert_eq!(
    +                writes_standard(area, *len, &caps),
    +                *fits,
    +                "{name}: write_standard"
    +            );
    +        }
    +
    +        // An area as a read returns it, every component in XSTATE_BV.
    +        let mut src: Vec<u8> = (0..4096).map(|b| (b * 13 + 5) as u8).collect();
    +        src[XSAVE_LEGACY_LEN..XSAVE_LEGACY_LEN + 8].copy_from_slice(&(0x22cu64 | 3).to_le_bytes());
    +        let read = Xsave::from_standard(&src, &caps);
    +        assert_eq!(read.data.len(), 121);
    +        assert!(read.fits_standard(4096, &caps), "a read area fits");
    +        assert!(writes_standard(&read, 4096, &caps), "a read area writes");
    +
    +        // Random layouts, headers, area lengths and buffer lengths.
    +        let mut seed = 0x2545_f491_4f6c_dd1du64;
    +        let mut random = move || {
    +            seed ^= seed << 13;
    +            seed ^= seed >> 7;
    +            seed ^= seed << 17;
    +            seed
    +        };
    +        let mut outcomes = [0; 2];
    +        for _ in 0..2000 {
    +            let components: Vec<(usize, u32, u32, bool)> = (0..random() % 6)
    +                .map(|_| {
    +                    (
    +                        2 + (random() % 61) as usize,
    +                        (random() % 4200) as u32,
    +                        (random() % 300) as u32,
    +                        random() % 2 == 0,
    +                    )
    +                })
    +                .collect();
    +            let caps = caps_with_xsave(&components);
    +            let words = match random() % 4 {
    +                0 => (random() % 80) as usize,
    +                _ => {
    +                    (caps.xsave.compact_len as usize / 8).saturating_sub(2)
    +                        + (random() % 4) as usize
    +                }
    +            };
    +            let xcomp = if random() % 2 == 0 {
    +                XCOMP_COMPRESSED | caps.xsave.features
    +            } else {
    +                random()
    +            };
    +            let xstate = match random() % 3 {
    +                0 => caps.xsave.features,
    +                1 => caps.xsave.features & random(),
    +                _ => random(),
    +            };
    +            let len = match random() % 3 {
    +                0 => 4096,
    +                1 => 570 + (random() % 10) as usize,
    +                _ => (random() % 4500) as usize,
    +            };
    +            let area = xsave_area(words, xstate, xcomp);
    +            let fits = area.fits_standard(len, &caps);
    +            assert_eq!(
    +                fits,
    +                writes_standard(&area, len, &caps),
    +                "components {components:?} words {words} xcomp {xcomp:#x} xstate {xstate:#x} len {len}"
    +            );
    +            outcomes[fits as usize] += 1;
    +        }
    +        assert!(
    +            outcomes[0] >= 200 && outcomes[1] >= 200,
    +            "both outcomes drawn: {outcomes:?}"
    +        );
    +    }
     }
    diff --git a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    index 59dd45b..c962559 100644
    --- a/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    +++ b/vmm_core/virt_kvm/src/arch/x86_64/vp_state.rs
    @@ -434,6 +434,17 @@ fn xsave(&mut self) -> Result<vp::Xsave, Self::Error> {
     
         fn set_xsave(&mut self, value: &vp::Xsave) -> Result<(), Self::Error> {
             let mut data = [0; 4096];
    +        // `write_standard` reads the legacy area and the header, then each
    +        // component the area's XCOMP_BV and XSTATE_BV select, at its compact
    +        // place and its standard offset. An area that does not hold them, or
    +        // a component past KVM's buffer, would panic it; a read never returns
    +        // one, so only a crafted snapshot holds it, and it is refused before
    +        // anything changes.
    +        if !value.fits_standard(data.len(), &self.vp.partition.caps) {
    +            return Err(KvmError::InvalidState(
    +                "an XSAVE area that does not hold the components its header selects",
    +            ));
    +        }
             value.write_standard(&mut data, &self.vp.partition.caps);
             self.kvm().set_xsave(&data)?;
             Ok(())
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