eBPF-based security platform
hardening: lock process memory before command dispatch
Leonard Cohnen179e648042cb
diff --git a/.github/scripts/target-tests.sh b/.github/scripts/target-tests.shindex dde531f9..79b358f8 100755--- a/.github/scripts/target-tests.sh+++ b/.github/scripts/target-tests.sh@@ -274,5 +274,7 @@ fi# emergency mode for instance, would otherwise hold the job for an hour.export TEST_VM_TIMEOUT=900+sudo prlimit --pid "$$" --memlock=unlimited:unlimited+make -C testing test-vm-bundlesudo -E env PATH="$PATH" make -C testing test-tgt TEST_VM_INITRD=1diff --git a/.github/workflows/deb.yml b/.github/workflows/deb.ymlindex 2664cfba..963a56f1 100644--- a/.github/workflows/deb.yml+++ b/.github/workflows/deb.yml@@ -283,7 +283,9 @@ jobs:rpm-operator:needs: debruns-on: ubuntu-latest- container: fedora:44+ container:+ image: fedora:44+ options: --ulimit memlock=-1:-1steps:- name: Install the build dependenciesdiff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex aa575ffb..2068b278 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -363,10 +363,13 @@ marked when those hooks run. The perf allocation check is not serialized withexec; the separate `perf/exec race investigation<https://github.com/shirudo-dev/shirudo/issues/11>`__ records this limitation.-Process hardening. ``util/harden.c`` runs at startup on the target:-``PR_SET_DUMPABLE`` off, ``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` to-nobody, ``RLIMIT_CORE`` zero, sensitive buffers locked in memory and-scrubbed after use. No host file may name code for it to load:+Process hardening. ``util/harden.c`` runs before command dispatch in+both the core executable and ``shirudo-build``: ``PR_SET_DUMPABLE`` off,+``PR_SET_NO_NEW_PRIVS``, ``PR_SET_PTRACER`` cleared and ``RLIMIT_CORE`` zero.+``mlockall(MCL_CURRENT | MCL_FUTURE)`` keeps current and future mappings+out of swap for the process lifetime; failure is logged and refuses+startup. Sensitive buffers are scrubbed after use.+No host file may name code for it to load:OpenSSL reads no configuration file, so none can activate a providermodule, and host names resolve through glibc's built-in ``files`` and``dns`` services alone, so ``nsswitch.conf`` cannot add a modulediff --git a/Documentation/operations.rst b/Documentation/operations.rstindex e697cbef..74b4ff38 100644--- a/Documentation/operations.rst+++ b/Documentation/operations.rst@@ -61,6 +61,15 @@ and the ``map_export.*`` keys of ``/etc/shirudo/shirudo.conf``; theto receive it. ``shirudo config`` prints every effective setting witha comment.+Every ``shirudo`` and ``shirudo-build`` invocation locks its current and future process+mappings before command dispatch. The shipped services set+``LimitMEMLOCK=infinity``. Manual invocations need ``CAP_IPC_LOCK`` or+a sufficiently large memory-lock limit; a failure is logged and exits+with status 1 before the command runs. Future allocations and stack+growth also consume the locked-memory allowance. The locks end on+process exit or exec; the operator helper reapplies memory locking+before its own command dispatch.+State lives in two places. ``/var/lib/shirudo`` is persistent:``keys/`` (the primary certificate, the trusted set with its vouches,the revoked certificates), ``eventlog``, the ``upatches/`` archivediff --git a/Documentation/requirements.rst b/Documentation/requirements.rstindex 545348a5..53604c22 100644--- a/Documentation/requirements.rst+++ b/Documentation/requirements.rst@@ -338,6 +338,14 @@ since the libguestfs appliance resolves names through DNS alone.The operator never needs a TPM, the kernel keyring or ``CAP_BPF``; onlythe private key under ``/var/lib/shirudo/keys/``.+Both ``shirudo`` and ``shirudo-build`` lock all current and future+process mappings before command dispatch. Manual invocations and host+tests need ``CAP_IPC_LOCK`` or a memory-lock limit large enough for the+process, its libraries, mapped artifacts and subsequent allocations.+Startup refuses to run if locking fails. The shipped systemd services+set ``LimitMEMLOCK=infinity``. Locks remain for the process lifetime;+an exec clears them and the operator helper applies its own hardening.+5) Target host--------------diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 038189d5..34f1d1ee 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -184,7 +184,8 @@ T3 Tampering with shirudo's files and processRuntime executables reject traced callers and run insecure-exec mode, so the loader ignores unsafe environment variables.The process drops dumpability, forbids new privileges and locks- sensitive buffers. The bpf keyring is sealed with+ all current and future process mappings against swapping before command+ dispatch, refusing startup if locking fails. The bpf keyring is sealed with``KEYCTL_RESTRICT_KEYRING`` so no key can be linked into it for therest of the boot, ``lsm/key_permission`` leaves writing, unlinking,revoking, invalidating and re-permissioning it and the keys itdiff --git a/Documentation/tpm.rst b/Documentation/tpm.rstindex 648dc29b..29945d6f 100644--- a/Documentation/tpm.rst+++ b/Documentation/tpm.rst@@ -583,6 +583,9 @@ context and the deferred mitigations the plan.8) Testing----------+``testing/t0051-harden.sh`` checks process-wide locking, fail-closed+startup and the memory-lock limits of every shipped service.+``testing/tpm/t0033-tpm-wire.sh`` checks the policy digests, names, wireparsers, the key derivations, the endorsement policy and thecredential blob against known vectors and an independentdiff --git a/Makefile.shirudo b/Makefile.shirudoindex b63620d5..6741547c 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -599,6 +599,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \testing/helper/test-string \testing/helper/test-conditions \testing/helper/test-config-image \+ testing/helper/test-harden \testing/helper/test-chain \testing/helper/test-tpm-wire \testing/helper/test-register \@@ -635,6 +636,11 @@ testing/helper/test-conditions: testing/helper/test-conditions.c util/conditions@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^+testing/helper/test-harden: testing/helper/test-harden.c util/harden.o util/log.o \+ util/string.o util/malloc.o+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -o $@ $^ $(LIBCRYPTO_LIBS)+# chain.o's read/extend paths reference util/tpm (libtss2); the tests# only exercise the pure helpers, but the symbols must still resolve.# util/malloc.o brings in x*/libc_free that chain.o/tpm.o use.diff --git a/shirudo.c b/shirudo.cindex 443f82ad..ba75b9a7 100644--- a/shirudo.c+++ b/shirudo.c@@ -112,7 +112,8 @@ int main(int argc, const char **argv)harden_stdio();log_init();- harden_self();+ if (harden_self())+ return 1;if (argc < 2) {cmd_help(0, NULL);diff --git a/systemd/shirudo-bpm-repo-update.service.in b/systemd/shirudo-bpm-repo-update.service.inindex b3a2833f..903da3a9 100644--- a/systemd/shirudo-bpm-repo-update.service.in+++ b/systemd/shirudo-bpm-repo-update.service.in@@ -9,6 +9,7 @@ RequiresMountsFor=/etc/shirudo/bpm /var/lib/shirudo[Service]Type=oneshot+LimitMEMLOCK=infinityTimeoutStartSec=30minEnvironmentFile=-/etc/shirudo/bpm/environment# One unreachable registry fails the update; the upgrade still runsdiff --git a/systemd/shirudo-bpm-tree-sync.service.in b/systemd/shirudo-bpm-tree-sync.service.inindex 9259fa19..7a38854f 100644--- a/systemd/shirudo-bpm-tree-sync.service.in+++ b/systemd/shirudo-bpm-tree-sync.service.in@@ -9,6 +9,7 @@ RequiresMountsFor=/etc/shirudo/bpm /var/lib/shirudo[Service]Type=oneshot+LimitMEMLOCK=infinityTimeoutStartSec=30minEnvironmentFile=-/etc/shirudo/bpm/environmentExecStart=@bindir@/shirudo bpm tree syncdiff --git a/systemd/shirudo-initrd.service.in b/systemd/shirudo-initrd.service.inindex 154b770f..09513771 100644--- a/systemd/shirudo-initrd.service.in+++ b/systemd/shirudo-initrd.service.in@@ -31,6 +31,7 @@ RequiresMountsFor=/sysroot/var/lib/shirudo[Service]Type=oneshot+LimitMEMLOCK=infinityRemainAfterExit=yesKeyringMode=inheritExecStart=@bindir@/shirudo initdiff --git a/systemd/shirudo-keepalive.service.in b/systemd/shirudo-keepalive.service.inindex 48b3cba0..9488b47c 100644--- a/systemd/shirudo-keepalive.service.in+++ b/systemd/shirudo-keepalive.service.in@@ -7,6 +7,7 @@ After=shirudo.service[Service]Type=oneshot+LimitMEMLOCK=infinityTimeoutStartSec=2minEnvironmentFile=-/etc/shirudo/map-export.envExecStart=@bindir@/shirudo status --otel --include-systemdiff --git a/systemd/shirudo-map-export.service.in b/systemd/shirudo-map-export.service.inindex 8273f63b..e72a63d6 100644--- a/systemd/shirudo-map-export.service.in+++ b/systemd/shirudo-map-export.service.in@@ -8,6 +8,7 @@ ConditionPathExists=/run/shirudo/bpffs/lsm_link[Service]Type=oneshot+LimitMEMLOCK=infinityTimeoutStartSec=5minEnvironmentFile=-/etc/shirudo/map-export.envExecStart=@bindir@/shirudo upatch map --alldiff --git a/systemd/shirudo.service.in b/systemd/shirudo.service.inindex 6e56c708..845baefd 100644--- a/systemd/shirudo.service.in+++ b/systemd/shirudo.service.in@@ -25,6 +25,7 @@ ConditionPathIsMountPoint=/run/shirudo/runtime[Service]Type=oneshot+LimitMEMLOCK=infinityRemainAfterExit=yesKeyringMode=inheritEnvironmentFile=-/etc/shirudo/init.envdiff --git a/testing/helper/test-harden.c b/testing/helper/test-harden.cnew file mode 100644index 00000000..8ff9cc7d--- /dev/null+++ b/testing/helper/test-harden.c@@ -0,0 +1,59 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <stdio.h>+#include <string.h>+#include <sys/mman.h>+#include <sys/prctl.h>+#include <sys/resource.h>++#include "harden.h"++static int lock_error;+static int lock_flags;++/* Supply deterministic outcomes without locking the test process. */+int mlockall(int flags)+{+ lock_flags = flags;+ if (lock_error) {+ errno = lock_error;+ return -1;+ }+ return 0;+}++int main(int argc, char **argv)+{+ struct rlimit core = {};+ int rc;++ if (argc != 2)+ goto usage;+ if (!strcmp(argv[1], "success"))+ lock_error = 0;+ else if (!strcmp(argv[1], "denied"))+ lock_error = EPERM;+ else if (!strcmp(argv[1], "limit"))+ lock_error = ENOMEM;+ else+ goto usage;++ rc = harden_self();+ if (lock_flags != (MCL_CURRENT | MCL_FUTURE) || rc != -lock_error) {+ fprintf(stderr, "unexpected lock flags or result\n");+ return 2;+ }+ if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) != 0 ||+ prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) != 1 ||+ getrlimit(RLIMIT_CORE, &core) || core.rlim_cur || core.rlim_max) {+ fprintf(stderr, "existing startup hardening changed\n");+ return 2;+ }+ printf("rc=%d\n", rc);+ return rc ? 1 : 0;+usage:+ fprintf(stderr, "usage: %s success|denied|limit\n", argv[0]);+ return 2;+}diff --git a/testing/t0051-harden.sh b/testing/t0051-harden.shnew file mode 100755index 00000000..4cd3ef49--- /dev/null+++ b/testing/t0051-harden.sh@@ -0,0 +1,35 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++cd "$(dirname "$0")"+. ./test-lib.sh+H=helper/test-harden+W=$(mktemp -d) || exit 1+trap 'rm -f "$W/out" "$W/error"; rmdir "$W"' EXIT HUP INT TERM++test_expect_success 'startup locks current and future mappings and preserves hardening' '+ ./$H success | grep -qx "rc=0"+'++test_expect_success 'denied memory locking is reported and fails closed' '+ ! ./$H denied >"$W/out" 2>"$W/error" &&+ grep -qx "rc=-1" "$W/out" &&+ grep -q "^hardening: cannot lock process memory:" "$W/error"+'++test_expect_success 'exhausted memory-lock limits are reported and fail closed' '+ ! ./$H limit >"$W/out" 2>"$W/error" &&+ grep -qx "rc=-12" "$W/out" &&+ grep -q "^hardening: cannot lock process memory:" "$W/error"+'++test_expect_success 'all shipped services allow process-wide memory locking' '+ missing=0 &&+ for unit in ../systemd/*.service.in; do+ grep -qx "LimitMEMLOCK=infinity" "$unit" || missing=1+ done &&+ [ "$missing" -eq 0 ]+'++test_donediff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex e7d4ea63..e2451e30 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -208,7 +208,8 @@ int main(int argc, char **argv)harden_stdio();log_init();- harden_self();+ if (harden_self())+ return 1;if (argc >= 2 && strcmp(argv[1], "image") == 0)return shirudo_build_image(argc - 1, argv + 1);diff --git a/util/harden.c b/util/harden.cindex bf587240..291461b2 100644--- a/util/harden.c+++ b/util/harden.c@@ -2,13 +2,14 @@/** Copyright (c) 2026 Authors of shirudo*- * Process-level hardening: deny ptrace, block core dumps, mlock- * sensitive buffers.+ * Process-level hardening and process-wide memory locking.*/#include <errno.h>#include <fcntl.h>#include <nss.h>+#include <string.h>+#include <sys/mman.h>#include <sys/prctl.h>#include <sys/resource.h>#include <unistd.h>@@ -16,6 +17,7 @@#include <openssl/crypto.h>#include "harden.h"+#include "log.h"/** Started with standard input, output or error closed, the process gives@@ -43,9 +45,9 @@ void harden_stdio(void)* the environment. glibc's own files and dns services answer host* lookups without a module, in a static binary too.*/-void harden_self(void)+int harden_self(void){- struct rlimit no_core = { 0, 0 };+ struct rlimit no_core = {};prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);@@ -53,4 +55,11 @@ void harden_self(void)setrlimit(RLIMIT_CORE, &no_core);OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);__nss_configure_lookup("hosts", "files dns");+ if (mlockall(MCL_CURRENT | MCL_FUTURE)) {+ int err = -errno;++ pr_err("hardening: cannot lock process memory: %s", strerror(-err));+ return err;+ }+ return 0;}diff --git a/util/harden.h b/util/harden.hindex 60aaf922..712e94ed 100644--- a/util/harden.h+++ b/util/harden.h@@ -11,6 +11,6 @@#include <stddef.h>void harden_stdio(void);-void harden_self(void);+int harden_self(void);#endif /* SHIRUDO_HARDEN_H */