bpf_dummy_struct_ops.c 5.2 KB

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