Skip to content

eBPF-based security platform

harden: drop the personality the caller passes down

Daniel Borkmann00f9fb81ee3e
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index b6c8c481..ef9bcf22 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -496,6 +496,9 @@ signed config image.
- The directories above the state root carry the ancestor mark; a
rename of one and the removal of the mark are refused and counted,
and a directory beside the state root still renames
+ * - ``t6151-personality``
+ - Started under ``setarch --uname-2.6``, shirudo still reads the
+ kernel's release
``bpm`` (7)
~~~~~~~~~~~
diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rst
index d38cc9ba..062b1b3b 100644
--- a/Documentation/internals/kernel-interfaces.rst
+++ b/Documentation/internals/kernel-interfaces.rst
@@ -421,6 +421,10 @@ Process setup and prctl
- Enforcement probe. ``lsm/task_prctl`` fails it with errno 1002
when ``bpf2`` is attached; ``EINVAL`` or ``ENOSYS`` means it is
not.
+ * - ``personality(PER_LINUX)``
+ - Drops the personality flags the caller passed down, ``UNAME26``
+ among them, which would have ``uname()`` report a 2.6 release to
+ the tree selector. A failure ends the command.
* - ``prctl(PR_SET_DUMPABLE, 0)``, ``prctl(PR_SET_NO_NEW_PRIVS, 1)``,
``prctl(PR_SET_PTRACER, 0)``
- Process hardening before every command.
diff --git a/Documentation/reference/cli/shirudo.rst b/Documentation/reference/cli/shirudo.rst
index b2c136a8..833168fa 100644
--- a/Documentation/reference/cli/shirudo.rst
+++ b/Documentation/reference/cli/shirudo.rst
@@ -85,12 +85,12 @@ Every invocation goes through the same steps before the command runs:
#. Standard input, output and error that are closed are reopened on
``/dev/null``.
-#. The process hardens itself: it is not dumpable, sets
- ``no_new_privs``, writes no core file, loads no OpenSSL configuration
- file, resolves host names through ``files`` and ``dns`` only and locks
- all its memory with ``mlockall()``. When the lock fails, ``shirudo``
- prints ``hardening: cannot lock process memory: <error>`` and exits
- with 1. The shipped systemd units set ``LimitMEMLOCK=infinity``.
+#. The process hardens itself: it drops the personality flags of its
+ caller, is not dumpable, sets ``no_new_privs``, writes no core file,
+ loads no OpenSSL configuration file, resolves host names through
+ ``files`` and ``dns`` only and locks all its memory with
+ ``mlockall()``. When the lock fails, ``shirudo`` prints
+ ``hardening: cannot lock process memory: <error>`` and exits with 1. The shipped systemd units set ``LimitMEMLOCK=infinity``.
#. ``shirudo`` resolves the command name (see below).
#. On a target where enforcement is up, the image gate runs (see below).
#. The command runs.
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index ec36e8c9..1ac20373 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -330,8 +330,9 @@ every verdict, ``tampered`` included. Parse ``.status`` of
* - Unknown or ambiguous command
- 1.
* - Any command
- - 1 when ``mlockall()`` fails (``hardening: cannot lock process
- memory``) or the gate refuses.
+ - 1 when the personality cannot be reset (``hardening: cannot reset
+ the personality``), when ``mlockall()`` fails (``hardening: cannot
+ lock process memory``) or when the gate refuses.
* - ``genkey``
- 0; 1 on failure or when keys exist without ``--force``; 2 for an
unknown option.
diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rst
index 883dc958..f9827160 100644
--- a/Documentation/security/threat-model.rst
+++ b/Documentation/security/threat-model.rst
@@ -1774,7 +1774,10 @@ U3 The environment reaches authorized processes
(libcurl), which open a file root names with the process's
authority, are refused on a target like shirudo's own overrides
(``t6136``): with the first set, ``shirudo status`` wrote the
- library's log into a sealed file.
+ library's log into a sealed file. The personality the caller sets
+ is dropped at start (``t6151``): under ``UNAME26``, ``uname()``
+ reported a 2.6 release to the tree selector, and an operation it
+ skipped for that release stayed skipped.
:Status: Confirmed in code for shirudo's own variables; the library
log file was reproduced on a test VM and is refused now.
:Meanwhile: Keep ``/etc/shirudo/bpm/environment``,
diff --git a/testing/functional/tamperproof/t6151-personality.sh b/testing/functional/tamperproof/t6151-personality.sh
new file mode 100755
index 00000000..ea7dab46
--- /dev/null
+++ b/testing/functional/tamperproof/t6151-personality.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t6151 -- shirudo drops the personality its caller gives it: started
+# under setarch --uname-2.6, which makes uname() report a 2.6 release, it
+# still reads the kernel's own, the one the tree selector compares.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+command -v setarch >/dev/null 2>&1 || test_skip_all "no setarch"
+setarch "$(uname -m)" --uname-2.6 true 2>/dev/null ||
+ test_skip_all "setarch cannot set UNAME26"
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+release() {
+ "$@" "$SHIRUDO_BIN" status --json --include-system 2>/dev/null |
+ python3 -c '
+import json, sys
+print(json.load(sys.stdin)["system"]["kernel_release"])
+'
+}
+
+test_expect_success 'the caller sees a 2.6 release under UNAME26' '
+ setarch "$(uname -m)" --uname-2.6 uname -r >"$W/caller" &&
+ grep -q "^2\.6\." "$W/caller"
+'
+test_expect_success 'shirudo reads the kernel release all the same' '
+ release env >"$W/plain" &&
+ release setarch "$(uname -m)" --uname-2.6 >"$W/uname26" &&
+ [ -s "$W/plain" ] &&
+ cmp -s "$W/plain" "$W/uname26" &&
+ ! grep -q "^2\.6\." "$W/uname26"
+'
+test_done
diff --git a/util/harden.c b/util/harden.c
index 291461b2..0147521d 100644
--- a/util/harden.c
+++ b/util/harden.c
@@ -10,6 +10,7 @@
#include <nss.h>
#include <string.h>
#include <sys/mman.h>
+#include <sys/personality.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <unistd.h>
@@ -43,12 +44,23 @@ void harden_stdio(void)
* can name an NSS module for the host lookups libcurl makes, both
* dlopen()ed from paths root controls whatever secure-exec mode does to
* the environment. glibc's own files and dns services answer host
- * lookups without a module, in a static binary too.
+ * lookups without a module, in a static binary too. The personality
+ * comes from the caller too: UNAME26 has uname() report a 2.6 release
+ * to the tree selector, READ_IMPLIES_EXEC makes later mappings
+ * executable.
*/
int harden_self(void)
{
struct rlimit no_core = {};
+ if (personality(PER_LINUX) < 0) {
+ int err = -errno;
+
+ pr_err("hardening: cannot reset the personality: %s",
+ strerror(-err));
+ return err;
+ }
+
prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
prctl(PR_SET_PTRACER, 0, 0, 0, 0);