Skip to content

Commit 8aae2da

Browse files
keesjmberg-intel
authored andcommitted
um: Replace strncpy() with strnlen()+memcpy_and_pad() in strncpy_chunk_from_user()
Replace the deprecated[1] strncpy() with strnlen() on the source followed by memcpy_and_pad(). This function is a chunk callback for UML's strncpy_from_user() implementation, called by buffer_op() to process userspace memory one page at a time. The source is a kernel-mapped userspace address that is not guaranteed to be NUL-terminated; "len" bounds how many bytes to read from it. By measuring the source string length first with strnlen(), we avoid reading past the NUL terminator in the source. memcpy_and_pad() then copies the string content and zero-fills the remainder of the chunk, preserving the original strncpy() behavior exactly: copy up to the first NUL, then pad with zeros to the full length. strtomem_pad() would be the idiomatic helper for this strnlen() + memcpy_and_pad() pattern, but it requires a compile-time-determinable destination size (via ARRAY_SIZE()). Here the destination is a char * into a caller-provided buffer and the chunk length is a runtime value, so the explicit two-step is necessary. No behavioral change: the same bytes are written to the destination (string content followed by zero padding), the pointer advances by the same amount, and the NUL-found return condition is unchanged. Link: KSPP/linux#90 [1] Signed-off-by: Kees Cook <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Johannes Berg <[email protected]>
1 parent d1895c1 commit 8aae2da

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

arch/um/kernel/skas/uaccess.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
170170
char **to_ptr = arg, *to = *to_ptr;
171171
int n;
172172

173-
strncpy(to, (void *) from, len);
174-
n = strnlen(to, len);
173+
n = strnlen((void *) from, len);
174+
memcpy_and_pad(to, len, (void *) from, n, 0);
175175
*to_ptr += n;
176176

177177
if (n < len)

0 commit comments

Comments
 (0)