From patchwork Fri Jan 13 12:31:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 43229 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp242986wrt; Fri, 13 Jan 2023 04:34:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXukiHY6v06btdZRFhyjwpBUUGpVEdX9dUMCg0lA/faA2zr29A3itooNums1MSd6H9ylWhsI X-Received: by 2002:a50:ff02:0:b0:491:6897:c5cb with SMTP id a2-20020a50ff02000000b004916897c5cbmr27089486edu.41.1673613293426; Fri, 13 Jan 2023 04:34:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673613293; cv=none; d=google.com; s=arc-20160816; b=idvzaUmVjZcCiTHHQHUNpsQI7dGIpSoN+iwHyKSXxKOpUBriPbnr22gr7WgWmkAL99 oImcHYuOPBS/31Exl7W0+OXr6UJ6EK2/7n8qDpwrqR9bgoYw743dLvzo6OgWcEuOwNev yqwPhq8m1wDeRrMSWqzKyga1qcX2YF1reWB+krWfuhilcI8S26fX3+rTlaKUe6XJSzvW fzIaG64YDbm9Gz80daXkM46J0WntzmFFEm62vd5O9dVgcISuXvrNDv5R53mSLqTVcy1U vayaOQFrUVi2F0JwTOGF08h3NJ7NGI3wdqRywvo6l5GMDs/L4/P7jRBws+bF6wuqJDSM NenQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=wDbKxqGYeN5BCJlyVJxQ3gTA0fCKqletaO/hYXAUUnc=; b=huQcj7AP01ZOk9mcRy8SUGOoznLtUlMkafIYsWsUDkq/5gWXopTg8/e9OzCO5itngi wKs3/ylGUx+zfRbo4tRWrcjT/a1u49ASxWuIuvXthXHy/++5FsR4DwqQYfXiJ4tawecP f9nB/k0jaLtSDlOE1VLCNDfNibB/t1QVpfbkQ2ppkeQOE77xMFGa8l0uDCakYzbACxEr OXEChLjW6lY7zVa+CVG0CEVDAxCGU8eQyoaSS1GnR9KzSAMGxuFcVhLi6LK2NuhfcFJ6 iKPUTgJVojnfnvhkJUF60fFe69G0c7jvN1atHtJJf/GkInOlVxol5knHczvmIrtxHdKc R4mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="vlO/ICev"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=vs9samWp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b13-20020a056402350d00b004700047ef96si23994876edd.289.2023.01.13.04.34.27; Fri, 13 Jan 2023 04:34:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="vlO/ICev"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=vs9samWp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241593AbjAMMdm (ORCPT + 99 others); Fri, 13 Jan 2023 07:33:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239679AbjAMMcJ (ORCPT ); Fri, 13 Jan 2023 07:32:09 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 216324FCD5; Fri, 13 Jan 2023 04:31:10 -0800 (PST) Date: Fri, 13 Jan 2023 12:31:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673613066; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wDbKxqGYeN5BCJlyVJxQ3gTA0fCKqletaO/hYXAUUnc=; b=vlO/ICev8KZN9TQl3yzW+cLGUJp6hWkhzFsvaQKQ67Ul6emOvti3t5IRszCEc23vlNWV1t kuUDyzaPQfCDf7ydIY7Q1kCcALg4MUTHHK8GeUwK7rysULSrVUMXAklzt0JAvF8weh/YG4 Rk2wrsZAHDOPIBxn8MtzJpq7N/YAk5Q3h0HqCPJrf/CFvAkvnOo/lvtd1k4XVBqdVyXfb4 5nLMJ8ZRTC5wN3v2gEk1gNyZqUr092bB9jlU/yQ16h+hCUh5fpckB412XmJOinfZO6LkOv cNSAqJbk2c66aaghjAbAD3qNEPXk/I9iY68bnIp1+grl0TOAELxMGQy63EiIeA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673613066; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wDbKxqGYeN5BCJlyVJxQ3gTA0fCKqletaO/hYXAUUnc=; b=vs9samWpAq+oNZD3JEF5YPfkZiLzQC92ChC09IxWdgbv3sJU6tNvWZBJ5sgfRlZbHHTQKU hZFlhv22R2fnG4BQ== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] entry, kasan, x86: Disallow overriding mem*() functions Cc: "Peter Zijlstra (Intel)" , Ingo Molnar , Tony Lindgren , Ulf Hansson , "Rafael J. Wysocki" , Frederic Weisbecker , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230112195542.028523143@infradead.org> References: <20230112195542.028523143@infradead.org> MIME-Version: 1.0 Message-ID: <167361306621.4906.6454025554442390032.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754848478274063887?= X-GMAIL-MSGID: =?utf-8?q?1754910732889123234?= The following commit has been merged into the sched/core branch of tip: Commit-ID: 69d4c0d3218692ffa56b0e1b9c76c50c699d7044 Gitweb: https://git.kernel.org/tip/69d4c0d3218692ffa56b0e1b9c76c50c699d7044 Author: Peter Zijlstra AuthorDate: Thu, 12 Jan 2023 20:43:58 +01:00 Committer: Ingo Molnar CommitterDate: Fri, 13 Jan 2023 11:48:17 +01:00 entry, kasan, x86: Disallow overriding mem*() functions KASAN cannot just hijack the mem*() functions, it needs to emit __asan_mem*() variants if it wants instrumentation (other sanitizers already do this). vmlinux.o: warning: objtool: sync_regs+0x24: call to memcpy() leaves .noinstr.text section vmlinux.o: warning: objtool: vc_switch_off_ist+0xbe: call to memcpy() leaves .noinstr.text section vmlinux.o: warning: objtool: fixup_bad_iret+0x36: call to memset() leaves .noinstr.text section vmlinux.o: warning: objtool: __sev_get_ghcb+0xa0: call to memcpy() leaves .noinstr.text section vmlinux.o: warning: objtool: __sev_put_ghcb+0x35: call to memcpy() leaves .noinstr.text section Remove the weak aliases to ensure nobody hijacks these functions and add them to the noinstr section. Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Tested-by: Tony Lindgren Tested-by: Ulf Hansson Acked-by: Rafael J. Wysocki Acked-by: Frederic Weisbecker Link: https://lore.kernel.org/r/20230112195542.028523143@infradead.org --- arch/x86/lib/memcpy_64.S | 5 ++--- arch/x86/lib/memmove_64.S | 4 +++- arch/x86/lib/memset_64.S | 4 +++- mm/kasan/kasan.h | 4 ++++- mm/kasan/shadow.c | 38 ++++++++++++++++++++++++++++++++++++++- tools/objtool/check.c | 3 +++- 6 files changed, 53 insertions(+), 5 deletions(-) diff --git a/arch/x86/lib/memcpy_64.S b/arch/x86/lib/memcpy_64.S index dd8cd88..a640176 100644 --- a/arch/x86/lib/memcpy_64.S +++ b/arch/x86/lib/memcpy_64.S @@ -8,7 +8,7 @@ #include #include -.pushsection .noinstr.text, "ax" +.section .noinstr.text, "ax" /* * We build a jump to memcpy_orig by default which gets NOPped out on @@ -43,7 +43,7 @@ SYM_TYPED_FUNC_START(__memcpy) SYM_FUNC_END(__memcpy) EXPORT_SYMBOL(__memcpy) -SYM_FUNC_ALIAS_WEAK(memcpy, __memcpy) +SYM_FUNC_ALIAS(memcpy, __memcpy) EXPORT_SYMBOL(memcpy) /* @@ -184,4 +184,3 @@ SYM_FUNC_START_LOCAL(memcpy_orig) RET SYM_FUNC_END(memcpy_orig) -.popsection diff --git a/arch/x86/lib/memmove_64.S b/arch/x86/lib/memmove_64.S index 724bbf8..0266186 100644 --- a/arch/x86/lib/memmove_64.S +++ b/arch/x86/lib/memmove_64.S @@ -13,6 +13,8 @@ #undef memmove +.section .noinstr.text, "ax" + /* * Implement memmove(). This can handle overlap between src and dst. * @@ -213,5 +215,5 @@ SYM_FUNC_START(__memmove) SYM_FUNC_END(__memmove) EXPORT_SYMBOL(__memmove) -SYM_FUNC_ALIAS_WEAK(memmove, __memmove) +SYM_FUNC_ALIAS(memmove, __memmove) EXPORT_SYMBOL(memmove) diff --git a/arch/x86/lib/memset_64.S b/arch/x86/lib/memset_64.S index fc9ffd3..6143b1a 100644 --- a/arch/x86/lib/memset_64.S +++ b/arch/x86/lib/memset_64.S @@ -6,6 +6,8 @@ #include #include +.section .noinstr.text, "ax" + /* * ISO C memset - set a memory block to a byte value. This function uses fast * string to get better performance than the original function. The code is @@ -43,7 +45,7 @@ SYM_FUNC_START(__memset) SYM_FUNC_END(__memset) EXPORT_SYMBOL(__memset) -SYM_FUNC_ALIAS_WEAK(memset, __memset) +SYM_FUNC_ALIAS(memset, __memset) EXPORT_SYMBOL(memset) /* diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index ea8cf13..71c1543 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -618,6 +618,10 @@ void __asan_set_shadow_f3(const void *addr, size_t size); void __asan_set_shadow_f5(const void *addr, size_t size); void __asan_set_shadow_f8(const void *addr, size_t size); +void *__asan_memset(void *addr, int c, size_t len); +void *__asan_memmove(void *dest, const void *src, size_t len); +void *__asan_memcpy(void *dest, const void *src, size_t len); + void __hwasan_load1_noabort(unsigned long addr); void __hwasan_store1_noabort(unsigned long addr); void __hwasan_load2_noabort(unsigned long addr); diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c index 2fba1f5..9826993 100644 --- a/mm/kasan/shadow.c +++ b/mm/kasan/shadow.c @@ -38,6 +38,12 @@ bool __kasan_check_write(const volatile void *p, unsigned int size) } EXPORT_SYMBOL(__kasan_check_write); +#ifndef 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. + */ #undef memset void *memset(void *addr, int c, size_t len) { @@ -68,6 +74,38 @@ void *memcpy(void *dest, const void *src, size_t len) return __memcpy(dest, src, len); } +#endif + +void *__asan_memset(void *addr, int c, size_t len) +{ + if (!kasan_check_range((unsigned long)addr, len, true, _RET_IP_)) + return NULL; + + return __memset(addr, c, len); +} +EXPORT_SYMBOL(__asan_memset); + +#ifdef __HAVE_ARCH_MEMMOVE +void *__asan_memmove(void *dest, const void *src, size_t len) +{ + if (!kasan_check_range((unsigned long)src, len, false, _RET_IP_) || + !kasan_check_range((unsigned long)dest, len, true, _RET_IP_)) + return NULL; + + return __memmove(dest, src, len); +} +EXPORT_SYMBOL(__asan_memmove); +#endif + +void *__asan_memcpy(void *dest, const void *src, size_t len) +{ + if (!kasan_check_range((unsigned long)src, len, false, _RET_IP_) || + !kasan_check_range((unsigned long)dest, len, true, _RET_IP_)) + return NULL; + + return __memcpy(dest, src, len); +} +EXPORT_SYMBOL(__asan_memcpy); void kasan_poison(const void *addr, size_t size, u8 value, bool init) { diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 9767bab..92554c5 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1074,6 +1074,9 @@ static const char *uaccess_safe_builtin[] = { "__asan_store16_noabort", "__kasan_check_read", "__kasan_check_write", + "__asan_memset", + "__asan_memmove", + "__asan_memcpy", /* KASAN in-line */ "__asan_report_load_n_noabort", "__asan_report_load1_noabort",