From mboxrd@z Thu Jan 1 00:00:00 1970 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 1BACFC433F5 for ; Wed, 19 Jan 2022 09:56:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4ijv1O/NQmobRmQdHgkGtLas87AeapjBHKn5IaQWSkA=; b=QUOukKHhqozEH/ LuFcJJWirctVk4gAITKrClPoHdPFrD6qN7qFh4OYVxuaQ9F6T32Dsn4wwTS41/0X+/yPaBHSBIWFk 2ywsUpLGJJOZbqinVg2voOVVzH6ZTYrxBlwYQ2uZrjByf14jYE5nXFoQMBsPcSPvnNVf3/Lfy8zt8 3hvz3ODxuK9le7+VZR4sx2dq288oVeyYq4Tt3qM6fKEIRVcg7T6nqJWf2cE4XHdgI8cE227+SLmKT FHi2iceip9DWLXJyp1tRhVRBrK0wuFu00xLgZpLOJuaCKfS5iejC0vOVNyeRVEICe07pVSohIIbVJ +8pZi+T0Y4RPAO4fCkvw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nA7ge-004k2t-Sk; Wed, 19 Jan 2022 09:55:28 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nA7gb-004k1X-3k for linux-arm-kernel@lists.infradead.org; Wed, 19 Jan 2022 09:55:26 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 89B491FB; Wed, 19 Jan 2022 01:55:23 -0800 (PST) Received: from C02TD0UTHF1T.local (unknown [10.57.38.123]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B5C253F766; Wed, 19 Jan 2022 01:55:21 -0800 (PST) Date: Wed, 19 Jan 2022 09:55:14 +0000 From: Mark Rutland To: Yury Norov Cc: Catalin Marinas , Will Deacon , Andrew Morton , Nicholas Piggin , Ding Tianhong , Anshuman Khandual , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [RFC PATCH] arm64: don't vmap() invalid page Message-ID: <20220119095514.GA42995@C02TD0UTHF1T.local> References: <20220118185354.464517-1-yury.norov@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220118185354.464517-1-yury.norov@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220119_015525_272247_80196B0E X-CRM114-Status: GOOD ( 29.29 ) 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-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Jan 18, 2022 at 10:53:54AM -0800, Yury Norov wrote: > vmap() takes struct page *pages as one of arguments, and user may provide > an invalid pointer, which would lead to DABT at address translation later. > Currently, kernel checks the pages against NULL. In my case, however, the > address was not NULL, and was big enough so that the hardware generated > Address Size Abort. Can you give an example of when this might happen? It sounds like you're actually hitting this, so a backtrace would be nice. I'm a bit confused as to when why we'd try to vmap() pages that we didn't have a legitimate struct page for -- where did these addresses come from? It sounds like this is going wrong at a higher level, and we're passing entirely bogus struct page pointers around. This seems like the sort of thing DEBUG_VIRTUAL or similar should check when we initially generate the struct page pointer. Thanks, Mark. > Interestingly, this abort happens even if copy_from_kernel_nofault() is used, > which is quite inconvenient for debugging purposes. > > This patch adds an arch_vmap_page_valid() helper into vmap() path, so that > architectures may add arch-specific checks of the pointer passed into vmap. > > For arm64, if the page passed to vmap() corresponds to a physical address > greater than maximum possible value as described in TCR_EL1.IPS register, the > following table walk would generate Address Size Abort. Instead of creating > the invalid mapping, kernel will return ERANGE in such situation. > > Signed-off-by: Yury Norov > --- > arch/arm64/include/asm/vmalloc.h | 41 ++++++++++++++++++++++++++++++++ > include/linux/vmalloc.h | 7 ++++++ > mm/vmalloc.c | 8 +++++-- > 3 files changed, 54 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/include/asm/vmalloc.h b/arch/arm64/include/asm/vmalloc.h > index b9185503feae..e9d43ee019ad 100644 > --- a/arch/arm64/include/asm/vmalloc.h > +++ b/arch/arm64/include/asm/vmalloc.h > @@ -4,6 +4,47 @@ > #include > #include > > +static inline u64 pa_size(u64 ips) > +{ > + switch (ips) { > + case 0b000: > + return 1UL << 32; > + case 0b001: > + return 1UL << 36; > + case 0b010: > + return 1UL << 40; > + case 0b011: > + return 1UL << 42; > + case 0b100: > + return 1UL << 44; > + case 0b101: > + return 1UL << 48; > + case 0b110: > + return 1UL << 52; > + /* All other values */ > + default: > + return 1UL << 52; > + } > +} > + > +#define arch_vmap_page_valid arch_vmap_page_valid > +static inline int arch_vmap_page_valid(struct page *page) > +{ > + u64 tcr, ips, paddr_size; > + > + if (!page) > + return -ENOMEM; > + > + tcr = read_sysreg_s(SYS_TCR_EL1); > + ips = (tcr & TCR_IPS_MASK) >> TCR_IPS_SHIFT; > + > + paddr_size = pa_size(ips); > + if (page_to_phys(page) >= paddr_size) > + return -ERANGE; > + > + return 0; > +} > + > #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP > > #define arch_vmap_pud_supported arch_vmap_pud_supported > diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h > index 6e022cc712e6..08b567d8bafc 100644 > --- a/include/linux/vmalloc.h > +++ b/include/linux/vmalloc.h > @@ -119,6 +119,13 @@ static inline int arch_vmap_pte_supported_shift(unsigned long size) > } > #endif > > +#ifndef arch_vmap_page_valid > +static inline int arch_vmap_page_valid(struct page *page) > +{ > + return page ? 0 : -ENOMEM; > +} > +#endif > + > /* > * Highlevel APIs for driver use > */ > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index d2a00ad4e1dd..ee0384405cdd 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -472,11 +472,15 @@ static int vmap_pages_pte_range(pmd_t *pmd, unsigned long addr, > return -ENOMEM; > do { > struct page *page = pages[*nr]; > + int ret; > > if (WARN_ON(!pte_none(*pte))) > return -EBUSY; > - if (WARN_ON(!page)) > - return -ENOMEM; > + > + ret = arch_vmap_page_valid(page); > + if (WARN_ON(ret)) > + return ret; > + > set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); > (*nr)++; > } while (pte++, addr += PAGE_SIZE, addr != end); > -- > 2.30.2 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel