From a7e75da771f40af311f1d46a9affec2fd988e183 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 01:14:50 +0200 Subject: [PATCH] feat(M7.2): authenticate and bound local clients Tests: cargo test --locked -p fader_master --lib --all-features Tests: cargo test --locked -p fader_master --test local_socket --all-features Lint: cargo clippy --locked -p fader_master --all-targets --all-features -- -D warnings --- Cargo.toml | 2 +- fader_master/src/local.rs | 52 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 282f4c9..94be40f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ midir = "0.11.1" proptest = "1.11.0" ratatui = { version = "0.29.0", features = ["all-widgets", "serde"] } regex = "1.13.1" -rustix = { version = "1.1.5", features = ["process"] } +rustix = { version = "1.1.5", features = ["net", "process"] } serde = { version = "1.0.228", features = ["derive"] } tempfile = "3.27.0" thiserror = "2.0.21" diff --git a/fader_master/src/local.rs b/fader_master/src/local.rs index 10a2f15..c58d179 100644 --- a/fader_master/src/local.rs +++ b/fader_master/src/local.rs @@ -22,6 +22,7 @@ use crate::{ }; const CLIENT_TIMEOUT: Duration = Duration::from_millis(500); +const SERVER_CLIENT_TIMEOUT: Duration = Duration::from_millis(20); const SERVER_TICK: Duration = Duration::from_millis(10); #[derive(Debug, Error)] @@ -32,6 +33,8 @@ pub enum LocalError { AlreadyRunning(PathBuf), #[error("refusing unsafe server socket {path}: {reason}")] UnsafeSocket { path: PathBuf, reason: String }, + #[error("refusing local client owned by uid {actual}; server uid is {expected}")] + UnauthorizedPeer { actual: u32, expected: u32 }, #[error("local socket operation failed for {path}: {source}")] Socket { path: PathBuf, @@ -100,7 +103,10 @@ impl OwnedListener { pub fn accept(&self) -> Result, LocalError> { match self.listener.accept() { - Ok((stream, _)) => Ok(Some(stream)), + Ok((stream, _)) => { + validate_peer(&stream)?; + Ok(Some(stream)) + } Err(error) if error.kind() == io::ErrorKind::WouldBlock => Ok(None), Err(source) => Err(LocalError::Socket { path: self.path.clone(), @@ -193,8 +199,8 @@ impl LocalServer { Err(RecvTimeoutError::Disconnected) => return Err(LocalError::WorkerDisconnected), } while let Some(mut client) = self.listener.accept()? { - let _ = client.set_read_timeout(Some(CLIENT_TIMEOUT)); - let _ = client.set_write_timeout(Some(CLIENT_TIMEOUT)); + let _ = client.set_read_timeout(Some(SERVER_CLIENT_TIMEOUT)); + let _ = client.set_write_timeout(Some(SERVER_CLIENT_TIMEOUT)); if let Err(error) = self.serve_client(&mut client) && matches!(error, LocalError::WorkerCommand) { @@ -235,6 +241,21 @@ impl LocalServer { } } +fn validate_peer(stream: &UnixStream) -> Result<(), LocalError> { + let peer = + rustix::net::sockopt::socket_peercred(stream).map_err(|source| LocalError::Socket { + path: PathBuf::from("accepted local client"), + source: source.into(), + })?; + let expected = rustix::process::getuid().as_raw(); + let actual = peer.uid.as_raw(); + if actual == expected { + Ok(()) + } else { + Err(LocalError::UnauthorizedPeer { actual, expected }) + } +} + fn validate_socket(path: &Path) -> Result<(), LocalError> { let metadata = fs::symlink_metadata(path).map_err(|source| LocalError::Socket { path: path.to_owned(), @@ -268,3 +289,28 @@ fn remove_stale_socket(path: &Path) -> Result<(), LocalError> { source, }) } + +#[cfg(test)] +mod tests { + use std::time::Instant; + + use super::*; + + #[test] + fn accepted_unix_peer_credentials_match_the_server_user() { + let (server, _client) = UnixStream::pair().unwrap(); + validate_peer(&server).unwrap(); + } + + #[test] + fn stalled_client_frame_is_dropped_within_the_server_budget() { + let (mut server, _client) = UnixStream::pair().unwrap(); + server + .set_read_timeout(Some(SERVER_CLIENT_TIMEOUT)) + .unwrap(); + let started = Instant::now(); + let error = read_frame::(&mut server).unwrap_err(); + assert!(matches!(error, crate::ipc::FrameError::Truncated(_))); + assert!(started.elapsed() < Duration::from_millis(250)); + } +}