Skip to content

Bound the declared length of PostgreSQL messages parsed before authentication - #130462

Merged
tiandiwonder merged 1 commit into
masterfrom
fix/postgresql-auth-message-size-limit
Nov 12, 2026
Merged

tiandiwonder merged 1 commit into
masterfrom
fix/postgresql-auth-message-size-limit

Conversation

@antonio2368

@antonio2368 antonio2368 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Related: #116998

Every frontend message of the PostgreSQL protocol is parsed inside a buffer that ends at the length
the message declares, so a field can no longer grow past the end of its own frame. The declared
length itself, however, was unbounded for the messages that are parsed before the client is
authenticated: PasswordMessage, SASLInitialResponse and SASLResponse are read into memory in
full, and the client that declares their length is not trusted yet. A client could therefore
announce the Int32 maximum and make the server buffer a payload it never finishes sending.

This bounds the declared length of those three messages by 10000 bytes, the limit that
receiveStartupMessage already applies to the startup message, which is parsed in the same phase.
FrontMessage::deserializePayload takes the limit as a parameter and rejects a larger declared
length before any payload is read, and max_startup_message_size now uses the same constant
instead of repeating the value.

The stateless PostgreSQL protocol test covers all three messages, both at the Int32 maximum and
just above the limit, plus the SCRAM final response that requires a well-formed client-first message
before it can be reached.

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

Messages of the PostgreSQL wire protocol that are parsed before authentication are now bounded by the server, so a client can no longer make the server buffer a payload that it never sends.


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

Version info

  • Merged into: 26.11.1.2018-master (included in 26.11 and later)

…tication

`PasswordMessage`, `SASLInitialResponse` and `SASLResponse` are parsed before the client is
authenticated, so the frame length they declare is chosen by a client the server does not trust yet.
The payload is parsed inside a buffer that ends at that length, but the length itself was unbounded,
so a client could make the server buffer a payload that never completes.

Bound it by 10000 bytes, the limit the startup message already applies to the same phase, and reuse
one constant for both. Cover the three messages in the stateless PostgreSQL protocol test.
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f578568]

Summary: ✅


AI Review

Summary

PR #130462, reviewed at f57856835544adc5a3ab206caa2dadb45764dd8f, caps the declared length of PostgreSQL PasswordMessage, SASLInitialResponse, and SASLResponse frames at 10000 bytes before reading their payloads. It reuses the startup-message limit and adds focused stateless regression coverage. No actionable findings.

Missing context / blind spots
  • ⚠️ Local validation with the PR's Fast test binary confirmed immediate rejection of password frames declaring 2147483647 and 10001 bytes. The full stateless test could not complete because that binary lacks SSL support and cannot create the SCRAM user. Run 05054_postgresql_protocol_message_size with an SSL-enabled build to complete live SCRAM validation; the targeted CI job was still pending during review.
Final Verdict

Status: ✅ Approve. No code changes requested; full regression execution remains subject to the normal CI checks.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... labels Nov 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f57856835 with master a380d00da (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse-keeper ×1.17 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 7.4 s 18.3 s +10.8 s (+145%)
Compile time of recompiled translation units

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

Job report

@tiandiwonder tiandiwonder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Currently we use 10000 for the following scenarios, which are different limits in postgresql, but it's good to go.

MAX_STARTUP_PACKET_LENGTH = 10000; // for startup message
PG_MAX_AUTH_TOKEN_LENGTH = 65535; // for password length
PG_MAX_SASL_MESSAGE_LENGTH = 1024; // for SASL message

@tiandiwonder
tiandiwonder added this pull request to the merge queue Nov 12, 2026
Merged via the queue into master with commit fb5720f Nov 12, 2026
192 checks passed
@tiandiwonder
tiandiwonder deleted the fix/postgresql-auth-message-size-limit branch November 12, 2026 02:26
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 2026
@antonio2368 antonio2368 added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 12, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 12, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 12, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #130462 to 26.8: Bound the declared length of PostgreSQL messages parsed before authentication
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #130462 to 26.10: Bound the declared length of PostgreSQL messages parsed before authentication
antonio2368 added a commit that referenced this pull request Nov 12, 2026
Backport #130462 to 26.9: Bound the declared length of PostgreSQL messages parsed before authentication
antonio2368 added a commit that referenced this pull request Nov 12, 2026
Backport #130462 to 26.3: Bound the declared length of PostgreSQL messages parsed before authentication
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…greSQL messages parsed before authentication
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…greSQL messages parsed before authentication
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…tgreSQL messages parsed before authentication
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... 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