|
| 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