1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

chroot: handle the error when invalid user

Currently fails with:
```
2024-12-28T14:55:18.9330231Z thread 'main' panicked at src/uu/chroot/src/chroot.rs:284:46:
2024-12-28T14:55:18.9330718Z called `Result::unwrap()` on an `Err` value: Custom { kind: NotFound, error: "Not found: nobody:+65535" }
2024-12-28T14:55:18.9331305Z note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
This commit is contained in:
Sylvestre Ledru 2024-12-28 22:27:53 +01:00
parent edf8be5e08
commit 7d628c65d3
3 changed files with 28 additions and 1 deletions

View file

@ -281,7 +281,8 @@ fn set_groups_from_str(groups: &str) -> UResult<()> {
fn set_user(user: &str) -> UResult<()> {
if !user.is_empty() {
let user_id = entries::usr2uid(user).unwrap();
let user_id =
entries::usr2uid(user).map_err(|_| ChrootError::NoSuchUser(user.to_string()))?;
let err = unsafe { setuid(user_id as libc::uid_t) };
if err != 0 {
return Err(

View file

@ -27,6 +27,9 @@ pub enum ChrootError {
/// The new root directory was not given.
MissingNewRoot,
/// Failed to find the specified user.
NoSuchUser(String),
/// Failed to find the specified group.
NoSuchGroup(String),
@ -71,6 +74,7 @@ impl Display for ChrootError {
"Missing operand: NEWROOT\nTry '{} --help' for more information.",
uucore::execution_phrase(),
),
Self::NoSuchUser(s) => write!(f, "no such user: {}", s.maybe_quote(),),
Self::NoSuchGroup(s) => write!(f, "no such group: {}", s.maybe_quote(),),
Self::NoSuchDirectory(s) => write!(
f,

View file

@ -64,6 +64,28 @@ fn test_invalid_user_spec() {
assert!(result.stderr_str().starts_with("chroot: invalid userspec"));
}
#[test]
fn test_invalid_user() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let dir = "CHROOT_DIR";
at.mkdir(dir);
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "whoami"]) {
result.success().no_stderr().stdout_is("root");
} else {
print!("Test skipped; requires root user");
}
if let Ok(result) = run_ucmd_as_root(&ts, &["--user=nobody:+65535", dir, "pwd"]) {
result
.failure()
.stderr_contains("no such user: nobody:+65535");
} else {
print!("Test skipped; requires root user");
}
}
#[test]
#[cfg(not(target_os = "android"))]
fn test_preference_of_userspec() {