Skip to contentSkip to conversation

gpiolib: acpi: Fix potential out-of-boundary left shift

PATCH Superseded Andy Shevchenko 2 participants 2 messages Jan 26 – Jan 27

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andy Shevchenko

author maintainer
#0 lore raw

PATCH 1/1 gpiolib: acpi: Fix potential out-of-boundary left shift

GPIO Address Space handler gets a pointer to the in or out value. This value is supposed to be at least 64-bit, but it's not limited to be exactly 64-bit. When ACPI tables are being parsed, for the bigger Connection():s ACPICA creates a Buffer instead of regular Integer object. The Buffer exists as long as Namespace holds the certain Connection(). Hence we can access the necessary bits without worrying. On the other hand, the left shift used in the code is limited by 31 (on 32-bit platforms) and otherwise considered to be Undefined Behaviour. Also the code uses only the first 64-bit
address for the value, and anything bigger than 63 will be also
subject to UB. Fix all this by modifying the code to correctly set or clear the respective bit in the bitmap constructed of 64-bit values.

---
 drivers/gpio/gpiolib-acpi-core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +12 -4
index 83dd227dbbec..da0ab749b4dc 100644
@@ -1104,6 +1104,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		unsigned int pin = agpio->pin_table[i];
 		struct acpi_gpio_connection *conn;
 		struct gpio_desc *desc;
+		u16 vala, valo;
 		bool found;
 
 		mutex_lock(&achip->conn_lock);
@@ -1158,10 +1159,17 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 
 		mutex_unlock(&achip->conn_lock);
 
-		if (function == ACPI_WRITE)
-			gpiod_set_raw_value_cansleep(desc, !!(*value & BIT(i)));
-		else
-			*value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
+		vala = i / 64;
+		valo = i % 64;
+
+		if (function == ACPI_WRITE) {
+			gpiod_set_raw_value_cansleep(desc, value[vala] & BIT_ULL(valo));
+		} else {
+			if (gpiod_get_raw_value_cansleep(desc))
+				value[vala] |= BIT_ULL(valo);
+			else
+				value[vala] &= ~BIT_ULL(valo);
+		}
 	}
 
 out:
Signature · 1 line
2.50.1
#1 lore raw
Quoted 10 lines · Andy Shevchenko

GPIO Address Space handler gets a pointer to the in or out value. This value is supposed to be at least 64-bit, but it's not limited to be exactly 64-bit. When ACPI tables are being parsed, for the bigger Connection():s ACPICA creates a Buffer instead of regular Integer object. The Buffer exists as long as Namespace holds the certain Connection(). Hence we can access the necessary bits without worrying. On the other hand, the left shift used in the code is limited by 31 (on 32-bit platforms) and otherwise considered to be Undefined Behaviour. Also the code uses only the first 64-bit
address for the value, and anything bigger than 63 will be also

                                                 ^^^^^^^

There is some extra whitespace above.

Quoted 13 lines

subject to UB. Fix all this by modifying the code to correctly set or clear the respective bit in the bitmap constructed of 64-bit values.

---
 drivers/gpio/gpiolib-acpi-core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +0 -0
index 83dd227dbbec..da0ab749b4dc 100644

drivers/gpio/gpiolib-acpi-core.c acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, · line 1107

Quoted 4 lines
drivers/gpio/gpiolib-acpi-core.c +1 -0
@@ -1104,6 +1104,7 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 		unsigned int pin = agpio->pin_table[i];
 		struct acpi_gpio_connection *conn;
 		struct gpio_desc *desc;
+		u16 vala, valo;

Perhaps bit more descriptive names.

And while at it, add comment why this is needed.

Quoted 27 lines
drivers/gpio/gpiolib-acpi-core.c +11 -4
 		bool found;
 
 		mutex_lock(&achip->conn_lock);
@@ -1158,10 +1159,17 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address,
 
 		mutex_unlock(&achip->conn_lock);
 
-		if (function == ACPI_WRITE)
-			gpiod_set_raw_value_cansleep(desc, !!(*value & BIT(i)));
-		else
-			*value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
+		vala = i / 64;
+		valo = i % 64;
+
+		if (function == ACPI_WRITE) {
+			gpiod_set_raw_value_cansleep(desc, value[vala] & BIT_ULL(valo));
+		} else {
+			if (gpiod_get_raw_value_cansleep(desc))
+				value[vala] |= BIT_ULL(valo);
+			else
+				value[vala] &= ~BIT_ULL(valo);
+		}
 	}
 
 out:
Signature · 1 line
2.50.1