Skip to content

eBPF-based security platform

coccinelle: report a secret left on stack by a function

Tobias Klauser04421ebb19fa
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index c3809107..cba1e7c5 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -561,6 +561,11 @@ can rewrite what it finds does so with ``-D patch``:
- A ``memset()`` of a local that nothing reads afterwards, which
the compiler may drop, rather than ``memzero_explicit()``.
Rewrites.
+ * - ``scrub_missing``
+ - A local filled with an activated credential, a KDF output or an
+ ECDH shared value, or by ``hex_bytes()``, ``hex_decode()`` or
+ ``getrandom()`` when named for a secret, that a path out leaves
+ without ``memzero_explicit()``.
* - ``secret_cmp``
- ``memcmp()`` of a secret or what derives from one rather than
``memneq_explicit()``.
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index bf111c87..b8512bac 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -6,8 +6,8 @@
# check with file, line, rule and message, one that does not passes it,
# a rule that can rewrite does with -D patch, a rule reads the headers
# its #spatch line asks for, on a throwaway repository, each rule taken
-# from CERT C and each BPF hazard rule reports a sample of what it
-# refuses, and the tree itself.
+# from CERT C, each BPF hazard rule and scrub_missing report a sample of
+# what they refuse, and the tree itself.
cd "$(dirname "$0")/.."
. ./test-lib.sh
@@ -368,6 +368,64 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'
[ "$(grep -c "^bpf_[a-z_]*: samples.bpf.c:" "$W/err")" = 10 ]
'
+# A secret left on the stack, and beside it the scrubbed form and a local
+# that holds nothing secret, which no rule reports: one report for each
+# return of the two that leave the secret.
+write_scrub_samples() {
+ cat >"$1" <<'EOF'
+int kdf_left(const __u8 *seed)
+{
+ __u8 key[16];
+
+ if (tpm_kdfa(seed, 32, "STORAGE", NULL, 0, NULL, 0, key, sizeof(key)))
+ return -EIO;
+ return use(key);
+}
+
+int kdf_scrubbed(const __u8 *seed)
+{
+ __u8 key[16];
+ int ret = -EIO;
+
+ if (tpm_kdfa(seed, 32, "STORAGE", NULL, 0, NULL, 0, key, sizeof(key)))
+ goto out;
+ ret = use(key);
+out:
+ memzero_explicit(key, sizeof(key));
+ return ret;
+}
+
+int credential_left(const char *hex)
+{
+ __u8 cred[16];
+
+ if (hex_decode(hex, cred, sizeof(cred)))
+ return -EINVAL;
+ return use(cred);
+}
+
+int digest_public(const char *hex)
+{
+ __u8 digest[32];
+
+ if (hex_decode(hex, digest, sizeof(digest)))
+ return -EINVAL;
+ return use(digest);
+}
+EOF
+}
+
+test_expect_success 'scrub_missing reports its samples and spares the scrubbed form' '
+ R4=$W/scrub &&
+ mkdir -p "$R4/testing/coccinelle" &&
+ git init -q "$R4" &&
+ cp "$ROOT/testing/coccinelle/scrub_missing.cocci" \
+ "$R4/testing/coccinelle/" &&
+ write_scrub_samples "$R4/samples.c" &&
+ ! (cd "$R4" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&
+ [ "$(grep -c "^scrub_missing: samples.c:" "$W/err")" = 4 ]
+'
+
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/scrub_missing.cocci b/testing/coccinelle/scrub_missing.cocci
new file mode 100644
index 00000000..a6973f1d
--- /dev/null
+++ b/testing/coccinelle/scrub_missing.cocci
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// 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
+// 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.
+
+virtual report
+
+@initialize:python@
+@@
+import re
+
+SECRET_NAME = re.compile(r"cred|secret|seed|auth|passw|priv")
+
+def unscrubbed(x, p1, p2):
+ # The tests hold the throwaway secrets of a test VM.
+ if not p2[0].file.startswith("testing/"):
+ coccilib.report.print_report(p2[0], "returns without memzero_explicit() of %s, which line %s filled with a secret" % (x, p1[0].line))
+
+@r exists@
+identifier x, fld;
+type T;
+expression C, E, E1;
+position p1, p2;
+@@
+(
+ T x[C];
+|
+ T x;
+)
+... when any
+(
+ tpm_activate_credential@p1(..., \(x\|x.fld\), E, E1)
+|
+ tpm_kdfa@p1(..., \(x\|x.fld\), E)
+|
+ tpm_kdfe@p1(..., \(x\|x.fld\))
+|
+ ecdh_p256_ephemeral@p1(..., \(x\|x.fld\))
+)
+... when != memzero_explicit(x, ...)
+ when != memzero_explicit(&x, ...)
+ when != memzero_explicit(x.fld, ...)
+ when != memzero_explicit(&x.fld, ...)
+return@p2 ...;
+
+@n exists@
+identifier x;
+type T;
+expression C, E;
+position p1, p2;
+@@
+(
+ T x[C];
+|
+ T x;
+)
+... when any
+(
+ hex_bytes@p1(E, x, ...)
+|
+ hex_decode@p1(E, x, ...)
+|
+ getrandom@p1(x, ...)
+)
+... when != memzero_explicit(x, ...)
+ when != memzero_explicit(&x, ...)
+return@p2 ...;
+
+@script:python depends on report@
+x << r.x;
+p1 << r.p1;
+p2 << r.p2;
+@@
+unscrubbed(x, p1, p2)
+
+@script:python depends on report@
+x << n.x;
+p1 << n.p1;
+p2 << n.p2;
+@@
+if SECRET_NAME.search(x):
+ unscrubbed(x, p1, p2)