Skip to content

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

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

tac: use MAP_PRIVATE to prevent SIGBUS on file truncation#11464
ctonneslan wants to merge 1 commit intouutils:mainfrom
ctonneslan:fix-tac-sigbus

Conversation

@ctonneslan
Copy link

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 uutils#9748
@sylvestre
Copy link
Contributor

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

@github-actions
Copy link

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!

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