public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390/sclp: replace deprecated strncpy with strtomem
@ 2023-10-23 19:14 Justin Stitt
  2023-10-24 23:46 ` Kees Cook
  2023-10-25 10:23 ` Vasily Gorbik
  0 siblings, 2 replies; 4+ messages in thread
From: Justin Stitt @ 2023-10-23 19:14 UTC (permalink / raw)
  To: Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle
  Cc: linux-s390, linux-kernel, linux-hardening, Justin Stitt

Let's move away from using strncpy() as it is deprecated [1].

Instead use strtomem() as `e.id` is already marked as nonstring:
|       char id[4] __nonstring;

We don't need strtomem_pad() because `e` is already memset to 0 --
rendering any additional NUL-padding useless.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/s390/char/sclp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/s390/char/sclp.c b/drivers/s390/char/sclp.c
index 8f74db689a0c..486cf57a1605 100644
--- a/drivers/s390/char/sclp.c
+++ b/drivers/s390/char/sclp.c
@@ -81,7 +81,7 @@ static inline void sclp_trace(int prio, char *id, u32 a, u64 b, bool err)
 	struct sclp_trace_entry e;
 
 	memset(&e, 0, sizeof(e));
-	strncpy(e.id, id, sizeof(e.id));
+	strtomem(e.id, id);
 	e.a = a;
 	e.b = b;
 	debug_event(&sclp_debug, prio, &e, sizeof(e));

---
base-commit: 9c5d00cb7b6bbc5a7965d9ab7d223b5402d1f02c
change-id: 20231023-strncpy-drivers-s390-char-sclp-c-bb66226a7eaa

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] s390/sclp: replace deprecated strncpy with strtomem
  2023-10-23 19:14 [PATCH] s390/sclp: replace deprecated strncpy with strtomem Justin Stitt
@ 2023-10-24 23:46 ` Kees Cook
  2023-10-24 23:57   ` Kees Cook
  2023-10-25 10:23 ` Vasily Gorbik
  1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2023-10-24 23:46 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle, linux-s390, linux-kernel,
	linux-hardening

On Mon, Oct 23, 2023 at 07:14:49PM +0000, Justin Stitt wrote:
> Let's move away from using strncpy() as it is deprecated [1].
> 
> Instead use strtomem() as `e.id` is already marked as nonstring:
> |       char id[4] __nonstring;

I'm surprised the compiler didn't complain about this -- I thought that
was the point of the __nonstring attribute: to diagnose when used with a
string function. Hmpf.

> 
> We don't need strtomem_pad() because `e` is already memset to 0 --
> rendering any additional NUL-padding useless.
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
> 
> Found with: $ rg "strncpy\("
> ---
>  drivers/s390/char/sclp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/s390/char/sclp.c b/drivers/s390/char/sclp.c
> index 8f74db689a0c..486cf57a1605 100644
> --- a/drivers/s390/char/sclp.c
> +++ b/drivers/s390/char/sclp.c
> @@ -81,7 +81,7 @@ static inline void sclp_trace(int prio, char *id, u32 a, u64 b, bool err)
>  	struct sclp_trace_entry e;
>  
>  	memset(&e, 0, sizeof(e));
> -	strncpy(e.id, id, sizeof(e.id));
> +	strtomem(e.id, id);

Looks right to me; thanks!

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

>  	e.a = a;
>  	e.b = b;
>  	debug_event(&sclp_debug, prio, &e, sizeof(e));
> 
> ---
> base-commit: 9c5d00cb7b6bbc5a7965d9ab7d223b5402d1f02c
> change-id: 20231023-strncpy-drivers-s390-char-sclp-c-bb66226a7eaa
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 
> 

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] s390/sclp: replace deprecated strncpy with strtomem
  2023-10-24 23:46 ` Kees Cook
@ 2023-10-24 23:57   ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2023-10-24 23:57 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
	Christian Borntraeger, Sven Schnelle, linux-s390, linux-kernel,
	linux-hardening

On Tue, Oct 24, 2023 at 04:46:04PM -0700, Kees Cook wrote:
> > Instead use strtomem() as `e.id` is already marked as nonstring:
> > |       char id[4] __nonstring;
> 
> I'm surprised the compiler didn't complain about this -- I thought that
> was the point of the __nonstring attribute: to diagnose when used with a
> string function. Hmpf.

Wow. GCC only warns if you call strlen() on it, but literally nothing
else... :(

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] s390/sclp: replace deprecated strncpy with strtomem
  2023-10-23 19:14 [PATCH] s390/sclp: replace deprecated strncpy with strtomem Justin Stitt
  2023-10-24 23:46 ` Kees Cook
@ 2023-10-25 10:23 ` Vasily Gorbik
  1 sibling, 0 replies; 4+ messages in thread
From: Vasily Gorbik @ 2023-10-25 10:23 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Heiko Carstens, Alexander Gordeev, Christian Borntraeger,
	Sven Schnelle, linux-s390, linux-kernel, linux-hardening

On Mon, Oct 23, 2023 at 07:14:49PM +0000, Justin Stitt wrote:
> Let's move away from using strncpy() as it is deprecated [1].
> 
> Instead use strtomem() as `e.id` is already marked as nonstring:
> |       char id[4] __nonstring;
> 
> We don't need strtomem_pad() because `e` is already memset to 0 --
> rendering any additional NUL-padding useless.
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> Link: https://github.com/KSPP/linux/issues/90
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Note: build-tested only.
> 
> Found with: $ rg "strncpy\("
> ---
>  drivers/s390/char/sclp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Applied, thank you!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-25 10:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-23 19:14 [PATCH] s390/sclp: replace deprecated strncpy with strtomem Justin Stitt
2023-10-24 23:46 ` Kees Cook
2023-10-24 23:57   ` Kees Cook
2023-10-25 10:23 ` Vasily Gorbik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox