Skip to content

Commit 613b562

Browse files
committed
fix: set gid and uid on cmd execution not main thread
1 parent 42fce57 commit 613b562

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

swhkd/src/daemon.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
ffi::CString,
1616
fs::{self, File, OpenOptions, Permissions},
1717
io::{Read, Write},
18-
os::unix::{fs::PermissionsExt, net::UnixStream},
18+
os::unix::{fs::PermissionsExt, net::UnixStream, process::CommandExt},
1919
path::{Path, PathBuf},
2020
process::{exit, id, Command, Stdio},
2121
sync::{Arc, Mutex},
@@ -195,25 +195,20 @@ async fn main() -> Result<(), Box<dyn Error>> {
195195
let pairs = pairs.clone();
196196
let log = log.clone();
197197

198-
// Set the user and group id to the invoking user for the thread
198+
// Command execution with user privileges
199199
let user_uid = Uid::from_raw(invoking_uid);
200200
let user = User::from_uid(user_uid)
201201
.expect("Failed to get user info")
202202
.expect(&format!("User with UID {} not found", invoking_uid));
203203

204-
let username = CString::new(user.name.as_str())
205-
.expect("Failed to convert username to CString");
206-
nix::unistd::initgroups(&username, user.gid)
207-
.expect(&format!("Failed to set supplementary groups for UID {}", invoking_uid));
208-
setgid(user.gid)
209-
.expect(&format!("Failed to set group-id to {}", user.gid));
210-
setuid(user_uid)
211-
.expect(&format!("Failed to set user-id to {}", invoking_uid));
204+
let username =
205+
CString::new(user.name.as_str()).expect("Failed to convert username to CString");
212206

213-
// Command execution
214207
let mut cmd = Command::new("sh");
215208
cmd.arg("-c")
216209
.arg(command)
210+
.uid(invoking_uid)
211+
.gid(user.gid.as_raw())
217212
.stdin(Stdio::null())
218213
.stdout(match File::open(&log) {
219214
Ok(file) => file,

0 commit comments

Comments
 (0)