8000 chroot: handle the error when invalid user by sylvestre · Pull Request #7015 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chroot: handle the error when invalid user #7015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/uu/chroot/src/chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/uu/chroot/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/by-util/test_chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the error message deliberately different from the one of GNU chroot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, it is for a next step
I am just removing the panic

} else {
print!("Test skipped; requires root user");
}
}

#[test]
#[cfg(not(target_os = "android"))]
fn test_preference_of_userspec() {
Expand Down
Loading
0