Skip to content

tac: use MAP_PRIVATE to prevent SIGBUS on file truncation#11464

Closed
ghost wants to merge 1 commit into
mainfrom
unknown repository
Closed

tac: use MAP_PRIVATE to prevent SIGBUS on file truncation#11464
ghost wants to merge 1 commit into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Mar 23, 2026

Copy link
Copy Markdown

Fixes #9748

When `tac` reads a file via `mmap` and that file gets truncated concurrently (e.g. during log rotation), the process gets SIGBUS because the mapped pages are no longer backed by the file.

The fix switches from `Mmap::map()` (which uses MAP_SHARED) to `map_copy_read_only()` (which uses MAP_PRIVATE). With MAP_PRIVATE, the kernel returns zero-filled pages for truncated regions instead of raising SIGBUS. The performance characteristics are the same for read-only access since no actual copy is made until a write occurs (which we never do).

This matches GNU `tac`'s approach of not using direct shared memory mapping on untrusted inputs.

All 29 tac integration tests pass.

When tac reads a file via mmap and that file gets truncated
concurrently (e.g. log rotation), the process receives SIGBUS
because the mapped pages are no longer backed by the file.

Switch from Mmap::map() (MAP_SHARED) to map_copy_read_only()
(MAP_PRIVATE). With MAP_PRIVATE, the kernel returns zero-filled
pages for truncated regions instead of raising SIGBUS, matching
GNU tac's behavior of graceful handling.

Fixes #9748
@sylvestre

Copy link
Copy Markdown
Contributor

could you please add a test to make sure we don't regress in the future? thanks

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/rm/isatty. tests/rm/isatty is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/follow-name (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/misc/io-errors is no longer failing!
Note: The gnu test tests/dd/no-allocate is now being skipped but was previously passing.
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Congrats! The gnu test tests/csplit/csplit-heap is now passing!

@ghost ghost closed this by deleting the head repository Mar 26, 2026
This pull request was closed.
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