Repository navigation
Validate Npy format shape dimensions to prevent DoS from crafted files - #100625
Conversation
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]>
|
Workflow [PR], commit [7e052e8] Summary: ❌
AI ReviewSummaryThis PR strengthens Findings❌ Blockers
ClickHouse Rules
Final Verdict
|
- Use range-based for loop in shape dimension validation (clang-tidy modernize-loop-convert) - Skip file-size check for compressed read buffers: `tryGetFileSizeFromReadBuffer` unwraps `CompressedReadBufferWrapper` and returns the compressed source size, which would cause false `INCORRECT_DATA` rejections for valid compressed `.npy` inputs Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
| if (element_size == 0) | ||
| throw Exception(ErrorCodes::INCORRECT_DATA, "Npy element size is zero"); | ||
|
|
||
| static constexpr size_t max_bytes_per_row = 2ULL * 1024 * 1024 * 1024; /// 2 GiB |
There was a problem hiding this comment.
This introduces a hard 2 GiB per-row cap for Npy imports (data + offsets) and fails otherwise. That is a behavior change for valid .npy data that previously could be read, so it is a backward-incompatibility unless it is gated behind a setting (or tied to compatibility) and documented in SettingsChangesHistory.cpp.
Could you expose this threshold as a format setting (with compatibility-controlled default), instead of a fixed constant in code?
There was a problem hiding this comment.
No without a really good reason.
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 87.70% (107/122, 0 noise lines excluded) |
6c73494
|
Hi @Algunenano @alexey-milovidov — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
Follow-up to #99812 which fixed negative shape dimensions. This addresses the
remaining attack surface with large non-negative dimensions
(comment).
A crafted
.npyfile can claim arbitrarily large shape dimensions in its headerwhile containing little or no actual data. Without validation, this leads to
unbounded loops, excessive memory allocation, or integer overflow in the format
parser. For multi-dimensional arrays, all inner-dimension processing happens
inside a single
readRowcall, which meansmax_execution_timecannot interruptit — the time check only runs between rows.
Changes:
size_talso protects non-file sources (pipes, HTTP) where file size is unavailable
|S<huge>allocation DoS)NumpyDataTypeUnicodeconstructor (size * 4wrap)shape[0]isCancelledcheck in the innerreadValueloop somax_execution_timeis respected for single-row large arraysinttosize_tfor correctness (NumPy allowsdimensions larger than
INT_MAX)readData,readRows)Closes #99585
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Validate Npy format shape dimensions against file size and overflow limits to prevent denial of service from crafted
.npyfiles with unreasonably large dimensions. Also reject empty shapes and cap per-row memory to 2 GiB.Documentation entry for user-facing changes
Version info
26.4.1.262