Skip to content

fix: shm_unlink path and at_offset bounds check in ShmBuffer - #3

Open
adamiwaniec wants to merge 2 commits into
XOR-op:mainfrom
adamiwaniec:fix/shm-buffer-safety
Open

adamiwaniec wants to merge 2 commits into
XOR-op:mainfrom
adamiwaniec:fix/shm-buffer-safety

Conversation

@adamiwaniec

Copy link
Copy Markdown

#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:

for i in $(seq 10); do
  cargo test -q --no-default-features --features cuda-13 --bins -p nixie policy >/dev/null
  ls /dev/shm | grep -c nixie-policy-test
  rm -f /dev/shm/nixie-policy-test-*
done

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).

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.
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.

1 participant