From: David Laight <david.laight.linux@gmail.com>
To: Thorsten Blum <blum@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
"Mike Rapoport (Microsoft)" <rppt@kernel.org>,
Christian Brauner <brauner@kernel.org>,
Breno Leitao <leitao@debian.org>,
Ryan Roberts <ryan.roberts@arm.com>,
Sang-Heon Jeon <ekffu200098@gmail.com>,
Wilson Felipe Pereira <wfelipe@google.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] init: Use scnprintf() to simplify setup_command_line()
Date: Wed, 18 Nov 2026 10:30:27 +0000 [thread overview]
Message-ID: <20261118103027.33256f2b@pumpkin> (raw)
In-Reply-To: <20261117234805.551734-2-blum@kernel.org>
On Wed, 18 Nov 2026 00:48:04 +0100
Thorsten Blum <blum@kernel.org> wrote:
> 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);
I think all the lengths are known (and used for the alloc) so the
code might as well uses memcpy().
blen = strlen(boot_command_line);
...
if (xlen)
memcpy(saved_command_line, extra_command_line, xlen);
memcpy(saved_command_line + xlen, boot_command_line, blen + 1);
(etc)
David
>
> - 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;
> + }
> }
>
> /*
>
prev parent reply other threads:[~2026-11-18 10:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 23:48 Thorsten Blum
2026-11-18 0:10 ` Masami Hiramatsu (Google)
2026-11-18 10:30 ` David Laight [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118103027.33256f2b@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=blum@kernel.org \
--cc=brauner@kernel.org \
--cc=ekffu200098@gmail.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=wfelipe@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®