hid_bpf_struct_ops.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HID-BPF support for Linux
  4. *
  5. * Copyright (c) 2024 Benjamin Tissoires
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bpf_verifier.h>
  9. #include <linux/bpf.h>
  10. #include <linux/btf.h>
  11. #include <linux/btf_ids.h>
  12. #include <linux/filter.h>
  13. #include <linux/hid.h>
  14. #include <linux/hid_bpf.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/stddef.h>
  18. #include <linux/workqueue.h>
  19. #include "hid_bpf_dispatch.h"
  20. static struct btf *hid_bpf_ops_btf;
  21. static int hid_bpf_ops_init(struct btf *btf)
  22. {
  23. hid_bpf_ops_btf = btf;
  24. return 0;
  25. }
  26. static bool hid_bpf_ops_is_valid_access(int off, int size,
  27. enum bpf_access_type type,
  28. const struct bpf_prog *prog,
  29. struct bpf_insn_access_aux *info)
  30. {
  31. return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
  32. }
  33. static int hid_bpf_ops_check_member(const struct btf_type *t,
  34. const struct btf_member *member,
  35. const struct bpf_prog *prog)
  36. {
  37. u32 moff = __btf_member_bit_offset(t, member) / 8;
  38. switch (moff) {
  39. case offsetof(struct hid_bpf_ops, hid_rdesc_fixup):
  40. case offsetof(struct hid_bpf_ops, hid_hw_request):
  41. case offsetof(struct hid_bpf_ops, hid_hw_output_report):
  42. break;
  43. default:
  44. if (prog->sleepable)
  45. return -EINVAL;
  46. }
  47. return 0;
  48. }
  49. struct hid_bpf_offset_write_range {
  50. const char *struct_name;
  51. u32 struct_length;
  52. u32 start;
  53. u32 end;
  54. };
  55. static int hid_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
  56. const struct bpf_reg_state *reg,
  57. int off, int size)
  58. {
  59. #define WRITE_RANGE(_name, _field, _is_string) \
  60. { \
  61. .struct_name = #_name, \
  62. .struct_length = sizeof(struct _name), \
  63. .start = offsetof(struct _name, _field), \
  64. .end = offsetofend(struct _name, _field) - !!(_is_string), \
  65. }
  66. const struct hid_bpf_offset_write_range write_ranges[] = {
  67. WRITE_RANGE(hid_bpf_ctx, retval, false),
  68. WRITE_RANGE(hid_device, name, true),
  69. WRITE_RANGE(hid_device, uniq, true),
  70. WRITE_RANGE(hid_device, phys, true),
  71. };
  72. #undef WRITE_RANGE
  73. const struct btf_type *state = NULL;
  74. const struct btf_type *t;
  75. const char *cur = NULL;
  76. int i;
  77. t = btf_type_by_id(reg->btf, reg->btf_id);
  78. for (i = 0; i < ARRAY_SIZE(write_ranges); i++) {
  79. const struct hid_bpf_offset_write_range *write_range = &write_ranges[i];
  80. s32 type_id;
  81. /* we already found a writeable struct, but there is a
  82. * new one, let's break the loop.
  83. */
  84. if (t == state && write_range->struct_name != cur)
  85. break;
  86. /* new struct to look for */
  87. if (write_range->struct_name != cur) {
  88. type_id = btf_find_by_name_kind(reg->btf, write_range->struct_name,
  89. BTF_KIND_STRUCT);
  90. if (type_id < 0)
  91. return -EINVAL;
  92. state = btf_type_by_id(reg->btf, type_id);
  93. }
  94. /* this is not the struct we are looking for */
  95. if (t != state) {
  96. cur = write_range->struct_name;
  97. continue;
  98. }
  99. /* first time we see this struct, check for out of bounds */
  100. if (cur != write_range->struct_name &&
  101. off + size > write_range->struct_length) {
  102. bpf_log(log, "write access for struct %s at off %d with size %d\n",
  103. write_range->struct_name, off, size);
  104. return -EACCES;
  105. }
  106. /* now check if we are in our boundaries */
  107. if (off >= write_range->start && off + size <= write_range->end)
  108. return NOT_INIT;
  109. cur = write_range->struct_name;
  110. }
  111. if (t != state)
  112. bpf_log(log, "write access to this struct is not supported\n");
  113. else
  114. bpf_log(log,
  115. "write access at off %d with size %d on read-only part of %s\n",
  116. off, size, cur);
  117. return -EACCES;
  118. }
  119. static const struct bpf_verifier_ops hid_bpf_verifier_ops = {
  120. .get_func_proto = bpf_base_func_proto,
  121. .is_valid_access = hid_bpf_ops_is_valid_access,
  122. .btf_struct_access = hid_bpf_ops_btf_struct_access,
  123. };
  124. static int hid_bpf_ops_init_member(const struct btf_type *t,
  125. const struct btf_member *member,
  126. void *kdata, const void *udata)
  127. {
  128. const struct hid_bpf_ops *uhid_bpf_ops;
  129. struct hid_bpf_ops *khid_bpf_ops;
  130. u32 moff;
  131. uhid_bpf_ops = (const struct hid_bpf_ops *)udata;
  132. khid_bpf_ops = (struct hid_bpf_ops *)kdata;
  133. moff = __btf_member_bit_offset(t, member) / 8;
  134. switch (moff) {
  135. case offsetof(struct hid_bpf_ops, hid_id):
  136. /* For hid_id and flags fields, this function has to copy it
  137. * and return 1 to indicate that the data has been handled by
  138. * the struct_ops type, or the verifier will reject the map if
  139. * the value of those fields is not zero.
  140. */
  141. khid_bpf_ops->hid_id = uhid_bpf_ops->hid_id;
  142. return 1;
  143. case offsetof(struct hid_bpf_ops, flags):
  144. if (uhid_bpf_ops->flags & ~BPF_F_BEFORE)
  145. return -EINVAL;
  146. khid_bpf_ops->flags = uhid_bpf_ops->flags;
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. static int hid_bpf_reg(void *kdata, struct bpf_link *link)
  152. {
  153. struct hid_bpf_ops *ops = kdata;
  154. struct hid_device *hdev;
  155. int count, err = 0;
  156. /* prevent multiple attach of the same struct_ops */
  157. if (ops->hdev)
  158. return -EINVAL;
  159. hdev = hid_get_device(ops->hid_id);
  160. if (IS_ERR(hdev))
  161. return PTR_ERR(hdev);
  162. ops->hdev = hdev;
  163. mutex_lock(&hdev->bpf.prog_list_lock);
  164. count = list_count_nodes(&hdev->bpf.prog_list);
  165. if (count >= HID_BPF_MAX_PROGS_PER_DEV) {
  166. err = -E2BIG;
  167. goto out_unlock;
  168. }
  169. if (ops->hid_rdesc_fixup) {
  170. if (hdev->bpf.rdesc_ops) {
  171. err = -EINVAL;
  172. goto out_unlock;
  173. }
  174. hdev->bpf.rdesc_ops = ops;
  175. }
  176. if (ops->hid_device_event) {
  177. err = hid_bpf_allocate_event_data(hdev);
  178. if (err)
  179. goto out_unlock;
  180. }
  181. if (ops->flags & BPF_F_BEFORE)
  182. list_add_rcu(&ops->list, &hdev->bpf.prog_list);
  183. else
  184. list_add_tail_rcu(&ops->list, &hdev->bpf.prog_list);
  185. synchronize_srcu(&hdev->bpf.srcu);
  186. out_unlock:
  187. mutex_unlock(&hdev->bpf.prog_list_lock);
  188. if (err) {
  189. if (hdev->bpf.rdesc_ops == ops)
  190. hdev->bpf.rdesc_ops = NULL;
  191. hid_put_device(hdev);
  192. } else if (ops->hid_rdesc_fixup) {
  193. hid_bpf_reconnect(hdev);
  194. }
  195. return err;
  196. }
  197. static void hid_bpf_unreg(void *kdata, struct bpf_link *link)
  198. {
  199. struct hid_bpf_ops *ops = kdata;
  200. struct hid_device *hdev;
  201. bool reconnect = false;
  202. hdev = ops->hdev;
  203. /* check if __hid_bpf_ops_destroy_device() has been called */
  204. if (!hdev)
  205. return;
  206. mutex_lock(&hdev->bpf.prog_list_lock);
  207. list_del_rcu(&ops->list);
  208. synchronize_srcu(&hdev->bpf.srcu);
  209. ops->hdev = NULL;
  210. reconnect = hdev->bpf.rdesc_ops == ops;
  211. if (reconnect)
  212. hdev->bpf.rdesc_ops = NULL;
  213. mutex_unlock(&hdev->bpf.prog_list_lock);
  214. if (reconnect)
  215. hid_bpf_reconnect(hdev);
  216. hid_put_device(hdev);
  217. }
  218. static int __hid_bpf_device_event(struct hid_bpf_ctx *ctx, enum hid_report_type type, u64 source)
  219. {
  220. return 0;
  221. }
  222. static int __hid_bpf_rdesc_fixup(struct hid_bpf_ctx *ctx)
  223. {
  224. return 0;
  225. }
  226. static int __hid_bpf_hw_request(struct hid_bpf_ctx *ctx, unsigned char reportnum,
  227. enum hid_report_type rtype, enum hid_class_request reqtype,
  228. u64 source)
  229. {
  230. return 0;
  231. }
  232. static int __hid_bpf_hw_output_report(struct hid_bpf_ctx *ctx, u64 source)
  233. {
  234. return 0;
  235. }
  236. static struct hid_bpf_ops __bpf_hid_bpf_ops = {
  237. .hid_device_event = __hid_bpf_device_event,
  238. .hid_rdesc_fixup = __hid_bpf_rdesc_fixup,
  239. .hid_hw_request = __hid_bpf_hw_request,
  240. .hid_hw_output_report = __hid_bpf_hw_output_report,
  241. };
  242. static struct bpf_struct_ops bpf_hid_bpf_ops = {
  243. .verifier_ops = &hid_bpf_verifier_ops,
  244. .init = hid_bpf_ops_init,
  245. .check_member = hid_bpf_ops_check_member,
  246. .init_member = hid_bpf_ops_init_member,
  247. .reg = hid_bpf_reg,
  248. .unreg = hid_bpf_unreg,
  249. .name = "hid_bpf_ops",
  250. .cfi_stubs = &__bpf_hid_bpf_ops,
  251. .owner = THIS_MODULE,
  252. };
  253. void __hid_bpf_ops_destroy_device(struct hid_device *hdev)
  254. {
  255. struct hid_bpf_ops *e;
  256. rcu_read_lock();
  257. list_for_each_entry_rcu(e, &hdev->bpf.prog_list, list) {
  258. hid_put_device(hdev);
  259. e->hdev = NULL;
  260. }
  261. rcu_read_unlock();
  262. }
  263. static int __init hid_bpf_struct_ops_init(void)
  264. {
  265. return register_bpf_struct_ops(&bpf_hid_bpf_ops, hid_bpf_ops);
  266. }
  267. late_initcall(hid_bpf_struct_ops_init);