|
| 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 | +} |
0 commit comments