* [PATCH v2 next] drivers/md/md: Use bounded strings ops when setting disk->disk_name
@ 2026-06-26 8:43 David Laight
2026-06-26 8:51 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: David Laight @ 2026-06-26 8:43 UTC (permalink / raw)
To: Kees Cook, linux-hardening, linux-kernel, linux-raid
Cc: Arnd Bergmann, Song Liu, Yu Kuai, David Laight
Replace strcpy() with strscpy() and sprintf() with snprintf() for safety.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
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(-)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 8b568eee8743..4b659d10fa4d 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -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;
--
2.39.5
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2 next] drivers/md/md: Use bounded strings ops when setting disk->disk_name
2026-06-26 8:43 [PATCH v2 next] drivers/md/md: Use bounded strings ops when setting disk->disk_name David Laight
@ 2026-06-26 8:51 ` sashiko-bot
2026-06-26 10:52 ` David Laight
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-06-26 8:51 UTC (permalink / raw)
To: David Laight; +Cc: yukuai, linux-raid
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 <david.laight.linux@gmail.com>
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.
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index d1465bcd86c81..afbdc52a11270 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -6354,11 +6354,11 @@ struct mddev *md_alloc(dev_t dev, char *name)
[ ... ]
> 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);
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260626084334.3012-1-david.laight.linux@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2 next] drivers/md/md: Use bounded strings ops when setting disk->disk_name
2026-06-26 8:51 ` sashiko-bot
@ 2026-06-26 10:52 ` David Laight
0 siblings, 0 replies; 3+ messages in thread
From: David Laight @ 2026-06-26 10:52 UTC (permalink / raw)
To: sashiko-bot; +Cc: sashiko-reviews, yukuai, linux-raid
On Fri, 26 Jun 2026 08:51:40 +0000
sashiko-bot@kernel.org 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 <david.laight.linux@gmail.com>
>
> 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.
>
> > diff --git a/drivers/md/md.c b/drivers/md/md.c
> > index d1465bcd86c81..afbdc52a11270 100644
> > --- a/drivers/md/md.c
> > +++ b/drivers/md/md.c
> > @@ -6354,11 +6354,11 @@ struct mddev *md_alloc(dev_t dev, char *name)
> [ ... ]
> > 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);
>
> [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.
>
B****r I only fixed one of them :-(
David
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-26 10:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-26 8:43 [PATCH v2 next] drivers/md/md: Use bounded strings ops when setting disk->disk_name David Laight
2026-06-26 8:51 ` sashiko-bot
2026-06-26 10:52 ` David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox