string: use __builtin_memcpy() in strlcpy/strlcat
Subsystems (3)
PATCH 1/1 string: use __builtin_memcpy() in strlcpy/strlcat
lib/string.c is built with -ffreestanding, which prevents the compiler from replacing certain functions with calls to their library versions.
On the other hand, this also prevents Clang and GCC from instrumenting calls to memcpy() when building with KASAN, KCSAN or KMSAN:
- KASAN normally replaces memcpy() with __asan_memcpy() with the additional cc-param,asan-kernel-mem-intrinsic-prefix=1;
- KCSAN and KMSAN replace memcpy() with __tsan_memcpy() and __msan_memcpy() by default.
To let the tools catch memory accesses from strlcpy/strlcat, replace the calls to memcpy() with __builtin_memcpy(), which KASAN, KCSAN and KMSAN are able to replace even in -ffreestanding mode.
This preserves the behavior in normal builds (__builtin_memcpy() ends up being replaced with memcpy()), and does not introduce new instrumentation in unwanted places, as strlcpy/strlcat are already instrumented.
--- lib/string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/string.c +2 -2
@@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size)if (size) {size_t len = (ret >= size) ? size -1 : ret; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] ='\0' ; }return ret;
@@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count) count -= dsize;if (len >= count) len = count-1 ; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] =0 ;return res; }
Signature · 1 line
2.40.0.634.g4ca3ef3211-goog
Quoted 15 lines · Alexander Potapenko
lib/string.cis built with -ffreestanding, which prevents the compiler from replacing certain functions with calls to their library versions.On the other hand, this also prevents Clang and GCC from instrumenting calls to
memcpy()when building with KASAN, KCSAN or KMSAN:
- KASAN normally replacesmemcpy()with__asan_memcpy()with the additional cc-param,asan-kernel-mem-intrinsic-prefix=1;
- KCSAN and KMSAN replacememcpy()with__tsan_memcpy()and__msan_memcpy()by default.To let the tools catch memory accesses from strlcpy/strlcat, replace the calls to
memcpy()with__builtin_memcpy(), which KASAN, KCSAN and KMSAN are able to replace even in -ffreestanding mode.
Quoted 7 lines
This preserves the behavior in normal builds (
__builtin_memcpy()ends up being replaced withmemcpy()), and does not introduce new instrumentation in unwanted places, as strlcpy/strlcat are already instrumented.Suggested-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>
Looks reasonable.
Quoted 29 lines
--- lib/string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/string.c +2
-3@@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size)if (size) {size_t len = (ret >= size) ? size -1 : ret; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] ='\0' ; }return ret;@@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count) count -= dsize;if (len >= count) len = count-1 ; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] =0 ;return res; } --2.40.0.634.g4ca3ef3211-goog
Quoted 39 lines · Alexander Potapenko
lib/string.cis built with -ffreestanding, which prevents the compiler from replacing certain functions with calls to their library versions.On the other hand, this also prevents Clang and GCC from instrumenting calls to
memcpy()when building with KASAN, KCSAN or KMSAN:
- KASAN normally replacesmemcpy()with__asan_memcpy()with the additional cc-param,asan-kernel-mem-intrinsic-prefix=1;
- KCSAN and KMSAN replacememcpy()with__tsan_memcpy()and__msan_memcpy()by default.To let the tools catch memory accesses from strlcpy/strlcat, replace the calls to
memcpy()with__builtin_memcpy(), which KASAN, KCSAN and KMSAN are able to replace even in -ffreestanding mode.This preserves the behavior in normal builds (
__builtin_memcpy()ends up being replaced withmemcpy()), and does not introduce new instrumentation in unwanted places, as strlcpy/strlcat are already instrumented.Suggested-by Marco Elver <[email protected]>Signed-off-by Alexander Potapenko <[email protected]>--- lib/string.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/string.c +1
-1@@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size)if (size) {size_t len = (ret >= size) ? size -1 : ret; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] ='\0' ; }return ret;
lib/string.c size_t strlcat(char *dest, const char *src, size_t count) · line 265
Quoted 7 lines
lib/string.c +1
-1@@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count) count -= dsize;if (len >= count) len = count-1 ; -memcpy (dest, src, len); +__builtin_memcpy (dest, src, len); dest[len] =0 ;return res;
I *think* this isn't a problem for CONFIG_FORTIFY, since these will be replaced and checked separately -- but it still seems strange that you need to explicitly use __builtin_memcpy.
Does this end up changing fortify coverage?
Signature · 1 line
Kees Cook
Quoted 6 lines
I *think* this isn't a problem for
CONFIG_FORTIFY, since these will be replaced and checked separately -- but it still seems strange that you need to explicitly use__builtin_memcpy.Does this end up changing fortify coverage?
Is fortify relevant here? Note that the whole file is compiled with __NO_FORTIFY.
Quoted 6 lines · Alexander Potapenko
Quoted 4 lines
FORTIFY_SOURCEglidear
I *think* this isn't a problem forCONFIG_FORTIFY, since these will be replaced and checked separately -- but it still seems strange that you need to explicitly use__builtin_memcpy.
Or did you mean we'd better use __underlying_memcpy() here instead? I am a bit puzzled.
Quoted 6 lines · Alexander Potapenko
Quoted 1 line · Alexander Potapenko
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
I *think* this isn't a problem for
CONFIG_FORTIFY, since these will be replaced and checked separately -- but it still seems strange that you need to explicitly use__builtin_memcpy.Or did you mean we'd better use
__underlying_memcpy()here instead? I am a bit puzzled.
Kees told me offline that the patch in question is fine. @Andrew, would it be possible to queue it for 6.4?
--
Alexander Potapenko
Software Engineer
Google Germany GmbH
Erika-Mann-Straße, 33
80636 München
Geschäftsführer: Paul Manicle, Liana Sebastian
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Kees Cook
Acked-byQuoted 9 lines · Alexander Potapenko
Quoted 6 lines
I *think* this isn't a problem for
CONFIG_FORTIFY, since these will be replaced and checked separately -- but it still seems strange that you need to explicitly use__builtin_memcpy.Does this end up changing fortify coverage?
Is fortify relevant here? Note that the whole file is compiled with
__NO_FORTIFY.
Yeah, agreed. I think I was just curious if that got verified. I'm good with this.
Signature · 1 line
Kees Cook