eBPF-based security platform
coccinelle: refuse a jump that leaves with a stale success
Daniel Borkmannc8ad22b7d0a9
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex ee346dc5..a48de297 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -763,6 +763,11 @@ can rewrite what it finds does so with ``-D patch``:MSC12-C. Following every path of a long function can takeminutes, so the rule gives up on a file after one; the checknames each file it gave up on.+ * - ``stale_success``+ - A ``goto`` or ``break`` that leaves with what a call or a 0 stored+ 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.* - ``bpf_map_rmw``- ``++``, ``+=`` or ``|=`` through the value of a map that is notper-CPU, or through inode, socket or cgroup storage, which otherdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex f904f49d..068ae33d 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -55,8 +55,9 @@ signed config image.- ``check-coccinelle.sh``: a match that fails the check with itsfile, line and rule, the vendored sources left out, a rule namedalone, 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 dead store beside the reads+ 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``, whichrewrites the one and leaves the other, a file a rule gives up ondiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex cf28a972..6b13d423 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -6,11 +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, 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 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.cd "$(dirname "$0")/..". ./test-lib.sh@@ -371,6 +372,77 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'[ "$(grep -c "^bpf_[a-z_]*: samples.bpf.c:" "$W/err")" = 10 ]'+# 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"+write_line_samples() {+ cat >"$1" <<'EOF'+int search_left(int fd, int key)+{+ int ret = -EACCES;++ ret = check(fd);+ if (ret)+ goto out;+ if (lookup(fd, key))+ goto out;+ ret = 0;+out:+ close(fd);+ return ret;+}++int search_reset(int fd, int key)+{+ int ret = -EACCES;++ ret = check(fd);+ if (ret)+ goto out;+ ret = -EACCES;+ if (lookup(fd, key))+ goto out;+ ret = 0;+out:+ close(fd);+ return ret;+}++int holds_exactly(int fd, int n)+{+ int ret = -ENOMEM;++ ret = read_links(fd);+ if (ret)+ goto out;+ if (links(fd) != n)+ goto out;+ ret = 1;+out:+ return ret;+}+EOF+}++test_expect_success 'each rule reports its sample at its line and spares the safe forms' '+ R9=$W/lines &&+ mkdir -p "$R9/testing/coccinelle" &&+ git init -q "$R9" &&+ for r in $SAMPLE_RULES; do+ cp "$ROOT/testing/coccinelle/$r.cocci" "$R9/testing/coccinelle/" ||+ exit 1+ done &&+ write_line_samples "$R9/samples.c" &&+ ! (cd "$R9" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ for l in $SAMPLE_LINES; do+ grep -q "^${l%%:*}: samples.c:${l#*:}:" "$W/err" || exit 1+ done &&+ [ "$(grep -c "^[a-z_]*: samples.c:" "$W/err")" = \+ "$(echo $SAMPLE_LINES | wc -w)" ]+'+# A secret left on the stack, and beside it the scrubbed form and a local# that holds nothing secret, which no rule reports: one report for each# return of the two that leave the secret.diff --git a/testing/coccinelle/stale_success.cocci b/testing/coccinelle/stale_success.coccinew file mode 100644index 00000000..b8a45abf--- /dev/null+++ b/testing/coccinelle/stale_success.cocci@@ -0,0 +1,65 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// A return code declared with an error, int ret = -EACCES, is the error+// of every goto or break that leaves without setting one. Once a call's+// result or a 0 has been stored in it, such a jump returns that instead,+// the success of the step before: a search that found nothing, or a+// response that held nothing, passes. A function that stores a positive+// value in it answers yes or no, and its 0 is an answer.++virtual report++@r exists@+attribute name SEC;+identifier ret, L, f, C;+expression E1;+expression list es;+type T;+position p1, p2, p3;+@@+ T ret@p1 = -C;+ ... when != ret = E1+(+ ret@p2 = f(es);+|+ ret@p2 = 0;+)+ ... when != ret = E1+(+ goto@p3 L;+|+ break@p3;+)+ ... when != ret = E1+ return ret;++@script:python depends on report@+p1 << r.p1;+p2 << r.p2;+p3 << r.p3;+ret << r.ret;+C << r.C;+@@+import re+name = re.compile(r"\b%s\b" % re.escape(ret))+lines = open(p1[0].file).read().split("\n")+def indent(l):+ return len(l.expandtabs(8)) - len(l.expandtabs(8).lstrip())+# The test that leads to the jump, from the if or else that opens it: a+# jump on a test of ret itself leaves with what ret holds on purpose.+opener = re.compile(r"^\s*(\}\s*)?(if|else)\b")+at = int(p3[0].line) - 1+guard = [lines[at]]+for i in range(at - 1, 0, -1):+ if opener.match(guard[-1]) and indent(guard[-1]) < indent(lines[at]) \+ or opener.match(lines[at]) or not indent(lines[i]):+ break+ guard.append(lines[i])+end = next((i for i in range(int(p1[0].line), len(lines))+ if lines[i] == "}"), len(lines))+answers = re.search(r"\b%s\s*=\s*[1-9]" % re.escape(ret),+ "\n".join(lines[int(p1[0].line):end]))+if re.match(r"^E[A-Z0-9]+$", C) and not answers and \+ not any(name.search(l) for l in guard):+ coccilib.report.print_report(p3[0], "leaves with what line %s stored in %s, not the -%s it was declared with" % (p2[0].line, ret, C))