Skip to contentSkip to conversation

x86/setup: replace strlcat() with snprintf() in setup_arch()

PATCH Superseded Thorsten Blum 3 participants 7 messages Jun 4 – Jun 6 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/setup: replace strlcat() with snprintf() in setup_arch()

In preparation to remove strlcat() from the kernel [1], replace two strlcat() calls with one snprintf() call in setup_arch().

Also drop the explicit size argument of strscpy() to further simplify the code since strscpy() can determine the size automatically when the destination buffer has a fixed length.

[1] https://github.com/KSPP/linux/issues/370

---
 arch/x86/kernel/setup.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/setup.c +6 -3
index 46882ce79c3a..b11b0ce31a27 100644
@@ -21,6 +21,7 @@
 #include <linux/pci.h>
 #include <linux/random.h>
 #include <linux/root_dev.h>
+#include <linux/sprintf.h>
 #include <linux/static_call.h>
 #include <linux/sysfb.h>
 #include <linux/swiotlb.h>
@@ -915,16 +916,18 @@ void __init setup_arch(char **cmdline_p)
 	strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
 #else
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);
 		strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
 	}
 #endif
 	builtin_cmdline_added = true;
 #endif
 
-	strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
+	strscpy(command_line, boot_command_line);
 	*cmdline_p = command_line;
 
 	/*
#1 lore raw
Quoted 8 lines · Thorsten Blum

In preparation to remove strlcat() from the kernel [1], replace two strlcat() calls with one snprintf() call in setup_arch().

Also drop the explicit size argument of strscpy() to further simplify the code since strscpy() can determine the size automatically when the destination buffer has a fixed length.

[1] https://github.com/KSPP/linux/issues/370

Make it Link tag?

...

Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

And this has also third argument.

Quoted 7 lines
arch/x86/kernel/setup.c +1 -1
 	}
 #endif
 	builtin_cmdline_added = true;
 #endif
 
-	strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
+	strscpy(command_line, boot_command_line);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Thorsten Blum

In preparation to remove strlcat() from the kernel [1], replace two strlcat() calls with one snprintf() call in setup_arch().

Also drop the explicit size argument of strscpy() to further simplify the code since strscpy() can determine the size automatically when the destination buffer has a fixed length.

[1] https://github.com/KSPP/linux/issues/370

Quoted 7 lines

Make it Link tag?

...

Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

That doesn't work because boot_command_line, at least the declaration in linux/init.h, doesn't have a fixed size.

Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

No warnings with W=1. Why would GCC warn here?

Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

And this has also third argument.

Same reason as above.

Thanks,
Thorsten

#3 lore raw
Quoted 2 lines · Thorsten Blum
Quoted 1 line · Andy Shevchenko

On Thu, Jun 04, 2026 at 03:17:53PM +0200, Thorsten Blum wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

That doesn't work because boot_command_line, at least the declaration in linux/init.h, doesn't have a fixed size.

Ah, okay.

Quoted 12 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

Quoted 1 line

No warnings with W=1. Why would GCC warn here?

Sometimes it complains if it can't prove the size of the string to fit the destination. You said that there is no size for boot_command_line, I'm not sure I understand how GCC proves that the above snprintf() won't ever truncate the input.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

And this has also third argument.

Same reason as above.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 27 lines · Andy Shevchenko
Quoted 2 lines · Thorsten Blum
Quoted 1 line · Andy Shevchenko

On Thu, Jun 04, 2026 at 03:17:53PM +0200, Thorsten Blum wrote:

...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

That doesn't work because boot_command_line, at least the declaration in linux/init.h, doesn't have a fixed size.

Ah, okay.

Quoted 12 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

Quoted 6 lines
Quoted 1 line

No warnings with W=1. Why would GCC warn here?

Sometimes it complains if it can't prove the size of the string to fit the destination. You said that there is no size for boot_command_line, I'm not sure I understand how GCC proves that the above snprintf() won't ever truncate the input.

The compiler doesn't prove that this cannot truncate. It only knows the buffer sizes, but not the runtime string lengths.

snprintf() can truncate, and its return value could be used to detect that. However, the previous version also ignored possible truncation by strlcat(), so I didn't add new truncation handling.

#5 lore raw
Quoted 4 lines · Thorsten Blum
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Thorsten Blum
Quoted 1 line · Andy Shevchenko

On Thu, Jun 04, 2026 at 03:17:53PM +0200, Thorsten Blum wrote:

...

Quoted 28 lines
Quoted 21 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

That doesn't work because boot_command_line, at least the declaration in linux/init.h, doesn't have a fixed size.

Ah, okay.

Quoted 12 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

Quoted 6 lines
Quoted 1 line

No warnings with W=1. Why would GCC warn here?

Sometimes it complains if it can't prove the size of the string to fit the destination. You said that there is no size for boot_command_line, I'm not sure I understand how GCC proves that the above snprintf() won't ever truncate the input.

Quoted 6 lines

The compiler doesn't prove that this cannot truncate. It only knows the buffer sizes, but not the runtime string lengths.

snprintf() can truncate, and its return value could be used to detect that. However, the previous version also ignored possible truncation by strlcat(), so I didn't add new truncation handling.

I understand that, but AFAIK strlcat() doesn't induce a warning in such a case, while GCC does (or at least should).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 39 lines · On Fri, 5 Jun 2026 21:28:50 +0300 Andy Shevchenko…
Quoted 4 lines · Thorsten Blum
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Thorsten Blum
Quoted 1 line · Andy Shevchenko

On Thu, Jun 04, 2026 at 03:17:53PM +0200, Thorsten Blum wrote:

...

Quoted 31 lines
Quoted 21 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);

This also has third argument fixed. Don't you want to change that?

That doesn't work because boot_command_line, at least the declaration in linux/init.h, doesn't have a fixed size.

Ah, okay.

Quoted 12 lines
Quoted 11 lines

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 923

Quoted 9 lines

#else

arch/x86/kernel/setup.c +4 -2
 	if (builtin_cmdline[0]) {
+		size_t len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
+
 		/* append boot loader cmdline to builtin */
-		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
-		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+		snprintf(builtin_cmdline + len, COMMAND_LINE_SIZE - len, " %s",
+			 boot_command_line);  

Hmm... Wouldn't GCC complain on this? (Build with make W=1.)

Quoted 6 lines
Quoted 1 line

No warnings with W=1. Why would GCC warn here?

Sometimes it complains if it can't prove the size of the string to fit the destination. You said that there is no size for boot_command_line, I'm not sure I understand how GCC proves that the above snprintf() won't ever truncate the input.

The compiler doesn't prove that this cannot truncate. It only knows the buffer sizes, but not the runtime string lengths.

Quoted 7 lines
Quoted 3 lines

snprintf() can truncate, and its return value could be used to detect that. However, the previous version also ignored possible truncation by strlcat(), so I didn't add new truncation handling.

I understand that, but AFAIK strlcat() doesn't induce a warning in such a case, while GCC does (or at least should).

gcc only complains about snprintf() when it knows the the sizes (including taking strings from arrays).
So I suspect the warnings are mostly false-positives.

But I'm not really sure using snprintf() to avoid strlcat() is a gain. This could be:

	len = strnlen(builtin_cmdline, COMMAND_LINE_SIZE);
	if (strscpy(builtin_cmdline + len + 1, boot_command_line,
			COMMAND_LINE_SIZE - len - 1) >= 0)
		builtin_cmdline[len] = ' ';

but I suspect that doesn't return a useful string on overflow.

I've been trying to remove strcpy(), a lot of code has already done strlen() for a bound check - so memcpy() can be used instead.

-- David