Skip to content

eBPF-based security platform

testing: verify names keep their terminator and are never cut

Daniel Borkmanned3f416f688a
diff --git a/Makefile.shirudo b/Makefile.shirudo
index eab00eb6..325c4ecf 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -621,6 +621,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \
testing/helper/test-otel-map \
testing/helper/test-upatch-map-types \
testing/helper/test-upatch-prog-metadata \
+ testing/helper/test-upatch-rodata \
testing/helper/test-upatch-decode \
testing/helper/test-upatch-serializer-pages \
testing/helper/test-signed-load \
@@ -809,6 +810,13 @@ testing/helper/test-upatch-serializer-pages: testing/helper/test-upatch-serializ
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)
+testing/helper/test-upatch-rodata: testing/helper/test-upatch-rodata.c \
+ util/upatch.o util/seal.o util/json.o \
+ util/string.o util/malloc.o util/io.o \
+ util/log.o util/hex.o
+ @printf ' CC $@\n'
+ $(Q)$(CC) $(CFLAGS) -o $@ $^ $(STATIC_LIBS) $(DYNAMIC_LIBS)
+
testing/helper/test-upatch-decode: testing/helper/test-upatch-decode.c \
util/upatch_decode.o util/upatch.o util/malloc.o util/hex.o \
util/string.o util/seal.o util/io.o util/log.o
diff --git a/testing/helper/test-upatch-rodata.c b/testing/helper/test-upatch-rodata.c
new file mode 100644
index 00000000..34edb04e
--- /dev/null
+++ b/testing/helper/test-upatch-rodata.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <shirudo/misc.h>
+
+#include "../../util/upatch_rodata.c"
+
+/*
+ * A typedef of len characters behind ptrs pointers, named into a buffer
+ * of cap bytes: the result, and whether the buffer kept its terminator.
+ */
+static int name_into(size_t len, int ptrs, size_t cap)
+{
+ char name[512], buf[512];
+ struct btf *btf;
+ size_t off = 0;
+ int id, rc;
+
+ if (len >= sizeof(name) || cap > sizeof(buf))
+ return 2;
+ memset(name, 'n', len);
+ name[len] = '\0';
+ memset(buf, 'x', sizeof(buf));
+ btf = btf__new_empty();
+ if (!btf)
+ return 2;
+ id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
+ if (id > 0)
+ id = btf__add_typedef(btf, name, id);
+ while (id > 0 && ptrs--)
+ id = btf__add_ptr(btf, id);
+ if (id <= 0) {
+ btf__free(btf);
+ return 2;
+ }
+ rc = append_type_name(btf, (__u32)id, buf, cap, &off, 0);
+ btf__free(btf);
+ printf("rc=%d off=%zu terminated=%d\n", rc, off,
+ !!memchr(buf, '\0', cap));
+ if (!memchr(buf, '\0', cap))
+ return 1;
+ return rc ? 3 : 0;
+}
+
+int main(int argc, char **argv)
+{
+ __u64 len, ptrs;
+
+ if (argc != 3 || parse_u64(argv[1], strlen(argv[1]), 500, &len) ||
+ parse_u64(argv[2], strlen(argv[2]), 4, &ptrs))
+ return 2;
+ return name_into((size_t)len, (int)ptrs, 256);
+}
diff --git a/testing/t0057-string-bounds.sh b/testing/t0057-string-bounds.sh
new file mode 100755
index 00000000..52bf148b
--- /dev/null
+++ b/testing/t0057-string-bounds.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t0057 -- names keep their terminator and are never cut into others: a
+# pointer type whose name fills the buffer, a signed name section with
+# no terminator, an upatch name the targets refuse or the header cannot
+# hold, and a tree repository longer than its field.
+
+cd "$(dirname "$0")"
+. ./test-lib.sh
+
+S=../shirudo
+B=../tools/shirudo-build/shirudo-build
+R=./helper/test-upatch-rodata
+[ -x "$S" ] && [ -x "$B" ] && [ -x "$R" ] || test_skip_all "binaries not built"
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+WORK=$(mktemp -d)
+trap 'rm -rf "$WORK"' EXIT
+"$S" genkey --keys-dir "$WORK/keys" >/dev/null 2>&1 ||
+ test_skip_all "cannot generate a signing key"
+PAYLOAD=$(printf 'a%.0s' $(seq 64))
+
+test_expect_success 'a pointer type name that fills the buffer is refused, terminated' '
+ "$R" 253 1 >"$WORK/fits" && grep -q "off=255 terminated=1" "$WORK/fits" &&
+ ! "$R" 254 1 >"$WORK/full" && grep -q "rc=-28 .*terminated=1" "$WORK/full" &&
+ ! "$R" 252 2 >"$WORK/full2" && grep -q "terminated=1" "$WORK/full2"
+'
+
+test_expect_success 'an upatch name the targets refuse is not signed' '
+ ! "$B" --unload ../x -o "$WORK/bad.upatch" --payload-sha256 "$PAYLOAD" \
+ --keys-dir "$WORK/keys" >/dev/null 2>"$WORK/bad.err" &&
+ grep -q "is not 1 to 255 characters" "$WORK/bad.err" &&
+ [ ! -e "$WORK/bad.upatch" ]
+'
+
+test_expect_success 'an upatch name the header cannot hold is not cut and signed' '
+ long=$(printf "n%.0s" $(seq 256)) &&
+ ! "$B" --unload "$long" -o "$WORK/long.upatch" --payload-sha256 "$PAYLOAD" \
+ --keys-dir "$WORK/keys" >/dev/null 2>&1 &&
+ [ ! -e "$WORK/long.upatch" ] &&
+ short=$(printf "n%.0s" $(seq 255)) &&
+ "$B" --unload "$short" -o "$WORK/short.upatch" --payload-sha256 "$PAYLOAD" \
+ --keys-dir "$WORK/keys" >/dev/null 2>&1
+'
+
+test_expect_success 'a signed name section without its terminator is no name' '
+ "$B" --unload good -o "$WORK/good.upatch" --payload-sha256 "$PAYLOAD" \
+ --keys-dir "$WORK/keys" >/dev/null 2>&1 &&
+ "$S" upatch info "$WORK/good.upatch" | grep -q "Name: *good" &&
+ python3 - "$WORK/good.upatch" "$WORK/unterminated.upatch" <<"PY" &&
+import sys
+data = bytearray(open(sys.argv[1], "rb").read())
+name = b"good" + bytes(252)
+# The header, the template data and last the signed sections carry it.
+at = data.rfind(name)
+assert at > 16, "no signed name section"
+data[at:at + 256] = b"A" * 256
+open(sys.argv[2], "wb").write(data)
+PY
+ "$S" upatch info "$WORK/unterminated.upatch" >"$WORK/info" 2>&1;
+ grep -q "Name: *(none)" "$WORK/info" && ! grep -q "AAAA" "$WORK/info"
+'
+
+test_expect_success 'a tree repository longer than its field is refused, not cut' '
+ repo="oci://registry.invalid/$(printf "r%.0s" $(seq 600))" &&
+ ! "$B" bpm tree append "$WORK/absent.bee" "$repo" --keys-dir "$WORK/keys" \
+ >/dev/null 2>"$WORK/tree.err" &&
+ grep -q "repository is longer than" "$WORK/tree.err"
+'
+
+test_done