mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-17 09:41:09 +02:00
selftests/bpf: Test stack-passed struct_ops arena arguments
Add a test_arena_stack member with eight leading scalar arguments so the arena pointer is passed on the stack. The callback validates the first and last scalar ctx slots before dereferencing the pointer in ctx[8]. This exercises the indirect trampoline stack layout and arena conversion together, and prevents a regression where stack arguments are read one slot late. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Tested-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://patch.msgid.link/20260808003938.3486067-13-memxor@gmail.com Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
This commit is contained in:
committed by
Eduard Zingerman
parent
596824de8c
commit
2d4de9a493
@@ -46,10 +46,24 @@ int test_arena_nullable_cb(unsigned long long *ctx)
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("struct_ops/test_arena_stack")
|
||||
int test_arena_stack_cb(unsigned long long *ctx)
|
||||
{
|
||||
u64 __arena *ptr = (u64 __arena *)ctx[8];
|
||||
|
||||
arena_touch++;
|
||||
/* pin the slot layout: the leading args fill ctx[0]..ctx[7] */
|
||||
if (ctx[0] != 1 || ctx[7] != 8)
|
||||
return 0xbad;
|
||||
*ptr += 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC(".struct_ops.link")
|
||||
struct bpf_testmod_ops3 testmod_arena = {
|
||||
.test_arena = (void *)test_arena_cb,
|
||||
.test_arena_nullable = (void *)test_arena_nullable_cb,
|
||||
.test_arena_stack = (void *)test_arena_stack_cb,
|
||||
};
|
||||
|
||||
SEC("syscall")
|
||||
@@ -88,6 +102,13 @@ int trigger(void *ctx)
|
||||
if (ret != 0xbee)
|
||||
return 7;
|
||||
|
||||
/* the arena pointer is stack-passed into the trampoline here */
|
||||
ret = bpf_testmod_ops3_call_test_arena_stack((u64 *)val);
|
||||
if (ret)
|
||||
return 8;
|
||||
if (*val != 44)
|
||||
return 9;
|
||||
|
||||
bpf_arena_free_pages(&arena, (void __arena *)val, 1);
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
@@ -395,11 +395,19 @@ static int bpf_testmod_ops3__test_arena_nullable(u64 *ptr__arena__nullable)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bpf_testmod_ops3__test_arena_stack(u64 a, u64 b, u64 c, u64 d,
|
||||
u64 e, u64 f, u64 g, u64 h,
|
||||
u64 *ptr__arena)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct bpf_testmod_ops3 __bpf_testmod_ops3 = {
|
||||
.test_1 = bpf_testmod_test_3,
|
||||
.test_2 = bpf_testmod_test_4,
|
||||
.test_arena = bpf_testmod_ops3__test_arena,
|
||||
.test_arena_nullable = bpf_testmod_ops3__test_arena_nullable,
|
||||
.test_arena_stack = bpf_testmod_ops3__test_arena_stack,
|
||||
};
|
||||
|
||||
static void bpf_testmod_test_struct_ops3(void)
|
||||
@@ -428,6 +436,11 @@ __bpf_kfunc int bpf_testmod_ops3_call_test_arena_nullable(u64 *ptr__arena__nulla
|
||||
return st_ops3->test_arena_nullable(ptr__arena__nullable);
|
||||
}
|
||||
|
||||
__bpf_kfunc int bpf_testmod_ops3_call_test_arena_stack(u64 *ptr__arena)
|
||||
{
|
||||
return st_ops3->test_arena_stack(1, 2, 3, 4, 5, 6, 7, 8, ptr__arena);
|
||||
}
|
||||
|
||||
struct bpf_testmod_btf_type_tag_1 {
|
||||
int a;
|
||||
};
|
||||
@@ -838,6 +851,7 @@ BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)
|
||||
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2)
|
||||
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena)
|
||||
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena_nullable)
|
||||
BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_arena_stack)
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_get_default_trusted_ptr_test);
|
||||
BTF_ID_FLAGS(func, bpf_kfunc_put_default_trusted_ptr_test);
|
||||
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
|
||||
|
||||
@@ -109,6 +109,9 @@ struct bpf_testmod_ops3 {
|
||||
/* Used to test arena pointer arguments. */
|
||||
int (*test_arena)(u64 *ptr);
|
||||
int (*test_arena_nullable)(u64 *ptr);
|
||||
/* enough leading args to force @ptr onto the stack on x86 and arm64 */
|
||||
int (*test_arena_stack)(u64 a, u64 b, u64 c, u64 d, u64 e, u64 f,
|
||||
u64 g, u64 h, u64 *ptr);
|
||||
};
|
||||
|
||||
struct st_ops_args {
|
||||
|
||||
@@ -122,6 +122,7 @@ u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused) __ksym;
|
||||
void bpf_testmod_test_mod_kfunc(int i) __ksym;
|
||||
int bpf_testmod_ops3_call_test_arena(__u64 *ptr__arena) __ksym;
|
||||
int bpf_testmod_ops3_call_test_arena_nullable(__u64 *ptr__arena__nullable) __ksym;
|
||||
int bpf_testmod_ops3_call_test_arena_stack(__u64 *ptr__arena) __ksym;
|
||||
|
||||
__u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
|
||||
__u32 c, __u64 d) __ksym;
|
||||
|
||||
Reference in New Issue
Block a user