Skip to contentSkip to conversation

ACPI: scan: Make acpi_bus_get_device() clear the adev ptr on error

PATCH In review Hans de Goede 3 participants 6 messages Jan 15, 2021 – Jan 19, 2021 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI: scan: Make acpi_bus_get_device() clear the adev ptr on error

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

---
 drivers/acpi/scan.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/acpi/scan.c +2 -0
index c575c9b67f76..c53e88deee1d 100644
@@ -585,6 +585,8 @@ static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
 	if (!device)
 		return -EINVAL;
 
+	*device = NULL;
+
 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
 				    (void **)device, callback);
 	if (ACPI_FAILURE(status) || !*device) {
Signature · 1 line
2.28.0
#1 lore raw
Quoted 11 lines · Hans de Goede

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

This should fix the crash reported by Pierre-Louis, so let me apply it instead of the two debug changes posted by me
(https://lore.kernel.org/linux-acpi/[email protected]/T/#md5add2fe554a30e3a929d87a66b435f4cc8bf628).

Pierre-Louis, can you please double check that the issue goes away with this patch applied?

Quoted 20 lines
---
 drivers/acpi/scan.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/acpi/scan.c +2 -1
index c575c9b67f76..c53e88deee1d 100644
@@ -585,6 +585,8 @@ static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
        if (!device)
                return -EINVAL;
 
+       *device = NULL;
+
        status = acpi_get_data_full(handle, acpi_scan_drop_device,
                                    (void **)device, callback);
        if (ACPI_FAILURE(status) || !*device) {
--

2.28.0

#2 lore raw

Hi,

Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Hans de Goede

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

Quoted 3 lines
Quoted 1 line

This should fix the crash reported by Pierre-Louis,

Ack, sounds good.

Quoted 3 lines

so let me apply it
instead of the two debug changes posted by me
(https://lore.kernel.org/linux-acpi/[email protected]/T/#md5add2fe554a30e3a929d87a66b435f4cc8bf628).

Note we should still fix the USB case, my patch will make failure there more obvious, but the code can theoretically still dereference a NULL pointer in drivers/usb/core/usb-acpi.c.

And we probably also want this change:

drivers/acpi/scan.c +2 -1
@@ -1867,7 +1867,8 @@ static u32 acpi_scan_check_dep(acpi_hand
 	 * 2. ACPI nodes describing USB ports.
 	 * Still, checking for _HID catches more then just these cases ...
 	 */
-	if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID"))
+	if (!acpi_has_method(handle, "_DEP") || acpi_has_method(handle, "_ADR")
+	    || !acpi_has_method(handle, "_HID"))
 		return 0;
 
 	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);

To reduce the amount of work we do checking _DEP-s.

If you want I can submit patches for both ?

Regards,

Hans

Quoted 25 lines

Pierre-Louis, can you please double check that the issue goes away with this patch applied?

Quoted 20 lines
---
 drivers/acpi/scan.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/acpi/scan.c +2 -1
index c575c9b67f76..c53e88deee1d 100644
@@ -585,6 +585,8 @@ static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
        if (!device)
                return -EINVAL;
 
+       *device = NULL;
+
        status = acpi_get_data_full(handle, acpi_scan_drop_device,
                                    (void **)device, callback);
        if (ACPI_FAILURE(status) || !*device) {
--

2.28.0

#3 lore raw
Quoted 21 lines · Hans de Goede

Hi,

Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Hans de Goede

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

Quoted 3 lines
Quoted 1 line

This should fix the crash reported by Pierre-Louis,

Ack, sounds good.

Quoted 7 lines
Quoted 3 lines

so let me apply it
instead of the two debug changes posted by me
(https://lore.kernel.org/linux-acpi/[email protected]/T/#md5add2fe554a30e3a929d87a66b435f4cc8bf628).

Note we should still fix the USB case, my patch will make failure there more obvious, but the code can theoretically still dereference a NULL pointer in drivers/usb/core/usb-acpi.c.

Because usb_acpi_find_port() checks the acpi_device pointer passed to it against NULL, we're safe here as well.

Quoted 15 lines

And we probably also want this change:

drivers/acpi/scan.c +2 -1
@@ -1867,7 +1867,8 @@ static u32 acpi_scan_check_dep(acpi_hand
         * 2. ACPI nodes describing USB ports.
         * Still, checking for _HID catches more then just these cases ...
         */
-       if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID"))
+       if (!acpi_has_method(handle, "_DEP") || acpi_has_method(handle, "_ADR")
+           || !acpi_has_method(handle, "_HID"))
                return 0;
 
        status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
Quoted 1 line

To reduce the amount of work we do checking _DEP-s.

So I was thinking about that, but I'd leave it as is unless we have a use case in which looking for _ADR is really necessary.

In the majority of cases the device objects with both _ADR and _HID really are _HID devices and _ADR returns 0. Of course, that could be treated as a special case, but unless it is necessary to add a check for this special case, I'd rather avoid doing that.

#4 lore raw

Hi,

Quoted 26 lines · Rafael J. Wysocki
Quoted 25 lines · Hans de Goede

Hi,

Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Hans de Goede

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

Quoted 3 lines
Quoted 1 line

This should fix the crash reported by Pierre-Louis,

Ack, sounds good.

Quoted 3 lines

so let me apply it
instead of the two debug changes posted by me
(https://lore.kernel.org/linux-acpi/[email protected]/T/#md5add2fe554a30e3a929d87a66b435f4cc8bf628).

Quoted 6 lines
Quoted 3 lines

Note we should still fix the USB case, my patch will make failure there more obvious, but the code can theoretically still dereference a NULL pointer in drivers/usb/core/usb-acpi.c.

Because usb_acpi_find_port() checks the acpi_device pointer passed to it against NULL, we're safe here as well.

Ah, good :)

Quoted 20 lines
Quoted 15 lines

And we probably also want this change:

drivers/acpi/scan.c +2 -1
@@ -1867,7 +1867,8 @@ static u32 acpi_scan_check_dep(acpi_hand
         * 2. ACPI nodes describing USB ports.
         * Still, checking for _HID catches more then just these cases ...
         */
-       if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID"))
+       if (!acpi_has_method(handle, "_DEP") || acpi_has_method(handle, "_ADR")
+           || !acpi_has_method(handle, "_HID"))
                return 0;
 
        status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
Quoted 1 line

To reduce the amount of work we do checking _DEP-s.

So I was thinking about that, but I'd leave it as is unless we have a use case in which looking for _ADR is really necessary.

Quoted 4 lines

In the majority of cases the device objects with both _ADR and _HID really are _HID devices and _ADR returns 0. Of course, that could be treated as a special case, but unless it is necessary to add a check for this special case, I'd rather avoid doing that.

Ok, that works for me.

Regards,

Hans

#5 lore raw
In reply to Rafael J. Wysocki
Quoted 11 lines · Rafael J. Wysocki
Quoted 10 lines · Hans de Goede

Set the acpi_device pointer which acpi_bus_get_device() returns-by-reference to NULL on error.

We've recently had 2 cases where callers of acpi_bus_get_device() did not properly error check the return value, using the returned-by-reference acpi_device pointer blindly, set it to NULL so that this will lead to an immediate oops, rather then following a pointer to who knows what.

Quoted 8 lines
Quoted 1 line

This should fix the crash reported by Pierre-Louis, so let me apply it instead of the two debug changes posted by me
(https://lore.kernel.org/linux-acpi/[email protected]/T/#md5add2fe554a30e3a929d87a66b435f4cc8bf628).

Pierre-Louis, can you please double check that the issue goes away with this patch applied?

yep, tested twice

Thanks Hans and Rafael for your work, much appreciated.

Quoted 20 lines
Quoted 20 lines
---
  drivers/acpi/scan.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/acpi/scan.c +2 -1
index c575c9b67f76..c53e88deee1d 100644
@@ -585,6 +585,8 @@ static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
         if (!device)
                 return -EINVAL;
 
+       *device = NULL;
+
         status = acpi_get_data_full(handle, acpi_scan_drop_device,
                                     (void **)device, callback);
         if (ACPI_FAILURE(status) || !*device) {
--

2.28.0