eBPF-based security platform
coccinelle: refuse a string literal strscpy()'d into an array
Daniel Borkmann34726e0996dc
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 610f6943..a50df8c2 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -704,6 +704,10 @@ can rewrite what it finds does so with ``-D patch``:``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.+ * - ``literal_strscpy``+ - A string literal copied into an array with ``strscpy()``, where+ ``strscpy_literal()`` refuses at build time a literal the array+ cannot hold.* - ``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 2cf4d7b3..2b42aea2 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -68,7 +68,9 @@ 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, two+ written out beside the lengths ``literal_count`` takes, a literal+ copied into an array with ``strscpy()`` beside the copies+ ``literal_strscpy`` leaves alone, twostrings hashed in a row beside the inputs ``digest_concat`` cantell apart, a file a rule gives up on at its time limit namedrather than failed, ``SEC`` declared an attribute in every rule,diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 8a472210..1f358e39 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -12,6 +12,7 @@# 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,+# literal_strscpy a literal copied into an array with strscpy(),# 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.@@ -1282,6 +1283,43 @@ test_expect_success 'literal_count reports a literal compared under a number' '[ "$(grep -c "^literal_count:" "$W/err")" = 5 ]'+# A literal copied into an array with strscpy(), with the size of the+# array or without parentheses, beside a copy into a pointer and a size,+# a copy of a variable and strscpy_literal(), which the rule leaves alone.+write_strscpy_samples() {+ cat >"$1" <<'EOF'+struct s { char name[16]; };++int copied(struct s *o, char *buf, size_t cap, const char *v)+{+ char local[8];++ if (strscpy(o->name, "fixed", sizeof(o->name)) < 0)+ return -E2BIG;+ if (strscpy(local, "x", sizeof local) < 0)+ return -E2BIG;+ if (strscpy(buf, "?", cap) < 0 ||+ strscpy(o->name, v, sizeof(o->name)) < 0)+ return -E2BIG;+ strscpy_literal(o->name, "fixed");+ return 0;+}+EOF+}++test_expect_success 'literal_strscpy reports a literal strscpy() into an array' '+ R12=$W/strscpy &&+ mkdir -p "$R12/testing/coccinelle" &&+ git init -q "$R12" &&+ cp "$ROOT/testing/coccinelle/literal_strscpy.cocci" \+ "$R12/testing/coccinelle/" &&+ write_strscpy_samples "$R12/samples.c" &&+ ! (cd "$R12" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^literal_strscpy: samples.c:7:.*not strscpy_literal()" "$W/err" &&+ grep -q "^literal_strscpy: samples.c:9:.*not strscpy_literal()" "$W/err" &&+ [ "$(grep -c "^literal_strscpy:" "$W/err")" = 2 ]+'+# 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'sdiff --git a/testing/coccinelle/literal_strscpy.cocci b/testing/coccinelle/literal_strscpy.coccinew file mode 100644index 00000000..2715585e--- /dev/null+++ b/testing/coccinelle/literal_strscpy.cocci@@ -0,0 +1,24 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A string literal copied into an array with strscpy() leaves a result+// to check that cannot fail until the literal outgrows the array, and+// then fails at run time on a path nobody tests: strscpy_literal()+// refuses that literal when it is built.++virtual report++@r@+attribute name SEC;+expression E;+constant char[] S;+position p;+@@+ strscpy@p(E, S, sizeof(E))++// The unit cases of strscpy() itself copy literals to test it.+@script:python depends on report@+p << r.p;+@@+if not p[0].file.endswith("testing/unit/string.c"):+ coccilib.report.print_report(p[0], "a literal copied into an array with strscpy(), not strscpy_literal()")