Skip to content

eBPF-based security platform

.github: reject the C library's fixed-width integer names

Daniel Borkmann6d58ded5553a
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 712d1ca2..680ba4c0 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -5,8 +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. The kernel checks that have no meaning in
-# userspace are switched off below, each with its reason.
+# 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.
#
# .github/scripts/check-style.sh [<rev-list arguments>]
# .github/scripts/check-style.sh --diff
@@ -87,8 +88,8 @@ ignored_types() {
# The OpenSSL, zlib and tpm2-tss APIs are CamelCase, as is
# _Static_assert.
CAMELCASE
- # uint32_t and its kin are the userspace spelling, and the BPF side
- # shares headers with it.
+ # It asks for the kernel-internal u32, which userspace does not have;
+ # check_int_types 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.
@@ -168,6 +169,70 @@ locate_checkpatch() {
printf '%s\n' "$dir/checkpatch.pl"
}
+# Fixed-width integers are spelled the kernel's way, __u32 and its kin from
+# <shirudo/misc.h> in userspace and from vmlinux.h in BPF, not the C
+# 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.
+check_int_types() {
+ awk '
+ /^\+\+\+ / {
+ file = $0
+ sub(/^\+\+\+ b\//, "", file)
+ incomment = 0
+ next
+ }
+ /^@@ / {
+ split($0, h, " ")
+ sub(/^\+/, "", h[3])
+ split(h[3], r, ",")
+ line = r[1] - 1
+ incomment = 0
+ next
+ }
+ /^[ +]/ {
+ line++
+ text = substr($0, 2)
+ gsub(/"([^"\\]|\\.)*"/, "\"\"", text)
+ code = ""
+ while (text != "") {
+ if (incomment) {
+ if ((end = index(text, "*/")) == 0)
+ break
+ text = substr(text, end + 2)
+ incomment = 0
+ } else if ((at = index(text, "/*")) > 0) {
+ code = code substr(text, 1, at - 1)
+ text = substr(text, at + 2)
+ incomment = 1
+ } else {
+ if ((at = index(text, "//")) > 0)
+ text = substr(text, 1, at - 1)
+ code = code text
+ text = ""
+ }
+ }
+ # A hunk can open inside a comment: " * ..." is one.
+ if (code ~ /^[ \t]*\*/)
+ next
+ if (substr($0, 1, 1) != "+")
+ next
+ if (match(code, /(^|[^A-Za-z0-9_])u?int(8|16|32|64)_t([^A-Za-z0-9_]|$)/)) {
+ type = substr(code, RSTART, RLENGTH)
+ gsub(/[^A-Za-z0-9_]/, "", type)
+ bits = type
+ gsub(/[^0-9]/, "", bits)
+ printf "ERROR:KERNEL_INT_TYPES: spell %s as %s%s, from <shirudo/misc.h> in userspace\n#%d: FILE: %s:%d:\n%s\n\n",
+ type, substr(type, 1, 1) == "u" ? "__u" : "__s", bits,
+ NR, file, line, $0
+ bad = 1
+ }
+ }
+ END { exit bad }
+ ' "$1"
+}
+
# checkpatch enforces the kernel's block-comment form everywhere but at the
# opener, which only the netdev trees have an opinion on, and theirs is
# the opposite of Documentation/coding-style.rst: a comment that runs on
@@ -265,6 +330,7 @@ check_patch() {
--quiet --show-types --ignore "$IGNORE" \
"$@" "$work/patch") >"$work/out" 2>&1 || rc=1
check_block_comments "$work/patch" >>"$work/out" || rc=1
+ check_int_types "$work/patch" >>"$work/out" || rc=1
annotate <"$work/out"
return $rc
}
@@ -330,7 +396,10 @@ list_types() {
perl "$checkpatch" --list-types | awk 'NR > 2 && NF >= 2 { print $2 }' |
sort >"$work/all"
ignored_types | grep -v '^#' | sort >"$work/ignored"
- comm -23 "$work/all" "$work/ignored"
+ {
+ comm -23 "$work/all" "$work/ignored"
+ printf '%s\n' KERNEL_INT_TYPES NETDEV_BLOCK_COMMENT
+ } | sort
}
check_files() {
diff --git a/AGENTS.md b/AGENTS.md
index ee738e20..5dab5108 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -74,6 +74,13 @@ before the LSM hooks and `attr.keyring_id` is a key serial, not a name.
form is the kernel convention (a GNU extension shirudo's `-std=gnu11`
accepts). Use it for every struct/array/union zero-init. When adding
new code, write `= {}`.
+- **Spell fixed-width integers the kernel's way**: `__u8`, `__u16`,
+ `__u32`, `__u64` and `__s8` to `__s64`, from `<shirudo/misc.h>` in
+ userspace 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.
- **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 4c599c2c..f56224bd 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 and the list of what stays enabled, on a throwaway
-# repository.
+# it lets through, the C library's integer names it refuses and the list
+# of what stays enabled, on a throwaway repository.
cd "$(dirname "$0")"
. ./test-lib.sh
@@ -161,10 +161,10 @@ Why the change is made." &&
test_expect_success 'the userspace spellings checkpatch would reject pass' '
put user.c "// SPDX-License-Identifier: BSD-2-Clause" "" \
- "#include <stdint.h>" "#include <string.h>" "" \
- "uint32_t user(X509 *c, char *dst, const char *src)" "{" \
+ "#include <string.h>" "" "#include <shirudo/misc.h>" "" \
+ "__u32 user(X509 *c, char *dst, const char *src)" "{" \
" gzFile gz = NULL;" "" " (void)gz;" " strcpy(dst, src);" \
- " return (uint32_t)ASN1_INTEGER_get(X509_get_serialNumber(c));" "}" &&
+ " return (__u32)ASN1_INTEGER_get(X509_get_serialNumber(c));" "}" &&
commit "a: userspace
Why the change is made." &&
@@ -180,10 +180,21 @@ test_expect_success '--types lists what stays enabled' '
check --types &&
grep -qx "C99_COMMENTS" "$W/out" &&
grep -qx "BLOCK_COMMENT_STYLE" "$W/out" &&
+ grep -qx "KERNEL_INT_TYPES" "$W/out" &&
! grep -qx "CAMELCASE" "$W/out" &&
! grep -qx "GIT_COMMIT_ID" "$W/out"
'
+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" &&
+ ! check --diff &&
+ grep -q "^ERROR:KERNEL_INT_TYPES: spell uint32_t as __u32" "$W/out" &&
+ [ "$(grep -c "^ERROR:KERNEL_INT_TYPES" "$W/out")" = 1 ] &&
+ git -C "$R" checkout -q -- good.c
+'
+
test_expect_success '--files checks whole files' '
! check --files bad.c &&
grep -q "^ERROR:C99_COMMENTS" "$W/out" &&