eBPF-based security platform
.github: reject the printf family other than strsprintf()
Daniel Borkmann265bd63df81d
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.shindex c2191e39..acdc92a5 100755--- a/.github/scripts/check-style.sh+++ b/.github/scripts/check-style.sh@@ -6,9 +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 and string copies. The-# kernel checks that have no meaning in userspace are switched off below,-# each with its reason.+# integers and the project's number parsers, string copies and formatting.+# 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@@ -188,7 +188,11 @@ locate_checkpatch() {# its kin from <shirudo/string.h>, which take a length, 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.+# terminates and says when it cut. Any other call to sprintf(),+# snprintf() or their va_list forms is reported outside the tests and+# the two files that implement strsprintf() and xasprintf() with them:+# strsprintf() and vstrsprintf() refuse output that does not fit rather+# than cut it.check_spellings() {awk 'function c_names_kept(f) {@@ -199,6 +203,10 @@ check_spellings() {function parsers_kept(f) {return f ~ /^(bpf|upatches|testing)\// || f ~ /\.bpf\.c$/}+ function formatters_kept(f) {+ return parsers_kept(f) || f == "util/string.c" ||+ f == "util/malloc.c"+ }/^\+\+\+ / {file = $0sub(/^\+\+\+ b\//, "", file)@@ -289,6 +297,13 @@ check_spellings() {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, $0bad = 1+ } else if (!formatters_kept(file) &&+ match(code, /(^|[^A-Za-z0-9_])v?sn?printf[ \t]*\(/)) {+ fn = substr(code, RSTART, RLENGTH)+ gsub(/[^A-Za-z0-9_]/, "", fn)+ printf "ERROR:STRING_FORMAT: format into a buffer with strsprintf() or vstrsprintf() 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 }@@ -461,7 +476,7 @@ list_types() {{LC_ALL=C comm -23 "$work/all" "$work/ignored"printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING \- STRING_COPY+ STRING_COPY STRING_FORMAT} | LC_ALL=C sort}diff --git a/testing/t0043-check-style.sh b/testing/t0043-check-style.shindex 46769fbb..bf9950d7 100644--- a/testing/t0043-check-style.sh+++ b/testing/t0043-check-style.sh@@ -185,6 +185,7 @@ test_expect_success '--types lists what stays enabled' 'grep -qx "KERNEL_INT_TYPES" "$W/out" &&grep -qx "NUMBER_PARSING" "$W/out" &&grep -qx "STRING_COPY" "$W/out" &&+ grep -qx "STRING_FORMAT" "$W/out" &&! grep -qx "CAMELCASE" "$W/out" &&! grep -qx "GIT_COMMIT_ID" "$W/out"'@@ -241,6 +242,30 @@ test_expect_success 'strings are copied with strscpy() outside the tests' 'rm -r "$R/tools" "$R/testing"'+test_expect_success 'strings are formatted with strsprintf() outside the tests' '+ printf "%s\n" "/* snprintf() in a comment */" \+ "static const char *op = \"sprintf(a, b)\";" "" \+ "void fmt(char *d, const char *s, va_list ap)" "{" \+ " snprintf(d, 8, \"%d\", 1);" " sprintf(d, \"%d\", 1);" \+ " vsnprintf(d, 8, s, ap);" " vsprintf(d, s, ap);" \+ " snprintf(d, 8, \"%s\", s);" \+ " (void)strsprintf(d, 8, \"%d\", 1);" "}" \+ >>"$R/good.c" &&+ ! check --diff &&+ grep -q "^ERROR:STRING_FORMAT: format into a buffer with strsprintf() or vstrsprintf() from <shirudo/string.h>, not snprintf()" "$W/out" &&+ grep -q "^ERROR:STRING_FORMAT: .*, not vsprintf()" "$W/out" &&+ [ "$(grep -c "^ERROR:STRING_FORMAT" "$W/out")" = 4 ] &&+ [ "$(grep -c "^ERROR:STRING_COPY" "$W/out")" = 1 ] &&+ git -C "$R" checkout -q -- good.c &&+ for f in util/string.c util/malloc.c testing/helper/fmt.c; do+ put $f "// SPDX-License-Identifier: BSD-2-Clause" "" \+ "int fmt(char *d)" "{" \+ " return snprintf(d, 8, \"%d\", 1);" "}" || return 1+ done &&+ check --files util/string.c util/malloc.c testing/helper/fmt.c &&+ rm -r "$R/util" "$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)" \