Skip to content

head: Fixed TOCTOU bug in checking of metadata#12439

Merged
sylvestre merged 3 commits into
uutils:mainfrom
max-amb:fix_head_TOCTOU
Jun 5, 2026
Merged

head: Fixed TOCTOU bug in checking of metadata#12439
sylvestre merged 3 commits into
uutils:mainfrom
max-amb:fix_head_TOCTOU

Conversation

@max-amb

@max-amb max-amb commented May 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #11972.

Doesn't look exploitable in any degree but lack of TOCTOU is perhaps nice to have.

Not sure if a closure was the best way to do print_header, very open to alternative approaches.

@github-actions

github-actions Bot commented May 22, 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/tail-n0f (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/unexpand/bounded-memory is now passing!

@oech3

oech3 commented May 23, 2026

Copy link
Copy Markdown
Contributor

This should be catched at 1st read. But it is currently difficult since we don't distinct read error and write error #12265 .

@max-amb

max-amb commented May 23, 2026

Copy link
Copy Markdown
Contributor Author

I don't see how the write error proposed in the PR would be incorporated? We don't have a specific metadata access error so does that need to be added? Otherwise it can fail at two different points with the same error.

@oech3

oech3 commented May 23, 2026

Copy link
Copy Markdown
Contributor

@oech3

oech3 commented May 23, 2026

Copy link
Copy Markdown
Contributor

We can just remove .is_dir()after read stated catching ErrorKind::IsADirectory.

@max-amb

max-amb commented May 23, 2026

Copy link
Copy Markdown
Contributor Author

Oh I see. To confirm, once ReadError is added we can handle directories while checking file metadata with map error?

@oech3

oech3 commented May 23, 2026

Copy link
Copy Markdown
Contributor

I think we can just do somethin like writeln!(stderr(), "{e}"); (without checking metadata).

@max-amb

max-amb commented May 23, 2026

Copy link
Copy Markdown
Contributor Author

How would this look with all of the print_header() calls required, because we need to print the header only if we have perms to open the file/header. So wouldn't we still need to check if we can get metadata before print_header().

@sylvestre sylvestre merged commit 4353689 into uutils:main Jun 5, 2026
174 checks passed
sylvestre added a commit to sylvestre/coreutils that referenced this pull request Jun 5, 2026
Follow-up to the metadata TOCTOU fix (uutils#11972, PR uutils#12439):

- Add a syscall-level regression guard in util/check-safe-traversal.sh:
  under strace, head must read metadata from the open descriptor
  (fstat/statx on the fd) and must not stat the path before opening it.
  Verified this fails on the pre-fix code and passes after.
- Add an integration test asserting that an unreadable file produces an
  error but no "==> name <==" header, matching GNU (the header is only
  printed after a successful open).
- Fix a stray paren in the Windows-branch comment.
@sylvestre

Copy link
Copy Markdown
Contributor

follow up : #12637

@max-amb max-amb deleted the fix_head_TOCTOU branch June 5, 2026 12:07
sylvestre added a commit that referenced this pull request Jun 5, 2026
Follow-up to the metadata TOCTOU fix (#11972, PR #12439):

- Add a syscall-level regression guard in util/check-safe-traversal.sh:
  under strace, head must read metadata from the open descriptor
  (fstat/statx on the fd) and must not stat the path before opening it.
  Verified this fails on the pre-fix code and passes after.
- Add an integration test asserting that an unreadable file produces an
  error but no "==> name <==" header, matching GNU (the header is only
  printed after a successful open).
- Fix a stray paren in the Windows-branch comment.
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.

head: potentially TOCTOU race with .is_dir()

3 participants