tac: use MAP_PRIVATE to prevent SIGBUS on file truncation#11464
Open
ctonneslan wants to merge 1 commit intouutils:mainfrom
Open
tac: use MAP_PRIVATE to prevent SIGBUS on file truncation#11464ctonneslan wants to merge 1 commit intouutils:mainfrom
ctonneslan wants to merge 1 commit intouutils:mainfrom
Conversation
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
Contributor
|
could you please add a test to make sure we don't regress in the future? thanks |
|
GNU testsuite comparison: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.