From patchwork Tue Dec 14 16:20:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12676409 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 DD05FC433EF for ; Tue, 14 Dec 2021 16:42:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FA636B00A6; Tue, 14 Dec 2021 11:23:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AA7A6B00A7; Tue, 14 Dec 2021 11:23:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 398E36B00A8; Tue, 14 Dec 2021 11:23:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0240.hostedemail.com [216.40.44.240]) by kanga.kvack.org (Postfix) with ESMTP id 29EC16B00A6 for ; Tue, 14 Dec 2021 11:23:45 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E0F00181AEF0B for ; Tue, 14 Dec 2021 16:23:31 +0000 (UTC) X-FDA: 78916920222.14.33EE2F0 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 0542B16000B for ; Tue, 14 Dec 2021 16:23:27 +0000 (UTC) Received: by mail-lj1-f202.google.com with SMTP id y11-20020a2e978b000000b00218df7f76feso5661147lji.11 for ; Tue, 14 Dec 2021 08:23:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uWmJ4cna30hge0JfJUa8tpBQzN6nH+qPm9bB+RIZvh4=; b=gnRo03X1rHbrBeUgP8SbekXfNxzT/s/gLZOQMVuQ98Mcrl1/W3OksXN1PcCbasxeAO UcbkG3ew4b8Be17NziHFV8PQdo/AVLvnN1uvzNE+/RCF8Yeo5nZcp8/4xUdImU0Nmub7 pUp8xOiBFqGYmE6XPUQwL+6h+P0/ZO9EIsfw8OtvYKyScA9IZPLC73pXdRqRDIlAz8Ac 68riDzHGh1lY5Kf42SDi08qLtAlbv7TaUPzDjvXTJ+J+g1Gk1S+wssewAKw3Q+7ruZI2 K9XseeVybm7Rc624Ez0X/JuNj+l4qFERRpZ+uevwUrAZ2RpTPmImVr4tiJ3KbMiF/MUH F4Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uWmJ4cna30hge0JfJUa8tpBQzN6nH+qPm9bB+RIZvh4=; b=d+fBE3A7AU7i7b34nEFOYKiHH92wHZXz4W5k6WXLEF+0GgFTuH4ruMHJakwsKe+4/y 93Cog6ABB7EuBYAxmFRQDkmwI1vzaIErusjbOwnRP8cnf5rr5XucVGOAZg2oUMc+IK+1 q/Cg2rmH72KVDvtS3XvO56LkITDea5wa0cuKA/qP7zpQ+fbDXmW00PqnUqnGrQkFkFX0 fQ4J8q9ZYra454nNApXYbLCBru1tIk5xae0zXKLU1eV1zwmjItOUfL95sNzM0XBTyLpR vvo1dyNgxqnjcLBFwOxdkkt2C8xTChPwgmlTOmY/brbZpUTYHKQcZS6rHhYVv8xssLLl VRPQ== X-Gm-Message-State: AOAM533CKhlPyw9t7ElgqUJZpn6MtyrPDcG9Ava9ltQecjeHR0VChzcj AyIIT85suKwuqq8dLnkTt6Z1XGB2bi8= X-Google-Smtp-Source: ABdhPJxVRSRgL2C45JzlUfnk64Umy5VS7QXVkbIUWGYNVfQ53/NEIAiExfqrIiGa5vb43rmr7CHrDJLiI0s= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:357e:2b9d:5b13:a652]) (user=glider job=sendgmr) by 2002:a05:6512:3993:: with SMTP id j19mr5680886lfu.581.1639499009067; Tue, 14 Dec 2021 08:23:29 -0800 (PST) Date: Tue, 14 Dec 2021 17:20:42 +0100 In-Reply-To: <20211214162050.660953-1-glider@google.com> Message-Id: <20211214162050.660953-36-glider@google.com> Mime-Version: 1.0 References: <20211214162050.660953-1-glider@google.com> X-Mailer: git-send-email 2.34.1.173.g76aa8bc2d0-goog Subject: [PATCH 35/43] x86: kmsan: use __msan_ string functions where possible. From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 0542B16000B X-Stat-Signature: 669esiiui4nejawr1da91n39kcz6ksdx Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=gnRo03X1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3AcW4YQYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com designates 209.85.208.202 as permitted sender) smtp.mailfrom=3AcW4YQYKCIImrojkxmuumrk.iusrot03-ssq1giq.uxm@flex--glider.bounces.google.com X-HE-Tag: 1639499007-868343 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 a6cd6815f2490..b2c74cb85e20e 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -198,6 +198,7 @@ __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) return p; } +#ifndef CONFIG_KMSAN __FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) { size_t p_size = __builtin_object_size(p, 0); @@ -240,6 +241,7 @@ __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) fortify_panic(__func__); return __underlying_memmove(p, q, size); } +#endif extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); __FORTIFY_INLINE void *memscan(void *p, int c, __kernel_size_t size)