Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1244253pxb; Fri, 21 Jan 2022 13:12:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKMOEDNo0SUXl+FQDfTSY45sX39lfrLvj8lyUjdgDAf8W9J8v/0ar+uiVVPL9cPHoAPUpQ X-Received: by 2002:aa7:888e:0:b0:4c4:3ad:9eee with SMTP id z14-20020aa7888e000000b004c403ad9eeemr5114847pfe.13.1642799569968; Fri, 21 Jan 2022 13:12:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642799569; cv=none; d=google.com; s=arc-20160816; b=Z2xNE9klB2xmsrHOd4+/SJhbxqTzS9SA/ImD5nyS6yoUEFlZbCTGZL0oMXw87A8Yic We3/bQHzLaOW20OrOM4FsyAiK+8H5f00RkoQnHQmA5Pu5dF2DxyseSvB9nSzEdykObtg ctdLah7wU+Z/eI1F5wIOUZt0ussA1wy2PmbKUNrS4bPBaWDahQwJiSeofK3o0VeZeicH f4siB4TFd7NtCKIIrYYJuXdsiQhraxX9wLZFOVRDHYXo6UQI7adUMIoux6ue/cOUuVa/ F+cAFT0JI7nSAT4fP1FMZ0aDHM6bBctGZJHYszNPe11HAMOwuLR8KJfShiV/mwfW1FcA paxw== 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=yqKw3cycAWdKrXDlMll7kewEtKu5ofsxrJPdCgYN6Fs=; b=V721vcdeGEXPHWDSNpKOg6rD8eUimAoWYIow8BdIinYnouocsayo55hvX6XiBFllXe muKOkGogFoMgzUg96y890lfF1njlDmYTOTupqqNd/mICO+c/mo5jOIP5tjtvOLwP9SNV lFEjp6/eyWIJH/a9/zo9UtS63avZ1Uom5CLPu5Uoey1xeQVWPD/FKwJs+o1Kfx8PpV8F +QZULIBmirY8cxWOx3UeyXfcHAnGfU9TC+V4rgh7eAxKZse7+Hq57W9GK8KFMio1bwI8 6hbphOFRDb7v1H4w27tF1x9Kj7puPY1WqESv8Al/RJhOODInrVlC/92eK7pPl661R37s xyag== 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 gl7si4288896pjb.5.2022.01.21.13.12.38; Fri, 21 Jan 2022 13:12:49 -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 S1359475AbiATIwI (ORCPT + 99 others); Thu, 20 Jan 2022 03:52:08 -0500 Received: from mailgw01.mediatek.com ([60.244.123.138]:50634 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S232683AbiATIwG (ORCPT ); Thu, 20 Jan 2022 03:52:06 -0500 X-UUID: c859b7443f714a1ea61a4b436e420731-20220120 X-UUID: c859b7443f714a1ea61a4b436e420731-20220120 Received: from mtkcas10.mediatek.inc [(172.21.101.39)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 748480370; Thu, 20 Jan 2022 16:52:02 +0800 Received: from mtkexhb02.mediatek.inc (172.21.101.103) 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 16:52:02 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkexhb02.mediatek.inc (172.21.101.103) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 20 Jan 2022 16:52:01 +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 16:52:00 +0800 From: To: CC: , , , , , , , , , , , , , , , , , , , , , Guangming Subject: [PATCH v6] dma-buf: system_heap: Add a size check for allocation Date: Thu, 20 Jan 2022 16:52:59 +0800 Message-ID: <20220120085259.98147-1-guangming.cao@mediatek.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <4f2631d3-0845-cae6-6fc0-9c00ce6301ce@amd.com> References: <4f2631d3-0845-cae6-6fc0-9c00ce6301ce@amd.com> 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..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); -- 2.17.1