Skip to content

Commit f9cfb19

Browse files
ramosian-gliderkees
authored andcommitted
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. Suggested-by: Marco Elver <elver@google.com> Signed-off-by: Alexander Potapenko <glider@google.com> Reviewed-by: Marco Elver <elver@google.com> Link: https://lore.kernel.org/all/20230224085942.1791837-1-elver@google.com/ Acked-by: Kees Cook <keescook@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20230530083911.1104336-1-glider@google.com
1 parent 2af4aa3 commit f9cfb19

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

lib/string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ size_t strlcpy(char *dest, const char *src, size_t size)
110110

111111
if (size) {
112112
size_t len = (ret >= size) ? size - 1 : ret;
113-
memcpy(dest, src, len);
113+
__builtin_memcpy(dest, src, len);
114114
dest[len] = '\0';
115115
}
116116
return ret;
@@ -260,7 +260,7 @@ size_t strlcat(char *dest, const char *src, size_t count)
260260
count -= dsize;
261261
if (len >= count)
262262
len = count-1;
263-
memcpy(dest, src, len);
263+
__builtin_memcpy(dest, src, len);
264264
dest[len] = 0;
265265
return res;
266266
}

0 commit comments

Comments
 (0)