[v7,4/6] iommu/mediatek: Validate number of phandles associated with "mediatek,larbs"
Message ID | 20221018024258.19073-5-yong.wu@mediatek.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1746631wrs; Mon, 17 Oct 2022 19:51:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7PZmcLruEF0V+JcLI30W0wg9+26+WAw2rnZdQPOlEpBZqTeRuwbV5SSMLrCZKtcJhV305Y X-Received: by 2002:a17:907:9729:b0:78d:9fb4:16e0 with SMTP id jg41-20020a170907972900b0078d9fb416e0mr565442ejc.455.1666061498906; Mon, 17 Oct 2022 19:51:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666061498; cv=none; d=google.com; s=arc-20160816; b=mcQNVfHFPyoxjwNgQa49nHGnAThJT+nsZ2TlaGXeiScEN0cX34BCjoYu0rZTXBR7U1 qiirVK5zpWyUecKDbOA0SM+BAQB8Akhs5GbZNAnrWKV/TBsIxt33g2qtg2YPdvzZ9fu8 B2sKJuBHALlp8RFfb8zdC+rDASGRrYHbXAP4xUwZ7q6hMhMyIL9DA9ZJ5oTAiFdYiivz Zd99ttDpO48aQpTso/lmTlDMhKF1X6u7mMJF5JnJCSdW4/gR3nZCi6oTNo/sZcxHXuFV tigsRkcQVrAkZuvz20EuuvNap9yuYTMbOggJrKbcvKETeDvehx/JI2M7+U1Jcjuta7xx MgYQ== 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:dkim-signature; bh=KNetZl/o2JnPeD3E+CZiSVznpIXZE73PG4SwXrc1kJQ=; b=PrQd6bhfseMqugQWK9hTa9padJblyxbuIA00ceE3oIv84tTmovlAz+cF6gFgjuvRGz 5Uut6UaHyYXdDzh0h/HP8XyTMfwTVuWhPojIYrfKsISr+PS26exx5LgzJPGjkZJyuqFL eBGDaf1+7KgPnkyei7i4W9deFqCJd2i8cHljMEB5j8ekZ1+SgGogZY2qyBOBBJkv4fBY qFR2cwgp0yA8Px8NPwT8NWGy0ZIimUYblKgmv5ZM4nLiLSSzTSPodCz2EWS6uXkVPDII LONCDyomSp27MntBl9raJTNh9nFG+/+DoZkLCmyaee3Eqs+YTYcyl+ZyvS+C5UKwDKqm rRag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=gw07ymVe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw2-20020a056402228200b0045a0e39062esi9964964edb.76.2022.10.17.19.51.14; Mon, 17 Oct 2022 19:51:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=gw07ymVe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230525AbiJRCoH (ORCPT <rfc822;carlos.wei.hk@gmail.com> + 99 others); Mon, 17 Oct 2022 22:44:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231218AbiJRCnu (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 17 Oct 2022 22:43:50 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9099795E7E for <linux-kernel@vger.kernel.org>; Mon, 17 Oct 2022 19:43:47 -0700 (PDT) X-UUID: 0342d1e47c5142f393f18d683981a361-20221018 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=KNetZl/o2JnPeD3E+CZiSVznpIXZE73PG4SwXrc1kJQ=; b=gw07ymVeq2l7OIDajuf1tEFx9DSA+F7GWo5Ah+ghNPDLOzFDvkaa9b0KKa8phHMf+UV6iUMgsN6J2Ho9dubC3xWk/+SqYzGPMMirg5TLYpLWFhDWVa4eePzK+L3hT3oVWampBkQtgZHcQ0tRyNvyNt3BQgT1D+uni/FJZci0ZbM=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.11,REQID:ecf00dfc-66a4-4593-9543-3e95ce17ba79,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.11,REQID:ecf00dfc-66a4-4593-9543-3e95ce17ba79,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:39a5ff1,CLOUDID:f7ef18a4-ebb2-41a8-a87c-97702aaf2e20,B ulkID:221018104344NXHIJVHW,BulkQuantity:0,Recheck:0,SF:38|28|17|19|48,TC:n il,Content:0,EDM:-3,IP:nil,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 X-UUID: 0342d1e47c5142f393f18d683981a361-20221018 Received: from mtkcas10.mediatek.inc [(172.21.101.39)] by mailgw01.mediatek.com (envelope-from <yong.wu@mediatek.com>) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1367881281; Tue, 18 Oct 2022 10:43:42 +0800 Received: from mtkmbs11n1.mediatek.inc (172.21.101.186) by mtkmbs13n1.mediatek.inc (172.21.101.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.792.15; Tue, 18 Oct 2022 10:43:41 +0800 Received: from localhost.localdomain (10.17.3.154) by mtkmbs11n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.792.15 via Frontend Transport; Tue, 18 Oct 2022 10:43:40 +0800 From: Yong Wu <yong.wu@mediatek.com> To: Joerg Roedel <joro@8bytes.org>, Matthias Brugger <matthias.bgg@gmail.com>, Will Deacon <will@kernel.org> CC: Robin Murphy <robin.murphy@arm.com>, <iommu@lists.linux.dev>, <linux-mediatek@lists.infradead.org>, <linux-arm-kernel@lists.infradead.org>, <linux-kernel@vger.kernel.org>, Yong Wu <yong.wu@mediatek.com>, AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>, <mingyuan.ma@mediatek.com>, <yf.wang@mediatek.com>, <libo.kang@mediatek.com>, <chengci.xu@mediatek.com>, <youlin.pei@mediatek.com>, <anan.sun@mediatek.com>, <xueqi.zhang@mediatek.com>, Guenter Roeck <groeck@chromium.org>, Dan Carpenter <dan.carpenter@oracle.com> Subject: [PATCH v7 4/6] iommu/mediatek: Validate number of phandles associated with "mediatek,larbs" Date: Tue, 18 Oct 2022 10:42:56 +0800 Message-ID: <20221018024258.19073-5-yong.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20221018024258.19073-1-yong.wu@mediatek.com> References: <20221018024258.19073-1-yong.wu@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746992102251931495?= X-GMAIL-MSGID: =?utf-8?q?1746992102251931495?= |
Series |
iommu/mediatek: Improve safety from invalid dts input
|
|
Commit Message
Yong Wu
Oct. 18, 2022, 2:42 a.m. UTC
From: Guenter Roeck <groeck@chromium.org> Fix the smatch warnings: drivers/iommu/mtk_iommu.c:878 mtk_iommu_mm_dts_parse() error: uninitialized symbol 'larbnode'. If someone abuse the dtsi node(Don't follow the definition of dt-binding), for example "mediatek,larbs" is provided as boolean property, "larb_nr" will be zero and cause abnormal. To fix this problem and improve the code safety, add some checking for the invalid input from dtsi, e.g. checking the larb_nr/larbid valid range, and avoid "mediatek,larb-id" property conflicts in the smi-larb nodes. Fixes: d2e9a1102cfc ("iommu/mediatek: Contain MM IOMMU flow with the MM TYPE") Reported-by: kernel test robot <lkp@intel.com> Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Guenter Roeck <groeck@chromium.org> Signed-off-by: Yong Wu <yong.wu@mediatek.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> --- drivers/iommu/mtk_iommu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Comments
On 18/10/2022 04:42, Yong Wu wrote: > From: Guenter Roeck <groeck@chromium.org> > > Fix the smatch warnings: > drivers/iommu/mtk_iommu.c:878 mtk_iommu_mm_dts_parse() error: uninitialized > symbol 'larbnode'. > > If someone abuse the dtsi node(Don't follow the definition of dt-binding), > for example "mediatek,larbs" is provided as boolean property, "larb_nr" > will be zero and cause abnormal. > > To fix this problem and improve the code safety, add some checking > for the invalid input from dtsi, e.g. checking the larb_nr/larbid valid > range, and avoid "mediatek,larb-id" property conflicts in the smi-larb > nodes. > > Fixes: d2e9a1102cfc ("iommu/mediatek: Contain MM IOMMU flow with the MM TYPE") > Reported-by: kernel test robot <lkp@intel.com> > Reported-by: Dan Carpenter <dan.carpenter@oracle.com> > Signed-off-by: Guenter Roeck <groeck@chromium.org> > Signed-off-by: Yong Wu <yong.wu@mediatek.com> > Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com> > --- > drivers/iommu/mtk_iommu.c | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c > index 912322494bc0..9cbff48f03c0 100644 > --- a/drivers/iommu/mtk_iommu.c > +++ b/drivers/iommu/mtk_iommu.c > @@ -1050,6 +1050,8 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m > larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL); > if (larb_nr < 0) > return larb_nr; > + if (larb_nr == 0 || larb_nr > MTK_LARB_NR_MAX) > + return -EINVAL; > > for (i = 0; i < larb_nr; i++) { > u32 id; > @@ -1068,6 +1070,11 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m > ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id); > if (ret)/* The id is consecutive if there is no this property */ > id = i; > + if (id >= MTK_LARB_NR_MAX) { > + of_node_put(larbnode); > + ret = -EINVAL; > + goto err_larbdev_put; > + } > > plarbdev = of_find_device_by_node(larbnode); > of_node_put(larbnode); > @@ -1075,6 +1082,11 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m > ret = -ENODEV; > goto err_larbdev_put; > } > + if (data->larb_imu[id].dev) { > + platform_device_put(plarbdev); > + ret = -EEXIST; > + goto err_larbdev_put; > + } > data->larb_imu[id].dev = &plarbdev->dev; > > if (!plarbdev->dev.driver) {
diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c index 912322494bc0..9cbff48f03c0 100644 --- a/drivers/iommu/mtk_iommu.c +++ b/drivers/iommu/mtk_iommu.c @@ -1050,6 +1050,8 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m larb_nr = of_count_phandle_with_args(dev->of_node, "mediatek,larbs", NULL); if (larb_nr < 0) return larb_nr; + if (larb_nr == 0 || larb_nr > MTK_LARB_NR_MAX) + return -EINVAL; for (i = 0; i < larb_nr; i++) { u32 id; @@ -1068,6 +1070,11 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id); if (ret)/* The id is consecutive if there is no this property */ id = i; + if (id >= MTK_LARB_NR_MAX) { + of_node_put(larbnode); + ret = -EINVAL; + goto err_larbdev_put; + } plarbdev = of_find_device_by_node(larbnode); of_node_put(larbnode); @@ -1075,6 +1082,11 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m ret = -ENODEV; goto err_larbdev_put; } + if (data->larb_imu[id].dev) { + platform_device_put(plarbdev); + ret = -EEXIST; + goto err_larbdev_put; + } data->larb_imu[id].dev = &plarbdev->dev; if (!plarbdev->dev.driver) {