Skip to content

implemented runuser - #612

Open
mustafaelrasheid wants to merge 7 commits into
uutils:mainfrom
mustafaelrasheid:implement_runuser
Open

mustafaelrasheid wants to merge 7 commits into
uutils:mainfrom
mustafaelrasheid:implement_runuser

Conversation

@mustafaelrasheid

Copy link
Copy Markdown

I've implemented a basic version of runuser and I feel honored to be able to share it.
This is my first ever contribution and I was a bit hesitant to share it because of how complex the argument parsing was, but it came out well.

.map(|s| s.to_string())
.collect::<Vec<_>>()
.to_vec();
let (username, path, command_args) = match matches.get_one::<String>("user") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this into a function?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, do you mean the parsing for username, path and command_args or the entirety of the args parsing?

@squell

squell commented Oct 7, 2026

Copy link
Copy Markdown

I just randomly noticed this PR. I haven't look at the code (and have no comment about it), but I do have a general observation.

In util-linux, runuser is very similar to su (from the manpage, it looks very much like a su that is always run as root and thus skips PAM authentication). And a clean-room Rust implementation of su already exists 😏.

Now, su-rs wasn't intended to be a 100% drop-in replacement for util-linux' flavour of su--notably, /etc/login.defs isn't parsed--but its rather rich command flag support is obviously informed by it (implementation-wise it is not informed by it at all but stands more on the shoulders of ogsudo and shadow-utils).

In particular, it has PTY support (same code paths as those in sudo-rs, and enabled by default even), which would be quite essential for a modern implementation of runuser, since its prime use case (privilege de-escalation) is the one in which TTY hijacking is more of a concern than in privilege escalation.

So we could also open an issue over at sudo-rs to add a runuser binary.


if status != 0 {
return Err(USimpleError::new(
status + 128,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this returns 128+status for a normal non-zero exit, runuser should just propagate the child's code
and code().unwrap_or(0) above turns a signal kill into success, 128+signal is expected there

let command = match matches.get_one::<String>("session_command") {
Some(cmd) => cmd,
None => {
sep_session()?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setsid() in the parent fails with EPERM when runuser is a process group leader (the usual case from a shell)
shouldn't it be done in the child, via pre_exec?


#[cfg(target_os = "linux")]
pub fn set_ids(uid: u32, gid: u32, supp_gids: &Vec<Gid>) -> UResult<()> {
setgroups(supp_gids.as_slice())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without -G this calls setgroups with an empty list, so the target user loses all its supplementary groups
please use initgroups for the user, and add the -G ones on top

pub shell: String,
}

pub fn get_user_info(username: &str) -> Result<Option<UserEntry>, Box<dyn Error>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why parse /etc/passwd by hand? this ignores NSS (ldap, sssd...)
nix::unistd::User::from_name / Group::from_name are already available with the features you enabled

.short('w')
.long("whitelist-environment")
.value_name("list")
.num_args(0..),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-w takes a comma-separated list, num_args(0..) will eat the user and the command that follow
please add a test

}

#[cfg(target_os = "linux")]
pub fn get_group_info(groupname: &str) -> Result<Option<u32>, Box<dyn Error>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a thiserror enum instead of Box<dyn Error> + USimpleError everywhere


if is_login {
if preserve_env {
println!("--preserve-environment is ignored in case `--login` is enabled");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should go to stderr, not stdout

}

#[cfg(not(target_os = "linux"))]
fn unsupported_platform() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing #[test], so this never runs
also, the tests only cover error paths, could you please add some that actually run a command (skipped when not root)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants