Skip to content

Commit

Permalink
um: refactor deprecated strncpy to memcpy
Browse files Browse the repository at this point in the history
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: KSPP#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>
Link: https://lore.kernel.org/r/20230809-arch-um-v3-1-f63e1122d77e@google.com
Signed-off-by: Kees Cook <keescook@chromium.org>
  • Loading branch information
JustinStitt authored and kees committed Aug 16, 2023
1 parent 30bed99 commit be8dffa
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions arch/um/drivers/mconsole_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand Down

0 comments on commit be8dffa

Please sign in to comment.