eBPF-based security platform
testing: fuzz the map data the Lua decoders read
Daniel Borkmannd806a88dc152
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 1e10ae0d..732340db 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -735,6 +735,10 @@ testing/fuzz/fuzz-serializer: FUZZ_CFLAGS += $(LIBELF_CFLAGS)testing/fuzz/fuzz-serializer: FUZZ_LIBS += $(LIBBPF_A) $(LIBELF_LIBS)testing/fuzz/fuzz-cms: testing/fuzz/fuzz-cms.c $(FUZZ_BASE) util/keys.c \util/crypto_openssl.c util/io.c util/seal.c+testing/fuzz/fuzz-decoder: testing/fuzz/fuzz-decoder.c $(FUZZ_BASE) \+ util/upatch_decode.c util/upatch.c util/io.c+testing/fuzz/fuzz-decoder: FUZZ_CFLAGS += $(LUAJIT_CFLAGS) -DSHIRUDO_HAVE_LUAJIT+testing/fuzz/fuzz-decoder: FUZZ_LIBS += $(LUAJIT_LIBS)testing/fuzz/fuzz-otel: testing/fuzz/fuzz-otel.c $(FUZZ_BASE) \util/upatch_maps_otel.c util/crypto_openssl.c \util/tls_openssl.c util/io.c util/version_id.c@@ -766,6 +770,7 @@ testing/fuzz/fuzz-tree-state testing/fuzz/fuzz-kconfig: \testing/fuzz/fuzz-otel: FUZZ_INCLUDED := util/upatch_maps_otel.ctesting/fuzz/fuzz-kernel: FUZZ_INCLUDED := util/kmsg.ctesting/fuzz/fuzz-serializer: FUZZ_INCLUDED := util/upatch_maps.c+testing/fuzz/fuzz-decoder: FUZZ_INCLUDED := util/upatch_decode.c$(FUZZ_TARGETS): testing/fuzz/fuzz.h@printf ' FUZZ $@\n'diff --git a/testing/fuzz/corpus/decoder/counters b/testing/fuzz/corpus/decoder/countersnew file mode 100644index 00000000..00c6972bBinary files /dev/null and b/testing/fuzz/corpus/decoder/counters differdiff --git a/testing/fuzz/corpus/decoder/dns b/testing/fuzz/corpus/decoder/dnsnew file mode 100644index 00000000..9f629b30Binary files /dev/null and b/testing/fuzz/corpus/decoder/dns differdiff --git a/testing/fuzz/corpus/decoder/dns-two-chunks b/testing/fuzz/corpus/decoder/dns-two-chunksnew file mode 100644index 00000000..abc111d1Binary files /dev/null and b/testing/fuzz/corpus/decoder/dns-two-chunks differdiff --git a/testing/fuzz/corpus/decoder/exec b/testing/fuzz/corpus/decoder/execnew file mode 100644index 00000000..bbc4aaceBinary files /dev/null and b/testing/fuzz/corpus/decoder/exec differdiff --git a/testing/fuzz/corpus/decoder/file b/testing/fuzz/corpus/decoder/filenew file mode 100644index 00000000..26dcc312Binary files /dev/null and b/testing/fuzz/corpus/decoder/file differdiff --git a/testing/fuzz/corpus/decoder/listen b/testing/fuzz/corpus/decoder/listennew file mode 100644index 00000000..2185379cBinary files /dev/null and b/testing/fuzz/corpus/decoder/listen differdiff --git a/testing/fuzz/corpus/decoder/net b/testing/fuzz/corpus/decoder/netnew file mode 100644index 00000000..420c21aaBinary files /dev/null and b/testing/fuzz/corpus/decoder/net differdiff --git a/testing/fuzz/corpus/decoder/priv b/testing/fuzz/corpus/decoder/privnew file mode 100644index 00000000..5e3d3b04Binary files /dev/null and b/testing/fuzz/corpus/decoder/priv differdiff --git a/testing/fuzz/corpus/decoder/watch b/testing/fuzz/corpus/decoder/watchnew file mode 100644index 00000000..23d1e5f1Binary files /dev/null and b/testing/fuzz/corpus/decoder/watch differdiff --git a/testing/fuzz/corpus/decoder/watch_stat b/testing/fuzz/corpus/decoder/watch_statnew file mode 100644index 00000000..49850f85Binary files /dev/null and b/testing/fuzz/corpus/decoder/watch_stat differdiff --git a/testing/fuzz/fuzz-decoder.c b/testing/fuzz/fuzz-decoder.cnew file mode 100644index 00000000..ccdb72f6--- /dev/null+++ b/testing/fuzz/fuzz-decoder.c@@ -0,0 +1,109 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The map data a signed Lua decoder reads through the host API: a page+ * at a cursor and array entries made of the input, under the kernel-obs+ * decoders and the fixture that reads fixed records.+ */++#include <libgen.h>+#include <limits.h>+#include <stdlib.h>++#include "fuzz.h"+#include "io.h"+#include "upatch_decode.c"++static const char * const sources[] = {+ "../../upatches/kernel-obs/decoders/dns.lua",+ "../../upatches/kernel-obs/decoders/exec.lua",+ "../../upatches/kernel-obs/decoders/file.lua",+ "../../upatches/kernel-obs/decoders/listen.lua",+ "../../upatches/kernel-obs/decoders/net.lua",+ "../../upatches/kernel-obs/decoders/priv.lua",+ "../../upatches/kernel-obs/decoders/watch.lua",+ "../../upatches/kernel-obs/decoders/watch_stat.lua",+ "../fixtures/decoders/counters.lua",+};++static struct shirudo_upatch_decoder_view views[ARRAY_SIZE(sources)];++/* The decoders sit beside the target in the tree, wherever it runs from. */+int LLVMFuzzerInitialize(int *argc, char ***argv)+{+ char self[PATH_MAX], path[PATH_MAX];+ size_t len;+ char *src;++ snprintf(self, sizeof(self), "%s", (*argv)[0]);+ for (size_t i = 0; i < ARRAY_SIZE(sources); i++) {+ snprintf(path, sizeof(path), "%s/%s", dirname(self), sources[i]);+ snprintf(self, sizeof(self), "%s", (*argv)[0]);+ if (shirudo_read_file(path, 1 << 20, &src, &len))+ abort();+ views[i].source = src;+ views[i].source_len = (__u32)len;+ snprintf(views[i].source_name, sizeof(views[i].source_name),+ "%s", strrchr(sources[i], '/') + 1);+ snprintf(views[i].entrypoint, sizeof(views[i].entrypoint),+ "decode");+ }+ return 0;+}++int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)+{+ struct shirudo_map_entry entries[32];+ struct shirudo_upatch_decoder_view *d;+ struct shirudo_serial_page page = {};+ struct shirudo_map_view v = {+ .type_name = "array",+ .key_size = 4,+ .value_size = 8,+ .value_stride = 8,+ .n_cpus = 1,+ .max_entries = ARRAY_SIZE(entries),+ .serialized = true,+ .n_serial_pages = 1,+ };+ size_t out_len;+ char *out;++ /* A decoder, the page's cursor and the page. */+ if (size < 9)+ return 0;+ d = &views[data[0] % ARRAY_SIZE(views)];+ memcpy(&page.cursor, data + 1, sizeof(page.cursor));+ data += 9;+ size -= 9;+ snprintf(v.upatch, sizeof(v.upatch), "fuzz");+ snprintf(v.name, sizeof(v.name), "fuzz_map");++ page.data = xmalloc(size + 1);+ if (!page.data)+ return 0;+ memcpy(page.data, data, size);+ page.bytes = (__u32)size;+ page.chunks = size ? (__u32)((size - 1) / SHIRUDO_SERIAL_CHUNK_PAYLOAD + 1) : 1;+ page.done = true;+ v.serial_pages = &page;+ v.serial_bytes_total = size;++ for (v.n_entries = 0; v.n_entries < ARRAY_SIZE(entries) &&+ (v.n_entries + 1) * 12 <= size; v.n_entries++) {+ entries[v.n_entries].key = (__u8 *)page.data + v.n_entries * 12;+ entries[v.n_entries].value = (__u8 *)page.data ++ v.n_entries * 12 + 4;+ }+ v.entries = entries;++ if (!run_lua_decoder(d, &v, false, SHIRUDO_JSON_MAX_BYTES, &out,+ &out_len))+ xfree(out, out_len + 1);+ if (!run_lua_decoder(d, &v, true, SHIRUDO_JSON_MAX_BYTES, &out,+ &out_len))+ xfree(out, out_len + 1);+ xfree(page.data, size + 1);+ return 0;+}