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
This commit is contained in:
Dandaedre 2026-10-15 01:14:50 +02:00
parent 0a0be30651
commit a7e75da771
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
2 changed files with 50 additions and 4 deletions

View File

@ -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"

View File

@ -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<Option<UnixStream>, 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::<ClientRequest>(&mut server).unwrap_err();
assert!(matches!(error, crate::ipc::FrameError::Truncated(_)));
assert!(started.elapsed() < Duration::from_millis(250));
}
}