From patchwork Thu Aug 23 08:56:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyeongdon Kim X-Patchwork-Id: 10573579 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2E726112E for ; Thu, 23 Aug 2018 08:56:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CF852BA17 for ; Thu, 23 Aug 2018 08:56:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 100352BA1E; Thu, 23 Aug 2018 08:56:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DBD362BA17 for ; Thu, 23 Aug 2018 08:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 016B76B292E; Thu, 23 Aug 2018 04:56:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EDEDF6B2930; Thu, 23 Aug 2018 04:56:55 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D58986B2931; Thu, 23 Aug 2018 04:56:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id 856CE6B292E for ; Thu, 23 Aug 2018 04:56:55 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id 33-v6so2241707plf.19 for ; Thu, 23 Aug 2018 01:56:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id; bh=DeiEUqLWYnFkqMhzh8HlGq6JINTBrLGyGq7qPiAdre4=; b=CeAtBCYj5fgoMm17udfDdz1Nuu+Ceg/zfgDK2j09PBJ1qvMoeR2LPTyNfQL95s8N// v91Zfy2oLcauJtkzwVhypb7EkLD1zeq6igjQ+BUmuohIPEV5Uts1Z59jLfEu1O3cFAbR 9acChEpSkm/vmSL5qqVFvHGq1m1muHmrgJmBtF7NgGT4dFrZ4CehxTVcSw7axq5Sz5UW rPzWMMyvCyvVKqQro0hDafa7EbPd/kXI6cra6v503fhdSEwT8FuqyN3soF3kepH4hJyT 1aXxDQPkeIswVNiVCo7J6a7jttLP4miObUdh3173Rnu5R7PjoVC+hLX+VNwn1fd42u5B 4Aag== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of kyeongdon.kim@lge.com designates 156.147.23.53 as permitted sender) smtp.mailfrom=kyeongdon.kim@lge.com X-Gm-Message-State: AOUpUlFZudOiA6InU1wZwXVOQVQ3i5OZ3yiJR7VfsUDSQCEx15ojh4IR jqHlT1FPzTtlSFi5cGRxh0XawfdSCRh5Jol/9yl6d5z2+wrzurS0hCBkjcq4vum4C6FZQHCcpov FqsOAffITtV/HpyravU+7yy5YoYTf57vCKo/cEj+ekNu4Z5rwhG+hrYcb3yfghoy6FQ== X-Received: by 2002:a17:902:654b:: with SMTP id d11-v6mr57272066pln.8.1535014615142; Thu, 23 Aug 2018 01:56:55 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzy4/ZmOLGYaP7t1KMUFreY6q/r3P56TQX7m60XHKfPwsApp6rcW10NsKnxJULIZuIS0cdf X-Received: by 2002:a17:902:654b:: with SMTP id d11-v6mr57272024pln.8.1535014614040; Thu, 23 Aug 2018 01:56:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535014614; cv=none; d=google.com; s=arc-20160816; b=hVUyqe1VUOlcIXVh2osyNQ/8PEhnYX3wKdkFKGWfBlCQU3SgZJAAVyV7XhrVEo8gHH 2MxP2iDcBGXUh2yTmCjjBIUsXIMJrQIQUtDvmTo2K14MeJfK4zzpBVi9ZjhW4G8fu3OI 04nb525K1EPYj37sHvz6fmbVnhMUNS25Ebnhde7G0bDxW5PYVph9NWF6uq3nIlAif3Fs f0WKMcbZBo5QRkX8dXagWBpjZMmZVpcGM0Gy3ePxCPEVdVzwTOlYRo32ZZ5C3pGGNCw7 rslEjP1OYjtXzdO8QZQOxLBWHXld/AApBC+eLugmQYhVEvk/5YFb4rND/EYQlLBpaL+d aWJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=DeiEUqLWYnFkqMhzh8HlGq6JINTBrLGyGq7qPiAdre4=; b=Si7LqK4yi4J9tkXwY+9fJ31BTokxmhN4A9kTrMAmYRPh/elfR49xzh1aid5BJ1hjen RPPlRWMpLXuFSRcRV1w5bY8cVLq0/QbCywgZ2RCyFHHKCNWcyB+HiJM/3JkAR6VFvCZR FvD/dsJkWzqIuLvWyFUh/NlLvDr2hdDF1SIh44Ovm2poS6Ah2lZ7bjIiK7gA7rS0o/6X OWgum9gFyuCLDmnF2WQVqH3yEccdDhb9eEheqP8yAE1BhX/p5OgbllHmJ6LeFVRmg0u0 raUsjTsStWqgDKSm60xcwlDAxcYGje6NvI0fNyJYShqbBc7Wa9Mlxx5UgSriAn4aaPMt F/UQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of kyeongdon.kim@lge.com designates 156.147.23.53 as permitted sender) smtp.mailfrom=kyeongdon.kim@lge.com Received: from lgeamrelo11.lge.com (lgeamrelo13.lge.com. [156.147.23.53]) by mx.google.com with ESMTP id p65-v6si3767815pga.401.2018.08.23.01.56.53 for ; Thu, 23 Aug 2018 01:56:54 -0700 (PDT) Received-SPF: pass (google.com: domain of kyeongdon.kim@lge.com designates 156.147.23.53 as permitted sender) client-ip=156.147.23.53; Authentication-Results: mx.google.com; spf=pass (google.com: domain of kyeongdon.kim@lge.com designates 156.147.23.53 as permitted sender) smtp.mailfrom=kyeongdon.kim@lge.com Received: from unknown (HELO lgeamrelo02.lge.com) (156.147.1.126) by 156.147.23.53 with ESMTP; 23 Aug 2018 17:56:51 +0900 X-Original-SENDERIP: 156.147.1.126 X-Original-MAILFROM: kyeongdon.kim@lge.com Received: from unknown (HELO localhost.localdomain) (10.168.176.142) by 156.147.1.126 with ESMTP; 23 Aug 2018 17:56:51 +0900 X-Original-SENDERIP: 10.168.176.142 X-Original-MAILFROM: kyeongdon.kim@lge.com From: Kyeongdon Kim To: aryabinin@virtuozzo.com, catalin.marinas@arm.com, will.deacon@arm.com, glider@google.com, dvyukov@google.com Cc: Jason@zx2c4.com, robh@kernel.org, ard.biesheuvel@linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, kyeongdon.kim@lge.com Subject: [PATCH v2] arm64: kasan: add interceptors for strcmp/strncmp functions Date: Thu, 23 Aug 2018 17:56:46 +0900 Message-Id: <1535014606-176525-1-git-send-email-kyeongdon.kim@lge.com> X-Mailer: git-send-email 2.6.2 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: X-Virus-Scanned: ClamAV using ClamSMTP This patch declares strcmp/strncmp as weak symbols. (2 of them are the most used string operations) Original functions declared as weak and strong ones in mm/kasan/kasan.c could replace them. Assembly optimized strcmp/strncmp functions cannot detect KASan bug. But, now we can detect them like the call trace below. ================================================================== BUG: KASAN: use-after-free in platform_match+0x1c/0x5c at addr ffffffc0ad313500 Read of size 1 by task swapper/0/1 CPU: 3 PID: 1 Comm: swapper/0 Tainted: G B 4.9.77+ #1 Hardware name: Generic (DT) based system Call trace: dump_backtrace+0x0/0x2e0 show_stack+0x14/0x1c dump_stack+0x88/0xb0 kasan_object_err+0x24/0x7c kasan_report+0x2f0/0x484 check_memory_region+0x20/0x14c strcmp+0x1c/0x5c platform_match+0x40/0xe4 __driver_attach+0x40/0x130 bus_for_each_dev+0xc4/0xe0 driver_attach+0x30/0x3c bus_add_driver+0x2dc/0x328 driver_register+0x118/0x160 __platform_driver_register+0x7c/0x88 alarmtimer_init+0x154/0x1e4 do_one_initcall+0x184/0x1a4 kernel_init_freeable+0x2ec/0x2f0 kernel_init+0x18/0x10c ret_from_fork+0x10/0x50 In case of xtensa and x86_64 kasan, no need to use this patch now. Signed-off-by: Kyeongdon Kim --- arch/arm64/include/asm/string.h | 5 +++++ arch/arm64/kernel/arm64ksyms.c | 2 ++ arch/arm64/kernel/image.h | 2 ++ arch/arm64/lib/strcmp.S | 3 +++ arch/arm64/lib/strncmp.S | 3 +++ mm/kasan/kasan.c | 23 +++++++++++++++++++++++ 6 files changed, 38 insertions(+) diff --git a/arch/arm64/include/asm/string.h b/arch/arm64/include/asm/string.h index dd95d33..ab60349 100644 --- a/arch/arm64/include/asm/string.h +++ b/arch/arm64/include/asm/string.h @@ -24,9 +24,11 @@ extern char *strchr(const char *, int c); #define __HAVE_ARCH_STRCMP extern int strcmp(const char *, const char *); +extern int __strcmp(const char *, const char *); #define __HAVE_ARCH_STRNCMP extern int strncmp(const char *, const char *, __kernel_size_t); +extern int __strncmp(const char *, const char *, __kernel_size_t); #define __HAVE_ARCH_STRLEN extern __kernel_size_t strlen(const char *); @@ -68,6 +70,9 @@ void memcpy_flushcache(void *dst, const void *src, size_t cnt); #define memmove(dst, src, len) __memmove(dst, src, len) #define memset(s, c, n) __memset(s, c, n) +#define strcmp(cs, ct) __strcmp(cs, ct) +#define strncmp(cs, ct, n) __strncmp(cs, ct, n) + #ifndef __NO_FORTIFY #define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */ #endif diff --git a/arch/arm64/kernel/arm64ksyms.c b/arch/arm64/kernel/arm64ksyms.c index d894a20..10b1164 100644 --- a/arch/arm64/kernel/arm64ksyms.c +++ b/arch/arm64/kernel/arm64ksyms.c @@ -50,6 +50,8 @@ EXPORT_SYMBOL(strcmp); EXPORT_SYMBOL(strncmp); EXPORT_SYMBOL(strlen); EXPORT_SYMBOL(strnlen); +EXPORT_SYMBOL(__strcmp); +EXPORT_SYMBOL(__strncmp); EXPORT_SYMBOL(memset); EXPORT_SYMBOL(memcpy); EXPORT_SYMBOL(memmove); diff --git a/arch/arm64/kernel/image.h b/arch/arm64/kernel/image.h index a820ed0..5ef7a57 100644 --- a/arch/arm64/kernel/image.h +++ b/arch/arm64/kernel/image.h @@ -110,6 +110,8 @@ __efistub___flush_dcache_area = KALLSYMS_HIDE(__pi___flush_dcache_area); __efistub___memcpy = KALLSYMS_HIDE(__pi_memcpy); __efistub___memmove = KALLSYMS_HIDE(__pi_memmove); __efistub___memset = KALLSYMS_HIDE(__pi_memset); +__efistub___strcmp = KALLSYMS_HIDE(__pi_strcmp); +__efistub___strncmp = KALLSYMS_HIDE(__pi_strncmp); #endif __efistub__text = KALLSYMS_HIDE(_text); diff --git a/arch/arm64/lib/strcmp.S b/arch/arm64/lib/strcmp.S index 471fe61..0dffef7 100644 --- a/arch/arm64/lib/strcmp.S +++ b/arch/arm64/lib/strcmp.S @@ -60,6 +60,8 @@ tmp3 .req x9 zeroones .req x10 pos .req x11 +.weak strcmp +ENTRY(__strcmp) ENTRY(strcmp) eor tmp1, src1, src2 mov zeroones, #REP8_01 @@ -232,3 +234,4 @@ CPU_BE( orr syndrome, diff, has_nul ) sub result, data1, data2, lsr #56 ret ENDPIPROC(strcmp) +ENDPROC(__strcmp) diff --git a/arch/arm64/lib/strncmp.S b/arch/arm64/lib/strncmp.S index e267044..b2648c7 100644 --- a/arch/arm64/lib/strncmp.S +++ b/arch/arm64/lib/strncmp.S @@ -64,6 +64,8 @@ limit_wd .req x13 mask .req x14 endloop .req x15 +.weak strncmp +ENTRY(__strncmp) ENTRY(strncmp) cbz limit, .Lret0 eor tmp1, src1, src2 @@ -308,3 +310,4 @@ CPU_BE( orr syndrome, diff, has_nul ) mov result, #0 ret ENDPIPROC(strncmp) +ENDPROC(__strncmp) diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index c3bd520..61ad7f1 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -304,6 +304,29 @@ void *memcpy(void *dest, const void *src, size_t len) return __memcpy(dest, src, len); } +#ifdef CONFIG_ARM64 +/* + * Arch arm64 use assembly variant for strcmp/strncmp, + * xtensa use inline asm operations and x86_64 use c one, + * so now this interceptors only for arm64 kasan. + */ +#undef strcmp +int strcmp(const char *cs, const char *ct) +{ + check_memory_region((unsigned long)cs, 1, false, _RET_IP_); + check_memory_region((unsigned long)ct, 1, false, _RET_IP_); + + return __strcmp(cs, ct); +} +#undef strncmp +int strncmp(const char *cs, const char *ct, size_t len) +{ + check_memory_region((unsigned long)cs, len, false, _RET_IP_); + check_memory_region((unsigned long)ct, len, false, _RET_IP_); + + return __strncmp(cs, ct, len); +} +#endif void kasan_alloc_pages(struct page *page, unsigned int order) {