Skip to content

Commit 946527f

Browse files
authored
Merge pull request #2309 from microsoft/agents/issue-2306-restore-store-share
sandbox: refuse a restore's share of its snapshot store at provision
2 parents d75c47c + 1c7d89d commit 946527f

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎doc/run.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,9 +1486,10 @@ otherwise read or change: `provision` refuses a share whose resolved host path
14861486
equals, contains, or lies inside the sandbox's state directory, its image, its
14871487
scratch image, or the OpenVMM, kernel, or initramfs of the artifact root, or
14881488
the OpenVMM that `NVX_OPENVMM` names, before it records the sandbox, and
1489-
`start` refuses one again before it launches the VM. The `start` of a restored
1490-
sandbox also refuses a share that reaches its snapshot store, whose files the
1491-
view of each restore links, before it takes the snapshot's lock.
1489+
`start` refuses one again before it launches the VM. A restored sandbox also
1490+
refuses a share that reaches its snapshot store, whose files the view of each
1491+
restore links: the provision of `snapshot restore` before it records the
1492+
sandbox, and each `start` before it takes the snapshot's lock.
14921493
`start` resolves the recorded host paths again, so a share that came to reach
14931494
one of these files through a link since `provision` is refused too. A
14941495
`provision` that finds no artifact root leaves the artifacts to `start`. A

‎nvx/src/host/cli/sandbox/lifecycle/start/tests/restore.rs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,55 @@ fn snapshot_restore_records_its_source_and_starts_a_restore_of_it() {
928928
assert!(!state.exists());
929929
}
930930

931+
/// `snapshot restore` refuses a share that reaches its snapshot store, whose files the view of
932+
/// each restore links, as every start of its sandbox would refuse it: before it records the
933+
/// sandbox, so that no provisioned sandbox stays behind, which a corrected restore into the same
934+
/// state directory would find provisioned.
935+
#[test]
936+
fn snapshot_restore_refuses_a_share_of_its_snapshot_store_before_it_provisions() {
937+
let fixture = Fixture::new(false);
938+
let root = artifact_root(&fixture.state("checkout"));
939+
let layer = fixture.state("distro.erofs");
940+
fs::write(&layer, "distro").unwrap();
941+
let store = fixture.root.path().join("store");
942+
let state = fixture.state("command");
943+
// The store itself, and a directory inside it.
944+
for shared in [store.clone(), store.join("snapshots")] {
945+
let arguments = [
946+
"restore",
947+
"source",
948+
"--store-dir",
949+
store.to_str().unwrap(),
950+
"--state-dir",
951+
state.to_str().unwrap(),
952+
"--layer",
953+
&format!(
954+
"distro,{},11111111-1111-1111-1111-111111111111",
955+
layer.display()
956+
),
957+
"--upper",
958+
"ram",
959+
"--hypervisor",
960+
if cfg!(windows) { "whp" } else { "kvm" },
961+
"--mount",
962+
&format!("/data,{},ro", shared.display()),
963+
]
964+
.map(str::to_owned);
965+
let (command, unrecognized) = snapshots::parse(&arguments).unwrap();
966+
assert!(unrecognized.is_empty(), "{unrecognized:?}");
967+
let error = snapshots::run(Some(&root), command).unwrap_err();
968+
assert_eq!(error.code(), Some(ErrorCode::Unsupported), "{error:?}");
969+
let message = error.message().unwrap_or_default();
970+
assert!(
971+
message.contains("overlaps the sandbox's snapshot store"),
972+
"{message}"
973+
);
974+
assert!(!state.exists(), "{}", shared.display());
975+
}
976+
assert!(fixture.snapshot_free());
977+
assert!(fixture.spawned().is_empty());
978+
}
979+
931980
/// `snapshot restore` refuses a hypervisor that this host's operating system cannot run, which
932981
/// every start of its sandbox would fail on, before it opens the store or locks the snapshot, so
933982
/// that it reports that refusal and leaves the store as it was, whatever the snapshot.

‎nvx/src/host/cli/sandbox/mod.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ fn run_from(root: Option<&str>, options: Options, source: Option<&Source>) -> Re
377377
export.as_ref().map(|export| &export.description),
378378
located.as_ref(),
379379
)?;
380+
// The view of each restore links the snapshot's files, which a share of the store
381+
// would give the guest to change, so every start of a restored sandbox refuses it.
382+
if let Some(source) = source {
383+
refuse_own_files(
384+
Some(("snapshot store", &source.store)),
385+
layer.path(),
386+
scratch.as_deref(),
387+
export.as_ref().map(|export| &export.description),
388+
None,
389+
)?;
390+
}
380391
lifecycle::provision(
381392
state_dir,
382393
&layer,

0 commit comments

Comments
 (0)