Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Input: Fix GPIO bounds, work teardown, and suspend bugs
@ 2026-11-27 14:51 Lee Jones
  2026-11-27 14:51 ` [PATCH v2 1/3] Input: matrix_keypad - validate GPIO counts against matrix limits Lee Jones
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Lee Jones @ 2026-11-27 14:51 UTC (permalink / raw)
  To: lee
  Cc: linux-kernel, Ronald Tschalär, Andy Shevchenko,
	Arnd Bergmann, Dmitry Torokhov, Eric Miao, Linus Walleij,
	linux-input, Marek Vasut, Pawel Zalewski, Robert Jarzmik,
	Russell King, Trilok Soni

These issues were reported by Sashiko.

This v2 series contains the remaining three patches from the initial
Input Sashiko bug-fix set, rebased onto input/next now that the other
seven patches have been applied.

v1 => v2:
 - Drop the seven patches already applied to input/next.
 - Drop the redundant zero GPIO count check and stable tag on matrix_keypad GPIO bounds validation, per Dmitry Torokhov's review.
 - Switch applespi removal from cancel_work_sync() to disable_work_sync(), per Dmitry Torokhov's review.
 - Update the Fixes tag on the matrix_keypad sleep fix.

Cc: "Ronald Tschalär" <ronald@innovation.ch>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Eric Miao <eric.y.miao@gmail.com>
Cc: Linus Walleij <linusw@kernel.org>
Cc: linux-input@vger.kernel.org
Cc: Marek Vasut <marek.vasut@gmail.com>
Cc: Pawel Zalewski <pzalewski@thegoodpenguin.co.uk>
Cc: Robert Jarzmik <robert.jarzmik@free.fr>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Trilok Soni <soni.trilok@gmail.com>

Lee Jones (3):
  Input: matrix_keypad - validate GPIO counts against matrix limits
  Input: applespi - disable touchpad registration work on removal
  Input: matrix_keypad - only stop and start active device across sleep

 drivers/input/keyboard/applespi.c      |  1 +
 drivers/input/keyboard/matrix_keypad.c | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/3] Input: matrix_keypad - validate GPIO counts against matrix limits
  2026-11-27 14:51 [PATCH v2 0/3] Input: Fix GPIO bounds, work teardown, and suspend bugs Lee Jones
@ 2026-11-27 14:51 ` Lee Jones
  2026-11-27 14:51 ` [PATCH v2 2/3] Input: applespi - disable touchpad registration work on removal Lee Jones
  2026-11-27 14:51 ` [PATCH v2 3/3] Input: matrix_keypad - only stop and start active device across sleep Lee Jones
  2 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-11-27 14:51 UTC (permalink / raw)
  To: lee, Dmitry Torokhov, Marek Vasut, Linus Walleij, Arnd Bergmann,
	linux-input, linux-kernel

matrix_keypad_init_gpio() obtains the row and column GPIO counts via
gpiod_count() and only checks for negative error codes before storing
them in keypad->num_row_gpios and keypad->num_col_gpios.

If firmware supplies more than MATRIX_MAX_ROWS (32) row GPIOs or
MATRIX_MAX_COLS (32) column GPIOs, the setup loops write past the fixed
row_gpios, col_gpios, and row_irqs arrays inside struct matrix_keypad,
and matrix_keypad_scan() overflows the stack-allocated new_state array.

Reject counts exceeding MATRIX_MAX_ROWS or MATRIX_MAX_COLS, and size
col_gpios with MATRIX_MAX_COLS for consistency.

Fixes: 584bf366f8fa ("Input: matrix_keypad - switch to gpiod API and generic device properties")
Closes: https://sashiko.dev/#/bug/linux-ed4b89af-6c76-4e5f-b140-fb2b107b12d0
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
 - Drop the redundant zero GPIO count check, as matrix_keypad_build_keymap() already validates positive dimensions, per Dmitry Torokhov's review.
 - Drop the stable tag, per Dmitry Torokhov's review.

 drivers/input/keyboard/matrix_keypad.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
index 11018382e84e..21c7df4e2e7b 100644
--- a/drivers/input/keyboard/matrix_keypad.c
+++ b/drivers/input/keyboard/matrix_keypad.c
@@ -33,7 +33,7 @@ struct matrix_keypad {
 	struct gpio_desc *row_gpios[MATRIX_MAX_ROWS];
 	unsigned int num_row_gpios;
 
-	struct gpio_desc *col_gpios[MATRIX_MAX_ROWS];
+	struct gpio_desc *col_gpios[MATRIX_MAX_COLS];
 	unsigned int num_col_gpios;
 
 	unsigned int row_irqs[MATRIX_MAX_ROWS];
@@ -307,6 +307,11 @@ static int matrix_keypad_init_gpio(struct platform_device *pdev,
 		return -EINVAL;
 	}
 
+	if (nrow > MATRIX_MAX_ROWS || ncol > MATRIX_MAX_COLS) {
+		dev_err(&pdev->dev, "too many row or column GPIOs\n");
+		return -EINVAL;
+	}
+
 	keypad->num_row_gpios = nrow;
 	keypad->num_col_gpios = ncol;
 
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] Input: applespi - disable touchpad registration work on removal
  2026-11-27 14:51 [PATCH v2 0/3] Input: Fix GPIO bounds, work teardown, and suspend bugs Lee Jones
  2026-11-27 14:51 ` [PATCH v2 1/3] Input: matrix_keypad - validate GPIO counts against matrix limits Lee Jones
@ 2026-11-27 14:51 ` Lee Jones
  2026-11-27 14:51 ` [PATCH v2 3/3] Input: matrix_keypad - only stop and start active device across sleep Lee Jones
  2 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-11-27 14:51 UTC (permalink / raw)
  To: lee, Dmitry Torokhov, Pawel Zalewski, Andy Shevchenko,
	Ronald Tschalär, linux-input, linux-kernel
  Cc: stable

applespi_probe() allocates its driver state with devm_kzalloc_obj() and
initiates an asynchronous touchpad information query whose completion
schedules applespi->work to allocate and register the managed touchpad
input device in applespi_worker().

applespi_remove() disables the ACPI GPE and drains in-flight SPI writes
and reads, but never disables applespi->work.  If a touchpad info
response scheduled applespi->work just before applespi_drain_reads()
completed, applespi_worker() can run during or after devres teardown,
corrupting the device resource list via devm_input_allocate_device() and
dereferencing freed driver data.

Disable applespi->work synchronously after applespi_drain_reads(), once
no further SPI responses can schedule it.

Fixes: 038b1a05eae6 ("Input: add Apple SPI keyboard and trackpad driver")
Closes: https://sashiko.dev/#/bug/linux-bb2fd773-6c20-468c-a092-6814f008077f
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
 - Use disable_work_sync() instead of cancel_work_sync() in applespi_remove(), per Dmitry Torokhov's review.

 drivers/input/keyboard/applespi.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/input/keyboard/applespi.c b/drivers/input/keyboard/applespi.c
index 1d059aff1f1b..1e63719dec4b 100644
--- a/drivers/input/keyboard/applespi.c
+++ b/drivers/input/keyboard/applespi.c
@@ -1822,6 +1822,7 @@ static void applespi_remove(struct spi_device *spi)
 	device_wakeup_disable(&spi->dev);
 
 	applespi_drain_reads(applespi);
+	disable_work_sync(&applespi->work);
 
 	debugfs_remove_recursive(applespi->debugfs_root);
 }
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] Input: matrix_keypad - only stop and start active device across sleep
  2026-11-27 14:51 [PATCH v2 0/3] Input: Fix GPIO bounds, work teardown, and suspend bugs Lee Jones
  2026-11-27 14:51 ` [PATCH v2 1/3] Input: matrix_keypad - validate GPIO counts against matrix limits Lee Jones
  2026-11-27 14:51 ` [PATCH v2 2/3] Input: applespi - disable touchpad registration work on removal Lee Jones
@ 2026-11-27 14:51 ` Lee Jones
  2 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-11-27 14:51 UTC (permalink / raw)
  To: lee, Dmitry Torokhov, Marek Vasut, Russell King, Uli Luckas,
	Robert Jarzmik, linux-input, linux-kernel
  Cc: stable

matrix_keypad_suspend() and matrix_keypad_resume() unconditionally call
matrix_keypad_stop() and matrix_keypad_start() without holding
input_dev->mutex or checking whether the input device is active.

When the keypad has no open users (or is inhibited), row interrupts are
already disabled and keypad->stopped is true.  Suspending calls
disable_row_irqs() a second time, unbalancing the IRQ disable depth, and
resuming clears keypad->stopped and schedules keypad->work.  If the
device is subsequently unbound while still unopened, Input core skips
the close callback (matrix_keypad_stop()) during unregistration, leaving
keypad->work pending or running after devres frees struct matrix_keypad.

Acquire input_dev->mutex in both sleep callbacks and only stop or start
scanning when input_device_enabled() is true.

Fixes: bab7614d6d1b ("Input: add support for generic GPIO-based matrix keypad")
Closes: https://sashiko.dev/#/bug/linux-340f2a74-becb-415d-b1c2-6da8f05a8bd8
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
v1 => v2:
 - Update the Fixes tag to commit bab7614d6d1b ("Input: add support for generic GPIO-based matrix keypad").

 drivers/input/keyboard/matrix_keypad.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
index 21c7df4e2e7b..370c08e6e264 100644
--- a/drivers/input/keyboard/matrix_keypad.c
+++ b/drivers/input/keyboard/matrix_keypad.c
@@ -268,7 +268,10 @@ static int matrix_keypad_suspend(struct device *dev)
 	struct platform_device *pdev = to_platform_device(dev);
 	struct matrix_keypad *keypad = platform_get_drvdata(pdev);
 
-	matrix_keypad_stop(keypad->input_dev);
+	guard(mutex)(&keypad->input_dev->mutex);
+
+	if (input_device_enabled(keypad->input_dev))
+		matrix_keypad_stop(keypad->input_dev);
 
 	if (device_may_wakeup(&pdev->dev))
 		matrix_keypad_enable_wakeup(keypad);
@@ -281,10 +284,13 @@ static int matrix_keypad_resume(struct device *dev)
 	struct platform_device *pdev = to_platform_device(dev);
 	struct matrix_keypad *keypad = platform_get_drvdata(pdev);
 
+	guard(mutex)(&keypad->input_dev->mutex);
+
 	if (device_may_wakeup(&pdev->dev))
 		matrix_keypad_disable_wakeup(keypad);
 
-	matrix_keypad_start(keypad->input_dev);
+	if (input_device_enabled(keypad->input_dev))
+		matrix_keypad_start(keypad->input_dev);
 
 	return 0;
 }
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-27 14:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 14:51 [PATCH v2 0/3] Input: Fix GPIO bounds, work teardown, and suspend bugs Lee Jones
2026-11-27 14:51 ` [PATCH v2 1/3] Input: matrix_keypad - validate GPIO counts against matrix limits Lee Jones
2026-11-27 14:51 ` [PATCH v2 2/3] Input: applespi - disable touchpad registration work on removal Lee Jones
2026-11-27 14:51 ` [PATCH v2 3/3] Input: matrix_keypad - only stop and start active device across sleep Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox