Input: matrix_keypad - validate GPIO counts against matrix limits

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>
Link: https://patch.msgid.link/20261127145209.2884170-2-lee@kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
index 1101838..21c7df4 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;