Skip to contentSkip to conversation

platform/surface: Replace deprecated strcpy in surface_button_add

PATCH Superseded Thorsten Blum 3 participants 6 messages Jan 5

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/surface: Replace deprecated strcpy in surface_button_add

strcpy() has been deprecated [1] because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy(). No functional changes.

---
 drivers/platform/surface/surfacepro3_button.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/platform/surface/surfacepro3_button.c +2 -1
index 2755601f979c..d027a064a62d 100644
@@ -10,6 +10,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
+#include <linux/string.h>
 #include <linux/types.h>
 #include <linux/input.h>
 #include <linux/acpi.h>
@@ -211,7 +212,7 @@ static int surface_button_add(struct acpi_device *device)
 	}
 
 	name = acpi_device_name(device);
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);
 	snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
 
 	input->name = name;
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
maintainer
#1 lore raw

Hi Thorsten,

Quoted 23 lines · Thorsten Blum

strcpy() has been deprecated [1] because it performs no bounds checking on the destination buffer, which can lead to buffer overflows. Replace it with the safer strscpy(). No functional changes.

---
  drivers/platform/surface/surfacepro3_button.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/platform/surface/surfacepro3_button.c +1 -0
index 2755601f979c..d027a064a62d 100644
@@ -10,6 +10,7 @@
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/init.h>
+#include <linux/string.h>
  #include <linux/types.h>
  #include <linux/input.h>
  #include <linux/acpi.h>

drivers/platform/surface/surfacepro3_button.c static int surface_button_add(struct acpi_device *device) · line 215

Quoted 5 lines
drivers/platform/surface/surfacepro3_button.c +1 -1
@@ -211,7 +212,7 @@ static int surface_button_add(struct acpi_device *device)
  	}
 
  	name = acpi_device_name(device);
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);

Thanks for the fix, a minor question might be why we do not reuse name by strscpy(name, SURFACE_BUTTON_DEVICE_NAME) ?

thanks,
Chenyu

Quoted 3 lines
drivers/platform/surface/surfacepro3_button.c +0 -0
  	snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
 
  	input->name = name;
#2 lore raw
Quoted 5 lines · Yu C

drivers/platform/surface/surfacepro3_button.c static int surface_button_add(struct acpi_device *device) · line 215

Quoted 2 lines
drivers/platform/surface/surfacepro3_button.c +1 -1
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);

Thanks for the fix, a minor question might be why we do not reuse name by strscpy(name, SURFACE_BUTTON_DEVICE_NAME) ?

Because 'name' is a 'char *' from which the size of the destination buffer cannot be inferred by the 2-argument version of strscpy().

Using 'name' would force us to use strscpy() with 3 arguments:

strscpy(name, SURFACE_BUTTON_DEVICE_NAME, MAX_ACPI_DEVICE_NAME_LEN);

Thanks,
Thorsten

maintainer
#3 lore raw
Quoted 7 lines · Thorsten Blum
Quoted 5 lines · Yu C

drivers/platform/surface/surfacepro3_button.c static int surface_button_add(struct acpi_device *device) · line 215

Quoted 2 lines
drivers/platform/surface/surfacepro3_button.c +1 -1
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);

Thanks for the fix, a minor question might be why we do not reuse name by strscpy(name, SURFACE_BUTTON_DEVICE_NAME) ?

Quoted 6 lines

Because 'name' is a 'char *' from which the size of the destination buffer cannot be inferred by the 2-argument version of strscpy().

Using 'name' would force us to use strscpy() with 3 arguments:

strscpy(name, SURFACE_BUTTON_DEVICE_NAME, MAX_ACPI_DEVICE_NAME_LEN);

I suggest you just remove 'name' instead. It seems pretty useless to begin with but especially after your patch. :-)

Please also add () to the subject line after function names.

Signature · 1 line
 i.
maintainer
#4 lore raw
Quoted 12 lines · Ilpo Järvinen
Quoted 10 lines · Thorsten Blum
Quoted 5 lines · Yu C

drivers/platform/surface/surfacepro3_button.c static int surface_button_add(struct acpi_device *device) · line 215

Quoted 2 lines
drivers/platform/surface/surfacepro3_button.c +1 -1
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);

Thanks for the fix, a minor question might be why we do not reuse name by strscpy(name, SURFACE_BUTTON_DEVICE_NAME) ?

Because 'name' is a 'char *' from which the size of the destination buffer cannot be inferred by the 2-argument version of strscpy().

Quoted 6 lines
Quoted 3 lines

Using 'name' would force us to use strscpy() with 3 arguments:

strscpy(name, SURFACE_BUTTON_DEVICE_NAME, MAX_ACPI_DEVICE_NAME_LEN);

I suggest you just remove 'name' instead. It seems pretty useless to begin with but especially after your patch. :-)

It was still used by other places.

thanks,
Chenyu

Quoted 3 lines

Please also add () to the subject line after function names.

maintainer Reviewed-by
#5 lore raw
In reply to Thorsten Blum
Quoted 7 lines · Thorsten Blum
Quoted 5 lines · Yu C

drivers/platform/surface/surfacepro3_button.c static int surface_button_add(struct acpi_device *device) · line 215

Quoted 2 lines
drivers/platform/surface/surfacepro3_button.c +1 -1
-	strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
+	strscpy(acpi_device_name(device), SURFACE_BUTTON_DEVICE_NAME);

Thanks for the fix, a minor question might be why we do not reuse name by strscpy(name, SURFACE_BUTTON_DEVICE_NAME) ?

Quoted 7 lines

Because 'name' is a 'char *' from which the size of the destination buffer cannot be inferred by the 2-argument version of strscpy().

Using 'name' would force us to use strscpy() with 3 arguments:

strscpy(name, SURFACE_BUTTON_DEVICE_NAME, MAX_ACPI_DEVICE_NAME_LEN);

Thanks for the explanation!

thanks,
Chenyu

Quoted 3 lines

Thanks,
Thorsten