Skip to content

tac: avoid mmap on regular files to prevent SIGBUS on truncation#11326

Open
mango766 wants to merge 1 commit intouutils:mainfrom
mango766:fix-tac-sigbus
Open

tac: avoid mmap on regular files to prevent SIGBUS on truncation#11326
mango766 wants to merge 1 commit intouutils:mainfrom
mango766:fix-tac-sigbus

Conversation

@mango766
Copy link

Summary

  • Replace mmap-based file reading in tac with read_to_end() to prevent SIGBUS crashes when input files are truncated during read
  • Remove the now-unused try_mmap_file() function
  • Retain mmap for stdin (via temp files owned by the process) where external truncation is not a concern

Context

When tac memory-maps a regular file and another process truncates that file concurrently, accessing the now-invalid mapped memory region triggers SIGBUS and crashes the process. This is a real concern in log rotation scenarios where tac might be reading logs that get rotated/truncated at the same time.

GNU tac avoids this by not directly memory-mapping untrusted input files. This PR brings the same safety to the uutils implementation by reading files into a heap buffer instead.

Test plan

  • All existing tac tests should continue to pass (file reads, stdin, separators, regex modes)
  • The reproduction scenario from the issue should no longer crash:
    dd if=/dev/zero of=/tmp/tactest bs=1M count=10 2>/dev/null
    (sleep 0.001; truncate -s 0 /tmp/tactest) &
    tac /tmp/tactest

Fixes #9748

Generated with Claude Code

When a file is memory-mapped and then truncated by another process
while tac is reading it, accessing the now-invalid mapped region
triggers SIGBUS and crashes the process. This is particularly
problematic for log rotation scenarios where tac might read logs
that are being rotated/truncated concurrently.

Replace the mmap-based file reading with read_to_end(), which reads
the entire file into a heap buffer. This matches how GNU tac avoids
the issue. The mmap path is retained for stdin (via temp files we
own), where external truncation is not a concern.

Fixes uutils#9748

Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/unexpand/bounded-memory is now being skipped but was previously passing.

@sylvestre
Copy link
Contributor

please add a test

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.

tac crashes with SIGBUS when input file is truncated during read

2 participants