From patchwork Mon Oct 24 21:21:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 10387 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp697265wru; Mon, 24 Oct 2022 16:13:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Mm+BTQsENnlaXjBKKDn7dkqephSlsjOra2Nh6ocaCeh5X98SHIvv1hy89PgERfGBe+sRn X-Received: by 2002:a17:902:d4c4:b0:186:a951:b8f9 with SMTP id o4-20020a170902d4c400b00186a951b8f9mr7410411plg.130.1666653196012; Mon, 24 Oct 2022 16:13:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666653196; cv=none; d=google.com; s=arc-20160816; b=LPdDDisphbfUQuVLN5fVwMRRBdpXTA79P01Ky9rLJx1d9E5CIZa/hqBjt3wXTPZSUz FKF8e7sB/vDdEx8H38LWtuBbsGpZreu9OFPFPsIkWn5GMn7gzdFEjdcUuZuyvpKnzBKg 9aPFt6tUAGJVJLzj3S5i1ezL2Esjuc4b2T88cfJcTpN/YXzmB4x2soj96NpZAoImQ7bf iV1nD7LA78Abim9ReMvdRFd+bbtWEN/5cN6ijV4yMegwSVV2Raur+aLMfS+K3QmRb9Ye S7GSE9xXVA5+svX/1tRI6/VL+efpHW4XPnuWZzdq/AdtYE/BlxEC1eXG6sZPbRzFmQG9 Iimw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=O4b5t5V5Gcz1RUui4LqlZYe/T5V0+jbf0EermbhLbCo=; b=tSQmqbvKmm8TLE4bAc6kSh2h/pD5Wnv2smj0xRUPZnwnOSrfJsIRvgn0YtMT2EqR9f v4bKI6RRrlt/8BxIms8gNLmHUguHGm7xy2njtiUEdfqNPM73QLRyi8XrCm0zF1hjMJth EfNxHOKKtf7gxUZfZxhCv/D97lQFn+sTif91QMv6jgWcZvGtDNkRz8j4nBCcQFnqovKU qnJSV5DU57zi4frPxxZcle/Z/tj1ohOE+EQClNH8APUEvpAXjIbagAI0EUlM6q5OUlyC ncz/i/N03pgcjoNSK7/mOuX4PuoBpIviePCmkZV++wlTn5FeyZA5z3CS11yJVMdZdssK iluQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UB5us+L6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o31-20020a635a1f000000b004561c1f132csi907723pgb.346.2022.10.24.16.13.02; Mon, 24 Oct 2022 16:13:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UB5us+L6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230016AbiJXXBj (ORCPT + 99 others); Mon, 24 Oct 2022 19:01:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229939AbiJXXAg (ORCPT ); Mon, 24 Oct 2022 19:00:36 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D97002D7D0C for ; Mon, 24 Oct 2022 14:22:52 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-367dc159c2fso101321987b3.19 for ; Mon, 24 Oct 2022 14:22:52 -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:message-id:reply-to; bh=O4b5t5V5Gcz1RUui4LqlZYe/T5V0+jbf0EermbhLbCo=; b=UB5us+L65WuSV7LKHhI52HcB3sYCuWwNnK8ltyO4u0vIvjaAB05ZLOAGafpo8l5cGa wBMFCT4Kkvrs5x4wtnBYfMOzahxa569zXfrrKTeHgqfmIyNP4oI+XztTL1fPKIJ8gpn3 0qkGExeFsAY6JZvk24haFa3HrCY2p0BIZwn7gyOEapmadRNJv3JRV0JguVi3J5+d36gN eqa6FcGmhPmYjXJhbPUKqqlQx3k9kRlFuROjUY1SCl88+13znc8K5XUxIlRLoL6bXrBN VHDy4KdKJBliLnYUrxmcBAiyhgWHSM9VL/a952Bi8+gPciMk2TXd+F4AepHVcsqQrC8T /i9w== 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:message-id:reply-to; bh=O4b5t5V5Gcz1RUui4LqlZYe/T5V0+jbf0EermbhLbCo=; b=NWTNvwuKzvM9Vscanu8rzKyD3sNsSZ8GslREPICw+d4bc6gl4XiXh/YZQqHER2QhGI 21XBt4ZCbStF0BgwS42gy0MD4LxijAe9Ynz3hxRaJVMbxgyBA+yTyUZQgJsyqltb/FN4 6uhXgM7GKrjRQo22MhECP6O4Nin9wp1Gqn6FY8u/hzjkepaC7WuY+XWw0lavWQLaAmCT HpR/rQefj2xYSap806ITQZKRNAniu2rlSAnfHOMnyo/APkJ63RLK/Y+5h9/4QTTRoU3j IvDXKkaDEOmkCUTLe+3LeenuAMTP7HH98S1N4X4PIdaVXHEBqg/dBE1BvA0BcyKbJ/L8 YotQ== X-Gm-Message-State: ACrzQf02uN4xYtGlIoYvb6ZZuqaLBuLvS18S8srU91ZLLWNAfkCJl71T mlV4UlM/sImSK8V9nLv1vJRBcFYM9LE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:8695:7c88:66ed:f9d1]) (user=glider job=sendgmr) by 2002:a81:5a57:0:b0:353:6de6:3263 with SMTP id o84-20020a815a57000000b003536de63263mr30747701ywb.358.1666646530679; Mon, 24 Oct 2022 14:22:10 -0700 (PDT) Date: Mon, 24 Oct 2022 23:21:44 +0200 In-Reply-To: <20221024212144.2852069-1-glider@google.com> Mime-Version: 1.0 References: <20221024212144.2852069-1-glider@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024212144.2852069-5-glider@google.com> Subject: [PATCH 5/5] x86: fortify: kmsan: fix KMSAN fortify builds From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Nathan Chancellor , Nick Desaulniers , Kees Cook , Tamas K Lengyel X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747612541815973117?= X-GMAIL-MSGID: =?utf-8?q?1747612541815973117?= Ensure that KMSAN builds replace memset/memcpy/memmove calls with the respective __msan_XXX functions, and that none of the macros are redefined twice. This should allow building kernel with both CONFIG_KMSAN and CONFIG_FORTIFY_SOURCE. Cc: Andrew Morton Cc: Nathan Chancellor Cc: Nick Desaulniers Cc: Kees Cook Cc: linux-mm@kvack.org Reported-by: Tamas K Lengyel Link: https://github.com/google/kmsan/issues/89 Signed-off-by: Alexander Potapenko --- arch/x86/include/asm/string_64.h | 11 +++++++---- include/linux/fortify-string.h | 17 +++++++++++++++-- include/linux/kmsan_string.h | 21 +++++++++++++++++++++ mm/kmsan/instrumentation.c | 1 + 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 include/linux/kmsan_string.h diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 3b87d889b6e16..888731ccf1f67 100644 --- a/arch/x86/include/asm/string_64.h +++ b/arch/x86/include/asm/string_64.h @@ -10,10 +10,13 @@ /* Even with __builtin_ the compiler may decide to use the out of line function. */ +#if defined(__SANITIZE_MEMORY__) && defined(__NO_FORTIFY) +#include +#endif + #define __HAVE_ARCH_MEMCPY 1 -#if defined(__SANITIZE_MEMORY__) +#if defined(__SANITIZE_MEMORY__) && defined(__NO_FORTIFY) #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); @@ -21,7 +24,7 @@ extern void *memcpy(void *to, const void *from, size_t len); extern void *__memcpy(void *to, const void *from, size_t len); #define __HAVE_ARCH_MEMSET -#if defined(__SANITIZE_MEMORY__) +#if defined(__SANITIZE_MEMORY__) && defined(__NO_FORTIFY) extern void *__msan_memset(void *s, int c, size_t n); #undef memset #define memset __msan_memset @@ -67,7 +70,7 @@ static inline void *memset64(uint64_t *s, uint64_t v, size_t n) } #define __HAVE_ARCH_MEMMOVE -#if defined(__SANITIZE_MEMORY__) +#if defined(__SANITIZE_MEMORY__) && defined(__NO_FORTIFY) #undef memmove void *__msan_memmove(void *dest, const void *src, size_t len); #define memmove __msan_memmove diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 4029fe368a4f6..18a31b125f9d6 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -43,11 +43,24 @@ extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen); extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat); extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy); #else -#define __underlying_memchr __builtin_memchr -#define __underlying_memcmp __builtin_memcmp + +#if defined(__SANITIZE_MEMORY__) +/* + * For KMSAN builds all memcpy/memset/memmove calls should be replaced by the + * corresponding __msan_XXX functions. + */ +#include +#define __underlying_memcpy __msan_memcpy +#define __underlying_memmove __msan_memmove +#define __underlying_memset __msan_memset +#else #define __underlying_memcpy __builtin_memcpy #define __underlying_memmove __builtin_memmove #define __underlying_memset __builtin_memset +#endif + +#define __underlying_memchr __builtin_memchr +#define __underlying_memcmp __builtin_memcmp #define __underlying_strcat __builtin_strcat #define __underlying_strcpy __builtin_strcpy #define __underlying_strlen __builtin_strlen diff --git a/include/linux/kmsan_string.h b/include/linux/kmsan_string.h new file mode 100644 index 0000000000000..7287da6f52eff --- /dev/null +++ b/include/linux/kmsan_string.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KMSAN string functions API used in other headers. + * + * Copyright (C) 2022 Google LLC + * Author: Alexander Potapenko + * + */ +#ifndef _LINUX_KMSAN_STRING_H +#define _LINUX_KMSAN_STRING_H + +/* + * KMSAN overrides the default memcpy/memset/memmove implementations in the + * kernel, which requires having __msan_XXX function prototypes in several other + * headers. Keep them in one place instead of open-coding. + */ +void *__msan_memcpy(void *dst, const void *src, size_t size); +void *__msan_memset(void *s, int c, size_t n); +void *__msan_memmove(void *dest, const void *src, size_t len); + +#endif /* _LINUX_KMSAN_STRING_H */ diff --git a/mm/kmsan/instrumentation.c b/mm/kmsan/instrumentation.c index 280d154132684..271f135f97a16 100644 --- a/mm/kmsan/instrumentation.c +++ b/mm/kmsan/instrumentation.c @@ -14,6 +14,7 @@ #include "kmsan.h" #include +#include #include #include