Skip to content

Commit 6e5303f

Browse files
ppennaCopilot
andcommitted
openvmm_helpers: refuse a malformed snapshot ID by its field
Restore refuses a manifest whose snapshot ID, field 18, is not a GUID of 16 bytes only as a manifest that failed to decode, which names neither the ID nor its field. For an ID of 0, 15, or 17 bytes: failed to decode snapshot manifest: decoding failed: invalid byte size for type and for an integer: failed to decode snapshot manifest: decoding failed: expected a byte array Upstream's SaveVM and RestoreVM (#4553) record the ID in field 18, where this manifest of version 2 carries it, and microsoft/nvx#1210 asks that a missing or malformed ID in this format be handled explicitly, with tests of both. Decode the ID alone, after the version and the magic, and refuse a field 18 of another type or length before decoding the rest: snapshot manifest is malformed: its snapshot ID, field 18, is not a GUID of 16 bytes Decoding the version alone has already read every field's key and length, so this decode can fail only on the ID. The version and the magic still come first, so a manifest of another version gets E_SNAPSHOT_VERSION whatever its field 18 holds. A manifest without field 18, such as one of version 2 that predates it, still reads with the zero ID, which stands for none, as upstream defines it: restore checks no ID unless RestoreVM expects one, which then refuses it. The format stays version 2: no field moves or changes its type, and a manifest without the ID keeps its meaning. The snapshot format guide now says how restore reads a missing or malformed ID, and why the ID needed no new version. Tests, in openvmm_helpers' restore tests: read_snapshot_refuses_a_malformed_snapshot_id, which fails at feature/microvm/dev a90c603 with the first error above, and read_snapshot_reads_a_manifest_without_a_snapshot_id_as_none, which passes there too and keeps the missing-ID policy on the read path that the new decode joins. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 383bbc9 commit 6e5303f

3 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎Guide/src/dev_guide/snapshot_format.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ snapshot has no machine contract. Tiered microVM snapshots record capture
3636
tier, clone/resume policy, and consumed configuration sections. Field 18 holds
3737
the snapshot ID that SaveVM and the command line's `save-snapshot` generate,
3838
which RestoreVM can require; a microVM capture records the zero ID, which
39-
stands for none.
39+
stands for none. A manifest without the field, such as one of version 2 that
40+
predates it, reads with the zero ID too, which RestoreVM refuses only when it
41+
expects an ID. Restore decodes the ID alone, after the magic, and refuses a
42+
manifest whose field 18 is not a GUID of 16 bytes as malformed before it
43+
decodes the rest. The ID joined version 2 without a new version: it moves no
44+
field and changes no field's type, and a manifest without it keeps its
45+
meaning.
4046

4147
The format is a local machine-state contract, not an authenticated container.
4248
Every snapshot receives the same regular-file, no-follow/no-reparse, bounded

‎openvmm/openvmm_helpers/src/snapshot/format.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ struct ManifestFormatMagic {
122122
format_magic: Vec<u8>,
123123
}
124124

125+
/// The snapshot ID of a manifest of [`MANIFEST_VERSION`], which it keeps in
126+
/// field 18 as [`SnapshotManifest`] does.
127+
#[derive(Protobuf)]
128+
#[mesh(package = "openvmm.snapshot")]
129+
struct ManifestSnapshotId {
130+
#[mesh(18, encoding = "mesh::payload::encoding::ZeroCopyEncoding")]
131+
snapshot_id: guid::Guid,
132+
}
133+
125134
/// Decodes a manifest of this format, one that [`validate_manifest_header`]
126135
/// accepts.
127136
///
@@ -131,13 +140,23 @@ struct ManifestFormatMagic {
131140
/// and refuses any other version or magic with `E_SNAPSHOT_VERSION` before it
132141
/// decodes the rest. The magic comes second, since only the formats of this
133142
/// version agree on the type of field 10.
143+
///
144+
/// It then decodes the snapshot ID alone, so that it refuses a manifest whose
145+
/// field 18 is not a GUID of 16 bytes as malformed by its ID, rather than as
146+
/// a manifest that failed to decode. A manifest without the field, such as one
147+
/// of this version that predates it, has the zero ID, which stands for none.
134148
pub(super) fn decode_manifest(bytes: &[u8]) -> anyhow::Result<SnapshotManifest> {
135149
let ManifestVersion { version } =
136150
mesh::payload::decode(bytes).context("failed to decode snapshot manifest")?;
137151
validate_manifest_version(version)?;
138152
let ManifestFormatMagic { format_magic } =
139153
mesh::payload::decode(bytes).context("failed to decode snapshot manifest")?;
140154
validate_format_magic(&format_magic)?;
155+
// Decoding the version read every field's key and length, so only the
156+
// snapshot ID's type or length can fail this decode.
157+
let ManifestSnapshotId { snapshot_id: _ } = mesh::payload::decode(bytes).context(
158+
"snapshot manifest is malformed: its snapshot ID, field 18, is not a GUID of 16 bytes",
159+
)?;
141160
let manifest: SnapshotManifest =
142161
mesh::payload::decode(bytes).context("failed to decode snapshot manifest")?;
143162
validate_manifest_header(&manifest)?;

‎openvmm/openvmm_helpers/src/snapshot/restore.rs‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ fn validate_snapshot_directory(
407407

408408
#[cfg(test)]
409409
mod tests {
410+
use super::super::SnapshotExpectations;
410411
use super::super::format::SNAPSHOT_RESTORE_POLICY_CLONE;
411412
use super::super::format::SNAPSHOT_TIER_INSTANCE_CHECKPOINT;
412413
use super::super::format::SNAPSHOT_TIER_WORKLOAD_START;
@@ -419,6 +420,7 @@ mod tests {
419420
#[cfg(target_os = "linux")]
420421
use super::super::publish::write_snapshot_from_owned_memory_and_scratch_files;
421422
use super::super::tests::test_manifest;
423+
use super::super::validate_expectations;
422424
use super::*;
423425
use mesh::payload::Protobuf;
424426
use mesh::payload::Timestamp;
@@ -791,6 +793,116 @@ mod tests {
791793
}
792794
}
793795

796+
/// A manifest's snapshot ID, field 18, alone, as `SnapshotManifest`
797+
/// encodes it.
798+
#[derive(Protobuf)]
799+
#[mesh(package = "openvmm.snapshot")]
800+
struct SnapshotIdField {
801+
#[mesh(18, encoding = "mesh::payload::encoding::ZeroCopyEncoding")]
802+
snapshot_id: guid::Guid,
803+
}
804+
805+
/// Encodes `manifest` without its snapshot ID, field 18, which it encodes
806+
/// last, as the manifests of version 2 that predate the field are encoded.
807+
fn encode_without_snapshot_id(manifest: SnapshotManifest) -> Vec<u8> {
808+
let id = mesh::payload::encode(SnapshotIdField {
809+
snapshot_id: manifest.snapshot_id,
810+
});
811+
mesh::payload::encode(manifest)
812+
.strip_suffix(id.as_slice())
813+
.expect("the manifest encodes its snapshot ID last")
814+
.to_vec()
815+
}
816+
817+
/// The manifest of a snapshot of [`test_manifest`] whose saved state is
818+
/// `b"state"`, as [`read_errors_with_manifest`] writes one, without its
819+
/// snapshot ID.
820+
fn test_manifest_without_snapshot_id() -> Vec<u8> {
821+
let mut manifest = test_manifest();
822+
manifest.state_size_bytes = b"state".len() as u64;
823+
encode_without_snapshot_id(manifest)
824+
}
825+
826+
/// A manifest of this format without a snapshot ID, field 18, as one of
827+
/// version 2 that predates the field is, reads with the zero ID, which
828+
/// stands for none, as a microVM capture's zero ID does: a restore that
829+
/// expects no ID accepts it, and one that expects an ID refuses it.
830+
#[test]
831+
fn read_snapshot_reads_a_manifest_without_a_snapshot_id_as_none() {
832+
let expected = test_manifest().snapshot_id;
833+
assert!(!expected.is_zero());
834+
let dir = tempfile::tempdir().unwrap();
835+
let snap_dir = dir.path().join("snap");
836+
let mem_path = dir.path().join("memory.bin");
837+
std::fs::write(&mem_path, vec![0_u8; 1024]).unwrap();
838+
write_snapshot(&snap_dir, &test_manifest(), b"state", &mem_path).unwrap();
839+
std::fs::write(
840+
snap_dir.join(MANIFEST_FILE_NAME),
841+
test_manifest_without_snapshot_id(),
842+
)
843+
.unwrap();
844+
845+
let (manifest, state) = read_snapshot(&snap_dir, 1024).unwrap();
846+
assert!(manifest.snapshot_id.is_zero());
847+
assert_eq!(state, b"state");
848+
assert!(
849+
read_snapshot_manifest(&snap_dir)
850+
.unwrap()
851+
.snapshot_id
852+
.is_zero()
853+
);
854+
validate_expectations(&manifest, &SnapshotExpectations::default()).unwrap();
855+
let error = validate_expectations(
856+
&manifest,
857+
&SnapshotExpectations {
858+
snapshot_id: Some(expected),
859+
},
860+
)
861+
.unwrap_err();
862+
assert!(error.to_string().contains("snapshot ID"), "{error}");
863+
}
864+
865+
/// A manifest of this format whose snapshot ID, field 18, is not a GUID of
866+
/// 16 bytes is malformed, and restore refuses it by its ID before it
867+
/// decodes the rest. A manifest of another version, whose field 18 this
868+
/// format does not define, is refused by its version first.
869+
#[test]
870+
fn read_snapshot_refuses_a_malformed_snapshot_id() {
871+
// Field 18's key, (18 << 3) | its wire type, is a varint of two bytes:
872+
// 0x92 0x01 for a length-delimited field, and 0x90 0x01 for an integer.
873+
let malformed = [
874+
vec![0x92, 0x01, 0],
875+
[&[0x92, 0x01, 15][..], &[0xa5; 15]].concat(),
876+
[&[0x92, 0x01, 17][..], &[0xa5; 17]].concat(),
877+
vec![0x90, 0x01, 5],
878+
];
879+
let mut other_version = test_manifest();
880+
other_version.version = 3;
881+
other_version.state_size_bytes = b"state".len() as u64;
882+
let other_version = encode_without_snapshot_id(other_version);
883+
for field in &malformed {
884+
let manifest = [test_manifest_without_snapshot_id().as_slice(), field].concat();
885+
for error in read_errors_with_manifest(&manifest) {
886+
assert!(
887+
error.contains(
888+
"snapshot manifest is malformed: its snapshot ID, field 18, \
889+
is not a GUID of 16 bytes"
890+
),
891+
"{error}"
892+
);
893+
assert!(!error.contains("[E_SNAPSHOT_VERSION]"), "{error}");
894+
}
895+
let manifest = [other_version.as_slice(), field].concat();
896+
for error in read_errors_with_manifest(&manifest) {
897+
assert!(error.contains("[E_SNAPSHOT_VERSION]"), "{error}");
898+
assert!(
899+
error.contains("snapshot manifest version 3 is not supported"),
900+
"{error}"
901+
);
902+
}
903+
}
904+
}
905+
794906
#[test]
795907
fn read_snapshot_rejects_a_command_line_over_the_limit_without_rewriting_it() {
796908
let dir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)