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 X-Spam-Level: X-Spam-Status: No, score=-15.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 F2BF5C433E0 for ; Tue, 23 Mar 2021 13:30:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A6A9761974 for ; Tue, 23 Mar 2021 13:30:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbhCWN3f (ORCPT ); Tue, 23 Mar 2021 09:29:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbhCWN3G (ORCPT ); Tue, 23 Mar 2021 09:29:06 -0400 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C64D1C061574 for ; Tue, 23 Mar 2021 06:29:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: 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=N22ubGJYBatnLB0FQ3aMgIjwQpebKFwTd2aBJXaAtcE=; b=dVP8Rnruf62ZNvmbO5B0fQyBT kNVTyXiDJvSL4bKQGHMBP84vP5U6BrykWzRDEUlOaIcnUnnRyzFnFGnwdZxLg+nvRu5VEuSqhWXEb Y3WgxnWAAcanp5GhzZeJaNSy5vGfLyNnmcJahmw7BtBMQHMmV9JqOCscWhXZdJ3CZsIaiBCyBzP2h Bh5AO1/XjT2ZPD2wdUb5TKqX4MDwHeHCgSJEQw7CqyHIqdfwbgtbP509FD18+rBmhhRX7ng9cdiO0 iN4vQa6p6iWFW/0sQrxU2X7u2pdxA1J47TNAQP9jm6mVfEmJTQ4E+Kiasv2FND7oXKfp4a1sBiWT3 eKpHlITDg==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:51624) by pandora.armlinux.org.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOh5a-0007la-IS; Tue, 23 Mar 2021 13:28:54 +0000 Received: from linux by shell.armlinux.org.uk with local (Exim 4.92) (envelope-from ) id 1lOh5W-0004Bp-JP; Tue, 23 Mar 2021 13:28:50 +0000 Date: Tue, 23 Mar 2021 13:28:50 +0000 From: Russell King - ARM Linux admin To: Arnd Bergmann Cc: Arnd Bergmann , Kyungmin Park , Marek Szyprowski , Thomas Bogendoerfer , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ARM: dma-mapping: fix out of bounds access in CMA Message-ID: <20210323132850.GB1463@shell.armlinux.org.uk> References: <20210323131423.2581218-1-arnd@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210323131423.2581218-1-arnd@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: Russell King - ARM Linux admin Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 23, 2021 at 02:14:13PM +0100, Arnd Bergmann wrote: > From: Arnd Bergmann > > Dereferencing a zero-length array is always a bug, and we get a warning > with 'make W=1' here: > > arch/arm/mm/dma-mapping.c: In function 'dma_contiguous_early_fixup': > arch/arm/mm/dma-mapping.c:395:15: error: array subscript is outside array bounds of 'struct dma_contig_early_reserve[0]' [-Werror=array-bounds] > 395 | dma_mmu_remap[dma_mmu_remap_num].base = base; > | ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ > arch/arm/mm/dma-mapping.c:389:40: note: while referencing 'dma_mmu_remap' > 389 | static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata; > | ^~~~~~~~~~~~~ > arch/arm/mm/dma-mapping.c:396:15: error: array subscript is outside array bounds of 'struct dma_contig_early_reserve[0]' [-Werror=array-bounds] > > Add a runtime check to prevent this from happening, while also > avoiding the compile-time warning. > > Fixes: c79095092834 ("ARM: integrate CMA with DMA-mapping subsystem") > Signed-off-by: Arnd Bergmann > --- > arch/arm/mm/dma-mapping.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index c4b8df2ad328..af29344fb150 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -392,6 +392,11 @@ static int dma_mmu_remap_num __initdata; > > void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) > { > + if (!MAX_CMA_AREAS || dma_mmu_remap_num >= MAX_CMA_AREAS) { > + WARN_ONCE(1, "number of CMA areas\n"); > + return; > + } > + What if dma_mmu_remap_num were negative - that condition is not checked and will also result in an overflow of the array. If we're being fussy enough to bounds check, we ought to do it properly. So, I think a better solution would be to make dma_mmu_remap_num an unsigned int, and then to use: if (dma_mmu_remap_num >= ARRAY_SIZE(dma_mmu_remap)) { ... } which is really the condition we're after here. -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last! 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 X-Spam-Level: X-Spam-Status: No, score=-15.5 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,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable 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 04744C433DB for ; Tue, 23 Mar 2021 13:32:15 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 7DF6C61974 for ; Tue, 23 Mar 2021 13:32:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7DF6C61974 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=armlinux.org.uk Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; 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=sv5cpvNDtzv5QHGhAD/UKKcWwha2lUhHJqospn9x9NY=; b=MrPEGt45x7yHwACxywSJqE1IW c1GphX52j5emQAls6lLV66mpB8KyNWsjIb4APhaf28z4VKq7jzeo5Blgnj2A7JiDt0rinKDADYkLA mLsqTiXEYA0EAx1HYIbhnDB7PvBzhzCf7bIdspXLYDURhWxH0F0ZhCTH/Y6IUNsvdsfmxhG0J5FRp rEc1X9S4uub0px4Oe/cPnzrm1PO4S1Qp1jUuzJ2tdftqi2DBeDRAhaJfvUgRkaKoj7VAlL4S1GlbZ ioPE2awsyLj58oHZcuqr0ChoD5ofm79KwxgJzNUn5hR0aYTNLwoU+X6vsx13KRRJ6jXfTb01Cvpgx fZyoksihw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOh5n-00F2kk-8S; Tue, 23 Mar 2021 13:29:07 +0000 Received: from pandora.armlinux.org.uk ([2001:4d48:ad52:32c8:5054:ff:fe00:142]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOh5j-00F2kO-4P for linux-arm-kernel@lists.infradead.org; Tue, 23 Mar 2021 13:29:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: 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=N22ubGJYBatnLB0FQ3aMgIjwQpebKFwTd2aBJXaAtcE=; b=dVP8Rnruf62ZNvmbO5B0fQyBT kNVTyXiDJvSL4bKQGHMBP84vP5U6BrykWzRDEUlOaIcnUnnRyzFnFGnwdZxLg+nvRu5VEuSqhWXEb Y3WgxnWAAcanp5GhzZeJaNSy5vGfLyNnmcJahmw7BtBMQHMmV9JqOCscWhXZdJ3CZsIaiBCyBzP2h Bh5AO1/XjT2ZPD2wdUb5TKqX4MDwHeHCgSJEQw7CqyHIqdfwbgtbP509FD18+rBmhhRX7ng9cdiO0 iN4vQa6p6iWFW/0sQrxU2X7u2pdxA1J47TNAQP9jm6mVfEmJTQ4E+Kiasv2FND7oXKfp4a1sBiWT3 eKpHlITDg==; Received: from shell.armlinux.org.uk ([fd8f:7570:feb6:1:5054:ff:fe00:4ec]:51624) by pandora.armlinux.org.uk with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOh5a-0007la-IS; Tue, 23 Mar 2021 13:28:54 +0000 Received: from linux by shell.armlinux.org.uk with local (Exim 4.92) (envelope-from ) id 1lOh5W-0004Bp-JP; Tue, 23 Mar 2021 13:28:50 +0000 Date: Tue, 23 Mar 2021 13:28:50 +0000 From: Russell King - ARM Linux admin To: Arnd Bergmann Cc: Arnd Bergmann , Kyungmin Park , Marek Szyprowski , Thomas Bogendoerfer , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] ARM: dma-mapping: fix out of bounds access in CMA Message-ID: <20210323132850.GB1463@shell.armlinux.org.uk> References: <20210323131423.2581218-1-arnd@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210323131423.2581218-1-arnd@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210323_132903_230330_39320607 X-CRM114-Status: GOOD ( 21.98 ) 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, Mar 23, 2021 at 02:14:13PM +0100, Arnd Bergmann wrote: > From: Arnd Bergmann > > Dereferencing a zero-length array is always a bug, and we get a warning > with 'make W=1' here: > > arch/arm/mm/dma-mapping.c: In function 'dma_contiguous_early_fixup': > arch/arm/mm/dma-mapping.c:395:15: error: array subscript is outside array bounds of 'struct dma_contig_early_reserve[0]' [-Werror=array-bounds] > 395 | dma_mmu_remap[dma_mmu_remap_num].base = base; > | ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~ > arch/arm/mm/dma-mapping.c:389:40: note: while referencing 'dma_mmu_remap' > 389 | static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata; > | ^~~~~~~~~~~~~ > arch/arm/mm/dma-mapping.c:396:15: error: array subscript is outside array bounds of 'struct dma_contig_early_reserve[0]' [-Werror=array-bounds] > > Add a runtime check to prevent this from happening, while also > avoiding the compile-time warning. > > Fixes: c79095092834 ("ARM: integrate CMA with DMA-mapping subsystem") > Signed-off-by: Arnd Bergmann > --- > arch/arm/mm/dma-mapping.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index c4b8df2ad328..af29344fb150 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -392,6 +392,11 @@ static int dma_mmu_remap_num __initdata; > > void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) > { > + if (!MAX_CMA_AREAS || dma_mmu_remap_num >= MAX_CMA_AREAS) { > + WARN_ONCE(1, "number of CMA areas\n"); > + return; > + } > + What if dma_mmu_remap_num were negative - that condition is not checked and will also result in an overflow of the array. If we're being fussy enough to bounds check, we ought to do it properly. So, I think a better solution would be to make dma_mmu_remap_num an unsigned int, and then to use: if (dma_mmu_remap_num >= ARRAY_SIZE(dma_mmu_remap)) { ... } which is really the condition we're after here. -- RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last! _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel