gpiolib: acpi: Add robust bounds-checking and safe address handling
Subsystems (3)
v6 cover gpiolib: acpi: Add robust bounds-checking and safe address handling
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/[email protected]/
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/[email protected]/
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/[email protected]/
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/[email protected]/
Changes in v1:
- Initial submission of the refactored, hardened, and modularized ACPI GPIO core.
- See v1 series at:
https://lore.kernel.org/linux-gpio/[email protected]/
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(-)
Signature · 1 line
2.54.0
PATCH v6 1/2 gpiolib: acpi: Add robust bounds-checking for GPIO pin resources
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.
--- drivers/gpio/gpiolib-acpi-core.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +19 -4
@@ -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 ];
Signature · 1 line
2.54.0
PATCH v6 2/2 gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler
In reply to Marco ScardoviThe 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.
--- drivers/gpio/gpiolib-acpi-core.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +11 -4
@@ -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;
Signature · 1 line
2.54.0
Hi,
Quoted 6 lines · Marco Scardovi
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,
Bartosz Golaszewski
maintainerQuoted 10 lines · Mika Westerberg
Hi,
Quoted 6 lines · Marco Scardovi
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.
Quoted 6 lines
I'm fine with these now.
For both,
Acked-by Mika Westerberg <[email protected]>
Andy, Mika: do you want me to queue these directly for v7.2?
Bart
Andy Shevchenko
maintainerQuoted 9 lines · Bartosz Golaszewski
Quoted 7 lines · Mika Westerberg
Quoted 5 lines · Marco Scardovi
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.
Quoted 7 lines
Quoted 5 lines
I'm fine with these now.
For both,
Acked-by Mika Westerberg <[email protected]>Andy, Mika: do you want me to queue these directly for v7.2?
I believe there is no hurry, but I have no objections if you pull it in.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Bartosz Golaszewski
maintainerQuoted 12 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Mika Westerberg
Quoted 5 lines · Marco Scardovi
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.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
For both,
Acked-by Mika Westerberg <[email protected]>Andy, Mika: do you want me to queue these directly for v7.2?
I believe there is no hurry, but I have no objections if you pull it in.
Your call, if you thknk these should wait until v7.3, I'll leave them for now.
Bart
Andy Shevchenko
maintainerQuoted 16 lines · Bartosz Golaszewski
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Bartosz Golaszewski
Quoted 11 lines · Mika Westerberg
Quoted 5 lines · Marco Scardovi
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,
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Acked-by Mika Westerberg <[email protected]>Andy, Mika: do you want me to queue these directly for v7.2?
I believe there is no hurry, but I have no objections if you pull it in.
Your call, if you thknk these should wait until v7.3, I'll leave them for now.
For the full consensus we need Mika's opinion :-)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Bartosz Golaszewski
Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Bartosz Golaszewski
Quoted 12 lines · Mika Westerberg
Quoted 5 lines · Marco Scardovi
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 <[email protected]>
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Andy, Mika: do you want me to queue these directly for v7.2?
I believe there is no hurry, but I have no objections if you pull it in.
Your call, if you thknk these should wait until v7.3, I'll leave them for now.
For the full consensus we need Mika's opinion :-)
If we can get these for v7.2-rc2+ or so that would be good enough IMHO.
Bartosz Golaszewski
maintainerQuoted 22 lines · Mika Westerberg
Quoted 21 lines · Andy Shevchenko
Quoted 20 lines · Bartosz Golaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · Bartosz Golaszewski
Quoted 7 lines · Mika Westerberg
Quoted 5 lines · Marco Scardovi
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.
Quoted 5 lines
I'm fine with these now.
For both,
Acked-by Mika Westerberg <[email protected]>Andy, Mika: do you want me to queue these directly for v7.2?
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I believe there is no hurry, but I have no objections if you pull it in.
Your call, if you thknk these should wait until v7.3, I'll leave them for now.
For the full consensus we need Mika's opinion :-)
If we can get these for v7.2-rc2+ or so that would be good enough IMHO.
Then let me just queue them now then.
Bart
Bartosz Golaszewski
AppliedQuoted 5 lines · Marco Scardovi
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.
Quoted 8 lines
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.[...]
Applied, thanks!
[1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources
https://git.kernel.org/brgl/c/dece79032f529d2c9fdbf63a9f2fc32244722775 [2/2] gpiolib: acpi: Prevent out-of-bounds pin access in OperationRegion handler https://git.kernel.org/brgl/c/ae9f812df3149729643d27d2af488c112f62af9a
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>