Skip to contentSkip to conversation

platform/x86: lg-laptop: Fix possible NULL pointer derefence

PATCH In review Arnd Bergmann 3 participants 3 messages Sep 20, 2021 – Sep 21, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: lg-laptop: Fix possible NULL pointer derefence

From: Arnd Bergmann <[email protected]>

When CONFIG_DMI is disabled, dmi_get_system_info() returns a NULL pointer, which is now caught by a warning:

In function 'strlen',

    inlined from 'acpi_add.part.0' at drivers/platform/x86/lg-laptop.c:658:6:
include/linux/fortify-string.h:25:33: error: argument 1 null where non-null expected [-Werror=nonnull]
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~
drivers/platform/x86/lg-laptop.c: In function 'acpi_add.part.0':
include/linux/fortify-string.h:25:33: note: in a call to built-in function '__builtin_strlen'
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~

The code in there does not appear essential, so an explicit NULL check should be sufficient. The string is also printed to the console, but printk() is able to handle NULL pointer arguments gracefully.

Fixes 8983bfd58d61 ("platform/x86: lg-laptop: Support for battery charge limit on newer models")
---
 drivers/platform/x86/lg-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/lg-laptop.c +1 -1
index 3e520d5bca07..88b551caeaaf 100644
@@ -655,7 +655,7 @@ static int acpi_add(struct acpi_device *device)
 		goto out_platform_registered;
 	}
 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
-	if (strlen(product) > 4)
+	if (product && strlen(product) > 4)
 		switch (product[4]) {
 		case '5':
 		case '6':
Signature · 1 line
2.29.2
#1 lore raw
Quoted 37 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When CONFIG_DMI is disabled, dmi_get_system_info() returns a NULL pointer, which is now caught by a warning:

In function 'strlen',

    inlined from 'acpi_add.part.0' at drivers/platform/x86/lg-laptop.c:658:6:
include/linux/fortify-string.h:25:33: error: argument 1 null where non-null expected [-Werror=nonnull]
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~
drivers/platform/x86/lg-laptop.c: In function 'acpi_add.part.0':
include/linux/fortify-string.h:25:33: note: in a call to built-in function '__builtin_strlen'
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~

The code in there does not appear essential, so an explicit NULL check should be sufficient. The string is also printed to the console, but printk() is able to handle NULL pointer arguments gracefully.

Fixes 8983bfd58d61 ("platform/x86: lg-laptop: Support for battery charge limit on newer models")
---
 drivers/platform/x86/lg-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/lg-laptop.c +0 -0
index 3e520d5bca07..88b551caeaaf 100644

drivers/platform/x86/lg-laptop.c static int acpi_add(struct acpi_device *device) · line 658

Quoted 5 lines
drivers/platform/x86/lg-laptop.c +1 -1
@@ -655,7 +655,7 @@ static int acpi_add(struct acpi_device *device)
 		goto out_platform_registered;
 	}
 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
-	if (strlen(product) > 4)
+	if (product && strlen(product) > 4)

seems appropriate.

--mark

Quoted 6 lines
drivers/platform/x86/lg-laptop.c +0 -0
 		switch (product[4]) {
 		case '5':
 		case '6':
Signature · 1 line
2.29.2
#2 lore raw
In reply to Arnd Bergmann

Hi Arnd,

Quoted 21 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

When CONFIG_DMI is disabled, dmi_get_system_info() returns a NULL pointer, which is now caught by a warning:

In function 'strlen',

    inlined from 'acpi_add.part.0' at drivers/platform/x86/lg-laptop.c:658:6:
include/linux/fortify-string.h:25:33: error: argument 1 null where non-null expected [-Werror=nonnull]
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~
drivers/platform/x86/lg-laptop.c: In function 'acpi_add.part.0':
include/linux/fortify-string.h:25:33: note: in a call to built-in function '__builtin_strlen'
   25 | #define __underlying_strlen     __builtin_strlen
      |                                 ^
include/linux/fortify-string.h:60:24: note: in expansion of macro '__underlying_strlen'
   60 |                 return __underlying_strlen(p);
      |                        ^~~~~~~~~~~~~~~~~~~
Quoted 7 lines

The code in there does not appear essential, so an explicit NULL check should be sufficient. The string is also printed to the console, but printk() is able to handle NULL pointer arguments gracefully.

Fixes 8983bfd58d61 ("platform/x86: lg-laptop: Support for battery charge limit on newer models")

Thank you for the patch, but this is already fixed in the pdx86 fixes branch:

https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/commit/?h=fixes&id=4c4a3d7cffb42da21ea8891fc7e6808ae05dbcb5

Regards,

Hans

Quoted 18 lines
---
 drivers/platform/x86/lg-laptop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/x86/lg-laptop.c +1 -1
index 3e520d5bca07..88b551caeaaf 100644
@@ -655,7 +655,7 @@ static int acpi_add(struct acpi_device *device)
 		goto out_platform_registered;
 	}
 	product = dmi_get_system_info(DMI_PRODUCT_NAME);
-	if (strlen(product) > 4)
+	if (product && strlen(product) > 4)
 		switch (product[4]) {
 		case '5':
 		case '6':