eBPF-based security platform
coccinelle: require complete secret scrubs
Tobias Klausered7829c3dbac
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 081c946c..2906e41d 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -564,13 +564,15 @@ can rewrite what it finds does so with ``-D patch``:* - ``scrub_missing``- A local filled with an activated credential, a KDF output or anECDH shared value, or by ``hex_bytes()``, ``hex_decode()`` or- ``getrandom()`` when named for a secret, that a path out leaves- without ``memzero_explicit()``.+ ``getrandom()`` when named for a secret, that a return or function+ fallthrough leaves without a full-size ``memzero_explicit()``. It+ recognizes output passed by address.* - ``sensitive_scrub``- A local of a struct with a member marked ``__sensitive`` that a- path out leaves, once the function has used it, without- ``memzero_explicit()`` of the whole struct or a ``__cleanup()``- function, and the address of a compound literal of such a struct.+ return path leaves, once the function has used it, without+ a full-size ``memzero_explicit()`` of the whole struct or a+ ``__cleanup()`` function, and the address of a compound literal+ 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.* - ``secret_cmp``diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 61eb69c1..fd53e4de 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -412,6 +412,35 @@ int digest_public(const char *hex)return -EINVAL;return use(digest);}++int address_output(const char *hex)+{+ __u8 credential[16];++ if (getrandom(&credential, sizeof(credential), 0) < 0)+ return -EIO;+ return use(credential);+}++int partial_scrub(const char *hex)+{+ __u8 credential[16];++ if (hex_decode(hex, credential, sizeof(credential))) {+ memzero_explicit(credential, 1);+ return -EINVAL;+ }+ memzero_explicit(credential, 1);+ return 0;+}++void credential_fallthrough(void)+{+ __u8 credential[16];++ getrandom(credential, sizeof(credential), 0);+ use(credential);+}EOF}@@ -420,7 +449,7 @@ EOF# return before its use, the scrubbed form and the __cleanup() one not.write_sensitive_samples() {mkdir -p "$1/include" &&- printf 'struct client {\n\tchar name[32];\n\tchar __sensitive password[64];\n};\n' \+ printf 'struct client {\n\tchar name[32];\n\tchar password[64] __sensitive;\n\tchar token[32] __sensitive;\n};\n' \>"$1/include/client.h" &&cat >"$1/sensitive.c" <<'EOF'int client_left(const char *url)@@ -471,6 +500,17 @@ int client_field(const char *url)memzero_explicit(c.password, sizeof(c.password));return 0;}++int client_partial_scrub(const char *url)+{+ struct client c = {};++ if (client_parse(url, &c))+ return -EINVAL;+ memzero_explicit(&c, 1);+ return use(&c);+}+EOF}@@ -485,9 +525,9 @@ test_expect_success 'each scrub rule reports its samples and spares the scrubbedwrite_sensitive_samples "$R4" &&! (cd "$R4" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&grep -q "2 of 2 rules matched" "$W/err" &&- [ "$(grep -c "^scrub_missing: samples.c:" "$W/err")" = 4 ] &&- [ "$(grep -c "^sensitive_scrub: sensitive.c:" "$W/err")" = 4 ] &&- [ "$(grep -c "^[a-z_]*: " "$W/err")" = 8 ]+ [ "$(grep -c "^scrub_missing: samples.c:" "$W/err")" = 9 ] &&+ [ "$(grep -c "^sensitive_scrub: sensitive.c:" "$W/err")" = 6 ] &&+ [ "$(grep -c "^[a-z_]*: " "$W/err")" = 15 ]'test_expect_success 'the tree passes' 'diff --git a/testing/coccinelle/scrub_missing.cocci b/testing/coccinelle/scrub_missing.cocciindex a6973f1d..6324e931 100644--- a/testing/coccinelle/scrub_missing.cocci+++ b/testing/coccinelle/scrub_missing.cocci@@ -2,9 +2,10 @@// Copyright (c) 2026 Authors of shirudo//// A local that a function filled with a secret is scrubbed with-// memzero_explicit() on every path out of the function, the error paths-// included: what the stack keeps is the next call's to read, and a core-// dump's. The producers are those of the tree whose output is secret, an+// a full-size memzero_explicit() on every path out of the function, the+// error paths included: what the stack keeps is the next call's to read,+// and a core dump's. The producers are those of the tree whose output is+// secret, an// activated credential, a KDF output and an ECDH shared value, and// hex_bytes(), hex_decode() and getrandom() when the local is named for// a secret. A secret in a struct is sensitive_scrub's.@@ -35,18 +36,18 @@ position p1, p2;)... when any(- tpm_activate_credential@p1(..., \(x\|x.fld\), E, E1)+ tpm_activate_credential@p1(..., \(x\|&x\|x.fld\|&x.fld\), E, E1)|- tpm_kdfa@p1(..., \(x\|x.fld\), E)+ tpm_kdfa@p1(..., \(x\|&x\|x.fld\|&x.fld\), E)|- tpm_kdfe@p1(..., \(x\|x.fld\))+ tpm_kdfe@p1(..., \(x\|&x\|x.fld\|&x.fld\))|- ecdh_p256_ephemeral@p1(..., \(x\|x.fld\))+ ecdh_p256_ephemeral@p1(..., \(x\|&x\|x.fld\|&x.fld\)))-... when != memzero_explicit(x, ...)- when != memzero_explicit(&x, ...)- when != memzero_explicit(x.fld, ...)- when != memzero_explicit(&x.fld, ...)+... when != memzero_explicit(x, sizeof(x))+ when != memzero_explicit(&x, sizeof(x))+ when != memzero_explicit(x.fld, sizeof(x.fld))+ when != memzero_explicit(&x.fld, sizeof(x.fld))return@p2 ...;@n exists@@@ -62,14 +63,14 @@ position p1, p2;)... when any(- hex_bytes@p1(E, x, ...)+ hex_bytes@p1(E, \(x\|&x\), ...)|- hex_decode@p1(E, x, ...)+ hex_decode@p1(E, \(x\|&x\), ...)|- getrandom@p1(x, ...)+ getrandom@p1(\(x\|&x\), ...))-... when != memzero_explicit(x, ...)- when != memzero_explicit(&x, ...)+... when != memzero_explicit(x, sizeof(x))+ when != memzero_explicit(&x, sizeof(x))return@p2 ...;@script:python depends on report@@@ -86,3 +87,4 @@ p2 << n.p2;@@if SECRET_NAME.search(x):unscrubbed(x, p1, p2)+diff --git a/testing/coccinelle/sensitive_scrub.cocci b/testing/coccinelle/sensitive_scrub.cocciindex f11cb350..31f05b51 100644--- a/testing/coccinelle/sensitive_scrub.cocci+++ b/testing/coccinelle/sensitive_scrub.cocci@@ -52,7 +52,7 @@ def declared_with_cleanup(p):return "__cleanup(" in linereturn False-// What a memzero_explicit() names is no use the secret starts from.+// Keep scrub references out of the use positions below.@m@identifier x;position pm;@@ -74,7 +74,7 @@ position pd, p1 != {m.pm, m2.pm}, p2;struct sname@pd x;... when != xx@p1-... when != memzero_explicit(&x, ...)+... when != memzero_explicit(&x, sizeof(x))return@p2 ...;@c@