From patchwork Thu Jan 20 10:00:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: guangming.cao@mediatek.com X-Patchwork-Id: 533759 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17545C433EF for ; Thu, 20 Jan 2022 09:59:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359702AbiATJ7n (ORCPT ); Thu, 20 Jan 2022 04:59:43 -0500 Received: from mailgw02.mediatek.com ([210.61.82.184]:54940 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S241779AbiATJ7m (ORCPT ); Thu, 20 Jan 2022 04:59:42 -0500 X-UUID: fd05882b4df84d1e9a1d3b7ebbe5731f-20220120 X-UUID: fd05882b4df84d1e9a1d3b7ebbe5731f-20220120 Received: from mtkcas11.mediatek.inc [(172.21.101.40)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1198683157; Thu, 20 Jan 2022 17:59:40 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.792.3; Thu, 20 Jan 2022 17:59:38 +0800 Received: from mszswglt01.gcn.mediatek.inc (10.16.20.20) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Thu, 20 Jan 2022 17:59:37 +0800 From: To: CC: , , , , , , , , , , , , , , , , , , , , , Guangming Subject: [PATCH v6 RESEND] dma-buf: system_heap: Add a size check for allocation Date: Thu, 20 Jan 2022 18:00:36 +0800 Message-ID: <20220120100036.99867-1-guangming.cao@mediatek.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220120085259.98147-1-guangming.cao@mediatek.com> References: <20220120085259.98147-1-guangming.cao@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Guangming Add a size check for allocation since the allocation size should be always less than the total DRAM size on system heap. Adding this check can prevent comsuming too much time for invalid allocations. Signed-off-by: Guangming Acked-by: John Stultz Acked-by: Christian König --- drivers/dma-buf/heaps/system_heap.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 23a7e74ef966..b65e597a742f 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -347,6 +347,14 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap, struct page *page, *tmp_page; int i, ret = -ENOMEM; + /* + * Size check. The "len" should be less than totalram since system_heap + * memory is comes from system. Adding check here can prevent comsuming + * too much time for invalid allocations. + */ + if (PFN_DOWN(len) > totalram_pages()) + return ERR_PTR(-EINVAL); + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) return ERR_PTR(-ENOMEM);