bpf: Add multi-level pointer parameter support for trampolines
Subsystems (5)
v3 cover bpf: Add multi-level pointer parameter support for trampolines
This patch adds BPF verifier support for multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as SCALAR_VALUE.
Background:
Prior to these changes, accessing multi-level pointer parameters or return values through BPF trampoline context arrays resulted in verification failures in btf_ctx_access, producing errors such as:
func '%s' arg%d type %s is not a struct
For example, consider a BPF program that logs an input parameter of type struct posix_acl **:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
bpf_printk("__posix_acl_chmod ppacl = %px\n", ppacl);
return 0;
}This program failed BPF verification with the following error:
libbpf: prog 'trace_posix_acl_chmod': -- BEGIN PROG LOAD LOG -- 0: R1=ctx() R10=fp0
; int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp, umode_t mode) @ posix_acl_monitor.bpf.c:23
0: (79) r6 = *(u64 *)(r1 +16) ; R1=ctx() R6_w=scalar()
1: (79) r1 = *(u64 *)(r1 +0)
func '__posix_acl_chmod' arg0 type PTR is not a struct invalid bpf_context access off=0 size=8
processed 2 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
The common workaround involved using helper functions to fetch parameter values by passing the address of the context array entry:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
struct posix_acl **pp;
bpf_probe_read_kernel(&pp, sizeof(ppacl), &ctx[0]);
bpf_printk("__posix_acl_chmod %px\n", pp);
return 0;
}This approach introduced helper call overhead and created inconsistency with parameter access patterns.
Improvements:
With this patch, trampoline programs can directly access multi-level pointer parameters, eliminating helper call overhead and explicit ctx access while ensuring consistent parameter handling. For example, the following ctx access with a helper call:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
struct posix_acl **pp;
bpf_probe_read_kernel(&pp, sizeof(pp), &ctx[0]);
bpf_printk("__posix_acl_chmod %px\n", pp);
...
}is replaced by a load instruction:
SEC("fentry/__posix_acl_chmod")
int BPF_PROG(trace_posix_acl_chmod, struct posix_acl **ppacl, gfp_t gfp,
umode_t mode)
{
bpf_printk("__posix_acl_chmod %px\n", ppacl);
...
}The bpf_core_cast macro can be used for deeper level dereferences, as illustrated in the tests added by this patch.
v1 -> v2:
* corrected maintainer's email
v2 -> v3:
* Addressed reviewers feedback:
* Changed the register type from PTR_TO_MEM to SCALAR_VALUE.
* Modified tests to accommodate SCALAR_VALUE handling.
* Fixed a compilation error for loongarch
* https://lore.kernel.org/oe-kbuild-all/[email protected]/
* Addressed AI bot review
* Added a commentary to address a NULL pointer case
* Removed WARN_ON
* Fixed a commentary
Slava Imameev (2):
bpf: Support multi-level pointer params via SCALAR_VALUE for
trampolines
selftests/bpf: Add trampolines multi-level pointer params test
coverage
kernel/bpf/btf.c | 20 +- net/bpf/test_run.c | 130 ++++++ .../prog_tests/fentry_fexit_multi_level_ptr.c | 206 +++++++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../progs/fentry_fexit_pptr_nullable_test.c | 56 +++ .../bpf/progs/fentry_fexit_pptr_test.c | 67 +++ .../bpf/progs/fentry_fexit_void_ppptr_test.c | 38 ++ .../bpf/progs/fentry_fexit_void_pptr_test.c | 71 +++ .../bpf/progs/verifier_ctx_multilevel_ptr.c | 435 ++++++++++++++++++ 9 files changed, 1024 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fentry_fexit_multi_level_ptr.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_nullable_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_ppptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_multilevel_ptr.c
Signature · 1 line
2.34.1
PATCH v3 1/2 bpf: Support multi-level pointer params via SCALAR_VALUE for trampolines
Add BPF verifier support for multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as SCALAR_VALUE.
--- kernel/bpf/btf.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +19 -1
@@ -760,6 +760,21 @@ const struct btf_type *btf_type_resolve_func_ptr(const struct btf *btf,return NULL ; } +static bool is_multilevel_ptr (const struct btf *btf,const struct btf_type *t) +{ +u32 depth =0 ; + +if (!btf_type_is_ptr (t)) +return false; + +do { + depth +=1 ; + t =btf_type_skip_modifiers (btf, t->type,NULL ); + }while (btf_type_is_ptr (t) && depth <2 ); + +return depth >1 ; +} +/* Types that act only as a source, not sink or intermediate * type when resolving. */
@@ -6905,8 +6920,11 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* * If it's a pointer to void, it's the same as scalar from the verifier * safety POV. Either way, no futher pointer walking is allowed. +* Multilevel pointers (e.g., int**, struct foo**, char***) of any type +* are treated as scalars because the verifier lacks the context to infer +* the size of their target memory regions. */ -if (is_void_or_int_ptr (btf, t)) +if (is_void_or_int_ptr (btf, t) ||is_multilevel_ptr (btf, t))return true;/* this is a pointer to another type */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH bpf-next v3 1/2] bpf: Support multi-level pointer params via SCALAR_VALUE for trampolines
"I'm less certain about 'char **'. If we make it scalar too it will be harder to make it a pointer to nul terminated string later.
Quoted 4 lines
kernel/bpf/btf.c +0
-0
[ ... ]
kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6928
Quoted 11 lines
kernel/bpf/btf.c +4
-1@@ -6905,8 +6920,11 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,/* * If it's a pointer to void, it's the same as scalar from the verifier * safety POV. Either way, no futher pointer walking is allowed. +* Multilevel pointers (e.g., int**, struct foo**, char***) of any type +* are treated as scalars because the verifier lacks the context to infer +* the size of their target memory regions. */ -if (is_void_or_int_ptr (btf, t)) +if (is_void_or_int_ptr (btf, t) ||is_multilevel_ptr (btf, t))return true;
In the v2 review thread, Alexei Starovoitov wrote:
"I'm less certain about 'char **'. If we make it scalar too
it will be harder to make it a pointer to nul terminated string later.
So I would do 'void **' -> scalar for now only."
https://lore.kernel.org/bpf/CAADnVQ+xwC+J1hZcHPEUjBoKcUKmnucG4AFQ-kkDmqx+C-GcoA@mail.gmail.com/
The comment says "of any type" and is_multilevel_ptr() matches all multi-level pointers (int**, struct foo**, char***), which is broader than the requested scope. Was the decision to treat all multi-level pointer types as scalar discussed with Alexei after the v2 feedback?
---
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/22298698870
AI-authorship-score: low
AI-authorship-explanation: Code exhibits human-authored characteristics including idiomatic kernel C patterns, iterative revision based on reviewer feedback across three versions, and deep domain knowledge of BPF verifier internals.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Unaddressed maintainer feedback about limiting scope to void** pointers only; the broader approach is functionally safe but contradicts the requested design direction.
Re: Re: [PATCH bpf-next v3 1/2] bpf: Support multi-level pointer params via SCALAR_VALUE for trampolines
I think support for broader types can be provided in a compatible way with future annotated support, as I explained in my reply to the v2 review:
"
Quoted 7 lines
so I suggest treating 'void **' as a scalar as Eduard suggested. This particular
sb_eat_lsm_opts()hook
doesn't have a useful type behind it anyway.
I'm less certain about 'char **'. If we make it scalar too it will be harder to make it a pointer to nul terminated string later.So I would do 'void **' -> scalar for now only.
I changed to scalar in v3, keeping broader scope for pointer types.
We encountered double pointers of various types that required workarounds, such as:
int __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Adding support for void** alone doesn't address the broader issue with other double pointer types.
When annotated array support (including char**) is added in the future, it should remain compatible with the scalar approach for legacy (unannotated) parameters. Unannotated parameters will continue using scalar handling.
"
Re: Re: [PATCH bpf-next v3 1/2] bpf: Support multi-level pointer params via SCALAR_VALUE for trampolines
Quoted 21 lines · Slava Imameev
I think support for broader types can be provided in a compatible way with future annotated support, as I explained in my reply to the v2 review:
"
Quoted 7 lines
so I suggest treating 'void **' as a scalar as Eduard suggested. This particular
sb_eat_lsm_opts()hook
doesn't have a useful type behind it anyway.
I'm less certain about 'char **'. If we make it scalar too it will be harder to make it a pointer to nul terminated string later.So I would do 'void **' -> scalar for now only.
I changed to scalar in v3, keeping broader scope for pointer types.
We encountered double pointers of various types that required workarounds, such as:
int
__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
Quoted 8 lines
Adding support for void** alone doesn't address the broader issue with other double pointer types.
When annotated array support (including char**) is added in the future, it should remain compatible with the scalar approach for legacy (unannotated) parameters. Unannotated parameters will continue using scalar handling.
"
but then there is no need to relax it for double pointers only.
- if (is_void_or_int_ptr(btf, t))
+ if (is_void_or_int_ptr(btf, t) || !btf_type_is_struct_ptr(btf, t))
would make 'scalar' a fallback for anything unrecognized, and we can argue that making it smarter in the future maybe hopefully won't break progs.
Re: Re: [PATCH bpf-next v3 1/2] bpf: Support multi-level pointer params via SCALAR_VALUE for trampolines
Quoted 38 lines · Alexei Starovoitov
Quoted 11 lines · On Mon, Feb 23, 2026 at 2:48=E2=80=AFAM Slava Imameev…
I think support for broader types can be provided in a compatible way with future annotated support, as I explained in my reply to the v2 review:
"
Quoted 5 lines
so I suggest treating 'void **' as a scalar as Eduard suggested. This particular
sb_eat_lsm_opts()hook
doesn't have a useful type behind it anyway.
I'm less certain about 'char **'. If we make it scalar too it will be harder to make it a pointer to nul terminated string later=.
Quoted 10 lines
Quoted 2 lines
So I would do 'void **' -> scalar for now only.
I changed to scalar in v3, keeping broader scope for pointer types.
We encountered double pointers of various types that required workarounds, such as:
int
__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)Quoted 8 lines
Adding support for void** alone doesn't address the broader issue with other double pointer types.
When annotated array support (including char**) is added in the future, it should remain compatible with the scalar approach for legacy (unannotated) parameters. Unannotated parameters will continue using scalar handling.
"but then there is no need to relax it for double pointers only.
(quoted diff) +1
-1- if (is_void_or_int_ptr(btf, t)) + if (is_void_or_int_ptr(btf, t) || !btf_type_is_struct_ptr(btf, t))
Quoted 3 lines
would make 'scalar' a fallback for anything unrecognized, and we can argue that making it smarter in the future maybe hopefully won't break progs.
I can increase coverage to anything that's not a pointer to structure.
I added support only for multi-level pointers of any type because we needed workarounds for them in our development. This was the real case we encountered in practice.
Multi-level pointers support appears to be a safe extension since any future support for arrays and output values would require annotation (similar to Microsoft SAL), which differentiates between current unannotated scalar cases and new annotated cases.
PATCH v3 2/2 selftests/bpf: Add trampolines multi-level pointer params test coverage
In reply to Slava ImameevMulti-level pointer params and return value test coverage for BPF trampolines:
- fentry/fexit programs covering struct and void double/triple pointer parameters and returned values
- verifier context tests covering multi-level pointers as parameters
- verifier context tests covering multi-level pointers as returned values
- verifier context tests for lsm to check trusted parameters handling
- verifier context tests covering out-of-bound access after cast
- verifier BPF helper tests to validate no change in verifier behaviour
--- net/bpf/test_run.c | 130 ++++++ .../prog_tests/fentry_fexit_multi_level_ptr.c | 206 +++++++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../progs/fentry_fexit_pptr_nullable_test.c | 56 +++ .../bpf/progs/fentry_fexit_pptr_test.c | 67 +++ .../bpf/progs/fentry_fexit_void_ppptr_test.c | 38 ++ .../bpf/progs/fentry_fexit_void_pptr_test.c | 71 +++ .../bpf/progs/verifier_ctx_multilevel_ptr.c | 435 ++++++++++++++++++ 8 files changed, 1005 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fentry_fexit_multi_level_ptr.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_nullable_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_ppptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_multilevel_ptr.c
net/bpf/test_run.c +130 -0
@@ -24,6 +24,9 @@#include <net/netdev_rx_queue.h> #include <net/xdp.h> #include <net/netfilter/nf_bpf_link.h> +#include <linux/set_memory.h> +#include <linux/string.h> +#include <asm/tlbflush.h> #define CREATE_TRACE_POINTS #include <trace/events/bpf_test_run.h>
@@ -563,6 +566,42 @@ noinline int bpf_fentry_test10(const void *a)return (long )a; } +struct bpf_fentry_test_pptr_t { +u32 value1; +u32 value2; +}; + +noinline int bpf_fentry_test11_pptr_nullable(struct bpf_fentry_test_pptr_t **pptr__nullable) +{ +if (!pptr__nullable) +return -1 ; + +return (*pptr__nullable)->value1; +} + +noinline u32 **bpf_fentry_test12_pptr(u32 id, u32 **pptr) +{ +/* prevent DCE */ + asmvolatile ("" :"+r" (id)); + asmvolatile ("" :"+r" (pptr)); +return pptr; +} + +noinline u8 bpf_fentry_test13_pptr(void **pptr) +{ +void *ptr; + +return copy_from_kernel_nofault (&ptr, pptr,sizeof (pptr)) ==0 ; +} + +/* Test the verifier can handle multi-level pointer types with qualifiers. */ +noinline void ***bpf_fentry_test14_ppptr(void **volatile *const ppptr) +{ +/* prevent DCE */ + asmvolatile ("" ::"r" (ppptr) :"memory" ); +return (void ***)ppptr; +} + noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo) { }
@@ -670,20 +709,110 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,return data; } +static void *create_bad_kaddr (void ) +{ +/* +* Try to get an address that passes kernel range checks but causes +* a page fault handler invocation if accessed from a BPF program. +*/ +#if defined(CONFIG_ARCH_HAS_SET_MEMORY ) && defined(CONFIG_X86 ) +void *addr =vmalloc (PAGE_SIZE ); + +if (!addr) +return NULL ; +/* Make it non-present - any access will fault */ +if (set_memory_np ((unsigned long )addr,1 )) { +vfree (addr); +return NULL ; + } +return addr; +#elif defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP ) +struct page *page =alloc_page (GFP_KERNEL ); + +if (!page) +return NULL ; +/* Remove from direct map - any access will fault */ +if (set_direct_map_invalid_noflush (page)) { +__free_page (page); +return NULL ; + } +flush_tlb_kernel_range ((unsigned long )page_address (page), + (unsigned long )page_address (page) +PAGE_SIZE ); +return page_address (page); +#endif +return NULL ; +} + +static void free_bad_kaddr (void *addr) +{ +if (!addr) +return ; + +/* +* Free an invalid test address created by create_bad_kaddr(). +* Restores the page to present state before freeing. +*/ +#if defined(CONFIG_ARCH_HAS_SET_MEMORY ) && defined(CONFIG_X86 ) +set_memory_p ((unsigned long )addr,1 ); +vfree (addr); +#elif defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP ) +struct page *page =virt_to_page (addr); + +set_direct_map_default_noflush (page); +flush_tlb_kernel_range ((unsigned long )addr, + (unsigned long )addr +PAGE_SIZE ); +__free_page (page); +#endif +} + +#define CONSUME (val) do { \ + typeof(val) __var = (val); \ + __asm__ __volatile__("" : "+r" (__var)); \ + (void)__var; \ +} while (0) +int bpf_prog_test_run_tracing (struct bpf_prog *prog,const union bpf_attr *kattr,union bpf_attr __user *uattr) {struct bpf_fentry_test_t arg = {}; +struct bpf_fentry_test_pptr_t ts = { .value1 =1979 , .value2 =2026 }; +struct bpf_fentry_test_pptr_t *ptr = &ts; +void *kaddr =NULL ; +u32 *u32_ptr = (u32 *)29 ;u16 side_effect =0 , ret =0 ;int b =2 , err = -EFAULT ;u32 retval =0 ; +const char *attach_name;if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)return -EINVAL ; + attach_name = prog->aux->attach_func_name; +if (!attach_name) + attach_name ="!" ; +switch (prog->expected_attach_type) {case BPF_TRACE_FENTRY : +if (!strcmp (attach_name,"bpf_fentry_test11_pptr_nullable" )) { +CONSUME (bpf_fentry_test11_pptr_nullable (&ptr)); +break ; + }else if (!strcmp (attach_name,"bpf_fentry_test12_pptr" )) { +CONSUME (bpf_fentry_test12_pptr (0 , &u32_ptr)); +CONSUME (bpf_fentry_test12_pptr (1 , (u32 **)17 )); +break ; + }else if (!strcmp (attach_name,"bpf_fentry_test13_pptr" )) { +/* If kaddr is NULL, the test handles this gracefully. */ + kaddr =create_bad_kaddr (); +CONSUME (bpf_fentry_test13_pptr (kaddr)); +CONSUME (bpf_fentry_test13_pptr ((void **)19 )); +CONSUME (bpf_fentry_test13_pptr (ERR_PTR (-ENOMEM ))); +break ; + }else if (!strcmp (attach_name,"bpf_fentry_test14_ppptr" )) { +CONSUME (bpf_fentry_test14_ppptr (ERR_PTR (-ENOMEM ))); +break ; + } + fallthrough;case BPF_TRACE_FEXIT :case BPF_TRACE_FSESSION : if (bpf_fentry_test1(1) != 2 ||
@@ -717,6 +846,7 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog, err =0 ; out: +free_bad_kaddr (kaddr);trace_bpf_test_finish (&err);return err; }
tools/testing/selftests/bpf/prog_tests/fentry_fexit_multi_level_ptr.c new file +206 -0
206 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -115,6 +115,7 @@#include "verifier_lsm.skel.h" #include "verifier_jit_inline.skel.h" #include "irq.skel.h" +#include "verifier_ctx_multilevel_ptr.skel.h" #define MAX_ENTRIES 11
@@ -259,6 +260,7 @@ void test_verifier_lsm(void) { RUN(verifier_lsm); }void test_irq (void ) {RUN (irq); }void test_verifier_mtu (void ) {RUN (verifier_mtu); }void test_verifier_jit_inline (void ) {RUN (verifier_jit_inline); } +void test_verifier_ctx_multilevel_ptr (void ) {RUN (verifier_ctx_multilevel_ptr); }static int init_test_val_map (struct bpf_object *obj,char *map_name) {
tools/testing/selftests/bpf/progs/fentry_fexit_pptr_nullable_test.c new file +56 -0
56 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fentry_fexit_pptr_test.c new file +67 -0
67 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fentry_fexit_void_ppptr_test.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 CrowdStrike, Inc. */ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> + +char _license[] SEC("license") = "GPL"; + +__u32 fentry_called =0 ; +__u32 fexit_called =0 ; +__u64 fentry_pptr =0 ; +__u64 fexit_retval =0 ; + +typedef void **volatile *const ppvpc_t ; + +/* +* Workaround for a bug in LLVM: +* fatal error: error in backend: Empty type name for BTF_TYPE_ID_REMOTE reloc +*/ +typedef void **void_pp ; + +SEC("fentry/bpf_fentry_test14_ppptr") +int BPF_PROG (test_fentry_void_ppptr ,ppvpc_t ppptr) +{ + fentry_called =1 ; +/* Invalid memory access is fixed by boundaries check or exception handler */ + fentry_pptr = (__u64)*bpf_core_cast ((void ***)ppptr, void_pp); +return 0 ; +} + +SEC("fexit/bpf_fentry_test14_ppptr") +int BPF_PROG (test_fexit_void_ppptr ,ppvpc_t ppptr,void ***ret) +{ + fexit_called =1 ; + fexit_retval = ret ? (__u64 )ret :0 ; +return 0 ; +}
tools/testing/selftests/bpf/progs/fentry_fexit_void_pptr_test.c new file +71 -0
71 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_ctx_multilevel_ptr.c new file +435 -0
435 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 2/2] selftests/bpf: Add trampolines multi-level pointer params test coverage
Quoted 113 lines · Slava Imameev
Multi-level pointer params and return value test coverage for BPF trampolines:
- fentry/fexit programs covering struct and void double/triple pointer parameters and returned values
- verifier context tests covering multi-level pointers as parameters
- verifier context tests covering multi-level pointers as returned values
- verifier context tests for lsm to check trusted parameters handling
- verifier context tests covering out-of-bound access after cast
- verifier BPF helper tests to validate no change in verifier behaviourSigned-off-by Slava Imameev <[email protected]>--- net/bpf/test_run.c | 130 ++++++ .../prog_tests/fentry_fexit_multi_level_ptr.c | 206 +++++++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../progs/fentry_fexit_pptr_nullable_test.c | 56 +++ .../bpf/progs/fentry_fexit_pptr_test.c | 67 +++ .../bpf/progs/fentry_fexit_void_ppptr_test.c | 38 ++ .../bpf/progs/fentry_fexit_void_pptr_test.c | 71 +++ .../bpf/progs/verifier_ctx_multilevel_ptr.c | 435 ++++++++++++++++++ 8 files changed, 1005 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fentry_fexit_multi_level_ptr.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_nullable_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_ppptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/fentry_fexit_void_pptr_test.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_ctx_multilevel_ptr.cnet/bpf/test_run.c +60
-0@@ -24,6 +24,9 @@#include <net/netdev_rx_queue.h> #include <net/xdp.h> #include <net/netfilter/nf_bpf_link.h> +#include <linux/set_memory.h> +#include <linux/string.h> +#include <asm/tlbflush.h> #define CREATE_TRACE_POINTS #include <trace/events/bpf_test_run.h> @@ -563,6 +566,42 @@ noinline int bpf_fentry_test10(const void *a)return (long )a; } +struct bpf_fentry_test_pptr_t { +u32 value1; +u32 value2; +}; + +noinline int bpf_fentry_test11_pptr_nullable(struct bpf_fentry_test_pptr_t **pptr__nullable) +{ +if (!pptr__nullable) +return -1 ; + +return (*pptr__nullable)->value1; +} + +noinline u32 **bpf_fentry_test12_pptr(u32 id, u32 **pptr) +{ +/* prevent DCE */ + asmvolatile ("" :"+r" (id)); + asmvolatile ("" :"+r" (pptr)); +return pptr; +} + +noinline u8 bpf_fentry_test13_pptr(void **pptr) +{ +void *ptr; + +return copy_from_kernel_nofault (&ptr, pptr,sizeof (pptr)) ==0 ; +} + +/* Test the verifier can handle multi-level pointer types with qualifiers. */ +noinline void ***bpf_fentry_test14_ppptr(void **volatile *const ppptr) +{ +/* prevent DCE */ + asmvolatile ("" ::"r" (ppptr) :"memory" ); +return (void ***)ppptr; +} + noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo) { }@@ -670,20 +709,110 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,return data; } +static void *create_bad_kaddr (void ) +{ +/* +* Try to get an address that passes kernel range checks but causes +* a page fault handler invocation if accessed from a BPF program. +*/ +#if defined(CONFIG_ARCH_HAS_SET_MEMORY ) && defined(CONFIG_X86 ) +void *addr =vmalloc (PAGE_SIZE ); + +if (!addr) +return NULL ; +/* Make it non-present - any access will fault */ +if (set_memory_np ((unsigned long )addr,1 )) { +vfree (addr); +return NULL ; + } +return addr; +#elif defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP ) +struct page *page =alloc_page (GFP_KERNEL ); + +if (!page)
net/bpf/test_run.c static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, · line 742
Quoted 10 lines
net/bpf/test_run.c +10
-0+return NULL ; +/* Remove from direct map - any access will fault */ +if (set_direct_map_invalid_noflush (page)) { +__free_page (page); +return NULL ; + } +flush_tlb_kernel_range ((unsigned long )page_address (page), + (unsigned long )page_address (page) +PAGE_SIZE ); +return page_address (page); +#endif
This is serious overkill for a test.
See how bpf_testmod_return_ptr() does it.
pw-bot: cr