Harden C++ FlatBuffers root access with checked verifier-backed APIs#8977
Open
0xEaS1 wants to merge 2 commits intogoogle:masterfrom
Open
Harden C++ FlatBuffers root access with checked verifier-backed APIs#89770xEaS1 wants to merge 2 commits intogoogle:masterfrom
0xEaS1 wants to merge 2 commits intogoogle:masterfrom
Conversation
|
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. |
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
This change adds new C++ helper APIs that combine FlatBuffers verification with obtaining the typed root pointer, providing a secure-by-default way to consume untrusted buffers while keeping all existing APIs and behavior intact.
Security motivation
flatbuffers::Verifier, then callGetRoot<T>()”; in practice, it is easy for callers to accidentally skip or mis-order the verification step, which can result in undefined behavior on malformed or adversarial inputs.nullptr.Changes
flatbuffers::GetRootCheckedandflatbuffers::GetSizePrefixedRootChecked(and their mutable variants) toinclude/flatbuffers/flatbuffers.h:(const void* buf, size_t len, const Verifier::Options& opts = Verifier::Options())(const void* buf, size_t len, const char* identifier, const Verifier::Options& opts = Verifier::Options())flatbuffers::Verifierand callVerifyBuffer<T>()/VerifyBuffer<T>(identifier)orVerifySizePrefixedBuffer<T, SizeT>()as appropriate.nullptrwhen verification fails instead of a potentially unsafe typed pointer.GetMutableRootChecked<T>(void* buf, size_t len, ...)GetMutableSizePrefixedRootChecked<T, SizeT>(void* buf, size_t len, ...)flatbuffers/flatbuffers.h, which already includesflatbuffers/verifier.h, so call sites don’t need any additional includes.tests/monster_test.cpp(CheckedRootApiTest) to exercise the new APIs against a known-goodMonsterbuffer and a truncated buffer:GetRootChecked<Monster>when given the correct identifier.GetRootCheckedto returnnullptrinstead of exposing undefined behavior.Testing
monster_testsuite passes with the new helpers in place.CheckedRootApiTestcovers successful and failing verification paths forGetRootChecked<Monster>.FLATBUFFERS_CODE_SANITIZEenabled, the additional code paths are covered by the same ASan/UBSan configuration that protects the rest of the C++ runtime.Backwards compatibility / migration notes
GetRoot<T>(),GetMutableRoot<T>(),GetSizePrefixedRoot<T>(), andGetMutableSizePrefixedRoot<T>()continue to behave exactly as before.flatbuffers::Verifier::Options) is reused as-is, so existing tuning of depth/table limits and alignment checks is preserved when callers choose to pass custom options to the checked helpers.