Skip to content

coreutils: restrict prefix matching so names like pwd-symlink do not false-match (#16848) - #16975

Open
yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:fix/multicall-symlink-resolution
Open

yunusarda-sudo wants to merge 4 commits into
uutils:mainfrom
yunusarda-sudo:fix/multicall-symlink-resolution

Conversation

@yunusarda-sudo

@yunusarda-sudo yunusarda-sudo commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

When the multicall binary is invoked as a name such as pwd-symlink, matched_utility previously performed unconstrained suffix matching (name.ends_with(util)). Because pwd-symlink ends with link, uu_link was dispatched, causing glibc's tst-spawn-chdir test to fail on Ubuntu (Launchpad #2161727).

This change tightens matched_utility to accept:

  1. An exact match,
  2. A prefix separated by -, _, or . (e.g. uu-cat, coreutils-cat, x86_64-linux-gnu-cat),
  3. Or a single-letter PROG_PREFIX with no separator (e.g. uls, ucat, gcat).

Symlink and hardlink resolution is left unchanged; that is complementary to #16981.

Fixes #16848

@oech3

oech3 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Sorry. I don't agree with it. This is downstreams packaging issue and resoving always means we introduce an overhead of readlink at most usual usecases.

I was considering to submit a PR to avoid that overhead when binaries are installed under /usr/bin.

Comment thread src/bin/coreutils.rs Outdated
return utils.keys().find(|&&k| k == name).copied();
}
// Prefix match: e.g. "uu-cat", "coreutils-cat", "x86_64-linux-gnu-cat", "gcat".
// Must be separated by a delimiter ('-', '_', '.') or the standard 'g' (GNU) prefix.

@oech3 oech3 Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need those limitations?! Thye don't have anything special.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Without them, pwd-symlink/true-symlink matches link.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is downstream's packaging issue. We should not have such hack.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also note that people can create pwdsymlink too.

Comment thread src/bin/coreutils.rs Outdated
let name = validation::name(&target)?;
matched_utility(name, utils)
let mut current = binary.to_path_buf();
for _ in 0..40 {

@oech3 oech3 Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please don't do that. There is no actual usecase. We don't need to increase complexity for downstream's packaging issue. Or use /proc/self/exe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This issue is existing on main too, but readlink causes race condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped the loop; a single read_link is enough here. The remaining race is pre-existing on main — happy to follow up separately.

@oech3 oech3 Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is no safe-traversal way to support symlink install. Thus, we must restrict this extension to hardlink installs i.e we must lost current extension.
Fake /proc is theorically possible, but I'm not sure if it is worth to doubt it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, dropped symlink resolution; this PR now only tightens prefix matching, complementary to #16981.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please just close this PR. Restricting prefix matching does not do anything.

@oech3 oech3 Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pwd-symlink "should" match with link. This is definition of our our extension.
pwd-sym is a valid prefix. The linked issue is really invalid.

Comment thread src/bin/coreutils.rs Outdated
if let Some(util) = matched_utility(name, utils) {
return Some(util);
}
if name.ends_with("utils") || name.ends_with("box") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

box is a hack existing just for running busybox testsuite on the CI. We don't need to care about it at here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed with the walker.

Comment thread src/bin/coreutils.rs Outdated
// binary name ends with util name?
let is_coreutils = binary_as_util.ends_with("utils");
let util_name = if let Some(util) = matched_utility(binary_as_util, &utils) {
let util_name = if let Some(util) = utility_from_symlink(&binary, &utils) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This introduces an overhead of readlink(2) call to the all usual usecases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved it back to the linux+gnu fallback after name matching, so usual cases no longer pay readlink.

Comment thread tests/test_util_name.rs Outdated
}

#[test]
#[cfg(all(unix, feature = "pwd"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Depending on pwd is highcost. true or echo is enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched the test to true.

Comment thread src/bin/coreutils.rs
// todo: Remove support of "*box" from binary
uucore::set_utility_is_second_arg();
args.next()
} else if cfg!(all(target_os = "linux", target_env = "gnu"))

@oech3 oech3 Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Removing this cfg does not help other platforms because full path is not used at other platforms.
Supporting it by parsing PATH causes race condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Restored the cfg.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix/multicall-symlink-resolution branch from 084de3b to e2f2370 Compare November 29, 2026 14:25
@yunusarda-sudo yunusarda-sudo changed the title coreutils: resolve utility from symlink target before prefix matching (#16848) coreutils: restrict prefix matching so symlink names do not false-match (#16848) Nov 29, 2026
@oech3

This comment was marked as outdated.

@oech3

oech3 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

I opened #16981 instead.

Comment thread src/bin/coreutils.rs Outdated
if let Some(prefix) = name.strip_suffix(util) {
prefix.ends_with('-')
|| prefix.ends_with('_')
|| prefix.ends_with('.')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this breaks PROG_PREFIX without a separator, e.g. homebrew installs uls, ucat (prefix u)
they would no longer dispatch, please add a test for such a name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In any case, this is regression and pwdsymlink, or simply link -> pwd is not supported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Restored single-letter prefixes (uls/ucat) and added a test.

Comment thread src/bin/coreutils.rs Outdated
}
// Prefer stty over tty, and never treat *utils as ls.
// Exact match has highest precedence.
if utils.contains_key(name) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could be folded into the filter with prefix.is_empty(), no?
and ends_with(['-', '_', '.']) would be shorter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Folded the exact match into the filter as suggested.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix/multicall-symlink-resolution branch from e2f2370 to 5b8bb59 Compare November 30, 2026 05:01
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

#16981 covers the hardlink/copy case, but not true-symlink -> true -> coreutils, and it still suffix-matches link; this PR keeps that case and restores undelimited PROG_PREFIX (uls/ucat). Happy to close or rebase onto #16981 if maintainers prefer.

@oech3

oech3 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

#16981 covers true-symlink -> true because /proc/self/exe is looked at 1st.
false -> true is also true.
/usr/bin/true-symlink is not supported, but packaging dummy under /usr/bin is very serious packaging issue. Downstream should not do that.

@oech3

oech3 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

This ofcause depends on hardlink install. But the limitation is no probrem because there is not compatibility issue with GNU's multi-call coreutils. Mimicing individual binaries is just a best effort. Downstream just need to choice installing binaries as hardlinks (which is GNUmakefile's default).

@yunusarda-sudo yunusarda-sudo changed the title coreutils: restrict prefix matching so symlink names do not false-match (#16848) coreutils: restrict prefix matching so names like pwd-symlink do not false-match (#16848) Nov 30, 2026
Comment thread src/bin/coreutils.rs
// Prefer stty over tty, and never treat *utils as ls.
// Exact match, delimited prefix (uu-cat, coreutils-cat, x86_64-linux-gnu-cat),
// or a single-letter PROG_PREFIX without a separator (uls, ucat, gcat).
// Rejects false suffix matches such as pwd-symlink -> link.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think that pwd-link and pwdlink should be pwd? I don't think so.
There is no way to make every one happy at here. Please close this PR.

yunusarda-sudo and others added 4 commits December 1, 2026 16:59
Keep restricted prefix matching as the uutils#16848 fix, but call
utility_from_symlink only on linux-gnu after name matching fails so
usual invocations do not pay readlink. Drop the multi-hop walker
(one hop is enough) and switch the regression test to true.
Fold exact matches into the prefix filter and keep uls/ucat working.
Leave hardlink/symlink resolution to uutils#16981 and keep a regression test
that true-symlink does not false-match link.
@sylvestre
sylvestre force-pushed the fix/multicall-symlink-resolution branch from 66699a9 to 87092c9 Compare December 1, 2026 15:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

coreutils: symlink named pwd-symlink runs link instead of pwd

3 participants