Skip to contentSkip to conversation

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

PATCH Superseded Andy Shevchenko 3 participants 6 messages Jan 27 – Jan 28

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andy Shevchenko

author maintainer
#0 lore raw

PATCH v2 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 word 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 words.

---
v2: added a comment, named variables differently (Mika), rebased on top of accepted patch

 drivers/gpio/gpiolib-acpi-core.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi-core.c +17 -4
index d42f769eeb11..9627b3a9c7f3 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 word, shift;
 		bool found;
 
 		mutex_lock(&achip->conn_lock);
@@ -1158,10 +1159,22 @@ 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_ULL(i)));
-		else
-			*value |= (u64)gpiod_get_raw_value_cansleep(desc) << i;
+		/*
+		 * For the cases when OperationRegion() consists of more than
+		 * 64 bits calculate the word and bit shift to use that one to
+		 * access the value.
+		 */
+		word = i / 64;
+		shift = i % 64;
+
+		if (function == ACPI_WRITE) {
+			gpiod_set_raw_value_cansleep(desc, value[word] & BIT_ULL(shift));
+		} else {
+			if (gpiod_get_raw_value_cansleep(desc))
+				value[word] |= BIT_ULL(shift);
+			else
+				value[word] &= ~BIT_ULL(shift);
+		}
 	}
 
 out:
Signature · 1 line
2.50.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 14 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 word 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 words.

Quoted 1 line

Perfect, thanks!

author maintainer
#2 lore raw
Quoted 15 lines · Mika Westerberg
Quoted 14 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 word 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 words.

Quoted 5 lines
Quoted 1 line

Perfect, thanks!

Thank you!

Bart, since you picked up the patch in the same lines of code, feel free to take this one on top (I think it's your gpio/for-current branch).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Andy Shevchenko
Quoted 14 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 word 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 words.

Quoted 3 lines

---
v2: added a comment, named variables differently (Mika), rebased on top of accepted patch

Should I take it for v6.19? Fixes:? Cc: stable?

Bart

#3 lore raw
In reply to Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 17 lines · Mika Westerberg
Quoted 15 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 word 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 words.

Quoted 9 lines
Quoted 3 lines

Perfect, thanks!

Thank you!

Bart, since you picked up the patch in the same lines of code, feel free to take this one on top (I think it's your gpio/for-current branch).

Don't you want a Fixes: and Cc: stable tags?

Bart

author maintainer
#4 lore raw
Quoted 22 lines · Bartosz Golaszewski
Quoted 20 lines · Andy Shevchenko
Quoted 18 lines · Mika Westerberg
Quoted 14 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 word 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 words.

Quoted 1 line

Perfect, thanks!

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Thank you!

Bart, since you picked up the patch in the same lines of code, feel free to take this one on top (I think it's your gpio/for-current branch).

Don't you want a Fixes: and Cc: stable tags?

Okay, I will add them in v3, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko