From patchwork Wed Mar 25 16:12:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 11458283 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1EFDF6CA for ; Wed, 25 Mar 2020 16:14:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C3E8920409 for ; Wed, 25 Mar 2020 16:14:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p0Q37psA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C3E8920409 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3AD7F6B009B; Wed, 25 Mar 2020 12:14:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 335CD6B00A0; Wed, 25 Mar 2020 12:14:20 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 188FD6B00A1; Wed, 25 Mar 2020 12:14:20 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id E28856B009B for ; Wed, 25 Mar 2020 12:14:19 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D065E844F4 for ; Wed, 25 Mar 2020 16:14:19 +0000 (UTC) X-FDA: 76634381838.17.boat21_7d0e85d495535 X-Spam-Summary: 2,0,0,7697d3f9fd7a6b19,d41d8cd98f00b204,3won7xgykcekrwtop2rzzrwp.nzxwty58-xxv6lnv.z2r@flex--glider.bounces.google.com,,RULES_HIT:1:2:41:152:355:379:541:800:960:967:973:988:989:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1593:1594:1605:1730:1747:1777:1792:1801:2393:2525:2538:2559:2563:2682:2685:2859:2895:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3870:3871:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4049:4250:4321:4605:5007:6261:6653:6691:6742:6743:7875:8603:8660:8784:9025:9969:10004:11026:11473:11657:11658:11914:12043:12048:12050:12296:12297:12438:12555:12895:12986:13148:13230:13846:14096:14097:14394:14659:21080:21365:21444:21451:21627:21990:30054:30055:30064:30067,0,RBL:209.85.222.74:@flex--glider.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23 ,LUA_SUM X-HE-Tag: boat21_7d0e85d495535 X-Filterd-Recvd-Size: 10332 Received: from mail-ua1-f74.google.com (mail-ua1-f74.google.com [209.85.222.74]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Wed, 25 Mar 2020 16:14:19 +0000 (UTC) Received: by mail-ua1-f74.google.com with SMTP id f15so1033673uap.19 for ; Wed, 25 Mar 2020 09:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rDP3zvgiw6o5ldgzCmXWwQQtGpnJpieSNSs99UNbhmM=; b=p0Q37psA7jmeP1LLf1OV6yMEYr/uopsM1ykTowb8hpLdpoU0MZJ6mIOBXr3Ar3s7eO yzc2lDMsO7ZRl3QuBlwkMUFwup8E557hPnMlg5CTmnn0nHHCb5MPDHeFL/8Q7L/DJVZU e22axRNO1ADL1EWZSdPIGCipMeDXYPoMQOa1xWaT8rA2X8RFTHgv80svvuLBFYQd9i8n Kw5e7Xit2pVG+w+H2e/o2XaTxbA+ZvIzHdimST+vpxaw+bGWgdXtfougs+BoyjZLaDMB 0rf3AHkov//E8O6HfvxaYHT5/JiRESJAQ2Fx70p8pmbGOQJH8qL/9y9wDlfHQm/HmcNq JtbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rDP3zvgiw6o5ldgzCmXWwQQtGpnJpieSNSs99UNbhmM=; b=rKGnPxBtnvxJkLlxpbXZNMOo4/kGwcsdgNlP5hFFSKFUaK/Gag3cdtn6ucgZlZHy1i jrBZmWptuc8EDjWWs9JJHxamxgaeZ6pTNMF2ugJaeFloCyOOp+YaaeK65Z1F0xxEMH/V taQiOuYDiEmYYdwwnNWTvtJLOrfKy4gTs1UFOLD8TcfxdZIaHLv/n2IGmmaP5amh5Y3W gil1AB+nmtRvu074D41+I3eh+XHcvz0Mfa3rfIiB/DLl9J/OfjwQftB7dxE5D5RvL163 GMLG6AwzvaeCJutpidR1KGtoVMkUUZgyTFGRrKjJP7htAoi8S2CNEuACd5/NlXTT7Uh+ bsMg== X-Gm-Message-State: ANhLgQ0SLgETK0Un3XoAVI+ihbadQu5JxYDjQJqAe8ZrGalmyB6+4Fzz rG4aZFH1ognK0AGyqPwUqnhl5COKHLE= X-Google-Smtp-Source: ADFU+vsUw+oOwSvnNREg6mLrvxXsGkgeqUufnnw6E3EvGa3IxIJIxu7jik333Z7SunlZSnNKbHS8f1zVScE= X-Received: by 2002:a1f:4c86:: with SMTP id z128mr2832911vka.70.1585152858447; Wed, 25 Mar 2020 09:14:18 -0700 (PDT) Date: Wed, 25 Mar 2020 17:12:37 +0100 In-Reply-To: <20200325161249.55095-1-glider@google.com> Message-Id: <20200325161249.55095-27-glider@google.com> Mime-Version: 1.0 References: <20200325161249.55095-1-glider@google.com> X-Mailer: git-send-email 2.25.1.696.g5e7596f4ac-goog Subject: [PATCH v5 26/38] kmsan: use __msan_ string functions where possible. From: glider@google.com To: Vegard Nossum , Dmitry Vyukov , Marco Elver , Andrey Konovalov , linux-mm@kvack.org Cc: glider@google.com, viro@zeniv.linux.org.uk, adilger.kernel@dilger.ca, akpm@linux-foundation.org, aryabinin@virtuozzo.com, luto@kernel.org, ard.biesheuvel@linaro.org, arnd@arndb.de, hch@infradead.org, hch@lst.de, darrick.wong@oracle.com, davem@davemloft.net, dmitry.torokhov@gmail.com, ebiggers@google.com, edumazet@google.com, ericvh@gmail.com, gregkh@linuxfoundation.org, harry.wentland@amd.com, herbert@gondor.apana.org.au, iii@linux.ibm.com, mingo@elte.hu, jasowang@redhat.com, axboe@kernel.dk, m.szyprowski@samsung.com, mark.rutland@arm.com, martin.petersen@oracle.com, schwidefsky@de.ibm.com, willy@infradead.org, mst@redhat.com, mhocko@suse.com, monstr@monstr.eu, pmladek@suse.com, cai@lca.pw, rdunlap@infradead.org, robin.murphy@arm.com, sergey.senozhatsky@gmail.com, rostedt@goodmis.org, tiwai@suse.com, tytso@mit.edu, tglx@linutronix.de, gor@linux.ibm.com, wsa@the-dreams.de 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 To: Alexander Potapenko Cc: Vegard Nossum Cc: Dmitry Vyukov Cc: Marco Elver Cc: Andrey Konovalov Cc: linux-mm@kvack.org --- v3: - use default string functions in the bootloader v4: - include kmsan-checks.h into compiler.h - also handle memset() and memmove() - fix https://github.com/google/kmsan/issues/64 v5: - don't compile memset() and memmove() under KMSAN Change-Id: Ib2512ce5aa8d457453dd38caa12f58f002166813 --- arch/x86/boot/compressed/misc.h | 1 + arch/x86/include/asm/string_64.h | 23 ++++++++++++++++++- .../firmware/efi/libstub/efi-stub-helper.c | 5 ++++ drivers/firmware/efi/libstub/tpm.c | 5 ++++ include/linux/compiler.h | 9 +++++++- include/linux/string.h | 2 ++ 6 files changed, 43 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index c8181392f70d7..dd4bd8c5d97a1 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -12,6 +12,7 @@ #undef CONFIG_PARAVIRT_XXL #undef CONFIG_PARAVIRT_SPINLOCKS #undef CONFIG_KASAN +#undef CONFIG_KMSAN /* cpu_feature_enabled() cannot be used this early */ #define USE_EARLY_PGTABLE_L5 diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 75314c3dbe471..0442e679b3079 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(CONFIG_KMSAN) +#undef memcpy +/* __msan_memcpy() is defined in compiler.h */ +#define memcpy(dst, src, len) __msan_memcpy(dst, src, len) +#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(CONFIG_KMSAN) +extern void *__msan_memset(void *s, int c, size_t n); +#undef memset +#define memset(dst, c, len) __msan_memset(dst, c, len) +#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(CONFIG_KMSAN) +#undef memmove +void *__msan_memmove(void *dest, const void *src, size_t len); +#define memmove(dst, src, len) __msan_memmove(dst, src, len) +#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,7 +82,8 @@ 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__)) || \ + (defined(CONFIG_KMSAN) && !defined(__SANITIZE_MEMORY__)) /* * For files that not instrumented (e.g. mm/slub.c) we @@ -73,7 +92,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/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 9f34c72429397..610f791c2493e 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -5,7 +5,12 @@ * implementation files. * * Copyright 2011 Intel Corporation; author Matt Fleming + * + * + * This file is not linked with KMSAN runtime. + * Do not replace memcpy with __memcpy. */ +#undef CONFIG_KMSAN #include #include diff --git a/drivers/firmware/efi/libstub/tpm.c b/drivers/firmware/efi/libstub/tpm.c index 1d59e103a2e3a..7e8906b1c1c98 100644 --- a/drivers/firmware/efi/libstub/tpm.c +++ b/drivers/firmware/efi/libstub/tpm.c @@ -6,7 +6,12 @@ * Copyright (C) 2017 Google, Inc. * Matthew Garrett * Thiebaud Weksteen + * + * + * This file is not linked with KMSAN runtime. + * Do not replace memcpy with __memcpy. */ +#undef CONFIG_KMSAN #include #include #include diff --git a/include/linux/compiler.h b/include/linux/compiler.h index c6c67729729e3..f2b97241fe2d4 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -180,6 +180,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, #include #include +#ifdef CONFIG_KMSAN +void *__msan_memcpy(void *dst, const void *src, u64 size); +#define __DO_MEMCPY(res, p, size) __msan_memcpy(res, p, size) +#else +#define __DO_MEMCPY(res, p, size) __builtin_memcpy(res, p, size) +#endif + #define __READ_ONCE_SIZE \ ({ \ switch (size) { \ @@ -189,7 +196,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \ default: \ barrier(); \ - __builtin_memcpy((void *)res, (const void *)p, size); \ + __DO_MEMCPY((void *)res, (const void *)p, size); \ barrier(); \ } \ }) diff --git a/include/linux/string.h b/include/linux/string.h index 6dfbb2efa8157..7ef92817c082f 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -356,6 +356,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); @@ -395,6 +396,7 @@ __FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) fortify_panic(__func__); return __builtin_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)