eBPF-based security platform
coccinelle: refuse a local's address left in the caller's object
Daniel Borkmann9e0b56e1ca72
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 2bff04b4..04a3aa1b 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -725,6 +725,10 @@ can rewrite what it finds does so with ``-D patch``:- Memory used or freed again after it was freed.* - ``uninit``- A local read after a ``goto`` jumped over its assignment.+ * - ``local_escape``+ - The address of a local array or variable stored in a member of an+ object the caller passed in and left there on a path out, where+ it points into a dead frame.* - ``alloc_size``- An allocation sized for another type than its pointer, and afree of memory no allocator returned.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 93416dcb..82a3fd0e 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -64,9 +64,11 @@ signed config image.alone, a buffer a read filled and a file read whole taken for astring, beside the forms that terminate or check first, a count byhand 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+ ``array_size``, which rewrites the one and leaves the other, a+ local's address left in the caller's object beside the forms+ ``local_escape`` leaves alone, 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 6a9e24d7..43e61a80 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -9,8 +9,9 @@# from CERT C, each BPF hazard rule, each scrub rule, dead_store and the# two rules for bytes taken for a string 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+# by hand in a macro and an ARRAY_SIZE() outside <shirudo/misc.h>,+# local_escape a local's address left in the caller's object, 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")/.."@@ -1118,6 +1119,105 @@ test_expect_success 'array_size rewrites the counts but not a definition' 'grep -qF "return ARRAY_SIZE(x) + ARRAY_SIZE(x);" "$R8/samples.c"'+# The address of a local left in an object the caller passed in: an+# array, a variable and an element, through a parameter or a pointer+# taken from one. Beside them what keeps nothing: a member cleared before+# the return, a static local, a pointer's value and an object of the+# function's own.+write_escape_samples() {+ cat >"$1" <<'EOF'+struct ctx {+ const int *entry;+ const char *name;+ int *count;+};++int kept_array(struct ctx *u, int n)+{+ int entry[2] = { n, n };++ u->entry = entry;+ return n;+}++int kept_variable(struct ctx *u)+{+ int count = 0;++ u->count = &count;+ return 0;+}++int kept_through_alias(void *arg)+{+ struct ctx *u = arg;+ int entry[2] = {};++ u->entry = entry;+ return 0;+}++void kept_element(struct ctx *u)+{+ int entry[2] = {};++ if (entry[0])+ u->entry = &entry[1];+}++int cleared(struct ctx *u)+{+ int entry[2] = {};++ u->entry = entry;+ use(u);+ u->entry = NULL;+ return 0;+}++int static_local(struct ctx *u)+{+ static const int+ entry[2] = { 1, 2 };++ u->entry = entry;+ return 0;+}++int pointer_value(struct ctx *u, const char *name)+{+ const char *local = name;++ u->name = local;+ return 0;+}++int own_object(struct ctx *other)+{+ struct ctx c, *u = &c;+ int entry[2] = {};++ u->entry = entry;+ return use(u) + use(other);+}+EOF+}++test_expect_success 'local_escape reports a local left in the caller'"'"'s object' '+ R9=$W/escape &&+ mkdir -p "$R9/testing/coccinelle" &&+ git init -q "$R9" &&+ cp "$ROOT/testing/coccinelle/local_escape.cocci" \+ "$R9/testing/coccinelle/" &&+ write_escape_samples "$R9/samples.c" &&+ ! (cd "$R9" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ for line in 11 19 28 37; do+ grep -q "^local_escape: samples.c:$line:" "$W/err" || exit 1+ done &&+ grep -q "samples.c:11:.*u->entry keeps the address of the local entry past the return" "$W/err" &&+ [ "$(grep -c "^local_escape:" "$W/err")" = 4 ]+'+# 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() {diff --git a/testing/coccinelle/local_escape.cocci b/testing/coccinelle/local_escape.coccinew file mode 100644index 00000000..b87fac5a--- /dev/null+++ b/testing/coccinelle/local_escape.cocci@@ -0,0 +1,131 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo+//+// The address of a local, stored through a pointer the caller passed in,+// stays in the caller's object when the function returns: the object then+// points into a dead frame, which whoever reads the member next takes for+// what it was told. Hand a callback a context of the function's own+// instead, or clear the member on every path out.++virtual report++@initialize:python@+@@+import re++callers = set()++# A static local outlives the call; its declaration says so ahead of its+# name, on the line of the name or on the ones the declaration began on.+def is_static(d, l):+ with open(d[0].file) as f:+ lines = f.read().splitlines()+ at = int(d[0].line) - 1+ decl = re.split(r"\b%s\b" % re.escape(l), lines[at])[0]+ while at > 0 and not re.search(r"[;{}]\s*$|^\s*$", lines[at - 1]):+ at -= 1+ decl = lines[at] + " " + decl+ return "static" in decl.split()++def escape(fp, d, pos, p, fld, l):+ if (fp[0].file, fp[0].line, p) in callers and not is_static(d, l):+ coccilib.report.print_report(pos[0], "%s->%s keeps the address of the local %s past the return" % (p, fld, l))++// A pointer that came in as a parameter, or was taken from one.+@owner@+attribute name SEC;+identifier f, p;+type T;+position fp;+@@+ f@fp(..., T *p, ...) { ... }++@script:python@+fp << owner.fp;+p << owner.p;+@@+callers.add((fp[0].file, fp[0].line, p))++@alias@+identifier f, p, q;+type T, T1;+position fp;+@@+ f@fp(..., T1 q, ...)+ {+ ... when any+(+ T *p = q;+|+ T *p = (T *)q;+)+ ... when any+ }++@script:python@+fp << alias.fp;+p << alias.p;+@@+callers.add((fp[0].file, fp[0].line, p))++@array exists@+identifier f, p, fld, l;+type T;+expression E;+position fp, d, pos;+@@+ f@fp(...)+ {+ ... when any+(+ T l@d[...];+|+ T l@d[...] = ...;+)+ ... when any+(+ p->fld@pos = l;+|+ p->fld@pos = &l[...];+)+ ... when != p->fld = E+ }++@script:python depends on report@+fp << array.fp;+d << array.d;+pos << array.pos;+p << array.p;+fld << array.fld;+l << array.l;+@@+escape(fp, d, pos, p, fld, l)++@scalar exists@+identifier f, p, fld, l;+type T;+expression E;+position fp, d, pos;+@@+ f@fp(...)+ {+ ... when any+(+ T l@d;+|+ T l@d = ...;+)+ ... when any+ p->fld@pos = &l;+ ... when != p->fld = E+ }++@script:python depends on report@+fp << scalar.fp;+d << scalar.d;+pos << scalar.pos;+p << scalar.p;+fld << scalar.fld;+l << scalar.l;+@@+escape(fp, d, pos, p, fld, l)