Enforcement in the kernel#
shirudo’s enforcement is a set of BPF LSM programs that shirudo init
attaches early in boot. This page explains what they protect, how they
tell shirudo from root, in which order init attaches them and why,
and ends with a reference table of every hook.
Problem#
A signature gate on program loads is not enough on its own. Root could leave the gate in place and remove what makes it work: unlink the pin that keeps a link attached, unmount the bpffs that holds the pins, write a map that a guard reads, unlink the certificate from the keyring, extend the measurement register with an event of its own choosing, trace the process that holds authority, or boot another kernel by kexec with this boot’s TPM state. Each of these is an ordinary system call that root is allowed to make.
A check in userspace cannot refuse any of them, for the reason Signed loader path gives: root does not have to ask. The refusal has to happen in the kernel, on the call itself. BPF LSM programs do that. Each one runs at an LSM hook, sees the call’s arguments and the calling task, and returns an error that the kernel hands back to the caller.
Two objects#
The binary embeds two BPF objects. bpf2 (bpf/bpf2.c) holds BPF
admission and the guards of shirudo’s own kernel state. seal
(bpf/seal.c) holds the task marks, the process protection and the
checks on writable state. Both share the LSM helpers and the verity
device records of bpf/lib/.
init loads both without a signature, from the binary the
authenticated initrd carries.
The objects share eight maps, which init hands from bpf2 to
seal with bpf_map__reuse_fd() and pins in the private bpffs:
Map |
Pin |
Contents |
|---|---|---|
|
|
The record |
|
|
Task storage with each task’s mark and exec candidate. |
|
|
Task storage with committed policy bits and the exec candidate. |
|
|
Socket storage for committed policy bits, cloned from listeners to accepted sockets. |
|
|
The dm-verity devices recorded as runtime or config, up to 128. |
|
|
Task storage with the config root an authorized task expects to map next. |
|
|
One counter per refusal point, see Tamper counters. |
|
|
One counter per way to change or read the running kernel that
|
init freezes every one of them against userspace writes except
shirudo_roots, which an authorized task writes before each config
mapping. bpf2 also keeps shirudo_mnt_rec for the bpffs
mount it records, and seal keeps its scratch buffer and
seal_dir_marks, the inode storage of its marks on files and
directories.
A map no BPF program ever writes, such as shirudo_bpffs_sb, is
created with BPF_F_RDONLY_PROG as well as frozen: freezing
(BPF_MAP_FREEZE) blocks only the syscall side, so without the flag
a BPF program could still write the map’s value through a lookup’s
return pointer and the verifier would allow it. A map a BPF program
does write, even once, such as shirudo_mnt_rec or the task, socket
and inode storage maps above, does not take the flag. The same applies
to a map a future change adds: set BPF_F_RDONLY_PROG at creation
the moment no BPF program writes it, and drop the flag the moment one
does.
Telling shirudo from root#
Every guard that allows shirudo what it refuses to everyone else asks one
question: does the calling task have runtime authority? A task has it
when it carries a mark in shirudo_tasks that a core dump or a uprobe
has not revoked. Figure 1 in Task marks shows how a task
gets, keeps and loses the mark.
The bpf() hooks have a second discriminator. lsm/bpf and
lsm/bpf_prog_load receive bool kernel, which is true for calls a
signed loader makes from inside the kernel. Those calls are allowed;
userspace calls are filtered.
No guard looks at the user ID, a capability or the path of the executable. Root has all of those, and a path can be replaced.
What the hooks guard#
- The BPF system call
lsm/bpfadmits a userspaceBPF_PROG_LOADonly for aBPF_PROG_TYPE_SYSCALLprogram with a signature, andlsm/bpf_prog_loadadmits it only when the kernel verified it against the.bpfkeyring. Map creation, update and freeze, test runs, link creation, raw tracepoint attach and pinning need runtime authority. Every command shirudo has not classified, includingBPF_LINK_DETACH,BPF_LINK_UPDATE,BPF_PROG_DETACH,BPF_BTF_LOADand every map delete, is refused to every userspace caller, shirudo included, so a command a later kernel adds stays refused until someone classifies it.- Descriptors and pins
A descriptor keeps its object alive. A link held open stays attached after shirudo unpins it for an unload, a revocation purge or the undo of a refused load, and no LSM hook sees a link descriptor being handed out. A map descriptor reads what the serializers own and, if writable, changes what the guards read.
lsm/bpftherefore refusesBPF_OBJ_GET,BPF_MAP_GET_FD_BY_IDandBPF_LINK_GET_FD_BY_IDto a task without runtime authority.lsm/bpf_map, which every route to a map descriptor passes, refuses every map descriptor to such a task, read-only or writable.lsm/file_openrefuses such a task the opening of anything but a directory in the private bpffs, since a pin opened as a file reads a map’s contents; directories stay listable.BPF_PROG_GET_FD_BY_IDstays allowed for types other than XDP.lsm/bpf_progkeeps XDP descriptors to runtime authority, since netlink can attach them without passing throughbpf().BPF_BTF_GET_FD_BY_IDstays allowed. Root without authority cannot read shirudo’s maps or hold one of its links open by pin or by id.lsm/file_ioctlrefusesPERF_EVENT_IOC_SET_BPF, which binds a program to a perf event without abpf()command, to the same tasks.- The private bpffs and /run/shirudo
The pins are the only reference that keeps the enforcement links and the upatch links attached once
initexits. The inode hooks ofbpf2refuse unlink, rmdir, mkdir, symlink, hard link, rename and extended attribute changes in the private bpffs and in the/run/shirudotmpfs to tasks without runtime authority. The mount hooks refuse to every caller, shirudo included, the unmount, move, remount and mount-over of the bpffs, and any unmount that would take the recorded bpffs mount down with it, through its subtree or through mount propagation.- Keyrings and the register
lsm/key_permissionrefuses write, setattr, unlink and unpossessed search on the.bpfand.dm-veritykeyrings and the keysinitrecorded, so root cannot remove a trust anchor.lsm/tpm_commandrefusesTPM2_NV_Extendof shirudo’s NV index to tasks without runtime authority, so a signature from the TPM-held writer key is not enough to change the register.- Images and device mapper
lsm.s/bdev_setintegrityrecords which dm-verity devices hold the runtime image and the config image, the mount hooks refuse to unmount, move or cover them, andlsm/file_ioctlallows a task without runtime authority only the read-only device-mapper control commands.- Exec and processes
sealgrants and inherits the task mark, keeps ptrace and task-bound perf events away from marked tasks and counts a signal to one from a task without authority.bpf2runs every runtime executable in secure-exec mode, so the dynamic loader ignoresLD_PRELOADand the other unsafe variables, and refusesPR_SET_MM_EXE_FILEandPR_SET_MM_MAPto tasks without authority.sealrefuses a task with runtime authority executable memory that does not come from the runtime image.- Kexec
A kexec hands the machine to a kernel and initrd of the caller’s choosing with the TPM as it is, so the evidence of this boot would vouch for whatever runs after.
lsm/kernel_load_dataandlsm/kernel_read_filerefuse every kexec image and initramfs to every caller.- Lockdown
lsm/locked_downrefuses, to every caller, what the kernel’s lockdown in integrity mode refuses, the ways root has to change the running kernel, and/proc/kcore, which that mode leaves readable, an unsigned module among them. The hibernation check is refused without a count, as every read of/sys/power/statemakes one, andlsm/file_openrefuses a write of theHibernateLocationEFI variable, from which the next boot’s initrd would resume. The kernel’s lockdown LSM, where it runs, decides first, and covers the boot beforeinit.- Writable state
sealrefuses changes to labeled inodes under/var/lib/shirudoto tasks without runtime authority. Seal labels and task marks explains the labels.
Attach order#
init_lsm() in builtin-init.c brings the enforcement up in this
order:
Load
bpf2.Fill the identity map and freeze it.
Seed the bootstrap mark for its own process through a pidfd, freeze
shirudo_tasks,shirudo_devices,shirudo_tamper,shirudo_task_policy,shirudo_sock_policyandshirudo_integrity, and pin them withshirudo_roots. Pin the identity map.Attach
task_prctl,bprm_check_security,inode_rmdir,inode_rename,inode_mkdir,inode_symlink,inode_link,kernel_load_data,kernel_read_fileandlocked_down.Attach
file_ioctl,file_ioctl_compat,bdev_setintegrityandbdev_free_security.Attach
inode_unlink,inode_setxattr,inode_removexattr,key_permissionandtpm_command.Attach the mount guards
sb_umount,move_mount,sb_mount,sb_remountand thefile_openrecorder, open the bpffs root so the recorder notes the mount, and freeze that record.Load
sealon the shared maps, freeze its scratch map, and attach its twenty-nine hooks, thenmmap_fileandfile_mprotect.Attach
lsm/bpf_map, thenlsm/bpf_prog, thenlsm/bpf_prog_load, thenlsm/bpf.
init pins every link as it attaches it, before the next fallible
step. A link that is not pinned detaches when init exits, and a
crash would leave an attached link nobody owns.
The order follows from three constraints. First, init loads the two
objects without a signature, and lsm/bpf and lsm/bpf_prog_load
refuse an unsigned program load to every caller, init included, so
they attach last. Second, every guard reads the identity map, so the map
is complete and frozen before the first guard attaches; no guard ever
decides on a half-written record. Third, the
bpffs mount must be immutable before admission goes live, so the mount
guards and the bpffs inode guards attach before the seal and the
admission gates. init itself passes every guard on the way because
its bootstrap mark is in place before the first one attaches.
When any step fails, init unpins every link it attached and removes
the map pins, so no partial enforcement survives a failed init. A
crash skips that cleanup. When it came before the identity map was
pinned, the next init removes the map pins it left. When it came
later, every later init in the same boot refuses before it loads
anything, with init: authenticated image settings: <error>, and the
guards attached so far stay until the next reboot.
Order at run time#
The order in which programs run on one hook is the reverse of the order
in which they attached. The kernel’s BPF trampoline puts a new program at
the head of the list, and for an LSM hook that returns a value, a
non-zero return ends the list. Where bpf2 and seal share a hook
(bprm_check_security, file_open, inode_unlink,
inode_rmdir, inode_rename, inode_setxattr,
inode_removexattr, inode_mkdir, inode_symlink,
inode_link), the seal
program runs first, and when it refuses, its errno and counter are the
ones the caller sees. A traced exec of a runtime file, for example, fails with
EACCES from seal rather than EPERM from bpf2.
The BPF LSM is one LSM among those the kernel runs. The kernel stops at the first LSM that refuses, and each shirudo program returns an earlier refusal unchanged. shirudo’s verdicts can only add refusals to those of SELinux, AppArmor, Smack or Yama; they never turn one of their refusals into a permission.
Counters#
Most refusals increment a counter in shirudo_tamper, a map
init freezes against userspace writes and pins as tamper.
shirudo status reads it back. The counters, their meanings and what
an operator does about each are in Tamper counters. The
table below names the counter of each refusal and says where a refusal
is not counted.
Hook reference#
The programs of bpf2 and those of seal have a table each.
“Authority” means runtime authority as above. The last column says
what the program does when it cannot decide: when a read fails, when
storage cannot be allocated, or when the record it needs is missing.
Hooks in bpf2#
Hook |
Refuses, and allows |
Errno and counter |
Cannot decide |
|---|---|---|---|
|
With |
|
Closed: unclassified commands are refused. |
|
A userspace load the kernel did not verify, verified against
another keyring kind than |
|
Closed: no frozen keyring serial refuses every load, uncounted. |
|
Every map descriptor to a task without authority. |
|
Closed. |
|
An XDP program descriptor to a task without authority, including acquisition by program ID. Other program types stay allowed. |
|
Closed. |
|
Opening a non-directory inode of the private bpffs, through any
mount of it, to a task without authority. Records the bpffs mount
when the bootstrap task opens its root. Opening a
|
|
Closed. |
|
|
|
Closed: unknown DM commands are refused. |
|
The operation in the private bpffs or the |
|
Closed for those two file systems. |
|
To every caller: unmounting the bpffs, an unmount whose subtree or
propagation reaches the recorded bpffs mount, unmounting
|
|
Closed: a walk beyond 1024 mounts, 32 levels or 4 masters is refused. |
|
To every caller: moving the bpffs, the |
|
Closed. |
|
To every caller: a mount onto the bpffs or the runtime image, and
a propagation change of the |
|
Closed. |
|
Remounting the bpffs, |
|
Closed. |
|
Records a dm-verity device as runtime when its SHA-256 root equals the frozen runtime root, or as config when it equals the root an authorized task staged for this mapping; the staged root is used once. Refuses a second record for a recorded device, a root it cannot read, disk sequence 0 and a full record map. |
|
Other mappings get no record and so no authority. |
|
Removes the device record. |
None |
The kernel can skip this call; lookups also compare the device number and disk sequence. |
|
Write, setattr, unlink, and search through an unpossessed
reference, on the |
|
Open before |
|
|
|
Closed for malformed commands. |
|
|
|
Closed. |
|
Sets secure-exec for every exec of a runtime file, and refuses a
traced one. The |
|
A file not recorded as runtime gets no secure-exec, and no mark. |
|
Every kexec kernel image and initramfs, by either system call, to every caller. |
|
Unconditional. |
|
Every lockdown reason of integrity mode, an unsigned module
included, and |
|
Unconditional; the kernel’s lockdown LSM decides first. |
Hooks in seal#
Hook |
Refuses, and allows |
Errno and counter |
Cannot decide |
|---|---|---|---|
|
Stages the mark candidate: trusted when the file is a direct, not
overlay, inode on a recorded runtime device. Refuses a trusted
candidate that is traced, shares its |
|
Closed: no task storage refuses the exec with |
|
Commits the staged candidate as the task’s mark when an exec succeeds, and clears a revocation. |
None |
No task storage leaves the task without authority. |
|
Seed with the task’s committed bits, accumulate the original executable and each interpreter, then commit. An absent policy or a file system without xattrs contributes zero bits; a policy value other than 8 bytes, or one that cannot be read, refuses the exec. |
|
Failed exec leaves committed policy unchanged. |
|
Copies nonzero committed task policy bits into socket storage.
|
|
No storage is needed when the task has no policy bits. |
|
Copies a marked parent’s mark, with its revocation, and any committed policy bits to the child. |
|
Closed: no storage for the mark or the bits refuses the fork. |
|
A signal other than 0 to a marked task from a task without authority. Let through, root may end a command that hangs and systemd stops the runs its timers start, and counted, so that a load stopped between its pins and its extend shows before the next command undoes what it left. |
None; counts |
Which signal is an attack: a stop by root and one by systemd count alike (R2). The kernel’s own signals and the freezer pass no hook. |
|
A change of a marked task’s resource limits by another task
without authority, |
|
Closed. |
|
Every ptrace access mode against a marked task, which covers
attach, |
|
Closed. |
|
A read-only access to a marked task’s memory that
|
|
Closed, with carried patch 0024. |
|
A perf event bound to a marked task. CPU-wide events have no task and are not checked. |
|
Not applicable. |
|
An executable mapping in a task with runtime authority, other than the bootstrap task, that is writable or does not come from a runtime file. |
|
Closed. |
|
In the same tasks: making an executable mapping writable, or making executable a mapping that was not, or that is not from a runtime file. |
|
Closed. |
|
A write open of a labeled file to a task without authority. On a directory open, and on a read open by an authorized task, records or clears the mark from the label; a write open only records it. |
|
|
|
The operation on a labeled inode to a task without authority. Setattr covers truncate, chmod, chown and timestamps. Removing the seal or the policy label or the ancestor mark is refused on any inode without authority. |
|
|
|
A POSIX ACL change or a change of the inode flags ( |
|
|
|
Setting |
|
|
|
A labeled source, a labeled destination directory or a labeled inode it would replace, or a source or replaced directory that carries the ancestor mark, to a task without authority. |
|
|
|
A new entry in a labeled directory to a task without authority, read from the entry’s parent dentry, and a hard link of a labeled inode anywhere. Each refreshes the directory’s mark. |
|
|
|
Labels a new inode under a marked directory, inside the creation, and marks the new inode. Refuses the creation when it cannot add the label. |
|
Cannot read: the entry hook of the same operation refreshed the parent’s mark from its label just before. |
|
The overlay copy-up of a labeled inode, by anyone. |
|
As the label verdict, but a label, any other read error and a failure to allocate the scratch buffer refuse every caller. |
|
Refresh an inode’s mark from its label after the change. |
None |
No label, or a label of another length, removes the mark; a failed read leaves it. |
The label verdict the seal rows name is the table of
Reading the label, which says what each result of a label read
decides and why.
Limits#
The hooks decide only what reaches them. These paths do not:
The
kernelargument is true for everybpf()call through a kernel pointer, including BPF that kernel code loads withkern_sys_bpf(), such as a light skeleton in a module.BPF_PROG_GET_FD_BY_IDstays open except for XDP descriptors, whichlsm/bpf_progguards.lsm/bpfandlsm/file_ioctlrefuse thebpf()attach commands and the perf ioctl to tasks without authority; other kernel interfaces that take a program descriptor are not hooked here.mount_setattr(2)has no LSM hook,pivot_root(2)is not hooked, andlsm/sb_mountdoes not check the source of a bind or legacy move.A perf event bound to a task before that task execs the runtime image survives the exec, and CPU-wide events and probes on the runtime binary are not checked.
lsm/tpm_commandsees commands written to/dev/tpm0and/dev/tpmrm0. A TPM reached around the kernel’s TPM character devices is outside it. The hook is not in bpf-next; Carried kernel patches describes the patch the test kernel carries.shirudo_rootsand the inode marks are not frozen, by design. Device records are limited to 128.Several refusals are uncounted, as the table shows.
Seal labels and task marks lists the gaps of the writable-state seal, and Threat model gives the residual risks.