eBPF-based security platform
coccinelle: refuse a literal compared under a length written out
Daniel Borkmannf0018db431e1
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 04a3aa1b..17085bb3 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -693,8 +693,13 @@ can rewrite what it finds does so with ``-D patch``:wherever it is taken, unless the pointer's own address goes tothe same call.* - ``literal_len``- - A length that does not match the string literal it compares or- copies.+ - A length that does not match the string literal ``memcpy()``+ copies, with or without its terminator.+ * - ``literal_count``+ - A string literal compared or searched with ``memcmp()``,+ ``memchr()`` or their kin under a length written out as a number,+ where ``sizeof`` of the literal or a helper of+ ``<shirudo/string.h>`` takes it from the literal.* - ``read_unterminated``- A buffer that ``read()``, ``pread()``, ``recv()``, ``readlink()``or the ``getxattr()`` family filled, passed to ``strlen()``,diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 82a3fd0e..ba5a4337 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -66,9 +66,10 @@ signed config image.hand in a macro and an ``ARRAY_SIZE()`` of a file's own for``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 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+ ``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* - ``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 43e61a80..c526aedf 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -10,7 +10,8 @@# two rules for bytes taken for a string report a sample of what they# 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, a file a+# 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# rule gives up on at its time limit is named rather than failed, every# rule declares SEC an attribute, and the tree itself.@@ -1218,6 +1219,49 @@ test_expect_success 'local_escape reports a local left in the caller'"'"'s objec[ "$(grep -c "^local_escape:" "$W/err")" = 4 ]'+# A literal compared or searched under a length written out as a number,+# on either side of the call, beside the lengths taken from the literal+# and a named one, which the rule leaves alone.+write_count_samples() {+ cat >"$1" <<'EOF'+#define TAG_LEN 7++int counted(const char *s, const void *p, int c)+{+ return !memcmp(s, "sha256:", 7) || !memcmp("GNU", p, 4) ||+ memchr("nrt", c, 3) || !strncasecmp(s, "az", 3) ||+ memmem(p, 64, "ustar", 5);+}++int derived(const char *s, const void *p, int c)+{+ static const char escapes[] = "nrt";++ return !memcmp(s, "sha256:", sizeof("sha256:") - 1) ||+ !memcmp(p, "GNU", sizeof("GNU")) ||+ !memcmp(s, "sha256:", TAG_LEN) ||+ memchr(escapes, c, sizeof(escapes) - 1) ||+ !memcmp(s, p, 7) || str_has_prefix(s, "sha256:");+}+EOF+}++test_expect_success 'literal_count reports a literal compared under a number' '+ R10=$W/count &&+ mkdir -p "$R10/testing/coccinelle" &&+ git init -q "$R10" &&+ cp "$ROOT/testing/coccinelle/literal_count.cocci" \+ "$R10/testing/coccinelle/" &&+ write_count_samples "$R10/samples.c" &&+ ! (cd "$R10" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^literal_count: samples.c:5:.*memcmp() of a literal with the length 7 written out" "$W/err" &&+ grep -q "^literal_count: samples.c:5:.*memcmp() of a literal with the length 4 written out" "$W/err" &&+ grep -q "^literal_count: samples.c:6:.*memchr() of a literal with the length 3" "$W/err" &&+ grep -q "^literal_count: samples.c:6:.*strncasecmp() of a literal with the length 3" "$W/err" &&+ grep -q "^literal_count: samples.c:7:.*memmem() of a literal with the length 5" "$W/err" &&+ [ "$(grep -c "^literal_count:" "$W/err")" = 5 ]+'+# 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/literal_count.cocci b/testing/coccinelle/literal_count.coccinew file mode 100644index 00000000..261141d9--- /dev/null+++ b/testing/coccinelle/literal_count.cocci@@ -0,0 +1,71 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A comparison with a string literal takes its length from the literal,+// sizeof(lit), sizeof(lit) - 1 or a helper of <shirudo/string.h> that+// does, not from a number beside it: a count one short compares less+// than the literal and still passes, one too long reads past it, and+// neither shows until the literal changes.++virtual report++@initialize:python@+@@+# A name, a sizeof or a strlen() is no number.+def counted(p, f, n):+ try:+ int(n, 0)+ except ValueError:+ return+ coccilib.report.print_report(p[0], "%s() of a literal with the length %s written out" % (f, n))++@cmp@+attribute name SEC;+expression E;+constant char[] S;+constant N;+identifier f = {memcmp, bcmp, strncmp, strncasecmp};+position p;+@@+(+ f@p(E, S, N)+|+ f@p(S, E, N)+)++@script:python depends on report@+p << cmp.p;+N << cmp.N;+f << cmp.f;+@@+counted(p, f, N)++@chr@+expression E;+constant char[] S;+constant N;+identifier f = {memchr, memrchr};+position p;+@@+ f@p(S, E, N)++@script:python depends on report@+p << chr.p;+N << chr.N;+f << chr.f;+@@+counted(p, f, N)++@mem@+expression E, L;+constant char[] S;+constant N;+position p;+@@+ memmem@p(E, L, S, N)++@script:python depends on report@+p << mem.p;+N << mem.N;+@@+counted(p, "memmem", N)diff --git a/testing/coccinelle/literal_len.cocci b/testing/coccinelle/literal_len.cocciindex e1772362..213f863a 100644--- a/testing/coccinelle/literal_len.cocci+++ b/testing/coccinelle/literal_len.cocci@@ -1,8 +1,9 @@// SPDX-License-Identifier: BSD-2-Clause// Copyright (c) 2026 Authors of shirudo//-// A length that does not match the string literal it bounds compares-// or copies more or less than the literal.+// A memcpy() of a string literal under a length that is neither the+// literal's nor the literal's with its terminator copies more or less+// than the literal. A compare is literal_count's, under any number.virtual report@@ -11,7 +12,7 @@ attribute name SEC;expression E;constant char[] S;constant N;-identifier f = {strncmp, strncasecmp, memcmp, memcpy};+identifier f = {memcpy};position p;@@(@@ -32,6 +33,5 @@ try:n = int(N, 0)except Exception:text = None-if text is not None and n != len(text.encode()) and \- not (f in ("memcmp", "memcpy") and n == len(text.encode()) + 1):+if text is not None and n not in (len(text.encode()), len(text.encode()) + 1):coccilib.report.print_report(p[0], "%s() of %d bytes against a %d-byte literal" % (f, n, len(text.encode())))