Skip to content

Commit 0368070

Browse files
author
Enrique Saurez
committed
host: require trusted log jobs for external VM launches
Expose the existing strict spawn path for caller-owned VM commands and use it in the external launcher, preserving the legacy spawn fallback. Exercise real Windows ACL, object-type and object-manager-link collisions before the process-creation window, and retain the permissive non-disk-log behavior only for ordinary spawns. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d5cb71c8-5f7f-4248-836b-4aae842c213d
1 parent de7af8d commit 0368070

7 files changed

Lines changed: 119 additions & 11 deletions

File tree

‎nvx-rs/README.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@ contract, including standard streams, allowlisted inheritance, Linux detachment
550550
and umask, Windows log job and breakaway, and cleanup of a failed child.
551551

552552
The compiled [external-launch example](examples/external-launch.rs) consumes
553-
this command, uses the existing public low-level spawn primitive without a
554-
sandbox store, checks the cold guest's boot marker and shell response, then asks
555-
it to exit cleanly. It requires the VMM's actual exit status 0 and a successful
553+
this command, uses `supervisor::spawn_vm` without a sandbox store, checks the
554+
cold guest's boot marker and shell response, then asks it to exit cleanly.
555+
It requires the VMM's actual exit status 0 and a successful
556556
outcome with complete teardown. Build with
557557
`cargo build --locked --example external-launch` and supply the backend, OpenVMM,
558558
kernel, initramfs, new VM directory, and the three artifact SHA-256 digests from
@@ -1048,6 +1048,12 @@ after, and returns a `Starting` launch that the caller commits once the VMM
10481048
serves its control endpoint, or abandons. A launch that fails never leaves an
10491049
unrecorded VMM running.
10501050

1051+
`spawn_vm` starts a caller-owned command without a store, with the same strict
1052+
Windows log-job requirement as `vm::spawn`: an untrusted job name or a log that
1053+
cannot name a job is refused before child creation. The legacy `spawn` keeps
1054+
its fallback without a job for those logs and names. Other spawn behavior is
1055+
shared.
1056+
10511057
`VmmCommand::inherit` lists the descriptors (Linux) or handles (Windows) that
10521058
the VMM inherits under their numbers, besides its standard streams, and it
10531059
inherits nothing else of the caller's: on Linux, they must be close-on-exec, a

‎nvx-rs/examples/external-launch.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A caller-owned cold launch, using the existing low-level spawn primitive without a store.
1+
//! A caller-owned cold launch, using the strict low-level spawn primitive without a store.
22
//!
33
//! Run the compiled example with a backend, OpenVMM, kernel, initramfs, new VM directory, and
44
//! the three artifact SHA-256 digests from their installation metadata, in that order.
@@ -87,7 +87,7 @@ fn main() -> Result<(), Box<dyn Error>> {
8787
breakaway_from_job,
8888
inherit: inherit.iter().map(Inherited::new).collect(),
8989
};
90-
let mut launched = supervisor::spawn(&command, capability.as_bytes(), log)?;
90+
let mut launched = supervisor::spawn_vm(&command, capability.as_bytes(), log)?;
9191
let result = (|| -> Result<(), Box<dyn Error>> {
9292
let process = launched.process()?;
9393
let deadline = Instant::now() + Duration::from_secs(60);

‎nvx-rs/public-api/linux.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,7 @@ fn nvx::host::supervisor::lock_spawns
33203320
fn nvx::host::supervisor::reattach
33213321
fn nvx::host::supervisor::spawn
33223322
fn nvx::host::supervisor::spawn_command
3323+
fn nvx::host::supervisor::spawn_vm
33233324
mod nvx::host::template
33243325
struct nvx::host::template::Acquired
33253326
field nvx::host::template::Acquired::lease

‎nvx-rs/public-api/windows.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ fn nvx::host::supervisor::lock_spawns
19051905
fn nvx::host::supervisor::reattach
19061906
fn nvx::host::supervisor::spawn
19071907
fn nvx::host::supervisor::spawn_command
1908+
fn nvx::host::supervisor::spawn_vm
19081909
mod nvx::host::template
19091910
struct nvx::host::template::Acquired
19101911
field nvx::host::template::Acquired::lease

‎nvx-rs/src/host/platform/windows/tests.rs‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::cell::Cell;
12
use std::ffi::OsString;
23
use std::fs::{self, OpenOptions};
34
use std::io::{self, Read, Write};
45
use std::mem;
56
use std::os::windows::fs::OpenOptionsExt;
67
use std::os::windows::io::AsHandle;
78
use std::path::Path;
9+
use std::rc::Rc;
810
use std::thread;
911
use std::time::{Duration, Instant};
1012

@@ -940,6 +942,81 @@ fn local_link(name: &[u16], target: &[u16]) -> OwnedHandle {
940942
owned(link).unwrap()
941943
}
942944

945+
#[test]
946+
fn vm_spawns_refuse_untrusted_log_job_names_before_creating_a_child() {
947+
use crate::host::supervisor::{self, VmmCommand};
948+
use crate::protocol::CAPABILITY_LEN;
949+
950+
let directory = tempfile::tempdir().unwrap();
951+
let log = fs::File::create(directory.path().join("vmm.log")).unwrap();
952+
let id = log_job_id(&log).unwrap().unwrap();
953+
let name = log_job_name(id);
954+
let system = std::env::var_os("SystemRoot").unwrap();
955+
let command = VmmCommand::new(
956+
Path::new(&system).join("System32").join("cmd.exe"),
957+
["/c", "exit", "0"].map(OsString::from).to_vec(),
958+
directory.path(),
959+
);
960+
let refused = || {
961+
let entered = Rc::new(Cell::new(false));
962+
let observed = Rc::clone(&entered);
963+
let result = with_spawn_window(
964+
move || observed.set(true),
965+
|| supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log.try_clone().unwrap()),
966+
);
967+
let error = match result {
968+
Err(error) => error,
969+
Ok(child) => {
970+
assert!(child.kill_and_wait(), "the unexpected child did not exit");
971+
panic!("an untrusted log-job name started a child");
972+
}
973+
};
974+
assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}");
975+
assert!(
976+
!entered.get(),
977+
"a refused spawn entered the creation window"
978+
);
979+
};
980+
981+
for sddl in ["D:P", "D:P(A;;GA;;;WD)"] {
982+
let other = job_with_descriptor(&name, sddl);
983+
refused();
984+
drop(other);
985+
}
986+
// SAFETY: the name is NUL-terminated; this event occupies only this test's fresh log name.
987+
let event = owned(unsafe { CreateEventW(ptr::null(), 1, 0, name.as_ptr()) }).unwrap();
988+
refused();
989+
drop(event);
990+
991+
let other_log = fs::File::create(directory.path().join("other.log")).unwrap();
992+
let other = create_log_job(&other_log).unwrap().unwrap();
993+
let link = local_link(&name, &object_name(raw(&other)).unwrap());
994+
refused();
995+
drop(link);
996+
drop(other);
997+
let elsewhere: Vec<u16> = local_directory()
998+
.unwrap()
999+
.into_iter()
1000+
.chain(format!(r"\nvx-vm-spawn-{:032x}", id.1).encode_utf16())
1001+
.collect();
1002+
let link = local_link(&name, &elsewhere);
1003+
refused();
1004+
drop(link);
1005+
1006+
let entered = Rc::new(Cell::new(false));
1007+
let observed = Rc::clone(&entered);
1008+
let child = with_spawn_window(
1009+
move || observed.set(true),
1010+
|| supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log),
1011+
)
1012+
.unwrap();
1013+
assert!(child.kill_and_wait(), "the trusted child did not exit");
1014+
assert!(
1015+
entered.get(),
1016+
"the successful spawn bypassed the observation"
1017+
);
1018+
}
1019+
9431020
#[test]
9441021
fn what_a_link_under_the_name_of_a_logs_job_leads_to_is_not_the_logs() {
9451022
let directory = tempfile::tempdir().unwrap();
@@ -1033,6 +1110,15 @@ fn a_vmm_whose_log_is_no_file_of_a_disk_volume_starts_without_a_job() {
10331110
std::env::temp_dir(),
10341111
);
10351112
for log in [null, pipe] {
1113+
let error =
1114+
match supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log.try_clone().unwrap()) {
1115+
Err(error) => error,
1116+
Ok(child) => {
1117+
assert!(child.kill_and_wait(), "the unexpected child did not exit");
1118+
panic!("a strict spawn accepted a log without a job");
1119+
}
1120+
};
1121+
assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}");
10361122
let launched = supervisor::spawn(&command, &[7; CAPABILITY_LEN], log).unwrap();
10371123
assert!(launched.kill_and_wait(), "the stand-in did not exit");
10381124
}

‎nvx-rs/src/host/spawn.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use crate::host::process::POLL_INTERVAL;
2121
use crate::host::process::{BootId, KILL_TIMEOUT, Process, ProcessRef};
2222
use crate::protocol::CAPABILITY_LEN;
2323

24-
/// How [`spawn`] starts a VMM: its executable, arguments, and working directory, whether it
25-
/// leaves the caller's job object on Windows, and the descriptors or handles of the caller that it
26-
/// inherits besides its standard streams.
24+
/// How [`spawn`] or [`spawn_vm`] starts a VMM: its executable, arguments, and working directory,
25+
/// whether it leaves the caller's job object on Windows, and the descriptors or handles of the
26+
/// caller that it inherits besides its standard streams.
2727
#[derive(Debug, Clone)]
2828
pub struct VmmCommand<'a> {
2929
/// The VMM's executable.
@@ -109,8 +109,18 @@ pub fn spawn(
109109
spawn_inner(command, capability, log, false)
110110
}
111111

112-
/// Starts a library VM only with a trusted log-named job on Windows.
113-
pub(crate) fn spawn_vm(
112+
/// Starts a VMM with the strict launch contract of [`crate::host::vm::VmmCommand`].
113+
///
114+
/// Uses [`spawn`]'s standard streams, inheritance allowlist, detachment, and process lifetime.
115+
/// Unlike [`spawn`], on Windows this requires a trusted log-named job: a log that is not a file
116+
/// of a disk volume, or an untrusted object or object-manager link under its job name, fails with
117+
/// [`io::ErrorKind::PermissionDenied`] before any child is created. A job of the user already
118+
/// holding that name fails with [`io::ErrorKind::AlreadyExists`], as in [`spawn`].
119+
///
120+
/// On other hosts this has the same behavior and errors as [`spawn`]. The caller retains its
121+
/// owned inherited resources until the spawn returns, and keeps or ends the returned child as
122+
/// [`Launched`] describes.
123+
pub fn spawn_vm(
114124
command: &VmmCommand<'_>,
115125
capability: &[u8; CAPABILITY_LEN],
116126
log: File,

‎nvx-rs/src/host/supervisor/mod.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
//! A launch that fails at any step ends as [`Launch::abandon`] ends it, and never leaves an
2727
//! unrecorded VMM running: a VMM whose identity is not recorded is ended through its handle first.
2828
//!
29+
//! A caller launching an owned [`crate::host::vm::VmmCommand`] without a store uses [`spawn_vm`],
30+
//! which refuses an untrusted Windows log-job name rather than taking [`spawn`]'s fallback
31+
//! without a job.
32+
//!
2933
//! ```no_run
3034
//! use std::time::{Duration, Instant};
3135
//!
@@ -234,4 +238,4 @@ pub(crate) use self::reattach::reattach_reporting;
234238
pub use self::reattach::{Reattached, reattach};
235239
pub use self::vmm::{StopOutcome, Stopped, Vmm};
236240
pub use super::platform::inherit::{Inherited, SpawnLock, lock_spawns, spawn_command};
237-
pub use super::spawn::{Launched, VmmCommand, spawn};
241+
pub use super::spawn::{Launched, VmmCommand, spawn, spawn_vm};

0 commit comments

Comments
 (0)