Skip to content

fix(flatbuffers): guard GetBufferStartFromRootPointer against OOB read before buffer start#9158

Open
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:samrigby64-patch-1
Open

fix(flatbuffers): guard GetBufferStartFromRootPointer against OOB read before buffer start#9158
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:samrigby64-patch-1

Conversation

@samrigby64

Copy link
Copy Markdown

Summary

GetBufferStartFromRootPointer walks backwards from the root pointer by repeatedly subtracting sizeof(uoffset_t) (up to 9 iterations for 32-byte-aligned buffers) to find the buffer start. It has no check that the walking pointer remains within the buffer allocation. A caller that supplies a root pointer near the beginning of the buffer — or a crafted FlatBuffer with a small or zero vtable offset — will cause the function to read memory before the heap allocation.

Root cause

// include/flatbuffers/flatbuffers.h — before this patch
for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
     possible_roots; possible_roots--) {
  start -= sizeof(uoffset_t);   // no lower-bound guard
  if (ReadScalar<uoffset_t>(start) + start == ...)
    return start;
}

Fix

Add an optional buf_start parameter (defaults to nullptr — all existing callers are unchanged). When provided, the backward search stops before walking past buf_start, breaking out of the loop and falling through to the existing FLATBUFFERS_ASSERT(false) path.

// After patch
inline const uint8_t* GetBufferStartFromRootPointer(
    const void* root, const uint8_t* buf_start = nullptr) {
  ...
  for (...) {
    if (buf_start != nullptr && start < buf_start + sizeof(uoffset_t)) break;
    start -= sizeof(uoffset_t);
    ...
  }
}

The guard fires before the subtraction, so it is safe against pointer underflow. Backward compatibility is preserved — callers that omit buf_start behave exactly as before.

@samrigby64 samrigby64 requested a review from dbaileychess as a code owner June 25, 2026 11:34
@github-actions github-actions Bot added the c++ label Jun 25, 2026
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