The shm_shmctl(), sem_semctl() and msg_queue_msgctl() BPF LSM hooks are reached with a NULL struct kern_ipc_perm by the informational IPC commands: ipc/shm.c shmctl_ipc_info() security_shm_shmctl(NULL, IPC_INFO) ipc/shm.c shmctl_shm_info() security_shm_shmctl(NULL, SHM_INFO) ipc/sem.c semctl_info() security_sem_semctl(NULL, cmd) ipc/msg.c msgctl_info() security_msg_queue_msgctl(NULL, cmd) None of these three hooks has a strong prototype in kernel/bpf/bpf_lsm_proto.c, so their BTF comes from the __weak prototypes generated from include/linux/lsm_hook_defs.h in kernel/bpf/bpf_lsm.c. Those carry no __nullable suffix, so btf_ctx_access() types perm as a plain PTR_TO_BTF_ID and the verifier accepts a program that dereferences it unconditionally: r6 = *(u64 *)(r1 + 0) /* struct kern_ipc_perm *perm */ r7 = *(u32 *)(r6 + 96) /* perm->seq */ The trampoline passes the raw NULL through and the program faults: BUG: KASAN: null-ptr-deref in bpf_prog_4c1689a6d8af2199+0x52/0x6e Read of size 4 at addr 0000000000000060 by task 03_lsm_raw/89 Call stack: bpf_prog_4c1689a6d8af2199+0x52/0x6e bpf_trampoline_6442537938+0x61/0xfd security_shm_shmctl+0x29/0x50 shmctl_ipc_info+0x1b/0x140 ksys_shmctl.constprop.0+0x22b/0x290 do_syscall_64+0x10b/0x5f0 BUG: kernel NULL pointer dereference, address: 0000000000000060 Oops: 0000 [#1] SMP KASAN PTI Loading the program requires CAP_BPF, but triggering it requires nothing: ksys_shmctl() has no capability check ahead of IPC_INFO and SHM_INFO, and the hook is the first statement of shmctl_ipc_info(), so an unprivileged shmctl(0, IPC_INFO, buf) kills its own process while the attachment stays in place for every later caller. With panic_on_oops=1 a single CAP_BPF principal takes down the host. kernel/bpf/bpf_lsm.c defines every attachable hook as __weak noinline RET bpf_lsm_##NAME(__VA_ARGS__) { return DEFAULT; } and notes that the linkage is weak "such that strong overrides can be implemented if need be", so an override has to be a definition rather than a declaration. The bodies below return the defaults that include/linux/lsm_hook_defs.h declares for these three hooks (all 0), so the generated stubs keep behaving identically; only the parameter name recorded in BTF changes. Add strong prototypes for the three hooks so that perm is typed PTR_MAYBE_NULL, the way mmap_file() and inode_init_security() are already handled. With the annotations in place the identical program is rejected at load time: 1: R1=ctx() R6=trusted_ptr_or_null_kern_ipc_perm(id=1) 1: (61) r7 = *(u32 *)(r6 +96) R6 invalid mem access 'trusted_ptr_or_null_' Reason: R6 may be NULL here (a nullable kernel object pointer). The program could dereference NULL on this path, so the verifier cannot prove this access is safe. and BPF_PROG_LOAD fails with EPERM. socket_getpeersec_dgram() passes a NULL struct sock on one path too, but its reachability from a BPF LSM program has not been confirmed, so it is left out here and can be handled separately. This is not a regression: the parameters were never annotated, and the NULL argument is part of the hook contract -- selinux_shm_shmctl() has carried the comment "No specific object, just general system-wide information" for IPC_INFO and SHM_INFO since the hook was added, so the callers are right and only the BPF side lacks the information. This change was drafted with an AI coding assistant during a BPF subsystem audit. I reviewed the code, built the kernel and ran the reproducer before and after, and take responsibility for it. Signed-off-by: Quan Sun --- Reproduce: boot bpf-next master (8dfce13340ae, 7.4.0-rc3-g8dfce13340ae, CONFIG_KASAN=y) in qemu, load a 4-instruction BPF_PROG_TYPE_LSM program attached to bpf_lsm_shm_shmctl that dereferences ctx offset 0 without a NULL check, then call shmctl(0, IPC_INFO, buf) from an unprivileged process. The PoC is raw bpf(2) only, no libbpf and no skeleton. sem_semctl() and msg_queue_msgctl() are annotated for symmetry with the shm case; I did not run separate reproducers for those two paths. kernel/bpf/bpf_lsm_proto.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kernel/bpf/bpf_lsm_proto.c b/kernel/bpf/bpf_lsm_proto.c index b81179ae2e0..7c48af32c2d 100644 --- a/kernel/bpf/bpf_lsm_proto.c +++ b/kernel/bpf/bpf_lsm_proto.c @@ -5,6 +5,7 @@ #include #include +#include #include /* @@ -32,3 +33,31 @@ noinline int bpf_lsm_inode_init_security(struct inode *inode, struct inode *dir, { return -EOPNOTSUPP; } + +/* + * Strong definitions of the IPC control BPF LSM hooks. The informational + * commands have no object to authorize, so they pass a NULL struct + * kern_ipc_perm: shmctl_ipc_info() and shmctl_shm_info() call + * security_shm_shmctl(NULL, IPC_INFO | SHM_INFO), semctl_info() calls + * security_sem_semctl(NULL, cmd) and msgctl_info() calls + * security_msg_queue_msgctl(NULL, cmd). The __nullable suffix marks perm as + * PTR_MAYBE_NULL so that BPF LSM programs have to check it before + * dereferencing it. Without these overrides the hooks fall back to the + * __weak prototypes generated from lsm_hook_defs.h in bpf_lsm.c, which carry + * no annotation and let the verifier type perm as non-nullable. + */ +noinline int bpf_lsm_shm_shmctl(struct kern_ipc_perm *perm__nullable, int cmd) +{ + return 0; +} + +noinline int bpf_lsm_sem_semctl(struct kern_ipc_perm *perm__nullable, int cmd) +{ + return 0; +} + +noinline int bpf_lsm_msg_queue_msgctl(struct kern_ipc_perm *perm__nullable, + int cmd) +{ + return 0; +} -- 2.43.0