diff mbox series

[v3] um: refactor deprecated strncpy to memcpy

Message ID 20230809-arch-um-v3-1-f63e1122d77e@google.com (mailing list archive)
State Mainlined
Commit be8dffa04de3894bad0bf31d1531a2f06353b70e
Headers show
Series [v3] um: refactor deprecated strncpy to memcpy | expand

Commit Message

Justin Stitt Aug. 9, 2023, 6:19 p.m. UTC
Use `memcpy` since `console_buf` is not expected to be NUL-terminated
and it more accurately describes what is happening with the buffers
`console_buf` and `string` as per Kees' analysis [1].

Also mark char buffer as `__nonstring` as per Kees' suggestion [2].

This change now makes it more clear what this code does and that
`console_buf` is not expected to be NUL-terminated.

Link: https://lore.kernel.org/all/202308081708.D5ADC80F@keescook/ [1]
Link: https://github.com/KSPP/linux/issues/90 [2]
Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Cc: linux-hardening@vger.kernel.org
Suggested-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Changes in v3:
- Swap `strtomem` to `memcpy` (thanks Kees)
- Link to v2: https://lore.kernel.org/r/20230808-arch-um-v2-1-5fbe76eaf853@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.
---
 arch/um/drivers/mconsole_kern.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


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

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

Comments

Kees Cook Aug. 10, 2023, 7:05 p.m. UTC | #1
On Wed, Aug 09, 2023 at 06:19:32PM +0000, Justin Stitt wrote:
> Use `memcpy` since `console_buf` is not expected to be NUL-terminated
> and it more accurately describes what is happening with the buffers
> `console_buf` and `string` as per Kees' analysis [1].
> 
> Also mark char buffer as `__nonstring` as per Kees' suggestion [2].
> 
> This change now makes it more clear what this code does and that
> `console_buf` is not expected to be NUL-terminated.
> 
> Link: https://lore.kernel.org/all/202308081708.D5ADC80F@keescook/ [1]
> Link: https://github.com/KSPP/linux/issues/90 [2]
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> Cc: linux-hardening@vger.kernel.org
> Suggested-by: Kees Cook <keescook@chromium.org>
> Signed-off-by: Justin Stitt <justinstitt@google.com>

Thanks for digging through this!

Reviewed-by: Kees Cook <keescook@chromium.org>
Kees Cook Aug. 16, 2023, 9:01 p.m. UTC | #2
On Wed, 09 Aug 2023 18:19:32 +0000, Justin Stitt wrote:
> Use `memcpy` since `console_buf` is not expected to be NUL-terminated
> and it more accurately describes what is happening with the buffers
> `console_buf` and `string` as per Kees' analysis [1].
> 
> Also mark char buffer as `__nonstring` as per Kees' suggestion [2].
> 
> This change now makes it more clear what this code does and that
> `console_buf` is not expected to be NUL-terminated.
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] um: refactor deprecated strncpy to memcpy
      https://git.kernel.org/kees/c/be8dffa04de3

Take care,
diff mbox series

Patch

diff --git a/arch/um/drivers/mconsole_kern.c b/arch/um/drivers/mconsole_kern.c
index 5026e7b9adfe..ff4bda95b9c7 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);
+		memcpy(console_buf, string, n);
 		string += n;
 		len -= n;