[PATCH v3 2/2] Input: synaptics-rmi4 - reject a PDT that grows between scans
Wei Jie LAW <[email protected]>
| Newsgroups | org.kernel.vger.stable,org.kernel.vger.linux-input,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
From: Wei Jie Law <[email protected]> rmi_driver_probe() walks the Page Description Table three times, and each walk reads the table back from the device: 1. rmi_initial_reset - issue the reset command in the F01 entry 2. rmi_count_irqs - total the interrupt sources 3. rmi_create_function - create the functions and set their irq bits Scan 2 fixes data->irq_count, data->num_of_irq_regs and the size of every per-function irq_mask[]. Scan 3 then accumulates fn->irq_pos and does for (i = 0; i < fn->num_of_irqs; i++) set_bit(fn->irq_pos + i, fn->irq_mask); without checking the result against the count that sized the bitmap. Nothing makes the device answer the third scan the way it answered the second, so a device that reports one function with one interrupt source on scan 2 and a long list of functions on scan 3 walks set_bit() past the end of the flexible array at the tail of every struct rmi_function: BUG: KASAN: slab-out-of-bounds in rmi_create_function+0x560/0x930 [rmi_core] Write of size 8 at addr ffff888110c92b58 by task kworker/1:2/129 Workqueue: events uhid_device_add_worker kasan_report+0xc6/0x100 kasan_check_range+0x105/0x1b0 rmi_create_function+0x560/0x930 [rmi_core] rmi_scan_pdt+0x190/0x3f0 [rmi_core] rmi_init_functions+0xb8/0x320 [rmi_core] rmi_driver_probe+0x31e/0xbf0 [rmi_core] one report per corrupted function object. The same unvalidated fn->irq_pos is used again by the set_bit() and irq_create_mapping() in rmi_create_function_irq(). Validate the counts before the function is allocated and fail the probe instead. The check is exact, not conservative: when both scans see the same table, the running interrupt total plus the new function's count is exactly what produced data->irq_count, so it never fires for a device that behaves. Placing the check before rmi_alloc_function() is deliberate. Since commit 58d42ec10b73 ("Input: rmi4 - refactor function allocation and registration") that function has already run device_initialize() and dev_set_name() on fn->dev, so disposing of a rejected function with kfree() would leak the name string and skip kobject cleanup on those trees, while put_device() on the same path broke on the pre-refactor trees that stable backports target: a kobject that device_initialize() never touched warns and its saturated refcount keeps the object allocated for good. Rejecting before the allocation needs no cleanup on any tree. Reproduced with an emulated RMI4 device driven over /dev/uhid, and again over dummy_hcd plus raw-gadget, on v6.12.69 booted slub_debug=FZPU and on v6.12.105 built with CONFIG_KASAN=y. After this change the same device gets rmi4_physical rmi4-03: F40: interrupt count changed between PDT scans (pos 1 + 6 > 1) rmi4_physical rmi4-03: Function creation failed with code -22. and a device that answers both scans consistently still probes normally. Fixes: 2b6a321da9a2 ("Input: synaptics-rmi4 - add support for Synaptics RMI4 devices") Cc: [email protected] Signed-off-by: Wei Jie Law <[email protected]> --- Changes in v3: - Validate before rmi_alloc_function() instead of freeing after it; no disposal is needed on any tree this way. See the cover letter. Changes in v2: - Dispose of the rejected function with kfree() instead of put_device(). See the cover letter. drivers/input/rmi4/rmi_driver.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/drivers/input/rmi4/rmi_driver.c b/drivers/input/rmi4/rmi_driver.c index 5d49a9021c7d..f66be55677a9 100644 --- a/drivers/input/rmi4/rmi_driver.c +++ b/drivers/input/rmi4/rmi_driver.c @@ -885,6 +885,25 @@ static int rmi_create_function(struct rmi_device *rmi_dev, rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n", pdt->function_number); + /* + * irq_mask[] was sized from the interrupt count collected by the + * earlier rmi_count_irqs() scan of the PDT, and irq[] holds + * RMI_FN_MAX_IRQS entries. Nothing guarantees that this scan sees + * the same table -- the PDT is read back from the device every + * time -- so a device that grows its interrupt counts between the + * two scans would push the set_bit() calls below past the end of + * the flexible array. Refuse the function instead, before anything + * is allocated for it. + */ + if (pdt->interrupt_source_count > RMI_FN_MAX_IRQS || + *current_irq_count + pdt->interrupt_source_count > data->irq_count) { + dev_err(dev, + "F%02X: interrupt count changed between PDT scans (pos %u + %u > %d)\n", + pdt->function_number, *current_irq_count, + pdt->interrupt_source_count, data->irq_count); + return -EINVAL; + } + fn = rmi_alloc_function(rmi_dev, pdt->function_number); if (!fn) { dev_err(dev, "Failed to allocate memory for F%02X\n", -- 2.43.0