eBPF-based security platform
testing: fuzz what the OTLP exporter parses
Daniel Borkmann90ad3a9c014c
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 3010516c..d29ac562 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -708,6 +708,9 @@ testing/fuzz/fuzz-bpm-tar: testing/fuzz/fuzz-bpm-tar.c $(FUZZ_BASE) \$(FUZZ_PACKAGE)testing/fuzz/fuzz-bpm-package: testing/fuzz/fuzz-bpm-package.c $(FUZZ_BASE) \$(FUZZ_PACKAGE)+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.ctesting/fuzz/fuzz-beefile: testing/fuzz/fuzz-beefile.c $(FUZZ_BASE) \tools/shirudo-build/bpm-build.c util/upatch.c \util/timefmt.c util/paths.c util/crypto_openssl.c \@@ -733,6 +736,7 @@ testing/fuzz/fuzz-kconfig: testing/fuzz/fuzz-kconfig.c $(FUZZ_BASE) \testing/fuzz/fuzz-oci-http: FUZZ_INCLUDED := util/bpm_oci.ctesting/fuzz/fuzz-tree-state testing/fuzz/fuzz-kconfig: \FUZZ_INCLUDED := util/bpm_tree_state.c+testing/fuzz/fuzz-otel: FUZZ_INCLUDED := util/upatch_maps_otel.c$(FUZZ_TARGETS): testing/fuzz/fuzz.h@printf ' FUZZ $@\n'diff --git a/testing/fuzz/corpus/otel/decoded-map b/testing/fuzz/corpus/otel/decoded-mapnew file mode 100644index 00000000..61f155b5--- /dev/null+++ b/testing/fuzz/corpus/otel/decoded-map@@ -0,0 +1 @@+{"map":"exec","records":[{"pid":4242,"comm":"bash","argv":["/bin/sh","-c","id"],"ok":true,"ratio":0.5,"none":null}],"overflow":0}\ No newline at end of filediff --git a/testing/fuzz/corpus/otel/headers b/testing/fuzz/corpus/otel/headersnew file mode 100644index 00000000..20506da3--- /dev/null+++ b/testing/fuzz/corpus/otel/headers@@ -0,0 +1 @@+x-tenant-id=6f0c2a4e-1b7d-4c39-9a52-3e8d1f7b9c10, authorization=Bearer%20abc, x-custom = a%2Cb\ No newline at end of filediff --git a/testing/fuzz/corpus/otel/partial-logs b/testing/fuzz/corpus/otel/partial-logsnew file mode 100644index 00000000..74bda04a--- /dev/null+++ b/testing/fuzz/corpus/otel/partial-logs@@ -0,0 +1,5 @@+HTTP/1.1 503 Service Unavailable+Retry-After: 7+Content-Type: application/json++{"partialSuccess":{"rejectedLogRecords":"3","errorMessage":"quota exceeded"}}\ No newline at end of filediff --git a/testing/fuzz/corpus/otel/partial-metrics b/testing/fuzz/corpus/otel/partial-metricsnew file mode 100644index 00000000..9755a083--- /dev/null+++ b/testing/fuzz/corpus/otel/partial-metrics@@ -0,0 +1 @@+{"partialSuccess":{"rejectedDataPoints":2,"errorMessage":"bad unit"}}\ No newline at end of filediff --git a/testing/fuzz/fuzz-otel.c b/testing/fuzz/fuzz-otel.cnew file mode 100644index 00000000..b604b33a--- /dev/null+++ b/testing/fuzz/fuzz-otel.c@@ -0,0 +1,112 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What an OTLP collector answers, the response header lines and the+ * partial-success body, the decoded map JSON a log record carries, and+ * the configured OTEL_EXPORTER_OTLP_HEADERS.+ */++#include "fuzz.h"+#include "upatch_maps_otel.c"++/* The map decoding the exporter calls is fuzzed on its own. */+const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 id)+{+ return NULL;+}++bool shirudo_upatch_map_is_percpu(__u32 type)+{+ return false;+}++int shirudo_upatch_decode_map_json(const struct shirudo_map_view *v,+ char **out, size_t *out_len)+{+ return -EOPNOTSUPP;+}++int shirudo_upatch_decode_map_json_limit(const struct shirudo_map_view *v,+ size_t max_bytes,+ char **out, size_t *out_len)+{+ return -EOPNOTSUPP;+}++int shirudo_upatch_decode_map_otel(const struct shirudo_map_view *v,+ char **out, size_t *out_len)+{+ return -EOPNOTSUPP;+}++int shirudo_upatch_decode_map_file_json(const char *path,+ const struct shirudo_map_view *v,+ char **out, size_t *out_len)+{+ return -EOPNOTSUPP;+}++int shirudo_upatch_decode_map_file_json_limit(const char *path,+ const struct shirudo_map_view *v,+ size_t max_bytes, char **out,+ size_t *out_len)+{+ return -EOPNOTSUPP;+}++int shirudo_upatch_decode_map_file_otel(const char *path,+ const struct shirudo_map_view *v,+ char **out, size_t *out_len)+{+ return -EOPNOTSUPP;+}++int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)+{+ struct shirudo_json_emit e = {};+ struct otel_response r = {};+ struct otel_state st = {};+ size_t off, n, cap;+ char *body, *line;++ /*+ * curl hands the header callback one line at a time, end included+ * and not NUL-terminated: each line in a buffer of its own size.+ */+ for (off = 0; off < size; off += n) {+ const __u8 *nl = memchr(data + off, '\n', size - off);++ n = nl ? (size_t)(nl - data) - off + 1 : size - off;+ line = xmalloc(n);+ if (!line)+ return 0;+ memcpy(line, data + off, n);+ response_header(line, 1, n, &r);+ xfree(line, n);+ }++ body = xmalloc(size + 1);+ if (!body)+ return 0;+ memcpy(body, data, size);+ body[size] = '\0';+ check_partial_success(SHIRUDO_OTEL_LOG, body, size);+ check_partial_success(SHIRUDO_OTEL_METRIC, body, size);++ cap = generic_log_capacity(size);+ e.buf = xmalloc(cap);+ if (e.buf) {+ e.cap = cap;+ shirudo_json_obj_open(&e);+ json_anyvalue_from_document(&e, body, size, false);+ xfree(e.buf, cap);+ }++ if (!memchr(data, '\0', size)) {+ add_configured_headers(&st, body);+ curl_slist_free_all(st.headers);+ }+ xfree(body, size + 1);+ return 0;+}