Repository navigation
Require the FILE source grant for an EmbeddedRocksDB table with an explicit rocksdb_dir - #117428
Conversation
…_dir `ENGINE = EmbeddedRocksDB(ttl, rocksdb_dir, read_only)` opened a directory under `user_files_path` with no access check of any kind, so a user holding only `CREATE TABLE` in a database of their own could point a table at another table's directory and read its rows. Reading the same bytes through `file` costs `READ ON FILE`; through this engine it cost nothing. `create` parsed both arguments and went straight to construction. The engine also declares no `source_access_type`, so it took the unconditional branch of `ContextAccess::calculateFinalAccess` and received the implicit `TABLE ENGINE` grant, which is the only engine-level check `InterpreterCreateQuery` makes. What remained was the constructor's `fileOrSymlinkPathStartsWith` fence, and that bounds where the directory may be, not who may read it. `read_only = 1` routes to `rocksdb::DB::OpenForReadOnly`, which takes no RocksDB LOCK, so the victim table can be live while it is read. The check goes in `create` rather than on the `StorageFeatures`, for three reasons. `source_access_type` is engine-wide and cannot see the arguments, so it would also gate the argument-less form, which touches only the table's own data directory under the database path and is the form the engine's documentation uses. It is tested with `isGranted(READ | WRITE, source)`, so a `read_only` table would need `WRITE ON FILE`. And `replaceExternalEngineWithNullIfNeeded` keys off the same field, so declaring it would silently start replacing the engine with `Null` under `restore_replace_external_engines_to_null`. The context must be `args.getLocalContext()`. `args.getContext()` is asserted to be the global context in `StorageFactory::get`, and `Context::getAccess` treats a context with no user as having full access, so a check written against it compiles, reads correctly, and enforces nothing. I built it that way to confirm: every denial assertion in the new test passes. The guard is the fresh-definition predicate already used by `StorageURL`, `StorageDistributed`, `StorageBuffer`, `StorageNATS` and `DatabaseBackup`: authorize a definition being introduced now, skip one replayed from this server's own metadata, so server startup, a short `ATTACH TABLE t`, an `ATTACH DATABASE` and an `UNDROP` keep working after a revoke. The weaker `mode <= LoadingStrictnessLevel::CREATE` was not used: it exempts `ATTACH`, and a full-definition `ATTACH TABLE ... ENGINE = EmbeddedRocksDB(0, 'dir', 1)` runs at that level while requiring only `CREATE TABLE`, which would leave the same hole open through a different keyword. `RESTORE` arrives at `SECONDARY_CREATE` and is therefore checked, matching the ruling in `StorageDistributed`. The `ApplicationType::LOCAL` exemption mirrors `FunctionFile`: in `clickhouse-local` the engine skips the `user_files` fence entirely, so there is no boundary there for a `FILE` grant to authorize. The new stateless test has seven arms, and deleting any one term of the condition reddens a specific one: the explicit-directory scope is pinned by the argument-less and `ttl`-only arms, the replayed-definition exemption by the short-`ATTACH` arm, the `WRITE` half by the writable arm, and the local context by all three denial arms. Names carry the shell pid because a `.sh` test's `CLICKHOUSE_DATABASE` is `default` rather than the runner's per-test database, so `CLICKHOUSE_TEST_UNIQUE_NAME` is identical in every concurrent copy while a user, a database and a `user_files` directory are global names. Two gaps of the same class are left open deliberately. An `ON CLUSTER` or `Replicated`-database replay executes with no user unless `distributed_ddl_use_initial_user_and_roles` is on, so the check is a no-op there; that is true of all four in-tree precedents, and the initiator-side preflight is a separate change. Directory ownership is untouched: a legitimate `READ ON FILE` holder can still co-opt a live table's directory, and rejecting that would break the supported configuration of one writable plus several `read_only` tables over one directory, so it needs an owner-level design rather than a guess. Co-Authored-By: Claude Opus 5 <[email protected]>
A RESTORE supplies a table definition under whoever is restoring, who need not be the user the backup was taken from, and it arrives at LoadingStrictnessLevel::SECONDARY_CREATE, which isLoadingFromExistingMetadata does not include. So the new FILE source check applies to it exactly as it does to a CREATE. That was true of the previous commit but only by reading the predicate: no arm reached SECONDARY_CREATE. The arm backs up an explicit-dir table as the admin, drops it, and has a user holding READ ON FILE but not WRITE ON FILE restore it. The RESTORE is refused with the privilege sentence naming the missing WRITE, leaves no table behind, and succeeds once WRITE ON FILE is granted. Modelled on the equivalent arm in 04401_url_engine_s3_dispatch_engine_grant.sh, which pins the same half of the same predicate for the URL engine. Measured against the same tree without the check: the denial half flips to NOT DENIED and the table is created anyway, so it is a live arm, while the granted half passes on both binaries and is therefore a positive control rather than a second denial assertion.
The stateless test config enables table_engines_require_grant (tests/config/config.d/enable_access_control_improvements.xml, installed by tests/config/install.sh), and EmbeddedRocksDB declares no source_access_type, so ContextAccess withholds the implicit TABLE ENGINE grant from the test user. InterpreterCreateQuery then rejects the statement before StorageFactory reaches create, which is where the FILE check this test is about lives. Without the engine grant the test does not exercise that check at all: the four denial arms report a TABLE ENGINE privilege sentence instead of a FILE one, and the six permitted arms cannot create their tables, so ten of the eleven arms break. The grant is a precondition, not part of the assertion, so it is taken once in the setup block and never revoked. Same shape as 04401_url_engine_s3_dispatch_engine_grant.sh, which pairs the table grant with GRANT TABLE ENGINE ON URL for the URL engine; 22 stateless tests use the idiom. No source change and no reference change: the arms' outputs are identical, only the precondition differs. Verified on a server carrying that config file, with a control that an ungranted argument-less CREATE is still refused naming TABLE ENGINE, and that with the engine grant held an explicit directory is still refused naming READ ON FILE.
Internal second-model review: adjudication log (click to expand)Three rounds of pre-publication review by an independent model (engine: codex; 3 findings, then 2,
Severity: ❌ blocker / Review sessions: cron:clickhouse-review-slot-11:20260831-173600, cron:clickhouse-review-slot-9:20260831-210500, cron:clickhouse-review-slot-49:20260831-224300 |
Pre-PR validation gate (click to expand)
Validation, on a server carrying Session id: cron:clickhouse-impl-slot-5:20260831-214504 |
|
Workflow [PR], commit [8bbab37] Summary: ❌
AI ReviewSummaryThis PR adds a Findings❌ Blockers
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 8bbab37.
Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code |
|
cc @GrigoryPervakov @pufit, could you review this? |
| { | ||
| const AccessFlags required | ||
| = read_only ? AccessFlags(AccessType::READ) : (AccessType::READ | AccessType::WRITE); | ||
| local_context->checkAccess(required, toStringSource(AccessTypeObjects::Source::FILE)); |
There was a problem hiding this comment.
This only protects the local CREATE / full-definition ATTACH / RESTORE path that actually reaches StorageFactory::get. CREATE in a Replicated database returns via tryEnqueueReplicatedDDL, and CREATE ... ON CLUSTER returns via executeQueryOnCluster, before the storage is instantiated at all (src/Interpreters/InterpreterCreateQuery.cpp:2039-2051), so the initiator never executes this branch.
On the worker side, DDLTaskBase::makeQueryContext preserves the initiating user only when distributed_ddl_use_initial_user_and_roles = 1 (src/Interpreters/DDLTask.cpp:332-353). Otherwise the query runs with no user_id, and Context::getAccess treats that as full_access (src/Interpreters/Context.cpp:2453-2461), so this checkAccess is a no-op there too.
That leaves the same RBAC hole open for distributed DDL: a user who can create in a Replicated database, or who also holds CLUSTER, can still create EmbeddedRocksDB(0, 'secret_rocks', 1) without any READ ON FILE grant and read another table's directory. The authorization needs an initiator-side, argument-aware preflight before the enqueue/ON CLUSTER return paths, otherwise the PR title and changelog are still false for those entrypoints.
There was a problem hiding this comment.
Citations check out at 71272f0. InterpreterCreateQuery.cpp:2039-2045 returns via tryEnqueueReplicatedDDL and :2048-2051 via executeQueryOnCluster, both before StorageFactory::get; DDLTask.cpp:332 sets the initiating user only when distributed_ddl_use_initial_user_and_roles is on, and it defaults to false (ServerSettings.cpp:1171); Context.cpp:2454 is bool full_access = !user_id.
The mechanism is not new to this thread. It is row 1 of the adjudication comment on this PR and it is in the description, with the same line numbers. Scope is unchanged: every creator-side source or engine check in the tree has this property, and DatabaseBackup.cpp:538-541 guards on has_real_user for exactly this reason. #111561 is the no-user root cause and #113073 is the argument-aware initiator-side preflight. Adding a new initiator-side authorization layer is a separate change with its own design question, and I am not making it inside this one.
Your last clause is the part that was not already covered, and it is right for the changelog. That entry lands in CHANGELOG.md on its own, without the description, so as written it read as if the hole were closed everywhere. I added to it: "ON CLUSTER and Replicated-database DDL are not covered: the replay runs with no user." I also named Replicated databases in the description, which said only ON CLUSTER even though that is a separate return path. I left the title, which describes the change at the same granularity as the precedents it follows, and the entry under it now carries the limitation.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes3 object files changed (+21.70 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 16 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 71272f0Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task This head is 170 nodes: 146 OK, 19 SKIPPED, 5 red. The diff is 3 files (
Session id: cron:our-pr-ci-monitor:20260901-060000 |
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
| && local_context->getApplicationType() != Context::ApplicationType::LOCAL) | ||
| { | ||
| const AccessFlags required | ||
| = read_only ? AccessFlags(AccessType::READ) : (AccessType::READ | AccessType::WRITE); |
There was a problem hiding this comment.
This read_only ? READ : READ|WRITE split is only safe if a read_only EmbeddedRocksDB table has no mutating entrypoints that touch rocksdb_dir. That is not true today. StorageEmbeddedRocksDB::write never checks read_only, and with the default optimize_for_bulk_insert = 1 the EmbeddedRocksDBBulkSink constructor creates insert_directory_queue under storage.getDataPaths()[0] before any RocksDB write is attempted (src/Storages/RocksDB/EmbeddedRocksDBBulkSink.cpp:99-105). truncate also remove_alls rocksdb_dir unconditionally (src/Storages/RocksDB/StorageEmbeddedRocksDB.cpp:299-310).
So a user with only READ ON FILE plus ordinary rights on their alias still gets filesystem-write effects on another table's directory. Either the read_only = 1 branch needs WRITE ON FILE too, or the mutating entrypoints on read_only instances need to throw before they touch the filesystem.
There was a problem hiding this comment.
Confirmed all three call sites and measured them on pristine master 26.9.1.1; this PR changes only create, so those paths are identical there. EmbeddedRocksDBBulkSink.cpp:103-104, StorageEmbeddedRocksDB.cpp:819, :309.
write: the INSERT does reach the sink, then fails withCode: 555 ... Not supported operation in read only mode. 0 rows written, victim row intact.- Bulk sink: two
tmp_insert_*directories appeared inside the victim's directory during the INSERT (7 of 31 samples at 20 ms; 0 of 28 withoptimize_for_bulk_insert = 0). Both were removed on sink destruction and the directory returned to its original 6 entries. truncate: this is the one with teeth. The alias owner'sTRUNCATEemptied the victim's directory, 6 files to 0. The victim kept answering from its still-open handle, and after a server restartSELECT count()on it returns 0. Silent, permanent data loss.
I am not taking the first remedy. Requiring WRITE ON FILE for read_only = 1 makes a reader hold WRITE, which breaks the one-writable-plus-N-read_only shared-directory configuration this engine documents, and it does not close truncate anyway: any legitimate WRITE ON FILE holder still wipes a neighbour's directory with that same statement.
Making read_only instances refuse mutating entrypoints is the right shape. For write it costs nothing given the error above. truncate succeeds destructively today, so guarding it changes documented read_only behavior, which is the directory-ownership half the description already flags as needing a maintainer call. I am not folding it into this PR.
CI finish ledger - 8bbab37Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260901-173000 |
9a853c0
Edited by the NightlyChangelog CI job following .claude/skills/edit-changelog/SKILL.md. Changelog-revert: 117651 117428 Revert "Require the FILE source grant for an EmbeddedRocksDB table with an explicit rocksdb_dir" Changelog-deleted-entry: 117428 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * `ENGINE = EmbeddedRocksDB(ttl, rocksdb_dir, read_only)` with an explicit `rocksdb_dir` now requires the `FILE` source grant that `file` requires for the same directory: `READ ON FILE` to create such a table, plus `WRITE ON FILE` when `read_only = 0`. Previously it required no source grant, so a user holding only `CREATE TABLE` in a database of their own could point a table at another table's directory and read its rows. `ON CLUSTER` and `Replicated`-database DDL are not covered: the replay runs with no user. The argument-less form is unaffected. Backward incompatible for anyone already using an explicit `rocksdb_dir` without them. [#117428](#117428) ([Groene AI](https://github.com/groeneai)).
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ENGINE = EmbeddedRocksDB(ttl, rocksdb_dir, read_only)with an explicitrocksdb_dirnow requires theFILEsource grant thatfilerequires for the same directory:READ ON FILEto create such a table, plusWRITE ON FILEwhenread_only = 0. Previously it required no source grant, so a user holding onlyCREATE TABLEin a database of their own could point a table at another table's directory and read its rows.ON CLUSTERandReplicated-database DDL are not covered: the replay runs with no user. The argument-less form is unaffected. Backward incompatible for anyone already using an explicitrocksdb_dirwithout them.Description
A user holding only
CREATE TABLEin their own database could read another table's rows:createperformed no access check, and the engine declares nosource_access_type, so the implicitTABLE ENGINEgrant was the only engine-level check. The containment fence bounds where the directory may be, not who may read it, andread_only = 1opens withOpenForReadOnly, which takes no RocksDB lock, so the victim table can be live.createnow requires that grant on the explicit-directory branch only,READalways plusWRITEwhenread_only = 0, using the fresh-definition predicate ofStorageURL,StorageDistributed,StorageBufferandDatabaseBackup: a definition replayed from this server's metadata (startup, shortATTACH TABLE t,UNDROP) stays loadable after a revoke, andclickhouse-localis exempt as infile.RESTOREis checked, perStorageDistributed.cpp:2675-2691. Not declared assource_access_type: that is engine-wide and argument-blind, so it would also gate the argument-less form the documentation uses.Two same-class gaps stay open. An
ON CLUSTERorReplicated-database replay runs with no user unlessdistributed_ddl_use_initial_user_and_rolesis on, so the check is a no-op there, as in the precedents above; #113073 is the initiator-side preflight. Directory ownership is untouched: aREAD ON FILEholder can still co-opt a live table's directory, andtruncatehas noread_onlyguard, so that half destroys data too. Rejecting it would break the supported one-writable-plus-N-read_onlyconfiguration, so it needs your call.StorageFileLogreads the same way, unexercised.New stateless test: four statements master allows are now refused, six others pinned.
Workflow [PR]
Sync PR [sync-upstream/pr/117428]
Version info
26.9.1.517(included in26.9and later)