From: David Laight <David.Laight@ACULAB.COM>
To: 'Guoxin Pu' <pugokushin@gmail.com>, "axboe@kernel.dk" <axboe@kernel.dk>
Cc: "linux-block@vger.kernel.org" <linux-block@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"stable@vger.kernel.org" <stable@vger.kernel.org>
Subject: RE: [PATCH] block: fix length of strscpy()
Date: Mon, 1 Jan 2024 21:47:23 +0000 [thread overview]
Message-ID: <ed0b9dd45fca4f6e910a9e1ffa756180@AcuMS.aculab.com> (raw)
In-Reply-To: <20240101175051.38479-2-pugokushin@gmail.com>
From: Guoxin Pu
> Sent: 01 January 2024 17:51
>
> In commit 146afeb235ccec10c17ad8ea26327c0c79dbd968 ("block: use strscpy()
> to instead of strncpy()") , the length that should now represent the length
> of the string with the terminating NULL was not updated alongside the
> change.
>
> This has caused blkdevparts= definition on kernel cmdline to be not
> correctly recognized and partitions not correctly initialized, breaking any
> device relying on such partitions to boot, on stable releases since 6.6
>
> This patch fixes the lengths to contain the terminating NULL.
>
> Cc: stable@vger.kernel.org # 6.6.x
> Signed-off-by: Guoxin Pu <pugokushin@gmail.com>
> ---
> block/partitions/cmdline.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/block/partitions/cmdline.c b/block/partitions/cmdline.c
> index c03bc105e575..c2aac5f4ab82 100644
> --- a/block/partitions/cmdline.c
> +++ b/block/partitions/cmdline.c
> @@ -79,8 +79,8 @@ static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
> goto fail;
> }
>
> - length = min_t(int, next - partdef,
> - sizeof(new_subpart->name) - 1);
> + length = min_t(int, next - partdef + 1,
> + sizeof(new_subpart->name));
> strscpy(new_subpart->name, partdef, length);
Shouldn't that be a memcpy() with the original length?
Since it looks as though there is something equivalent to:
next = strchr(partdef, ',');
just above?
Maybe with:
new_subpart->name[length] = '\0';
if the target isn't zero filled (which the strncpy() probably
relied on.)
> @@ -138,7 +138,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
> goto fail;
> }
>
> - length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
> + length = min_t(int, next - bdevdef + 1, sizeof(newparts->name));
> strscpy(newparts->name, bdevdef, length);
Same.
> @@ -148,8 +148,8 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
> bdevdef = next;
> next = strchr(bdevdef, ',');
>
> - length = (!next) ? (sizeof(buf) - 1) :
> - min_t(int, next - bdevdef, sizeof(buf) - 1);
> + length = (!next) ? sizeof(buf) :
> + min_t(int, next - bdevdef + 1, sizeof(buf));
>
> strscpy(buf, bdevdef, length);
Same
> @@ -262,7 +262,7 @@ static int add_part(int slot, struct cmdline_subpart *subpart,
>
> info = &state->parts[slot].info;
>
> - label_min = min_t(int, sizeof(info->volname) - 1,
> + label_min = min_t(int, sizeof(info->volname),
> sizeof(subpart->name));
> strscpy(info->volname, subpart->name, label_min);
WTF?
That only makes any sense if subpart->name might not be '\0'
terminated - which strncpy() would have handled fine (with the -1).
Otherwise what is wrong with:
strscpy(info->volname, subpart->name, sizeof (info->volname));
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2024-01-01 21:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-01 17:50 [PATCH] block: fix length of strscpy() Guoxin Pu
2024-01-01 21:26 ` Jens Axboe
2024-01-17 6:11 ` Guoxin Pu
2024-01-01 21:47 ` David Laight [this message]
2024-01-02 2:31 ` Guoxin Pu
2024-01-02 9:14 ` David Laight
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=ed0b9dd45fca4f6e910a9e1ffa756180@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pugokushin@gmail.com \
--cc=stable@vger.kernel.org \
/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