Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions nvx-rs/src/host/cli/store/liveness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Bounded launch proofs, without recovery, record removal, or process termination.

use std::io;

use super::StateFile;
use super::locked::LockedState;
use super::record::LaunchJournal;
use crate::host::platform;
use crate::host::process::{BootId, Opened, Process, ProcessRef};
use crate::{Error, Result};

impl LockedState {
/// Returns whether a launch process may still run. Only a released claim proves exit;
/// failure to examine it proves neither, and preserves the original error.
pub(crate) fn claim_held(&self, journal: &LaunchJournal) -> Result<bool> {
let path = self.path(StateFile::Log);
if journal.vm_dir != self.dir() || journal.log != path {
return Err(Error::io(
"the launch journal belongs to another sandbox directory",
));
}
platform::log_claim_held(&path, journal.log_identity)
.map_err(|error| Error::io("cannot check the launch log claim").with_source(error))
}

/// Holds the server of the recorded endpoint, rechecks its ownership, and on Linux proves
/// that this very process also holds the launch's log. An error is not proof of exit.
pub(crate) fn endpoint_owner(&self, journal: &LaunchJournal) -> Result<Process> {
if !self.claim_held(journal)? {
return Err(Error::io("the launch log claim is released"));
}
let process =
hold_endpoint_server(|| platform::endpoint_server_pid(&journal.endpoint), find)?;
let reference = process.reference();
let matches =
platform::serves_launch(reference.pid, reference.start_time, journal.log_identity)
.map_err(|error| {
Error::io("cannot verify the endpoint owner's log").with_source(error)
})?;
if !matches {
return Err(Error::io(
"the endpoint owner does not hold the launch log as its standard output",
));
}
Ok(process)
}
}

/// Opens only the process whose start time was just read, never another that reused its ID.
fn find(pid: u32) -> io::Result<Option<Process>> {
let Some(start_time) = platform::process_start_time(pid)? else {
return Ok(None);
};
Ok(
match Process::open(&ProcessRef {
pid,
start_time,
boot: BootId::current()?,
})? {
Opened::Running(process) => Some(process),
Opened::Exited(_) => None,
},
)
}

/// Adapted from the old store's owner check: the second endpoint lookup happens only after
/// the process is held. The callbacks keep replacement races testable without test-only hooks.
pub(super) fn hold_endpoint_server(
mut server_pid: impl FnMut() -> io::Result<Option<u32>>,
hold: impl FnOnce(u32) -> io::Result<Option<Process>>,
) -> Result<Process> {
let pid = server_pid()
.map_err(|error| Error::io("cannot examine the control endpoint").with_source(error))?
.ok_or_else(|| Error::io("no process serves the control endpoint"))?;
let process = hold(pid)
.map_err(|error| Error::io("cannot hold the control endpoint owner").with_source(error))?
.ok_or_else(|| Error::io("the control endpoint owner exited before it could be held"))?;
let again = server_pid()
.map_err(|error| Error::io("cannot recheck the control endpoint").with_source(error))?;
if again != Some(pid) {
return Err(Error::io(
"the control endpoint changed while its owner was held",
));
}
Ok(process)
}
4 changes: 4 additions & 0 deletions nvx-rs/src/host/cli/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
//! A locked launch writes its prepared capability and outputs before spawn, records the spawned
//! process durably, and publishes the caller's exact saved VM only at commit. Dropping an
//! uncommitted launch keeps its journal; these transitions do not recover or signal a process.
//! Its journal also identifies the inherited log claim: held means a launch process may run,
//! and released proves exit. An endpoint owner is held and rechecked before it is returned;
//! Linux additionally verifies that this process holds that launch's log.
#![cfg_attr(
not(test),
expect(
Expand All @@ -40,6 +43,7 @@

mod files;
mod launch;
mod liveness;
mod locked;
mod names;
mod record;
Expand Down
8 changes: 8 additions & 0 deletions nvx-rs/src/host/cli/store/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use super::StateFile;
use super::files;
use super::locked::LockedState;
use crate::host::platform;
use crate::host::process::ProcessRef;
use crate::host::runtime::RuntimeIdentity;
use crate::host::vm::{Prepared, SavedVm, VmDir, VmOutputs};
Expand All @@ -21,6 +22,7 @@ pub(crate) struct LaunchJournal {
pub(super) vm_dir: PathBuf,
pub(super) capability: [u8; CAPABILITY_LEN],
pub(super) log: PathBuf,
pub(super) log_identity: (u64, u64),
pub(super) outcome: PathBuf,
pub(super) endpoint: String,
pub(super) console: Option<String>,
Expand All @@ -34,6 +36,7 @@ impl fmt::Debug for LaunchJournal {
.debug_struct("LaunchJournal")
.field("vm_dir", &self.vm_dir)
.field("log", &self.log)
.field("log_identity", &self.log_identity)
.field("outcome", &self.outcome)
.field("endpoint", &self.endpoint)
.field("console", &self.console)
Expand Down Expand Up @@ -64,10 +67,15 @@ impl LaunchJournal {
"the prepared launch belongs to another sandbox directory",
));
}
platform::claim_log(prepared.log())
.map_err(|error| Error::io("cannot claim the prepared VM log").with_source(error))?;
let log_identity = platform::opened_file_identity(prepared.log())
.map_err(|error| Error::io("cannot identify the prepared VM log").with_source(error))?;
Ok(Self {
vm_dir: vm_dir.path().to_path_buf(),
capability: *prepared.capability().as_bytes(),
log: log.clone(),
log_identity,
outcome: outcome.clone(),
endpoint: endpoint.clone(),
console: console.clone(),
Expand Down
1 change: 1 addition & 0 deletions nvx-rs/src/host/cli/store/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::host::cli::deadline::Deadline;
use crate::host::store::{JOURNAL_NAME, RUNTIME_NAME, SandboxStore};

mod launch;
mod liveness;

/// How long a test waits for a lock that it released to be free.
///
Expand Down
5 changes: 4 additions & 1 deletion nvx-rs/src/host/cli/store/tests/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::host::vm::{self, Prepared, SavedVm, VmDir};
use crate::hypervisor::Hypervisor;
use crate::protocol::CAPABILITY_LEN;

fn prepared(state: &StateDir, console: ConsoleAttachment) -> (Prepared, RuntimeIdentity) {
pub(super) fn prepared(
state: &StateDir,
console: ConsoleAttachment,
) -> (Prepared, RuntimeIdentity) {
let identity = |text| ArtifactIdentity::new(text).unwrap();
let runtime = Runtime {
openvmm: "missing-openvmm".into(),
Expand Down
Loading
Loading