Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: bpf: prevent infinite recursions with hid_hw_raw_requests hooks

When we attach a sleepable hook to hid_hw_raw_requests, we can (and in
many cases should) call ourself hid_bpf_raw_request(), to actually fetch
data from the device itself.

However, this means that we might enter an infinite loop between
hid_hw_raw_requests hooks and hid_bpf_hw_request() call.

To prevent that, if a hid_bpf_hw_request() call is emitted, we prevent
any new call of this kfunc by storing the information in the context.
This way we can always trace/monitor/filter the incoming bpf requests,
while preventing those loops to happen.

I don't think exposing "from_bpf" is very interesting because while
writing such a bpf program, you need to match at least the report number
and/or the source of the call. So a blind "if there is a
hid_hw_raw_request() call, I'm emitting another one" makes no real
sense.

Link: https://patch.msgid.link/20240626-hid_hw_req_bpf-v2-5-cfd60fb6c79f@kernel.org
Acked-by: Jiri Kosina <jkosina@suse.com>
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>

+20 -11
+10 -2
drivers/hid/bpf/hid_bpf_dispatch.c
··· 78 78 unsigned char reportnum, u8 *buf, 79 79 u32 size, enum hid_report_type rtype, 80 80 enum hid_class_request reqtype, 81 - u64 source) 81 + u64 source, bool from_bpf) 82 82 { 83 83 struct hid_bpf_ctx_kern ctx_kern = { 84 84 .ctx = { ··· 87 87 .size = size, 88 88 }, 89 89 .data = buf, 90 + .from_bpf = from_bpf, 90 91 }; 91 92 struct hid_bpf_ops *e; 92 93 int ret, idx; ··· 365 364 hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, 366 365 enum hid_report_type rtype, enum hid_class_request reqtype) 367 366 { 367 + struct hid_bpf_ctx_kern *ctx_kern; 368 368 struct hid_device *hdev; 369 369 size_t size = buf__sz; 370 370 u8 *dma_data; 371 371 int ret; 372 + 373 + ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx); 374 + 375 + if (ctx_kern->from_bpf) 376 + return -EDEADLOCK; 372 377 373 378 /* check arguments */ 374 379 ret = __hid_bpf_hw_check_params(ctx, buf, &size, rtype); ··· 405 398 size, 406 399 rtype, 407 400 reqtype, 408 - (__u64)ctx); 401 + (__u64)ctx, 402 + true); /* prevent infinite recursions */ 409 403 410 404 if (ret > 0) 411 405 memcpy(buf, dma_data, ret);
+1
drivers/hid/bpf/hid_bpf_dispatch.h
··· 8 8 struct hid_bpf_ctx_kern { 9 9 struct hid_bpf_ctx ctx; 10 10 u8 *data; 11 + bool from_bpf; 11 12 }; 12 13 13 14 struct hid_device *hid_get_device(unsigned int hid_id);
+3 -3
drivers/hid/hid-core.c
··· 2403 2403 unsigned char reportnum, __u8 *buf, 2404 2404 size_t len, enum hid_report_type rtype, 2405 2405 enum hid_class_request reqtype, 2406 - __u64 source) 2406 + __u64 source, bool from_bpf) 2407 2407 { 2408 2408 unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE; 2409 2409 int ret; ··· 2415 2415 return -EINVAL; 2416 2416 2417 2417 ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype, 2418 - reqtype, source); 2418 + reqtype, source, from_bpf); 2419 2419 if (ret) 2420 2420 return ret; 2421 2421 ··· 2441 2441 unsigned char reportnum, __u8 *buf, 2442 2442 size_t len, enum hid_report_type rtype, enum hid_class_request reqtype) 2443 2443 { 2444 - return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0); 2444 + return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0, false); 2445 2445 } 2446 2446 EXPORT_SYMBOL_GPL(hid_hw_raw_request); 2447 2447
+2 -2
drivers/hid/hidraw.c
··· 151 151 } 152 152 153 153 ret = __hid_hw_raw_request(dev, buf[0], buf, count, report_type, 154 - HID_REQ_SET_REPORT, (__u64)file); 154 + HID_REQ_SET_REPORT, (__u64)file, false); 155 155 156 156 out_free: 157 157 kfree(buf); ··· 228 228 } 229 229 230 230 ret = __hid_hw_raw_request(dev, report_number, buf, count, report_type, 231 - HID_REQ_GET_REPORT, (__u64)file); 231 + HID_REQ_GET_REPORT, (__u64)file, false); 232 232 233 233 if (ret < 0) 234 234 goto out_free;
+1 -1
include/linux/hid.h
··· 1129 1129 unsigned char reportnum, __u8 *buf, 1130 1130 size_t len, enum hid_report_type rtype, 1131 1131 enum hid_class_request reqtype, 1132 - __u64 source); 1132 + __u64 source, bool from_bpf); 1133 1133 int __hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len, __u64 source); 1134 1134 int hid_hw_raw_request(struct hid_device *hdev, 1135 1135 unsigned char reportnum, __u8 *buf,
+3 -3
include/linux/hid_bpf.h
··· 68 68 unsigned char reportnum, __u8 *buf, 69 69 size_t len, enum hid_report_type rtype, 70 70 enum hid_class_request reqtype, 71 - __u64 source); 71 + __u64 source, bool from_bpf); 72 72 int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len, 73 73 __u64 source); 74 74 int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type, ··· 181 181 unsigned char reportnum, __u8 *buf, 182 182 u32 size, enum hid_report_type rtype, 183 183 enum hid_class_request reqtype, 184 - __u64 source); 184 + __u64 source, bool from_bpf); 185 185 int hid_bpf_connect_device(struct hid_device *hdev); 186 186 void hid_bpf_disconnect_device(struct hid_device *hdev); 187 187 void hid_bpf_destroy_device(struct hid_device *hid); ··· 195 195 unsigned char reportnum, u8 *buf, 196 196 u32 size, enum hid_report_type rtype, 197 197 enum hid_class_request reqtype, 198 - u64 source) { return 0; } 198 + u64 source, bool from_bpf) { return 0; } 199 199 static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; } 200 200 static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {} 201 201 static inline void hid_bpf_destroy_device(struct hid_device *hid) {}