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