* [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat
@ 2023-05-30 8:39 Alexander Potapenko
2023-05-30 8:40 ` Alexander Potapenko
2023-05-30 23:10 ` Kees Cook
0 siblings, 2 replies; 4+ messages in thread
From: Alexander Potapenko @ 2023-05-30 8:39 UTC (permalink / raw)
To: glider, andy, akpm
Cc: linux-kernel, linux-mm, elver, dvyukov, kasan-dev, ndesaulniers,
nathan, keescook
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>
Link: https://lore.kernel.org/all/20230224085942.1791837-1-elver@google.com/
Acked-by: Kees Cook <keescook@chromium.org>
---
lib/string.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/string.c b/lib/string.c
index 3d55ef8901068..be26623953d2e 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -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.41.0.rc0.172.g3f132b7071-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat
2023-05-30 8:39 [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat Alexander Potapenko
@ 2023-05-30 8:40 ` Alexander Potapenko
2023-05-30 23:10 ` Kees Cook
1 sibling, 0 replies; 4+ messages in thread
From: Alexander Potapenko @ 2023-05-30 8:40 UTC (permalink / raw)
To: glider, andy, akpm
Cc: linux-kernel, linux-mm, elver, dvyukov, kasan-dev, ndesaulniers,
nathan, keescook
On Tue, May 30, 2023 at 10:39 AM Alexander Potapenko <glider@google.com> wrote:
>
> 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>
> Link: https://lore.kernel.org/all/20230224085942.1791837-1-elver@google.com/
Sorry, missed a "Reviewed-by: Marco Elver <elver@google.com>" from the
previous thread:
https://lore.kernel.org/lkml/CAG_fn=UzQ-jnQrxzvLE6EV37zSVCOGPmsVTxyfp1wXzBir4vAg@mail.gmail.com/T/
> Acked-by: Kees Cook <keescook@chromium.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat
2023-05-30 8:39 [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat Alexander Potapenko
2023-05-30 8:40 ` Alexander Potapenko
@ 2023-05-30 23:10 ` Kees Cook
2023-05-31 7:57 ` Alexander Potapenko
1 sibling, 1 reply; 4+ messages in thread
From: Kees Cook @ 2023-05-30 23:10 UTC (permalink / raw)
To: andy, Andrew Morton, glider
Cc: Kees Cook, nathan, dvyukov, elver, linux-kernel, linux-mm,
kasan-dev, ndesaulniers
On Tue, 30 May 2023 10:39:11 +0200, Alexander Potapenko wrote:
> 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
--
Kees Cook
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat
2023-05-30 23:10 ` Kees Cook
@ 2023-05-31 7:57 ` Alexander Potapenko
0 siblings, 0 replies; 4+ messages in thread
From: Alexander Potapenko @ 2023-05-31 7:57 UTC (permalink / raw)
To: Kees Cook
Cc: andy, Andrew Morton, nathan, dvyukov, elver, linux-kernel,
linux-mm, kasan-dev, ndesaulniers
On Wed, May 31, 2023 at 1:10 AM Kees Cook <keescook@chromium.org> wrote:
>
> On Tue, 30 May 2023 10:39:11 +0200, Alexander Potapenko wrote:
> > 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
Note that Andrew also picked it to mm-unstable
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-05-31 7:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-30 8:39 [PATCH v2] string: use __builtin_memcpy() in strlcpy/strlcat Alexander Potapenko
2023-05-30 8:40 ` Alexander Potapenko
2023-05-30 23:10 ` Kees Cook
2023-05-31 7:57 ` Alexander Potapenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).