Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1241219pxb; Fri, 21 Jan 2022 13:08:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJzIHsJ4k1aZkGuBtY44tNoLqzlSIEGMbUDdIpj3Eh0FWhmtHKhtbapzXFH+1l0Xl+E9CLTm X-Received: by 2002:a17:90a:194e:: with SMTP id 14mr2499742pjh.106.1642799322313; Fri, 21 Jan 2022 13:08:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642799322; cv=none; d=google.com; s=arc-20160816; b=iveWLzJPmY4X1OLKumw/F3WZy9rfM45ffKIaEPD2DL0961SgYPO/+QiMi7yymY6uGy QhzXWI8l8eiyhgPEqGCXxJCInryIfdRuesszz9WNWJFmsBovKUwhpczX1h1GyGqvbn7l WlBwrGjP2Xyoud31sGMIqQmikacvWYQ5+xzCLtudqPY6dj4BJnk3XGNISCkp3erreg+l R/Qe3euiDRhTcc2pzU3+1CHZS698OzaW5pbuvK2VKecm7ijqeG0+NV4JjSehpnaEXBmo Bk/1vkDMLGWiUWsU4UIuNZYqr6WDnfnHdy90wbkobs97YkGRGA/eWoABPJhIPhdXUOeQ P8TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=CE/7/ZtIcdtMz8rdzGRaekNnYFTSF6Nr0u4TdchMA6w=; b=Fau9utGjGPpLvtEsMwwOmGf2Lan4O6QMBekIK8NTTJRoWdGcS0ItDdQPuMZueaV1zl Wu52YrzEulkZ2HRW4l1jtD7g4RFBnRJ2jp1slRuZKWEfHqEYPcbCSXP3i+U2Uk2aaY9/ laY8jMIPoyjehmpJtKe81lW1IPOThUa9DhV/7JHq4t1H7I7zxOUYSEll04KZzRpZyPvW VRBreOdnE456C58o9QOTcifEsxEmFFIN6QVATia041khNA7cthXaXVjVC96DEAtpUihB /nEpR6UQALcvf6txW+j9iUSgEGLkNXdc2kXjp/VtPSehTSzfNLLKcNDarD+KWsxvwOjZ Z9IA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z7si6983778pgf.116.2022.01.21.13.08.30; Fri, 21 Jan 2022 13:08:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358911AbiATHH7 (ORCPT + 99 others); Thu, 20 Jan 2022 02:07:59 -0500 Received: from mailgw01.mediatek.com ([60.244.123.138]:44794 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S238319AbiATHH6 (ORCPT ); Thu, 20 Jan 2022 02:07:58 -0500 X-UUID: 67f177b04d4f4e9dad3fc6fb74513b7a-20220120 X-UUID: 67f177b04d4f4e9dad3fc6fb74513b7a-20220120 Received: from mtkexhb01.mediatek.inc [(172.21.101.102)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1004976437; Thu, 20 Jan 2022 15:07:54 +0800 Received: from mtkexhb01.mediatek.inc (172.21.101.102) by mtkmbs10n1.mediatek.inc (172.21.101.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.792.15; Thu, 20 Jan 2022 15:07:54 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkexhb01.mediatek.inc (172.21.101.102) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 20 Jan 2022 15:07:53 +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 15:07:52 +0800 From: To: CC: , , , , , , , , , , , , , , , , , , , , , Guangming Subject: [PATCH v5] dma-buf: system_heap: Add a size check for allocation Date: Thu, 20 Jan 2022 15:08:50 +0800 Message-ID: <20220120070850.95495-1-guangming.cao@mediatek.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 --- 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..459dc18bc4a2 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 (len >> PAGE_SHIFT > totalram_pages()) + return ERR_PTR(-EINVAL); + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) return ERR_PTR(-ENOMEM); -- 2.17.1