Version Information
- swhkd 1.3.0-dev (built from latest commit)
- NixOS 26.05, Linux 6.12
Describe the bug
When swhkd runs as a setuid binary (as described in the README), commands spawned by swhkd are missing the user's supplementary groups. For example, if the user is in the wheel group, sudo fails with "user may not run sudo" because the spawned shell has Groups: 0 (root's groups) instead of the user's groups.
Root cause
In daemon.rs around line 207, Command::new("sh") sets .uid(invoking_uid) and .gid(user.gid.as_raw()) but does not call initgroups() to set up the user's supplementary groups. The spawned process inherits the supplementary groups of the swhkd process (running as root), which is just group 0.
When swhkd is launched via sudo or pkexec, this isn't an issue because those tools call initgroups() themselves. But in setuid mode, swhkd is responsible for setting up groups before spawning commands.
Expected behavior
Commands spawned by swhkd should have the same supplementary groups as the invoking user (e.g., wheel, input, networkmanager).
Fix
Add a pre_exec call to initgroups(username, gid) before spawning commands. Something like:
unsafe {
cmd.pre_exec(move || {
nix::unistd::initgroups(&username, user.gid)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
});
}
To Reproduce
- Make swhkd setuid root (
chown root:root swhkd && chmod u+s swhkd)
- Start
swhks && swhkd
- Press a hotkey bound to e.g.
kitty
- In the spawned terminal, run
id -Gn — supplementary groups are missing
sudo fails because the user is not seen as being in wheel
Version Information
Describe the bug
When swhkd runs as a setuid binary (as described in the README), commands spawned by swhkd are missing the user's supplementary groups. For example, if the user is in the
wheelgroup,sudofails with "user may not run sudo" because the spawned shell hasGroups: 0(root's groups) instead of the user's groups.Root cause
In
daemon.rsaround line 207,Command::new("sh")sets.uid(invoking_uid)and.gid(user.gid.as_raw())but does not callinitgroups()to set up the user's supplementary groups. The spawned process inherits the supplementary groups of the swhkd process (running as root), which is just group 0.When swhkd is launched via
sudoorpkexec, this isn't an issue because those tools callinitgroups()themselves. But in setuid mode, swhkd is responsible for setting up groups before spawning commands.Expected behavior
Commands spawned by swhkd should have the same supplementary groups as the invoking user (e.g.,
wheel,input,networkmanager).Fix
Add a
pre_execcall toinitgroups(username, gid)before spawning commands. Something like:To Reproduce
chown root:root swhkd && chmod u+s swhkd)swhks && swhkdkittyid -Gn— supplementary groups are missingsudofails because the user is not seen as being inwheel