From patchwork Thu Feb 16 23:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13144152 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 9AD5CC636D7 for ; Thu, 16 Feb 2023 23:45:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230022AbjBPXph (ORCPT ); Thu, 16 Feb 2023 18:45:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjBPXpg (ORCPT ); Thu, 16 Feb 2023 18:45:36 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A69A505D8 for ; Thu, 16 Feb 2023 15:45:35 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id k15-20020a5b0a0f000000b007eba3f8e3baso3636924ybq.4 for ; Thu, 16 Feb 2023 15:45:35 -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=f9fSJ8LX1LV0NJhDIu0hFxwhZk7Rq4zVsb0PZTFK4Co=; b=T1m8J9zu54fxOtNw1pfhMlQVMKTBI98xdI/WbelYkJUrDITtxfYmR5FLfm9XU00+xB RrI0OR1yS6IMQO/YgtX1dk+cglRU1BcwoVEv268fpi4FOw1p+H/khXU3KeXcBcUs0P/a N5oBiP/Mprdk9yX27S1VfpUsCYZGE1kSAaJiBZkAmchx904ORP6dD2n+6vCCCc9Bmp35 pTCFcu6YRrrm8sSn44TcuV4lSaYwyvsQmmSp//MdVpCxCNi05VziSOIZz6BIYIBOanmh wBxNwYgItWLMyNXHDJbtrh/NSCyNkbK+FGvTJlhndfBhKp5KQOkoEawctV9VNckVmuVk mA/Q== 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=f9fSJ8LX1LV0NJhDIu0hFxwhZk7Rq4zVsb0PZTFK4Co=; b=WXt9O1SY0qbtY33CVwrvk0E2TvdV6sACDcdGd148UUjWudNiWa5DCBQpr46lgwYm7O mWNY3XY1mUerJqlrk50WdDQYz39p3ElXxuTF92uI8hLOEwWnDEpOFPQSg73r5oBJHxzj bSde+qJCa3hyRtTUcjt+MZ0UWU2WVteUk3ZH8V/fi3a5mGvpY6RNUYgdSTH8AZYH58cq cZkHgm1cKaGgM0GdDdy7EepkLvVhl19AV3Xf4mgoBxHbrQaAKjpRWIIT0jPjWEfJH6A4 OXXg6eEFCHT1TNcuWbi74dY/TJFcgflBBQ8SvcO81KucjEUGkBrPCKVeWnXrx97Pv54C /kHA== X-Gm-Message-State: AO0yUKXAvWcOLLGsWj4Mvt6UCH+hVRnbbZSugfUR0esEUasz7xPRo/hk L3Q3//rYPOMnJyXV5h3kMUODJHT5pw== X-Google-Smtp-Source: AK7set/IFbohazrUlym85MQHWcUsKV5YXF2glA/nmcXrwKnVDZvE8l4eL3cW+ZnSjjVoiViI/jThdbWCyQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:34a3:b9c:4ef:ef85]) (user=elver job=sendgmr) by 2002:a5b:6c7:0:b0:8dd:4f2c:ede4 with SMTP id r7-20020a5b06c7000000b008dd4f2cede4mr5888ybq.2.1676591134138; Thu, 16 Feb 2023 15:45:34 -0800 (PST) Date: Fri, 17 Feb 2023 00:45:20 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230216234522.3757369-1-elver@google.com> Subject: [PATCH -tip v4 1/3] kasan: Emit different calls for instrumentable memintrinsics From: Marco Elver To: elver@google.com, Peter Zijlstra Cc: Ingo Molnar , Jakub Jelinek , linux-toolchains@vger.kernel.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Nathan Chancellor , Nick Desaulniers , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org 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 Thu Feb 16 23:45:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13144153 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 86473C636CC for ; Thu, 16 Feb 2023 23:45:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230125AbjBPXpl (ORCPT ); Thu, 16 Feb 2023 18:45:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230103AbjBPXpk (ORCPT ); Thu, 16 Feb 2023 18:45:40 -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 92AB850AEC for ; Thu, 16 Feb 2023 15:45:38 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id fd23-20020a056402389700b004aaa054d189so3554658edb.11 for ; Thu, 16 Feb 2023 15:45:38 -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=WEP0gdOIpvbUmMDY5eO53Tqx5XD06xr9LMwpgFaq1D0=; b=HPdFHv+aFKSsMbpRelJocXIyNNaSTVp3qtYGElmUkrSvs23G5uEnD+LPzwqpz2nqAX joUBeuv79NzEkPLc7mOxZoqwzVDUX2ycxbzeYQwSktyAJ44NBt7BtMjs+ZUCkCJbBGvh hMjDWAN0oodfC6ZSIMrPXtmSXIRP5wm4Cw2yQhBzH61FP8u0+Aj+P/vCkrSqwC/++qBV M1WDwrfKp3+bR+SMQbvIsRyz719j59IB0w/+Zbj4dTOfA7wrlaGroAwL4HUwMff9vMPe iAK38PEmmHmE2A0T11QxgDCFcAV7XQGZaPSySDNXm41D+0sxrmP2uphiQZZXVvsbIgFn Dkjg== 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=WEP0gdOIpvbUmMDY5eO53Tqx5XD06xr9LMwpgFaq1D0=; b=NSpm2tNTsF6Trxlvn3YUy6OUK2aUtglEuvOkQgGap9+Pa+ml6cYujw43OoYVgEQvQj SVHfiKVjV454nQ5TrIyShUeUB4fAtkIgm7/cmBg0sXb7Lnhk58EJ5W0uvde+1VB4uhhU HU5xdIQ1qTdV/QnVOfTteX/WXkv7/hi98W98P5c7rCKucSxrbHa3WgKVYI/pPOCN5H7v 2flAZz9NU2U5PTM3IRIiaieu+fofqc98zpEu817gxcp+ayqvV9C7DjHZdL9WkxqtOG23 tlPUQgbXb1r6uQY/o9wDZPYb+XIqJin+pV8bWdtAjVLHB54daSzuogbxyD8j00a7fvzl STEA== X-Gm-Message-State: AO0yUKW1oZVT2TUgFNMxECB6V7vTPk1VSdCKP5WlnLpThktBt50UW3ut MnxuhP4Kz2m55EzsPXKadn46NW+5Uw== X-Google-Smtp-Source: AK7set/LwR60te1ZQ71uZxsc1KgkcxZbDo0ZLCv+udJZ5Z75cmOSDIH+Ct1XO9+Xmd+GDajMudFF6OPa5g== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:34a3:b9c:4ef:ef85]) (user=elver job=sendgmr) by 2002:a05:6402:2485:b0:4ad:739c:b38e with SMTP id q5-20020a056402248500b004ad739cb38emr1528006eda.1.1676591136985; Thu, 16 Feb 2023 15:45:36 -0800 (PST) Date: Fri, 17 Feb 2023 00:45:21 +0100 In-Reply-To: <20230216234522.3757369-1-elver@google.com> Mime-Version: 1.0 References: <20230216234522.3757369-1-elver@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230216234522.3757369-2-elver@google.com> Subject: [PATCH -tip v4 2/3] kasan: Treat meminstrinsic as builtins in uninstrumented files From: Marco Elver To: elver@google.com, Peter Zijlstra Cc: Ingo Molnar , Jakub Jelinek , linux-toolchains@vger.kernel.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Nathan Chancellor , Nick Desaulniers , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org 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 --- 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 Thu Feb 16 23:45:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13144154 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 A3BE5C61DA4 for ; Thu, 16 Feb 2023 23:45:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230154AbjBPXpr (ORCPT ); Thu, 16 Feb 2023 18:45:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230147AbjBPXpn (ORCPT ); Thu, 16 Feb 2023 18:45:43 -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 491E054D07 for ; Thu, 16 Feb 2023 15:45:41 -0800 (PST) Received: by mail-ed1-x549.google.com with SMTP id h10-20020a056402280a00b004acc6cf6322so3449914ede.18 for ; Thu, 16 Feb 2023 15:45:41 -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=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=kxkMJWrbmyrtSo3LrSby0SwZzdaqZYA+OtPPgxXN6/0QaRyj5lavrbQFjW2bbILnFt 5uPFH5+4U0eiKUrEoG4olWBF7AAs3gt01G7UXt+ZVT2oc+Ioxre902/va0qyIw0IsYNo UIGftrchUxlwRzYYZJHYUWpIGU1U7SBya34EhiVUGPSzSiFz1ty7CaGlT4xX+OP0LO1n IaD4vgSOVs7KgbjfZLPAzA+zD1a1zkIbZerhqZV3N3PeTYV+Kn3ChU+3Xq/a7iBt3NhV JmHVCSOj+q0jzhSDGJ/URyBxo+1KPH+Tvb57p5JAqSc2pdjvVNv5DVVXQ85YmxcCu7Ss xnyQ== 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=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=jgnI4RCZEAsR30DV7m41qHII1foejRjHuG/2pNSkjhvFC3rVYYOxyE46c10Xh4wG4T Rn6lgkczGJGa6Mj//HWVPfktx/NbqVrSwBhArZlXlw5QPQKus6LLxn53xiGnHnLHfVZc RWGjaugVhPnZqQE4YKOph/dV3sAGIHn+wCe+3PF1+55AIsOJYwXFqUc26NH4PqOko+mP RcF8wxUkOVhDhBEy8Ytw/1fKP8VLQ/h+jIzm3uahmzGL7ngaysBcS/eS9oU6BP6LOUDQ B/b1TBDHnS4EnZXkI/t2AcfeWuzk30xTyNCz+sQB0a2DndvmAtAhgNGqqtXz+tr9/LK4 1fZg== X-Gm-Message-State: AO0yUKWO4m4S5BBojzKsqwEuSlC70zKpkdGE0SVyWl5VPvYjPHSgRL07 Tv7GbloBWlIxhDMPij1prwyaynk0AA== X-Google-Smtp-Source: AK7set/j8U4qSwH8f7/DIupIA2FVdAxH3q0WmqpUrL8Ezg1GdgSGfKKPajMXy5B6e+X7QpIqBfmsb2AzLA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:34a3:b9c:4ef:ef85]) (user=elver job=sendgmr) by 2002:a17:907:206e:b0:8b1:2653:c5f3 with SMTP id qp14-20020a170907206e00b008b12653c5f3mr3693391ejb.4.1676591139804; Thu, 16 Feb 2023 15:45:39 -0800 (PST) Date: Fri, 17 Feb 2023 00:45:22 +0100 In-Reply-To: <20230216234522.3757369-1-elver@google.com> Mime-Version: 1.0 References: <20230216234522.3757369-1-elver@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230216234522.3757369-3-elver@google.com> Subject: [PATCH -tip v4 3/3] kasan: test: Fix test for new meminstrinsic instrumentation From: Marco Elver To: elver@google.com, Peter Zijlstra Cc: Ingo Molnar , Jakub Jelinek , linux-toolchains@vger.kernel.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Nathan Chancellor , Nick Desaulniers , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org, Linux Kernel Functional Testing 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 Tested-by: Alexander Potapenko Reviewed-by: Andrey Konovalov --- 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 17 12:53:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13144666 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 28182C05027 for ; Fri, 17 Feb 2023 12:53:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229575AbjBQMxX (ORCPT ); Fri, 17 Feb 2023 07:53:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229695AbjBQMxW (ORCPT ); Fri, 17 Feb 2023 07:53:22 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8521D19F34 for ; Fri, 17 Feb 2023 04:53:19 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id w13so836379wrl.13 for ; Fri, 17 Feb 2023 04:53:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=user-agent: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=Ulgx+po/trB+4FXQcLmj6nxw3N0m6LL1KXnQpc+SYhI=; b=BVGRp9vYDHaCoxsJwjIYvbuDVbtvx0mVDEmbX2XZy1ny1uCuSjxjVE1KzSyOk3rj68 k44oJEa1DPZ4PRmT7xT/Q0u4DRT2JSJ/yo8S1a7AaMmYCvBjLHcX2R10GNIz6orDQOsX caM8QvKnausu95V3Nykyrl6B1e4AjbF2MdOKqNLT54r0MUW5HnpnlqBmXGnGzka3U4KQ G+4xM3Mi9Rkg+zl7MNlhU3uRT5nCpv8Ej9bPU+mSGdU2obiShaU/RgpDqwmiPx6rPtwe npy5gWb5Kq7NBI0JHLXVs3DeTqD9M+5UWblWrMSfPkrJk1MR6fQPP9yWt51aSkt66MPZ QKHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent: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=Ulgx+po/trB+4FXQcLmj6nxw3N0m6LL1KXnQpc+SYhI=; b=c5Rn0PdU09H3sRRaaeTi0+Pr1C73mCcNoQOExE9YWqLulAigVvCHQh9uyRHfNbfnVv v87XzY2oybJ5yCyPyDgyOL2a5U9cBT7Y33nrWJINiUfjEslYyYzqQ5iH7rBoCI87Z+xB /Mo+gepOJSIDBWBiBVRk6e0cHciwRGz/j3P5c+gVD97ZoCXY8jI5ywiq6V7WPzeOpdcO KAqUi2HBHXX+MaZ+NoZaZaWUQcX+yaCMdBUX0CZO9vRgKr1Pn3gs83Xu0eUPm8aMuWSJ udlXGVqCH/N8R6GNZ6kFMRGMvjUQkUhSxsxpnCI3R4GyA94ml3i604lGzICFqNGC+OUP gCeA== X-Gm-Message-State: AO0yUKWsFAFGcEvCfRPZbFENhKJhqL56zUDlLblCJxeE4+BJovMCUbME /EtFcPlSSbwm2awv3AH0LcwrSA== X-Google-Smtp-Source: AK7set8a/gyHg3t36Zyr4mxRFV/aOKMOB5cnYYED79FZtuRdCJQaJGG18Ti/CRRpve9Jdk6dcbhgQA== X-Received: by 2002:adf:fb92:0:b0:2c5:5933:1752 with SMTP id a18-20020adffb92000000b002c559331752mr700692wrr.52.1676638397783; Fri, 17 Feb 2023 04:53:17 -0800 (PST) Received: from elver.google.com ([2a00:79e0:9c:201:34a3:b9c:4ef:ef85]) by smtp.gmail.com with ESMTPSA id s17-20020a5d4251000000b002c6e8cb612fsm1050348wrr.92.2023.02.17.04.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Feb 2023 04:53:17 -0800 (PST) Date: Fri, 17 Feb 2023 13:53:10 +0100 From: Marco Elver To: Peter Zijlstra Cc: Ingo Molnar , Jakub Jelinek , linux-toolchains@vger.kernel.org, Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Andrew Morton , Nathan Chancellor , Nick Desaulniers , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Subject: [PATCH -tip v4 4/4] kasan, x86: Don't rename memintrinsics in uninstrumented files Message-ID: References: <20230216234522.3757369-1-elver@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230216234522.3757369-1-elver@google.com> User-Agent: Mutt/2.2.9 (2022-11-12) 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 --- v4: * 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);