Skip to content

eBPF-based security platform

coccinelle: refuse sizeof of a pointer wherever it is taken

Daniel Borkmann9af3d2b181e0
diff --git a/.github/scripts/check-coccinelle.sh b/.github/scripts/check-coccinelle.sh
index 21671eb2..599f724a 100755
--- a/.github/scripts/check-coccinelle.sh
+++ b/.github/scripts/check-coccinelle.sh
@@ -45,9 +45,16 @@ for rule in "$@"; do
*) rule=testing/coccinelle/$rule.cocci ;;
esac
name=$(basename "$rule" .cocci)
+ # Without the headers a file includes the run is fast, but the types
+ # they declare are unknown. A rule that needs them names the include
+ # options on a #spatch line, which --no-includes would override.
+ includes=--no-includes
+ if grep -q '^#spatch' "$rule"; then
+ includes=
+ fi
# Each file on its own, so that one Coccinelle cannot parse costs only
# that file, and in parallel.
- xargs -P "$jobs" -n 16 spatch -D report --very-quiet --no-includes \
+ xargs -P "$jobs" -n 16 spatch -D report --very-quiet $includes \
--include-headers --sp-file "$rule" <"$work/files" \
>"$work/$name.out" 2>"$work/$name.err" || {
cat "$work/$name.err" >&2
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 11d752fc..06ee052d 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -543,7 +543,9 @@ can rewrite what it finds does so with ``-D patch``:
- A NULL passed to a memory or string function, and a
``memcpy()`` of overlapping buffers.
* - ``sizeof_ptr``
- - The size of a pointer where the object it points to is meant.
+ - The size of a pointer where the object it points to is meant,
+ wherever it is taken, unless the pointer's own address goes to
+ the same call.
* - ``literal_len``
- A length that does not match the string literal it compares or
copies.
@@ -587,6 +589,10 @@ can rewrite what it finds does so with ``-D patch``:
A new pattern worth refusing gets a rule: write it from scratch under
the BSD-2-Clause, with a header that says why the pattern is refused,
fix the tree in the commits before it, and add it to the table.
+``check-coccinelle.sh`` runs a rule without the headers a file
+includes, which keeps it fast but leaves the types they declare
+unknown. A rule that needs them, as ``sizeof_ptr`` needs the type of a
+struct member, names its include options on a ``#spatch`` line.
Next steps
----------
diff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.sh
index 1b11cf28..9aeab9b4 100755
--- a/testing/ci-checks/t0006-check-coccinelle.sh
+++ b/testing/ci-checks/t0006-check-coccinelle.sh
@@ -4,8 +4,9 @@
#
# t0006 -- .github/scripts/check-coccinelle.sh: a rule that matches fails the
# check with file, line, rule and message, one that does not passes it,
-# a rule that can rewrite does with -D patch, on a throwaway repository,
-# and the tree itself.
+# a rule that can rewrite does with -D patch, a rule reads the headers
+# its #spatch line asks for, on a throwaway repository, and the tree
+# itself.
cd "$(dirname "$0")/.."
. ./test-lib.sh
@@ -45,6 +46,16 @@ test_expect_success 'a rule is named alone or by path' '
! check testing/coccinelle/zero_init.cocci
'
+test_expect_success 'a rule reads the headers its #spatch line asks for' '
+ cp "$ROOT/testing/coccinelle/sizeof_ptr.cocci" "$R/testing/coccinelle/" &&
+ mkdir -p "$R/include" &&
+ printf "struct foo {\n\tchar *name;\n};\n" >"$R/include/foo.h" &&
+ printf "#include \"foo.h\"\n\nvoid f(struct foo *x)\n{\n\tmemset(x->name, 0, sizeof(x->name));\n}\n" >"$R/member.c" &&
+ ! check sizeof_ptr &&
+ grep -q "^sizeof_ptr: member.c:5:" "$W/err" &&
+ rm -r "$R/testing/coccinelle/sizeof_ptr.cocci" "$R/include" "$R/member.c"
+'
+
test_expect_success 'a rule rewrites what it finds with -D patch' '
(cd "$R" && spatch -D patch --very-quiet --in-place \
--sp-file testing/coccinelle/zero_init.cocci bad.c) &&
diff --git a/testing/coccinelle/sizeof_ptr.cocci b/testing/coccinelle/sizeof_ptr.cocci
index ff42e87e..1106d219 100644
--- a/testing/coccinelle/sizeof_ptr.cocci
+++ b/testing/coccinelle/sizeof_ptr.cocci
@@ -2,24 +2,40 @@
// Copyright (c) 2026 Authors of shirudo
//
// sizeof a pointer where the object it points to was meant: what is
-// copied, cleared, compared, scrubbed or allocated is 8 bytes.
+// copied, cleared, compared, scrubbed, allocated or counted is the 8
+// bytes of the pointer. The pointer is the object only where its own
+// address goes to the same call, as in memcpy(&p, src, sizeof(p)). The
+// type of a struct member comes from the header that declares it, so
+// this rule reads the headers a file includes.
+
+#spatch --include-headers-for-types --all-includes -I include -I util -I .
virtual report
-@r@
+@own@
type T;
-idexpression T *P;
-identifier f = {memcpy, memmove, memset, memcmp, memzero_explicit,
- memneq_explicit, xfree, xmalloc, read, write, pread, pwrite,
- strscpy, strsprintf};
+T *P;
+identifier f;
position p;
@@
(
- f(..., sizeof@p(P), ...)
+ f(..., &P, ..., sizeof@p(P), ...)
+|
+ f(..., sizeof@p(P), ..., &P, ...)
+)
+
+@r@
+type T;
+T *P;
+expression E, I;
+position p != own.p;
+@@
+(
+ sizeof(*E)
|
- f(..., sizeof@p(P) * ..., ...)
+ sizeof(E[I])
|
- xcalloc(..., sizeof@p(P))
+ sizeof@p(P)
|
sizeof@p(sizeof(...))
)