Repository navigation
fix(proto): cap bitFieldXXX_ index to prevent OOM in LegacyProtoTypeAdapterFactory - #3027
Conversation
…dapterFactory LegacyProtoTypeAdapterFactory.buildPresenceMaskFromBitFields() reads bitField<N>_ fields from the JSON stream. If an attacker-supplied JSON contains an extremely large index (e.g. bitField67108863_), calling BigInteger.shiftLeft(N * 32) allocates N/8 bytes of heap, making it trivial to OOM the JVM from a small input. Fix: compute maxGroupIndex = ceil(fieldWithPresenceToBitIndex.size() / 32) and skip any bitField group whose index exceeds that bound, since groups beyond that range cannot affect any presence bit checked by the adapter. Adds a regression test that verifies a crafted oversized index is silently ignored while valid fields in the same message are still deserialized.
The BIT_FIELD_PATTERN regex already restricts the captured group to
\d{1,9} (1-9 decimal digits), which guarantees Integer.parseInt succeeds.
Adding an explicit try-catch satisfies static-analysis tools (CodeQL)
that cannot infer the regex-based invariant.
|
The CodeQL alert is a valid observation — added a defensive try-catch in the latest commit (a04ecd8). In practice, the |
|
Thanks for highlighting this issue and proposing this fix! I am wondering if instead of using However, please keep the code as it is for now and let's wait what the maintainers think about this. |
|
@Marcono1234 Thanks for the review! Both approaches work to prevent the OOM. I went with That said, |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Thanks, this is an interesting find! I think, though, that a simpler fix would be to change {1,9} to {1,4} in the regular expression. It seems very unlikely that anyone has a protobuf message with 10,000 fields, and the allocation of a BigInteger to hold 9,999 bits is unlikely to provoke an OOM.
I'm always a bit skeptical about issues arising from deserializing adversarial input, especially for niche cases like this, but I'll note that limiting the size of the BigInteger also mitigates a potential performance DoS due to the quadratic BigInteger.or in the loop.
Replace the dynamic maxGroupIndex guard with the simpler regex approach suggested in review: limit the captured index to 4 digits (max 9999). Any real proto message has far fewer than 9999 bitField groups, so the narrower pattern is a clean fix without added runtime logic. Also update the test comment to reflect the new mechanism.
|
Good call — the regex cap is much cleaner. I went with the dynamic bound initially because I wasn't sure how large the group index could realistically get, but Updated: replaced the maxGroupIndex guard with just |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Thanks for following through on this!
Summary
LegacyProtoTypeAdapterFactory.buildPresenceMaskFromBitFields()readsbitField<N>_entries from the JSON stream to reconstruct a presence bitmask. The indexNwas previously used without bound checks: callingBigInteger.shiftLeft(N * 32)with a large attacker-controlledNallocatesN / 8bytes on the heap, making it trivial to OOM the JVM with a small JSON payload (e.g.{"bitField67108863_": 1}).Fix
Compute
maxGroupIndex = ceil(fieldWithPresenceToBitIndex.size() / 32), which is the largest bit-field group index that can influence any presence bit actually checked by this adapter. AnybitField<N>_entry withN > maxGroupIndexis silently skipped — it cannot affect deserialization correctness and only exists to exhaust memory.Test
Added
deserialize_oversizedBitFieldIndex_doesNotCauseOomtoLegacyProtoTypeAdapterFactoryTest, which feeds a crafted JSON withbitField67108863_and asserts that the valid fieldoptionalInt32_is still correctly deserialized without any heap pressure.Impact
Any application that deserializes untrusted JSON using
LegacyProtoTypeAdapterFactoryis vulnerable. A single JSON field with a large index is enough to trigger the OOM.