use std::path::PathBuf;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(windows)]
mod windows;
#[cfg(test)]
mod tests;
#[cfg(all(target_os = "macos", feature = "test-support"))]
pub mod fixture;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObservedPeer {
Unobserved,
Observed(PeerObservation),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PeerObservation {
pub user: PeerUser,
pub pid: u32,
pub exe: Option<PathBuf>,
pub signing_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PeerUser {
Uid(u32),
Sid(String),
}
#[derive(Clone)]
#[non_exhaustive]
pub struct CapturedPeer {
observation: ObservedPeer,
#[cfg(target_os = "macos")]
token: Option<macos::AuditToken>,
}
impl std::fmt::Debug for CapturedPeer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CapturedPeer")
.field("observation", &self.observation)
.finish_non_exhaustive()
}
}
impl From<ObservedPeer> for CapturedPeer {
fn from(observation: ObservedPeer) -> Self {
Self {
observation,
#[cfg(target_os = "macos")]
token: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodeRequirementOutcome {
Satisfied(VerifiedCode),
NotSatisfied,
Unverifiable(CodeVerificationError),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VerifiedCode {
pub short_version: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodeVerificationError {
UnsupportedPlatform,
MissingAuditToken,
FrameworkUnavailable,
InvalidRequirement,
GuestUnavailable,
SigningInformationUnavailable,
ValidityCheckFailed(i32),
}
impl CapturedPeer {
pub fn observation(&self) -> &ObservedPeer {
&self.observation
}
pub fn has_audit_token(&self) -> bool {
#[cfg(target_os = "macos")]
return self.token.is_some();
#[cfg(not(target_os = "macos"))]
false
}
#[cfg(target_os = "macos")]
pub fn observe_code(&self) -> ObservedPeer {
macos::observe_code(self, ForcedFailures::default())
}
pub fn check_code_requirement(&self, requirement: &str) -> CodeRequirementOutcome {
#[cfg(target_os = "macos")]
return macos::check_requirement(self.token, requirement);
#[cfg(not(target_os = "macos"))]
{
let _ = requirement;
CodeRequirementOutcome::Unverifiable(CodeVerificationError::UnsupportedPlatform)
}
}
}
#[cfg(unix)]
pub fn capture_unix_peer(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
#[cfg(target_os = "macos")]
return macos::capture(socket.as_fd(), ForcedFailures::default());
#[cfg(not(target_os = "macos"))]
observe_unix_peer(socket).into()
}
#[cfg(unix)]
pub fn capture_unix_peer_credentials(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
#[cfg(target_os = "macos")]
return macos::capture_credentials(socket.as_fd(), ForcedFailures::default());
#[cfg(not(target_os = "macos"))]
capture_unix_peer(socket)
}
#[cfg(unix)]
pub fn capture_unix_peer_with_diagnostic_pid(
socket: &impl std::os::fd::AsFd,
) -> (CapturedPeer, Option<u32>) {
#[cfg(target_os = "linux")]
{
let (peer, diagnostic_pid) = linux::observe_with_pid(
socket.as_fd(),
ForcedFailures::default(),
linux::pidfd_alive,
);
(peer.into(), diagnostic_pid)
}
#[cfg(not(target_os = "linux"))]
{
use std::os::fd::AsRawFd;
(
capture_unix_peer_credentials(socket),
unix_peer_pid(socket.as_fd().as_raw_fd()),
)
}
}
#[cfg(unix)]
pub(crate) fn unix_peer_pid(socket: std::os::fd::RawFd) -> Option<u32> {
#[cfg(target_os = "linux")]
return linux::peer_pid(socket);
#[cfg(target_os = "macos")]
return macos::peer_pid(socket);
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = socket;
None
}
}
#[derive(Debug, Clone)]
pub struct DiagnosticProcess {
pid: u32,
#[cfg(any(target_os = "linux", target_os = "macos"))]
start: Option<u64>,
#[cfg(windows)]
process: Option<std::sync::Arc<std::os::windows::io::OwnedHandle>>,
}
impl DiagnosticProcess {
pub fn capture(pid: u32) -> Self {
Self {
pid,
#[cfg(any(target_os = "linux", target_os = "macos"))]
start: crate::start_time(pid),
#[cfg(windows)]
process: windows::open_diagnostic_process(pid).map(std::sync::Arc::new),
}
}
pub fn pid(&self) -> u32 {
self.pid
}
pub fn short_name(&self) -> Option<String> {
#[cfg(target_os = "linux")]
let name = {
let bytes = std::fs::read(format!("/proc/{}/comm", self.pid)).ok()?;
String::from_utf8_lossy(bytes.strip_suffix(b"\n").unwrap_or(&bytes)).into_owned()
};
#[cfg(target_os = "macos")]
let name = crate::process_tree::program_name(self.pid);
#[cfg(any(target_os = "linux", target_os = "macos"))]
return (!name.is_empty() && self.start? == crate::start_time(self.pid)?).then_some(name);
#[cfg(windows)]
return windows::process_name(self.process.as_deref()?);
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
None
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ForcedFailures {
pub user: bool,
pub exe: bool,
pub signing_id: bool,
pub peer_pidfd: bool,
}
#[cfg(unix)]
pub fn observe_unix_peer(socket: &impl std::os::fd::AsFd) -> ObservedPeer {
unix_peer(socket.as_fd(), ForcedFailures::default())
}
#[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
}
}
#[cfg(windows)]
pub fn observe_pipe_client(pipe: &impl std::os::windows::io::AsHandle) -> ObservedPeer {
windows::observe(pipe.as_handle(), ForcedFailures::default())
}
#[cfg(windows)]
pub fn capture_pipe_client_with_diagnostic_pid(
pipe: &impl std::os::windows::io::AsRawHandle,
) -> std::io::Result<(CapturedPeer, u32)> {
let pid = crate::named_pipe::local_client_pid(pipe.as_raw_handle())?;
Ok((
windows::observe_local_process(pid, ForcedFailures::default()).into(),
pid,
))
}
#[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)
}