Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1509040imm; Mon, 3 Sep 2018 02:16:05 -0700 (PDT) X-Google-Smtp-Source: ANB0VdayfMGPRcCySyrU5HLI7bshUtSiKCYmb12mmnRIqqBH4HJ4Fl6Ka7gxbRQpMalFT3r3QgFS X-Received: by 2002:a17:902:be08:: with SMTP id r8-v6mr27693736pls.265.1535966165805; Mon, 03 Sep 2018 02:16:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535966165; cv=none; d=google.com; s=arc-20160816; b=q896S+HdBolEG8LCoL9PzW9LnOu6+6pVaMYTxkOHumsXtKthR+kghFX5A9YGmmGd3h 2lf8npSB/zWXXOb3xN1qRY/JYomXClPx2IQFiDu074QUixryavqvxUonFtvO/aL4e8My YcNZxAnuV9HdlloMYXZ5/bjfNhCaZUdB0BDXi9FIqe2vyUt1ObI5n/Q5i3z2Vo3LPpPA 9lA3W/n7B+aUyuxg2JJc1OR+Y7xzTaBP8OQMejBET97+3qXiyW6TNKoLTJ8VUlV+ZBUZ h7VvdOdbtkrkcwuT18NJUfuidIVvAii/fW7JEtDl58/nq2Fs+jWbQofLf4b13DvKPGOL QjVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=t++vTVhu9ygKCY2NSZpL+hZ41FR2WUk/Lah+sgjU5so=; b=BCdCoUO4EC1majH/mGzkB8dhSqX7Uqw+mVX3Tm7fVp3tUpYBfor+Wpslv+cUuSGM28 53cnfZ3lpH+AQTB8PSWr64LDPMGeGuHd1XPvnPynLV2GY//WF5AasXzysbawgd279546 SV9HDYyGmkeUuKudPmFr9YEs50CZfl2GaVt+oa1pSzuW4x3vuw8oQXhHABk9/yREru7F k8Ig5+3cSonGRZDUUOU4A37Q/KOq7n1Py2a54fbtz+x1pZ2lWCJf/uJqhKUS2oxxVpxZ qyJeq/anyLV6w1LQYedBtIwv4ZjAIArpiN+LvCpYMVu9WX+Hm6ORc9KvxDD+Nq8kZ8Bb xkaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=uNUWCB1y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g4-v6si15983899pll.384.2018.09.03.02.15.50; Mon, 03 Sep 2018 02:16:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=uNUWCB1y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727050AbeICNde (ORCPT + 99 others); Mon, 3 Sep 2018 09:33:34 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:35967 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725866AbeICNdd (ORCPT ); Mon, 3 Sep 2018 09:33:33 -0400 Received: by mail-pg1-f195.google.com with SMTP id d1-v6so8333345pgo.3 for ; Mon, 03 Sep 2018 02:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=t++vTVhu9ygKCY2NSZpL+hZ41FR2WUk/Lah+sgjU5so=; b=uNUWCB1ydX2/sZ6Jlyd/oYX62bdfU7Au5zcBEYQy5GjFc6PYiEVHdZr1B7JAOegWoE 2epaD0zKzOJQbGhSRoAzUIHkL6SZdAZV4vpCUCeQTxvYe0xdEzojMXoHzgfW/Dkm3cXV ucJw2Dx4/fpReg+4T5Jhq3nhnMoBZnAG4+HU7MAJkj9cx3G22FzmPxvosMdwc799nfyL pqJf6MsepMFe82Krtlxd2V1kNwJbdbL5ly3R5jr2O1uFfjwG08LWPeEcOwbzuTPzzOx3 AsZz3p9wNP6y+MKBsch9Z/+CYtZ3CY9JqY7lM847uH1NU2klBk+y1kKWDdmyHQi8rVvH M9eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=t++vTVhu9ygKCY2NSZpL+hZ41FR2WUk/Lah+sgjU5so=; b=iAi5qnXLnAy82Plo3nm+Ga6nFq8leOdjbyAZd9lw+YNlHmA+LS8YycKuDkwwWVojpX 7SvepaW5dMyuEhBYUsOQUhZj9DYwx2UxDq/8HZ7Kco1mc1tn1PlJ1P7dkdDEedBdlO/X QJTIBq/MZP6jq7p+2yZjPvwocWO9bRRPYwyz/IHC8CMLEf/1gUBawGh+byiNoZFo33N5 3N4sXhpPvtVx9TqUxLLolD/FjRW3yGEWsoLqpvhsFTd+2UoWc7lYjui50cu1cWiDRw8c x5R+Oxtil4+felqEZIwXF2XW5GycxdzC7fC25K8MpxFRmeKa5xoO4TLBtk3Je9by82Iw uzpg== X-Gm-Message-State: APzg51BE4iIsAvloEBhqITBU+fXlcuMuuf0kw6+2+hSdqcY/j9lz75X6 kr571pg//giQUBzfpSEXYBXyseCEDHgQnXrCznA6aQ== X-Received: by 2002:a63:6183:: with SMTP id v125-v6mr25876201pgb.242.1535966058571; Mon, 03 Sep 2018 02:14:18 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a17:90a:ac14:0:0:0:0 with HTTP; Mon, 3 Sep 2018 02:13:58 -0700 (PDT) In-Reply-To: <7387f67e-1ac5-12e1-c9be-060e9c403bf7@lge.com> References: <1535014606-176525-1-git-send-email-kyeongdon.kim@lge.com> <7387f67e-1ac5-12e1-c9be-060e9c403bf7@lge.com> From: Dmitry Vyukov Date: Mon, 3 Sep 2018 11:13:58 +0200 Message-ID: Subject: Re: [PATCH v2] arm64: kasan: add interceptors for strcmp/strncmp functions To: Kyeongdon Kim Cc: Andrey Ryabinin , Catalin Marinas , Will Deacon , Alexander Potapenko , "Jason A. Donenfeld" , Rob Herring , Ard Biesheuvel , Linux ARM , LKML , kasan-dev , Linux-MM Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 3, 2018 at 11:02 AM, Kyeongdon Kim wrot= e: > Dear all, > > Could anyone review this and provide me appropriate approach ? > I think str[n]cmp are frequently used functions so I believe very useful = w/ > arm64 KASAN. Hi Kyeongdon, Please add tests for this to lib/test_kasan.c. > On 2018-08-23 =EC=98=A4=ED=9B=84 5:56, Kyeongdon Kim wrote: >> >> 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. >> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >> 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 =3D >> KALLSYMS_HIDE(__pi___flush_dcache_area); >> __efistub___memcpy =3D KALLSYMS_HIDE(__pi_memcpy); >> __efistub___memmove =3D KALLSYMS_HIDE(__pi_memmove); >> __efistub___memset =3D KALLSYMS_HIDE(__pi_memset); >> +__efistub___strcmp =3D KALLSYMS_HIDE(__pi_strcmp); >> +__efistub___strncmp =3D KALLSYMS_HIDE(__pi_strncmp); >> #endif >> >> __efistub__text =3D 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 le= n) >> >> 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) >> { >> -- >> 2.6.2 > >