BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
@ 2026-11-20  4:28 Jose Fernandez (Anthropic)
  2026-11-23 13:08 ` Anton Protopopov
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jose Fernandez (Anthropic) @ 2026-11-20  4:28 UTC (permalink / raw)
  To: KP Singh, Matt Bobrowski, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Anton Protopopov
  Cc: bpf, linux-kernel, Jose Fernandez (Anthropic)

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) <jose.fernandez@linux.dev>
---
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) <jose.fernandez@linux.dev>


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-20  4:28 [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void Jose Fernandez (Anthropic)
@ 2026-11-23 13:08 ` Anton Protopopov
  2026-11-24  0:28 ` Alexei Starovoitov
  2026-11-24  0:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-11-23 13:08 UTC (permalink / raw)
  To: Jose Fernandez (Anthropic)
  Cc: KP Singh, Matt Bobrowski, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, bpf,
	linux-kernel

On 26/11/20 04:28AM, Jose Fernandez (Anthropic) wrote:
> 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) <jose.fernandez@linux.dev>

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,

Acked-by: Anton Protopopov <a.s.protopopov@gmail.com>

> ...

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-20  4:28 [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void Jose Fernandez (Anthropic)
  2026-11-23 13:08 ` Anton Protopopov
@ 2026-11-24  0:28 ` Alexei Starovoitov
  2026-11-24 14:15   ` Anton Protopopov
  2026-11-24  0:30 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-24  0:28 UTC (permalink / raw)
  To: Jose Fernandez (Anthropic), KP Singh, Matt Bobrowski,
	Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, Anton Protopopov
  Cc: bpf

On Thu, Nov 19, 2026 at 08:28 PM Jose Fernandez (Anthropic) <jose.fernandez@linux.dev> wrote:
> and make the function return void, so resolve_btfids no longer tags
> it and no fmod_ret program 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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-20  4:28 [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void Jose Fernandez (Anthropic)
  2026-11-23 13:08 ` Anton Protopopov
  2026-11-24  0:28 ` Alexei Starovoitov
@ 2026-11-24  0:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-11-24  0:30 UTC (permalink / raw)
  To: Jose Fernandez
  Cc: kpsingh, matt, ast, daniel, andrii, eddyz87, memxor,
	a.s.protopopov, bpf, linux-kernel

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Fri, 20 Nov 2026 04:28:36 +0000 you wrote:
> 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.
> 
> [...]

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-24  0:28 ` Alexei Starovoitov
@ 2026-11-24 14:15   ` Anton Protopopov
  2026-11-24 19:46     ` Alexei Starovoitov
  0 siblings, 1 reply; 7+ messages in thread
From: Anton Protopopov @ 2026-11-24 14:15 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Jose Fernandez (Anthropic), KP Singh, Matt Bobrowski,
	Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, bpf

On Tue, Nov 24, 2026 at 1:28 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Thu, Nov 19, 2026 at 08:28 PM Jose Fernandez (Anthropic) <jose.fernandez@linux.dev> wrote:
> > and make the function return void, so resolve_btfids no longer tags
> > it and no fmod_ret program 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.

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.)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-24 14:15   ` Anton Protopopov
@ 2026-11-24 19:46     ` Alexei Starovoitov
  2026-11-24 19:50       ` Anton Protopopov
  0 siblings, 1 reply; 7+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 19:46 UTC (permalink / raw)
  To: Anton Protopopov
  Cc: Jose Fernandez (Anthropic), KP Singh, Matt Bobrowski,
	Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, bpf

On Tue, Nov 24, 2026 at 06:15 AM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> 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 image

Let'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.

> 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?

> (Just in case, I am preparing patch for "fmodret -> functions
> returning pointers", so numbers can be a bit different later.)

Nice. Looking forward.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void
  2026-11-24 19:46     ` Alexei Starovoitov
@ 2026-11-24 19:50       ` Anton Protopopov
  0 siblings, 0 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-11-24 19:50 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Jose Fernandez (Anthropic), KP Singh, Matt Bobrowski,
	Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman,
	Kumar Kartikeya Dwivedi, bpf

On Tue, Nov 24, 2026 at 8:46 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Nov 24, 2026 at 06:15 AM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> > 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 image
>
> Let'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.
>
> > 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?

Yes, will do tomorrow.

>
> > (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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-24 19:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  4:28 [PATCH bpf-next] bpf, lsm: Make bpf_lsm_get_retval_range() return void Jose Fernandez (Anthropic)
2026-11-23 13:08 ` Anton Protopopov
2026-11-24  0:28 ` Alexei Starovoitov
2026-11-24 14:15   ` Anton Protopopov
2026-11-24 19:46     ` Alexei Starovoitov
2026-11-24 19:50       ` Anton Protopopov
2026-11-24  0:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox