Repository navigation
Conversation
Fix a memory-exhaustion vulnerability in the `CustomSeparated` input
format. When the configured `format_custom_row_after_delimiter` does
not appear in the input (malformed or adversarial data), the loop
inside `CustomSeparatedFormatReader::readRowImpl` accumulates fields
into a `std::vector<String>` without any upper bound. With millions
of short fields the backing array grows geometrically and can request
multi-gigabyte allocations — large enough to trip MemorySanitizer's
8 GB allocation cap or OOM the server outright.
The CI failure triggering this was a fuzzer hit on
`Stress test (experimental, serverfuzz, arm_msan)` (STID 4260-52c8):
ERROR: MemorySanitizer: requested allocation size 0x300000000
exceeds maximum supported size of 0x200000000
The 12 GB allocation was the backing array of `values` during
`readRowForHeaderDetection` — roughly a quarter of a billion accumulated
fields before the bounds check would otherwise have ever run.
Fix:
* introduce `input_format_custom_max_number_of_fields_per_row`
(default `1'000'000`) — high enough for every realistic schema,
low enough to cap memory usage at well under 30 MB;
* check the bound inside `readRowImpl` before each `push_back`;
reads exceeding the bound fail fast with `INCORRECT_DATA` and
a message pointing to the setting;
* covers both the header-detection path
(`tryDetectHeader -> readRowForHeaderDetection`) and the schema
inference path (`CustomSeparatedSchemaReader::readRow`), since
both share `readRowImpl`'s unknown-columns branch.
A regression test at `tests/queries/0_stateless/04110_custom_separated_max_fields_bound.sh`
exercises both protected paths with a low limit (10), and confirms
well-formed inputs still parse under the default.
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7b48c1bd31ece9f7af46100f1c07c58577ba7f87&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_msan%29
Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pre-PR validation gate (session
|
|
cc @Avogar @alexey-milovidov — could you review this? The fix bounds the unbounded |
…m-separated-header-detection-bounds-20260422
|
Rebased onto current master. Branch now tracks master cleanly (was CONFLICTING since ~2026-04-24 00:00 UTC). Summary of the change:
No code changes from the previous revision — only rebasing to resolve the merge conflict so CI can run against current master. @Avogar @alexey-milovidov — this is a P0 sanitizer fix on master (MSAN 12 GiB allocation, STID 4260-52c8). Would appreciate a review when you have a moment. Pre-PR validation gate is in the comment above. Session: |
|
Workflow [PR], commit [d6856b2] Summary: ❌
AI ReviewSummaryThis PR adds a hard per-row field cap ( Final Verdict
|
CI status after rebase — all 4 failures unrelated to this fixVerified against CIDB for rebased HEAD
Why none can have been caused by this PRThis PR modifies only
STID 4260-52c8 master trunk status~40 hours clean since PR opened. Only the original 2026-04-22 20:45 UTC master hit (commit Request@Avogar @alexey-milovidov — could you take another look when you have a moment? Fix summary from the pre-PR validation gate: new Session: |
…m-separated-header-detection-bounds-20260422
Rebased onto current master at 2026-04-26 01:50 UTCcc @Avogar @alexey-milovidov — sorry for the silence; PR went
Master STID 4260-52c8 status
Reviewer askCould you take another look at this when you get a chance? AI Review previously approved on the rebased HEAD Ref: STID 4260-52c8 — (Session: cron:clickhouse-ci-task-worker:20260426-014500) |
…constant Following @Avogar's review feedback on PR ClickHouse#103404 (ClickHouse#103404 (comment)): no realistic use case wants to tune this number, so replace the user-facing `input_format_custom_max_number_of_fields_per_row` setting with a file-local `constexpr size_t MAX_FIELDS_PER_ROW = 1'000'000` inside `src/Processors/Formats/Impl/CustomSeparatedRowInputFormat.cpp`. Drops: * the setting declaration in `src/Core/FormatFactorySettings.h`, * the matching `26.5` entry in `src/Core/SettingsChangesHistory.cpp`, * the wire-up in `src/Formats/FormatFactory.cpp`, * the `FormatSettings::custom::max_number_of_fields_per_row` member. The regression test `tests/queries/0_stateless/04110_custom_separated_max_fields_bound.sh` is refactored to exercise the hard-coded bound directly: it feeds 1_000_001 `-`-separated fields with no `format_custom_row_after_delimiter` through `readRowForHeaderDetection -> readRowImpl<AS_POSSIBLE_STRING>` and greps for the `INCORRECT_DATA` preamble; the well-formed subtest is kept as a baseline. Local verification: bounded subtest fails fast with code 117 (`INCORRECT_DATA`) in ~0.4 s, peak RSS ~450 MB; well-formed subtest returns 3 rows. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7b48c1bd31ece9f7af46100f1c07c58577ba7f87&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_msan%29 PR: ClickHouse#103404 Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@Avogar — addressed in Pre-PR validation gate (re-run for the new commit)
Inline reply on the original thread: #103404 (comment) cc @alexey-milovidov FYI — same fix scope, just internalised. |
LLVM Coverage Report
Changed lines: 100.00% (11/11) · Uncovered code |
8d3cf43
|
Hi — could this be a backport candidate for Affected code: Why this might apply: The vulnerable code in Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
`test_hedged_requests/test.py::test_stuck_replica` is chronically flaky
on heavily overcommitted ASan/UBSan integration shards. The failure shape
in CIDB is identical across 9 unrelated PRs in the past 30 days:
pause_container('node_1') did not become observably effective within
90.0s ... still succeed after ~890 probe iterations; last outcome:
server replied (4 bytes)
The probe in `_wait_for_pause_effective` keeps receiving live ClickHouse
exception packets from `node_1` for the entire 90s budget, which proves
that `docker compose pause` returned success while the cgroup freezer
never actually froze the user-space tasks. The existing SIGSTOP fallback
in `ClickHouseCluster.pause_container` only fires when `docker compose
pause` raises an exception, not when it lies about success, so the
escalation path was never taken.
Switch this test to `cluster.pause_container_using_signal`, which uses
`SIGSTOP`/`SIGCONT` from the start. A signal cannot be silently lost the
way the cgroup freezer can, so the pause becomes observable on the very
first probe iteration. Same helper is already used by
`test_postgresql_replica_database_engine` for the same reason.
The change is local to one call site. The chronic flake hit
`Integration tests (amd_asan_ubsan, db disk, old analyzer, 1/6)` in the
following PRs (CIDB, last 30 days): ClickHouse#103404, ClickHouse#106311, ClickHouse#105010, ClickHouse#105056,
ClickHouse#104830, ClickHouse#106025, ClickHouse#100391, ClickHouse#106571, ClickHouse#103229. All show the same
`server replied (4 bytes)` outcome, distinguishing this from generic
flakiness.
Related: ClickHouse#106571
A fuzzer hit on
Stress test (experimental, serverfuzz, arm_msan)flagged a 12 GB MemorySanitizer allocation insideCustomSeparatedFormatReader::readRowImpl(STID4260-52c8, master commit7b48c1bd31ece9f7af46100f1c07c58577ba7f87, 2026-04-22 20:45 UTC):Root cause
readRowImplaccumulates fields intostd::vector<String> valueswith:When
format_custom_row_after_delimiterdoes not appear in the input — either malformed data or a fuzzer-crafted payload —checkForEndOfRownever returnstrue. The vector grows geometrically until an allocation crosses MSan's 8 GB cap (0x300000000is the allocation request for the next doubling; the previous capacity already held ≈250 Mstd::stringslots).Reproduced locally (release debug build) with a 100 MB CustomSeparated input of 50 million short fields and no
row_after_delimiter— peak RSS reached 3.1 GiB in ~7 s. Under MSan's smaller cap the same shape trips the assertion earlier.The same
readRowImplunknown-columns branch is reached by every call site that reads a row without a known column count:readRowForHeaderDetection(this crash),readNames/readTypes(WithNames / WithNamesAndTypes variants), andCustomSeparatedSchemaReader::readRow. All of them share the vulnerability.Fix
Hard-code a per-row field cap inside
CustomSeparatedRowInputFormat.cpp:readRowImplchecksvalues.size() >= MAX_FIELDS_PER_ROWat the top of the loop and fails fast withINCORRECT_DATA. The cap covers every legitimate CustomSeparated schema by a comfortable margin and bounds the worst-case allocation at well under 30 MB on the parser's hot path. No user-facing setting — per @Avogar's review feedback, there is no realistic use case for tuning this number.Test
Regression test at
tests/queries/0_stateless/04110_custom_separated_max_fields_bound.sh:bounded— header detection path (tryDetectHeader -> readRowForHeaderDetection -> readRowImpl<AS_POSSIBLE_STRING>) fed an adversarial 1 000 001-field input with noformat_custom_row_after_delimiter. ExpectsINCORRECT_DATA. Without the patch the same input growsvaluesunboundedly and is killed by MSan / OOM before reaching the bound.well-formed— a normal 3-column / 3-row input through the same path. Expectscount() = 3, demonstrating the cap does not interfere with legitimate use.readRowImplis the only place the bound is checked, and every caller (header detection, schema inference,readNames/readTypes, variable-column rows) goes through it, so testing the header-detection caller exercises the protection for the whole family.Scope note
TSV and CSV row readers have analogous unbounded do/while loops (
TabSeparatedFormatReader::readRowImpl,CSVFormatReader::readRowImpl), but each of their iterations consumes at least one delimiter byte, so the field count is naturally capped by the input size. The CustomSeparated reader is unique in that its configured delimiters can be multi-character or empty, allowing zero-byte iterations. This PR intentionally targets only the format where the fuzzer demonstrated the issue; the TSV / CSV paths can be hardened separately if desired.Failure report
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=7b48c1bd31ece9f7af46100f1c07c58577ba7f87&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_msan%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Protect the
CustomSeparatedinput format against malformed or adversarial data that omitsformat_custom_row_after_delimiter. Header detection, schema inference, and variable-column rows previously accumulated fields unboundedly and could allocate many gigabytes of memory before failing. Reads now fail withINCORRECT_DATAonce a single row contains more than 1 000 000 fields.Documentation entry for user-facing changes
Version info
26.5.1.823