Skip to content

eBPF-based security platform

upatch: refuse to load a upatch that is already loaded

Daniel Borkmann1f0280bfeae0
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 44dad6b0..6f652273 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -182,6 +182,8 @@ signed config image.
multiple policy bits with a preexisting xattr; its signed policy
section binds verdict programs to shared task and socket policy
maps
+ * - ``t3110-double-load``
+ - A second load of a loaded upatch is refused as ``already_loaded``
``tpm`` (4)
~~~~~~~~~~~
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index 1c3d58d1..d39a84e5 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -176,6 +176,12 @@ Categories
priority, a kprobe at another function or offset.
- Run the load in the host's initial cgroup and network namespaces
and with the host's sysfs.
+ * - ``already_loaded``
+ - ``pin``
+ - ``EEXIST``
+ - A upatch of that name is loaded, with the payload a completed load
+ records. Nothing was touched.
+ - Unload it with its signed directive first.
* - ``payload_mismatch``
- ``pin``
- ``EPERM``, errno of the read
diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rst
index 690e5e9f..de3fd96a 100644
--- a/Documentation/upatches/learn/concepts.rst
+++ b/Documentation/upatches/learn/concepts.rst
@@ -167,7 +167,9 @@ signed digest with the payload attribute of the pin directory. A
different digest fails with ``payload_mismatch``, so a directive removes
the exact build its signer made it for and nothing else. When nothing of
that name is pinned, the result is ``already_unloaded`` and nothing is
-measured.
+measured. The other way round, a load of an artifact whose upatch is
+loaded is refused as ``already_loaded`` before anything is touched: a
+second copy would rebind the serializers to maps nothing reads.
Build every unload directive with ``--payload-sha256`` set to the digest
of the load it undoes. Without the option, the helper looks for an
diff --git a/Documentation/upatches/test/debugging.rst b/Documentation/upatches/test/debugging.rst
index 710219f4..8fa6bfb2 100644
--- a/Documentation/upatches/test/debugging.rst
+++ b/Documentation/upatches/test/debugging.rst
@@ -179,6 +179,10 @@ Find the cause
expects, or its recorded digest cannot be read. Build the
directive with ``--payload-sha256`` set to the digest of the
loaded build, from ``shirudo upatch info --json``.
+ * - ``pin``
+ - ``already_loaded``
+ - A upatch of that name is loaded. Unload it with its signed
+ directive first; nothing was touched.
* - ``done``
- ``already_unloaded``
- Nothing of that name is loaded. Exit status 0, nothing measured.
diff --git a/builtin-upatch.c b/builtin-upatch.c
index 6166faad..26b7db1f 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2796,8 +2796,8 @@ static int cmd_upatch_load(int argc, const char **argv)
{
const char *file, *expect_chain, *expect_version_id, *report;
char name[UPATCH_NAME_MAX + 1] = {};
+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN], live[UPATCH_PAYLOAD_HASH_LEN];
char staged[PATH_MAX] = {};
- __u8 payload[UPATCH_PAYLOAD_HASH_LEN];
struct upatch u;
struct op_report rep = { .op = "upatch_load", .phase = "done" };
bool no_precheck, no_version_check, have_id = false, measured;
@@ -2861,6 +2861,19 @@ static int cmd_upatch_load(int argc, const char **argv)
"unsafe upatch name");
goto done;
}
+ /*
+ * Only a completed load records the payload and only an unload
+ * removes it. A load over a live upatch rebinds its serializer pins
+ * to maps nothing reads, and an undo of that load would take the
+ * live one's pins and archive with it.
+ */
+ if (have_id && !get_pin_payload(name, live)) {
+ pr_err("upatch load: %s is already loaded; unload it with its signed directive first",
+ name);
+ rep_fail(&rep, "pin", "already_loaded", EEXIST,
+ "upatch already loaded");
+ goto done;
+ }
if (__upatch_apply(file, false, &rep) != 0)
goto done;
diff --git a/testing/functional/upatch/t3110-double-load.sh b/testing/functional/upatch/t3110-double-load.sh
new file mode 100755
index 00000000..bf74df73
--- /dev/null
+++ b/testing/functional/upatch/t3110-double-load.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t3110 -- a second load of a loaded upatch is refused as already_loaded
+# and leaves the loaded one, its pins and its serializers as they are.
+
+cd "$(dirname "$0")/../.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+
+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}
+EVENTLOG=/var/lib/shirudo/eventlog
+PINS=/run/shirudo/bpffs/upatches
+BASIC=shirudo-test-basic
+SERIAL=shirudo-test-serialized-metric
+
+for n in $BASIC $SERIAL; do
+ [ -s "$FIXTURES/$n.upatch" ] && [ -s "$FIXTURES/$n.unload.upatch" ] ||
+ test_skip_all "missing fixture $n"
+done
+
+restore() {
+ for n in $BASIC $SERIAL; do
+ [ -d "$PINS/$n" ] || continue
+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$n.unload.upatch" \
+ --no-precheck >/dev/null 2>&1 || true
+ done
+}
+trap restore EXIT HUP INT TERM
+
+load_events() {
+ grep -c "\"upatch_load\".*\"$1\"" "$EVENTLOG"
+}
+
+second_load() {
+ "$SHIRUDO_BIN" upatch load "$FIXTURES/$1.upatch" --no-precheck \
+ --report=- 2>/dev/null | report_field category
+}
+
+test_expect_success 'a second load of a upatch with links is refused and detaches nothing' '
+ require_loaded $BASIC &&
+ before=$(load_events $BASIC) &&
+ [ "$(second_load $BASIC)" = already_loaded ] &&
+ [ -e "$PINS/$BASIC/link" ] &&
+ [ "$(load_events $BASIC)" = "$before" ]
+'
+
+test_expect_success 'a second load of a upatch without links keeps its serializer bound' '
+ require_loaded $SERIAL &&
+ before=$(load_events $SERIAL) &&
+ [ "$(second_load $SERIAL)" = already_loaded ] &&
+ [ -e "$PINS/$SERIAL/serializers/counters" ] &&
+ [ "$(load_events $SERIAL)" = "$before" ] &&
+ ! "$SHIRUDO_BIN" upatch map $SERIAL counters 2>&1 |
+ grep -q "Bad message"
+'
+
+test_done
diff --git a/util/report.h b/util/report.h
index 666df251..db90f8e6 100644
--- a/util/report.h
+++ b/util/report.h
@@ -21,8 +21,8 @@
* pin, persist, tpm_extend, done
* Category strings: ok, tampered, keyring_missing, lsm_denied,
* signature_rejected, verifier_rejected, parser_rejected,
- * payload_mismatch, already_unloaded, detach_failed, tpm_error,
- * unknown.
+ * payload_mismatch, already_loaded, already_unloaded, detach_failed,
+ * tpm_error, unknown.
*/
struct op_report {
bool ok;