* [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable
@ 2026-11-25 17:59 Quan Sun
2026-11-25 18:10 ` sashiko-bot
2026-11-25 18:25 ` bot+bpf-ci
0 siblings, 2 replies; 3+ messages in thread
From: Quan Sun @ 2026-11-25 17:59 UTC (permalink / raw)
To: bpf, kpsingh; +Cc: matt, paul, ast, daniel, andrii, emil, Quan Sun
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 <m202672221@hust.edu.cn>
---
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 <linux/fs.h>
#include <linux/xattr.h>
+#include <linux/ipc.h>
#include <linux/bpf_lsm.h>
/*
@@ -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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable
2026-11-25 17:59 [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable Quan Sun
@ 2026-11-25 18:10 ` sashiko-bot
2026-11-25 18:25 ` bot+bpf-ci
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-25 18:10 UTC (permalink / raw)
To: Quan Sun, bpf
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] bpf, lsm: missing __nullable annotation on kacl in
bpf_lsm_inode_set_acl() [1]
--
commit 77496f1a0298baa887511d2928e2317763dbc7b6
Author: Quan Sun <m202672221@hust.edu.cn>
bpf, lsm: Mark IPC control hook perm parameters nullable
This commit adds strong overrides in kernel/bpf/bpf_lsm_proto.c for
bpf_lsm_shm_shmctl(), bpf_lsm_sem_semctl(), and
bpf_lsm_msg_queue_msgctl() to annotate their perm parameters with
__nullable.
> diff --git a/kernel/bpf/bpf_lsm_proto.c b/kernel/bpf/bpf_lsm_proto.c
> index b81179ae2e086..7c48af32c2d40 100644
> --- a/kernel/bpf/bpf_lsm_proto.c
> +++ b/kernel/bpf/bpf_lsm_proto.c
> @@ -5,6 +5,7 @@
>
> #include <linux/fs.h>
> #include <linux/xattr.h>
> +#include <linux/ipc.h>
> #include <linux/bpf_lsm.h>
>
> /*
> @@ -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;
> +}
[Severity: High]
This problem wasn't introduced by this patch, but should
bpf_lsm_inode_set_acl() also have a strong definition [1] marking
its kacl parameter with __nullable?
When setxattr() is called on a POSIX ACL attribute with size == 0,
do_set_acl() passes a NULL acl through vfs_set_acl() and
security_inode_set_acl() to bpf_lsm_inode_set_acl(). Without the
__nullable annotation, the verifier treats kacl as a trusted non-null
pointer and neither requires a NULL check nor rewrites loads to use
BPF_PROBE_MEM, allowing a BPF LSM program attached to inode_set_acl to
trigger a NULL pointer dereference.
[1] https://sashiko.dev/#/bug/linux-bc5e250e-6772-4ebe-b887-e6439d751142
--
Sashiko AI review · https://sashiko.dev/#/patchset/51ae120f0b3cd8c20dbaaf02c9c384dc2afab6bc.1795627805.git.m202672221@hust.edu.cn?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable
2026-11-25 17:59 [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable Quan Sun
2026-11-25 18:10 ` sashiko-bot
@ 2026-11-25 18:25 ` bot+bpf-ci
1 sibling, 0 replies; 3+ messages in thread
From: bot+bpf-ci @ 2026-11-25 18:25 UTC (permalink / raw)
To: m202672221, bpf, kpsingh
Cc: matt, paul, ast, daniel, andrii, emil, m202672221, ast, andrii,
daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3487 bytes --]
> bpf, lsm: Mark IPC control hook perm parameters nullable
TL;DR:
- Commit message may misdescribe the problem: LSM perm args are trusted, so an
unchecked NULL deref may oops, possibly a regression since c6b0337f0120
- Minor nits: EPERM vs EACCES, misleading IPC_INFO | SHM_INFO comment
> 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:
> [ ... ]
> This is not a regression: the parameters were never annotated, and the
> NULL argument is part of the hook contract
Is this description accurate? For an LSM program, btf_ctx_access() ORs in
PTR_TRUSTED when prog_args_trusted() is true, and prog_args_trusted()
returns bpf_lsm_is_trusted(prog) for BPF_PROG_TYPE_LSM. None of the IPC
hooks is in untrusted_lsm_hooks, so perm is PTR_TO_BTF_ID | PTR_TRUSTED.
The verifier log quoted in the commit message shows this too:
trusted_ptr_or_null_kern_ipc_perm.
The difference matters for the oops. A plain PTR_TO_BTF_ID load would not
crash:
static inline bool bpf_may_fault_on_deref(enum bpf_reg_type type)
{
return type == PTR_TO_BTF_ID || (type_flag(type) & PTR_UNTRUSTED);
}
bpf_convert_ctx_accesses() rewrites a load from a flagless PTR_TO_BTF_ID
into BPF_PROBE_MEM, which goes through the exception table and reads 0 for
a NULL+0x60 address. Only the trusted load is emitted as a direct BPF_MEM
load that faults.
LSM arguments became trusted, and trusted loads stopped being
probe-converted, in c6b0337f0120 ("bpf: Don't mark arguments to fentry/fexit
programs as trusted."). That commit replaced prog_type_args_trusted()
(TRACING || STRUCT_OPS only) with prog_args_trusted(), which has
"case BPF_PROG_TYPE_LSM: return true", and removed the PROBE_MEM conversion
for trusted loads. Before it, the same program read perm->seq through
BPF_PROBE_MEM and got 0.
So does "This is not a regression" hold for the crash itself? That seems
relevant for stable backporting, since the trigger needs no privilege.
There is also a smaller point:
> and BPF_PROG_LOAD fails with EPERM.
The path that prints "R6 invalid mem access 'trusted_ptr_or_null_'" is the
final else of check_mem_access(), which returns -EACCES, not -EPERM.
> diff --git a/kernel/bpf/bpf_lsm_proto.c b/kernel/bpf/bpf_lsm_proto.c
> --- a/kernel/bpf/bpf_lsm_proto.c
> +++ b/kernel/bpf/bpf_lsm_proto.c
[ ... ]
> +/*
> + * 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
This isn't a bug, but the comment reads as a single call with the bitwise
OR of the two commands, and no such call exists. shmctl_ipc_info() calls
security_shm_shmctl(NULL, IPC_INFO) and shmctl_shm_info() calls
security_shm_shmctl(NULL, SHM_INFO).
IPC_INFO (3) | SHM_INFO (14) is 15, which is SHM_STAT_ANY, and that command
goes through shmctl_stat() and passes a real &shp->shm_perm, not NULL.
Could the comment say "call security_shm_shmctl(NULL, IPC_INFO) and
security_shm_shmctl(NULL, SHM_INFO) respectively" instead?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45672586202
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 18:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 17:59 [PATCH] bpf, lsm: Mark IPC control hook perm parameters nullable Quan Sun
2026-11-25 18:10 ` sashiko-bot
2026-11-25 18:25 ` bot+bpf-ci
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox