Skip to content

eBPF-based security platform

.github: reject the C library's number parsers in userspace C

Daniel Borkmann98d74472d48d
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 57f38b91..821b2b25 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -5,9 +5,9 @@
# .github/scripts/check-style.sh -- run the kernel's checkpatch.pl, in strict
# 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 and the kernel's spelling of fixed-width
-# integers. The kernel checks that have no meaning in userspace are
-# switched off below, each with its reason.
+# 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.
#
# .github/scripts/check-style.sh [<rev-list arguments>]
# .github/scripts/check-style.sh --diff
@@ -179,7 +179,10 @@ locate_checkpatch() {
# where an interface fixes them: BPF programs spell hook arguments as
# the kernel's prototypes do, include/shirudo/misc.h defines the kernel
# types from them, util/elf_target.c hands libbpf the unsigned long **
-# it takes, and util/sign.c is vendored.
+# 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() {
awk '
function c_names_kept(f) {
@@ -187,6 +190,9 @@ check_int_types() {
f == "include/shirudo/misc.h" ||
f == "util/elf_target.c" || f == "util/sign.c"
}
+ function parsers_kept(f) {
+ return f ~ /^(bpf|upatches|testing)\// || f ~ /\.bpf\.c$/
+ }
/^\+\+\+ / {
file = $0
sub(/^\+\+\+ b\//, "", file)
@@ -257,6 +263,14 @@ check_int_types() {
c, k, NR, file, line, $0
bad = 1
}
+ if (!parsers_kept(file) &&
+ match(code, /(^|[^A-Za-z0-9_])(strto(u?ll?|d|f|ld|imax|umax)|ato(i|l|ll|f))[ \t]*\(/)) {
+ fn = substr(code, RSTART, RLENGTH)
+ gsub(/[^A-Za-z0-9_]/, "", fn)
+ printf "ERROR:NUMBER_PARSING: parse numbers with parse_u64(), parse_s64() or parse_x64() 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"
@@ -427,7 +441,7 @@ list_types() {
ignored_types | grep -v '^#' | sort >"$work/ignored"
{
comm -23 "$work/all" "$work/ignored"
- printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT
+ printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT NUMBER_PARSING
} | sort
}
diff --git a/AGENTS.md b/AGENTS.md
index f1592f19..a69d852b 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -84,6 +84,11 @@ before the LSM hooks and `attr.keyring_id` is a key serial, not a name.
kernel's prototypes do. `check-style.sh` rejects `uint32_t` and its kin
everywhere and `unsigned char`, `short`, `int` and `long` in userspace,
less the files it names.
+- **Parse numbers with `parse_u64()`, `parse_s64()` and `parse_x64()`**
+ from `<shirudo/string.h>`, and find where one ends with `digits()`:
+ they take a length, read nothing past it and refuse a sign, blank or
+ base prefix. `check-style.sh` rejects `strtoul()` and its kin and
+ `atoi()` outside `testing/`.
- **Declare locals in reverse-Christmas-tree order** (the netdev
convention): longest declaration line first, shortest last. Group
declarations that have no initializer dependency and sort them by
diff --git a/testing/t0043-check-style.sh b/testing/t0043-check-style.sh
index 48ba245e..1358f849 100644
--- a/testing/t0043-check-style.sh
+++ b/testing/t0043-check-style.sh
@@ -6,8 +6,8 @@
# 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 it refuses and the list
-# of what stays enabled, on a throwaway repository.
+# 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.
cd "$(dirname "$0")"
. ./test-lib.sh
@@ -181,6 +181,7 @@ test_expect_success '--types lists what stays enabled' '
grep -qx "C99_COMMENTS" "$W/out" &&
grep -qx "BLOCK_COMMENT_STYLE" "$W/out" &&
grep -qx "KERNEL_INT_TYPES" "$W/out" &&
+ grep -qx "NUMBER_PARSING" "$W/out" &&
! grep -qx "CAMELCASE" "$W/out" &&
! grep -qx "GIT_COMMIT_ID" "$W/out"
'
@@ -199,6 +200,22 @@ test_expect_success 'the C library'"'"'s integer names are refused in code only'
git -C "$R" checkout -q -- good.c
'
+test_expect_success 'the C library'"'"'s number parsers are refused outside the tests' '
+ printf "%s\n" "/* strtoul() in a comment */" \
+ "static const char *name = \"atoi(s)\";" "" \
+ "long parse(const char *s)" "{" \
+ " return strtoll(s, NULL, 10) + atoi(s);" "}" \
+ >>"$R/good.c" &&
+ ! check --diff &&
+ grep -q "^ERROR:NUMBER_PARSING: parse numbers with parse_u64(), parse_s64() or parse_x64() from <shirudo/string.h>, not strtoll()" "$W/out" &&
+ [ "$(grep -c "^ERROR:NUMBER_PARSING" "$W/out")" = 1 ] &&
+ git -C "$R" checkout -q -- good.c &&
+ put testing/helper/args.c "// SPDX-License-Identifier: BSD-2-Clause" "" \
+ "long arg(const char *s)" "{" " return strtol(s, NULL, 10);" "}" &&
+ check --files testing/helper/args.c &&
+ rm -r "$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)" \