Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling
@ 2026-06-10 15:42 Marco Scardovi
  2026-06-10 15:42 ` [PATCH v6 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources Marco Scardovi
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Marco Scardovi @ 2026-06-10 15:42 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Linus Walleij,
	Bartosz Golaszewski
  Cc: linux-gpio, linux-acpi, linux-kernel

Hi all,

The series adds explicit bounds checking for GPIO pin accesses and
ensures safe handling of ACPI addresses in OperationRegion handlers,
without referring to truncation or wrap-around behavior, which does
not apply.

Changes in v6:
- Rebased onto next-20260609.
- Corrected commit messages to describe bounds checking and
  safe ACPI GPIO address handling.
- Removed references to truncation or wrap-around behavior.
- No code changes compared to v5.

Changes in v5:
- Changed the types of length and loop counter 'i' to unsigned int in
  acpi_gpio_adr_space_handler() to prevent signedness mismatches.
- Replaced the min() macro in the length calculation with an
  overflow-safe check (bits > agpio->pin_table_length - pin_index)
  to prevent integer overflow.
- See v5 series at:
  https://lore.kernel.org/linux-gpio/20260602113529.52570-1-scardracs@disroot.org/

Changes in v4:
- Dropped the connection resource leak fix to keep the series focused
  strictly on bounds-checking and address validation.
- See v4 series at:
  https://lore.kernel.org/linux-gpio/20260531120816.17255-1-scardracs@disroot.org/

Changes in v3:
- Fixed a global over-cleanup bug in the error path of the OpRegion
  handler.
- See v3 series at:
  https://lore.kernel.org/linux-gpio/20260520074955.55443-1-mscardovi95@gmail.com/

Changes in v2:
- Split the original single patch into two distinct patches: one for
  bounds checking in the event/wakeup path, and one for safe
  handling of ACPI addresses.
- See v2 series at:
  https://lore.kernel.org/linux-gpio/20260519070039.9280-1-mscardovi95@gmail.com/

Changes in v1:
- Initial submission of the refactored, hardened, and modularized
  ACPI GPIO core.
- See v1 series at:
  https://lore.kernel.org/linux-gpio/20260518075357.112584-1-mscardovi95@gmail.com/

Marco Scardovi (2):
  gpiolib: acpi: Add robust bounds-checking for GPIO pin resources
  gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion
    handler

 drivers/gpio/gpiolib-acpi-core.c | 38 +++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 8 deletions(-)

-- 
2.54.0


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

* [PATCH v6 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources
  2026-06-10 15:42 [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Marco Scardovi
@ 2026-06-10 15:42 ` Marco Scardovi
  2026-06-10 15:42 ` [PATCH v6 2/2] gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler Marco Scardovi
  2026-06-12  8:52 ` [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Mika Westerberg
  2 siblings, 0 replies; 4+ messages in thread
From: Marco Scardovi @ 2026-06-10 15:42 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Linus Walleij,
	Bartosz Golaszewski
  Cc: linux-gpio, linux-acpi, linux-kernel

Ensure that GPIO pin resource arrays are safely bounded before accessing
indices. Add explicit bounds checking in acpi_request_own_gpiod(),
acpi_gpio_irq_is_wake(), and acpi_gpiochip_alloc_event() to prevent
out-of-bounds array reads if the ACPI namespace provides malformed or empty
pin tables.

This change addresses potential safety issues arising from
inconsistent or invalid ACPI pin tables. It does not alter functional
behavior in well-formed tables.

Assisted-by: Antigravity:gemini-3.5-flash
Signed-off-by: Marco Scardovi <scardracs@disroot.org>
---
 drivers/gpio/gpiolib-acpi-core.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/gpio/gpiolib-acpi-core.c b/drivers/gpio/gpiolib-acpi-core.c
index 1a762a2988b7..b09f89832890 100644
--- a/drivers/gpio/gpiolib-acpi-core.c
+++ b/drivers/gpio/gpiolib-acpi-core.c
@@ -316,10 +316,17 @@ static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
 						unsigned int index,
 						const char *label)
 {
-	int polarity = GPIO_ACTIVE_HIGH;
-	enum gpiod_flags flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
-	unsigned int pin = agpio->pin_table[index];
+	enum gpiod_flags flags;
 	struct gpio_desc *desc;
+	unsigned int pin;
+	int polarity;
+
+	if (index >= agpio->pin_table_length)
+		return ERR_PTR(-EINVAL);
+
+	pin = agpio->pin_table[index];
+	polarity = GPIO_ACTIVE_HIGH;
+	flags = acpi_gpio_to_gpiod_flags(agpio, polarity);
 
 	desc = gpiochip_request_own_desc(chip, pin, label, polarity, flags);
 	if (IS_ERR(desc))
@@ -333,7 +340,12 @@ static struct gpio_desc *acpi_request_own_gpiod(struct gpio_chip *chip,
 static bool acpi_gpio_irq_is_wake(struct device *parent,
 				  const struct acpi_resource_gpio *agpio)
 {
-	unsigned int pin = agpio->pin_table[0];
+	unsigned int pin;
+
+	if (agpio->pin_table_length == 0)
+		return false;
+
+	pin = agpio->pin_table[0];
 
 	if (agpio->wake_capable != ACPI_WAKE_CAPABLE)
 		return false;
@@ -363,6 +375,9 @@ static acpi_status acpi_gpiochip_alloc_event(struct acpi_resource *ares,
 	if (!acpi_gpio_get_irq_resource(ares, &agpio))
 		return AE_OK;
 
+	if (agpio->pin_table_length == 0)
+		return AE_OK;
+
 	handle = ACPI_HANDLE(chip->parent);
 	pin = agpio->pin_table[0];
 
-- 
2.54.0


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

* [PATCH v6 2/2] gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler
  2026-06-10 15:42 [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Marco Scardovi
  2026-06-10 15:42 ` [PATCH v6 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources Marco Scardovi
@ 2026-06-10 15:42 ` Marco Scardovi
  2026-06-12  8:52 ` [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Mika Westerberg
  2 siblings, 0 replies; 4+ messages in thread
From: Marco Scardovi @ 2026-06-10 15:42 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Linus Walleij,
	Bartosz Golaszewski
  Cc: linux-gpio, linux-acpi, linux-kernel

The ACPI GPIO OperationRegion handler receives pin offsets as a
64-bit address. Previously, this value could be assigned to a pin index
without validation, potentially causing out-of-bounds access if
the ACPI table provides an invalid offset.

This patch explicitly checks that the 64-bit address is less than
agpio->pin_table_length before using it, returning AE_BAD_PARAMETER
if the check fails. Additionally, it makes the length calculation
overflow-safe and ensures proper unsigned types for loop counters.

This corrects the commit message from v5 to accurately reflect the
underlying issue, removing references to truncation or wrap-around,
which do not occur in ACPICA.

Assisted-by: Antigravity:gemini-3.5-flash
Signed-off-by: Marco Scardovi <scardracs@disroot.org>
---
 drivers/gpio/gpiolib-acpi-core.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/gpio/gpiolib-acpi-core.c b/drivers/gpio/gpiolib-acpi-core.c
index b09f89832890..220f0ac4204e 100644
--- a/drivers/gpio/gpiolib-acpi-core.c
+++ b/drivers/gpio/gpiolib-acpi-core.c
@@ -1098,10 +1098,10 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 	struct gpio_chip *chip = achip->chip;
 	struct acpi_resource_gpio *agpio;
 	struct acpi_resource *ares;
-	u16 pin_index = address;
+	unsigned int length;
 	acpi_status status;
-	int length;
-	int i;
+	unsigned int i;
+	u16 pin_index;
 
 	status = acpi_buffer_to_resource(achip->conn_info.connection,
 					 achip->conn_info.length, &ares);
@@ -1121,7 +1121,14 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		return AE_BAD_PARAMETER;
 	}
 
-	length = min(agpio->pin_table_length, pin_index + bits);
+	/* address represents GPIO pin index in connection table */
+	if (address >= agpio->pin_table_length) {
+		ACPI_FREE(ares);
+		return AE_BAD_PARAMETER;
+	}
+
+	pin_index = address;
+	length = min_t(unsigned int, agpio->pin_table_length, pin_index + bits);
 	for (i = pin_index; i < length; ++i) {
 		unsigned int pin = agpio->pin_table[i];
 		struct acpi_gpio_connection *conn;
-- 
2.54.0


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

* Re: [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling
  2026-06-10 15:42 [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Marco Scardovi
  2026-06-10 15:42 ` [PATCH v6 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources Marco Scardovi
  2026-06-10 15:42 ` [PATCH v6 2/2] gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler Marco Scardovi
@ 2026-06-12  8:52 ` Mika Westerberg
  2 siblings, 0 replies; 4+ messages in thread
From: Mika Westerberg @ 2026-06-12  8:52 UTC (permalink / raw)
  To: Marco Scardovi
  Cc: Mika Westerberg, Andy Shevchenko, Linus Walleij,
	Bartosz Golaszewski, linux-gpio, linux-acpi, linux-kernel

Hi,

On Wed, Jun 10, 2026 at 05:42:02PM +0200, Marco Scardovi wrote:
> Hi all,
> 
> The series adds explicit bounds checking for GPIO pin accesses and
> ensures safe handling of ACPI addresses in OperationRegion handlers,
> without referring to truncation or wrap-around behavior, which does
> not apply.

I'm fine with these now.

For both,

Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com>

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

end of thread, other threads:[~2026-06-12  8:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-10 15:42 [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Marco Scardovi
2026-06-10 15:42 ` [PATCH v6 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources Marco Scardovi
2026-06-10 15:42 ` [PATCH v6 2/2] gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler Marco Scardovi
2026-06-12  8:52 ` [PATCH v6 0/2] gpiolib: acpi: Add robust bounds-checking and safe address handling Mika Westerberg

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