mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] init: Use scnprintf() to simplify setup_command_line()
@ 2026-11-17 23:48 Thorsten Blum
  2026-11-18  0:10 ` Masami Hiramatsu (Google)
  2026-11-18 10:30 ` David Laight
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-11-17 23:48 UTC (permalink / raw)
  To: Andrew Morton, Masami Hiramatsu (Google),
	Thorsten Blum, Mike Rapoport (Microsoft),
	Christian Brauner, Breno Leitao, Ryan Roberts
  Cc: Sang-Heon Jeon, Wilson Felipe Pereira, linux-kernel

strcpy() has been deprecated [1] because it performs no bounds checking
on the destination buffer, which can lead to buffer overflows.

Use scnprintf() to format the saved and static command lines while
taking their buffer sizes into account. Use scnprintf()'s return value
to keep track of saved_command_line_len and avoid extra strlen() calls.

No functional change.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 init/main.c | 54 +++++++++++++++++++++++------------------------------
 1 file changed, 23 insertions(+), 31 deletions(-)

diff --git a/init/main.c b/init/main.c
index 8a6c3b4f1d5e..0c3bc7efe0f4 100644
--- a/init/main.c
+++ b/init/main.c
@@ -669,36 +669,31 @@ static inline void smp_prepare_cpus(unsigned int maxcpus) { }
  */
 static void __init setup_command_line(char *command_line)
 {
-	size_t len, xlen = 0, ilen = 0;
+	size_t xlen = 0, ilen = 0;
+	size_t saved_cmdline_size;
+	size_t static_cmdline_size;
 
 	if (extra_command_line)
 		xlen = strlen(extra_command_line);
 	if (extra_init_args) {
-		extra_init_args = strim(extra_init_args); /* remove trailing space */
-		ilen = strlen(extra_init_args) + 4; /* for " -- " */
+		extra_init_args = strim(extra_init_args);
+		ilen = strlen(extra_init_args) + strlen(" -- ");
 	}
 
-	len = xlen + strlen(boot_command_line) + ilen + 1;
+	/* Keep bootconfig kernel parameters before any "--" separator. */
+	saved_cmdline_size = xlen + strlen(boot_command_line) + ilen + 1;
+	saved_command_line = memblock_alloc_or_panic(saved_cmdline_size, SMP_CACHE_BYTES);
+	saved_command_line_len = scnprintf(saved_command_line, saved_cmdline_size, "%s%s",
+					   extra_command_line ?: "", boot_command_line);
 
-	saved_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
-
-	len = xlen + strlen(command_line) + 1;
-
-	static_command_line = memblock_alloc_or_panic(len, SMP_CACHE_BYTES);
-
-	if (xlen) {
-		/*
-		 * We have to put extra_command_line before boot command
-		 * lines because there could be dashes (separator of init
-		 * command line) in the command lines.
-		 */
-		strcpy(saved_command_line, extra_command_line);
-		strcpy(static_command_line, extra_command_line);
-	}
-	strcpy(saved_command_line + xlen, boot_command_line);
-	strcpy(static_command_line + xlen, command_line);
+	static_cmdline_size = xlen + strlen(command_line) + 1;
+	static_command_line = memblock_alloc_or_panic(static_cmdline_size, SMP_CACHE_BYTES);
+	scnprintf(static_command_line, static_cmdline_size, "%s%s",
+		  extra_command_line ?: "", command_line);
 
 	if (ilen) {
+		size_t len;
+
 		/*
 		 * Append supplemental init boot args to saved_command_line
 		 * so that user can check what command line options passed
@@ -708,19 +703,16 @@ static void __init setup_command_line(char *command_line)
 		 */
 		if (initargs_offs) {
 			len = xlen + initargs_offs;
-			strcpy(saved_command_line + len, extra_init_args);
-			len += ilen - 4;	/* strlen(extra_init_args) */
-			strcpy(saved_command_line + len,
-				boot_command_line + initargs_offs - 1);
+			len += scnprintf(saved_command_line + len, saved_cmdline_size - len, "%s%s",
+					 extra_init_args, boot_command_line + initargs_offs - 1);
 		} else {
-			len = strlen(saved_command_line);
-			strcpy(saved_command_line + len, " -- ");
-			len += 4;
-			strcpy(saved_command_line + len, extra_init_args);
+			len = saved_command_line_len;
+			len += scnprintf(saved_command_line + len, saved_cmdline_size - len,
+					 " -- %s", extra_init_args);
 		}
-	}
 
-	saved_command_line_len = strlen(saved_command_line);
+		saved_command_line_len = len;
+	}
 }
 
 /*

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18 10:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 23:48 [PATCH] init: Use scnprintf() to simplify setup_command_line() Thorsten Blum
2026-11-18  0:10 ` Masami Hiramatsu (Google)
2026-11-18 10:30 ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®