Repository navigation
Apply a seccomp system call policy to clickhouse-server at startup - #119545
Conversation
The server installs a seccomp-BPF filter on itself, allowing only the system calls it is known to use, so that code execution inside the server process does not come with the kernel interfaces that turn it into a host compromise. The policy refuses loading kernel modules, rebooting, mounting and the new mount API, `chroot`, changing the process identity, creating namespaces, `ptrace` and cross-process memory access, eBPF, the kernel keyring, `userfaultfd` and `vmsplice`, file handles, `fanotify`, swap and quota control, setting the system clock and the host name, System V and POSIX IPC, and the extended-attribute calls. `ioctl` is allowed except for `TIOCSTI` and `TIOCLINUX`, the two requests that let a process holding a terminal take over the session that owns it. The server also sets `PR_SET_NO_NEW_PRIVS`, so neither it nor anything it starts can gain privileges by running a setuid program. Within each group it keeps, the allow-list is deliberately generous: an older or a newer spelling of an allowed call (`open` next to `openat`, `clone` next to `clone3`) is allowed too, because which one is used depends on the libc version and on the running kernel rather than on anything ClickHouse decides. The two largest pieces of surface that remain are `perf_event_open` and the `io_uring` family, and both are there because ClickHouse uses them - for `metrics_perf_events_enabled` and for `local_filesystem_read_method = io_uring`. The new `seccomp` server setting chooses what a call outside the policy does: `trap` (the default) raises `SIGSYS`, which the fatal signal handler reports with the system call number and a stack trace before the server terminates; `kill`, `errno` and `log` are also available, and `disabled` installs no filter. `log` blocks nothing and only records, which is the mode to run first when checking the policy against an unusual workload. The BPF program is generated as a binary search over merged runs of allowed numbers, so it costs about a dozen instructions per system call rather than a few hundred. Before the kernel is asked to enforce it, `verifyProgram` runs the program through a small interpreter and checks it against the table it was generated from, for every number the kernel could produce plus the x32 and foreign-ABI cases, so a mistake in the generator fails loudly at startup instead of opening a hole or taking the server down on an ordinary system call. The filter is installed with `SECCOMP_FILTER_FLAG_TSYNC`, so it covers the threads that already exist, and with `SECCOMP_FILTER_FLAG_SPEC_ALLOW` where the kernel supports it: without that flag a seccomp filter makes the kernel force the Speculative Store Bypass mitigation on the process for the rest of its life, which costs throughput on every query. Flag support is established with the documented null-program probe rather than by trial and error. A filter cannot be removed or relaxed once installed and is inherited across both `fork` and `execve`, so executable dictionaries and user defined functions, the bridges, and the OOM canary run under the same policy. Only x86-64 and AArch64 are covered; elsewhere the server logs a warning at startup and runs without a filter. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [931446c] Summary: ✅
AI ReviewSummaryThis PR adds a self-installed Findings
Final VerdictLLVM Coverage ReportMeasured on commit 931446c.
Changed lines: Changed C/C++ lines covered: 360/428 (84.11%) · Uncovered code |
`clang-tidy`'s `google-runtime-int` refuses the `long` that `syscall` returns. 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 Object file sizes11 object files changed (+202.75 KiB total), 1 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 units1913 translation units recompiled, 11455 s compile time in total, 1912 of them have a recent master baseline. |
…t cover The loongarch64 sysroot has no `linux/audit.h`, and the includes sat outside the architecture condition, so the build failed there instead of compiling the stub. Both branches now compile under `-Weverything -Werror`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Address review: the policy said it refuses namespace creation, but it only refused `unshare` and `setns`. `clone` creates one just as well, by naming it among its flags, so a compromised server could still reach a user namespace - the usual step from code execution inside a process to the kernel. `clone` is now decided by its flags rather than by its number alone: a call carrying any of the seven `CLONE_NEW*` bits is refused, and everything a libc passes for a thread, for `fork` and for `posix_spawn` is allowed. The kernel takes only the low half of that argument, so matching on the low half - which comes first on a little-endian machine - is exact. `clone3` cannot be inspected at all: what it is asked to do lives in a structure in the calling process's memory, which a filter cannot read, so a namespace cannot be told from a thread. It is therefore refused as a whole, with `ENOSYS` rather than with the configured action. `ENOSYS` is how a libc discovers that the kernel it runs on has no `clone3` and falls back to `clone`, whose flags the filter can see; a recent glibc asks once and remembers the answer, so this costs one refused system call in a process rather than one per thread. `docker` and `systemd` refuse `clone3` the same way in their own policies. In the `log` mode, where nothing is refused, `clone3` gets that mode's own action like everything else outside the policy. The two checks that need an argument now sit after the binary search instead of ahead of it, next to the `ioctl` one, so that looking at an argument is paid for by the call whose argument it is rather than by every system call the server makes. Measured on AArch64 with the program the policy generates: an allowed `read` takes 16 instructions and `futex` 14, against 15 and 15 before, and `clone` and `ioctl` 19. The test asserts through the executable user defined function that already probes the policy from a forked process that a `clone` asking for a user namespace is refused, that `clone3` answers `ENOSYS`, and that making a thread still works. The flags it passes name a combination the kernel rejects on its own with `EINVAL`, so the probe creates nothing when the policy does not refuse it.
Address review, two ways in which the setting did not do what it said. On a Linux architecture other than x86-64 or AArch64 there is no policy to install, and `installSeccompFilter` did nothing at all there - including `prctl(PR_SET_NO_NEW_PRIVS)`, which the setting documents as part of the feature and which does not depend on the architecture. Executable user defined functions and dictionaries, the bridges and the OOM canary could still gain privileges through a setuid binary or one carrying file capabilities on those builds. It is now applied there as well, in every mode but `disabled`, and only the filter itself is left to the architectures that have a policy. The warning at startup says so. The `log` mode said it enforces no policy at all, while it did set `PR_SET_NO_NEW_PRIVS` - the kernel asks for that before it accepts a filter, and a filter is what `log` installs, so it cannot be skipped. The setting documentation and the comments now say that `log` refuses no system call but does set `PR_SET_NO_NEW_PRIVS`, which is the one thing it has in common with the enforcing modes. The test gets a `log` node and asserts both halves of that: the filter and `NoNewPrivs` are there, and the probe from a forked process sees exactly what it sees without a filter - a missing extended attribute rather than `EPERM`, and a `clone3` the kernel gets to look at rather than `ENOSYS`.
The probe prints a tab-separated row, and the test unpacks it into five names, so a message with a space in it would fail the test with a confusing unpacking error rather than with the assertion that means something.
The two architecture branches each declared `SYSTEM_ERROR`. The duplicate-`ErrorCodes` style check reads the file as text and has no idea that the branches are mutually exclusive, so it reported a duplicate declaration. Declare both codes once, above the branches. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…y denies The seccomp filter is installed early in startup, well before the integrity check of the executable, and `ptrace` is outside the policy. The debugger probe of that check therefore stopped working in every enforcing mode: `trap` and `kill` take the server down on the `ptrace` itself, and `errno` turns it into `EPERM`, which the check reads as "running under debugger" - so a real checksum mismatch was reported as a modified binary under a debugger instead of raising `CORRUPTED_DATA`. Read `TracerPid` from `/proc/self/status` instead. It is a plain read of a file the server may open anyway, so it answers the same way whatever the `seccomp` setting denies. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Defaulting a brand-new process-wide deny-list to `trap` would start enforcing `SIGSYS` on upgrade, before an operator has had any chance to run their workload under the policy. The `log` mode exists exactly for that validation, so make it the default: the filter is installed, the kernel records every system call the policy does not cover, and nothing is refused. Turning the setting to `trap`, `kill` or `errno` stays a deliberate step. The integration test gains a `trap_node` so the enforcing mode keeps its coverage, and a test that the integrity check of the executable runs to the end under the filter in every mode. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Both reds on
@groeneai, neither has landed on The branch has since been brought up to |
|
🕵 On the AArch64 coverage point from the AI review: there is already AArch64 runtime evidence for this policy. That is the interesting configuration for the hard-coded generic numbers the review lists ( On top of that, |
|
#120932 is green and ready. Its PR workflow on head You do not have to wait for it to land to get a clean run on this branch, though: |
|
Thanks @alexey-milovidov for letting us continue reviewing active ClickHouse changes. Veridical.dev reviewed the change
2 surfaced items · 2 with source evidence Supported defect
The policy permits Normal file/device permissions, capabilities, and LSM policy still apply. This is a containment gap, not an independent privilege escalation. 🔍 Evidence and suggested directionThe allowlist admits the full Register a minimal opcode set with Existing bot finding correctedThe Review scopeSource-only Ultra review of the exact revision above. Existing bot and human discussion was included for deduplication. No build or runtime execution was performed. Reviewed by Veridical · ultra depth · source-only, no build |
`test_binary_integrity_check_survives_the_filter` failed in every integration job: the common configuration of the integration tests sets `skip_binary_checksum_checks`, so the check never ran and neither of the two log lines the test waits for was written. The nodes it looks at now turn the setting back off. Also assert `NoNewPrivs: 1` on every thread, not only `Seccomp: 2`: the review worried that `PR_SET_NO_NEW_PRIVS` stays on the installing thread, but `TSYNC` carries it over together with the filter (`seccomp_sync_threads` calls `task_set_no_new_privs` for every sibling), and the test now pins it. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119545&sha=4165cf5f33ff6717555c67d17948305a89288087&name_0=PR #119545 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…m starting With `seccomp` defaulting to `log`, a server that used to start on a kernel older than Linux 4.14, one built without `CONFIG_SECCOMP_FILTER`, or inside a container runtime whose own policy refuses the `seccomp` system call, would now fail at startup with `Cannot install the seccomp filter` - an outage caused by a mode that enforces nothing. `installSeccompFilter` now asks up front whether the requested action is implemented (`/proc/sys/kernel/seccomp/actions_avail`) and whether a filter can be installed at all (the null-program probe, which gives `EFAULT` only when it can). If not, the `log` mode sets `PR_SET_NO_NEW_PRIVS`, returns the reason, and the server logs a warning and runs without a filter. The enforcing modes still refuse to start, now with the reason in the message. The function returns a `SeccompFilterStatus` instead of a bare count, so the warning names the actual reason rather than always blaming the architecture. The new `test_log_mode_starts_where_seccomp_is_unavailable` starts a second server under an outer filter that refuses `seccomp` with `EPERM`, the way a restrictive runtime does. Checked locally on AArch64 with the binary of the previous head (fails to start with `Code: 425`) and with this change (starts with the warning, answers queries, `NoNewPrivs: 1` on all 169 threads); `errno` under the same outer filter still refuses to start. #119545 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…o_uring` The `io_uring` system calls are allowed for `local_filesystem_read_method = io_uring`, and a ring lets whoever can make one reach the operations `io_uring` implements - the extended-attribute ones, for example - even though their system calls are refused. `IORING_REGISTER_RESTRICTIONS` on our own ring would not close that, since a second, unrestricted ring can be made; refusing `io_uring_setup` would, at the price of the read method. State the gap in the setting documentation and next to the policy instead of claiming more containment than there is. Raised in the review at #119545 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Pushed 8d8318d with a merge of
|
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=119545&sha=8d8318d32e2bee7975d30d270a261708319966ba&name_0=PR&name_1=Stress%20test%20(arm_release) and provide a fix in a separate PR. If the fix is already in progress, link it here.
|
|
Acknowledged. Nothing to link yet: #120940 was closed unmerged, so no fixing PR is open for this. Opening the change you named: checkpoint/ |
…se device nodes in `mknod` The filter was installed from the local configuration, before `loadConfigWithZooKeeperIncludes` and the reload of the server settings that follows it. A `seccomp` value from `from_zk` then showed in `system.server_settings` while the filter in force was the one for the local value - and a filter cannot be replaced afterwards. The filter is now installed right after that reload. `mknodat` (and the legacy `mknod` on x86-64) were allowed unconditionally, which also allows making character and block device nodes; with `CAP_MKNOD` that reaches any device through the allowed `openat` and `ioctl`. The filter now looks at the `S_IFMT` bits of the mode and refuses `S_IFCHR` and `S_IFBLK`. FIFOs, which executable user defined functions use, regular files and sockets are still allowed - the server can make those with `openat` and `bind` anyway. `verifyProgram` checks both, including junk in the ignored bits of the mode. `test_seccomp` gets a node whose `seccomp` setting comes from ZooKeeper and must end up enforcing, and the probe script checks that `mknod` of a device is refused while `mkfifo` works. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
#121898, as instructed: checkpoint/ The description carries one question for you: the collected values are now consistent, but the filter can still be partial through paths this change does not touch. I left that fence out, since it is what #120940 attempted. |
…n force after a config reload The filter is installed once at startup and cannot be installed, removed or relaxed afterwards, but `SYSTEM RELOAD CONFIG` and the automatic reload rebuild `system.server_settings` from the new configuration, so a changed `seccomp` there was reported as if it were in force. `installSeccompFilter` now remembers the mode it was called with, and `collectChangeableServerSettings` reports that mode for `seccomp` (with `changeable_without_restart = No`) instead of the configured one. A reload that changes the setting logs a warning that it takes effect only after a restart. `test_reloaded_setting_does_not_replace_the_one_in_force` covers both directions (`disabled` -> `errno` and `errno` -> `disabled`). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Pushed 931446c on top of a merge of
|
|
|
||
| /// The seccomp filter is installed once at startup and cannot be changed afterwards, so a reloaded | ||
| /// configuration that says otherwise does not describe the policy in force. | ||
| if (const auto seccomp_mode = getInstalledSeccompMode()) |
There was a problem hiding this comment.
collectChangeableServerSettings now treats getInstalledSeccompMode as "the policy in force", but that value is set before installSeccompFilter knows whether a filter was actually installed. On the startup fallback paths (log where the seccomp system call is unavailable, or any unsupported architecture), system.server_settings will still report seccomp = log|trap|... even though the process is running without a seccomp filter and only PR_SET_NO_NEW_PRIVS was applied. That recreates the same false-security-state bug the PR just fixed for SYSTEM RELOAD CONFIG, only on the startup-unavailable path.
Can we preserve/report installation success here instead of only the requested mode?
|
Next step: set it to |
The setting keeps its `log` default, but the `config.xml` shipped with the packages and the Docker image now enforces the policy. It works in Docker with the default seccomp profile: the server's filter stacks on top of the runtime's. Related: ClickHouse#119545 (comment) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
clickhouse-servernow applies aseccompsystem call filter to itself at startup, allowing onlythe system calls it is known to use.
The motivation is containment: an attacker who reaches code execution inside the server process
should not also inherit the kernel interfaces that turn that into a host compromise. The policy
refuses everything a healthy server never needs - loading kernel modules, rebooting, mounting and
the whole new mount API,
chroot, changing the process identity, creating namespaces,ptraceandcross-process memory access, eBPF, the kernel keyring,
userfaultfdandvmsplice, file handles,fanotify, swap and quota control, setting the system clock and the host name, System V and POSIXIPC, and the extended-attribute calls.
ioctlis allowed except forTIOCSTIandTIOCLINUX, thetwo requests that let a process holding a terminal take over the session that owns it. In every
mode but
disabledthe server also setsPR_SET_NO_NEW_PRIVS, so neither it nor anything itstarts can gain privileges by running a setuid program.
The allow-list is generous within each group it keeps: an older or newer spelling of a call that
is allowed anyway (
opennext toopenat,forknext toclone,epoll_waitnext toepoll_pwait2) is allowed too, because which one gets used depends on the libc version and on therunning kernel rather than on anything ClickHouse decides. The two largest pieces of surface that
remain are
perf_event_openand theio_uringfamily, and both are there because ClickHousegenuinely uses them - for
metrics_perf_events_enabledand forlocal_filesystem_read_method = io_uring. The filter does not see the operations submitted through anio_uringring, so the operations
io_uringimplements stay reachable that way; the documentation says so.The new
seccompserver setting chooses what happens to a call outside the policy:trapturns itinto
SIGSYS, which the existing fatal signal handler reports with the system call number and astack trace before the server terminates;
killhas the kernel kill the process outright;errnofails the call with
EPERMand lets the server run on;logrefuses nothing and only records; anddisabledinstalls no filter at all. The kernel records every refused call in its audit log in allof the enforcing modes, which is the only evidence left behind under
kill.The default is
log, so that this first release enforces nothing: the filter is installed and thekernel records every system call the policy does not cover, which is how the policy gets checked
against a real workload before anyone has to trust it.
logdoes still setPR_SET_NO_NEW_PRIVS,because the kernel asks for that before it accepts a filter at all. Turning the setting to
trap,killorerrnois a deliberate step, and an upgrade does not take it for you. Where the kernel cannot install alogfilter (older than Linux 4.14, noCONFIG_SECCOMP_FILTER, or an outer sandbox that refuses theseccompsystem call), thelogmode warns with the reason and runs without a filter, since it would have enforced nothing; the
enforcing modes refuse to start instead.
Implementation notes:
allowed numbers, so that it costs about a dozen instructions per system call rather than a few
hundred - it runs on every system call the server makes. The calls whose fate takes more than
their number to decide are checked after the search rather than ahead of it, so looking at an
argument is paid for by the call whose argument it is.
unshareand
setnsout of the list. Aclonecarrying any of the sevenCLONE_NEW*flags is refused byits flags - the kernel looks at the low half of that argument only, so matching on the low half
is exact - while the flags a libc passes for a thread, for
forkand forposix_spawnareallowed.
clone3cannot be inspected at all, since what it is asked to do lives in a structurein the calling process's memory, so it is refused as a whole, with
ENOSYSrather than with theconfigured action:
ENOSYSis how a libc discovers that the kernel has noclone3and fallsback to
clone, whose flags the filter can see. A recent glibc asks once and remembers theanswer, so this costs one refused system call in a process rather than one per thread.
dockerand
systemdrefuseclone3the same way in their own policies.verifyProgramand checked against the table it was generated from, for every number the kernelcould produce plus the x32 and foreign-ABI cases, the
ioctlrequests and thecloneflags. Amistake in the generator therefore fails loudly at startup instead of either opening a hole or
taking the server down on the first perfectly ordinary system call.
SECCOMP_FILTER_FLAG_TSYNC, so it covers the threads that alreadyexist, and with
SECCOMP_FILTER_FLAG_SPEC_ALLOWwhere the kernel supports it: without that flaga
seccompfilter makes the kernel force the Speculative Store Bypass mitigation on the processfor the rest of its life, which costs throughput on every query. Flag support is established with
the documented null-program probe rather than by trial and error.
forkandexecve, so executable dictionaries and user defined functions, the bridges, and the OOM canaryrun under the same policy. This is asserted by the test.
architecture-specific numbers. Elsewhere the server logs a warning at startup and runs without
one, but still sets
PR_SET_NO_NEW_PRIVS, which does not depend on the architecture.ptrace(PTRACE_TRACEME)failed, andptraceis outside the policy - so under an enforcing modethe check would either take the server down on that call or read the
EPERMas a debugger andhide a real checksum mismatch. It reads
TracerPidfrom/proc/self/statusinstead, which nomode of the setting can change the answer to.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
clickhouse-servernow applies aseccompsystem call filter to itself at startup, allowing only the system calls it uses and refusing the rest - kernel module loading, mounting,chroot, identity changes, namespace creation (including aclonethat asks for one;clone3, whose arguments a filter cannot read, is refused withENOSYS, which sends a libc back toclone),ptrace, eBPF, the kernel keyring,userfaultfd, and others. The newseccompserver setting selects what a refused call does: it defaults tolog, which enforces nothing and only has the kernel record the calls outside the policy, so that the policy can be checked against a workload before it is turned on;trap,killanderrnoenforce it, anddisabledinstalls no filter.Workflow [PR]
Sync PR [sync-upstream/pr/119545]
Version info
26.10.1.837-master(included in26.10and later)