Skip to content

eBPF-based security platform

include: take the kernel's types from <linux/types.h>

Daniel Borkmanne263ea629cd6
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 59e4ceb8..37191a24 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -174,7 +174,7 @@ locate_checkpatch() {
}
# 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
+# <shirudo/types.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
@@ -182,7 +182,6 @@ locate_checkpatch() {
# 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. A call to strtoul() and its kin or atoi() is
# reported too, outside the tests: a number is parsed with parse_u64() and
@@ -198,7 +197,6 @@ check_spellings() {
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"
}
function parsers_kept(f) {
diff --git a/AGENTS.md b/AGENTS.md
index d2f1961f..23d4d1c3 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -91,18 +91,18 @@ package for `bpm info`.
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`, 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. `<shirudo/misc.h>` poisons `uint8_t` to
- `int64_t`, so a system header that uses them, `<netinet/in.h>`,
- `<elf.h>` or one of OpenSSL's or tpm2-tss's, goes above the first
- shirudo header. `check-style.sh` rejects `uint32_t` and its kin
- everywhere and `unsigned char`, `short`, `int` and `long` in userspace,
- less the files it names.
+ `__u32`, `__u64` and `__s8` to `__s64`, from `<shirudo/types.h>`,
+ which `<shirudo/misc.h>` includes, 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`, 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.
+ `<shirudo/misc.h>` poisons `uint8_t` to `int64_t`, so a system header
+ that uses them, `<netinet/in.h>`, `<elf.h>` or one of OpenSSL's or
+ tpm2-tss's, goes above the first shirudo header. `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
diff --git a/include/shirudo/misc.h b/include/shirudo/misc.h
index 1fd30d76..f706f83f 100644
--- a/include/shirudo/misc.h
+++ b/include/shirudo/misc.h
@@ -2,8 +2,8 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * The kernel's fixed-width integer types, ARRAY_SIZE(), min() and max(),
- * ptr_to_u64() and shirudo_trunc(), for userspace.
+ * ARRAY_SIZE(), min() and max(), ptr_to_u64() and shirudo_trunc() for
+ * userspace, with the kernel's types from <shirudo/types.h>.
*/
#ifndef SHIRUDO_MISC_H
@@ -13,20 +13,7 @@
#include <stdlib.h>
#include <shirudo/compiler.h>
-
-/*
- * Defined as asm-generic/int-ll64.h defines them on x86-64, arm64 and
- * riscv64, so a file that also includes <linux/types.h> sees the same
- * types twice, which C11 allows.
- */
-typedef signed char __s8;
-typedef unsigned char __u8;
-typedef signed short __s16;
-typedef unsigned short __u16;
-typedef signed int __s32;
-typedef unsigned int __u32;
-typedef signed long long __s64;
-typedef unsigned long long __u64;
+#include <shirudo/types.h>
/* 0, or no build when a is a pointer rather than an array. */
#define __must_be_array(a) \
diff --git a/include/shirudo/types.h b/include/shirudo/types.h
new file mode 100644
index 00000000..eaa6f590
--- /dev/null
+++ b/include/shirudo/types.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * The kernel's fixed-width and byte-order types, for userspace.
+ */
+
+#ifndef SHIRUDO_TYPES_H
+#define SHIRUDO_TYPES_H
+
+/*
+ * The UAPI header's own types, the ones every kernel and libbpf header
+ * uses: __u8 to __s64 as on x86-64, arm64 and riscv64, and __le16 to
+ * __be64, which sparse checks as restricted types.
+ */
+#include <linux/types.h>
+
+#endif /* SHIRUDO_TYPES_H */