From patchwork Thu Sep 15 15:04:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12977553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A3FAC6FA89 for ; Thu, 15 Sep 2022 15:06:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D04F98D000A; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB5A48D0001; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B56D48D000A; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A5D5D8D0001 for ; Thu, 15 Sep 2022 11:06:16 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8A05DA03AB for ; Thu, 15 Sep 2022 15:06:16 +0000 (UTC) X-FDA: 79914645552.11.7D79C9F Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.202]) by imf27.hostedemail.com (Postfix) with ESMTP id B340A4010C for ; Thu, 15 Sep 2022 15:06:15 +0000 (UTC) Received: by mail-lj1-f202.google.com with SMTP id e1-20020a2e8ec1000000b0026c27b66a2aso2094351ljl.11 for ; Thu, 15 Sep 2022 08:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=eNKbMoJoeuDScU3OOR/LNFBkpoKPQhsynLwHhgiKEFDLcfCUexACZU4WQwQbZu34vG 7EoMGIrYWbmu1bNMt0W+B5g5HgcQObOvffkhInl291EPfXwfk6f1zh4XfEiJsxWsenlr Ed4YXhAoY4YGF7IFsdXO3XaeL5U81+P1LxeR/adMP2xTvKy7uUHwo3zzlz3YILCmQAM9 oGJR9XF/iCNgNM5pAcXM1COHTmQdzkg1GvzZO0f7nHLLKPfwPGUECgDQMa1Sx6Ary1cf j/6i5CyxJIXk+i7w4Sm1NuosHfgLH7wIyV5t0WH39UMQ7ye73Vr8soMmOIHTNweZUxjp PWwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=vNHlnC6+l+yT1tBn14gSB26K5jRcQenHNAmCXz2yKB2/IvX5gG64/VdunlVN9Q+kA+ EEGc1RUN0uFYrRNhWTWuJpTmhrnsa2bQfGbDDJ2OATwUEmWiHJoe5nMi/5Q1f6q7dfmF R7fTx6oo6euqELt+76zmbxWZh2WwSMUQ1gQnW96htAPm5hyuYHa23S81BSGGFqna91ZI 73FidM42pu6oZHnw8eu01vT7vt2RSobJ5QVmIBIIZTO+XI6CPLaZuSR7s6hB0w2Y6Ruz ml59ugtnrqrjLSuzjmdQH9SQbNtY5w1Tp21hUJm23q3ZuEfvusIRbTslOvm67WlqymIt 1UlQ== X-Gm-Message-State: ACrzQf3I7eVJiPF/NFQ0jopbo5NVpm7SOfTAb6wcMAoEdPpP8OuqQ1Wf mnaXmscKG48bfpKQ7s7YrV5/16CJABw= X-Google-Smtp-Source: AMsMyM5qrEYlyDR9o7INh6dG4GptGhlGiHCpycCy1PeKN2X8Ws1c8xuxIuaS5rjB9JFmB5DaKlv5ICDQnbI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:ac2:4f03:0:b0:496:e4:4d16 with SMTP id k3-20020ac24f03000000b0049600e44d16mr117698lfr.250.1663254373987; Thu, 15 Sep 2022 08:06:13 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:09 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-36-glider@google.com> Subject: [PATCH v7 35/43] x86: kmsan: use __msan_ string functions where possible. From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Biggers , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Stephen Rothwell , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eNKbMoJo; spf=pass (imf27.hostedemail.com: domain of 3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.208.202 as permitted sender) smtp.mailfrom=3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663254375; a=rsa-sha256; cv=none; b=0RmktIzr4drqal0BDkaucVp9us8jJI7jV0ApCB0uTEeyZIIc54U5DFQAHzx4C/QjeOVXrf yC+lxWKHET0BKrWtzL4as71JbySYpDq6jgSFUw0nPzq3kszBAHUyXuI6tmnwXLu74A2Rfx aJy0qksrNa/+g6kJxw817LO/U2fM5bM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663254375; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s0jAEbnxevXU2w5QvhShkO+su70yEEyH0HIO5IJWDY4=; b=1NoVIpyaQpOl3JbohWgkHLEB09vpdI4d1pPh31cFUIhiQyqNOFezu/DAXmQOHKd8vfVAY4 jXVUuDmhgR4q36qv5IrD+vJwNc4py0l2Rm9bf7ClBxUL+hHCadmznN0jbXdeChf/eQUxta ca7/oT3XH9nd0TpYm9HvdAHKUY2Unlg= X-Rspam-User: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B340A4010C Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=eNKbMoJo; spf=pass (imf27.hostedemail.com: domain of 3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com designates 209.85.208.202 as permitted sender) smtp.mailfrom=3ZT8jYwYKCJA052xyB08805y.w86527EH-664Fuw4.8B0@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: gqyiyux4ztzenag59kahafccqx1tfznh X-HE-Tag: 1663254375-625268 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Unless stated otherwise (by explicitly calling __memcpy(), __memset() or __memmove()) we want all string functions to call their __msan_ versions (e.g. __msan_memcpy() instead of memcpy()), so that shadow and origin values are updated accordingly. Bootloader must still use the default string functions to avoid crashes. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I7ca9bd6b4f5c9b9816404862ae87ca7984395f33 --- arch/x86/include/asm/string_64.h | 23 +++++++++++++++++++++-- include/linux/fortify-string.h | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 6e450827f677a..3b87d889b6e16 100644 --- a/arch/x86/include/asm/string_64.h +++ b/arch/x86/include/asm/string_64.h @@ -11,11 +11,23 @@ function. */ #define __HAVE_ARCH_MEMCPY 1 +#if defined(__SANITIZE_MEMORY__) +#undef memcpy +void *__msan_memcpy(void *dst, const void *src, size_t size); +#define memcpy __msan_memcpy +#else extern void *memcpy(void *to, const void *from, size_t len); +#endif extern void *__memcpy(void *to, const void *from, size_t len); #define __HAVE_ARCH_MEMSET +#if defined(__SANITIZE_MEMORY__) +extern void *__msan_memset(void *s, int c, size_t n); +#undef memset +#define memset __msan_memset +#else void *memset(void *s, int c, size_t n); +#endif void *__memset(void *s, int c, size_t n); #define __HAVE_ARCH_MEMSET16 @@ -55,7 +67,13 @@ static inline void *memset64(uint64_t *s, uint64_t v, size_t n) } #define __HAVE_ARCH_MEMMOVE +#if defined(__SANITIZE_MEMORY__) +#undef memmove +void *__msan_memmove(void *dest, const void *src, size_t len); +#define memmove __msan_memmove +#else void *memmove(void *dest, const void *src, size_t count); +#endif void *__memmove(void *dest, const void *src, size_t count); int memcmp(const void *cs, const void *ct, size_t count); @@ -64,8 +82,7 @@ char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); int strcmp(const char *cs, const char *ct); -#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) - +#if (defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)) /* * For files that not instrumented (e.g. mm/slub.c) we * should use not instrumented version of mem* functions. @@ -73,7 +90,9 @@ int strcmp(const char *cs, const char *ct); #undef memcpy #define memcpy(dst, src, len) __memcpy(dst, src, len) +#undef memmove #define memmove(dst, src, len) __memmove(dst, src, len) +#undef memset #define memset(s, c, n) __memset(s, c, n) #ifndef __NO_FORTIFY diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 3b401fa0f3746..6c8a1a29d0b63 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -285,8 +285,10 @@ __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size, * __builtin_object_size() must be captured here to avoid evaluating argument * side-effects further into the macro layers. */ +#ifndef CONFIG_KMSAN #define memset(p, c, s) __fortify_memset_chk(p, c, s, \ __builtin_object_size(p, 0), __builtin_object_size(p, 1)) +#endif /* * To make sure the compiler can enforce protection against buffer overflows,