eBPF-based security platform
coccinelle: refuse strerror() of a negative error number
Daniel Borkmann7a20b8263453
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex c1aaecda..17f77858 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -744,6 +744,10 @@ can rewrite what it finds does so with ``-D patch``:- A ``getenv()`` result kept in a variable and read before a testthat it is not ``NULL``, in its own condition or before it, where``cert_misc`` refuses one passed on directly.+ * - ``strerror_negative``+ - ``strerror()`` of a variable that holds a negative error number:+ one assigned ``-errno`` or another negated value, or tested below+ 0 by the ``if`` that holds the call.* - ``errno_clobber``- A cleanup call, such as ``close()``, between a failure and theread of its ``errno``, after CERT C ERR30-C.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex d8efb9db..22a729eb 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -57,10 +57,10 @@ signed config image.alone, the rewrite of ``-D patch``, a case for each rule takenfrom CERT C, a case for each BPF hazard rule beside the safe formit leaves alone, a case at its line for ``stale_success``,- ``break_error``, ``sum_overwrite``, ``fd_leak`` and- ``getenv_null`` 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+ ``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``+ and ``strerror_negative`` 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, afile a rule gives up on at its time limit named rather thanfailed, ``SEC`` declared an attribute in every rule, and the treediff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 8c6856a7..c3c9feac 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -375,9 +375,10 @@ 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 sum_overwrite fd_leak getenv_null"+SAMPLE_RULES="stale_success break_error sum_overwrite fd_leak getenv_null+strerror_negative"SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148-getenv_null:170 getenv_null:177"+getenv_null:170 getenv_null:177 strerror_negative:196 strerror_negative:202"write_line_samples() {cat >"$1" <<'EOF'int search_left(int fd, int key)@@ -569,6 +570,28 @@ int env_tested(void)return -EINVAL;return endpoint[0] && strlen(endpoint) < 256;}++void report_stored(const char *path)+{+ int ret;++ ret = -errno;+ pr_err("open %s: %s", path, strerror(ret));+}++void report_tested(int ret)+{+ if (ret < 0)+ pr_err("load: %s", strerror(ret));+}++void report_negated(int ret, int err)+{+ if (ret < 0)+ pr_err("load: %s", strerror(-ret));+ err = errno;+ pr_err("read: %s", strerror(err));+}EOF}diff --git a/testing/coccinelle/strerror_negative.cocci b/testing/coccinelle/strerror_negative.coccinew file mode 100644index 00000000..ee0a790b--- /dev/null+++ b/testing/coccinelle/strerror_negative.cocci@@ -0,0 +1,39 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// strerror() takes a positive error number. Given a return code that+// holds a negative one, as ret = -errno or the ret a test found below 0+// does, it prints "Unknown error -22" where the message was to say why:+// pass strerror(-ret).++virtual report++@r exists@+attribute name SEC;+identifier X;+expression E, E1;+position p;+@@+ X = -E;+ ... when != X = E1+ strerror@p(X)++@b@+identifier X;+position p;+@@+ if (X < 0) {+ <+... strerror@p(X) ...+>+ }++@script:python depends on report@+p << r.p;+X << r.X;+@@+coccilib.report.print_report(p[0], "strerror() of %s, which holds a negative error number" % X)++@script:python depends on report@+p << b.p;+X << b.X;+@@+coccilib.report.print_report(p[0], "strerror() of %s, which holds a negative error number" % X)