Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 210C6C432BE for ; Wed, 1 Sep 2021 07:07:43 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DFDAF61056 for ; Wed, 1 Sep 2021 07:07:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DFDAF61056 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:CC:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=6g6a/SbxCvbXdYM54MOIFtZVs6Jal71dEKtiisB2VR8=; b=TKZXO+AkXkvizrvZtrF/EKaHKS 4Px2etosr8FUbHl/gP0Qxt99WvZbppm4TBe2PxaETliCfaoTyCqLBNE0LqAGv+aV67n/lFPIkCJUm jVMxEQ9qbpw57pB+S8VqhZRxIZe+6FS7AKVQlyw8SkhrHlei6f8UpPJMOtccK4AS6Qvb0lWXkfQuq NUT1tWeKFMTGo0ZCkKLjy/r4UuIT+U8dG+9dnKlBP4xYar42Duz6NzSzvx2zByeDJtTK+YnmoeFfo Bf1bpJBRp4AcKWeyJHUifGZvLG3nAKVklMDRlIBVWL5oIvdqziJdMxZIU7GHxR+Mdlaby/GhV8PXc Ycv/uqPA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mLKJo-004Gnj-Gk; Wed, 01 Sep 2021 07:05:56 +0000 Received: from szxga08-in.huawei.com ([45.249.212.255]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mLKJj-004Glo-Cj for linux-arm-kernel@lists.infradead.org; Wed, 01 Sep 2021 07:05:53 +0000 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Gzw6n50q1z1DFjw; Wed, 1 Sep 2021 15:05:05 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 1 Sep 2021 15:05:47 +0800 Received: from [10.174.177.243] (10.174.177.243) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Wed, 1 Sep 2021 15:05:47 +0800 Subject: Re: [PATCH v2] ARM: kasan: Fix __get_user_check failure with kasan To: Lexi Shao , CC: , , , , , , , , , References: <20210827125601.GT22278@shell.armlinux.org.uk> <20210828022552.45221-1-shaolexi@huawei.com> From: Kefeng Wang Message-ID: <26084f5b-d002-5bef-55db-7ae47a3e73af@huawei.com> Date: Wed, 1 Sep 2021 15:05:46 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: <20210828022552.45221-1-shaolexi@huawei.com> Content-Language: en-US X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210901_000551_936966_94525933 X-CRM114-Status: GOOD ( 24.12 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2021/8/28 10:25, Lexi Shao wrote: > In macro __get_user_check defined in arch/arm/include/asm/uaccess.h, > error code is store in register int __e(r0). When kasan is > enabled, assigning value to kernel address might trigger kasan check, > which unexpectedly overwrites r0 and causes undefined behavior on arm > kasan images. > > One example is failure in do_futex and results in process soft lockup. > Log: > watchdog: BUG: soft lockup - CPU#0 stuck for 62946ms! [rs:main > Q:Reg:1151] > ... > (__asan_store4) from (futex_wait_setup+0xf8/0x2b4) > (futex_wait_setup) from (futex_wait+0x138/0x394) > (futex_wait) from (do_futex+0x164/0xe40) > (do_futex) from (sys_futex_time32+0x178/0x230) > (sys_futex_time32) from (ret_fast_syscall+0x0/0x50) > > The soft lockup happens in function futex_wait_setup. The reason is > function get_futex_value_locked always return EINVAL, thus pc jump > back to retry label and causes looping. > > This line in function get_futex_value_locked > ret = __get_user(*dest, from); > is expanded to > *dest = (typeof(*(p))) __r2; , > in macro __get_user_check. Writing to pointer dest triggers kasan check > and overwrites the return value of __get_user_x function. > The assembly code of get_futex_value_locked in kernel/futex.c: > ... > c01f6dc8: eb0b020e bl c04b7608 <__get_user_4> > // "x = (typeof(*(p))) __r2;" triggers kasan check and r0 is overwritten > c01f6dcc: e1a00007 mov r0, r7 > c01f6dd0: e1a05002 mov r5, r2 > c01f6dd4: eb04f1e6 bl c0333574 <__asan_store4> > c01f6dd8: e5875000 str r5, [r7] > // save ret value of __get_user(*dest, from), which is dest address now > c01f6ddc: e1a05000 mov r5, r0 > ... > // checking return value of __get_user failed > c01f6e00: e3550000 cmp r5, #0 > ... > c01f6e0c: 01a00005 moveq r0, r5 > // assign return value to EINVAL > c01f6e10: 13e0000d mvnne r0, #13 > > Return value is the destination address of get_user thus certainly > non-zero, so get_futex_value_locked always return EINVAL. > > Fix it by using a tmp vairable to store the error code before the > assignment. This fix has no effects to non-kasan images thanks to compiler > optimization. It only affects cases that overwrite r0 due to kasan check. > > This should fix bug discussed in link: > [1] https://lore.kernel.org/linux-arm-kernel/0ef7c2a5-5d8b-c5e0-63fa-31693fd4495c@gmail.com/ > > Fixes: 421015713b30 ("ARM: 9017/2: Enable KASan for ARM") > Signed-off-by: Lexi Shao Reviewed-by: Kefeng Wang > --- > arch/arm/include/asm/uaccess.h | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/arch/arm/include/asm/uaccess.h b/arch/arm/include/asm/uaccess.h > index a13d90206472..d9db752c51fe 100644 > --- a/arch/arm/include/asm/uaccess.h > +++ b/arch/arm/include/asm/uaccess.h > @@ -200,6 +200,7 @@ extern int __get_user_64t_4(void *); > register unsigned long __l asm("r1") = __limit; \ > register int __e asm("r0"); \ > unsigned int __ua_flags = uaccess_save_and_enable(); \ > + int __tmp_e; \ > switch (sizeof(*(__p))) { \ > case 1: \ > if (sizeof((x)) >= 8) \ > @@ -227,9 +228,10 @@ extern int __get_user_64t_4(void *); > break; \ > default: __e = __get_user_bad(); break; \ > } \ > + __tmp_e = __e; \ > uaccess_restore(__ua_flags); \ > x = (typeof(*(p))) __r2; \ > - __e; \ > + __tmp_e; \ > }) > > #define get_user(x, p) \ _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel