Fix DoS via panic in Go GetRootAs functions on short buffers#9039
Open
YLChen-007 wants to merge 1 commit intogoogle:masterfrom
Open
Fix DoS via panic in Go GetRootAs functions on short buffers#9039YLChen-007 wants to merge 1 commit intogoogle:masterfrom
YLChen-007 wants to merge 1 commit 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. |
Add bounds checking to GetRootAs, GetSizePrefixedRootAs, GetSizePrefix, GetIndirectOffset, and GetBufferIdentifier in go/lib.go to prevent panics when called with buffers shorter than SizeUOffsetT (4 bytes). Also update the flatc Go code generator (idl_gen_go.cpp) so that all generated GetRootAs<Type> and GetSizePrefixedRootAs<Type> functions include the same bounds check, returning nil on short buffers instead of panicking. This extends the fix previously applied only to the gRPC path (grpc.go, PR google#8684) to the general-purpose API.
9e254ff to
b5b9929
Compare
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
Fix a Denial of Service (DoS) vulnerability (CWE-125) in the Go FlatBuffers runtime and code generator. The
GetRootAsfamily of functions panic on buffers shorter than 4 bytes due to missing bounds checks before reading the rootUOffsetT, causing complete process termination.Root Cause
When
GetRootAs(buf, offset, fb)is called, it passesbuf[offset:]toGetUOffsetT(), which internally callsGetUint32():If the buffer has fewer than 4 bytes remaining after
offset,buf[3]triggers an unrecoverable Go panic. Since Go panics crash the entire process unless explicitly recovered, an attacker can crash any Go application processing untrusted FlatBuffers data by sending a single 3-byte payload.A similar vulnerability in the gRPC deserialization path was previously fixed in PR #8684 by adding a
len(data) < SizeUOffsetTcheck. However, the general-purposeGetRootAsAPI was left unprotected.Fix
This PR adds bounds checking to:
go/lib.go(runtime) — All public deserialization entry points:GetRootAs()— checksint(offset)+SizeUOffsetT > len(buf)GetSizePrefixedRootAs()— checksint(offset)+sizePrefixLength+SizeUOffsetT > len(buf)GetSizePrefix()— checks before reading size prefixGetIndirectOffset()— checks before reading indirect offsetGetBufferIdentifier()/GetSizePrefixedBufferIdentifier()— checks minimum buffer lengthsrc/idl_gen_go.cpp(code generator) — Allflatc-generatedGetRootAs<Type>()andGetSizePrefixedRootAs<Type>()functions now include the same bounds check, returningnilon short buffers.The fix maintains backward API compatibility — no function signatures are changed. Functions silently return zero-values/nil on insufficient data, which is consistent with Go's zero-value semantics and the existing gRPC error handling pattern.
Testing
go/lib_test.go— New unit tests covering all short-buffer panic scenarios (nil, empty, 1-3 byte buffers) plus regression tests for valid buffersflattestspass:ALL TESTS PASSEDflatc -gPoC (Before Fix)
Related
grpc.go)