Skip to content

git Reject negative shape dimensions in NPY format parser - #99812

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
Daedalus-Icarus:fix-npy-negative-shape
Mar 18, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
Daedalus-Icarus:fix-npy-negative-shape

Conversation

@Daedalus-Icarus

@Daedalus-Icarus Daedalus-Icarus commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Reject negative shape dimensions in NPY format parser.

A maliciously crafted .npy file with negative shape dimensions (e.g. shape=(-1,)) causes an infinite loop because the signed int value wraps to a huge number when implicitly cast to size_t. This adds validation that all dimensions are non-negative during header parsing.

Closes #99585

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix infinite loop when reading Npy format files with negative shape dimensions.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

No documentation changes needed — this is a bug fix for invalid input handling.

Version info

  • Merged into: 26.3.1.776

@Daedalus-Icarus

Copy link
Copy Markdown
Contributor Author

Hi @PedroTadim @Algunenano
Could you please review this PR?

@Algunenano Algunenano self-assigned this Mar 17, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Mar 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6dcfdf7]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness issue in Npy header parsing by rejecting negative shape dimensions before they are used as size_t, and adds a focused regression test for both 1D and multidimensional negative-shape inputs. I did not find any blocker/major issue in the patch itself.

Missing context

  • ⚠️ No CI run results/logs were provided in this review context, so validation here is code-level only.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.80% +0.10%
Functions 23.90% 23.90% +0.00%
Branches 76.20% 76.30% +0.10%

PR changed lines: PR changed-lines coverage: 87.50% (7/8, 0 noise lines excluded)
Diff coverage report
Uncovered code

@Daedalus-Icarus

Copy link
Copy Markdown
Contributor Author

Hi @PedroTadim @Algunenano
I've done. Could you please review this?

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this change.

But what happens with large non-negative dimensions?

@alexey-milovidov alexey-milovidov self-assigned this Mar 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 18, 2026
Merged via the queue into ClickHouse:master with commit 98c6a9f Mar 18, 2026
159 checks passed
@Daedalus-Icarus

Copy link
Copy Markdown
Contributor Author

Thanks @alexey-milovidov and @Algunenano

@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 18, 2026
Algunenano added a commit to Algunenano/ClickHouse that referenced this pull request Mar 24, 2026
Follow-up to ClickHouse#99812 which fixed negative shape dimensions. This addresses
the remaining attack surface with large non-negative dimensions:

- Validate that the product of all shape dimensions does not overflow `size_t`
- Validate that claimed data size matches the actual file size (when known)
- Cap per-row memory footprint (data + array offset overhead) to 2 GiB
- Cap individual element size to 2 GiB (prevents `|S<huge>` DoS)
- Add overflow check in `NumpyDataTypeUnicode` constructor (`size * 4`)
- Reject empty shape (prevents OOB access on `shape[0]`)
- Add periodic `isCancelled` check in the inner `readValue` loop so
  `max_execution_time` is respected even for single-row large arrays
- Widen shape dimensions from `int` to `size_t` for correctness
- Remove dead declarations (`readData`, `readRows`)

Closes ClickHouse#99585

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Npy format: negative shape dimension causes infinite loop

4 participants