Skip to content

eBPF-based security platform

coccinelle: refuse a sum stored over a total between additions

Daniel Borkmannf14fb9b88f93
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index a785a6e6..7ba5f628 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -772,6 +772,9 @@ can rewrite what it finds does so with ``-D patch``:
- 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.
+ * - ``sum_overwrite``
+ - A sum stored in a total under a condition, between a value it had
+ and an addition to it, where ``+=`` is likely meant.
* - ``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 bf8f3e83..51ca4285 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -56,14 +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`` 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
+ it leaves alone, a case at its line for ``stale_success``,
+ ``break_error`` and ``sum_overwrite`` 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 6032de5d..df07f4b2 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 break_error"
-SAMPLE_LINES="stale_success:9 break_error:53"
+SAMPLE_RULES="stale_success break_error sum_overwrite"
+SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110"
write_line_samples() {
cat >"$1" <<'EOF'
int search_left(int fd, int key)
@@ -481,6 +481,41 @@ int scan_fallback(DIR *d, char *root)
closedir(d);
return ret;
}
+
+size_t status_cap(int rows, int events, int lines)
+{
+ size_t cap;
+
+ cap = 16384 + (size_t)rows * 256;
+ if (events)
+ cap = 65536 + 3 * (size_t)events;
+ if (lines)
+ cap += (size_t)lines * 1024;
+ return cap;
+}
+
+size_t status_cap_added(int rows, int events, int lines)
+{
+ size_t cap;
+
+ cap = 16384 + (size_t)rows * 256;
+ if (events)
+ cap += 65536 + 3 * (size_t)events;
+ if (lines)
+ cap += (size_t)lines * 1024;
+ return cap;
+}
+
+size_t header_size(size_t fixed, size_t chosen, int custom)
+{
+ size_t n;
+
+ n = fixed;
+ if (custom)
+ n = chosen;
+ n += 64;
+ return n;
+}
EOF
}
diff --git a/testing/coccinelle/sum_overwrite.cocci b/testing/coccinelle/sum_overwrite.cocci
new file mode 100644
index 00000000..a091feed
--- /dev/null
+++ b/testing/coccinelle/sum_overwrite.cocci
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: BSD-2-Clause
+// Copyright (c) 2026 Authors of shirudo
+//
+// A total built up step by step, each step adding what its case needs,
+// loses what came before when one step stores a sum of its own where it
+// meant to add it: the size of the status JSON dropped the room for the
+// loaded upatches once the eventlog was asked for, and status printed
+// nothing on a target with many.
+
+virtual report
+
+@r exists@
+attribute name SEC;
+identifier x;
+expression A, B1, B2, C, E, E1;
+position p;
+@@
+(
+ x = A;
+|
+ x += A;
+)
+ ... when != x
+ if (C) x =@p B1 + B2;
+ ... when != x = E1
+ x += E;
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+A << r.A;
+B1 << r.B1;
+B2 << r.B2;
+@@
+import re
+if A not in ("0", "0U", "0u", "NULL") and \
+ not re.search(r"\b%s\b" % re.escape(x), B1 + " " + B2):
+ coccilib.report.print_report(p[0], "%s is stored over between the additions to it; += may be meant" % x)