subc-os 0.1.22

Operating-system primitives for the subc daemon that need unsafe code, behind a safe API: per-process identity, signalling, memory and CPU usage, and the launch-nonce descriptor handoff.
Documentation
//! Who is at the other end of an accepted local-endpoint connection: a
//! Unix-domain socket on macOS and Linux, a named-pipe client on Windows.
//!
//! Unlike [`crate::socket_peer`], which searches process tables for the holder
//! of a loopback TCP socket, every value here comes from a kernel primitive
//! tied to the connection itself and read when it is accepted:
//!
//! - macOS: the `LOCAL_PEERTOKEN` audit token. `uid` is the token's effective
//!   uid. The guest code for the token (`SecCodeCopyGuestWithAttributes`)
//!   gives `exe`, its path, and `signing_id`, its signing identifier, which is
//!   reported only when `SecCodeCheckValidity` with no requirement passes.
//! - Linux: `SO_PEERPIDFD` (kernel 6.5 and later) and the `SO_PEERCRED` uid
//!   and pid. `exe` is read from `/proc/<pid>/exe` while the pidfd is held, and
//!   kept only if the pidfd still reports the process alive after the read, so
//!   a pid reused after the peer exited cannot lend its executable. Without
//!   `SO_PEERPIDFD` the peer is [`ObservedPeer::Unobserved`]: the credentials
//!   alone cannot pin the process the pid names. There is no `signing_id`.
//! - Windows: `GetNamedPipeClientProcessId` on a local pipe client, then a
//!   handle on that pid opened with `PROCESS_QUERY_LIMITED_INFORMATION`: `sid`
//!   is the user SID of its token and `exe` comes from
//!   `QueryFullProcessImageNameW`. A client on another machine is never
//!   observed. There is no `signing_id`.
//!
//! If the user (uid or SID) cannot be read, the peer is unobserved. If only
//! `exe` or `signing_id` cannot be read, that value alone is absent.
//!
//! An observation describes the peer; it is not an authorization decision.

use std::path::PathBuf;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(windows)]
mod windows;

#[cfg(test)]
mod tests;

/// The result of observing the peer of an accepted local connection.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObservedPeer {
    /// The platform lacks the primitive, or the peer's user could not be read.
    Unobserved,
    /// The kernel named the peer process and its user.
    Observed(PeerObservation),
}

/// What the kernel reported about an observed peer process.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PeerObservation {
    /// The peer's user: a uid on macOS and Linux, a SID on Windows.
    pub user: PeerUser,
    /// The peer's process id as the kernel recorded it for this connection.
    pub pid: u32,
    /// The peer's executable path, absent when it could not be read.
    pub exe: Option<PathBuf>,
    /// The peer's code-signing identifier (macOS only), present only when its
    /// signature passed `SecCodeCheckValidity`.
    pub signing_id: Option<String>,
}

/// The user a peer runs as.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PeerUser {
    /// Effective uid (macOS, from the audit token) or uid (Linux, from
    /// `SO_PEERCRED`).
    Uid(u32),
    /// The user SID of the peer process's token, in `S-1-…` string form.
    Sid(String),
}

/// Read failures a test can force, to show how each one shapes the result
/// without needing a platform that actually fails that read.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ForcedFailures {
    /// The uid or SID read fails: the peer is unobserved.
    pub user: bool,
    /// The executable read fails: only `exe` is absent.
    pub exe: bool,
    /// macOS: `SecCodeCheckValidity` fails, as for a broken signature: only
    /// `signing_id` is absent.
    pub signing_id: bool,
    /// Linux: the kernel does not support `SO_PEERPIDFD`: the peer is
    /// unobserved.
    pub peer_pidfd: bool,
}

/// Observe the peer of an accepted Unix-domain stream socket. Call it when the
/// connection is accepted. On a Unix other than macOS and Linux the peer is
/// unobserved.
#[cfg(unix)]
pub fn observe_unix_peer(socket: &impl std::os::fd::AsFd) -> ObservedPeer {
    unix_peer(socket.as_fd(), ForcedFailures::default())
}

/// [`observe_unix_peer`] with read failures forced, for tests.
#[cfg(all(unix, feature = "test-support"))]
pub fn observe_unix_peer_forcing(
    socket: &impl std::os::fd::AsFd,
    forced: ForcedFailures,
) -> ObservedPeer {
    unix_peer(socket.as_fd(), forced)
}

#[cfg(unix)]
fn unix_peer(socket: std::os::fd::BorrowedFd<'_>, forced: ForcedFailures) -> ObservedPeer {
    #[cfg(target_os = "macos")]
    return macos::observe(socket, forced);
    #[cfg(target_os = "linux")]
    return linux::observe(socket, forced);
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        let _ = (socket, forced);
        ObservedPeer::Unobserved
    }
}

/// Observe the client of a connected named-pipe server instance. Call it when
/// the client has connected. A client on another machine is unobserved.
#[cfg(windows)]
pub fn observe_pipe_client(pipe: &impl std::os::windows::io::AsHandle) -> ObservedPeer {
    windows::observe(pipe.as_handle(), ForcedFailures::default())
}

/// [`observe_pipe_client`] with read failures forced, for tests.
#[cfg(all(windows, feature = "test-support"))]
pub fn observe_pipe_client_forcing(
    pipe: &impl std::os::windows::io::AsHandle,
    forced: ForcedFailures,
) -> ObservedPeer {
    windows::observe(pipe.as_handle(), forced)
}