eBPF-based security platform
coccinelle: refuse a store that is stored over before any read
Daniel Borkmann50c4df1937ad
diff --git a/.github/scripts/check-coccinelle.sh b/.github/scripts/check-coccinelle.shindex 599f724a..85af1df3 100755--- a/.github/scripts/check-coccinelle.sh+++ b/.github/scripts/check-coccinelle.sh@@ -53,15 +53,28 @@ for rule in "$@"; doincludes=fi# Each file on its own, so that one Coccinelle cannot parse costs only- # that file, and in parallel.- xargs -P "$jobs" -n 16 spatch -D report --very-quiet $includes \+ # that file, and in parallel. A rule with a time limit takes a file+ # per run, as spatch gives up on all a run was given.+ per=16+ if grep -q '^#spatch.*--timeout' "$rule"; then+ per=1+ fi+ xargs -P "$jobs" -n "$per" spatch -D report --very-quiet $includes \--include-headers --sp-file "$rule" <"$work/files" \- >"$work/$name.out" 2>"$work/$name.err" || {- cat "$work/$name.err" >&2- printf 'check-coccinelle: %s: spatch failed\n' "$name" >&2- bad=$((bad + 1))- continue- }+ >"$work/$name.out" 2>"$work/$name.err" ||+ if grep -v -e '^timeout (we abort): ' \+ -e '^Fatal error: exception .*Timeout$' \+ "$work/$name.err" | grep -q .; then+ cat "$work/$name.err" >&2+ printf 'check-coccinelle: %s: spatch failed\n' "$name" >&2+ bad=$((bad + 1))+ continue+ fi+ sed -n 's/^timeout (we abort): //p' "$work/$name.err" |+ while IFS= read -r file; do+ printf 'check-coccinelle: %s: gave up on %s at its time limit\n' \+ "$name" "$file" >&2+ donesort -u "$work/$name.out" >"$work/$name.sorted"[ -s "$work/$name.sorted" ] || continuebad=$((bad + 1))diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex c90e55ce..b13fc658 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -720,6 +720,12 @@ can rewrite what it finds does so with ``-D patch``:* - ``fclose_unchecked``- ``fclose()`` of a stream opened for writing with its resultignored, after CERT C ERR33-C.+ * - ``dead_store``+ - A value stored in a local and stored over on every path, the+ error exits included, before anything reads it, after CERT C+ MSC12-C. Following every path of a long function can take+ minutes, so the rule gives up on a file after one; the check+ names each file it gave up on.* - ``bpf_map_rmw``- ``++``, ``+=`` or ``|=`` through the value of a map that is notper-CPU, or through inode, socket or cgroup storage, which other@@ -751,7 +757,10 @@ it refuses to ``t0006-check-coccinelle``, which shows that it matches.``check-coccinelle.sh`` runs a rule without the headers a file includes,which keeps it fast but leaves the types they declare unknown. A rulethat needs them, as ``sizeof_ptr`` needs the type of a struct member,-names its include options on a ``#spatch`` line.+names its include options on a ``#spatch`` line. A rule whose search can+run long names ``--timeout <seconds>`` there, as ``dead_store`` does: the+check then runs it one file at a time and names each file spatch gave up+on, rather than failing.The ``bpf_`` rules read the BPF sources through:src:`testing/coccinelle/bpf.macros`, which tells Coccinelle whatdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 4c172b7f..8c803b6f 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -55,7 +55,9 @@ signed config image.file, line and rule, the vendored sources left out, a rule namedalone, the rewrite of ``-D patch``, a case for each rule takenfrom CERT C, a case for each BPF hazard rule beside the safe- form it leaves alone, and the tree itself+ form it leaves alone, a dead store beside the reads+ ``dead_store`` leaves alone, a file a rule gives up on at its+ time limit named rather than failed, 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 8d136b14..3b064027 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -6,8 +6,9 @@# 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 and each scrub rule report a sample of-# what they refuse, and the tree itself.+# from CERT C, each BPF hazard rule, each scrub rule and dead_store report+# a sample of what they refuse, a file a rule gives up on at its time+# limit is named rather than failed, and the tree itself.cd "$(dirname "$0")/..". ./test-lib.sh@@ -542,6 +543,98 @@ test_expect_success 'curl_slist_free reports lists freed without scrubbing' '[ "$(grep -c "^curl_slist_free: samples.c:" "$W/err")" = 1 ]'+# The error of the first call is stored over before anything reads it;+# a value read on an error path, one read through its address and one+# the next store reads are all read first.+write_dead_store_samples() {+ cat >"$1" <<'EOF'+int lost(const char *buf)+{+ int ret;++ ret = parse_header(buf);+ ret = parse_body(buf);+ return ret;+}++int on_error_path(const char *buf)+{+ int ret;++ ret = -EBADMSG;+ if (!buf)+ goto out;+ ret = parse_body(buf);+out:+ return ret;+}++int through_address(int fd)+{+ int value;++ value = 0;+ read_into(fd, &value);+ value = 1;+ return value;+}++int accumulated(int n)+{+ int sum;++ sum = n;+ sum = sum + 1;+ return sum;+}+EOF+}++test_expect_success 'dead_store reports a store stored over and spares the reads' '+ R6=$W/dead-store &&+ mkdir -p "$R6/testing/coccinelle" &&+ git init -q "$R6" &&+ cp "$ROOT/testing/coccinelle/dead_store.cocci" "$R6/testing/coccinelle/" &&+ write_dead_store_samples "$R6/samples.c" &&+ ! (cd "$R6" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^dead_store: samples.c:5:" "$W/err" &&+ [ "$(grep -c "^dead_store: samples.c:" "$W/err")" = 1 ]+'++# 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() {+ cat >"$1" <<'EOF'+#!/bin/sh+for arg; do+ file=$arg+done+if [ -n "${FAKE_SPATCH_FAIL:-}" ]; then+ echo "parse error" >&2+else+ echo "timeout (we abort): $file" >&2+ echo "Fatal error: exception Coccinelle_modules.Common.Timeout" >&2+fi+exit 2+EOF+ chmod +x "$1"+}++test_expect_success 'a file a rule gives up on is named, not failed' '+ R7=$W/timeout &&+ mkdir -p "$R7/testing/coccinelle" "$W/bin" &&+ git init -q "$R7" &&+ cp "$ROOT/testing/coccinelle/dead_store.cocci" "$R7/testing/coccinelle/" &&+ printf "int x;\n" >"$R7/slow.c" &&+ write_fake_spatch "$W/bin/spatch" &&+ (cd "$R7" && git add -A && PATH="$W/bin:$PATH" sh "$S") \+ >"$W/out" 2>"$W/err" &&+ grep -q "dead_store: gave up on slow.c at its time limit" "$W/err" &&+ ! (cd "$R7" && FAKE_SPATCH_FAIL=1 PATH="$W/bin:$PATH" sh "$S") \+ >"$W/out" 2>"$W/err" &&+ grep -q "dead_store: spatch failed" "$W/err"+'+test_expect_success 'the tree passes' '(cd "$ROOT" && sh "$S") >"$W/out" 2>"$W/err" &&grep -q "rules, no match" "$W/out"diff --git a/testing/coccinelle/dead_store.cocci b/testing/coccinelle/dead_store.coccinew file mode 100644index 00000000..dea9d384--- /dev/null+++ b/testing/coccinelle/dead_store.cocci@@ -0,0 +1,37 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A value stored in a local and stored over on every path, the error+// exits included, before anything reads it is lost: the error a call+// returned, say, which the second store was meant to keep, or a store+// that is dead code (CERT C MSC12-C). The compiler warns only of a local+// never read at all. Following every path of a long function can take+// minutes, so spatch gives up on a file after one, and check-coccinelle.sh+// names each file it gave up on.+#spatch --no-includes --timeout 60++virtual report++@r@+local idexpression x;+expression E1, E2;+position p1, p2;+@@+ x@p1 = E1;+ ... when != x+ when != &x+ when strict+ x@p2 = E2;++@script:python depends on report@+p1 << r.p1;+p2 << r.p2;+x << r.x;+E2 << r.E2;+@@+import re+# A store that reads the value it replaces, x = x + 1, reads it first.+if not re.search(r"\b%s\b" % re.escape(x), E2):+ coccilib.report.print_report(p1[0],+ "%s stored here is stored over at line %s before any read" %+ (x, p2[0].line))