tracing/uprobe: Replace strlcpy() with strscpy()
authorKees Cook <keescook@chromium.org>
Thu, 30 Nov 2023 20:56:08 +0000 (12:56 -0800)
committerKees Cook <keescook@chromium.org>
Fri, 1 Dec 2023 18:25:35 +0000 (10:25 -0800)
strlcpy() reads the entire source buffer first. This read may exceed
the destination size limit. This is both inefficient and can lead
to linear read overflows if a source string is not NUL-terminated[1].
Additionally, it returns the size of the source string, not the
resulting size of the destination string. In an effort to remove strlcpy()
completely[2], replace strlcpy() here with strscpy().

The negative return value is already handled by this code so no new
handling is needed here.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy
Link: https://github.com/KSPP/linux/issues/89
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: linux-trace-kernel@vger.kernel.org
Acked-by: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Link: https://lore.kernel.org/r/20231130205607.work.463-kees@kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
kernel/trace/trace_uprobe.c

index 99c051de412afa4d6a433d1250a7abbd0d2deb3f..a84b85d8aac12464b80312431e68ceb46c5b19c5 100644 (file)
@@ -151,7 +151,7 @@ fetch_store_string(unsigned long addr, void *dest, void *base)
                return -ENOMEM;
 
        if (addr == FETCH_TOKEN_COMM)
-               ret = strlcpy(dst, current->comm, maxlen);
+               ret = strscpy(dst, current->comm, maxlen);
        else
                ret = strncpy_from_user(dst, src, maxlen);
        if (ret >= 0) {