bpf: Add multi-level pointer parameter support for trampolines
Subsystems (6)
v2 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 PTR_TO_MEM with read-only semantics, applying either untrusted or trusted access patterns while honoring __nullable annotations. Runtime safety is ensured through existing exception handling mechanisms for untrusted memory reads, with the verifier enforcing bounds checking and null validation. The series includes selftests covering double and triple pointer arguments across fentry/fexit/lsm programs and verifier context validation.
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 **p;
bpf_probe_read_kernel(&p, sizeof(ppacl), &ctx[0]);
bpf_printk("__posix_acl_chmod before %px\n", p);
return 0;
}This approach introduced helper call overhead and created inconsistency with parameter access patterns.
Improvements:
With this patch, trampoline programs can directly read parameters and dereference memory using load instructions, eliminating helper call overhead and ensuring consistent parameter handling. For example, the following helper call sequence:
{
struct posix_acl **pp;
struct posix_acl *p;
bpf_probe_read_kernel(&pp, sizeof(pp), &ctx[0]);
bpf_probe_read_kernel(&p, sizeof(p), pp);
...
}can be replaced by two load instructions implementing a single C statement:
{
struct posix_acl *p = *ppacl;
...
}Design Rationale: PTR_TO_MEM vs SCALAR
The verifier assigns SCALAR type to single-level pointers (void*, int*). For multi-level pointers, I selected PTR_TO_MEM to enable memory access through a single load instruction for the first level of dereference, with subsequent dereferences becoming SCALAR. This design eliminates helper call for parameter dereference, replacing it with a load instruction (e.g., void* ptr = *pptr).
Access safety is maintained through existing verify-time checks, exception handling, and kernel virtual address range boundary checks:
- User-mode memory address access is prevented by runtime virtual address range checks for untrusted PTR_TO_MEM
- Invalid kernel address space accesses are intercepted by the exception handler for untrusted PTR_TO_MEM
- Trusted PTR_TO_MEM access safety is maintained at verify time
v1 -> v2: corrected maintainer's email
Slava Imameev (2):
bpf: Support multi-level pointer params via PTR_TO_MEM for trampolines selftests/bpf: Add trampolines multi-level pointer params test
coverage
include/linux/bpf.h | 3 +- kernel/bpf/btf.c | 54 ++- kernel/bpf/verifier.c | 4 +- net/bpf/test_run.c | 128 ++++++ .../prog_tests/fentry_fexit_multi_level_ptr.c | 204 +++++++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../progs/fentry_fexit_pptr_nullable_test.c | 52 +++ .../bpf/progs/fentry_fexit_pptr_test.c | 60 +++ .../bpf/progs/fentry_fexit_void_ppptr_test.c | 31 ++ .../bpf/progs/fentry_fexit_void_pptr_test.c | 64 +++ .../bpf/progs/verifier_ctx_multilevel_ptr.c | 429 ++++++++++++++++++ 11 files changed, 1021 insertions(+), 10 deletions(-) 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.50.1 (Apple Git-155)
PATCH v2 1/2 bpf: Support multi-level pointer params via PTR_TO_MEM for trampolines
Add BPF verifier support for multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as PTR_TO_MEM with read-only semantics, applying either untrusted or trusted access patterns while honoring __nullable annotations. Runtime safety is ensured through existing exception handling mechanisms for untrusted memory reads, with the verifier enforcing bounds checking and null validation.
--- include/linux/bpf.h | 3 ++- kernel/bpf/btf.c | 54 ++++++++++++++++++++++++++++++++++++------- kernel/bpf/verifier.c | 4 +++- 3 files changed, 51 insertions(+), 10 deletions(-)
include/linux/bpf.h +2 -1
@@ -1052,7 +1052,8 @@ struct bpf_insn_access_aux {struct btf *btf;u32 btf_id;u32 ref_obj_id; - }; + };/* base type PTR_TO_BTF_ID */ +u32 mem_size;/* base type PTR_TO_MEM */ };struct bpf_verifier_log *log;/* for verbose logs */ bool is_retval;/* is accessing function return value ? */
kernel/bpf/btf.c +46 -8
86 lines not on this page · raw message
kernel/bpf/verifier.c +3 -1
@@ -6311,7 +6311,7 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, off);return -EACCES ; } - }else { + }else if (base_type (info->reg_type) !=PTR_TO_MEM ) { env->insn_aux_data[insn_idx].ctx_field_size = info->ctx_field_size; }/* remember the offset of last byte accessed in ctx */
@@ -7771,6 +7771,8 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn regs[value_regno].btf = info.btf; regs[value_regno].btf_id = info.btf_id; regs[value_regno].ref_obj_id = info.ref_obj_id; + }else if (base_type (info.reg_type) ==PTR_TO_MEM ) { + regs[value_regno].mem_size = info.mem_size; } } regs[value_regno].type = info.reg_type;
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v2 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
- nullable pointer cases to validate required NULL checks
- verifier context tests for lsm to check trusted parameters handling
- verifier context tests to exercise PTR_TO_MEM sizing and read-only behavior
- verifier BPF helper tests to validate no change in verifier behaviour
--- net/bpf/test_run.c | 128 ++++++ .../prog_tests/fentry_fexit_multi_level_ptr.c | 204 +++++++++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../progs/fentry_fexit_pptr_nullable_test.c | 52 +++ .../bpf/progs/fentry_fexit_pptr_test.c | 60 +++ .../bpf/progs/fentry_fexit_void_ppptr_test.c | 31 ++ .../bpf/progs/fentry_fexit_void_pptr_test.c | 64 +++ .../bpf/progs/verifier_ctx_multilevel_ptr.c | 429 ++++++++++++++++++ 8 files changed, 970 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 +128 -0
@@ -24,6 +24,8 @@#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> #define CREATE_TRACE_POINTS #include <trace/events/bpf_test_run.h>
@@ -563,6 +565,41 @@ noinline int bpf_fentry_test10(const void *a)return (long )a; } +struct bpf_fentry_test_pptr_t { +int value; +}; + +noinline int bpf_fentry_test11_pptr_nullable(struct bpf_fentry_test_pptr_t **pptr__nullable) +{ +if (!pptr__nullable) +return -1 ; + +return (*pptr__nullable)->value; +} + +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 +707,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 get_invalid_address(). +* 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 = { .value =1979 }; +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" )) { + kaddr =create_bad_kaddr (); +WARN_ON (!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 +844,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 +204 -0
204 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 +52 -0
52 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fentry_fexit_pptr_test.c new file +60 -0
60 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fentry_fexit_void_ppptr_test.c new file +31 -0
@@ -0,0 +1,31 @@ +// 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> + +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 ; + +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 = (unsigned long )*ppptr; +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 +64 -0
64 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_ctx_multilevel_ptr.c new file +429 -0
429 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/2] selftests/bpf: Add trampolines multi-level pointer params test coverage
The first branch requires both CONFIG_ARCH_HAS_SET_MEMORY and CONFIG_X86. The second branch requires CONFIG_ARCH_HAS_SET_DIRECT_MAP.
Quoted 4 lines
net/bpf/test_run.c +0
-0
[ ... ]
net/bpf/test_run.c static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, · line 716
Quoted 7 lines
net/bpf/test_run.c +7
-0+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 )
The first branch requires both CONFIG_ARCH_HAS_SET_MEMORY and CONFIG_X86. The second branch requires CONFIG_ARCH_HAS_SET_DIRECT_MAP.
PowerPC selects ARCH_HAS_SET_MEMORY (arch/powerpc/Kconfig) but not ARCH_HAS_SET_DIRECT_MAP, so neither branch compiles in and the function always returns NULL on that architecture.
Quoted 11 lines
net/bpf/test_run.c +11
-0+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 )
[ ... ]
Quoted 3 lines
net/bpf/test_run.c +3
-0+#endif +return NULL ; +}
[ ... ]
net/bpf/test_run.c static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, · line 752
Quoted 9 lines
net/bpf/test_run.c +9
-0+static void free_bad_kaddr (void *addr) +{ +if (!addr) +return ; + +/* +* Free an invalid test address created by get_invalid_address(). +* Restores the page to present state before freeing. +*/
The comment references get_invalid_address() but the function that creates the address is create_bad_kaddr().
Quoted 1 line
+#if
defined(CONFIG_ARCH_HAS_SET_MEMORY)&&defined(CONFIG_X86)
[ ... ]
Quoted 2 lines
net/bpf/test_run.c +2
-0+#endif +}
[ ... ]
net/bpf/test_run.c static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, · line 804
Quoted 3 lines
net/bpf/test_run.c +3
-0+ }else if (!strcmp (attach_name,"bpf_fentry_test13_pptr" )) { + kaddr =create_bad_kaddr (); +WARN_ON (!kaddr);
Since create_bad_kaddr() returns NULL on PowerPC (which supports BPF trampolines via HAVE_DYNAMIC_FTRACE_WITH_ARGS), would this WARN_ON fire every time the bpf_fentry_test13_pptr test runs on that architecture?
net/bpf/test_run.c static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size, · line 808
Quoted 4 lines
net/bpf/test_run.c +4
-0+CONSUME (bpf_fentry_test13_pptr (kaddr)); +CONSUME (bpf_fentry_test13_pptr ((void **)19 )); +CONSUME (bpf_fentry_test13_pptr (ERR_PTR (-ENOMEM ))); +break ;
[ ... ]
---
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/22118172993
AI-authorship-score: low
AI-authorship-explanation: Stale comment referencing a renamed function and an architecture coverage gap with WARN_ON are characteristic of human-authored code that underwent refactoring.
issues-found: 2
issue-severity-score: medium
issue-severity-explanation: WARN_ON(!kaddr) fires unconditionally on PowerPC where create_bad_kaddr() always returns NULL, producing kernel warnings on every test run and potential panics with panic_on_warn.
kernel test robot
build botRe: [PATCH bpf-next v2 2/2] selftests/bpf: Add trampolines multi-level pointer params test coverage
In reply to Slava Imameevkernel test robot noticed the following build errors:
Hi Slava,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Slava-Imameev/bpf-Support-multi-level-pointer-params-via-PTR_TO_MEM-for-trampolines/20260218-062417 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/20260217221357.18215-3-slava.imameev%40crowdstrike.com patch subject: [PATCH bpf-next v2 2/2] selftests/bpf: Add trampolines multi-level pointer params test coverage config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20260218/[email protected]/config) compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260218/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> net/bpf/test_run.c:737:2: error: call to undeclared function 'flush_tlb_kernel_range'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
737 | flush_tlb_kernel_range((unsigned long)page_address(page),
| ^
net/bpf/test_run.c:760:2: error: call to undeclared function 'flush_tlb_kernel_range'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
760 | flush_tlb_kernel_range((unsigned long)addr,
| ^2 errors generated.
vim +/flush_tlb_kernel_range +737 net/bpf/test_run.c
709
710 static void *create_bad_kaddr(void)
711 {
712 /*
713 * Try to get an address that passes kernel range checks but causes
714 * a page fault handler invocation if accessed from a BPF program.
715 */
716 #if defined(CONFIG_ARCH_HAS_SET_MEMORY) && defined(CONFIG_X86)
717 void *addr = vmalloc(PAGE_SIZE);
718
719 if (!addr)
720 return NULL;
721 /* Make it non-present - any access will fault */
722 if (set_memory_np((unsigned long)addr, 1)) {
723 vfree(addr);
724 return NULL;
725 }
726 return addr;
727 #elif defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP)
728 struct page *page = alloc_page(GFP_KERNEL);
729
730 if (!page)
731 return NULL;
732 /* Remove from direct map - any access will fault */
733 if (set_direct_map_invalid_noflush(page)) {
734 __free_page(page);
735 return NULL;
736 }
> 737 flush_tlb_kernel_range((unsigned long)page_address(page),
738 (unsigned long)page_address(page) + PAGE_SIZE);
739 return page_address(page);
740 #endif
741 return NULL;
742 }
743
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Eduard Zingerman
maintainerOn Wed, 2026-02-18 at 09:13 +1100, Slava Imameev wrote:
[...]
Quoted 1 line
The verifier assigns SCALAR type to single-level pointers (void*, int*).
So, the simplest change for pointers to pointers would be as below, right?
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6906,7 +6906,8 @@ 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. */
- if (is_void_or_int_ptr(btf, t))
+ if (is_void_or_int_ptr(btf, t) || !is_ptr_to_struct(btf, t))
return true;
/* this is a pointer to another type */Except that loaded value would be marked as scalar() and one would need to cast it using e.g. bpf_core_cast() to obtain an untrusted pointer.
Quoted 5 lines
For multi-level pointers, I selected
PTR_TO_MEMto enable memory access through a single load instruction for the first level of dereference, with subsequent dereferences becoming SCALAR. This design eliminates helper call for parameter dereference, replacing it with a load instruction (e.g., void* ptr = *pptr).
If going this route instead, is there a technical reason to limit this logic to multi-level pointers? Applying same rules to int * and alike seem more consistent.
[...]
Re: Re: [PATCH bpf-next v2 0/2] bpf: Add multi-level pointer parameter
Quoted 7 lines
Quoted 1 line
The verifier assigns SCALAR type to single-level pointers (void*, int*).
So, the simplest change for pointers to pointers would be as below, right?
---
a/kernel/bpf/btf.c
+++b/kernel/bpf/btf.c
@@ -6906,7 +6906,8 @@ bool btf_ctx_access(int off, int size, enum bpf_acc=
ess_type type,
Quoted 1 line
* If it's a pointer to void, it's the same as scalar from the ve=
rifier
Quoted 11 lines
* safety POV. Either way, no futher pointer walking is allowed. */
- if (is_void_or_int_ptr(btf, t))
+ if (is_void_or_int_ptr(btf, t)|| !is_ptr_to_struct(btf, t))return true; /* this is a pointer to another type */Except that loaded value would be marked as
scalar()and one would need to cast it using e.g.bpf_core_cast()to obtain an untrusted pointer.
I considered using a scalar as a simpler solution, but there are some disadvantages with casting to scalar and using bpf_core_cast:
- Casting to scalar removes nullable and trusted properties
- bpf_core_cast cannot cast to multi-level pointers without introducing a new typedef or a wrapper for a pointer
Let's consider the following LSM program which has trusted parameters, and logs the value for (*mnt_opts):
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
With this patch:
- This program is valid:
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
{
bpf_printk("%p\n", *mnt_opts);
return 0;
}- This program is semantically invalid as mnt_opts is a trusted parameter, so there are no run-time checks and the verifier rejects out-of-bounds access:
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
{
bpf_printk("%p\n", *(mnt_opts+10));
return 0;
}With casting to a scalar and following bpf_core_cast:
- This programs cannot be compiled as bpf_core_cast cannot cast to a multi-level pointer:
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
{
void** ppt = bpf_core_cast(mnt_opts, void*);
bpf_printk("%p\t", *ppt);
return 0;
}- There is a workaround, which requires introducing a wrapper for a pointer or typedef:
struct pvoid {
void* v;
};typedef void* pvoid;
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
{
struct pvoid* ppt = bpf_core_cast(mnt_opts, struct pvoid);
bpf_printk("%p\t", ppt->v);
return 0;
}SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_2,char *options, void **mnt_opts)
{
pvoid* ppt = bpf_core_cast(mnt_opts, pvoid);
bpf_printk("%p\t", *ppt);
return 0;
}- This program passes verifier, though it is semantically invalid as logs an invalid data using a trusted parameter:
SEC("lsm/sb_eat_lsm_opts")
int BPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts)
{
struct pvoid* ppt = bpf_core_cast(mnt_opts + 10, struct pvoid);
bpf_printk("%p\t", ppt->v);
return 0;
}The similar examples can be done for nullable annotation, which is ignored for a scalar allowing semantically invalid BPF programs to pass verifier.
Quoted 9 lines
Quoted 5 lines
For multi-level pointers, I selected
PTR_TO_MEMto enable memory access through a single load instruction for the first level of dereference, with subsequent dereferences becoming SCALAR. This design eliminates helper call for parameter dereference, replacing it with a load instruction (e.g., void* ptr =3D *pptr).If going this route instead, is there a technical reason to limit this logic to multi-level pointers? Applying same rules to
int *and alike seem more consistent.
I decided to address only multilevel pointers as this is what we encountered in practice and have to use BPF helper workarounds. I think there are no technical restrictions for treating single level pointers as PTR_TO_MEM.
However, there is some cohesion between multilevel pointers being PTR_TO_MEM and single level being scalar, as verifier infers a scalar for PTR_TO_MEM dereference, so:
foo(void *ptr1, void **pptr)
{
void* ptr2 = *pptr; /* verifier infers a scalar for ptr2*/ /* both ptr1 and ptr2 are scalars */
}
Re: Re: [PATCH bpf-next v2 0/2] bpf: Add multi-level pointer parameter
Quoted 1 line
I decided to address only multilevel pointers as this is what we
encountered in practice and have to use BPF helper workarounds. I think there are no technical restrictions for treating single level pointers as PTR_TO_MEM.
Hi Slava and Eduard,
If we add support for writable single-level int pointers, we could trivially implement bpf_inode_set_xattr in the way that Alexei originally suggested[1] when it was first attempted to be added.
One note, for this particular case, the kfunc would need to be able to write to the xattr int* param, as lsm_get_xattr_slot[2] increments the LSM-internal xattr_count. Others would be possible as well (cred_getsecid).
[1] https://kernsec.org/pipermail/linux-security-module-archive/2022-October/034878.html
[2] https://elixir.bootlin.com/linux/v6.19-rc5/source/include/linux/lsm_hooks.h#L215
Re: Re: [PATCH bpf-next v2 0/2] bpf: Add multi-level pointer parameter
In reply to Slava ImameevQuoted 5 lines · Slava Imameev
- This programs cannot be compiled as
bpf_core_castcannot cast to a multi-level pointer:
Quoted 7 lines
SEC("lsm/sb_eat_lsm_opts")
intBPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts){ void** ppt = bpf_core_cast(mnt_opts, void*); bpf_printk("%p\t", *ppt); return 0; }
Looks like there is a bug in llvm, since it crashes on the above. But the following works:
void** ppt = bpf_rdonly_cast(mnt_opts, 0);bpf_printk("%lx\t", *ppt);
Plus Eduard's diff:
- if (is_void_or_int_ptr(btf, t))
+ if (is_void_or_int_ptr(btf, t) || !btf_type_is_struct_ptr(btf, t))
Quoted 6 lines
- There is a workaround, which requires introducing a wrapper for a pointer or typedef:
struct pvoid { void* v; };
llvm should have handled it without the workaround. It's a bug that should be fixed.
Quoted 2 lines
I think there are no technical restrictions for treating single level pointers as
PTR_TO_MEM.
I think it will be a missed opportunity and a potential foot gun.
We didn't support access to 'char *' initially.
Later relaxed it to mean that it's a valid pointer to a single byte, but since the code is generic it also became the case that 'char *' is allowed in kfunc and the verifier checks that one valid byte is there.
That was a bad footgun, since we saw several cases of broken kfunc implementations that assume that 'char *' means a string.
There are only two lsm hooks that pass 'struct foo **'.
If we make it ptr_to_mem of 8 bytes we lose the ability to do something smarter in the future.
I think we better add support to annotate such '**' as an actual array with given size and track types completely, so 'struct foo * var[N]' will become array_to_ptr_to_btf_id. That's probably more work that you signed up to do,
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.
Yonghong Song
reviewerRe: [PATCH bpf-next v2 0/2] bpf: Add multi-level pointer parameter
Quoted 24 lines · Alexei Starovoitov
Quoted 11 lines · Slava Imameev
- This programs cannot be compiled as
bpf_core_castcannot cast to a multi-level pointer:
SEC("lsm/sb_eat_lsm_opts")
intBPF_PROG(sb_eat_lsm_opts_1,char *options, void **mnt_opts){ void** ppt = bpf_core_cast(mnt_opts, void*); bpf_printk("%p\t", *ppt); return 0; }Looks like there is a bug in llvm, since it crashes on the above. But the following works:
void** ppt =
bpf_rdonly_cast(mnt_opts, 0);bpf_printk("%lx\t", *ppt);Plus Eduard's diff:
(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 8 lines
Quoted 6 lines
- There is a workaround, which requires introducing a wrapper for a pointer or typedef:
struct pvoid { void* v; };llvm should have handled it without the workaround. It's a bug that should be fixed.
Okay, I will take a look.
Quoted 27 lines
Quoted 3 lines
I think there are no technical restrictions for treating single level pointers as
PTR_TO_MEM.I think it will be a missed opportunity and a potential foot gun.
We didn't support access to 'char *' initially.
Later relaxed it to mean that it's a valid pointer to a single byte, but since the code is generic it also became the case that 'char *' is allowed in kfunc and the verifier checks that one valid byte is there.
That was a bad footgun, since we saw several cases of broken kfunc implementations that assume that 'char *' means a string.There are only two lsm hooks that pass 'struct foo **'.
If we make itptr_to_memof 8 bytes we lose the ability to do something smarter in the future.
I think we better add support to annotate such '**' as an actual array with given size and track types completely, so 'struct foo * var[N]' will becomearray_to_ptr_to_btf_id. That's probably more work that you signed up to do,
so I suggest treating 'void **' as a scalar as Eduard suggested. This particularsb_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.
Re: Re: Re: [PATCH bpf-next v2 0/2] bpf: Add multi-level pointer parameter
In reply to Alexei StarovoitovOn Wed, 18 Feb 2026 19:15:47 Alexei Starovoitov
<[email protected]> wrote
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.