Skip to content

GH-50063: [C++] Validate buffer size for row-major tensors#50064

Merged
rok merged 1 commit into
apache:mainfrom
metsw24-max:tensor-rowmajor-bounds
May 29, 2026
Merged

GH-50063: [C++] Validate buffer size for row-major tensors#50064
rok merged 1 commit into
apache:mainfrom
metsw24-max:tensor-rowmajor-bounds

Conversation

@metsw24-max
Copy link
Copy Markdown
Contributor

@metsw24-max metsw24-max commented May 29, 2026

Rationale for this change

ValidateTensorParameters in cpp/src/arrow/tensor.cc only runs the CheckTensorStridesValidity buffer-overrun guard when strides are passed explicitly. With implicit (row-major) strides it computes strides for overflow but never checks the data buffer is large enough for the shape, so a tensor whose shape exceeds its buffer is accepted and later read out of bounds. This is reachable from IPC ReadTensor, where the shape comes from the flatbuffer and the body size is independent of it.

What changes are included in this PR?

Run CheckTensorStridesValidity on the computed row-major strides too.

Are these changes tested?

Added a case to TestTensor.MakeFailureCases.

Are there any user-facing changes?

No.

This PR contains a "Critical Fix". Crafted IPC tensor metadata (or any caller building a row-major tensor over an undersized buffer) bypassed the bounds check, enabling an out-of-bounds read.

@github-actions
Copy link
Copy Markdown

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

@github-actions
Copy link
Copy Markdown

⚠️ GitHub issue #50063 has no components, please add labels for components.

@raulcd raulcd requested a review from rok May 29, 2026 10:49
Copy link
Copy Markdown
Member

@rok rok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should do this.

@github-actions github-actions Bot added awaiting merge Awaiting merge and removed awaiting review Awaiting review labels May 29, 2026
@rok rok merged commit ca8a194 into apache:main May 29, 2026
59 of 60 checks passed
@rok rok removed the awaiting merge Awaiting merge label May 29, 2026
@rok rok requested a review from Copilot May 29, 2026 11:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a safety gap in C++ tensor validation where row-major tensors created with implicit strides could bypass the existing buffer-overrun guard, allowing tensors whose shape exceeds the backing buffer to be constructed (notably reachable via IPC ReadTensor where shape comes from metadata).

Changes:

  • Run CheckTensorStridesValidity even when strides are implicit (row-major), using the computed row-major strides.
  • Add a regression test ensuring Tensor::Make rejects row-major tensors whose shape requires more bytes than the buffer provides.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
cpp/src/arrow/tensor.cc Ensures implicit row-major strides also trigger the buffer-size / overrun validation.
cpp/src/arrow/tensor_test.cc Adds a failure-case regression test for undersized buffers with implicit row-major strides.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rok
Copy link
Copy Markdown
Member

rok commented May 29, 2026

Thanks contributing @metsw24-max

@conbench-apache-arrow
Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit ca8a194.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 8 possible false positives for unstable benchmarks that are known to sometimes produce them.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants