Skip to content

eBPF-based security platform

bpm: add tree show to the operator helper

John Fastabend3a9cc3fd97ee
diff --git a/.agents/skills/upatch/SKILL.md b/.agents/skills/upatch/SKILL.md
index 04fec2b3..c35ad75b 100644
--- a/.agents/skills/upatch/SKILL.md
+++ b/.agents/skills/upatch/SKILL.md
@@ -1,6 +1,6 @@
---
name: upatch
-description: Write, build, sign, inspect and package a shirudo upatch. Covers the BPF C program written for the target kernels' preemption model and at a low cost on machines large and small, `shirudo build` for a .upatch and its unload directive, the Beefile and `shirudo bpm build` for a .bee package, `bpm info` to inspect one, `bpm push` and `bpm tree append` to publish, and loading it on a test target. Use when asked to create, change or ship an upatch or a bpm package, or to reproduce a failed upatch build or load.
+description: Write, build, sign, inspect and package a shirudo upatch. Covers the BPF C program written for the target kernels' preemption model and at a low cost on machines large and small, `shirudo build` for a .upatch and its unload directive, the Beefile and `shirudo bpm build` for a .bee package, `bpm info` to inspect one, `bpm push` and `bpm tree append` to publish, `bpm tree show` to read a publication back, and loading it on a test target. Use when asked to create, change or ship an upatch or a bpm package, or to reproduce a failed upatch build or load.
---
# Upatch
@@ -133,6 +133,7 @@ the `.rodata` a rodata entry wrote, the quickest check of a generator.
```sh
shirudo bpm push <name>-<version>.bee oci://<registry>/<namespace>/<repo> --keys-dir <keys>
shirudo bpm tree append <name>-<version>.bee oci://<registry>/<repository> [<append-options>]
+shirudo bpm tree show oci://<registry>/<repository> --cert <keys>/cert.der [--reference <ref>]
```
`push` signs the namespace's index along with the package. Pass
@@ -142,7 +143,11 @@ append` adds a load or delete operation to a signed upatch tree that
targets sync; `Documentation/reference/cli/bpm.rst` lists its options.
It refuses an `add` whose XDP programs select a device class another
upatch of the tree selects on the same nodes: give each class to one
-upatch, or the two entries selectors that exclude each other.
+upatch, or the two entries selectors that exclude each other. `tree
+show` prints the publication verified as a target verifies it, as
+`{"manifest_digest":...,"document":...}` with the signed document
+verbatim, `fixes` included: the check that an append published what
+was meant.
## Load it on a target
diff --git a/Documentation/concepts/glossary.rst b/Documentation/concepts/glossary.rst
index 1de0bfa6..25b77d4a 100644
--- a/Documentation/concepts/glossary.rst
+++ b/Documentation/concepts/glossary.rst
@@ -361,7 +361,7 @@ it.
``shirudo-build``
The operator-only helper that the core ``shirudo`` binary executes
for ``build``, ``bpm build``, ``bpm push``, ``bpm tree append``,
- ``bpm config update``, ``image build``, ``image verify`` and ``keys
+ ``bpm tree show``, ``bpm config update``, ``image build``, ``image verify`` and ``keys
vouch``. It links the signer, runs clang and runs ``veritysetup``.
The operator package installs it and the target package does not.
See :doc:`/concepts/operator-and-target`.
diff --git a/Documentation/concepts/operator-and-target.rst b/Documentation/concepts/operator-and-target.rst
index 9326355f..dfdc3738 100644
--- a/Documentation/concepts/operator-and-target.rst
+++ b/Documentation/concepts/operator-and-target.rst
@@ -53,7 +53,8 @@ shirudo-build helper
The code that signs artifacts is not in the core ``shirudo`` binary.
Every operator command except ``genkey`` and the offline ``attest``
commands executes a separate helper, ``shirudo-build``: ``build``, ``bpm
-build``, ``bpm push``, ``bpm tree append``, ``bpm config update``, ``image
+build``, ``bpm push``, ``bpm tree append``, ``bpm tree show``, ``bpm
+config update``, ``image
build``, ``image verify`` and ``keys vouch``. The helper links the
signer, runs clang to compile BPF and runs ``veritysetup`` to build and
check images. The core binary finds it through ``SHIRUDO_BUILD``, then
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index ef949feb..20e8954a 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -209,8 +209,8 @@ Build the shirudo-build helper
The core binary never compiles or signs. ``shirudo build``, ``shirudo
image build``, ``shirudo image verify``, ``shirudo keys vouch`` and the
-operator ``bpm`` subcommands (``build``, ``push``, ``tree append`` and
-``config update``) execute the separate ``shirudo-build`` helper, which
+operator ``bpm`` subcommands (``build``, ``push``, ``tree append``,
+``tree show`` and ``config update``) execute the separate ``shirudo-build`` helper, which
is the only binary that links the signer.
1. Build the helper:
@@ -394,7 +394,7 @@ Project layout
* - :src:`tools/shirudo-build/`
- The operator-only helper: compile, sign, unload directives,
image signing, ``keys vouch``, ``bpm build``, ``push``,
- ``tree append`` and ``config update``.
+ ``tree append``, ``tree show`` and ``config update``.
* - :src:`tools/quote-check/`
- ``shirudo-quote-check``, a program of its own that checks
keepalive quotes with the TPM parsers of :src:`util/tpm_wire.c`.
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 8f44fc34..81d19fa8 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -1237,7 +1237,8 @@ a proof covers.
:src:`builtin-image.c`, :src:`builtin-init.c`,
:src:`builtin-keys.c`, :src:`builtin-status.c`,
:src:`builtin-upatch.c`, :src:`builtin-version.c`,
- :src:`shirudo.c`, :src:`tools/shirudo-build/compile.c`,
+ :src:`shirudo.c`, :src:`tools/shirudo-build/bpm-show.c`,
+ :src:`tools/shirudo-build/compile.c`,
:src:`tools/shirudo-build/image.c`,
:src:`tools/shirudo-build/shirudo-build.c`,
:src:`tools/shirudo-build/vmlinux.c`,
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index cb5aacb1..694f447e 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -507,6 +507,12 @@ signed config image.
after ``artifact`` and outside ``operation_digest``, none on a
``delete``, carries every earlier operation's list into the next
generation, and refuses to carry a list it would not write
+ * - ``t7008-bpm-tree-show``
+ - ``bpm tree show`` prints the manifest digest and the signed
+ document verbatim, ``fixes`` included, refuses a signature by
+ another key, a wrong certificate, a tampered layer and a document
+ ``tree sync`` would refuse with the errno sync reports, and
+ writes nothing to the registry
* - ``t7101-bpm-tree``
- Tree sync with a selector
* - ``t7102-bpm-tree-sequential``
@@ -767,7 +773,7 @@ Coverage by area
``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``. Unit:
``bpm_oci``, ``bpm_package``
* - upatch tree
- - ``t3011``, ``t5004``, ``t7005``, ``t7007``, ``t7101``,
+ - ``t3011``, ``t5004``, ``t7005``, ``t7007``, ``t7008``, ``t7101``,
``t7102``, ``t7103``, ``t7104``, ``t7105``, ``t7106``,
``t7107``, ``t7109``, ``t9105``. Fuzz: ``tree``,
``tree-state``, ``kconfig``. Unit: ``bpm_tree``,
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index 40a141f1..022e0624 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -30,6 +30,8 @@ Usage
shirudo bpm repo update [<source>]
shirudo bpm tree sync [--dry-run] [--report <path> | --report=<path>]
shirudo bpm tree append <package> oci://<registry>/<repository> [<append-options>]
+ shirudo bpm tree show oci://<registry>/<repository> (--cert <certificate> | --cert=<certificate>)
+ [--reference <reference> | --reference=<reference>]
shirudo bpm key (add | fingerprint) <certificate | package | oci-reference>
shirudo bpm key list [--json]
shirudo bpm pull oci://<registry>/<repository>:<tag>
@@ -80,7 +82,7 @@ Usage
- target
- yes
- yes
- * - ``tree append``
+ * - ``tree append``, ``tree show``
- operator host, helper
- no
- no
@@ -113,8 +115,8 @@ The lock is ``/var/lib/shirudo/locks/bpm.lock``. A locked subcommand waits
for it. The helper subcommands execute ``shirudo-build`` (see
:doc:`shirudo`). ``shirudo bpm --help`` and ``--list-cmds`` show ``build``
and ``push`` only where the helper resolves. Elsewhere they are unknown
-subcommands. ``config update`` and ``tree append`` always execute the
-helper and fail with 127 without it.
+subcommands. ``config update``, ``tree append`` and ``tree show`` always
+execute the helper and fail with 127 without it.
Subcommand names after ``bpm`` and ``bpm key`` may be abbreviated (see
:doc:`shirudo`). The words after ``config``, ``repo`` and ``tree`` may
@@ -269,6 +271,34 @@ the publication at <repository>:<reference> has a fixes list bpm tree
append would not write, so it cannot carry it into the next
generation``.
+tree show
+---------
+
+``tree show`` fetches the tree publication at ``<repository>:<reference>``
+(``reference`` defaults to ``stable``) and verifies it as ``tree sync``
+does, against the certificate ``--cert`` names instead of a config
+image: each layer against its descriptor, the signature, then the tree
+ID, the sequence numbers and each operation's digest. It reads no node
+state and writes nothing. It prints one JSON object and a newline:
+
+.. code-block:: none
+
+ {"manifest_digest":"sha256:<hex>","document":<document>}
+
+``manifest_digest`` is the digest of the publication's manifest, and
+``document`` the signed document's bytes as published, not emitted
+again, so keys a node ignores, such as ``fixes``, appear as signed. The
+document may hold newlines; read the output as a JSON value, not as a
+line.
+
+A refusal prints ``bpm tree show: <error>`` with the errno ``tree sync``
+reports for the same publication: ``Key was rejected by service`` for a
+signature that does not verify under the certificate, a layer that does
+not match its digest or an operation digest that does not match its
+operation, ``No such file or directory`` for a reference that does not
+exist, and ``Invalid argument`` for a document that is not a tree
+publication.
+
key
---
@@ -417,8 +447,8 @@ Exit status
- Success, or ``tree sync`` without a configured tree.
* - 1
- Failure, printed as ``bpm <action>: <error>``. Also the usage
- errors of ``build``, ``config update``, ``tree append`` and
- ``tree sync``, and ``config register add`` or ``remove`` and
+ errors of ``build``, ``config update``, ``tree append``,
+ ``tree show`` and ``tree sync``, and ``config register add`` or ``remove`` and
``key add`` on a target with a config image.
* - 2
- A usage error of the other subcommands, an unknown subcommand, and
@@ -476,6 +506,13 @@ Preview a tree sync:
target# shirudo bpm tree sync --dry-run --report=/tmp/tree.json
+Print the tree publication a target would sync, verified with the
+operator certificate:
+
+.. code-block:: console
+
+ operator$ shirudo bpm tree show oci://quay.io/example/tree --cert ~/shirudo-keys/cert.der
+
Print the fingerprint of the certificate a package carries:
.. code-block:: console
diff --git a/Documentation/reference/cli/shirudo.rst b/Documentation/reference/cli/shirudo.rst
index 1e04e886..b2c136a8 100644
--- a/Documentation/reference/cli/shirudo.rst
+++ b/Documentation/reference/cli/shirudo.rst
@@ -50,7 +50,7 @@ Commands
:doc:`upatch-info`, :doc:`upatch-get`, :doc:`upatch-map`.
* - :doc:`bpm`
- target, and the operator host for ``build``, ``push``,
- ``tree append`` and ``config update``
+ ``tree append``, ``tree show`` and ``config update``
- Install, upgrade and remove signed packages. Build and publish
them.
* - :doc:`image`
@@ -118,7 +118,8 @@ An unknown name prints ``'<input>' is not a shirudo command.``, a list of
similar names, and exits with 1. The subcommands of ``upatch``, ``keys``,
``attest``, ``bpm`` and ``bpm key`` resolve the same way. The subcommands
of ``image``, the actions of ``bpm config register`` and the words
-``repo update``, ``tree sync`` and ``tree append`` must be spelled out.
+``repo update``, ``tree sync``, ``tree append`` and ``tree show`` must be
+spelled out.
``shirudo --list-cmds`` and ``shirudo help --list-cmds`` print the
command names one per line. Bash completion reads them from there.
@@ -204,7 +205,8 @@ Operator helper
~~~~~~~~~~~~~~~
``build``, ``keys vouch``, ``image build``, ``image verify``, ``bpm
-build``, ``bpm push``, ``bpm tree append`` and ``bpm config update`` run
+build``, ``bpm push``, ``bpm tree append``, ``bpm tree show`` and ``bpm
+config update`` run
in the separate ``shirudo-build`` binary, which the operator package
installs. ``shirudo`` executes the first of:
diff --git a/Documentation/reference/environment.rst b/Documentation/reference/environment.rst
index 4f52bb7d..430263ec 100644
--- a/Documentation/reference/environment.rst
+++ b/Documentation/reference/environment.rst
@@ -99,8 +99,9 @@ shirudo and shirudo-build
Registry access
---------------
-Read by the target's ``bpm`` commands and by ``shirudo-build bpm push``
-and ``shirudo-build bpm tree append``. Honored on a target.
+Read by the target's ``bpm`` commands and by ``shirudo-build bpm push``,
+``shirudo-build bpm tree append`` and ``shirudo-build bpm tree show``.
+Honored on a target.
.. list-table::
:header-rows: 1
diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rst
index b022a5f6..5c93f51f 100644
--- a/Documentation/reference/failure-categories.rst
+++ b/Documentation/reference/failure-categories.rst
@@ -375,7 +375,8 @@ every verdict, ``tampered`` included. Parse ``.status`` of
- 0, also when no tree is configured; 1 on failure and for an
unknown option; 2 for too many arguments or a ``--report`` path
that is too long.
- * - ``bpm build``, ``bpm config update``, ``bpm tree append``
+ * - ``bpm build``, ``bpm config update``, ``bpm tree append``,
+ ``bpm tree show``
- 0; 1 on failure and for a usage error; 127 when the helper is
missing.
* - ``bpm push``
diff --git a/Makefile.shirudo b/Makefile.shirudo
index c3742604..bd512046 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -332,6 +332,7 @@ BUILD_SRCS := \
tools/shirudo-build/vmlinux.c \
tools/shirudo-build/bpm-build.c \
tools/shirudo-build/bpm-publish.c \
+ tools/shirudo-build/bpm-show.c \
tools/shirudo-build/vouch.c \
tools/shirudo-build/image.c
@@ -495,8 +496,8 @@ build/target/util/loader.o: $(BPF_OBJS)
# shirudo-build links util/sign.c (CMS signer) plus a subset of util,
# util/loader.o for its .incbin of bpf/unload.bpf.o (used by the
# --unload directive emission path), and the OCI client for the
-# operator-only `bpm build|push|tree append`. Not linked into the core
-# shirudo binary.
+# operator-only `bpm build|push|tree append|tree show`. Not linked into
+# the core shirudo binary.
tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o \
util/log.o util/json.o util/loader.o \
util/io.o util/malloc.o util/paths.o util/config.o \
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 08d04a59..0c2861e4 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2289,11 +2289,13 @@ static void bpm_tree_usage(void)
{
fprintf(stderr,
"usage: shirudo bpm tree sync [--dry-run] [--report=<path>]\n"
- " shirudo bpm tree append <package-file> <repository> [options]\n\n"
- "sync applies the configured tree on a target. append is operator-only:\n"
- "it runs in shirudo-build and publishes the [upatches.tree] entries of a\n"
- "built package as the next operations of the signed tree; see\n"
- "`shirudo bpm tree append --help` where the operator package is installed.\n");
+ " shirudo bpm tree append <package-file> <repository> [options]\n"
+ " shirudo bpm tree show <repository> --cert <file> [--reference <ref>]\n\n"
+ "sync applies the configured tree on a target. append and show are\n"
+ "operator-only and run in shirudo-build: append publishes the\n"
+ "[upatches.tree] entries of a built package as the next operations of the\n"
+ "signed tree, show prints a verified publication; see their --help where\n"
+ "the operator package is installed.\n");
}
static int remove_tree_entry(const char *path, const struct stat *st,
@@ -2785,7 +2787,8 @@ int cmd_bpm(int argc, const char **argv)
bpm_tree_usage();
return argc < 3 ? 2 : 0;
}
- if (!strcmp(sub, "tree") && !strcmp(argv[2], "append"))
+ if (!strcmp(sub, "tree") &&
+ (!strcmp(argv[2], "append") || !strcmp(argv[2], "show")))
return bpm_exec_operator(sub, argc, argv);
if (!strcmp(sub, "tree") && strcmp(argv[2], "sync")) {
bpm_tree_usage();
diff --git a/completion/shirudo.bash b/completion/shirudo.bash
index 98060119..f5db1e6d 100644
--- a/completion/shirudo.bash
+++ b/completion/shirudo.bash
@@ -157,9 +157,13 @@ _shirudo()
elif [[ $sub == build ]]; then
COMPREPLY=( $(compgen -d -- "$cur") )
elif [[ $sub == tree && cword -eq 3 ]]; then
- COMPREPLY=( $(compgen -W "sync append" -- "$cur") )
+ COMPREPLY=( $(compgen -W "sync append show" -- "$cur") )
elif [[ $sub == tree && ${COMP_WORDS[3]} == sync && $cur == -* ]]; then
COMPREPLY=( $(compgen -W "--dry-run --report" -- "$cur") )
+ elif [[ $sub == tree && ${COMP_WORDS[3]} == show && $prev == --cert ]]; then
+ COMPREPLY=( $(compgen -f -- "$cur") )
+ elif [[ $sub == tree && ${COMP_WORDS[3]} == show && $cur == -* ]]; then
+ COMPREPLY=( $(compgen -W "--cert --reference" -- "$cur") )
elif [[ $sub == tree && ${COMP_WORDS[3]} == append && $prev == --previous-cert ]]; then
COMPREPLY=( $(compgen -f -- "$cur") )
elif [[ $sub == tree && ${COMP_WORDS[3]} == append && $cur == -* ]]; then
diff --git a/testing/functional/bpm/t7008-bpm-tree-show.sh b/testing/functional/bpm/t7008-bpm-tree-show.sh
new file mode 100755
index 00000000..ba998daa
--- /dev/null
+++ b/testing/functional/bpm/t7008-bpm-tree-show.sh
@@ -0,0 +1,256 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t7008 -- bpm tree show prints a tree publication's manifest digest and
+# its document bytes verbatim, fixes included, after verifying it as tree
+# sync does; refuses a bad signature, a wrong certificate, a tampered
+# layer and a signed document sync would refuse with the errno sync
+# reports; and writes nothing to the registry.
+# Needs clang, shirudo-build, python3 and an OpenSSL >= 3.5 (ML-DSA).
+
+cd "$(dirname "$0")/../.."
+. ./test-lib.sh
+
+S=../shirudo
+SHB=../tools/shirudo-build/shirudo-build
+
+command -v clang >/dev/null 2>&1 || test_skip_all "no clang"
+command -v openssl >/dev/null 2>&1 || test_skip_all "no openssl"
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+[ -x "$S" ] || test_skip_all "shirudo not built"
+[ -x "$SHB" ] || test_skip_all "shirudo-build not built (make shirudo-build)"
+[ -e ../vendor/linux/vmlinux.h ] || test_skip_all "no vendor/linux/vmlinux.h (build the tree)"
+openssl list -public-key-algorithms 2>/dev/null | grep -qi 'ML-DSA' ||
+ test_skip_all "openssl without ML-DSA (needs OpenSSL >= 3.5)"
+
+REPO=$(cd .. && pwd)
+case "$SHB" in
+/*) B="$SHB" ;;
+*) B="$REPO/${SHB#../}" ;;
+esac
+S="$REPO/shirudo"
+
+WORK=$(mktemp -d)
+SERVER_PID=
+cleanup() {
+ [ -z "$SERVER_PID" ] || kill "$SERVER_PID" 2>/dev/null || :
+ rm -rf "$WORK"
+}
+trap cleanup EXIT HUP INT TERM
+
+"$S" genkey --keys-dir "$WORK/keys" >/dev/null 2>&1 &&
+ "$S" genkey --keys-dir "$WORK/other" >/dev/null 2>&1 ||
+ test_skip_all "genkey failed"
+CERT="$WORK/keys/cert.der"
+
+export SHIRUDO_BUILD="$B" BPM_OCI_SCHEME=http
+export REGISTRY_AUTH_FILE="$WORK/absent-auth.json" NO_PROXY=127.0.0.1 no_proxy=127.0.0.1
+unset BPM_OCI_CA_FILE BPM_OCI_USERNAME BPM_OCI_PASSWORD
+
+mkdir -p "$WORK/t7008" &&
+ln -s "$REPO/vendor" "$WORK/t7008/vendor" &&
+ln -s "$REPO/bpf" "$WORK/t7008/bpf" &&
+printf '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause\n#include "vmlinux.h"\n#include <bpf/bpf_helpers.h>\n#include <bpf/bpf_tracing.h>\n\nchar _license[] SEC("license") = "Dual BSD/GPL";\n\nSEC("fentry/do_nanosleep")\nint BPF_PROG(t7008_fn)\n{\n\treturn 0;\n}\n' \
+ >"$WORK/t7008/t7008.bpf.c" &&
+printf '[package]\nname = "t7008"\n\n[[upatches]]\nsource = "t7008.bpf.c"\n\n[upatches.tree]\nid = "t7008"\nfixes = ["EXAMPLE-0101"]\n' \
+ >"$WORK/t7008/Beefile" ||
+ test_skip_all "cannot write the package sources"
+
+test_expect_success 'bpm build packages an upatch with fixes' '
+ ( cd "$WORK" && "$S" bpm build t7008 --version 1.0.0 \
+ --keys-dir "$WORK/keys" ) >&2 &&
+ test -s "$WORK/t7008-1.0.0.bee"
+'
+[ -s "$WORK/t7008-1.0.0.bee" ] || test_done
+
+PORT=$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')
+REG="$WORK/registry"
+python3 ./helper/test-oci-registry.py --root "$REG" --port "$PORT" --writable \
+ --log "$WORK/requests.log" --ready-file "$WORK/ready" &
+SERVER_PID=$!
+i=0
+while [ ! -e "$WORK/ready" ]; do
+ i=$((i + 1))
+ [ "$i" -lt 100 ] || test_skip_all "registry fixture did not start"
+ sleep .05
+done
+TREE="oci://127.0.0.1:$PORT/ns/tree"
+
+# A digest of every file of the registry, to show that show writes none.
+registry_sum() {
+ (cd "$REG" && find . -type f | LC_ALL=C sort | xargs sha256sum)
+}
+
+# Run show with $@; its output lands in $WORK/out, its errors in
+# $WORK/err.
+show() {
+ "$S" bpm tree show "$@" >"$WORK/out" 2>"$WORK/err"
+}
+
+# show refuses with exit status 1 and "bpm tree show: $1", prints
+# nothing on standard output, and leaves the registry as it was.
+refused() {
+ msg=$1
+ shift
+ registry_sum >"$WORK/before" &&
+ { show "$@"; test $? -eq 1; } &&
+ grep -qxF "bpm tree show: $msg" "$WORK/err" &&
+ test ! -s "$WORK/out" &&
+ registry_sum >"$WORK/after" &&
+ cmp -s "$WORK/before" "$WORK/after"
+}
+
+test_expect_success 'tree append publishes the package with its fixes' '
+ "$S" bpm tree append "$WORK/t7008-1.0.0.bee" "$TREE" \
+ --keys-dir "$WORK/keys" >"$WORK/append.out" &&
+ "$S" bpm tree append "$WORK/t7008-1.0.0.bee" "$TREE" \
+ --keys-dir "$WORK/keys" --reference beta >>"$WORK/append.out" &&
+ test "$(wc -l <"$WORK/append.out")" -eq 2
+'
+
+test_expect_success 'show prints the digest and the document bytes verbatim' '
+ registry_sum >"$WORK/before" &&
+ show "$TREE" --cert "$CERT" &&
+ registry_sum >"$WORK/after" &&
+ cmp -s "$WORK/before" "$WORK/after" &&
+ test ! -s "$WORK/err" &&
+ python3 - "$REG/ns/tree" "$WORK/out" "$(head -n1 "$WORK/append.out")" <<-\PY
+ import hashlib, json, pathlib, sys
+ repo, out = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])
+ raw = (repo / "manifests" / "stable").read_bytes()
+ digest = "sha256:" + hashlib.sha256(raw).hexdigest()
+ assert sys.argv[3].endswith(" " + digest), sys.argv[3]
+ manifest = json.loads(raw)
+ layer = [l for l in manifest["layers"] if l["mediaType"].endswith("+json")][0]
+ document = (repo / "blobs" / layer["digest"]).read_bytes()
+ text = out.read_bytes()
+ assert text == (b"{\"manifest_digest\":\"" + digest.encode() +
+ b"\",\"document\":" + document + b"}\n"), text
+ shown = json.loads(text)
+ assert list(shown) == ["manifest_digest", "document"], shown
+ op = shown["document"]["operations"][0]
+ assert op["fixes"] == ["EXAMPLE-0101"], op
+ PY
+'
+
+test_expect_success 'show takes --reference and its value after =' '
+ show "$TREE" --cert="$CERT" --reference=beta &&
+ python3 -c "
+import json, sys
+shown = json.load(open(sys.argv[1]))
+assert sys.argv[2].endswith(\" \" + shown[\"manifest_digest\"]), shown
+" "$WORK/out" "$(tail -n1 "$WORK/append.out")"
+'
+
+test_expect_success 'show refuses a reference that does not exist' '
+ refused "No such file or directory" "$TREE" --cert "$CERT" \
+ --reference absent
+'
+
+test_expect_success 'show refuses a wrong certificate' '
+ refused "Key was rejected by service" "$TREE" \
+ --cert "$WORK/other/cert.der"
+'
+
+# Replace the stable publication's layer of media type $1 with $2, a
+# file, under the digest of the bytes or, with $3 set, under the old one.
+replace_layer() {
+ python3 - "$REG/ns/tree" "$1" "$2" "${3:-}" <<-\PY
+ import hashlib, json, pathlib, sys
+ repo, media = pathlib.Path(sys.argv[1]), sys.argv[2]
+ body = pathlib.Path(sys.argv[3]).read_bytes()
+ path = repo / "manifests" / "stable"
+ manifest = json.loads(path.read_bytes())
+ layer = [l for l in manifest["layers"] if l["mediaType"] == media][0]
+ if sys.argv[4]:
+ (repo / "blobs" / layer["digest"]).write_bytes(body)
+ else:
+ layer["digest"] = "sha256:" + hashlib.sha256(body).hexdigest()
+ layer["size"] = len(body)
+ (repo / "blobs" / layer["digest"]).write_bytes(body)
+ path.write_bytes(json.dumps(manifest, separators=(",", ":")).encode())
+ PY
+}
+
+# Sign $1, a document, with the keys in directory $2 into $1.sig.
+sign() {
+ openssl cms -sign -binary -md sha512 -in "$1" -signer "$2/cert.der" \
+ -inkey "$2/signing_key.pem" -outform DER -out "$1.sig" \
+ -nosmimecap -nocerts -keyid
+}
+
+# Put back the publication the appends left.
+restore() {
+ rm -rf "$REG/ns/tree" && cp -R "$WORK/saved" "$REG/ns/tree"
+}
+
+DOC_MEDIA=application/vnd.shirudo.upatch.tree.v1+json
+SIG_MEDIA=application/vnd.shirudo.upatch.tree.signature.v1
+
+test_expect_success 'show refuses a signature by another key' '
+ cp -R "$REG/ns/tree" "$WORK/saved" &&
+ python3 - "$REG/ns/tree" "$WORK/doc.json" <<-\PY &&
+ import json, pathlib, sys
+ repo = pathlib.Path(sys.argv[1])
+ manifest = json.loads((repo / "manifests" / "stable").read_bytes())
+ layer = [l for l in manifest["layers"] if l["mediaType"].endswith("+json")][0]
+ pathlib.Path(sys.argv[2]).write_bytes((repo / "blobs" / layer["digest"]).read_bytes())
+ PY
+ sign "$WORK/doc.json" "$WORK/other" &&
+ replace_layer "$SIG_MEDIA" "$WORK/doc.json.sig" &&
+ refused "Key was rejected by service" "$TREE" --cert "$CERT"
+'
+
+test_expect_success 'show refuses a document its signature does not cover' '
+ restore &&
+ sed "s/EXAMPLE-0101/EXAMPLE-0102/" "$WORK/doc.json" >"$WORK/forged.json" &&
+ replace_layer "$DOC_MEDIA" "$WORK/forged.json" &&
+ refused "Key was rejected by service" "$TREE" --cert "$CERT"
+'
+
+test_expect_success 'show refuses a layer that does not match its digest' '
+ restore &&
+ replace_layer "$DOC_MEDIA" "$WORK/forged.json" keep &&
+ refused "Key was rejected by service" "$TREE" --cert "$CERT"
+'
+
+# Publish $1, a document, signed with the operator key.
+resigned() {
+ sign "$1" "$WORK/keys" &&
+ replace_layer "$DOC_MEDIA" "$1" &&
+ replace_layer "$SIG_MEDIA" "$1.sig"
+}
+
+test_expect_success 'show prints a re-signed document with other fixes as signed' '
+ restore &&
+ resigned "$WORK/forged.json" &&
+ show "$TREE" --cert "$CERT" &&
+ python3 -c "
+import json, sys
+shown = json.load(open(sys.argv[1]))
+assert shown[\"document\"][\"operations\"][0][\"fixes\"] == [\"EXAMPLE-0102\"], shown
+" "$WORK/out"
+'
+
+test_expect_success 'show refuses a signed document tree sync would refuse' '
+ restore &&
+ sed "s/\"id\":\"t7008\"/\"id\":\"t7009\"/" "$WORK/doc.json" >"$WORK/renamed.json" &&
+ ! cmp -s "$WORK/doc.json" "$WORK/renamed.json" &&
+ resigned "$WORK/renamed.json" &&
+ refused "Key was rejected by service" "$TREE" --cert "$CERT" &&
+ restore &&
+ printf "not json" >"$WORK/text.json" &&
+ resigned "$WORK/text.json" &&
+ refused "Invalid argument" "$TREE" --cert "$CERT"
+'
+
+test_expect_success 'show without --cert is a usage error' '
+ restore &&
+ { show "$TREE"; test $? -eq 1; } &&
+ grep -q "^usage: shirudo bpm tree show" "$WORK/err" &&
+ test ! -s "$WORK/out" &&
+ show "$TREE" --cert "$CERT"
+'
+
+test_done
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 7dd5c26a..08bda115 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -2377,7 +2377,8 @@ static void bpm_usage(FILE *fp)
" [--rodata-input <name>=<path>]...\n"
" shirudo bpm push <package-file> <repository> [--keys-dir <dir> |\n"
" --index-cert <cert.der> --index-key <key.pem>]\n"
- " shirudo bpm tree append <package-file> <repository> [options]\n\n"
+ " shirudo bpm tree append <package-file> <repository> [options]\n"
+ " shirudo bpm tree show <repository> --cert <cert.der> [--reference <ref>]\n\n"
"Operator-only BPM commands, implemented in shirudo-build and reached\n"
"through `shirudo bpm`. Each accepts --help.\n");
}
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 10c77c6f..de3181e8 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -1326,6 +1326,8 @@ int build_bpm_tree(int argc, char **argv)
{
int ret;
+ if (argc >= 2 && !strcmp(argv[1], "show"))
+ return build_bpm_tree_show(argc - 1, argv + 1);
if (argc < 2 || strcmp(argv[1], "append")) {
tree_append_usage();
return argc >= 2 && build_is_help(argv[1]) ? 0 : 2;
diff --git a/tools/shirudo-build/bpm-show.c b/tools/shirudo-build/bpm-show.c
new file mode 100644
index 00000000..2fd38fc3
--- /dev/null
+++ b/tools/shirudo-build/bpm-show.c
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Operator-only `bpm tree show`: print a tree publication verified as a
+ * node verifies it, with its document bytes verbatim.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <curl/curl.h>
+
+#include <shirudo/misc.h>
+
+#include "bpm.h"
+#include "bpm_oci.h"
+#include "bpm_package.h"
+#include "bpm_tree.h"
+#include "io.h"
+#include "json.h"
+#include "keys.h"
+#include "log.h"
+#include "malloc.h"
+
+static void tree_show_usage(void)
+{
+ fprintf(stderr,
+ "usage: shirudo bpm tree show <repository> --cert <cert.der>\n"
+ " [--reference <ref>]\n\n"
+ "Fetches the tree publication at <repository>:<ref> (default stable),\n"
+ "verifies it against the certificate as tree sync does, and prints\n"
+ "{\"manifest_digest\":\"sha256:<hex>\",\"document\":<document>} with the\n"
+ "document's bytes verbatim, then a newline.\n");
+}
+
+/* The checks tree sync makes before it compares with its own state. */
+static int tree_show_check(const struct bpm_buffer *document)
+{
+ struct tree_publication pub = {};
+ int ret;
+
+ ret = tree_parse((const char *)document->data, document->len, &pub);
+ if (ret)
+ return ret;
+ if (!bpm_package_name_ok(pub.tree_id))
+ ret = -EINVAL;
+ if (!ret)
+ ret = tree_publication_check(&pub);
+ for (int i = 0; !ret && i < pub.n_operations; i++)
+ ret = tree_operation_validate(&pub.operations[i]);
+ tree_publication_free(&pub);
+ return ret;
+}
+
+static int tree_show_print(const char *manifest_digest,
+ const struct bpm_buffer *document)
+{
+ struct json_emit e = { .max = JSON_MAX_BYTES * 2 };
+ int ret;
+
+ json_obj_open(&e);
+ json_kv_string(&e, "manifest_digest", manifest_digest);
+ json_kv_raw(&e, "document", (const char *)document->data,
+ document->len);
+ json_obj_close(&e);
+ ret = e.err;
+ if (!ret)
+ ret = write_complete(STDOUT_FILENO, e.buf, e.len);
+ if (!ret)
+ ret = write_complete(STDOUT_FILENO, "\n", 1);
+ xfree(e.buf, e.cap);
+ return ret;
+}
+
+static int cmd_bpm_tree_show(int argc, char **argv)
+{
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
+ const char *raw_repository = NULL, *cert = NULL;
+ char repository[BPM_MAX_ENDPOINT], digest[BPM_MAX_DIGEST];
+ const char *reference = "stable";
+ struct trusted_set *trusted = NULL;
+ struct bpm_buffer document = {};
+ int ret;
+
+ for (int i = 1; i < argc; i++) {
+ const char *a = argv[i];
+
+ if (!strcmp(a, "--cert") && i + 1 < argc) {
+ cert = argv[++i];
+ } else if (!strncmp(a, "--cert=", 7)) {
+ cert = a + 7;
+ } else if (!strcmp(a, "--reference") && i + 1 < argc) {
+ reference = argv[++i];
+ } else if (!strncmp(a, "--reference=", 12)) {
+ reference = a + 12;
+ } else if (a[0] != '-' && !raw_repository) {
+ raw_repository = a;
+ } else {
+ tree_show_usage();
+ return -EINVAL;
+ }
+ }
+ if (!raw_repository || !cert || !cert[0]) {
+ tree_show_usage();
+ return -EINVAL;
+ }
+ ret = bpm_oci_parse_tree_repo(raw_repository, &c, repository,
+ sizeof(repository));
+ if (ret) {
+ pr_err("bpm tree show: the repository must be oci://registry/repository");
+ return ret;
+ }
+ trusted = xcalloc(1, sizeof(*trusted));
+ if (!trusted)
+ return -ENOMEM;
+ /*
+ * A node verifies against its config image's set; a set of this
+ * certificate alone takes the same path, so a refusal names the
+ * errno sync would.
+ */
+ ret = keys_load_image_at(cert, -ENOENT, false, trusted);
+ if (ret) {
+ pr_err("bpm tree show: cannot read the certificate %s", cert);
+ goto out;
+ }
+ ret = bpm_tree_fetch_document(&c, repository, reference, NULL, trusted,
+ &document, digest, sizeof(digest));
+ if (!ret)
+ ret = tree_show_check(&document);
+ if (!ret)
+ ret = tree_show_print(digest, &document);
+ bpm_buffer_free(&document);
+out:
+ xfree(trusted, sizeof(*trusted));
+ return ret;
+}
+
+int build_bpm_tree_show(int argc, char **argv)
+{
+ int ret;
+
+ if (argc == 2 && build_is_help(argv[1])) {
+ tree_show_usage();
+ return 0;
+ }
+ if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
+ pr_err("bpm tree show: %s", strerror(EIO));
+ return 1;
+ }
+ ret = cmd_bpm_tree_show(argc, argv);
+ curl_global_cleanup();
+ if (ret < 0)
+ pr_err("bpm tree show: %s", strerror(-ret));
+ return ret ? 1 : 0;
+}
diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.h
index 25f53817..5336747a 100644
--- a/tools/shirudo-build/bpm.h
+++ b/tools/shirudo-build/bpm.h
@@ -114,6 +114,7 @@ bool bpm_fix_id_ok(const char *id);
int build_bpm(int argc, char **argv);
int build_bpm_push(int argc, char **argv);
int build_bpm_tree(int argc, char **argv);
+int build_bpm_tree_show(int argc, char **argv);
bool build_is_help(const char *arg);
int build_check_keys(const char *pkey, const char *cert);
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index cb29f385..50d937a4 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -50,7 +50,7 @@ static void usage(FILE *fp)
" [--key <signing_key.pem>] [--cert <cert.der>]\n"
"\n"
" shirudo-build image build <filesystem.img> -o <prefix> ...\n"
- " shirudo-build bpm <build|push|tree append> ...\n"
+ " shirudo-build bpm <build|push|tree append|tree show> ...\n"
" shirudo-build keys vouch <cert.der> [-o <file>] ...\n"
"\n"
"<input> is .bpf.c (compiled via clang) or .bpf.o.\n"
@@ -75,7 +75,7 @@ static void usage(FILE *fp)
" file that is removed). Reports every emitted\n"
" path on stdout. A debugging aid.\n"
"bpm: the operator-only package commands (`shirudo bpm build`,\n"
- " `push`, `tree append`); see `shirudo-build bpm --help`.\n",
+ " `push`, `tree append`, `tree show`); see `shirudo-build bpm --help`.\n",
KEYS_DIR_ENV, KEYS_DIR_ENV,
SIGNING_KEY_PATH, KEYS_DIR_ENV,
CERT_PATH, VMLINUX_PATH);