Skip to content

load: Lock down spawn kernels, with a per-reason deny list - #22

Merged
congwang-mk merged 8 commits into
mainfrom
spawn-lockdown
Nov 24, 2026
Merged

congwang-mk merged 8 commits into
mainfrom
spawn-lockdown

Conversation

@congwang-mk

@congwang-mk congwang-mk commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A spawn can now be booted in kernel lockdown mode, and the host admin can choose which confidentiality restrictions apply to it:

kerf load web --kernel=vmlinux.signed --image=nginx:latest \
         --lockdown=integrity --deny=kcore,xfrm-secret

The kernel's lockdown LSM has two levels and nothing finer. --deny gets per-reason selection without patching it: a BPF LSM program on the locked_down hook denies the named reasons on top of lockdown=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|confidentiality appends lockdown= to the spawn's command line. --deny=<names> additionally passes the names to kerf-init in kerf.deny=. Requests that could not be enforced are refused before anything is extracted or allocated: a conflicting lockdown= or an lsm= list without lockdown/bpf in --cmdline, --deny without --image/--rootfs-dir, or --deny with --initrd (the initrd would run before the deny list is in force). kerf never writes lsm= itself.
  • kerf-init enforces the request before it starts the entrypoint or accepts an exec session. It checks the level the kernel is actually at in securityfs, requires the BPF LSM to be active, resolves reason values, the two hook ids and the offset of task_struct.tgid from /sys/kernel/btf/vmlinux, and loads and attaches two programs with raw bpf(2): the deny list, and a guard on ptrace_access_check that 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.
  • The programs are 10 and 9 fixed instructions with three numbers filled in from BTF, so they are written out by kerf-init instead of compiled. No clang, no libbpf, no BPF object in the package, and the same kerf-init works on any spawn kernel. The BTF reader is about 300 lines and handles the layout section and new kinds that newer kernels add.
  • kerf-init now closes inherited file descriptors and mounts securityfs for every spawn.
  • kerf show prints the requested level and deny list.

Spawn kernel requirements for --deny: CONFIG_SECURITY_LOCKDOWN_LSM, CONFIG_BPF_LSM, CONFIG_DEBUG_INFO_BTF, bpf in the active LSM list, and CONFIG_FUNCTION_TRACER with CONFIG_DYNAMIC_FTRACE. The last two are not Kconfig dependencies of BPF_LSM, but without them the hook functions have no patchable entry and the attach fails (EBUSY on 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 byte
  • BTF reader agrees with bpftool (hook ids) and pahole (tgid offset) on real kernels
  • In a VM, real spawn with kerf-init as PID 1 (rdinit=, from an initramfs) and lockdown=integrity kerf.deny=kcore,xfrm-secret: /proc/kcore and writes to /proc/1/mem refused as root, /dev/mem refused by integrity, tracefs and kprobe events still usable, ps and /proc/1/exe still readable. Control spawn with integrity only: /proc/kcore readable, /proc/1/mem writable
  • Five bad requests each stop the spawn with the right message: unknown reason, integrity reason, lsm= without bpf, lsm= without lockdown, kerf.deny= without lockdown=
  • kerf load --lockdown=confidentiality in the VM: parameter appended, kerf show row, spawn at confidentiality; four invalid requests rejected with exit code 2
  • --deny through --image not run end to end: the VM's spawn kernel has no daxfs, so kerf-init was started from an initramfs with a hand-written kerf.deny=
  • The xfrm-secret denial itself not exercised (no IPsec state in the test spawn)
  • arm64 not tested beyond what CI builds and runs; no program was loaded into an arm64 kernel
  • Residual risk: root in the spawn keeps the BPF syscall under integrity mode. I know of no BPF facility that undoes a denial or reaches PID 1's fds, but that is reasoning, not an audit

🤖 Generated with Claude Code

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>
--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
congwang-mk merged commit 66781aa into main Nov 24, 2026
8 checks passed
@congwang-mk
congwang-mk deleted the spawn-lockdown branch November 24, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant