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. Fixes: bc45414d1690 ("bpf: Allow fmod_ret on tagged functions and check the return value") Signed-off-by: Jose Fernandez (Anthropic) --- 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(-) diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h index eb4a38eef87e..14b8bb6cc25f 100644 --- a/include/linux/bpf_lsm.h +++ b/include/linux/bpf_lsm.h @@ -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) diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 0aa2fd61daba..f28754e8ac72 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -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; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1a8e5a108a68..3ebd5106358a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -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)