mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiang Mei <xmei5@asu.edu>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, Sashiko <sashiko-bot@kernel.org>,
	co+86678873d3803945@bugs.sh, co+9a1ebd50dd939705@bugs.sh,
	Xiang Mei <xmei5@asu.edu>,
	stable@vger.kernel.org
Subject: [PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit
Date: Mon, 30 Nov 2026 15:03:14 -0700	[thread overview]
Message-ID: <20261130220315.659533-2-xmei5@asu.edu> (raw)
In-Reply-To: <20261130220315.659533-1-xmei5@asu.edu>

hid_connect() allocates hdev->bpf.device_data in hid_bpf_connect_device(),
but its "device has no listeners" exit returns -ENODEV without freeing it.
hid_hw_start() then only calls ll_driver->stop(), so hid_disconnect(), the
sole caller of hid_bpf_disconnect_device(), never runs.  The stale buffer
and its allocated_data are reused by the next successful probe, since
hid_bpf_allocate_event_data() returns early when device_data is set, and
dispatch_hid_bpf_device_event() copies a full report into it unclamped.
A HID-BPF program with ->hid_rdesc_fixup and ->hid_device_event can do
this: attaching it reprobes with a small, listener-less descriptor (64-byte
buffer leaked), detaching reprobes with the original one, and a 1024-byte
UHID_INPUT2 overflows the buffer:

  BUG: KASAN: slab-out-of-bounds in dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54)
  Write of size 1024 at addr ffff88800a401480 by task exploit/87
  Call Trace:
   ...
   __asan_memcpy (mm/kasan/shadow.c:106 (discriminator 1))
   dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54)
   __hid_input_report (drivers/hid/hid-core.c:2150)
   hid_safe_input_report (drivers/hid/hid-core.c:2227)
   uhid_char_write (drivers/hid/uhid.c:609 drivers/hid/uhid.c:767)
   vfs_write (fs/read_write.c:665)
   ksys_write (fs/read_write.c:719)
   do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
  ...
   which belongs to the cache kmalloc-64 of size 64
  ...
  Oops: general protection fault, probably for non-canonical address 0x4847464544434261: ...
  RIP: 0010:__refill_objects_node (mm/slub.c:584 mm/slub.c:597 mm/slub.c:7420)
  Call Trace:
   ...
   __kmalloc_noprof (mm/slub.c:4887 mm/slub.c:5021 mm/slub.c:5530 mm/slub.c:5556)
   __hid_bpf_allocate_data (... drivers/hid/bpf/hid_bpf_dispatch.c:254)
   hid_connect (drivers/hid/hid-core.c:2309)
   hid_hw_start (drivers/hid/hid-core.c:2436 drivers/hid/hid-core.c:2427)
   hid_device_probe (drivers/hid/hid-core.c:2839 drivers/hid/hid-core.c:2878)
   ...
   device_reprobe (drivers/base/bus.c:873 drivers/base/bus.c:907)
   hid_bpf_reg (drivers/hid/bpf/hid_bpf_struct_ops.c:239)
   bpf_struct_ops_map_update_elem (kernel/bpf/bpf_struct_ops.c:925)
   ...
   __sys_bpf (kernel/bpf/syscall.c:6443)
   ...
   do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
  Kernel panic - not syncing: Fatal exception

Release the buffer on that exit.  hid_bpf_disconnect_device() also clears
device_data, so later paths through hid_disconnect() are unaffected.

Cc: stable@vger.kernel.org
Fixes: 658ee5a64fcf ("HID: bpf: allocate data memory for device_event BPF programs")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260908202056.698371F00A3A@smtp.kernel.org/
Reported-by: co+86678873d3803945@bugs.sh
Signed-off-by: Xiang Mei <xmei5@asu.edu>
---
 drivers/hid/hid-core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 4a47deb9d833..3f93b0d48537 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -2339,6 +2339,7 @@ int hid_connect(struct hid_device *hdev, unsigned int connect_mask)
 	 * to any other listener. */
 	if (!hdev->claimed && !hdev->driver->raw_event) {
 		hid_err(hdev, "device has no listeners, quitting\n");
+		hid_bpf_disconnect_device(hdev);
 		return -ENODEV;
 	}
 
-- 
2.43.0


  reply	other threads:[~2026-11-30 22:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:03 [PATCH 0/2] HID: bpf: fix stale device_event buffer reuse Xiang Mei
2026-11-30 22:03 ` Xiang Mei [this message]
2026-11-30 22:03 ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Xiang Mei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130220315.659533-2-xmei5@asu.edu \
    --to=xmei5@asu.edu \
    --cc=bentiss@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=co+86678873d3803945@bugs.sh \
    --cc=co+9a1ebd50dd939705@bugs.sh \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®