Skip to content

eBPF-based security platform

coccinelle: refuse bytes taken for a string unchecked

Daniel Borkmann4c2eb0dfa9cb
diff --git a/.agents/skills/security-audit/SKILL.md b/.agents/skills/security-audit/SKILL.md
index 5a1f51d4..a6290f2f 100644
--- a/.agents/skills/security-audit/SKILL.md
+++ b/.agents/skills/security-audit/SKILL.md
@@ -115,8 +115,9 @@ of that tree, as one about a guard does.
`testing/coccinelle` has rules for the mechanical shapes of several
classes below (`openssl_ret`, `errno_clobber`, `alloc_unchecked`,
-`fd_leak`, `double_close`, `use_after_free`, the `bpf_` rules): a clean
-`check-coccinelle.sh` says those shapes are absent, not the class.
+`fd_leak`, `double_close`, `use_after_free`, `read_unterminated`,
+`file_as_string`, the `bpf_` rules): a clean `check-coccinelle.sh` says
+those shapes are absent, not the class.
- **Parse before verify.** Anything read before its signature or digest
is checked (`upatch info`, `bpm info`, manifests, indexes, the
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 350ce0a4..2bff04b4 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -695,6 +695,19 @@ can rewrite what it finds does so with ``-D patch``:
* - ``literal_len``
- A length that does not match the string literal it compares or
copies.
+ * - ``read_unterminated``
+ - A buffer that ``read()``, ``pread()``, ``recv()``, ``readlink()``
+ or the ``getxattr()`` family filled, passed to ``strlen()``,
+ ``strcmp()``, ``strchr()`` or their kin before anything
+ terminated it: ``buf[n] = '\0'``, ``span_to_str()``, or an array
+ zeroed where it is declared and read into for one byte less than
+ its size.
+ * - ``file_as_string``
+ - The bytes ``read_file()``, ``read_fd()`` or a reader of a package
+ member returned, passed to ``strstr()``, ``strtok_r()``,
+ ``strlen()`` or their kin before any function took them with
+ their length: such a function takes the file to end at its first
+ NUL. ``span_to_str()`` on the buffer in place refuses one.
* - ``option_by_hand``
- A value compared with an option name, a literal that starts with
``-`` and is longer than one character, outside
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 1ad5e842..c161cccc 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -60,11 +60,12 @@ signed config image.
``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``,
``strerror_negative`` and ``double_test`` beside the forms it
leaves alone, a dead store beside the reads ``dead_store`` leaves
- alone, a count by hand in a macro and an ``ARRAY_SIZE()`` of a
- file's own for ``array_size``, which rewrites the one and leaves
- the other, 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
+ alone, a buffer a read filled and a file read whole taken for a
+ string, beside the forms that terminate or check first, a count by
+ hand in a macro and an ``ARRAY_SIZE()`` of a file's own for
+ ``array_size``, which rewrites the one and leaves the other, 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 binary
may link and its own interpreter, another loader, a library
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index 13dc69c3..6a9e24d7 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -6,12 +6,12 @@
# 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, each BPF hazard rule, each scrub rule and dead_store
-# 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>, 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.
+# from CERT C, each BPF hazard rule, each scrub rule, dead_store and the
+# 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>, 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.
cd "$(dirname "$0")/.."
. ./test-lib.sh
@@ -937,6 +937,142 @@ test_expect_success 'option_by_hand reports an option compared outside the parse
[ "$(grep -c "^option_by_hand:" "$W/err")" = 1 ]
'
+# Bytes taken for a string: a buffer a read filled that nothing
+# terminated, and a file read whole that nothing saw with its length,
+# each beside the forms that terminate or check first, which no rule
+# reports.
+write_string_samples() {
+ cat >"$1" <<'EOF'
+int id_unterminated(int fd, const char *want)
+{
+ char id[64];
+
+ if (read(fd, id, sizeof(id) - 1) <= 0)
+ return -EIO;
+ return strcmp(id, want);
+}
+
+int id_terminated(int fd, const char *want)
+{
+ char id[64];
+ ssize_t n;
+
+ n = read(fd, id, sizeof(id) - 1);
+ if (n <= 0)
+ return -EIO;
+ id[n] = '\0';
+ return strcmp(id, want);
+}
+
+int id_zeroed(int fd, const char *want)
+{
+ char id[64] = {};
+
+ if (read(fd, id, sizeof(id) - 1) <= 0)
+ return -EIO;
+ return strcmp(id, want);
+}
+
+int id_checked(int fd, const char *want)
+{
+ char id[64];
+ ssize_t n;
+
+ n = read(fd, id, sizeof(id) - 1);
+ if (n <= 0 || span_to_str(id, sizeof(id), id, (size_t)n))
+ return -EIO;
+ return strcmp(id, want);
+}
+
+size_t label_unterminated(const char *path)
+{
+ char label[65];
+
+ if (getxattr(path, "user.label", label, sizeof(label)) < 0)
+ return 0;
+ return strlen(label);
+}
+
+int file_searched(const char *path, const char *needle)
+{
+ size_t len;
+ char *text;
+ int found;
+
+ if (read_file(path, 4096, &text, &len))
+ return -EIO;
+ found = strstr(text, needle) != NULL;
+ xfree(text, len + 1);
+ return found;
+}
+
+int file_checked(const char *path, const char *needle)
+{
+ size_t len;
+ char *text;
+ int found;
+
+ if (read_file(path, 4096, &text, &len))
+ return -EIO;
+ if (span_to_str(text, len + 1, text, len)) {
+ xfree(text, len + 1);
+ return -EINVAL;
+ }
+ found = strstr(text, needle) != NULL;
+ xfree(text, len + 1);
+ return found;
+}
+
+int file_parsed(const char *path, struct json_field *f, size_t n)
+{
+ size_t len;
+ char *text;
+ int ret;
+
+ if (read_file(path, 4096, &text, &len))
+ return -EIO;
+ ret = json_parse(text, len, f, n);
+ xfree(text, len + 1);
+ return ret;
+}
+
+int member_walked(int fd)
+{
+ char *save, *line;
+ size_t len;
+ __u8 *body;
+ int n = 0;
+
+ if (bpm_pkg_read_member_fd(fd, "list", NULL, NULL, 0, &body, &len,
+ 4096))
+ return -EIO;
+ for (line = strtok_r((char *)body, "\n", &save); line;
+ line = strtok_r(NULL, "\n", &save))
+ n++;
+ xfree(body, len + 1);
+ return n;
+}
+EOF
+}
+
+test_expect_success 'the string rules report bytes taken for a string unchecked' '
+ R9=$W/string &&
+ mkdir -p "$R9/testing/coccinelle" &&
+ git init -q "$R9" &&
+ cp "$ROOT/testing/coccinelle/read_unterminated.cocci" \
+ "$ROOT/testing/coccinelle/file_as_string.cocci" \
+ "$R9/testing/coccinelle/" &&
+ write_string_samples "$R9/samples.c" &&
+ ! (cd "$R9" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&
+ grep -q "2 of 2 rules matched" "$W/err" &&
+ grep -q "^read_unterminated: samples.c:7:" "$W/err" &&
+ grep -q "^read_unterminated: samples.c:48:" "$W/err" &&
+ [ "$(grep -c "^read_unterminated: samples.c:" "$W/err")" = 2 ] &&
+ grep -q "^file_as_string: samples.c:59:" "$W/err" &&
+ grep -q "^file_as_string: samples.c:104:" "$W/err" &&
+ [ "$(grep -c "^file_as_string: samples.c:" "$W/err")" = 2 ]
+'
+
# A count by hand in a macro, which writes its argument in parentheses,
# and an ARRAY_SIZE() of a file's own, while the one of <shirudo/misc.h>
# is spared.
diff --git a/testing/coccinelle/file_as_string.cocci b/testing/coccinelle/file_as_string.cocci
new file mode 100644
index 00000000..9c28b597
--- /dev/null
+++ b/testing/coccinelle/file_as_string.cocci
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// Copyright (c) 2026 Authors of shirudo
+//
+// read_file(), read_fd() and the readers of a package member hand over
+// the bytes of a file with their length, and a terminator behind them
+// that says nothing of the bytes: a file can hold a NUL. A function
+// that reads up to a terminator, strstr(), strtok_r(), strlen() and
+// their kin, takes such a file to end at its first NUL, so that what it
+// searches or walks is not what was read, hashed or signed, and what
+// stands behind the NUL is never looked at. Before such a use the
+// bytes go, with their length, to what checks them: span_to_str(), a
+// parser that takes a length, or the reader's own check.
+
+virtual report
+
+@r exists@
+attribute name SEC;
+expression buf, len, E, E1, E2, E3, E4, E5;
+identifier strfn = {strlen, strcmp, strcasecmp, strchr, strrchr, strstr,
+ strcspn, strspn, strpbrk, strtok_r, strdup, xstrdup,
+ atoi, puts, hex_decode, sha256_digest_decode};
+identifier second = {strscpy, strcmp, strcasecmp, strstr};
+identifier f;
+position p1, p2;
+@@
+(
+ read_file@p1(E, E1, &buf, &len)
+|
+ read_fd@p1(E, E1, E2, &buf, &len)
+|
+ bpm_pkg_read_member@p1(E, E1, &buf, &len, E2)
+|
+ \(bpm_pkg_read_member_in@p1\|bpm_pkg_read_member_fd@p1\)(E, E1, E2, E3, E4, &buf, &len, E5)
+)
+... when != f(..., buf, len, ...)
+ when != f(..., (char *)buf, len, ...)
+(
+ strfn@p2(buf, ...)
+|
+ strfn@p2((char *)buf, ...)
+|
+ second@p2(E, buf, ...)
+)
+
+@script:python depends on report@
+p1 << r.p1;
+p2 << r.p2;
+@@
+coccilib.report.print_report(p2[0], "takes for a string the bytes line %s read, before anything saw them with their length" % p1[0].line)
diff --git a/testing/coccinelle/read_unterminated.cocci b/testing/coccinelle/read_unterminated.cocci
new file mode 100644
index 00000000..150e056a
--- /dev/null
+++ b/testing/coccinelle/read_unterminated.cocci
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// Copyright (c) 2026 Authors of shirudo
+//
+// read(), pread(), recv(), readlink() and the getxattr() family fill a
+// buffer and return a count; they write no terminator. A function that
+// reads up to one, strlen(), strcmp(), strchr() and their kin, then
+// runs past what was read, into what the buffer held before or beyond
+// it. Between the read and such a use the buffer is terminated, with
+// buf[n] = '\0' or span_to_str(), which also refuses a NUL among the
+// bytes read. An array zeroed where it is declared and read into for
+// one byte less than its size is terminated as it is.
+
+virtual report
+
+@zeroed3@
+attribute name SEC;
+identifier buf;
+identifier rd3 = {read, pread, recv, readlink};
+expression E;
+type T;
+position p;
+@@
+T buf[...] = {};
+...
+rd3@p(E, buf, sizeof(buf) - 1, ...)
+
+@zeroed4@
+identifier buf;
+identifier rd4 = {readlinkat, getxattr, lgetxattr, fgetxattr};
+expression E, E1;
+type T;
+position p;
+@@
+T buf[...] = {};
+...
+rd4@p(E, E1, buf, sizeof(buf) - 1, ...)
+
+@r exists@
+expression buf, E, E1;
+identifier rd3 = {read, pread, recv, readlink};
+identifier rd4 = {readlinkat, getxattr, lgetxattr, fgetxattr};
+identifier strfn = {strlen, strcmp, strcasecmp, strchr, strrchr, strstr,
+ strcspn, strspn, strpbrk, strtok_r, strdup, xstrdup,
+ atoi, puts, hex_decode, sha256_digest_decode};
+identifier second = {strscpy, strcmp, strcasecmp, strstr};
+position p1 != {zeroed3.p, zeroed4.p};
+position p2;
+@@
+(
+ rd3@p1(E, buf, ...)
+|
+ rd4@p1(E, E1, buf, ...)
+)
+... when != buf[...] = \('\0'\|0\)
+ when != span_to_str(buf, ...)
+(
+ strfn@p2(buf, ...)
+|
+ second@p2(E, buf, ...)
+)
+
+@script:python depends on report@
+p1 << r.p1;
+p2 << r.p2;
+@@
+coccilib.report.print_report(p2[0], "takes for a string what line %s read, which nothing terminated" % p1[0].line)