eBPF-based security platform
.github: reject the unsigned C integer names in userspace C
Daniel Borkmannb5aee71debbe
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.shindex 680ba4c0..57f38b91 100755--- a/.github/scripts/check-style.sh+++ b/.github/scripts/check-style.sh@@ -174,9 +174,19 @@ locate_checkpatch() {# library's uint32_t. checkpatch's PREFER_KERNEL_TYPES suggests the# kernel-internal u32 and leaves tools/ out, so it stays off and this# reports an added line that names uint8_t through int64_t outside a-# string or a comment, in the form checkpatch reports in.+# string or a comment, in the form checkpatch reports in, and in+# userspace one that names unsigned char, short, int or long. Those stay+# 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.check_int_types() {awk '+ function c_names_kept(f) {+ return f ~ /^(bpf|upatches)\// || f ~ /\.bpf\.c$/ ||+ f == "include/shirudo/misc.h" ||+ f == "util/elf_target.c" || f == "util/sign.c"+ }/^\+\+\+ / {file = $0sub(/^\+\+\+ b\//, "", file)@@ -227,6 +237,25 @@ check_int_types() {type, substr(type, 1, 1) == "u" ? "__u" : "__s", bits,NR, file, line, $0bad = 1+ } else if (!c_names_kept(file) &&+ match(code, /(^|[^A-Za-z0-9_])unsigned[ \t]+(char|short|int|long)([^A-Za-z0-9_]|$)/)) {+ type = substr(code, RSTART, RLENGTH)+ if (type ~ /char/) {+ c = "char"+ k = "__u8"+ } else if (type ~ /short/) {+ c = "short"+ k = "__u16"+ } else if (type ~ /int/) {+ c = "int"+ k = "__u32"+ } else {+ c = "long"+ k = "__u64"+ }+ printf "ERROR:KERNEL_INT_TYPES: spell unsigned %s as %s, from <shirudo/misc.h> in userspace\n#%d: FILE: %s:%d:\n%s\n\n",+ c, k, NR, file, line, $0+ bad = 1}}END { exit bad }diff --git a/AGENTS.md b/AGENTS.mdindex 5dab5108..86152ea7 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -78,9 +78,12 @@ before the LSM hooks and `attr.keyring_id` is a key serial, not a name.`__u32`, `__u64` and `__s8` to `__s64`, from `<shirudo/misc.h>` inuserspace and `vmlinux.h` in BPF, not `uint32_t` and its kin nor`unsigned int`, `unsigned long` and the like. `__u64` is `unsigned long- long`, so it prints with `%llu`. A library interface that takes another- type keeps it, as does `(unsigned long)` in a pointer-to-integer cast.- `check-style.sh` rejects the C library's names.+ long`, so it prints with `%llu`, and `ptr_to_u64()` turns a pointer+ into the `__u64` the bpf() ABI carries. A library interface that takes+ another type keeps it, and BPF programs spell hook arguments as the+ 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.- **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 f56224bd..48ba245e 100644--- a/testing/t0043-check-style.sh+++ b/testing/t0043-check-style.sh@@ -188,13 +188,28 @@ test_expect_success '--types lists what stays enabled' 'test_expect_success 'the C library'"'"'s integer names are refused in code only' 'printf "%s\n" "/* uint32_t in a comment */" \"static const char *name = \"uint64_t\";" \- "static __u16 kept;" "static uint32_t width;" >>"$R/good.c" &&+ "static __u16 kept;" "static uint32_t width;" \+ "static unsigned long mask;" "static unsigned char byte;" \+ >>"$R/good.c" &&! check --diff &&grep -q "^ERROR:KERNEL_INT_TYPES: spell uint32_t as __u32" "$W/out" &&- [ "$(grep -c "^ERROR:KERNEL_INT_TYPES" "$W/out")" = 1 ] &&+ grep -q "^ERROR:KERNEL_INT_TYPES: spell unsigned long as __u64" "$W/out" &&+ grep -q "^ERROR:KERNEL_INT_TYPES: spell unsigned char as __u8" "$W/out" &&+ [ "$(grep -c "^ERROR:KERNEL_INT_TYPES" "$W/out")" = 3 ] &&git -C "$R" checkout -q -- good.c'+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)" \+ "{" " return arg > port + c;" "}" &&+ check --files hook.bpf.c &&+ printf "%s\n" "static uint32_t width;" >>"$R/hook.bpf.c" &&+ ! check --files hook.bpf.c &&+ grep -q "^ERROR:KERNEL_INT_TYPES: spell uint32_t as __u32" "$W/out" &&+ rm "$R/hook.bpf.c"+'+test_expect_success '--files checks whole files' '! check --files bad.c &&grep -q "^ERROR:C99_COMMENTS" "$W/out" &&