Skip to content
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
6 changes: 5 additions & 1 deletion src/uu/chmod/src/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl Chmoder {
// should not change the permissions in this case
continue;
}
if self.recursive && self.preserve_root && file == Path::new("/") {
if self.recursive && self.preserve_root && Self::is_root(file) {
return Err(ChmodError::PreserveRoot("/".into()).into());
}
if self.recursive {
Expand All @@ -419,6 +419,10 @@ impl Chmoder {
r
}

fn is_root(file: impl AsRef<Path>) -> bool {
matches!(fs::canonicalize(&file), Ok(p) if p == Path::new("/"))
}

#[cfg(not(target_os = "linux"))]
fn walk_dir_with_context(&self, file_path: &Path, is_command_line_arg: bool) -> UResult<()> {
let mut r = self.chmod_file(file_path);
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ fn test_chmod_preserve_root() {
.stderr_contains("chmod: it is dangerous to operate recursively on '/'");
}

#[test]
fn test_chmod_preserve_root_with_paths_that_resolve_to_root() {
new_ucmd!()
.arg("-R")
.arg("--preserve-root")
.arg("755")
.arg("/../")
.fails_with_code(1)
.stderr_contains("chmod: it is dangerous to operate recursively on '/'");
}

#[test]
fn test_chmod_symlink_non_existing_file() {
let scene = TestScenario::new(util_name!());
Expand Down
Loading