Skip to content

eBPF-based security platform

upatch: exercise target arena serializer

John Fastabendc6f47284b385
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.sh
index e373a93d..c5f33fda 100755
--- a/contrib/build-test-vm-bundle.sh
+++ b/contrib/build-test-vm-bundle.sh
@@ -576,6 +576,7 @@ build_fixture marker.bpf.c shirudo-test-marker "$FIXTURE_WORK/trusted"
build_fixture map-export.bpf.c shirudo-test-map-export "$FIXTURE_WORK/trusted"
build_fixture otel-backend.bpf.c shirudo-test-otel-backend "$FIXTURE_WORK/trusted"
build_fixture serialized-metric.bpf.c shirudo-test-serialized-metric "$FIXTURE_WORK/trusted"
+build_fixture arena-tree-serializer.bpf.c shirudo-test-arena-tree "$FIXTURE_WORK/trusted"
build_fixture uprobe-multi.bpf.c shirudo-test-uprobe-multi "$FIXTURE_WORK/trusted"
build_fixture basic.bpf.c shirudo-test-foreign "$FIXTURE_WORK/foreign"
build_fixture basic.bpf.c shirudo-test-own-keyring "$FIXTURE_WORK/own"
@@ -608,6 +609,9 @@ otel_backend_payload="$($REPO_ROOT/shirudo upatch info \
serialized_metric_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/shirudo-test-serialized-metric.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
+arena_tree_payload="$($REPO_ROOT/shirudo upatch info \
+ "$TMP_DIR/fixtures/shirudo-test-arena-tree.upatch" --json |
+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
observability_payload="$($REPO_ROOT/shirudo upatch info \
"$TMP_DIR/fixtures/kernel-obs.upatch" --json |
python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"
@@ -663,6 +667,11 @@ build_tree_test_registry
-o "$TMP_DIR/fixtures/shirudo-test-serialized-metric.unload.upatch" \
--key "$FIXTURE_WORK/trusted/signing_key.pem" \
--cert "$FIXTURE_WORK/trusted/cert.der"
+"$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \
+ shirudo-test-arena-tree --payload-sha256 "$arena_tree_payload" \
+ -o "$TMP_DIR/fixtures/shirudo-test-arena-tree.unload.upatch" \
+ --key "$FIXTURE_WORK/trusted/signing_key.pem" \
+ --cert "$FIXTURE_WORK/trusted/cert.der"
"$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \
kernel-obs --payload-sha256 "$observability_payload" \
-o "$TMP_DIR/fixtures/kernel-obs.unload.upatch" \
diff --git a/testing/fixtures/arena-tree-serializer.bpf.c b/testing/fixtures/arena-tree-serializer.bpf.c
index 74159f92..0262a6f8 100644
--- a/testing/fixtures/arena-tree-serializer.bpf.c
+++ b/testing/fixtures/arena-tree-serializer.bpf.c
@@ -45,6 +45,13 @@ struct {
__type(value, struct shirudo_serial_chunk);
} shirudo_ser SEC(".maps");
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u32);
+} tree_error SEC(".maps");
+
struct tree_node {
__u32 key;
__u32 value;
@@ -270,4 +277,13 @@ int shirudo_serialize__tree_arena(struct shirudo_serialize_ctx *ctx)
return 0;
}
+SEC("syscall")
+int shirudo_serialize__tree_error(struct shirudo_serialize_ctx *ctx)
+{
+ if (!ctx || !ctx->invocation_id)
+ return -EINVAL;
+ set_error(ctx, -EINVAL);
+ return -EINVAL;
+}
+
char _license[] SEC("license") = "Dual BSD/GPL";
diff --git a/testing/helper/test-upatch-prog-metadata.c b/testing/helper/test-upatch-prog-metadata.c
index f084f841..fc48ccda 100644
--- a/testing/helper/test-upatch-prog-metadata.c
+++ b/testing/helper/test-upatch-prog-metadata.c
@@ -63,30 +63,37 @@ static int expect_attach(const struct shirudo_upatch *u, __u32 kind,
static int expect_serializer(const struct shirudo_upatch *u)
{
+ struct shirudo_upatch_prog_entry progs[2];
struct shirudo_upatch_prog_metadata md;
- struct shirudo_upatch_prog_entry prog;
+ bool arena = false, error = false;
const __u8 *raw;
+ size_t i;
- if (u->hdr.nr_maps != 3 || u->hdr.nr_progs != 1)
+ if (u->hdr.nr_maps != 4 || u->hdr.nr_progs != 2)
return fail("unexpected map/prog counts");
- if (u->hdr.metadata_len != sizeof(md) + sizeof(prog))
+ if (u->hdr.metadata_len != sizeof(md) + sizeof(progs))
return fail("unexpected metadata length");
raw = shirudo_upatch_metadata(u);
if (!raw)
return fail("missing metadata");
memcpy(&md, raw, sizeof(md));
- memcpy(&prog, raw + sizeof(md), sizeof(prog));
+ memcpy(progs, raw + sizeof(md), sizeof(progs));
if (le32toh(md.magic) != SHIRUDO_UPATCH_PROG_MAGIC ||
- le32toh(md.version) != SHIRUDO_UPATCH_PROG_VERSION)
- return fail("unexpected metadata magic/version");
- if (le32toh(md.nr_progs) != 1)
- return fail("unexpected metadata prog count");
- if (le32toh(prog.role) != SHIRUDO_UPATCH_PROG_ROLE_SERIALIZER)
- return fail("program is not a serializer");
- if (!memchr(prog.map, '\0', sizeof(prog.map)) ||
- strcmp(prog.map, "tree_arena") != 0)
- return fail("serializer map mismatch");
- return 0;
+ le32toh(md.version) != SHIRUDO_UPATCH_PROG_VERSION ||
+ le32toh(md.nr_progs) != 2)
+ return fail("unexpected metadata header");
+ for (i = 0; i < 2; i++) {
+ if (le32toh(progs[i].role) != SHIRUDO_UPATCH_PROG_ROLE_SERIALIZER ||
+ !memchr(progs[i].map, '\0', sizeof(progs[i].map)))
+ return fail("program is not a named serializer");
+ if (strcmp(progs[i].map, "tree_arena") == 0)
+ arena = true;
+ else if (strcmp(progs[i].map, "tree_error") == 0)
+ error = true;
+ else
+ return fail("unexpected serializer target map");
+ }
+ return arena && error ? 0 : fail("serializer map mismatch");
}
static int expect_uprobe_multi(const struct shirudo_upatch *u,
diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.sh
index 00348eae..94bd528e 100755
--- a/testing/run-target-tests.sh
+++ b/testing/run-target-tests.sh
@@ -47,6 +47,8 @@ for t in \
tamperproof/t1130-map-lockdown.sh \
tamperproof/t1131-pr-set-mm-identity.sh \
tamperproof/t1125-key-permission.sh \
+ tamperproof/t1132-upatch-integrity.sh \
+ t1133-upatch-arena-serializer.sh \
tamperproof/t1106-keyring-swap.sh
do
if sh "$t"; then
diff --git a/testing/t1133-upatch-arena-serializer.sh b/testing/t1133-upatch-arena-serializer.sh
new file mode 100644
index 00000000..61396210
--- /dev/null
+++ b/testing/t1133-upatch-arena-serializer.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1133 -- real target arena serializer pagination and error result.
+
+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
+NAME=shirudo-test-arena-tree
+LOAD="$FIXTURES/$NAME.upatch"
+UNLOAD="$FIXTURES/$NAME.unload.upatch"
+PIN="/run/shirudo/bpffs/upatches/$NAME/link"
+SERIALIZER="/run/shirudo/bpffs/upatches/$NAME/serializers/tree_arena"
+WORK=/tmp/shirudo-upatch-arena-$$
+TREE_JSON=$WORK.txt
+CHECK=$WORK.py
+restore() {
+ if [ -e "$PIN" ]; then
+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \
+ >/dev/null 2>&1 || true
+ fi
+ rm -f "$TREE_JSON" "$CHECK"
+}
+trap restore EXIT
+[ -s "$LOAD" ] || test_skip_all "missing arena-tree fixture"
+[ -s "$UNLOAD" ] || test_skip_all "missing arena-tree unload fixture"
+cat >"$CHECK" <<'PY'
+import re, struct, sys
+text = open(sys.argv[1], encoding="utf-8").read()
+assert re.search(r"Serialized pages:\s+3", text), text
+parts = re.split(r"\nPage (\d+):\n", text)
+assert len(parts) == 7, len(parts)
+records = []
+for index in range(3):
+ page_index = int(parts[1 + index * 2])
+ body = parts[2 + index * 2]
+ assert page_index == index, page_index
+ cursor = int(re.search(r"Cursor:\s+(\d+)", body).group(1))
+ next_cursor = int(re.search(r"Next cursor:\s+(\d+)", body).group(1))
+ done = re.search(r"Done:\s+(true|false)", body).group(1)
+ assert cursor == index * 64, cursor
+ assert next_cursor == (index + 1) * 64, next_cursor
+ assert done == ("true" if index == 2 else "false"), done
+ raw_hex = "".join(line.split(":", 1)[1].replace(" ", "")
+ for line in body.splitlines()
+ if re.match(r"\s*[0-9a-fA-F]{8}:", line))
+ raw = bytes.fromhex(raw_hex)
+ assert len(raw) == 64 * 16, (index, len(raw))
+ records.extend(struct.unpack_from("<IIii", raw, off)
+ for off in range(0, len(raw), 16))
+assert len(records) == 192, len(records)
+for key, value, left, right in records:
+ assert value == key * 3 + 7, (key, value)
+ assert left == (2 * key + 1 if 2 * key + 1 < 192 else -1), (key, left)
+ assert right == (2 * key + 2 if 2 * key + 2 < 192 else -1), (key, right)
+PY
+test_expect_success 'loaded arena serializer returns all pages and records' '
+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&
+ [ -e "$SERIALIZER" ] &&
+ "$SHIRUDO_BIN" upatch map "$NAME" tree_arena >"$TREE_JSON" 2>&1 &&
+ python3 "$CHECK" "$TREE_JSON" &&
+ {
+ if "$SHIRUDO_BIN" upatch map "$NAME" tree_error \
+ >/dev/null 2>&1; then false; fi
+ }
+'
+test_done