* [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports
@ 2026-08-24 5:43 Wei Jie Law
2026-08-24 6:01 ` sashiko-bot
2026-08-24 12:38 ` Wei Jie Law
0 siblings, 2 replies; 3+ messages in thread
From: Wei Jie Law @ 2026-08-24 5:43 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires
Cc: Andrew Duggan, linux-input, linux-kernel, stable
The hid-rmi driver sizes its writeReport/readReport buffer purely from
the report descriptor supplied by the device, with no minimum bound:
data->input_report_size = hid_report_len(input_report);
data->output_report_size = hid_report_len(output_report);
alloc_size = data->output_report_size + data->input_report_size;
data->writeReport = devm_kzalloc(&hdev->dev, alloc_size, GFP_KERNEL);
but then reads and writes fixed offsets into that buffer without
validating the sizes, and readReport is placed inside the same
allocation:
data->readReport = data->writeReport + data->output_report_size;
A device declaring a 1-byte output report (0x09) and a 1-byte input
report (0x0c) makes hid_report_len() return 2 for each, so alloc_size is
4, while rmi_set_page() -- reached unconditionally at probe time through
rmi_input_configured() -- stores writeReport[4] and rmi_hid_read_block()
stores writeReport[0..5]. Since readReport is writeReport +
output_report_size, those stores land on top of the read buffer and
corrupt the window the next reply is parsed out of.
The read path is worse: the copy length comes from readReport[1], which
is filled in from the device's response and can be up to 255, and the
copy starts at &readReport[2] without any regard for
input_report_size, so it runs past the end of the allocation and into
adjacent slab objects. This does not even need a lying device --
rmi_f01_probe() issues a fixed 21-byte register read, so any device
declaring an input report smaller than 23 bytes makes the driver read
out of bounds even when the device answers truthfully. Those bytes
become the register values the RMI core acts on; they are printed to the
kernel log as the product id by rmi_f01_probe() and exported through the
mode 0444 sysfs attribute of the same name, and they are sent back to
the device as the interrupt mask by rmi_driver_set_irq_bits(), so an
undersized report descriptor leaks heap contents both to unprivileged
userspace and to the device itself.
The write path has no bound either: rmi_hid_write_block() copies an
unbounded len to &writeReport[4], and the largest caller a device can
drive at probe time is rmi_driver_set_irq_bits(), which passes
data->num_of_irq_regs -- derived from the interrupt source counts the
device declares in its Page Description Table, 39 entries per page over
as many pages as it likes.
Finally, the read loop cannot terminate on a zero-length reply: such a
reply copies nothing and advances neither bytes_read nor bytes_needed,
and because a reply did arrive the one second wait_event_timeout() does
not fire either, so a device answering 0 forever keeps the loop running
forever inside the probe worker with page_mutex held. khungtaskd does
not notice, because every reply wakes the task and bumps its context
switch count.
Reject reports that are too small at probe time, where the driver needs
6 output bytes for the write reports it builds and 3 input bytes for the
read handshake, clamp the write and the read copy to the report sizes
the device declared, and treat a zero-length reply as an error.
Clamping the read count does not regress working hardware: the RMI read
loop already handles a reply carrying fewer bytes than requested, it
just goes round again. A write longer than the output report was
overrunning the buffer already, so rejecting it cannot regress a device
that used to work.
Verified on v6.12.69 and on v6.12.105 built with CONFIG_KASAN=y and
booted kasan_multi_shot (generic KASAN otherwise reports only the first
error per boot), whose hid-rmi.c is identical to mainline here. An
emulated RMI4 device driven over /dev/uhid, and the same device again
over dummy_hcd plus raw-gadget, give identical results:
BUG: KASAN: slab-out-of-bounds in rmi_hid_read_block+0x409/0x750 [hid_rmi]
Read of size 21 at addr ffff88800bf33bba by task kworker/0:3/285
Workqueue: events uhid_device_add_worker
kasan_report+0xc6/0x100
kasan_check_range+0x105/0x1b0
__asan_memcpy+0x23/0x60
rmi_hid_read_block+0x409/0x750 [hid_rmi]
rmi_f01_probe+0x5dd/0x1dc0 [rmi_core]
BUG: KASAN: slab-out-of-bounds in rmi_hid_write_block+0x1a9/0x350 [hid_rmi]
Write of size 35 at addr ffff88810a2b24ac by task kworker/1:10/666
__asan_memcpy+0x3c/0x60
rmi_hid_write_block+0x1a9/0x350 [hid_rmi]
rmi_driver_set_irq_bits+0x1f6/0x4d0 [rmi_core]
rmi_f30_config+0x27a/0x4d0 [rmi_core]
rmi_driver_process_config_requests+0xe7/0x150 [rmi_core]
rmi_driver_probe+0x636/0xbf0 [rmi_core]
rmi_register_transport_device+0x19e/0x3e0 [rmi_core]
rmi_input_configured+0x184/0x2e0 [hid_rmi]
hidinput_connect+0x13e3/0x2a50
hid_hw_start+0x89/0x120
rmi_probe+0x952/0xcf0 [hid_rmi]
and for the zero-length reply, after 225 replies at 200 ms intervals:
kworker/1:0+events state=D
rmi_hid_read_block+0x5cc/0x750 [hid_rmi]
rmi_scan_pdt+0x211/0x3f0 [rmi_core]
rmi_driver_probe+0x1c0/0xbf0 [rmi_core]
really_probe+0x1e3/0x930
After this change the undersized descriptor is refused at probe with
"rmi reports too small (out=2 in=2)", the oversized read and write are
both rejected, the zero-length reply fails the read with -EIO and the
worker returns, and a device declaring reports large enough for a
21-byte register read still probes normally and reports its real product
id.
Link: https://lore.kernel.org/linux-input/20260822121007.153988-1-98lawweijie@gmail.com/
Fixes: 9fb6bf02e3ad ("HID: rmi: introduce RMI driver for Synaptics touchpads")
Cc: stable@vger.kernel.org
Signed-off-by: Wei Jie Law <98lawweijie@gmail.com>
---
Changes in v2:
- Corrected the claim in the v1 commit message that rmi_set_page()
writes one byte past the allocation. That has not been true since
commit 6fcd7e702d3d ("devres: Use kmalloc_size_roundup() to match
ksize() usage"), which makes check_dr_size() round the devres
allocation up to the whole kmalloc bucket, so devm_kzalloc(4) is a
64-byte kmalloc and the store at offset 44 is in bounds -- KASAN
stays silent on it, correctly. What that store does do is land on
top of readReport, since readReport is writeReport +
output_report_size. The out-of-bounds accesses are the read and the
unbounded write, and the KASAN reports for both are now quoted.
- Reject a zero-length READ_DATA reply. Such a reply advances neither
bytes_read nor bytes_needed, and because a reply did arrive the
wait_event_timeout() does not fire either, so a device answering 0
forever spins in rmi_hid_read_block() indefinitely inside the probe
worker with page_mutex held. The v1 clamp does not help, since
min_t(int, 0, input_report_size - 2) is still 0. khungtaskd does not
notice because every reply wakes the task.
- No functional change to the three checks already in v1.
v1: https://lore.kernel.org/linux-input/20260822121007.153988-1-98lawweijie@gmail.com/
drivers/hid/hid-rmi.c | 33 ++++++++++++++++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/hid-rmi.c b/drivers/hid/hid-rmi.c
index d4af17fdba46..98fafa7499a7 100644
--- a/drivers/hid/hid-rmi.c
+++ b/drivers/hid/hid-rmi.c
@@ -235,7 +235,22 @@ static int rmi_hid_read_block(struct rmi_transport_dev *xport, u16 addr,
break;
}
- read_input_count = data->readReport[1];
+ read_input_count = min_t(int, data->readReport[1],
+ data->input_report_size - 2);
+ if (!read_input_count) {
+ /*
+ * A zero length reply advances neither
+ * bytes_read nor bytes_needed, and because a
+ * reply did arrive the wait above does not
+ * time out either, so a device answering 0
+ * forever would spin here indefinitely with
+ * page_mutex held.
+ */
+ hid_warn(hdev, "%s: zero-length read reply\n",
+ __func__);
+ ret = -EIO;
+ break;
+ }
memcpy(buf + bytes_read, &data->readReport[2],
min(read_input_count, bytes_needed));
@@ -271,6 +286,11 @@ static int rmi_hid_write_block(struct rmi_transport_dev *xport, u16 addr,
goto exit;
}
+ if (len > data->output_report_size - 4) {
+ ret = -EINVAL;
+ goto exit;
+ }
+
data->writeReport[0] = RMI_WRITE_REPORT_ID;
data->writeReport[1] = len;
data->writeReport[2] = addr & 0xFF;
@@ -696,6 +716,17 @@ static int rmi_probe(struct hid_device *hdev, const struct hid_device_id *id)
data->output_report_size = hid_report_len(output_report);
+ /*
+ * The write reports built by this driver occupy 6 bytes and the read
+ * handshake looks at the first 3 bytes of an input report, so refuse
+ * to drive a device whose reports cannot hold them.
+ */
+ if (data->output_report_size < 6 || data->input_report_size < 3) {
+ hid_err(hdev, "rmi reports too small (out=%u in=%u)\n",
+ data->output_report_size, data->input_report_size);
+ goto start;
+ }
+
data->device_flags |= RMI_DEVICE;
alloc_size = data->output_report_size + data->input_report_size;
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports
2026-08-24 5:43 [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports Wei Jie Law
@ 2026-08-24 6:01 ` sashiko-bot
2026-08-24 12:38 ` Wei Jie Law
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-08-24 6:01 UTC (permalink / raw)
To: Wei Jie Law; +Cc: linux-input, dmitry.torokhov
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] The device's read state machine becomes permanently poisoned because the new zero-length reply error path leaks the `RMI_READ_DATA_PENDING` flag.
Pre-existing issues:
- [High] Use-After-Free occurs because `reset_work` can execute after `rmi_data` is freed, due to improper cleanup ordering in `rmi_remove()` and missing cancellation in the `probe()` error path.
- [High] Subsystem guide violation: `rmi_report()` accesses report fields by index without validating the field count first.
- [Medium] Data corruption can occur because `hdata->readReport` is modified concurrently by the IRQ handler while being read by the process context.
--
--- Patch [1]: [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports ---
commit 08dac6ce9a3b8c9a54acc37d7e783d9b9cfd4864
Author: Wei Jie Law <98lawweijie@gmail.com>
HID: rmi: fix OOB access with undersized RMI reports
This commit addresses out-of-bounds read and write accesses in the hid-rmi
driver caused by undersized RMI reports. It introduces size validation during
probe and clamps read/write lengths to the device-declared sizes. It also
treats zero-length read replies as errors to prevent an infinite loop.
Link: https://lore.kernel.org/linux-input/20260822121007.153988-1-98lawweijie@gmail.com/
Fixes: 9fb6bf02e3ad ("HID: rmi: introduce RMI driver for Synaptics touchpads")
Cc: stable@vger.kernel.org
> diff --git a/drivers/hid/hid-rmi.c b/drivers/hid/hid-rmi.c
> --- a/drivers/hid/hid-rmi.c
> +++ b/drivers/hid/hid-rmi.c
> @@ -235,7 +235,22 @@ static int rmi_hid_read_block(struct rmi_transport_dev *xport, u16 addr,
> break;
> }
>
> - read_input_count = data->readReport[1];
> + read_input_count = min_t(int, data->readReport[1],
> + data->input_report_size - 2);
> + if (!read_input_count) {
> + /*
> + * A zero length reply advances neither
> + * bytes_read nor bytes_needed, and because a
> + * reply did arrive the wait above does not
> + * time out either, so a device answering 0
> + * forever would spin here indefinitely with
> + * page_mutex held.
> + */
> + hid_warn(hdev, "%s: zero-length read reply\n",
> + __func__);
> + ret = -EIO;
> + break;
> + }
[Severity: High]
If a zero-length reply is received and we break here, does this bypass the
clear_bit(RMI_READ_DATA_PENDING, &data->flags) at the end of the loop? It
looks like leaving this flag set might permanently poison the state machine,
causing subsequent loops or calls to rmi_hid_read_block() to pass the wait
condition immediately and fail with -EIO.
> memcpy(buf + bytes_read, &data->readReport[2],
> min(read_input_count, bytes_needed));
[Severity: Medium]
This is a pre-existing issue, but could this memcpy race with the IRQ
handler? While rmi_hid_read_block() is reading from data->readReport,
rmi_read_data_event() might concurrently overwrite hdata->readReport if an
unsolicited report arrives, potentially leading to corrupted register reads.
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but can a use-after-free occur
during device removal? In rmi_remove(), the work is cancelled before hardware
events are stopped:
cancel_work_sync(&hdata->reset_work);
rmi_unregister_transport_device(&hdata->xport);
}
hid_hw_stop(hdev);
Because hid_hw_stop() is called after cancel_work_sync(), incoming events
can still arrive after the work is cancelled. If rmi_event() queues the work
again via schedule_work(&data->reset_work), could it execute after devres
frees the rmi_data structure? Additionally, if hid_hw_start() fails in
rmi_probe(), it appears to return without cancelling the work.
[Severity: High]
This is a pre-existing issue, but does rmi_report() lack report field
validation?
struct hid_field *field = report->field[0];
The HID subsystem guidelines state that accessing report fields by index
without first checking that report->maxfield is at least 1 can lead to
out-of-bounds accesses with crafted descriptors.
--
Sashiko AI review · https://sashiko.dev/#/patchset/00a489f38b240624dcb5a4bae36a53fcba9cfb47.1787549195.git.98lawweijie@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports
2026-08-24 5:43 [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports Wei Jie Law
2026-08-24 6:01 ` sashiko-bot
@ 2026-08-24 12:38 ` Wei Jie Law
1 sibling, 0 replies; 3+ messages in thread
From: Wei Jie Law @ 2026-08-24 12:38 UTC (permalink / raw)
To: Jiri Kosina, Benjamin Tissoires; +Cc: Andrew Duggan, linux-input, linux-kernel
Superseded by v3:
https://lore.kernel.org/linux-input/20260824122708.76168-1-98lawweijie@gmail.com/
Please do not apply this version. It bails out of the read loop on a
zero-length reply without clearing RMI_READ_DATA_PENDING, and that flag
is what the wait at the top of the loop tests. Leaving it set makes
every later wait_event_timeout() return immediately on the stale reply,
so the four remaining retries of that call -- and every subsequent
rmi_hid_read_block() -- fail instantly with -EIO without ever waiting
for the device again. One zero-length reply from an otherwise honest
device is enough to kill the read path for the rest of the device's
life.
v3 clears the flag on that path.
Please mark this one superseded.
Thanks,
Wei Jie
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-24 12:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24 5:43 [PATCH v2] HID: rmi: fix OOB access with undersized RMI reports Wei Jie Law
2026-08-24 6:01 ` sashiko-bot
2026-08-24 12:38 ` Wei Jie Law
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.