* [PATCH] HID: bpf: fix use-after-free in hid_bpf_reg()
@ 2026-11-25 16:47 Sechang Lim
2026-11-25 17:07 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Sechang Lim @ 2026-11-25 16:47 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: linux-input, bpf, Alexei Starovoitov, Shen Yongchao, stable
hid_bpf_reg() keeps the reference from hid_get_device() in ops->hdev
and adds ops to prog_list. After prog_list_lock is dropped, a concurrent
hid_destroy_device() can put that reference and free the device. With
hid_rdesc_fixup set, hid_bpf_reconnect() then runs on a freed device:
BUG: KASAN: use-after-free in device_reprobe+0x111/0x1c0
Call Trace:
device_reprobe+0x111/0x1c0
hid_bpf_reg+0x3ce/0x420
bpf_struct_ops_link_create+0x39b/0x4e0
__sys_bpf+0xaca/0xc30
Hold an extra device reference until hid_bpf_reg() returns.
Fixes: ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")
Cc: stable@vger.kernel.org
Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
---
drivers/hid/bpf/hid_bpf_struct_ops.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
index c90b68956cb3..e2512bd6e6b2 100644
--- a/drivers/hid/bpf/hid_bpf_struct_ops.c
+++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
@@ -198,6 +198,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
return PTR_ERR(hdev);
ops->hdev = hdev;
+ get_device(&hdev->dev);
mutex_lock(&hdev->bpf.prog_list_lock);
@@ -239,6 +240,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
hid_bpf_reconnect(hdev);
}
+ put_device(&hdev->dev);
return err;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] HID: bpf: fix use-after-free in hid_bpf_reg()
2026-11-25 16:47 [PATCH] HID: bpf: fix use-after-free in hid_bpf_reg() Sechang Lim
@ 2026-11-25 17:07 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 17:07 UTC (permalink / raw)
To: Sechang Lim; +Cc: linux-input, bpf, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] HID: bpf: use-after-free on struct hid_device in
hid_bpf_unreg() [1]
--
commit 8a1a1e9fd3f55ee6ffdf0b624cf6f20fd17120ed
Author: Sechang Lim <rhkrqnwk98@gmail.com>
HID: bpf: fix use-after-free in hid_bpf_reg()
This commit holds an extra device reference across hid_bpf_reg() so that
concurrent device destruction cannot free struct hid_device before
hid_bpf_reconnect() runs.
> diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
> index c90b68956cb3b..e2512bd6e6b29 100644
> --- a/drivers/hid/bpf/hid_bpf_struct_ops.c
> +++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
> @@ -198,6 +198,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
> return PTR_ERR(hdev);
>
> ops->hdev = hdev;
> + get_device(&hdev->dev);
>
> mutex_lock(&hdev->bpf.prog_list_lock);
>
> @@ -239,6 +240,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
> hid_bpf_reconnect(hdev);
> }
>
> + put_device(&hdev->dev);
> return err;
> }
[Severity: Critical]
This problem wasn't introduced by this patch, but can the paired
hid_bpf_unreg() hit a similar use-after-free on struct hid_device
[1]?
drivers/hid/bpf/hid_bpf_struct_ops.c:hid_bpf_unreg() {
...
hdev = ops->hdev;
/* check if __hid_bpf_ops_destroy_device() has been called */
if (!hdev)
return;
mutex_lock(&hdev->bpf.prog_list_lock);
...
}
hid_bpf_unreg() reads ops->hdev without a lock or reference before locking
hdev->bpf.prog_list_lock:
CPU0 CPU1
hid_bpf_unreg()
hdev = ops->hdev;
hid_destroy_device()
hid_bpf_destroy_device()
__hid_bpf_ops_destroy_device()
mutex_lock(&hdev->bpf.prog_list_lock)
e->hdev = NULL
mutex_unlock(&hdev->bpf.prog_list_lock)
hid_put_device(hdev)
put_device(&hdev->dev)
kfree(hdev)
mutex_lock(&hdev->bpf.prog_list_lock)
<-- use-after-free
In addition, if hid_bpf_unreg() takes prog_list_lock after
hid_bpf_destroy_device() sets hdev->bpf.destroyed to true but before
__hid_bpf_ops_destroy_device() clears e->hdev, hid_bpf_unreg() still sees
a non-NULL ops->hdev and calls hid_bpf_reconnect() on a device being
destroyed.
[1] https://sashiko.dev/#/bug/linux-6599bf1d-e091-4b20-bab0-cb93dd34be79
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125164728.24914-1-rhkrqnwk98@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 17:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 16:47 [PATCH] HID: bpf: fix use-after-free in hid_bpf_reg() Sechang Lim
2026-11-25 17:07 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox