Skip to content

eBPF-based security platform

fuzz: add a target for what bpm info prints of a package

Daniel Borkmann907e5286019d
diff --git a/.agents/skills/fuzz-target/SKILL.md b/.agents/skills/fuzz-target/SKILL.md
index d6e55833..9cef9627 100644
--- a/.agents/skills/fuzz-target/SKILL.md
+++ b/.agents/skills/fuzz-target/SKILL.md
@@ -14,8 +14,10 @@ Every parser of input from outside the host gets a target in
`testing/fuzz/fuzz-<name>.c` defines `LLVMFuzzerTestOneInput()`. System
headers come first, then `"fuzz.h"`, then shirudo's headers, which
poison the libc names they replace. A target that reaches a static
-function includes its `.c` file, a builtin's too. A parser that reads a
-file by name gets one from `fuzz_file()`, or `fuzz_gzip_file()`, both in
+function includes its `.c` file, a builtin's too, with a stub that
+`abort()`s for each function the builtin calls only on paths the target
+does not take, as `fuzz-bpm-info.c` does. A parser that reads a file by
+name gets one from `fuzz_file()`, or `fuzz_gzip_file()`, both in
`fuzz.h`. A target whose code prints sends stdout to `/dev/null` in
`LLVMFuzzerInitialize()`, as `fuzz-rodata.c` does, and one that needs a
directory makes it under `TMPDIR` and removes it `atexit()`, as
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 144725f7..234313f9 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -824,6 +824,9 @@ the functional tests run.
- A Beefile
* - ``bpm-index``
- A repository index
+ * - ``bpm-info``
+ - A ``.bee`` package as ``bpm info`` prints its README, its
+ upatches and the ``.rodata`` of each, as text and as JSON
* - ``bpm-package``
- A ``.bee`` package as ``bpm install`` unpacks it
* - ``bpm-tar``
@@ -879,11 +882,11 @@ the functional tests run.
* - ``upatch-info``
- A ``.upatch`` as ``upatch info`` prints it, as text and as JSON
-``upatch-info`` holds what its command prints from a file it has not
-verified to what ``text_sanitize()`` leaves for a terminal, UTF-8 with
-no control character but a tab, a line feed and a carriage return
-before a line feed, and its JSON to one document :src:`util/json.c`
-reads.
+``bpm-info`` and ``upatch-info`` hold what their command prints from a
+file it has not verified to what ``text_sanitize()`` leaves for a
+terminal, UTF-8 with no control character but a tab, a line feed and a
+carriage return before a line feed, and its JSON to one document
+:src:`util/json.c` reads.
Backend tests
~~~~~~~~~~~~~
@@ -966,8 +969,9 @@ Coverage by area
* - Packages and repositories
- ``t7001``, ``t7002``, ``t7003``, ``t7004``, ``t7005``,
``t7006``. Fuzz:
- ``beefile``, ``bpm-index``, ``bpm-package``, ``bpm-tar``,
- ``oci-http``, ``oci-manifest``, ``oci-ref``, ``registry-auth``. Unit:
+ ``beefile``, ``bpm-index``, ``bpm-info``, ``bpm-package``,
+ ``bpm-tar``, ``oci-http``, ``oci-manifest``, ``oci-ref``,
+ ``registry-auth``. Unit:
``bpm_oci``, ``bpm_package``
* - upatch tree
- ``t3011``, ``t5004``, ``t7005``, ``t7007``, ``t7008``, ``t7009``,
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 8141e3b5..6de42917 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -1299,6 +1299,20 @@ $(FUZZ_OUT)/fuzz-upatch-info: FUZZ_CFLAGS += $(TSS2_CFLAGS) $(LIBELF_CFLAGS) \
$(LUAJIT_CFLAGS) -DHAVE_LUAJIT
$(FUZZ_OUT)/fuzz-upatch-info: FUZZ_LIBS += $(TSS2_LIBS) $(LIBBPF_A) \
$(LIBELF_LIBS) $(LUAJIT_LIBS)
+$(FUZZ_OUT)/fuzz-bpm-info: testing/fuzz/fuzz-bpm-info.c $(FUZZ_BASE) \
+ testing/fuzz/output.c builtin-bpm.c builtin.h \
+ $(FUZZ_PACKAGE) util/bpm_source_config.c \
+ util/bpm_state.c util/bpm_tree_state.c \
+ util/chain.c util/eventlog.c util/help.c \
+ util/identity.c util/image_keys.c \
+ util/initrd.c util/kernel_build_id.c \
+ util/kernel_release.c util/keyring.c \
+ util/nv_writer.c util/options.c util/paths.c \
+ util/similar.c util/timefmt.c util/tpm.c \
+ util/tpm_wire.c util/upatch_pins.c \
+ util/upatch_rodata.c util/format.c $(LIBBPF_A)
+$(FUZZ_OUT)/fuzz-bpm-info: FUZZ_CFLAGS += $(TSS2_CFLAGS) $(LIBELF_CFLAGS)
+$(FUZZ_OUT)/fuzz-bpm-info: FUZZ_LIBS += $(TSS2_LIBS) $(LIBBPF_A) $(LIBELF_LIBS)
# A target that reaches static parsers or printers includes their source,
# which it then depends on without linking it a second time.
@@ -1310,6 +1324,7 @@ $(FUZZ_OUT)/fuzz-serializer: FUZZ_INCLUDED := util/upatch_maps.c
$(FUZZ_OUT)/fuzz-decoder: FUZZ_INCLUDED := util/upatch_decode.c
$(FUZZ_OUT)/fuzz-config-update: FUZZ_INCLUDED := util/config_update.c
$(FUZZ_OUT)/fuzz-upatch-info: FUZZ_INCLUDED := builtin-upatch.c
+$(FUZZ_OUT)/fuzz-bpm-info: FUZZ_INCLUDED := builtin-bpm.c
# A target compiles its sources and links in one step, so it depends on
# every header a source may include, and on the compiler and flags it was
diff --git a/testing/fuzz/corpus/bpm-info/member-name-escape b/testing/fuzz/corpus/bpm-info/member-name-escape
new file mode 100644
index 00000000..41b7e79d
Binary files /dev/null and b/testing/fuzz/corpus/bpm-info/member-name-escape differ
diff --git a/testing/fuzz/corpus/bpm-info/readme b/testing/fuzz/corpus/bpm-info/readme
new file mode 100644
index 00000000..5184b3c0
Binary files /dev/null and b/testing/fuzz/corpus/bpm-info/readme differ
diff --git a/testing/fuzz/corpus/bpm-info/rodata b/testing/fuzz/corpus/bpm-info/rodata
new file mode 100644
index 00000000..f82a390d
Binary files /dev/null and b/testing/fuzz/corpus/bpm-info/rodata differ
diff --git a/testing/fuzz/corpus/bpm-info/rodata-arrays b/testing/fuzz/corpus/bpm-info/rodata-arrays
new file mode 100644
index 00000000..6cb313b0
Binary files /dev/null and b/testing/fuzz/corpus/bpm-info/rodata-arrays differ
diff --git a/testing/fuzz/fuzz-bpm-info.c b/testing/fuzz/fuzz-bpm-info.c
new file mode 100644
index 00000000..70f99cf6
--- /dev/null
+++ b/testing/fuzz/fuzz-bpm-info.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * bpm info on a .bee, which it reads unverified: the README, the list of
+ * upatches, and the .rodata of each upatch as text and as JSON.
+ */
+
+#include <curl/curl.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/x509.h>
+
+#include "fuzz.h"
+
+#include "../../builtin-bpm.c"
+
+/* The upatches of a package whose .rodata one run prints, at most. */
+#define DATA_MEMBERS_MAX 4
+/* The size up to which bpm info reads a Beefile. */
+#define BEEFILE_MAX (4u * 1024u * 1024u)
+
+struct data_members {
+ char name[DATA_MEMBERS_MAX][PATH_MAX];
+ size_t n;
+};
+
+/* bpm info installs, removes, revokes and forwards nothing. */
+int upatch_install(const char *path)
+{
+ abort();
+}
+
+int upatch_remove(const char *path)
+{
+ abort();
+}
+
+int keys_revoke_image(const __u8 *der, size_t size, bool boot)
+{
+ abort();
+}
+
+bool build_available(void)
+{
+ abort();
+}
+
+int build_forward(const char *label, const char *const words[], int argc,
+ const char **argv)
+{
+ abort();
+}
+
+/* Held for the run, so that bpm info's own init and cleanup only count. */
+int LLVMFuzzerInitialize(int *argc, char ***argv)
+{
+ if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK)
+ abort();
+ fuzz_output_capture();
+ return 0;
+}
+
+/* The upatches by the names the archive has, as a script passes them. */
+static int add_data_member(void *ctx, const char *member, char type)
+{
+ struct data_members *m = ctx;
+
+ if (m->n < DATA_MEMBERS_MAX && type == '0' &&
+ str_has_prefix(member, "upatches/") &&
+ strscpy(m->name[m->n], member, sizeof(m->name[0])) > 0)
+ m->n++;
+ return 0;
+}
+
+static void find_data_members(const char *path, struct data_members *m)
+{
+ size_t beefile_len = 0;
+ __u8 *beefile = NULL;
+ char package_dir[101];
+ int fd;
+
+ fd = open(path, O_RDONLY | O_CLOEXEC);
+ if (fd < 0)
+ return;
+ if (!bpm_pkg_read_member_fd(fd, "Beefile", NULL, package_dir,
+ sizeof(package_dir), &beefile,
+ &beefile_len, BEEFILE_MAX))
+ bpm_pkg_visit_members_fd(fd, package_dir, add_data_member, m);
+ xfree(beefile, beefile_len + 1);
+ close(fd);
+}
+
+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
+{
+ static struct data_members members;
+ const char *member;
+ char path[64];
+ int fd;
+
+ fd = fuzz_gzip_file(data, size, path, sizeof(path));
+ if (fd < 0)
+ return 0;
+ fuzz_run(bpm_info_cmd, "bpm", "info", path, NULL);
+ fuzz_run(bpm_info_cmd, "bpm", "info", "upatches", path, "--all", NULL);
+ members.n = 0;
+ find_data_members(path, &members);
+ for (size_t i = 0; i < members.n; i++) {
+ member = members.name[i];
+ fuzz_run(bpm_info_cmd, "bpm", "info", "data", path, member, NULL);
+ fuzz_run(bpm_info_cmd, "bpm", "info", "data", path, member,
+ "--json", NULL);
+ }
+ close(fd);
+ return 0;
+}