Skip to contentSkip to conversation

init: Use strscpy() in do_initcalls()

PATCH In review Thorsten Blum 2 participants 3 messages Nov 14 – Nov 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 init: Use strscpy() in do_initcalls()

Replace the deprecated strcpy() [1] with strscpy() to bound the copy to the allocated buffer size.

---
 init/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/main.c +1 -1
index db8fb6ebe830..93a2b12976f0 100644
@@ -1481,7 +1481,7 @@ static void __init do_initcalls(void)
 
 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
 		/* Parser modifies command_line, restore it each time */
-		strcpy(command_line, saved_command_line);
+		strscpy(command_line, saved_command_line, len);
 		do_initcall_level(level, command_line);
 	}
 
#1 lore raw

PATCH 2/2 init: Use kmalloc() in do_initcalls()

Use kmalloc() since strscpy() already NUL-terminates command_line.

---
 init/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/main.c +1 -1
index 93a2b12976f0..7f52a1b00c0f 100644
@@ -1475,7 +1475,7 @@ static void __init do_initcalls(void)
 	size_t len = saved_command_line_len + 1;
 	char *command_line;
 
-	command_line = kzalloc(len, GFP_KERNEL);
+	command_line = kmalloc(len, GFP_KERNEL);
 	if (!command_line)
 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
 
#2 lore raw
In reply to Thorsten Blum
Quoted 14 lines · On Sat, 14 Nov 2026 12:52:29 +0100 Thorsten Blum <[email protected]> wrote:

Replace the deprecated strcpy() [1] with strscpy() to bound the copy to the allocated buffer size.

---
 init/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
init/main.c +0 -0
index db8fb6ebe830..93a2b12976f0 100644

init/main.c static void __init do_initcalls(void) · line 1484

Quoted 5 lines
init/main.c +1 -1
@@ -1481,7 +1481,7 @@ static void __init do_initcalls(void)
 
 	for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
 		/* Parser modifies command_line, restore it each time */
-		strcpy(command_line, saved_command_line);
+		strscpy(command_line, saved_command_line, len);

That could be a memcpy().

David

Quoted 4 lines
init/main.c +0 -0
 		do_initcall_level(level, command_line);
 	}