Skip to contentSkip to conversation

gdrom: use strscpy in probe_gdrom_{setupcd,setupdisk}

PATCH Superseded Thorsten Blum 2 participants 2 messages Apr 17 – Apr 19 SuperH

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gdrom: use strscpy in probe_gdrom_{setupcd,setupdisk}

strcpy() has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows.

While the current code works correctly, replace strcpy() with the safer strscpy() to follow secure coding best practices.

¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

---
Compile-tested only. Adding arch/sh/ maintainers since that's the only platform where this driver can be used.
Cc: Yoshinori Sato <[email protected]>
Cc: Rich Felker <[email protected]>
Cc: John Paul Adrian Glaubitz <[email protected]>
Cc: [email protected]

---
 drivers/cdrom/gdrom.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/cdrom/gdrom.c +3 -2
index 4ba4dd06cbf4..5768dc1aebc0 100644
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/dma-mapping.h>
 #include <linux/cdrom.h>
 #include <linux/bio.h>
@@ -708,7 +709,7 @@ static void probe_gdrom_setupcd(void)
 {
 	gd.cd_info->ops = &gdrom_ops;
 	gd.cd_info->capacity = 1;
-	strcpy(gd.cd_info->name, GDROM_DEV_NAME);
+	strscpy(gd.cd_info->name, GDROM_DEV_NAME);
 	gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
 		CDC_SELECT_DISC;
 }
@@ -719,7 +720,7 @@ static void probe_gdrom_setupdisk(void)
 	gd.disk->first_minor = 1;
 	gd.disk->minors = 1;
 	gd.disk->flags |= GENHD_FL_NO_PART;
-	strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
+	strscpy(gd.disk->disk_name, GDROM_DEV_NAME);
 }
 
 static int probe_gdrom_setupqueue(void)
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Thorsten Blum

strcpy() has been deprecated¹ because it performs no bounds checking on the destination buffer, which can lead to buffer overflows.

While the current code works correctly, replace strcpy() with the safer strscpy() to follow secure coding best practices.

¹ https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy

Hey Thorsten,

tested on real hardware. LGTM.

Quoted 41 lines

---
Compile-tested only. Adding arch/sh/ maintainers since that's the only platform where this driver can be used.
Cc: Yoshinori Sato <[email protected]>
Cc: Rich Felker <[email protected]>
Cc: John Paul Adrian Glaubitz <[email protected]>
Cc: [email protected]

---
 drivers/cdrom/gdrom.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/cdrom/gdrom.c +3 -2
index 4ba4dd06cbf4..5768dc1aebc0 100644
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <linux/dma-mapping.h>
 #include <linux/cdrom.h>
 #include <linux/bio.h>
@@ -708,7 +709,7 @@ static void probe_gdrom_setupcd(void)
 {
 	gd.cd_info->ops = &gdrom_ops;
 	gd.cd_info->capacity = 1;
-	strcpy(gd.cd_info->name, GDROM_DEV_NAME);
+	strscpy(gd.cd_info->name, GDROM_DEV_NAME);
 	gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
 		CDC_SELECT_DISC;
 }
@@ -719,7 +720,7 @@ static void probe_gdrom_setupdisk(void)
 	gd.disk->first_minor = 1;
 	gd.disk->minors = 1;
 	gd.disk->flags |= GENHD_FL_NO_PART;
-	strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
+	strscpy(gd.disk->disk_name, GDROM_DEV_NAME);
 }
 
 static int probe_gdrom_setupqueue(void)