Skip to content

eBPF-based security platform

bpm: scrub copied curl headers before freeing

Tobias Klauserd58174977747
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 2906e41d..3d7bc676 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -575,6 +575,9 @@ can rewrite what it finds does so with ``-D patch``:
of such a struct.
The structs come from the headers under :src:`include/`,
:src:`util/` and :src:`tools/` and at the top of the tree.
+ * - ``curl_slist_free``
+ - A libcurl header list freed without first scrubbing its copied
+ header strings with ``curl_slist_free_sensitive()``.
* - ``secret_cmp``
- ``memcmp()`` of a secret or what derives from one rather than
``memneq_explicit()``.
diff --git a/Makefile.shirudo b/Makefile.shirudo
index f70709e3..5d2dfd88 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -243,6 +243,7 @@ UTIL_SRCS := \
util/bpm_tree.c \
util/bpm_tree_state.c \
util/bpm_oci.c \
+ util/curl.c \
util/bpm_package.c \
util/bpm_state.c \
util/kernel_build_id.c \
@@ -488,7 +489,7 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o
util/elf_target.o util/hex.o util/keys.o \
util/seal.o util/harden.o util/string.o \
util/crypto_openssl.o util/tls_openssl.o \
- util/bpm_oci.o util/bpm_package.o \
+ util/bpm_oci.o util/curl.o util/bpm_package.o \
util/upatch_rodata.o \
util/bpm_tree.o $(VERSION_OBJ) \
$(LIBBPF_A)
@@ -815,7 +816,7 @@ testing/helper/test-signed-load: testing/helper/test-signed-load.c
-o $@ $^ -lcrypto
testing/helper/test-otel-map: testing/helper/test-otel-map.c util/upatch_maps.o \
- util/upatch_maps_otel.o util/crypto_openssl.o \
+ util/upatch_maps_otel.o util/curl.o util/crypto_openssl.o \
util/tls_openssl.o util/serializer.o util/seal.o \
util/config.o util/image_config.o \
util/upatch_decode.o util/upatch_resolve.o \
@@ -952,9 +953,11 @@ $(UNIT_LIB): $(UTIL_OBJS) $(VERSION_OBJ)
build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \
$(UNIT_LIB) $(LIBBPF_A)
@printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS) -Itesting/unit -o $@ $< testing/unit/unit.c \
+ $(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Itesting/unit -o $@ $< testing/unit/unit.c \
$(UNIT_LIB) $(LIBBPF_A) $(DYNAMIC_LIBS)
+build/unit/curl: LDFLAGS += -Wl,--wrap=curl_slist_free_all
+
unit-tests: $(UNIT_TESTS)
# ----- sparse -----------------------------------------------------
@@ -1003,8 +1006,8 @@ FUZZ_BASE := util/json.c util/string.c util/hex.c util/malloc.c \
testing/fuzz/log.c
FUZZ_CONFIG := util/config.c util/image_config.c
FUZZ_SEAL := util/seal.c $(FUZZ_CONFIG)
-FUZZ_OCI := util/bpm_oci.c util/io.c $(FUZZ_SEAL) util/crypto_openssl.c \
- util/tls_openssl.c
+FUZZ_OCI := util/bpm_oci.c util/curl.c util/io.c $(FUZZ_SEAL) \
+ util/crypto_openssl.c util/tls_openssl.c
FUZZ_PACKAGE := util/bpm_package.c util/bpm_tree.c util/upatch.c \
util/keys.c $(FUZZ_OCI)
@@ -1068,7 +1071,8 @@ testing/fuzz/fuzz-decoder: testing/fuzz/fuzz-decoder.c $(FUZZ_BASE) \
testing/fuzz/fuzz-decoder: FUZZ_CFLAGS += $(LUAJIT_CFLAGS) -DHAVE_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/upatch_maps_otel.c util/curl.c \
+ util/crypto_openssl.c \
util/tls_openssl.c util/io.c util/version_id.c \
$(FUZZ_SEAL)
testing/fuzz/fuzz-beefile: testing/fuzz/fuzz-beefile.c $(FUZZ_BASE) \
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index fd53e4de..8d136b14 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -530,6 +530,18 @@ test_expect_success 'each scrub rule reports its samples and spares the scrubbed
[ "$(grep -c "^[a-z_]*: " "$W/err")" = 15 ]
'
+test_expect_success 'curl_slist_free reports lists freed without scrubbing' '
+ R5=$W/curl-slist &&
+ mkdir -p "$R5/testing/coccinelle" &&
+ git init -q "$R5" &&
+ cp "$ROOT/testing/coccinelle/curl_slist_free.cocci" \
+ "$R5/testing/coccinelle/" &&
+ printf "void headers_free(struct curl_slist *headers)\n{\n\tcurl_slist_free_all(headers);\n}\n" \
+ >"$R5/samples.c" &&
+ ! (cd "$R5" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&
+ [ "$(grep -c "^curl_slist_free: samples.c:" "$W/err")" = 1 ]
+'
+
test_expect_success 'the tree passes' '
(cd "$ROOT" && sh "$S") >"$W/out" 2>"$W/err" &&
grep -q "rules, no match" "$W/out"
diff --git a/testing/coccinelle/curl_slist_free.cocci b/testing/coccinelle/curl_slist_free.cocci
new file mode 100644
index 00000000..260464e3
--- /dev/null
+++ b/testing/coccinelle/curl_slist_free.cocci
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// Copyright (c) 2026 Authors of shirudo
+//
+// curl_slist_append() copies header contents into heap storage. Freeing a
+// list directly can leave authorization headers in freed allocator memory.
+
+virtual report
+
+@r@
+expression E;
+position p;
+@@
+curl_slist_free_all@p(E)
+
+@script:python depends on report@
+p << r.p;
+@@
+if p[0].file != "util/curl.c":
+ coccilib.report.print_report(p[0], "free curl header lists with curl_slist_free_sensitive()")
diff --git a/testing/fuzz/fuzz-otel.c b/testing/fuzz/fuzz-otel.c
index 6bc9e8c8..10860d35 100644
--- a/testing/fuzz/fuzz-otel.c
+++ b/testing/fuzz/fuzz-otel.c
@@ -156,7 +156,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
abort();
}
}
- curl_slist_free_all(st.headers);
+ curl_slist_free_sensitive(st.headers);
}
xfree(body, size + 1);
return 0;
diff --git a/testing/unit/curl.c b/testing/unit/curl.c
new file mode 100644
index 00000000..4a45bf80
--- /dev/null
+++ b/testing/unit/curl.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include "curl.h"
+#include "curl_test.h"
+#include "unit.h"
+
+static struct curl_slist *freed_headers;
+
+void __wrap_curl_slist_free_all(struct curl_slist *headers)
+{
+ freed_headers = headers;
+}
+
+static void sensitive_header_is_scrubbed_before_free(void)
+{
+ static const char header[] = "Authorization: Bearer secret";
+ static const __u8 zero[sizeof(header)];
+ struct curl_slist *headers;
+ char *data;
+
+ headers = curl_slist_append(NULL, header);
+ ASSERT_TRUE(headers);
+ data = headers->data;
+ curl_slist_free_sensitive(headers);
+ ASSERT_TRUE(freed_headers);
+ EXPECT_MEMEQ(data, zero, sizeof(header));
+ __real_curl_slist_free_all(freed_headers);
+ freed_headers = NULL;
+}
+
+static const struct unit_case cases[] = {
+ UNIT_CASE(sensitive_header_is_scrubbed_before_free),
+ {}
+};
+
+UNIT_SUITE(curl, cases);
diff --git a/testing/unit/curl_test.h b/testing/unit/curl_test.h
new file mode 100644
index 00000000..75442b45
--- /dev/null
+++ b/testing/unit/curl_test.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/* Copyright (c) 2026 Authors of shirudo */
+
+#ifndef SHIRUDO_TEST_CURL_H
+#define SHIRUDO_TEST_CURL_H
+
+#include <curl/curl.h>
+
+void __wrap_curl_slist_free_all(struct curl_slist *headers);
+void __real_curl_slist_free_all(struct curl_slist *headers);
+
+#endif /* SHIRUDO_TEST_CURL_H */
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index e4a1acec..e28421bb 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -25,6 +25,7 @@
#include "bpm_oci.h"
#include "crypto.h"
+#include "curl.h"
#include "hex.h"
#include "io.h"
#include "seal.h"
@@ -464,7 +465,7 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
}
curl = curl_easy_init();
if (!curl) {
- curl_slist_free_all(headers);
+ curl_slist_free_sensitive(headers);
return -ENOMEM;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -499,8 +500,8 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
if (tls_curl_policy(curl, &c->tls_ca)) {
pr_err("bpm OCI request %s: cannot apply TLS policy", url);
- curl_slist_free_all(headers);
curl_easy_cleanup(curl);
+ curl_slist_free_sensitive(headers);
return -EHOSTUNREACH;
}
if (body) {
@@ -527,16 +528,16 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
}
if (file && cc == CURLE_FILESIZE_EXCEEDED)
file->overflow = true;
- curl_slist_free_all(headers);
curl_easy_cleanup(curl);
+ curl_slist_free_sensitive(headers);
tls_ca_cleanup(&c->tls_ca);
return status < 0 ? -EHOSTUNREACH : 0;
too_long:
- curl_slist_free_all(headers);
+ curl_slist_free_sensitive(headers);
return -E2BIG;
no_memory:
- curl_slist_free_all(headers);
+ curl_slist_free_sensitive(headers);
return -ENOMEM;
}
@@ -718,8 +719,8 @@ out:
memzero_explicit(auth_header, sizeof(auth_header));
curl_free(esc_service);
curl_free(esc_scope);
- curl_slist_free_all(headers);
curl_easy_cleanup(curl);
+ curl_slist_free_sensitive(headers);
tls_ca_cleanup(&c->tls_ca);
bpm_buffer_free(&r.body);
return ret;
diff --git a/util/curl.c b/util/curl.c
new file mode 100644
index 00000000..dbd1a740
--- /dev/null
+++ b/util/curl.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/* Copyright (c) 2026 Authors of shirudo */
+
+#include <string.h>
+
+#include <shirudo/string.h>
+
+#include "curl.h"
+
+void curl_slist_free_sensitive(struct curl_slist *headers)
+{
+ struct curl_slist *entry, *next;
+
+ for (entry = headers; entry; entry = next) {
+ next = entry->next;
+ if (entry->data)
+ memzero_explicit(entry->data, strlen(entry->data) + 1);
+ }
+ curl_slist_free_all(headers);
+}
diff --git a/util/curl.h b/util/curl.h
new file mode 100644
index 00000000..d8645147
--- /dev/null
+++ b/util/curl.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * libcurl memory helpers.
+ */
+
+#ifndef SHIRUDO_CURL_H
+#define SHIRUDO_CURL_H
+
+#include <curl/curl.h>
+
+/* Scrub copied headers before libcurl frees their list. */
+void curl_slist_free_sensitive(struct curl_slist *headers);
+
+#endif /* SHIRUDO_CURL_H */
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 69e821e5..bbe252db 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -33,11 +33,12 @@
#include <shirudo/string.h>
#include <shirudo/upatch.h>
+#include "config.h"
#include "crypto.h"
+#include "curl.h"
#include "hex.h"
#include "json.h"
#include "log.h"
-#include "config.h"
#include "malloc.h"
#include "paths.h"
#include "tenant_id.h"
@@ -1484,7 +1485,7 @@ static int otel_open_transport(enum otel_signal signal,
err_global:
curl_global_cleanup();
err:
- curl_slist_free_all(st->headers);
+ curl_slist_free_sensitive(st->headers);
xfree(st, sizeof(*st));
return ret;
}
@@ -1543,7 +1544,7 @@ static int otel_close(void *state)
if (!st)
return 0;
- curl_slist_free_all(st->headers);
+ curl_slist_free_sensitive(st->headers);
xfree(st, sizeof(*st));
curl_global_cleanup();
return 0;
@@ -2163,7 +2164,7 @@ static noinline_for_stack int s3_put(struct otel_s3_state *st,
return -EINVAL;
ret = s3_headers(st, v, snapshot, &headers);
if (ret) {
- curl_slist_free_all(headers);
+ curl_slist_free_sensitive(headers);
return ret;
}
if (strsprintf(sigv4, sizeof(sigv4), "aws:amz:%s:s3", st->region) < 0) {
@@ -2243,7 +2244,7 @@ static noinline_for_stack int s3_put(struct otel_s3_state *st,
response.retry_after_valid);
}
out:
- curl_slist_free_all(headers);
+ curl_slist_free_sensitive(headers);
return ret;
}