eBPF-based security platform
string: scrub secrets with memzero_explicit()
Daniel Borkmann3dae9144a69c
diff --git a/AGENTS.md b/AGENTS.mdindex df6fa0ce..274668c7 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -327,6 +327,12 @@ one usually still compiles and passes tests.`xstrdup` is strlen-based, so never apply it to user, file or networkinput; use `xstrndup(s, bound)`. `xasprintf` formats into memory ofthe size needed, freed with `xstrfree`.+- Scrub a secret, a key, an auth value, a seed, a KDF output, a+ credential or a buffer that held one, with `memzero_explicit()` from+ `<shirudo/string.h>` on every path out of its scope, the error paths+ included. A `memset()` of memory nothing reads afterwards is a dead+ store the compiler drops; `explicit_bzero()` is poisoned in favor of+ the kernel's name.- `util/json.{c,h}` is the only JSON code. Extend its schemadefinitions for new fields or documents; never open-code JSON byteselsewhere.diff --git a/builtin-attest.c b/builtin-attest.cindex d9bedd49..8a989949 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -763,7 +763,7 @@ static int cmd_attest_challenge(int argc, const char **argv)printf("challenge: %s\ncredential: %s\n", out_path, hex);ret = 0;out:- explicit_bzero(credential, sizeof(credential));+ memzero_explicit(credential, sizeof(credential));xfree(doc, cap);xfree(evidence, evidence ? evidence_len + 1 : 0);xfree(report, ATTEST_REPORT_MAX);diff --git a/builtin-init.c b/builtin-init.cindex f2c09224..4cdd654c 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -1151,14 +1151,14 @@ static int chain_extend_init(struct measured_upatch *set, size_t n)ret = config_load(&cfg);if (ret) {- explicit_bzero(&cfg, sizeof(cfg));+ memzero_explicit(&cfg, sizeof(cfg));pr_err("chain: configuration unreadable: %s",strerror(-ret));return ret;}max_bytes = cfg.eventlog_max_bytes;full = eventlog_would_exceed(max_bytes);- explicit_bzero(&cfg, sizeof(cfg));+ memzero_explicit(&cfg, sizeof(cfg));if (full) {pr_err("eventlog %s would exceed eventlog.max_bytes=%llu; ""refusing chain extend (eventlog_full)",diff --git a/include/shirudo/compiler.h b/include/shirudo/compiler.hindex 46bdfcc8..3bc57fcc 100644--- a/include/shirudo/compiler.h+++ b/include/shirudo/compiler.h@@ -12,6 +12,14 @@#define __must_check __attribute__((warn_unused_result))#define __noreturn __attribute__((noreturn))+/*+ * An empty asm that may read memory through ptr, so that a store to that+ * memory before it is not dead. The pointer has to be an operand: the+ * memory clobber alone does not reach an object whose address never+ * escaped, and clang drops a store to a local then.+ */+#define barrier_data(ptr) __asm__ __volatile__("" : : "r"(ptr) : "memory")+/* sparse's cast between a restricted type and its base; nothing otherwise. */#ifdef __CHECKER__#define __force __attribute__((force))diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 402221ec..7a36e8b2 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -3,7 +3,7 @@* Copyright (c) 2026 Authors of shirudo** Bounded number parsing, string copies and formatting, DNS names, from- * util/string.c.+ * util/string.c, and the scrub for secrets.*/#ifndef SHIRUDO_STRING_H@@ -59,10 +59,22 @@ int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)*/bool dns_name_valid(const char *name);+/*+ * Zero the count bytes at s even when nothing reads them afterwards, as+ * when a secret is scrubbed before it goes out of scope or is freed,+ * where the compiler may drop a plain memset() as a dead store.+ */+static inline void memzero_explicit(void *s, size_t count)+{+ memset(s, 0, count);+ barrier_data(s);+}+/** What the functions above replace, refused once the headers included* above have declared it, so that a later include of those still works;- * util/string.c opts out with STRING_INTERNAL.+ * util/string.c and the xasprintf() of util/malloc.c opt out with+ * STRING_INTERNAL.*/#ifndef STRING_INTERNAL# pragma GCC poison strtol strtoll strtoul strtoull strtoimax strtoumax@@ -72,6 +84,7 @@ bool dns_name_valid(const char *name);# undef sprintf# undef snprintf# pragma GCC poison sprintf vsprintf snprintf vsnprintf+# pragma GCC poison explicit_bzero#endif#endif /* SHIRUDO_STRING_H */diff --git a/testing/unit/string.c b/testing/unit/string.cindex 8e5bae05..95f79997 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -281,6 +281,17 @@ static void dns_name_is_ldh_labels_and_no_address(void)EXPECT_FALSE(dns_name_valid(name));}+static void memzero_explicit_clears_its_span_only(void)+{+ __u8 want[16], buf[16];++ memset(buf, 0xa5, sizeof(buf));+ memset(want, 0xa5, sizeof(want));+ memset(want + 4, 0, 8);+ memzero_explicit(buf + 4, 8);+ EXPECT_MEMEQ(buf, want, sizeof(buf));+}+static const struct unit_case cases[] = {UNIT_CASE(digits_counts_leading_digits),UNIT_CASE(decimal_fills_its_span),@@ -297,6 +308,7 @@ static const struct unit_case cases[] = {UNIT_CASE(strsprintf_leaves_nothing_when_cut),UNIT_CASE(xasprintf_allocates_the_size_needed),UNIT_CASE(dns_name_is_ldh_labels_and_no_address),+ UNIT_CASE(memzero_explicit_clears_its_span_only),{}};diff --git a/util/attest.c b/util/attest.cindex 62a16243..0a0a9d02 100644--- a/util/attest.c+++ b/util/attest.c@@ -397,11 +397,11 @@ out:xfree(ek_b64, ek_b64_cap);xfree(fw, fw_cap);xfree(sd, sd_cap);- explicit_bzero(cred, sizeof(cred));- explicit_bzero(&ak, sizeof(ak));- explicit_bzero(&ek, sizeof(ek));- explicit_bzero(&ak_copy, sizeof(ak_copy));- explicit_bzero(&ek_copy, sizeof(ek_copy));+ memzero_explicit(cred, sizeof(cred));+ memzero_explicit(&ak, sizeof(ak));+ memzero_explicit(&ek, sizeof(ek));+ memzero_explicit(&ak_copy, sizeof(ak_copy));+ memzero_explicit(&ek_copy, sizeof(ek_copy));if (ret && *out) {xfree(*out, *cap);*out = NULL;@@ -1517,13 +1517,13 @@ int attest_challenge_make(const char *evidence, size_t len,*out_len = e.len;ret = 0;out:- explicit_bzero(seed, sizeof(seed));- explicit_bzero(z, sizeof(z));+ memzero_explicit(seed, sizeof(seed));+ memzero_explicit(z, sizeof(z));cert_free(&cert);xfree(der, der ? EK_CERT_MAX : 0);xfree(cert_b64, EK_CERT_B64_MAX);if (ret) {- explicit_bzero(credential, TPM_CREDENTIAL_LEN);+ memzero_explicit(credential, TPM_CREDENTIAL_LEN);if (*out) {xfree(*out, *cap);*out = NULL;diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex e0136dc6..c9588e4e 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -224,7 +224,7 @@ static int decode_basic(const char *in, char *user, size_t usz,return -E2BIG;out[n] = '\0';if (memchr(out, '\0', n)) {- explicit_bzero(out, sizeof(out));+ memzero_explicit(out, sizeof(out));return -EINVAL;}colon = memchr(out, ':', n);diff --git a/util/chain.c b/util/chain.cindex 4d6f4862..4fc3d457 100644--- a/util/chain.c+++ b/util/chain.c@@ -243,7 +243,7 @@ static int register_define(struct tpm *tpm, bool defined,ret = tpm_nv_define(tpm, NV_INDEX, policy,cfg.tpm_owner_auth, cfg.tpm_owner_auth_len);out:- explicit_bzero(&cfg, sizeof(cfg));+ memzero_explicit(&cfg, sizeof(cfg));return ret;}diff --git a/util/config.c b/util/config.cindex 71e8ca41..b8f4a5c9 100644--- a/util/config.c+++ b/util/config.c@@ -127,14 +127,14 @@ static void set_owner_auth(struct config *cfg, const char *val)if (hi < 0 || lo < 0) {pr_warn("config: tpm.owner_auth: bad hex ""(ignored)");- explicit_bzero(auth, sizeof(auth));+ memzero_explicit(auth, sizeof(auth));return;}auth[i] = (char)((hi << 4) | lo);}memcpy(cfg->tpm_owner_auth, auth, hl / 2);cfg->tpm_owner_auth_len = hl / 2;- explicit_bzero(auth, sizeof(auth));+ memzero_explicit(auth, sizeof(auth));return;}@@ -277,14 +277,14 @@ static int config_parse(struct config *cfg, FILE *f,/* The TPM owner's authorization is for no other user's eyes. */if (cfg->tpm_owner_auth_len &&check_trusted(fileno(f), path, true)) {- explicit_bzero(cfg->tpm_owner_auth,- sizeof(cfg->tpm_owner_auth));+ memzero_explicit(cfg->tpm_owner_auth,+ sizeof(cfg->tpm_owner_auth));cfg->tpm_owner_auth_len = 0;ret = -EPERM;}if (line) {/* The owner's authorization may have been read into it. */- explicit_bzero(line, capacity);+ memzero_explicit(line, capacity);libc_free(line);}if (fclose(f) && !ret)diff --git a/util/image_config.c b/util/image_config.cindex 48999687..c578841f 100644--- a/util/image_config.c+++ b/util/image_config.c@@ -225,7 +225,7 @@ int image_config_validate(int root_fd)if (!ret && (!candidate.map_export_valid ||!candidate.tls_classical_hosts_valid))ret = -EINVAL;- explicit_bzero(&candidate, sizeof(candidate));+ memzero_explicit(&candidate, sizeof(candidate));}return ret;}@@ -275,5 +275,5 @@ void image_config_clear(void)close(config_fd);config_fd = -1;active = false;- explicit_bzero(&effective, sizeof(effective));+ memzero_explicit(&effective, sizeof(effective));}diff --git a/util/image_runtime.c b/util/image_runtime.cindex 5c1e23b4..70b5d3b9 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -1054,7 +1054,7 @@ void image_leave(void)if (image.certificate_fd >= 0)close(image.certificate_fd);image.certificate_fd = -1;- explicit_bzero(&image.bootstrap_config, sizeof(image.bootstrap_config));+ memzero_explicit(&image.bootstrap_config, sizeof(image.bootstrap_config));}int image_update(const char *prefix)diff --git a/util/malloc.c b/util/malloc.cindex 1b83019c..bcf65deb 100644--- a/util/malloc.c+++ b/util/malloc.c@@ -13,6 +13,10 @@#include <shirudo/misc.h>+/* xasprintf() formats with vsnprintf(), which <shirudo/string.h> poisons. */+#define STRING_INTERNAL+#include <shirudo/string.h>+/* Implements the wrappers; opt out of the poison pragma in malloc.h. */#define MALLOC_INTERNAL#include "malloc.h"@@ -111,36 +115,11 @@ char *xasprintf(const char *fmt, ...)return p;}-#if defined(__SANITIZE_ADDRESS__)-#define ASAN 1-#elif defined(__has_feature)-#if __has_feature(address_sanitizer)-#define ASAN 1-#endif-#endif--/*- * ASan does not look inside explicit_bzero(), so a size that runs past- * the allocation would go unreported in the sanitized builds and the- * fuzzers: there the scrub goes through memset(), which it checks, by a- * pointer the compiler cannot see through to drop it before free().- */-static void scrub(void *ptr, size_t size)-{-#ifdef ASAN- static void *(*volatile checked)(void *, int, size_t) = memset;-- checked(ptr, 0, size);-#else- explicit_bzero(ptr, size);-#endif-}-void xfree(void *ptr, size_t size){if (!ptr)return;- scrub(ptr, size);+ memzero_explicit(ptr, size);free(ptr);}@@ -148,7 +127,7 @@ void xstrfree(char *s){if (!s)return;- scrub(s, strlen(s) + 1);+ memzero_explicit(s, strlen(s) + 1);free(s);}diff --git a/util/nv_writer.c b/util/nv_writer.cindex c5c9d1ea..78bcc833 100644--- a/util/nv_writer.c+++ b/util/nv_writer.c@@ -5,6 +5,8 @@#include <string.h>#include <sys/random.h>+#include <shirudo/string.h>+#include "identity.h"#include "log.h"#include "nv_writer.h"@@ -36,7 +38,7 @@ int nv_writer_create(__u8 seed[TPM_DIGEST_LEN],return 0;fail:pr_err("nv writer: TPM key creation failed: %s", strerror(-err));- explicit_bzero(seed, TPM_DIGEST_LEN);+ memzero_explicit(seed, TPM_DIGEST_LEN);memset(public, 0, TPM_NV_RSA_BYTES);return err;}diff --git a/util/preflight.c b/util/preflight.cindex c0a5426a..4f3790c4 100644--- a/util/preflight.c+++ b/util/preflight.c@@ -356,7 +356,7 @@ static void check_tpm(struct preflight_result *r)strscpy(c->detail, "TCTI override",sizeof(c->detail));}- explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */+ memzero_explicit(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */if (override)return;}diff --git a/util/tpm.c b/util/tpm.cindex 7a1ec4e3..5467acfe 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -45,11 +45,11 @@ int tpm_tcti(char *buf, size_t size)if (!tcti || !*tcti)tcti = "device:/dev/tpmrm0";if (image_config_active() && strncmp(tcti, "device:", 7)) {- explicit_bzero(&cfg, sizeof(cfg));+ memzero_explicit(&cfg, sizeof(cfg));return -EACCES;}n = strscpy(buf, tcti, size);- explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */+ memzero_explicit(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */return n < 0 ? (int)n : 0;}@@ -311,7 +311,7 @@ static int owner_auth_set(struct tpm *t, const void *auth, size_t len)owner.size = (__u16)len;}ret = Esys_TR_SetAuth(t->esys, ESYS_TR_RH_OWNER, &owner);- explicit_bzero(owner.buffer, sizeof(owner.buffer));+ memzero_explicit(owner.buffer, sizeof(owner.buffer));if (ret != TSS2_RC_SUCCESS) {pr_err("tpm: TR_SetAuth(owner): rc=0x%08x", (__u32)ret);return -EIO;@@ -517,7 +517,7 @@ static int tpm_writer_primary(struct tpm *t,err = config_load(&cfg);if (!err)err = owner_auth_set(t, cfg.tpm_owner_auth, cfg.tpm_owner_auth_len);- explicit_bzero(&cfg, sizeof(cfg));+ memzero_explicit(&cfg, sizeof(cfg));if (err)return err;ret = Esys_CreatePrimary(t->esys, ESYS_TR_RH_OWNER,@@ -689,7 +689,7 @@ static int policy_writer_signed(struct tpm *t, ESYS_TR session,}out:session_flush(t, key);- explicit_bzero(signature, sizeof(signature));+ memzero_explicit(signature, sizeof(signature));return err;}@@ -1038,7 +1038,7 @@ void tpm_key_flush(struct tpm *t, struct tpm_key *key){if (key->handle != ESYS_TR_NONE && key->handle != 0)Esys_FlushContext(t->esys, key->handle);- explicit_bzero(key, sizeof(*key));+ memzero_explicit(key, sizeof(*key));key->handle = ESYS_TR_NONE;}diff --git a/util/tpm_wire.c b/util/tpm_wire.cindex 35769c59..161a155c 100644--- a/util/tpm_wire.c+++ b/util/tpm_wire.c@@ -9,6 +9,7 @@#include <string.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "crypto.h"#include "tpm_wire.h"@@ -484,7 +485,7 @@ int tpm_kdfa(const __u8 *key, size_t key_len, const char *label,memcpy(out + done, block, n);done += n;}- explicit_bzero(block, sizeof(block));+ memzero_explicit(block, sizeof(block));return 0;}@@ -512,7 +513,7 @@ void tpm_kdfe(const __u8 z[32], const char *label,memcpy(buf + off, party_v, v_len);off += v_len;sha256_buffer(buf, off, out);- explicit_bzero(buf, sizeof(buf));+ memzero_explicit(buf, sizeof(buf));}int tpm_credential_protect(const __u8 seed[32],@@ -544,9 +545,9 @@ int tpm_credential_protect(const __u8 seed[32],memcpy(out + 34, enc, sizeof(enc));ret = 0;out:- explicit_bzero(plain, sizeof(plain));- explicit_bzero(sym_key, sizeof(sym_key));- explicit_bzero(hmac_key, sizeof(hmac_key));+ memzero_explicit(plain, sizeof(plain));+ memzero_explicit(sym_key, sizeof(sym_key));+ memzero_explicit(hmac_key, sizeof(hmac_key));return ret;}diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.cindex 96e580f6..2a6786b1 100644--- a/util/upatch_maps_otel.c+++ b/util/upatch_maps_otel.c@@ -1178,12 +1178,12 @@ static int add_configured_headers(struct otel_state *st, const char *input)}ret = strsprintf(line, n, "%s: %s", name, value) < 0 ? -E2BIG :append_header(st, line);- explicit_bzero(line, n);+ memzero_explicit(line, n);xfree(line, n);if (ret)break;}- explicit_bzero(copy, total);+ memzero_explicit(copy, total);xfree(copy, total + 1);return ret;}@@ -2445,7 +2445,7 @@ static void otel_s3_state_free(struct otel_s3_state *st)if (st->otel)otel_close(st->otel);if (st->credentials) {- explicit_bzero(st->credentials, st->credentials_len);+ memzero_explicit(st->credentials, st->credentials_len);xfree(st->credentials, st->credentials_len);}xfree(st, sizeof(*st));