fix: guard against null prev_val in JsonPrinter::PrintOffset for unions (fixes #9033) - #9236
Open
jdymitarai wants to merge 1 commit into
Open
fix: guard against null prev_val in JsonPrinter::PrintOffset for unions (fixes #9033)#9236jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
…ns (fixes google#9033) In JsonPrinter::PrintOffset(), union fields require reading the union type discriminator byte via `prev_val`. Previously, the code only checked `FLATBUFFERS_ASSERT(prev_val)`. In release builds (-DNDEBUG), asserts are compiled out. If a malformed or corrupt buffer has the union value field present in the vtable but the union type discriminator absent, `prev_val` is nullptr, causing an immediate segmentation fault (CWE-476). Similarly, if vector_index is out of range for the union type vector, it could lead to an out-of-bounds read. This commit adds runtime validation for `prev_val` and vector bounds before dereferencing, returning descriptive error messages and propagating them safely through GenText(). Also adds a unit test in tests/test.cpp to ensure corrupt buffers are rejected cleanly. Fixes google#9033.
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.
Summary of Changes
Fixes #9033 (CWE-476 Null Pointer Dereference in
JsonPrinter::PrintOffset()for union types).Root Cause
In
src/idl_gen_text.cpp,JsonPrinter::PrintOffset()dereferencesprev_valto obtain the union type discriminator:In release builds (
-DNDEBUG),FLATBUFFERS_ASSERTis removed. When processing an untrusted or malformed FlatBuffer where the union value field is present in the table vtable but the union type discriminator is absent (vtable offset zeroed),prev_valisnullptr. Attempting*prev_valimmediately causes a segmentation fault (SIGSEGV), terminatingflatcor any library converting unverified/corrupted binary buffers to JSON.Additionally, for vectors of unions (
vector_index >= 0), ifvector_index >= type_vec->size(), accessingtype_vec->Get(vector_index)can result in an out-of-bounds read.Fix
FLATBUFFERS_ASSERT(prev_val)with a runtime check returning"corrupt buffer: missing union type field".static_cast<uoffset_t>(vector_index) >= type_vec->size()) returning"corrupt buffer: union type vector out of range".tests/test.cpp(UnionVectorTest) that crafts a corrupted vtable with missing union type discriminator and verifies thatGenText()returns an error string instead of crashing.