From patchwork Fri May 17 13:01:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13666981 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 320BBC04FFE for ; Fri, 17 May 2024 13:01:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A958C6B0089; Fri, 17 May 2024 09:01:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A1EEE6B008A; Fri, 17 May 2024 09:01:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C05F6B008C; Fri, 17 May 2024 09:01:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6E9A06B0089 for ; Fri, 17 May 2024 09:01:32 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E1C4B4024A for ; Fri, 17 May 2024 13:01:31 +0000 (UTC) X-FDA: 82127899182.26.368EC70 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf18.hostedemail.com (Postfix) with ESMTP id C0B401C0027 for ; Fri, 17 May 2024 13:01:27 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=twrr19Ft; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715950888; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=tyN60hwyyyZujQaT4M6CmN3OZiKg7vscoBXPblyi5YU=; b=F5vlobshsAZSq9W4UwgSBx8sCPCkv4coaVCafeVe5sYZEzCoBOAtlA/skRV+OeDl/hM6oq 5pbtp8iNEfTq1xQExd1RkKLBfRWXuX2x68aSikyLaxXLVzWUkrlqxXicbrqs0qSONJ6WbJ MjYAVlgaH0eHc6Q084kuNreGC5s1Jf8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715950888; a=rsa-sha256; cv=none; b=0FI9DcJRXGxDRlfF+gS3WD10Lo2nLW3xa6Hz1TJR7q+bXA37QIetFUE3TAcJjfsgiMK8M0 Xj9XKVIBdicnk+ap1G3AXN5qLwbldZVv/7slTgVAyl//aT3Qa2jSmkaHfAgwoFr+hdE3Qu bLM+R93Y4XdWSKZDhW3jzbvrpQzhVlo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=twrr19Ft; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: elver@google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715950885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=tyN60hwyyyZujQaT4M6CmN3OZiKg7vscoBXPblyi5YU=; b=twrr19FtZFJhnMK2v+2f28RrN946+7BrzNyj1S55GO3spUzE3hAx5r6VPCxP6XmssMJaKN Nk5CmJu1U7SA464VU/C/Q+JGY53nj/ySWvo5jyO5ydRoS5bscXhBoKXs5eX0yVPgshmw5z 4SVvRcTTxUrv4QyUihstdKk1NTcXDgk= X-Envelope-To: andreyknvl@gmail.com X-Envelope-To: glider@google.com X-Envelope-To: dvyukov@google.com X-Envelope-To: ryabinin.a.a@gmail.com X-Envelope-To: kasan-dev@googlegroups.com X-Envelope-To: akpm@linux-foundation.org X-Envelope-To: linux-mm@kvack.org X-Envelope-To: erhard_f@mailbox.org X-Envelope-To: npache@redhat.com X-Envelope-To: dja@axtens.net X-Envelope-To: linux-kernel@vger.kernel.org X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: andrey.konovalov@linux.dev To: Marco Elver Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Andrew Morton , linux-mm@kvack.org, Erhard Furtner , Nico Pache , Daniel Axtens , linux-kernel@vger.kernel.org Subject: [PATCH] kasan, fortify: properly rename memintrinsics Date: Fri, 17 May 2024 15:01:18 +0200 Message-Id: <20240517130118.759301-1-andrey.konovalov@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C0B401C0027 X-Rspam-User: X-Stat-Signature: aw6jcjtyqb7dw54j63649dwbjgsn3h8p X-HE-Tag: 1715950887-459827 X-HE-Meta: U2FsdGVkX18c61hqQt9IvTWxtYuVC06g4pYf8LHoo9pUzyhnARkqKSGaeFrCdUdbsMnQkeEU/oafnuVNfPLscAs7YoaPJNqAndRw4mjOZzwXOEnp/2QxOH4E1Jd2VwsMQrB2ybOwwPsTTfbiKxEYtv05WnmsotTDnTXS8cytoEIMYsNs1x390EZubyVnCrTS3MG1ICKfuRTrdEQjy3XI999VfK/uRhK4TdhGKL6hIA5LWu19ic5hZjSmRAdUXGluOijimtysc3wTEOdoXvj1QtuEHXtt0uDKAH39b/IXtbDfKf7uavlxBfv+xqT+HORL7vufb1QOESfjxbQfVYKUwOyThWLOqPgIWs+Rrxrelk2JtvSxVIEG8Dfjscs0wMiZ8zJQyqLPFBsOoBkbg8gdrPThojlu4T8kG9+YXYwoNd0D/TZRv3h0f+NGCJKrFd3ECEI1PwyTKYhAadAs0ETEyVydAINIqcbniqECpAXGgZmd4KCcWpnYEkSSGoLhMRC282LKxl4wMuqANdHXJKyKS0P3f7nFYiVx2M5cMMIzN4+z/eiJjKgdDClVJtsA7yJ/l6Yfife+o5gF9jv9FZhkDcfbHrC+gAkfLdJHoggO9GhqCO76BKSVz/+0oZOzfE1aKiVxtSeq+dft19O46O4chKao3VcE0wYB6k8MYo73w7kzGj79GVSjYekrc3Dgi2rUHGldh43i6LGiiL5vCpd1Wk+98HOco8/0ygs9cbje2APZTf2Q9AT1qXiBJ9CY37f27JwxKCgXABhjxQ+NloMlLFQZnkgn5T/lJtVppqPlfknZulNsG3SGYMFRfhNNyJAZnpM5xeZUb/UQKof1PZqomB7++LeB5MCIitRoNbyGlt+K2wSaQ1cpHbwpU/aCpmgOuDnrIeHF/14afK3qanRX7FNtdsRi03hDbostDyhmT8u4GVm0vjlpSgYXFmHy01pqO9i/TF7h2iizWSLZg1y kGkmuTgq f7rkYGYUNpTZXhgCb2/7Q/oqZe8fZTztYbVYiTIBlSOAaj88FLYJ7WDBgbFvWW5k0KyklJizSxIZpbRxf4HRH2zHg4RUur4M9ALr5Y+OvRQSOz4kWCokdgrPI+dnMidX8p48ffo0iT5FF/GwKpymWhCna0LaDxQdrmzCeyR1BL1tuclFvOgawREe9NQfdhSn2SSIo4eOm8BjU5rR08hmIbjwUExTHkYPCc7CgWsYw5snofkNhxy5QuPMFPOHQjvo1gkmqDWXQoTdPbkhk1PgQF5WCHZnwi53wzCGv643puY9XSDmbW7ROGl+Aww== 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: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov After commit 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") and the follow-up fixes, with CONFIG_FORTIFY_SOURCE enabled, even though the compiler instruments meminstrinsics by generating calls to __asan/__hwasan_ prefixed functions, FORTIFY_SOURCE still uses uninstrumented memset/memmove/memcpy as the underlying functions. As a result, KASAN cannot detect bad accesses in memset/memmove/memcpy. This also makes KASAN tests corrupt kernel memory and cause crashes. To fix this, use __asan_/__hwasan_memset/memmove/memcpy as the underlying functions whenever appropriate. Do this only for the instrumented code (as indicated by __SANITIZE_ADDRESS__). Reported-by: Erhard Furtner Reported-by: Nico Pache Closes: https://lore.kernel.org/all/20240501144156.17e65021@outsider.home/ Fixes: 69d4c0d32186 ("entry, kasan, x86: Disallow overriding mem*() functions") Fixes: 51287dcb00cc ("kasan: emit different calls for instrumentable memintrinsics") Fixes: 36be5cba99f6 ("kasan: treat meminstrinsic as builtins in uninstrumented files") Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver Tested-by: Nico Pache Acked-by: Nico Pache --- include/linux/fortify-string.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index 85fc0e6f0f7f..bac010cfc42f 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -75,17 +75,30 @@ void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning(" __ret; \ }) -#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) +#if defined(__SANITIZE_ADDRESS__) + +#if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY) +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); +#elif defined(CONFIG_KASAN_GENERIC) +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__asan_memset); +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memmove); +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__asan_memcpy); +#else /* CONFIG_KASAN_SW_TAGS */ +extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(__hwasan_memset); +extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memmove); +extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(__hwasan_memcpy); +#endif + extern void *__underlying_memchr(const void *p, int c, __kernel_size_t size) __RENAME(memchr); extern int __underlying_memcmp(const void *p, const void *q, __kernel_size_t size) __RENAME(memcmp); -extern void *__underlying_memcpy(void *p, const void *q, __kernel_size_t size) __RENAME(memcpy); -extern void *__underlying_memmove(void *p, const void *q, __kernel_size_t size) __RENAME(memmove); -extern void *__underlying_memset(void *p, int c, __kernel_size_t size) __RENAME(memset); extern char *__underlying_strcat(char *p, const char *q) __RENAME(strcat); extern char *__underlying_strcpy(char *p, const char *q) __RENAME(strcpy); extern __kernel_size_t __underlying_strlen(const char *p) __RENAME(strlen); extern char *__underlying_strncat(char *p, const char *q, __kernel_size_t count) __RENAME(strncat); extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) __RENAME(strncpy); + #else #if defined(__SANITIZE_MEMORY__) @@ -110,6 +123,7 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) #define __underlying_strlen __builtin_strlen #define __underlying_strncat __builtin_strncat #define __underlying_strncpy __builtin_strncpy + #endif /**