Skip to content
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

chroot: improve support of --skip-chdir #4004

Merged
merged 1 commit into from
Oct 10, 2022
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
17 changes: 16 additions & 1 deletion src/uu/chroot/src/chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use std::io::Error;
use std::os::unix::prelude::OsStrExt;
use std::path::Path;
use std::process;
use uucore::error::{set_exit_code, UClapError, UResult};
use uucore::error::{set_exit_code, UClapError, UResult, UUsageError};
use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
use uucore::libc::{self, chroot, setgid, setgroups, setuid};
use uucore::{entries, format_usage};

Expand Down Expand Up @@ -48,6 +49,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
None => return Err(ChrootError::MissingNewRoot.into()),
};

let skip_chdir = matches.contains_id(options::SKIP_CHDIR);
// We are resolving the path in case it is a symlink or /. or /../
if skip_chdir
&& canonicalize(newroot, MissingHandling::Normal, ResolveMode::Logical)
.unwrap()
.to_str()
!= Some("/")
{
return Err(UUsageError::new(
125,
"option --skip-chdir only permitted if NEWROOT is old '/'",
));
}

if !newroot.is_dir() {
return Err(ChrootError::NoSuchDirectory(format!("{}", newroot.display())).into());
}
Expand Down
43 changes: 29 additions & 14 deletions tests/by-util/test_chroot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// spell-checker:ignore (words) araba newroot userspec chdir pwd's
// spell-checker:ignore (words) araba newroot userspec chdir pwd's isroot

use crate::common::util::*;

Expand Down Expand Up @@ -146,21 +146,36 @@ fn test_chroot() {
}

#[test]
fn test_chroot_skip_chdir() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
fn test_chroot_skip_chdir_not_root() {
let (at, mut ucmd) = at_and_ucmd!();

let dir = "CHROOT_DIR";
let dir = "foobar";
at.mkdir(dir);
let env_cd = std::env::current_dir().unwrap();
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "--skip-chdir", "pwd"]) {
// Should return the same path
assert_eq!(
result.success().no_stderr().stdout_str(),
env_cd.to_str().unwrap()
);
} else {
print!("Test skipped; requires root user");

ucmd.arg("--skip-chdir")
.arg(dir)
.fails()
.stderr_contains("chroot: option --skip-chdir only permitted if NEWROOT is old '/'")
.code_is(125);
}

#[test]
fn test_chroot_skip_chdir() {
let ts = TestScenario::new(util_name!());
let at = ts.fixtures.clone();
let dirs = ["/", "/.", "/..", "isroot"];
at.symlink_file("/", "isroot");
for dir in dirs {
let env_cd = std::env::current_dir().unwrap();
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "--skip-chdir"]) {
// Should return the same path
assert_eq!(
result.success().no_stderr().stdout_str(),
env_cd.to_str().unwrap()
);
} else {
print!("Test skipped; requires root user");
}
}
}

Expand Down