fortify: Short-circuit known-safe calls to strscpy()
authorKees Cook <keescook@chromium.org>
Sun, 2 Oct 2022 16:17:03 +0000 (09:17 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 1 Nov 2022 17:04:52 +0000 (10:04 -0700)
commit62e1cbfc5d795381a0f237ae7ee229a92d51cf9e
treecf730cfe96c528feef173c9e5ae642485ac84f44
parent41eefc46a3a4682976afb5f8c4b9734ed6bfd406
fortify: Short-circuit known-safe calls to strscpy()

Replacing compile-time safe calls of strcpy()-related functions with
strscpy() was always calling the full strscpy() logic when a builtin
would be better. For example:

char buf[16];
strcpy(buf, "yes");

would reduce to __builtin_memcpy(buf, "yes", 4), but not if it was:

strscpy(buf, yes, sizeof(buf));

Fix this by checking if all sizes are known at compile-time.

Cc: linux-hardening@vger.kernel.org
Tested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
include/linux/fortify-string.h
lib/strscpy_kunit.c