162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2021. Huawei Technologies Co., Ltd 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci#include <linux/kernel.h> 662306a36Sopenharmony_ci#include <linux/bpf_verifier.h> 762306a36Sopenharmony_ci#include <linux/bpf.h> 862306a36Sopenharmony_ci#include <linux/btf.h> 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciextern struct bpf_struct_ops bpf_bpf_dummy_ops; 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci/* A common type for test_N with return value in bpf_dummy_ops */ 1362306a36Sopenharmony_citypedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...); 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_cistruct bpf_dummy_ops_test_args { 1662306a36Sopenharmony_ci u64 args[MAX_BPF_FUNC_ARGS]; 1762306a36Sopenharmony_ci struct bpf_dummy_ops_state state; 1862306a36Sopenharmony_ci}; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cistatic struct bpf_dummy_ops_test_args * 2162306a36Sopenharmony_cidummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr) 2262306a36Sopenharmony_ci{ 2362306a36Sopenharmony_ci __u32 size_in; 2462306a36Sopenharmony_ci struct bpf_dummy_ops_test_args *args; 2562306a36Sopenharmony_ci void __user *ctx_in; 2662306a36Sopenharmony_ci void __user *u_state; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci size_in = kattr->test.ctx_size_in; 2962306a36Sopenharmony_ci if (size_in != sizeof(u64) * nr) 3062306a36Sopenharmony_ci return ERR_PTR(-EINVAL); 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci args = kzalloc(sizeof(*args), GFP_KERNEL); 3362306a36Sopenharmony_ci if (!args) 3462306a36Sopenharmony_ci return ERR_PTR(-ENOMEM); 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci ctx_in = u64_to_user_ptr(kattr->test.ctx_in); 3762306a36Sopenharmony_ci if (copy_from_user(args->args, ctx_in, size_in)) 3862306a36Sopenharmony_ci goto out; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci /* args[0] is 0 means state argument of test_N will be NULL */ 4162306a36Sopenharmony_ci u_state = u64_to_user_ptr(args->args[0]); 4262306a36Sopenharmony_ci if (u_state && copy_from_user(&args->state, u_state, 4362306a36Sopenharmony_ci sizeof(args->state))) 4462306a36Sopenharmony_ci goto out; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci return args; 4762306a36Sopenharmony_ciout: 4862306a36Sopenharmony_ci kfree(args); 4962306a36Sopenharmony_ci return ERR_PTR(-EFAULT); 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_cistatic int dummy_ops_copy_args(struct bpf_dummy_ops_test_args *args) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci void __user *u_state; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci u_state = u64_to_user_ptr(args->args[0]); 5762306a36Sopenharmony_ci if (u_state && copy_to_user(u_state, &args->state, sizeof(args->state))) 5862306a36Sopenharmony_ci return -EFAULT; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci return 0; 6162306a36Sopenharmony_ci} 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_cistatic int dummy_ops_call_op(void *image, struct bpf_dummy_ops_test_args *args) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci dummy_ops_test_ret_fn test = (void *)image; 6662306a36Sopenharmony_ci struct bpf_dummy_ops_state *state = NULL; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci /* state needs to be NULL if args[0] is 0 */ 6962306a36Sopenharmony_ci if (args->args[0]) 7062306a36Sopenharmony_ci state = &args->state; 7162306a36Sopenharmony_ci return test(state, args->args[1], args->args[2], 7262306a36Sopenharmony_ci args->args[3], args->args[4]); 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ciextern const struct bpf_link_ops bpf_struct_ops_link_lops; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ciint bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr, 7862306a36Sopenharmony_ci union bpf_attr __user *uattr) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci const struct bpf_struct_ops *st_ops = &bpf_bpf_dummy_ops; 8162306a36Sopenharmony_ci const struct btf_type *func_proto; 8262306a36Sopenharmony_ci struct bpf_dummy_ops_test_args *args; 8362306a36Sopenharmony_ci struct bpf_tramp_links *tlinks; 8462306a36Sopenharmony_ci struct bpf_tramp_link *link = NULL; 8562306a36Sopenharmony_ci void *image = NULL; 8662306a36Sopenharmony_ci unsigned int op_idx; 8762306a36Sopenharmony_ci int prog_ret; 8862306a36Sopenharmony_ci int err; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci if (prog->aux->attach_btf_id != st_ops->type_id) 9162306a36Sopenharmony_ci return -EOPNOTSUPP; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci func_proto = prog->aux->attach_func_proto; 9462306a36Sopenharmony_ci args = dummy_ops_init_args(kattr, btf_type_vlen(func_proto)); 9562306a36Sopenharmony_ci if (IS_ERR(args)) 9662306a36Sopenharmony_ci return PTR_ERR(args); 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL); 9962306a36Sopenharmony_ci if (!tlinks) { 10062306a36Sopenharmony_ci err = -ENOMEM; 10162306a36Sopenharmony_ci goto out; 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci image = bpf_jit_alloc_exec(PAGE_SIZE); 10562306a36Sopenharmony_ci if (!image) { 10662306a36Sopenharmony_ci err = -ENOMEM; 10762306a36Sopenharmony_ci goto out; 10862306a36Sopenharmony_ci } 10962306a36Sopenharmony_ci set_vm_flush_reset_perms(image); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci link = kzalloc(sizeof(*link), GFP_USER); 11262306a36Sopenharmony_ci if (!link) { 11362306a36Sopenharmony_ci err = -ENOMEM; 11462306a36Sopenharmony_ci goto out; 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci /* prog doesn't take the ownership of the reference from caller */ 11762306a36Sopenharmony_ci bpf_prog_inc(prog); 11862306a36Sopenharmony_ci bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_link_lops, prog); 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci op_idx = prog->expected_attach_type; 12162306a36Sopenharmony_ci err = bpf_struct_ops_prepare_trampoline(tlinks, link, 12262306a36Sopenharmony_ci &st_ops->func_models[op_idx], 12362306a36Sopenharmony_ci image, image + PAGE_SIZE); 12462306a36Sopenharmony_ci if (err < 0) 12562306a36Sopenharmony_ci goto out; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci set_memory_rox((long)image, 1); 12862306a36Sopenharmony_ci prog_ret = dummy_ops_call_op(image, args); 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci err = dummy_ops_copy_args(args); 13162306a36Sopenharmony_ci if (err) 13262306a36Sopenharmony_ci goto out; 13362306a36Sopenharmony_ci if (put_user(prog_ret, &uattr->test.retval)) 13462306a36Sopenharmony_ci err = -EFAULT; 13562306a36Sopenharmony_ciout: 13662306a36Sopenharmony_ci kfree(args); 13762306a36Sopenharmony_ci bpf_jit_free_exec(image); 13862306a36Sopenharmony_ci if (link) 13962306a36Sopenharmony_ci bpf_link_put(&link->link); 14062306a36Sopenharmony_ci kfree(tlinks); 14162306a36Sopenharmony_ci return err; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_cistatic int bpf_dummy_init(struct btf *btf) 14562306a36Sopenharmony_ci{ 14662306a36Sopenharmony_ci return 0; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic bool bpf_dummy_ops_is_valid_access(int off, int size, 15062306a36Sopenharmony_ci enum bpf_access_type type, 15162306a36Sopenharmony_ci const struct bpf_prog *prog, 15262306a36Sopenharmony_ci struct bpf_insn_access_aux *info) 15362306a36Sopenharmony_ci{ 15462306a36Sopenharmony_ci return bpf_tracing_btf_ctx_access(off, size, type, prog, info); 15562306a36Sopenharmony_ci} 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_cistatic int bpf_dummy_ops_check_member(const struct btf_type *t, 15862306a36Sopenharmony_ci const struct btf_member *member, 15962306a36Sopenharmony_ci const struct bpf_prog *prog) 16062306a36Sopenharmony_ci{ 16162306a36Sopenharmony_ci u32 moff = __btf_member_bit_offset(t, member) / 8; 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci switch (moff) { 16462306a36Sopenharmony_ci case offsetof(struct bpf_dummy_ops, test_sleepable): 16562306a36Sopenharmony_ci break; 16662306a36Sopenharmony_ci default: 16762306a36Sopenharmony_ci if (prog->aux->sleepable) 16862306a36Sopenharmony_ci return -EINVAL; 16962306a36Sopenharmony_ci } 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci return 0; 17262306a36Sopenharmony_ci} 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_cistatic int bpf_dummy_ops_btf_struct_access(struct bpf_verifier_log *log, 17562306a36Sopenharmony_ci const struct bpf_reg_state *reg, 17662306a36Sopenharmony_ci int off, int size) 17762306a36Sopenharmony_ci{ 17862306a36Sopenharmony_ci const struct btf_type *state; 17962306a36Sopenharmony_ci const struct btf_type *t; 18062306a36Sopenharmony_ci s32 type_id; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci type_id = btf_find_by_name_kind(reg->btf, "bpf_dummy_ops_state", 18362306a36Sopenharmony_ci BTF_KIND_STRUCT); 18462306a36Sopenharmony_ci if (type_id < 0) 18562306a36Sopenharmony_ci return -EINVAL; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci t = btf_type_by_id(reg->btf, reg->btf_id); 18862306a36Sopenharmony_ci state = btf_type_by_id(reg->btf, type_id); 18962306a36Sopenharmony_ci if (t != state) { 19062306a36Sopenharmony_ci bpf_log(log, "only access to bpf_dummy_ops_state is supported\n"); 19162306a36Sopenharmony_ci return -EACCES; 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci if (off + size > sizeof(struct bpf_dummy_ops_state)) { 19562306a36Sopenharmony_ci bpf_log(log, "write access at off %d with size %d\n", off, size); 19662306a36Sopenharmony_ci return -EACCES; 19762306a36Sopenharmony_ci } 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci return NOT_INIT; 20062306a36Sopenharmony_ci} 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_cistatic const struct bpf_verifier_ops bpf_dummy_verifier_ops = { 20362306a36Sopenharmony_ci .is_valid_access = bpf_dummy_ops_is_valid_access, 20462306a36Sopenharmony_ci .btf_struct_access = bpf_dummy_ops_btf_struct_access, 20562306a36Sopenharmony_ci}; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_cistatic int bpf_dummy_init_member(const struct btf_type *t, 20862306a36Sopenharmony_ci const struct btf_member *member, 20962306a36Sopenharmony_ci void *kdata, const void *udata) 21062306a36Sopenharmony_ci{ 21162306a36Sopenharmony_ci return -EOPNOTSUPP; 21262306a36Sopenharmony_ci} 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cistatic int bpf_dummy_reg(void *kdata) 21562306a36Sopenharmony_ci{ 21662306a36Sopenharmony_ci return -EOPNOTSUPP; 21762306a36Sopenharmony_ci} 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_cistatic void bpf_dummy_unreg(void *kdata) 22062306a36Sopenharmony_ci{ 22162306a36Sopenharmony_ci} 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_cistruct bpf_struct_ops bpf_bpf_dummy_ops = { 22462306a36Sopenharmony_ci .verifier_ops = &bpf_dummy_verifier_ops, 22562306a36Sopenharmony_ci .init = bpf_dummy_init, 22662306a36Sopenharmony_ci .check_member = bpf_dummy_ops_check_member, 22762306a36Sopenharmony_ci .init_member = bpf_dummy_init_member, 22862306a36Sopenharmony_ci .reg = bpf_dummy_reg, 22962306a36Sopenharmony_ci .unreg = bpf_dummy_unreg, 23062306a36Sopenharmony_ci .name = "bpf_dummy_ops", 23162306a36Sopenharmony_ci}; 232