Skip to content

Fix ssl_certificate authentication bypass via embedded NUL byte in certificate CN - #117403

Merged
thevar1able merged 5 commits into
masterfrom
fix-tls-cert-cn-nul-byte-bypass
Oct 2, 2026
Merged

thevar1able merged 5 commits into
masterfrom
fix-tls-cert-cn-nul-byte-bypass

Conversation

@thevar1able

@thevar1able thevar1able commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Fixes an authentication bypass in ssl_certificate user identification.

X509Certificate::subjectName and issuerName extracted the name entry with X509_NAME_get_text_by_NID, which copies the value into a fixed buffer and NUL-terminates it. Building a std::string from that C buffer stops at the first embedded NUL byte, so a certificate whose Common Name is john\0.evil.com was seen by the server as john. A holder of a CA-signed certificate for *.evil.com could therefore embed a NUL byte in the CN and authenticate as the ssl_certificate user john.

The value is now read directly from the ASN1_STRING (X509_NAME_get_index_by_NID + X509_NAME_ENTRY_get_data + ASN1_STRING_length) and built from its exact byte length, so the full CN is preserved and compares unequal to any NUL-free configured subject. This also removes a pre-existing silent truncation of names longer than the 256-byte buffer. The approach matches how SAN entries are already extracted in extractAllSubjects, so both the native and HTTPS authentication paths are covered.

Added integration tests over both the native and HTTPS interfaces using a CA-signed certificate (client13) whose CN carries an embedded NUL byte; the certificate generation is added to generate_certs.sh. This is the sibling of the wildcard matching fix in #108472.

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

Fixed an ssl_certificate authentication bypass where an embedded NUL byte in a certificate's Common Name (for example john\0.evil.com) was truncated server-side to john, letting a holder of such a certificate authenticate as a different user.


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

Version info

  • Merged into: 26.10.1.1231-master (included in 26.10 and later)
  • Backported to: 26.9.9.14, 26.8.16.20, 26.7.20.11, 26.3.39.7

X509Certificate::subjectName and issuerName read the name entry with
X509_NAME_get_text_by_NID, which copies the value into a fixed buffer and
NUL-terminates it. Constructing a std::string from that buffer stops at the
first embedded NUL byte, so a certificate with a common name such as
"john\0.evil.com" was seen as "john" during ssl_certificate authentication
and could impersonate the user john.

Read the ASN1_STRING bytes directly (X509_NAME_get_index_by_NID plus
X509_NAME_ENTRY_get_data) and build the string from its exact length. The
full value is preserved, so it compares unequal to any NUL-free configured
subject, and long names are no longer silently truncated. This mirrors how
the SAN entries are already extracted in extractAllSubjects.

Add integration tests over both the native and HTTPS interfaces using a
CA-signed certificate whose CN carries an embedded NUL byte.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5dafb6f]

Summary: ✅


AI Review

Summary

The PR fixes the ssl_certificate embedded-NUL bypass by preserving the exact CN bytes instead of building a std::string from a NUL-terminated buffer, and it also closes the wildcard follow-up by refusing wildcard matches against certificate subjects that contain \0. The current diff now includes discriminating regression coverage for both native and HTTPS authentication, plus wildcard CN / DNS: SAN cases, so I do not see any remaining correctness or security issues in the reviewed surface.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 5dafb6f.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 17/24 (70.83%) · 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 31, 2026
@evillique evillique self-assigned this Aug 31, 2026
@zlareb1

This comment was marked as low quality.

@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5dafb6f42 with master ab0345d9f (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.

Compile time of recompiled translation units

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

Job report

…lity

The previous tests used a certificate with CN "john\0.evil.com" presented
as user 'john', but 'john' is configured with <common_name>client1</common_name>.
Neither a truncating (vulnerable) nor a fixed server extracts "client1" from
that certificate, so both reject and the tests passed on either binary without
exercising the truncation-to-match path.

Give client13 the CN "client1\0.evil.com" instead. A server that truncates at
the embedded NUL extracts "client1", which matches john's configured common
name and wrongly authenticates the certificate; the fixed server preserves the
full CN and correctly rejects. The tests now fail on the vulnerable binary.

Reported by @zlareb1 in review of the pull request.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-authentication Identity verification: users, credentials, auth methods, external auth providers. label Sep 4, 2026
@rschu1ze

Copy link
Copy Markdown
Member

@thevar1able Let's merge?

Comment thread src/Common/Crypto/X509Certificate.cpp Resolved
thevar1able and others added 2 commits October 1, 2026 13:27
…ching

Now that CN extraction preserves exact bytes (and `DNS:` SAN extraction already did),
a subject like `evil\0.corp.example.com` reached `checkSSLCertificateAuthentication`
intact, and a wildcard `*.corp.example.com` accepted `evil\0` as a single label.
No valid hostname or URI contains a NUL byte, so skip such subjects in the wildcard
branch. Add a regression test with a CA-signed certificate carrying the NUL byte in
both the CN and the `DNS:` SAN.

Address review: #117403

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

clickhouse-gh Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Dear @evillique, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@evillique evillique self-assigned this Oct 1, 2026
@thevar1able
thevar1able added this pull request to the merge queue Oct 1, 2026
Merged via the queue into master with commit bee127a Oct 2, 2026
150 checks passed
@thevar1able
thevar1able deleted the fix-tls-cert-cn-nul-byte-bypass branch October 2, 2026 00:10
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 2, 2026
@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 Oct 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #117403 to 26.9: Fix ssl_certificate authentication bypass via embedded NUL byte in certificate CN
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #117403 to 26.8: Fix ssl_certificate authentication bypass via embedded NUL byte in certificate CN
clickhouse-gh Bot added a commit that referenced this pull request Oct 2, 2026
Backport #117403 to 26.7: Fix ssl_certificate authentication bypass via embedded NUL byte in certificate CN
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…n bypass via embedded NUL byte in certificate CN
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…n bypass via embedded NUL byte in certificate CN
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…n bypass via embedded NUL byte in certificate CN
PedroTadim added a commit that referenced this pull request Oct 2, 2026
Backport #117403 to 26.3: Fix ssl_certificate authentication bypass via embedded NUL byte in certificate CN
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
…n bypass via embedded NUL byte in certificate CN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-authentication Identity verification: users, credentials, auth methods, external auth providers. 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.

7 participants