Skip to content

fix: guard against null struct_def in IsStruct/Deserialize (fixes #9144)#9148

Open
Ashutosh0x wants to merge 3 commits into
google:masterfrom
Ashutosh0x:fix/segv-deserialize-null-structdef
Open

fix: guard against null struct_def in IsStruct/Deserialize (fixes #9144)#9148
Ashutosh0x wants to merge 3 commits into
google:masterfrom
Ashutosh0x:fix/segv-deserialize-null-structdef

Conversation

@Ashutosh0x

Copy link
Copy Markdown

Fixes #9144

Problem

StructDef::Deserialize crashes with SEGV when deserializing a bfbs schema that passes VerifySchemaBuffer but has unresolved struct cross-references. IsStruct(), IsIncompleteStruct(), and IsTable() all dereference ype.struct_def without null check.

Root Cause

VerifySchemaBuffer validates FlatBuffer table/vtable structure but not the schema cross-references that Deserialize depends on. A field declared as BASE_TYPE_STRUCT can have a null struct_def, which IsStruct() dereferences unconditionally at idl.h:533:

cpp inline bool IsStruct(const Type& type) { return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed; // ^^ NULL DEREF }

The crash path: Parser::Deserialize -> StructDef::Deserialize -> InlineSize -> IsStruct -> SEGV.

Fix

  1. Add ype.struct_def != nullptr guard to IsStruct(), IsIncompleteStruct(), and IsTable() in idl.h
  2. Add explicit validation in StructDef::Deserialize that rejects fields with unresolved struct references before computing layout (defense-in-depth)
  3. Regression test with the exact 134-byte PoC from SEGV in StructDef::Deserialize via IsStruct on a bfbs schema that passes VerifySchemaBuffer #9144

Testing

  • Regression test confirms Deserialize returns alse instead of crashing with SEGV
  • Existing tests unaffected (null check only triggers on malformed input)

Impact

Any application following the recommended 'verify before use' pattern (VerifySchemaBuffer + Deserialize) is vulnerable to DoS via crafted bfbs schema input.

Add bounds checking and null validation when deserializing .bfbs files:
- Null-check object->fields() before dereferencing
- Detect duplicate field IDs to prevent silent overwrites
- Null-check individual field pointers in the loop
- Null-check enum values() and included_filenames() pointers

These checks prevent heap buffer overflow via maliciously crafted
.bfbs files where field IDs exceed the fields array size.

Fixes google#8932
GenText/GenTextFile trusts serialized field offsets and vector lengths
without running the buffer through flatbuffers::Verifier first. A
malformed binary with a corrupted vector length causes flatc --json to
read past the buffer allocation, leaking heap contents into the JSON
output or crashing with SIGSEGV.

Add a Verifier check before buffer traversal in GenTextFile. If the
buffer fails verification, return an error instead of proceeding with
potentially dangerous reads.

Fixes google#9051
@Ashutosh0x Ashutosh0x requested a review from dbaileychess as a code owner June 21, 2026 13:19
@github-actions github-actions Bot added c++ codegen Involving generating code from schema labels Jun 21, 2026
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.

SEGV in StructDef::Deserialize via IsStruct on a bfbs schema that passes VerifySchemaBuffer

1 participant