eBPF-based security platform
bpm: publish Java upatches through a tree
John Fastabendd559721ffc5e
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex ca7521da..d5b9538b 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -599,7 +599,8 @@ signed config image.refusal of a target below another member and of members a targetcould not unpack together, ``bpm push`` with a signed index,``bpm tree append`` and its refusal of an XDP device class two- upatches of a tree select and of an entry no target would take,+ upatches of a tree select, which a Java upatch beside them does+ not trip, and of an entry no target would take,each before anything is uploaded, its refusal to extend a treewithout ``--tree-id`` or with another tree's id, A-to-B treesigning-key rotation with explicit predecessor verification@@ -713,6 +714,11 @@ signed config image.- The signed layers of a publication served under a manifest thatis not the one ``bpm tree append`` writes are refused before thenode records anything, and the publisher's form is taken again+ * - ``t7112-bpm-tree-java``+ - A Java upatch through the tree: an ``add`` installs its jar and+ names it in ``JAVA_TOOL_OPTIONS`` under a ``java_load``, a+ ``delete`` takes the agent and the options out under a+ ``java_unload``* - Test- Covers* - ``t8001-otel-map``diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex 00cd0f0e..ea9eb1b3 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -323,7 +323,7 @@ selectors of the two operations do not exclude each other. A device runsone native XDP program, so the second upatch would fail its load on everynode with such a device and ``tree sync`` would stop there. The messagenames both upatches, the sequence number of the earlier operation and the-shared classes, and nothing is published.+shared classes, and nothing is published. A Java upatch selects no class.- Only an ``add`` that no later ``delete`` withdrew counts. A ``delete``withdraws it when its selector reaches every node the ``add`` reaches:diff --git a/Documentation/reference/formats/beefile.rst b/Documentation/reference/formats/beefile.rstindex d182fe1a..d57abe5e 100644--- a/Documentation/reference/formats/beefile.rst+++ b/Documentation/reference/formats/beefile.rst@@ -215,8 +215,8 @@ from the artifact's payload digest, so a package always ships both.- no- ``bpf``, the default, or ``java``, in any case. A ``java`` upatchsigns its ``.jar`` source as a Java agent (see- :doc:`/reference/cli/build`) and takes no ``[[upatches.rodata]]``,- ``[[upatches.policy]]`` or ``[upatches.tree]``.+ :doc:`/reference/cli/build`) and takes no ``[[upatches.rodata]]``+ or ``[[upatches.policy]]``.* - ``load_target``- string- nodiff --git a/Documentation/upatches/write/java-agents.rst b/Documentation/upatches/write/java-agents.rstindex ebd9bc6c..61b82de0 100644--- a/Documentation/upatches/write/java-agents.rst+++ b/Documentation/upatches/write/java-agents.rst@@ -21,9 +21,10 @@ Build and ship``shirudo build --unload agent`` signs its directive (see:doc:`/reference/cli/build`). In a package, a Beefile upatch with``type = "java"`` and a ``.jar`` source does the same (see-:doc:`/reference/formats/beefile`). The container carries no loader: its-signed data holds the name, the payload type and the jar-(:doc:`/reference/formats/upatch-format`).+:doc:`/reference/formats/beefile`), and its ``[upatches.tree]`` entry+publishes it through a tree as it does a BPF upatch. The container+carries no loader: its signed data holds the name, the payload type and+the jar (:doc:`/reference/formats/upatch-format`).The jar must name a ``Premain-Class`` in its manifest, and with itssignature fit the 16 MiB a ``.upatch`` may have; ``build`` refuses one@@ -32,8 +33,8 @@ that does not.On the target--------------``shirudo upatch load agent.upatch``, or a ``bpm install`` of the-package:+``shirudo upatch load agent.upatch``, a ``bpm install`` of the+package, or a ``bpm tree sync`` that takes its ``add``:#. checks the signature over the signed data against the certificatesof the authenticated runtime that the ``.bpf`` keyring holds, as the@@ -46,15 +47,15 @@ package:upatch, in name order, appended to the ``JAVA_TOOL_OPTIONS`` that``environment.d`` files sorting before it set.-``shirudo upatch unload agent.unload.upatch`` measures a ``java_unload``-event, replaces the jar with one whose agent does nothing, removes the-archive, and rewrites the options without the agent. At boot ``init``-verifies each archived Java upatch the eventlog records as loaded and-writes its jar and the options, which ``/run`` carries to the root file-system. A later run of ``init`` in the same boot leaves a jar that is-the archived one in place, and refuses one that is missing or another as-tampered, as it refuses a BPF upatch without its pins. ``upatch list``-and ``status`` show a Java+``shirudo upatch unload agent.unload.upatch``, or a tree ``delete``,+measures a ``java_unload`` event, replaces the jar with one whose agent+does nothing, removes the archive, and rewrites the options without the+agent. At boot ``init`` verifies each archived Java upatch the eventlog+records as loaded and writes its jar and the options, which ``/run``+carries to the root file system. A later run of ``init`` in the same+boot leaves a jar that is the archived one in place, and refuses one+that is missing or another as tampered, as it refuses a BPF upatch+without its pins. ``upatch list`` and ``status`` show a Javaupatch loaded while its installed jar is the archived one; ``status``reports one whose jar is missing or another under ``UpatchesMeasured``,and a jar no archive backs, which a load stopped before its archivediff --git a/builtin-bpm.c b/builtin-bpm.cindex 0d32207d..bfe44c1e 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -46,6 +46,7 @@#include "util/hex.h"#include "util/io.h"#include "util/initrd.h"+#include "util/java_agent.h"#include "util/image_config.h"#include "util/image_keys.h"#include "util/keys.h"@@ -2158,7 +2159,8 @@ static int tree_recover_pending(const struct tree_publication *pub,history_root);if (ret)return ret;- absent = !strcmp(op->op, "add") && !upatch_loaded_pins(op->id);+ absent = !strcmp(op->op, "add") && !upatch_loaded_pins(op->id) &&+ !java_agent_loaded(op->id);ret = tree_outcome_record(op->seq, digest,absent ? TREE_OUTCOME_MODULE_ABSENT :TREE_OUTCOME_APPLIED,diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.shindex 4aa821de..09a08dd7 100644--- a/testing/functional/bpm/t7005-bpm-publish.sh+++ b/testing/functional/bpm/t7005-bpm-publish.sh@@ -545,6 +545,26 @@ test_expect_success 'tree append takes a shared class on selectors that excludexdp_refused t7005xf "t7005xb (seq 3)" 2'+# A Java upatch has no XDP program: its add selects no class, and an XDP+# add whose selector meets it reads it without refusing either.+test_expect_success 'tree append takes a Java upatch beside the XDP upatches' '+ mkdir -p "$WORK/t7005ja" &&+ python3 -I -c "+import sys, zipfile+with zipfile.ZipFile(sys.argv[1], \"w\") as z:+ z.writestr(\"META-INF/MANIFEST.MF\", \"Manifest-Version: 1.0\\nPremain-Class: A\\n\")+" "$WORK/t7005ja/t7005ja.jar" &&+ printf "[package]\nname = \"t7005ja\"\n\n[[upatches]]\nsource = \"t7005ja.jar\"\ntype = \"java\"\n\n[upatches.tree]\nid = \"t7005ja\"\n" \+ >"$WORK/t7005ja/Beefile" &&+ ( cd "$WORK" && "$S" bpm build t7005ja --version 1.0.0 \+ --keys-dir "$WORK/keys" ) >&2 &&+ xdp_append t7005ja &&+ ls "$REG/ns/xdp/manifests" | grep -q "^upatch-t7005ja-" &&+ xdp_package t7005xj 1 "$(site \"z\")" &&+ xdp_append t7005xj &&+ xdp_append t7005ja --op=delete+'+test_expect_success 'tree append refuses two upatches of one package that select a class' 'xdp_package t7005xg 16 "" t7005xh &&! "$S" bpm tree append "$WORK/t7005xg-1.0.0.bee" "oci://$REGISTRY/ns/xdp2" \diff --git a/testing/functional/bpm/t7112-bpm-tree-java.sh b/testing/functional/bpm/t7112-bpm-tree-java.shnew file mode 100755index 00000000..1cb1286e--- /dev/null+++ b/testing/functional/bpm/t7112-bpm-tree-java.sh@@ -0,0 +1,90 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t7112 -- a Java upatch through the tree: an add installs its jar and+# names it in JAVA_TOOL_OPTIONS under a java_load, and a delete takes the+# agent and the options out again under a java_unload. Runs after t7111,+# on the tree it left.++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}+REGISTRY="$FIXTURES/tree-oci"+TREE_REPO="$REGISTRY/shirudo/upatch-tree"+NAME=shirudo-test-java+JAR=/run/shirudo-java/$NAME.jar+ARCHIVE=/var/lib/shirudo/upatches/$NAME.upatch+ENV_FILE=/run/environment.d/shirudo.conf+EVENTLOG=/var/lib/shirudo/eventlog+WORK=$(mktemp -d)++for fixture in \+ "$TREE_REPO/manifests/java-added" \+ "$TREE_REPO/manifests/java-deleted" \+ "$FIXTURES/$NAME.jar" \+ "$FIXTURES/$NAME.unload.upatch" \+ "$SHIRUDO_TEST_KEY_FIXTURES/config-tree-java-added.img" \+ "$SHIRUDO_TEST_KEY_FIXTURES/config-tree-java-deleted.img" \+ "./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)" = 22 ] &&+ [ ! -e "$ARCHIVE" ] ||+ test_skip_all "tree state is not where t7111 leaves it"++cleanup()+{+ tree_registry_stop+ if [ -e "$ARCHIVE" ]; then+ "$SHIRUDO_BIN" upatch unload "$FIXTURES/$NAME.unload.upatch" \+ --no-precheck >/dev/null 2>&1 || true+ fi+ tree_cleanup_config+ rm -rf "$WORK"+}+trap cleanup EXIT++# Whether the eventlog holds an event of type $1 for the upatch.+has_event() {+ python3 - "$EVENTLOG" "$1" "$NAME" <<-'PY'+ import json, sys+ for line in open(sys.argv[1], encoding="utf-8"):+ r = json.loads(line)+ if r["type"] == sys.argv[2] and \+ r.get("upatch", {}).get("name") == sys.argv[3]:+ sys.exit(0)+ sys.exit(1)+ PY+}++tree_registry_start "$REGISTRY" "$WORK"++test_expect_success 'an add installs the jar and names it in the options' '+ tree_configure java-added &&+ tree_sync "$WORK/added.json" &&+ [ "$(report_field applied <"$WORK/added.json")" = 1 ] &&+ [ "$(report_field last_seq <"$WORK/added.json")" = 23 ] &&+ cmp "$JAR" "$FIXTURES/$NAME.jar" &&+ grep -qxF "JAVA_TOOL_OPTIONS=\"\${JAVA_TOOL_OPTIONS:+\$JAVA_TOOL_OPTIONS }-javaagent:$JAR\"" "$ENV_FILE" &&+ has_event java_load+'++test_expect_success 'a delete takes the agent and the options out' '+ tree_configure java-deleted &&+ tree_sync "$WORK/deleted.json" &&+ [ "$(report_field applied <"$WORK/deleted.json")" = 1 ] &&+ [ "$(report_field last_seq <"$WORK/deleted.json")" = 24 ] &&+ ! cmp -s "$JAR" "$FIXTURES/$NAME.jar" &&+ [ ! -e "$ENV_FILE" ] &&+ [ ! -e "$ARCHIVE" ] &&+ has_event java_unload+'++test_donediff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.cindex 6a1a6ece..3690a0d2 100644--- a/testing/proofs/cbmc/bpm-tree-sync-model.c+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c@@ -589,6 +589,12 @@ bool upatch_loaded_pins(const char *name)return pins_loaded;}+/* The model's upatches are BPF ones. */+bool java_agent_loaded(const char *name)+{+ return false;+}+int tree_pending_write(const struct tree_operation *op){struct write w = { .kind = W_PENDING, .op = op_of(op) };diff --git a/testing/vm/build-bundle-payload.sh b/testing/vm/build-bundle-payload.shindex b9727d2c..be9594e3 100755--- a/testing/vm/build-bundle-payload.sh+++ b/testing/vm/build-bundle-payload.sh@@ -197,7 +197,7 @@ build_tree_test_registry()local payloads="$FIXTURE_WORK/tree-payload"local reference- # One append-only tree that t7101 to t7107 and t7109 walk in order:+ # One append-only tree that t7101 to t7107, t7109 and t7112 walk in order:# the tree state a target writes is sealed, so no test can start# over, and each generation extends the one before it.python3 - "$registry_dir" \@@ -208,7 +208,10 @@ build_tree_test_registry()"$tree_match_unload_payload" "$TEST_VM_ARCH" \"$TMP_DIR/fixtures/shirudo-test-tree-skip.unload.upatch" \"$TMP_DIR/fixtures/shirudo-test-uprobe-multi.upatch" \- "$tree_skip_unload_payload" "$uprobe_payload" <<'PY'+ "$tree_skip_unload_payload" "$uprobe_payload" \+ "$TMP_DIR/fixtures/shirudo-test-java.upatch" \+ "$TMP_DIR/fixtures/shirudo-test-java.unload.upatch" \+ "$java_payload" "$java_unload_payload" <<'PY'import hashlibimport jsonimport pathlib@@ -219,6 +222,8 @@ registry, match_path, skip_path, match_unload_path, payloads = \match_payload, skip_payload, match_unload_payload, node_arch = sys.argv[6:10]skip_unload_path, absent_path = map(pathlib.Path, sys.argv[10:12])skip_unload_payload, absent_payload = sys.argv[12:14]+java_path, java_unload_path = map(pathlib.Path, sys.argv[14:16])+java_payload, java_unload_payload = sys.argv[16:18]def digest(body):return "sha256:" + hashlib.sha256(body).hexdigest()@@ -287,6 +292,11 @@ skip_unload_artifact = artifact("shirudo/tree-skip-unload", skip_unload_path,# Its uprobe target is a file the bpm family's VM does not have.absent_artifact = artifact("shirudo/tree-absent", absent_path,"shirudo-test-uprobe-multi.upatch", absent_payload)+java_artifact = artifact("shirudo/tree-java", java_path,+ "shirudo-test-java.upatch", java_payload)+java_unload_artifact = artifact("shirudo/tree-java-unload", java_unload_path,+ "shirudo-test-java.unload.upatch",+ java_unload_payload)this_node = {"architecture": [node_arch], "labels": {"env": ["tree-test"]}}other_node = {"architecture": [node_arch],@@ -333,10 +343,15 @@ operations = [operation(21, "shirudo-test-tree-skip", other_node,skip_unload_artifact, op="delete"),operation(22, "shirudo-test-tree-skip", other_node, skip_artifact),+ # t7112: a Java upatch added and deleted.+ operation(23, "shirudo-test-java", this_node, java_artifact),+ operation(24, "shirudo-test-java", this_node, java_unload_artifact,+ op="delete"),]heads = {"stable": 2, "updated": 3, "deleted": 4, "restored": 5,"kconfig": 6, "reloaded": 7, "outcomes": 10, "stopped": 12,- "fetched": 13, "fixes": 16, "keepalive": 19, "unmatched": 22}+ "fetched": 13, "fixes": 16, "keepalive": 19, "unmatched": 22,+ "java-added": 23, "java-deleted": 24}for generation, (reference, head) in enumerate(heads.items(), start=1):publication = {"schema": 1,@@ -352,7 +367,8 @@ for generation, (reference, head) in enumerate(heads.items(), start=1):PYfor reference in stable updated deleted restored kconfig reloaded \- outcomes stopped fetched fixes keepalive unmatched; do+ outcomes stopped fetched fixes keepalive unmatched \+ java-added java-deleted; doopenssl cms -sign -binary -md sha512 \-in "$payloads-$reference.json" \-signer "$FIXTURE_WORK/trusted/cert.der" \@@ -383,7 +399,7 @@ empty_config = b"{}"write(repo / "blobs" / digest(empty_config), empty_config)for reference in ("stable", "updated", "deleted", "restored", "kconfig","reloaded", "outcomes", "stopped", "fetched", "fixes",- "keepalive", "unmatched"):+ "keepalive", "unmatched", "java-added", "java-deleted"):stem = payloads.name + "-" + referencepayload = payloads.with_name(stem + ".json").read_bytes()signature = payloads.with_name(stem + ".sig").read_bytes()@@ -915,6 +931,12 @@ tree_match_unload_payload="$($REPO_ROOT/shirudo upatch info \tree_skip_unload_payload="$($REPO_ROOT/shirudo upatch info \"$TMP_DIR/fixtures/shirudo-test-tree-skip.unload.upatch" --json |python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+java_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/shirudo-test-java.upatch" --json |+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+java_unload_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/shirudo-test-java.unload.upatch" --json |+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"build_tree_test_registrybuild_tree_segments_registrydiff --git a/testing/vm/image-fixtures.sh b/testing/vm/image-fixtures.shindex ebdded4b..23ec7dfd 100755--- a/testing/vm/image-fixtures.sh+++ b/testing/vm/image-fixtures.sh@@ -76,7 +76,8 @@ for name in config-boot-keys config-live-new-key config-live-revoke-a config-a cconfig-tree-stable config-tree-updated config-tree-deleted config-tree-restored \config-tree-kconfig config-tree-reloaded config-tree-outcomes \config-tree-stopped config-tree-fetched config-tree-fixes \- config-tree-keepalive config-tree-unmatched config-tree-segments \+ config-tree-keepalive config-tree-unmatched config-tree-java-added \+ config-tree-java-deleted config-tree-segments \config-otel config-jsonl config-jsonl-bad config-interrupted config-end2end \config-edge config-core config-local-stack config-builtin config-untrusted; do[ "$name" != config-builtin ] || [ -n "$builtin_key" ] || continuediff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex d827a08e..1ed556bd 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -389,7 +389,7 @@ static bool build_upatch_type_ok(const struct bpm_build_upatch *u)return true;return !strcmp(u->type, "java") &&name_has_suffix(u->source, strlen(u->source), ".jar") &&- !u->n_rodata && !u->n_policies && !u->tree.present;+ !u->n_rodata && !u->n_policies;}static int build_default_targets(struct bpm_build_upatch *u)diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 0c4091f7..8c62c44b 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -815,7 +815,10 @@ static int tree_upload_artifact(struct bpm_oci_client *c, const char *repositoryreturn ret;}-/* The classes of a load upatch, none for one without an XDP program. */+/*+ * The classes of a load upatch, none for one without an XDP program or+ * one that is not BPF.+ */static int upatch_xdp_classes(const void *body, size_t len, __u32 *classes){struct prog_spec *specs = NULL;@@ -827,6 +830,11 @@ static int upatch_xdp_classes(const void *body, size_t len, __u32 *classes)ret = upatch_open_memory(body, len, &u);if (ret)return ret;+ if (upatch_signed_type(&u) != UPATCH_TYPE_BPF) {+ *classes = 0;+ upatch_close(&u);+ return 0;+ }attach = upatch_signed_attach(&u, &attach_len);ret = attach ? prog_specs_parse(attach, attach_len, u.hdr.nr_maps,u.hdr.nr_progs, &specs) : -EINVAL;