Skip to content

openvmm: pin the XSAVE restore's bounds check - #1642

Merged
Pedro Henrique Penna (ppenna) merged 2 commits into
devfrom
agents/issue-1381-short-xsave
Nov 15, 2026
Merged

Pedro Henrique Penna (ppenna) merged 2 commits into
devfrom
agents/issue-1381-short-xsave

Conversation

@ppenna

@ppenna Pedro Henrique Penna (ppenna) commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1381

What and why

On KVM, restoring a saved VP state whose XSAVE element is shorter than 72 words, or whose header selects a component that ends past the area, panicked the VM worker, in debug and release builds. A save never returns such an area, so only a crafted or damaged snapshot reaches it.

Root cause. xsave::restore builds the KVM_SET_XSAVE buffers with Buffers::new, which calls Xsave::write_standard (vmm_core/virt/src/x86/vp.rs) on the area of the saved state. write_standard unwrapped its reads of the legacy area (fxsave) and of the header (xsave_header), and sliced the area at the compact place of each component that both XCOMP_BV and XSTATE_BV select, without checking either bound. The checks that restore already made do not cover it: check_xstate_bv (#1380, landed as #1589) reads XSTATE_BV only, with get, and check_xsave_layout (#1382) checks the caps' layout against KVM's buffer, not a restored area. MSHV and WHP pass the compact area to the hypervisor as it is, and never call write_standard.

Change, in OpenVMM's virt and virt_kvm (microsoft/openvmm#5119, one commit on feature/microvm/dev, head f9cc4163bc5fe03a578a07064ec2fc212268d9bf):

  • write_standard returns Result<(), XsaveError>. Before it writes a byte, it checks that the area holds the legacy area and the header, that the buffer holds them, and that each component that both masks select lies in the area at its compact place and in the buffer at its standard offset, and it fails with AreaTooShort, ComponentPastArea, or BufferTooShort otherwise, so a refusal writes nothing. The check and the copy take the components' places from one iterator, so they cannot disagree.
  • Buffers::new, the only caller, returns the error, and restore propagates it before any KVM_SET_XSAVE. KvmError gains Xsave, which wraps it, so restore_all fails with the StateError of the xsave element and the VP's XSAVE state stays as it was.
  • Every area that a save returns is written as before, and so is an area that only XCOMP_BV or only XSTATE_BV selects a component of, which write_standard never read. Only areas that panicked are refused, with an error. The guest sees nothing different.

Why this is not the issue's patch, nor #1589's InvalidState

The patch of #1381 adds a predicate, Xsave::fits_standard(len, caps), which set_xsave calls before write_standard. It stays a second description of where each component lies, which must agree with a function that panics on any area it does not accept, and its test catches the panics of write_standard to compare the two. The check of #1380 that #1589 landed refuses with KvmError::InvalidState and a fixed message, with no new type, which cannot say what is wrong, and leaves write_standard panicking on its own for any other caller.

This change makes the conversion itself fallible and typed instead:

  • No function that panics on an area of a saved state remains.
  • Validation and copy share one definition of the places, so they cannot drift apart when a layout rule changes, which two descriptions could.
  • The error names the number of words or the component, and the tests compare write_standard with its specification directly.
  • The cost is a signature change, with no other caller in this tree, and one KvmError variant.

The shapes, at the base and with this change

An area that a saved state holds, restored through restore_all or set_xsave on a real KVM VP (the contract test), or through write_standard (the virt tests):

Area At the base With this change
0, 1, or 63 words panic, called Result::unwrap() on an Err value: SizeError, in fxsave AreaTooShort { words }
64 words (the legacy area only), 65, or 71 words panic, called Result::unwrap() on an Err value: Size(SizeError), in xsave_header AreaTooShort { words }
72 words, whose header selects AVX in XCOMP_BV and XSTATE_BV panic, range end index 832 out of range for slice of length 576 ComponentPastArea { component: 2, words: 72 }
103 words, the same header panic, range end index 832 out of range for slice of length 824 ComponentPastArea { component: 2, words: 103 }
104 words, the same header (AVX ends at byte 832) restores restores
the area that a save returns restores restores
72 words, AVX in only one of the two masks restores restores
a buffer shorter than 576 bytes, or a selected component past the buffer's end panic BufferTooShort { len } (KVM's 4096-byte buffer, which check_xsave_layout sizes for the caps, cannot reach this)

No version

Nothing is versioned, and no reader refuses a snapshot: no saved-state field, format, schema, capability, or version changes, and none is created or bumped. No profile, contract, or time ABI version, digest, or identity changes either. XsaveError and KvmError::Xsave are in-process types. A snapshot of an earlier build restores exactly as before, except that an area that panicked the worker is refused with an error.

Validation

The change is OpenVMM's, so most of the evidence is OpenVMM's tests, which microsoft/openvmm#5119 lists. On the KVM host nvx-kvm-test (an Azure virtual machine: Linux 6.18.57.1-1.azl3 on an Intel Xeon Platinum 8573C):

  • Before. The new contract test, added alone to 23fb67887, fails: called Result::unwrap() on an Err value: SizeError, at vmm_core/virt/src/x86/vp.rs:863, in fxsave, and the other 11 KVM contract tests pass. A scratch variant of it, not kept, restored each shape above through a real VP at 9d64daad3 and showed the panics of the table's second column; write_standard is the same at 23fb67887.
  • After, at the pinned f9cc4163b: the 12 KVM contract tests pass, the new one included, and the 255 tests of virt, virt_kvm, and kvm, the ignored /dev/kvm ones included, pass. Each of four mutants of write_standard fails 2 to 4 of the five new virt tests.
  • With the openvmm release binary built at the pin (source_revision f9cc4163bc5fe03a578a07064ec2fc212268d9bf, clean), test-microvm passes the scenarios that restore real VPs' XSAVE areas through restore.

Checks that ran

Profile Backend Host type Check Result
local Windows python scripts/nvx.py verify pass
local Windows python scripts/local_gates.py --base origin/dev --run --keep-going --only GATE..., in two runs, at the head test-changes, markdown-links, test-layout, openvmm-vectors, and openvmm-cpu-profiles passed, and nvx-cli passed (2210 tests with 88 skipped, and the adversarial suite's 84 with 11 skipped). openvmm-ci, the one other gate that the change selects, was not run: it cannot read the pin's OpenVMM runs from here (GitHub's rate limit), so CI does
local Windows python scripts/openvmm_pin.py check-roadmap --worktree, python scripts/openvmm_pin.py check-branch --topic, python scripts/check_markdown_links.py, python scripts/check_test_layout.py, python scripts/check_test_changes.py --base origin/dev (0 changes), git diff --check pass
local Windows cargo test --locked --manifest-path nvx-rs/Cargo.toml --no-default-features --test openvmm_vectors -- --ignored, and --test openvmm_cpu_profiles -- --ignored, at the pin pass: no coordinated change
local Windows in openvmm/ at the pin: cargo test -p virt (126 tests), cargo clippy --locked --all-targets -p virt -- -D warnings, cargo doc --no-deps -p virt, and cargo clippy --locked --all-targets -p virt --target aarch64-apple-darwin -- -D warnings pass (the aarch64 check builds none of this change's x86-64 code, and checks the rest of virt there, where OpenVMM's CI also runs it)
nvx-kvm-test kvm virtual-machine in openvmm/ at the pin: cargo nextest run --profile agent --run-ignored all -p virt -p virt_kvm -p kvm, and cargo nextest run -p vmm_tests --test virt_backend_tests 255 pass; 12 pass
nvx-kvm-test kvm virtual-machine in openvmm/ at the pin: cargo clippy --locked --all-targets -p virt -p virt_kvm -p vmm_tests -- -D warnings (glibc, and virt_kvm with --target x86_64-unknown-linux-musl), cargo doc --no-deps -p virt -p virt_kvm, cargo xtask fmt --pass rustfmt --pass lints pass: no warnings, nothing to change
nvx-kvm-test kvm virtual-machine python3 scripts/nvx.py test-microvm --backend kvm --processors 1 2 with --scenario smp, smp-snapshot, snapshot-core, snapshot-rollback, restore-downtime, restore-processors, restore-memory, snapshot-tiers, and time-abi-conformance, with the openvmm release binary built at the pin (clean) in a checkout of this branch pass (exit 0)

Left to CI: openvmm-ci and OpenVMM's own CI of the pinned revision, which openvmm-ci waits for, among it the first run of the new contract test on AMD (x64-linux-amd-kvm); the MSHV and WHP jobs (the change is confined to virt_kvm and the cross-platform virt, whose new code is tested on Windows too); and the benchmarks.

CI of this head, f05340789df, whose OpenVMM is f9cc4163b:

  • NVX run 43753160622: every job passed or was skipped (35 passed, 8 skipped), OpenVMM CI of the pinned revision, the microVM tests on KVM, MSHV, and WHP, the performance regression gate, and Required status check included, with no rerun.
  • OpenVMM run 43750566441 for [DO NOT REVIEW] virt_kvm: refuse to restore a short or malformed XSAVE area openvmm#5119 at f9cc4163b: the six jobs that openvmm-ci requires passed, the unit tests on x64-linux and x64-windows, and the vmm-tests on x64-linux-amd-kvm, x64-linux-intel-mshv, x64-windows-intel, and x64-windows-amd. The new contract test xsave::kvm::refuses_a_short_or_malformed_xsave_area passed on x64-linux-amd-kvm (job 132930492056), its first run on AMD. x64-windows-amd-snp failed in Hyper-V OpenHCL SNP guest tests and is not required. No job was rerun.
  • Copilot's review of the first head (2d97fae11ff) had one thread: the roadmap remark said that position 302 changes only lines of positions 281 and 301, where it also rewrites upstream's write_standard. f05340789df fixes the remark and the commit message, and the thread is resolved. Its review of this head has no open issue.

Tests modified

python scripts/check_test_changes.py --base origin/dev lists no change to an existing test asset, so this pull request has no tests-modified label. The tests are OpenVMM's, listed in microsoft/openvmm#5119: five unit tests of write_standard in virt, a_short_or_malformed_area_gets_no_buffers in virt_kvm, and the KVM-only contract test xsave::kvm::refuses_a_short_or_malformed_xsave_area. The existing tests of xsave.rs changed only in unwrapping the result of Buffers::new.

OpenVMM pin

  • Pull request and pin. [DO NOT REVIEW] virt_kvm: refuse to restore a short or malformed XSAVE area openvmm#5119, whose head is the pinned f9cc4163bc5fe03a578a07064ec2fc212268d9bf, a signed commit.
  • Scope. One commit on 23fb67887, the head of feature/microvm/dev, which holds [DO NOT REVIEW] virt_kvm: restore the init components of an XSAVE area as init openvmm#5118 (openvmm: KVM's set_xsave keeps old PKRU or MXCSR after a VM reset on some hosts #1360). It changes vmm_core/virt/src/x86/vp.rs, vmm_core/virt_kvm/src/arch/x86_64/xsave.rs, vmm_core/virt_kvm/src/lib.rs, vmm_tests/vmm_tests/tests/virt_backend_tests/main.rs, and the new vmm_tests/vmm_tests/tests/virt_backend_tests/xsave.rs. No prerequisites.
  • Equivalence. None is claimed: this is the first revision of the pull request, not a replacement of a reviewed one.
  • Roadmap. Position 302, the pin at position 302, 178 commits past the mapped tip, not yet assigned to a PR; its remark records that it changes no line that a mapped PR adds, since it rewrites write_standard, which upstream main has and no commit of the chain changes, changes lines that positions 281 and 301 added, and adds the rest; and that it follows positions 132, 279, 281, 284, 296, and 301 upstream, where it conflicts with upstream main in three files, none of them in write_standard, and needs the kvm_only form of backend_test! and a handling of the new result in upstream's other caller of write_standard.
  • Coordinated changes. None: the vector, excerpt, contract-version, activation-bound, and CPU-profile comparisons (openvmm-vectors and openvmm-cpu-profiles) pass at the pin, and nothing versioned changes.
  • Checks. python scripts/openvmm_pin.py check-roadmap --worktree and check-branch --topic pass.
  • Landing order. Land [DO NOT REVIEW] virt_kvm: refuse to restore a short or malformed XSAVE area openvmm#5119 first, by fast-forwarding feature/microvm/dev to the pinned revision f9cc4163bc5fe03a578a07064ec2fc212268d9bf (OpenVMM's merge button squashes and would leave the pin off the branch), then queue this pull request with gh pr merge --merge.

Restoring a KVM VP whose saved XSAVE area was shorter than its legacy
area and header, or whose header selected a component that ended past
the end of the area, panicked the VM worker, in debug and release
builds: Xsave::write_standard unwrapped its reads and sliced the area
without checking either bound. A save never returns such an area, so
only a crafted or damaged snapshot reaches this (#1381).

Pin f9cc4163bc5fe03a578a07064ec2fc212268d9bf, the head of
microsoft/openvmm#5119, one commit on 23fb67887, the head of
feature/microvm/dev:

- f9cc4163b virt_kvm: refuse to restore a short or malformed XSAVE area

write_standard now returns a typed XsaveError, and checks the area's
words and each selected component's place before it writes a byte, so
the restore fails with the xsave element's StateError before any
KVM_SET_XSAVE changes the VP. The issue proposed a predicate that
set_xsave would call before a write_standard that still panics, and the
check of #1380 refuses with a fixed InvalidState message. The typed
result has one definition of the places, so that the check and the copy
cannot disagree, and it says which word count or component is wrong. No
saved state, saved-state format, or version changes, and none is created
or bumped. An area that a save returns restores as before.

Tests: five unit tests of write_standard in virt and one in virt_kvm,
which OpenVMM's CI runs, and a KVM-only backend contract test that
restores shortened and malformed areas into a real VP, which runs in the
vmm-tests jobs of the KVM hosts. The contract test fails at the base,
with a panic, and passes with the pin.

NVX needs no coordinated change: the control-session vectors, the source
excerpts, the contract versions, the activation bound, and the CPU
profiles that nvx-rs copies from OpenVMM match the revision's.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The roadmap inaccurately describes position 302 as changing only chain-introduced code, omitting changes to the existing upstream Xsave::write_standard signature and component-copy loop that must also be carried during upstreaming.

1 open issue
What changed in this PR

This PR updates NVX’s OpenVMM pin to address #1381: malformed XSAVE state should fail KVM restore with an error rather than panic the VM worker. The reviewed documentation records the revision and its upstream integration context.

Changes:

  • Records pin f9cc4163b at position 302 and updates the chain counts.
  • Documents XSAVE bounds validation, error propagation, and regression tests.
  • Describes upstream conflicts and prerequisite chain positions.
File Description
doc/​openvmm-upstream-roadmap.md Updates the pin mapping and documents the XSAVE restore fix and upstream dependencies.

Review effort: Balanced

Comment thread doc/openvmm-upstream-roadmap.md Resolved
The NVX pin moves to f9cc4163b, position 302, 178 commits past the
mapped tip. Position 302, the one commit of microsoft/openvmm#5119 on
23fb67887, the head of feature/microvm/dev, is not yet assigned to a PR.

Position 302 makes a restore of a short or malformed XSAVE area fail
with an error, where it panicked the VM worker: Xsave::write_standard,
which Buffers::new calls on the area of the saved state, unwrapped its
reads of the legacy area and of the header, and sliced the area at each
selected component without checking either bound. It now returns a
typed XsaveError, checking the area and the buffer before it writes a
byte, and KvmError gains Xsave. It changes no line that a mapped PR
adds: it rewrites write_standard, which upstream main has and no commit
of the chain changes, changes lines that positions 281 and 301 added,
which no PR has yet, and adds the rest. Replaying it on upstream main
conflicts in xsave.rs, which does not exist there since position 281
adds it, in lib.rs beside position 284's GifClear, and in vp.rs beside
the tests of positions 132 and 296, never in write_standard. Its
contract test needs the kvm_only form of backend_test! that position 279
adds, and upstream main's other caller of write_standard, in set_xsave
of vp_state.rs, which position 281 replaces, would have to handle the
result. So it follows positions 132, 279, 281, 284, 296, and 301
upstream.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 19:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The KVM saved-state pin needs maintainer confirmation because exact-pin AMD/KVM contract testing is still running and OpenVMM #5119 has not landed on feature/microvm/dev.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@ppenna
Pedro Henrique Penna (ppenna) added this pull request to the merge queue Nov 15, 2026
Merged via the queue into dev with commit ad43631 Nov 15, 2026
48 checks passed
@ppenna
Pedro Henrique Penna (ppenna) deleted the agents/issue-1381-short-xsave branch November 15, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants