eBPF-based security platform
coccinelle: follow a local's address into any slot of the caller's object
Daniel Borkmann4a39185c8299
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex a50df8c2..27eb1272 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -739,9 +739,10 @@ can rewrite what it finds does so with ``-D patch``:* - ``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.+ - The address of a local array or variable, or of a member or element+ of one, stored in an object the caller passed in, in a member at+ any depth, an element or ``*out``, 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 2b42aea2..0fbdbd17 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -66,7 +66,8 @@ signed config image.string, 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- local's address left in the caller's object beside the forms+ local's address, or that of its member or element, left in the+ caller's object at any depth or in ``*out`` beside the forms``local_escape`` leaves alone, a literal compared under a lengthwritten out beside the lengths ``literal_count`` takes, a literalcopied into an array with ``strscpy()`` beside the copiesdiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 1f358e39..66e805fe 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -10,7 +10,8 @@# 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>,-# local_escape a local's address left in the caller's object,+# local_escape a local's address, or that of its member or element,+# left in the caller's object at any depth or in *out,# literal_count a literal compared under a length written out,# literal_strscpy a literal copied into an array with strscpy(),# digest_concat two strings hashed with nothing between them, a file a@@ -1143,9 +1144,11 @@ test_expect_success 'array_size rewrites the counts but not a definition' '# 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.+# taken from one, in a member at any depth, an element and *out, and the+# address of a local's member or element, cast or offset. Beside them+# what keeps nothing: a member or *out cleared before the return, a static+# local, a pointer's value, the address of what a member points to and an+# object of the function's own.write_escape_samples() {cat >"$1" <<'EOF'struct ctx {@@ -1222,6 +1225,87 @@ int own_object(struct ctx *other)u->entry = entry;return use(u) + use(other);}++struct nest {+ struct ctx in;+ const int *slot[2];+ struct ctx *next;+ struct {+ int n;+ } pair;+};++int kept_nested(struct nest *n)+{+ int entry[2] = {};++ n->in.entry = entry + 1;+ return 0;+}++int kept_slot(struct nest *n, int i)+{+ int v = i;++ n->slot[i] = &v;+ return 0;+}++int kept_out(const int **out)+{+ struct {+ int n;+ } vals[2] = {};++ *out = &vals[1].n;+ return 0;+}++int kept_through(struct nest *n)+{+ int v = 0;++ (*n).next->count = &v;+ return 0;+}++int kept_member(struct ctx *u)+{+ struct nest local = {};++ u->entry = &local.pair.n;+ u->name = (const char *)&local.in;+ return 0;+}++int cleared_out(const int **out)+{+ int v = 0;++ *out = &v;+ use(*out);+ *out = NULL;+ return 0;+}++int pointee(const int **out, struct ctx *u)+{+ struct ctx local = *u;++ *out = local.entry;+ *out = &local.entry[1];+ return 0;+}++int own_nested(struct nest *other)+{+ struct nest n = {};+ int entry[2] = {};++ n.in.entry = entry;+ n.slot[0] = entry;+ return use(&n) + use(other);+}EOF}@@ -1233,11 +1317,12 @@ test_expect_success 'local_escape reports a local left in the caller'"'"'s objec"$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+ for line in 11 19 28 37 89 97 107 115 123 124; dogrep -q "^local_escape: samples.c:$line:" "$W/err" || exit 1done &&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 ]+ grep -q "samples.c:107:.*\*out keeps the address of the local vals past the return" "$W/err" &&+ [ "$(grep -c "^local_escape:" "$W/err")" = 10 ]'# A literal compared or searched under a length written out as a number,diff --git a/testing/coccinelle/local_escape.cocci b/testing/coccinelle/local_escape.cocciindex b87fac5a..7dabfc23 100644--- a/testing/coccinelle/local_escape.cocci+++ b/testing/coccinelle/local_escape.cocci@@ -4,7 +4,9 @@// 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+// what it was told. That holds for a member at any depth, an element and+// *out alike, and for the address of a local's member or element as for+// the local's own. Hand a callback a context of the function's own// instead, or clear the member on every path out.virtual report@@ -27,9 +29,15 @@ def is_static(d, l):decl = lines[at] + " " + declreturn "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))+# The pointer a store goes through: p of p->a.b, p->a[i], (*p).a and *p.+# The pointer itself is a variable of the frame, which dies with it.+def escape(fp, d, pos, lhs, l):+ lhs = re.sub(r"\s+", "", lhs)+ m = re.match(r"^[*(]*(\w+)", lhs)+ if not m or m.group(1) == lhs:+ return+ if (fp[0].file, fp[0].line, m.group(1)) in callers and not is_static(d, l):+ coccilib.report.print_report(pos[0], "%s keeps the address of the local %s past the return" % (lhs, l))// A pointer that came in as a parameter, or was taken from one.@owner@@@ -68,10 +76,11 @@ p << alias.p;@@callers.add((fp[0].file, fp[0].line, p))+// A cast in the pattern also matches the store without one.@array exists@-identifier f, p, fld, l;-type T;-expression E;+identifier f, l, m;+type T, T2;+expression LHS, E, E2;position fp, d, pos;@@f@fp(...)@@ -84,27 +93,30 @@ position fp, d, pos;)... when any(- p->fld@pos = l;+ LHS@pos = (T2)l;+|+ LHS@pos = (T2)&l[...];|- p->fld@pos = &l[...];+ LHS@pos = (T2)&l[...].m;+|+ LHS@pos = l + E2;)- ... when != p->fld = E+ ... when != LHS = E}@script:python depends on report@fp << array.fp;d << array.d;pos << array.pos;-p << array.p;-fld << array.fld;+lhs << array.LHS;l << array.l;@@-escape(fp, d, pos, p, fld, l)+escape(fp, d, pos, lhs, l)@scalar exists@-identifier f, p, fld, l;-type T;-expression E;+identifier f, l, m, m2;+type T, T2;+expression LHS, E;position fp, d, pos;@@f@fp(...)@@ -116,16 +128,21 @@ position fp, d, pos;T l@d = ...;)... when any- p->fld@pos = &l;- ... when != p->fld = E+(+ LHS@pos = (T2)&l;+|+ LHS@pos = (T2)&l.m;+|+ LHS@pos = (T2)&l.m.m2;+)+ ... when != LHS = E}@script:python depends on report@fp << scalar.fp;d << scalar.d;pos << scalar.pos;-p << scalar.p;-fld << scalar.fld;+lhs << scalar.LHS;l << scalar.l;@@-escape(fp, d, pos, p, fld, l)+escape(fp, d, pos, lhs, l)