From patchwork Wed Jul 9 11:29:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Ryabinin X-Patchwork-Id: 4513761 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A65FBBEECB for ; Wed, 9 Jul 2014 11:38:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B92D020395 for ; Wed, 9 Jul 2014 11:38:58 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CE2D320379 for ; Wed, 9 Jul 2014 11:38:57 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1X4qBN-0000D8-2G; Wed, 09 Jul 2014 11:37:05 +0000 Received: from mailout4.w1.samsung.com ([210.118.77.14]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1X4qB9-0008Ov-6u for linux-arm-kernel@lists.infradead.org; Wed, 09 Jul 2014 11:36:51 +0000 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N8G0034Z08GSY60@mailout4.w1.samsung.com> for linux-arm-kernel@lists.infradead.org; Wed, 09 Jul 2014 12:36:16 +0100 (BST) X-AuditID: cbfec7f5-b7f626d000004b39-68-53bd293822e2 Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 41.A4.19257.8392DB35; Wed, 09 Jul 2014 12:36:24 +0100 (BST) Received: from localhost.localdomain ([106.109.129.143]) by eusync1.samsung.com (Oracle Communications Messaging Server 7u4-23.01 (7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0N8G0029508FLQ20@eusync1.samsung.com>; Wed, 09 Jul 2014 12:36:24 +0100 (BST) From: Andrey Ryabinin To: linux-kernel@vger.kernel.org Subject: [RFC/PATCH RESEND -next 03/21] x86: add kasan hooks fort memcpy/memmove/memset functions Date: Wed, 09 Jul 2014 15:29:57 +0400 Message-id: <1404905415-9046-4-git-send-email-a.ryabinin@samsung.com> X-Mailer: git-send-email 1.8.5.5 In-reply-to: <1404905415-9046-1-git-send-email-a.ryabinin@samsung.com> References: <1404905415-9046-1-git-send-email-a.ryabinin@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrALMWRmVeSWpSXmKPExsVy+t/xy7oWmnuDDa4+M7DY9usRm8XvvTNZ LeasX8Nmcf3bG0aLCQ/b2C1WdjezWWx/9pbJYmXnA1aLTY+vsVr82bWDyeLyrjlsFvfW/Ge1 uH2Z1+LSgQVMFi37LjBZtH3+x2qxb+V5IGvJRiaLxUduM1u8ezaZ2WLzpqnMFj82PGZ1EPNo ae5h89g56y67x4JNpR6bVnWyeWz6NIndo+vtFSaPd+fOsXucmPGbxePJlelMHpuX1Ht8fHqL xeP9vqtsHn1bVjF6nFlwhN3j8ya5AP4oLpuU1JzMstQifbsEroy5W/8zFeyTr7i9diV7A+Mc qS5GTg4JAROJQ+/3s0PYYhIX7q1n62Lk4hASWMooMbXpOzuE08cksfDpTUaQKjYBPYl/s7az gdgiAgoSm3ufsYIUMQs0s0m0d3xgBUkICyRL7DrcxwRiswioSkxevwjM5hVwlVh88wLUOgWJ ZctngtVzCrhJTJh+jbmLkQNom6vEhBVqExh5FzAyrGIUTS1NLihOSs810itOzC0uzUvXS87P 3cQIiaWvOxiXHrM6xCjAwajEw/ti955gIdbEsuLK3EOMEhzMSiK8tqJ7g4V4UxIrq1KL8uOL SnNSiw8xMnFwSjUwWj79cyfyeOAyDadnWluefnQs+ppecUjy4u/Njz6G9Gf9lBTVVoj3PCHg WcxtUOmhJVih+uDNja3LnvonR9q9vZucfnH1j92dsp/3Hu/s1uoWmdxq/bfu5DMzk+yyfd/X HElhuKAz+e5z+WvbIo2drm5zFPV4NDFkvlGZmZ1Bh+LVz+L2R9ctVGIpzkg01GIuKk4EAJp5 nJqDAgAA X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140709_043651_388279_C7078610 X-CRM114-Status: GOOD ( 10.86 ) X-Spam-Score: -5.7 (-----) Cc: Michal Marek , Christoph Lameter , x86@kernel.org, Russell King , Andrew Morton , linux-kbuild@vger.kernel.org, Andrey Ryabinin , Joonsoo Kim , David Rientjes , linux-mm@kvack.org, Pekka Enberg , Konstantin Serebryany , Yuri Gribov , Dmitry Vyukov , Sasha Levin , Andrey Konovalov , Thomas Gleixner , Alexey Preobrazhensky , Ingo Molnar , Konstantin Khlebnikov , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since functions memset, memmove, memcpy are written in assembly, compiler can't instrument memory accesses inside them. This patch replaces these functions with our own instrumented functions (kasan_mem*) for CONFIG_KASAN = y In rare circumstances you may need to use the original functions, in such case put #undef KASAN_HOOKS before includes. Signed-off-by: Andrey Ryabinin --- arch/x86/include/asm/string_32.h | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/string_64.h | 24 ++++++++++++++++++++++++ arch/x86/lib/Makefile | 2 ++ 3 files changed, 54 insertions(+) diff --git a/arch/x86/include/asm/string_32.h b/arch/x86/include/asm/string_32.h index 3d3e835..a86615a 100644 --- a/arch/x86/include/asm/string_32.h +++ b/arch/x86/include/asm/string_32.h @@ -321,6 +321,32 @@ void *__constant_c_and_count_memset(void *s, unsigned long pattern, : __memset_generic((s), (c), (count))) #define __HAVE_ARCH_MEMSET + +#if defined(CONFIG_KASAN) && defined(KASAN_HOOKS) + +/* + * Since some of the following functions (memset, memmove, memcpy) + * are written in assembly, compiler can't instrument memory accesses + * inside them. + * + * To solve this issue we replace these functions with our own instrumented + * functions (kasan_mem*) + * + * In rare circumstances you may need to use the original functions, + * in such case put #undef KASAN_HOOKS before includes. + */ + +#undef memcpy +void *kasan_memset(void *ptr, int val, size_t len); +void *kasan_memcpy(void *dst, const void *src, size_t len); +void *kasan_memmove(void *dst, const void *src, size_t len); + +#define memcpy(dst, src, len) kasan_memcpy((dst), (src), (len)) +#define memset(ptr, val, len) kasan_memset((ptr), (val), (len)) +#define memmove(dst, src, len) kasan_memmove((dst), (src), (len)) + +#else /* CONFIG_KASAN && KASAN_HOOKS */ + #if (__GNUC__ >= 4) #define memset(s, c, count) __builtin_memset(s, c, count) #else @@ -331,6 +357,8 @@ void *__constant_c_and_count_memset(void *s, unsigned long pattern, : __memset((s), (c), (count))) #endif +#endif /* CONFIG_KASAN && KASAN_HOOKS */ + /* * find the first occurrence of byte 'c', or 1 past the area if none */ diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h index 19e2c46..2af2dbe 100644 --- a/arch/x86/include/asm/string_64.h +++ b/arch/x86/include/asm/string_64.h @@ -63,6 +63,30 @@ char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); int strcmp(const char *cs, const char *ct); +#if defined(CONFIG_KASAN) && defined(KASAN_HOOKS) + +/* + * Since some of the following functions (memset, memmove, memcpy) + * are written in assembly, compiler can't instrument memory accesses + * inside them. + * + * To solve this issue we replace these functions with our own instrumented + * functions (kasan_mem*) + * + * In rare circumstances you may need to use the original functions, + * in such case put #undef KASAN_HOOKS before includes. + */ + +void *kasan_memset(void *ptr, int val, size_t len); +void *kasan_memcpy(void *dst, const void *src, size_t len); +void *kasan_memmove(void *dst, const void *src, size_t len); + +#define memcpy(dst, src, len) kasan_memcpy((dst), (src), (len)) +#define memset(ptr, val, len) kasan_memset((ptr), (val), (len)) +#define memmove(dst, src, len) kasan_memmove((dst), (src), (len)) + +#endif /* CONFIG_KASAN && KASAN_HOOKS */ + #endif /* __KERNEL__ */ #endif /* _ASM_X86_STRING_64_H */ diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile index 4d4f96a..d82bc35 100644 --- a/arch/x86/lib/Makefile +++ b/arch/x86/lib/Makefile @@ -2,6 +2,8 @@ # Makefile for x86 specific library files. # +KASAN_SANITIZE_memcpy_32.o := n + inat_tables_script = $(srctree)/arch/x86/tools/gen-insn-attr-x86.awk inat_tables_maps = $(srctree)/arch/x86/lib/x86-opcode-map.txt quiet_cmd_inat_tables = GEN $@