fix(rust): bound NUL scan in get_option_string to reported length - #37
Open
fornwall wants to merge 1 commit into
Open
fix(rust): bound NUL scan in get_option_string to reported length#37fornwall wants to merge 1 commit into
fornwall wants to merge 1 commit into
Conversation
`get_option_string` called `CStr::from_ptr` on a heap buffer that was truncated to exactly the length the external (untrusted) driver reported. `CStr::from_ptr` scans for a NUL terminator with no upper bound, so if a misbehaving driver reports a length that excludes the terminator (or reports 0, yielding an empty Vec with a dangling pointer), the scan reads past the end of the allocation — undefined behavior. Read the buffer in a bounded way instead: reinterpret the bytes as a `&[u8]` bounded by `value.len()` and use `CStr::from_bytes_until_nul`. If no NUL is found within the reported length, treat the whole bounded buffer as the string. No read can exceed `value.len()`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNCrC87g9MkppGpL4MDgh5
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.
Bug
get_option_stringinrust/ffi/src/options.rscalledCStr::from_ptron a heap buffer (Vec<c_char>) thatget_option_buffersizes to exactly the length the external (untrusted) driver reports.CStr::from_ptrscans for a NUL terminator with no upper bound. A misbehaving driver that reports a length excluding the terminator — or reports 0, yielding an emptyVecwith a dangling pointer — causes the scan to read past the end of the allocation. This is an out-of-bounds read / undefined behavior on untrusted driver output.Fix
Read the buffer in a bounded way: reinterpret its bytes as a
&[u8]bounded byvalue.len()and useCStr::from_bytes_until_nul. When no NUL is present within the reported length, the whole bounded buffer is treated as the string viaString::from_utf8_lossy. No read can exceedvalue.len().CStr::from_bytes_until_nulis stable since Rust 1.69; the workspace MSRV is 1.85, so it is available.Verification
cd rust && cargo build -p adbc_ffisucceeds.🤖 Generated with Claude Code
https://claude.ai/code/session_01XNCrC87g9MkppGpL4MDgh5