2023-08-02 00:55:36

by Justin Stitt

[permalink] [raw]
Subject: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

Favor not copying strings onto stack and instead use strings directly.
This avoids hard-coding sizes and buffer lengths all together.

Link: https://github.com/KSPP/linux/issues/90
Cc: [email protected]
Suggested-by: Kees Cook <[email protected]>
Signed-off-by: Justin Stitt <[email protected]>
---
Changes in v2:
- Use strings directly instead of copying onto stack with `strscpy` (thanks Kees)
- Link to v1: https://lore.kernel.org/r/[email protected]
---
arch/riscv/kernel/cpu.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
index a2fc952318e9..872fa7a47d68 100644
--- a/arch/riscv/kernel/cpu.c
+++ b/arch/riscv/kernel/cpu.c
@@ -271,21 +271,21 @@ static void print_isa(struct seq_file *f, const char *isa)

static void print_mmu(struct seq_file *f)
{
- char sv_type[16];
+ const char *sv_type;

#ifdef CONFIG_MMU
#if defined(CONFIG_32BIT)
- strncpy(sv_type, "sv32", 5);
+ sv_type = "sv32";
#elif defined(CONFIG_64BIT)
if (pgtable_l5_enabled)
- strncpy(sv_type, "sv57", 5);
+ sv_type = "sv57";
else if (pgtable_l4_enabled)
- strncpy(sv_type, "sv48", 5);
+ sv_type = "sv48";
else
- strncpy(sv_type, "sv39", 5);
+ sv_type = "sv39";
#endif
#else
- strncpy(sv_type, "none", 5);
+ sv_type = "none";
#endif /* CONFIG_MMU */
seq_printf(f, "mmu\t\t: %s\n", sv_type);
}

---
base-commit: 5d0c230f1de8c7515b6567d9afba1f196fb4e2f4
change-id: 20230801-arch-riscv-kernel-14a048cc6467

Best regards,
--
Justin Stitt <[email protected]>



2023-08-02 20:56:22

by Conor Dooley

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

On Wed, Aug 02, 2023 at 12:21:58AM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> Favor not copying strings onto stack and instead use strings directly.
> This avoids hard-coding sizes and buffer lengths all together.
>
> Link: https://github.com/KSPP/linux/issues/90
> Cc: [email protected]
> Suggested-by: Kees Cook <[email protected]>
> Signed-off-by: Justin Stitt <[email protected]>
> ---
> Changes in v2:
> - Use strings directly instead of copying onto stack with `strscpy` (thanks Kees)

Yeah, that is what I was thinking of when I decried it as being horrid.
Reviewed-by: Conor Dooley <[email protected]>

Thanks,
Conor.


Attachments:
(No filename) (744.00 B)
signature.asc (235.00 B)
Download all attachments

2023-08-02 21:28:44

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

On August 2, 2023 1:47:14 PM PDT, Palmer Dabbelt <[email protected]> wrote:
>On Wed, 02 Aug 2023 13:41:52 PDT (-0700), Palmer Dabbelt wrote:
>> On Wed, 02 Aug 2023 13:36:03 PDT (-0700), [email protected] wrote:
>>> On Wed, Aug 02, 2023 at 12:21:58AM +0000, Justin Stitt wrote:
>>>> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>>>>
>>>> Favor not copying strings onto stack and instead use strings directly.
>>>> This avoids hard-coding sizes and buffer lengths all together.
>>>>
>>>> Link: https://github.com/KSPP/linux/issues/90
>>>> Cc: [email protected]
>>>> Suggested-by: Kees Cook <[email protected]>
>>>> Signed-off-by: Justin Stitt <[email protected]>
>>>
>>> I like it! ;)
>>>
>>> Reviewed-by: Kees Cook <[email protected]>
>>
>> Thanks, I just queue it up for fixes (might take a bit to build test,
>> I'm a bit backed up).
>>
>> Reviewed-by: Palmer Dabbelt <[email protected]>
>> Acked-by: Palmer Dabbelt <[email protected]>
>>
>> If you wanted to take it for some hardening thing, though -- otherwise
>> it'll likely end up on my fixes later today.
>
>Sorry this is for-next as it's not a fix, I was just looking at other
>fixes. It's queued up aimed at for-next.

Awesome; thank you! :)


--
Kees Cook

2023-08-02 22:22:46

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

On Wed, 02 Aug 2023 13:41:52 PDT (-0700), Palmer Dabbelt wrote:
> On Wed, 02 Aug 2023 13:36:03 PDT (-0700), [email protected] wrote:
>> On Wed, Aug 02, 2023 at 12:21:58AM +0000, Justin Stitt wrote:
>>> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>>>
>>> Favor not copying strings onto stack and instead use strings directly.
>>> This avoids hard-coding sizes and buffer lengths all together.
>>>
>>> Link: https://github.com/KSPP/linux/issues/90
>>> Cc: [email protected]
>>> Suggested-by: Kees Cook <[email protected]>
>>> Signed-off-by: Justin Stitt <[email protected]>
>>
>> I like it! ;)
>>
>> Reviewed-by: Kees Cook <[email protected]>
>
> Thanks, I just queue it up for fixes (might take a bit to build test,
> I'm a bit backed up).
>
> Reviewed-by: Palmer Dabbelt <[email protected]>
> Acked-by: Palmer Dabbelt <[email protected]>
>
> If you wanted to take it for some hardening thing, though -- otherwise
> it'll likely end up on my fixes later today.

Sorry this is for-next as it's not a fix, I was just looking at other
fixes. It's queued up aimed at for-next.

2023-08-02 22:23:48

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

On Wed, Aug 02, 2023 at 12:21:58AM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> Favor not copying strings onto stack and instead use strings directly.
> This avoids hard-coding sizes and buffer lengths all together.
>
> Link: https://github.com/KSPP/linux/issues/90
> Cc: [email protected]
> Suggested-by: Kees Cook <[email protected]>
> Signed-off-by: Justin Stitt <[email protected]>

I like it! ;)

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2023-08-02 22:24:16

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

On Wed, 02 Aug 2023 13:36:03 PDT (-0700), [email protected] wrote:
> On Wed, Aug 02, 2023 at 12:21:58AM +0000, Justin Stitt wrote:
>> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>>
>> Favor not copying strings onto stack and instead use strings directly.
>> This avoids hard-coding sizes and buffer lengths all together.
>>
>> Link: https://github.com/KSPP/linux/issues/90
>> Cc: [email protected]
>> Suggested-by: Kees Cook <[email protected]>
>> Signed-off-by: Justin Stitt <[email protected]>
>
> I like it! ;)
>
> Reviewed-by: Kees Cook <[email protected]>

Thanks, I just queue it up for fixes (might take a bit to build test,
I'm a bit backed up).

Reviewed-by: Palmer Dabbelt <[email protected]>
Acked-by: Palmer Dabbelt <[email protected]>

If you wanted to take it for some hardening thing, though -- otherwise
it'll likely end up on my fixes later today.

Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy

Hello:

This patch was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <[email protected]>:

On Wed, 02 Aug 2023 00:21:58 +0000 you wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> Favor not copying strings onto stack and instead use strings directly.
> This avoids hard-coding sizes and buffer lengths all together.
>
> Link: https://github.com/KSPP/linux/issues/90
> Cc: [email protected]
> Suggested-by: Kees Cook <[email protected]>
> Signed-off-by: Justin Stitt <[email protected]>
>
> [...]

Here is the summary with links:
- [v2] RISC-V: cpu: refactor deprecated strncpy
https://git.kernel.org/riscv/c/12d61a1bc28e

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



2023-08-09 16:56:27

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH v2] RISC-V: cpu: refactor deprecated strncpy


On Wed, 02 Aug 2023 00:21:58 +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> Favor not copying strings onto stack and instead use strings directly.
> This avoids hard-coding sizes and buffer lengths all together.
>
>

Applied, thanks!

[1/1] RISC-V: cpu: refactor deprecated strncpy
https://git.kernel.org/palmer/c/12d61a1bc28e

Best regards,
--
Palmer Dabbelt <[email protected]>