Skip to content

Commit c196906

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Add dummy BPF STRUCT_OPS for test purpose
Currently the test of BPF STRUCT_OPS depends on the specific bpf implementation of tcp_congestion_ops, but it can not cover all basic functionalities (e.g, return value handling), so introduce a dummy BPF STRUCT_OPS for test purpose. Loading a bpf_dummy_ops implementation from userspace is prohibited, and its only purpose is to run BPF_PROG_TYPE_STRUCT_OPS program through bpf(BPF_PROG_TEST_RUN). Now programs for test_1() & test_2() are supported. The following three cases are exercised in bpf_dummy_struct_ops_test_run(): (1) test and check the value returned from state arg in test_1(state) The content of state is copied from userspace pointer and copied back after calling test_1(state). The user pointer is saved in an u64 array and the array address is passed through ctx_in. (2) test and check the return value of test_1(NULL) Just simulate the case in which an invalid input argument is passed in. (3) test multiple arguments passing in test_2(state, ...) 5 arguments are passed through ctx_in in form of u64 array. The first element of array is userspace pointer of state and others 4 arguments follow. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 35346ab commit c196906

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

include/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,22 @@ static inline void bpf_module_put(const void *data, struct module *owner)
10181018
else
10191019
module_put(owner);
10201020
}
1021+
1022+
#ifdef CONFIG_NET
1023+
/* Define it here to avoid the use of forward declaration */
1024+
struct bpf_dummy_ops_state {
1025+
int val;
1026+
};
1027+
1028+
struct bpf_dummy_ops {
1029+
int (*test_1)(struct bpf_dummy_ops_state *cb);
1030+
int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1031+
char a3, unsigned long a4);
1032+
};
1033+
1034+
int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1035+
union bpf_attr __user *uattr);
1036+
#endif
10211037
#else
10221038
static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
10231039
{

kernel/bpf/bpf_struct_ops.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ const struct bpf_verifier_ops bpf_struct_ops_verifier_ops = {
9393
};
9494

9595
const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
96+
#ifdef CONFIG_NET
97+
.test_run = bpf_struct_ops_test_run,
98+
#endif
9699
};
97100

98101
static const struct btf_type *module_type;

kernel/bpf/bpf_struct_ops_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
/* internal file - do not include directly */
33

44
#ifdef CONFIG_BPF_JIT
5+
#ifdef CONFIG_NET
6+
BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
7+
#endif
58
#ifdef CONFIG_INET
69
#include <net/tcp.h>
710
BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)

net/bpf/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
obj-$(CONFIG_BPF_SYSCALL) := test_run.o
3+
ifeq ($(CONFIG_BPF_JIT),y)
4+
obj-$(CONFIG_BPF_SYSCALL) += bpf_dummy_struct_ops.o
5+
endif

net/bpf/bpf_dummy_struct_ops.c

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2021. Huawei Technologies Co., Ltd
4+
*/
5+
#include <linux/kernel.h>
6+
#include <linux/bpf_verifier.h>
7+
#include <linux/bpf.h>
8+
#include <linux/btf.h>
9+
10+
extern struct bpf_struct_ops bpf_bpf_dummy_ops;
11+
12+
/* A common type for test_N with return value in bpf_dummy_ops */
13+
typedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...);
14+
15+
struct bpf_dummy_ops_test_args {
16+
u64 args[MAX_BPF_FUNC_ARGS];
17+
struct bpf_dummy_ops_state state;
18+
};
19+
20+
static struct bpf_dummy_ops_test_args *
21+
dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
22+
{
23+
__u32 size_in;
24+
struct bpf_dummy_ops_test_args *args;
25+
void __user *ctx_in;
26+
void __user *u_state;
27+
28+
size_in = kattr->test.ctx_size_in;
29+
if (size_in != sizeof(u64) * nr)
30+
return ERR_PTR(-EINVAL);
31+
32+
args = kzalloc(sizeof(*args), GFP_KERNEL);
33+
if (!args)
34+
return ERR_PTR(-ENOMEM);
35+
36+
ctx_in = u64_to_user_ptr(kattr->test.ctx_in);
37+
if (copy_from_user(args->args, ctx_in, size_in))
38+
goto out;
39+
40+
/* args[0] is 0 means state argument of test_N will be NULL */
41+
u_state = u64_to_user_ptr(args->args[0]);
42+
if (u_state && copy_from_user(&args->state, u_state,
43+
sizeof(args->state)))
44+
goto out;
45+
46+
return args;
47+
out:
48+
kfree(args);
49+
return ERR_PTR(-EFAULT);
50+
}
51+
52+
static int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args)
53+
{
54+
void __user *u_state;
55+
56+
u_state = u64_to_user_ptr(args->args[0]);
57+
if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state)))
58+
return -EFAULT;
59+
60+
return 0;
61+
}
62+
63+
static int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args)
64+
{
65+
dummy_ops_test_ret_fn test = (void *)image;
66+
struct bpf_dummy_ops_state *state = NULL;
67+
68+
/* state needs to be NULL if args[0] is 0 */
69+
if (args->args[0])
70+
state = &args->state;
71+
return test(state, args->args[1], args->args[2],
72+
args->args[3], args->args[4]);
73+
}
74+
75+
int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
76+
union bpf_attr __user *uattr)
77+
{
78+
const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops;
79+
const struct btf_type *func_proto;
80+
struct bpf_dummy_ops_test_args *args;
81+
struct bpf_tramp_progs *tprogs;
82+
void *image = NULL;
83+
unsigned int op_idx;
84+
int prog_ret;
85+
int err;
86+
87+
if (prog->aux->attach_btf_id != st_ops->type_id)
88+
return -EOPNOTSUPP;
89+
90+
func_proto = prog->aux->attach_func_proto;
91+
args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto));
92+
if (IS_ERR(args))
93+
return PTR_ERR(args);
94+
95+
tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
96+
if (!tprogs) {
97+
err = -ENOMEM;
98+
goto out;
99+
}
100+
101+
image = bpf_jit_alloc_exec(PAGE_SIZE);
102+
if (!image) {
103+
err = -ENOMEM;
104+
goto out;
105+
}
106+
set_vm_flush_reset_perms(image);
107+
108+
op_idx = prog->expected_attach_type;
109+
err = bpf_struct_ops_prepare_trampoline(tprogs, prog,
110+
&st_ops->func_models[op_idx],
111+
image, image + PAGE_SIZE);
112+
if (err < 0)
113+
goto out;
114+
115+
set_memory_ro((long)image, 1);
116+
set_memory_x((long)image, 1);
117+
prog_ret = dummy_ops_call_op(image, args);
118+
119+
err = dummy_ops_copy_args(args);
120+
if (err)
121+
goto out;
122+
if (put_user(prog_ret, &uattr->test.retval))
123+
err = -EFAULT;
124+
out:
125+
kfree(args);
126+
bpf_jit_free_exec(image);
127+
kfree(tprogs);
128+
return err;
129+
}
130+
131+
static int bpf_dummy_init(struct btf *btf)
132+
{
133+
return 0;
134+
}
135+
136+
static bool bpf_dummy_ops_is_valid_access(int off, int size,
137+
enum bpf_access_type type,
138+
const struct bpf_prog *prog,
139+
struct bpf_insn_access_aux *info)
140+
{
141+
return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
142+
}
143+
144+
static int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log,
145+
const struct btf *btf,
146+
const struct btf_type *t, int off,
147+
int size, enum bpf_access_type atype,
148+
u32 *next_btf_id)
149+
{
150+
const struct btf_type *state;
151+
s32 type_id;
152+
int err;
153+
154+
type_id = btf_find_by_name_kind(btf, "bpf_dummy_ops_state",
155+
BTF_KIND_STRUCT);
156+
if (type_id < 0)
157+
return -EINVAL;
158+
159+
state = btf_type_by_id(btf, type_id);
160+
if (t != state) {
161+
bpf_log(log, "only access to bpf_dummy_ops_state is supported\n");
162+
return -EACCES;
163+
}
164+
165+
err = btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
166+
if (err < 0)
167+
return err;
168+
169+
return atype == BPF_READ ? err : NOT_INIT;
170+
}
171+
172+
static const struct bpf_verifier_ops bpf_dummy_verifier_ops = {
173+
.is_valid_access = bpf_dummy_ops_is_valid_access,
174+
.btf_struct_access = bpf_dummy_ops_btf_struct_access,
175+
};
176+
177+
static int bpf_dummy_init_member(const struct btf_type *t,
178+
const struct btf_member *member,
179+
void *kdata, const void *udata)
180+
{
181+
return -EOPNOTSUPP;
182+
}
183+
184+
static int bpf_dummy_reg(void *kdata)
185+
{
186+
return -EOPNOTSUPP;
187+
}
188+
189+
static void bpf_dummy_unreg(void *kdata)
190+
{
191+
}
192+
193+
struct bpf_struct_ops bpf_bpf_dummy_ops = {
194+
.verifier_ops = &bpf_dummy_verifier_ops,
195+
.init = bpf_dummy_init,
196+
.init_member = bpf_dummy_init_member,
197+
.reg = bpf_dummy_reg,
198+
.unreg = bpf_dummy_unreg,
199+
.name = "bpf_dummy_ops",
200+
};

0 commit comments

Comments
 (0)