eBPF-based security platform
coccinelle: refuse a block's local kept in a pointer read after the block
Daniel Borkmann1479faea03e2
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 27eb1272..78ebc85e 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -742,7 +742,9 @@ can rewrite what it finds does so with ``-D patch``:- The address of a local array or variable, or of a member or elementof one, stored in an object the caller passed in, in a member atany depth, an element or ``*out``, and left there on a path out,- where it points into a dead frame.+ where it points into a dead frame; and the address of a local of a+ block left in a pointer declared outside it and read after the+ block ends.* - ``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 0fbdbd17..b3314f6e 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -67,7 +67,8 @@ signed config image.hand in a macro and an ``ARRAY_SIZE()`` of a file's own for``array_size``, which rewrites the one and leaves the other, alocal's address, or that of its member or element, left in the- caller's object at any depth or in ``*out`` beside the forms+ caller's object at any depth or in ``*out``, and a block's local+ left in a pointer read after the block, 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 66e805fe..6dd8b73d 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -11,7 +11,8 @@# 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, or that of its member or element,-# left in the caller's object at any depth or in *out,+# left in the caller's object at any depth or in *out, and a block's+# local left in a pointer read after the block,# 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@@ -1145,10 +1146,12 @@ 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, 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.+# address of a local's member or element, cast or offset, and a pointer+# from outside a loop's or a branch's block left at a local of the block+# and read after it. 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, an object of the function's own,+# and a pointer cleared in its block or set again before it is read.write_escape_samples() {cat >"$1" <<'EOF'struct ctx {@@ -1306,6 +1309,90 @@ int own_nested(struct nest *other)n.slot[0] = entry;return use(&n) + use(other);}++int kept_past_loop(int n)+{+ const char *name = "none";+ int i;++ for (i = 0; i < n; i++) {+ char buf[16];++ buf[0] = 'a';+ name = buf;+ }+ return use(name);+}++int kept_past_branch(int n)+{+ const int *p = NULL;+ const char *argv[2] = {};++ if (n) {+ int v = n;+ char arg[4] = "-v";++ p = &v;+ argv[0] = arg;+ }+ return p ? *p + use(argv[0]) : 0;+}++int kept_past_while(int n)+{+ const int *slots[2] = {};+ const char *name = "none";++ while (n--) {+ int v = n;+ char buf[8] = "ab";++ slots[0] = &v;+ name = &buf[1];+ }+ return use(name) + *slots[0];+}++int used_in_block(int n)+{+ const char *name = "none";++ if (n) {+ char buf[16] = "x";++ name = buf;+ use(name);+ name = "none";+ }+ return use(name);+}++int reassigned_past_block(int n)+{+ const char *name = "none";++ if (n) {+ char buf[16] = "x";++ name = buf;+ use(name);+ }+ name = "none";+ return use(name);+}++int static_in_block(int n)+{+ const char *name = "none";++ if (n) {+ static char buf[16];++ name = buf;+ }+ return use(name);+}EOF}@@ -1317,12 +1404,13 @@ 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 89 97 107 115 123 124; do+ for line in 11 19 28 37 89 97 107 115 123 124 166 180 181 195 196; 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 -q "samples.c:107:.*\*out keeps the address of the local vals past the return" "$W/err" &&- [ "$(grep -c "^local_escape:" "$W/err")" = 10 ]+ grep -q "samples.c:166:.*name keeps the address of the local buf past its block" "$W/err" &&+ [ "$(grep -c "^local_escape:" "$W/err")" = 15 ]'# 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 7dabfc23..38696082 100644--- a/testing/coccinelle/local_escape.cocci+++ b/testing/coccinelle/local_escape.cocci@@ -7,7 +7,9 @@// 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.+// instead, or clear the member on every path out. A pointer declared+// outside a block that keeps the address of a local of the block points+// at storage just as dead once the block ends: read it in the block only.virtual report@@ -29,6 +31,14 @@ def is_static(d, l):decl = lines[at] + " " + declreturn "static" in decl.split()+def is_array(d, l):+ with open(d[0].file) as f:+ line = f.read().splitlines()[int(d[0].line) - 1]+ return re.search(r"\b%s\s*\[" % re.escape(l), line) is not None++def past_block(pos, q, l):+ coccilib.report.print_report(pos[0], "%s keeps the address of the local %s past its block" % (q, 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):@@ -146,3 +156,77 @@ lhs << scalar.LHS;l << scalar.l;@@escape(fp, d, pos, lhs, l)++// A pointer declared outside a block, pointed at a local of the block and+// read once the block has ended, on its last pass or after a jump out.+@block exists@+identifier l, q;+type T, T1;+expression E, E0, E2;+position d, pos, pv, u;+@@+(+ T1 *q;+|+ T1 *q = E0;+|+ T1 *q[...];+|+ T1 *q[...] = E0;+)+ ... when any+ {+ ... when any+(+ T l@d[...];+|+ T l@d[...] = ...;+|+ T l@d;+|+ T l@d = ...;+)+ ... when any+(+ q@pos = &l;+|+ q[...]@pos = &l;+|+ q@pv = l;+|+ q@pv = &l[...];+|+ q[...]@pv = l;+)+ ... when != q = E+ }+ ... when != q = E+(+ q = E2+|+ q[...] = E2+|+ q@u+)++// u is bound only where q is read after the block, not set again.+@script:python depends on report@+d << block.d;+pos << block.pos;+u << block.u;+q << block.q;+l << block.l;+@@+if not is_static(d, l):+ past_block(pos, q, l)++// Only an array decays to its address.+@script:python depends on report@+d << block.d;+pv << block.pv;+u << block.u;+q << block.q;+l << block.l;+@@+if is_array(d, l) and not is_static(d, l):+ past_block(pv, q, l)