Linux Input/HID development
 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 2/2] HID: bpf: don't allocate the event buffer for an unbound device
Date: Mon, 30 Nov 2026 15:03:15 -0700	[thread overview]
Message-ID: <20261130220315.659533-3-xmei5@asu.edu> (raw)
In-Reply-To: <20261130220315.659533-1-xmei5@asu.edu>

hid_bpf_reg() allocates hdev->bpf.device_data for a ->hid_device_event
program even when the device is not bound.  hid_close_report() has then
cleared the report enums, so the buffer is sized for no reports:
kzalloc(0) returns ZERO_SIZE_PTR and allocated_data is 0.  The next
successful hid_connect() keeps it, because hid_bpf_allocate_event_data()
returns early when device_data is set, and the first input report is
copied into it.  Attaching a ->hid_device_event program while a
->hid_rdesc_fixup program keeps the device unbound, then detaching the
latter, rebinds it with the original descriptor:

  BUG: KASAN: null-ptr-deref in dispatch_hid_bpf_device_event (drivers/hid/bpf/hid_bpf_dispatch.c:54)
  Write of size 1024 at addr 0000000000000010 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)
  ...
  BUG: kernel NULL pointer dereference, address: 0000000000000010
  Oops: Oops: 0002 [#1] SMP KASAN NOPTI
  RIP: 0010:memcpy (arch/x86/lib/memcpy_64.S:37)
  Call Trace:
   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)
  Kernel panic - not syncing: Fatal exception

Only allocate when the report descriptor is parsed; otherwise
hid_bpf_connect_device() allocates it when the device binds.

Cc: stable@vger.kernel.org
Fixes: ebc0d8093e8c ("HID: bpf: implement HID-BPF through bpf_struct_ops")
Reported-by: co+9a1ebd50dd939705@bugs.sh
Signed-off-by: Xiang Mei <xmei5@asu.edu>
---
 drivers/hid/bpf/hid_bpf_struct_ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
index c90b68956cb3..1039adeac8ae 100644
--- a/drivers/hid/bpf/hid_bpf_struct_ops.c
+++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
@@ -216,7 +216,7 @@ static int hid_bpf_reg(void *kdata, struct bpf_link *link)
 		hdev->bpf.rdesc_ops = ops;
 	}
 
-	if (ops->hid_device_event) {
+	if (ops->hid_device_event && (hdev->status & HID_STAT_PARSED)) {
 		err = hid_bpf_allocate_event_data(hdev);
 		if (err)
 			goto out_unlock;
-- 
2.43.0


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

Thread overview: 5+ 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 ` [PATCH 1/2] HID: core: release the HID-BPF event buffer on hid_connect()'s no-listeners exit Xiang Mei
2026-11-30 22:21   ` sashiko-bot
2026-11-30 22:03 ` Xiang Mei [this message]
2026-11-30 22:21   ` [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device sashiko-bot

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-3-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