2020-12-03 12:46:26 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2020 Facebook */
|
2021-10-02 06:47:57 +05:30
|
|
|
#include <linux/btf.h>
|
|
|
|
#include <linux/btf_ids.h>
|
2020-12-03 12:46:26 -08:00
|
|
|
#include <linux/error-injection.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
2021-01-11 23:55:20 -08:00
|
|
|
#include <linux/percpu-defs.h>
|
2020-12-03 12:46:26 -08:00
|
|
|
#include <linux/sysfs.h>
|
|
|
|
#include <linux/tracepoint.h>
|
|
|
|
#include "bpf_testmod.h"
|
2023-05-15 15:37:56 +02:00
|
|
|
#include "bpf_testmod_kfunc.h"
|
2020-12-03 12:46:26 -08:00
|
|
|
|
|
|
|
#define CREATE_TRACE_POINTS
|
|
|
|
#include "bpf_testmod-events.h"
|
|
|
|
|
2022-02-04 01:55:19 +01:00
|
|
|
typedef int (*func_proto_typedef)(long);
|
|
|
|
typedef int (*func_proto_typedef_nested1)(func_proto_typedef);
|
|
|
|
typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
|
|
|
|
|
2021-01-11 23:55:20 -08:00
|
|
|
DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
|
2022-08-31 08:27:13 -07:00
|
|
|
long bpf_testmod_test_struct_arg_result;
|
|
|
|
|
|
|
|
struct bpf_testmod_struct_arg_1 {
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
struct bpf_testmod_struct_arg_2 {
|
|
|
|
long a;
|
|
|
|
long b;
|
|
|
|
};
|
|
|
|
|
2023-04-20 11:27:35 +08:00
|
|
|
struct bpf_testmod_struct_arg_3 {
|
|
|
|
int a;
|
|
|
|
int b[];
|
|
|
|
};
|
|
|
|
|
2023-04-11 20:46:47 -07:00
|
|
|
__diag_push();
|
|
|
|
__diag_ignore_all("-Wmissing-prototypes",
|
|
|
|
"Global functions as their definitions will be in bpf_testmod.ko BTF");
|
|
|
|
|
2022-08-31 08:27:13 -07:00
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_1(struct bpf_testmod_struct_arg_2 a, int b, int c) {
|
|
|
|
bpf_testmod_test_struct_arg_result = a.a + a.b + b + c;
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_2(int a, struct bpf_testmod_struct_arg_2 b, int c) {
|
|
|
|
bpf_testmod_test_struct_arg_result = a + b.a + b.b + c;
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_3(int a, int b, struct bpf_testmod_struct_arg_2 c) {
|
|
|
|
bpf_testmod_test_struct_arg_result = a + b + c.a + c.b;
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_4(struct bpf_testmod_struct_arg_1 a, int b,
|
|
|
|
int c, int d, struct bpf_testmod_struct_arg_2 e) {
|
|
|
|
bpf_testmod_test_struct_arg_result = a.a + b + c + d + e.a + e.b;
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_5(void) {
|
|
|
|
bpf_testmod_test_struct_arg_result = 1;
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
2021-01-11 23:55:20 -08:00
|
|
|
|
2023-04-20 11:27:35 +08:00
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_struct_arg_6(struct bpf_testmod_struct_arg_3 *a) {
|
|
|
|
bpf_testmod_test_struct_arg_result = a->b[0];
|
|
|
|
return bpf_testmod_test_struct_arg_result;
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:30:15 -06:00
|
|
|
__bpf_kfunc void
|
2021-10-02 06:47:57 +05:30
|
|
|
bpf_testmod_test_mod_kfunc(int i)
|
|
|
|
{
|
|
|
|
*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:41:21 -08:00
|
|
|
__bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
|
|
|
|
{
|
|
|
|
if (cnt < 0) {
|
|
|
|
it->cnt = 0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
it->value = value;
|
|
|
|
it->cnt = cnt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
|
|
|
|
{
|
|
|
|
if (it->cnt <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
it->cnt--;
|
|
|
|
|
|
|
|
return &it->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
|
|
|
|
{
|
|
|
|
it->cnt = 0;
|
|
|
|
}
|
|
|
|
|
selftests/bpf: add a selftest with __user tag
Added a selftest with three__user usages: a __user pointer-type argument
in bpf_testmod, a __user pointer-type struct member in bpf_testmod,
and a __user pointer-type struct member in vmlinux. In all cases,
directly accessing the user memory will result verification failure.
$ ./test_progs -v -n 22/3
...
libbpf: prog 'test_user1': BPF program load failed: Permission denied
libbpf: prog 'test_user1': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user1, struct bpf_testmod_btf_type_tag_1 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_1' arg0 has btf_id 136561 type STRUCT 'bpf_testmod_btf_type_tag_1'
1: R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->a;
1: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/3 btf_tag/btf_type_tag_user_mod1:OK
$ ./test_progs -v -n 22/4
...
libbpf: prog 'test_user2': BPF program load failed: Permission denied
libbpf: prog 'test_user2': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user2, struct bpf_testmod_btf_type_tag_2 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_2' arg0 has btf_id 136563 type STRUCT 'bpf_testmod_btf_type_tag_2'
1: R1_w=ptr_bpf_testmod_btf_type_tag_2(id=0,off=0,imm=0)
; g = arg->p->a;
1: (79) r1 = *(u64 *)(r1 +0) ; R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->p->a;
2: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/4 btf_tag/btf_type_tag_user_mod2:OK
$ ./test_progs -v -n 22/5
...
libbpf: prog 'test_sys_getsockname': BPF program load failed: Permission denied
libbpf: prog 'test_sys_getsockname': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_sys_getsockname, int fd, struct sockaddr *usockaddr,
0: (79) r1 = *(u64 *)(r1 +8)
func '__sys_getsockname' arg1 has btf_id 2319 type STRUCT 'sockaddr'
1: R1_w=user_ptr_sockaddr(id=0,off=0,imm=0)
; g = usockaddr->sa_family;
1: (69) r1 = *(u16 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/5 btf_tag/btf_type_tag_user_vmlinux:OK
Signed-off-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20220127154616.659314-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2022-01-27 07:46:16 -08:00
|
|
|
struct bpf_testmod_btf_type_tag_1 {
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bpf_testmod_btf_type_tag_2 {
|
|
|
|
struct bpf_testmod_btf_type_tag_1 __user *p;
|
|
|
|
};
|
|
|
|
|
2022-03-04 11:16:57 -08:00
|
|
|
struct bpf_testmod_btf_type_tag_3 {
|
|
|
|
struct bpf_testmod_btf_type_tag_1 __percpu *p;
|
|
|
|
};
|
|
|
|
|
selftests/bpf: add a selftest with __user tag
Added a selftest with three__user usages: a __user pointer-type argument
in bpf_testmod, a __user pointer-type struct member in bpf_testmod,
and a __user pointer-type struct member in vmlinux. In all cases,
directly accessing the user memory will result verification failure.
$ ./test_progs -v -n 22/3
...
libbpf: prog 'test_user1': BPF program load failed: Permission denied
libbpf: prog 'test_user1': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user1, struct bpf_testmod_btf_type_tag_1 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_1' arg0 has btf_id 136561 type STRUCT 'bpf_testmod_btf_type_tag_1'
1: R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->a;
1: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/3 btf_tag/btf_type_tag_user_mod1:OK
$ ./test_progs -v -n 22/4
...
libbpf: prog 'test_user2': BPF program load failed: Permission denied
libbpf: prog 'test_user2': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user2, struct bpf_testmod_btf_type_tag_2 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_2' arg0 has btf_id 136563 type STRUCT 'bpf_testmod_btf_type_tag_2'
1: R1_w=ptr_bpf_testmod_btf_type_tag_2(id=0,off=0,imm=0)
; g = arg->p->a;
1: (79) r1 = *(u64 *)(r1 +0) ; R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->p->a;
2: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/4 btf_tag/btf_type_tag_user_mod2:OK
$ ./test_progs -v -n 22/5
...
libbpf: prog 'test_sys_getsockname': BPF program load failed: Permission denied
libbpf: prog 'test_sys_getsockname': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_sys_getsockname, int fd, struct sockaddr *usockaddr,
0: (79) r1 = *(u64 *)(r1 +8)
func '__sys_getsockname' arg1 has btf_id 2319 type STRUCT 'sockaddr'
1: R1_w=user_ptr_sockaddr(id=0,off=0,imm=0)
; g = usockaddr->sa_family;
1: (69) r1 = *(u16 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/5 btf_tag/btf_type_tag_user_vmlinux:OK
Signed-off-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20220127154616.659314-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2022-01-27 07:46:16 -08:00
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_btf_type_tag_user_1(struct bpf_testmod_btf_type_tag_1 __user *arg) {
|
2022-02-04 01:55:19 +01:00
|
|
|
BTF_TYPE_EMIT(func_proto_typedef);
|
|
|
|
BTF_TYPE_EMIT(func_proto_typedef_nested1);
|
|
|
|
BTF_TYPE_EMIT(func_proto_typedef_nested2);
|
selftests/bpf: add a selftest with __user tag
Added a selftest with three__user usages: a __user pointer-type argument
in bpf_testmod, a __user pointer-type struct member in bpf_testmod,
and a __user pointer-type struct member in vmlinux. In all cases,
directly accessing the user memory will result verification failure.
$ ./test_progs -v -n 22/3
...
libbpf: prog 'test_user1': BPF program load failed: Permission denied
libbpf: prog 'test_user1': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user1, struct bpf_testmod_btf_type_tag_1 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_1' arg0 has btf_id 136561 type STRUCT 'bpf_testmod_btf_type_tag_1'
1: R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->a;
1: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/3 btf_tag/btf_type_tag_user_mod1:OK
$ ./test_progs -v -n 22/4
...
libbpf: prog 'test_user2': BPF program load failed: Permission denied
libbpf: prog 'test_user2': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_user2, struct bpf_testmod_btf_type_tag_2 *arg)
0: (79) r1 = *(u64 *)(r1 +0)
func 'bpf_testmod_test_btf_type_tag_user_2' arg0 has btf_id 136563 type STRUCT 'bpf_testmod_btf_type_tag_2'
1: R1_w=ptr_bpf_testmod_btf_type_tag_2(id=0,off=0,imm=0)
; g = arg->p->a;
1: (79) r1 = *(u64 *)(r1 +0) ; R1_w=user_ptr_bpf_testmod_btf_type_tag_1(id=0,off=0,imm=0)
; g = arg->p->a;
2: (61) r1 = *(u32 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/4 btf_tag/btf_type_tag_user_mod2:OK
$ ./test_progs -v -n 22/5
...
libbpf: prog 'test_sys_getsockname': BPF program load failed: Permission denied
libbpf: prog 'test_sys_getsockname': -- BEGIN PROG LOAD LOG --
R1 type=ctx expected=fp
0: R1=ctx(id=0,off=0,imm=0) R10=fp0
; int BPF_PROG(test_sys_getsockname, int fd, struct sockaddr *usockaddr,
0: (79) r1 = *(u64 *)(r1 +8)
func '__sys_getsockname' arg1 has btf_id 2319 type STRUCT 'sockaddr'
1: R1_w=user_ptr_sockaddr(id=0,off=0,imm=0)
; g = usockaddr->sa_family;
1: (69) r1 = *(u16 *)(r1 +0)
R1 invalid mem access 'user_ptr_'
...
#22/5 btf_tag/btf_type_tag_user_vmlinux:OK
Signed-off-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20220127154616.659314-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2022-01-27 07:46:16 -08:00
|
|
|
return arg->a;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_btf_type_tag_user_2(struct bpf_testmod_btf_type_tag_2 *arg) {
|
|
|
|
return arg->p->a;
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:16:57 -08:00
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_btf_type_tag_percpu_1(struct bpf_testmod_btf_type_tag_1 __percpu *arg) {
|
|
|
|
return arg->a;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int
|
|
|
|
bpf_testmod_test_btf_type_tag_percpu_2(struct bpf_testmod_btf_type_tag_3 *arg) {
|
|
|
|
return arg->p->a;
|
|
|
|
}
|
|
|
|
|
2021-09-10 11:33:52 -07:00
|
|
|
noinline int bpf_testmod_loop_test(int n)
|
|
|
|
{
|
selftests/bpf: Keep the loop in bpf_testmod_loop_test
Some compilers (for example clang-15) optimize bpf_testmod_loop_test and
remove the loop:
gcc version
(gdb) disassemble bpf_testmod_loop_test
Dump of assembler code for function bpf_testmod_loop_test:
0x0000000000000570 <+0>: callq 0x575 <bpf_testmod_loop_test+5>
0x0000000000000575 <+5>: xor %eax,%eax
0x0000000000000577 <+7>: test %edi,%edi
0x0000000000000579 <+9>: jle 0x587 <bpf_testmod_loop_test+23>
0x000000000000057b <+11>: xor %edx,%edx
0x000000000000057d <+13>: add %edx,%eax
0x000000000000057f <+15>: add $0x1,%edx
0x0000000000000582 <+18>: cmp %edx,%edi
0x0000000000000584 <+20>: jne 0x57d <bpf_testmod_loop_test+13>
0x0000000000000586 <+22>: retq
0x0000000000000587 <+23>: retq
clang-15 version
(gdb) disassemble bpf_testmod_loop_test
Dump of assembler code for function bpf_testmod_loop_test:
0x0000000000000450 <+0>: nopl 0x0(%rax,%rax,1)
0x0000000000000455 <+5>: test %edi,%edi
0x0000000000000457 <+7>: jle 0x46b <bpf_testmod_loop_test+27>
0x0000000000000459 <+9>: lea -0x1(%rdi),%eax
0x000000000000045c <+12>: lea -0x2(%rdi),%ecx
0x000000000000045f <+15>: imul %rax,%rcx
0x0000000000000463 <+19>: shr %rcx
0x0000000000000466 <+22>: lea -0x1(%rdi,%rcx,1),%eax
0x000000000000046a <+26>: retq
0x000000000000046b <+27>: xor %eax,%eax
0x000000000000046d <+29>: retq
Note: The jne instruction is removed in clang-15 version.
Force the compile to keep the loop by making sum volatile.
Signed-off-by: Song Liu <song@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20230412210423.900851-4-song@kernel.org
2023-04-12 14:04:23 -07:00
|
|
|
/* Make sum volatile, so smart compilers, such as clang, will not
|
|
|
|
* optimize the code by removing the loop.
|
|
|
|
*/
|
|
|
|
volatile int sum = 0;
|
|
|
|
int i;
|
2021-09-10 11:33:52 -07:00
|
|
|
|
|
|
|
/* the primary goal of this test is to test LBR. Create a lot of
|
|
|
|
* branches in the function, so we can catch it easily.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
sum += i;
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2021-12-15 12:35:34 -08:00
|
|
|
__weak noinline struct file *bpf_testmod_return_ptr(int arg)
|
|
|
|
{
|
|
|
|
static struct file f = {};
|
|
|
|
|
|
|
|
switch (arg) {
|
|
|
|
case 1: return (void *)EINVAL; /* user addr */
|
|
|
|
case 2: return (void *)0xcafe4a11; /* user addr */
|
|
|
|
case 3: return (void *)-EINVAL; /* canonical, but invalid */
|
|
|
|
case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
|
|
|
|
case 5: return (void *)~(1ull << 30); /* trigger extable */
|
|
|
|
case 6: return &f; /* valid addr */
|
|
|
|
case 7: return (void *)((long)&f | 1); /* kernel tricks */
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:41:46 +02:00
|
|
|
noinline int bpf_testmod_fentry_test1(int a)
|
|
|
|
{
|
|
|
|
return a + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int bpf_testmod_fentry_test2(int a, u64 b)
|
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
noinline int bpf_testmod_fentry_test3(char a, int b, u64 c)
|
|
|
|
{
|
|
|
|
return a + b + c;
|
|
|
|
}
|
|
|
|
|
2023-04-11 20:46:47 -07:00
|
|
|
__diag_pop();
|
|
|
|
|
2022-10-25 15:41:46 +02:00
|
|
|
int bpf_testmod_fentry_ok;
|
|
|
|
|
2020-12-03 12:46:26 -08:00
|
|
|
noinline ssize_t
|
|
|
|
bpf_testmod_test_read(struct file *file, struct kobject *kobj,
|
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t off, size_t len)
|
|
|
|
{
|
|
|
|
struct bpf_testmod_test_read_ctx ctx = {
|
|
|
|
.buf = buf,
|
|
|
|
.off = off,
|
|
|
|
.len = len,
|
|
|
|
};
|
2022-08-31 08:27:13 -07:00
|
|
|
struct bpf_testmod_struct_arg_1 struct_arg1 = {10};
|
|
|
|
struct bpf_testmod_struct_arg_2 struct_arg2 = {2, 3};
|
2023-04-20 11:27:35 +08:00
|
|
|
struct bpf_testmod_struct_arg_3 *struct_arg3;
|
2021-12-15 12:35:34 -08:00
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
while (bpf_testmod_return_ptr(i))
|
|
|
|
i++;
|
2020-12-03 12:46:26 -08:00
|
|
|
|
2022-08-31 08:27:13 -07:00
|
|
|
(void)bpf_testmod_test_struct_arg_1(struct_arg2, 1, 4);
|
|
|
|
(void)bpf_testmod_test_struct_arg_2(1, struct_arg2, 4);
|
|
|
|
(void)bpf_testmod_test_struct_arg_3(1, 4, struct_arg2);
|
|
|
|
(void)bpf_testmod_test_struct_arg_4(struct_arg1, 1, 2, 3, struct_arg2);
|
|
|
|
(void)bpf_testmod_test_struct_arg_5();
|
|
|
|
|
2023-04-20 11:27:35 +08:00
|
|
|
struct_arg3 = kmalloc((sizeof(struct bpf_testmod_struct_arg_3) +
|
|
|
|
sizeof(int)), GFP_KERNEL);
|
|
|
|
if (struct_arg3 != NULL) {
|
|
|
|
struct_arg3->b[0] = 1;
|
|
|
|
(void)bpf_testmod_test_struct_arg_6(struct_arg3);
|
|
|
|
kfree(struct_arg3);
|
|
|
|
}
|
|
|
|
|
2021-09-10 11:33:52 -07:00
|
|
|
/* This is always true. Use the check to make sure the compiler
|
|
|
|
* doesn't remove bpf_testmod_loop_test.
|
|
|
|
*/
|
|
|
|
if (bpf_testmod_loop_test(101) > 100)
|
|
|
|
trace_bpf_testmod_test_read(current, &ctx);
|
2020-12-03 12:46:26 -08:00
|
|
|
|
2021-10-04 17:48:57 +08:00
|
|
|
/* Magic number to enable writable tp */
|
|
|
|
if (len == 64) {
|
|
|
|
struct bpf_testmod_test_writable_ctx writable = {
|
|
|
|
.val = 1024,
|
|
|
|
};
|
|
|
|
trace_bpf_testmod_test_writable_bare(&writable);
|
|
|
|
if (writable.early_ret)
|
|
|
|
return snprintf(buf, len, "%d\n", writable.val);
|
|
|
|
}
|
|
|
|
|
2022-10-25 15:41:46 +02:00
|
|
|
if (bpf_testmod_fentry_test1(1) != 2 ||
|
|
|
|
bpf_testmod_fentry_test2(2, 3) != 5 ||
|
|
|
|
bpf_testmod_fentry_test3(4, 5, 6) != 15)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
bpf_testmod_fentry_ok = 1;
|
|
|
|
out:
|
2020-12-03 12:46:26 -08:00
|
|
|
return -EIO; /* always fail */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bpf_testmod_test_read);
|
|
|
|
ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
|
|
|
|
|
2021-01-19 12:22:37 +00:00
|
|
|
noinline ssize_t
|
|
|
|
bpf_testmod_test_write(struct file *file, struct kobject *kobj,
|
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t off, size_t len)
|
|
|
|
{
|
|
|
|
struct bpf_testmod_test_write_ctx ctx = {
|
|
|
|
.buf = buf,
|
|
|
|
.off = off,
|
|
|
|
.len = len,
|
|
|
|
};
|
|
|
|
|
|
|
|
trace_bpf_testmod_test_write_bare(current, &ctx);
|
|
|
|
|
|
|
|
return -EIO; /* always fail */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bpf_testmod_test_write);
|
|
|
|
ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
|
|
|
|
|
2020-12-03 12:46:26 -08:00
|
|
|
static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
|
2021-01-19 12:22:37 +00:00
|
|
|
.attr = { .name = "bpf_testmod", .mode = 0666, },
|
2020-12-03 12:46:26 -08:00
|
|
|
.read = bpf_testmod_test_read,
|
2021-01-19 12:22:37 +00:00
|
|
|
.write = bpf_testmod_test_write,
|
2020-12-03 12:46:26 -08:00
|
|
|
};
|
|
|
|
|
2023-03-08 10:41:21 -08:00
|
|
|
BTF_SET8_START(bpf_testmod_common_kfunc_ids)
|
|
|
|
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
|
|
|
|
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
|
|
|
|
BTF_SET8_END(bpf_testmod_common_kfunc_ids)
|
|
|
|
|
|
|
|
static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.set = &bpf_testmod_common_kfunc_ids,
|
|
|
|
};
|
|
|
|
|
2023-05-15 15:37:56 +02:00
|
|
|
__bpf_kfunc u64 bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d)
|
|
|
|
{
|
|
|
|
return a + b + c + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc int bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b)
|
|
|
|
{
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc struct sock *bpf_kfunc_call_test3(struct sock *sk)
|
|
|
|
{
|
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc long noinline bpf_kfunc_call_test4(signed char a, short b, int c, long d)
|
|
|
|
{
|
|
|
|
/* Provoke the compiler to assume that the caller has sign-extended a,
|
|
|
|
* b and c on platforms where this is required (e.g. s390x).
|
|
|
|
*/
|
|
|
|
return (long)a + (long)b + (long)c + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct prog_test_ref_kfunc prog_test_struct = {
|
|
|
|
.a = 42,
|
|
|
|
.b = 108,
|
|
|
|
.next = &prog_test_struct,
|
|
|
|
.cnt = REFCOUNT_INIT(1),
|
|
|
|
};
|
|
|
|
|
|
|
|
__bpf_kfunc struct prog_test_ref_kfunc *
|
|
|
|
bpf_kfunc_call_test_acquire(unsigned long *scalar_ptr)
|
|
|
|
{
|
|
|
|
refcount_inc(&prog_test_struct.cnt);
|
|
|
|
return &prog_test_struct;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_offset(struct prog_test_ref_kfunc *p)
|
|
|
|
{
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc struct prog_test_member *
|
|
|
|
bpf_kfunc_call_memb_acquire(void)
|
|
|
|
{
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p)
|
|
|
|
{
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int *__bpf_kfunc_call_test_get_mem(struct prog_test_ref_kfunc *p, const int size)
|
|
|
|
{
|
|
|
|
if (size > 2 * sizeof(int))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (int *)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p,
|
|
|
|
const int rdwr_buf_size)
|
|
|
|
{
|
|
|
|
return __bpf_kfunc_call_test_get_mem(p, rdwr_buf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p,
|
|
|
|
const int rdonly_buf_size)
|
|
|
|
{
|
|
|
|
return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the next 2 ones can't be really used for testing expect to ensure
|
|
|
|
* that the verifier rejects the call.
|
|
|
|
* Acquire functions must return struct pointers, so these ones are
|
|
|
|
* failing.
|
|
|
|
*/
|
|
|
|
__bpf_kfunc int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p,
|
|
|
|
const int rdonly_buf_size)
|
|
|
|
{
|
|
|
|
return __bpf_kfunc_call_test_get_mem(p, rdonly_buf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_int_mem_release(int *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p)
|
|
|
|
{
|
|
|
|
/* p != NULL, but p->cnt could be 0 */
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc void bpf_kfunc_call_test_destructive(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
__bpf_kfunc static u32 bpf_kfunc_call_test_static_unused_arg(u32 arg, u32 unused)
|
|
|
|
{
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:42:35 +02:00
|
|
|
BTF_SET8_START(bpf_testmod_check_kfunc_ids)
|
|
|
|
BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc)
|
2023-05-15 15:37:56 +02:00
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test2)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test3)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test4)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_pass1)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail1)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_mem_len_fail2)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_acquire, KF_ACQUIRE | KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_memb_acquire, KF_ACQUIRE | KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_memb1_release, KF_RELEASE)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdwr_mem, KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_get_rdonly_mem, KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_acq_rdonly_mem, KF_ACQUIRE | KF_RET_NULL)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_int_mem_release, KF_RELEASE)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass_ctx)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass1)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_pass2)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail1)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail2)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_fail3)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_ref, KF_TRUSTED_ARGS | KF_RCU)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_destructive, KF_DESTRUCTIVE)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
|
|
|
|
BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
|
2022-07-21 15:42:35 +02:00
|
|
|
BTF_SET8_END(bpf_testmod_check_kfunc_ids)
|
2021-10-02 06:47:57 +05:30
|
|
|
|
2022-01-14 22:09:46 +05:30
|
|
|
static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
|
2022-07-21 15:42:35 +02:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.set = &bpf_testmod_check_kfunc_ids,
|
2022-01-14 22:09:46 +05:30
|
|
|
};
|
2021-10-02 06:47:57 +05:30
|
|
|
|
2023-03-10 08:41:00 +01:00
|
|
|
noinline int bpf_fentry_shadow_test(int a)
|
|
|
|
{
|
|
|
|
return a + 2;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(bpf_fentry_shadow_test);
|
|
|
|
|
2022-01-14 22:09:53 +05:30
|
|
|
extern int bpf_fentry_test1(int a);
|
|
|
|
|
2020-12-03 12:46:26 -08:00
|
|
|
static int bpf_testmod_init(void)
|
|
|
|
{
|
2021-10-02 06:47:57 +05:30
|
|
|
int ret;
|
|
|
|
|
2023-03-08 10:41:21 -08:00
|
|
|
ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_testmod_common_kfunc_set);
|
|
|
|
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
|
2023-05-15 15:37:56 +02:00
|
|
|
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
|
|
|
|
ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
|
2022-01-14 22:09:46 +05:30
|
|
|
if (ret < 0)
|
2021-10-02 06:47:57 +05:30
|
|
|
return ret;
|
2022-01-14 22:09:53 +05:30
|
|
|
if (bpf_fentry_test1(0) < 0)
|
|
|
|
return -EINVAL;
|
2022-01-14 22:09:46 +05:30
|
|
|
return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
|
2020-12-03 12:46:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bpf_testmod_exit(void)
|
|
|
|
{
|
|
|
|
return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(bpf_testmod_init);
|
|
|
|
module_exit(bpf_testmod_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Andrii Nakryiko");
|
|
|
|
MODULE_DESCRIPTION("BPF selftests module");
|
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|