Skip to content

GH-49884: [C++] Fix integer overflow in BufferBuilder reachable from JSON parsing#49883

Open
metsw24-max wants to merge 2 commits intoapache:mainfrom
metsw24-max:bufferbuilder-integer-overflow-json-path
Open

GH-49884: [C++] Fix integer overflow in BufferBuilder reachable from JSON parsing#49883
metsw24-max wants to merge 2 commits intoapache:mainfrom
metsw24-max:bufferbuilder-integer-overflow-json-path

Conversation

@metsw24-max
Copy link
Copy Markdown
Contributor

@metsw24-max metsw24-max commented Apr 28, 2026

Rationale for this change

BufferBuilder and TypedBufferBuilder perform 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::AddWithOverflow and internal::MultiplyWithOverflow for:

    • size_ + additional_bytes
    • num_elements * sizeof(T)
    • capacity growth logic
  • Added validation for negative inputs across:

    • Append
    • Reserve
    • Resize
    • Advance
    • FinishWithLength
  • Hardened GrowByFactor to prevent overflow when doubling capacity

  • Updated TypedBufferBuilder to safely compute byte sizes from element counts

  • Added regression tests covering:

    • negative values
    • addition overflow
    • multiplication overflow

Are these changes tested?

Yes.

New tests were added to:

  • Validate rejection of negative inputs
  • Detect overflow conditions in both BufferBuilder and TypedBufferBuilder
  • Ensure proper error handling (Invalid, CapacityError) instead of undefined behavior

Are 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.

@github-actions
Copy link
Copy Markdown

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?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@metsw24-max metsw24-max changed the title Fix integer overflow in BufferBuilder reachable from JSON parsing GH-49884: [C++] Fix integer overflow in BufferBuilder reachable from JSON parsing Apr 28, 2026
@github-actions
Copy link
Copy Markdown

⚠️ GitHub issue #49884 has been automatically assigned in GitHub to PR creator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant