bpf: Disable file_alloc_security hook
Subsystems (4)
PATCH 1/2 bpf: Disable file_alloc_security hook
A use-after-free bug may be triggered by calling bpf_inode_storage_get() in a BPF LSM program hooked to file_alloc_security. Disable the hook to prevent this from happening.
The cause of the bug is shown in the trace below. In alloc_file(), a file struct is first allocated through kmem_cache_alloc(). Then, file_alloc_security hook is invoked. Since the zero initialization or assignment of f->f_inode happen after this LSM hook, a BPF program may get a dangeld inode pointer by walking the file struct.
alloc_file()
-> alloc_empty_file()
-> f = kmem_cache_alloc()
-> init_file()
-> security_file_alloc() // f->f_inode not init-ed yet!
-> f->f_inode = NULL;
-> file_init_path()
-> f->f_inode = path->dentry->d_inode--- kernel/bpf/bpf_lsm.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/bpf_lsm.c +1 -0
@@ -51,6 +51,7 @@ BTF_ID(func, bpf_lsm_key_getsecurity) BTF_ID(func, bpf_lsm_audit_rule_match)#endif BTF_ID(func, bpf_lsm_ismaclabel) +BTF_ID(func, bpf_lsm_file_alloc_security)BTF_SET_END (bpf_lsm_disabled_hooks)/* List of LSM hooks that should operate on 'current' cgroup regardless
Signature · 1 line
2.47.3
PATCH 2/2 selftests/bpf: Remove usage of lsm/file_alloc_security in selftest
file_alloc_security hook is disabled. Use other LSM hooks in selftests instead.
--- tools/testing/selftests/bpf/prog_tests/test_lsm.c | 2 +- tools/testing/selftests/bpf/progs/lsm_tailcall.c | 8 ++++---- tools/testing/selftests/bpf/progs/verifier_lsm.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_lsm.c +1 -1
@@ -139,7 +139,7 @@ static void test_lsm_tailcall(void)if (CHECK_FAIL (!err)) goto close_prog; - prog_fd =bpf_program__fd (skel->progs.lsm_file_alloc_security_prog); + prog_fd =bpf_program__fd (skel->progs.lsm_kernfs_init_security_prog);if (CHECK_FAIL (prog_fd <0 )) goto close_prog;
tools/testing/selftests/bpf/progs/lsm_tailcall.c +4 -4
@@ -20,14 +20,14 @@ int lsm_file_permission_prog(void *ctx)return 0 ; } -SEC("lsm/file_alloc_security") -int lsm_file_alloc_security_prog (void *ctx) +SEC("lsm/kernfs_init_security") +int lsm_kernfs_init_security_prog (void *ctx) {return 0 ; } -SEC("lsm/file_alloc_security") -int lsm_file_alloc_security_entry (void *ctx) +SEC("lsm/kernfs_init_security") +int lsm_kernfs_init_security_entry (void *ctx) {bpf_tail_call_static (ctx, &jmp_table,0 );return 0 ;
tools/testing/selftests/bpf/progs/verifier_lsm.c +2 -2
@@ -4,7 +4,7 @@#include <bpf/bpf_helpers.h> #include "bpf_misc.h" -SEC("lsm/file_alloc_security") +SEC("lsm/file_permission") __description("lsm bpf prog with -4095~0 retval. test 1") __success__naked int errno_zero_retval_test1 (void *ctx)
@@ -15,7 +15,7 @@ __naked int errno_zero_retval_test1(void *ctx) ::: __clobber_all); } -SEC("lsm/file_alloc_security") +SEC("lsm/file_permission") __description("lsm bpf prog with -4095~0 retval. test 2") __success__naked int errno_zero_retval_test2 (void *ctx)
Signature · 1 line
2.47.3
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 11 lines · On Wed, 26 Nov 2025 12:29:26 -0800 you wrote:
A use-after-free bug may be triggered by calling
bpf_inode_storage_get()in a BPF LSM program hooked tofile_alloc_security. Disable the hook to prevent this from happening.The cause of the bug is shown in the trace below. In
alloc_file(), a file struct is first allocated throughkmem_cache_alloc(). Then,file_alloc_securityhook is invoked. Since the zero initialization or assignment off->f_inodehappen after this LSM hook, a BPF program may get a dangeld inode pointer by walking the file struct.[...]
Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Disable file_alloc_security hook
https://git.kernel.org/bpf/bpf-next/c/b4bf1d23dc1d - [bpf-next,v1,2/2] selftests/bpf: Remove usage of lsm/file_alloc_security in selftest https://git.kernel.org/bpf/bpf-next/c/a3a60cc120d6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html