Tag fmod_ret functions that return ERR_PTR()
Subsystems (5)
cover Tag fmod_ret functions that return ERR_PTR()
With CONFIG_BPF_FMODRET_SCAN=y the build tags the functions that return errors, so that an fmod_ret program can attach to them. But it looks only at the functions that return an integer. This set tags the functions that return ERR_PTR() too.
One note is that it turned out that verifier already allowed to attach fmod_ret programs to functions returning pointers (see patch 2). Besides cleanups, the two main changes are:
* From now on we disallow fmod_ret programs attaching to targets returning pointers to return anything but -errno or 0 (meaning, as in the integer case, "exec the original function").
* We tag new targets in resolve_btfids
Patches 1-3 are fixes for the declared targets. Patch 1 requires the JIT for every fmod_ret program on a function which returns 64 bits, because the interpreter returns only 32 of them. Patch 2 removes a wrong comment in btf_ctx_access(): fmod_ret programs can already attach to functions which return pointers, they just can't read ret. Some functions on the error injection list return a pointer, like xe_device_create(). An fmod_ret program on such a function may return any value and the caller dereferences it. Patch 3 allows only NULL or an error pointer. Patch 4 tests it, together with an extension of a global function called from such a program. Patches 5-7 enable tagging for new functions and test them.
In an arm64 vmlinux built with clang this series tags 1380 more
functions: 15804 instead of 14424.
As for the integer functions, among the callers of the new targets there are ones which do not check the error, and a few functions get a tag though they never return an error pointer: they turn one into NULL, as inet_frag_find() does with "IS_ERR_OR_NULL(prev) ? fq :
prev". I do not fix this here, as this is a shared broblem with integer functions, a follow-up will be send later.
Anton Protopopov (7):
bpf: Require the JIT for fmod_ret programs on 64-bit functions
bpf: Remove a wrong comment about fmod_ret targets
bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions selftests/bpf: Test fmod_ret on a declared function that returns a
pointer
bpf: Check fmod_ret return values of tagged pointer targets in 64 bits
resolve_btfids: Tag the functions that return ERR_PTR() selftests/bpf: Test fmod_ret on functions that return a pointer
kernel/bpf/btf.c | 3 -- kernel/bpf/verifier.c | 22 ++++++++-- tools/bpf/resolve_btfids/fmodret.c | 11 +++-- .../selftests/bpf/prog_tests/fmodret_retval.c | 2 +- .../selftests/bpf/prog_tests/modify_return.c | 44 +++++++++++++++++++ tools/testing/selftests/bpf/progs/err.h | 5 +++ .../selftests/bpf/progs/fmodret_retval.c | 10 +++++ .../selftests/bpf/progs/modify_return_ext.c | 12 +++++ .../selftests/bpf/progs/modify_return_ptr.c | 43 ++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 7 +++ .../bpf/test_kmods/bpf_testmod_retval.c | 2 +- 11 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/modify_return_ext.c create mode 100644 tools/testing/selftests/bpf/progs/modify_return_ptr.c
Signature · 1 line
2.43.0
PATCH 1/7 bpf: Require the JIT for fmod_ret programs on 64-bit functions
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.
--- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -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 ;/*
Signature · 1 line
2.43.0
PATCH 2/7 bpf: Remove a wrong comment about fmod_ret targets
In reply to Anton ProtopopovCommit 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.)
--- kernel/bpf/btf.c | 3 --- 1 file changed, 3 deletions(-)
kernel/bpf/btf.c +0 -3
@@ -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;
Signature · 1 line
2.43.0
PATCH 3/7 bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
In reply to Anton ProtopopovThe 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.
--- kernel/bpf/verifier.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +14 -1
@@ -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 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v1 bpf-next 3/7] 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…
Quoted 1 line
bpf: Allow only NULL or
ERR_PTR()fromfmod_reton 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.
Quoted 3 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 8 lines
kernel/bpf/verifier.c +2
-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 */
kernel/bpf/verifier.c static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_ · line 19779
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+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
Re: [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
Quoted 84 lines · On Wed, Nov 25, 2026 at 11:47 AM <[email protected]> wrote:
Quoted 2 lines
bpf: Allow only NULL or
ERR_PTR()fromfmod_reton pointer functionsTL;DR: The new
fmod_retpointer-return check may be bypassed by an freplace extension of thefmod_retprogram's main function, so a non-error pointer like 1 could reach the caller and be dereferenced. Not covered by the selftest.Quoted 3 lines
kernel/bpf/verifier.c +0
-0[ ... ]
Quoted 8 lines
kernel/bpf/verifier.c +2
-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 */ kernel/bpf/verifier.c static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_ · line 19779
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+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_retprogram?An extension verified against an
fmod_retprogram P getsresolve_prog_type()==BPF_PROG_TYPE_TRACINGand, incheck_attach_btf_id(),expected_attach_type==BPF_MODIFY_RETURN. Itsaux->attach_btfis NULL becausebpf_prog_load()setsdst_proginstead, so it takes this branch:if (!aux->attach_btf) return false;and
check_return_code()accepts any R0.env->fmodret_retvalsis also NULL for an extension, socheck_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 linkageBTF_FUNC_GLOBALand reliable BTF, for example:long
p(unsigned long long *ctx __arg_ctx)which
btf_validate_prog_ctx_type()accepts forBPF_MODIFY_RETURN. Only fentry/fexit/fsession targets are refused ("Cannot extend fentry/fexit/fsession");BPF_MODIFY_RETURNis not on that list. At the end ofbpf_check()the extension'sexpected_attach_typeis 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'sbpf_funcentry.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 (structxe_device*)1 toxe_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 anfmod_retprogram returns").The selftest added later in the series (
c210c64e053c"selftests/bpf: Testfmod_reton a declared function that returns a pointer") only loads an extension of the non-mainglobal_func(), so this path is not covered, and no later commit in the series changes this branch.
Quoted 5 lines
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_RETURNtarget, or could replacing the main function of anfmod_retprogram be refused? The existingfmodret_retvalscheck 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).)
Quoted 6 lines
---
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.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/45595181490
Re: [PATCH v1 bpf-next 3/7] bpf: Allow only NULL or ERR_PTR() from fmod_ret on pointer functions
In reply to Anton ProtopopovQuoted 23 lines · Anton Protopopov
The caller of such a function dereferences what an
fmod_retprogram returns. Functions on the error injection list likexe_device_create()return a pointer. Let the program return only NULL or an error pointer.Signed-off-by Anton Protopopov <[email protected]>--- kernel/bpf/verifier.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)kernel/bpf/verifier.c +2
-0@@ -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 );
kernel/bpf/verifier.c static bool return_retval_range(struct bpf_verifier_env *env, struct bpf_retval_ · line 19771
Quoted 9 lines
kernel/bpf/verifier.c +4
-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/[email protected]/
lands.
Quoted 13 lines
kernel/bpf/verifier.c +7
-0+/* 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 ;Signature · 1 line
2.43.0
PATCH 4/7 selftests/bpf: Test fmod_ret on a declared function that returns a pointer
In reply to Anton ProtopopovAdd 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.
--- .../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
tools/testing/selftests/bpf/prog_tests/modify_return.c +44 -0
@@ -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); +}
tools/testing/selftests/bpf/progs/err.h +5 -0
@@ -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__ */
tools/testing/selftests/bpf/progs/modify_return_ext.c new file +12 -0
@@ -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 ; +}
tools/testing/selftests/bpf/progs/modify_return_ptr.c new file +43 -0
43 lines not on this page · raw message
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +7 -0
@@ -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) +{ + asmvolatile ("" ); +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,
Signature · 1 line
2.43.0
Re: [PATCH v1 bpf-next 4/7] selftests/bpf: Test fmod_ret on a declared function that returns a pointer
Quoted 4 lines · 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.
Once the https://lore.kernel.org/bpf/[email protected]/
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.
tools/testing/selftests/bpf/prog_tests/modify_return.c +7 -0
@@ -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); +}
tools/testing/selftests/bpf/progs/err.h +5 -0
@@ -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__ */
tools/testing/selftests/bpf/progs/modify_return_ptr.c new file +24 -0
@@ -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 ; +}
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +7 -0
@@ -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) +{ + asmvolatile ("" ); +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,
PATCH 5/7 bpf: Check fmod_ret return values of tagged pointer targets in 64 bits
In reply to Anton ProtopopovA 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.
--- kernel/bpf/verifier.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +3 -1
@@ -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 &®_umax (reg) <=U32_MAX ) { +if (!btf_type_is_ptr (t) && t->size ==4 &®_umax (reg) <=U32_MAX ) { smin =reg_s32_min (reg); smax =reg_s32_max (reg); }
Signature · 1 line
2.43.0
PATCH 6/7 resolve_btfids: Tag the functions that return ERR_PTR()
In reply to Anton ProtopopovA 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.
--- tools/bpf/resolve_btfids/fmodret.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/bpf/resolve_btfids/fmodret.c +7 -4
@@ -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 */
Signature · 1 line
2.43.0
PATCH 7/7 selftests/bpf: Test fmod_ret on functions that return a pointer
In reply to Anton Protopopovbpf_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).
--- .../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(-)
tools/testing/selftests/bpf/prog_tests/fmodret_retval.c +1 -1
@@ -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", "" },
tools/testing/selftests/bpf/progs/fmodret_retval.c +10 -0
@@ -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)")
tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c +1 -1
@@ -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 ;
Signature · 1 line
2.43.0