eBPF-based security platform
testing: fuzz the upatch tree's state and selector readers
Daniel Borkmann1c96b8fcaaee
diff --git a/Makefile.shirudo b/Makefile.shirudoindex fa82d6b8..1ea7dbfb 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -705,10 +705,19 @@ testing/fuzz/fuzz-registry-auth: testing/fuzz/fuzz-registry-auth.c \$(FUZZ_BASE) $(FUZZ_OCI)testing/fuzz/fuzz-config: testing/fuzz/fuzz-config.c $(FUZZ_BASE) \util/config.c util/io.c util/bpm_source_config.c+FUZZ_TREE_STATE := util/bpm_tree_state.c util/io.c util/seal.c \+ util/kernel_build_id.c util/kernel_release.c \+ util/crypto_openssl.c testing/fuzz/chain.c+testing/fuzz/fuzz-tree-state: testing/fuzz/fuzz-tree-state.c $(FUZZ_BASE) \+ $(FUZZ_TREE_STATE)+testing/fuzz/fuzz-kconfig: testing/fuzz/fuzz-kconfig.c $(FUZZ_BASE) \+ $(FUZZ_TREE_STATE)# A target that reaches static parsers includes their source, which it# then depends on without linking it a second time.testing/fuzz/fuzz-oci-http: FUZZ_INCLUDED := util/bpm_oci.c+testing/fuzz/fuzz-tree-state testing/fuzz/fuzz-kconfig: \+ FUZZ_INCLUDED := util/bpm_tree_state.c$(FUZZ_TARGETS): testing/fuzz/fuzz.h@printf ' FUZZ $@\n'diff --git a/testing/fuzz/chain.c b/testing/fuzz/chain.cnew file mode 100644index 00000000..8cb79312--- /dev/null+++ b/testing/fuzz/chain.c@@ -0,0 +1,22 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The digests of util/chain.c for the fuzz targets, without its TPM.+ */++#include <string.h>++#include "chain.h"+#include "crypto.h"++void shirudo_sha256(const void *data, size_t len,+ __u8 out[SHIRUDO_CHAIN_DIGEST_LEN])+{+ shirudo_sha256_buffer(data, len, out);+}++int shirudo_ct_eq(const void *a, const void *b, size_t n)+{+ return !memcmp(a, b, n);+}diff --git a/testing/fuzz/corpus/kconfig/config b/testing/fuzz/corpus/kconfig/confignew file mode 100644index 00000000..c37b7c0b--- /dev/null+++ b/testing/fuzz/corpus/kconfig/config@@ -0,0 +1,10 @@+#+# Automatically generated file; DO NOT EDIT.+# Linux/x86 7.4.0 Kernel Configuration+#+CONFIG_LOCALVERSION="-shirudo"+CONFIG_MODULES=y+CONFIG_BPF_LSM=y+CONFIG_LSM="landlock,lockdown,yama,integrity,bpf"+# CONFIG_DEBUG_INFO_BTF is not set+CONFIG_NR_CPUS=8192diff --git a/testing/fuzz/corpus/kconfig/config.gz b/testing/fuzz/corpus/kconfig/config.gznew file mode 100644index 00000000..7454d4fdBinary files /dev/null and b/testing/fuzz/corpus/kconfig/config.gz differdiff --git a/testing/fuzz/corpus/kconfig/node.labels b/testing/fuzz/corpus/kconfig/node.labelsnew file mode 100644index 00000000..c3889c02--- /dev/null+++ b/testing/fuzz/corpus/kconfig/node.labels@@ -0,0 +1,5 @@+# /etc/shirudo/node.labels+role = edge+site=fra1++ zone = bdiff --git a/testing/fuzz/corpus/tree-state/state.json b/testing/fuzz/corpus/tree-state/state.jsonnew file mode 100644index 00000000..140c1fe5--- /dev/null+++ b/testing/fuzz/corpus/tree-state/state.json@@ -0,0 +1,9 @@+{+ "schema": 1,+ "tree_id": "prod",+ "generation": 3,+ "last_seq": 5,+ "projection_root": "5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27",+ "projection_root_tpm": "5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27",+ "manifest_digest": "sha256:05b3abf2579a5eb66403cd78be557fd860633a1fe2103c7642030defe32c657f"+}diff --git a/testing/fuzz/corpus/tree-state/tree-eventlog b/testing/fuzz/corpus/tree-state/tree-eventlognew file mode 100644index 00000000..c8de3a31--- /dev/null+++ b/testing/fuzz/corpus/tree-state/tree-eventlog@@ -0,0 +1,3 @@+{"seq":1,"digest":"sha256:71a0ef7195df486543f63f0cb1dff83ee245c10fb566219b128a7fa99a8bbbac","projection_root":"b4de282050b5656ffc2bb03536206b9f657b44eac91cef95732adf7478b9d245"}+{"seq":2,"digest":"sha256:e85fddc91eb032ab01ba4273740111289939def3322ad7da2c163273fa3e19d4","projection_root":"321431aaf6250ce18fcca71ad3717ee9821e2cb20c68a70dbbfbfef0eee2572e"}+{"seq":5,"digest":"sha256:2f5a5ab8709118d4fcf9447ce53bbe244b7fadd410fe4358e1d6ee4da9eb3315","projection_root":"5c7761b120a9cf59e79e5a078227b450b7a39294db74bb3ba28e28c6ae196d27"}diff --git a/testing/fuzz/fuzz-kconfig.c b/testing/fuzz/fuzz-kconfig.cnew file mode 100644index 00000000..f9c1e49c--- /dev/null+++ b/testing/fuzz/fuzz-kconfig.c@@ -0,0 +1,46 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * A kernel config, plain or gzip'd, as an upatch tree selector reads it,+ * and node.labels.+ */++#include "fuzz.h"+#include "bpm_tree_state.c"++static struct shirudo_tree_selector sel;+static int slots[SHIRUDO_TREE_SELECTOR_HASH_SIZE];++int LLVMFuzzerInitialize(int *argc, char ***argv)+{+ static const char * const keys[] = {+ "CONFIG_BPF_LSM", "CONFIG_DEBUG_INFO_BTF", "CONFIG_LOCALVERSION",+ "CONFIG_LSM", "CONFIG_MODULES",+ };++ for (size_t i = 0; i < ARRAY_SIZE(keys); i++)+ snprintf(sel.kconfig[i].key, sizeof(sel.kconfig[i].key), "%s",+ keys[i]);+ sel.n_kconfig = ARRAY_SIZE(keys);+ selector_hash_build(&sel, slots);+ return 0;+}++int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)+{+ static char values[SHIRUDO_TREE_MAX_KCONFIG][SHIRUDO_TREE_STRING_MAX];+ static struct shirudo_tree_kv labels[SHIRUDO_TREE_MAX_LABELS];+ bool found[SHIRUDO_TREE_MAX_KCONFIG] = {};+ char path[64];+ int fd, n;++ fd = fuzz_file(data, size, path, sizeof(path));+ if (fd < 0)+ return 0;+ read_selector_config_file(path, &sel, slots, values, found, false);+ read_selector_config_file(path, &sel, slots, values, found, true);+ read_config_file(path, labels, &n, ARRAY_SIZE(labels), false);+ close(fd);+ return 0;+}diff --git a/testing/fuzz/fuzz-tree-state.c b/testing/fuzz/fuzz-tree-state.cnew file mode 100644index 00000000..12809103--- /dev/null+++ b/testing/fuzz/fuzz-tree-state.c@@ -0,0 +1,34 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The upatch tree's state.json and its eventlog, as a target reads them+ * back before a sync.+ */++#include "fuzz.h"+#include "paths.h"++static char fuzz_path[64];++#undef SHIRUDO_BPM_TREE_STATE_PATH+#define SHIRUDO_BPM_TREE_STATE_PATH fuzz_path+#undef SHIRUDO_BPM_TREE_EVENTLOG_PATH+#define SHIRUDO_BPM_TREE_EVENTLOG_PATH fuzz_path++#include "bpm_tree_state.c"++int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)+{+ struct shirudo_tree_state state;+ __u8 root[32];+ int fd;++ fd = fuzz_file(data, size, fuzz_path, sizeof(fuzz_path));+ if (fd < 0)+ return 0;+ shirudo_tree_state_read(&state);+ shirudo_tree_event_replay(root);+ close(fd);+ return 0;+}