Repository navigation
Skip deprecated fields when generating JSON text from a buffer - #9473
Open
professor-moody wants to merge 1 commit into
Open
professor-moody wants to merge 1 commit into
professor-moody wants to merge 1 commit into
Conversation
This branch has not been deployed
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
The
Verify()that flatc generates for a table skips deprecated fields (thefield->deprecatedcheck insrc/idl_gen_cpp.cpp). That matches the schema docs: a deprecated field has no accessor, so nothing is supposed to read it.GenText()still walks every field in the schema, though, and for a deprecated field whose vtable slot is non-zero it reads the value and prints it. The vtable slot of a deprecated field is therefore never covered by a successful verification, and a buffer that passesVerifyMonsterBuffer()can still makeGenText()read outside the buffer.To see it with the stock
monster_test.fbs, build a Monster withnameandinventoryset (52 bytes), then set the vtable entry offriendly(id 4, deprecated) to 28, which points one byte past the end.VerifyMonsterBuffer()returns true. Copying the buffer into an exact-size heap allocation and callingGenText()makes AddressSanitizer report a heap-buffer-overflow READ of size 1 inflatbuffers::ReadScalar<unsigned char>, reached through the JSON printer's GenField. In a Release build the byte after the buffer is printed as the value offriendly. The stock schema only has a deprecated bool, so I also tried a deprecated string. With a small schema that has a deprecated string field, a 28 byte buffer that passes the generated verifier madeGenText()follow a crafted offset to memory after the buffer and print 64 bytes from there, with both the offset and the length taken from the file.This change makes
JsonPrinterskip deprecated fields. It does not makeGenText()safe for buffers that were never verified (that is a separate problem); it removes the case where a buffer that did pass verification is still unsafe.Behavior change to be aware of: a deprecated field that is present in the data, and fully in bounds, is no longer printed in the JSON output. The schema docs say old data may still contain such a field but "it won't be accessible anymore by newer code", so I read omitting it as consistent, but it is visible to anyone who relies on the old output. If you would rather keep printing deprecated fields (for example behind an option), I can rework it.
Test plan
Added
GenTextValidBufferControlTestandGenTextCraftedDeprecatedSlotTesttotests/test.cpp. Both build the Monster above, check thatVerifyMonsterBuffer()accepts it, and check that theGenText()output containsnamebut notfriendly. The first leaves the buffer as built; the second sets the vtable entry offriendlyto point at the first byte after the buffer. The buffer passed to the verifier sits in a vector with 8 readable bytes after the verified region, so the second test fails cleanly on the old code instead of faulting.I built
flattestswith CMake and ran the whole binary with no filter, on Linux x86_64 with clang 22.1.8 at 5eb190f, once as a Release build and once with -O1 and the address and undefined behavior sanitizers. Both print ALL TESTS PASSED and the sanitizer build reports nothing. Without thesrc/idl_gen_text.cppchange the crafted test fails in both builds at its last assertion and the control test passes. Making the buffer exactly the verified size, with no trailing bytes, reproduces the AddressSanitizer report above on the old code and runs clean with the change.A coverage build shows the new
continueis taken 55 times by the existing tests, all of which still pass. clang-format with the repository style leaves both files unchanged.AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.