Skip to content

Commit e055022

Browse files
JustinStittpmladek
authored andcommitted
printk: cleanup deprecated uses of strncpy/strcpy
Cleanup some deprecated uses of strncpy() and strcpy() [1]. There doesn't seem to be any bugs with the current code but the readability of this code could benefit from a quick makeover while removing some deprecated stuff as a benefit. The most interesting replacement made in this patch involves concatenating "ttyS" with a digit-led user-supplied string. Instead of doing two distinct string copies with carefully managed offsets and lengths, let's use the more robust and self-explanatory scnprintf(). scnprintf will 1) respect the bounds of @buf, 2) null-terminate @buf, 3) do the concatenation. This allows us to drop the manual NUL-byte assignment. Also, since isdigit() is used about a dozen lines after the open-coded version we'll replace it for uniformity's sake. All the strcpy() --> strscpy() replacements are trivial as the source strings are literals and much smaller than the destination size. No behavioral change here. Use the new 2-argument version of strscpy() introduced in Commit e6584c3 ("string: Allow 2-argument strscpy()"). However, to make this work fully (since the size must be known at compile time), also update the extern-qualified declaration to have the proper size information. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: KSPP/linux#90 [2] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [3] Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt <justinstitt@google.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20240429-strncpy-kernel-printk-printk-c-v1-1-4da7926d7b69@google.com [pmladek@suse.com: Removed obsolete brackets and added empty lines.] Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent b37cafa commit e055022

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

include/linux/printk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extern void console_verbose(void);
7171

7272
/* strlen("ratelimit") + 1 */
7373
#define DEVKMSG_STR_MAX_SIZE 10
74-
extern char devkmsg_log_str[];
74+
extern char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE];
7575
struct ctl_table;
7676

7777
extern int suppress_printk;

kernel/printk/printk.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ static int __init control_devkmsg(char *str)
178178
* Set sysctl string accordingly:
179179
*/
180180
if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
181-
strcpy(devkmsg_log_str, "on");
181+
strscpy(devkmsg_log_str, "on");
182182
else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
183-
strcpy(devkmsg_log_str, "off");
183+
strscpy(devkmsg_log_str, "off");
184184
/* else "ratelimit" which is set by default. */
185185

186186
/*
@@ -209,7 +209,7 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
209209
return -EINVAL;
210210

211211
old = devkmsg_log;
212-
strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE);
212+
strscpy(old_str, devkmsg_log_str);
213213
}
214214

215215
err = proc_dostring(table, write, buffer, lenp, ppos);
@@ -227,7 +227,7 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
227227

228228
/* ... and restore old setting. */
229229
devkmsg_log = old;
230-
strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE);
230+
strscpy(devkmsg_log_str, old_str);
231231

232232
return -EINVAL;
233233
}
@@ -2500,22 +2500,22 @@ static int __init console_setup(char *str)
25002500
/*
25012501
* Decode str into name, index, options.
25022502
*/
2503-
if (str[0] >= '0' && str[0] <= '9') {
2504-
strcpy(buf, "ttyS");
2505-
strncpy(buf + 4, str, sizeof(buf) - 5);
2506-
} else {
2507-
strncpy(buf, str, sizeof(buf) - 1);
2508-
}
2509-
buf[sizeof(buf) - 1] = 0;
2503+
if (isdigit(str[0]))
2504+
scnprintf(buf, sizeof(buf), "ttyS%s", str);
2505+
else
2506+
strscpy(buf, str);
2507+
25102508
options = strchr(str, ',');
25112509
if (options)
25122510
*(options++) = 0;
2511+
25132512
#ifdef __sparc__
25142513
if (!strcmp(str, "ttya"))
2515-
strcpy(buf, "ttyS0");
2514+
strscpy(buf, "ttyS0");
25162515
if (!strcmp(str, "ttyb"))
2517-
strcpy(buf, "ttyS1");
2516+
strscpy(buf, "ttyS1");
25182517
#endif
2518+
25192519
for (s = buf; *s; s++)
25202520
if (isdigit(*s) || *s == ',')
25212521
break;

0 commit comments

Comments
 (0)