Repository navigation
Isolate the filesystem of the server at startup - #128412
Conversation
Add the `filesystem_isolation` server setting (`disabled`, `best_effort`, `enforce`). At startup, while the process has a single thread and before the `seccomp` filter is installed, the server moves into a new mount namespace whose root is an empty read-only `tmpfs`, bind-mounts only the paths it needs according to its configuration (data, logs, configuration, certificates, a few system files, the executable and its libraries) plus the paths listed in `filesystem_isolation_paths`, and removes the original root with `pivot_root`. Writable mounts are `noexec`, every mount is `nosuid`, `/tmp` is private. There is no shell inside, which makes it harder to turn a remote code execution into anything else. With `CAP_SYS_ADMIN` only a mount namespace is created and the capabilities that could undo the isolation are dropped; without it a user namespace is created as well, and the lost capabilities are reported. Whether the namespaces can be created is checked in a child process first, so that `best_effort` does not leave the server in a namespace without its capabilities. The shipped `config.xml` enables `best_effort`; the code default stays `disabled`, so existing configurations keep working after an upgrade. The stateless tests run with `enforce`; the integration tests keep it disabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [6fae18a] Summary: ✅
AI ReviewSummaryAdds startup filesystem isolation with configuration-derived mounts and capability restrictions. Reviewed the current diff and all 44 discussion threads at Findings❌ Blockers
Final Verdict❌ Block — Correct the basename-log directory calculation and add the focused daemon regression case. No genuinely new inline findings were identified. |
…s namespaces The fast test runs in a container that is not privileged, where `unshare` of a mount or user namespace fails with `EPERM`, so the server refused to start with `filesystem_isolation` set to `enforce`. `install.sh` now probes with `unshare` and links `config.d/filesystem_isolation.xml` only when it succeeds, and the test `05473_filesystem_isolation` is tagged `no-fasttest`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=a0e331182c0d2bbde8868144e39acbbd86d8be87&name_0=PR&name_1=Fast%20test #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/clickhouse_common_io.dir/Common/FilesystemIsolation.cpp.o |
new | 449.19 KiB | +449.19 KiB |
programs/server/CMakeFiles/clickhouse-server-lib.dir/FilesystemIsolationPaths.cpp.o |
new | 237.25 KiB | +237.25 KiB |
programs/server/CMakeFiles/clickhouse-server-lib.dir/Server.cpp.o |
1.60 MiB | 1.64 MiB | +36.79 KiB (+2.24%) |
src/CMakeFiles/dbms.dir/Core/SettingsEnums.cpp.o |
2.28 MiB | 2.30 MiB | +22.57 KiB (+0.97%) |
src/CMakeFiles/dbms.dir/Core/ServerSettings.cpp.o |
960.96 KiB | 977.75 KiB | +16.79 KiB (+1.75%) |
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Slowest function optimization changes (ThinLTO)
Median per-function time ratio to the master baseline: programs/clickhouse ×0.61, programs/clickhouse-keeper ×0.72 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.
| Binary | Function | Master | PR | Δ vs median |
|---|---|---|---|---|
programs/clickhouse |
DB::registerFunctionConversion(DB::FunctionFactory&) |
6.0 s | 11.8 s | +8.1 s (+221%) |
programs/clickhouse |
_ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… |
3.5 s | 7.4 s | +5.3 s (+248%) |
Compile time of recompiled translation units
1224 translation units recompiled, 8758 s compile time in total, 1222 of them have a recent master baseline.
…onfiguration `beforeStartingThreads` ran before `ConfigProcessor::savePreprocessedConfig`, which has two consequences: - The encrypted elements of the configuration were not decrypted yet, and loading all server settings to read `filesystem_isolation` failed to parse the ciphertext of `max_table_size_to_drop` in `test_config_decryption`, even with the isolation disabled. - `savePreprocessedConfig` writes into the `path` of the configuration file, not the one overridden on the command line, and that directory was already hidden: the stateless tests failed to start the server with `Read-only file system ["/var/lib/clickhouse/preprocessed_configs"]`. Now the hook runs after `savePreprocessedConfig` (and before `Jemalloc::setup`, as before), and it reads only the `filesystem_isolation` setting instead of loading all server settings. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=228464365d153278ad73b68c1943b9fdd6f50ede&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=228464365d153278ad73b68c1943b9fdd6f50ede&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20targeted%29 #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…state directory visible - For a file from the configuration, such as a certificate, also make visible the directories of the targets of its chain of symbolic links: a Let's Encrypt `live/<domain>/fullchain.pem` points to `../../archive/<domain>/fullchain<N>.pem`. - Make visible the directory of the Keeper state file, derived as in `KeeperContext::getStatePathFromConfig`: without `storage_path` and `state_storage_disk`, the parent of `snapshot_storage_path` or of `log_storage_path`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s, fix the style check Do not make the paths from `TZDIR`, `SSL_CERT_FILE` and `SSL_CERT_DIR` visible: they do not need to be supported. Use `FS::isSymlinkNoThrow` and `FS::readSymlink` instead of `std::filesystem::is_symlink` and `std::filesystem::read_symlink`, which the style check forbids. Clarify in the documentation of `filesystem_isolation` that the watchdog runs inside the isolation, and which processes `/proc` still exposes. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=cdf8576cf820ecc1193d4e94fd37f7f002e8b130&name_0=PR&name_1=Style%20check #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fig targets, default named collection storage type - Follow the symbolic links of the main configuration file, of `users_config` and of the files merged into them from `<name>.d` and `conf.d`, so that a reload does not find dangling links to targets outside the configuration directory. - Enumerate all the entries of `user_directories` with the same type names as `AccessControl::addStoragesFromUserDirectoriesConfig`, including `local_directory[N]`. - Use `local` as the default type of `named_collections_storage`, as `NamedCollectionsMetadataStorage::create` does. - Keep the directory of jemalloc's `opt.prof_prefix` visible and writable, so that heap profiles (`system.jemalloc_profile_text`, `SYSTEM JEMALLOC FLUSH PROFILE`) can be written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lesystem - Mount a new instance of `devpts` at `/dev/pts` with `/dev/ptmx` linked to it, so that the web terminal and the SSH server can open pseudoterminals (`posix_openpt`). The probe in the forked child checks that it can be mounted. - Recreate the symbolic links next to the mapped libraries that name them (SONAME links such as `libc.so.6 -> libc-2.31.so`), so that a new process of a dynamically linked executable, such as the ODBC bridge or the OOM canary, finds its libraries. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… isolation Tests such as `02051_symlinks_to_user_files`, `02455_one_row_from_csv_memory_usage` and `05042_glob_globstar_path_virtual_column` link files in the tests directory and in `CLICKHOUSE_TMP` from `user_files`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=c505781d14f42df7521069f2d54d6aec86aa6c24&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29 PR: #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged
I also addressed the bot review: the targets of symbolic links among config files, every |
…in the functional tests With `DBReplicated`, the replicas share `jemalloc_profiles`, and every start removed and recreated it with `Utils.clean_dir`. A replica that was already running isolates its filesystem (`filesystem_isolation`), so it keeps a bind mount of the directory it saw at startup, which was then deleted, and jemalloc could not create its heap profiles there. `system.jemalloc_profile_text` and the other tests reading the profiles failed with `FILE_DOESNT_EXIST`. Empty the directory in place instead. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=5b4b97bf799708460a7424221e4ff95f0dde25f2&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20s3%20storage%2C%20DBReplicated%2C%20parallel%2C%201%2F2%29 #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e filesystem The runtime of the thread sanitizer starts its background thread in the child of every `fork`, even if the parent did not have one, and it cannot be stopped then (`__sanitizer_sandbox_on_notify` stops it only if the parent started it). The process that `--daemon` forks therefore has two threads before the isolation, and the server aborted with a `LOGICAL_ERROR` before creating its log, so the stress tests with TSan could not start it. A mount namespace can be entered with this thread, and a user namespace cannot, which the probe in a child reports. Reproduced locally with the `arm_tsan` binary of the CI and `clickhouse server --daemon`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=5b4b97bf799708460a7424221e4ff95f0dde25f2&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…'s `include_from`, local object storage locations and SSH host keys visible - With `allow_experimental_webassembly_udf`, `WasmModuleManager` creates and writes the subdirectory `wasm` of `user_scripts_path`, which is otherwise mounted read-only. Keep it read-write. - A users configuration in a separate file is processed by its own `ConfigProcessor`, which reads its own `include_from`. Load it the same way and keep that substitutions file visible. - An object storage disk with `locations` creates a separate object storage for each of them; keep the `path` of the local ones. - Keep `ssh_server.host_rsa_key`, `ssh_server.host_ecdsa_key` and `ssh_server.host_ed25519_key` visible, as the TLS keys are. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…user_scripts_path` before the isolation A link in a CA directory through a directory that is a link back into it, as `0123abcd.0 -> /opt/ca-link/root.pem` with `/opt/ca-link -> /srv/ca`, was skipped because its target is inside the CA directory, so `/opt/ca-link` was not recreated and the link was dangling. Now only a location that is inside the directory as the link names it is skipped. The tests `05636` and `05637` now read the certificates inside the isolated server, with `file` in a startup script, because an absolute link seen from outside, through `/proc/<pid>/root`, leads to the root of the observer, and `05636` covers this case. A `user_scripts_path` that did not exist at the startup, as in a new data directory, was not mounted, so the scripts added later were on the `noexec` mount of `path` and could not run. Now it is created before the isolation. The new test `05638_filesystem_isolation_user_scripts_created` checks that it is a read-only executable mount of its own. #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… leads outside A link in a CA directory whose target is named inside the directory but leads outside through a link to a directory and `..`, as `0123abcd.0 -> nested/via/../root.pem` with `nested/via -> /opt/trust/active`, was skipped as inside, so `/opt/trust` was hidden. Now a target is skipped only if it is inside the directory both as the link names it and with the links resolved; otherwise `resolvePath` follows it as the kernel does. `05636` covers this layout, read inside the isolated server. #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…`backups.allowed_path` as the backup engine does A relative link in a CA directory that leaves it with `..` and comes back through a link, as `0123abcd.0 -> ../current/root.pem` with `current -> ca`, was skipped as inside the directory, so `current` was not recreated. The decision by the spelling of the path cannot be right for every layout, so now every target location is kept as the link names it, and `resolvePath` recreates the links on the way; a location that is visible anyway is dropped by `planMounts`. `05636` covers this layout too. `backups.allowed_path` is resolved by the `File` backup engine relative to `path` and normalized lexically, before the links are followed, so with `path` a link to another directory, `../backups` is next to the link. The collector followed the link first, and the real directory of the backups was hidden. Now it resolves the path as the backup engine does. The new test `05639_filesystem_isolation_backup_allowed_path` makes a backup with such a `path`. #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lative one resolved `getCanonicalPath` resolves the links of a relative `path` (`weakly_canonical` relative to the original working directory) and keeps an absolute one as it is written, and the paths relative to it, as `backups.allowed_path` in the `File` backup engine, are resolved against that. The collector kept a relative `path` as written, so with `path` `data-link/`, a link to `/srv/storage/data`, `../backups` was `/srv/work/backups` instead of `/srv/storage/backups`, which was hidden. Now the collector makes `path` the same way. `05639_filesystem_isolation_backup_allowed_path` covers a relative `path` that is a link, next to an absolute one. #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the server inside `Server::main` resolves `path` again with `getCanonicalPath` after the isolation, so for a relative `path` with a link and `..`, as `link/../data/`, the link has to be in the isolated filesystem, but the collector mounted only the canonical path since `getDataPath`, and the server opened `work/data/status` instead of `storage/data/status`. Now `path` as it is written, relative to the original working directory, goes through `resolvePath` too, which recreates its links, and the canonical one stays the base of the paths relative to `path`, as `backups.allowed_path`. `05628_filesystem_isolation_symlink_parent` covers the first, `05639` the second. #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…th glibc `getExecutableDependencies` and the functions it uses are defined only with glibc, but it was called unconditionally. A build with musl is linked statically, as the programs it starts are, so there is nothing to collect there. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128412&sha=bef29e937e18176e1338e4cfeb23ea416afaf789&name_0=PR&name_1=Build%20%28amd_musl%29 #128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`clickhouse-test` gives every process a profile path relative to its
working directory (`clickhouse-client-proc{N}-%c%2m.profraw`, continuous
mode). The test starts its own server from `$DIR/work`, so in a coverage
build the profiling runtime creates the profile there and maps it
(`MAP_SHARED`) before `main`. Filesystem isolation keeps every file mapped
into the process visible, read-only, so the listing of the working
directory inside the server's root printed the profile next to
`definitions`, and the test failed in every `amd_llvm_coverage` stateless
job on master since ClickHouse#128412 (the PR's own CI has no coverage jobs).
Leave `*.profraw` out of that listing. Everything else of the working
directory, such as `secret`, must still be invisible: with the working
directory made visible through `access_control_path`, the changed test
still fails with `working directory: definitions secret`.
Validated as root with the CI binaries of 52d3d52: coverage, unchanged
test FAIL 3/3 with the CI line, changed test PASS 10/10; amd_release PASS
10/10, and the unchanged test passes there too.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@Algunenano |
Fix for what? |
For this security issue: https://github.com/ClickHouse/clickhouse-private/issues/89131 We also have bunch of issues by groenai and clickgap, they could be also resolved by just reverting this change. Actually I am not sure whether we need this change at all |
|
@alexbakharew The security issue is about hardening this feature even more. The issues from groeneai and clickgap are minor. The default (no setting) is disabled, although the default configf.xml sets it as best effort. This is an inconsistency we should address @alexey-milovidov
We do |
In Kubernetes, the server with the isolated filesystem could not read the token of the service account for the IAM roles of EKS: `Can't open token file: /var/run/secrets/eks.amazonaws.com/serviceaccount/token`. Keep visible, read-only: `/run/secrets` and `/var/run/secrets`, where Kubernetes projects the tokens of the service accounts and Docker mounts its secrets, the files named by `AWS_WEB_IDENTITY_TOKEN_FILE`, `AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE`, `AWS_CONFIG_FILE`, `AWS_SHARED_CREDENTIALS_FILE` and `AZURE_FEDERATED_TOKEN_FILE`, and `~/.aws`. The directories of the files and of the targets of the links are mounted, so that a token that the kubelet rotates (by pointing `..data` at a new directory) is seen. Related: ClickHouse#128412 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The server can now confine itself to a private view of the filesystem at startup, like a container: a new mount namespace whose root is an empty read-only
tmpfs, with only the paths it needs bind-mounted at their usual locations, and the original root removed withpivot_root. An attacker who gets code execution in the server process finds no shell or other program to run, and no files of the host outside the server's own directories. This complements theseccompfilter, which is installed right after and refuses the system calls that would undo the isolation (mount,pivot_root,unshare,setns, ...).The set of visible paths is derived from the configuration:
path,tmp_path,user_files_path, the disks ofstorage_configuration, Keeper storage, logs, pid file, the configuration directory, included files and the targets of symbolic links among the configuration files,users_config,user_scripts_path(read-only and executable), TLS certificates and keys (their directories, so renewed certificates are seen), Kerberos keytab,clickhouse-odbc-bridge, plus the executable with its loader and the shared libraries mapped into the process (with the SONAME links that name them),/proc, a read-only/sys,/dev/{null,zero,full,random,urandom}, a privatedevptsinstance at/dev/ptswith/dev/ptmx(for the web terminal and the SSH server), the directory of jemalloc'sprof_prefix, the network/timezone/CA files of/etcand/usr/share, theNOTIFY_SOCKETof systemd, and a private empty/tmp. Writable mounts arenoexec, all mounts arenosuid. Extra paths go into<filesystem_isolation_paths>with<read_only>/<read_write>- needed, for example, for the interpreters of executable UDF scripts and for ODBC drivers.BaseDaemon::beforeStartingThreadshook, right after the configuration is loaded and before any thread is started (a mount namespace is per thread, and a multithreaded process cannot enter a user namespace). The background threads of jemalloc, started by its compiled-inbackground_thread:true, are joined first and restarted byJemalloc::setupright after. A process with more than one thread at that point is aLOGICAL_ERRORexception.CAP_SYS_ADMIN(e.g. root, orCAP_SYS_ADMINgranted by the systemd unit), only a mount namespace is created and the process keeps the capabilities it uses (CAP_NET_BIND_SERVICE,CAP_IPC_LOCK,CAP_SYS_NICE,CAP_NET_ADMIN, ...), dropping those that could undo the isolation (CAP_SYS_ADMIN,CAP_SYS_CHROOT,CAP_SYS_PTRACE,CAP_DAC_READ_SEARCH,CAP_SETUID,CAP_SETGID,CAP_MKNOD, ...). Without it, a user namespace mapping only the current user and group is created as well, and the capabilities of the initial namespace are lost - a warning names them.PR_SET_NO_NEW_PRIVSis set in both cases.best_effortdoes not leave the server in a user namespace without its capabilities when, e.g., Ubuntu'skernel.apparmor_restrict_unprivileged_usernsor Docker refuses it. Any failure after the server has entered the namespace stops the startup in both modes.disabled(the code default, so existing configurations do not change behavior on upgrade),best_effort(the shippedconfig.xml),enforce.system.server_settingsreports the mode in force; a changed value in a reloaded configuration logs a warning, as forseccomp./procof the host stays visible (a fresh one requires a PID namespace, which needs a fork and changes the main PID that systemd and the watchdog rely on), so/proc/<pid>/rootof other processes of the same user outside the isolation is reachable. This is documented; a PID namespace could be a follow-up.Tests: the stateless tests run with
enforce(they run as root in a privileged container) and with/bin,/usr,/lib*,/etcvisible for the executable dictionaries and UDFs that run shell commands; a new test checks that a program run by the server sees a read-onlytmpfsroot without the original root. The integration tests strip the element from the main config and keep the isolation disabled, since whether it applies depends on the container and it would hide the ODBC drivers and interpreters many of them use.Manually verified on Linux 7.0 aarch64: as root (mount namespace path) the server starts, only the planned paths exist under
/proc/<pid>/root,/is read-only,/bin/shis absent (a shell UDF fails), with the test paths it works again,CapEfflacks the dropped capabilities,NoNewPrivsis 1,seccompis applied on top; as an unprivileged user on a host withkernel.apparmor_restrict_unprivileged_userns = 1,best_effortlogs a warning and runs without the isolation. The unprivileged user namespace path has not been exercised at runtime yet.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
New server setting
filesystem_isolation: at startup, the server moves into a new mount namespace where only the paths it needs according to its configuration are visible - in particular, no shell - which makes exploitation of a remote code execution harder. It is enabled asbest_effortin the defaultconfig.xml; extra paths, such as interpreters for executable user defined functions, can be listed infilesystem_isolation_paths.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128412]
Version info
26.11.1.2324-master(included in26.11and later)