eBPF-based security platform
string: compare secrets with memneq_explicit()
Daniel Borkmannc0e06caa65e3
diff --git a/AGENTS.md b/AGENTS.mdindex 274668c7..03a4c431 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -332,7 +332,9 @@ one usually still compiles and passes tests.`<shirudo/string.h>` on every path out of its scope, the error pathsincluded. A `memset()` of memory nothing reads afterwards is a deadstore the compiler drops; `explicit_bzero()` is poisoned in favor of- the kernel's name.+ the kernel's name. Compare such a value with `memneq_explicit()`+ from the same header, which reads every byte whatever the bytes+ before it held; `memcmp()` returns at the first difference.- `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-bpm.c b/builtin-bpm.cindex 82901953..5f0b05d1 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1913,10 +1913,10 @@ static int tree_recover_pending(const struct tree_publication *pub,ret = tree_event_replay(replay);if (!ret) {- if (ct_eq(replay, next, sizeof(replay))) {+ if (!memneq_explicit(replay, next, sizeof(replay))) {applied = true;event_present = true;- } else if (!ct_eq(replay, previous, sizeof(replay))) {+ } else if (memneq_explicit(replay, previous, sizeof(replay))) {return -EKEYREJECTED;}} else if (ret != -ENOENT) {diff --git a/builtin-status.c b/builtin-status.cindex a78fc4a8..c0f4fc19 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -1051,8 +1051,8 @@ int cmd_status(int argc, const char **argv)match = cst.anchored && !cst.rebound &&eventlog_replay(replay) == 0 &&- ct_eq(cst.value, replay,- sizeof(replay));+ !memneq_explicit(cst.value, replay,+ sizeof(replay));}}diff --git a/builtin-upatch.c b/builtin-upatch.cindex 21bebaf9..3241355d 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -1784,8 +1784,8 @@ static int __upatch_apply(const char *path, bool verify_only,sha256_2(insns, u.hdr.loader_insns_len,data, u.hdr.data_len, content_digest);- if (!ct_eq(content_digest, u.hdr.payload_sha256,- sizeof(content_digest))) {+ if (memneq_explicit(content_digest, u.hdr.payload_sha256,+ sizeof(content_digest))) {pr_err("upatch: payload_sha256 does not match signed ""content (insns || data); refusing");rep_fail(rep, "parse", "parser_rejected", 0,@@ -2115,7 +2115,7 @@ static int upatch_preflight(const char *expect_chain,pr_err("upatch load: chain read failed: %s", strerror(-ret));return -1;}- if (!ct_eq(want, have, sizeof(want))) {+ if (memneq_explicit(want, have, sizeof(want))) {hex_encode(have, sizeof(have), have_hex);pr_err("upatch load: pre-flight refused (category: tampered, ""message: chain_drift) -- live chain %s != expected; "@@ -2881,7 +2881,7 @@ static int cmd_upatch_unload(int argc, const char **argv)xrc = get_pin_payload(name, live);if (xrc == 0) {- if (!ct_eq(live, payload, sizeof(payload))) {+ if (memneq_explicit(live, payload, sizeof(payload))) {pr_err("upatch unload: payload_mismatch -- the ""upatch loaded under '%s' is not what the ""directive expects; refusing", name);diff --git a/include/shirudo/compiler.h b/include/shirudo/compiler.hindex 3bc57fcc..8ac81678 100644--- a/include/shirudo/compiler.h+++ b/include/shirudo/compiler.h@@ -20,6 +20,13 @@*/#define barrier_data(ptr) __asm__ __volatile__("" : : "r"(ptr) : "memory")+/*+ * var passes through an empty asm and comes back changed as far as the+ * compiler knows, so that it cannot reason from how var was computed,+ * such as by leaving a loop once var can no longer change.+ */+#define OPTIMIZER_HIDE_VAR(var) __asm__ ("" : "+r"(var))+/* 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 7a36e8b2..20a342e8 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, and the scrub for secrets.+ * util/string.c, and the scrub and compare for secrets.*/#ifndef SHIRUDO_STRING_H@@ -70,6 +70,24 @@ static inline void memzero_explicit(void *s, size_t count)barrier_data(s);}+/*+ * 0 when the size bytes at a and b are equal and 1 when they differ, in+ * a time that depends on size alone, for comparing a secret or a value+ * derived from one, where memcmp() returns at the first difference.+ */+static inline int memneq_explicit(const void *a, const void *b, size_t size)+{+ const __u8 *pa = a, *pb = b;+ __u8 neq = 0;+ size_t i;++ for (i = 0; i < size; i++) {+ neq |= pa[i] ^ pb[i];+ OPTIMIZER_HIDE_VAR(neq);+ }+ return neq != 0;+}+/** What the functions above replace, refused once the headers included* above have declared it, so that a later include of those still works;diff --git a/testing/fuzz/chain.c b/testing/fuzz/chain.cindex 47ee4648..ca119bfb 100644--- a/testing/fuzz/chain.c+++ b/testing/fuzz/chain.c@@ -5,8 +5,6 @@* The digests of util/chain.c for the fuzz targets, without its TPM.*/-#include <string.h>-#include "chain.h"#include "crypto.h"@@ -15,8 +13,3 @@ int sha256_calc(const void *data, size_t len,{return sha256_buffer(data, len, out);}--int ct_eq(const void *a, const void *b, size_t n)-{- return !memcmp(a, b, n);-}diff --git a/testing/unit/chain.c b/testing/unit/chain.cindex 087b6c88..0b84f5ca 100644--- a/testing/unit/chain.c+++ b/testing/unit/chain.c@@ -17,18 +17,8 @@ static void sha256_matches_fips_180_vector(void)EXPECT_STREQ(hex, want);}-static void ct_eq_tells_one_bit_apart(void)-{- static const __u8 a[] = { 0x00, 0xff }, b[] = { 0x00, 0xff };- static const __u8 c[] = { 0x01, 0xff };-- EXPECT_EQ(ct_eq(a, b, sizeof(a)), 1);- EXPECT_EQ(ct_eq(a, c, sizeof(a)), 0);-}-static const struct unit_case cases[] = {UNIT_CASE(sha256_matches_fips_180_vector),- UNIT_CASE(ct_eq_tells_one_bit_apart),{}};diff --git a/testing/unit/string.c b/testing/unit/string.cindex 95f79997..48101108 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -292,6 +292,27 @@ static void memzero_explicit_clears_its_span_only(void)EXPECT_MEMEQ(buf, want, sizeof(buf));}+static void memneq_explicit_tells_every_bit_apart(void)+{+ __u8 a[33], b[33];+ size_t i;+ int bit;++ for (i = 0; i < sizeof(a); i++)+ a[i] = (__u8)(i * 37 + 1);+ memcpy(b, a, sizeof(b));+ EXPECT_EQ(memneq_explicit(a, b, sizeof(a)), 0);+ EXPECT_EQ(memneq_explicit(a, NULL, 0), 0);+ for (i = 0; i < sizeof(a); i++) {+ for (bit = 0; bit < 8; bit++) {+ b[i] ^= 1u << bit;+ EXPECT_EQ(memneq_explicit(a, b, sizeof(a)), 1);+ EXPECT_EQ(memneq_explicit(a, b, i), 0);+ b[i] ^= 1u << bit;+ }+ }+}+static const struct unit_case cases[] = {UNIT_CASE(digits_counts_leading_digits),UNIT_CASE(decimal_fills_its_span),@@ -309,6 +330,7 @@ static const struct unit_case cases[] = {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),+ UNIT_CASE(memneq_explicit_tells_every_bit_apart),{}};diff --git a/util/attest.c b/util/attest.cindex 0a0a9d02..5fb91d91 100644--- a/util/attest.c+++ b/util/attest.c@@ -138,7 +138,7 @@ static bool quote_covers(const struct tpm_attestation *quote,}if (sha256_final(&ctx, digest) != 0)return false;- return ct_eq(digest, a.pcr_digest, 32);+ return !memneq_explicit(digest, a.pcr_digest, 32);}int attest_evidence_config(const __u8 *nonce, size_t nonce_len,@@ -564,7 +564,7 @@ static int on_record(void *ctx)chain_upatch_event(load ? "shirudo-upatch" :"shirudo-unload", p->upatch_name,payload, event);- r->derived = ct_eq(event, r->digest, 32);+ r->derived = !memneq_explicit(event, r->digest, 32);}} else if (!strcmp(p->type, "tree_op")) {__u8 operation[32], event[32];@@ -576,7 +576,7 @@ static int on_record(void *ctx)!strncmp(p->tree_digest_hex, "sha256:", 7) &&!hex_decode(p->tree_digest_hex + 7, operation, 32)) {chain_tree_event(p->tree_seq, operation, event);- r->derived = ct_eq(event, r->digest, 32);+ r->derived = !memneq_explicit(event, r->digest, 32);}} else if (!strcmp(p->type, "key_revoke")) {__u8 sha[32], event[32];@@ -584,7 +584,7 @@ static int on_record(void *ctx)strscpy(r->label, p->key_fingerprint, sizeof(r->label));if (!hex_decode(p->key_sha_hex, sha, 32)) {chain_key_event("shirudo-key-revoke", sha, event);- r->derived = ct_eq(event, r->digest, 32);+ r->derived = !memneq_explicit(event, r->digest, 32);}}p->n_recs++;@@ -703,10 +703,11 @@ static int check_signed(struct reporter *rep, const char *what,say(rep, ret == 0, "%s: ECDSA signature by the attestation key", what);tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qn);say(rep, parsed->signer_len == TPM_NAME_LEN &&- ct_eq(parsed->signer, qn, TPM_NAME_LEN),+ !memneq_explicit(parsed->signer, qn, TPM_NAME_LEN),"%s: signed by the key as an endorsement primary", what);say(rep, parsed->extra_len == nonce_len &&- (nonce_len == 0 || ct_eq(parsed->extra, nonce, nonce_len)),+ (nonce_len == 0 ||+ !memneq_explicit(parsed->extra, nonce, nonce_len)),"%s: carries the nonce", what);return 0;}@@ -904,7 +905,7 @@ int attest_verify(const char *evidence, size_t len,}say(&rep, expect->nonce_len == nonce_len &&(nonce_len == 0 ||- ct_eq(nonce, expect->nonce, nonce_len)),+ !memneq_explicit(nonce, expect->nonce, nonce_len)),"nonce matches the request");if (hex_bytes(p->ak_public_hex, ak_public, sizeof(ak_public),@@ -926,12 +927,12 @@ int attest_verify(const char *evidence, size_t len,__u8 computed[TPM_NAME_LEN];tpm_object_name(ak_public, ak_public_len, computed);- say(&rep, ct_eq(computed, ak_name, sizeof(computed)),+ say(&rep, !memneq_explicit(computed, ak_name, sizeof(computed)),"attestation key: name matches its public area");}if (expect->have_ak_name)- say(&rep, ct_eq(ak_name, expect->ak_name,- TPM_NAME_LEN),+ say(&rep,+ !memneq_explicit(ak_name, expect->ak_name, TPM_NAME_LEN),"attestation key: the pinned key");elsenote(&rep, "attestation key not pinned: name %s",@@ -954,12 +955,12 @@ int attest_verify(const char *evidence, size_t len,parsed = tpm_public_parse(ek_public, ek_public_len,&ek_pub) == 0;tpm_object_name(ek_public, ek_public_len, computed);- say(&rep, parsed && ct_eq(computed, ek_name,- TPM_NAME_LEN),+ say(&rep,+ parsed && !memneq_explicit(computed, ek_name, TPM_NAME_LEN),"endorsement key: name matches its public area");say(&rep, parsed && ek_pub.attrs == TPM_EK_ATTRS &&ek_pub.policy_len == 32 &&- ct_eq(ek_pub.policy, shirudo_tpm_ek_policy, 32),+ !memneq_explicit(ek_pub.policy, shirudo_tpm_ek_policy, 32),"endorsement key: TCG default template under the ""endorsement policy");} else {@@ -972,7 +973,7 @@ int attest_verify(const char *evidence, size_t len,say(&rep, have_ek &&!hex_bytes(p->ek_name_hex, ek_name, sizeof(ek_name), &n) &&n == TPM_NAME_LEN &&- ct_eq(ek_name, expect->ek_name, TPM_NAME_LEN),+ !memneq_explicit(ek_name, expect->ek_name, TPM_NAME_LEN),"endorsement key: the pinned key");}if (have_cred && hex_bytes(p->cred_hex, cred, sizeof(cred), &cred_len)) {@@ -981,8 +982,8 @@ int attest_verify(const char *evidence, size_t len,}if (expect->have_credential)say(&rep, have_cred && cred_len == TPM_CREDENTIAL_LEN &&- ct_eq(cred, expect->credential,- TPM_CREDENTIAL_LEN),+ !memneq_explicit(cred, expect->credential,+ TPM_CREDENTIAL_LEN),"attestation key: activated the credential made for it ""under the endorsement key");else if (have_cred)@@ -1015,7 +1016,7 @@ int attest_verify(const char *evidence, size_t len,sha256_final(&ctx, digest);}vouched = complete &&- ct_eq(digest, quote.pcr_digest, 32);+ !memneq_explicit(digest, quote.pcr_digest, 32);say(&rep, vouched,"quote: PCR digest matches the listed values");if (!vouched)@@ -1045,12 +1046,12 @@ int attest_verify(const char *evidence, size_t len,if (ret == 0) {say(&rep, certify.type == TPM_ST_ATTEST_NV &&certify.index_name_len == TPM_NAME_LEN &&- ct_eq(certify.index_name, reg_name,- TPM_NAME_LEN),+ !memneq_explicit(certify.index_name, reg_name,+ TPM_NAME_LEN),"register: certification names the listed index");say(&rep, certify.type == TPM_ST_ATTEST_NV &&certify.offset == 0 && certify.nv_len == 32 &&- ct_eq(certify.nv_data, reg_value, 32),+ !memneq_explicit(certify.nv_data, reg_value, 32),"register: certified contents match the listed value");}if (qrc == 0 && crc == 0)@@ -1115,8 +1116,8 @@ int attest_verify(const char *evidence, size_t len,tpm_nv_name(NV_INDEX,TPM_REGISTER_ATTRS | TPM_NV_WRITTEN,policy, 32, expected_name);- say(&rep, ct_eq(expected_name, reg_name, TPM_NAME_LEN) &&- ct_eq(expected_name, p->init.name, TPM_NAME_LEN),+ say(&rep, !memneq_explicit(expected_name, reg_name, TPM_NAME_LEN) &&+ !memneq_explicit(expected_name, p->init.name, TPM_NAME_LEN),"register: public area is bound to PCRs %u and %u as recorded",CHAIN_PCR, CHAIN_PHASE_PCR);for (bit = 0; bit < 24; bit++) {@@ -1127,8 +1128,8 @@ int attest_verify(const char *evidence, size_t len,held = bit == CHAIN_PCR ? p->init.pcr_before :bit == CHAIN_PHASE_PCR ? p->init.phase_before :NULL;- say(&rep, held && ct_eq(held, expect->bind_values[bit],- 32),+ say(&rep, held &&+ !memneq_explicit(held, expect->bind_values[bit], 32),"binding: PCR %u held the expected value", bit);}if (expect->require_initrd)@@ -1156,7 +1157,7 @@ int attest_verify(const char *evidence, size_t len,if (i)runtime++;}- say(&rep, ct_eq(chain, reg_value, 32),+ say(&rep, !memneq_explicit(chain, reg_value, 32),"eventlog: %zu record%s replay to the certified value", p->n_recs,p->n_recs == 1 ? "" : "s");/*@@ -1177,14 +1178,14 @@ int attest_verify(const char *evidence, size_t len,sizeof(anchor));sha256_calc(anchor, strlen(anchor), anchor_digest);- say(&rep, named && ct_eq(anchor_digest, p->init.anchor_digest,- 32),+ say(&rep, named &&+ !memneq_explicit(anchor_digest, p->init.anchor_digest, 32),"anchor: record digest matches the index name");memcpy(pcr_expected, p->init.pcr_before, 32);chain_fold(pcr_expected, anchor_digest);if (p->pcr_have & (1u << CHAIN_PCR)) {- bool exact = ct_eq(pcr_expected,- p->pcrs[CHAIN_PCR], 32);+ bool exact = !memneq_explicit(pcr_expected,+ p->pcrs[CHAIN_PCR], 32);if (exact || expect->strict_anchor)say(&rep, exact, "anchor: PCR %u equals the "@@ -1202,18 +1203,19 @@ int attest_verify(const char *evidence, size_t len,}if (expect->have_init)- say(&rep, ct_eq(p->init.digest, expect->init_event, 32),+ say(&rep,+ !memneq_explicit(p->init.digest, expect->init_event, 32),"state: init event covers the expected certificate, set ""and tree root");if (expect->have_chain) {- say(&rep, ct_eq(reg_value, expect->chain, 32),+ say(&rep, !memneq_explicit(reg_value, expect->chain, 32),"state: register equals the expected value");} else if (expect->have_init) {__u8 predicted[32] = {};chain_fold(predicted, expect->init_event);if (runtime == 0)- say(&rep, ct_eq(reg_value, predicted, 32),+ say(&rep, !memneq_explicit(reg_value, predicted, 32),"state: register equals the value predicted for ""the set at boot");else@@ -1231,7 +1233,7 @@ int attest_verify(const char *evidence, size_t len,if (!(expect->pcr_expect_mask & (1u << bit)))continue;say(&rep, (p->pcr_have & (1u << bit)) &&- ct_eq(p->pcrs[bit], expect->pcrs[bit], 32),+ !memneq_explicit(p->pcrs[bit], expect->pcrs[bit], 32),"PCR %u matches the expected value", bit);}ret = rep.failed ? -EKEYREJECTED : 0;@@ -1305,10 +1307,10 @@ static bool cert_describes(const struct cert_pubkey *key,return false;if (key->rsa)return key->n_len == pub->n_len &&- ct_eq(key->n, pub->n, pub->n_len) &&+ !memneq_explicit(key->n, pub->n, pub->n_len) &&key->e == (pub->rsa_exponent ? pub->rsa_exponent : 65537);- return ct_eq(key->x, pub->x, 32) &&- ct_eq(key->y, pub->y, 32);+ return !memneq_explicit(key->x, pub->x, 32) &&+ !memneq_explicit(key->y, pub->y, 32);}int attest_challenge_make(const char *evidence, size_t len,@@ -1394,20 +1396,18 @@ int attest_challenge_make(const char *evidence, size_t len,* operator enrolled, not for whatever the document names.*/if (pol->ak_name)- say(&rep, ct_eq(ak_name, pol->ak_name,- TPM_NAME_LEN),+ say(&rep, !memneq_explicit(ak_name, pol->ak_name, TPM_NAME_LEN),"attestation key: the pinned key");if (pol->ek_name)- say(&rep, ct_eq(ek_name, pol->ek_name,- TPM_NAME_LEN),+ say(&rep, !memneq_explicit(ek_name, pol->ek_name, TPM_NAME_LEN),"endorsement key: the pinned key");/* The key must be an endorsement key, not any decryption key. */tpm_object_name(ek_public, ek_public_len, computed);- say(&rep, ct_eq(computed, ek_name, TPM_NAME_LEN),+ say(&rep, !memneq_explicit(computed, ek_name, TPM_NAME_LEN),"endorsement key: name matches its public area");say(&rep, pub.attrs == TPM_EK_ATTRS && pub.policy_len == 32 &&- ct_eq(pub.policy, shirudo_tpm_ek_policy, 32),+ !memneq_explicit(pub.policy, shirudo_tpm_ek_policy, 32),"endorsement key: TCG default template under the endorsement ""policy");if (top[2].seen) {diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex efe8df66..df2ecbe6 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -671,7 +671,7 @@ int tree_event_replay(__u8 out[32])ret = sha256_calc(next, sizeof(next), chain);if (ret)break;- if (!ct_eq(chain, recorded, 32)) {+ if (memneq_explicit(chain, recorded, 32)) {ret = -EINVAL;break;}@@ -708,7 +708,7 @@ void tree_emit_status(struct json_emit *e)json_kv_bool(e, "eventlog_matches_state",hex_decode(s.projection_root, live,32) == 0 &&- ct_eq(live, replay, 32));+ !memneq_explicit(live, replay, 32));} else {json_kv_null(e, "eventlog_projection_root");json_kv_null(e, "eventlog_matches_state");diff --git a/util/chain.c b/util/chain.cindex 4fc3d457..ebf319b4 100644--- a/util/chain.c+++ b/util/chain.c@@ -55,17 +55,6 @@ void sha256_2(const void *a, size_t a_len,}}-int ct_eq(const void *a, const void *b, size_t n)-{- const __u8 *pa = a, *pb = b;- __u8 diff = 0;- size_t i;-- for (i = 0; i < n; i++)- diff |= pa[i] ^ pb[i];- return diff == 0;-}-static int member_cmp(const void *a, const void *b){return strcmp(((const struct measured_upatch *)a)->name,@@ -223,7 +212,7 @@ static bool name_equals(const struct tpm_nv_info *info,const __u8 name[TPM_NAME_LEN]){return info->name_len == TPM_NAME_LEN &&- ct_eq(info->name, name, TPM_NAME_LEN);+ !memneq_explicit(info->name, name, TPM_NAME_LEN);}static int register_define(struct tpm *tpm, bool defined,@@ -284,7 +273,7 @@ int chain_tpm_bound(struct tpm *tpm)if (ret)return ret;if (ak.name_len != TPM_NAME_LEN ||- !ct_eq(ak.name, id.ak_name, TPM_NAME_LEN)) {+ memneq_explicit(ak.name, id.ak_name, TPM_NAME_LEN)) {pr_err("tpm: not the TPM init bound (attestation key name ""differs)");ret = -EACCES;@@ -353,8 +342,8 @@ int chain_init(const __u8 event[32],ret = -EEXIST;goto out;}- if (!ct_eq(info->writer_public, rec.writer_public,- sizeof(info->writer_public))) {+ if (memneq_explicit(info->writer_public, rec.writer_public,+ sizeof(info->writer_public))) {pr_err("tpm: recorded NV writer differs from the guarded key");ret = -EKEYREJECTED;goto out;@@ -486,7 +475,7 @@ int chain_extend(const __u8 event[32], __u8 out_chain[32])ret = -EACCES;goto out;}- if (!ct_eq(nv.policy, policy, sizeof(policy))) {+ if (memneq_explicit(nv.policy, policy, sizeof(policy))) {pr_err("tpm: register write policy is obsolete or substituted");ret = -EACCES;goto out;@@ -502,7 +491,8 @@ int chain_extend(const __u8 event[32], __u8 out_chain[32])ret = register_writer(tpm, writer_public);if (ret)goto out;- if (!ct_eq(writer_public, rec.writer_public, sizeof(writer_public))) {+ if (memneq_explicit(writer_public, rec.writer_public,+ sizeof(writer_public))) {pr_err("tpm: recorded NV writer differs from the guarded key");ret = -EKEYREJECTED;goto out;diff --git a/util/chain.h b/util/chain.hindex 6e811bff..0aa4b67a 100644--- a/util/chain.h+++ b/util/chain.h@@ -41,9 +41,6 @@ void sha256_2(const void *a, size_t a_len,const void *b, size_t b_len,__u8 out[CHAIN_DIGEST_LEN]);-/* Constant time; 1 when equal. */-int ct_eq(const void *a, const void *b, size_t n);-struct measured_upatch {char name[UPATCH_NAME_MAX + 1];char payload_hex[2 * UPATCH_PAYLOAD_HASH_LEN + 1];