From patchwork Fri Feb 24 08:59:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13151054 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59B92C678DB for ; Fri, 24 Feb 2023 09:00:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229915AbjBXJAL (ORCPT ); Fri, 24 Feb 2023 04:00:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229902AbjBXJAC (ORCPT ); Fri, 24 Feb 2023 04:00:02 -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 9B7A94DE06 for ; Fri, 24 Feb 2023 00:59:59 -0800 (PST) Received: by mail-ed1-x54a.google.com with SMTP id t9-20020a056402524900b004af59c073abso13205238edd.6 for ; Fri, 24 Feb 2023 00:59:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=utTJOkvRGl0nLL0ic/scm4HYcaWRO/7AK96vkAXGENA=; b=nsiAeT3sqIcbbasW+A96bARZ2VdXg782ZVWDGh5Re7HD9f4mYtT+ysgkT9VaSYSDrH Rt4TDQx49hmvv3dcUub42jH8GPt5vPl0M/QeQG2yLn5WWBP/FQw+2Yan4s6yH0Ro/ji+ qS04eow8+5dPkdqHVrlo7OrazijhFKLKXddf6vGB5bcrovGys+5MHMKJGlr3sX0g+aeK HJKPaB6YSIopEMkopNcvfHJzy5rQ3+3mg6ey/bGzmAV3npR1fWi4TbHzdXQXTxHDub6b LPjWI4kXgF5wO0d9LH+hU9z9czrjgeeciuNaAms6fH57FV7NV3wEL+gW76aoozJ6QbnN vFGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=utTJOkvRGl0nLL0ic/scm4HYcaWRO/7AK96vkAXGENA=; b=Yobal+5JfaveBGGwkyuGuFihsbCgORxDvJRpIHjRcXr7x2udv9hWZ9xBx0TSVRczAu VDaPGluPTF0EvXIfG4THwY2YOr/JFJ0rrWF7eE51CRrNNfJNnTIAev249MMQJvdSr+ho bx5WpmSwhWGJuNyask9mMSquhwgad/z0yyWu86yy8WysGcKMUnVdQMHdqo1PhmkdkBR5 xbIershLhcCYnZCsMrzYEKoF8bcKUrBbnwxHt7WFsdGMxlsPXcmD9FkF+F4Us55HCsSf dK1wn1viwr/Llxm5QaDrFdUj5yL3MX4dn+cdLqUzTd+ViXdVsAZQtchWILZnuEspBQt8 2Buw== X-Gm-Message-State: AO0yUKUekT9CIQakLO7f9m3qTNbzeJg1kmuqZphv5cJ8xdsyQkY+zysW yngCzgV4UevIdlTdOn9LlBt353pZCw== X-Google-Smtp-Source: AK7set9s24s+5VohUfii+zFTuaQPsjyDds6x0J8bwgW3veskTcuKrcCryCoVd2C3OOn3WOO7VsqdRu1bog== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:53eb:6453:f5f5:3bb9]) (user=elver job=sendgmr) by 2002:a50:d682:0:b0:4af:70a5:55af with SMTP id r2-20020a50d682000000b004af70a555afmr2075203edi.1.1677229198008; Fri, 24 Feb 2023 00:59:58 -0800 (PST) Date: Fri, 24 Feb 2023 09:59:39 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224085942.1791837-1-elver@google.com> Subject: [PATCH v5 1/4] kasan: Emit different calls for instrumentable memintrinsics 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 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Clang 15 provides an option to prefix memcpy/memset/memmove calls with __asan_/__hwasan_ in instrumented functions: https://reviews.llvm.org/D122724 GCC will add support in future: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108777 Use it to regain KASAN instrumentation of memcpy/memset/memmove on architectures that require noinstr to be really free from instrumented mem*() functions (all GENERIC_ENTRY architectures). Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Signed-off-by: Marco Elver Acked-by: Peter Zijlstra (Intel) Reviewed-by: Andrey Konovalov Tested-by: Linux Kernel Functional Testing Tested-by: Naresh Kamboju --- v4: * Also enable it for KASAN_SW_TAGS (__hwasan_mem*). v3: * No change. v2: * Use asan-kernel-mem-intrinsic-prefix=1, so that once GCC supports the param, it also works there (it needs the =1). The Fixes tag is just there to show the dependency, and that people shouldn't apply this patch without 69d4c0d32186. --- mm/kasan/kasan.h | 4 ++++ mm/kasan/shadow.c | 11 +++++++++++ scripts/Makefile.kasan | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 71c15438afcf..172713b87556 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -637,4 +637,8 @@ void __hwasan_storeN_noabort(unsigned long addr, size_t size); void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size); +void *__hwasan_memset(void *addr, int c, size_t len); +void *__hwasan_memmove(void *dest, const void *src, size_t len); +void *__hwasan_memcpy(void *dest, const void *src, size_t len); + #endif /* __MM_KASAN_KASAN_H */ diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index 98269936a5e4..f8a47cb299cb 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -107,6 +107,17 @@ void *__asan_memcpy(void *dest, const void *src, size_t len) } EXPORT_SYMBOL(__asan_memcpy); +#ifdef CONFIG_KASAN_SW_TAGS +void *__hwasan_memset(void *addr, int c, size_t len) __alias(__asan_memset); +EXPORT_SYMBOL(__hwasan_memset); +#ifdef __HAVE_ARCH_MEMMOVE +void *__hwasan_memmove(void *dest, const void *src, size_t len) __alias(__asan_memmove); +EXPORT_SYMBOL(__hwasan_memmove); +#endif +void *__hwasan_memcpy(void *dest, const void *src, size_t len) __alias(__asan_memcpy); +EXPORT_SYMBOL(__hwasan_memcpy); +#endif + void kasan_poison(const void *addr, size_t size, u8 value, bool init) { void *shadow_start, *shadow_end; diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan index b9e94c5e7097..fa9f836f8039 100644 --- a/scripts/Makefile.kasan +++ b/scripts/Makefile.kasan @@ -38,6 +38,11 @@ endif CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable)) +# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*() +# instead. With compilers that don't support this option, compiler-inserted +# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures. +CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1) + endif # CONFIG_KASAN_GENERIC ifdef CONFIG_KASAN_SW_TAGS @@ -54,6 +59,9 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ $(call cc-param,hwasan-inline-all-checks=0) \ $(instrumentation_flags) +# Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*(). +CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1) + endif # CONFIG_KASAN_SW_TAGS export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE 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: 13151055 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C9C3C677F1 for ; Fri, 24 Feb 2023 09:00:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229734AbjBXJAM (ORCPT ); Fri, 24 Feb 2023 04:00:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229907AbjBXJAF (ORCPT ); Fri, 24 Feb 2023 04:00:05 -0500 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A5B663A06 for ; Fri, 24 Feb 2023 01:00:03 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id eg35-20020a05640228a300b004ad6e399b73so17740496edb.10 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=t/Lq7YO3Szv1x8uNBZOucP/Wbn92mPm83LmOiKQ7p5Qn6qRXX7z8QY7E6MHK9GQiNp MHoYhnp7apH3dWpa2EzMZr4lFBo8MwW71/bmp2TDN5L1PmD0PDZg1w7lyBfBXgsxfCZ4 WuB12ZquR3BtKo3VGVEkm1NjDTNEAum/67v+C0ec+B8hno/Nkby2SJ3YPZVqOabY3AdX vAoeiBDTeAQLmum/yQHytTB3ls74apTgjKYvRmEtKkvRugTyF3PWxnZV4jc4msiouF6E /TB5yRgLQcfFM5W/8zUENSPqKtGhLhmNck87T470HDJxb0cgntvswaimRzIJiMFy9ons 7QsA== X-Gm-Message-State: AO0yUKVVSIqzT79hR4aoI3V4oE+cr1/7gOE5VVbJyiLRMZHrQbXP40XM pglU277GShin7Ub3FDjSiJV1kg4cOQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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))) From patchwork Fri Feb 24 08:59:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13151056 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81E59C61DA3 for ; Fri, 24 Feb 2023 09:00:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229923AbjBXJAO (ORCPT ); Fri, 24 Feb 2023 04:00:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbjBXJAI (ORCPT ); Fri, 24 Feb 2023 04:00:08 -0500 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE3F163DC2 for ; Fri, 24 Feb 2023 01:00:05 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id ec13-20020a0564020d4d00b004a621e993a8so18498485edb.13 for ; Fri, 24 Feb 2023 01:00:05 -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=U+KSe6yW61OhH8F5xK9sHNFGGyhwmdu+LoC5TlHU1sA=; b=MeczQ9i/Gh5ybhKSh6LHvUv3XoWphMzafnUqnCe84CpugDhL780/nnP4v86GRpxut3 fN83m3TjZfUfh40XNOmFhKFmdEqHFqEVZmuhpk3Tlq5HdyAFoH1GpaVMlfTlB9Lj27L0 5bHOtC8kl5EXXZfaVzoBwKtBYrvNBPogX+U10Qu55u5F6kaBmMS4LWai8CnqIxTtePbJ r5WOcRQcj4QKFbLshIPoCj3NywHr80ZOAwMDfB2al588iDlEXtct6RN7xY79W1bHTy7a IqsyMT2L4sF1zgurIlgG2HNGoVkEKVSau31sfRUAOzTckgDPykx8WLBc2nLSHcolzkQf +4AA== 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=U+KSe6yW61OhH8F5xK9sHNFGGyhwmdu+LoC5TlHU1sA=; b=dk5AyWm0c+5Hzizib0lPbEfbTutDiz4aTzyFVJThEk/MZ+NBpGzJeEg019b044/EHa O6KDb74yD+7jeaH8WWz4d/T2ltxc27/3StbNWE94bvFzajzURuY/fI+zJL/XMnjxIz1Y LtlX8SLtUnvTHnkxtK7T7zJYmncb0AWYamCEBRz+F+Takw6QFAK7gXV1VasvgFQTfFy0 s0M9Up/xOPo4jQBWIVchVOvcRgKG3RUtLyHsgiI0cUv8zLYRCiL142SsBn/hmv/tSGXK blpWPKKG+EtTLzRhosR1UVNqlC5bK3StQkyyPqkvVZe9ubs4Iq5EOSb+e6DXF7iin+dU s3/g== X-Gm-Message-State: AO0yUKXJtsWz6/YfUuDL9LYeP4xkC9ZYo/waOJuUUCGZf6cCMLVu09jA mS8n1fMsFb1OLlhWyXqdgJEqQxJ+ng== X-Google-Smtp-Source: AK7set8yeA9c9phELQWpHGJcxcfHs4Ohf8boIVTjey61Dmo3Jly/lRrvnHxnoJmxBuCnZumHAIuEQLl2Dg== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:53eb:6453:f5f5:3bb9]) (user=elver job=sendgmr) by 2002:a50:c301:0:b0:49d:ec5e:1e9a with SMTP id a1-20020a50c301000000b0049dec5e1e9amr7086377edb.7.1677229204289; Fri, 24 Feb 2023 01:00:04 -0800 (PST) Date: Fri, 24 Feb 2023 09:59:41 +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-3-elver@google.com> Subject: [PATCH v5 3/4] kasan: test: Fix test for new meminstrinsic instrumentation 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 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org The tests for memset/memmove have been failing since they haven't been instrumented in 69d4c0d32186. Fix the test to recognize when memintrinsics aren't instrumented, and skip test cases accordingly. We also need to conditionally pass -fno-builtin to the test, otherwise the instrumentation pass won't recognize memintrinsics and end up not instrumenting them either. Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Reported-by: Linux Kernel Functional Testing Signed-off-by: Marco Elver Reviewed-by: Andrey Konovalov Tested-by: Linux Kernel Functional Testing Tested-by: Naresh Kamboju --- v4: * New patch. --- mm/kasan/Makefile | 9 ++++++++- mm/kasan/kasan_test.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile index d4837bff3b60..7634dd2a6128 100644 --- a/mm/kasan/Makefile +++ b/mm/kasan/Makefile @@ -35,7 +35,14 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME) -CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) -fno-builtin $(call cc-disable-warning, vla) +CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla) +ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX +# If compiler instruments memintrinsics by prefixing them with __asan/__hwasan, +# we need to treat them normally (as builtins), otherwise the compiler won't +# recognize them as instrumentable. If it doesn't instrument them, we need to +# pass -fno-builtin, so the compiler doesn't inline them. +CFLAGS_KASAN_TEST += -fno-builtin +endif CFLAGS_kasan_test.o := $(CFLAGS_KASAN_TEST) CFLAGS_kasan_test_module.o := $(CFLAGS_KASAN_TEST) diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c index 74cd80c12b25..627eaf1ee1db 100644 --- a/mm/kasan/kasan_test.c +++ b/mm/kasan/kasan_test.c @@ -165,6 +165,15 @@ static void kasan_test_exit(struct kunit *test) kunit_skip((test), "Test requires " #config "=n"); \ } while (0) +#define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do { \ + if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \ + break; /* No compiler instrumentation. */ \ + if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX)) \ + break; /* Should always be instrumented! */ \ + if (IS_ENABLED(CONFIG_GENERIC_ENTRY)) \ + kunit_skip((test), "Test requires checked mem*()"); \ +} while (0) + static void kmalloc_oob_right(struct kunit *test) { char *ptr; @@ -454,6 +463,8 @@ static void kmalloc_oob_16(struct kunit *test) u64 words[2]; } *ptr1, *ptr2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* This test is specifically crafted for the generic mode. */ KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC); @@ -476,6 +487,8 @@ static void kmalloc_uaf_16(struct kunit *test) u64 words[2]; } *ptr1, *ptr2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr1 = kmalloc(sizeof(*ptr1), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1); @@ -498,6 +511,8 @@ static void kmalloc_oob_memset_2(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -511,6 +526,8 @@ static void kmalloc_oob_memset_4(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -524,6 +541,8 @@ static void kmalloc_oob_memset_8(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -537,6 +556,8 @@ static void kmalloc_oob_memset_16(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -550,6 +571,8 @@ static void kmalloc_oob_in_memset(struct kunit *test) char *ptr; size_t size = 128 - KASAN_GRANULE_SIZE; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -566,6 +589,8 @@ static void kmalloc_memmove_negative_size(struct kunit *test) size_t size = 64; size_t invalid_size = -2; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* * Hardware tag-based mode doesn't check memmove for negative size. * As a result, this test introduces a side-effect memory corruption, @@ -590,6 +615,8 @@ static void kmalloc_memmove_invalid_size(struct kunit *test) size_t size = 64; size_t invalid_size = size; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + ptr = kmalloc(size, GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr); @@ -618,6 +645,8 @@ static void kmalloc_uaf_memset(struct kunit *test) char *ptr; size_t size = 33; + KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test); + /* * Only generic KASAN uses quarantine, which is required to avoid a * kernel memory corruption this test causes. From patchwork Fri Feb 24 08:59:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13151057 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7256BC64ED8 for ; Fri, 24 Feb 2023 09:00:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229880AbjBXJAP (ORCPT ); Fri, 24 Feb 2023 04:00:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229881AbjBXJAK (ORCPT ); Fri, 24 Feb 2023 04:00:10 -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 74DD44FA94 for ; Fri, 24 Feb 2023 01:00:08 -0800 (PST) Received: by mail-ed1-x54a.google.com with SMTP id co14-20020a0564020c0e00b004aab4319cedso17976918edb.2 for ; Fri, 24 Feb 2023 01:00:08 -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=AjV3k78q3XXhT1GHWCKfTuGEDWgoZOrO6Fv+xEYetZM=; b=EAGIOwQG78YZ4s5FvVIyWmJSdj/9+oRp2Z+JOiSUHX7UEHTavbHpWoSCToIN25ZVBh adJIFeTdGpgUgUlgPbR4Ed7A8vOy6wisOboBbYRYTdNzBqbwUwCAPRSSvoPl6Bh2MdOs DxBjn4j0OaOB/g65HjuRXZVQ1recSusgjrsuNkNrBC2P4Rnt9uhiw7JBjRqIzDCLljyl RPUozPvaTPHrb+3qYhJ4gLVhfC9eX0jRr0iYQ685BfQTxNz35kPbfNpdQBbihXcZZQ/x MfmXpF+Z0ozIFxGcdCWnKbZF7Ky7Dr+DKmP+UpWWa7yzYqJWwtqt9X0IGq+6o/8Df7+R thCA== 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=AjV3k78q3XXhT1GHWCKfTuGEDWgoZOrO6Fv+xEYetZM=; b=Tty2SMBEfBm7B0JGfv4hyyh6gxi7zqf4srbxyrUVXCXZ44YALNhveIxbCjGJLE0iWz XUXQaeb9Fk0JAxfjmkEKr3tbaqGbDyGyzJbUM7WeXRhXrlCgNn70lOMJ2BiIOzqQo3N0 TCnclrqIQC3NDti3csKSnxdusu6P568CrKbmaJL18rqDQi5TDPBfNeLhjJmZ1anp1RWy zxXgcobD+iKL3JasPbsz9X1TL6u1ws++Mnfd6TNHvO1RheA76fmUHIS4m/2vSYdYh9Ji V2HJvQEFoD8jIXSIJO56GGRv39E8TSW0G77GcV2dqKgWkCkH5pK5/zOXEUSxbpgGcqOs PfTg== X-Gm-Message-State: AO0yUKXJ+O0SchASBS7zAybohGHwlLQkonyqTUEkZd4nkT6WZauJnjj9 kNqqI9ktvkqDBvBO7sn5DpiTuJl/WQ== X-Google-Smtp-Source: AK7set+F0E0+hHGoUz0EDxGYty9hHOhM9cQA2n6bHMhFaQ+TBLOYuMWa+x6cFVA/P5UZsJl9kORIJ4SYdA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:53eb:6453:f5f5:3bb9]) (user=elver job=sendgmr) by 2002:a05:6402:3216:b0:4ad:7bb2:eefb with SMTP id g22-20020a056402321600b004ad7bb2eefbmr9255387eda.3.1677229206892; Fri, 24 Feb 2023 01:00:06 -0800 (PST) Date: Fri, 24 Feb 2023 09:59:42 +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-4-elver@google.com> Subject: [PATCH v5 4/4] kasan, x86: Don't rename memintrinsics 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 Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org Now that memcpy/memset/memmove are no longer overridden by KASAN, we can just use the normal symbol names in uninstrumented files. Drop the preprocessor redefinitions. Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Signed-off-by: Marco Elver Reviewed-by: Andrey Konovalov --- v5: * New patch. --- arch/x86/include/asm/string_64.h | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 888731ccf1f6..c1e14cee0722 100644 --- a/arch/x86/include/asm/string_64.h +++ b/arch/x86/include/asm/string_64.h @@ -85,25 +85,6 @@ 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__)) -/* - * For files that not instrumented (e.g. mm/slub.c) we - * should use not instrumented version of mem* functions. - */ - -#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 -#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ -#endif - -#endif - #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE #define __HAVE_ARCH_MEMCPY_FLUSHCACHE 1 void __memcpy_flushcache(void *dst, const void *src, size_t cnt);