drivers/md/md: Use bounded strings ops when setting disk->disk_name
PATCH v2 1/1 drivers/md/md: Use bounded strings ops when setting disk->disk_name
Replace strcpy() with strscpy() and sprintf() with snprintf() for safety.
---
v2: Make sizeof look like a function call for consistency.
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
@@ -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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Inconsistent coding style for the sizeof operator in newly added code. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Inconsistent coding style for the sizeof operator in newly added code. --
commit 663613700e83a5bb9c1749d80b4fae68752645fb
Author: David Laight <[email protected]>
drivers/md/md: Use bounded strings ops when setting disk->disk_name
This commit replaces unbounded strcpy() and sprintf() with bounded strscpy() and snprintf() operations when setting the disk_name in md_alloc() to improve safety.
Quoted 5 lines
drivers/md/md.c +0
-0@@ -6354,11 +6354,11 @@ struct mddev *md_alloc(dev_t dev, char *name)
[ ... ]
drivers/md/md.c struct mddev *md_alloc(dev_t dev, char *name) · line 6353
Quoted 9 lines
drivers/md/md.c +3
-3if (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);
[Severity: Low]
This isn't a bug, but there is an inconsistent coding style for the sizeof operator here in md_alloc(). The adjacent branch uses sizeof(disk->disk_name) without a space, while this newly added branch includes a space. The kernel coding style prefers no space after sizeof.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 19 lines · On Fri, 26 Jun 2026 08:51:40 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Inconsistent coding style for the sizeof operator in newly added code. --commit
663613700e83a5bb9c1749d80b4fae68752645fb
Author: David Laight <[email protected]>
drivers/md/md: Use bounded strings ops when settingdisk->disk_nameThis commit replaces unbounded
strcpy()andsprintf()with boundedstrscpy()andsnprintf()operations when setting thedisk_nameinmd_alloc()to improve safety.Quoted 5 lines
drivers/md/md.c +0
-0@@ -6354,11 +6354,11 @@ struct mddev *md_alloc(dev_t dev, char *name)[ ... ]
Quoted 16 lines
drivers/md/md.c struct mddev *md_alloc(dev_t dev, char *name) · line 6353
Quoted 9 lines
drivers/md/md.c +3
-3if (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);[Severity: Low]
This isn't a bug, but there is an inconsistent coding style for the sizeof operator here inmd_alloc(). The adjacent branch usessizeof(disk->disk_name)without a space, while this newly added branch includes a space. The kernel coding style prefers no space after sizeof.
B****r I only fixed one of them :-(
David