Commit 75839101 authored by Benjamin Tissoires's avatar Benjamin Tissoires

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.orgAcked-by: default avatarJiri Kosina <jkosina@suse.com>
Signed-off-by: default avatarBenjamin Tissoires <bentiss@kernel.org>
parent 8bd0488b
...@@ -78,7 +78,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev, ...@@ -78,7 +78,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
unsigned char reportnum, u8 *buf, unsigned char reportnum, u8 *buf,
u32 size, enum hid_report_type rtype, u32 size, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
u64 source) u64 source, bool from_bpf)
{ {
struct hid_bpf_ctx_kern ctx_kern = { struct hid_bpf_ctx_kern ctx_kern = {
.ctx = { .ctx = {
...@@ -87,6 +87,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev, ...@@ -87,6 +87,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
.size = size, .size = size,
}, },
.data = buf, .data = buf,
.from_bpf = from_bpf,
}; };
struct hid_bpf_ops *e; struct hid_bpf_ops *e;
int ret, idx; int ret, idx;
...@@ -364,11 +365,17 @@ __bpf_kfunc int ...@@ -364,11 +365,17 @@ __bpf_kfunc int
hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz,
enum hid_report_type rtype, enum hid_class_request reqtype) enum hid_report_type rtype, enum hid_class_request reqtype)
{ {
struct hid_bpf_ctx_kern *ctx_kern;
struct hid_device *hdev; struct hid_device *hdev;
size_t size = buf__sz; size_t size = buf__sz;
u8 *dma_data; u8 *dma_data;
int ret; int ret;
ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx);
if (ctx_kern->from_bpf)
return -EDEADLOCK;
/* check arguments */ /* check arguments */
ret = __hid_bpf_hw_check_params(ctx, buf, &size, rtype); ret = __hid_bpf_hw_check_params(ctx, buf, &size, rtype);
if (ret) if (ret)
...@@ -398,7 +405,8 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz, ...@@ -398,7 +405,8 @@ hid_bpf_hw_request(struct hid_bpf_ctx *ctx, __u8 *buf, size_t buf__sz,
size, size,
rtype, rtype,
reqtype, reqtype,
(__u64)ctx); (__u64)ctx,
true); /* prevent infinite recursions */
if (ret > 0) if (ret > 0)
memcpy(buf, dma_data, ret); memcpy(buf, dma_data, ret);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
struct hid_bpf_ctx_kern { struct hid_bpf_ctx_kern {
struct hid_bpf_ctx ctx; struct hid_bpf_ctx ctx;
u8 *data; u8 *data;
bool from_bpf;
}; };
struct hid_device *hid_get_device(unsigned int hid_id); struct hid_device *hid_get_device(unsigned int hid_id);
......
...@@ -2403,7 +2403,7 @@ int __hid_hw_raw_request(struct hid_device *hdev, ...@@ -2403,7 +2403,7 @@ int __hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
size_t len, enum hid_report_type rtype, size_t len, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
__u64 source) __u64 source, bool from_bpf)
{ {
unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE; unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
int ret; int ret;
...@@ -2415,7 +2415,7 @@ int __hid_hw_raw_request(struct hid_device *hdev, ...@@ -2415,7 +2415,7 @@ int __hid_hw_raw_request(struct hid_device *hdev,
return -EINVAL; return -EINVAL;
ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype, ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype,
reqtype, source); reqtype, source, from_bpf);
if (ret) if (ret)
return ret; return ret;
...@@ -2441,7 +2441,7 @@ int hid_hw_raw_request(struct hid_device *hdev, ...@@ -2441,7 +2441,7 @@ int hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
size_t len, enum hid_report_type rtype, enum hid_class_request reqtype) size_t len, enum hid_report_type rtype, enum hid_class_request reqtype)
{ {
return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0); return __hid_hw_raw_request(hdev, reportnum, buf, len, rtype, reqtype, 0, false);
} }
EXPORT_SYMBOL_GPL(hid_hw_raw_request); EXPORT_SYMBOL_GPL(hid_hw_raw_request);
......
...@@ -151,7 +151,7 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, ...@@ -151,7 +151,7 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
} }
ret = __hid_hw_raw_request(dev, buf[0], buf, count, report_type, ret = __hid_hw_raw_request(dev, buf[0], buf, count, report_type,
HID_REQ_SET_REPORT, (__u64)file); HID_REQ_SET_REPORT, (__u64)file, false);
out_free: out_free:
kfree(buf); kfree(buf);
...@@ -228,7 +228,7 @@ static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t ...@@ -228,7 +228,7 @@ static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t
} }
ret = __hid_hw_raw_request(dev, report_number, buf, count, report_type, ret = __hid_hw_raw_request(dev, report_number, buf, count, report_type,
HID_REQ_GET_REPORT, (__u64)file); HID_REQ_GET_REPORT, (__u64)file, false);
if (ret < 0) if (ret < 0)
goto out_free; goto out_free;
......
...@@ -1129,7 +1129,7 @@ int __hid_hw_raw_request(struct hid_device *hdev, ...@@ -1129,7 +1129,7 @@ int __hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
size_t len, enum hid_report_type rtype, size_t len, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
__u64 source); __u64 source, bool from_bpf);
int __hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len, __u64 source); int __hid_hw_output_report(struct hid_device *hdev, __u8 *buf, size_t len, __u64 source);
int hid_hw_raw_request(struct hid_device *hdev, int hid_hw_raw_request(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
......
...@@ -68,7 +68,7 @@ struct hid_ops { ...@@ -68,7 +68,7 @@ struct hid_ops {
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
size_t len, enum hid_report_type rtype, size_t len, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
__u64 source); __u64 source, bool from_bpf);
int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len, int (*hid_hw_output_report)(struct hid_device *hdev, __u8 *buf, size_t len,
__u64 source); __u64 source);
int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type, int (*hid_input_report)(struct hid_device *hid, enum hid_report_type type,
...@@ -181,7 +181,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev, ...@@ -181,7 +181,7 @@ int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
unsigned char reportnum, __u8 *buf, unsigned char reportnum, __u8 *buf,
u32 size, enum hid_report_type rtype, u32 size, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
__u64 source); __u64 source, bool from_bpf);
int hid_bpf_connect_device(struct hid_device *hdev); int hid_bpf_connect_device(struct hid_device *hdev);
void hid_bpf_disconnect_device(struct hid_device *hdev); void hid_bpf_disconnect_device(struct hid_device *hdev);
void hid_bpf_destroy_device(struct hid_device *hid); void hid_bpf_destroy_device(struct hid_device *hid);
...@@ -195,7 +195,7 @@ static inline int dispatch_hid_bpf_raw_requests(struct hid_device *hdev, ...@@ -195,7 +195,7 @@ static inline int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
unsigned char reportnum, u8 *buf, unsigned char reportnum, u8 *buf,
u32 size, enum hid_report_type rtype, u32 size, enum hid_report_type rtype,
enum hid_class_request reqtype, enum hid_class_request reqtype,
u64 source) { return 0; } u64 source, bool from_bpf) { return 0; }
static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; } static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; }
static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {} static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {}
static inline void hid_bpf_destroy_device(struct hid_device *hid) {} static inline void hid_bpf_destroy_device(struct hid_device *hid) {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment