Skip to content

eBPF-based security platform

include: add BUILD_BUG_ON() and the kernel's attribute names

Daniel Borkmann92f4ed26b1f5
diff --git a/.github/scripts/check-style.sh b/.github/scripts/check-style.sh
index 9aa673c9..59e4ceb8 100755
--- a/.github/scripts/check-style.sh
+++ b/.github/scripts/check-style.sh
@@ -94,14 +94,15 @@ ignored_types() {
PREFER_KERNEL_TYPES
# check_spellings below asks for the strscpy() of <shirudo/string.h>
# in tools/ too, which these leave out, and fallthrough;, __packed
- # and the other compiler.h macros do not exist in userspace; BPF has
- # what bpf_helpers.h defines.
+ # and most other compiler.h macros do not exist in userspace, which
+ # has those of <shirudo/compiler.h>; BPF has what bpf_helpers.h
+ # defines.
STRCPY
STRNCPY
PREFER_FALLTHROUGH
PREFER_DEFINED_ATTRIBUTE_MACRO
- # SHIRUDO_BUILD_BUG_ON is a compile-time assertion; there is no
- # kernel to crash.
+ # BUILD_BUG_ON() is a compile-time assertion; there is no kernel to
+ # crash.
AVOID_BUG
# The task_prctl LSM hook returns -ENOSYS for "not handled" by
# contract.
diff --git a/bpf/lib/misc.h b/bpf/lib/misc.h
index 2183109d..468d9684 100644
--- a/bpf/lib/misc.h
+++ b/bpf/lib/misc.h
@@ -8,6 +8,7 @@
#ifndef SHIRUDO_BPF_MISC_H
#define SHIRUDO_BPF_MISC_H
+/* Token for token as in include/shirudo/compiler.h; see there. */
#define BUILD_BUG_ON_MSG(cond, msg) _Static_assert(!(cond), msg)
#define BUILD_BUG_ON(cond) \
BUILD_BUG_ON_MSG(cond, "BUILD_BUG_ON failed: " #cond)
diff --git a/builtin-init.c b/builtin-init.c
index a85d7232..96259b33 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -467,8 +467,8 @@ static int attach_seal(struct bpf_object *policy, struct bpf_object **obj_out,
return 0;
}
-_Static_assert(IDENTITY_KEYS_MAX == KEYS_MAX,
- "the identity map holds one serial per trusted certificate");
+BUILD_BUG_ON_MSG(IDENTITY_KEYS_MAX != KEYS_MAX,
+ "the identity map holds one serial per trusted certificate");
struct anchor_keys {
__s32 serials[IDENTITY_KEYS_MAX];
diff --git a/include/shirudo/compiler.h b/include/shirudo/compiler.h
index 278d4fcc..638dc2f9 100644
--- a/include/shirudo/compiler.h
+++ b/include/shirudo/compiler.h
@@ -2,12 +2,28 @@
/*
* Copyright (c) 2026 Authors of shirudo
*
- * Function attributes spelled the kernel's way.
+ * Attributes and compile-time checks spelled the kernel's way.
*/
#ifndef SHIRUDO_COMPILER_H
#define SHIRUDO_COMPILER_H
#define __printf(a, b) __attribute__((format(printf, a, b)))
+#define __must_check __attribute__((warn_unused_result))
+#define __noreturn __attribute__((noreturn))
+
+/*
+ * Declarations, so they stand at file scope as well as in a block.
+ * Token for token as in bpf/lib/misc.h, which userspace also reaches
+ * through bpf/lib/serializer.h: a redefinition that differs does not
+ * compile.
+ */
+#define BUILD_BUG_ON_MSG(cond, msg) _Static_assert(!(cond), msg)
+#define BUILD_BUG_ON(cond) \
+ BUILD_BUG_ON_MSG(cond, "BUILD_BUG_ON failed: " #cond)
+
+/* 0 as an int constant expression, or no build when e holds. */
+#define BUILD_BUG_ON_ZERO(e) \
+ ((int)(0 * sizeof(struct { _Static_assert(!(e), #e); char c; })))
#endif /* SHIRUDO_COMPILER_H */
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 42fddbe0..402221ec 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -48,9 +48,9 @@ ssize_t strscpy(char *dst, const char *src, size_t size);
* the arguments as a va_list.
*/
int strsprintf(char *buf, size_t size, const char *fmt, ...)
- __printf(3, 4) __attribute__((warn_unused_result));
+ __printf(3, 4) __must_check;
int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
- __printf(3, 0) __attribute__((warn_unused_result));
+ __printf(3, 0) __must_check;
/*
* A DNS host name: labels of letters, digits and inner hyphens, 1 to 63
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index dc01aa7d..6889c13d 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -61,8 +61,7 @@ struct shirudo_upatch_header {
__u32 reserved1; /* zero; keeps the header 8-byte aligned */
};
-_Static_assert(sizeof(struct shirudo_upatch_header) ==
- UPATCH_HEADER_SIZE, "unexpected upatch header padding");
+BUILD_BUG_ON(sizeof(struct shirudo_upatch_header) != UPATCH_HEADER_SIZE);
/*
* The signed sections: what the target needs beyond the loader and its
diff --git a/testing/unit/unit.h b/testing/unit/unit.h
index 05126a5d..ea88dcb6 100644
--- a/testing/unit/unit.h
+++ b/testing/unit/unit.h
@@ -35,8 +35,8 @@ extern const struct unit_suite unit_suite;
void unit_fail(const char *file, int line, const char *fmt, ...)
__printf(3, 4);
-__attribute__((noreturn)) void unit_abort(void);
-__attribute__((noreturn)) void unit_skip(const char *reason);
+__noreturn void unit_abort(void);
+__noreturn void unit_skip(const char *reason);
void unit_fail_mem(const char *file, int line, const char *what,
const void *a, const void *b, size_t len);
diff --git a/util/config_image.c b/util/config_image.c
index 47447962..98202b31 100644
--- a/util/config_image.c
+++ b/util/config_image.c
@@ -35,8 +35,7 @@ struct verity_superblock {
__u8 padding[168];
};
-_Static_assert(sizeof(struct verity_superblock) == 512,
- "verity superblock layout must match the disk format");
+BUILD_BUG_ON(sizeof(struct verity_superblock) != 512);
static int verity_hash_blocks(__u64 data_blocks, __u64 *out)
{
diff --git a/util/identity.h b/util/identity.h
index 1de8b093..e52fa2ff 100644
--- a/util/identity.h
+++ b/util/identity.h
@@ -54,8 +54,7 @@ struct shirudo_expected_root {
__u32 valid;
};
-_Static_assert(sizeof(struct shirudo_expected_root) == 36,
- "expected config root ABI");
+BUILD_BUG_ON(sizeof(struct shirudo_expected_root) != 36);
struct shirudo_task_authority {
__u8 trusted;
@@ -65,20 +64,16 @@ struct shirudo_task_authority {
__u32 pad2;
};
-_Static_assert(sizeof(struct shirudo_device_authority) == 48,
- "device authority ABI");
-_Static_assert(sizeof(struct shirudo_task_authority) == 8,
- "task authority ABI");
-_Static_assert(sizeof(struct shirudo_identity) == 584,
- "frozen identity ABI");
+BUILD_BUG_ON(sizeof(struct shirudo_device_authority) != 48);
+BUILD_BUG_ON(sizeof(struct shirudo_task_authority) != 8);
+BUILD_BUG_ON(sizeof(struct shirudo_identity) != 584);
/* The bpffs mount init made, which the unmount guard keeps mounted. */
struct shirudo_mount_record {
__u64 mount;
};
-_Static_assert(sizeof(struct shirudo_mount_record) == 8,
- "mount record ABI");
+BUILD_BUG_ON(sizeof(struct shirudo_mount_record) != 8);
/*
* The identity map init populated, froze and pinned this boot. -ENOENT
diff --git a/util/image_guards.c b/util/image_guards.c
index cc8640d6..b90fc192 100644
--- a/util/image_guards.c
+++ b/util/image_guards.c
@@ -26,8 +26,8 @@
#include "paths.h"
#include "tamper.h"
-_Static_assert(SHIRUDO_PERF_EVENT_IOC_SET_BPF == PERF_EVENT_IOC_SET_BPF,
- "the ioctl number the BPF side denies");
+BUILD_BUG_ON_MSG(SHIRUDO_PERF_EVENT_IOC_SET_BPF != PERF_EVENT_IOC_SET_BPF,
+ "the ioctl number the BPF side denies");
const struct shirudo_image_guard
shirudo_seal_guards[SEAL_HOOK_COUNT] = {
diff --git a/util/macros.h b/util/macros.h
index 759c057c..ce94c16a 100644
--- a/util/macros.h
+++ b/util/macros.h
@@ -8,11 +8,7 @@
#ifndef SHIRUDO_MACROS_H
#define SHIRUDO_MACROS_H
-/*
- * Compile-time assertion: 0 if `e` is false, else a 1-bit signed
- * bitfield of width -1 -> compiler error.
- */
-#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
+#include <shirudo/compiler.h>
/*
* ARRAY_SIZE() refuses a pointer: __builtin_types_compatible_p tells
diff --git a/util/tpm_wire.h b/util/tpm_wire.h
index 00e47149..ba8e4ca6 100644
--- a/util/tpm_wire.h
+++ b/util/tpm_wire.h
@@ -39,8 +39,8 @@ static inline size_t tpm_put2b(__u8 *p, const __u8 *data, size_t len)
}
#define TPM_DIGEST_LEN 32
-_Static_assert(TPM_NAME_LEN == 2 + TPM_DIGEST_LEN,
- "a name is the algorithm and its digest");
+BUILD_BUG_ON_MSG(TPM_NAME_LEN != 2 + TPM_DIGEST_LEN,
+ "a name is the algorithm and its digest");
#define TPM_ALG_RSA 0x0001u
#define TPM_ALG_AES 0x0006u
#define TPM_ALG_SHA256 0x000Bu