Skip to content

Commit b2dfce4

Browse files
Enrique SaurezCopilot
andcommitted
host: verify CLI launch liveness from its log and endpoint
Port only SL3.3a onto coordinator-qualified SL3.2b 99d0045. Retain the prepared log claim and identity, hold and recheck the endpoint owner, and require the Linux log match. Preserve Windows job/handle fallback primitives and the donor fork-race fix without adding recovery or signaling. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: eb3cc7e0-f51e-45ed-b911-c7b214740e05
1 parent cf5f914 commit b2dfce4

6 files changed

Lines changed: 367 additions & 1 deletion

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! Bounded launch proofs, without recovery, record removal, or process termination.
2+
3+
use std::io;
4+
5+
use super::StateFile;
6+
use super::locked::LockedState;
7+
use super::record::LaunchJournal;
8+
use crate::host::platform;
9+
use crate::host::process::{BootId, Process, ProcessRef};
10+
use crate::{Error, Result};
11+
12+
impl LockedState {
13+
/// Returns whether a launch process may still run. Only a released claim proves exit;
14+
/// failure to examine it proves neither, and preserves the original error.
15+
pub(crate) fn claim_held(&self, journal: &LaunchJournal) -> Result<bool> {
16+
let path = self.path(StateFile::Log);
17+
if journal.vm_dir != self.dir() || journal.log != path {
18+
return Err(Error::io(
19+
"the launch journal belongs to another sandbox directory",
20+
));
21+
}
22+
platform::log_claim_held(&path, journal.log_identity)
23+
.map_err(|error| Error::io("cannot check the launch log claim").with_source(error))
24+
}
25+
26+
/// Holds the server of the recorded endpoint, rechecks its ownership, and on Linux proves
27+
/// that this very process also holds the launch's log. An error is not proof of exit.
28+
pub(crate) fn endpoint_owner(&self, journal: &LaunchJournal) -> Result<Process> {
29+
if !self.claim_held(journal)? {
30+
return Err(Error::io("the launch log claim is released"));
31+
}
32+
let process =
33+
hold_endpoint_server(|| platform::endpoint_server_pid(&journal.endpoint), find)?;
34+
let reference = process.reference();
35+
let matches =
36+
platform::serves_launch(reference.pid, reference.start_time, journal.log_identity)
37+
.map_err(|error| {
38+
Error::io("cannot verify the endpoint owner's log").with_source(error)
39+
})?;
40+
if !matches {
41+
return Err(Error::io(
42+
"the endpoint owner does not hold the launch log as its standard output",
43+
));
44+
}
45+
Ok(process)
46+
}
47+
}
48+
49+
/// Opens only the process whose start time was just read, never another that reused its ID.
50+
fn find(pid: u32) -> io::Result<Option<Process>> {
51+
let Some(start_time) = platform::process_start_time(pid)? else {
52+
return Ok(None);
53+
};
54+
Process::open(&ProcessRef {
55+
pid,
56+
start_time,
57+
boot: BootId::current()?,
58+
})
59+
}
60+
61+
/// Adapted from the old store's owner check: the second endpoint lookup happens only after
62+
/// the process is held. The callbacks keep replacement races testable without test-only hooks.
63+
pub(super) fn hold_endpoint_server(
64+
mut server_pid: impl FnMut() -> io::Result<Option<u32>>,
65+
hold: impl FnOnce(u32) -> io::Result<Option<Process>>,
66+
) -> Result<Process> {
67+
let pid = server_pid()
68+
.map_err(|error| Error::io("cannot examine the control endpoint").with_source(error))?
69+
.ok_or_else(|| Error::io("no process serves the control endpoint"))?;
70+
let process = hold(pid)
71+
.map_err(|error| Error::io("cannot hold the control endpoint owner").with_source(error))?
72+
.ok_or_else(|| Error::io("the control endpoint owner exited before it could be held"))?;
73+
let again = server_pid()
74+
.map_err(|error| Error::io("cannot recheck the control endpoint").with_source(error))?;
75+
if again != Some(pid) {
76+
return Err(Error::io(
77+
"the control endpoint changed while its owner was held",
78+
));
79+
}
80+
Ok(process)
81+
}

‎nvx-rs/src/host/cli/store/mod.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
//! A locked launch writes its prepared capability and outputs before spawn, records the spawned
3131
//! process durably, and publishes the caller's exact saved VM only at commit. Dropping an
3232
//! uncommitted launch keeps its journal; these transitions do not recover or signal a process.
33+
//! Its journal also identifies the inherited log claim: held means a launch process may run,
34+
//! and released proves exit. An endpoint owner is held and rechecked before it is returned;
35+
//! Linux additionally verifies that this process holds that launch's log.
3336
#![cfg_attr(
3437
not(test),
3538
expect(
@@ -40,6 +43,7 @@
4043

4144
mod files;
4245
mod launch;
46+
mod liveness;
4347
mod locked;
4448
mod names;
4549
mod record;

‎nvx-rs/src/host/cli/store/record.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
99
use super::StateFile;
1010
use super::files;
1111
use super::locked::LockedState;
12+
use crate::host::platform;
1213
use crate::host::process::ProcessRef;
1314
use crate::host::runtime::RuntimeIdentity;
1415
use crate::host::vm::{Prepared, SavedVm, VmDir, VmOutputs};
@@ -21,6 +22,7 @@ pub(crate) struct LaunchJournal {
2122
pub(super) vm_dir: PathBuf,
2223
pub(super) capability: [u8; CAPABILITY_LEN],
2324
pub(super) log: PathBuf,
25+
pub(super) log_identity: (u64, u64),
2426
pub(super) outcome: PathBuf,
2527
pub(super) endpoint: String,
2628
pub(super) console: Option<String>,
@@ -34,6 +36,7 @@ impl fmt::Debug for LaunchJournal {
3436
.debug_struct("LaunchJournal")
3537
.field("vm_dir", &self.vm_dir)
3638
.field("log", &self.log)
39+
.field("log_identity", &self.log_identity)
3740
.field("outcome", &self.outcome)
3841
.field("endpoint", &self.endpoint)
3942
.field("console", &self.console)
@@ -64,10 +67,15 @@ impl LaunchJournal {
6467
"the prepared launch belongs to another sandbox directory",
6568
));
6669
}
70+
platform::claim_log(prepared.log())
71+
.map_err(|error| Error::io("cannot claim the prepared VM log").with_source(error))?;
72+
let log_identity = platform::opened_file_identity(prepared.log())
73+
.map_err(|error| Error::io("cannot identify the prepared VM log").with_source(error))?;
6774
Ok(Self {
6875
vm_dir: vm_dir.path().to_path_buf(),
6976
capability: *prepared.capability().as_bytes(),
7077
log: log.clone(),
78+
log_identity,
7179
outcome: outcome.clone(),
7280
endpoint: endpoint.clone(),
7381
console: console.clone(),

‎nvx-rs/src/host/cli/store/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::host::cli::deadline::Deadline;
99
use crate::host::store::{JOURNAL_NAME, RUNTIME_NAME, SandboxStore};
1010

1111
mod launch;
12+
mod liveness;
1213

1314
/// How long a test waits for a lock that it released to be free.
1415
///

‎nvx-rs/src/host/cli/store/tests/launch.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use crate::host::vm::{self, Prepared, SavedVm, VmDir};
1111
use crate::hypervisor::Hypervisor;
1212
use crate::protocol::CAPABILITY_LEN;
1313

14-
fn prepared(state: &StateDir, console: ConsoleAttachment) -> (Prepared, RuntimeIdentity) {
14+
pub(super) fn prepared(
15+
state: &StateDir,
16+
console: ConsoleAttachment,
17+
) -> (Prepared, RuntimeIdentity) {
1518
let identity = |text| ArtifactIdentity::new(text).unwrap();
1619
let runtime = Runtime {
1720
openvmm: "missing-openvmm".into(),

0 commit comments

Comments
 (0)