Skip to content

Stop the native SSH-key handshake from disclosing whether a user exists - #121704

Merged
tiandiwonder merged 11 commits into
ClickHouse:masterfrom
tiandiwonder:fix-ssh-marker-user-enumeration-oracle
Oct 16, 2026
Merged

tiandiwonder merged 11 commits into
ClickHouse:masterfrom
tiandiwonder:fix-ssh-marker-user-enumeration-oracle

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A native-protocol client asks for SSH-key authentication by prefixing the user name in its Hello
with EncodedUserInfo::SSH_KEY_AUTHENTICAION_MARKER (" SSH KEY AUTHENTICATION "). Holding no
credential of any kind, such a client could tell three states apart on the first round trip:

  • the user does not exist — There is no user '<name>' in 'user directories';
  • the user exists but authenticates some other way — Expected authentication with SSH key;
  • the user exists and has an SSH key — the server sends Protocol::Server::SSHChallenge and waits.

So the handshake answered "does this user exist, and how does it authenticate?" for anyone who
could reach the native port. The probes also never reached AccessControl::authenticate, so they
did not count against that user's FAILED_SEQUENTIAL_AUTHENTICATIONS quota — a quota configured
to slow credential stuffing did not apply to them.

The password path has never behaved this way: it answers Authentication failed: password is incorrect, or there is no user with such name for a wrong password and for an unknown user alike.

Root cause

TCPHandler::receiveHello resolved the user before any credential was exchanged:

const auto authentication_types = session->getAuthenticationTypesOrLogInFailure(user);
...
if (!user_supports_ssh_authentication)
    throw Exception(ErrorCodes::AUTHENTICATION_FAILED, "Expected authentication with SSH key");

The lookup's own exception text reached the wire for a name that does not exist, and the explicit
rejection above covered the name that exists but is configured for something else. Only the third
case got as far as the challenge.

Fix

The challenge is now issued unconditionally, and every outcome is left to the credential check
that already followed. checkSshAuthentication returns false when the user's authentication
method is not SSH_KEY, and an unknown user throws inside the storage lookup, so both reach
AccessControl::authenticate's single message — and both now consume the failed-authentication
quota.

The client-version check moved to the top of the branch. It depends only on the client's own
version, so it discloses nothing about the user, and it must stay reachable before the server
starts expecting challenge packets.

The challenge is a server-side random value and carries nothing about the user. The cost is one
extra round trip for a handshake that cannot succeed, and an unauthenticated connection held open
while the server waits for a signature — neither is new: a client can already hold a connection
open with a partial hello, and an existing SSH user already produced that state.

One behaviour change worth noting for review: a client that asks for SSH-key authentication as a
user configured with a password used to be told Expected authentication with SSH key; it now
completes the challenge and receives the ordinary authentication failure. The precise reason is
still logged server-side at information level.

Regression coverage: test_ssh_handshake_does_not_disclose_user_existence in
tests/integration/test_ssh_keys_authentication/ speaks the native protocol on a raw socket — the
only way to request the marker for an arbitrary user — and asserts that an SSH user, a password
user and an unknown user are all challenged and then all fail with the same error code and the
same text. On the unfixed binary it reports both leaking messages at once. The module's existing
tests (test_ecdsa, test_ed25519, test_rsa, test_wrong_key, test_key_with_passphrase,
test_key_with_wrong_passphrase) are unchanged and still pass.

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

Requesting SSH-key authentication over the native protocol no longer reveals whether the named user exists, or how it authenticates, before its signature is checked.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Bug fix only, no user-facing documentation changes.


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

Version info

  • Merged into: 26.11.1.69-master (included in 26.11 and later)
  • Backported to: 26.10.2.12, 26.9.20.26, 26.8.29.11, 26.7.33.3, 26.3.56.2

A native-protocol client asks for SSH-key authentication by prefixing the user
name in its Hello with `EncodedUserInfo::SSH_KEY_AUTHENTICAION_MARKER`.
`TCPHandler::receiveHello` then resolved that user before any credential was
exchanged, so the first round trip separated three states for a client holding
nothing at all: the user does not exist (`There is no user ... in user
directories`), the user exists but authenticates some other way (`Expected
authentication with SSH key`), and the user exists with an SSH key (a
challenge). That is a user-existence and authentication-method oracle, and
because the attempts never reached `AccessControl::authenticate` they also did
not count against the user's `FAILED_SEQUENTIAL_AUTHENTICATIONS` quota.

The password path has never behaved this way: it answers `Authentication
failed: password is incorrect, or there is no user with such name` for a wrong
password and for an unknown user alike.

Issue the challenge unconditionally instead, and leave every outcome to the
credential check that already followed. `checkSshAuthentication` returns false
when the user's method is not `SSH_KEY`, and an unknown user throws inside the
storage lookup, so both arrive at that one message - and both now consume the
failed-authentication quota. The client-version check moves to the top of the
branch: it depends only on the client's own version, so it discloses nothing,
and it has to stay reachable before challenge packets are read.

The challenge is a server-side random value and says nothing about the user.
The cost is one extra round trip for a handshake that cannot succeed, and an
unauthenticated connection held open while the server waits for a signature -
neither is new, since a client can already hold a connection with a partial
hello and an existing SSH user already reached this state.

One behaviour change: a client that asks for SSH-key authentication as a user
configured with a password used to be told `Expected authentication with SSH
key`, and now completes the challenge and gets the ordinary authentication
failure. The precise reason is still logged server-side.

`test_ssh_handshake_does_not_disclose_user_existence` speaks the protocol on a
raw socket - the only way to request the marker for an arbitrary user - and
asserts that an SSH user, a password user and an unknown user are all
challenged and then all fail with the same code and the same text.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Sep 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bd3f38c]

Summary: ✅


AI Review

Summary

The PR removes the user lookup before the native SSH-key challenge, preventing the initial handshake from revealing user existence or authentication type. It adds a focused protocol regression test and updates the existing RSA test fixture for FIPS compatibility. The scoped fix is sound; no new actionable findings.

Missing context / blind spots
  • ⚠️ CI for head bd3f38cc is still running, including validation of the updated RSA fixture. The unchanged server fix and enumeration regression already passed repeated ARM ASan/UBSan runs and bugfix validation at 420aa220. Completion of the current functional checks would close this gap.
Final Verdict

Status: ✅ Approve. No code changes requested; current CI must complete successfully.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 23, 2026
Comment thread src/Server/TCPHandler.cpp Outdated

/// Challenge the client without looking the user up first: whether a user exists and how
/// it authenticates must not be observable before a signature is checked. Reporting the
/// failure is left to the credential check below, which answers an unknown user and a

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.

FAILED_SEQUENTIAL_AUTHENTICATIONS still leaves this path distinguishable after a few probes. session->authenticate calls AccessControl::authenticate, which only creates an authentication quota for existing users (getAuthenticationQuota returns nullptr for an unknown name) and then calls authentication_quota->used(...) before entering the generic AUTHENTICATION_FAILED rewrite. EnabledQuota::used throws QUOTA_EXCEEDED directly, so a real user with an authentication quota still returns a different public error than an unknown user once the limit trips.

That means the oracle is reduced from one attempt to N attempts, not removed. Please make quota exhaustion on an unauthenticated SSH-marker handshake collapse to the same public auth failure as the other bad-signature paths, and add a regression with a tiny FAILED_SEQUENTIAL_AUTHENTICATIONS quota.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is not specific to the SSH marker: AccessControl::authenticate consumes the failed-authentication quota outside its unifying catch, so a plain password attempt against the same user distinguishes it in exactly the same way, and collapsing QUOTA_EXCEEDED into the generic failure would change the public error on every authentication path — it is tracked separately rather than widened into this fix.

The claim here is scoped to the handshake: nothing distinguishes the three states before a signature is checked, and SSH-marker probes now consume that quota instead of bypassing it entirely, which is strictly better than the current behaviour.

@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Sep 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bd3f38ccc with master 2d7962a59 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 5d779130d; 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.

Compile time of recompiled translation units

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

Job report

tiandiwonder and others added 2 commits September 24, 2026 12:29
The module header repeated what the test's own docstring says, the docstring
narrated what the handshake used to answer before this change - which belongs
in the pull request, not in a file that outlives it - and several comments
restated the line below them.

What stays is what a reader cannot recover from the code: the invariant that
keeps someone from reintroducing the lookup, why the revision constant is that
number, why `default` is not among the probed names, and the field labels on an
otherwise opaque byte sequence.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The regression test is what keeps the lookup from coming back, and the comment
said nothing the invariant it guards does not already say through that test.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@rschu1ze rschu1ze 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.

Before I review this PR, kindly de-AI the test. It may do the right thing but it lacks a clear control flow, it needs better variable names, and it should have clear comments that readers are able to understand intuitively.

Name the wire helpers as encode/decode pairs, return a named
`HandshakeResult` instead of a tuple whose first element had to be
compared against a packet id, and record where every protocol constant
comes from and which leak each assertion rules out.

`read_varstring` is split into `read_binary_field` and `read_string`:
the challenge is a nonce that must not be decoded, while the
exception's name, message and stack trace must be.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

This PR removes a per-user SSH-key check from the server's login handshake and adds a test. Neither change runs during query execution, so none of the flagged deltas can be attributed to the diff. The six ClickBench slowdowns (Q16 +17.1%, Q17 +13.2%, Q31 +12.9%, Q32 +12.1%, Q33 +8.9%, Q34 +7.1%) are all high-cardinality GROUP BY queries with higher CPU time, and five fall inside master's current range. A shared environment cause fits better, since two of the PR's ClickBench runs shared one host. All rows stay inconclusive, TPC-H showed no flagged changes, and this PR needs no performance follow-up.

clickbench

⚠️ 6 inconclusive

Flagged queries (6 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 531 622 +17.1% <0.0001 aggregation: The +17.1% slowdown on this high-cardinality GROUP BY is within master's current range. The PR only changes the SSH-key login handshake, which this query never runs.
⚠️ 17 not_sure 235 266 +13.2% <0.0001 aggregation: The +13.2% slowdown is within master's current range. The PR only changes the SSH-key login handshake, so nothing here ties it to this aggregation query.
⚠️ 31 not_sure 264 298 +12.9% <0.0001 aggregation: The +12.9% slowdown is within master's current range and the measurements were noisy. The changed login handshake code is not on this query's path.
⚠️ 32 not_sure 711 797 +12.1% <0.0001 aggregation: The +12.1% delta is borderline: the two tests disagree and both sides are very noisy. The PR's SSH-key login change does not touch query execution.
⚠️ 33 not_sure 1142 1244 +8.9% <0.0001 aggregation: The +8.9% slowdown on this GROUP BY URL query is within master's current range. The PR's handshake-only change cannot affect it.
⚠️ 34 not_sure 1138 1219 +7.1% <0.0001 aggregation: The +7.1% slowdown is within master's current range. It matches the other GROUP BY URL query, Q33, and the PR does not touch aggregation code.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 8711bb00-32bf-4fca-972d-f3926ee47549
  • MIRAI run: 4025393e-91e8-45ce-a0a8-b2c0d766cd0a
  • PR check IDs:
    • clickbench_3311229_1791184378
    • clickbench_3311235_1791184378
    • clickbench_3311245_1791184378
    • tpch_adapted_1_official_3311252_1791184378
    • tpch_adapted_1_official_3311266_1791184378
    • tpch_adapted_1_official_3311300_1791184378

tiandiwonder and others added 4 commits September 29, 2026 12:50
The probe needs nothing but a TCP connection and two users, so it belongs in
`tests/queries/0_stateless` next to the other native-protocol probes, where it
runs across every functional-test configuration rather than only the
integration matrix. Its two users are created with `CREATE USER`, so the
`users.xml` change the integration test needed goes away with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The reviewer asked for a clear control flow, better names, and comments a
reader can follow. The probe returns a plain tuple instead of a dataclass, the
three user states are now checked in one loop as each handshake completes
rather than in three passes over a dictionary, the one-line codec helpers lost
their docstrings, and `CREATE USER` moved inside the block the cleanup covers.

Tagged `no-openssl-fips`, as every other stateless test that touches an
`ssh-ed25519` key is.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A test comment that names a C++ header or symbol goes stale the moment the
code moves, and it makes the reader chase a file to understand a local
constraint. The docstring now opens with what the test checks rather than the
bug it came from, says it in fewer words, and no longer carries the pull
request link.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@tiandiwonder

Copy link
Copy Markdown
Contributor Author

Before I review this PR, kindly de-AI the test. It may do the right thing but it lacks a clear control flow, it needs better variable names, and it should have clear comments that readers are able to understand intuitively.

Fixed.

…ts do

The test read `CLICKHOUSE_HOST` and `CLICKHOUSE_PORT_TCP` with `os.environ[...]`, and
`clickhouse-test` does not set either: they come from the environment the suite was started
in, and `shell_config.sh`, which supplies them, is only sourced by the `.sh` tests. So the
test died on `KeyError: 'CLICKHOUSE_HOST'` before opening a single connection, on every
sanitizer run.

Read them with a default instead, as every other `.py` test in this directory does
(`04078_native_protocol_input_validation`, `04092_tcp_settings_parse_error_closes_connection`,
`05100_mysql_handshake_underflow`). `CLICKHOUSE_BINARY` and `CLICKHOUSE_DATABASE` are set by
`clickhouse-test`, but are read the same way so the test can also be run by hand.
`clickhouse-test` runs a `.py` test by executing it through its shebang, so the file needs the
executable bit, as every other `.py` test in `0_stateless` has. The previous commit dropped it,
and the test failed with `return code: 126` / `Permission denied` before it ever opened a
connection - which also means the environment fix in that commit has not actually been exercised
by CI yet.
@tiandiwonder tiandiwonder added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 15, 2026
This test creates a user without an `ssh_key` authentication method and connects with
`--ssh-key-file`, expecting `AUTHENTICATION_FAILED`. Until now the server rejected it before sending
a challenge, so the client never signed anything and the key in the test was never used. Removing that
early rejection is the point of this change, so the client now signs the challenge.

The hardcoded key is a 1024-bit RSA key, which OpenSSL rejects in FIPS mode as it is below the
approved minimum modulus. `libssh`'s `sshsig_sign` then takes its `sig == NULL` branch, which reaches
`cleanup` without setting `rc`, so it returns `SSH_OK` while leaving the out parameter at `nullptr`;
`SSHKey::signString` trusts the return code and constructs a `String` from it. The client segfaults
before it can receive the error, and the test fails on the `amd_fips_release` build.

Replace it with a freshly generated 4096-bit key. The test was written with
`ssh-keygen -t rsa -b 4096`, which `819a654f5e5` replaced with a hardcoded key to avoid generating one
under the five-second `expect` timeout; the drop to 1024 bits was incidental to that. Hardcoding keeps
that benefit, and 4096 bits restores the original intent with room above the FIPS minimum. The test now
covers the full challenge-sign-verify path on FIPS builds as well, rather than being excluded from them.

The missing out-parameter check in `SSHKey::signString` is a separate pre-existing defect, reachable
today on a FIPS build by any user authenticating with a key that FIPS rejects, and is fixed separately.
@tiandiwonder
tiandiwonder added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ClickHouse:master with commit 2908b8f Oct 16, 2026
351 of 353 checks passed
@tiandiwonder
tiandiwonder deleted the fix-ssh-marker-user-enumeration-oracle branch October 16, 2026 11:44
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 16, 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 Oct 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 16, 2026
Backport #121704 to 26.8: Stop the native SSH-key handshake from disclosing whether a user exists
clickhouse-gh Bot added a commit that referenced this pull request Oct 16, 2026
Backport #121704 to 26.9: Stop the native SSH-key handshake from disclosing whether a user exists
clickhouse-gh Bot added a commit that referenced this pull request Oct 16, 2026
Backport #121704 to 26.7: Stop the native SSH-key handshake from disclosing whether a user exists
tiandiwonder added a commit that referenced this pull request Oct 18, 2026
Backport #121704 to 26.3: Stop the native SSH-key handshake from disclosing whether a user exists
tiandiwonder added a commit that referenced this pull request Oct 18, 2026
Backport #121704 to 26.10: Stop the native SSH-key handshake from disclosing whether a user exists
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 21, 2026
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 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.

4 participants