1 // SPDX-License-Identifier: GPL-2.0-only
4 * HID-BPF support for Linux
6 * Copyright (c) 2022 Benjamin Tissoires
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/bitops.h>
11 #include <linux/btf.h>
12 #include <linux/btf_ids.h>
13 #include <linux/filter.h>
14 #include <linux/hid.h>
15 #include <linux/hid_bpf.h>
16 #include <linux/init.h>
17 #include <linux/kfifo.h>
18 #include <linux/minmax.h>
19 #include <linux/module.h>
20 #include <linux/workqueue.h>
21 #include "hid_bpf_dispatch.h"
22 #include "entrypoints/entrypoints.lskel.h"
24 struct hid_bpf_ops *hid_bpf_ops;
25 EXPORT_SYMBOL(hid_bpf_ops);
28 * hid_bpf_device_event - Called whenever an event is coming in from the device
30 * @ctx: The HID-BPF context
32 * @return %0 on success and keep processing; a positive value to change the
33 * incoming size buffer; a negative error code to interrupt the processing
36 * Declare an %fmod_ret tracing bpf program to this function and attach this
37 * program through hid_bpf_attach_prog() to have this helper called for
38 * any incoming event from the device itself.
40 * The function is called while on IRQ context, so we can not sleep.
42 /* never used by the kernel but declared so we can load and attach a tracepoint */
43 __weak noinline int hid_bpf_device_event(struct hid_bpf_ctx *ctx)
49 dispatch_hid_bpf_device_event(struct hid_device *hdev, enum hid_report_type type, u8 *data,
50 u32 *size, int interrupt)
52 struct hid_bpf_ctx_kern ctx_kern = {
56 .allocated_size = hdev->bpf.allocated_data,
59 .data = hdev->bpf.device_data,
63 if (type >= HID_REPORT_TYPES)
64 return ERR_PTR(-EINVAL);
66 /* no program has been attached yet */
67 if (!hdev->bpf.device_data)
70 memset(ctx_kern.data, 0, hdev->bpf.allocated_data);
71 memcpy(ctx_kern.data, data, *size);
73 ret = hid_bpf_prog_run(hdev, HID_BPF_PROG_TYPE_DEVICE_EVENT, &ctx_kern);
78 if (ret > ctx_kern.ctx.allocated_size)
79 return ERR_PTR(-EINVAL);
86 EXPORT_SYMBOL_GPL(dispatch_hid_bpf_device_event);
89 * hid_bpf_rdesc_fixup - Called when the probe function parses the report
90 * descriptor of the HID device
92 * @ctx: The HID-BPF context
94 * @return 0 on success and keep processing; a positive value to change the
95 * incoming size buffer; a negative error code to interrupt the processing
98 * Declare an %fmod_ret tracing bpf program to this function and attach this
99 * program through hid_bpf_attach_prog() to have this helper called before any
100 * parsing of the report descriptor by HID.
102 /* never used by the kernel but declared so we can load and attach a tracepoint */
103 __weak noinline int hid_bpf_rdesc_fixup(struct hid_bpf_ctx *ctx)
108 u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size)
111 struct hid_bpf_ctx_kern ctx_kern = {
115 .allocated_size = HID_MAX_DESCRIPTOR_SIZE,
119 ctx_kern.data = kzalloc(ctx_kern.ctx.allocated_size, GFP_KERNEL);
123 memcpy(ctx_kern.data, rdesc, min_t(unsigned int, *size, HID_MAX_DESCRIPTOR_SIZE));
125 ret = hid_bpf_prog_run(hdev, HID_BPF_PROG_TYPE_RDESC_FIXUP, &ctx_kern);
130 if (ret > ctx_kern.ctx.allocated_size)
136 rdesc = krealloc(ctx_kern.data, *size, GFP_KERNEL);
141 kfree(ctx_kern.data);
142 return kmemdup(rdesc, *size, GFP_KERNEL);
144 EXPORT_SYMBOL_GPL(call_hid_bpf_rdesc_fixup);
147 * hid_bpf_get_data - Get the kernel memory pointer associated with the context @ctx
149 * @ctx: The HID-BPF context
150 * @offset: The offset within the memory
151 * @rdwr_buf_size: the const size of the buffer
153 * @returns %NULL on error, an %__u8 memory pointer on success
156 hid_bpf_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t rdwr_buf_size)
158 struct hid_bpf_ctx_kern *ctx_kern;
163 ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx);
165 if (rdwr_buf_size + offset > ctx->allocated_size)
168 return ctx_kern->data + offset;
172 * The following set contains all functions we agree BPF programs
175 BTF_SET8_START(hid_bpf_kfunc_ids)
176 BTF_ID_FLAGS(func, hid_bpf_get_data, KF_RET_NULL)
177 BTF_SET8_END(hid_bpf_kfunc_ids)
179 static const struct btf_kfunc_id_set hid_bpf_kfunc_set = {
180 .owner = THIS_MODULE,
181 .set = &hid_bpf_kfunc_ids,
184 static int device_match_id(struct device *dev, const void *id)
186 struct hid_device *hdev = to_hid_device(dev);
188 return hdev->id == *(int *)id;
191 static int __hid_bpf_allocate_data(struct hid_device *hdev, u8 **data, u32 *size)
194 unsigned int i, j, max_report_len = 0;
195 size_t alloc_size = 0;
197 /* compute the maximum report length for this device */
198 for (i = 0; i < HID_REPORT_TYPES; i++) {
199 struct hid_report_enum *report_enum = hdev->report_enum + i;
201 for (j = 0; j < HID_MAX_IDS; j++) {
202 struct hid_report *report = report_enum->report_id_hash[j];
205 max_report_len = max(max_report_len, hid_report_len(report));
210 * Give us a little bit of extra space and some predictability in the
211 * buffer length we create. This way, we can tell users that they can
212 * work on chunks of 64 bytes of memory without having the bpf verifier
215 alloc_size = DIV_ROUND_UP(max_report_len, 64) * 64;
217 alloc_data = kzalloc(alloc_size, GFP_KERNEL);
227 static int hid_bpf_allocate_event_data(struct hid_device *hdev)
229 /* hdev->bpf.device_data is already allocated, abort */
230 if (hdev->bpf.device_data)
233 return __hid_bpf_allocate_data(hdev, &hdev->bpf.device_data, &hdev->bpf.allocated_data);
236 int hid_bpf_reconnect(struct hid_device *hdev)
238 if (!test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
239 return device_reprobe(&hdev->dev);
245 * hid_bpf_attach_prog - Attach the given @prog_fd to the given HID device
247 * @hid_id: the system unique identifier of the HID device
248 * @prog_fd: an fd in the user process representing the program to attach
249 * @flags: any logical OR combination of &enum hid_bpf_attach_flags
251 * @returns an fd of a bpf_link object on success (> %0), an error code otherwise.
252 * Closing this fd will detach the program from the HID device (unless the bpf_link
253 * is pinned to the BPF file system).
255 /* called from syscall */
257 hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, __u32 flags)
259 struct hid_device *hdev;
261 int fd, err, prog_type = hid_bpf_get_prog_attach_type(prog_fd);
269 if (prog_type >= HID_BPF_PROG_TYPE_MAX)
272 if ((flags & ~HID_BPF_FLAG_MASK))
275 dev = bus_find_device(hid_bpf_ops->bus_type, NULL, &hid_id, device_match_id);
279 hdev = to_hid_device(dev);
281 if (prog_type == HID_BPF_PROG_TYPE_DEVICE_EVENT) {
282 err = hid_bpf_allocate_event_data(hdev);
287 fd = __hid_bpf_attach_prog(hdev, prog_type, prog_fd, flags);
291 if (prog_type == HID_BPF_PROG_TYPE_RDESC_FIXUP) {
292 err = hid_bpf_reconnect(hdev);
303 * hid_bpf_allocate_context - Allocate a context to the given HID device
305 * @hid_id: the system unique identifier of the HID device
307 * @returns A pointer to &struct hid_bpf_ctx on success, %NULL on error.
309 noinline struct hid_bpf_ctx *
310 hid_bpf_allocate_context(unsigned int hid_id)
312 struct hid_device *hdev;
313 struct hid_bpf_ctx_kern *ctx_kern = NULL;
319 dev = bus_find_device(hid_bpf_ops->bus_type, NULL, &hid_id, device_match_id);
323 hdev = to_hid_device(dev);
325 ctx_kern = kzalloc(sizeof(*ctx_kern), GFP_KERNEL);
329 ctx_kern->ctx.hid = hdev;
331 return &ctx_kern->ctx;
335 * hid_bpf_release_context - Release the previously allocated context @ctx
337 * @ctx: the HID-BPF context to release
341 hid_bpf_release_context(struct hid_bpf_ctx *ctx)
343 struct hid_bpf_ctx_kern *ctx_kern;
345 ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx);
351 * hid_bpf_hw_request - Communicate with a HID device
353 * @ctx: the HID-BPF context previously allocated in hid_bpf_allocate_context()
354 * @buf: a %PTR_TO_MEM buffer
355 * @buf__sz: the size of the data to transfer
356 * @rtype: the type of the report (%HID_INPUT_REPORT, %HID_FEATURE_REPORT, %HID_OUTPUT_REPORT)
357 * @reqtype: the type of the request (%HID_REQ_GET_REPORT, %HID_REQ_SET_REPORT, ...)
359 * @returns %0 on success, a negative error code otherwise.
362 hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz,
363 enum hid_report_type rtype, enum hid_class_request reqtype)
365 struct hid_device *hdev;
366 struct hid_report *report;
367 struct hid_report_enum *report_enum;
372 /* check arguments */
373 if (!ctx || !hid_bpf_ops || !buf)
377 case HID_INPUT_REPORT:
378 case HID_OUTPUT_REPORT:
379 case HID_FEATURE_REPORT:
386 case HID_REQ_GET_REPORT:
387 case HID_REQ_GET_IDLE:
388 case HID_REQ_GET_PROTOCOL:
389 case HID_REQ_SET_REPORT:
390 case HID_REQ_SET_IDLE:
391 case HID_REQ_SET_PROTOCOL:
400 hdev = (struct hid_device *)ctx->hid; /* discard const */
402 report_enum = hdev->report_enum + rtype;
403 report = hid_bpf_ops->hid_get_report(report_enum, buf);
407 report_len = hid_report_len(report);
409 if (buf__sz > report_len)
410 buf__sz = report_len;
412 dma_data = kmemdup(buf, buf__sz, GFP_KERNEL);
416 ret = hid_bpf_ops->hid_hw_raw_request(hdev,
424 memcpy(buf, dma_data, ret);
430 /* our HID-BPF entrypoints */
431 BTF_SET8_START(hid_bpf_fmodret_ids)
432 BTF_ID_FLAGS(func, hid_bpf_device_event)
433 BTF_ID_FLAGS(func, hid_bpf_rdesc_fixup)
434 BTF_ID_FLAGS(func, __hid_bpf_tail_call)
435 BTF_SET8_END(hid_bpf_fmodret_ids)
437 static const struct btf_kfunc_id_set hid_bpf_fmodret_set = {
438 .owner = THIS_MODULE,
439 .set = &hid_bpf_fmodret_ids,
442 /* for syscall HID-BPF */
443 BTF_SET8_START(hid_bpf_syscall_kfunc_ids)
444 BTF_ID_FLAGS(func, hid_bpf_attach_prog)
445 BTF_ID_FLAGS(func, hid_bpf_allocate_context, KF_ACQUIRE | KF_RET_NULL)
446 BTF_ID_FLAGS(func, hid_bpf_release_context, KF_RELEASE)
447 BTF_ID_FLAGS(func, hid_bpf_hw_request)
448 BTF_SET8_END(hid_bpf_syscall_kfunc_ids)
450 static const struct btf_kfunc_id_set hid_bpf_syscall_kfunc_set = {
451 .owner = THIS_MODULE,
452 .set = &hid_bpf_syscall_kfunc_ids,
455 int hid_bpf_connect_device(struct hid_device *hdev)
457 struct hid_bpf_prog_list *prog_list;
460 prog_list = rcu_dereference(hdev->bpf.progs[HID_BPF_PROG_TYPE_DEVICE_EVENT]);
463 /* only allocate BPF data if there are programs attached */
467 return hid_bpf_allocate_event_data(hdev);
469 EXPORT_SYMBOL_GPL(hid_bpf_connect_device);
471 void hid_bpf_disconnect_device(struct hid_device *hdev)
473 kfree(hdev->bpf.device_data);
474 hdev->bpf.device_data = NULL;
475 hdev->bpf.allocated_data = 0;
477 EXPORT_SYMBOL_GPL(hid_bpf_disconnect_device);
479 void hid_bpf_destroy_device(struct hid_device *hdev)
484 /* mark the device as destroyed in bpf so we don't reattach it */
485 hdev->bpf.destroyed = true;
487 __hid_bpf_destroy_device(hdev);
489 EXPORT_SYMBOL_GPL(hid_bpf_destroy_device);
491 void hid_bpf_device_init(struct hid_device *hdev)
493 spin_lock_init(&hdev->bpf.progs_lock);
495 EXPORT_SYMBOL_GPL(hid_bpf_device_init);
497 static int __init hid_bpf_init(void)
501 /* Note: if we exit with an error any time here, we would entirely break HID, which
502 * is probably not something we want. So we log an error and return success.
504 * This is not a big deal: the syscall allowing to attach a BPF program to a HID device
505 * will not be available, so nobody will be able to use the functionality.
508 err = register_btf_fmodret_id_set(&hid_bpf_fmodret_set);
510 pr_warn("error while registering fmodret entrypoints: %d", err);
514 err = hid_bpf_preload_skel();
516 pr_warn("error while preloading HID BPF dispatcher: %d", err);
520 /* register tracing kfuncs after we are sure we can load our preloaded bpf program */
521 err = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &hid_bpf_kfunc_set);
523 pr_warn("error while setting HID BPF tracing kfuncs: %d", err);
527 /* register syscalls after we are sure we can load our preloaded bpf program */
528 err = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &hid_bpf_syscall_kfunc_set);
530 pr_warn("error while setting HID BPF syscall kfuncs: %d", err);
537 static void __exit hid_bpf_exit(void)
539 /* HID depends on us, so if we hit that code, we are guaranteed that hid
540 * has been removed and thus we do not need to clear the HID devices
542 hid_bpf_free_links_and_skel();
545 late_initcall(hid_bpf_init);
546 module_exit(hid_bpf_exit);
547 MODULE_AUTHOR("Benjamin Tissoires");
548 MODULE_LICENSE("GPL");