Skip to contentSkip to conversation

string: use __builtin_memcpy() in strlcpy/strlcat

PATCH Applied Alexander Potapenko 2 participants 4 messages May 30, 2023 – May 31, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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
index 3d55ef8901068..be26623953d2e 100644
@@ -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.41.0.rc0.172.g3f132b7071-goog
#1 lore raw
Quoted 15 lines · Alexander Potapenko

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.

Quoted 7 lines

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.

Sorry, missed a "Reviewed-by: Marco Elver <[email protected]>" from the previous thread:
https://lore.kernel.org/lkml/CAG_fn=UzQ-jnQrxzvLE6EV37zSVCOGPmsVTxyfp1wXzBir4vAg@mail.gmail.com/T/

Quoted 1 line
Applied
#2 lore raw
In reply to Alexander Potapenko
Quoted 11 lines · Alexander Potapenko

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.

[...]

Applied to for-next/hardening, thanks!

[1/1] string: use __builtin_memcpy() in strlcpy/strlcat

      https://git.kernel.org/kees/c/cfe93c8c9a7a
Signature · 1 line
Kees Cook
#3 lore raw
Quoted 12 lines · Kees Cook
Quoted 10 lines · Alexander Potapenko

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.

Quoted 6 lines
Quoted 1 line

[...]

Applied to for-next/hardening, thanks!

[1/1] string: use __builtin_memcpy() in strlcpy/strlcat

      https://git.kernel.org/kees/c/cfe93c8c9a7a

Note that Andrew also picked it to mm-unstable