Skip to content

eBPF-based security platform

bpm: print every segment of a tree in bpm tree show --walk

John Fastabendcdb49aad4a1c
diff --git a/.agents/skills/upatch/SKILL.md b/.agents/skills/upatch/SKILL.md
index e3155a34..e47debf4 100644
--- a/.agents/skills/upatch/SKILL.md
+++ b/.agents/skills/upatch/SKILL.md
@@ -133,7 +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>]
+shirudo bpm tree show oci://<registry>/<repository> --cert <keys>/cert.der [--reference <ref>] [--walk]
```
`push` signs the namespace's index along with the package. Pass
@@ -157,7 +157,8 @@ or rewrite an operation a target may have taken. `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.
+was meant. `--walk` prints one such object per segment, oldest first,
+after checking the chain back to `base.seq` 0 as a joining target does.
## Load it on a target
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 987742d3..22670421 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -514,7 +514,10 @@ signed config image.
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
+ writes nothing to the registry; with ``--walk`` prints a tree
+ never sealed as without it and every segment of a tree sealed
+ by ``--segment-ops 1`` oldest first, stops at ``seq`` 0 from a
+ sealed segment, and refuses a tree whose sealed segment is gone
* - ``t7009-bpm-tree-seal``
- ``bpm tree append`` seals a segment at ``--segment-ops``, a value
that may change between appends, or before one append would pass
@@ -568,7 +571,10 @@ signed config image.
ending before its successor's base and a running node's walk past
its limit are refused with nothing applied; a dry run walks and
records nothing; ``init`` walks nothing and takes the projection
- root one publication gives
+ root one publication gives; ``bpm tree show --walk``, run by the
+ bundle on the operator host, printed every segment of the tree
+ oldest first, each as plain ``show`` prints it and naming the
+ one before
* - ``t7109-bpm-tree-keepalive``
- ``status --keepalive``: the keys of the body in order, its tree
keys and mask as ``state.json`` and the outcomes record have them,
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rst
index d056b2df..0d3f8abf 100644
--- a/Documentation/reference/cli/bpm.rst
+++ b/Documentation/reference/cli/bpm.rst
@@ -31,7 +31,7 @@ Usage
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>]
+ [--reference <reference> | --reference=<reference>] [--walk]
shirudo bpm key (add | fingerprint) <certificate | package | oci-reference>
shirudo bpm key list [--json]
shirudo bpm pull oci://<registry>/<repository>:<tag>
@@ -362,6 +362,30 @@ 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.
+With ``--walk``, it prints every segment of the tree that way, one
+object and a newline per segment, oldest first: the segment at
+``base.seq`` 0 first and the publication ``<reference>`` names last. It
+walks back from that publication through the sealed segments by the
+``base.segment_manifest_digest`` each names, and checks each one as a
+target walking back does: fetched by its digest, verified, of the same
+tree, ending at its successor's ``base.seq`` and of a lower generation.
+It keeps only their digests and fetches each again, by digest, to print
+it, so it fetches every sealed segment twice. A tree never sealed prints
+the one object ``tree show`` prints without it. Like a joining target,
+it refuses a tree whose current segment starts more than 1,048,576
+operations in before it fetches a sealed segment, with ``bpm tree show:
+the current segment starts after seq <n>, more than the 1048576
+operations a joining node walks`` and ``Argument list too long``. A sealed segment it cannot fetch or that does not check is
+refused with ``bpm tree show: the sealed segment <digest>, which ends at
+seq <n>, cannot be fetched or does not verify``, then the errno: ``No
+such file or directory`` for one the registry no longer holds, and
+``Numerical result out of range`` for one that is not the segment before
+its successor. Nothing is printed before the whole walk back checks; a
+segment that the registry no longer serves, or serves with other bytes,
+when it is fetched again stops the output after the segments before it,
+with ``bpm tree show: the sealed segment <digest> cannot be fetched
+again or changed`` and the errno.
+
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
@@ -584,6 +608,12 @@ operator certificate:
operator$ shirudo bpm tree show oci://quay.io/example/tree --cert ~/shirudo-keys/cert.der
+Print every segment of that tree, oldest first:
+
+.. code-block:: console
+
+ operator$ shirudo bpm tree show oci://quay.io/example/tree --cert ~/shirudo-keys/cert.der --walk
+
Print the fingerprint of the certificate a package carries:
.. code-block:: console
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 620633a9..8a55e95d 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2583,7 +2583,7 @@ 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"
- " shirudo bpm tree show <repository> --cert <file> [--reference <ref>]\n\n"
+ " shirudo bpm tree show <repository> --cert <file> [--reference <ref>] [--walk]\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"
diff --git a/completion/shirudo.bash b/completion/shirudo.bash
index 706dee95..051c5f0c 100644
--- a/completion/shirudo.bash
+++ b/completion/shirudo.bash
@@ -163,7 +163,7 @@ _shirudo()
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") )
+ COMPREPLY=( $(compgen -W "--cert --reference --walk" -- "$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-segments/t7108-bpm-tree-segments.sh b/testing/functional/bpm-segments/t7108-bpm-tree-segments.sh
index 5e5b659d..47bab559 100644
--- a/testing/functional/bpm-segments/t7108-bpm-tree-segments.sh
+++ b/testing/functional/bpm-segments/t7108-bpm-tree-segments.sh
@@ -10,7 +10,9 @@
# operation pending resumes with a shorter one and applies nothing twice; a
# forked base, a forked prefix, a missing sealed segment, a broken link and
# a walk past either limit are refused before anything applies; init reads
-# the state and walks nothing. Its own VM: the node starts without a tree.
+# the state and walks nothing. bpm tree show --walk, which the bundle ran on
+# the operator host when it published the tree, printed every segment
+# oldest first. Its own VM: the node starts without a tree.
# The bundle lowers the limits to 4 operations from a cursor past 0 and 6
# from 0.
@@ -37,6 +39,7 @@ for fixture in "$SOURCE/port" "$SOURCE/shirudo/segments/manifests/g28" \
"$SOURCE/shirudo/segments/manifests/fork-prefix" \
"$SOURCE/shirudo/segments/manifests/head-gap" \
"$SHIRUDO_TEST_KEY_FIXTURES/config-tree-segments.img" \
+ "$FIXTURES/segments-show-walk.json" \
./helper/test-oci-registry.py
do
[ -s "$fixture" ] || test_skip_all "missing segment fixture: $fixture"
@@ -134,8 +137,34 @@ def agree(n):
assert state["projection_root_tpm"] == projection.hex(), state
+# bpm tree show --walk of the tag at g28: every segment as plain show
+# prints it, oldest first, each naming the one before.
+def walked(path):
+ expected = b""
+ for k in range(2, 29, 2):
+ raw = manifest("g%d" % k)
+ layers = json.loads(raw)["layers"]
+ blob = [layer for layer in layers if layer["mediaType"] == DOC][0]
+ expected += (b'{"manifest_digest":"' + digest(raw).encode() +
+ b'","document":' +
+ (repo / "blobs" / blob["digest"]).read_bytes() + b"}\n")
+ shown = path.read_bytes()
+ assert shown == expected, ("bpm tree show --walk", shown)
+ decoder, text, before = json.JSONDecoder(), shown.decode(), None
+ for k in range(0, 28, 2):
+ segment, end = decoder.raw_decode(text)
+ text = text[end + 1:]
+ base = segment["document"]["base"]
+ assert base["seq"] == k, segment
+ assert base.get("segment_manifest_digest") == before, segment
+ before = segment["manifest_digest"]
+ assert text == "", text
+
+
if sys.argv[2] == "digest":
print(digest(manifest(sys.argv[3])))
+elif sys.argv[2] == "walked":
+ walked(pathlib.Path(sys.argv[3]))
elif sys.argv[2] == "agree":
agree(int(sys.argv[3]))
else:
@@ -427,4 +456,8 @@ test_expect_success 'init mid-tree walks nothing and takes the projection root o
segments agree 24
'
+test_expect_success 'bpm tree show --walk printed every segment, oldest first' '
+ segments walked "$FIXTURES/segments-show-walk.json"
+'
+
test_done
diff --git a/testing/functional/bpm/t7008-bpm-tree-show.sh b/testing/functional/bpm/t7008-bpm-tree-show.sh
index ba998daa..5600b7e6 100755
--- a/testing/functional/bpm/t7008-bpm-tree-show.sh
+++ b/testing/functional/bpm/t7008-bpm-tree-show.sh
@@ -6,7 +6,8 @@
# 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.
+# reports; and writes nothing to the registry. With --walk it prints every
+# segment of a sealed tree, oldest first, and refuses a lost segment.
# Needs clang, shirudo-build, python3 and an OpenSSL >= 3.5 (ML-DSA).
cd "$(dirname "$0")/../.."
@@ -253,4 +254,74 @@ test_expect_success 'show without --cert is a usage error' '
show "$TREE" --cert "$CERT"
'
+test_expect_success 'show --walk of a tree never sealed prints what show prints' '
+ show "$TREE" --cert "$CERT" &&
+ mv "$WORK/out" "$WORK/plain" &&
+ show "$TREE" --cert "$CERT" --walk &&
+ cmp -s "$WORK/plain" "$WORK/out"
+'
+
+SEGS="oci://127.0.0.1:$PORT/ns/segs"
+
+# The digest of the manifest in the file $1.
+manifest_digest() {
+ printf 'sha256:%s\n' "$(sha256sum <"$1" | cut -d" " -f1)"
+}
+
+test_expect_success 'tree append with --segment-ops 1 seals a segment per append' '
+ n=0 &&
+ for op in add delete add; do
+ "$S" bpm tree append "$WORK/t7008-1.0.0.bee" "$SEGS" \
+ --keys-dir "$WORK/keys" --segment-ops 1 --op "$op" \
+ >/dev/null || return 1
+ n=$((n + 1)) &&
+ cp "$REG/ns/segs/manifests/stable" "$WORK/g$n" || return 1
+ done
+'
+
+test_expect_success 'show --walk prints every segment, oldest first, as show prints it' '
+ registry_sum >"$WORK/before" &&
+ show "$SEGS" --cert "$CERT" --walk &&
+ registry_sum >"$WORK/after" &&
+ cmp -s "$WORK/before" "$WORK/after" &&
+ test ! -s "$WORK/err" &&
+ python3 - "$REG/ns/segs" "$WORK/out" "$WORK/g1" "$WORK/g2" "$WORK/g3" <<-\PY
+ import hashlib, json, pathlib, sys
+ repo, out = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])
+ expected = b""
+ for path in sys.argv[3:]:
+ raw = pathlib.Path(path).read_bytes()
+ digest = "sha256:" + hashlib.sha256(raw).hexdigest()
+ manifest = json.loads(raw)
+ layer = [l for l in manifest["layers"] if l["mediaType"].endswith("+json")][0]
+ document = (repo / "blobs" / layer["digest"]).read_bytes()
+ expected += (b"{\"manifest_digest\":\"" + digest.encode() +
+ b"\",\"document\":" + document + b"}\n")
+ text = out.read_bytes()
+ assert text == expected, text
+ decoder, rest, bases = json.JSONDecoder(), text.decode(), []
+ while rest.strip():
+ shown, end = decoder.raw_decode(rest.lstrip())
+ rest = rest.lstrip()[end:]
+ bases.append(shown["document"]["base"]["seq"])
+ assert bases == [0, 1, 2], bases
+ PY
+'
+
+test_expect_success 'show --walk from a sealed segment stops at seq 0' '
+ g2=$(manifest_digest "$WORK/g2") &&
+ show "$SEGS" --cert "$CERT" --walk --reference "$g2" &&
+ test "$(grep -c "^{\"manifest_digest\"" "$WORK/out")" -eq 2 &&
+ tail -n1 "$WORK/out" | grep -q "^{\"manifest_digest\":\"$g2\""
+'
+
+test_expect_success 'show --walk refuses a tree whose sealed segment is gone' '
+ g1=$(manifest_digest "$WORK/g1") &&
+ mv "$REG/ns/segs/manifests/$g1" "$WORK/sealed" &&
+ { refused "No such file or directory" "$SEGS" --cert "$CERT" --walk
+ ret=$?; } &&
+ mv "$WORK/sealed" "$REG/ns/segs/manifests/$g1" &&
+ test "$ret" -eq 0
+'
+
test_done
diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.sh
index ef7d93fc..543924ad 100755
--- a/testing/vm/build-bundle.sh
+++ b/testing/vm/build-bundle.sh
@@ -730,6 +730,16 @@ build_tree_segments_registry()
{ kill "$pid"; die "bpm tree append $k of the segment tree failed"; }
cp "$repo/manifests/stable" "$repo/manifests/g$k"
done
+ # The guest has no shirudo-build: t7108 checks what bpm tree show
+ # --walk printed of the whole tree here against the copy it serves.
+ env -u BPM_OCI_CA_FILE -u BPM_OCI_USERNAME -u BPM_OCI_PASSWORD \
+ BPM_OCI_SCHEME=http REGISTRY_AUTH_FILE="$work/absent-auth.json" \
+ NO_PROXY=127.0.0.1 no_proxy=127.0.0.1 \
+ SHIRUDO_BUILD="$builder" "$REPO_ROOT/shirudo" bpm tree show \
+ "oci://127.0.0.1:$port/shirudo/segments" \
+ --cert "$FIXTURE_WORK/trusted/cert.der" --walk \
+ >"$TMP_DIR/fixtures/segments-show-walk.json" ||
+ { kill "$pid"; die "bpm tree show --walk of the segment tree failed"; }
kill "$pid"
wait "$pid" 2>/dev/null || :
printf '%s\n' "$port" >"$registry/port"
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 8890a78b..70502e30 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -943,40 +943,17 @@ static int tree_refuse_shared_xdp_class(struct bpm_oci_client *c,
return 0;
}
-/* What tree sync checks of a publication before it applies anything. */
-static int tree_publication_verify(const struct tree_publication *pub)
-{
- int ret = tree_publication_check(pub);
-
- for (int i = 0; !ret && i < pub->n_operations; i++)
- ret = tree_operation_validate(&pub->operations[i]);
- return ret;
-}
-
-/*
- * The sealed segment succ names, as a node walking back fetches it: by
- * manifest digest, signed, well formed and ending where succ starts.
- */
+/* The sealed segment succ names, as a node walking back fetches it. */
static int tree_fetch_sealed(struct bpm_oci_client *c, const char *repository,
const char *cert,
const struct tree_publication *succ,
struct tree_publication *pred)
{
- struct bpm_buffer document = {};
char digest[BPM_MAX_DIGEST];
int ret;
- ret = bpm_tree_fetch_document(c, repository,
- succ->segment_manifest_digest, cert,
- NULL, &document, digest, sizeof(digest));
- if (!ret)
- ret = tree_parse((const char *)document.data, document.len,
- pred);
- if (!ret)
- ret = tree_publication_verify(pred);
- if (!ret)
- ret = tree_segment_precedes(pred, digest, succ);
- bpm_buffer_free(&document);
+ ret = build_tree_fetch_sealed(c, repository, cert, NULL, succ, pred,
+ digest, sizeof(digest));
if (ret)
pr_err("bpm tree append: the sealed segment %s, which ends at seq %lld, does not verify",
succ->segment_manifest_digest,
@@ -1345,7 +1322,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)
ret = -EINVAL;
goto out;
}
- ret = tree_publication_verify(&pub);
+ ret = build_tree_publication_verify(&pub);
if (ret) {
pr_err("bpm tree append: the publication at %s:%s does not verify",
repo_ref, reference);
diff --git a/tools/shirudo-build/bpm-show.c b/tools/shirudo-build/bpm-show.c
index 2fd38fc3..cb626c21 100644
--- a/tools/shirudo-build/bpm-show.c
+++ b/tools/shirudo-build/bpm-show.c
@@ -2,8 +2,9 @@
/*
* 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.
+ * Operator-only `bpm tree show`: print a tree publication, or with --walk
+ * every segment of it, verified as a node verifies it, with its document
+ * bytes verbatim. The publisher fetches sealed segments through it too.
*/
#include <errno.h>
@@ -13,11 +14,13 @@
#include <curl/curl.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "bpm.h"
#include "bpm_oci.h"
#include "bpm_package.h"
#include "bpm_tree.h"
+#include "hex.h"
#include "io.h"
#include "json.h"
#include "keys.h"
@@ -28,32 +31,73 @@ static void tree_show_usage(void)
{
fprintf(stderr,
"usage: shirudo bpm tree show <repository> --cert <cert.der>\n"
- " [--reference <ref>]\n\n"
+ " [--reference <ref>] [--walk]\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");
+ "document's bytes verbatim, then a newline. --walk prints every\n"
+ "segment of the tree that way, oldest first, back from the one the\n"
+ "reference names.\n");
}
-/* The checks tree sync makes before it compares with its own state. */
-static int tree_show_check(const struct bpm_buffer *document)
+int build_tree_publication_verify(const struct tree_publication *pub)
{
- 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;
+ /*
+ * The node records the tree ID in its state, which takes names
+ * alone, so it refuses one that is not.
+ */
+ if (!bpm_package_name_ok(pub->tree_id))
+ return -EINVAL;
+ ret = tree_publication_check(pub);
+ for (int i = 0; !ret && i < pub->n_operations; i++)
+ ret = tree_operation_validate(&pub->operations[i]);
+ return ret;
+}
+
+int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,
+ const char *reference, const char *cert_path,
+ const struct trusted_set *trusted,
+ struct bpm_buffer *document,
+ struct tree_publication *pub,
+ char *manifest_digest, size_t digest_sz)
+{
+ struct bpm_buffer bytes = {};
+ int ret;
+
+ ret = bpm_tree_fetch_document(c, repository, reference, cert_path,
+ trusted, &bytes, manifest_digest,
+ digest_sz);
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);
+ ret = tree_parse((const char *)bytes.data, bytes.len, pub);
+ if (!ret)
+ ret = build_tree_publication_verify(pub);
+ if (ret || !document)
+ bpm_buffer_free(&bytes);
+ else
+ *document = bytes;
return ret;
}
+int build_tree_fetch_sealed(struct bpm_oci_client *c, const char *repository,
+ const char *cert_path,
+ const struct trusted_set *trusted,
+ const struct tree_publication *succ,
+ struct tree_publication *pred,
+ char *manifest_digest, size_t digest_sz)
+{
+ int ret;
+
+ ret = build_tree_fetch_checked(c, repository,
+ succ->segment_manifest_digest,
+ cert_path, trusted, NULL, pred,
+ manifest_digest, digest_sz);
+ if (ret)
+ return ret;
+ return tree_segment_precedes(pred, manifest_digest, succ);
+}
+
static int tree_show_print(const char *manifest_digest,
const struct bpm_buffer *document)
{
@@ -74,6 +118,127 @@ static int tree_show_print(const char *manifest_digest,
return ret;
}
+/*
+ * The manifest digests of the sealed segments, newest first. Only these
+ * are kept: the printing pass fetches each segment again by its digest,
+ * which names the same bytes, so a long tree costs no more memory than
+ * one document.
+ */
+struct tree_show_walk {
+ __u8 (*digests)[32];
+ size_t n, cap;
+};
+
+static void tree_show_walk_free(struct tree_show_walk *w)
+{
+ xfree(w->digests, w->cap * sizeof(*w->digests));
+ *w = (struct tree_show_walk){};
+}
+
+static int tree_show_walk_add(struct tree_show_walk *w,
+ const char *manifest_digest)
+{
+ if (w->n == w->cap) {
+ size_t cap = w->cap ? 2 * w->cap : 8;
+ __u8 (*digests)[32];
+
+ digests = xreallocarray(w->digests, w->cap, cap,
+ sizeof(*digests));
+ if (!digests)
+ return -ENOMEM;
+ w->digests = digests;
+ w->cap = cap;
+ }
+ if (sha256_digest_decode(manifest_digest, w->digests[w->n]))
+ return -EINVAL;
+ w->n++;
+ return 0;
+}
+
+/*
+ * Back from tag to the segment at base.seq 0, each sealed segment
+ * fetched by the digest its successor names and checked as the one it
+ * names. A tree too long for a node to join is refused, as it is by
+ * tree sync and bpm tree append.
+ */
+static int tree_show_walk_back(struct bpm_oci_client *c,
+ const char *repository,
+ const struct trusted_set *trusted,
+ const struct tree_publication *tag,
+ struct tree_show_walk *w)
+{
+ const struct tree_publication *succ = tag;
+ struct tree_publication fetched[2] = {};
+ char digest[BPM_MAX_DIGEST];
+ int next = 0, ret;
+
+ ret = tree_walk_within(tag->base_seq, 0);
+ if (ret)
+ pr_err("bpm tree show: the current segment starts after seq %lld, more than the %d operations a joining node walks",
+ (long long)tag->base_seq, TREE_MAX_JOIN_OPS);
+ while (!ret && succ->base_seq > 0) {
+ tree_publication_free(&fetched[next]);
+ ret = build_tree_fetch_sealed(c, repository, NULL, trusted,
+ succ, &fetched[next], digest,
+ sizeof(digest));
+ if (ret) {
+ pr_err("bpm tree show: the sealed segment %s, which ends at seq %lld, cannot be fetched or does not verify",
+ succ->segment_manifest_digest,
+ (long long)succ->base_seq);
+ break;
+ }
+ ret = tree_show_walk_add(w, digest);
+ succ = &fetched[next];
+ next ^= 1;
+ }
+ tree_publication_free(&fetched[0]);
+ tree_publication_free(&fetched[1]);
+ return ret;
+}
+
+/* A sealed segment the walk back checked, fetched and printed again. */
+static int tree_show_sealed(struct bpm_oci_client *c, const char *repository,
+ const struct trusted_set *trusted,
+ const __u8 manifest_digest[32])
+{
+ char hex[65], reference[BPM_MAX_DIGEST], digest[BPM_MAX_DIGEST];
+ struct tree_publication pub = {};
+ struct bpm_buffer document = {};
+ int ret;
+
+ hex_encode(manifest_digest, 32, hex);
+ if (strsprintf(reference, sizeof(reference), "sha256:%s", hex) < 0)
+ return -E2BIG;
+ ret = build_tree_fetch_checked(c, repository, reference, NULL, trusted,
+ &document, &pub, digest,
+ sizeof(digest));
+ tree_publication_free(&pub);
+ if (!ret && strcmp(digest, reference))
+ ret = -EKEYREJECTED;
+ if (ret)
+ pr_err("bpm tree show: the sealed segment %s cannot be fetched again or changed",
+ reference);
+ else
+ ret = tree_show_print(digest, &document);
+ bpm_buffer_free(&document);
+ return ret;
+}
+
+static int tree_show_walk(struct bpm_oci_client *c, const char *repository,
+ const struct trusted_set *trusted,
+ const struct tree_publication *tag)
+{
+ struct tree_show_walk w = {};
+ int ret;
+
+ ret = tree_show_walk_back(c, repository, trusted, tag, &w);
+ for (size_t i = w.n; !ret && i > 0; i--)
+ ret = tree_show_sealed(c, repository, trusted,
+ w.digests[i - 1]);
+ tree_show_walk_free(&w);
+ return ret;
+}
+
static int cmd_bpm_tree_show(int argc, char **argv)
{
struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
@@ -81,7 +246,9 @@ static int cmd_bpm_tree_show(int argc, char **argv)
char repository[BPM_MAX_ENDPOINT], digest[BPM_MAX_DIGEST];
const char *reference = "stable";
struct trusted_set *trusted = NULL;
+ struct tree_publication pub = {};
struct bpm_buffer document = {};
+ bool walk = false;
int ret;
for (int i = 1; i < argc; i++) {
@@ -95,6 +262,8 @@ static int cmd_bpm_tree_show(int argc, char **argv)
reference = argv[++i];
} else if (!strncmp(a, "--reference=", 12)) {
reference = a + 12;
+ } else if (!strcmp(a, "--walk")) {
+ walk = true;
} else if (a[0] != '-' && !raw_repository) {
raw_repository = a;
} else {
@@ -125,12 +294,14 @@ static int cmd_bpm_tree_show(int argc, char **argv)
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);
+ ret = build_tree_fetch_checked(&c, repository, reference, NULL,
+ trusted, &document, &pub, digest,
+ sizeof(digest));
+ if (!ret && walk)
+ ret = tree_show_walk(&c, repository, trusted, &pub);
if (!ret)
ret = tree_show_print(digest, &document);
+ tree_publication_free(&pub);
bpm_buffer_free(&document);
out:
xfree(trusted, sizeof(*trusted));
diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.h
index 5336747a..352eabb7 100644
--- a/tools/shirudo-build/bpm.h
+++ b/tools/shirudo-build/bpm.h
@@ -117,6 +117,37 @@ int build_bpm_tree(int argc, char **argv);
int build_bpm_tree_show(int argc, char **argv);
bool build_is_help(const char *arg);
+struct bpm_buffer;
+struct bpm_oci_client;
+struct trusted_set;
+
+/*
+ * What tree sync checks of a publication before it compares it with its
+ * state.
+ *
+ * build_tree_fetch_checked() fetches the publication at reference, a tag
+ * or a manifest digest, checks it against trusted, or cert_path when
+ * trusted is NULL, as tree sync does and parses it into pub. document,
+ * when not NULL, keeps the checked bytes for bpm_buffer_free().
+ *
+ * build_tree_fetch_sealed() fetches the sealed segment succ names as a
+ * node walking back does: by digest, checked, and the one that ends where
+ * succ starts.
+ */
+int build_tree_publication_verify(const struct tree_publication *pub);
+int build_tree_fetch_checked(struct bpm_oci_client *c, const char *repository,
+ const char *reference, const char *cert_path,
+ const struct trusted_set *trusted,
+ struct bpm_buffer *document,
+ struct tree_publication *pub,
+ char *manifest_digest, size_t digest_sz);
+int build_tree_fetch_sealed(struct bpm_oci_client *c, const char *repository,
+ const char *cert_path,
+ const struct trusted_set *trusted,
+ const struct tree_publication *succ,
+ struct tree_publication *pred,
+ char *manifest_digest, size_t digest_sz);
+
int build_check_keys(const char *pkey, const char *cert);
/* The file name of source less .bpf.c, .bpf.o or .o. 0 or -E2BIG. */