subc_os/local_peer.rs
1//! Who is at the other end of an accepted local-endpoint connection: a
2//! Unix-domain socket on macOS and Linux, a named-pipe client on Windows.
3//!
4//! Unlike [`crate::socket_peer`], which searches process tables for the holder
5//! of a loopback TCP socket, every value here comes from a kernel primitive
6//! tied to the connection itself and read when it is accepted:
7//!
8//! - macOS: the `LOCAL_PEERTOKEN` audit token. `uid` is the token's effective
9//! uid. The guest code for the token (`SecCodeCopyGuestWithAttributes`)
10//! gives `exe`, its path, and `signing_id`, its signing identifier, which is
11//! reported only when `SecCodeCheckValidity` with no requirement passes.
12//! - Linux: `SO_PEERPIDFD` (kernel 6.5 and later) and the `SO_PEERCRED` uid
13//! and pid. `exe` is read from `/proc/<pid>/exe` while the pidfd is held, and
14//! kept only if the pidfd still reports the process alive after the read, so
15//! a pid reused after the peer exited cannot lend its executable. Without
16//! `SO_PEERPIDFD` the peer is [`ObservedPeer::Unobserved`]: the credentials
17//! alone cannot pin the process the pid names. There is no `signing_id`.
18//! - Windows: `GetNamedPipeClientProcessId` on a local pipe client, then a
19//! handle on that pid opened with `PROCESS_QUERY_LIMITED_INFORMATION`: `sid`
20//! is the user SID of its token and `exe` comes from
21//! `QueryFullProcessImageNameW`. A client on another machine is never
22//! observed. There is no `signing_id`.
23//!
24//! If the user (uid or SID) cannot be read, the peer is unobserved. If only
25//! `exe` or `signing_id` cannot be read, that value alone is absent.
26//!
27//! An observation describes the peer; it is not an authorization decision.
28
29use std::path::PathBuf;
30
31#[cfg(target_os = "linux")]
32mod linux;
33#[cfg(target_os = "macos")]
34mod macos;
35#[cfg(windows)]
36mod windows;
37
38#[cfg(test)]
39mod tests;
40
41/// The result of observing the peer of an accepted local connection.
42#[derive(Clone, Debug, PartialEq, Eq)]
43pub enum ObservedPeer {
44 /// The platform lacks the primitive, or the peer's user could not be read.
45 Unobserved,
46 /// The kernel named the peer process and its user.
47 Observed(PeerObservation),
48}
49
50/// What the kernel reported about an observed peer process.
51#[derive(Clone, Debug, PartialEq, Eq)]
52pub struct PeerObservation {
53 /// The peer's user: a uid on macOS and Linux, a SID on Windows.
54 pub user: PeerUser,
55 /// The peer's process id as the kernel recorded it for this connection.
56 pub pid: u32,
57 /// The peer's executable path, absent when it could not be read.
58 pub exe: Option<PathBuf>,
59 /// The peer's code-signing identifier (macOS only), present only when its
60 /// signature passed `SecCodeCheckValidity`.
61 pub signing_id: Option<String>,
62}
63
64/// The user a peer runs as.
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub enum PeerUser {
67 /// Effective uid (macOS, from the audit token) or uid (Linux, from
68 /// `SO_PEERCRED`).
69 Uid(u32),
70 /// The user SID of the peer process's token, in `S-1-…` string form.
71 Sid(String),
72}
73
74/// Read failures a test can force, to show how each one shapes the result
75/// without needing a platform that actually fails that read.
76#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
77pub struct ForcedFailures {
78 /// The uid or SID read fails: the peer is unobserved.
79 pub user: bool,
80 /// The executable read fails: only `exe` is absent.
81 pub exe: bool,
82 /// macOS: `SecCodeCheckValidity` fails, as for a broken signature: only
83 /// `signing_id` is absent.
84 pub signing_id: bool,
85 /// Linux: the kernel does not support `SO_PEERPIDFD`: the peer is
86 /// unobserved.
87 pub peer_pidfd: bool,
88}
89
90/// Observe the peer of an accepted Unix-domain stream socket. Call it when the
91/// connection is accepted. On a Unix other than macOS and Linux the peer is
92/// unobserved.
93#[cfg(unix)]
94pub fn observe_unix_peer(socket: &impl std::os::fd::AsFd) -> ObservedPeer {
95 unix_peer(socket.as_fd(), ForcedFailures::default())
96}
97
98/// [`observe_unix_peer`] with read failures forced, for tests.
99#[cfg(all(unix, feature = "test-support"))]
100pub fn observe_unix_peer_forcing(
101 socket: &impl std::os::fd::AsFd,
102 forced: ForcedFailures,
103) -> ObservedPeer {
104 unix_peer(socket.as_fd(), forced)
105}
106
107#[cfg(unix)]
108fn unix_peer(socket: std::os::fd::BorrowedFd<'_>, forced: ForcedFailures) -> ObservedPeer {
109 #[cfg(target_os = "macos")]
110 return macos::observe(socket, forced);
111 #[cfg(target_os = "linux")]
112 return linux::observe(socket, forced);
113 #[cfg(not(any(target_os = "macos", target_os = "linux")))]
114 {
115 let _ = (socket, forced);
116 ObservedPeer::Unobserved
117 }
118}
119
120/// Observe the client of a connected named-pipe server instance. Call it when
121/// the client has connected. A client on another machine is unobserved.
122#[cfg(windows)]
123pub fn observe_pipe_client(pipe: &impl std::os::windows::io::AsHandle) -> ObservedPeer {
124 windows::observe(pipe.as_handle(), ForcedFailures::default())
125}
126
127/// [`observe_pipe_client`] with read failures forced, for tests.
128#[cfg(all(windows, feature = "test-support"))]
129pub fn observe_pipe_client_forcing(
130 pipe: &impl std::os::windows::io::AsHandle,
131 forced: ForcedFailures,
132) -> ObservedPeer {
133 windows::observe(pipe.as_handle(), forced)
134}