eBPF-based security platform
coccinelle: refuse two strings hashed with nothing between them
Daniel Borkmannd71928eaabdc
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 17085bb3..d55256d8 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -676,6 +676,10 @@ can rewrite what it finds does so with ``-D patch``:* - ``secret_cmp``- ``memcmp()`` of a secret or what derives from one rather than``memneq_explicit()``.+ * - ``digest_concat``+ - Two strings fed to a digest one after the other, each by its+ ``strlen()`` alone, with no terminator, length or delimiter+ between them to say where the first ends.* - ``openssl_ret``- A test of the truth of an OpenSSL function that returns 0 or anegative value on failure.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex ba5a4337..d1f55de5 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -67,9 +67,11 @@ signed config image.``array_size``, which rewrites the one and leaves the other, alocal's address left in the caller's object beside the forms``local_escape`` leaves alone, a literal compared under a length- written out beside the lengths ``literal_count`` takes, a file a- rule gives up on at its time limit named rather than failed,- ``SEC`` declared an attribute in every rule, and the tree itself+ written out beside the lengths ``literal_count`` takes, two+ strings hashed in a row beside the inputs ``digest_concat`` can+ tell apart, a file a rule gives up on at its time limit named+ rather than failed, ``SEC`` declared an attribute in every rule,+ and the tree itself* - ``t0007-check-operator-linkage``- ``check-operator-linkage.sh``: the libraries an operator binarymay link and its own interpreter, another loader, a librarydiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex c526aedf..1a8cb539 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -11,7 +11,8 @@# refuse, the rules of SAMPLE_RULES each at its line, array_size a count# by hand in a macro and an ARRAY_SIZE() outside <shirudo/misc.h>,# local_escape a local's address left in the caller's object,-# literal_count a literal compared under a length written out, a file a+# literal_count a literal compared under a length written out,+# digest_concat two strings hashed with nothing between them, a file a# rule gives up on at its time limit is named rather than failed, every# rule declares SEC an attribute, and the tree itself.@@ -1262,6 +1263,96 @@ test_expect_success 'literal_count reports a literal compared under a number' '[ "$(grep -c "^literal_count:" "$W/err")" = 5 ]'+# Two strings fed to a digest in a row, each by its strlen() alone: as+# statements, through a return code, in one condition and through+# sha256_2(). Beside them what tells the two apart: the first string's+# terminator, a delimiter, a fixed size, and two digests.+write_digest_samples() {+ cat >"$1" <<'EOF'+int plain(struct sha256_ctx *ctx, const char *host, const char *port)+{+ sha256_update(ctx, host, strlen(host));+ sha256_update(ctx, port, strlen(port));+ return 0;+}++int chained(struct sha256_ctx *ctx, const char *host, const char *port)+{+ int ret;++ ret = sha256_update(ctx, host, strlen(host));+ if (!ret)+ ret = sha256_update(ctx, port, strlen(port));+ return ret;+}++int in_one_condition(struct sha256_ctx *ctx, const char *host,+ const char *port)+{+ if (sha256_update(ctx, host, strlen(host)) != 0 ||+ sha256_update(ctx, port, strlen(port)) != 0)+ return -EIO;+ return 0;+}++int both_at_once(const char *host, const char *port, __u8 out[32])+{+ return sha256_2(host, strlen(host), port, strlen(port), out);+}++int terminated(struct sha256_ctx *ctx, const char *host, const char *port)+{+ sha256_update(ctx, host, strlen(host) + 1);+ sha256_update(ctx, port, strlen(port));+ return 0;+}++int delimited(struct sha256_ctx *ctx, const char *host, const char *port)+{+ if (sha256_update(ctx, host, strlen(host)) != 0 ||+ sha256_update(ctx, ":", 1) != 0 ||+ sha256_update(ctx, port, strlen(port)) != 0)+ return -EIO;+ return 0;+}++int fixed_size(struct sha256_ctx *ctx, const char *host, const __u8 key[32])+{+ sha256_update(ctx, host, strlen(host));+ sha256_update(ctx, key, 32);+ return 0;+}++int two_digests(struct sha256_ctx *a, struct sha256_ctx *b, const char *host,+ const char *port)+{+ sha256_update(a, host, strlen(host));+ sha256_update(b, port, strlen(port));+ return 0;+}++int tagged(const char *tag, const __u8 cert[32], __u8 out[32])+{+ return sha256_2(tag, strlen(tag) + 1, cert, 32, out);+}+EOF+}++test_expect_success 'digest_concat reports two strings hashed with nothing between' '+ R11=$W/digest &&+ mkdir -p "$R11/testing/coccinelle" &&+ git init -q "$R11" &&+ cp "$ROOT/testing/coccinelle/digest_concat.cocci" \+ "$R11/testing/coccinelle/" &&+ write_digest_samples "$R11/samples.c" &&+ ! (cd "$R11" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ for line in 4 14 21 29; do+ grep -q "^digest_concat: samples.c:$line:.*port is hashed right after host, each by its strlen() alone" "$W/err" ||+ exit 1+ done &&+ [ "$(grep -c "^digest_concat:" "$W/err")" = 4 ]+'+# A spatch that gives up, as a rule with --timeout makes it: the file it# gave up on is named and the check passes, while another failure fails.write_fake_spatch() {diff --git a/testing/coccinelle/digest_concat.cocci b/testing/coccinelle/digest_concat.coccinew file mode 100644index 00000000..0ff4c973--- /dev/null+++ b/testing/coccinelle/digest_concat.cocci@@ -0,0 +1,96 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A digest fed two strings in a row, each by its strlen(), takes "ab"+// and "c" for "a" and "bc": nothing of what is hashed says where the+// first ends, so two inputs that differ share one digest. Feed a string+// with its terminator, strlen() + 1, put its length ahead of it, or+// something no string holds between the two.++virtual report++@initialize:python@+@@+import re++UPDATE = r"\b(?:sha256_update|EVP_Digest(?:Sign|Verify)?Update|EVP_MAC_update|HMAC_Update|SHA256_Update|SHA1_Update)\s*\("++def message(p, a, b):+ coccilib.report.print_report(p[0], "%s is hashed right after %s, each by its strlen() alone" % (b, a))++# The digest calls of an expression, in the order it runs them: the+# context each feeds, and the string it feeds by strlen() alone, if any.+def updates(text):+ found = []+ for m in re.finditer(UPDATE, text):+ depth, args, start = 0, [], m.end()+ for i in range(m.end(), len(text)):+ c = text[i]+ if c in "([":+ depth += 1+ elif c in ")]":+ if depth == 0:+ args.append(text[start:i].strip())+ break+ depth -= 1+ elif c == "," and depth == 0:+ args.append(text[start:i].strip())+ start = i + 1+ if len(args) != 3:+ continue+ counted = re.sub(r"\s", "", args[2]) == "strlen(%s)" % re.sub(r"\s", "", args[1])+ found.append((re.sub(r"\s", "", args[0]), args[1] if counted else None))+ return found++@seq exists@+attribute name SEC;+expression C, A, B;+identifier f = {sha256_update, EVP_DigestUpdate, EVP_DigestSignUpdate,+ EVP_DigestVerifyUpdate, EVP_MAC_update, HMAC_Update,+ SHA256_Update, SHA1_Update};+position p;+@@+ f(C, A, strlen(A))+ ... when != f(C, ...)+ f@p(C, B, strlen(B))++@script:python depends on report@+p << seq.p;+A << seq.A;+B << seq.B;+@@+message(p, A, B)++// Two calls one condition runs in turn, which is a single node to the+// path above.+@both@+expression E1, E2;+position p;+@@+(+ E1 ||@p E2+|+ E1 &&@p E2+)++@script:python depends on report@+p << both.p;+E1 << both.E1;+E2 << both.E2;+@@+left, right = updates(E1), updates(E2)+if left and right and left[-1][0] == right[0][0] and left[-1][1] and right[0][1]:+ message(p, left[-1][1], right[0][1])++@pair@+expression A, B;+position p;+@@+ sha256_2@p(A, strlen(A), B, strlen(B), ...)++@script:python depends on report@+p << pair.p;+A << pair.A;+B << pair.B;+@@+message(p, A, B)