From patchwork Wed Oct 11 09:41:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Punit Agrawal X-Patchwork-Id: 9999189 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CC6F26037F for ; Wed, 11 Oct 2017 10:02:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAD7A289D1 for ; Wed, 11 Oct 2017 10:02:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AED4A289D3; Wed, 11 Oct 2017 10:02:29 +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=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 135DF289D1 for ; Wed, 11 Oct 2017 10:02:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=63ehw7X1z5si8EvEl5Nk56MNBaL3PGO5pqvUqm3qRxA=; b=t9U8k9tE0G02TLqRTuAH6hho5l DKrSxWYx4wsnzwYicFTE5fre2G1258lIZME115vy2tdtoBKZ6fMaxLe+BUlXhA9TNZcVNEr4Kg8qv kzSVZh0JhokvK+aMDDWBRwI6Ya2AGYXxxzZWRbw576/veRlA/73ZvAPeaTvAIaD2WLqfFYs0QG6MT 5kfl3xiY6Ufu86yMWOEjMo7e3RkUHICqP23dMze909Vfr6dzgFLWsOFdDOFmnW6lTNPsZHOnKMYGx HiYfbzXNyLenPKAFMCCC3xeiBUcqHKxie3ob+q70g2IKcKSxZLKJPq6Bs4CTuHumbVdHInx4yRBEL mpBB9idQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e2Dq2-0002HZ-AZ; Wed, 11 Oct 2017 10:02:06 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e2Dpk-000205-Mm for linux-arm-kernel@bombadil.infradead.org; Wed, 11 Oct 2017 10:01:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=3cyqGEWHVPEZ1G7KBzcidqYrKrzoPT2KIdG8DtB0u5k=; b=m+QgKAESwdVFFhsHXWqt3roQI 0adY1RMAG1BeHBoJ3wEOc2X3+L0aXDmiHS7C9E1KkvEJDag/NA8JAQJnLvx0ZQngeyTr+DUoDQAi+ TnYCKum2SJlUEBTR1qzUIzc4Q5YGKv4XjxrFzQempzFtIXXeVvQjRu8r7MHyqirX6Qzx71Bbhmr1l KGe9fnv1Opvv6/BnPxqaAlD6Lpj6HgXYd9+OKvojqg4ab1f5gE8q3mjFmKcbL0cRyw7iCClvEwRuL kObTBd1hu/npH2r+YQZ2y6XGtfB7FgVDqgymJfaVSm/KAimbegWhGLG2R40b/MTpQWZmQi6sEcNo6 wywnaQSVw==; Received: from foss.arm.com ([217.140.101.70]) by merlin.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e2DXl-0006HD-TE for linux-arm-kernel@lists.infradead.org; Wed, 11 Oct 2017 09:43:14 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 67A6B1435; Wed, 11 Oct 2017 02:42:51 -0700 (PDT) Received: from localhost (e105922-lin.cambridge.arm.com [10.1.207.29]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0BCB13F3E1; Wed, 11 Oct 2017 02:42:50 -0700 (PDT) From: Punit Agrawal To: linux-kernel@vger.kernel.org Subject: [PATCH 3/3] irqchip/gic-v3: Bounds check redistributor accesses Date: Wed, 11 Oct 2017 10:41:48 +0100 Message-Id: <20171011094148.15674-4-punit.agrawal@arm.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171011094148.15674-1-punit.agrawal@arm.com> References: <20171011094148.15674-1-punit.agrawal@arm.com> X-ARM-No-Footer: FoSSMail X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marc.zyngier@arm.com, Punit Agrawal , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The kernel crashes while iterating over a redistributor that is in-correctly sized by the platform firmware or doesn't contain the last record. Prevent the crash by checking accesses against the size of the region provided by the firmware. While we are at it, warn the user about incorrect region size. Signed-off-by: Punit Agrawal Cc: Marc Zyngier --- drivers/irqchip/irq-gic-v3.c | 48 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/drivers/irqchip/irq-gic-v3.c b/drivers/irqchip/irq-gic-v3.c index 881d327c53fa..754d936c95e5 100644 --- a/drivers/irqchip/irq-gic-v3.c +++ b/drivers/irqchip/irq-gic-v3.c @@ -429,11 +429,21 @@ static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *)) int i; for (i = 0; i < gic_data.nr_redist_regions; i++) { - void __iomem *ptr = gic_data.redist_regions[i].redist_base; struct resource *res = &gic_data.redist_regions[i].res; - u64 typer; + void __iomem *ptr, *base; + u64 typer, size, stride; u32 reg; + ptr = base = gic_data.redist_regions[i].redist_base; + size = resource_size(res); + + stride = gic_data.redist_stride ?: SZ_64K * 2; + if (ptr + stride > base + size) { + pr_warn("Insufficient size for redistributor region @%llx. Skipping\n", + res->start); + continue; + } + reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK; if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */ @@ -442,7 +452,28 @@ static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *)) } do { + /* + * We can access GICR_TYPER as we have already + * checked that we have atleast 128kB or + * redist_stride + */ typer = gic_read_typer(ptr + GICR_TYPER); + if (!gic_data.redist_stride && + (typer & GICR_TYPER_VLPIS)) { + /* VLPI_base + reserved page */ + stride += SZ_64K * 2; + + /* + * We are larger than we thought, do + * we still fit? + */ + if (ptr + stride > base + size) { + pr_warn("No last record found in redistributor region @%llx\n", + gic_data.redist_regions[i].res.start); + break; + } + } + ret = fn(gic_data.redist_regions + i, ptr); if (!ret) return 0; @@ -450,12 +481,13 @@ static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *)) if (gic_data.redist_regions[i].single_redist) break; - if (gic_data.redist_stride) { - ptr += gic_data.redist_stride; - } else { - ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */ - if (typer & GICR_TYPER_VLPIS) - ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */ + ptr += stride; + + stride = gic_data.redist_stride ?: SZ_64K * 2; + if (ptr + stride > base + size) { + pr_warn("No last record found in redistributor region @%llx\n", + gic_data.redist_regions[i].res.start); + break; } } while (!(typer & GICR_TYPER_LAST)); }