Skip to contentSkip to conversation

drivers/md/md: Use bounded strings ops when setting disk->disk_name

PATCH Superseded [email protected] 2 participants 2 messages Jun 8 – Jun 12 MD RAID

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/md/md: Use bounded strings ops when setting disk->disk_name

From: David Laight <[email protected]>

Replace strcpy() with strscpy() and sprintf() with snprintf() for safety.

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

 drivers/md/md.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/md/md.c +3 -3
index 8b568eee8743..4b659d10fa4d 100644
@@ -6346,11 +6346,11 @@ struct mddev *md_alloc(dev_t dev, char *name)
 	disk->first_minor = unit << shift;
 	disk->minors = 1 << shift;
 	if (name)
-		strcpy(disk->disk_name, name);
+		strscpy(disk->disk_name, name);
 	else if (partitioned)
-		sprintf(disk->disk_name, "md_d%d", unit);
+		snprintf(disk->disk_name, sizeof (disk->disk_name), "md_d%d", unit);
 	else
-		sprintf(disk->disk_name, "md%d", unit);
+		snprintf(disk->disk_name, sizeof (disk->disk_name), "md%d", unit);
 	disk->fops = &md_fops;
 	disk->private_data = mddev;
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 36 lines · david.laight.linux

From: David Laight <[email protected]>

Replace strcpy() with strscpy() and sprintf() with snprintf() for safety.

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

 drivers/md/md.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/md/md.c +0 -0
index 8b568eee8743..4b659d10fa4d 100644

drivers/md/md.c struct mddev *md_alloc(dev_t dev, char *name) · line 6351

Quoted 8 lines
drivers/md/md.c +2 -2
@@ -6346,11 +6346,11 @@ struct mddev *md_alloc(dev_t dev, char *name)
 	disk->first_minor = unit << shift;
 	disk->minors = 1 << shift;
 	if (name)
-		strcpy(disk->disk_name, name);
+		strscpy(disk->disk_name, name);
 	else if (partitioned)
-		sprintf(disk->disk_name, "md_d%d", unit);
+		snprintf(disk->disk_name, sizeof (disk->disk_name), "md_d%d", unit);

Please remove the space after sizeof to match the style used elsewhere in this file.

Quoted 6 lines
drivers/md/md.c +1 -1
 	else
-		sprintf(disk->disk_name, "md%d", unit);
+		snprintf(disk->disk_name, sizeof (disk->disk_name), "md%d", unit);
 	disk->fops = &md_fops;
 	disk->private_data = mddev;
 
Signature · 2 lines
Thanks,
Nan