bpf, lsm: Make bpf_lsm_get_retval_range() return void
PATCH 1/1 bpf, lsm: Make bpf_lsm_get_retval_range() return void
With CONFIG_BPF_FMODRET_SCAN=y, fmod_ret programs can attach to tagged functions, so a program on bpf_lsm_get_retval_range() can make it fail for an LSM hook that returns a value. get_func_retval_range() then reports no range, and do_refine_retval_range() and check_helper_call() ignore the error and use a range the call did not set.
The function fails only for a void hook, which every caller but get_func_retval_range() already rules out. Check for a void hook there and make the function return void, so resolve_btfids no longer tags it and no fmod_ret program can attach to it.
---
Built with W=1 on x86_64 and arm64, with gcc and clang, with and without CONFIG_BPF_LSM. With CONFIG_BPF_FMODRET_SCAN=y, the BPF LSM selftests give the same results with and without this patch. They ran under QEMU emulation only, not on real hardware, and we have no reproducer for the failure.
AI coding agents found the bug by reading the code, wrote this patch and its changelog, and ran the builds and tests above.
--- include/linux/bpf_lsm.h | 9 ++++----- kernel/bpf/bpf_lsm.c | 10 +++------- kernel/bpf/verifier.c | 5 +++-- 3 files changed, 10 insertions(+), 14 deletions(-)
include/linux/bpf_lsm.h +4 -5
@@ -55,8 +55,8 @@ void bpf_inode_storage_free(struct inode *inode);void bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); -int bpf_lsm_get_retval_range (const struct bpf_prog *prog, -struct bpf_retval_range *range); +void bpf_lsm_get_retval_range (const struct bpf_prog *prog, +struct bpf_retval_range *range);int bpf_set_dentry_xattr_locked (struct dentry *dentry,const char *name__str,const struct bpf_dynptr *value_p,int flags);int bpf_remove_dentry_xattr_locked (struct dentry *dentry,const char *name__str);
@@ -98,10 +98,9 @@ static inline void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, { } -static inline int bpf_lsm_get_retval_range (const struct bpf_prog *prog, -struct bpf_retval_range *range) +static inline void bpf_lsm_get_retval_range (const struct bpf_prog *prog, +struct bpf_retval_range *range) { -return -EOPNOTSUPP ; }static inline int bpf_set_dentry_xattr_locked (struct dentry *dentry,const char *name__str,const struct bpf_dynptr *value_p,int flags)
kernel/bpf/bpf_lsm.c +3 -7
@@ -478,13 +478,10 @@ bool bpf_lsm_hook_returns_errno(u32 btf_id)return true; } -int bpf_lsm_get_retval_range (const struct bpf_prog *prog, -struct bpf_retval_range *retval_range) +/* Callers must skip void hooks, which have no return value range. */ +void bpf_lsm_get_retval_range (const struct bpf_prog *prog, +struct bpf_retval_range *retval_range) { -/* no return value range for void hooks */ -if (!prog->aux->attach_func_proto->type) -return -EINVAL ; -if (btf_id_set_contains (&bool_lsm_hooks, prog->aux->attach_btf_id)) { retval_range->minval =0 ; retval_range->maxval =1 ;
@@ -497,5 +494,4 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog, retval_range->minval = -MAX_ERRNO ; retval_range->maxval =0 ; } -return 0 ; }
kernel/bpf/verifier.c +3 -2
@@ -6978,9 +6978,10 @@ static int check_stack_access_within_bounds(static bool get_func_retval_range (struct bpf_prog *prog,struct bpf_retval_range *range) { -if (prog->type ==BPF_PROG_TYPE_LSM && +if (IS_ENABLED (CONFIG_BPF_LSM ) && prog->type ==BPF_PROG_TYPE_LSM && prog->expected_attach_type ==BPF_LSM_MAC && - !bpf_lsm_get_retval_range (prog, range)) { + prog->aux->attach_func_proto->type) { +bpf_lsm_get_retval_range (prog, range);return true; }return false;
---
base-commit: 32b66b94c86f009b49668de7340f2ed65aad2dc5
change-id: 20261118-jf-bpf-lsm-retval-range-8a04025ee458
Best regards,
--
Jose Fernandez (Anthropic) <[email protected]>
Quoted 6 lines · Jose Fernandez (Anthropic)
With
CONFIG_BPF_FMODRET_SCAN=y,fmod_retprograms can attach to tagged functions, so a program onbpf_lsm_get_retval_range()can make it fail for an LSM hook that returns a value.get_func_retval_range()then reports no range, anddo_refine_retval_range()andcheck_helper_call()ignore the error and use a range the call did not set.
Quoted 7 lines
The function fails only for a void hook, which every caller but
get_func_retval_range()already rules out. Check for a void hook there and make the function return void, soresolve_btfidsno longer tags it and nofmod_retprogram can attach to it.Fixes bc45414d1690 ("bpf: Allow fmod_ret on tagged functions and check the return value")Signed-off-by Jose Fernandez (Anthropic) <[email protected]>
The change looks ok, makes sense for this function.
I am not sure if the topic should be 'bpf, lsm',
I see no lsm part here. Otherwise,
Quoted 1 line
...
Alexei Starovoitov
AppliedQuoted 2 lines · Jose Fernandez (Anthropic)
and make the function return void, so
resolve_btfidsno longer tags it and nofmod_retprogram can attach to it.
Applied, but that closes one function. By the rules in fmodret.c the verifier, helpers and kfuncs get tags too. A skip is worse there:
target error after the skip
--------------------- ------- ----------------------------------------
bpf_find_subprog() -ENOENT jit_subprogs(): func[-2]->bpf_func
bpf_iter_task_new() -EINVAL no kit->pos = NULL. bpf_iter_task_next()
derefs what the prog left on its stack
bpf_dynptr_from_mem() -EINVAL no bpf_dynptr_set_null(). The prog picks
dst->data for bpf_dynptr_write()We cannot make them all void.
Should we teach resolve_btfid to filter out all code in kernel/bpf/verifier.o and/or funcs with "bpf_" prefix? and/or funcs that take 'bpf_*' types ? btf_*() looks ok to stay as fmodretable.
Quoted 16 lines · Alexei Starovoitov
Quoted 2 lines · Jose Fernandez (Anthropic)
and make the function return void, so
resolve_btfidsno longer tags it and nofmod_retprogram can attach to it.Applied, but that closes one function. By the rules in
fmodret.cthe verifier, helpers and kfuncs get tags too. A skip is worse there:target error after the skip --------------------- ------- ---------------------------------------- bpf_find_subprog() -ENOENT jit_subprogs(): func[-2]->bpf_func bpf_iter_task_new() -EINVAL no kit->pos = NULL. bpf_iter_task_next() derefs what the prog left on its stack bpf_dynptr_from_mem() -EINVAL no bpf_dynptr_set_null(). The prog picks dst->data for bpf_dynptr_write()
Quoted 4 lines
We cannot make them all void.
Should we teachresolve_btfidto filter out all code inkernel/bpf/verifier.oand/or funcs with "bpf_" prefix? and/or funcs that take 'bpf_*' types ? btf_*() looks ok to stay as fmodretable.
I think that this makes sense to not list helpers, kfuncs, and verifier (and jits), they're building blocks for BPF programs themselves. I've checked that most of external functions called by verifier (~20 files) are not in the tagged list, five are, but look legitimate or "harmless" (security_bpf_prog_load, get_callchain_buffers, copy_from_kernel_nofault, bpf_set_dentry_xattr_locked, bpf_remove_dentry_xattr_locked). Blacklisting these removes about 200 hooks. (Blacklisting all kernel/bpf/* removes around 600 hooks. I wonder, maybe this make sense to do this, after all?)
(Just in case, I am preparing patch for "fmodret -> functions returning pointers", so numbers can be a bit different later.)
Quoted 7 lines · Anton Protopopov
I think that this makes sense to not list helpers, kfuncs, and verifier (and jits), they're building blocks for BPF programs themselves. I've checked that most of external functions called by verifier (~20 files) are not in the tagged list, five are, but look legitimate or "harmless" (
security_bpf_prog_load,get_callchain_buffers,copy_from_kernel_nofault,bpf_set_dentry_xattr_locked,bpf_remove_dentry_xattr_locked).
For the verifier, yes. But copy_from_kernel_nofault() breaks ftrace. A prog on it returns -EFAULT, and fentry on foo() detaches on x86:
unregister_fentry()
update_ftrace_direct_del() returns 0
ftrace_replace_code() ftrace_bug(), no text poke
ftrace_verify_code() -EFAULT
bpf_tramp_image_put() foo() still calls the freed imageLet's deny it by name.
The two *_locked() are kfuncs without bpf_kfunc tag. See specialize_kfunc().
The verifier also calls kfunc filters via a pointer. In visit_insn():
if (ret == 0 && bpf_is_throw_kfunc(insn)) sub->might_throw = true;
A prog on tracing_iter_filter() makes ret -EACCES for this one call. Then check_func_call() skips the exception path of a global subprog. That undoes commit 3d562d35a044.
This filter is in net/core/filter.c and has no "bpf_" prefix. So funcs that take 'bpf_*' types have to go too.
Quoted 3 lines
Blacklisting these removes about 200 hooks. (Blacklisting all
kernel/bpf/*removes around 600 hooks. I wonder, maybe this make sense to do this, after all?)
Yes. All of kernel/bpf/*. btf_*() cannot stay either. By the rules in fmodret.c these get tags too:
target error after the skip ---------------------- ------- --------------------------------------- btf_kfunc_check_flag() -ENOENT fentry/bpf_wq_set_callback saves 3 args
of 4. aux is garbage. That undoes commit 3917b1012ee2
bpf_mprog_detach() -ENOENT tcx_link_release() only warns.
tcx_run() runs the freed prog
security_bpf*() and the bpf syscall entry need no tag. They stay. So a prog can still block a bad bpf command.
Could you send a patch for the above?
Quoted 2 lines
(Just in case, I am preparing patch for "fmodret -> functions returning pointers", so numbers can be a bit different later.)
Nice. Looking forward.
Quoted 49 lines · Alexei Starovoitov
Quoted 7 lines · Anton Protopopov
I think that this makes sense to not list helpers, kfuncs, and verifier (and jits), they're building blocks for BPF programs themselves. I've checked that most of external functions called by verifier (~20 files) are not in the tagged list, five are, but look legitimate or "harmless" (
security_bpf_prog_load,get_callchain_buffers,copy_from_kernel_nofault,bpf_set_dentry_xattr_locked,bpf_remove_dentry_xattr_locked).For the verifier, yes. But
copy_from_kernel_nofault()breaks ftrace. A prog on it returns-EFAULT, and fentry onfoo()detaches on x86:
unregister_fentry()update_ftrace_direct_del() returns 0 ftrace_replace_code() ftrace_bug(), no text poke ftrace_verify_code() -EFAULT bpf_tramp_image_put() foo() still calls the freed imageLet's deny it by name.
The two*_locked()are kfuncs withoutbpf_kfunctag. Seespecialize_kfunc().The verifier also calls kfunc filters via a pointer. In
visit_insn():if (ret == 0 && bpf_is_throw_kfunc(insn)) sub->might_throw = true;A prog on
tracing_iter_filter()makes ret-EACCESfor this one call. Thencheck_func_call()skips the exception path of a global subprog. That undoes commit3d562d35a044.
This filter is innet/core/filter.cand has no "bpf_" prefix. So funcs that take 'bpf_*' types have to go too.Quoted 3 lines
Blacklisting these removes about 200 hooks. (Blacklisting all
kernel/bpf/*removes around 600 hooks. I wonder, maybe this make sense to do this, after all?)Yes. All of
kernel/bpf/*. btf_*() cannot stay either. By the rules infmodret.cthese get tags too:target error after the skip ---------------------- ------- --------------------------------------- btf_kfunc_check_flag() -ENOENT fentry/bpf_wq_set_callback saves 3 argsof 4. aux is garbage. That undoes commit
3917b1012ee2
bpf_mprog_detach()-ENOENTtcx_link_release()only warns.
tcx_run()runs the freed prog
Quoted 4 lines
security_bpf*() and the bpf syscall entry need no tag. They stay. So a prog can still block a bad bpf command.
Could you send a patch for the above?
Yes, will do tomorrow.
Quoted 5 lines
Quoted 3 lines
(Just in case, I am preparing patch for "fmodret -> functions returning pointers", so numbers can be a bit different later.)
Nice. Looking forward.
Will send out tomorrow as well.
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 6 lines · On Fri, 20 Nov 2026 04:28:36 +0000 you wrote:
With
CONFIG_BPF_FMODRET_SCAN=y,fmod_retprograms can attach to tagged functions, so a program onbpf_lsm_get_retval_range()can make it fail for an LSM hook that returns a value.get_func_retval_range()then reports no range, anddo_refine_retval_range()andcheck_helper_call()ignore the error and use a range the call did not set.
Quoted 6 lines
The function fails only for a void hook, which every caller but
get_func_retval_range()already rules out. Check for a void hook there and make the function return void, soresolve_btfidsno longer tags it and nofmod_retprogram can attach to it.[...]
Here is the summary with links:
- [bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
https://git.kernel.org/bpf/bpf-next/c/351668b0b8b7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html