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