feat: require root privilege to call trigger

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-10-09 09:56:49 +00:00
1 parent 5700a89954
commit bbf4250677
2 files changed
+37 -16

No files matched your search

+17 -7
View File
@@ -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<ClientIdentity>,
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);
}
+20 -9
View File
@@ -18,14 +18,9 @@ impl Listener {
}
pub async fn accept(&self) -> std::io::Result<Connection> {
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<u8>,
pub uid: u32,
pub gid: u32,
pub pid: u32,
}
impl Connection {
pub async fn recv(&mut self) -> std::io::Result<Request> {
@@ -58,4 +50,23 @@ impl Connection {
self.stream.write_all(reply.as_bytes()).await?;
Ok(())
}
pub fn client_identity(&self) -> Option<ClientIdentity> {
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
}
}