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: 61195 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp1210230dyk; Fri, 24 Feb 2023 01:12:12 -0800 (PST) X-Google-Smtp-Source: AK7set8gSh31PKfQWPNsrw4UkL8nGZrpkZUqS/odmYqEXbj7DL7bEtUHHlHdJYcL6P9nL3mz7N8J X-Received: by 2002:a62:1c41:0:b0:5a8:46b0:8269 with SMTP id c62-20020a621c41000000b005a846b08269mr15118869pfc.31.1677229932340; Fri, 24 Feb 2023 01:12:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229932; cv=none; d=google.com; s=arc-20160816; b=QhXT3SUWrCQjREbXb1eK4lBFnzFT+JSAtFkLJBQ79plbMOQXfwN3OX3URzBesWrMFi sGBAx0WlTrY0pj0LAZMjERBCP2W607llCnqNT82OC7t4SJSEQvCj0WKoF9dtnWRH8r/2 ahkbbKgbqVIXlAz9+QkkkKQg/4EPlKg91SpohN5eZ0o8PtZfFMqhid0TwjbA4vVvdnpg o58VbqfyL4KYxQfjnr8RtWrFFoXe4Y2zoDk6SF8SmgZkXlKHY3nXqKEqbDMyq0U4I3Pa fseX9UGytyDPUy03ScQtGU1l7a/ERLl1/g2MssEtzj0nGbdJenUMpZvLGAEE0WMMGIjT DCTQ== 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=Ni0X0/loZejzrEM3ahtyHsQ4czmgqm9WNAw1TT3btvM=; b=NhRmRHzKu9+It7xas+fK9fZXkGnz5bhzyqIwWxJgyzOtKK0kO+xVX1Y0/Bk/0M/VGy ETn5KfWY2Kz7LSEhSk0DQ3L2h3Vk2y63uC/7iEp8u9swyInN2UP+Z2zNQOAJApCJ2TB/ p/zJnox5qEN3mT90992JOwzuGjNbTF59dxQRujdry8DSv6r0Dpxdez9wb6NJmv6HbqZc 0sb1EopzSoxNOhX0Lrj+0ba5q4DXOAK4aaFrp0dmAZAxKr41Y57lWzRUIgr7bMAXr3sM tKaoFMlZx6NxeHcc8cqK8KwGMc01Gmr+Kay2s3mkOcgjIeWWF4ZSsCeStT1MAlgEHAIE 6Wjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=KNP4d6RV; 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 ck5-20020a056a00328500b005d52796254bsi7281343pfb.284.2023.02.24.01.11.57; Fri, 24 Feb 2023 01:12:12 -0800 (PST) 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=KNP4d6RV; 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 S229920AbjBXJAO (ORCPT + 99 others); Fri, 24 Feb 2023 04:00:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229908AbjBXJAF (ORCPT ); Fri, 24 Feb 2023 04:00:05 -0500 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A71663A07 for ; Fri, 24 Feb 2023 01:00:03 -0800 (PST) Received: by mail-ed1-x54a.google.com with SMTP id r6-20020aa7c146000000b004acd97105ffso18875635edp.19 for ; Fri, 24 Feb 2023 01:00:03 -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=p1nbP1rUDtoYxvM4rg1VhA7jZR8eX6Lqo9SVPX5hSkjCuRYP/bt8Qdg9mpVIvOzDvY LGqDVga9hOa2GnhOnTCqk7CD83hLVMfMoQGQNCNHNDtzrF/c8BUH3gmpgRKMRulmphll Uca5ffFR0lLQMVhQ3RXdqfe2pXLoeyrfqDgHpZvHkLQT4kpi02bwpNeE55mMdZyrSak1 fCwUN3yayIMqtoaN+jz8MfRRJzw+hMINwCoo0HgBTrdkx3o+yLsQZrnfJ2mC9kgTnUPE 9vdflodI1fO/eLv+Gb1otDVWDGOL5LL2pW/YF3cOkKZodUtbiiezUGOdsGwRFKhea4yW XltQ== X-Gm-Message-State: AO0yUKUO3aBTjwGeqSYKijSWezaO7dsdGmviSVJuk1xMzMyAF8V2jCBp 2l+Va+JDZPentI1O0X2Tk9ktUpklcg== 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-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=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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758703053222997994?= X-GMAIL-MSGID: =?utf-8?q?1758703053222997994?= 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)))