| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (words) araba newroot userspec chdir pwd's isroot |
| 7 |
|
| 8 |
use uutests::at_and_ucmd; |
| 9 |
use uutests::new_ucmd; |
| 10 |
#[cfg(not(target_os = "android"))] |
| 11 |
use uutests::util::is_ci; |
| 12 |
use uutests::util::{TestScenario, run_ucmd_as_root}; |
| 13 |
use uutests::util_name; |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_invalid_arg() { |
| 17 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(125); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_missing_operand() { |
| 22 |
let result = new_ucmd!().fails_with_code(125); |
| 23 |
|
| 24 |
assert!( |
| 25 |
result |
| 26 |
.stderr_str() |
| 27 |
.starts_with("error: the following required arguments were not provided") |
| 28 |
); |
| 29 |
|
| 30 |
assert!(result.stderr_str().contains("<newroot>")); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
#[cfg(not(target_os = "android"))] |
| 35 |
fn test_enter_chroot_fails() { |
| 36 |
// NOTE: since #2689 this test also ensures that we don't regress #2687 |
| 37 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 38 |
|
| 39 |
at.mkdir("jail"); |
| 40 |
|
| 41 |
ucmd.arg("jail") |
| 42 |
.fails_with_code(125) |
| 43 |
.stderr_only("chroot: cannot chroot to 'jail': Operation not permitted\n"); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
#[cfg(target_env = "gnu")] // todo: override by proper error message on other platforms |
| 48 |
fn test_too_long_name() { |
| 49 |
let (_at, mut ucmd) = at_and_ucmd!(); |
| 50 |
let long_name = "a".repeat(256); |
| 51 |
|
| 52 |
ucmd.arg(&long_name) |
| 53 |
.fails_with_code(125) |
| 54 |
.stderr_only(format!( |
| 55 |
"chroot: cannot chroot to '{long_name}': File name too long\n", |
| 56 |
)); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
#[cfg(target_env = "gnu")] // todo: override by proper error message on other platforms |
| 61 |
fn test_no_such() { |
| 62 |
let (_at, mut ucmd) = at_and_ucmd!(); |
| 63 |
|
| 64 |
ucmd.arg("missing") |
| 65 |
.fails_with_code(125) |
| 66 |
.stderr_only("chroot: cannot chroot to 'missing': No such file or directory\n"); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
#[cfg(target_env = "gnu")] // todo: override by proper error message on other platforms |
| 71 |
fn test_not_a_directory() { |
| 72 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 73 |
at.touch(at.plus_as_string("a")); |
| 74 |
ucmd.arg("a") |
| 75 |
.fails_with_code(125) |
| 76 |
.stderr_only("chroot: cannot chroot to 'a': Not a directory\n"); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn test_multiple_group_args() { |
| 81 |
let ts = TestScenario::new(util_name!()); |
| 82 |
let at = &ts.fixtures; |
| 83 |
at.mkdir("id"); |
| 84 |
|
| 85 |
if let Ok(result) = run_ucmd_as_root( |
| 86 |
&ts, |
| 87 |
&["--groups='invalid ignored'", "--groups=''", "/", "id", "-G"], |
| 88 |
) { |
| 89 |
result.success().stdout_is("0"); |
| 90 |
} else { |
| 91 |
print!("Test skipped; requires root user"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn test_invalid_user_spec() { |
| 97 |
let ts = TestScenario::new(util_name!()); |
| 98 |
|
| 99 |
if let Ok(result) = run_ucmd_as_root(&ts, &["--userspec=ARABA:", "/"]) { |
| 100 |
result |
| 101 |
.failure() |
| 102 |
.code_is(125) |
| 103 |
.stderr_is("chroot: invalid user"); |
| 104 |
} else { |
| 105 |
print!("Test skipped; requires root user"); |
| 106 |
} |
| 107 |
|
| 108 |
if let Ok(result) = run_ucmd_as_root(&ts, &["--userspec=ARABA:ARABA", "/"]) { |
| 109 |
result |
| 110 |
.failure() |
| 111 |
.code_is(125) |
| 112 |
.stderr_is("chroot: invalid user"); |
| 113 |
} else { |
| 114 |
print!("Test skipped; requires root user"); |
| 115 |
} |
| 116 |
|
| 117 |
if let Ok(result) = run_ucmd_as_root(&ts, &["--userspec=:ARABA", "/"]) { |
| 118 |
result |
| 119 |
.failure() |
| 120 |
.code_is(125) |
| 121 |
.stderr_is("chroot: invalid group"); |
| 122 |
} else { |
| 123 |
print!("Test skipped; requires root user"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn test_invalid_user() { |
| 129 |
let ts = TestScenario::new(util_name!()); |
| 130 |
let at = &ts.fixtures; |
| 131 |
|
| 132 |
let dir = "CHROOT_DIR"; |
| 133 |
at.mkdir(dir); |
| 134 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "whoami"]) { |
| 135 |
result.success().no_stderr().stdout_is("root"); |
| 136 |
} else { |
| 137 |
print!("Test skipped; requires root user"); |
| 138 |
} |
| 139 |
|
| 140 |
// `--user` is an abbreviation of `--userspec`. |
| 141 |
if let Ok(result) = run_ucmd_as_root(&ts, &["--user=nobody:+65535", dir, "pwd"]) { |
| 142 |
result.failure().stderr_is("chroot: invalid user"); |
| 143 |
} else { |
| 144 |
print!("Test skipped; requires root user"); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
#[cfg(not(target_os = "android"))] |
| 150 |
fn test_preference_of_userspec() { |
| 151 |
let scene = TestScenario::new(util_name!()); |
| 152 |
let result = scene.cmd("whoami").run(); |
| 153 |
if is_ci() && result.stderr_str().contains("No such user/group") { |
| 154 |
// In the CI, some server are failing to return whoami. |
| 155 |
// As seems to be a configuration issue, ignoring it |
| 156 |
return; |
| 157 |
} |
| 158 |
println!("result.stdout = {}", result.stdout_str()); |
| 159 |
println!("result.stderr = {}", result.stderr_str()); |
| 160 |
let username = result.stdout_str().trim_end(); |
| 161 |
|
| 162 |
let ts = TestScenario::new("id"); |
| 163 |
let result = ts.cmd("id").arg("-g").arg("-n").run(); |
| 164 |
println!("result.stdout = {}", result.stdout_str()); |
| 165 |
println!("result.stderr = {}", result.stderr_str()); |
| 166 |
|
| 167 |
if is_ci() && result.stderr_str().contains("cannot find name for user ID") { |
| 168 |
// In the CI, some server are failing to return id. |
| 169 |
// As seems to be a configuration issue, ignoring it |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
let group_name = result.stdout_str().trim_end(); |
| 174 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 175 |
|
| 176 |
at.mkdir("a"); |
| 177 |
|
| 178 |
// `--user` is an abbreviation of `--userspec`. |
| 179 |
let result = ucmd |
| 180 |
.arg("a") |
| 181 |
.arg("--user") |
| 182 |
.arg("fake") |
| 183 |
.arg("--groups") |
| 184 |
.arg("ABC,DEF") |
| 185 |
.arg(format!("--userspec={username}:{group_name}")) |
| 186 |
.fails_with_code(125); |
| 187 |
|
| 188 |
println!("result.stdout = {}", result.stdout_str()); |
| 189 |
println!("result.stderr = {}", result.stderr_str()); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn test_default_shell() { |
| 194 |
// NOTE: This test intends to trigger code which can only be reached with root permissions. |
| 195 |
let ts = TestScenario::new(util_name!()); |
| 196 |
let at = &ts.fixtures; |
| 197 |
|
| 198 |
let dir = "CHROOT_DIR"; |
| 199 |
at.mkdir(dir); |
| 200 |
|
| 201 |
let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()); |
| 202 |
let expected = format!("chroot: failed to run command '{shell}': No such file or directory"); |
| 203 |
|
| 204 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir]) { |
| 205 |
result.stderr_contains(expected); |
| 206 |
} else { |
| 207 |
print!("Test skipped; requires root user"); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn test_chroot_command_not_found_error() { |
| 213 |
let ts = TestScenario::new(util_name!()); |
| 214 |
let at = &ts.fixtures; |
| 215 |
|
| 216 |
let dir = "CHROOT_DIR"; |
| 217 |
at.mkdir(dir); |
| 218 |
|
| 219 |
let missing = "definitely_missing_command"; |
| 220 |
|
| 221 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, missing]) { |
| 222 |
result |
| 223 |
.failure() |
| 224 |
.code_is(127) |
| 225 |
.stderr_contains(format!("failed to run command '{missing}'")) |
| 226 |
.stderr_contains("No such file or directory"); |
| 227 |
} else { |
| 228 |
print!("Test skipped; requires root user"); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_chroot_command_permission_denied_error() { |
| 234 |
let ts = TestScenario::new(util_name!()); |
| 235 |
let at = &ts.fixtures; |
| 236 |
|
| 237 |
let dir = "CHROOT_DIR"; |
| 238 |
at.mkdir(dir); |
| 239 |
|
| 240 |
let script_path = format!("{dir}/noexec.sh"); |
| 241 |
at.write(&script_path, "#!/bin/sh\necho unreachable\n"); |
| 242 |
#[cfg(not(windows))] |
| 243 |
{ |
| 244 |
at.set_mode(&script_path, 0o644); |
| 245 |
} |
| 246 |
|
| 247 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "/noexec.sh"]) { |
| 248 |
result |
| 249 |
.failure() |
| 250 |
.code_is(126) |
| 251 |
.stderr_contains("failed to run command '/noexec.sh'") |
| 252 |
.stderr_contains("Permission denied"); |
| 253 |
} else { |
| 254 |
print!("Test skipped; requires root user"); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn test_chroot() { |
| 260 |
let ts = TestScenario::new(util_name!()); |
| 261 |
let at = &ts.fixtures; |
| 262 |
|
| 263 |
let dir = "CHROOT_DIR"; |
| 264 |
at.mkdir(dir); |
| 265 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "whoami"]) { |
| 266 |
result.success().no_stderr().stdout_is("root"); |
| 267 |
} else { |
| 268 |
print!("Test skipped; requires root user"); |
| 269 |
} |
| 270 |
|
| 271 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "pwd"]) { |
| 272 |
result.success().no_stderr().stdout_is("/"); |
| 273 |
} else { |
| 274 |
print!("Test skipped; requires root user"); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_chroot_retains_uid_gid() { |
| 280 |
let ts = TestScenario::new(util_name!()); |
| 281 |
let at = &ts.fixtures; |
| 282 |
|
| 283 |
let dir = "CHROOT_DIR"; |
| 284 |
at.mkdir(dir); |
| 285 |
|
| 286 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "id", "-u"]) { |
| 287 |
result.success().no_stderr().stdout_is("0"); |
| 288 |
} else { |
| 289 |
print!("Test skipped; requires root user"); |
| 290 |
} |
| 291 |
|
| 292 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "id", "-g"]) { |
| 293 |
result.success().no_stderr().stdout_is("0"); |
| 294 |
} else { |
| 295 |
print!("Test skipped; requires root user"); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
#[test] |
| 300 |
fn test_chroot_skip_chdir_not_root() { |
| 301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 302 |
|
| 303 |
let dir = "foobar"; |
| 304 |
at.mkdir(dir); |
| 305 |
|
| 306 |
ucmd.arg("--skip-chdir") |
| 307 |
.arg(dir) |
| 308 |
.fails_with_code(125) |
| 309 |
.stderr_contains("chroot: option --skip-chdir only permitted if NEWROOT is old '/'"); |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn test_chroot_skip_chdir_nonexistent() { |
| 314 |
new_ucmd!() |
| 315 |
.arg("--skip-chdir") |
| 316 |
.arg("/nonexistent/sub") |
| 317 |
.fails_with_code(125) |
| 318 |
.stderr_contains("chroot: option --skip-chdir only permitted if NEWROOT is old '/'"); |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn test_chroot_skip_chdir() { |
| 323 |
let ts = TestScenario::new(util_name!()); |
| 324 |
let at = &ts.fixtures; |
| 325 |
let dirs = ["/", "/.", "/..", "isroot"]; |
| 326 |
at.symlink_file("/", "isroot"); |
| 327 |
for dir in dirs { |
| 328 |
let env_cd = std::env::current_dir().unwrap(); |
| 329 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "--skip-chdir"]) { |
| 330 |
// Should return the same path |
| 331 |
assert_eq!( |
| 332 |
result.success().no_stderr().stdout_str(), |
| 333 |
env_cd.to_str().unwrap() |
| 334 |
); |
| 335 |
} else { |
| 336 |
print!("Test skipped; requires root user"); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn test_chroot_extra_arg() { |
| 343 |
let ts = TestScenario::new(util_name!()); |
| 344 |
let at = &ts.fixtures; |
| 345 |
|
| 346 |
let dir = "CHROOT_DIR"; |
| 347 |
at.mkdir(dir); |
| 348 |
let env_cd = std::env::current_dir().unwrap(); |
| 349 |
// Verify that -P is pwd's and not chroot |
| 350 |
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "pwd", "-P"]) { |
| 351 |
assert_eq!( |
| 352 |
result.success().no_stderr().stdout_str(), |
| 353 |
env_cd.to_str().unwrap() |
| 354 |
); |
| 355 |
} else { |
| 356 |
print!("Test skipped; requires root user"); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn test_chroot_userspec_does_not_set_gid_with_uid() { |
| 362 |
use uucore::entries::{usr2gid, usr2uid}; |
| 363 |
|
| 364 |
let ts = TestScenario::new(util_name!()); |
| 365 |
if let Ok(uid) = usr2uid("sync") { |
| 366 |
if let Ok(gid) = usr2gid("sync") { |
| 367 |
if gid == uid { |
| 368 |
println!("Test skipped; requires sync user to have uid != gid"); |
| 369 |
return; |
| 370 |
} |
| 371 |
// Ubuntu has a sync user whose gid is 65534 per default |
| 372 |
if let Ok(result) = run_ucmd_as_root(&ts, &["--userspec=sync", "/", "id", "-g"]) { |
| 373 |
result.success().no_stderr().stdout_is(format!("{gid}\n")); |
| 374 |
} else { |
| 375 |
println!("Test skipped; requires root user"); |
| 376 |
} |
| 377 |
} else { |
| 378 |
println!("Test skipped; requires 'sync' user"); |
| 379 |
} |
| 380 |
} else { |
| 381 |
println!("Test skipped; requires 'sync' user"); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn test_chroot_userspec_unknown_uid() { |
| 387 |
let ts = TestScenario::new(util_name!()); |
| 388 |
if let Ok(result) = |
| 389 |
run_ucmd_as_root(&ts, &["--userspec=99999", "--groups=root", "/", "id", "-g"]) |
| 390 |
{ |
| 391 |
result |
| 392 |
.failure() |
| 393 |
.code_is(125) |
| 394 |
.stderr_is("chroot: no group specified for unknown uid: 99999\n"); |
| 395 |
} else { |
| 396 |
println!("Test skipped; requires root user"); |
| 397 |
} |
| 398 |
} |