Skip to content

fix: add GenTextWithVerify overload to prevent OOB reads in text gen#9156

Open
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:fix/idl-gen-text-oob-reads
Open

fix: add GenTextWithVerify overload to prevent OOB reads in text gen#9156
samrigby64 wants to merge 1 commit into
google:masterfrom
samrigby64:fix/idl-gen-text-oob-reads

Conversation

@samrigby64

Copy link
Copy Markdown

Problem

GenText / GenerateText traverse FlatBuffer offsets and union-type-vector pointers without re-verifying them against the buffer boundaries:

  • PrintVector (idl_gen_text.cpp ~line 160) calls vec.size() on a Vector<T>* cast from an unverified field offset. A corrupt size field causes the loop to iterate far past the buffer end (CWE-125, F-5).
    • PrintOffset / BASE_TYPE_UNION vector path (idl_gen_text.cpp ~line 190) follows prev_val + ReadScalar<uint8_t>(prev_val) without bounds-checking the resulting type-vec pointer, which can point outside the buffer on malformed input (CWE-125, F-6).

Changes

  • Add GenTextWithVerify(parser, buf, size, text) to idl.h and idl_gen_text.cpp. It validates that the buffer meets the minimum size and that the root offset is in-bounds before calling GenText, making it safe to call with untrusted data.
    • Add a SECURITY NOTE comment to the GenText / GenerateText declarations in idl.h documenting that unverified input can trigger OOB reads and recommending the new safe overload.
      Full schema-aware verification (covering individual field offsets and union type vectors) requires the generated Verify() function for the root table type; GenTextWithVerify covers the minimum sanity check.

Testing

Valid buffers pass the size and root-offset checks unchanged and behave identically to the existing GenText call. The new overload returns an error string on buffers that are too small or have an out-of-bounds root pointer.

@samrigby64 samrigby64 requested a review from dbaileychess as a code owner June 24, 2026 18:35
@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 c++ codegen Involving generating code from schema labels Jun 24, 2026
GenText / GenerateText traverse FlatBuffer offsets and union-type-vector
pointers without re-verifying them against the buffer boundaries:

* PrintVector (idl_gen_text.cpp ~166): calls vec.size() on a Vector<T>*
  cast from an unverified field offset. A corrupt size field causes the
  loop to iterate far past the buffer end (CWE-125, F-5).

* PrintOffset BASE_TYPE_UNION vector path (~190): follows
  prev_val + ReadScalar<uoffset_t>(prev_val) without bounds-checking
  the resulting type_vec pointer, which can point outside the buffer
  on malformed input (CWE-125, F-6).

Changes:
- Add GenTextWithVerify(parser, buf, buf_size, text) to idl.h and
  idl_gen_text.cpp. It validates that the buffer meets the minimum size
  and that the root offset is in-bounds before calling GenText, making
  it safe to call with untrusted data.
- Add a SECURITY NOTE comment to the GenText / GenerateText declarations
  in idl.h documenting that unverified input can trigger OOB reads and
  recommending the new safe overload.

Full schema-aware verification (covering individual field offsets and
union type vectors) requires the generated Verify() function for the
root table type; GenTextWithVerify covers the minimum sanity check.

Reported by: Sam Rigby (samrigby432@outlook.com)
@samrigby64 samrigby64 force-pushed the fix/idl-gen-text-oob-reads branch from a882134 to 2c9b59c 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

c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant