From: Conor Dooley <conor@kernel.org>
To: Justin Stitt <justinstitt@google.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Kees Cook <keescook@chromium.org>,
Nick Desaulniers <ndesaulniers@google.com>,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH] RISC-V: cpu: refactor deprecated strncpy
Date: Tue, 1 Aug 2023 22:26:51 +0100 [thread overview]
Message-ID: <20230801-tinsel-parcel-aa034d07aeb6@spud> (raw)
In-Reply-To: <20230801-arch-riscv-kernel-v1-1-2b3f2dc0bc61@google.com>
[-- Attachment #1: Type: text/plain, Size: 3621 bytes --]
Hey Justin,
On Tue, Aug 01, 2023 at 09:14:56PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ the case for `strncpy`!
>
> The `sv_type` buffer is declared with a size of 16 which is then
> followed by some `strncpy` calls to populate the buffer with one of:
> "sv32", "sv57", "sv48", "sv39" or "none". Hard-coding the max length as 5 is
> error-prone and involves counting the number of characters (and
> hopefully not forgetting to count the NUL-byte) in the raw string.
What is error prone about it when there are only 4 characters possible?
> Using a pre-determined max length in combination with `strscpy` provides
> a cleaner, less error-prone as well as a less ambiguous implementation.
> `strscpy` guarantees that it's destination buffer is NUL-terminated even
> if it's source argument exceeds the max length as defined by the third
Wrong its ;)
> argument.
>
> To be clear, there is no bug (i think?) in the current implementation
> but the current hard-coded values in combination with using a deprecated
> interface make this a worthwhile change, IMO.
>
> [1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> [2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
This link is broken, it should be
https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
Also, in the future, please use the form
Link: <url> [ref]
so
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [1]
and so on.
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> arch/riscv/kernel/cpu.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
> index a2fc952318e9..1c576e4ec171 100644
> --- a/arch/riscv/kernel/cpu.c
> +++ b/arch/riscv/kernel/cpu.c
> @@ -17,6 +17,8 @@
> #include <asm/smp.h>
> #include <asm/pgtable.h>
>
> +#define SV_TYPE_MAX_LENGTH 16
> +
> /*
> * Returns the hart ID of the given device tree node, or -ENODEV if the node
> * isn't an enabled and valid RISC-V hart node.
> @@ -271,21 +273,21 @@ static void print_isa(struct seq_file *f, const char *isa)
>
> static void print_mmu(struct seq_file *f)
> {
> - char sv_type[16];
> + char sv_type[SV_TYPE_MAX_LENGTH];
>
> #ifdef CONFIG_MMU
> #if defined(CONFIG_32BIT)
> - strncpy(sv_type, "sv32", 5);
> + strscpy(sv_type, "sv32", SV_TYPE_MAX_LENGTH);
> #elif defined(CONFIG_64BIT)
> if (pgtable_l5_enabled)
> - strncpy(sv_type, "sv57", 5);
> + strscpy(sv_type, "sv57", SV_TYPE_MAX_LENGTH);
> else if (pgtable_l4_enabled)
> - strncpy(sv_type, "sv48", 5);
> + strscpy(sv_type, "sv48", SV_TYPE_MAX_LENGTH);
> else
> - strncpy(sv_type, "sv39", 5);
> + strscpy(sv_type, "sv39", SV_TYPE_MAX_LENGTH);
> #endif
> #else
> - strncpy(sv_type, "none", 5);
> + strscpy(sv_type, "none", SV_TYPE_MAX_LENGTH);
> #endif /* CONFIG_MMU */
> seq_printf(f, "mmu\t\t: %s\n", sv_type);
> }
This all seems rather horrible, we should probably clean it up, but that
is nothing to do with your patch. To be clear, I am also not requesting a
resubmission for the commit message nitpickery.
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Thanks,
Conor.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
WARNING: multiple messages have this Message-ID (diff)
From: Conor Dooley <conor@kernel.org>
To: Justin Stitt <justinstitt@google.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Kees Cook <keescook@chromium.org>,
Nick Desaulniers <ndesaulniers@google.com>,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH] RISC-V: cpu: refactor deprecated strncpy
Date: Tue, 1 Aug 2023 22:26:51 +0100 [thread overview]
Message-ID: <20230801-tinsel-parcel-aa034d07aeb6@spud> (raw)
In-Reply-To: <20230801-arch-riscv-kernel-v1-1-2b3f2dc0bc61@google.com>
[-- Attachment #1.1: Type: text/plain, Size: 3621 bytes --]
Hey Justin,
On Tue, Aug 01, 2023 at 09:14:56PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ the case for `strncpy`!
>
> The `sv_type` buffer is declared with a size of 16 which is then
> followed by some `strncpy` calls to populate the buffer with one of:
> "sv32", "sv57", "sv48", "sv39" or "none". Hard-coding the max length as 5 is
> error-prone and involves counting the number of characters (and
> hopefully not forgetting to count the NUL-byte) in the raw string.
What is error prone about it when there are only 4 characters possible?
> Using a pre-determined max length in combination with `strscpy` provides
> a cleaner, less error-prone as well as a less ambiguous implementation.
> `strscpy` guarantees that it's destination buffer is NUL-terminated even
> if it's source argument exceeds the max length as defined by the third
Wrong its ;)
> argument.
>
> To be clear, there is no bug (i think?) in the current implementation
> but the current hard-coded values in combination with using a deprecated
> interface make this a worthwhile change, IMO.
>
> [1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> [2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
This link is broken, it should be
https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
Also, in the future, please use the form
Link: <url> [ref]
so
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [1]
and so on.
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> arch/riscv/kernel/cpu.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
> index a2fc952318e9..1c576e4ec171 100644
> --- a/arch/riscv/kernel/cpu.c
> +++ b/arch/riscv/kernel/cpu.c
> @@ -17,6 +17,8 @@
> #include <asm/smp.h>
> #include <asm/pgtable.h>
>
> +#define SV_TYPE_MAX_LENGTH 16
> +
> /*
> * Returns the hart ID of the given device tree node, or -ENODEV if the node
> * isn't an enabled and valid RISC-V hart node.
> @@ -271,21 +273,21 @@ static void print_isa(struct seq_file *f, const char *isa)
>
> static void print_mmu(struct seq_file *f)
> {
> - char sv_type[16];
> + char sv_type[SV_TYPE_MAX_LENGTH];
>
> #ifdef CONFIG_MMU
> #if defined(CONFIG_32BIT)
> - strncpy(sv_type, "sv32", 5);
> + strscpy(sv_type, "sv32", SV_TYPE_MAX_LENGTH);
> #elif defined(CONFIG_64BIT)
> if (pgtable_l5_enabled)
> - strncpy(sv_type, "sv57", 5);
> + strscpy(sv_type, "sv57", SV_TYPE_MAX_LENGTH);
> else if (pgtable_l4_enabled)
> - strncpy(sv_type, "sv48", 5);
> + strscpy(sv_type, "sv48", SV_TYPE_MAX_LENGTH);
> else
> - strncpy(sv_type, "sv39", 5);
> + strscpy(sv_type, "sv39", SV_TYPE_MAX_LENGTH);
> #endif
> #else
> - strncpy(sv_type, "none", 5);
> + strscpy(sv_type, "none", SV_TYPE_MAX_LENGTH);
> #endif /* CONFIG_MMU */
> seq_printf(f, "mmu\t\t: %s\n", sv_type);
> }
This all seems rather horrible, we should probably clean it up, but that
is nothing to do with your patch. To be clear, I am also not requesting a
resubmission for the commit message nitpickery.
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Thanks,
Conor.
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
[-- Attachment #2: Type: text/plain, Size: 161 bytes --]
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2023-08-01 21:27 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-01 21:14 [PATCH] RISC-V: cpu: refactor deprecated strncpy Justin Stitt
2023-08-01 21:14 ` Justin Stitt
2023-08-01 21:26 ` Conor Dooley [this message]
2023-08-01 21:26 ` Conor Dooley
2023-08-01 22:22 ` Justin Stitt
2023-08-01 22:22 ` Justin Stitt
2023-08-01 23:02 ` Jessica Clarke
2023-08-01 23:02 ` Jessica Clarke
2023-08-01 23:30 ` Kees Cook
2023-08-01 23:30 ` Kees Cook
2023-08-01 23:33 ` Kees Cook
2023-08-01 23:33 ` Kees Cook
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=20230801-tinsel-parcel-aa034d07aeb6@spud \
--to=conor@kernel.org \
--cc=aou@eecs.berkeley.edu \
--cc=justinstitt@google.com \
--cc=keescook@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=ndesaulniers@google.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.