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: 13144149 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 27F08C636CC for ; Thu, 16 Feb 2023 23:45:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F8816B0071; Thu, 16 Feb 2023 18:45:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A92E6B0072; Thu, 16 Feb 2023 18:45:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 397DB6B0074; Thu, 16 Feb 2023 18:45:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2AE3D6B0071 for ; Thu, 16 Feb 2023 18:45:37 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F2295140843 for ; Thu, 16 Feb 2023 23:45:36 +0000 (UTC) X-FDA: 80474789472.10.5E6E57E Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf02.hostedemail.com (Postfix) with ESMTP id 3449D80017 for ; Thu, 16 Feb 2023 23:45:34 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=T1m8J9zu; spf=pass (imf02.hostedemail.com: domain of 3HsDuYwUKCH8hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3HsDuYwUKCH8hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676591135; a=rsa-sha256; cv=none; b=hDM/LChEU8w7Vut8+R/Jnows1ywHDe6PvzTS+f8Xfvub2Lt3Bg4XkOvkUHtfYz2rvRIl7c c3ee6pp12dYq365vqluBW83Xh4Z1puAYIKdzPaUyFyWcr7wOyeu9wVUki8kMxOyHUHan3Z N9hZJwHKUZ7kN6eVKH9TuxnZO7w1nA4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=T1m8J9zu; spf=pass (imf02.hostedemail.com: domain of 3HsDuYwUKCH8hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3HsDuYwUKCH8hoyhujrrjoh.frpolqx0-ppnydfn.ruj@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=1676591135; 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: references:dkim-signature; bh=f9fSJ8LX1LV0NJhDIu0hFxwhZk7Rq4zVsb0PZTFK4Co=; b=WnhSGio35RyUtfsmftQ2lC0d3qusbzth3RF4aU75xL5pnT9do70xzeDQpU+NuazEssyz8+ Wr/402UFddOGZUcTyNDoCD+wCt8tEO4P2T0HflUNq24dZsPTB9blrIhM3avXKy7nwpJLdW eUP9GZaSo0tgu2D2tIGkKTPTaabSIVY= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-4cddba76f55so38828607b3.23 for ; Thu, 16 Feb 2023 15:45:34 -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=V25gBGqo+shjeMCriJrVQmiY97UIXuEPnltIiSe5xx9PWGB/I3o+3wpF+IwHHKrWjd QfeOK9fZ9iiSE57JQPZek9AAfVL0xI8TuFegMqIGcV48TdejvBiuJLirLb88r8IE6JLU mttrSP3PrwT7Jzyui8OtGOzrZhQKx8s7WJk60svUO79ajXbeBHhYMyISYJyKvnWMvvix ygjWjanc1WMPlVJam4JvOsNTGzZyVbZelmVfRHIKAL1qBZHBJh40wqJfztn3O0L+XdGt KdIpvMzDMgbXAIz1mdgCkn5kYiIwZMIm2yIIWSGKKifBA4ps/yzOgeWqg8CuhU/X8bg6 pjPg== X-Gm-Message-State: AO0yUKWsAE6jQmsFQlEXlnmNvHyJLB6dVqFSI7uVJuxWPfrPRhOyMolG DyouE7E/dvQUL+oxFNCpv/42dpAYGg== 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 X-Rspam-User: X-Rspamd-Queue-Id: 3449D80017 X-Rspamd-Server: rspam01 X-Stat-Signature: n5objkeyey7zpmn6npusa9bah4gg6f59 X-HE-Tag: 1676591134-871959 X-HE-Meta: U2FsdGVkX1++DZjhqs7U0lgzh8oW/8yvbBP/0XHZqnIGcMT2TrTisk/cyoshheYaKlSq+BhFXxYoWkocM759FsmLXENy+xw1Mp7fbSGwFQOQ3y1d9C8n8FPG29x63VZ0R1CZjSCkdS+lnwp/7gj04E/PCpxIC3Gh1J/sutAJ2vpr/1N49b/uOoYiGBGSlsZlWuAyv96g1eWqeF9zvE+nBOxCR7TBEKhW8t4PRrNvlZ5F3oQR+2e/gJRH8m29YvwplpYXQrCtq+BnKQ5sw8RFCpDERyXlJTDR3sM2W5wnB3jhRiH/jL/UY2XdEbi23IEEUk08/2VQ1My+4gSAPCLwTLKumALg4oYkmzA14yx13bSt7afk+0TeyzAX1QJOTKYyKIChQArB2dkNtHmIm+Y6qi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ckHolIP28zXq/xO+i+bZCxvLgO2UkfasnsqCdmg63EFFtHGJFtIILB5M7YqbtPdAI7fVnhRowkWIfORDlzNVemA8ANKhR1n6jUy7DywQMJtKMGL84Rgp7uN318ejw/UZ6HWYBACSvSS7z8uhpIaBxM5dGREjgrO8NDNA9k7OCpQuEaBOAZ0MoZF4xUeiU7DsW6WK3AtE1yKrDEYqSslwXuicV+F5YD8VSS72G8p++3yXfbUwAmrrzHG/na5Zkz7HPGJ9vxusLnioqskzArzaoevlkwilp8wLvckIl81kddcE/eKqhVMLftRDBaUrQNehHELmZBoTua9HCuteuTFlC8op86Uu/vkKRkqar8sXT0UhCqEDpW6dVZb1Tgo= 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: 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: 13144150 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 6B8ECC61DA4 for ; Thu, 16 Feb 2023 23:45:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D86216B0072; Thu, 16 Feb 2023 18:45:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D0D596B0074; Thu, 16 Feb 2023 18:45:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAE806B0075; Thu, 16 Feb 2023 18:45:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AC15D6B0072 for ; Thu, 16 Feb 2023 18:45:40 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7720941204 for ; Thu, 16 Feb 2023 23:45:40 +0000 (UTC) X-FDA: 80474789640.03.8892B58 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf16.hostedemail.com (Postfix) with ESMTP id A7D4C18000C for ; Thu, 16 Feb 2023 23:45:38 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HPdFHv+a; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3IMDuYwUKCIEjq0jwlttlqj.htrqnsz2-rrp0fhp.twl@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3IMDuYwUKCIEjq0jwlttlqj.htrqnsz2-rrp0fhp.twl@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676591138; 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=WEP0gdOIpvbUmMDY5eO53Tqx5XD06xr9LMwpgFaq1D0=; b=Rn8B/EGeMcvbXCOnfCx14/MoFUy9ij+JrC6dVF6QxvGdOSAJqdPRIWXco1XZZGbWlo/vNN g0hWjtP6qtnlYbbmySSilYlylZ4Fo5RRuY/KFy24OE9ueQAZTlRZHFVeN+NKnnJI+tFJEQ EXdgDuTDUQm0t5M86/SSf3p58NtnNv8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HPdFHv+a; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3IMDuYwUKCIEjq0jwlttlqj.htrqnsz2-rrp0fhp.twl@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3IMDuYwUKCIEjq0jwlttlqj.htrqnsz2-rrp0fhp.twl@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676591138; a=rsa-sha256; cv=none; b=Vdh8kMOq/p44DJzyhz5ID8ysudz5aYl8LxUqujp+pfDngsHa5koYM7LG7eT9gi99ZH0Vni /ojgpwc/pM0ScBfqC01y0yL4MYNOjC6WF/OU4Nc1kV3jcjM3TwxblPf82c5WdeUiJ6zLyd HnACWJgyoJEpwDhcUHezi1pGMCsSH5k= Received: by mail-ed1-f74.google.com with SMTP id o21-20020aa7dd55000000b004a245f58006so3555462edw.12 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=zZ9DiHBZ9rAsvTvXkpzhjR+r/sIZHhWMhwQgYYFOqmrJSst4YCK3TGAyuDUJARm0ZG j5T+/QgU8IMYJizobVlTF+aqNQXRkeQV2D4j743jVgetg2Nt2YvaH2zpI7Fa5gGURvZt +dTcMnSwWRw6b6Zmq2kWnIiWObf4d2LplWk5QX00Vt+gIWtVWd+GwnTRiTZRiPFOgosM SlOljWJJU/QWfH4Sya0j5Ye4VvTeUXS4k7tXBwLxP+MTo0AoP3bB5lCwxWZQCijXcq99 F2cymrjQqHtUJeld6rMkT1RPcXNinZq2psNEdgsprUvKiOpR3oufdsBTRCwT3kYa7BB4 LUcw== X-Gm-Message-State: AO0yUKVPXSnBlpc7kHxo7ZU+5p2HF8XYh2KZZwxGh5CA0ilG6j1tvqOm vCZ5WUca5qrvqJiZPtOK/AeHw4oUJw== 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 X-Rspamd-Queue-Id: A7D4C18000C X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: ibitdg8qmdpy51oe9c4kfh1wecd4cjfe X-HE-Tag: 1676591138-249787 X-HE-Meta: U2FsdGVkX198yKBQs+LEoKiIJCa817XUxNtV9BM5VKVuQzggpPqaSDoZJaZP6YqOtiJvlLrbwkFiKqtaiKUR+gNLSBk+q1rPgz6m6CUZQoXaUc4+hPE1rWUgE8TJLe/ZLzvH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PGi/NNg/2f1HDkUGz7uUYkMm1SO4wI6VmxPW8uTUm0mNqB6GjoppFuqUwX0da/XmPYA1T8PLhJ1AZFxDF0M7UOMsKCmSgY/ujZzJ1pxJUUEG0xfFZiiO71jrH3hsMHRumG2dJsUUTqAG0/3iphDFImRghPXywGRo7tZjYdKlfHK/l9D1Ms5G4wHAMP7gkhra/qbp8HN7Tq7h939VFEgMb1N8nCOUqsDmj2nIW3yV/udCczDImR9ValYOPwL0RgZkNCjSppMGFh4nKii90lLX32+j1SHyvnv9Afq//8hs4Tmz1TVp36qRa8QKTB/VAF7twMXUN/nJIWO9hG8f91mrKkrHeOqwXTzxPrdJY59dt35T/6ZJHlHp5+e9XjYVS89iGo4QX5pqDl4bI 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 --- 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: 13144151 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 D252BC61DA4 for ; Thu, 16 Feb 2023 23:45:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6808D6B0074; Thu, 16 Feb 2023 18:45:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 62FE96B0075; Thu, 16 Feb 2023 18:45:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F8046B0078; Thu, 16 Feb 2023 18:45:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 405976B0074 for ; Thu, 16 Feb 2023 18:45:44 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 16626A12D7 for ; Thu, 16 Feb 2023 23:45:44 +0000 (UTC) X-FDA: 80474789808.25.75557FE Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 2F0EC4000D for ; Thu, 16 Feb 2023 23:45:40 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kxkMJWrb; spf=pass (imf01.hostedemail.com: domain of 3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@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=1676591141; 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=gW18Fl3dm52InVgglE8m5dF8kxrMU+Nd8TZ0107EoLE=; b=P9XeQQtG5gdvKWuKDp5A7ZFaATOOl72z1sWdHqdd+22BaBof/6/vtiAgs2p9Q65RQYyT2k 6j9PGueTMZKUBEbZs+ncw65I2ahI+7OKZYmwZTrNN5G25/0bkKPKpMOFQ/uETZPef9uMBA Xg/vLEIvARugUA+tchAHkvDrYtzfiBU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=kxkMJWrb; spf=pass (imf01.hostedemail.com: domain of 3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3I8DuYwUKCIQmt3mzowwotm.kwutqv25-uus3iks.wzo@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676591141; a=rsa-sha256; cv=none; b=G8ZgzQUqxDYX2fERgfS+vfL/vYZDOTnwgkRCbSPXrFOX4IW4ml0MzELuj+dlaZ/5AVB5Zk 6RXVJMQTrSRCqmdmaLCmjNgZuKzmCs+3/5OlBc9vOiXLGZNPMtgDqwa65tATE9FZTuRNsZ xLiX1LngJHYK07YK+7Kcx0iHko85G+c= Received: by mail-ed1-f74.google.com with SMTP id bo27-20020a0564020b3b00b004a6c2f6a226so3550851edb.15 for ; Thu, 16 Feb 2023 15:45:40 -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=W9QpKig3pV84P9YyO6p+EpwmswNLlTx458va9yreAD86YNvhoNpfPE4rLjzfmjrJmv V8BJkKxxCAds30q30OhYEKl+rkCy6zRqY3/Rcww5prpLvL9N2NqK/2jSSiZwrL15Z8tK 3LXkVfWjB36fJucJVF9faHnX+XpiP/jdDxsIqc0nyuLw5HJ9kiBKuUiZOo1cGgXVML8c Iwb6oPhA3AMcHxIjxde4EVbbGKdWAcYAvE/3gl+qqUXKhQcEZFoPOcwttOar9+i3uSbG 37pLQ5Co0VRqwDyb/2gADmypxt5m0YK/DZ4QL0/quZSynTA4HcOzTJV/zAqJ0twtSNoq gOVA== X-Gm-Message-State: AO0yUKW6qyUfgSgBl4hnQw1ZIBAxwJ7voYwPHsG3PifbWQQzbw7rMvX1 sqeucI+YatSvU8JuXNVkZ+gK/Td7CQ== 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 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2F0EC4000D X-Stat-Signature: z6orpo178xp9mgjwdijyso8z6cmr6381 X-HE-Tag: 1676591140-763796 X-HE-Meta: U2FsdGVkX1+N0UDnbR/Qypq2xQykODR4lo1LWqgRtHiY0Q5pvWUE9LUcj8J3wQW4T1HD3EGUZw5v/R3oAzejfid1xoRnwvWNzCGepHhnZ/3K9ytdA1wSeE5VtWHJQhSHkLxIJQCkEU4iLfCAkPG9LLheftDOwEykuse2aJgum5Q7h6CIbeQBlEOf/RhbxOXxqqrPUpqj9fbLb/gIdqdpB5JCKzwmqMw3bOS0w+GX/34Ik4ok99ACnpD0384k19KYNJ2FVcuYJrsR4hkMHTq1LEm+wvAzYZuF04RZqGa4VVhKKh2H3JGJTgSHrgVU2o+Ys1BPYLrRYicznF5FIn4uU1xqfvrorMHVaSmQtiGgqje51wrCqFxFThMaaTtyHUyxO1mgvkXZ5XGlU1ZC4pI149I1+M9iTAZ50nLrE5MfYRrIQoC0mphwR9O2Hb2596vV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9we0f9ckaNtMFIpFW+BFUm9+m3C5UsbN37YJ1ZH+uivW+lPCBoIChnQ/mYgo8lK8MeSy7ezK9BPoQgjZk7DYc3aqZjMuG3gJEPHh7XiyE4FSxTjDIPoVKahjk7pe7g== 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: 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: 13144665 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 54F0CC636D4 for ; Fri, 17 Feb 2023 12:53:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2EE26B0072; Fri, 17 Feb 2023 07:53:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB8656B0073; Fri, 17 Feb 2023 07:53:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A59636B0074; Fri, 17 Feb 2023 07:53:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9233E6B0072 for ; Fri, 17 Feb 2023 07:53:21 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5F0EE4076A for ; Fri, 17 Feb 2023 12:53:21 +0000 (UTC) X-FDA: 80476774602.02.8550D29 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by imf13.hostedemail.com (Postfix) with ESMTP id 832AC2000A for ; Fri, 17 Feb 2023 12:53:19 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BVGRp9vY; spf=pass (imf13.hostedemail.com: domain of elver@google.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=elver@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=1676638399; 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=Ulgx+po/trB+4FXQcLmj6nxw3N0m6LL1KXnQpc+SYhI=; b=NFjCc58Uj0TFy8YDoXvjwyaqtUCG2LwlSpQyCW6TjZ6cwCdgDqzB1VMAc4RW8GwsJU1zE+ 1bb0qiAqXLQn3r5zxRT0EbjLgbZNSSjzi5sGK3xjmENMdNz1VwEu6cpFdmINU5hDJexubj cZ+/ehRxpNo8Ow6OqrF75kjqjgc2OwE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=BVGRp9vY; spf=pass (imf13.hostedemail.com: domain of elver@google.com designates 209.85.221.46 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676638399; a=rsa-sha256; cv=none; b=w9zvoPIVWPiL9qmXb3twR52LQ8JzQ6I92dINMRStm6Co0AxjB6QPc47PT30Dt/DqIBKJIE fgHtovCg8tBMeR7/FuwWPcC/Oct65pHkM/z8tnE0GiafZ37FGSKtp4InYX8k/NePjkeaO1 /3tdciZrxmtbgl2DzUCNl+nZpowiEkw= Received: by mail-wr1-f46.google.com with SMTP id ba10so549301wrb.11 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=1KNEv2reFzxS5y/bEjcfydKX3IhzbmIvvJhifwva9XmETE3rDIgxXpM4+foUtxory6 R/TPKd+/bTZOOzobuIqyCKELfH2pZrPXxEh4Fq88MigK113vBwVonmUAuRlPMGoBs62o QYSxzoHhUuvsEyFybJw9fzBpVspHnDhThZIbOzABMCNK89InqCAsJcUZJtQbdZZeilKE RmPn2G+c9Fw8BTSdRkA9Z+jIG+P3ROQFkTelkREUUvp1rQDGbBMnChVtY6ex63S7RP5w BLg3/RMmRBTQngGzMTfoHxPi4OhNbwvAdfdNlup7idvV8yB96iyHbKDEcx+jlgUBV6r7 omzg== X-Gm-Message-State: AO0yUKXdio6qdk5EvUiBEZjw2tgKuoasx5jpUbiILoTfWN0wxL2qY/wp pOMdPi7qC5OeCQbaT3JYhUnFsw== 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) X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 832AC2000A X-Rspam-User: X-Stat-Signature: 1iut6b4durz767hodj7rcnseh5d8qng6 X-HE-Tag: 1676638399-217169 X-HE-Meta: U2FsdGVkX1+NLMk596PZ4USRKbdbVYUVW5uNG3i8+WdO4AQQTCxnZRZcCDZgrYj/rulJ3+B3OpOxMVDDsTbl3KB+HfRLtpvTq9RoVAhchqXdOxOdLCJrMxLC4sZC/zN5Rty23Ix+IhwzxKKEkgdg5VeCpl2SlH+CG6JG5YgDFSG1EXeXRarfjA6KHLyi3Qm2VA1nNu9tmDi9kUkEC+1CicvwfJZVfF0HqWkxTrA5kD1a410GcTq92AXaFaFoQEycwPldFMFDFvc+kEIGKUT4G+lp0gGHD8JFbnNj3ZDumUmzSP8DTNAMjfpbTEAdjSgFErDKOjv9iIBtJFGxFRIJGwcT0y1HeSQVbabnrwVNHVjrNLuUS8Wmq/xf+zfwdR3Cw1Q6dCkuZsSUFh/feOtQOncVZftjH1h5K5D68i8vHFC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linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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);