Skip to content

eBPF-based security platform

coccinelle: refuse a positive errno chosen by a conditional

Daniel Borkmann37d9e9599e1f
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index d55256d8..610f6943 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -784,7 +784,8 @@ can rewrite what it finds does so with ``-D patch``:
read of its ``errno``, after CERT C ERR30-C.
* - ``positive_errno``
- A bare error number or ``errno`` returned or put in ``ret``,
- where a negative error number belongs.
+ alone or as a branch of a conditional, where a negative error
+ number belongs.
* - ``double_close``
- A descriptor passed to a call after it was closed, after CERT C
FIO46-C.
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 0e6d011d..2cf4d7b3 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -56,8 +56,9 @@ signed config image.
- ``check-coccinelle.sh``: a match that fails the check with its
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``,
+ from CERT C and a second, a conditional's branch, for
+ ``positive_errno``, a case for each BPF hazard rule beside the
+ safe form it leaves alone, a case at its line for ``stale_success``,
``break_error``, ``sum_overwrite``, ``fd_leak``, ``getenv_null``,
``strerror_negative`` and ``double_test`` beside the forms it
leaves alone, a dead store beside the reads ``dead_store`` leaves
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index 32ee927f..8a472210 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -72,8 +72,8 @@ test_expect_success 'a rule rewrites what it finds with -D patch' '
'
# One case of what each rule taken from CERT C and ISO/IEC TS 17961
-# refuses. A rule the tree gives no match proves nothing until it matches
-# here.
+# refuses, and for positive_errno a second, the branch of a conditional.
+# A rule the tree gives no match proves nothing until it matches here.
CERT_RULES="errno_clobber positive_errno double_close wrong_test scaled_arith fclose_unchecked"
write_samples() {
cat >"$1" <<'EOF'
@@ -103,6 +103,14 @@ int positive(int x)
return 0;
}
+int positive_chosen(long key)
+{
+ int ret;
+
+ ret = key < 0 ? errno : 0;
+ return ret;
+}
+
int reuse(int fd)
{
close(fd);
@@ -150,6 +158,7 @@ test_expect_success 'each rule taken from CERT C reports its sample' '
for r in $CERT_RULES; do
grep -q "^$r: samples.c:" "$W/err" || exit 1
done &&
+ [ "$(grep -c "^positive_errno: samples.c:" "$W/err")" = 2 ] &&
grep -q "6 of 6 rules matched" "$W/err"
'
diff --git a/testing/coccinelle/positive_errno.cocci b/testing/coccinelle/positive_errno.cocci
index d899c0a8..70004485 100644
--- a/testing/coccinelle/positive_errno.cocci
+++ b/testing/coccinelle/positive_errno.cocci
@@ -3,8 +3,8 @@
//
// A function returns 0 or a negative error number, and its callers test
// for a value below 0: a bare error number, or errno itself, returned or
-// put in ret, passes as success. errno kept in err for a message is
-// positive by design.
+// put in ret, alone or as a branch of a conditional, passes as success.
+// errno kept in err for a message is positive by design.
virtual report
@@ -23,6 +23,7 @@ identifier E = {E2BIG, EACCES, EADDRINUSE, EADDRNOTAVAIL, EAFNOSUPPORT, EAGAIN,
EPROTONOSUPPORT, ERANGE, EROFS, ESPIPE, ESRCH, ESTALE, ETIME,
ETIMEDOUT, ETXTBSY, EUCLEAN, EWOULDBLOCK, EXDEV, EHWPOISON};
identifier ret = {ret};
+expression C, X;
position p;
@@
(
@@ -33,6 +34,14 @@ position p;
return errno@p;
|
ret = errno@p
+|
+ return C ? \(E@p\|errno@p\) : X;
+|
+ return C ? X : \(E@p\|errno@p\);
+|
+ ret = C ? \(E@p\|errno@p\) : X
+|
+ ret = C ? X : \(E@p\|errno@p\)
)
@script:python depends on report@