Skip to content
Open
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
5 changes: 4 additions & 1 deletion docs/src/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ $ ls -w=80
With GNU coreutils, `--help` usually prints the help message and `--version` prints the version.
We also commonly provide short options: `-h` for help and `-V` for version.

## `coreutils`
## `coreutils` (multi-call binary)

Our `coreutils` calls utility by `coreutils utility-name` and has `--list` to run against busybox test suite.
Our `coreutils` is called as `utility-name` if its binary name ends with `utility-name` to support prefixed names.
Longer name is prioritized e.g. `sum` with the prefix `ck` is called as `cksum`.

On Linux, the symlink chain `dummy` -> `utility-name` -> `coreutils` tries to run `utility-name`
i.e. `dummy` tries to behave like individual binaries. It is important if `utility-name` is `true`.

## `env`

GNU `env` allows the empty string to be used as an environment variable name.
Expand Down
12 changes: 12 additions & 0 deletions src/bin/coreutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ fn main() {
// todo: Remove support of "*box" from binary
uucore::set_utility_is_second_arg();
args.next()
// with the invalid_name -> true -> coreutils symlink chain, run true
// this is possible on the platform not using argv0
} else if cfg!(any(target_os = "linux", target_os = "android"))
&& let Ok(binary) = std::fs::read_link(&binary)
&& let Some(valid) = validation::name(&binary)
&& !valid.ends_with("utils")
&& let Some(&matched) = utils
.keys()
.filter(|&&u| valid.ends_with(u))
.max_by_key(|u| u.len())
{
Some(OsString::from(matched))
} else {
validation::not_found(&OsString::from(binary_as_util));
};
Expand Down
8 changes: 5 additions & 3 deletions src/common/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ pub fn binary_path(args: &mut impl Iterator<Item = OsString>) -> PathBuf {
let exec_path = Path::new(OsStr::from_bytes(execfn_bytes));
let argv0 = args.next().unwrap();
let mut shebang_buf = [0u8; 2];
// exec_path is wrong when called from shebang or memfd_create (/proc/self/fd/*)
// argv0 is not full-path when called from PATH
if execfn_bytes.rsplit(|&b| b == b'/').next() == argv0.as_bytes().rsplit(|&b| b == b'/').next()
|| execfn_bytes.starts_with(b"/proc/")
{
// argv0 is not full-path when called from PATH
exec_path.into()
} else if execfn_bytes.starts_with(b"/proc/")
|| (File::open(Path::new(exec_path))
.and_then(|mut f| f.read_exact(&mut shebang_buf))
.is_ok()
&& &shebang_buf == b"#!")
{
// exec_path is wrong when called from shebang or memfd_create (/proc/self/fd/*)
argv0.into()
} else {
exec_path.into()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_util_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ fn init() {
eprintln!("Setting UUTESTS_BINARY_PATH={TESTS_BINARY}");
}

#[test]
#[cfg(all(feature = "env", any(target_os = "linux", target_os = "android")))]
fn binary_name_symlink_chain() {
let ts = TestScenario::new("chain");
let core_path = &ts.bin_path;
let env_path = ts.fixtures.plus("prefixed-env");
let dummy_path = ts.fixtures.plus("dummy");
symlink_file(core_path, &env_path).unwrap();
symlink_file(&env_path, &dummy_path).unwrap();
let is_env = std::process::Command::new(&dummy_path)
.status()
.unwrap()
.success();
assert!(is_env, "symlink chain dummy -> env -> coreutils failed");
}

#[test]
#[cfg(all(feature = "env", any(target_os = "linux", target_os = "android")))]
fn binary_name_protection() {
Expand Down
Loading