Skip to content

fix: prevent OOB read in GetBufferStartFromRootPointer backward walk#9154

Open
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:fix/get-buffer-start-oob-read
Open

fix: prevent OOB read in GetBufferStartFromRootPointer backward walk#9154
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:fix/get-buffer-start-oob-read

Conversation

@samrigby64

Copy link
Copy Markdown

Problem

GetBufferStartFromRootPointer walks backward from start by sizeof(uoffset_t) each iteration, up to FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1 steps (~65 iterations on typical platforms). There is no lower bound check, so on buffers located close to the start of the address space the pointer can underflow past the allocation's beginning, triggering an out-of-bounds read (CWE-125).

The existing comment already calls this out: "Assert, because calling this function with bad data may cause reads outside of buffer boundaries." This PR addresses it.

Changes

Single-argument overload (existing API, no breaking change)

Adds a search_limit lower bound calculated from the initial start position so the loop terminates before walking more than FLATBUFFERS_MAX_ALIGNMENT bytes backward — the maximum distance a valid FlatBuffer start could ever be from start.

Two-argument overload (new, safe API)

Adds GetBufferStartFromRootPointer(root, buf, buf_size) that accepts the known buffer boundaries and uses the exact buf_start pointer as the hard lower bound. This makes the function safe to call even for buffers at low addresses.

Testing

No behavioral change for valid buffers; the loop exits the same way as before on well-formed input. The new overload adds a compile-time–detectable safety improvement for callers that have the buffer size available.

@samrigby64 samrigby64 requested a review from dbaileychess as a code owner June 24, 2026 18:33
@google-cla

google-cla Bot commented Jun 24, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the c++ label Jun 24, 2026
The backward pointer walk can read before the start of the buffer when
`root` is close to the beginning of the allocation and the loop iterates
the full FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1 steps.

Changes:
- Add a `search_limit` lower bound in the existing single-argument overload
  so the loop breaks before the pointer can underflow below the region that
  could plausibly contain the buffer start.
- Add a new two-argument overload `GetBufferStartFromRootPointer(root, buf,
  buf_size)` that accepts the known buffer boundaries and uses the exact
  buffer start as the hard lower bound, making the walk provably safe.

Reported by: Sam Rigby (samrigby432@outlook.com)
@samrigby64 samrigby64 force-pushed the fix/get-buffer-start-oob-read branch from 71a0bc7 to 92ca218 Compare June 24, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant