Skip to contentSkip to conversation

bpf: Add multi-level pointer parameter support for trampolines

PATCH Superseded Slava Imameev 3 participants 15 messages Mar 3 – Mar 11

Subsystems (5)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover bpf: Add multi-level pointer parameter support for trampolines

This is v4 of a series adding support for new pointer types for trampoline parameters.

Originally, only support for multi-level pointers was proposed. As suggested during review, it was extended to some single level pointers. During discussion, it was proposed to add support for any pointer type that is not a pointer to structure, with a condition like if (!btf_type_is_struct(t)), but I found this might not be compatible with some cases, e.g., some tests failed. Though this depended on the exact place the check is done - if the check is moved before the exit from btf_ctx_access, tests are passed. Anyway, this looked like an extension which might conflict with future changes for some types falling in the !btf_type_is_struct(t) case. Instead, I used explicit type checks to add support only for single and multi-level pointer types that are not supported but can be supported with scalar. This is a cautious approach which can be verified with explicit tests for each type.

These changes appear 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.

This series adds BPF verifier support for single- and multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as SCALAR_VALUE.

The following new single level pointer support is added:
- pointers to enum, 32 and 64
- pointers to functions

The following multi-level pointer support is added:
- multi-level pointers to int
- multi-level pointers to void
- multi-level pointers to enum, 32 and 64
- multi-level pointers to function
- multi-level pointers to structure

This is consistent with the existing pointers to int and void already treated as SCALAR.

This provides consistent logic for single and multi-level pointers
- if the type is treated as SCALAR for a single level pointer, the same is applicable for multi-level pointers, except the pointer to struct which is currently PTR_TO_BTF_ID, but in case of multi-level pointer it is treated as scalar as the verifier lacks the context to infer the size of their target memory regions.

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
v3 -> v4:
* Added more consistent support for single and multi-level pointers
as suggested by reviewers.
* added single level pointers to enum 32 and 64
* added single level pointers to functions
* harmonized support for single and multi-level pointer types
* added new tests to support the above changes
* Removed create_bad_kaddr that allocated and invalidated kernel VA for tests, and replaced it with hardcoded values similar to bpf_testmod_return_ptr as suggested by reviewers.

Slava Imameev (2):
bpf: Support new pointer param types via SCALAR_VALUE for trampolines selftests/bpf: Add trampolines single and multi-level pointer params
test coverage

 kernel/bpf/btf.c                              |  31 +-
 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   |  60 ++
 .../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_ptr_param.c        | 523 ++++++++++++++++++
 9 files changed, 1120 insertions(+), 8 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_ptr_param.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/2 bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Add BPF verifier support for single- and multi-level pointer parameters and return values in BPF trampolines. The implementation treats these parameters as SCALAR_VALUE.

The following new single level pointer support is added:
- pointers to enum, 32 and 64
- pointers to functions

The following multi-level pointer support is added:
- multi-level pointers to int
- multi-level pointers to void
- multi-level pointers to enum, 32 and 64
- multi-level pointers to function
- multi-level pointers to structure

This is consistent with the existing pointers to int and void already treated as SCALAR.

This provides consistent logic for single and multi-level pointers
- if the type is treated as SCALAR for a single level pointer, the same is applicable for multi-level pointers, except the pointer to struct which is currently PTR_TO_BTF_ID, but in case of multi-level pointer it is treated as scalar as the verifier lacks the context to infer the size of their target memory regions.

---
 kernel/bpf/btf.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
kernel/bpf/btf.c +23 -8
index 4872d2a6c42d..c2d06d2597d6 100644
@@ -6508,11 +6508,30 @@ struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog)
 		return prog->aux->attach_btf;
 }
 
-static bool is_void_or_int_ptr(struct btf *btf, const struct btf_type *t)
+static bool is_ptr_treated_as_scalar(const struct btf *btf,
+	const struct btf_type *t)
 {
-	/* skip modifiers */
+	int depth = 1;
+
+	WARN_ON(!btf_type_is_ptr(t));
+
 	t = btf_type_skip_modifiers(btf, t->type, NULL);
-	return btf_type_is_void(t) || btf_type_is_int(t);
+	while (btf_type_is_ptr(t) && depth < MAX_RESOLVE_DEPTH) {
+		depth += 1;
+		t = btf_type_skip_modifiers(btf, t->type, NULL);
+	}
+
+	/*
+	 * If it's a single or multilevel pointer to void, int, enum,
+	 * or function, it's the same as scalar from the verifier
+	 * safety POV. Multilevel pointers to structures are treated as
+	 * scalars. The verifier lacks the context to infer the size of
+	 * their target memory regions. Either way, no further pointer
+	 * walking is allowed.
+	 */
+	return btf_type_is_void(t) || btf_type_is_int(t) ||
+		   btf_is_any_enum(t) || btf_type_is_func_proto(t) ||
+		   (btf_type_is_struct(t) && depth > 1);
 }
 
 u32 btf_ctx_arg_idx(struct btf *btf, const struct btf_type *func_proto,
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
 		return true;
 
 	/* this is a pointer to another type */
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

On Tue, 2026-03-03 at 20:54 +1100, Slava Imameev wrote:

[...]

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
 		return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

	if (is_ptr_treated_as_scalar(btf, t))
		 return true;

?

Only two new tests fail:
- #554/62 verifier_ctx_ptr_param/fentry/pointer to float - invalid ctx access:FAIL
- #554/63 verifier_ctx_ptr_param/fentry/double pointer to float - invalid ctx access:FAIL

But I'd say this shouldn't matter.
This will also make selftests much simpler.

Quoted 2 lines
kernel/bpf/btf.c +0 -0
 	/* this is a pointer to another type */
#3 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

This reflects my belief in a cautious approach: adding support only for selected types with tests added for each new type. That said, I can add the suggested broader condition and make it pass the tests, but I cannot be sure it will be future-proof against conflicts.

I think the broader check like

	/* skip modifiers */
	tt = t;
	while (btf_type_is_modifier(tt))
		tt = btf_type_by_id(btf, tt->type);
	if (!btf_type_is_struct(tt))
		return true;

might have some incompatibility with future changes, compared to explicit type checks for selected types. This condition is open-ended, including anything instead of selecting specific types.

This broader check also needs to be moved down closer to the exit from btf_ctx_access; otherwise, btf_ctx_access can exit early without executing the following code. In my case, this resulted in existing test failures if the above !btf_type_is_struct(tt) replaces current master's branch condition

	if (is_void_or_int_ptr(btf, t))
		return true;

The result for:

./vmtest.sh -- ./test_progs

was:

	Summary: 617/5770 PASSED, 80 SKIPPED, 82 FAILED

with a lot of:

unexpected_load_success

Compared to:

	Summary: 692/6045 PASSED, 80 SKIPPED, 7 FAILED

for the master branch.

As I noted this diff, closer to the exit from btf_ctx_access, makes tests to pass:

        if (!btf_type_is_struct(t)) {
-               bpf_log(log,
-                       "func '%s' arg%d type %s is not a struct\n",
-                       tname, arg, btf_type_str(t));
-               return false;
+               info->reg_type = SCALAR_VALUE;
+               return true;
        }
Quoted 6 lines

Only two new tests fail:
- #554/62 verifier_ctx_ptr_param/fentry/pointer to float - invalid ctx access:FAIL
- #554/63 verifier_ctx_ptr_param/fentry/double pointer to float - invalid ctx access:FAIL

But I'd say this shouldn't matter.
This will also make selftests much simpler.

Yes, I decided not to add support for pointers to float.

maintainer
#4 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 17 lines · Slava Imameev
Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines
Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

Uh-oh, I copy-pasted the wrong snippet, sorry.
The correct snippet is:

         if (btf_type_is_struct_ptr(btf, t))
                  return true;

With it the selftests pass (except for float tests noted earlier). And regardless of selftests, the code below this point will error out if t is not a pointer to struct.

Quoted 5 lines

This reflects my belief in a cautious approach: adding support only for selected types with tests added for each new type. That said, I can add the suggested broader condition and make it pass the tests, but I cannot be sure it will be future-proof against conflicts.

Quoted 8 lines

I think the broader check like

	/* skip modifiers */
	tt = t;
	while (btf_type_is_modifier(tt))
		tt = btf_type_by_id(btf, tt->type);
	if (!btf_type_is_struct(tt))
		return true;

btf_type_is_struct_ptr() is almost identical to the snippet above.

Quoted 3 lines

might have some incompatibility with future changes, compared to explicit type checks for selected types. This condition is open-ended, including anything instead of selecting specific types.

What potential incompatibility do you expect?
Two things change:
- types other then struct foo * or int can be read:
- do you expect we would want to deny reading some ctx fields in the future?
- the value read is marked as scalar:
- not much can be done with a scalar, except for leaking it to e.g. some map or ring buffer. Do you expect this to problematic?

Note that the above are selected based on type, not on the function/parameter combination, which is already not a very effective filter if some parameters need to be hidden.

Quoted 48 lines

This broader check also needs to be moved down closer to the exit from btf_ctx_access; otherwise, btf_ctx_access can exit early without executing the following code. In my case, this resulted in existing test failures if the above !btf_type_is_struct(tt) replaces current master's branch condition

	if (is_void_or_int_ptr(btf, t))
		return true;

The result for:

./vmtest.sh -- ./test_progs

was:

	Summary: 617/5770 PASSED, 80 SKIPPED, 82 FAILED

with a lot of:

unexpected_load_success

Compared to:

	Summary: 692/6045 PASSED, 80 SKIPPED, 7 FAILED

for the master branch.

As I noted this diff, closer to the exit from btf_ctx_access, makes tests to pass:

        if (!btf_type_is_struct(t)) {
(quoted diff) +2 -4
-               bpf_log(log,
-                       "func '%s' arg%d type %s is not a struct\n",
-                       tname, arg, btf_type_str(t));
-               return false;
+               info->reg_type = SCALAR_VALUE;
+               return true;
        }
Quoted 8 lines

Only two new tests fail:
- #554/62 verifier_ctx_ptr_param/fentry/pointer to float - invalid ctx access:FAIL
- #554/63 verifier_ctx_ptr_param/fentry/double pointer to float - invalid ctx access:FAIL

But I'd say this shouldn't matter.
This will also make selftests much simpler.

Yes, I decided not to add support for pointers to float.

#5 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 25 lines · Eduard Zingerman
Quoted 17 lines · Slava Imameev
Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines
Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

Uh-oh, I copy-pasted the wrong snippet, sorry.
The correct snippet is:

Quoted 6 lines
         if (btf_type_is_struct_ptr(btf, t))
                  return true;

With it the selftests pass (except for float tests noted earlier). And regardless of selftests, the code below this point will error out if t is not a pointer to struct.

I think you tested with

	if (!btf_type_is_struct_ptr(btf, t))
		return true;

I decided on a narrower condition, as

- if (!btf_type_is_struct_ptr(btf, t)) -

changes the existing selection condition from "treat only these types as scalar" to "treat as scalar any type that is not a pointer to structure". Technically both approaches cover the problem I'm trying to solve - multilevel pointer support for structures, but the latter is open-ended and changes the current approach, which checks for pointers to int and void. So I'm extending this to int, void, enum 32/64, function, and corresponding multilevel pointers to these types and multilevel pointers to structures.

It seems - if (!btf_type_is_struct_ptr(btf, t)) - works, but it's challenging to strictly prove it's sufficiently future-proof.

Quoted 29 lines
Quoted 3 lines

This reflects my belief in a cautious approach: adding support only for selected types with tests added for each new type. That said, I can add the suggested broader condition and make it pass the tests,

but I cannot be sure it will be future-proof against conflicts.

Quoted 9 lines

I think the broader check like

      /* skip modifiers */
      tt = t;
      while (btf_type_is_modifier(tt))
              tt = btf_type_by_id(btf, tt->type);
      if (!btf_type_is_struct(tt))
              return true;

btf_type_is_struct_ptr() is almost identical to the snippet above.

Quoted 3 lines

might have some incompatibility with future changes, compared to explicit type checks for selected types. This condition is open-ended, including anything instead of selecting specific types.

What potential incompatibility do you expect?
Two things change:
- types other then struct foo * or int can be read:
- do you expect we would want to deny reading some ctx fields in the future?
- the value read is marked as scalar:
- not much can be done with a scalar, except for leaking it to e.g. some map or ring buffer. Do you expect this to problematic?

Quoted 3 lines

Note that the above are selected based on type, not on the function/parameter combination, which is already not a very effective filter if some parameters need to be hidden.

I do not think any of these represent a real problem. As I said, my approach is based mostly on narrowing the supported types to reduce potential conflicts.

I do not have a good example of such conflicts.
The added tests for pointer to float, which failed with - if (!btf_type_is_struct_ptr(btf, t)) - might be an example when adding a new type might silently pass this check because of missing tests.

I was not able to convince myself a conflict will not happen.

That said, changing

	if (is_ptr_treated_as_scalar(btf, t))
		return true;

	to

	if (!btf_type_is_struct_ptr(btf, t))
		return true;

just makes the scope of these changes wider. This was my initial approach to this problem, but I was worried by its wide scope.

#6 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 109 lines · Slava Imameev
Quoted 25 lines · Eduard Zingerman
Quoted 17 lines · Slava Imameev
Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines
Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

Uh-oh, I copy-pasted the wrong snippet, sorry.
The correct snippet is:

Quoted 6 lines
         if (btf_type_is_struct_ptr(btf, t))
                  return true;

With it the selftests pass (except for float tests noted earlier). And regardless of selftests, the code below this point will error out if t is not a pointer to struct.

I think you tested with

        if (!btf_type_is_struct_ptr(btf, t))
                return true;

I decided on a narrower condition, as

- if (!btf_type_is_struct_ptr(btf, t)) -

changes the existing selection condition from "treat only these types as scalar" to "treat as scalar any type that is not a pointer to structure". Technically both approaches cover the problem I'm trying to solve - multilevel pointer support for structures, but the latter is open-ended and changes the current approach, which checks for pointers to int and void. So I'm extending this to int, void, enum 32/64, function, and corresponding multilevel pointers to these types and multilevel pointers to structures.

It seems - if (!btf_type_is_struct_ptr(btf, t)) - works, but it's challenging to strictly prove it's sufficiently future-proof.

Quoted 29 lines
Quoted 3 lines

This reflects my belief in a cautious approach: adding support only for selected types with tests added for each new type. That said, I can add the suggested broader condition and make it pass the tests,

but I cannot be sure it will be future-proof against conflicts.

Quoted 9 lines

I think the broader check like

      /* skip modifiers */
      tt = t;
      while (btf_type_is_modifier(tt))
              tt = btf_type_by_id(btf, tt->type);
      if (!btf_type_is_struct(tt))
              return true;

btf_type_is_struct_ptr() is almost identical to the snippet above.

Quoted 3 lines

might have some incompatibility with future changes, compared to explicit type checks for selected types. This condition is open-ended, including anything instead of selecting specific types.

What potential incompatibility do you expect?
Two things change:
- types other then struct foo * or int can be read:
- do you expect we would want to deny reading some ctx fields in the future?
- the value read is marked as scalar:
- not much can be done with a scalar, except for leaking it to e.g. some map or ring buffer. Do you expect this to problematic?

Quoted 3 lines

Note that the above are selected based on type, not on the function/parameter combination, which is already not a very effective filter if some parameters need to be hidden.

I do not think any of these represent a real problem. As I said, my approach is based mostly on narrowing the supported types to reduce potential conflicts.

I do not have a good example of such conflicts.
The added tests for pointer to float, which failed with - if (!btf_type_is_struct_ptr(btf, t)) - might be an example when adding a new type might silently pass this check because of missing tests.

I was not able to convince myself a conflict will not happen.

That said, changing

        if (is_ptr_treated_as_scalar(btf, t))
                return true;

        to

        if (!btf_type_is_struct_ptr(btf, t))
                return true;
Quoted 3 lines

just makes the scope of these changes wider. This was my initial approach to this problem, but I was worried by its wide scope.

You contradict yourself. If returning scalar today for pointer-to-pointer is backward compatible in case we want to make it smarter, then returning scalar for all is backward compat as well.
So no reason to introduce is_ptr_treated_as_scalar().

Also simplify tests as Eduard suggested.

pw-bot: cr

maintainer
#7 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

In reply to Slava Imameev
Quoted 33 lines · Slava Imameev
Quoted 25 lines · Eduard Zingerman
Quoted 17 lines · Slava Imameev
Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines
Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

Uh-oh, I copy-pasted the wrong snippet, sorry.
The correct snippet is:

Quoted 6 lines
         if (btf_type_is_struct_ptr(btf, t))
                  return true;

With it the selftests pass (except for float tests noted earlier). And regardless of selftests, the code below this point will error out if t is not a pointer to struct.

Quoted 8 lines

I think you tested with

	if (!btf_type_is_struct_ptr(btf, t))
		return true;

I decided on a narrower condition, as

- if (!btf_type_is_struct_ptr(btf, t)) -

Yes, sorry again.

Quoted 8 lines

changes the existing selection condition from "treat only these types as scalar" to "treat as scalar any type that is not a pointer to structure". Technically both approaches cover the problem I'm trying to solve - multilevel pointer support for structures, but the latter is open-ended and changes the current approach, which checks for pointers to int and void. So I'm extending this to int, void, enum 32/64, function, and corresponding multilevel pointers to these types and multilevel pointers to structures.

BTF is defined for the following non-modifier types:

- void        [allowed already]
- int         [allowed already]
- ptr         [multi-level pointers allowed by your patch]
- array       [disallowed?]
- struct      [single level pointers allowed already,

- union multi-level allowed by your patch]
- enum/enum64 [allowed by your patch]
- func_proto [allowed by your patch]
- float [disallowed]

And a few not reachable from function fields (I think BTF validation checks that these can't be met, but would be good to double-check. If it doesn't, it should):
- func
- var
- datasec

So, effectively you disallow reading from tracing context fields of
type: struct (non-pointer), array, float and a few types that can't be specified for struct fields.

Does not seem necessary, tbh.

Quoted 36 lines

It seems - if (!btf_type_is_struct_ptr(btf, t)) - works, but it's challenging to strictly prove it's sufficiently future-proof.

Quoted 29 lines
Quoted 3 lines

This reflects my belief in a cautious approach: adding support only for selected types with tests added for each new type. That said, I can add the suggested broader condition and make it pass the tests,

but I cannot be sure it will be future-proof against conflicts.

Quoted 9 lines

I think the broader check like

      /* skip modifiers */
      tt = t;
      while (btf_type_is_modifier(tt))
              tt = btf_type_by_id(btf, tt->type);
      if (!btf_type_is_struct(tt))
              return true;

btf_type_is_struct_ptr() is almost identical to the snippet above.

Quoted 3 lines

might have some incompatibility with future changes, compared to explicit type checks for selected types. This condition is open-ended, including anything instead of selecting specific types.

What potential incompatibility do you expect?
Two things change:
- types other then struct foo * or int can be read:
- do you expect we would want to deny reading some ctx fields in the future?
- the value read is marked as scalar:
- not much can be done with a scalar, except for leaking it to e.g. some map or ring buffer. Do you expect this to problematic?

Quoted 3 lines

Note that the above are selected based on type, not on the function/parameter combination, which is already not a very effective filter if some parameters need to be hidden.

Quoted 8 lines

I do not think any of these represent a real problem. As I said, my approach is based mostly on narrowing the supported types to reduce potential conflicts.

I do not have a good example of such conflicts.
The added tests for pointer to float, which failed with - if (!btf_type_is_struct_ptr(btf, t)) - might be an example when adding a new type might silently pass this check because of missing tests.

Yes, but that does not really matter if verifier treats floats as unbound scalars.

Quoted 12 lines

I was not able to convince myself a conflict will not happen.

That said, changing

	if (is_ptr_treated_as_scalar(btf, t))
		return true;

	to

	if (!btf_type_is_struct_ptr(btf, t))
		return true;
Quoted 3 lines

just makes the scope of these changes wider. This was my initial approach to this problem, but I was worried by its wide scope.

Let's see what Alexei would say, but I'd say there is no need to complicate things w/o clear necessity.

#8 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 72 lines · Eduard Zingerman
Quoted 33 lines · Slava Imameev
Quoted 25 lines · Eduard Zingerman
Quoted 17 lines · Slava Imameev
Quoted 15 lines · Eduard Zingerman

kernel/bpf/btf.c bool btf_ctx_access(int off, int size, enum bpf_access_type type, · line 6925

Quoted 11 lines
kernel/bpf/btf.c +1 -5
@@ -6902,11 +6921,7 @@ 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_ptr_treated_as_scalar(btf, t))
              return true;

I'm probably missing a point here, but what's wrong with Alexei's suggestion to do this instead:

Quoted 3 lines
Quoted 3 lines

if (is_ptr_treated_as_scalar(btf, t))
return true;
?

Uh-oh, I copy-pasted the wrong snippet, sorry.
The correct snippet is:

Quoted 6 lines
         if (btf_type_is_struct_ptr(btf, t))
                  return true;

With it the selftests pass (except for float tests noted earlier). And regardless of selftests, the code below this point will error out if t is not a pointer to struct.

Quoted 8 lines

I think you tested with

	if (!btf_type_is_struct_ptr(btf, t))
		return true;

I decided on a narrower condition, as

- if (!btf_type_is_struct_ptr(btf, t)) -

Yes, sorry again.

Quoted 8 lines

changes the existing selection condition from "treat only these types as scalar" to "treat as scalar any type that is not a pointer to structure". Technically both approaches cover the problem I'm trying to solve - multilevel pointer support for structures, but the latter is open-ended and changes the current approach, which checks for pointers to int and void. So I'm extending this to int, void, enum 32/64, function, and corresponding multilevel pointers to these types and multilevel pointers to structures.

BTF is defined for the following non-modifier types:

- void        [allowed already]
- int         [allowed already]
- ptr         [multi-level pointers allowed by your patch]
- array       [disallowed?]
- struct      [single level pointers allowed already,

- union multi-level allowed by your patch]
- enum/enum64 [allowed by your patch]
- func_proto [allowed by your patch]
- float [disallowed]

And a few not reachable from function fields (I think BTF validation checks that these can't be met, but would be good to double-check. If it doesn't, it should):
- func
- var
- datasec

Quoted 5 lines

So, effectively you disallow reading from tracing context fields of
type: struct (non-pointer), array, float and a few types that can't be specified for struct fields.

Does not seem necessary, tbh.

I verified whether PTR->FUNC, PTR->DATASEC, PTR->VAR can be passed to btf_ctx_access() in the current mainline.

I added helpers that inject PTR->FUNC, PTR->DATASEC, PTR->VAR as pre or post calls to btf_check_meta(). In all cases, the BPF program load failed with errors "arg0 type FUNC / DATASEC / VAR is not a struct", which indicates that btf_check_meta() can indeed be called with PTR->FUNC, PTR->DATASEC, PTR->VAR.

If the condition for pointer check is changed to
if (!btf_type_is_struct_ptr(btf, t)), these BPF programs will load successfully with arguments set to scalar().

Do we accept this change in behavior?

Test case with invalid BTF types injection:
https://github.com/slava-at-cs/bpf/commit/c49af6500ace4e4aceee01c570e3b067aae7e48c

Branch:
https://github.com/slava-at-cs/bpf/commits/inject-invalid-btf/

To run test:
./vmtest.sh -- ./test_progs -t verifier_btf_ctx_access

The verifier log:
=============
0: R1=ctx() R10=fp0
; asm volatile (" \ @ verifier_btf_ctx_access.c:85
0: (79) r2 = *(u64 *)(r1 +0)
func 'bpf_fentry_test_invalid_ptr_func' arg0 type FUNC is not a struct
invalid bpf_context access off=0 size=8
processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============

=============
0: R1=ctx() R10=fp0
; asm volatile (" \ @ verifier_btf_ctx_access.c:85
0: (79) r2 = *(u64 *)(r1 +0)
func 'bpf_fentry_test_invalid_ptr_func' arg0 type DATASEC is not a struct invalid bpf_context access off=0 size=8
processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============

=============
0: R1=ctx() R10=fp0
; asm volatile (" \ @ verifier_btf_ctx_access.c:85
0: (79) r2 = *(u64 *)(r1 +0)
func 'bpf_fentry_test_invalid_ptr_func' arg0 type VAR is not a struct
invalid bpf_context access off=0 size=8
processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
=============

maintainer
#9 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

On Tue, 2026-03-10 at 23:16 +1100, Slava Imameev wrote:

[...]

Quoted 9 lines

I verified whether PTR->FUNC, PTR->DATASEC, PTR->VAR can be passed to btf_ctx_access() in the current mainline.

I added helpers that inject PTR->FUNC, PTR->DATASEC, PTR->VAR as pre or post calls to btf_check_meta(). In all cases, the BPF program load failed with errors "arg0 type FUNC / DATASEC / VAR is not a struct", which indicates that btf_check_meta() can indeed be called with PTR->FUNC, PTR->DATASEC, PTR->VAR.

Quoted 5 lines

If the condition for pointer check is changed to
if (!btf_type_is_struct_ptr(btf, t)), these BPF programs will load successfully with arguments set to scalar().

Do we accept this change in behavior?

Kernel validates BTF before loading, see kernel/bpf/btf.c:btf_resolve(). Validation is applied to kernel, module and program-level BTF. Does BTF containing PTR->DATASEC and PTR->VAR pass validation? If it does, validation should be updated to reject such cases. For PTR->FUNC, which one is legit PTR->FUNC or PTR->FUNC_PROTO? The legit one should be allowed and invalid should be rejected at validation phase.

You can craft invalid BTF as in the following selftest:
tools/testing/selftests/bpf/prog_tests/btf.c.

[...]

#10 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

Quoted 25 lines · Eduard Zingerman

[...]

Quoted 9 lines

I verified whether PTR->FUNC, PTR->DATASEC, PTR->VAR can be passed to btf_ctx_access() in the current mainline.

I added helpers that inject PTR->FUNC, PTR->DATASEC, PTR->VAR as pre or post calls to btf_check_meta(). In all cases, the BPF program load failed with errors "arg0 type FUNC / DATASEC / VAR is not a struct", which indicates that btf_check_meta() can indeed be called with PTR->FUNC, PTR->DATASEC, PTR->VAR.

Quoted 5 lines

If the condition for pointer check is changed to
if (!btf_type_is_struct_ptr(btf, t)), these BPF programs will load successfully with arguments set to scalar().

Do we accept this change in behavior?

Kernel validates BTF before loading, see kernel/bpf/btf.c:btf_resolve(). Validation is applied to kernel, module and program-level BTF. Does BTF containing PTR->DATASEC and PTR->VAR pass validation? If it does, validation should be updated to reject such cases. For PTR->FUNC, which one is legit PTR->FUNC or PTR->FUNC_PROTO? The legit one should be allowed and invalid should be rejected at validation phase.

Quoted 4 lines

You can craft invalid BTF as in the following selftest:
tools/testing/selftests/bpf/prog_tests/btf.c.

[...]

Invalid BTF pointer types PTR->DATASEC, PTR->FUNC, PTR->VAR are rejected by btf_ptr_resolve(), which is called through the sequence btf_check_all_types()->btf_resolve()->btf_ptr_resolve().

PTR->FUNC_PROTO is a valid type.

vmlinux BTF is processed by btf_parse_vmlinux, which calls btf_parse_base. Since btf_parse_base doesn't call btf_check_all_types, it becomes possible to invoke btf_ctx_access with PTR->DATASEC, PTR->FUNC, PTR->VAR in case of vmlinux BTF.

Modules and programs BTF are processed by btf_parse, which calls btf_check_all_types and detects invalid pointer types, so btf_ctx_access cannot see PTR->DATASEC, PTR->FUNC, PTR->VAR in these cases.

If btf_check_all_types is added to btf_parse_base, invalid pointer types get detected inside btf_parse_vmlinux, resulting in failure to process vmlinux BTF and effectively disabling BPF. libbpf returns this error:
libbpf: Error in bpf_object__probe_loading(): -EINVAL. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value.

If vmlinux BTF is trusted not to contain invalid types like PTR->DATASEC, PTR->FUNC, PTR->VAR, which seems reasonable, we can conclude that btf_ctx_access will never observe these types.

Adopting the view that vmlinux BTF is consistent, we can replace btf_ctx_access's condition for inferring scalar() for pointers from "if (is_void_or_int_ptr(btf, t))" to
"if (!btf_type_is_struct_ptr(btf, t))".

Otherwise, we need to either check types for vmlinux BTF, incurring additional cost, or use explicit checks for pointer types that can be inferred as scalar by btf_ctx_access to exclude invalid types. This would let invalid pointer types be rejected with an error like "func 'bpf_fentry_test_invalid_ptr_func' arg0 type FUNC is not a struct".

maintainer
#11 lore raw

Re: [PATCH bpf-next v4 1/2] bpf: Support new pointer param types via SCALAR_VALUE for trampolines

On Thu, 2026-03-12 at 00:07 +1100, Slava Imameev wrote:

[...]

Quoted 4 lines

Adopting the view that vmlinux BTF is consistent, we can replace btf_ctx_access's condition for inferring scalar() for pointers from "if (is_void_or_int_ptr(btf, t))" to
"if (!btf_type_is_struct_ptr(btf, t))".

I think it should be fine to adopt this view.

#12 lore raw

PATCH v4 2/2 selftests/bpf: Add trampolines single and multi-level pointer params test coverage

In reply to Slava Imameev

Single and multi-level pointer params and return value test coverage for BPF trampolines:
- fentry/fexit programs covering struct and void double/triple pointer parameters and return values
- verifier context tests covering pointers as parameters, these tests cover single and double pointers to int, enum 32 and 64, void, function, and double pointers to struct, triple pointers for void
- verifier context tests covering single and double pointers to float, to check proper error is returned as pointers to float are not supported
- verifier context tests covering pointers as return 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 behavior

---
 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   |  60 ++
 .../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_ptr_param.c        | 523 ++++++++++++++++++
 8 files changed, 1097 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_ptr_param.c
net/bpf/test_run.c +130 -0
index 178c4738e63b..73191c4a586e 100644
@@ -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,96 @@ 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)
+{
+	barrier_data(&id);
+	barrier_data(&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)
+{
+	barrier_data(&ppptr);
+	return (void ***)ppptr;
+}
+
+enum fentry_test_enum32;
+
+noinline void bpf_fentry_test15_penum32(enum fentry_test_enum32 *pe)
+{
+}
+
+enum fentry_test_enum64 {
+	TEST_ENUM64 = 0xffffffffFFFFFFFFULL
+};
+
+noinline void bpf_fentry_test15_penum64(enum fentry_test_enum64 *pe)
+{
+}
+
+noinline void bpf_fentry_test16_ppenum32(enum fentry_test_enum32 **ppe)
+{
+}
+
+noinline void bpf_fentry_test16_ppenum64(enum fentry_test_enum64 **ppe)
+{
+}
+
+noinline void bpf_fentry_test17_pfunc(void (*pf)(void))
+{
+}
+
+noinline void bpf_fentry_test18_ppfunc(void (**ppf)(void))
+{
+}
+
+noinline void bpf_fentry_test19_pfloat(float *pff)
+{
+}
+
+noinline void bpf_fentry_test20_ppfloat(float **ppff)
+{
+}
+
+noinline void bpf_fentry_test21_pchar(char *pc)
+{
+}
+
+noinline void bpf_fentry_test22_ppchar(char **ppc)
+{
+}
+
+noinline char **bpf_fentry_test23_ret_ppchar(void)
+{
+	return (char **)NULL;
+}
+
+noinline struct file **bpf_fentry_test24_ret_ppfile(void **a)
+{
+	return (struct file **)NULL;
+}
+
 noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo)
 {
 }
@@ -670,20 +762,58 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,
 	return data;
 }
 
+#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;
+	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")) {
+			/* valid kernel pointer, valid pointer after dereference */
+			CONSUME(bpf_fentry_test11_pptr_nullable(&ptr));
+			break;
+		} else if (!strcmp(attach_name, "bpf_fentry_test12_pptr")) {
+			/* valid kernel pointer, user pointer after dereference */
+			CONSUME(bpf_fentry_test12_pptr(0, &u32_ptr));
+			/* user address on most systems */
+			CONSUME(bpf_fentry_test12_pptr(1, (u32 **)17));
+			break;
+		} else if (!strcmp(attach_name, "bpf_fentry_test13_pptr")) {
+			/* should trigger extable on most systems */
+			CONSUME(bpf_fentry_test13_pptr((void **)~(1ull << 30)));
+			/* user address on most systems */
+			CONSUME(bpf_fentry_test13_pptr((void **)19));
+			/* kernel address at top 4KB, invalid */
+			CONSUME(bpf_fentry_test13_pptr(ERR_PTR(-ENOMEM)));
+			break;
+		} else if (!strcmp(attach_name, "bpf_fentry_test14_ppptr")) {
+			/* kernel address at top 4KB, invalid */
+			CONSUME(bpf_fentry_test14_ppptr(ERR_PTR(-ENOMEM)));
+			break;
+		}
+		fallthrough;
 	case BPF_TRACE_FEXIT:
 	case BPF_TRACE_FSESSION:
 		if (bpf_fentry_test1(1) != 2 ||
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
index 8cdfd74c95d7..bcf01cb4cfe4 100644
@@ -115,6 +115,7 @@
 #include "verifier_lsm.skel.h"
 #include "verifier_jit_inline.skel.h"
 #include "irq.skel.h"
+#include "verifier_ctx_ptr_param.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_ptr_param(void)       { RUN(verifier_ctx_ptr_param); }
 
 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 +60 -0

60 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
new file mode 100644
index 000000000000..15e908f0a1ed
@@ -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 boundary checks 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_ptr_param.c new file +523 -0

523 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#13 lore raw

Re: [PATCH bpf-next v4 2/2] selftests/bpf: Add trampolines single and multi-level pointer params test coverage

Quoted 18 lines · Slava Imameev

Single and multi-level pointer params and return value test coverage for BPF trampolines:
- fentry/fexit programs covering struct and void double/triple pointer parameters and return values
- verifier context tests covering pointers as parameters, these tests cover single and double pointers to int, enum 32 and 64, void, function, and double pointers to struct, triple pointers for void
- verifier context tests covering single and double pointers to float, to check proper error is returned as pointers to float are not supported
- verifier context tests covering pointers as return 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 behavior

Quoted 2 lines

---

Again, I probably miss a point, but with current implementation it seems sufficient in verifier_ctx_ptr_param() to add one or two tests accessing void** or similar and checking verification log to validate that parameter has expected type scalar(). Why so many tests are necessary?

[...]

#14 lore raw

Re: [PATCH bpf-next v4 2/2] selftests/bpf: Add trampolines single and multi-level pointer params test coverage

Quoted 19 lines · Eduard Zingerman
Quoted 18 lines · Slava Imameev

Single and multi-level pointer params and return value test coverage for BPF trampolines:
- fentry/fexit programs covering struct and void double/triple pointer parameters and return values
- verifier context tests covering pointers as parameters, these tests cover single and double pointers to int, enum 32 and 64, void, function, and double pointers to struct, triple pointers for void
- verifier context tests covering single and double pointers to float, to check proper error is returned as pointers to float are not supported
- verifier context tests covering pointers as return 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 behavior

Quoted 8 lines
Quoted 2 lines

---

Again, I probably miss a point, but with current implementation it seems sufficient in verifier_ctx_ptr_param() to add one or two tests accessing void** or similar and checking verification log to validate that parameter has expected type scalar(). Why so many tests are necessary?

This reflects my belief in more comprehensive test coverage.

I can certainly reduce the number of tests if this seems excessive, but I made 90% of the added tests one-liners to keep them maintainable.

These changes add support for multilevel pointers, so double and triple pointers need to be checked at minimum. I think adding checks for any new type support is beneficial. I tried to verify a broader set of conditions that might be broken by future changes. I tried to make most tests one-liners to facilitate future modification.