Skip to content

Commit 8bd0488

Browse files
author
Benjamin Tissoires
committed
HID: bpf: add HID-BPF hooks for hid_hw_raw_requests
This allows to intercept and prevent or change the behavior of hid_hw_raw_request() from a bpf program. The intent is to solve a couple of use case: - firewalling a HID device: a firewall can monitor who opens the hidraw nodes and then prevent or allow access to write operations on that hidraw node. - change the behavior of a device and emulate a new HID feature request The hook is allowed to be run as sleepable so it can itself call hid_bpf_hw_request(), which allows to "convert" one feature request into another or even call the feature request on a different HID device on the same physical device. Link: https://patch.msgid.link/[email protected] Acked-by: Jiri Kosina <[email protected]> Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 6cd735f commit 8bd0488

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

drivers/hid/bpf/hid_bpf_dispatch.c

+38
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,44 @@ dispatch_hid_bpf_device_event(struct hid_device *hdev, enum hid_report_type type
7474
}
7575
EXPORT_SYMBOL_GPL(dispatch_hid_bpf_device_event);
7676

77+
int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
78+
unsigned char reportnum, u8 *buf,
79+
u32 size, enum hid_report_type rtype,
80+
enum hid_class_request reqtype,
81+
u64 source)
82+
{
83+
struct hid_bpf_ctx_kern ctx_kern = {
84+
.ctx = {
85+
.hid = hdev,
86+
.allocated_size = size,
87+
.size = size,
88+
},
89+
.data = buf,
90+
};
91+
struct hid_bpf_ops *e;
92+
int ret, idx;
93+
94+
if (rtype >= HID_REPORT_TYPES)
95+
return -EINVAL;
96+
97+
idx = srcu_read_lock(&hdev->bpf.srcu);
98+
list_for_each_entry_srcu(e, &hdev->bpf.prog_list, list,
99+
srcu_read_lock_held(&hdev->bpf.srcu)) {
100+
if (!e->hid_hw_request)
101+
continue;
102+
103+
ret = e->hid_hw_request(&ctx_kern.ctx, reportnum, rtype, reqtype, source);
104+
if (ret)
105+
goto out;
106+
}
107+
ret = 0;
108+
109+
out:
110+
srcu_read_unlock(&hdev->bpf.srcu, idx);
111+
return ret;
112+
}
113+
EXPORT_SYMBOL_GPL(dispatch_hid_bpf_raw_requests);
114+
77115
u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *size)
78116
{
79117
int ret;

drivers/hid/bpf/hid_bpf_struct_ops.c

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static int hid_bpf_ops_check_member(const struct btf_type *t,
4444

4545
switch (moff) {
4646
case offsetof(struct hid_bpf_ops, hid_rdesc_fixup):
47+
case offsetof(struct hid_bpf_ops, hid_hw_request):
4748
break;
4849
default:
4950
if (prog->sleepable)

drivers/hid/hid-core.c

+6
Original file line numberDiff line numberDiff line change
@@ -2406,13 +2406,19 @@ int __hid_hw_raw_request(struct hid_device *hdev,
24062406
__u64 source)
24072407
{
24082408
unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2409+
int ret;
24092410

24102411
if (hdev->ll_driver->max_buffer_size)
24112412
max_buffer_size = hdev->ll_driver->max_buffer_size;
24122413

24132414
if (len < 1 || len > max_buffer_size || !buf)
24142415
return -EINVAL;
24152416

2417+
ret = dispatch_hid_bpf_raw_requests(hdev, reportnum, buf, len, rtype,
2418+
reqtype, source);
2419+
if (ret)
2420+
return ret;
2421+
24162422
return hdev->ll_driver->raw_request(hdev, reportnum, buf, len,
24172423
rtype, reqtype);
24182424
}

include/linux/hid_bpf.h

+35
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ struct hid_bpf_ops {
130130
*/
131131
int (*hid_rdesc_fixup)(struct hid_bpf_ctx *ctx);
132132

133+
/**
134+
* @hid_hw_request: called whenever a hid_hw_raw_request() call is emitted
135+
* on the HID device
136+
*
137+
* It has the following arguments:
138+
*
139+
* ``ctx``: The HID-BPF context as &struct hid_bpf_ctx
140+
* ``reportnum``: the report number, as in hid_hw_raw_request()
141+
* ``rtype``: the report type (``HID_INPUT_REPORT``, ``HID_FEATURE_REPORT``,
142+
* ``HID_OUTPUT_REPORT``)
143+
* ``reqtype``: the request
144+
* ``source``: a u64 referring to a uniq but identifiable source. If %0, the
145+
* kernel itself emitted that call. For hidraw, ``source`` is set
146+
* to the associated ``struct file *``.
147+
*
148+
* Return: %0 to keep processing the request by hid-core; any other value
149+
* stops hid-core from processing that event. A positive value should be
150+
* returned with the number of bytes returned in the incoming buffer; a
151+
* negative error code interrupts the processing of this call.
152+
*/
153+
int (*hid_hw_request)(struct hid_bpf_ctx *ctx, unsigned char reportnum,
154+
enum hid_report_type rtype, enum hid_class_request reqtype,
155+
__u64 source);
156+
157+
133158
/* private: do not show up in the docs */
134159
struct hid_device *hdev;
135160
};
@@ -152,6 +177,11 @@ struct hid_bpf {
152177
#ifdef CONFIG_HID_BPF
153178
u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type, u8 *data,
154179
u32 *size, int interrupt, u64 source);
180+
int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
181+
unsigned char reportnum, __u8 *buf,
182+
u32 size, enum hid_report_type rtype,
183+
enum hid_class_request reqtype,
184+
__u64 source);
155185
int hid_bpf_connect_device(struct hid_device *hdev);
156186
void hid_bpf_disconnect_device(struct hid_device *hdev);
157187
void hid_bpf_destroy_device(struct hid_device *hid);
@@ -161,6 +191,11 @@ u8 *call_hid_bpf_rdesc_fixup(struct hid_device *hdev, u8 *rdesc, unsigned int *s
161191
static inline u8 *dispatch_hid_bpf_device_event(struct hid_device *hid, enum hid_report_type type,
162192
u8 *data, u32 *size, int interrupt,
163193
u64 source) { return data; }
194+
static inline int dispatch_hid_bpf_raw_requests(struct hid_device *hdev,
195+
unsigned char reportnum, u8 *buf,
196+
u32 size, enum hid_report_type rtype,
197+
enum hid_class_request reqtype,
198+
u64 source) { return 0; }
164199
static inline int hid_bpf_connect_device(struct hid_device *hdev) { return 0; }
165200
static inline void hid_bpf_disconnect_device(struct hid_device *hdev) {}
166201
static inline void hid_bpf_destroy_device(struct hid_device *hid) {}

0 commit comments

Comments
 (0)