Skip to content

realpath: don't require read permission on a directory just to confirm a trailing slash#13315

Open
Chaganti-Reddy wants to merge 1 commit into
uutils:mainfrom
Chaganti-Reddy:realpath-path-canonicalization
Open

realpath: don't require read permission on a directory just to confirm a trailing slash#13315
Chaganti-Reddy wants to merge 1 commit into
uutils:mainfrom
Chaganti-Reddy:realpath-path-canonicalization

Conversation

@Chaganti-Reddy

Copy link
Copy Markdown
Contributor

a trailing slash makes realpath assert the target is a directory, but that check was calling read_dir on it, which needs permission to list the directory's contents. GNU only needs a stat-level check there (search permission on the parent, not read on the target itself), so e.g. realpath /root/ works fine as a regular user even though they can't list /root.

swapped it for a metadata().is_dir() check, falling back to read_dir only when it's actually not a directory, so the existing 'Not a directory' error is unchanged.

Fixes #13151

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)

Comment on lines +364 to +365
read_dir(path)?;
Ok(())

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 looks odd. Why not return an error with ErrorKind::NotADirectory?

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.

if I just do ErrorKind::NotADirectory.into(), the message changes from GNU's "Not a directory" to rust's generic lowercase "not a directory" — the capitalized version we have now only exists because read_dir surfaces the real OS error text. want me to keep the exact GNU wording with Error::new(ErrorKind::NotADirectory, "Not a directory"), or are you fine with the plain rust message even though it drifts from GNU here?

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.

bug(realpath): ///.///../../../root/./ errors out in that path

2 participants