eBPF-based security platform
status: test the keepalive window over sealed tree segments
John Fastabendf2770201144c
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 22670421..51ecd2fd 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -577,10 +577,19 @@ signed config image.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,+ keys and mask as ``state.json`` and the outcomes record have them+ over the last 7 operations, the bundle's lowered window,flags bit 4 after a sync is killed after its load, and after thesync the loaded set its mask folds to and the masks of node A andnode B+ * - ``t7110-bpm-tree-window``+ - In ``functional/bpm-segments/``, after ``t7108``, with the+ keepalive window lowered to 7 operations: ``status --keepalive``+ on a node of the segmented tree gives the ``base_seq``,+ ``base_root`` and mask of the outcomes record over the last 7+ operations up to the head, from inside a segment older than the+ recorded one, as the window slides with each segment and while an+ operation is pending, its operations past ``last_seq`` not reached``otel`` (8)~~~~~~~~~~~~@@ -830,7 +839,8 @@ Coverage by area* - upatch tree- ``t3011``, ``t5004``, ``t7005``, ``t7007``, ``t7008``, ``t7009``,``t7101``, ``t7102``, ``t7103``, ``t7104``, ``t7105``, ``t7106``,- ``t7107``, ``t7108``, ``t7109``, ``t9105``, ``t9109``. Fuzz: ``tree``,+ ``t7107``, ``t7108``, ``t7109``, ``t7110``, ``t9105``, ``t9109``.+ Fuzz: ``tree``,``tree-state``, ``kconfig``. Unit: ``bpm_tree``,``bpm_tree_overlap``, ``bpm_tree_state``, ``chain_history``,``kernel_build_id``, ``kernel_release``@@ -839,7 +849,7 @@ Coverage by areaUnauthorized policy-xattr changes: ``t6111``. Task and socketinheritance: ``t6141``, ``t6142``* - Telemetry- - ``t4006``, ``t4104``, ``t6139``, ``t7109``, ``t8001`` to+ - ``t4006``, ``t4104``, ``t6139``, ``t7109``, ``t7110``, ``t8001`` to``t8007``, ``t8101`` to ``t8106``, ``t9101`` to ``t9105``,``t9107``, ``t9109``. Fuzz: ``otel``, ``quote-check``, ``serializer``.Unit: ``keepalive``, ``keepalive_quote``, ``sysinfo``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex da99cbbe..2ac3afb2 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -153,6 +153,7 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:- Package commands, Beefile policy labels, publishing and theupatch tree; ``bpm-segments`` boots a VM of its own, whose nodestarts without a tree, for the walk through a tree's segments+ and the keepalive window over them* - 8- ``functional/otel/``- Map export, the status keepalive, OTLP transport and TLS policydiff --git a/Documentation/reference/cli/status.rst b/Documentation/reference/cli/status.rstindex 4accb2a9..54080132 100644--- a/Documentation/reference/cli/status.rst+++ b/Documentation/reference/cli/status.rst@@ -119,10 +119,15 @@ last: ``00`` not evaluated, ``01`` ``OK``, ``10`` ``WARN``, ``11``of :doc:`/security/tamper-counters`, zeros included, in the order ofthat page.-``mask`` holds two bits per operation from ``base_seq + 1`` to the head-of the recorded publication, read from the outcomes record of-:doc:`/reference/formats/bpm-formats`: ``01`` applied, ``10`` passed-over, ``11`` module absent, ``00`` not reached. ``set_sha256`` is+The tree keys report a window of the last 512 operations up to the head+of the recorded publication, whatever segments hold them: ``base_seq``+is the head less 512, or 0 for a tree of 512 operations or fewer, and+``base_root`` the node's projection root after ``base_seq``, 64 zeros+at 0. ``mask`` holds two bits per operation from ``base_seq + 1`` to the+head: ``01`` applied, ``10`` passed over, ``11`` module absent, ``00``+not reached. Both come from the outcomes record of+:doc:`/reference/formats/bpm-formats`, which keeps every operation the+node processed, those of sealed segments included. ``set_sha256`` isSHA-256 over a ``<payload_sha256> <name>`` line for every loaded upatch,sorted by name, the ``set_digest`` form of the ``init`` event.``not_ok`` counts the loaded upatches the tree holds a status file for.diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 36c7e22b..8740bbac 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -647,7 +647,9 @@ not published.left: the next sync drops them before it appends. It then refusesa state with ``last_seq`` above 0 whose ``outcomes`` is missing(``ENOENT``), or has a gap or a malformed line or ends before- ``last_seq`` (``EBADMSG``). Nothing rebuilds or prunes the file.+ ``last_seq`` (``EBADMSG``). Nothing rebuilds or prunes the file:+ the keepalive reads its last 512 operations from it whatever+ segments held them (:doc:`/reference/cli/status`).* - ``pending.json``- JSON object with ``schema`` (1), ``seq``, ``id`` and``operation_digest`` while an operation is in progress. The nextdiff --git a/Makefile.shirudo b/Makefile.shirudoindex 6fbe0432..411fbe17 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -429,10 +429,11 @@ build/target/%.o: %.c Makefile.shirudo $(CONFIG_MK_DEP)-include $(DEPS)-# The tree walk limits of util/bpm_tree.h. The test VM bundle builds its-# target with them low, TREE_LIMIT_CFLAGS='-DTREE_MAX_WALK_OPS=<n>-# -DTREE_MAX_JOIN_OPS=<n>', so that a target test reaches them; the operator-# profile ignores it. The objects that read them rebuild whenever the value+# The tree walk limits of util/bpm_tree.h and the keepalive window of+# util/keepalive.h. The test VM bundle builds its target with them low,+# TREE_LIMIT_CFLAGS='-DTREE_MAX_WALK_OPS=<n> -DTREE_MAX_JOIN_OPS=<n>+# -DKEEPALIVE_WINDOW_OPS=<n>', so that a target test reaches them; the+# operator profile ignores it. The objects that read them rebuild whenever the value# differs from the one they were built with, so a build without it never# keeps the lowered limits.ifeq ($(PROFILE),target)@@ -441,7 +442,8 @@ elseoverride TREE_LIMIT_CFLAGS :=endifTREE_LIMIT_STAMP := build/tree-limits-$(PROFILE)-TREE_LIMIT_OBJS := $(call obj,builtin-bpm.o) $(call obj,util/bpm_tree.o)+TREE_LIMIT_OBJS := $(call obj,builtin-bpm.o) $(call obj,builtin-status.o) \+ $(call obj,util/bpm_tree.o).PHONY: FORCE_TREE_LIMITSFORCE_TREE_LIMITS:diff --git a/builtin-status.c b/builtin-status.cindex b6a17f06..048e82fa 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -508,14 +508,22 @@ static bool keepalive_pending(void *ctx)return access(BPM_TREE_PENDING_PATH, F_OK) == 0;}+/*+ * The window reaches past the recorded segment's base into the segments+ * before it, whose outcomes the record keeps from seq 1.+ */static void keepalive_window(struct keepalive_view *v,const struct tree_state *s){__u8 projection[CHAIN_DIGEST_LEN], history[CHAIN_DIGEST_LEN];- v->base_seq = max_t(__s64, s->head_seq - KEEPALIVE_MASK_OPS, 0);memset(v->codes, 0, sizeof(v->codes));- if (s->last_seq < v->base_seq || s->last_seq > s->head_seq) {+ if (s->last_seq < 0 || s->last_seq > s->head_seq) {+ v->outcomes_rc = -EBADMSG;+ return;+ }+ v->base_seq = max_t(__s64, s->head_seq - KEEPALIVE_WINDOW_OPS, 0);+ if (s->last_seq < v->base_seq) {v->outcomes_rc = -EBADMSG;return;}diff --git a/testing/functional/bpm-segments/t7110-bpm-tree-window.sh b/testing/functional/bpm-segments/t7110-bpm-tree-window.shnew file mode 100755index 00000000..f1048582--- /dev/null+++ b/testing/functional/bpm-segments/t7110-bpm-tree-window.sh@@ -0,0 +1,253 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t7110 -- the keepalive body reports the last operations up to the head of+# a segmented tree whatever segments hold them: its base_seq, base_root and+# mask are those of the outcomes record over a window that starts in an+# older segment than the recorded one, also while an operation is pending.+# In bpm-segments, after t7108, since the bpm VM's node follows another+# tree. The bundle lowers the window to 7 operations, the segments hold 2.++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}+SOURCE="$FIXTURES/segments-oci"+TREE_DIR=/var/lib/shirudo/bpm/upatch-tree+OUTCOMES=$TREE_DIR/outcomes+TREE_EVENTLOG=$TREE_DIR/tree-eventlog+PENDING=$TREE_DIR/pending.json+EVENTLOG=/var/lib/shirudo/eventlog+WINDOW=7+WORK=$(mktemp -d)+SERVED="$WORK/registry"+REPO="$SERVED/shirudo/segments"+SERVER_PID=+HOLDER_PID=++for fixture in "$SOURCE/port" "$SOURCE/shirudo/segments/manifests/g28" \+ "$SHIRUDO_TEST_KEY_FIXTURES/config-tree-segments.img" \+ ./helper/test-oci-registry.py+do+ [ -s "$fixture" ] || test_skip_all "missing segment fixture: $fixture"+done+if [ -e "$TREE_STATE_FILE" ]; then+ [ "$(tree_state_field tree_id)" = shirudo-test-segments ] &&+ [ ! -e "$PENDING" ] &&+ [ "$(tree_state_field last_seq)" -le 24 ] &&+ [ "$(tree_state_field last_seq)" = "$(tree_state_field head_seq)" ] ||+ test_skip_all "the node is not where t7108 leaves it"+fi+PORT=$(cat "$SOURCE/port")++release_tree_eventlog()+{+ [ -n "$HOLDER_PID" ] || return 0+ kill "$HOLDER_PID" 2>/dev/null || true+ wait "$HOLDER_PID" 2>/dev/null || true+ HOLDER_PID=+}++cleanup()+{+ release_tree_eventlog+ if [ -n "$SERVER_PID" ]; then+ kill "$SERVER_PID" 2>/dev/null || true+ wait "$SERVER_PID" 2>/dev/null || true+ fi+ rm -rf "$WORK"+}+trap cleanup EXIT++# The target follows the reference segments, which names generation $1.+serve()+{+ cp "$REPO/manifests/g$1" "$REPO/manifests/segments"+}++sync_tree()+{+ BPM_OCI_SCHEME=http "$SHIRUDO_BIN" bpm tree sync \+ --report "$WORK/$1.json" >/dev/null 2>"$WORK/$1.err"+}++keepalive()+{+ "$SHIRUDO_BIN" status --keepalive >"$1"+}++# The body in $1 has the tree keys of state.json, and base_seq, base_root+# and mask of the outcomes record over the last $WINDOW operations up to+# head_seq. With settled as $2 flags bits 2, 4 and 5 are clear, with+# pending bit 4 is set. With $3, the window starts before the recorded+# segment's base and its root is not that of seq 0.+window_agrees()+{+ python3 - "$1" "$2" "${3:-}" "$TREE_STATE_FILE" "$OUTCOMES" \+ "$WINDOW" <<'PY'+import base64+import json+import pathlib+import sys++TREE_LOG_MISMATCH, TREE_OP_PENDING, SNAPSHOT_UNSETTLED = 4, 16, 32++body_path, mode, spans, state_path, outcomes_path = sys.argv[1:6]+window = int(sys.argv[6])+body = json.loads(pathlib.Path(body_path).read_bytes())+state = json.loads(pathlib.Path(state_path).read_bytes())+lines = [json.loads(text) for text in+ pathlib.Path(outcomes_path).read_text(encoding="utf-8").splitlines()]+outcome = {line["seq"]: line for line in lines}+head, last = state["head_seq"], state["last_seq"]+base = max(0, head - window)+for key in ("tree_id", "generation", "last_seq", "projection_root"):+ if body[key] != state[key]:+ sys.exit(f"{key} is {body[key]}, state.json has {state[key]}")+root = outcome[base]["projection_root"] if base else "00" * 32+if body["base_seq"] != base or body["base_root"] != root:+ sys.exit(f"the window starts at {body['base_seq']} {body['base_root']}, "+ f"not {base} {root}")+if spans and (base >= state["segment_base_seq"] or root == "00" * 32):+ sys.exit(f"the window from {base} stays in the segment from "+ f"{state['segment_base_seq']}")++mask = base64.b64decode(body["mask"], validate=True)+if len(mask) != (head - base + 3) // 4:+ sys.exit(f"the mask has {len(mask)} bytes for {head - base} operations")+for i in range(4 * len(mask)):+ seq = base + 1 + i+ code = mask[i // 4] >> (6 - 2 * (i % 4)) & 3+ want = outcome[seq]["outcome"] if seq <= min(last, head) else 0+ if code != want:+ sys.exit(f"the mask gives {code} for {seq}, not {want}")++flags = body["flags"]+if mode == "pending":+ if not flags & TREE_OP_PENDING:+ sys.exit(f"flags {flags} lack the pending operation")+elif flags & (TREE_LOG_MISMATCH | TREE_OP_PENDING | SNAPSHOT_UNSETTLED):+ sys.exit(f"flags {flags} say the tree is busy or does not agree")+PY+}++# Hold the lock tree_event_append() takes, so that a sync stops after an+# operation's load and register extend, before its outcomes line.+hold_tree_eventlog()+{+ python3 -c '+import fcntl, os, sys, time+fd = os.open(sys.argv[1], os.O_RDONLY)+fcntl.flock(fd, fcntl.LOCK_EX)+open(sys.argv[2], "w").close()+time.sleep(600)+' "$TREE_EVENTLOG" "$WORK/held" &+ HOLDER_PID=$!+ for _ in 1 2 3 4 5 6 7 8 9 10; do+ [ -e "$WORK/held" ] && return 0+ sleep 1+ done+ return 1+}++# Wait until this boot's eventlog has the tree_op record of seq $1.+tree_op_measured()+{+ python3 - "$EVENTLOG" "$1" <<'PY'+import json+import sys+import time++seq = int(sys.argv[2])+for _ in range(600):+ with open(sys.argv[1], encoding="utf-8") as stream:+ for text in stream:+ try:+ record = json.loads(text)+ except ValueError:+ continue+ if record.get("type") == "tree_op" and \+ record.get("tree", {}).get("seq") == seq:+ sys.exit(0)+ time.sleep(0.1)+sys.exit(1)+PY+}++cp -R "$SOURCE" "$SERVED"+python3 ./helper/test-oci-registry.py --root "$SERVED" --port "$PORT" \+ --ready-file "$WORK/ready" &+SERVER_PID=$!+for _ in 1 2 3 4 5; do+ [ -e "$WORK/ready" ] && break+ sleep 1+done+[ -e "$WORK/ready" ] || test_skip_all "the segment registry did not start"+"$SHIRUDO_BIN" image update "$SHIRUDO_TEST_KEY_FIXTURES/config-tree-segments" \+ >/dev/null || test_skip_all "cannot activate config-tree-segments"++# A node t7108 did not bring along takes the tree one segment at a time.+LAST=$(tree_state_field last_seq)+k=$((${LAST:-0} + 2))+while [ "$k" -le 24 ]; do+ serve "$k" && sync_tree "catch-up-$k" ||+ test_skip_all "cannot sync the segment tree to $k"+ k=$((k + 2))+done++test_expect_success 'a body at seq 24 reports a window that starts in an older segment' '+ [ "$(tree_state_field last_seq)" = 24 ] &&+ [ "$(tree_state_field segment_base_seq)" = 22 ] &&+ keepalive "$WORK/at-24.json" &&+ [ "$(report_field base_seq <"$WORK/at-24.json")" = 17 ] &&+ window_agrees "$WORK/at-24.json" settled spans+'++test_expect_success 'the window slides with the next segment' '+ serve 26 &&+ sync_tree at-26 &&+ [ "$(tree_state_field last_seq)" = 26 ] &&+ keepalive "$WORK/at-26.json" &&+ [ "$(report_field base_seq <"$WORK/at-26.json")" = 19 ] &&+ window_agrees "$WORK/at-26.json" settled spans+'++# A test body runs in a subshell, which could not hand the lock holder to+# the cleanup: stop the sync here and let the bodies look at what is left.+serve 28+STOPPED=no+if hold_tree_eventlog; then+ BPM_OCI_SCHEME=http "$SHIRUDO_BIN" bpm tree sync \+ --report "$WORK/stopped.json" >/dev/null 2>&1 &+ SYNC_PID=$!+ tree_op_measured 27 && STOPPED=yes+ kill -9 "$SYNC_PID" 2>/dev/null || true+ wait "$SYNC_PID" 2>/dev/null || true+fi+release_tree_eventlog+if [ "$STOPPED" = yes ] && ! keepalive "$WORK/stopped-body.json"; then+ STOPPED=no+fi++test_expect_success 'with an operation pending the window ends at the head and leaves it not reached' '+ [ "$STOPPED" = yes ] &&+ [ -e "$PENDING" ] &&+ [ "$(tree_state_field last_seq)" = 26 ] &&+ [ "$(tree_state_field head_seq)" = 28 ] &&+ [ "$(report_field base_seq <"$WORK/stopped-body.json")" = 21 ] &&+ window_agrees "$WORK/stopped-body.json" pending spans+'++test_expect_success 'after the sync completes the window covers the head' '+ sync_tree at-28 &&+ [ ! -e "$PENDING" ] &&+ [ "$(tree_state_field last_seq)" = 28 ] &&+ keepalive "$WORK/at-28.json" &&+ [ "$(report_field base_seq <"$WORK/at-28.json")" = 21 ] &&+ window_agrees "$WORK/at-28.json" settled spans+'++test_donediff --git a/testing/functional/bpm/t7109-bpm-tree-keepalive.sh b/testing/functional/bpm/t7109-bpm-tree-keepalive.shindex 6891162c..bb9cb0bd 100644--- a/testing/functional/bpm/t7109-bpm-tree-keepalive.sh+++ b/testing/functional/bpm/t7109-bpm-tree-keepalive.sh@@ -22,6 +22,8 @@ TREE_EVENTLOG=$TREE_DIR/tree-eventlogPENDING=$TREE_DIR/pending.jsonEVENTLOG=/var/lib/shirudo/eventlogMATCH_PIN=/run/shirudo/bpffs/upatches/shirudo-test-tree-match/link+# The bundle lowers the keepalive window to the last 7 operations.+WINDOW=7WORK=$(mktemp -d)HOLDER_PID=@@ -107,14 +109,15 @@ PY}# The body in $1 is compact, has the keys of the format in their order,-# and its tree keys are those of state.json and the outcomes record. With+# and its tree keys are those of state.json and the outcomes record over+# the last $WINDOW operations. With# settled as $2, flags bits 2, 4 and 5 are clear and the loaded set is the# fold of the operations the mask has applied in the publication of the# reference $3; with pending, flags bit 4 is set.keepalive_agrees(){python3 - "$1" "$2" "$TREE_STATE_FILE" "$OUTCOMES" \- "$TREE_REPO" "${3:-}" <<'PY'+ "$TREE_REPO" "${3:-}" "$WINDOW" <<'PY'import base64import hashlibimport json@@ -129,7 +132,7 @@ KEYS = ["schema", "boot_id", "version_id", "config_root", "kernel_release",TREE_LOG_MISMATCH, TREE_OP_PENDING, SNAPSHOT_UNSETTLED = 4, 16, 32body_path, mode, state_path, outcomes_path, repo = sys.argv[1:6]-reference = sys.argv[6]+reference, window = sys.argv[6], int(sys.argv[7])raw = pathlib.Path(body_path).read_bytes()if not raw.endswith(b"\n"):sys.exit("the body does not end in a newline")@@ -146,11 +149,12 @@ lines = [json.loads(text) for text inpathlib.Path(outcomes_path).read_text(encoding="utf-8").splitlines()]outcome = {line["seq"]: line["outcome"] for line in lines}head, last = state["head_seq"], state["last_seq"]-base = max(0, head - 512)+base = max(0, head - window)for key in ("tree_id", "generation", "last_seq", "projection_root"):if body[key] != state[key]:sys.exit(f"{key} is {body[key]}, state.json has {state[key]}")-if body["base_seq"] != base or body["base_root"] != "00" * 32:+root = lines[base - 1]["projection_root"] if base else "00" * 32+if body["base_seq"] != base or body["base_root"] != root:sys.exit(f"the window starts at {body['base_seq']} {body['base_root']}")mask = base64.b64decode(body["mask"], validate=True)diff --git a/testing/vm/build-bundle.sh b/testing/vm/build-bundle.shindex 543924ad..6198bf34 100755--- a/testing/vm/build-bundle.sh+++ b/testing/vm/build-bundle.sh@@ -339,9 +339,10 @@ cleanup()trap cleanup EXIT# t7108 reaches the tree walk limits: the guest runs a target that walks-# 4 operations back from a cursor past 0 and 6 from 0.+# 4 operations back from a cursor past 0 and 6 from 0. t7110 reaches past+# the keepalive window, which the target lowers to the last 7 operations.make -C "$REPO_ROOT" -j"$jobs" \- TREE_LIMIT_CFLAGS="-DTREE_MAX_WALK_OPS=4 -DTREE_MAX_JOIN_OPS=6" \+ TREE_LIMIT_CFLAGS="-DTREE_MAX_WALK_OPS=4 -DTREE_MAX_JOIN_OPS=6 -DKEEPALIVE_WINDOW_OPS=7" \DESTDIR="$TMP_DIR/root" install-targetmake -C "$REPO_ROOT" -j"$jobs" -f Makefile.shirudo \PROFILE=target CONFIG_MK=config-target.mk verity-test-helpersdiff --git a/util/keepalive.h b/util/keepalive.hindex be9f56a0..dab79714 100644--- a/util/keepalive.h+++ b/util/keepalive.h@@ -22,6 +22,17 @@#define KEEPALIVE_MASK_BYTES (KEEPALIVE_MASK_OPS / 4)#define KEEPALIVE_MASK_B64_MAX (4 * ((KEEPALIVE_MASK_BYTES + 2) / 3) + 1)+/*+ * The body reports the last this many operations up to the head of the+ * tree, whatever segments hold them. A test build lowers it so that the+ * window of a short tree starts past seq 0, inside an older segment.+ */+#ifndef KEEPALIVE_WINDOW_OPS+#define KEEPALIVE_WINDOW_OPS KEEPALIVE_MASK_OPS+#endif+BUILD_BUG_ON(KEEPALIVE_WINDOW_OPS < 1 ||+ KEEPALIVE_WINDOW_OPS > KEEPALIVE_MASK_OPS);+/* Holds the largest body and its terminator. */#define KEEPALIVE_BODY_MAX 4096