Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1443319rwi; Wed, 19 Oct 2022 10:36:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6pgiJS9rJPu2MsqNr5Da9zS1qLysBUY2XvjuOmFQZA/jAPrk04aVZqIFkjov6qzFZM7FMc X-Received: by 2002:a17:907:6d08:b0:78e:e87:5c06 with SMTP id sa8-20020a1709076d0800b0078e0e875c06mr7466855ejc.511.1666201007872; Wed, 19 Oct 2022 10:36:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666201007; cv=none; d=google.com; s=arc-20160816; b=mupGWlWwvmBEu9bZT5xFY89PUu117LDZ2TZrJn08AsHd6WcPsVOr/aeUTkcB8X/pti 9muaEK+b4hkwCOgVSVktz3dSUXHSuws0Klq4Ru6VdkHEtHGKj0oSGv1J1sbYL7t/dR3k 5BiOW8EcyBYF4QHJXQBQrbQk8GKJ65WnTd4GaADuSufNCq5G20OSCI5bJzsZwOjnngVt BLr2+B8bY0bWfTITvYFgkM9R7JSvHb2IuiZ/91TXTH1jIVYXo2tFF2T2yFRMgL9K430I OsRJaA8yOWAWQ17R0ucPItVnqKSmzRtAuOSrb7coZehBUHiZGDqqDA24RA8UjMJq5VVO JKDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GKQx/U9Oi8K1uuimUXOQCwOAtdhTQa9BCE+heeFHVSk=; b=rjc5CARSVVCGiung6b5YsJRVcHT3xh05bJpdk9jeZOfQzV/KOunQ4Hcq8E/wpCoDyf hd4KW425HxuIaVBNpGyDDVyoJdnR587BL9fC0U6BP+rGzce1IeMQRh7bz0nMNrSS2ph+ WNcK/6ayulwCi5pVdXEw8G39iRM5R4yrGSQ+QRRAMG3iZNTZx5X1MqSU6QV/BJGiU08f XEVktACkODMeJhmp8VqdmhHAOZCGr6Zc/6oxtWVPWcreV2Ft2ZxCokSZpGcnXtgO+K8n KtgxEo4t99PGl27VG6hGVB1Bdyr9/ztRVGuYYQr0zhu0rjRnJzFP45FiqEnR0eWUR3qB 7QzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=llqmXm0C; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id st11-20020a170907c08b00b0078e1d20b9dcsi11314245ejc.916.2022.10.19.10.36.20; Wed, 19 Oct 2022 10:36:47 -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=@chromium.org header.s=google header.b=llqmXm0C; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230417AbiJSR34 (ORCPT + 99 others); Wed, 19 Oct 2022 13:29:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230151AbiJSR3t (ORCPT ); Wed, 19 Oct 2022 13:29:49 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E6BE6C117 for ; Wed, 19 Oct 2022 10:29:45 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id k9so17475583pll.11 for ; Wed, 19 Oct 2022 10:29:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=GKQx/U9Oi8K1uuimUXOQCwOAtdhTQa9BCE+heeFHVSk=; b=llqmXm0CMlRdNxGJQhZzu79UZsKVDaHayVNH4B/+FR4a9XyRxvDx+sElvKdhad4pNv mdLaT0+TyxGeqlA5slg0RvPDNxFyAi+/JB2MjN8PttYL66+BXuR2g8J0hhUwFniWn/aF BHuDOF4poTYwjD4PiI/xZLXK5T6ND5/qAuzDI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GKQx/U9Oi8K1uuimUXOQCwOAtdhTQa9BCE+heeFHVSk=; b=LolPJAS3uvK3rdZZK3XhevIjMp/ttG6/QPpF3hNuUrk+U+2NVoEuqrI2PwA+KrLxH/ FaUbCjWm6TeEx9dL/zjbTVqfjzh/2L65FkWaH7T/03VvKAa2GzdaAASU3OXEL31lYxK2 a5HTd8RVMxObdUQIKuS2pvdAGhchW1AtnhmFR4CgRgS7VSLwx3iEU0Dn50AiAYu2dY93 IsgQcN3G4u07Gq/OHxvHtatJWirdeLbS6zOYhXWhV8OC0ONw8iaBCYtnfZsMGTbawGIs oqQf9ggKkTLCu5l3/V5/8lWy7Wwj3TsTYaj4XzCaw7fl8gXDSbHkB5B6rYAytFKsVtkW 2Dfg== X-Gm-Message-State: ACrzQf2Vj4WWz91JrM00cLzBg0347dLPw8RutwngvPblL5+5e1Rcflcb TlyFUA3ccdrFmfjONIaw34fqmA== X-Received: by 2002:a17:903:2312:b0:185:43a2:3d0e with SMTP id d18-20020a170903231200b0018543a23d0emr9702582plh.118.1666200584506; Wed, 19 Oct 2022 10:29:44 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y8-20020a17090a16c800b00205d85cfb30sm234077pje.20.2022.10.19.10.29.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 10:29:43 -0700 (PDT) Date: Wed, 19 Oct 2022 10:29:42 -0700 From: Kees Cook To: Alexander Potapenko Cc: Nathan Chancellor , Andrew Morton , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, kasan-dev@googlegroups.com, linux-mm@kvack.org Subject: Re: -Wmacro-redefined in include/linux/fortify-string.h Message-ID: <202210190930.26BF0CE2@keescook> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 On Wed, Oct 19, 2022 at 08:37:19AM -0700, Nathan Chancellor wrote: > I am seeing the following set of warnings when building an x86_64 > configuration that has CONFIG_FORTIFY_SOURCE=y and CONFIG_KMSAN=y: > > In file included from scripts/mod/devicetable-offsets.c:3: > In file included from ./include/linux/mod_devicetable.h:13: > In file included from ./include/linux/uuid.h:12: > In file included from ./include/linux/string.h:253: > ./include/linux/fortify-string.h:496:9: error: 'memcpy' macro redefined [-Werror,-Wmacro-redefined] > #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ > ^ > ./arch/x86/include/asm/string_64.h:17:9: note: previous definition is here > #define memcpy __msan_memcpy > ^ > In file included from scripts/mod/devicetable-offsets.c:3: > In file included from ./include/linux/mod_devicetable.h:13: > In file included from ./include/linux/uuid.h:12: > In file included from ./include/linux/string.h:253: > ./include/linux/fortify-string.h:500:9: error: 'memmove' macro redefined [-Werror,-Wmacro-redefined] > #define memmove(p, q, s) __fortify_memcpy_chk(p, q, s, \ > ^ > ./arch/x86/include/asm/string_64.h:73:9: note: previous definition is here > #define memmove __msan_memmove > ^ > 2 errors generated. > > I can see that commit ff901d80fff6 ("x86: kmsan: use __msan_ string > functions where possible.") appears to include a fix up for this warning > with memset() but not memcpy() or memmove(). If I apply a similar fix up > like so: > > diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h > index 4029fe368a4f..718ee17b31e3 100644 > --- a/include/linux/fortify-string.h > +++ b/include/linux/fortify-string.h > @@ -493,6 +493,7 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size, > * __struct_size() vs __member_size() must be captured here to avoid > * evaluating argument side-effects further into the macro layers. > */ > +#ifndef CONFIG_KMSAN > #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ > __struct_size(p), __struct_size(q), \ > __member_size(p), __member_size(q), \ > @@ -501,6 +502,7 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size, > __struct_size(p), __struct_size(q), \ > __member_size(p), __member_size(q), \ > memmove) > +#endif > > extern void *__real_memscan(void *, int, __kernel_size_t) __RENAME(memscan); > __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size) > > Then the instances of -Wmacro-redefined disappear but the fortify tests > no longer pass for somewhat obvious reasons: > > warning: unsafe memcpy() usage lacked '__read_overflow2' symbol in lib/test_fortify/read_overflow2-memcpy.c > warning: unsafe memmove() usage lacked '__read_overflow2' symbol in lib/test_fortify/read_overflow2-memmove.c > warning: unsafe memcpy() usage lacked '__read_overflow2_field' symbol in lib/test_fortify/read_overflow2_field-memcpy.c > warning: unsafe memmove() usage lacked '__read_overflow2_field' symbol in lib/test_fortify/read_overflow2_field-memmove.c > warning: unsafe memcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-memcpy.c > warning: unsafe memmove() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-memmove.c > warning: unsafe memset() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-memset.c > warning: unsafe memcpy() usage lacked '__write_overflow_field' symbol in lib/test_fortify/write_overflow_field-memcpy.c > warning: unsafe memmove() usage lacked '__write_overflow_field' symbol in lib/test_fortify/write_overflow_field-memmove.c > warning: unsafe memset() usage lacked '__write_overflow_field' symbol in lib/test_fortify/write_overflow_field-memset.c > > Should CONFIG_KMSAN depend on CONFIG_FORTIFY_SOURCE=n like so? It seems > like the two features are incompatible if I am reading ff901d80fff6 > correctly. > > diff --git a/lib/Kconfig.kmsan b/lib/Kconfig.kmsan > index b2489dd6503f..6a681621e3c5 100644 > --- a/lib/Kconfig.kmsan > +++ b/lib/Kconfig.kmsan > @@ -11,7 +11,7 @@ config HAVE_KMSAN_COMPILER > config KMSAN > bool "KMSAN: detector of uninitialized values use" > depends on HAVE_ARCH_KMSAN && HAVE_KMSAN_COMPILER > - depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN > + depends on SLUB && DEBUG_KERNEL && !KASAN && !KCSAN && !FORTIFY_SOURCE > select STACKDEPOT > select STACKDEPOT_ALWAYS_INIT > help > > or is there a different obvious fix that I am missing? Hm, why can't KMSAN use the same thing KASAN does, and compose correctly with FORTIFY? (i.e. redefine the "__underlaying_mem*" macros?) -- Kees Cook