From patchwork Fri Aug 26 15:07:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956209 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 A828AECAAD4 for ; Fri, 26 Aug 2022 15:09:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B439940011; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 41581940007; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24064940011; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 09170940007 for ; Fri, 26 Aug 2022 11:09:55 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E6590A0FA8 for ; Fri, 26 Aug 2022 15:09:54 +0000 (UTC) X-FDA: 79842078708.16.F672681 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf04.hostedemail.com (Postfix) with ESMTP id 9D0DD4001C for ; Fri, 26 Aug 2022 15:09:54 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id sc3-20020a1709078a0300b0073d77f805b3so732535ejc.22 for ; Fri, 26 Aug 2022 08:09:54 -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; bh=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=BC1C5JPZyis+Kes9Uu/ankA8YmaxeSDQUFcfBHXb3GZVLgly9+DxFLSdN6o6y7p7/P zVWe/gK4/+LWKxQGuvyhxewoxvI19U5hTqi2IpBvOJg3uq8Ik8xr0FYeUi72cpJlmij9 nf0JKqqIO86qyQ9jue4hqNcyWyObBn/CgX3JM/gkgO+Veqds0ttkLZ3Zg/KDXgiSSHRr oeNOglxG71/shVVy6M1hhBJW9QDDOpd1SAOcY4gcwkPldDcHeI0LxKdaN/3bAC+XERwl jFbWJk+m8LHqzyufpaIWhW/s3AL9MyZZGADD18fKDUyYSRK0x9/0JYGypV1K9dG68puO R0ZA== 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; bh=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=wfkCkHtVLYGGWyWAyR5m2OijVMMKonQ8FzMX4O88t9CMv8s2RaJX9QS3kia+oeFn4s EIkVA6hHoRuaO70mWNG3ey/oNZgwd7yThWe4MErE6EeCmuenLVi+6omNaW6BQUJrIxsC yLeUaprn0PAf1ShUXNI1yJfE9rrutNW2daNEf5yGt6lvLDH+XqYF7Uy8miRvAbCaQTLm 4VgmWr68aHH9Qt0FrEj1D9VqpmSEOq57b02wpt7odKhf361MSDNlORGrWS00smzrhTcW 6t/w/Ouh69wAFrIfoS9J885N/Q69a1aCg7p+6vDSUAgpBD7qXatLe86IZwPx0R7aO6Wh oFmw== X-Gm-Message-State: ACgBeo0cHztUX7wyKNmcBsNTuW92jV2CGagesmVRCSwp7wZEdVHR1qEV 6NjVw/VzKCMA1zHtUKNbVOF1uVn4qC4= X-Google-Smtp-Source: AA6agR46uXnxQGKu9ULnyAc1BLhctUrVNAcVdLs11pJsS5/NKYX8LczIxzp8tXMl7eS110R8HE5vOzH3J7w= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:368c:b0:446:48d9:2be with SMTP id ej12-20020a056402368c00b0044648d902bemr6858842edb.167.1661526593376; Fri, 26 Aug 2022 08:09:53 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:59 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-37-glider@google.com> Subject: [PATCH v5 36/44] 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 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 , 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526594; a=rsa-sha256; cv=none; b=jAxVlFyV3UikljdfKMmOysRInnT0fYs1plzP1BlgubRg/mvl86EG5ZRt9MPBlsnCpkNbbL jJOWKzPHqjuloaPg2VNHNZrTbPAVNZGo9hLN8gAlu622pLPgSFoTbHQH5PkhELdcPB4NJd qDRr6Ilrl1fbZFBfml/ke/De6qdd80s= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BC1C5JPZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526594; 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=uVzdHqFGYZKd4Qu5GJB2AnYrpVyvjfHfk7LqoEQIkSc=; b=g0GgI2TOzj240Wa29u3VV9tIPXB9z3Jf+O5eqbzFq6wahDTAZu7EpLTAY5VpVtAMEsP0TB S9u5KVoKtSp5yZnvUX/z8Tir24qxa6XAlo10hXrpHbxytrijyxIxaItO/OwwI0emqMbZN+ lcscrgg3R5bR/miKW6Ife3gRkQms6Ec= X-Rspamd-Queue-Id: 9D0DD4001C Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BC1C5JPZ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3QeIIYwYKCEgqvsno1qyyqvo.mywvsx47-wwu5kmu.y1q@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Stat-Signature: gw65bcoykq6wkxz9js97s53ar4eh8hin X-HE-Tag: 1661526594-85523 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,