eBPF-based security platform
bpf2: refuse unsigned modules and a write of the resume location
Daniel Borkmannedf25442fc0c
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 71c6cf8d..540bc47e 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -508,9 +508,10 @@ signed config image.counted. The XFS case needs XFS in the kernel and ``mkfs.xfs``,which the slim test VM kernel lacks* - ``t6154-lockdown``- - A PCI config write, ``/proc/kcore`` and ``/dev/mem``, where the- kernel has them, are refused, the first two counted as- ``Lockdown``, and a read of ``/sys/power/state`` counts nothing+ - A module without its signature, a PCI config write,+ ``/proc/kcore`` and ``/dev/mem``, where the kernel has them, are+ refused, all but the last counted as ``Lockdown``, and a read of+ ``/sys/power/state`` counts nothing* - ``t6155-mountpoint-removal``- From a namespace without ``/run``, an ``rmdir`` and a rename ofthe directory init's ``/run`` covers are refused and counted, and@@ -1093,6 +1094,8 @@ Most of these attacks are open threats in- Stopping a running shirudo with ``SIGSTOP``, ``mount_setattr(2)``,hibernation and ``/dev/snapshot``, ``/dev/mem``, and TPM access over``i2c-dev`` or ``spidev``.+- A write of the ``HibernateLocation`` EFI variable: the test guest+ boots without UEFI firmware and has no efivarfs.Tests that assert behavior the threat model lists as a threat toresolve. They change when that threat is resolved:diff --git a/Documentation/internals/boot-flow.rst b/Documentation/internals/boot-flow.rstindex 4cdd3228..f603ae1b 100644--- a/Documentation/internals/boot-flow.rst+++ b/Documentation/internals/boot-flow.rst@@ -192,7 +192,8 @@ verifier that requires an initrd binding rejects the target's evidence.Hibernation and TPM resets---------------------------shirudo does not refuse hibernation. The register and the config record+shirudo refuses hibernation from ``init`` on (A9 in+:doc:`/security/threat-model`). The register and the config recordare tied to the TPM's reset and restart counts. When the TPM is reset orrestarted without a reboot, which a resume from hibernation can do, theregister is cleared and one of the counts changes while the kernel'sdiff --git a/Documentation/internals/enforcement.rst b/Documentation/internals/enforcement.rstindex fb8008d7..948fed75 100644--- a/Documentation/internals/enforcement.rst+++ b/Documentation/internals/enforcement.rst@@ -206,10 +206,12 @@ Lockdown``lsm/locked_down`` refuses, to every caller, what the kernel'slockdown in integrity mode refuses, the ways root has to change therunning kernel, and ``/proc/kcore``, which that mode leaves- readable. Module signatures stay with the kernel's enforcement, and- the hibernation check is refused without a count, as every read of- ``/sys/power/state`` makes one. The kernel's lockdown LSM, where it- runs, decides first, and covers the boot before ``init``.+ readable, an unsigned module among them. The hibernation check is+ refused without a count, as every read of ``/sys/power/state`` makes+ one, and ``lsm/file_open`` refuses a write of the+ ``HibernateLocation`` EFI variable, from which the next boot's+ initrd would resume. The kernel's lockdown LSM, where it runs,+ decides first, and covers the boot before ``init``.Writable state``seal`` refuses changes to labeled inodes under@@ -346,9 +348,10 @@ when the record it needs is missing.* - ``lsm/file_open`` (``bpf2``)- Opening a non-directory inode of the private bpffs, through anymount of it, to a task without authority. Records the bpffs mount- when the bootstrap task opens its root.- - ``EACCES``- - ``ObjGet``+ when the bootstrap task opens its root. Opening a+ ``HibernateLocation`` EFI variable for writing, to every caller.+ - ``EACCES``; ``EPERM`` for the EFI variable+ - ``ObjGet``; ``Lockdown``- Closed.* - ``lsm/file_ioctl``, ``lsm/file_ioctl_compat`` (``bpf2``)- ``PERF_EVENT_IOC_SET_BPF`` on any file,@@ -546,8 +549,8 @@ when the record it needs is missing.- ``Kexec``- Unconditional.* - ``lsm/locked_down`` (``bpf2``)- - Every lockdown reason of integrity mode but module signatures,- and ``LOCKDOWN_KCORE``, to every caller.+ - Every lockdown reason of integrity mode, an unsigned module+ included, and ``LOCKDOWN_KCORE``, to every caller.- ``EPERM``- ``Lockdown``; hibernation uncounted- Unconditional; the kernel's lockdown LSM decides first.diff --git a/Documentation/operate/plan/hardening.rst b/Documentation/operate/plan/hardening.rstindex 76155637..5b32bfc1 100644--- a/Documentation/operate/plan/hardening.rst+++ b/Documentation/operate/plan/hardening.rst@@ -37,21 +37,25 @@ Kernel configuration- Checked* - Lockdown in integrity mode: optional- shirudo's ``bpf2`` object refuses, from ``init`` on, what- integrity mode refuses but module signatures: ``/dev/mem``, MSRs,+ integrity mode refuses: unsigned modules, ``/dev/mem``, MSRs,I/O ports, PCI resources, debugfs writes, ACPI table overridesand hibernation, and ``/proc/kcore`` with them, which the modeleaves readable. The kernel's lockdown LSM, where it runs, refusesfirst and covers the boot before ``init``; module signatures and- hibernation need the rows below either way. Do not use+ the resume device need the rows below either way. Do not useconfidentiality mode: it withholds ``bpf_probe_read_kernel()``from BPF programs, which shirudo's own programs need, so theywould not load.- no- * - Module signature enforcement: ``CONFIG_MODULE_SIG_FORCE`` or- ``module.sig_enforce=1``, or lockdown, which also refuses unsigned- modules- - An unsigned module runs in the kernel with no BPF involved.- - no+ * - Module signatures: ``CONFIG_MODULE_SIG``, with every module the+ target loads signed by a key the kernel trusts+ - Without it the kernel loads a module without asking lockdown, and+ the module runs in the kernel with no BPF involved. With it,+ ``bpf2`` refuses an unsigned module from ``init`` on where the+ kernel does not refuse it first (``CONFIG_MODULE_SIG_FORCE`` or+ ``module.sig_enforce=1``).+ - partly: from ``init`` on, an unsigned module is refused and+ counted as ``Lockdown``* - ``CONFIG_STRICT_DEVMEM`` and ``CONFIG_IO_STRICT_DEVMEM``, or no``/dev/mem`` at all- Limits ``/dev/mem`` before ``init``, after which ``bpf2`` refuses@@ -63,13 +67,17 @@ Kernel configurationany of them. With the option off, the kernel refuses writes to amounted block device.- no- * - No hibernation: ``CONFIG_HIBERNATION=n``, ``nohibernate`` or- ``lockdown=integrity``+ * - No resume device: no ``resume=`` on the command line, an empty+ ``CONFIG_PM_STD_PARTITION`` and none named in the initrd, or no+ hibernation (``CONFIG_HIBERNATION=n``, ``nohibernate`` or+ ``lockdown=integrity``)- Resuming from a hibernation image replaces kernel memory with- what the image holds. shirudo refuses hibernating once ``init``- runs, but the initrd resumes before ``init``, from an image root- can write to swap.- - no+ what the image holds, and the initrd resumes before ``init``,+ from an image root can write to swap.+ - partly: from ``init`` on, hibernating is refused, and so is a+ write of the ``HibernateLocation`` EFI variable through which+ systemd's initrd finds an image the UKI does not name, counted+ as ``Lockdown``* - Kexec- A kexec boots a kernel and initrd of root's choosing while theTPM keeps this boot's register and PCRs.diff --git a/Documentation/operate/plan/interoperability.rst b/Documentation/operate/plan/interoperability.rstindex a8617c70..2ff9786b 100644--- a/Documentation/operate/plan/interoperability.rst+++ b/Documentation/operate/plan/interoperability.rst@@ -276,18 +276,24 @@ kexec and kdumpModule loading and kernel live patching----------------------------------------:Mechanism: shirudo does not gate module loading.+:Mechanism: shirudo loads signed modules only.``lsm/kernel_load_data`` and ``lsm/kernel_read_file`` let every load- other than a kexec image or initramfs through, and- ``lsm/locked_down`` leaves module signatures to the kernel. Whether a- module loads is the kernel's decision: module signature enforcement+ other than a kexec image or initramfs through, and the kernel checks+ the module's signature. When the signature is missing or the kernel+ cannot verify it, and the kernel does not refuse the module itself,+ it asks lockdown, and ``lsm/locked_down`` refuses it from ``init`` on(A4 in :doc:`/security/threat-model`). A livepatch module, as kpatchor the kernel's ``klp-build`` produce, is a kernel module like anyother.-:Symptom: None from shirudo.-:What to do: Enforce module signatures, as :doc:`/operate/plan/hardening`- lists, and sign livepatch modules with a key the kernel trusts. A- module runs as kernel code outside every shirudo check. Unloading a+:Symptom: ``init_module(2)`` and ``finit_module(2)`` of an unsigned+ module fail with ``EPERM`` (``modprobe`` prints ``Operation not+ permitted``), and ``Lockdown`` counts it. ``modprobe --force``, which+ strips the version checks, makes a signed module count as unsigned.+:What to do: Build the kernel with ``CONFIG_MODULE_SIG``, as+ :doc:`/operate/plan/hardening` lists, and sign every module the+ target loads, livepatch modules included, with a key the kernel+ trusts. A signed module runs as kernel code outside every shirudo+ check. Unloading amodule that a kprobe upatch targets leaves the upatch loaded andmeasured but without effect, a reading of the kernel source (R21).shirudo's tests do not cover a livepatch and an upatch on the same@@ -326,16 +332,20 @@ Other LSMsHibernation and suspend------------------------:Mechanism: shirudo does not refuse hibernation. Lockdown in integrity- mode disables it (A9 in :doc:`/security/threat-model`), and the- behavior of the register across hibernation is not analyzed. Before a- suspend, the kernel's TPM driver sends ``TPM2_Shutdown(STATE)`` to a+:Mechanism: shirudo refuses hibernation from ``init`` on:+ ``lsm/locked_down`` refuses the kernel's hibernation check, and+ ``lsm/file_open`` the write of the ``HibernateLocation`` EFI variable+ from which systemd's initrd would resume (A9 in+ :doc:`/security/threat-model`). The behavior of the register across+ hibernation is not analyzed. Before a suspend, the kernel's TPM driver sends ``TPM2_Shutdown(STATE)`` to aTPM 2.0 unless the TPM stays powered, and the TPM counts that in itsrestart count (:doc:`/concepts/background/tpm`). The config activationrecord holds the TPM's reset and restart counts, and shirudo comparesthem with the TPM's before a command runs.-:Symptom: After a resume, shirudo commands refuse with ``shirudo: image- state: Stale file handle`` and exit 1, and ``shirudo status`` reports+:Symptom: ``/sys/power/state`` lists no ``disk``, and ``systemctl+ hibernate`` fails as hibernation is not supported. After a resume+ from suspend, shirudo commands refuse with ``shirudo: image state:+ Stale file handle`` and exit 1, and ``shirudo status`` reports``tampered``.:doc:`/operate/maintain/backup-recovery` describes the state. For suspend toRAM this is a reading of the code and of the TPM's counters and is notdiff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex 500c7d19..43e0e1bb 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -207,7 +207,7 @@ keyring when ``CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG_SECONDARY_KEYRING`` or``CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG_PLATFORM_KEYRING`` is set.:doc:`/operate/plan/hardening` lists what that means for who can sign aconfig, and the options the trust model assumes beyond this table:-lockdown, module signature enforcement, ``CONFIG_COMPAT`` and others.+lockdown, module signatures, ``CONFIG_COMPAT`` and others.Kernel command line~~~~~~~~~~~~~~~~~~~diff --git a/Documentation/security/security-model.rst b/Documentation/security/security-model.rstindex 12400e12..bd49f46e 100644--- a/Documentation/security/security-model.rst+++ b/Documentation/security/security-model.rst@@ -95,9 +95,8 @@ The properties above hold only on a deployment that provides the- A trusted kernel with the BPF LSM, the signed-loader ABI and thecarried kernel patches-- Module signature enforcement and lockdown in integrity mode, no- 32-bit compatibility, no hibernation and no writes under mounted- file systems+- Module signatures, no 32-bit compatibility, no resume device in the+ UKI and no writes under mounted file systems- Secure Boot with a signed, measured UKI and an authenticated kernelcommand line- A kernel whose dm-verity trust path consults only keys trusted todiff --git a/Documentation/security/tamper-counters.rst b/Documentation/security/tamper-counters.rstindex 0d5c3643..546a7b47 100644--- a/Documentation/security/tamper-counters.rst+++ b/Documentation/security/tamper-counters.rst@@ -260,10 +260,10 @@ counter.- ``EPERM``* - ``Lockdown``- change or read of the running kernel that lockdown refuses- - ``lsm/locked_down``: every reason of integrity mode but module- signatures, which the kernel's enforcement keeps, and- hibernation, refused uncounted, and ``LOCKDOWN_KCORE``, for- every caller+ - ``lsm/locked_down``: every reason of integrity mode, an unsigned+ module included, but hibernation, refused uncounted, and+ ``LOCKDOWN_KCORE``, for every caller; ``lsm/file_open``: a write+ open of a ``HibernateLocation`` EFI variable, for every caller- ``EPERM``Unless the row says every caller, a task with runtime authority isdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex f48ce2b3..85a33bb2 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -174,17 +174,18 @@ without the protection.label read, the seal falls back to its marks where a read failsfor the caller (T3).* - A4- - The kernel refuses unsigned modules (``CONFIG_MODULE_SIG`` with- enforcement).- - An unsigned module is kernel code of root's choosing, and- shirudo leaves module signatures to the kernel. The other ways- root has to change or replace the running kernel, ``/dev/mem``,- MSR writes, port I/O, PCI BAR access, debugfs writes and ACPI- table overrides, the ones lockdown in integrity mode refuses,- ``bpf2`` refuses from ``init`` on through ``lsm/locked_down``,- with ``/proc/kcore``, which that mode leaves readable- (``Lockdown``, ``t6154``). The kernel's lockdown LSM is not- needed for them, and where it runs it decides first.+ - The kernel checks module signatures (``CONFIG_MODULE_SIG``).+ - An unsigned module is kernel code of root's choosing. A kernel+ that does not enforce signatures itself asks lockdown about one,+ and ``bpf2`` refuses it from ``init`` on through+ ``lsm/locked_down``, with the other ways root has to change or+ replace the running kernel that lockdown in integrity mode+ refuses, ``/dev/mem``, MSR writes, port I/O, PCI BAR access,+ debugfs writes and ACPI table overrides, and ``/proc/kcore``,+ which that mode leaves readable (``Lockdown``, ``t6154``). The+ kernel's lockdown LSM is not needed for them, and where it runs+ it decides first. A kernel without ``CONFIG_MODULE_SIG`` asks+ nothing and loads any module.- No, see U10. Lockdown in confidentiality mode would withhold``bpf_probe_read_kernel()`` from shirudo's own programs and stop``init``. This is a reading of the kernel source and is not@@ -222,13 +223,17 @@ without the protection.and ``/dev/tpmrm0``.- No.* - A9- - Hibernation is unavailable from boot: ``CONFIG_HIBERNATION=n``,- or ``nohibernate`` or ``lockdown=integrity`` on the command line.- - A hibernation image is kernel memory on disk that root can- change before resume, through ``/dev/snapshot`` among others.- ``bpf2`` refuses hibernation once ``init`` runs, but a resume- runs in the initrd before it, from an image root can write to- swap.+ - The boot resumes from no hibernation image: the command line has+ no ``resume=``, ``CONFIG_PM_STD_PARTITION`` is empty and the+ initrd names no resume device, or hibernation is off from boot+ (``CONFIG_HIBERNATION=n``, ``nohibernate`` or+ ``lockdown=integrity``).+ - A hibernation image is kernel memory on disk, and a resume runs+ in the initrd before ``init``, from an image root can write to+ swap. ``bpf2`` refuses hibernation once ``init`` runs, and the+ write of a ``HibernateLocation`` EFI variable, through which+ systemd's initrd resumes from a device the running system names+ (``Lockdown``). What the authenticated UKI names is left.The behavior of the register across hibernation is not analyzedhere.- No.@@ -241,8 +246,7 @@ without the protection.* - A11- The kernel command line is authenticated as part of the UKI. Itcarries ``lsm=`` with ``bpf``, ``bpf.keyring_unsealed=1`` and- ``dm_verity.keyring_unsealed=1``, ``nohibernate`` or- ``lockdown=integrity`` on a kernel with hibernation (A9), and+ ``dm_verity.keyring_unsealed=1``, no ``resume=`` (A9), andnothing that masks or skips ``shirudo-initrd.service``.- The command line decides whether the LSM runs, whether thekeyrings accept shirudo's keys and whether ``init`` runs at all.@@ -1098,19 +1102,24 @@ either system call (``Kexec``).The kernel that runs this boot has other ways in: ``/dev/mem``, MSRs,port I/O, PCI resources, debugfs writes and ACPI table overrides change-it, and ``/proc/kcore`` reads it. ``lsm/locked_down`` refuses what-lockdown in integrity mode refuses, module signatures aside (A4), and+it, an unsigned module joins it, and ``/proc/kcore`` reads it.+``lsm/locked_down`` refuses what lockdown in integrity mode refuses, an+unsigned module the kernel asks about included (A4), and``/proc/kcore`` with it, to every task, whatever the command line says(``Lockdown``). Hibernation is refused too, but a resume runs in the-initrd before ``init``, which A9 keeps off.+initrd before ``init``: ``lsm/file_open`` refuses the write of a+``HibernateLocation`` EFI variable, which would have the next boot's+initrd resume from a device root chose, and A9 keeps the UKI from naming+one... rst-class:: facts:Adversary: ADV-B, root on the target without the signing key:Enforced at: ``lsm/kernel_load_data``, ``lsm/kernel_read_file``,- ``lsm/locked_down``+ ``lsm/locked_down``, ``lsm/file_open``:Refusal: ``EPERM`` from ``kexec_load(2)`` and ``kexec_file_load(2)``,- and from each lockdown check+ from each lockdown check and from the open of a+ ``HibernateLocation`` EFI variable for writing:Counters: ``Kexec``, ``Lockdown``:Tests: ``t4003``, ``t4101``, ``t6126``, ``t6154``:Residual: R6, R7, R12, U11, U16@@ -1894,11 +1903,11 @@ U9 init can rebind the register after hiding this boot's recordU10 Kernel prerequisites are not checked:Affects: A4, A5, A7, A9 and A10.- :Consequence: A target without module signature enforcement, with- 32-bit compatibility, with the dm-verity keyring options on, with- hibernation available at boot or with writes under mounted file- systems allowed initializes without an error and without the- protection those assumptions give.+ :Consequence: A target without module signatures, with 32-bit+ compatibility, with the dm-verity keyring options on, with a+ resume device in its UKI or with writes under mounted file systems+ allowed initializes without an error and without the protection+ those assumptions give.:Status: Confirmed in code: the pre-flight checks none of them.:Meanwhile: Build the kernel and its command line as:doc:`/operate/plan/hardening` describes.@@ -2060,15 +2069,15 @@ the procedures.The deployer:- Builds the kernel with the signed-loader ABI and the carried kernel- patches (A3), ``CONFIG_MODULE_SIG`` with enforcement (A4),+ patches (A3), ``CONFIG_MODULE_SIG`` (A4),``CONFIG_COMPAT=n`` (A5), bothdm-verity keyring options off and no foreign built-in certificate(A7), the TPM driver without ``i2c-dev`` or ``spidev`` on its bus(A8), and ``CONFIG_BLK_DEV_WRITE_MOUNTED=n`` (A10)- Boots a signed, measured UKI under Secure Boot whose command line- carries ``lsm=`` with ``bpf``, ``bpf.keyring_unsealed=1``,- ``dm_verity.keyring_unsealed=1`` and, on a kernel with hibernation,- ``nohibernate`` (A6, A9, A11)+ carries ``lsm=`` with ``bpf``, ``bpf.keyring_unsealed=1`` and+ ``dm_verity.keyring_unsealed=1``, and whose command line, kernel and+ initrd name no resume device (A6, A9, A11)- Keeps generic tools off the runtime image (A14)- Keeps the environment files of the units absent or empty (U3) andstarts every shirudo unit from the runtime image (U7)diff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 6fc89698..ef79c34f 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -511,21 +511,17 @@ int BPF_PROG(shirudo_lsm_kernel_read_file, struct file *file,/** Lockdown in integrity mode, the ways root has to change the running- * kernel, from /dev/mem to debugfs writes, and /proc/kcore, which that- * mode leaves readable, whether or not the command line asks for- * lockdown: the kernel's own lockdown LSM decides first where it runs.- * Module signatures stay with the kernel's enforcement (A4), and the- * hibernation check, which every read of /sys/power/state makes, is- * refused without a count.+ * kernel, from an unsigned module and /dev/mem to debugfs writes, and+ * /proc/kcore, which that mode leaves readable, whether or not the+ * command line asks for lockdown: the kernel's own lockdown LSM decides+ * first where it runs. The hibernation check, which every read of+ * /sys/power/state makes, is refused without a count.*/SEC("lsm/locked_down")int BPF_PROG(shirudo_lsm_locked_down, enum lockdown_reason what, int ret){if (ret)return ret;- if (what == bpf_core_enum_value(enum lockdown_reason,- LOCKDOWN_MODULE_SIGNATURE))- return 0;if (what == bpf_core_enum_value(enum lockdown_reason,LOCKDOWN_HIBERNATION))return -EPERM;@@ -785,6 +781,29 @@ deny:return shirudo_tamper_deny(TAMPER_BPFFS_UMOUNT_ABOVE, -EPERM);}+#define EFIVARFS_MAGIC 0xde5e81e4+#define FMODE_WRITE 0x2+#define HIBERNATE_LOCATION "HibernateLocation-"++/*+ * systemd in the initrd resumes from the device the HibernateLocation+ * EFI variable names, before init attaches a guard: a variable root+ * writes now would have the next boot resume an image of root's making.+ */+static __always_inline bool hibernate_location(struct file *file)+{+ char name[sizeof(HIBERNATE_LOCATION)];+ const unsigned char *s;++ if (BPF_CORE_READ(file, f_inode, i_sb, s_magic) != EFIVARFS_MAGIC ||+ !(BPF_CORE_READ(file, f_mode) & FMODE_WRITE))+ return false;+ s = BPF_CORE_READ(file, f_path.dentry, d_name.name);+ if (bpf_probe_read_kernel_str(name, sizeof(name), s) != sizeof(name))+ return false;+ return !bpf_strncmp(name, sizeof(name) - 1, HIBERNATE_LOCATION);+}+/** A pin opened as a file reads a map's contents through bpffs, so pins* open for shirudo alone; directories stay listable. The bpffs mount of@@ -800,8 +819,12 @@ int BPF_PROG(shirudo_lsm_file_open, struct file *file, int ret)struct vfsmount *mnt;__u32 zero = 0;- if (ret || !id)+ if (ret)return ret;+ if (hibernate_location(file))+ return shirudo_tamper_deny(TAMPER_LOCKDOWN, -EPERM);+ if (!id)+ return 0;mnt = BPF_CORE_READ(file, f_path.mnt);if (!mnt || BPF_CORE_READ(mnt, mnt_sb, s_dev) != id->sb_dev)return 0;diff --git a/testing/functional/tamperproof/t6154-lockdown.sh b/testing/functional/tamperproof/t6154-lockdown.shindex 9fad32f6..92e86407 100755--- a/testing/functional/tamperproof/t6154-lockdown.sh+++ b/testing/functional/tamperproof/t6154-lockdown.sh@@ -3,9 +3,10 @@# Copyright (c) 2026 Authors of shirudo## t6154 -- bpf2 refuses what lockdown in integrity mode refuses, whatever-# the command line says, and /proc/kcore with it: the reads and writes of-# the running kernel fail with EPERM and count as Lockdown, while the-# hibernation check every read of /sys/power/state makes counts nothing.+# the command line says, and /proc/kcore with it: an unsigned module and+# the reads and writes of the running kernel fail with EPERM and count as+# Lockdown, while the hibernation check every read of /sys/power/state+# makes counts nothing.cd "$(dirname "$0")/../..". ./integration-lib.sh@@ -30,12 +31,38 @@ except OSError as e:print(errno.errorcode[e.errno])' "$1"}-before=$(counter Lockdown) || test_skip_all "no tamper counters"+counter Lockdown >/dev/null || test_skip_all "no tamper counters"++# The guest's modules are signed; a copy without the signature is what the+# kernel asks lockdown about.+test_expect_success 'a module without its signature is refused and counted' '+ module=$(find "/lib/modules/$(uname -r)" -name "*.ko" | head -1) &&+ [ -n "$module" ] &&+ now=$(counter Lockdown) &&+ python3 - "$module" "$W/unsigned.ko" >"$W/module" <<-\PY &&+ import ctypes, errno, os, platform, sys+ MARKER = b"~Module signature appended~\n"+ FINIT_MODULE = {"x86_64": 313, "aarch64": 273, "riscv64": 273}+ data = open(sys.argv[1], "rb").read()+ if not data.endswith(MARKER):+ sys.exit("not signed: " + sys.argv[1])+ sig_len = int.from_bytes(data[-len(MARKER) - 4:-len(MARKER)], "big")+ with open(sys.argv[2], "wb") as f:+ f.write(data[:len(data) - len(MARKER) - 12 - sig_len])+ fd = os.open(sys.argv[2], os.O_RDONLY)+ libc = ctypes.CDLL(None, use_errno=True)+ ret = libc.syscall(FINIT_MODULE[platform.machine()], fd, b"", 0)+ print("done" if ret == 0 else errno.errorcode[ctypes.get_errno()])+ PY+ grep -qx EPERM "$W/module" &&+ [ "$(counter Lockdown)" -gt "$now" ]+'if [ -e /proc/kcore ]; thentest_expect_success '/proc/kcore is refused and counted' '+ now=$(counter Lockdown) &&[ "$(open_errno /proc/kcore)" = EPERM ] &&- [ "$(counter Lockdown)" -gt "$before" ]+ [ "$(counter Lockdown)" -gt "$now" ]'fiCONFIG=$(ls /sys/bus/pci/devices/*/config 2>/dev/null | head -1)diff --git a/testing/vm/build-kernel.sh b/testing/vm/build-kernel.shindex 8888cd05..2440be92 100755--- a/testing/vm/build-kernel.sh+++ b/testing/vm/build-kernel.sh@@ -389,7 +389,7 @@ CONFIG_SYSTEM_TRUSTED_KEYS=""CONFIG_SYSTEM_REVOCATION_KEYS=""CONFIG_UTS_NS=yCONFIG_MODULE_SIG=y-CONFIG_MODULE_SIG_ALL=n+CONFIG_MODULE_SIG_ALL=yCONFIG_CRYPTO_RSA=yCONFIG_CRYPTO_SHA256=yCONFIG_CRYPTO_MLDSA=y@@ -566,6 +566,9 @@ require_config CONFIG_SECCOMP yrequire_config CONFIG_SECCOMP_FILTER yrequire_config CONFIG_CROSS_MEMORY_ATTACH yrequire_config CONFIG_SYSTEM_DATA_VERIFICATION y+# shirudo refuses unsigned modules once init runs, and the guest loads its+# networking modules after that.+require_config CONFIG_MODULE_SIG_ALL yrequire_config CONFIG_PKCS8_PRIVATE_KEY_PARSER yrequire_config CONFIG_CRYPTO_RSA yrequire_config CONFIG_UPROBES y