eBPF-based security platform
coccinelle: declare SEC an attribute in every rule
Daniel Borkmanne908e9beb4c2
diff --git a/.agents/skills/bpf-coccinelle/SKILL.md b/.agents/skills/bpf-coccinelle/SKILL.mdindex 3d7f9ab2..770496bd 100644--- a/.agents/skills/bpf-coccinelle/SKILL.md+++ b/.agents/skills/bpf-coccinelle/SKILL.md@@ -79,9 +79,10 @@ branch; read GPL-only collections for ideas, never copy them. Then:says what the pattern breaks and on which kernels, as the others do,then `#spatch --no-includes --macro-file testing/coccinelle/bpf.macros`and `virtual report`.-- Put `attribute name SEC;` in the first rule. Without it Coccinelle- takes a map defined as `} name SEC(".maps");` for a typedef and skips- every function that uses `&name`.+- Put `attribute name SEC;` in the first rule, as every rule of the+ tree does. Without it Coccinelle takes a map defined as+ `} name SEC(".maps");` for a typedef, and no pattern matches a call+ that passes `&name`.- Add a sample to `write_bpf_samples()` in`testing/ci-checks/t0006-check-coccinelle.sh` that the rule must reportand a safe form it must leave alone, and add the rule to `BPF_RULES`.diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 566d3a43..ee346dc5 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -799,18 +799,20 @@ run long names ``--timeout <seconds>`` there, as ``dead_store`` does: thecheck then runs it one file at a time and names each file spatch gave upon, rather than failing.-The ``bpf_`` rules read the BPF sources through-:src:`testing/coccinelle/bpf.macros`, which tells Coccinelle what-``SEC()``, ``BPF_PROG()`` and the map definition macros mean, and each-declares ``SEC`` an attribute. Without that, Coccinelle takes a map-defined as ``} name SEC(".maps");`` for a type and skips every function-that takes its address, without an error. A file that defines its own-token-pasting macros, as :src:`upatches/kernel-obs/obs_table.h` does,-stays out of their reach. Each matches a hazard that exists on every-kernel. Per-CPU memory used as scratch is a hazard only under some-preemption models and program types, which a rule cannot tell, so no-rule refuses it. :doc:`/upatches/write/memory-consistency/hazards`-explains each hazard and where it applies.+Every rule declares ``SEC`` an attribute, as the rules run over the BPF+sources too. Without that, Coccinelle takes a map defined as+``} name SEC(".maps");`` for a type, and no pattern matches a call that+takes the map's address, without an error; ``t0006`` checks that each+rule declares it. The ``bpf_`` rules also read the BPF sources+through :src:`testing/coccinelle/bpf.macros`, which tells Coccinelle what+``SEC()``, ``BPF_PROG()`` and the map definition macros mean. A file that+defines its own token-pasting macros, as+:src:`upatches/kernel-obs/obs_table.h` does, stays out of their reach.+Each matches a hazard that exists on every kernel. Per-CPU memory used+as scratch is a hazard only under some preemption models and program+types, which a rule cannot tell, so no rule refuses it.+:doc:`/upatches/write/memory-consistency/hazards` explains each hazard+and where it applies.Next steps----------diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex a33f0dbf..e50c6ec5 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -60,7 +60,8 @@ signed config image.``dead_store`` leaves alone, a count by hand in a macro and an``ARRAY_SIZE()`` of a file's own for ``array_size``, whichrewrites the one and leaves the other, a file a rule gives up on- at its time limit named rather than failed, and the tree itself+ 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 a653e88f..cf28a972 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -9,7 +9,8 @@# from CERT C, each BPF hazard rule, each scrub rule and dead_store report# a sample of what they refuse, 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, and the tree itself.+# its time limit is named rather than failed, every rule declares SEC an+# attribute, and the tree itself.cd "$(dirname "$0")/..". ./test-lib.sh@@ -717,6 +718,12 @@ test_expect_success 'a file a rule gives up on is named, not failed' 'grep -q "dead_store: spatch failed" "$W/err"'+test_expect_success 'every rule declares SEC an attribute' '+ for r in "$ROOT"/testing/coccinelle/*.cocci; do+ grep -q "^attribute name SEC;$" "$r" || exit 1+ done+'+test_expect_success 'the tree passes' '(cd "$ROOT" && sh "$S") >"$W/out" 2>"$W/err" &&grep -q "rules, no match" "$W/out"diff --git a/testing/coccinelle/alloc_size.cocci b/testing/coccinelle/alloc_size.cocciindex d2adfcfd..be95fd41 100644--- a/testing/coccinelle/alloc_size.cocci+++ b/testing/coccinelle/alloc_size.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;type T1, T2;T1 *P;position p;diff --git a/testing/coccinelle/alloc_unchecked.cocci b/testing/coccinelle/alloc_unchecked.cocciindex 4208f24a..6bf06876 100644--- a/testing/coccinelle/alloc_unchecked.cocci+++ b/testing/coccinelle/alloc_unchecked.cocci@@ -7,6 +7,7 @@virtual report@r exists@+attribute name SEC;expression E, E1;identifier fld;identifier alloc = {xmalloc, xcalloc, xreallocarray, xstrdup, xstrndup,diff --git a/testing/coccinelle/anssi_misc.cocci b/testing/coccinelle/anssi_misc.cocciindex 09a10524..1cb49fd3 100644--- a/testing/coccinelle/anssi_misc.cocci+++ b/testing/coccinelle/anssi_misc.cocci@@ -9,6 +9,7 @@virtual report@bitbool@+attribute name SEC;bool A;expression B;binary operator op = {&, |, ^};diff --git a/testing/coccinelle/array_size.cocci b/testing/coccinelle/array_size.cocciindex 28c74f41..25280004 100644--- a/testing/coccinelle/array_size.cocci+++ b/testing/coccinelle/array_size.cocci@@ -9,6 +9,7 @@ virtual patchvirtual report@r@+attribute name SEC;expression E;position p;@@diff --git a/testing/coccinelle/banned.cocci b/testing/coccinelle/banned.cocciindex 95b5689b..228a1fab 100644--- a/testing/coccinelle/banned.cocci+++ b/testing/coccinelle/banned.cocci@@ -12,6 +12,7 @@virtual report@r@+attribute name SEC;identifier f = {rand, srand, random, srandom, rand_r, drand48, lrand48,mrand48, strtok, gets, tmpnam, tempnam, mktemp, vfork, alloca,setjmp, longjmp, sigsetjmp, siglongjmp, usleep, signal,diff --git a/testing/coccinelle/cert_misc.cocci b/testing/coccinelle/cert_misc.cocciindex 1c4e1af6..67c1b3a4 100644--- a/testing/coccinelle/cert_misc.cocci+++ b/testing/coccinelle/cert_misc.cocci@@ -9,6 +9,7 @@virtual report@strbound@+attribute name SEC;identifier x;constant C;constant char[] S;diff --git a/testing/coccinelle/cloexec.cocci b/testing/coccinelle/cloexec.cocciindex 30491fc8..6c2234fe 100644--- a/testing/coccinelle/cloexec.cocci+++ b/testing/coccinelle/cloexec.cocci@@ -11,6 +11,7 @@ virtual reportclosing = set()@ok@+attribute name SEC;expression A, B, C, D;position p;@@diff --git a/testing/coccinelle/ctype.cocci b/testing/coccinelle/ctype.cocciindex ec04b32f..2ecdcbf3 100644--- a/testing/coccinelle/ctype.cocci+++ b/testing/coccinelle/ctype.cocci@@ -8,6 +8,7 @@ virtual patchvirtual report@r@+attribute name SEC;identifier f = {isalnum, isalpha, isascii, isblank, iscntrl, isdigit,isgraph, islower, isprint, ispunct, isspace, isupper,isxdigit, tolower, toupper};diff --git a/testing/coccinelle/curl_slist_free.cocci b/testing/coccinelle/curl_slist_free.cocciindex 260464e3..8124bf05 100644--- a/testing/coccinelle/curl_slist_free.cocci+++ b/testing/coccinelle/curl_slist_free.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;expression E;position p;@@diff --git a/testing/coccinelle/dead_store.cocci b/testing/coccinelle/dead_store.cocciindex dea9d384..ea274330 100644--- a/testing/coccinelle/dead_store.cocci+++ b/testing/coccinelle/dead_store.cocci@@ -13,6 +13,7 @@virtual report@r@+attribute name SEC;local idexpression x;expression E1, E2;position p1, p2;diff --git a/testing/coccinelle/double_close.cocci b/testing/coccinelle/double_close.cocciindex 01d19c87..290b66d6 100644--- a/testing/coccinelle/double_close.cocci+++ b/testing/coccinelle/double_close.cocci@@ -8,6 +8,7 @@virtual report@r exists@+attribute name SEC;expression fd, E;identifier f;position p1, p2;diff --git a/testing/coccinelle/double_init.cocci b/testing/coccinelle/double_init.cocciindex a21c8dfe..c380dd31 100644--- a/testing/coccinelle/double_init.cocci+++ b/testing/coccinelle/double_init.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;identifier f, x;type T;expression E1, E2;diff --git a/testing/coccinelle/double_test.cocci b/testing/coccinelle/double_test.cocciindex 52044f1a..3e9783f0 100644--- a/testing/coccinelle/double_test.cocci+++ b/testing/coccinelle/double_test.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;expression E;position p;@@diff --git a/testing/coccinelle/errno_clobber.cocci b/testing/coccinelle/errno_clobber.cocciindex aad9a78c..245487fe 100644--- a/testing/coccinelle/errno_clobber.cocci+++ b/testing/coccinelle/errno_clobber.cocci@@ -11,6 +11,7 @@virtual report@r exists@+attribute name SEC;identifier g = {close, closedir, fclose, unlink, unlinkat, rmdir, munmap,umount, umount2, rename, renameat, fsync, fflush, flock,ftruncate, fchmod, chmod, write, syslog, fprintf, printf,diff --git a/testing/coccinelle/fclose_unchecked.cocci b/testing/coccinelle/fclose_unchecked.cocciindex b563b38e..b42f2667 100644--- a/testing/coccinelle/fclose_unchecked.cocci+++ b/testing/coccinelle/fclose_unchecked.cocci@@ -8,6 +8,7 @@virtual report@r exists@+attribute name SEC;expression F, A, M;identifier o = {fopen, fdopen};identifier f;diff --git a/testing/coccinelle/fd_leak.cocci b/testing/coccinelle/fd_leak.cocciindex 3e3c63f7..79d98904 100644--- a/testing/coccinelle/fd_leak.cocci+++ b/testing/coccinelle/fd_leak.cocci@@ -16,6 +16,7 @@ def leak(p1, p2):coccilib.report.print_report(p2[0], "returns without closing the descriptor opened on line %s" % p1[0].line)@r exists@+attribute name SEC;local idexpression int fd;expression E, E1, E2;statement S, S1;diff --git a/testing/coccinelle/ifnullfree.cocci b/testing/coccinelle/ifnullfree.cocciindex 18fa254d..29214c02 100644--- a/testing/coccinelle/ifnullfree.cocci+++ b/testing/coccinelle/ifnullfree.cocci@@ -7,6 +7,7 @@ virtual patchvirtual report@r depends on report@+attribute name SEC;expression E, S;position p;@@diff --git a/testing/coccinelle/literal_len.cocci b/testing/coccinelle/literal_len.cocciindex 1a300bcb..e1772362 100644--- a/testing/coccinelle/literal_len.cocci+++ b/testing/coccinelle/literal_len.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;expression E;constant char[] S;constant N;diff --git a/testing/coccinelle/memnull.cocci b/testing/coccinelle/memnull.cocciindex b6fbe48d..2d662436 100644--- a/testing/coccinelle/memnull.cocci+++ b/testing/coccinelle/memnull.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;identifier f = {memcpy, memmove, memset, memcmp, memchr, strlen, strnlen,strcmp, strncmp, strchr, strrchr, strstr, strscpy,memzero_explicit, memneq_explicit};diff --git a/testing/coccinelle/minmax.cocci b/testing/coccinelle/minmax.cocciindex 2b36d3f9..3fab3727 100644--- a/testing/coccinelle/minmax.cocci+++ b/testing/coccinelle/minmax.cocci@@ -9,6 +9,7 @@ virtual patchvirtual report@r depends on report@+attribute name SEC;expression A, B;position p;@@diff --git a/testing/coccinelle/minmax_cast.cocci b/testing/coccinelle/minmax_cast.cocciindex b128aa61..4dba7edb 100644--- a/testing/coccinelle/minmax_cast.cocci+++ b/testing/coccinelle/minmax_cast.cocci@@ -7,6 +7,7 @@virtual report@r disable drop_cast@+attribute name SEC;type T;expression A, B;identifier f = {min, max};diff --git a/testing/coccinelle/minmax_t.cocci b/testing/coccinelle/minmax_t.cocciindex d4bce2d1..c5468990 100644--- a/testing/coccinelle/minmax_t.cocci+++ b/testing/coccinelle/minmax_t.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;type T;T A, B;constant C;diff --git a/testing/coccinelle/no_exec.cocci b/testing/coccinelle/no_exec.cocciindex 8044a216..7536329f 100644--- a/testing/coccinelle/no_exec.cocci+++ b/testing/coccinelle/no_exec.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;identifier f = {fork, vfork, clone, clone3, execv, execve, execvp, execvpe,execl, execle, execlp, fexecve, execveat, system, popen,posix_spawn, posix_spawnp};diff --git a/testing/coccinelle/null_deref.cocci b/testing/coccinelle/null_deref.cocciindex 2e7b8a56..bdd097d7 100644--- a/testing/coccinelle/null_deref.cocci+++ b/testing/coccinelle/null_deref.cocci@@ -9,6 +9,7 @@virtual report@unevaluated@+attribute name SEC;expression E;identifier fld;position p;diff --git a/testing/coccinelle/openssl_ret.cocci b/testing/coccinelle/openssl_ret.cocciindex d7d6d119..eae5cbc8 100644--- a/testing/coccinelle/openssl_ret.cocci+++ b/testing/coccinelle/openssl_ret.cocci@@ -12,6 +12,7 @@ virtual reportcompared = set()@ok@+attribute name SEC;identifier f = {EVP_PKEY_verify, EVP_PKEY_verify_init, EVP_PKEY_verify_recover,EVP_PKEY_sign, EVP_PKEY_sign_init, EVP_PKEY_derive,EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer,diff --git a/testing/coccinelle/option_by_hand.cocci b/testing/coccinelle/option_by_hand.cocciindex 0d94ac9c..355190af 100644--- a/testing/coccinelle/option_by_hand.cocci+++ b/testing/coccinelle/option_by_hand.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;expression E, F;constant char[] S;identifier f = {strcmp, strncmp, strcasecmp, strncasecmp, str_has_prefix,diff --git a/testing/coccinelle/overflow.cocci b/testing/coccinelle/overflow.cocciindex 367256e3..c1e4eed7 100644--- a/testing/coccinelle/overflow.cocci+++ b/testing/coccinelle/overflow.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;expression A, B, C;identifier M = {SIZE_MAX, UINT64_MAX, UINT32_MAX, UINT16_MAX, UINT8_MAX,INT64_MAX, INT32_MAX, INT_MAX, LONG_MAX, SSIZE_MAX, UINT_MAX,diff --git a/testing/coccinelle/positive_errno.cocci b/testing/coccinelle/positive_errno.cocciindex cfc9c47d..d899c0a8 100644--- a/testing/coccinelle/positive_errno.cocci+++ b/testing/coccinelle/positive_errno.cocci@@ -9,6 +9,7 @@virtual report@r@+attribute name SEC;identifier E = {E2BIG, EACCES, EADDRINUSE, EADDRNOTAVAIL, EAFNOSUPPORT, EAGAIN,EALREADY, EBADF, EBADMSG, EBUSY, ECANCELED, ECHILD, ECONNABORTED,ECONNREFUSED, ECONNRESET, EDEADLK, EDESTADDRREQ, EDOM, EDQUOT,diff --git a/testing/coccinelle/scaled_arith.cocci b/testing/coccinelle/scaled_arith.cocciindex 3cbe497c..dd731f78 100644--- a/testing/coccinelle/scaled_arith.cocci+++ b/testing/coccinelle/scaled_arith.cocci@@ -8,6 +8,7 @@virtual report@r@+attribute name SEC;type T, T2;T *ptr;expression E;diff --git a/testing/coccinelle/scrub.cocci b/testing/coccinelle/scrub.cocciindex e3202a31..6a6dce32 100644--- a/testing/coccinelle/scrub.cocci+++ b/testing/coccinelle/scrub.cocci@@ -8,6 +8,7 @@ virtual patchvirtual report@r@+attribute name SEC;identifier x;expression C, L;position p;diff --git a/testing/coccinelle/scrub_missing.cocci b/testing/coccinelle/scrub_missing.cocciindex 6324e931..1dd8cfd1 100644--- a/testing/coccinelle/scrub_missing.cocci+++ b/testing/coccinelle/scrub_missing.cocci@@ -24,6 +24,7 @@ def unscrubbed(x, p1, p2):coccilib.report.print_report(p2[0], "returns without memzero_explicit() of %s, which line %s filled with a secret" % (x, p1[0].line))@r exists@+attribute name SEC;identifier x, fld;type T;expression C, E, E1;diff --git a/testing/coccinelle/secret_cmp.cocci b/testing/coccinelle/secret_cmp.cocciindex 4c9686a5..06a4c93c 100644--- a/testing/coccinelle/secret_cmp.cocci+++ b/testing/coccinelle/secret_cmp.cocci@@ -7,6 +7,7 @@virtual report@r@+attribute name SEC;expression A, B, N;position p;@@diff --git a/testing/coccinelle/sensitive_scrub.cocci b/testing/coccinelle/sensitive_scrub.cocciindex 31f05b51..269d58a7 100644--- a/testing/coccinelle/sensitive_scrub.cocci+++ b/testing/coccinelle/sensitive_scrub.cocci@@ -54,6 +54,7 @@ def declared_with_cleanup(p):// Keep scrub references out of the use positions below.@m@+attribute name SEC;identifier x;position pm;@@diff --git a/testing/coccinelle/sizeof_ptr.cocci b/testing/coccinelle/sizeof_ptr.cocciindex 1106d219..67afdd5e 100644--- a/testing/coccinelle/sizeof_ptr.cocci+++ b/testing/coccinelle/sizeof_ptr.cocci@@ -13,6 +13,7 @@virtual report@own@+attribute name SEC;type T;T *P;identifier f;diff --git a/testing/coccinelle/test_after_use.cocci b/testing/coccinelle/test_after_use.cocciindex 06b80391..132a3992 100644--- a/testing/coccinelle/test_after_use.cocci+++ b/testing/coccinelle/test_after_use.cocci@@ -11,6 +11,7 @@ virtual reportunevaluated = set()@ufld@+attribute name SEC;expression E, B;identifier fld;position p;diff --git a/testing/coccinelle/uninit.cocci b/testing/coccinelle/uninit.cocciindex 804936f0..e52d83f9 100644--- a/testing/coccinelle/uninit.cocci+++ b/testing/coccinelle/uninit.cocci@@ -9,6 +9,7 @@virtual report@r disable decl_init exists@+attribute name SEC;type T;identifier x, l;expression E;diff --git a/testing/coccinelle/use_after_free.cocci b/testing/coccinelle/use_after_free.cocciindex 642cf7e2..d1799337 100644--- a/testing/coccinelle/use_after_free.cocci+++ b/testing/coccinelle/use_after_free.cocci@@ -6,6 +6,7 @@virtual report@r exists@+attribute name SEC;expression E, E1;identifier fr = {xfree, xstrfree, libc_free, X509_free, EVP_PKEY_free,BIO_free, BN_free, CMS_ContentInfo_free, btf__free,diff --git a/testing/coccinelle/va_end.cocci b/testing/coccinelle/va_end.cocciindex 7bae3d67..5364f829 100644--- a/testing/coccinelle/va_end.cocci+++ b/testing/coccinelle/va_end.cocci@@ -7,6 +7,7 @@virtual report@r exists@+attribute name SEC;expression A, B;position p;@@diff --git a/testing/coccinelle/void_cast.cocci b/testing/coccinelle/void_cast.cocciindex 71231a9e..4c0a37fe 100644--- a/testing/coccinelle/void_cast.cocci+++ b/testing/coccinelle/void_cast.cocci@@ -10,6 +10,7 @@ virtual patchvirtual report@r depends on report@+attribute name SEC;expression E;position p;@@diff --git a/testing/coccinelle/wrong_test.cocci b/testing/coccinelle/wrong_test.cocciindex 38582956..da7532bf 100644--- a/testing/coccinelle/wrong_test.cocci+++ b/testing/coccinelle/wrong_test.cocci@@ -8,6 +8,7 @@virtual report@r disable is_null, isnt_null1@+attribute name SEC;identifier x, y, f;expression list es;position p;diff --git a/testing/coccinelle/zero_init.cocci b/testing/coccinelle/zero_init.cocciindex 6cfdec4a..07fc2ef9 100644--- a/testing/coccinelle/zero_init.cocci+++ b/testing/coccinelle/zero_init.cocci@@ -7,6 +7,7 @@ virtual patchvirtual report@r depends on report@+attribute name SEC;type T;identifier x;position p;