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: 13151046 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 AB5CDC64ED8 for ; Fri, 24 Feb 2023 09:00:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3C306B0072; Fri, 24 Feb 2023 04:00:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEC8E6B0073; Fri, 24 Feb 2023 04:00:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB4626B0074; Fri, 24 Feb 2023 04:00:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CC0AE6B0072 for ; Fri, 24 Feb 2023 04:00:02 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 79B4016173D for ; Fri, 24 Feb 2023 09:00:02 +0000 (UTC) X-FDA: 80501588244.10.1529608 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf13.hostedemail.com (Postfix) with ESMTP id B961620012 for ; Fri, 24 Feb 2023 08:59:59 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nsiAeT3s; spf=pass (imf13.hostedemail.com: domain of 3jnz4YwUKCI8x4ExAz77z4x.v75416DG-553Etv3.7Az@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3jnz4YwUKCI8x4ExAz77z4x.v75416DG-553Etv3.7Az@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=1677229199; 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=utTJOkvRGl0nLL0ic/scm4HYcaWRO/7AK96vkAXGENA=; b=asFDm4AZEPW301rOscy93j/hfeHY+QeWPhv/zPVOhhShvSmAy0TtUbe0ZP0zE3PQr5RRaW pgiH9K+NAv7BQUwP1AmKk00JYZHPV9A2QBryJGp00dhgJnZHC7KsMDBggJE87UG9d+ruLr 8NuXLZh4i0Jfd4z9bAc7MZdjiOhI3MY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nsiAeT3s; spf=pass (imf13.hostedemail.com: domain of 3jnz4YwUKCI8x4ExAz77z4x.v75416DG-553Etv3.7Az@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3jnz4YwUKCI8x4ExAz77z4x.v75416DG-553Etv3.7Az@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677229199; a=rsa-sha256; cv=none; b=vwu7jFlxsxl+lljWn6QqZ9bFSKv/0q6TGdImK85OR0mOJOsjeD0unSbSrtZnc6frKTpvue 7JwukpF1smcny905L5aRm8cQgASTUsrsY34gwx0mYpI9TzCcDI8pU+QxHEBw1qKu2GgtcW iLffOm1VNzaboEZnGq2Zb1TLP1dIkZY= Received: by mail-ed1-f74.google.com with SMTP id ec13-20020a0564020d4d00b004a621e993a8so18498103edb.13 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=s8Ry2ohoWVmDxiqrwde4X7SkCtArGMglfVzzU/8x8MofMm/2w0cezSh4LqEqrV9yXS Lo8w7vxzH3wg3d38byZPDXZoWItDt37utDFpvHqSJjYaD74WvMVCmIEiSD1OF++f0Jg6 B18MqcacDIOqnMwtlhJfDG8keBeTF90ce2me+T3SPW0f6HXExHPRqLdIq/PuOTcaHfR3 Tqt6C1XI/OZYoG6nevKYu0FSu7oHJHvzMfBOovHlZ3eXGDMfF86RcEryEqFanytu2L1V 79vxYFYcdnOyvU/8AiokIKv6dr4VndWGjKaxQ0H4OquvlM0y8RXOSmAU5CJbty86BJgw VlxQ== X-Gm-Message-State: AO0yUKXFP+ZdmadkVl42/caFv75iwZS4PdidSnp2M3/sXM4U6AfcUvGD oD5GlNm6v5p6BpvRL7FOAX5+kVFZ4g== 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 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B961620012 X-Stat-Signature: etfa8tfouei8aw5sxx4dbp1c578zsu1a X-HE-Tag: 1677229199-340659 X-HE-Meta: U2FsdGVkX1+dKtiAeWMQEdF9zATFnBSB21ZbCPuEpdzIgb79/sWBWthJiYu5BtfMT4zyQFG54hj15BUcVw2FhgliSp3PoDCKZVwN9c+hV1vdvARcio6ENaz9tVgZWmod4Il+5QRF17iIfgn0roh2QvO6+svtnmZoNAq9vY19oNkJqppPQqpxxKesbLPoq6GKRYMgCkhdiRx8jq2ARurDBbTvQEqy0PmZlAtQDuFUr+HRiCxCTt5F/DJINVSaNV834f8LlGF42HChamZ4fbsp1Sjio1tp2+XXw+g+/2Zo9AsjKgad7B8V2oZU4nPulTl4gADbkxG7vkypkoj9eJ7qN+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yJdlRJbrIispZzoUXB1shHvziOGB8VtK1iYdQlKZFl8Veqr/cXbFeQtnpg2AYKS9QidS+xy+UvNXlQr9LS4d66+ldZ3xHg6xY1sIUoo8alyKC8JkZH1mDhLzdWvOr0Kgrr0Zfw6E3U11B0AdG8q1mdE6wEaQUCSjjdsQMyiPYaNtMOtT6JdFE7ANzySa5chnGEI9R4g6XIved5tFr15ophNu1OQwqkjswc/6+pO49BPqPj7WT6abGGPGVWYUZ00cAWA+2EiqT5m4e9IjA3bC9HaD5vNqMxVjeqe+Syf2KoIguKLCFAToQ7ouKlGp2CfaY3ykwOr68khkprUJfaK3TKZzW5bgEUUoV2paV76FTTMoOInl/+cg1BHXdywnfjnv3KDDsXsvH9dFBj3Koj6494iC2Eh5kIUs0LqflDZlcmupWXI0tjQLT6 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