eBPF-based security platform
coccinelle: follow a getenv() result into the variable it is kept in
Daniel Borkmannaf038e2d7adf
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex f0aee1c6..c1aaecda 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -740,6 +740,10 @@ can rewrite what it finds does so with ``-D patch``:- A ``char`` array too short for the NUL of the literal it holds, a``char`` that keeps what ``getc()`` returns, and ``getenv()``passed on unchecked, after the CERT C rules.+ * - ``getenv_null``+ - A ``getenv()`` result kept in a variable and read before a test+ that it is not ``NULL``, in its own condition or before it, where+ ``cert_misc`` refuses one passed on directly.* - ``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 595126a2..d8efb9db 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -57,13 +57,14 @@ 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`` and ``fd_leak`` 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+ ``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+ ``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 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 8db85e34..8c6856a7 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -375,8 +375,9 @@ 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"-SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148"+SAMPLE_RULES="stale_success break_error sum_overwrite fd_leak getenv_null"+SAMPLE_LINES="stale_success:9 break_error:53 sum_overwrite:110 fd_leak:148+getenv_null:170 getenv_null:177"write_line_samples() {cat >"$1" <<'EOF'int search_left(int fd, int key)@@ -542,6 +543,32 @@ int measure_unlocked(const struct op *op)eventlog_unlock(lock);return ret;}++int env_stored(void)+{+ const char *scheme;++ scheme = getenv("BPM_OCI_SCHEME");+ return !strcmp(scheme, "http");+}++size_t env_declared(void)+{+ const char *home = getenv("HOME");++ return strlen(home);+}++int env_tested(void)+{+ const char *region, *endpoint;++ region = getenv("AWS_REGION");+ endpoint = getenv("AWS_ENDPOINT_URL_S3");+ if (!region || !region[0] || !endpoint)+ return -EINVAL;+ return endpoint[0] && strlen(endpoint) < 256;+}EOF}diff --git a/testing/coccinelle/getenv_null.cocci b/testing/coccinelle/getenv_null.coccinew file mode 100644index 00000000..9b24677a--- /dev/null+++ b/testing/coccinelle/getenv_null.cocci@@ -0,0 +1,63 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// getenv() returns NULL for a variable that is not set (CERT C EXP34-C),+// which the environment of a command run by hand, by systemd or by a+// test decides: its result is tested before it is read, in the same+// condition as in value && value[0], or before. cert_misc refuses it+// passed on unchecked; this rule follows it into a variable.++virtual report++@r exists@+attribute name SEC;+expression E, E1, N;+identifier g = {getenv, secure_getenv};+identifier f = {strlen, strnlen, strcmp, strncmp, strchr, strrchr, strstr,+ strpbrk, strspn, strcspn, xstrdup, xstrndup, strscpy,+ parse_u64, parse_s64, parse_x64, str_has_prefix,+ str_skip_prefix, memcpy, open, fopen, realpath, stat, lstat,+ access};+type T;+identifier x;+position p2;+@@+(+ E = g(N);+ ... when != E+ when != E = E1+(+ *E@p2+|+ E@p2[...]+|+ f(..., E@p2, ...)+)+|+ T x = g(N);+ ... when != x+ when != x = E1+(+ *x@p2+|+ x@p2[...]+|+ f(..., x@p2, ...)+)+)++@script:python depends on report@+p2 << r.p2;+@@+import re+# What comes before the read in its statement: a test there, as in+# value && value[0] or !value || f(value), guards it.+lines = open(p2[0].file).read().split("\n")+line, col = int(p2[0].line), int(p2[0].column)+before = "\n".join(lines[max(0, line - 6):line - 1] + [lines[line - 1][:col]])+before = re.split(r"[;{}]", before)[-1]+read = lines[line - 1][col:int(p2[0].column_end)]+name = r"\s*".join(re.escape(t) for t in re.findall(r"\w+|[^\w\s]+", read))+test = r"(!\s*%s\s*\|\||\b%s\s*(&&|\?)|\b%s\s*[!=]=\s*NULL)" % (name, name, name)+if not re.search(test, before):+ coccilib.report.print_report(p2[0], "%s is read before its NULL test; getenv() returns NULL for a variable that is not set" % read)