Repository navigation
load: Lock down spawn kernels, with a per-reason deny list - #22
Merged
Merged
Conversation
The kernel locks down at one of two levels, integrity or confidentiality, and offers nothing finer. A BPF LSM program on the locked_down hook can deny individual reasons on top, but only the confidentiality ones are worth selecting: with any integrity reason left open, root in the spawn can remove the program. Add what kerf load needs for a --lockdown and a --deny option: one function that checks the request against the rest of the command and returns the words to put on the spawn's command line, lockdown= for the kernel and kerf.deny= for kerf-init. It refuses a request that could not be enforced rather than accept it and hope: a different lockdown= or an lsm= list without lockdown or bpf in --cmdline, a deny list with no kerf-init to load it, or one behind an initrd that would run first. Also tell whether a kernel image has the lockdown LSM at all, by looking for the message lock_kernel_down() prints, since a kernel without it ignores lockdown= silently. Signed-off-by: Cong Wang <cwang@multikernel.io>
--lockdown=integrity|confidentiality boots the spawn at one of the kernel's two lockdown levels by adding lockdown= to its command line. --deny adds individual confidentiality restrictions on top of integrity, for a spawn that should not read /proc/kcore, say, but still needs perf: their names go to kerf-init in kerf.deny=, which will enforce them with a BPF LSM program. The request is checked before anything is extracted or allocated, so a refusal leaves nothing behind. A kernel image without the lockdown LSM only draws a warning: the check is a string search, and such a kernel boots fine, just not locked down. The request is recorded with the instance for kerf show. Signed-off-by: Cong Wang <cwang@multikernel.io>
Print the level, and the deny list when there is one, with the rest of what kerf load recorded about the kernel image. The row says what was asked for at load; it needs no query of the spawn, because kerf-init does not start the workload unless the request is in force. Records from before the option have no such key and get no row. Signed-off-by: Cong Wang <cwang@multikernel.io>
To deny lockdown reasons with a BPF LSM program, kerf-init needs a few numbers that differ from one kernel build to the next: the value of each reason, the BTF ids of the hooks to attach to, and where tgid sits in task_struct. The running kernel publishes all of them as BTF in /sys/kernel/btf/vmlinux. Add a reader that finds just those. kerf-init is a static musl binary, and pulling in libbpf with libelf and zlib for three lookups is not worth it. Type ids are positions in the type section, so every record has to be stepped over at its right size, including kinds this code has no use for. Newer kernels add kinds and a layout section that says how big each kind's records are; use it when it is there and a built-in table otherwise, and refuse a blob with a kind neither describes instead of guessing ids. lockdown-test runs the reader on a file so the tests can feed it hand-built blobs. Signed-off-by: Cong Wang <cwang@multikernel.io>
Turn the names in kerf.deny= into the two BPF programs that enforce them. The first sits on the locked_down hook and refuses the reasons whose bits are set in a mask. The second sits on ptrace_access_check and refuses attaching to PID 1: the programs live only as long as kerf-init holds their links, and root in the spawn could otherwise ptrace it, or write to /proc/1/mem, to make it let go. Both are a few fixed instructions with three numbers filled in from the kernel's BTF, so they are written out here instead of compiled: no clang at build time, no object file to ship, and the same kerf-init works on any spawn kernel. A name is only accepted if this kernel places it among the confidentiality reasons. An integrity reason enforced by this program alone could be undone through any other integrity hole, so those stay with lockdown=integrity. Nothing loads the programs yet. Signed-off-by: Cong Wang <cwang@multikernel.io>
kerf load only writes the request on the command line: lockdown= for the kernel and kerf.deny= for kerf-init. Whether either took effect is known only inside the spawn. A kernel without the lockdown LSM, or with lockdown left out of its LSM list, ignores lockdown= without a word, and a BPF LSM that is built in but not in that list accepts programs and never calls them. So have kerf-init check, before it starts the entrypoint or accepts an exec session. It reads the level the kernel is actually at from securityfs and requires at least the one asked for. For a deny list it also requires the BPF LSM to be active, resolves the names against the kernel's BTF, and loads and attaches the two programs, the PID 1 guard first so the deny list is never in force without it. The links stay open in PID 1 for the life of the spawn; that type of link cannot be detached any other way. If any of it fails, kerf-init says why and returns, which panics the spawn like its other fatal errors. A spawn that was asked to be locked down does not run open. Also close whatever file descriptors kerf-init inherited, for every spawn: an initrd or loader must not be able to hand the workload an open /proc/kcore. lockdown-test gains a load mode, which checks as root that the running kernel's verifier takes the programs without attaching them, and a setup mode for the command line handling. Signed-off-by: Cong Wang <cwang@multikernel.io>
congwang-mk
force-pushed
the
spawn-lockdown
branch
from
November 20, 2026 01:28
0c32a01 to
39dcbd2
Compare
--lockdown named a handful of what integrity mode restricts and --deny listed bare reason names, so choosing a deny list meant reading the kernel's lockdown.c to learn what each name covers. Print both levels' features after the options, each name with the kernel's own description of it. The confidentiality table is also the list of names --deny accepts. Signed-off-by: Cong Wang <cwang@multikernel.io>
--deny needed kerf-init as the spawn's PID 1, which holds the links that keep the deny list attached, so it was refused without --image or --rootfs-dir and refused with --initrd. With --initrd alone, kerf load now appends kerf-init to the initramfs as one more cpio archive and puts it ahead of the initramfs's own init with rdinit=. kerf-init applies the deny list before anything else runs, pins its link in a bpffs at /.kerf-lockdown, unmounts what it mounted and execs the init named by kerf.init=, which is the user's rdinit= or /init. No process holds the link in this mode, so no guard program is loaded. The pin is an ordinary file: root in the spawn can remove it and lift the deny list. That is the documented limit of this mode, and kerf load and kerf show both say so. --initrd together with --image or --rootfs-dir is allowed as well. kerf-init is PID 1 there once the initramfs has switched root, and the initramfs is trusted, as it runs before the deny list is in force. Signed-off-by: Cong Wang <cwang@multikernel.io>
congwang-mk
force-pushed
the
spawn-lockdown
branch
from
November 24, 2026 03:02
0a77fac to
d00320b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A spawn can now be booted in kernel lockdown mode, and the host admin can choose which confidentiality restrictions apply to it:
The kernel's lockdown LSM has two levels and nothing finer.
--denygets per-reason selection without patching it: a BPF LSM program on thelocked_downhook denies the named reasons on top oflockdown=integrity. Only the 8 confidentiality reasons are selectable. A partial set of integrity reasons could not be held against root in the spawn, since any one left open (unsigned modules,/dev/mem, kexec) is a way to remove the program.kerf load --lockdown=integrity|confidentialityappendslockdown=to the spawn's command line.--deny=<names>additionally passes the names to kerf-init inkerf.deny=. Requests that could not be enforced are refused before anything is extracted or allocated: a conflictinglockdown=or anlsm=list withoutlockdown/bpfin--cmdline,--denywithout--image/--rootfs-dir, or--denywith--initrd(the initrd would run before the deny list is in force). kerf never writeslsm=itself.task_struct.tgidfrom/sys/kernel/btf/vmlinux, and loads and attaches two programs with rawbpf(2): the deny list, and a guard onptrace_access_checkthat refuses attaching to PID 1, which holds the links that keep both programs alive. Any failure logs a specific message and stops the spawn; a spawn asked to be locked down does not run open.kerf showprints the requested level and deny list.Spawn kernel requirements for
--deny:CONFIG_SECURITY_LOCKDOWN_LSM,CONFIG_BPF_LSM,CONFIG_DEBUG_INFO_BTF,bpfin the active LSM list, andCONFIG_FUNCTION_TRACERwithCONFIG_DYNAMIC_FTRACE. The last two are not Kconfig dependencies ofBPF_LSM, but without them the hook functions have no patchable entry and the attach fails (EBUSYon x86). kerf-init reports the attach error and stops the spawn; it does not try to name the cause.Test plan
pytest: 591 passed, 1 skipped, including new tests for host validation, the BTF reader against hand-built blobs, and the emitted instructions byte for bytebpftool(hook ids) andpahole(tgidoffset) on real kernelsrdinit=, from an initramfs) andlockdown=integrity kerf.deny=kcore,xfrm-secret:/proc/kcoreand writes to/proc/1/memrefused as root,/dev/memrefused by integrity, tracefs and kprobe events still usable,psand/proc/1/exestill readable. Control spawn with integrity only:/proc/kcorereadable,/proc/1/memwritablelsm=withoutbpf,lsm=withoutlockdown,kerf.deny=withoutlockdown=kerf load --lockdown=confidentialityin the VM: parameter appended,kerf showrow, spawn at confidentiality; four invalid requests rejected with exit code 2--denythrough--imagenot run end to end: the VM's spawn kernel has no daxfs, so kerf-init was started from an initramfs with a hand-writtenkerf.deny=xfrm-secretdenial itself not exercised (no IPsec state in the test spawn)🤖 Generated with Claude Code