Skip to content

Fix a segfault of showCertificate when TLS certificates are provisioned by ACME - #115976

Merged
thevar1able merged 1 commit into
masterfrom
fix-showcertificate-null-certificate
Aug 23, 2026
Merged

thevar1able merged 1 commit into
masterfrom
fix-showcertificate-null-certificate

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

showCertificate took the server certificate from the default SSL_CTX. When certificates are provisioned dynamically with the <acme> configuration, that context never receives a certificate: SSL_CTX_get0_certificate returns a null pointer, X509_dup of it returns a null pointer as well, and the result was wrapped into an X509Certificate whose every accessor dereferences it. Any user - including a read-only one - could crash the server with SELECT showCertificate():

<Fatal> BaseDaemon: Address: NULL pointer. Access: <not available>. Address not mapped to object.
<Fatal> BaseDaemon: 2. DB::(anonymous namespace)::FunctionShowCertificate::executeImpl(...) const @ 0x000000000ee52f05
<Fatal> BaseDaemon: 3. DB::IExecutableFunction::executeWithoutLowCardinalityColumns(...) const @ 0x0000000016b11df8
...
<Fatal> BaseDaemon: 5. DB::QueryAnalyzer::resolveFunction(...) @ 0x0000000018196eec

It is reproducible without an ACME server, because it is enough that the certificate is not there:

$ cat acme.xml
<clickhouse>
    <acme>
        <email>[email protected]</email>
        <terms_of_service_agreed>true</terms_of_service_agreed>
        <domains><domain>example.com</domain></domains>
        <directory_url>https://127.0.0.1:14000/dir</directory_url>
    </acme>
</clickhouse>

$ clickhouse local --config-file=acme.xml --query "SELECT showCertificate()"
Segmentation fault

The null check existed in the original implementation and was lost when Poco::Crypto was replaced with our own X509Certificate wrapper; it became reachable when ACME support was added.

The certificate is now taken from CertificateReloader - the component that owns the certificate actually served to clients. Besides ACME, this also makes the function report the certificate in use after a reload, instead of the one loaded into the SSL context at startup. When there is no certificate at all, an empty map is returned, as it was before ACME.

Additionally, X509Certificate no longer accepts a null pointer silently: constructing it from nullptr throws instead of producing an object that segfaults on the first use.

Related: #78591
Related: #66315

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix a crash of the showCertificate function on a server that obtains its TLS certificate with ACME (Let's Encrypt). The function now reports the certificate that is actually served, also after a certificate reload.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

Version info

  • Merged into: 26.8.1.1943 (included in 26.8 and later)
  • Backported to: 26.7.6.45, 26.6.4.33, 26.3.24.3

…oned by ACME

`showCertificate` took the certificate from the default server `SSL_CTX`. When
certificates are provisioned dynamically with the `<acme>` configuration, that
context never receives a certificate: `SSL_CTX_get0_certificate` returns a null
pointer, `X509_dup` of it returns a null pointer as well, and the result was
wrapped into an `X509Certificate` whose every accessor dereferences it. As a
result, any user - including a read-only one - could crash the server with
`SELECT showCertificate()`.

The null check existed in the original implementation and was lost when
`Poco::Crypto` was replaced with our own `X509Certificate` wrapper; it became
reachable when ACME support was added.

Take the certificate from `CertificateReloader` instead - it is the component
that owns the certificate actually served to clients. Besides ACME, this also
makes the function report the certificate in use after a reload instead of the
one loaded into the context at startup. When there is no certificate at all,
an empty map is returned, as before.

Additionally, `X509Certificate` no longer accepts a null pointer silently:
constructing it from `nullptr` now throws instead of producing an object that
segfaults on the first use.
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cb0db7b]

Summary: ✅


AI Review

Summary

This PR fixes the null-dereference in showCertificate() for ACME-managed certificates and hardens X509Certificate against null inputs. The native TCP path looks better, but the new "certificate actually served" contract is still violated on PostgreSQL sessions that use their own TLS context, so I would not merge it as-is.

Findings

⚠️ Majors

  • [src/Functions/FunctionShowCertificate.cpp:42] getServerCertificate() hardcodes Poco::Net::SSLManager::CFG_SERVER_PREFIX, but PostgreSQLHandler::makeSecureConnectionSSL() can serve a different certificate from a per-protocol prefix while PostgreSQLHandler::run() creates the Session without passing that certificate path/prefix. In that configuration, SELECT showCertificate() over the PostgreSQL wire still returns the native TCP certificate or {} instead of the certificate the client is actually using.
    Suggested fix: thread the PostgreSQL handler's certificate path or config prefix into ClientInfo the same way TCPHandler does, and make showCertificate() pick the certificate for the active interface/prefix.
Final Verdict

Changes requested.

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: 27/36 (75.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Aug 23, 2026
/// `CertificateReloader` knows the certificate in use.
std::optional<X509Certificate> getServerCertificate()
{
auto served_certificate = CertificateReloader::instance().getCertificate(Poco::Net::SSLManager::CFG_SERVER_PREFIX);

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.

showCertificate() still does not follow the new "certificate actually served" contract on the PostgreSQL wire path. This helper always reads openSSL.server via CFG_SERVER_PREFIX, but PostgreSQLHandler::makeSecureConnectionSSL() can serve a different per-protocol context from prefix, and PostgreSQLHandler::run() never threads that certificate path/prefix into Session the way TCPHandler does. In a deployment with a dedicated PostgreSQL TLS certificate, SELECT showCertificate() over PostgreSQL will still report the native TCP certificate or {}.

Please propagate the PostgreSQL TLS certificate (or at least its config prefix) into ClientInfo and select it here, otherwise the new comment/docs/PR description should be scoped to the native TCP interface only.

@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cb0db7b45 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.02 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

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

Job report

@thevar1able
thevar1able added this pull request to the merge queue Aug 23, 2026
@thevar1able thevar1able self-assigned this Aug 23, 2026
Merged via the queue into master with commit 9df9510 Aug 23, 2026
184 checks passed
@thevar1able
thevar1able deleted the fix-showcertificate-null-certificate branch August 23, 2026 15:35
@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-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 23, 2026
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Merge `backport/26.6/115976` into the cherry-pick branch of #115976.

The only conflict is in the documentation string of `showCertificate` in
`src/Functions/FunctionShowCertificate.cpp`: 26.6 links the TLS guide as
`/guides/sre/tls/configuring-tls` while master uses
`/concepts/features/security/tls/configuring-tls`. Kept the branch's link and added the new
sentence about an empty map being returned.

The rest applies as-is: 26.6 already has `registerAdditionalContext`, `data_index`,
`data_mutex` and the `<acme>` support that `CertificateReloader::getCertificate` relies on.
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115976 to 26.3: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115976 to 26.6: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115976 to 26.5: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
Cherry pick #115976 to 26.7: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
robot-clickhouse added a commit that referenced this pull request Aug 26, 2026
@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 #115976 to 26.5: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
alexey-milovidov added a commit that referenced this pull request Aug 26, 2026
Backport #115976 to 26.3: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
clickhouse-gh Bot added a commit that referenced this pull request Aug 28, 2026
Backport #115976 to 26.6: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
clickhouse-gh Bot added a commit that referenced this pull request Aug 29, 2026
Backport #115976 to 26.7: Fix a segfault of showCertificate when TLS certificates are provisioned by ACME
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-critical-bugfix 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