Repository navigation
fix: shm_unlink path and at_offset bounds check in ShmBuffer - #3
Open
adamiwaniec wants to merge 2 commits into
Open
adamiwaniec wants to merge 2 commits into
adamiwaniec wants to merge 2 commits into
Conversation
prevents libc from reading past the allocation, causing leaks. this issue doesnt really cause problems until the heap has churned enough and the byte after the string ischanged to something other than 0 (possible to happen in normal runs but not often). reproduced the bug non-deterministically using the existing test suite, at least one leak happened in 5/10 runs. also removed the 'as *const i8' cast since CString::as_ptr returns this type already, which is caught by clippy as a now unnecessary cast.
at_offset added offset and size before comparing against shm_size, so a large offset wraps and passes the bounds check. debug builds panic on the overflow, release builds return a pointer outside the mapping. this issue cant possibly cause problems from normal use (offsets are never going to be large enough). but it lets a bad offset through instead of rejecting it to prevent future offset calculation bugs and attackers from utilizing this security vulnerability. use checked_add, and added a test that fails without the fix.
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.
#1. shm_unlink path fix: ensures shm_unlink gets a null-terminated path string to prevent libc reading past the string.
the bug only causes failures when the next byte is changed to something non-zero. it's a rare case since that byte will stay as zero until the heap touches it (nondeterministic) after lots of churn, which is probably why it hasn't been noticed.
The existing policy tests reproduce the bug since they create their segments with is_creator=true and rely on Drop to remove them. so when the bug happens, the segment will just stay inside /dev/shm where we can find it:
on my machine, without the fix, each run leaves a varying number of segments behind. and with the fix it leaves none behind on 100s of runs.
#2. at_offset bounds check fix: prevents a large enough offset from overflowing and wrapping successfully when the size is added to it.
as my commit message explains, this cant possibly happen unless you're mapping petabytes of memory. but this fix will prevent buggy offset values being let through, potentially due to future offset calculation bugs or attackers taking advantage of this vulnerability.
ran fmt, clippy and test with the same flags as CI (--no-default-features --features
cuda-13).