GH-49884: [C++] Fix integer overflow in BufferBuilder reachable from JSON parsing#49883
GH-49884: [C++] Fix integer overflow in BufferBuilder reachable from JSON parsing#49883metsw24-max wants to merge 2 commits intoapache:mainfrom
Conversation
|
Thanks for opening a pull request! If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project. Then could you also rename the pull request title in the following format? or See also: |
|
|
| if (ARROW_PREDICT_FALSE(new_capacity < 0)) { | ||
| return Status::Invalid("Resize: negative capacity"); | ||
| } | ||
| if (ARROW_PREDICT_FALSE(BufferBuilderCapacityTooLarge(new_capacity))) { | ||
| return Status::CapacityError("Resize: capacity overflow"); | ||
| } |
There was a problem hiding this comment.
Why not put these checks in AllocateResizableBuffer and PoolBuffer::Resize instead? That would benefit more use cases.
| /// \return Status | ||
| Status Reserve(const int64_t additional_bytes) { | ||
| auto min_capacity = size_ + additional_bytes; | ||
| if (ARROW_PREDICT_FALSE(additional_bytes < 0)) { |
There was a problem hiding this comment.
That's a lot of similar checks that we're adding in many methods. Can we find a way of factoring these out, and hopefully avoid forgetting any call sites where such checks must be done?
Rationale for this change
BufferBuilderandTypedBufferBuilderperform size calculations that are reachable from JSON parsing of untrusted input. These calculations previously used unchecked integer arithmetic (e.g.,size_ + additional_bytes,num_elements * sizeof(T)), which can overflow.Since the JSON parser constructs arrays, strings, and offsets based on input data, an attacker can influence these values. Overflow in these calculations can result in incorrect buffer sizing and unsafe memory operations.
What changes are included in this PR?
Added overflow-safe arithmetic using
internal::AddWithOverflowandinternal::MultiplyWithOverflowfor:size_ + additional_bytesnum_elements * sizeof(T)Added validation for negative inputs across:
AppendReserveResizeAdvanceFinishWithLengthHardened
GrowByFactorto prevent overflow when doubling capacityUpdated
TypedBufferBuilderto safely compute byte sizes from element countsAdded regression tests covering:
Are these changes tested?
Yes.
New tests were added to:
BufferBuilderandTypedBufferBuilderInvalid,CapacityError) instead of undefined behaviorAre there any user-facing changes?
No functional changes for valid inputs.
Invalid inputs (e.g., negative sizes or values causing overflow) that previously resulted in undefined behavior are now explicitly rejected with clear error statuses.
This PR contains a "Critical Fix".
Unchecked integer arithmetic in buffer size calculations could overflow when processing untrusted JSON input. This may result in under-allocation of buffers followed by writes of larger logical sizes, leading to potential out-of-bounds memory writes and undefined behavior.
This patch ensures all such arithmetic is validated, preventing incorrect buffer sizing and eliminating this class of memory safety issues.