linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] um: refactor deprecated strncpy to strtomem
@ 2023-08-08 21:17 Justin Stitt
  2023-08-09  0:42 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2023-08-08 21:17 UTC (permalink / raw)
  To: Richard Weinberger, Anton Ivanov, Johannes Berg
  Cc: linux-hardening, linux-um, linux-kernel, Kees Cook, Justin Stitt

Favor a more robust and less ambiguous interface over `strncpy`.

Use `strtomem` here since `console_buf` is not expected to be
NUL-terminated.

Also mark char buffer as `__nonstring` as per Kees' suggestion here [1]

Link: https://github.com/KSPP/linux/issues/90 [1]
Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Changes in v2:
- Keep usage of ARRAY_SIZE (thanks Bill)
- Remove unnecessary import (thanks Kees and Bill)
- Link to v1: https://lore.kernel.org/r/20230807-arch-um-v1-1-86dbbfb59709@google.com
---
Notes:
I only build tested this patch.

I am also making an assumption that `console_buf` need not be
NUL-terminated. Am I right in believing this?
---
 arch/um/drivers/mconsole_kern.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/um/drivers/mconsole_kern.c b/arch/um/drivers/mconsole_kern.c
index 5026e7b9adfe..d5fa9c8dcd18 100644
--- a/arch/um/drivers/mconsole_kern.c
+++ b/arch/um/drivers/mconsole_kern.c
@@ -554,7 +554,7 @@ struct mconsole_output {
 
 static DEFINE_SPINLOCK(client_lock);
 static LIST_HEAD(clients);
-static char console_buf[MCONSOLE_MAX_DATA];
+static char console_buf[MCONSOLE_MAX_DATA] __nonstring;
 
 static void console_write(struct console *console, const char *string,
 			  unsigned int len)
@@ -567,7 +567,7 @@ static void console_write(struct console *console, const char *string,
 
 	while (len > 0) {
 		n = min((size_t) len, ARRAY_SIZE(console_buf));
-		strncpy(console_buf, string, n);
+		strtomem(console_buf, string);
 		string += n;
 		len -= n;
 

---
base-commit: c1a515d3c0270628df8ae5f5118ba859b85464a2
change-id: 20230807-arch-um-3ef24413427e

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


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

* Re: [PATCH v2] um: refactor deprecated strncpy to strtomem
  2023-08-08 21:17 [PATCH v2] um: refactor deprecated strncpy to strtomem Justin Stitt
@ 2023-08-09  0:42 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2023-08-09  0:42 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Richard Weinberger, Anton Ivanov, Johannes Berg, linux-hardening,
	linux-um, linux-kernel

On Tue, Aug 08, 2023 at 09:17:35PM +0000, Justin Stitt wrote:
> Favor a more robust and less ambiguous interface over `strncpy`.
> 
> Use `strtomem` here since `console_buf` is not expected to be
> NUL-terminated.
> 
> Also mark char buffer as `__nonstring` as per Kees' suggestion here [1]
> 
> Link: https://github.com/KSPP/linux/issues/90 [1]
> Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Changes in v2:
> - Keep usage of ARRAY_SIZE (thanks Bill)
> - Remove unnecessary import (thanks Kees and Bill)
> - Link to v1: https://lore.kernel.org/r/20230807-arch-um-v1-1-86dbbfb59709@google.com
> ---
> Notes:
> I only build tested this patch.
> 
> I am also making an assumption that `console_buf` need not be
> NUL-terminated. Am I right in believing this?
> ---
>  arch/um/drivers/mconsole_kern.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/um/drivers/mconsole_kern.c b/arch/um/drivers/mconsole_kern.c
> index 5026e7b9adfe..d5fa9c8dcd18 100644
> --- a/arch/um/drivers/mconsole_kern.c
> +++ b/arch/um/drivers/mconsole_kern.c
> @@ -554,7 +554,7 @@ struct mconsole_output {
>  
>  static DEFINE_SPINLOCK(client_lock);
>  static LIST_HEAD(clients);
> -static char console_buf[MCONSOLE_MAX_DATA];
> +static char console_buf[MCONSOLE_MAX_DATA] __nonstring;
>  
>  static void console_write(struct console *console, const char *string,
>  			  unsigned int len)
> @@ -567,7 +567,7 @@ static void console_write(struct console *console, const char *string,
>  
>  	while (len > 0) {
>  		n = min((size_t) len, ARRAY_SIZE(console_buf));
> -		strncpy(console_buf, string, n);
> +		strtomem(console_buf, string);

See my reply to v1: I think this should just be memcpy(console_buf, string, n)

-Kees

>  		string += n;
>  		len -= n;
>  
> 
> ---
> base-commit: c1a515d3c0270628df8ae5f5118ba859b85464a2
> change-id: 20230807-arch-um-3ef24413427e
> 
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
> 

-- 
Kees Cook

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

end of thread, other threads:[~2023-08-09  0:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-08 21:17 [PATCH v2] um: refactor deprecated strncpy to strtomem Justin Stitt
2023-08-09  0:42 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).