[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6446ad55.170a0220.c82cd.cedc@mx.google.com>
Date: Mon, 24 Apr 2023 09:24:52 -0700
From: Kees Cook <keescook@...omium.org>
To: Alexander Potapenko <glider@...gle.com>
Cc: linux-kernel@...r.kernel.org, linux-mm@...ck.org,
akpm@...ux-foundation.org, elver@...gle.com, dvyukov@...gle.com,
kasan-dev@...glegroups.com, andy@...nel.org,
ndesaulniers@...gle.com, nathan@...nel.org
Subject: Re: [PATCH] string: use __builtin_memcpy() in strlcpy/strlcat
On Mon, Apr 24, 2023 at 01:23:13PM +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.
>
> 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@...gle.com>
> Signed-off-by: Alexander Potapenko <glider@...gle.com>
> Link: https://lore.kernel.org/all/20230224085942.1791837-1-elver@google.com/
> ---
> 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;
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?
--
Kees Cook
Powered by blists - more mailing lists