eBPF-based security platform
upatch: take an install of the payload already loaded as done
Daniel Borkmannf12b1c99d0c1
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 08f9b2c9..4e6354e3 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -427,6 +427,9 @@ signed config image.* - ``t7104-bpm-tree-kconfig``- A kconfig selector the target cannot read stops the sync where itis; the operation applies once the config reads+ * - ``t7105-bpm-tree-loaded``+ - An add whose upatch is already loaded with its payload counts as+ installed; a direct load of it is still refused``otel`` (8)~~~~~~~~~~~~@@ -638,8 +641,8 @@ Coverage by area``bpm_oci``, ``bpm_package``* - upatch tree- ``t3011``, ``t5004``, ``t7005``, ``t7101``, ``t7102``,- ``t7103``, ``t7104``, ``t9105``. Fuzz: ``tree``, ``tree-state``,- ``kconfig``. Unit: ``bpm_tree``, ``bpm_tree_state``,+ ``t7103``, ``t7104``, ``t7105``, ``t9105``. Fuzz: ``tree``,+ ``tree-state``, ``kconfig``. Unit: ``bpm_tree``, ``bpm_tree_state``,``kernel_build_id``, ``kernel_release``* - Policy labels- upatch labels and Beefile serialization: ``t3109``, ``t7006``.@@ -700,7 +703,7 @@ The threats are those of :doc:`/security/threat-model`.- ``t1006``, ``t4101``, ``t6115``, ``t6126``* - T7 The delivery path- ``t7002``, ``t7003``, ``t7005``, ``t7101``, ``t7102``,- ``t7103``, ``t7104``, ``t8005``, ``t9105``+ ``t7103``, ``t7104``, ``t7105``, ``t8005``, ``t9105``* - T8 Quantum-capable adversaries- ``t2001``, ``t2002``, ``t5001``, ``t8005``* - T9 A target lying about its statediff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 6c381a5a..6de97f37 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -181,7 +181,8 @@ Categories- ``pin``- ``EEXIST``- A upatch of that name is loaded, with the payload a completed load- records. Nothing was touched.+ records. Nothing was touched. A ``bpm`` install or a tree sync of+ an artifact with that very payload counts it as installed instead.- Unload it with its signed directive first.* - ``payload_mismatch``- ``pin``diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 25e45c24..822aa40b 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -545,7 +545,9 @@ not published.``operation_digest`` while an operation is in progress. The nextsync completes the operation without applying it again when thetree eventlog or this boot's eventlog already records it, and- applies it again otherwise.+ applies it again otherwise. An ``add`` applied again whose upatch+ a stopped sync already loaded with its payload counts as+ installed.* - ``artifacts/<hex>.upatch``- Fetched artifacts, named by their layer digest.* - ``status/<id>``diff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 8ec3cfce..75a2f171 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1037,7 +1037,8 @@ revocation does not reach tree publications (U13).kernel verifies each loader (T1):Refusal: the ``bpm`` command fails and installs nothing:Counters: none-:Tests: ``t7005``, ``t7101``, ``t7102``, ``t7103``, ``t7104``, ``t8005``+:Tests: ``t7005``, ``t7101``, ``t7102``, ``t7103``, ``t7104``,+ ``t7105``, ``t8005``:Proofs: ``bpm-tar-header``, ``bpm-tar-octal``, ``bpm-archive-path``,``bpm-oci-challenge``, ``bpm-oci-token``, ``bpm-oci-header``,``bpm-oci-descriptor``, ``bpm-semver-model``, ``Shirudo.SemVer``,diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex da5de776..891385fa 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -169,7 +169,9 @@ the exact build its signer made it for and nothing else. When nothing ofthat name is pinned, the result is ``already_unloaded`` and nothing ismeasured. The other way round, a load of an artifact whose upatch isloaded is refused as ``already_loaded`` before anything is touched: a-second copy would rebind the serializers to maps nothing reads.+second copy would rebind the serializers to maps nothing reads. ``bpm``+takes an artifact whose own payload is the one loaded as installed, so+that it can retry an install a stop cut off after the load.Build every unload directive with ``--payload-sha256`` set to the digestof the load it undoes. Without the option, the helper looks for andiff --git a/builtin-upatch.c b/builtin-upatch.cindex b8b0f567..1fc16c2c 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -2781,31 +2781,31 @@ static int undo_unmeasured(struct op_report *rep, const char *op)return ret;}-static int cmd_upatch_load(int argc, const char **argv)+struct load_args {+ const char *file;+ const char *expect_chain;+ const char *expect_version_id;+ const char *report;+ bool no_precheck;+ bool no_version_check;+ bool loaded_is_done;+};++static int upatch_load(const struct load_args *args){- const char *file, *expect_chain, *expect_version_id, *report;+ const char *file = args->file, *report = args->report;char name[UPATCH_NAME_MAX + 1] = {};__u8 payload[UPATCH_PAYLOAD_HASH_LEN], live[UPATCH_PAYLOAD_HASH_LEN];char staged[PATH_MAX] = {};struct upatch u;struct op_report rep = { .op = "upatch_load", .phase = "done" };- bool no_precheck, no_version_check, have_id = false, measured;+ bool have_id = false, measured;int err, lock = -1, ret = 1;- for (int i = 1; i < argc; i++) {- if (is_help(argv[i])) {- print_help(&upatch_load_help);- return 0;- }- }- err = parse_op_args(argc, argv, "load", &upatch_load_help,- &file, &expect_chain, &expect_version_id,- &no_precheck, &no_version_check, &report);- if (err)- return err;rep.path = file;- if (version_gate(expect_version_id, no_version_check, &rep, "load") != 0)+ if (version_gate(args->expect_version_id, args->no_version_check, &rep,+ "load") != 0)goto done;err = stage_upatch(file, staged, sizeof(staged));@@ -2831,10 +2831,10 @@ static int cmd_upatch_load(int argc, const char **argv)upatch_close(&u);have_id = true;- if (no_precheck) {+ if (args->no_precheck) {pr_warn("upatch load: --no-precheck, pre-flight state check ""skipped");- } else if (upatch_preflight(expect_chain, &rep) != 0) {+ } else if (upatch_preflight(args->expect_chain, &rep) != 0) {if (!rep.category)rep_fail(&rep, "pre_flight", "tampered", EPERM,"pre-flight check failed");@@ -2861,12 +2861,23 @@ static int cmd_upatch_load(int argc, const char **argv)if (undo_unmeasured(&rep, "load"))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.+ * Past the undo pass a payload on the pins is a load this boot's+ * records hold, 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. An install of the very payload loaded is done: bpm retries one+ * that a stop cut off between this load and its own record.*/if (have_id && !get_pin_payload(name, live)) {+ if (args->loaded_is_done &&+ !memcmp(live, payload, sizeof(live))) {+ pr_info("upatch load: %s is already loaded with this payload",+ name);+ rep.ok = true;+ rep.category = "already_loaded";+ ret = 0;+ goto done;+ }pr_err("upatch load: %s is already loaded; unload it with its signed directive first",name);rep_fail(&rep, "pin", "already_loaded", EEXIST,@@ -2931,6 +2942,26 @@ done:return ret;}+static int cmd_upatch_load(int argc, const char **argv)+{+ struct load_args args = {};+ int err;++ for (int i = 1; i < argc; i++) {+ if (is_help(argv[i])) {+ print_help(&upatch_load_help);+ return 0;+ }+ }+ err = parse_op_args(argc, argv, "load", &upatch_load_help,+ &args.file, &args.expect_chain,+ &args.expect_version_id, &args.no_precheck,+ &args.no_version_check, &args.report);+ if (err)+ return err;+ return upatch_load(&args);+}+static int cmd_upatch_unload(int argc, const char **argv){const char *file, *expect_chain, *expect_version_id, *report;@@ -3224,12 +3255,19 @@ int upatch_purge(const char *name, const struct cert *signer,return ret;}-/* Package installs take the same path as the upatch commands. */+/*+ * Package installs take the same path as the upatch commands, and take+ * the payload already loaded under the name for one done.+ */int upatch_install(const char *path){- const char *argv[] = { "load", "--no-precheck", path };+ const struct load_args args = {+ .file = path,+ .no_precheck = true,+ .loaded_is_done = true,+ };- return cmd_upatch_load(3, argv);+ return upatch_load(&args);}int upatch_remove(const char *path)diff --git a/testing/functional/bpm/t7105-bpm-tree-loaded.sh b/testing/functional/bpm/t7105-bpm-tree-loaded.shnew file mode 100755index 00000000..7d12f0a6--- /dev/null+++ b/testing/functional/bpm/t7105-bpm-tree-loaded.sh@@ -0,0 +1,68 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t7105 -- an add whose upatch is already loaded with its payload, as a+# sync stopped between the load and its tree record leaves it, counts as+# installed, while a direct load of it is still refused.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard++FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+REGISTRY="$FIXTURES/tree-oci"+TREE_REPO="$REGISTRY/shirudo/upatch-tree"+MATCH=$FIXTURES/shirudo-test-tree-match.upatch+MATCH_PIN=/run/shirudo/bpffs/upatches/shirudo-test-tree-match/link+WORK=$(mktemp -d)++for fixture in \+ "$TREE_REPO/manifests/reloaded" \+ "$MATCH" \+ "$FIXTURES/shirudo-test-tree-match.unload.upatch" \+ "$FIXTURES/cert.der" \+ "./helper/test-oci-registry.py"+do+ [ -s "$fixture" ] || test_skip_all "missing tree fixture: $fixture"+done++[ "$(tree_state_field tree_id)" = shirudo-test-tree ] &&+ [ "$(tree_state_field last_seq)" = 6 ] && [ ! -e "$MATCH_PIN" ] ||+ test_skip_all "tree state is not where t7104 leaves it"++cleanup()+{+ tree_registry_stop+ if [ -e "$MATCH_PIN" ]; then+ "$SHIRUDO_BIN" upatch unload \+ "$FIXTURES/shirudo-test-tree-match.unload.upatch" \+ --no-precheck >/dev/null 2>&1 || true+ fi+ tree_cleanup_config+ rm -rf "$WORK"+}+trap cleanup EXIT++tree_configure reloaded+tree_registry_start "$REGISTRY" "$WORK"++test_expect_success 'the upatch of the next add is loaded by hand first' '+ "$SHIRUDO_BIN" upatch load "$MATCH" --no-precheck >/dev/null 2>&1 &&+ [ -e "$MATCH_PIN" ]+'++test_expect_success 'a direct load of the same artifact is still refused' '+ ! "$SHIRUDO_BIN" upatch load "$MATCH" --no-precheck \+ >/dev/null 2>"$WORK/again.err" &&+ grep -q "is already loaded; unload it" "$WORK/again.err"+'++test_expect_success 'the sync counts the loaded payload as installed' '+ tree_sync "$WORK/applied.json" &&+ [ "$(report_field applied <"$WORK/applied.json")" = 1 ] &&+ [ "$(report_field last_seq <"$WORK/applied.json")" = 7 ] &&+ [ -e "$MATCH_PIN" ]+'++test_donediff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.shindex d480ae1b..00225639 100755--- a/testing/vm/build-bundle.sh+++ b/testing/vm/build-bundle.sh@@ -541,8 +541,11 @@ operations = [operation(5, "shirudo-test-tree-match", this_node, match_artifact),operation(6, "shirudo-test-tree-match", kconfig_node,match_unload_artifact, op="delete"),+ # t7105 loads this upatch by hand first.+ operation(7, "shirudo-test-tree-match", this_node, match_artifact),]-references = ("stable", "updated", "deleted", "restored", "kconfig")+references = ("stable", "updated", "deleted", "restored", "kconfig",+ "reloaded")for generation, reference in enumerate(references, start=1):head = generation + 1publication = {@@ -558,7 +561,7 @@ for generation, reference in enumerate(references, start=1):.encode())PY- for reference in stable updated deleted restored kconfig; do+ for reference in stable updated deleted restored kconfig reloaded; doopenssl cms -sign -binary -md sha512 \-in "$payloads-$reference.json" \-signer "$FIXTURE_WORK/trusted/cert.der" \@@ -586,7 +589,8 @@ def write(path, body):path.write_bytes(body)-for reference in ("stable", "updated", "deleted", "restored", "kconfig"):+for reference in ("stable", "updated", "deleted", "restored", "kconfig",+ "reloaded"):stem = payloads.name + "-" + referencepayload = payloads.with_name(stem + ".json").read_bytes()signature = payloads.with_name(stem + ".sig").read_bytes()diff --git a/testing/vm/image-fixtures.sh b/testing/vm/image-fixtures.shindex 59c9e033..31dd3e75 100755--- a/testing/vm/image-fixtures.sh+++ b/testing/vm/image-fixtures.sh@@ -66,7 +66,7 @@ cp "$keys_dir/cert.der" "$output/public/primary.der"for name in config-boot-keys config-live-new-key config-a config-b config-bad config-revoke \config-tree-stable config-tree-updated config-tree-deleted config-tree-restored \- config-tree-kconfig \+ config-tree-kconfig config-tree-reloaded \config-otel config-jsonl config-jsonl-bad config-interrupted config-end2end \config-builtin; do[ "$name" != config-builtin ] || [ -n "$builtin_key" ] || continue