From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796077318; b=sWws4SP8mne9r0CGm/uWqFQfaQLB+6W87UMZuOsqFYTgCXEsK4Pu5CnDzAcZF1AqkRRg k7Lpx4WRDqoawCFGigCFPbdbH7Dehh0Vh+Rn0LDTN2ilE13IBMI+0HyfmAAWGBAq8ns7Q OJc27hX7kyV3OQ4LbMgyUYcuvwLJRkPQKnWYOd3tkTSysGg3iBuAyRfKtEzHQGU66D5sH PgoIjk850/ZXmoUQK2lj4Z2SNMQ0zqxb+Z506T2L9uwUm1V9m2neKhwMcmzSMkNiIFVB+ t/iHveO40hZLfd+NxQdBFxS3j0GWjVT83iRyjwMk1DX+2JpgtbKcQvQhunI7NfsDcRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796077318; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=pGBTJh4V9RXHflKhzPO74jWiUCyrLDQEBJohrXnFM5k=; b=KkPk7BxLZ4/CDjOgiBlyWu7jT3oZHrjHYMG0J/yn1SufLpnBhDx8iEhxX8GJrlBUz8rv 4ijHD1RojMKBDIiEQgISm6YqAHDQWqEt5MbseGK+rPxv4s3E3EDjgZT9zVjG/Rb5hPHFL TgXH4cYVN0WTMKGllIf4FfR3kNaHlARblb2I3oIo488NjFX2N/Zw8FdmI20bHt1qmHwwh 10eZEZmnVCL3Pd5FOXyWvC2SV+p5wKCQu/liAyC46nSzCPDinPEugQJwYvZSYc3tQjfkG TnA60Mt6+cbQMdO1wFeKrj5mbIuMviOunuYTAIcApUraDI3SQTzoegG/bxhMnvPWiyQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QCwT22ol; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B3BA606A31A; Mon, 30 Nov 2026 22:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QCwT22ol Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A3321F00896; Mon, 30 Nov 2026 22:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796077317; bh=pGBTJh4V9RXHflKhzPO74jWiUCyrLDQEBJohrXnFM5k=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=QCwT22olTDghRVcYzzmLqVKfZBrvwUbTx2UlGnp/oNZqW8YF40EGwj+rV98nS7pyD zDHqXsLLsj3lX8dC9hk55qDEEybGf/afWSdHX+4P+X61LsvT7NZbWYm3rc/PBEpZ75 ty6M6srcTR3o4YyVzBW+LDP92Kw7CbAYYbGddkzzkg8Cn4V6aItSsot67q2dCCfA6D QFiahi2PxyIQop7FKnTkRDaUy9vBACVmj3JqadVw3o7+4vPz2hx5uzNRFQqAi1tv2f RuhVYv+HVdLOf/PS6RNZxbduwxNPTk9scll2c3ogP5F7j8HZ3MJ11vChOLxHfni9MF LihBIuoWYBQPg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 2/2] HID: bpf: don't allocate the event buffer for an unbound device Reply-To: sashiko-reviews@lists.linux.dev To: "Xiang Mei" Cc: linux-input@vger.kernel.org, bpf@vger.kernel.org, dmitry.torokhov@gmail.com In-Reply-To: <20261130220315.659533-3-xmei5@asu.edu> References: <20261130220315.659533-1-xmei5@asu.edu> <20261130220315.659533-3-xmei5@asu.edu> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 22:21:57 +0000 X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] HID: bpf: ZERO_SIZE_PTR alloc in __hid_bpf_allocate_data() when reports empty - [Medium] HID: bpf: unsynchronized HID_STAT_PARSED check in hid_bpf_reg() races unbind - [Medium] HID: bpf: missing event buffer alloc when hid_hw_start() skips hid_connect() It also noticed 1 related pre-existing issue this patch does not introduce: - [Critical] HID: bpf: data race in hid_bpf_disconnect_device() [1] -- commit 0ebd0ca34564242602066bbb7c6f117841d9ff5a Author: Xiang Mei HID: bpf: don't allocate the event buffer for an unbound device This commit gates hid_bpf_allocate_event_data() in hid_bpf_reg() on HID_STAT_PARSED so that hdev->bpf.device_data is not allocated as ZERO_SIZE_PTR when attaching a hid_device_event program to an unbound devic= e. > diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_b= pf_struct_ops.c > index c90b68956cb3b..1039adeac8ae2 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 =3D ops; > } > =20 > - if (ops->hid_device_event) { > + if (ops->hid_device_event && (hdev->status & HID_STAT_PARSED)) { > err =3D hid_bpf_allocate_event_data(hdev); > if (err) > goto out_unlock; [Severity: High] Can __hid_bpf_allocate_data() still store ZERO_SIZE_PTR in hdev->bpf.device_data even when HID_STAT_PARSED is set? hid_open_report() -> hid_parse_collections() sets HID_STAT_PARSED even when the report descriptor defines no reports (or only zero-length unnumbered reports), and hidraw_connect() still claims the device during hid_connect(). When hid_bpf_allocate_event_data() runs on such a device, max_report_len is 0, so __hid_bpf_allocate_data() calls kzalloc(0, GFP_KERNEL) and sets hdev->bpf.device_data to ZERO_SIZE_PTR with allocated_data =3D=3D 0. Deliv= ering an input report then hits the same memcpy() into ZERO_SIZE_PTR in dispatch_hid_bpf_device_event(). [Severity: Medium] What prevents hid_bpf_reg() from racing with hid_device_remove() and hid_close_report() while checking HID_STAT_PARSED and walking hdev->report_enum? hid_bpf_reg() holds only hdev->bpf.prog_list_lock, whereas probe and remove update hdev->report_enum and HID_STAT_PARSED under device_lock(&hdev->dev) and hdev->driver_input_lock. During unbind, hid_disconnect() calls hid_bpf_disconnect_device() before hid_close_report() frees the reports and clears HID_STAT_PARSED at the very end: CPU0 CPU1 hid_device_remove() hid_hw_stop() hid_disconnect() hid_bpf_disconnect_device() ... hid_close_report() hid_free_report(report) hid_bpf_reg() if (hdev->status & HID_STAT_PARSED) hid_bpf_allocate_event_data() __hid_bpf_allocate_data() hid_report_len(report) <-- use-after-free memset(report_enum, 0, ...) device->status &=3D ~HID_STAT_PARSED If __hid_bpf_allocate_data() runs after memset(report_enum, 0, ...) and before HID_STAT_PARSED is cleared, it sees all reports zeroed and allocates ZERO_SIZE_PTR into hdev->bpf.device_data after hid_bpf_disconnect_device() has already run, crashing on the next input report after rebind. If it runs before hid_close_report() frees the reports (or during a failing __hid_device_probe() after hid_open_report()), hdev->bpf.device_data is left allocated on the unbound device. [Severity: Medium] Does skipping hid_bpf_allocate_event_data() here leave hdev->bpf.device_data NULL for drivers that do not call hid_connect()? When a hid_device_event program is attached while the device is unbound (or reprobed via hid_rdesc_fixup), allocation is deferred to hid_bpf_connect_device(), which is only called from hid_connect(). Drivers such as hid-ft260, hid-mcp2200, hid-mcp2221 and hid-picolcd_core call hid_parse() and hid_hw_start(hdev, 0) without calling hid_connect(), so hdev->bpf.device_data stays NULL and dispatch_hid_bpf_device_event() silent= ly skips all attached hid_device_event programs. [Severity: Critical] This problem wasn't introduced by this patch, but nothing serializes [1] hid_bpf_allocate_event_data() in hid_bpf_reg() against hid_bpf_disconnect_device() during device unbind: CPU0 CPU1 hid_bpf_reg() mutex_lock(&hdev->bpf.prog_list_loc= k) hid_bpf_allocate_event_data() hdev->bpf.device_data =3D alloc_d= ata hid_bpf_disconnect_device() kfree(hdev->bpf.device_data) hdev->bpf.device_data =3D NULL mutex_unlock(&hdev->bpf.prog_list_l= ock) hid_bpf_disconnect_device() runs under hdev->driver_input_lock without taki= ng hdev->bpf.prog_list_lock, so concurrent attach and unbind race directly on hdev->bpf.device_data and hdev->bpf.allocated_data, either freeing the newly allocated buffer while attach is in progress or leaking it if the allocation stores into hdev->bpf.device_data after hid_bpf_disconnect_device() returns. [1] https://sashiko.dev/#/bug/linux-a4c268e1-ac73-484a-9c4c-064a698eb6e6 --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130220315.6595= 33-1-xmei5@asu.edu?part=3D2