Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
return Ok(());
}

let (uid, gid) = possible_pw.as_ref().map(|p| (p.uid, p.gid)).unwrap_or((
if state.rflag { getuid() } else { geteuid() },
if state.rflag { getgid() } else { getegid() },
));
let (uid, gid) = possible_pw.as_ref().map(|p| (p.uid, p.gid)).unwrap_or({
let use_effective = !state.rflag && (state.uflag || state.gflag || state.gsflag);
if use_effective {
(geteuid(), getegid())
} else {
(getuid(), getgid())
}
});
state.ids = Some(Ids {
uid,
gid,
Expand Down
30 changes: 29 additions & 1 deletion tests/by-util/test_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use uutests::new_ucmd;
use uutests::unwrap_or_return;
use uutests::util::{TestScenario, check_coreutil_version, expected_result, is_ci, whoami};
use uutests::util_name;
use uutests::{at_and_ucmd, util_name};

const VERSION_MIN_MULTIPLE_USERS: &str = "8.31"; // this feature was introduced in GNU's coreutils 8.31

Expand Down Expand Up @@ -478,3 +478,31 @@ fn test_id_pretty_print_password_record() {
.fails()
.stderr_contains("the argument '-p' cannot be used with '-P'");
}

fn compile_preload_file_with_gcc(
c_file: &str,
so_file: &str,
) -> Result<std::process::ExitStatus, String> {
Ok(std::process::Command::new("gcc")
.args(["-fPIC", "-shared", "-o", &so_file, &c_file])
.status()
.map_err(|_| "`gcc` is not installed")?)
}

#[test]
#[cfg(all(unix, not(target_os = "android")))]
fn test_id_different_uid_and_euid() {
let (at, mut ucmd) = at_and_ucmd!();

// Compile the preload file required for mocking different UID and EUID.
// The UID should be 1000, whereas the EUID should be 0.
let c_file = at.as_string() + "/different_uid_and_euid.c";
let so_file = at.as_string() + "/different_uid_and_euid.so";
let status = unwrap_or_return!(compile_preload_file_with_gcc(&c_file, &so_file));
if !status.success() {
panic!("Preload file compilation failed")
}

let result = ucmd.env("LD_PRELOAD", so_file).run();
result.stdout_contains("uid=1000").stdout_contains("euid=0");
}
7 changes: 7 additions & 0 deletions tests/fixtures/id/different_uid_and_euid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <unistd.h>

const uid_t USER_UID = 1000;
const uid_t ROOT_UID = 0;

uid_t getuid(void) { return USER_UID; }
uid_t geteuid(void) { return ROOT_UID; }