Skip to content

Reject paths with an embedded NUL byte in filesystem containment checks - #119846

Merged
alexey-milovidov merged 5 commits into
masterfrom
sec-nul-path-containment
Sep 18, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
sec-nul-path-containment

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

A path with an embedded NUL byte passed the filesystem containment checks. pathStartsWith and fileOrSymlinkPathStartsWith compare the whole value of the path, while every syscall the path is later passed to (open, mkdir, stat) stops at the first NUL byte, so a path shaped as <target>\0/<traversal back into the base directory> was reported as contained in the base directory while the kernel addressed <target>.

Both helpers now report such a path as not contained, so that every containment check built on them fails closed - among them the user_files fence of the local data lake table engines and table functions. resolvePathRelativelyToBase rejects it explicitly, because it returns a path that is passed to syscalls directly.

The file table function and the File engine stat the path and expand its globs on the filesystem before the matched paths are checked for containment, so StorageFile rejects a path with an embedded NUL byte up front, before the first filesystem probe.

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 filesystem containment checks accepting a path with an embedded NUL byte. A DeltaLakeLocal or IcebergLocal table could be pointed outside of user_files this way.

🤖 Generated with Claude Code


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

Version info

  • Backported to: 26.9.1.1604, 26.8.8.7, 26.7.12.7, 26.3.33.111

`pathStartsWith` and `fileOrSymlinkPathStartsWith` compare the whole value of the
path, while every syscall that the path is later passed to (`open`, `mkdir`,
`stat`) stops at the first NUL byte. A path shaped as
`<target>\0/<traversal back into the base directory>` was therefore reported as
contained in the base directory, while the kernel addressed `<target>`, which can
be anywhere on the filesystem.

Both helpers now report such a path as not contained, so that every containment
check built on them fails closed - among them `assertLocalPathCorrect`, the
`user_files` fence of the local data lake table engines and table functions.
`resolvePathRelativelyToBase` rejects it explicitly, because it returns a path
that is passed to syscalls directly; `listObjects` already had that check for a
reason of its own.

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

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a6a4be6]

Summary: ✅


AI Review

Summary

This PR hardens filesystem containment against embedded-NUL paths by making pathStartsWith and fileOrSymlinkPathStartsWith fail closed, rejecting embedded NUL bytes directly in resolvePathRelativelyToBase, and adding an up-front StorageFile guard before any stat/glob probe. The previously raised gaps around focused regression coverage and the early StorageFile rejection are fixed in the current code, and I did not find any remaining unresolved correctness or security issues in the current patch.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit a6a4be6.

Metric Baseline Current Δ
Lines 89.10% 89.00% -0.10%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.50% +0.00%

Changed lines: Changed C/C++ lines covered: 68/69 (98.55%) · 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 Sep 13, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-multidisk Multiple disks/volumes storage policies, disk abstractions. label Sep 13, 2026
`grep -c` counted the error twice, because the client prints the log line of the
server next to the exception.

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

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a6a4be611 with master 95448e95a (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

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

Job report

The existing regression only exercised `deltaLakeLocal` / `icebergLocal`, whose check ANDs
`fileOrSymlinkPathStartsWith` with `pathStartsWith`, so it stayed green if either helper regressed
alone and never reached the guard in `resolvePathRelativelyToBase`. Add:

- helper-level tests for `pathStartsWith` (both overloads) and `fileOrSymlinkPathStartsWith`, with
  the escape shape `<prefix>/../probe\0/../<prefix>/file` and the same traversal without a NUL as a
  positive control;
- a direct test of `resolvePathRelativelyToBase`, asserting `BAD_ARGUMENTS` for NUL-bearing paths
  and keeping the plain-traversal `PATH_ACCESS_DENIED` and accepted-path behaviour as controls;
- a functional test for a `fileOrSymlinkPathStartsWith`-only surface (the `file` function) and a
  `pathStartsWith`-only surface (the `Filesystem` database engine). Each names an existing entry
  in `user_files` followed by a NUL and a suffix, so a check that ignored the NUL would let the
  truncated path through and the query would succeed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Comment thread src/Common/filesystemHelpers.cpp Resolved
…irst filesystem probe

`getPathsList` stats the user-supplied path (`fs::is_directory`, `fs::file_size`) and expands its globs on the
filesystem before the matched paths are checked for containment in `user_files_path`, and every syscall stops at
the first NUL byte. The `file` table function and the `File` engine could therefore still make the server probe
a truncated location that the containment check never sees. Reject such a path up front with `BAD_ARGUMENTS`,
and do the same in `checkCreationIsAllowedResolvingDotDot`, whose `..` resolver canonicalizes the head of the
path on the filesystem.

Test `05213_storage_file_embedded_nul` covers the table function (plain, glob, archive syntax, `INSERT`) and
the engine, each with an existing entry followed by a NUL and a suffix, so that the truncated path would succeed.

Addresses the AI review on #119846

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 3bdbfcaf-e6af-4143-8604-21d3d7eed317
  • MIRAI run: d54b0912-f1dd-44c4-bc6d-8887054168f1
  • PR check IDs:
    • clickbench_149223_1789392037
    • clickbench_149229_1789392037
    • clickbench_149235_1789392037
    • tpch_adapted_1_official_149245_1789392037
    • tpch_adapted_1_official_149256_1789392037
    • tpch_adapted_1_official_149294_1789392038

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119846&sha=8ed22a0374c684436783efb7b7c3278d9443e1ff&name_0=PR&name_1=AST%20fuzzer%20(amd_debug,%20targeted,%20old_compatibility) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The fuzzer hit Logical error: Can't determine table for parallel replicas in buildQueryPlanForParallelReplicas on a RIGHT JOIN ... UNION ALL ... RIGHT JOIN inside viewExplain. This is the shape tracked by #113292 and #119408, both closed by #113515, but that fix is already an ancestor of the failing head 8ed22a03, and the same error also hit #119818 on 2026-09-13 after the fix merged, so the fix is incomplete. This PR only touches embedded-NUL checks in filesystem path containment and does not reach the planner.

The other red, Stress test (arm_msan) with Invalid number of columns in chunk pushed to OutputPort for a countResample state column, is the empty-serialized-aggregate-state bulk read already being fixed in #119700.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is in progress: #110020.

Verified today against the failing query, on master 95448e95a (which contains #113515), same tree and build dir for both directions:

  • pristine master (build id 3c7a8125): Logical error: Can't determine table for parallel replicas at findParallelReplicasQuery.cpp:639, with the report's frames (buildQueryPlanForParallelReplicas <- Planner::buildPlanForUnionNode <- TableFunctionExplain::executeImpl). The query is run verbatim, with the __fuzz tables recreated as plain MergeTree, which is what the fuzzer produces: QueryFuzzer.cpp:1417 strips Replicated when it clones a table.
  • same tree plus the two source files from Decide parallel replicas eligibility with the subquery's own context #110020 (build id 1f3b1ae9): no error, the query returns 0.

The trigger is the UNION ALL, not the RIGHT JOIN, which is why #113515 does not cover it. Each arm sets parallel_replicas_for_non_replicated_merge_tree = 0 while the enclosing query has it at 1, and on a non-replicated MergeTree that setting is what decides eligibility. The query is chosen for parallel replicas once for the whole union, with the union's context (findParallelReplicasQuery.cpp:364), so the arm looks eligible at 1; the per-arm planner then builds with the arm's own context (Planner.cpp:2298), where it is 0, so findTableForParallelReplicas finds no eligible table and :639 throws. The same arm outside a union is judged with its own context and plans correctly on master, which is why the single-arm test from #113515 still passes.

#110020 threads the enclosing (sub)query context through both walks, so the arm is judged ineligible and no parallel-replicas plan is attempted. It has been open since 2026-07-10 and has not been reviewed yet.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks. So the AST fuzzer red on 8ed22a0 is the UNION ALL per-arm parallel_replicas_for_non_replicated_merge_tree context mismatch, unrelated to this PR, tracked by #110020. Nothing to change here; the current head a6a4be6 already has master merged.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Primitive, but okay.

clickhouse-gh Bot added a commit that referenced this pull request Sep 18, 2026
Backport #119846 to 26.9: Reject paths with an embedded NUL byte in filesystem containment checks
PedroTadim added a commit that referenced this pull request Sep 18, 2026
One conflict, in `StorageFile.cpp`. Upstream puts its new
`throwIfPathContainsEmbeddedNul` call in `checkCreationIsAllowedResolvingDotDot`,
a wrapper that resolves `..` on the filesystem before the containment check and
therefore has to reject the path first. That wrapper, and the
`resolveDotDotForContainmentCheck` it is built on, do not exist on this branch:
the four sites that call it on master call `checkCreationIsAllowed` directly here.

The wrapper is dropped and the call is placed at the top of
`checkCreationIsAllowed` instead, which is the function the wrapper delegates to
and which has no other callers on this branch, so the four sites are covered
exactly as upstream covers them. It runs before the `ApplicationType::SERVER`
early return, as upstream's does. The other two hunks of the file, the helper
itself and the `getPathsList` call, are taken unchanged, as are
`filesystemHelpers.cpp` and `LocalObjectStorage.cpp`.

Without this the branch would still fail closed - a NUL path reaches
`fileOrSymlinkPathStartsWith`, which the same change teaches to report it as not
contained - but with DATABASE_ACCESS_DENIED instead of upstream's BAD_ARGUMENTS.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Cherry pick #119846 to 26.3: Reject paths with an embedded NUL byte in filesystem containment checks
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Cherry pick #119846 to 26.7: Reject paths with an embedded NUL byte in filesystem containment checks
PedroTadim added a commit that referenced this pull request Sep 18, 2026
One conflict, in `StorageFile.cpp`. Upstream puts its new
`throwIfPathContainsEmbeddedNul` call in `checkCreationIsAllowedResolvingDotDot`,
a wrapper that resolves `..` on the filesystem before the containment check and
therefore has to reject the path first. That wrapper, and the
`resolveDotDotForContainmentCheck` it is built on, do not exist on this branch:
the four sites that call it on master call `checkCreationIsAllowed` directly here.

The wrapper is dropped and the call is placed at the top of
`checkCreationIsAllowed` instead, which is the function the wrapper delegates to
and which has no other callers on this branch, so the four sites are covered
exactly as upstream covers them. It runs before the `ApplicationType::SERVER`
early return, as upstream's does. The other two hunks of the file, the helper
itself and the `getPathsList` call, are taken unchanged, as are
`filesystemHelpers.cpp` and `LocalObjectStorage.cpp`.

Without this the branch would still fail closed - a NUL path reaches
`fileOrSymlinkPathStartsWith`, which the same change teaches to report it as not
contained - but with DATABASE_ACCESS_DENIED instead of upstream's BAD_ARGUMENTS.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Cherry pick #119846 to 26.8: Reject paths with an embedded NUL byte in filesystem containment checks
PedroTadim added a commit that referenced this pull request Sep 18, 2026
One conflict, in `StorageFile.cpp`. Upstream puts its new
`throwIfPathContainsEmbeddedNul` call in `checkCreationIsAllowedResolvingDotDot`,
a wrapper that resolves `..` on the filesystem before the containment check and
therefore has to reject the path first. That wrapper, and the
`resolveDotDotForContainmentCheck` it is built on, do not exist on this branch:
the four sites that call it on master call `checkCreationIsAllowed` directly here.

The wrapper is dropped and the call is placed at the top of
`checkCreationIsAllowed` instead, which is the function the wrapper delegates to
and which has no other callers on this branch, so the four sites are covered
exactly as upstream covers them. It runs before the `ApplicationType::SERVER`
early return, as upstream's does. The other two hunks of the file, the helper
itself and the `getPathsList` call, are taken unchanged, as are
`filesystemHelpers.cpp` and `LocalObjectStorage.cpp`.

Without this the branch would still fail closed - a NUL path reaches
`fileOrSymlinkPathStartsWith`, which the same change teaches to report it as not
contained - but with DATABASE_ACCESS_DENIED instead of upstream's BAD_ARGUMENTS.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
robot-clickhouse added a commit that referenced this pull request Sep 18, 2026
robot-clickhouse added a commit that referenced this pull request Sep 18, 2026
robot-clickhouse added a commit that referenced this pull request Sep 18, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 18, 2026
Backport #119846 to 26.8: Reject paths with an embedded NUL byte in filesystem containment checks
clickhouse-gh Bot added a commit that referenced this pull request Sep 18, 2026
Backport #119846 to 26.7: Reject paths with an embedded NUL byte in filesystem containment checks
PedroTadim added a commit that referenced this pull request Sep 18, 2026
Backport #119846 to 26.3: Reject paths with an embedded NUL byte in filesystem containment checks
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 25, 2026
@clickhouse-gh clickhouse-gh Bot added comp-common-library Common library: the general-purpose code in src/Common and src/Core that belongs to no feature - ... and removed comp-multidisk Multiple disks/volumes storage policies, disk abstractions. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-common-library Common library: the general-purpose code in src/Common and src/Core that belongs to no feature - ... 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.

5 participants