Skip to content

eBPF-based security platform

coccinelle: refuse an error a loop broke with that nothing tests

Daniel Borkmannd5b93cc5f022
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index a48de297..a785a6e6 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -768,6 +768,10 @@ can rewrite what it finds does so with ``-D patch``:
in a return code declared with an error, ``int ret = -EACCES``,
where the jump relies on that error. A function that stores a
positive value in it answers yes or no, and its 0 is left alone.
+ * - ``break_error``
+ - An error stored before a ``break`` that the code after the loop
+ does not test before it calls something other than a cleanup,
+ and then stores over.
* - ``bpf_map_rmw``
- ``++``, ``+=`` or ``|=`` through the value of a map that is not
per-CPU, or through inode, socket or cgroup storage, which other
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 068ae33d..bf8f3e83 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -56,13 +56,14 @@ signed config image.
file, line and rule, the vendored sources left out, a rule named
alone, the rewrite of ``-D patch``, a case for each rule taken
from CERT C, a case for each BPF hazard rule beside the safe form
- it leaves alone, a case at its line for ``stale_success`` 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
+ it leaves alone, a case at its line for ``stale_success`` and
+ ``break_error`` 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
* - ``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 6b13d423..6032de5d 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -375,8 +375,8 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'
# One case of each rule below at the line given with it, and beside it
# the forms the rule leaves alone: the count of reports shows they stay
# quiet.
-SAMPLE_RULES="stale_success"
-SAMPLE_LINES="stale_success:9"
+SAMPLE_RULES="stale_success break_error"
+SAMPLE_LINES="stale_success:9 break_error:53"
write_line_samples() {
cat >"$1" <<'EOF'
int search_left(int fd, int key)
@@ -423,6 +423,64 @@ int holds_exactly(int fd, int n)
out:
return ret;
}
+
+int upgrade_left(DIR *d, int n)
+{
+ struct dirent *de;
+ int ret = 0;
+
+ while ((de = readdir(d))) {
+ if (pair(de)) {
+ ret = -ENAMETOOLONG;
+ break;
+ }
+ n++;
+ }
+ closedir(d);
+ sort_pairs(n);
+ if (unload(n))
+ ret = -EIO;
+ return ret;
+}
+
+int upgrade_tested(DIR *d, int n)
+{
+ struct dirent *de;
+ int ret = 0;
+
+ while ((de = readdir(d))) {
+ if (pair(de)) {
+ ret = -ENAMETOOLONG;
+ break;
+ }
+ n++;
+ }
+ closedir(d);
+ if (ret)
+ return ret;
+ sort_pairs(n);
+ if (unload(n))
+ ret = -EIO;
+ return ret;
+}
+
+int scan_fallback(DIR *d, char *root)
+{
+ struct dirent *de;
+ int ret = 0;
+
+ while ((de = readdir(d))) {
+ if (bad(de)) {
+ ret = -EINVAL;
+ break;
+ }
+ take(root, de);
+ }
+ if (!root[0])
+ ret = -EINVAL;
+ closedir(d);
+ return ret;
+}
EOF
}
diff --git a/testing/coccinelle/break_error.cocci b/testing/coccinelle/break_error.cocci
new file mode 100644
index 00000000..138a8190
--- /dev/null
+++ b/testing/coccinelle/break_error.cocci
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// Copyright (c) 2026 Authors of shirudo
+//
+// An error stored before a break is the loop's failure, and the code
+// after the loop tests it before it acts. When a call that is not a
+// cleanup runs after it untested and a store replaces it, the work after
+// the loop went ahead on a failure nobody read: an upgrade unloaded part
+// of the old version and loaded the new one after the listing of the old
+// one had failed.
+
+virtual report
+
+@r exists@
+attribute name SEC;
+identifier ret;
+identifier f != {close, closedir, fclose, gzclose, munmap, unlink, xfree,
+ xstrfree, libc_free, memzero_explicit, pr_err, pr_warn,
+ pr_info, pr_audit};
+expression E, E1, E2;
+position p1, p3;
+@@
+ ret@p1 = -E;
+ ... when != ret
+(
+ ret@p3 = f(...);
+|
+ f(...);
+ ... when != ret
+ ret@p3 = E1;
+|
+ E2 = f(...);
+ ... when != ret
+ ret@p3 = E1;
+)
+
+@script:python depends on report@
+p1 << r.p1;
+p3 << r.p3;
+ret << r.ret;
+f << r.f;
+@@
+# A break in the pattern would end its path there, so the break comes
+# from the line after the store.
+lines = open(p1[0].file).read().split("\n")
+after = lines[int(p1[0].line)].strip() if int(p1[0].line) < len(lines) else ""
+if after == "break;":
+ coccilib.report.print_report(p1[0], "%s is not tested after the loop before %s() runs, and line %s stores over it" % (ret, f, p3[0].line))