Repository navigation
Do not create a disk defined in SQL while deciding whether ALTER ... MODIFY SETTING disk is allowed - #119850
Conversation
…an name Two locations of a disk created by a `disk(...)` clause were not confined: The name of the disk is used verbatim as a path component of the state the server keeps for it - the metadata storage builds `<clickhouse path>/disks/<name>/` and creates that directory - so a name containing a path separator or `..` relocated server-managed state to any directory the server can write to, `user_scripts/` among them. The name must now be a plain name. The `custom_local_disks_base_directory` fence was applied only to disks that report `isRemote() == false`, which `DiskObjectStorage` never does - not even when its backing object storage is the local filesystem, where `path` is a plain directory that the query just named, and where the metadata is written to the local filesystem as well. The fence now asks whether the backing storage is local instead, and covers both locations. This is a backward incompatible change for a disk defined in SQL as `object_storage` over `local`: such a disk now requires `custom_local_disks_base_directory` to be configured and its `path` and `metadata_path` to be inside it, the same requirement that `type = local` disks have had. Disks defined in the server configuration are not affected, and neither is attaching a table that already uses such a disk. Finally, the entries that `DatabaseCatalog::maybeRemoveDirectory` is asked to remove are exactly the ones that failed the "is a directory" test, so they can be regular files, and widening a regular file's mode to `S_IRWXU` makes it executable. Removal needs write and execute permissions on the directory whose entries are unlinked, not any permission on the entry itself, so only a directory gets its mode widened now. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [747619c] Summary: ✅
AI ReviewSummaryThis PR fixes the main single-node bypass where several pre-validation Findings
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 747619c.
Changed lines: Changed C/C++ lines covered: 28/33 (84.85%) · Uncovered code |
…disks These tests define a disk backed by the local filesystem in SQL and give it a path next to the data directory rather than inside `disks/`, which this change confines them to. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… the server Two corrections, both found by the functional tests. A relative path of a disk backed by the local filesystem was resolved against the working directory of the process, which is not a property of the server: it is `/` under systemd, the log directory when running as a daemon, and the directory the server happened to be started from otherwise. So the same definition described a different location on the next start, and no directory could be said to contain it. Such a path is now resolved against the directory of the server, which is what every such definition already means in the environments where it works today. The metadata path of an object storage disk is resolved the same way. The containment check asked the created disk for its path, which sees through a disk that refers to a disk of the server configuration: a `cache` disk over a configured local disk was refused because of the path the administrator gave that disk. The check now reads the locations out of the disk definition, so it covers exactly what the query named - and it runs before the disk is created, so a rejected definition also creates no directory. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ctory The working directory of the server process, which the filesystem resolves a relative path against, is not a property of the server: it is `/` under systemd, the log directory when running as a daemon, and the directory the server was started from otherwise - and it is not the data directory in the test environment either. A relative path therefore named a different place depending on how the server was started, and no directory could be said to contain it. A relative path of a disk defined in SQL is now resolved against `custom_local_disks_base_directory`, so it is inside that directory by construction, and the definition keeps naming the same place on the next start. An absolute path still has to be inside that directory. Both the data and the metadata location of the disk are resolved in the disk definition before the disk is created, so the disk uses exactly the location that was checked. Because of that, the tests that give their disk a relative path did not need to change after all; only the one test that built an absolute path out of the data directory of the server had to name the base directory instead. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… test The disks the test creates on purpose stay registered for the lifetime of the server, so a pattern over `system.disks` counted them on the second run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units105 translation units recompiled, 1366 s compile time in total, 105 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.25 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…finition An `ATTACH TABLE` that writes the disk out in the query names a location just as a `CREATE TABLE` does, so it is checked the same way; only loading a table from the metadata the server already stored is not. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A `type = local` disk passes the path to the filesystem exactly as it was given, so there is nothing to resolve for it, and the check that runs after it is created is truthful as it stands. Its wording matters too: a test reads the configured base directory out of that message. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Resolving a relative path against the base directory made the disk write somewhere the definition did not name, which in the test environment is a directory shared by every job on the runner rather than the sandbox of the run - a leftover from an earlier run then broke the `plain_rewritable` metadata of a later one, and the whole shard with it. The check now compares the location exactly as the definition gives it, which is also exactly how the disk uses it. A relative path is resolved by the filesystem against the working directory of the process - `/` under systemd - so it is inside the base directory only by accident, and the tests that had one now name the base directory instead, the way the tests of `type = local` disks already did. `04103` becomes a shell test for that reason, and gives its disk a per-run directory: its metadata lives in that directory, so a leftover from another run is not inert. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
|
lukaszszafranski
left a comment
There was a problem hiding this comment.
Thanks @alexey-milovidov for the opportunity to run our tool on this PR. Here are the findings that we found:
Veridical.dev reviewed the change:
- Merge readiness — ⛔ Do not merge
- Correctness — 🟠 Needs work
- Security — 🟠 Flagged
- Tests — 🟡 Three focused coverage suggestions; overall coverage is not established
- Changed hunks with no finding — 80% (finding distribution only, not review recall)
7 surfaced items · 2 with source/evidence text attached
📝 Walkthrough
Confines SQL-defined local disks to the configured base directory, validates names and metadata paths before registration, and avoids making removable files executable. The patch updates affected integration and stateless tests to use the allowed disk directory.
| File | Change |
|---|---|
src/Disks/DiskFromAST.cpp |
Validates custom disk names, data paths, and metadata paths before creation. |
src/Interpreters/DatabaseCatalog.cpp |
Limits chmod during cleanup to directories. |
tests/integration/test_attach_backup_from_s3_plain/configs/disk_s3.xml |
Configures the allowed base directory for the test. |
tests/queries/0_stateless/04103_plain_rewritable_get_last_modified_bug.sh |
Uses the runtime disk directory in the rewritten regression. |
tests/queries/0_stateless/05216_custom_disk_local_path_validation.sql |
Exercises invalid names, outside paths, metadata traversal, cleanup, and valid disks. |
| 15 additional changed test files | Move custom local-disk paths beneath the configured base directory. |
🗺️ How the changes interact
flowchart TD
A[SQL custom disk definition] --> B[DiskFromAST validation]
B --> C{Path and name allowed?}
C -->|No| D[Reject]
C -->|Yes| E[Create or reuse disk]
E --> F[Local filesystem]
H[Test configuration] --> I[Allowed base directory]
I --> B
J[Updated regressions] --> A
K[DatabaseCatalog cleanup] --> L[chmod directories only]
Supported defects
| Severity | Where | Issue |
|---|---|---|
| 🟠 High | src/Disks/DiskFromAST.cpp:187 |
The backup exemption trusts a SQL-supplied disk name, allowing a plain local disk to skip the confinement fence. |
| 🟡 Medium | src/Disks/DiskFromAST.cpp:187 |
A rejected type = local disk can create or touch its out-of-base path before the post-construction validation runs. |
Nonblocking observations
🔍 Five coverage and cleanup observations
- Add a direct
type = localrejection case and assert that the rejected path was not created. - Add a negative case for the
type = local_blob_storagecompatibility spelling. - Exercise each representable invalid-name predicate, not only a slash-containing name.
- Correct the cleanup comment in
DatabaseCatalog.cpp; valid directories without a UUID mapping can reach the helper too. - Align the disk-name error text with the implemented exact
./..checks.
Findings that were reported by other bots
clickhouse-gh had already reported these findings on the same head, so Veridical will not repost them as original findings:
- Resolved
locations.*local backends introduced throughincludeare not traversed by the root-only pre-check. Veridical independently generated the same mechanism, but terminal verification left it inconclusive and withheld it. - Live
ALTER TABLE ... MODIFY SETTING disk = disk(type = local, ...)can memoize an unchecked disk during the metadata-loading pass. Veridical ingested that thread; the separate medium finding above is retained only because it adds distinct evidence thatDiskLocal::setup()creates the rejected directory before the fence runs.
🔬 How the two supported defects were verified
src/Disks/DiskFromAST.cpp:187—DiskFactory::createreceives the user-deriveddisk_name, and the branch skipscheckCustomDiskPathIsAllowedwhenever that resulting name equalsbackup.src/Disks/DiskFromAST.cpp:187— the pre-creation helper excludes plain local disks, whileDiskLocal::setup()performscreate_directoriesbefore control reaches this check.
Review scope: complete
The reviewed revision changes 20 files / 190 added lines. All 20 changed files were supplied to the review; no changed file was skipped. The changed-hunk percentage above describes where findings landed, not which files were omitted.
Reviewed by Veridical · ultra depth · some optional execution checks did not complete
… by its type Two locations escaped the fence. A multi-location `DiskObjectStorage` builds one object storage per `locations.<name>` child, each with its own backend and its own `path`, so an `include` could resolve a local child behind a root that names a remote backend, and the check only ever looked at the root. And a plain `type = local` disk was checked only after `DiskFactory::create` had built and started it, so `DiskLocal::setup` created the rejected directory first, and `ALTER TABLE ... MODIFY SETTING disk = disk(type = local, ...)` slipped past entirely: it resolves the disk once with `is_loading_from_existing_metadata`, which memoizes it in `Context::getOrCreateDisk`, and the stricter pass that follows finds the disk already there and never runs the check. Both are now checked before the disk is created, by one rule: every backend of the resolved configuration - the root and every `locations.<name>` child - whose type starts with `local` has its `path` checked. The check that runs after the disk is created stays as a backstop for the local disks whose directory the definition does not name outright. Its exemption for the backup disk went by the disk name, which the query chooses, so `disk(name = 'backup', type = local, path = '/etc/')` avoided the only fence a plain local disk had. It goes by the type of the disk now. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`05217` covers `ALTER TABLE ... MODIFY SETTING disk = disk(type = local, ...)`, a disk named `backup`, and the absence of a directory for a disk the fence rejected. The integration test covers a `locations.<name>` child that an `include` resolves to a local backend outside the base directory. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…allowed `ALTER TABLE ... MODIFY SETTING disk = disk(type = local, path = '...')` was rejected, and yet left the disk registered and its directory created. Several computations that run before the statement is known to be allowed rebuild the settings the table would end up with - `AlterCommands::apply` for the implicit index settings, `checkAlterIsPossible` for the projection and part-format checks, `checkColumnFilenamesForCollision` - and each of them applied the whole change list as if it came from stored metadata, which resolves the `disk` setting, and resolving it creates and registers the disk. None of those computations reads the `disk` setting, so they now apply the changes with `applyChangesLeavingDiskUnresolved`, which leaves it alone. The settings update itself, `MergeTreeData::changeSettings`, still resolves it - by then the statement has passed the check. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…al-paths Master gained #110665, which moved the `custom_local_disks_base_directory` fence into the disk factories (`DiskLocal`, the local object storage, `metadata_path`, `encrypted`, every `locations.<name>` child), added the disk-name check, and dropped the name-based `backup` exemption. That covers the checks this branch added to `DiskFromAST.cpp`, so the file is taken from master as is, and `05216`, which duplicated master's `05212_custom_disk_local_paths_confined`, is removed. The `custom_local_disks_base_directory` key this branch added to `test_attach_backup_from_s3_plain` is already on master. What master does not cover is kept: the factory checks run only for `!attach`, and the computations that rebuild the post-`ALTER` settings resolved `disk` as if it came from stored metadata, which created and registered an unchecked disk. `refreshSettingsDerivedMetadata`, which replaced the inline block in `AlterCommand::apply`, now uses `applyChangesLeavingDiskUnresolved` as well. The `DatabaseCatalog::maybeRemoveDirectory` change is dropped, as asked in review, to keep this fix minimal for backporting.
Since #110665 the path check runs inside the disk factory, and the server log line of the exception is forwarded to the client by `--send_logs_level`, so the message appears twice on stderr. Stop at the first match. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ALTER ... MODIFY SETTING disk is allowed
…e fence tests `alter_effective_settings` left `disk` at its default, so the diff passed to `checkMergeTreeSettingsConstraints` missed a change of `disk` on a table that had none, and reported one on every ALTER of a table that had one. Resolve the new `disk` there instead, as a fresh definition when it changes, so the fence runs before anything registers the disk. Extend 05217 to a local object storage disk, an outside `metadata_path`, a table without a SETTINGS clause, and a constant `merge_tree_disk` constraint. `test_dynamic_disk_include_locations_child_local_path_is_fenced` never reached the path check: a `type = object_storage` disk with an `include` and no literal `object_storage_type` is treated as possibly S3 and rejected with ACCESS_DENIED first. Lift the S3 credentials restriction for that query. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 On the AI review's remaining major: a definition the fence rejects is never built. The fence runs in the disk factory before That is not specific to I also merged master (747619c). |
| auto new_changes = new_metadata.settings_changes->as<const ASTSetQuery &>().changes; | ||
| /// The settings constraints below compare the resolved `disk`, so it is resolved here. A changed | ||
| /// `disk` is a fresh definition and is checked as one, before anything registers the disk unchecked. | ||
| MergeTreeSettings::resolveDiskSetting(new_changes, local_context, /*is_loading_from_existing_metadata=*/!disk_setting_changed); |
There was a problem hiding this comment.
This new resolveDiskSetting call runs before the secondary-replay checks just below. That matters for the new “table had no previous SETTINGS clause” case this hunk is fixing: on a Replicated / Shared Catalog follower, replaying ALTER ... MODIFY SETTING disk = disk(...) now takes the fresh-definition path here with is_loading_from_existing_metadata = false.
If that follower has stricter local config (custom_local_disks_base_directory, dynamic-disk credential policy, etc.), it can throw an exception here even though the initiator already accepted the ALTER and persisted it. changeSettings replays the same metadata with is_loading_from_existing_metadata = true, so this path needs the same replay exemption: compute the replay flag first and skip the fresh resolution here (or resolve with true) on non-initial executions.
Resolve the conflicts of the cherry-pick of #119850 to 26.7. On 26.7, `refreshSettingsDerivedMetadata` does not exist (the implicit index refresh is inline in `AlterCommands::apply`), and the post-ALTER effective settings in `MergeTreeData::checkAlterIsPossible` are built inside the replay check instead of in `checkAlterEligibility`. The change is ported to those places: the implicit index refresh and `checkColumnFilenamesForCollision` and the part format check use `applyChangesLeavingDiskUnresolved`, and the effective settings for the constraints check resolve `disk` with `is_loading_from_existing_metadata = !disk_setting_changed` under `local_context`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #119850 to 26.7: Do not create a disk defined in SQL while deciding whether `ALTER ... MODIFY SETTING disk` is allowed
…eciding whether `ALTER ... MODIFY SETTING disk` is allowed
Cherry pick #119850 to 26.8: Do not create a disk defined in SQL while deciding whether `ALTER ... MODIFY SETTING disk` is allowed
…eciding whether `ALTER ... MODIFY SETTING disk` is allowed
The tests of #119850 also cover behaviour that came from #110665, which was not backported to 26.8 (its cherry-pick was closed). In 26.8 the `custom_local_disks_base_directory` check of a disk defined in SQL runs after the disk is registered and its directory created, does not cover object storage disks over the local filesystem (they are reported as remote), and exempts a disk named `backup`. Keep the part that this backport fixes: `ALTER TABLE ... MODIFY SETTING disk = disk(...)` with a local path outside the base directory is now rejected, and the settings constraints still compare the resolved disk. Drop the checks of registration and directory creation, the object storage cases, and the integration test for a `locations` child with a local backend, which all depend on #110665. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=124151&sha=1348e3964bcfa0dfa2569720e5461b11401f7b5b&name_0=BackportPR PR: #124151 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #119850 to 26.8: Do not create a disk defined in SQL while deciding whether `ALTER ... MODIFY SETTING disk` is allowed
Backport #119850 to 26.7: Do not create a disk defined in SQL while deciding whether `ALTER ... MODIFY SETTING disk` is allowed
ALTER TABLE ... MODIFY SETTING disk = disk(type = local, path = '<outside>')bypassed thecustom_local_disks_base_directoryfence for disks defined in SQL: the statement succeeded, the disk was registered insystem.disks, and its directory was created outside the base directory.The fence (since #110665 it lives in the disk factories) runs only for a fresh definition, not for one loaded from stored metadata. Several computations that run before the
ALTERis known to be allowed rebuild the post-ALTERsettings only to read a scalar out of them, and they applied the whole change list as if it came from stored metadata:refreshSettingsDerivedMetadata(the implicit minmax indices),MergeTreeData::checkAlterEligibility(projection and part-format checks), andcheckColumnFilenamesForCollision. Resolving thedisksetting creates and registers the disk, so the first of them created it unchecked, and the later fresh-definition check found it already registered and never ran.None of those computations reads
disk, so they now use the newMergeTreeSettings::applyChangesLeavingDiskUnresolved, which leaves thediskchange out. The check incheckAlterEligibilitystill resolves the newdiskas a fresh definition, andMergeTreeData::changeSettingsresolves it once the statement has passed.Verified on a local server: without the change, the
ALTERabove succeeds, registers the disk and creates the directory. With it, theALTERis rejected and nothing is left behind (05217_custom_disk_local_path_alter.sh). An integration case intest_s3_credentials_hardeningalso checks that anincludecannot slip alocations.<name>child of typelocalwith a path outside the base directory past the fence.Related: #110665
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER TABLE ... MODIFY SETTING disk = disk(...)could register a disk defined in SQL whose local path is outsidecustom_local_disks_base_directory, and create its directory, bypassing the check thatCREATE TABLEapplies. Now it is rejected the same way, and a statement rejected this way leaves no disk and no directory behind.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119850]
Version info
26.10.1.1451-master(included in26.10and later)26.8.20.6,26.7.24.7