* [PATCH v1 bpf-next 1/7] bpf: Require the JIT for fmod_ret programs on 64-bit functions
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
@ 2026-11-25 10:27 ` Anton Protopopov
2026-11-25 10:27 ` [PATCH v1 bpf-next 2/7] bpf: Remove a wrong comment about fmod_ret targets Anton Protopopov
` (5 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:27 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
The interpreter returns R0 as unsigned int. For a function which
returns long or a pointer a trampoline may pass on all 64 bits, as on
s390x, and ERR_PTR(-EINVAL) becomes 0xffffffea, which IS_ERR() does
not catch. Commit 3c5d146f9c69 ("bpf: Require the JIT for fmod_ret
programs on tagged 64-bit targets") requires the JIT only for tagged
functions. Require it for the declared ones too.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
kernel/bpf/verifier.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index e7013ed8cadd..18c7b9a6f456 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -24882,7 +24882,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
env->fmodret_retvals = tgt_info.fmodret_retvals;
/* The interpreter returns only 32 bits */
- if (env->fmodret_retvals && tgt_info.fmodel.ret_size == 8)
+ if (prog->expected_attach_type == BPF_MODIFY_RETURN &&
+ tgt_info.fmodel.ret_size == 8)
prog->jit_required = 1;
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v1 bpf-next 2/7] bpf: Remove a wrong comment about fmod_ret targets
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
2026-11-25 10:27 ` [PATCH v1 bpf-next 1/7] bpf: Require the JIT for fmod_ret programs on 64-bit functions Anton Protopopov
@ 2026-11-25 10:27 ` Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions Anton Protopopov
` (4 subsequent siblings)
6 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:27 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
Commit 6ba43b761c41 ("bpf: Attachment verification for
BPF_MODIFY_RETURN") stated that only functions returning int are
attachable. However, the check was done only when a program actually
accessed ret, so a program could attach to a function which returns
a pointer if it didn't touch ret.
Instead of letting programs attached to functions returning pointers
read ret, just delete the comment. The reason is that a trampoline
runs an fmod_ret program only if all the earlier ones returned 0, so
ret is always 0. (Another option is to let any prog read it and mark
it as known zero, but not letting it doesn't break any old prog, as
reading ret of functions returning pointers was never allowed.)
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
kernel/bpf/btf.c | 3 ---
1 file changed, 3 deletions(-)
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index c4386a9a1ad4..3c57e66bbb36 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7553,9 +7553,6 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
t = btf_type_by_id(btf, t->type);
break;
case BPF_MODIFY_RETURN:
- /* For now the BPF_MODIFY_RETURN can only be attached to
- * functions that return an int.
- */
if (!t)
return false;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
2026-11-25 10:27 ` [PATCH v1 bpf-next 1/7] bpf: Require the JIT for fmod_ret programs on 64-bit functions Anton Protopopov
2026-11-25 10:27 ` [PATCH v1 bpf-next 2/7] bpf: Remove a wrong comment about fmod_ret targets Anton Protopopov
@ 2026-11-25 10:28 ` Anton Protopopov
2026-11-25 10:47 ` bot+bpf-ci
2026-11-25 13:38 ` Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer Anton Protopopov
` (3 subsequent siblings)
6 siblings, 2 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
The caller of such a function dereferences what an fmod_ret program
returns. Functions on the error injection list like xe_device_create()
return a pointer. Let the program return only NULL or an error pointer.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
kernel/bpf/verifier.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 18c7b9a6f456..d2475edeecd9 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -19710,6 +19710,8 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_range *range)
{
enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
+ const struct bpf_prog_aux *aux = env->prog->aux;
+ const struct btf_type *t;
/* Default return value range. */
*range = retval_range(0, 1);
@@ -19762,8 +19764,19 @@ static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_
*range = retval_range(0, 0);
break;
case BPF_TRACE_RAW_TP:
- case BPF_MODIFY_RETURN:
return false;
+ case BPF_MODIFY_RETURN:
+ /* An extension replaces a global function of the program */
+ if (!aux->attach_btf)
+ return false;
+
+ /* Allow only NULL or an error pointer for functions returning pointers */
+ t = btf_type_skip_modifiers(aux->attach_btf,
+ aux->attach_func_proto->type, NULL);
+ if (!btf_type_is_ptr(t))
+ return false;
+ *range = retval_range(-MAX_ERRNO, 0);
+ break;
case BPF_TRACE_ITER:
default:
break;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
2026-11-25 10:28 ` [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions Anton Protopopov
@ 2026-11-25 10:47 ` bot+bpf-ci
2026-11-25 13:14 ` Anton Protopopov
2026-11-25 13:38 ` Anton Protopopov
1 sibling, 1 reply; 12+ messages in thread
From: bot+bpf-ci @ 2026-11-25 10:47 UTC (permalink / raw)
To: a.s.protopopov, ast, daniel, andrii, eddyz87, memxor, almagui,
john.fastabend, bpf
Cc: a.s.protopopov, ast, andrii, daniel, martin.lau, eddyz87,
yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 3929 bytes --]
> bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
TL;DR: The new fmod_ret pointer-return check may be bypassed by an freplace
extension of the fmod_ret program's main function, so a non-error pointer like
1 could reach the caller and be dereferenced. Not covered by the selftest.
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
[ ... ]
> @@ -19762,8 +19764,19 @@ static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_
> *range = retval_range(0, 0);
> break;
> case BPF_TRACE_RAW_TP:
> - case BPF_MODIFY_RETURN:
> return false;
> + case BPF_MODIFY_RETURN:
> + /* An extension replaces a global function of the program */
> + if (!aux->attach_btf)
> + return false;
> +
> + /* Allow only NULL or an error pointer for functions returning pointers */
> + t = btf_type_skip_modifiers(aux->attach_btf,
> + aux->attach_func_proto->type, NULL);
> + if (!btf_type_is_ptr(t))
> + return false;
> + *range = retval_range(-MAX_ERRNO, 0);
> + break;
Can this restriction be bypassed by an freplace program that replaces the
main function of an fmod_ret program?
An extension verified against an fmod_ret program P gets
resolve_prog_type() == BPF_PROG_TYPE_TRACING and, in
check_attach_btf_id(), expected_attach_type == BPF_MODIFY_RETURN. Its
aux->attach_btf is NULL because bpf_prog_load() sets dst_prog instead, so
it takes this branch:
if (!aux->attach_btf)
return false;
and check_return_code() accepts any R0. env->fmodret_retvals is also NULL
for an extension, so check_fmodret_retval() does not run either.
The comment assumes the extension replaces a global function whose result
goes back to P, and P's own exit is then checked. But
bpf_check_attach_target() also lets an extension replace subprog 0 of a
tracing program. That needs linkage BTF_FUNC_GLOBAL and reliable BTF, for
example:
long p(unsigned long long *ctx __arg_ctx)
which btf_validate_prog_ctx_type() accepts for BPF_MODIFY_RETURN. Only
fentry/fexit/fsession targets are refused ("Cannot extend
fentry/fexit/fsession"); BPF_MODIFY_RETURN is not on that list. At the
end of bpf_check() the extension's expected_attach_type is reset to 0:
if (env->prog->type == BPF_PROG_TYPE_EXT)
env->prog->expected_attach_type = 0;
so bpf_tracing_prog_attach() accepts it, and __bpf_trampoline_link_prog()
then pokes a jump at P's bpf_func entry.
For example, P is:
SEC("fmod_ret/xe_device_create")
long p(unsigned long long *ctx __arg_ctx) { return 0; }
which passes the new [-4095, 0] check. E is an freplace of p that does
"return 1;". With both attached, the xe_device_create() trampoline calls
P's entry, which jumps to E. E's 1 is non-zero, so the trampoline skips
the function and returns (struct xe_device *)1 to xe_pci_probe().
IS_ERR() does not catch that, and the caller dereferences it. That is the
case the commit message says it prevents ("The caller of such a function
dereferences what an fmod_ret program returns").
The selftest added later in the series (c210c64e053c "selftests/bpf: Test
fmod_ret on a declared function that returns a pointer") only loads an
extension of the non-main global_func(), so this path is not covered, and
no later commit in the series changes this branch.
Could the range from the target program's attach_btf/attach_func_proto be
applied when an extension replaces subprog 0 of a BPF_MODIFY_RETURN
target, or could replacing the main function of an fmod_ret program be
refused? The existing fmodret_retvals check has the same extension gap
for int-returning targets.
---
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/45595181490
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
2026-11-25 10:47 ` bot+bpf-ci
@ 2026-11-25 13:14 ` Anton Protopopov
0 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 13:14 UTC (permalink / raw)
To: bot+bpf-ci
Cc: ast, daniel, andrii, eddyz87, memxor, almagui, john.fastabend,
bpf, martin.lau, yonghong.song, mason, ihor.solodrai
On Wed, Nov 25, 2026 at 11:47 AM <bot+bpf-ci@kernel.org> wrote:
>
> > bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
>
> TL;DR: The new fmod_ret pointer-return check may be bypassed by an freplace
> extension of the fmod_ret program's main function, so a non-error pointer like
> 1 could reach the caller and be dereferenced. Not covered by the selftest.
>
> > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> > --- a/kernel/bpf/verifier.c
> > +++ b/kernel/bpf/verifier.c
>
> [ ... ]
>
> > @@ -19762,8 +19764,19 @@ static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_
> > *range = retval_range(0, 0);
> > break;
> > case BPF_TRACE_RAW_TP:
> > - case BPF_MODIFY_RETURN:
> > return false;
> > + case BPF_MODIFY_RETURN:
> > + /* An extension replaces a global function of the program */
> > + if (!aux->attach_btf)
> > + return false;
> > +
> > + /* Allow only NULL or an error pointer for functions returning pointers */
> > + t = btf_type_skip_modifiers(aux->attach_btf,
> > + aux->attach_func_proto->type, NULL);
> > + if (!btf_type_is_ptr(t))
> > + return false;
> > + *range = retval_range(-MAX_ERRNO, 0);
> > + break;
>
> Can this restriction be bypassed by an freplace program that replaces the
> main function of an fmod_ret program?
>
> An extension verified against an fmod_ret program P gets
> resolve_prog_type() == BPF_PROG_TYPE_TRACING and, in
> check_attach_btf_id(), expected_attach_type == BPF_MODIFY_RETURN. Its
> aux->attach_btf is NULL because bpf_prog_load() sets dst_prog instead, so
> it takes this branch:
>
> if (!aux->attach_btf)
> return false;
>
> and check_return_code() accepts any R0. env->fmodret_retvals is also NULL
> for an extension, so check_fmodret_retval() does not run either.
>
> The comment assumes the extension replaces a global function whose result
> goes back to P, and P's own exit is then checked. But
> bpf_check_attach_target() also lets an extension replace subprog 0 of a
> tracing program. That needs linkage BTF_FUNC_GLOBAL and reliable BTF, for
> example:
>
> long p(unsigned long long *ctx __arg_ctx)
>
> which btf_validate_prog_ctx_type() accepts for BPF_MODIFY_RETURN. Only
> fentry/fexit/fsession targets are refused ("Cannot extend
> fentry/fexit/fsession"); BPF_MODIFY_RETURN is not on that list. At the
> end of bpf_check() the extension's expected_attach_type is reset to 0:
>
> if (env->prog->type == BPF_PROG_TYPE_EXT)
> env->prog->expected_attach_type = 0;
>
> so bpf_tracing_prog_attach() accepts it, and __bpf_trampoline_link_prog()
> then pokes a jump at P's bpf_func entry.
>
> For example, P is:
>
> SEC("fmod_ret/xe_device_create")
> long p(unsigned long long *ctx __arg_ctx) { return 0; }
>
> which passes the new [-4095, 0] check. E is an freplace of p that does
> "return 1;". With both attached, the xe_device_create() trampoline calls
> P's entry, which jumps to E. E's 1 is non-zero, so the trampoline skips
> the function and returns (struct xe_device *)1 to xe_pci_probe().
> IS_ERR() does not catch that, and the caller dereferences it. That is the
> case the commit message says it prevents ("The caller of such a function
> dereferences what an fmod_ret program returns").
>
> The selftest added later in the series (c210c64e053c "selftests/bpf: Test
> fmod_ret on a declared function that returns a pointer") only loads an
> extension of the non-main global_func(), so this path is not covered, and
> no later commit in the series changes this branch.
>
> Could the range from the target program's attach_btf/attach_func_proto be
> applied when an extension replaces subprog 0 of a BPF_MODIFY_RETURN
> target, or could replacing the main function of an fmod_ret program be
> refused? The existing fmodret_retvals check has the same extension gap
> for int-returning targets.
In a parallel series a patch forbids to freplace fmodret (and LSM)
progs, so this doesn't apply anymore.
(And, in fact, this also failed to load for different reasons (BTF
mismatch on load).)
>
> ---
> 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/45595181490
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
2026-11-25 10:28 ` [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions Anton Protopopov
2026-11-25 10:47 ` bot+bpf-ci
@ 2026-11-25 13:38 ` Anton Protopopov
1 sibling, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 13:38 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
On 26/11/25 10:28AM, Anton Protopopov wrote:
> The caller of such a function dereferences what an fmod_ret program
> returns. Functions on the error injection list like xe_device_create()
> return a pointer. Let the program return only NULL or an error pointer.
>
> Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
> ---
> kernel/bpf/verifier.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 18c7b9a6f456..d2475edeecd9 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -19710,6 +19710,8 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
> static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_range *range)
> {
> enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
> + const struct bpf_prog_aux *aux = env->prog->aux;
> + const struct btf_type *t;
>
> /* Default return value range. */
> *range = retval_range(0, 1);
> @@ -19762,8 +19764,19 @@ static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_
> *range = retval_range(0, 0);
> break;
> case BPF_TRACE_RAW_TP:
> - case BPF_MODIFY_RETURN:
> return false;
> + case BPF_MODIFY_RETURN:
> + /* An extension replaces a global function of the program */
> + if (!aux->attach_btf)
> + return false;
This ^ check will also go away after Alexei's series
https://lore.kernel.org/bpf/20261125032938.670542-1-alexei.starovoitov@gmail.com/
lands.
> + /* Allow only NULL or an error pointer for functions returning pointers */
> + t = btf_type_skip_modifiers(aux->attach_btf,
> + aux->attach_func_proto->type, NULL);
> + if (!btf_type_is_ptr(t))
> + return false;
> + *range = retval_range(-MAX_ERRNO, 0);
> + break;
> case BPF_TRACE_ITER:
> default:
> break;
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
` (2 preceding siblings ...)
2026-11-25 10:28 ` [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions Anton Protopopov
@ 2026-11-25 10:28 ` Anton Protopopov
2026-11-25 13:34 ` Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 5/7] bpf: Check fmod_ret return values of tagged pointer targets in 64 bits Anton Protopopov
` (2 subsequent siblings)
6 siblings, 1 reply; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
Add bpf_testmod_modify_return_ptr() to the error injection list.
Check that a program on it may return an error pointer but not
another value, and that an extension of a global function of such a
program loads.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
.../selftests/bpf/prog_tests/modify_return.c | 44 +++++++++++++++++++
tools/testing/selftests/bpf/progs/err.h | 5 +++
.../selftests/bpf/progs/modify_return_ext.c | 12 +++++
.../selftests/bpf/progs/modify_return_ptr.c | 43 ++++++++++++++++++
.../selftests/bpf/test_kmods/bpf_testmod.c | 7 +++
5 files changed, 111 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/modify_return_ext.c
create mode 100644 tools/testing/selftests/bpf/progs/modify_return_ptr.c
diff --git a/tools/testing/selftests/bpf/prog_tests/modify_return.c b/tools/testing/selftests/bpf/prog_tests/modify_return.c
index 2417030f104a..2308609de69c 100644
--- a/tools/testing/selftests/bpf/prog_tests/modify_return.c
+++ b/tools/testing/selftests/bpf/prog_tests/modify_return.c
@@ -8,6 +8,8 @@
#include <unistd.h>
#include "modify_return.skel.h"
#include "modify_return_width.skel.h"
+#include "modify_return_ptr.skel.h"
+#include "modify_return_ext.skel.h"
#define LOWER(x) ((x) & 0xffff)
#define UPPER(x) ((x) >> 16)
@@ -144,3 +146,45 @@ void test_modify_return_graph(void)
out:
ASSERT_OK(rmdir(instance), "rmdir");
}
+
+/* An extension of a global function of such a program */
+static void test_extension(void)
+{
+ struct modify_return_ptr *skel;
+ struct modify_return_ext *ext;
+ struct bpf_program *prog;
+
+ /* The function returns 64 bits */
+ if (!is_jit_enabled()) {
+ printf("%s:SKIP:JIT is disabled\n", __func__);
+ test__skip();
+ return;
+ }
+
+ skel = modify_return_ptr__open();
+ if (!ASSERT_OK_PTR(skel, "open"))
+ return;
+ bpf_object__for_each_program(prog, skel->obj)
+ bpf_program__set_autoload(prog, prog == skel->progs.calls_global);
+ if (!ASSERT_OK(modify_return_ptr__load(skel), "load"))
+ goto out;
+ ext = modify_return_ext__open();
+ if (!ASSERT_OK_PTR(ext, "open extension"))
+ goto out;
+ ASSERT_OK(bpf_program__set_attach_target(ext->progs.replace_global,
+ bpf_program__fd(skel->progs.calls_global),
+ "global_func"), "set_attach_target");
+ ASSERT_OK(modify_return_ext__load(ext), "load extension");
+ modify_return_ext__destroy(ext);
+out:
+ modify_return_ptr__destroy(skel);
+}
+
+/* NULL or an error from a declared function that returns a pointer */
+void test_modify_return_ptr(void)
+{
+ if (test__start_subtest("extension"))
+ test_extension();
+
+ RUN_TESTS(modify_return_ptr);
+}
diff --git a/tools/testing/selftests/bpf/progs/err.h b/tools/testing/selftests/bpf/progs/err.h
index 38529779a236..be3100752d35 100644
--- a/tools/testing/selftests/bpf/progs/err.h
+++ b/tools/testing/selftests/bpf/progs/err.h
@@ -25,4 +25,9 @@ static inline long PTR_ERR(const void *ptr)
return (long) ptr;
}
+static inline void *ERR_PTR(long error)
+{
+ return (void *) error;
+}
+
#endif /* __ERR_H__ */
diff --git a/tools/testing/selftests/bpf/progs/modify_return_ext.c b/tools/testing/selftests/bpf/progs/modify_return_ext.c
new file mode 100644
index 000000000000..80f70a1f452e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/modify_return_ext.c
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/* An extension of a global function of an fmod_ret program, see prog_tests/modify_return.c */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("freplace")
+int replace_global(int x)
+{
+ return 7;
+}
diff --git a/tools/testing/selftests/bpf/progs/modify_return_ptr.c b/tools/testing/selftests/bpf/progs/modify_return_ptr.c
new file mode 100644
index 000000000000..9c9d5cf543f3
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/modify_return_ptr.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "err.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("fmod_ret/bpf_testmod_modify_return_ptr")
+__load_if_JITed()
+__success
+void *BPF_PROG(error)
+{
+ return ERR_PTR(-EINVAL);
+}
+
+/* For the extension of prog_tests/modify_return.c */
+__noinline int global_func(int x)
+{
+ /*
+ * The barrier here is needed because otherwise clang can drop
+ * the call, and there is nothing to replace
+ */
+ barrier_var(x);
+ return x;
+}
+
+SEC("fmod_ret/bpf_testmod_modify_return_ptr")
+__load_if_JITed()
+__success
+void *BPF_PROG(calls_global)
+{
+ return global_func(0) ? ERR_PTR(-EINVAL) : NULL;
+}
+
+SEC("fmod_ret/bpf_testmod_modify_return_ptr")
+__failure __msg("should have been in [-4095, 0]")
+void *BPF_PROG(not_an_error)
+{
+ return (void *)1;
+}
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1fbc0061cc24..73d5992cc17a 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -774,6 +774,13 @@ noinline bool bpf_testmod_modify_return_bool(void)
}
ALLOW_ERROR_INJECTION(bpf_testmod_modify_return_bool, TRUE);
+noinline void *bpf_testmod_modify_return_ptr(void)
+{
+ asm volatile ("");
+ return NULL;
+}
+ALLOW_ERROR_INJECTION(bpf_testmod_modify_return_ptr, ERRNO);
+
noinline ssize_t
bpf_testmod_test_read(struct file *file, struct kobject *kobj,
const struct bin_attribute *bin_attr,
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer
2026-11-25 10:28 ` [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer Anton Protopopov
@ 2026-11-25 13:34 ` Anton Protopopov
0 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 13:34 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
On 26/11/25 10:28AM, Anton Protopopov wrote:
> Add bpf_testmod_modify_return_ptr() to the error injection list.
> Check that a program on it may return an error pointer but not
> another value, and that an extension of a global function of such a
> program loads.
Once the https://lore.kernel.org/bpf/20261125032938.670542-1-alexei.starovoitov@gmail.com/
series loads this selftest will fail. So I will send v2 later. Just
in case, the new patch is (after removing freplace piece):
selftests/bpf: Test fmod_ret on a declared function that returns a pointer
Add bpf_testmod_modify_return_ptr() to the error injection list.
Check that a program on it may return an error pointer but not
another value.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
diff --git a/tools/testing/selftests/bpf/prog_tests/modify_return.c b/tools/testing/selftests/bpf/prog_tests/modify_return.c
index 2417030f104a..91f810319e87 100644
--- a/tools/testing/selftests/bpf/prog_tests/modify_return.c
+++ b/tools/testing/selftests/bpf/prog_tests/modify_return.c
@@ -8,6 +8,7 @@
#include <unistd.h>
#include "modify_return.skel.h"
#include "modify_return_width.skel.h"
+#include "modify_return_ptr.skel.h"
#define LOWER(x) ((x) & 0xffff)
#define UPPER(x) ((x) >> 16)
@@ -144,3 +145,9 @@ void test_modify_return_graph(void)
out:
ASSERT_OK(rmdir(instance), "rmdir");
}
+
+/* NULL or an error from a declared function that returns a pointer */
+void test_modify_return_ptr(void)
+{
+ RUN_TESTS(modify_return_ptr);
+}
diff --git a/tools/testing/selftests/bpf/progs/err.h b/tools/testing/selftests/bpf/progs/err.h
index 38529779a236..be3100752d35 100644
--- a/tools/testing/selftests/bpf/progs/err.h
+++ b/tools/testing/selftests/bpf/progs/err.h
@@ -25,4 +25,9 @@ static inline long PTR_ERR(const void *ptr)
return (long) ptr;
}
+static inline void *ERR_PTR(long error)
+{
+ return (void *) error;
+}
+
#endif /* __ERR_H__ */
diff --git a/tools/testing/selftests/bpf/progs/modify_return_ptr.c b/tools/testing/selftests/bpf/progs/modify_return_ptr.c
new file mode 100644
index 000000000000..dfc6f0185caf
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/modify_return_ptr.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "err.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("fmod_ret/bpf_testmod_modify_return_ptr")
+__load_if_JITed()
+__success
+void *BPF_PROG(error)
+{
+ return ERR_PTR(-EINVAL);
+}
+
+SEC("fmod_ret/bpf_testmod_modify_return_ptr")
+__failure __msg("should have been in [-4095, 0]")
+void *BPF_PROG(not_an_error)
+{
+ return (void *)1;
+}
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1fbc0061cc24..73d5992cc17a 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -774,6 +774,13 @@ noinline bool bpf_testmod_modify_return_bool(void)
}
ALLOW_ERROR_INJECTION(bpf_testmod_modify_return_bool, TRUE);
+noinline void *bpf_testmod_modify_return_ptr(void)
+{
+ asm volatile ("");
+ return NULL;
+}
+ALLOW_ERROR_INJECTION(bpf_testmod_modify_return_ptr, ERRNO);
+
noinline ssize_t
bpf_testmod_test_read(struct file *file, struct kobject *kobj,
const struct bin_attribute *bin_attr,
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v1 bpf-next 5/7] bpf: Check fmod_ret return values of tagged pointer targets in 64 bits
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
` (3 preceding siblings ...)
2026-11-25 10:28 ` [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer Anton Protopopov
@ 2026-11-25 10:28 ` Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 6/7] resolve_btfids: Tag the functions that return ERR_PTR() Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 7/7] selftests/bpf: Test fmod_ret on functions that return a pointer Anton Protopopov
6 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
A later patch tags the functions that return ERR_PTR().
check_fmodret_retval() compares the return value in 32 bits if
t->size of the return type is 4. For a pointer t->size is the id of
the type it points to. Compare it in 64 bits as for long.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
kernel/bpf/verifier.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index d2475edeecd9..8e866f3ea7f4 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -19903,9 +19903,11 @@ static int check_fmodret_retval(struct bpf_verifier_env *env, int regno,
*
* The trampolines of x86-64, arm64 and s390x do that: look here before
* another architecture is added to CONFIG_BPF_FMODRET_SCAN.
+ *
+ * A pointer is compared in 64 bits. Its t->size is the id of the type it points to.
*/
t = btf_type_skip_modifiers(aux->attach_btf, aux->attach_func_proto->type, NULL);
- if (t->size == 4 && reg_umax(reg) <= U32_MAX) {
+ if (!btf_type_is_ptr(t) && t->size == 4 && reg_umax(reg) <= U32_MAX) {
smin = reg_s32_min(reg);
smax = reg_s32_max(reg);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v1 bpf-next 6/7] resolve_btfids: Tag the functions that return ERR_PTR()
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
` (4 preceding siblings ...)
2026-11-25 10:28 ` [PATCH v1 bpf-next 5/7] bpf: Check fmod_ret return values of tagged pointer targets in 64 bits Anton Protopopov
@ 2026-11-25 10:28 ` Anton Protopopov
2026-11-25 10:28 ` [PATCH v1 bpf-next 7/7] selftests/bpf: Test fmod_ret on functions that return a pointer Anton Protopopov
6 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
A function that returns ERR_PTR(-EINVAL) fails in the same way as one
that returns -EINVAL. Its callers check the pointer with IS_ERR() and
get the error with PTR_ERR(). But resolve_btfids looks only at the
functions that return an integer.
Look at the functions that return a pointer too. A pointer has 8
bytes and its errors are sign extended as for a function that returns
long. NULL is not listed: a program that returns 0 lets the
trampoline call the function.
The verifier checks all 64 bits of the return value of a program on
such a function and requires the JIT for it, as for a function that
returns long.
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
tools/bpf/resolve_btfids/fmodret.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c
index e1afdb6dedd5..db9794a88aef 100644
--- a/tools/bpf/resolve_btfids/fmodret.c
+++ b/tools/bpf/resolve_btfids/fmodret.c
@@ -29,8 +29,8 @@
/* More than any arch has argument registers */
#define MAX_PARAMS 16
-/* Return the size of the integer which FUNC returns, or 0 */
-static __u32 int_return_size(const struct btf *btf, const struct btf_type *func)
+/* Return the size of the integer or of the pointer which FUNC returns, or 0 */
+static __u32 return_size(const struct btf *btf, const struct btf_type *func)
{
const struct btf_type *t = btf__type_by_id(btf, func->type);
int ret = t->type ? btf__resolve_type(btf, t->type) : -1;
@@ -38,6 +38,8 @@ static __u32 int_return_size(const struct btf *btf, const struct btf_type *func)
if (ret < 0)
return 0;
t = btf__type_by_id(btf, ret);
+ if (btf_is_ptr(t))
+ return 8;
return btf_is_int(t) || btf_is_any_enum(t) ? t->size : 0;
}
@@ -80,7 +82,8 @@ static int cmp_error(const void *va, const void *vb)
/*
* Write the tag for a function which returns SIZE bytes, and in them one of the
- * NR VALUES of the return value register. In 8 bytes an error is sign extended.
+ * NR VALUES of the return value register. In 8 bytes an error is sign extended,
+ * as in ERR_PTR(). NULL is 0: no program can return it instead of the function.
* Return false if there is nothing to tag.
*
* The machine code cannot tell the -EPERM of a permission check from the -1 of a
@@ -217,7 +220,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose)
if (!btf_is_func(t))
continue;
nr_funcs++;
- size = int_return_size(btf, t);
+ size = return_size(btf, t);
if ((size != 4 && size != 8) || !fits_trampoline(btf, t))
continue;
/* Init code is gone before a program can attach */
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v1 bpf-next 7/7] selftests/bpf: Test fmod_ret on functions that return a pointer
2026-11-25 10:27 [PATCH v1 bpf-next 0/7] Tag fmod_ret functions that return ERR_PTR() Anton Protopopov
` (5 preceding siblings ...)
2026-11-25 10:28 ` [PATCH v1 bpf-next 6/7] resolve_btfids: Tag the functions that return ERR_PTR() Anton Protopopov
@ 2026-11-25 10:28 ` Anton Protopopov
6 siblings, 0 replies; 12+ messages in thread
From: Anton Protopopov @ 2026-11-25 10:28 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, Alan Maguire,
John Fastabend, bpf
Cc: Anton Protopopov
bpf_testmod_retval_pointer() returns ERR_PTR(-EINVAL) or NULL. Expect
the tag "bpf_fmodret_retval:-22" for it and check that a program on
it may return ERR_PTR(-EINVAL).
Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
---
.../testing/selftests/bpf/prog_tests/fmodret_retval.c | 2 +-
tools/testing/selftests/bpf/progs/fmodret_retval.c | 10 ++++++++++
.../selftests/bpf/test_kmods/bpf_testmod_retval.c | 2 +-
3 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/fmodret_retval.c b/tools/testing/selftests/bpf/prog_tests/fmodret_retval.c
index 281c6192e570..ef2a57f52dac 100644
--- a/tools/testing/selftests/bpf/prog_tests/fmodret_retval.c
+++ b/tools/testing/selftests/bpf/prog_tests/fmodret_retval.c
@@ -24,7 +24,7 @@ static const struct {
{ "bpf_testmod_retval_no_effect", "-28,-22,-16" },
{ "bpf_testmod_retval_err_ptr", "-34,-14" },
{ "bpf_testmod_retval_no_branch", "-14" },
- { "bpf_testmod_retval_pointer", "" },
+ { "bpf_testmod_retval_pointer", "-22" },
{ "bpf_testmod_retval_argument", "" },
{ "bpf_testmod_retval_notrace", "" },
{ "bpf_testmod_retval_variadic", "" },
diff --git a/tools/testing/selftests/bpf/progs/fmodret_retval.c b/tools/testing/selftests/bpf/progs/fmodret_retval.c
index 112f1575ce1f..699f3603dfa0 100644
--- a/tools/testing/selftests/bpf/progs/fmodret_retval.c
+++ b/tools/testing/selftests/bpf/progs/fmodret_retval.c
@@ -10,6 +10,7 @@
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "bpf_experimental.h"
+#include "err.h"
char _license[] SEC("license") = "GPL";
@@ -108,6 +109,15 @@ __naked void zero_extended(void)
::: __clobber_all);
}
+/* NULL calls the function */
+SEC("fmod_ret/bpf_testmod_retval_pointer")
+__load_if_JITed()
+__success
+void *BPF_PROG(err_ptr, int value)
+{
+ return value ? ERR_PTR(-EINVAL) : NULL;
+}
+
/* A privileged program may return a pointer, but not here */
SEC("fmod_ret/bpf_testmod_retval_called")
__failure __msg("R0 is not a known value (ctx)")
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c
index 85722e48d34c..ec093098108b 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c
@@ -131,7 +131,7 @@ static __used noinline int bpf_testmod_retval_no_branch(unsigned long left)
return left ? -EFAULT : 0;
}
-/* Not an integer, and an error which is not returned */
+/* ERR_PTR() is an error, NULL is not */
static __used noinline void *bpf_testmod_retval_pointer(int value)
{
return value ? ERR_PTR(-EINVAL) : NULL;
--
2.43.0
^ permalink raw reply related [flat|nested] 12+ messages in thread