Skip to content

eBPF-based security platform

fuzz: add a target for what upatch info prints

Daniel Borkmannc9c927f9763b
diff --git a/.agents/skills/fuzz-target/SKILL.md b/.agents/skills/fuzz-target/SKILL.md
index 984b417f..d6e55833 100644
--- a/.agents/skills/fuzz-target/SKILL.md
+++ b/.agents/skills/fuzz-target/SKILL.md
@@ -14,13 +14,21 @@ 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 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
+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
+`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
`fuzz-config-update.c` does.
+A target of what a command prints from a file it has not verified calls
+`fuzz_output_capture()` in `LLVMFuzzerInitialize()` and runs the command
+as the CLI does with `fuzz_run()` of `testing/fuzz/output.c`, as
+`fuzz-upatch-info.c` does. That holds what the command printed to the
+promise of `text_sanitize()` for a terminal, or to one JSON document
+after `--json`. The command must not reach the network, the kernel or
+the TPM: give it a local file and options that keep it off those paths.
+
Not crashing is the least a target checks. Make it `abort()` when a
property of the parser breaks:
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 6961bf2c..144725f7 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -876,6 +876,14 @@ the functional tests run.
- The tree's ``state.json``, eventlog and outcome lines
* - ``upatch``
- A ``.upatch`` container and its decoder table
+ * - ``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.
Backend tests
~~~~~~~~~~~~~
diff --git a/Makefile.shirudo b/Makefile.shirudo
index d0d7478a..8141e3b5 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -1277,9 +1277,31 @@ $(FUZZ_OUT)/fuzz-kconfig: testing/fuzz/fuzz-kconfig.c $(FUZZ_BASE) \
$(FUZZ_OUT)/fuzz-quote-check: testing/fuzz/fuzz-quote-check.c $(FUZZ_BASE) \
tools/quote-check/row.c util/tpm_wire.c \
util/crypto_openssl.c util/io.c util/seal.c
-
-# A target that reaches static parsers includes their source, which it
-# then depends on without linking it a second time.
+$(FUZZ_OUT)/fuzz-upatch-info: testing/fuzz/fuzz-upatch-info.c $(FUZZ_BASE) \
+ testing/fuzz/output.c builtin-upatch.c \
+ builtin.h util/chain.c util/crypto_openssl.c \
+ util/curl.c util/elf_target.c util/eventlog.c \
+ util/help.c util/identity.c util/initrd.c \
+ util/io.c util/kernel_abi.c util/keyring.c \
+ util/measured.c util/nv_writer.c \
+ util/options.c util/paths.c util/report.c \
+ util/serializer.c util/similar.c \
+ util/timefmt.c util/tls_openssl.c util/tpm.c \
+ util/tpm_wire.c util/upatch.c \
+ util/upatch_decode.c util/upatch_maps.c \
+ util/upatch_maps_otel.c util/upatch_pins.c \
+ util/upatch_resolve.c util/upatch_table.c \
+ util/upatch_verify.c util/version_id.c \
+ util/xdp.c $(FUZZ_SEAL) $(LIBBPF_A)
+# util/upatch_decode.c defines upatch_decode_map_signal(), which
+# builtin-upatch.c calls, with LuaJIT alone; upatch info runs no Lua.
+$(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)
+
+# A target that reaches static parsers or printers includes their source,
+# which it then depends on without linking it a second time.
$(FUZZ_OUT)/fuzz-oci-http: FUZZ_INCLUDED := util/bpm_oci.c
$(FUZZ_OUT)/fuzz-tree-state $(FUZZ_OUT)/fuzz-kconfig: \
FUZZ_INCLUDED := util/bpm_tree_state.c
@@ -1287,6 +1309,7 @@ $(FUZZ_OUT)/fuzz-otel: FUZZ_INCLUDED := util/upatch_maps_otel.c
$(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
# 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/upatch-info/decoders b/testing/fuzz/corpus/upatch-info/decoders
new file mode 100644
index 00000000..2780ca60
Binary files /dev/null and b/testing/fuzz/corpus/upatch-info/decoders differ
diff --git a/testing/fuzz/corpus/upatch-info/unload-directive b/testing/fuzz/corpus/upatch-info/unload-directive
new file mode 100644
index 00000000..b1c04aca
Binary files /dev/null and b/testing/fuzz/corpus/upatch-info/unload-directive differ
diff --git a/testing/fuzz/corpus/upatch-info/unsigned-sections b/testing/fuzz/corpus/upatch-info/unsigned-sections
new file mode 100644
index 00000000..2469a39a
Binary files /dev/null and b/testing/fuzz/corpus/upatch-info/unsigned-sections differ
diff --git a/testing/fuzz/fuzz-upatch-info.c b/testing/fuzz/fuzz-upatch-info.c
new file mode 100644
index 00000000..a2d01eb6
--- /dev/null
+++ b/testing/fuzz/fuzz-upatch-info.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * upatch info on a .upatch, which it reads unverified, as text and as
+ * JSON.
+ */
+
+#include "fuzz.h"
+
+#include "../../builtin-upatch.c"
+
+int LLVMFuzzerInitialize(int *argc, char ***argv)
+{
+ fuzz_output_capture();
+ return 0;
+}
+
+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
+{
+ char path[64];
+ int fd;
+
+ fd = fuzz_file(data, size, path, sizeof(path));
+ if (fd < 0)
+ return 0;
+ fuzz_run(cmd_upatch_info, "info", path, NULL);
+ fuzz_run(cmd_upatch_info, "info", path, "--json", NULL);
+ close(fd);
+ return 0;
+}
diff --git a/testing/fuzz/fuzz.h b/testing/fuzz/fuzz.h
index 76d587e9..f6b0327a 100644
--- a/testing/fuzz/fuzz.h
+++ b/testing/fuzz/fuzz.h
@@ -19,6 +19,21 @@
int LLVMFuzzerInitialize(int *argc, char ***argv);
int LLVMFuzzerTestOneInput(const __u8 *data, size_t size);
+/*
+ * testing/fuzz/output.c, for the targets of what a command prints, which
+ * call fuzz_output_capture() once to make stdout a memfd. fuzz_run() runs
+ * cmd with the words up to the NULL as its argv, as the CLI does, and
+ * returns its exit status. It abort()s unless what the command printed
+ * to stdout is none or one JSON value that json_walk() takes and a line
+ * feed, where the last word is --json, and otherwise keeps the promise of
+ * text_sanitize() for a terminal: well-formed UTF-8 with no C0 or C1
+ * control and no DEL, but for a tab, a line feed and a carriage return
+ * before a line feed.
+ */
+void fuzz_output_capture(void);
+int fuzz_run(int (*cmd)(int argc, const char **argv), const char *word,
+ ...) __sentinel;
+
/*
* The input as a file for the parsers that read one by name: a memfd,
* named through /proc/self/fd. -1, or the descriptor to close after.
diff --git a/testing/fuzz/output.c b/testing/fuzz/output.c
new file mode 100644
index 00000000..af9ff30e
--- /dev/null
+++ b/testing/fuzz/output.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * A command run as the CLI runs it, with what it prints to stdout held to
+ * the rules of a terminal or of JSON, for the targets of the printers.
+ */
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#include "fuzz.h"
+
+#include <shirudo/string.h>
+
+#include "json.h"
+#include "malloc.h"
+
+/* The words of a command line fuzz_run() takes. */
+#define RUN_ARGC_MAX 16
+
+static int capture_fd = -1;
+static char *printed;
+static size_t printed_len;
+static size_t printed_cap;
+
+/*
+ * A memfd rather than a memstream assigned to stdout, which would miss
+ * what a printer writes to STDOUT_FILENO with write_complete().
+ */
+void fuzz_output_capture(void)
+{
+ capture_fd = memfd_create("fuzz-stdout", MFD_CLOEXEC);
+ if (capture_fd < 0 || dup2(capture_fd, STDOUT_FILENO) < 0)
+ abort();
+}
+
+/* What was written to stdout since the last take, which empties it. */
+static void output_take(void)
+{
+ struct stat st;
+ size_t size;
+ char *grown;
+
+ if (fflush(stdout) || fstat(capture_fd, &st) || st.st_size < 0)
+ abort();
+ size = (size_t)st.st_size;
+ if (size >= printed_cap) {
+ grown = xreallocarray(printed, printed_cap, size + 1, 1);
+ if (!grown)
+ abort();
+ printed = grown;
+ printed_cap = size + 1;
+ }
+ if (pread(capture_fd, printed, size, 0) != (ssize_t)size ||
+ ftruncate(capture_fd, 0) || lseek(capture_fd, 0, SEEK_SET))
+ abort();
+ printed[size] = '\0';
+ printed_len = size;
+}
+
+/* Whether the width bytes at i are a control text_sanitize() takes out. */
+static bool stray_control(const char *s, size_t len, size_t i, size_t width)
+{
+ __u8 c = s[i];
+
+ if (width == 2)
+ return c == 0xc2 && (__u8)s[i + 1] < 0xa0;
+ if (width != 1 || (c >= 0x20 && c != 0x7f) || c == '\t' || c == '\n')
+ return false;
+ return c != '\r' || i + 1 == len || s[i + 1] != '\n';
+}
+
+static bool text_sanitized(const char *s, size_t len)
+{
+ size_t i, width;
+
+ for (i = 0; i < len; i += width) {
+ width = utf8_width(s + i, len - i);
+ if (!width || stray_control(s, len, i, width))
+ return false;
+ }
+ return true;
+}
+
+static bool one_json_line(const char *s, size_t len)
+{
+ static const struct json_visitor any;
+
+ return !len ||
+ (s[len - 1] == '\n' && !json_walk(s, len - 1, &any, NULL));
+}
+
+int fuzz_run(int (*cmd)(int argc, const char **argv), const char *word, ...)
+{
+ const char *argv[RUN_ARGC_MAX] = {};
+ int argc = 0, status;
+ va_list ap;
+ bool json;
+
+ va_start(ap, word);
+ for (; word; word = va_arg(ap, const char *)) {
+ if (argc == RUN_ARGC_MAX)
+ abort();
+ argv[argc++] = word;
+ }
+ va_end(ap);
+ status = cmd(argc, argv);
+ output_take();
+ json = argc && !strcmp(argv[argc - 1], "--json");
+ if (json ? !one_json_line(printed, printed_len) :
+ !text_sanitized(printed, printed_len))
+ abort();
+ return status;
+}