From: Petr Pavlu <petr.pavlu@suse.com>
To: Jiacheng Yu <yujiacheng3@huawei.com>
Cc: samitolvanen@google.com, rusty@rustcorp.com.au,
hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com,
liuyongqiang13@huawei.com, linux-modules@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] params: fix charp corruption on allocation failure
Date: Tue, 28 Jul 2026 14:57:47 +0200 [thread overview]
Message-ID: <ff0b7cdd-8fa7-4cb8-9e21-6a7204809766@suse.com> (raw)
In-Reply-To: <c9643f86-f196-4c99-8129-2314a33a990f@huawei.com>
On 7/28/26 2:22 PM, Jiacheng Yu wrote:
> On 28/07/2026 18:46, Petr Pavlu wrote:
>> On 7/28/26 10:55 AM, Jiacheng Yu wrote:
>>> diff --git a/kernel/params.c b/kernel/params.c
>>> index a668863a4bb6..e4f2b71dde1e 100644
>>> --- a/kernel/params.c
>>> +++ b/kernel/params.c
>>> @@ -261,6 +261,7 @@ EXPORT_SYMBOL_GPL(param_set_uint_minmax);
>>>
>>> int param_set_charp(const char *val, const struct kernel_param *kp)
>>> {
>>> + char *tmp;
>>> size_t len, maxlen = 1024;
>>>
>>> len = strnlen(val, maxlen + 1);
>>> @@ -269,19 +270,20 @@ int param_set_charp(const char *val, const struct kernel_param *kp)
>>> return -ENOSPC;
>>> }
>>>
>>> - maybe_kfree_parameter(*(char **)kp->arg);
>>> -
>>> /*
>>> * This is a hack. We can't kmalloc() in early boot, and we
>>> * don't need to; this mangled commandline is preserved.
>>> */
>>> if (slab_is_available()) {
>>> - *(char **)kp->arg = kmalloc_parameter(len + 1);
>>> - if (!*(char **)kp->arg)
>>> + tmp = kmalloc_parameter(len + 1);
>>> + if (!tmp)
>>> return -ENOMEM;
>>> - strcpy(*(char **)kp->arg, val);
>>> + strscpy(tmp, val, len + 1);
>>
>> What's wrong with the plain strcpy() here?
>
> Functionally, plain strcpy() is fine here. The preceding
> strnlen(val, maxlen + 1) either finds the NUL byte within the limit or
> rejects the string, and the new allocation is exactly len + 1 bytes.
>
> However, when this patch rewrites the line to use tmp,
> checkpatch --strict reports the following warning:
>
> WARNING: Prefer strscpy over strcpy
>
> Since the line is being rewritten anyway, I changed strcpy() to
> strscpy() to follow that preference:
> https://github.com/KSPP/linux/issues/88
I don't see a benefit to using strscpy() here. The length of the string
is already known and the tmp buffer is correctly sized, so using
`memcpy(tmp, val, len + 1)` seems most appropriate to me.
--
Thanks,
Petr
prev parent reply other threads:[~2026-07-28 12:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 8:55 [PATCH] params: fix charp corruption on allocation failure Jiacheng Yu
2026-07-28 7:58 ` sashiko-bot
2026-07-28 10:46 ` Petr Pavlu
2026-07-28 12:22 ` Jiacheng Yu
2026-07-28 12:57 ` Petr Pavlu [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=ff0b7cdd-8fa7-4cb8-9e21-6a7204809766@suse.com \
--to=petr.pavlu@suse.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-modules@vger.kernel.org \
--cc=liuyongqiang13@huawei.com \
--cc=nphamcs@gmail.com \
--cc=rusty@rustcorp.com.au \
--cc=samitolvanen@google.com \
--cc=stable@vger.kernel.org \
--cc=yosry@kernel.org \
--cc=yujiacheng3@huawei.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