eBPF-based security platform
.github: reject string copies other than strscpy()
Daniel Borkmannc98805b2d070
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.shindex 821b2b25..47fbcc83 100755--- a/.github/scripts/check-style.sh+++ b/.github/scripts/check-style.sh@@ -6,8 +6,9 @@# mode, over the C sources a commit touches and over its message, the way# cilium checks its BPF tree, plus the block-comment opener that checkpatch# leaves to the netdev trees, the kernel's spelling of fixed-width-# integers and the project's number parsers. The kernel checks that have-# no meaning in userspace are switched off below, each with its reason.+# integers and the project's number parsers and string copies. The+# kernel checks that have no meaning in userspace are switched off below,+# each with its reason.## .github/scripts/check-style.sh [<rev-list arguments>]# .github/scripts/check-style.sh --diff@@ -89,10 +90,12 @@ ignored_types() {# _Static_assert.CAMELCASE# It asks for the kernel-internal u32, which userspace does not have;- # check_int_types below asks for __u32 and its kin instead.+ # check_spellings below asks for __u32 and its kin instead.PREFER_KERNEL_TYPES- # strscpy, fallthrough;, __packed and the other compiler.h macros do- # not exist in userspace; BPF has what bpf_helpers.h defines.+ # check_spellings below asks for the strscpy() of <shirudo/string.h>+ # in tools/ too, which these leave out, and fallthrough;, __packed+ # and the other compiler.h macros do not exist in userspace; BPF has+ # what bpf_helpers.h defines.STRCPYSTRNCPYPREFER_FALLTHROUGH@@ -182,8 +185,10 @@ locate_checkpatch() {# it takes, and util/sign.c is vendored. A call to strtoul() and its kin# or atoi() is reported too, outside the tests: a number is parsed with# parse_u64() and its kin from <shirudo/string.h>, which take a length,-# read nothing past it and refuse a sign, blank or base prefix.-check_int_types() {+# read nothing past it and refuse a sign, blank or base prefix. So is a+# string copied with strcpy() and its kin or with snprintf("%s") on one+# line: strscpy() always terminates and says when it cut.+check_spellings() {awk 'function c_names_kept(f) {return f ~ /^(bpf|upatches)\// || f ~ /\.bpf\.c$/ ||@@ -271,6 +276,17 @@ check_int_types() {fn, NR, file, line, $0bad = 1}+ if (!parsers_kept(file) &&+ (match(code, /(^|[^A-Za-z0-9_])str(n?cpy|n?cat|lcpy)[ \t]*\(/) ||+ (match(code, /(^|[^A-Za-z0-9_])snprintf[ \t]*\([^,]*,[^,]*,[ \t]*""[ \t]*,/) &&+ $0 ~ /"%s"[ \t]*,/))) {+ fn = substr(code, RSTART, RLENGTH)+ sub(/\(.*/, "", fn)+ gsub(/[^A-Za-z0-9_]/, "", fn)+ printf "ERROR:STRING_COPY: copy strings with strscpy() from <shirudo/string.h>, not %s()\n#%d: FILE: %s:%d:\n%s\n\n",+ fn, NR, file, line, $0+ bad = 1+ }}END { exit bad }' "$1"@@ -373,7 +389,7 @@ check_patch() {--quiet --show-types --ignore "$IGNORE" \"$@" "$work/patch") >"$work/out" 2>&1 || rc=1check_block_comments "$work/patch" >>"$work/out" || rc=1- check_int_types "$work/patch" >>"$work/out" || rc=1+ check_spellings "$work/patch" >>"$work/out" || rc=1annotate <"$work/out"return $rc}@@ -441,7 +457,8 @@ list_types() {ignored_types | grep -v '^#' | sort >"$work/ignored"{comm -23 "$work/all" "$work/ignored"- printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING+ printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING \+ STRING_COPY} | sort}diff --git a/AGENTS.md b/AGENTS.mdindex a69d852b..674435ed 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -89,6 +89,11 @@ before the LSM hooks and `attr.keyring_id` is a key serial, not a name.they take a length, read nothing past it and refuse a sign, blank orbase prefix. `check-style.sh` rejects `strtoul()` and its kin and`atoi()` outside `testing/`.+- **Copy strings with `strscpy()`** from `<shirudo/string.h>`: it always+ terminates and returns `-E2BIG` when it cut, which a caller checks+ wherever a cut path or name would mean a different one.+ `check-style.sh` rejects `strcpy()`, `strncpy()` and their kin and a+ `snprintf(dst, size, "%s", src)` copy outside `testing/`.- **Declare locals in reverse-Christmas-tree order** (the netdevconvention): longest declaration line first, shortest last. Groupdeclarations that have no initializer dependency and sort them bydiff --git a/testing/t0043-check-style.sh b/testing/t0043-check-style.shindex 1358f849..6a3b3440 100644--- a/testing/t0043-check-style.sh+++ b/testing/t0043-check-style.sh@@ -6,8 +6,9 @@# enforces on the C a commit touches at 100 columns, the block-comment# opener and the subject length the wrapper adds, the message of a commit# that touches no C, the sources it leaves alone, the userspace spellings-# it lets through, the C library's integer names and number parsers it-# refuses and the list of what stays enabled, on a throwaway repository.+# it lets through, the C library's integer names, number parsers and+# string copies it refuses and the list of what stays enabled, on a+# throwaway repository.cd "$(dirname "$0")". ./test-lib.sh@@ -163,7 +164,7 @@ test_expect_success 'the userspace spellings checkpatch would reject pass' 'put user.c "// SPDX-License-Identifier: BSD-2-Clause" "" \"#include <string.h>" "" "#include <shirudo/misc.h>" "" \"__u32 user(X509 *c, char *dst, const char *src)" "{" \- " gzFile gz = NULL;" "" " (void)gz;" " strcpy(dst, src);" \+ " gzFile gz = NULL;" "" " (void)gz;" " strscpy(dst, src, 8);" \" return (__u32)ASN1_INTEGER_get(X509_get_serialNumber(c));" "}" &&commit "a: userspace@@ -182,6 +183,7 @@ test_expect_success '--types lists what stays enabled' 'grep -qx "BLOCK_COMMENT_STYLE" "$W/out" &&grep -qx "KERNEL_INT_TYPES" "$W/out" &&grep -qx "NUMBER_PARSING" "$W/out" &&+ grep -qx "STRING_COPY" "$W/out" &&! grep -qx "CAMELCASE" "$W/out" &&! grep -qx "GIT_COMMIT_ID" "$W/out"'@@ -216,6 +218,27 @@ test_expect_success 'the C library'"'"'s number parsers are refused outside therm -r "$R/testing"'+test_expect_success 'strings are copied with strscpy() outside the tests' '+ printf "%s\n" "/* strcpy() in a comment */" \+ "static const char *op = \"strncpy(a, b, c)\";" "" \+ "void copy(char *d, const char *s)" "{" " strcpy(d, s);" \+ " snprintf(d, 8, \"%s\", s);" " snprintf(d, 8, \"%d\", 1);" "}" \+ >>"$R/good.c" &&+ ! check --diff &&+ grep -q "^ERROR:STRING_COPY: copy strings with strscpy() from <shirudo/string.h>, not strcpy()" "$W/out" &&+ grep -q "^ERROR:STRING_COPY: copy strings with strscpy() from <shirudo/string.h>, not snprintf()" "$W/out" &&+ [ "$(grep -c "^ERROR:STRING_COPY" "$W/out")" = 2 ] &&+ git -C "$R" checkout -q -- good.c &&+ put tools/x/name.c "// SPDX-License-Identifier: BSD-2-Clause" "" \+ "void name(char *d, const char *s)" "{" " strncpy(d, s, 8);" "}" &&+ ! check --files tools/x/name.c &&+ grep -q "^ERROR:STRING_COPY: copy strings with strscpy() from <shirudo/string.h>, not strncpy()" "$W/out" &&+ put testing/helper/name.c "// SPDX-License-Identifier: BSD-2-Clause" "" \+ "void name(char *d, const char *s)" "{" " strcpy(d, s);" "}" &&+ check --files testing/helper/name.c &&+ rm -r "$R/tools" "$R/testing"+'+test_expect_success 'a BPF program keeps the kernel'"'"'s argument types' 'put hook.bpf.c "// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause" "" \"int hook(unsigned long arg, unsigned short port, unsigned char c)" \