From patchwork Fri Feb 24 08:59:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13151047 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 34C6FC61DA3 for ; Fri, 24 Feb 2023 09:00:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D4076B0073; Fri, 24 Feb 2023 04:00:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8841D6B0074; Fri, 24 Feb 2023 04:00:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FE416B0075; Fri, 24 Feb 2023 04:00:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 634C86B0073 for ; Fri, 24 Feb 2023 04:00:05 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 05721AC108 for ; Fri, 24 Feb 2023 09:00:04 +0000 (UTC) X-FDA: 80501588370.09.D7E593F Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf04.hostedemail.com (Postfix) with ESMTP id 2C7A24001E for ; Fri, 24 Feb 2023 09:00:02 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=KNP4d6RV; spf=pass (imf04.hostedemail.com: domain of 3kXz4YwUKCJI07H0D2AA270.yA8749GJ-886Hwy6.AD2@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3kXz4YwUKCJI07H0D2AA270.yA8749GJ-886Hwy6.AD2@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677229203; 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=Ni0X0/loZejzrEM3ahtyHsQ4czmgqm9WNAw1TT3btvM=; b=0yBqfQHgQlCAaAMPza7eBCDAaa6RuLC+j3qbnK4ljS+opry1A+4CeFGEiMtPSEpTnKu5XW ul4Q+NX2OQ11ig1g4Qq1LF8fYQX6BXhzYOdtnURpvJ6Y0VSteE0P5zM47n4MepZgAECSNi 5RxJg7Tq4aZ4DhIzfoxFmvAcDzBWC5M= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=KNP4d6RV; spf=pass (imf04.hostedemail.com: domain of 3kXz4YwUKCJI07H0D2AA270.yA8749GJ-886Hwy6.AD2@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3kXz4YwUKCJI07H0D2AA270.yA8749GJ-886Hwy6.AD2@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677229203; a=rsa-sha256; cv=none; b=MxD+jXutWItE0KvuJJAbeqqc3ytnCrYJwFAF/Y5qA8BXy7tjkxO22oZE1EF87KjC45m+LO bsRjlKfa7kaNTincs7wa7KbmIxNDFLXZh3DpszRAN4XlTobcA1ez2PBdvqrzKJsKkXDgLt QYWigKVoVhTfteAnIwDxZxzhSKFjfq8= Received: by mail-ed1-f74.google.com with SMTP id b1-20020aa7dc01000000b004ad062fee5eso18442516edu.17 for ; Fri, 24 Feb 2023 01:00:02 -0800 (PST) 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=Ni0X0/loZejzrEM3ahtyHsQ4czmgqm9WNAw1TT3btvM=; b=KNP4d6RVhpEjbQC3l2fT7kAl4C3wq4IJLBgd79IVsMNh23YbWQPxqNeNp4PNlX68dW rVjsIqPdml9xCdbYExTQnHkglHOraVKUHi8/qeMC3A0BjfhF00nI7oV0i9lYvUjp+3Jy 06s7+Vsgaq+qITEL4UlWWmUEyFOmvxmbI33WEdvg/44hzTfS1YsYyJIlEIoO1pADPPyI wUvDfvgb4DtiXPJvyc4ll/OFNetXkSHQURg/uKU+DyYrlA7oBOh6BQ/bmc0NaIgLL9F7 fa7+HKeZezfK5A1GpEJhWgtD/OGQgow/R7raRNgP6kyn44OIn90x9bADVFlDj7e3pksb GLRg== 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=Ni0X0/loZejzrEM3ahtyHsQ4czmgqm9WNAw1TT3btvM=; b=bTB69VVYxa03scLIRIzU8yc5UKmIK8X4JuhRviIOa13gUaUWlkkokPRsBaxgGCd4mw gmk2MnS43WaO/CHFxLeck2mWRlzoWYU39DYTGlO0QUxlc34NngFUXveFi5VEwpfI+FB8 vZiolHK9Oa6gRzLUQuKUKaAJX3UjzSMpuPAT0zKL1TbzL9QuhB1FnsLknaLlDvUVhray CQ0HqLSk5rTojVme6wmq/SQzobQUarObtkMaIXkh2oXuFzhLgmvFmJ9JYBxZ3Mt5Rn6l +9OQH/YabZcF4a2/fkRsiTY8KY/SHMmUMxuhD4j65xrWh7PZuBIMKau6+KZfDMOPNePa Ew6w== X-Gm-Message-State: AO0yUKXSuEihchIvSnr78VpOa3F/WUXslzCFaqbJHx9hRi/L4Z24kqjr o/zJtcY0XKAFaZ9WURrYduAQt9qkAw== X-Google-Smtp-Source: AK7set9rZ9eUb7bGWTMJ7DQioL+NOIJFyBh22lkV7CejLhB9j4FRWsGV+zy8NVqE9CeBwZV5bua6ewKcaw== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:53eb:6453:f5f5:3bb9]) (user=elver job=sendgmr) by 2002:a50:d544:0:b0:4ad:6e3e:7da6 with SMTP id f4-20020a50d544000000b004ad6e3e7da6mr7001699edj.6.1677229201684; Fri, 24 Feb 2023 01:00:01 -0800 (PST) Date: Fri, 24 Feb 2023 09:59:40 +0100 In-Reply-To: <20230224085942.1791837-1-elver@google.com> Mime-Version: 1.0 References: <20230224085942.1791837-1-elver@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224085942.1791837-2-elver@google.com> Subject: [PATCH v5 2/4] kasan: Treat meminstrinsic as builtins in uninstrumented files From: Marco Elver To: elver@google.com, Andrew Morton Cc: Peter Zijlstra , Jakub Jelinek , linux-toolchains@vger.kernel.org, Alexander Potapenko , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Andrey Ryabinin , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Kees Cook , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kbuild@vger.kernel.org, linux-hardening@vger.kernel.org, Linux Kernel Functional Testing , Naresh Kamboju X-Rspamd-Queue-Id: 2C7A24001E X-Stat-Signature: pbns15fkkftjuoxzraawbd7nj4qh3nhm X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1677229202-683957 X-HE-Meta: U2FsdGVkX1/ZAzWA8XVDIZWX5TkgHHSworIPMZgyzEuhKAX2vvx4aXwpjKJ4atM4bplZJFsHbsrWhCjUBQFtrdpXES9K8rAg52Yr+mnBQySQCTEKVjl6YVOoAqEc9MuWMy0by+ZCBEZ/hnKhVc92CeTfNCSwIFmsrxf+4Q0dnpOih/0Yj6L/3LvyFWItNTWFUfa/jNtKPJ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gGm9x6po71jEQ5nkcjyRlXBKRz6F1euwKDA1Jl7QlZ77EulqUZnb+HaLvSM3Cwb0ysjMsxFNBVTAKP0PXbCsY8uekyM+VqUHWhmH28j4fVpJQgPU90D/u/XhxKixa4Padbh7DnRjSkC3LFHP0pAWPOf0meXHV/bRosyhpA0Bfzwvb0tNnICETQD6cosGxiKrLBxQ2arPFhkLofqvusrSjcxwfnf8CE2GB/Y8gunGvbEi8TxtsdXPq2L8+qp0GzJpXnLO8weIqqcyuZwOzjQAMjT9IUsS5wCyJIzf7mIbIEgYzaiKCvanB4QubTGgOHK1JzLmBjdfV6Vnv0Jlz5xvFjjaSvXf0tlT3DxYem3hvoIIp6ZWH/yaim/fzNrBepJSmkH70= 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: Where the compiler instruments meminstrinsics by generating calls to __asan/__hwasan_ prefixed functions, let the compiler consider memintrinsics as builtin again. To do so, never override memset/memmove/memcpy if the compiler does the correct instrumentation - even on !GENERIC_ENTRY architectures. Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Signed-off-by: Marco Elver Reviewed-by: Andrey Konovalov Tested-by: Linux Kernel Functional Testing Tested-by: Naresh Kamboju --- v4: * New patch. --- lib/Kconfig.kasan | 9 +++++++++ mm/kasan/shadow.c | 5 ++++- scripts/Makefile.kasan | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index be6ee6020290..fdca89c05745 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -49,6 +49,15 @@ menuconfig KASAN if KASAN +config CC_HAS_KASAN_MEMINTRINSIC_PREFIX + def_bool (CC_IS_CLANG && $(cc-option,-fsanitize=kernel-address -mllvm -asan-kernel-mem-intrinsic-prefix=1)) || \ + (CC_IS_GCC && $(cc-option,-fsanitize=kernel-address --param asan-kernel-mem-intrinsic-prefix=1)) + # Don't define it if we don't need it: compilation of the test uses + # this variable to decide how the compiler should treat builtins. + depends on !KASAN_HW_TAGS + help + The compiler is able to prefix memintrinsics with __asan or __hwasan. + choice prompt "KASAN mode" default KASAN_GENERIC diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index f8a47cb299cb..43b6a59c8b54 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -38,11 +38,14 @@ bool __kasan_check_write(const volatile void *p, unsigned int size) } EXPORT_SYMBOL(__kasan_check_write); -#ifndef CONFIG_GENERIC_ENTRY +#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY) /* * CONFIG_GENERIC_ENTRY relies on compiler emitted mem*() calls to not be * instrumented. KASAN enabled toolchains should emit __asan_mem*() functions * for the sites they want to instrument. + * + * If we have a compiler that can instrument meminstrinsics, never override + * these, so that non-instrumented files can safely consider them as builtins. */ #undef memset void *memset(void *addr, int c, size_t len) diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index fa9f836f8039..c186110ffa20 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -1,5 +1,14 @@ # SPDX-License-Identifier: GPL-2.0 + +ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +# Safe for compiler to generate meminstrinsic calls in uninstrumented files. +CFLAGS_KASAN_NOSANITIZE := +else +# Don't let compiler generate memintrinsic calls in uninstrumented files +# because they are instrumented. CFLAGS_KASAN_NOSANITIZE := -fno-builtin +endif + KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET) cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))