Skip to content

Bound the size of the PostgreSQL startup message - #115708

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-postgresql-startup-message-size
Aug 23, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-postgresql-startup-message-size

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

The startup message of the PostgreSQL wire protocol carries its own size, which is read from the
socket before any authentication, and the message is read into memory in full. The size was not
bounded, so an unauthenticated peer could make the server hold an arbitrary amount of memory per
connection. Bound it at 10000 bytes, the same limit PostgreSQL itself uses.

Bounding the declared size is not sufficient on its own, because StartupMessage::deserialize reads
parameter names and values with readNullTerminated, which keeps appending to the string until the
next '\0' arrives, regardless of the declared size. Therefore MessageTransport::receiveWithPayloadSize
now parses the message through a LimitReadBuffer bounded by the declared payload size, so the parser
cannot read past the end of the message.

The guard inside StartupMessage::deserialize that was meant to catch a message whose parameters
run past the declared size checked the declared size itself, which is a member that never changes,
instead of the remaining size.

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):

Bound the size of the startup message of the PostgreSQL wire protocol, which is read before authentication.


Workflow [PR]
Sync PR [sync-upstream/pr/115708]

Version info

  • Backported to: 26.7.6.10, 26.6.4.14, 26.5.8.26, 26.3.24.2, 25.8.34.3

@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [47928ab]

Summary: ✅


AI Review

Summary

The PR hardens the PostgreSQL startup-message path before authentication by rejecting oversized packets, parsing the declared payload through LimitReadBuffer, and adding a socket-level regression test for the unterminated-parameter case. I reviewed the current diff and prior discussion, and I did not find any remaining correctness, compatibility, or test-coverage issues in the touched code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.10% 87.10% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 16/20 (80.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 21, 2026
Comment thread src/Core/PostgreSQLProtocol.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 47928ab4b with master b6bbf49a3 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 705.75 MiB 702.73 MiB -3.03 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

11 translation units recompiled, 45 s compile time in total, 11 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +1.4 s (+3%) in total before that adjustment.

Job report

Bounding the declared size of the startup message is not enough on its own:
`StartupMessage::deserialize` reads parameter names and values with
`readNullTerminated`, which keeps appending to the string until the next `'\0'`
arrives, regardless of the declared size. A client could therefore declare a tiny
startup packet and then stream an arbitrarily long unterminated parameter,
consuming memory before authentication.

Wrap the connection in a `LimitReadBuffer` bounded by the declared payload size
in `MessageTransport::receiveWithPayloadSize`, so the parser cannot read past the
end of the message. The remaining-size check inside `StartupMessage::deserialize`
stays as a consistency check within that bounded payload.

Add a socket-level test that declares a small startup message and then streams
bytes without a terminator.

Address review: #115708
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115708&sha=a201a80e7992e32864d0fa026e4acdd612804788&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 — Logical error: Initiator received more initial requests than there are replicas: replica_num=A in Stress test (amd_tsan), and provide a fix in a separate PR. If the fix is already in progress, link it here.

It is unrelated to this PR (which only touches the PostgreSQL wire protocol): the same logical error has been hit on many unrelated pull requests over the last two months (#115168, #115518, #114679, #97032, #114477, #114249, #103526, #102192). It looks like a recurrence of #65043, which was closed in 2025.

@groeneai

Copy link
Copy Markdown
Collaborator

A fix is already in progress: #115786 ("Do not run the server-side AST fuzzer on a collaborative worker context"), mine, open. No new PR needed.

I read this off the report's own server log rather than the message text, because this family has two adjacent guards in the same function that one event can trip.

Carrier: query 2ae5091f, test 04649_atomic_populate_materialized_view_query_local_parallel_replicas (CREATE MATERIALIZED VIEW ... POPULATE, max_parallel_replicas = 3, parallel_distributed_insert_select = 2).

t (12:58:41) event
.884917 coordinator created for test_1.src
.885 / .887 / .888 initial requests from replicas 2, 0, 1, giving Received initial requests: 3 Replicas count: 3, i.e. at the limit
.900482 worker 797202c5 (replica 0): Got response: 0 parts. Finish: true, its read is done
.906106 same worker: ASTFuzzer: Fuzzed query: INSERT INTO test_1.mv SETTINGS enable_parallel_replicas = 1, ...
.908028 the fuzzed copy runs as internal query e8c975c3
.916320 e8c975c3: MergeTreeReadPoolParallelReplicas: Chosen segment size: 128, so it built its own parallel-replicas read pool
.916685 abort at ParallelReplicasReadingCoordinator.cpp:229

The discriminator: e8c975c3 creates zero coordinators of its own, against a positive control of 2 for the initiator. So it inherited the worker context's coordination callbacks and replica number, and sent a fourth sendInitialRequest into an already-full three-replica coordinator. That is exactly the re-entry #115786 blocks, one level above any statement-level setting.

Breadth over 60 days: 9 hits, all in fuzzer-enabled flavours (6 Stress, 3 AST fuzzer) and none outside them, while test 04649 itself ran 53653 times under Stateless tests with zero hits.

On #65043: same assertion, but the reproducers recorded there are merge()/view/join planning shapes, so I read this as a second producer reaching the same guard rather than a straight recurrence. #115786 covers this carrier's producer, not that planning-route one.

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 23, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Aug 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 23, 2026
Merged via the queue into master with commit 9b147d0 Aug 23, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-postgresql-startup-message-size branch August 23, 2026 17:49
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 23, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 23, 2026
Backport #115708 to 26.6: Bound the size of the PostgreSQL startup message
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Aug 23, 2026
Backport #115708 to 26.7: Bound the size of the PostgreSQL startup message
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Resolved a conflict in `src/Server/PostgreSQLHandler.cpp`: 26.5 has no
`classifyNoOpDriverCommand` anonymous-namespace block and no
`UNEXPECTED_PACKET_FROM_CLIENT` error code (both come from unrelated master
changes), so only the `UNKNOWN_PACKET_FROM_CLIENT` declaration needed by
#115708 was kept.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115708 to 25.8: Bound the size of the PostgreSQL startup message
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115708 to 26.3: Bound the size of the PostgreSQL startup message
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115708 to 26.5: Bound the size of the PostgreSQL startup message
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 26, 2026
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115708 to 26.5: Bound the size of the PostgreSQL startup message
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115708 to 26.3: Bound the size of the PostgreSQL startup message
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115708 to 25.8: Bound the size of the PostgreSQL startup message
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
PROTO: PostgreSQL wire startup message before auth -> DoS.
Effect in the production build a394e92: its upstream test(s) 05037_postgresql_startup_message_size fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 05037_postgresql_startup_message_size.

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

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants