Skip to content

eBPF-based security platform

testing: fuzz the registry client's parsers

Daniel Borkmann9de07277ace3
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 2d86a771..3ab4ef25 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -697,10 +697,21 @@ testing/fuzz/fuzz-upatch: testing/fuzz/fuzz-upatch.c $(FUZZ_BASE) \
util/upatch.c
testing/fuzz/fuzz-bpm-tar: testing/fuzz/fuzz-bpm-tar.c $(FUZZ_BASE) \
$(FUZZ_PACKAGE)
+testing/fuzz/fuzz-oci-http: testing/fuzz/fuzz-oci-http.c $(FUZZ_BASE) \
+ $(FUZZ_OCI)
+testing/fuzz/fuzz-oci-ref: testing/fuzz/fuzz-oci-ref.c $(FUZZ_BASE) \
+ $(FUZZ_OCI)
+testing/fuzz/fuzz-registry-auth: testing/fuzz/fuzz-registry-auth.c \
+ $(FUZZ_BASE) $(FUZZ_OCI)
+
+# 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
$(FUZZ_TARGETS): testing/fuzz/fuzz.h
@printf ' FUZZ $@\n'
- $(Q)$(FUZZ_CC) $(FUZZ_CFLAGS) -o $@ $(filter %.c,$^) $(FUZZ_LIBS)
+ $(Q)$(FUZZ_CC) $(FUZZ_CFLAGS) -o $@ \
+ $(filter-out $(FUZZ_INCLUDED),$(filter %.c,$^)) $(FUZZ_LIBS)
fuzz-targets: $(FUZZ_TARGETS)
diff --git a/testing/fuzz/corpus/oci-http/access-token b/testing/fuzz/corpus/oci-http/access-token
new file mode 100644
index 00000000..9bdc3bb2
--- /dev/null
+++ b/testing/fuzz/corpus/oci-http/access-token
@@ -0,0 +1 @@
+{"access_token": "abc.def.ghi", "token_type": "Bearer"}
\ No newline at end of file
diff --git a/testing/fuzz/corpus/oci-http/challenge b/testing/fuzz/corpus/oci-http/challenge
new file mode 100644
index 00000000..00815e25
--- /dev/null
+++ b/testing/fuzz/corpus/oci-http/challenge
@@ -0,0 +1,6 @@
+HTTP/1.1 401 Unauthorized
+Content-Type: application/json; charset=utf-8
+WWW-Authenticate: Bearer realm="https://auth.example/token",service="registry.example:5000",scope="repository:shirudo/kernel-obs:pull"
+Content-Length: 44
+
+{"token":"eyJhbGciOiJFUzI1NiJ9","expires_in":300}
diff --git a/testing/fuzz/corpus/oci-http/upload b/testing/fuzz/corpus/oci-http/upload
new file mode 100644
index 00000000..cbdab490
--- /dev/null
+++ b/testing/fuzz/corpus/oci-http/upload
@@ -0,0 +1,5 @@
+HTTP/1.1 202 Accepted
+Location: /v2/shirudo/kernel-obs/blobs/uploads/4b8a?_state=Zm9v
+Docker-Upload-UUID: 4b8a
+Content-Length: 0
+
diff --git a/testing/fuzz/corpus/oci-ref/pull b/testing/fuzz/corpus/oci-ref/pull
new file mode 100644
index 00000000..39f69ed9
--- /dev/null
+++ b/testing/fuzz/corpus/oci-ref/pull
@@ -0,0 +1 @@
+oci://registry.example:5000/shirudo/kernel-obs:1.2.0-rc.1
\ No newline at end of file
diff --git a/testing/fuzz/corpus/oci-ref/tree b/testing/fuzz/corpus/oci-ref/tree
new file mode 100644
index 00000000..59fc26a5
--- /dev/null
+++ b/testing/fuzz/corpus/oci-ref/tree
@@ -0,0 +1 @@
+oci://registry.example:5000/shirudo/trees/prod
\ No newline at end of file
diff --git a/testing/fuzz/corpus/registry-auth/docker-config b/testing/fuzz/corpus/registry-auth/docker-config
new file mode 100644
index 00000000..ddb84d7d
--- /dev/null
+++ b/testing/fuzz/corpus/registry-auth/docker-config
@@ -0,0 +1,7 @@
+{
+ "auths": {
+ "registry.example:5000": {
+ "auth": "c2hpcnVkbzpzM2NyM3Q="
+ }
+ }
+}
diff --git a/testing/fuzz/fuzz-oci-http.c b/testing/fuzz/fuzz-oci-http.c
new file mode 100644
index 00000000..0d38ed3a
--- /dev/null
+++ b/testing/fuzz/fuzz-oci-http.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * What a registry and its token server answer: the header lines, the
+ * challenge and upload location taken from them, and the token.
+ */
+
+#include "fuzz.h"
+#include "bpm_oci.c"
+
+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
+{
+ char realm[PATH_MAX] = {}, service[256] = {}, scope[512] = {};
+ struct bpm_http_response r = {};
+ struct bpm_oci_client c = {};
+ char upload[PATH_MAX], *body;
+ size_t off, n;
+
+ /* curl hands the header callback one line at a time, end included. */
+ 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;
+ bpm_header((void *)(data + off), 1, n, &r);
+ }
+ parse_challenge(r.www_auth, realm, sizeof(realm), service,
+ sizeof(service), scope, sizeof(scope));
+ bpm_copy_string(c.base_url, sizeof(c.base_url),
+ "https://registry.example:5000");
+ upload_location(&c, r.location, upload, sizeof(upload));
+
+ body = xmalloc(size + 1);
+ if (!body)
+ return 0;
+ memcpy(body, data, size);
+ body[size] = '\0';
+ token_from_body(body, c.token, sizeof(c.token));
+ xfree(body, size + 1);
+ return 0;
+}
diff --git a/testing/fuzz/fuzz-oci-ref.c b/testing/fuzz/fuzz-oci-ref.c
new file mode 100644
index 00000000..7b0a11a4
--- /dev/null
+++ b/testing/fuzz/fuzz-oci-ref.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * An oci:// reference as an endpoint, a pull reference, a push repository
+ * and a tree repository, and the registry URL built from what they accept.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "bpm_oci.h"
+#include "fuzz.h"
+#include "malloc.h"
+
+int LLVMFuzzerInitialize(int *argc, char ***argv)
+{
+ /* Credentials from here, not from the auth file of whoever runs it. */
+ setenv("BPM_OCI_USERNAME", "fuzz", 1);
+ setenv("BPM_OCI_PASSWORD", "fuzz", 1);
+ unsetenv("BPM_OCI_SCHEME");
+ unsetenv("BPM_OCI_CA_FILE");
+ return 0;
+}
+
+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
+{
+ char repository[BPM_MAX_ENDPOINT], tag[BPM_MAX_NAME], url[PATH_MAX];
+ struct bpm_oci_client c = {};
+ char *ref;
+
+ ref = xmalloc(size + 1);
+ if (!ref)
+ return 0;
+ memcpy(ref, data, size);
+ ref[size] = '\0';
+ bpm_oci_parse_endpoint(ref, &c);
+ if (!bpm_oci_parse_pull_ref(ref, &c, repository, sizeof(repository),
+ tag, sizeof(tag)))
+ bpm_oci_url(&c, repository, "manifests", tag, url, sizeof(url));
+ bpm_oci_parse_push_repo(ref, &c, repository, sizeof(repository));
+ if (!bpm_oci_parse_tree_repo(ref, &c, repository, sizeof(repository)))
+ bpm_oci_url(&c, repository, "blobs", ref, url, sizeof(url));
+ xfree(ref, size + 1);
+ return 0;
+}
diff --git a/testing/fuzz/fuzz-registry-auth.c b/testing/fuzz/fuzz-registry-auth.c
new file mode 100644
index 00000000..5312d940
--- /dev/null
+++ b/testing/fuzz/fuzz-registry-auth.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The registry credentials in a container auth file, REGISTRY_AUTH_FILE.
+ */
+
+#include <stdlib.h>
+
+#include "bpm_oci.h"
+#include "fuzz.h"
+
+int LLVMFuzzerInitialize(int *argc, char ***argv)
+{
+ unsetenv("BPM_OCI_USERNAME");
+ unsetenv("BPM_OCI_SCHEME");
+ unsetenv("BPM_OCI_CA_FILE");
+ return 0;
+}
+
+int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
+{
+ struct bpm_oci_client c = {};
+ char path[64];
+ int fd;
+
+ fd = fuzz_file(data, size, path, sizeof(path));
+ if (fd < 0)
+ return 0;
+ setenv("REGISTRY_AUTH_FILE", path, 1);
+ bpm_oci_parse_endpoint("oci://registry.example:5000", &c);
+ close(fd);
+ return 0;
+}
diff --git a/testing/fuzz/fuzz.h b/testing/fuzz/fuzz.h
index 313bfc35..3626536a 100644
--- a/testing/fuzz/fuzz.h
+++ b/testing/fuzz/fuzz.h
@@ -15,6 +15,7 @@
#include <shirudo/misc.h>
+int LLVMFuzzerInitialize(int *argc, char ***argv);
int LLVMFuzzerTestOneInput(const __u8 *data, size_t size);
/*