From bbf42506778d0b4c59fc1577a7bd9b1179d41211d9ef40cb6cd4e85fd33c2f65 Mon Sep 17 00:00:00 2001 From: sisungo Date: Fri, 9 Oct 2026 09:56:49 +0000 Subject: [PATCH] feat: require root privilege to call trigger Signed-off-by: sisungo --- daemon/src/ipc.rs | 24 +++++++++++++++++------- libs/ipc/src/server.rs | 29 ++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/daemon/src/ipc.rs b/daemon/src/ipc.rs index 5dd1d9d..a82bdb7 100644 --- a/daemon/src/ipc.rs +++ b/daemon/src/ipc.rs @@ -1,6 +1,6 @@ use ipc::{ repr::{Error, Request}, - server::{Connection, Listener}, + server::{ClientIdentity, Connection, Listener}, }; use rule::parser::When; use rustc_hash::FxHashMap; @@ -19,10 +19,8 @@ async fn server(listener: Listener) { continue; }; tracing::debug!( - "Established connection with {{ uid={}, gid={}, pid={} }}", - connection.uid, - connection.gid, - connection.pid + "Established connection with {:?}", + connection.client_identity(), ); tokio::spawn(handle_connection(connection)); } @@ -32,7 +30,9 @@ async fn handle_connection(mut connection: Connection) -> std::io::Result<()> { loop { let request = connection.recv().await?; let handler = match request { - Request::Trigger(set, action) => handler(trigger(set, action)), + Request::Trigger(set, action) => { + handler(trigger(connection.client_identity(), set, action)) + } Request::ReloadRules => handler(reload_rules()), Request::ListDevices(when) => handler(list_devices(when)), Request::WaitForIdle => handler(wait_for_idle()), @@ -44,7 +44,17 @@ async fn handle_connection(mut connection: Connection) -> std::io::Result<()> { } } -async fn trigger(set: String, action: String) -> Result<(), Error> { +async fn trigger( + identity: Option, + set: String, + action: String, +) -> Result<(), Error> { + let Some(identity) = identity else { + return Err(Error::PermissionDenied); + }; + if !identity.is_root() { + return Err(Error::PermissionDenied); + } if !crate::trigger::VALID_ACTIONS.contains(&action.as_str()) { return Err(Error::BadRequest); } diff --git a/libs/ipc/src/server.rs b/libs/ipc/src/server.rs index e8d1eec..bb8800c 100644 --- a/libs/ipc/src/server.rs +++ b/libs/ipc/src/server.rs @@ -18,14 +18,9 @@ impl Listener { } pub async fn accept(&self) -> std::io::Result { - let stream = self.0.accept().await?.0; - let cred = stream.peer_cred()?; Ok(Connection { - stream, + stream: self.0.accept().await?.0, buf: Vec::with_capacity(128), - uid: cred.uid(), - gid: cred.gid(), - pid: cred.pid().unwrap_or_default() as _, }) } } @@ -34,9 +29,6 @@ impl Listener { pub struct Connection { stream: UnixStream, buf: Vec, - pub uid: u32, - pub gid: u32, - pub pid: u32, } impl Connection { pub async fn recv(&mut self) -> std::io::Result { @@ -58,4 +50,23 @@ impl Connection { self.stream.write_all(reply.as_bytes()).await?; Ok(()) } + + pub fn client_identity(&self) -> Option { + let cred = self.stream.peer_cred().ok()?; + Some(ClientIdentity { + uid: cred.uid(), + pid: cred.pid()?, + }) + } +} + +#[derive(Debug, Clone)] +pub struct ClientIdentity { + pub uid: u32, + pub pid: i32, +} +impl ClientIdentity { + pub fn is_root(&self) -> bool { + self.uid == 0 + } }