Skip to content

Do not resolve the peer host name on every TLS handshake - #132627

Merged
thevar1able merged 1 commit into
ClickHouse:masterfrom
groeneai:tls-verify-no-dns-lookup-per-handshake
Nov 18, 2026
Merged

thevar1able merged 1 commit into
ClickHouse:masterfrom
groeneai:tls-verify-no-dns-lookup-per-handshake

Conversation

@groeneai

@groeneai groeneai commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116724
Related: #126138

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Outbound TLS connections no longer resolve the server host name during the handshake when the server certificate names that host. Since hostname verification of server certificates became the default, every new HTTPS connection (for example to a web or s3 disk) did this extra lookup, which slowed down cold queries.

Description

@ nihalzp reported that since #116724 (relanded by #126138) six cold queries of ClickBench ClickHouse (web) take 5.2-5.6 s instead of 0.6-1.4 s (results).

Root cause: with openSSL.client.extendedVerification on by default, SecureSocketImpl::verifyPeerCertificateImpl evaluated the loopback exemption isLocalHost(hostName) before X509_check_host. isLocalHost builds SocketAddress(hostName, 0), which calls an uncached getaddrinfo for a host name. The connection itself uses the cached DNSResolver, so every new TLS connection did one extra lookup.

Change: check the exemption only when the certificate does not name the host. The result is the same for every certificate, and strict still never exempts. A lookup remains only on the loopback exemption path, or before a connection is rejected.

Validation (master vs this PR): a url() read of N files from a local HTTPS endpoint whose certificate names localhost did 17 and 65 lookups for 16 and 64 handshakes on master, and 1 for both with this PR. Accept/reject results and error texts are identical for IP-literal and name loopback mismatches, strict, a non-loopback mismatch and extendedVerification = false. ClickBench hits attached from the same web endpoint, cold Q37 (65 handshakes): 66 DNS queries on master, 1 with this PR. This box uses a local caching resolver, so I did not reproduce the 5 s and see no wall-clock difference here.

No new test: the verification result does not change and the lookup is not observable from SQL. 00505_shard_secure, 00505_secure and 03595_parallel_replicas_join_remote cover the exemption path and pass.

The lookup is also in 26.3, 26.7, 26.8 and 26.9 (backports of #116724).

🤖 Generated with Claude Code


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

Version info

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

Since `openSSL.client.extendedVerification` is on by default (ClickHouse#116724,
relanded by ClickHouse#126138), `SecureSocketImpl::verifyPeerCertificateImpl`
evaluated the loopback exemption `isLocalHost(hostName)` before
`X509_check_host`. `isLocalHost` builds `SocketAddress(hostName, 0)`,
which for a host name calls an uncached `getaddrinfo`, so every outbound
TLS handshake did one DNS lookup that the TCP connect (served by the
cached `DNSResolver`) never needed. A cold query over a `web` or `s3`
disk opens many new HTTPS connections, so the lookups add up.

The exemption only matters when the certificate does not name the host;
check it only then. The result is the same for every certificate: the
same default, the same loopback exemption, and `strict` mode still never
exempts.

Reported in ClickHouse#116724 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own review of the diff (no code findings; two wording fixes in the PR description).

# Sev Finding Verdict Evidence / action
1 💡 The description said the lookup remains only for a loopback name; it also runs for a non-loopback name whose certificate does not name it, right before the rejection (SecureSocketImpl.cpp:679) AGREE description corrected before publication

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261118-110104

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local reading url('https://localhost:<port>/f{1..N}.csv') from a local endpoint whose certificate names localhost, under strace -e trace=openat: master opens /etc/hosts (one getaddrinfo each) 17 and 65 times for 16 and 64 handshakes, every run.
b Root cause explained? With extendedVerification on by default, verifyPeerCertificateImpl evaluated the loopback exemption isLocalHost(hostName) before X509_check_host; isLocalHost builds SocketAddress(hostName, 0), an uncached getaddrinfo, on every handshake (strace -k stack: getaddrinfo <- DNS::hostByName <- SocketAddress::init <- isLocalHost <- verifyPeerCertificateImpl).
c Fix matches root cause? Yes. The exemption is checked only when the certificate does not name the host, where it can change the result. Verification, its default and the exemption are unchanged; no cache, no guard.
d Test intent preserved / new tests added? No test changed. No new test: the verification result is identical for every input and the lookup is not observable from SQL. 00505_shard_secure, 00505_secure, 03595_parallel_replicas_join_remote (exemption path over TLS) pass on the fix build.
e Both directions demonstrated? Yes. Lookups per handshake: master slope 1 (17 @ 16, 65 @ 64), this PR slope 0 (1 @ 16, 1 @ 64). Real web endpoint, ClickBench Q37 cold, 65 handshakes: 66 DNS queries on master, 1 with the fix.
f Fix is general across code paths? isLocalHost has one caller and holds the only name lookup in NetSSL_OpenSSL/src; every Poco TLS client (HTTP pool, native protocol, Keeper, remoteSecure) goes through this function. The proxy tunnel path is unchanged (no use of the peer address added).
g Fix generalizes across inputs (params/datatypes/wrappers)? Same outcome and error text on master and the fix for: certificate names the host (relaxed, strict), IP-literal loopback mismatch, loopback name mismatch, strict mismatch, non-loopback mismatch, extendedVerification = false; no-certificate and malformed-name paths are unchanged by construction.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change, and no change in which certificates are accepted.
i Invariants and contracts preserved? The result for each (mode, extended verification, host name, certificate) is unchanged; X509_free still runs for every fetched certificate; the function still runs under the socket mutex; isLocalHost stays static and lock-free.

Session id: cron:clickhouse-impl-slot-40:20261118-101200

@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f85c977]

Summary: ✅


AI Review

Summary

PR #132627, reviewed at f85c97716aa9aabe58fd8511371d326cd84d8dec, defers isLocalHost until certificate-name verification fails, avoiding redundant DNS resolution for matching TLS certificates. No actionable findings. The reported before/after measurements—65 lookups reduced to 1 for 64 handshakes—support the performance claim.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. labels Nov 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f85c97716 with master 75fcef2b7 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 2f06a797f; 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, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@nihalzp
nihalzp requested a review from thevar1able November 18, 2026 14:45
@thevar1able thevar1able self-assigned this Nov 18, 2026
@thevar1able
thevar1able added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit 81cc0d9 Nov 18, 2026
571 of 573 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 18, 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-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. pr-performance Pull request with some performance improvements 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.

3 participants