* [PATCH next] drivers/mtd/ubi/block: Replace strcpy() with strscpy()
@ 2026-06-08 9:55 ` david.laight.linux
0 siblings, 0 replies; 2+ messages in thread
From: david.laight.linux @ 2026-06-08 9:55 UTC (permalink / raw)
To: Kees Cook, linux-hardening, linux-kernel, linux-mtd
Cc: Arnd Bergmann, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, David Laight
From: David Laight <david.laight.linux@gmail.com>
Use the result of the strscpy() that copies the parameter block
to save an extra strnlen() call
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
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/mtd/ubi/block.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index 8880a783c3bc..be9655b4a9e9 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -96,7 +96,7 @@ static int __init ubiblock_set_param(const char *val,
const struct kernel_param *kp)
{
int i, ret;
- size_t len;
+ ssize_t len;
struct ubiblock_param *param;
char buf[UBIBLOCK_PARAM_LEN];
char *pbuf = &buf[0];
@@ -105,20 +105,18 @@ static int __init ubiblock_set_param(const char *val,
if (!val)
return -EINVAL;
- len = strnlen(val, UBIBLOCK_PARAM_LEN);
+ len = strscpy(buf, val);
if (len == 0) {
pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
return 0;
}
- if (len == UBIBLOCK_PARAM_LEN) {
+ if (len < 0) {
pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
val, UBIBLOCK_PARAM_LEN);
return -EINVAL;
}
- strcpy(buf, val);
-
/* Get rid of the final newline */
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
@@ -137,12 +135,12 @@ static int __init ubiblock_set_param(const char *val,
ret = kstrtoint(tokens[1], 10, ¶m->vol_id);
if (ret < 0) {
param->vol_id = -1;
- strcpy(param->name, tokens[1]);
+ strscpy(param->name, tokens[1]);
}
} else {
/* One parameter: must be device path */
- strcpy(param->name, tokens[0]);
+ strscpy(param->name, tokens[0]);
param->ubi_num = -1;
param->vol_id = -1;
}
--
2.39.5
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH next] drivers/mtd/ubi/block: Replace strcpy() with strscpy()
@ 2026-06-08 9:55 ` david.laight.linux
0 siblings, 0 replies; 2+ messages in thread
From: david.laight.linux @ 2026-06-08 9:55 UTC (permalink / raw)
To: Kees Cook, linux-hardening, linux-kernel, linux-mtd
Cc: Arnd Bergmann, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, David Laight
From: David Laight <david.laight.linux@gmail.com>
Use the result of the strscpy() that copies the parameter block
to save an extra strnlen() call
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
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/mtd/ubi/block.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
index 8880a783c3bc..be9655b4a9e9 100644
--- a/drivers/mtd/ubi/block.c
+++ b/drivers/mtd/ubi/block.c
@@ -96,7 +96,7 @@ static int __init ubiblock_set_param(const char *val,
const struct kernel_param *kp)
{
int i, ret;
- size_t len;
+ ssize_t len;
struct ubiblock_param *param;
char buf[UBIBLOCK_PARAM_LEN];
char *pbuf = &buf[0];
@@ -105,20 +105,18 @@ static int __init ubiblock_set_param(const char *val,
if (!val)
return -EINVAL;
- len = strnlen(val, UBIBLOCK_PARAM_LEN);
+ len = strscpy(buf, val);
if (len == 0) {
pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
return 0;
}
- if (len == UBIBLOCK_PARAM_LEN) {
+ if (len < 0) {
pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
val, UBIBLOCK_PARAM_LEN);
return -EINVAL;
}
- strcpy(buf, val);
-
/* Get rid of the final newline */
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
@@ -137,12 +135,12 @@ static int __init ubiblock_set_param(const char *val,
ret = kstrtoint(tokens[1], 10, ¶m->vol_id);
if (ret < 0) {
param->vol_id = -1;
- strcpy(param->name, tokens[1]);
+ strscpy(param->name, tokens[1]);
}
} else {
/* One parameter: must be device path */
- strcpy(param->name, tokens[0]);
+ strscpy(param->name, tokens[0]);
param->ubi_num = -1;
param->vol_id = -1;
}
--
2.39.5
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-08 9:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-08 9:55 [PATCH next] drivers/mtd/ubi/block: Replace strcpy() with strscpy() david.laight.linux
2026-06-08 9:55 ` david.laight.linux
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.