iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>, Sven Peter <sven@kernel.org>,
	Janne Grunau <j@jannau.net>,
	Rob Clark <robin.clark@oss.qualcomm.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Yong Wu <yong.wu@mediatek.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	Chen-Yu Tsai <wens@csie.org>,
	Thierry Reding <thierry.reding@gmail.com>,
	Krishna Reddy <vdumpa@nvidia.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	Johan Hovold <johan@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH v3 06/14] iommu/mediatek: fix use-after-free on probe deferral
Date: Mon, 20 Oct 2025 06:53:10 +0200	[thread overview]
Message-ID: <20251020045318.30690-7-johan@kernel.org> (raw)
In-Reply-To: <20251020045318.30690-1-johan@kernel.org>

The driver is dropping the references taken to the larb devices during
probe after successful lookup as well as on errors. This can
potentially lead to a use-after-free in case a larb device has not yet
been bound to its driver so that the iommu driver probe defers.

Fix this by keeping the references as expected while the iommu driver is
bound.

Fixes: 26593928564c ("iommu/mediatek: Add error path for loop of mm_dts_parse")
Cc: stable@vger.kernel.org
Cc: Yong Wu <yong.wu@mediatek.com>
Acked-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/iommu/mtk_iommu.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index 8d8e85186188..64ce041238fd 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -1213,16 +1213,19 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
 		}
 
 		component_match_add(dev, match, component_compare_dev, &plarbdev->dev);
-		platform_device_put(plarbdev);
 	}
 
-	if (!frst_avail_smicomm_node)
-		return -EINVAL;
+	if (!frst_avail_smicomm_node) {
+		ret = -EINVAL;
+		goto err_larbdev_put;
+	}
 
 	pcommdev = of_find_device_by_node(frst_avail_smicomm_node);
 	of_node_put(frst_avail_smicomm_node);
-	if (!pcommdev)
-		return -ENODEV;
+	if (!pcommdev) {
+		ret = -ENODEV;
+		goto err_larbdev_put;
+	}
 	data->smicomm_dev = &pcommdev->dev;
 
 	link = device_link_add(data->smicomm_dev, dev,
@@ -1230,7 +1233,8 @@ static int mtk_iommu_mm_dts_parse(struct device *dev, struct component_match **m
 	platform_device_put(pcommdev);
 	if (!link) {
 		dev_err(dev, "Unable to link %s.\n", dev_name(data->smicomm_dev));
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err_larbdev_put;
 	}
 	return 0;
 
@@ -1402,8 +1406,12 @@ static int mtk_iommu_probe(struct platform_device *pdev)
 	iommu_device_sysfs_remove(&data->iommu);
 out_list_del:
 	list_del(&data->list);
-	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))
+	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
 		device_link_remove(data->smicomm_dev, dev);
+
+		for (i = 0; i < MTK_LARB_NR_MAX; i++)
+			put_device(data->larb_imu[i].dev);
+	}
 out_runtime_disable:
 	pm_runtime_disable(dev);
 	return ret;
@@ -1423,6 +1431,9 @@ static void mtk_iommu_remove(struct platform_device *pdev)
 	if (MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM)) {
 		device_link_remove(data->smicomm_dev, &pdev->dev);
 		component_master_del(&pdev->dev, &mtk_iommu_com_ops);
+
+		for (i = 0; i < MTK_LARB_NR_MAX; i++)
+			put_device(data->larb_imu[i].dev);
 	}
 	pm_runtime_disable(&pdev->dev);
 	for (i = 0; i < data->plat_data->banks_num; i++) {
-- 
2.49.1


  parent reply	other threads:[~2025-10-20  4:56 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-20  4:53 [PATCH v3 00/14] iommu: fix device leaks Johan Hovold
2025-10-20  4:53 ` [PATCH v3 01/14] iommu/apple-dart: fix device leak on of_xlate() Johan Hovold
2025-10-20  4:53 ` [PATCH v3 02/14] iommu/qcom: " Johan Hovold
2025-10-20  4:53 ` [PATCH v3 03/14] iommu/exynos: " Johan Hovold
2025-10-20  4:53 ` [PATCH v3 04/14] iommu/ipmmu-vmsa: " Johan Hovold
2025-10-20  4:53 ` [PATCH v3 05/14] iommu/mediatek: " Johan Hovold
2025-10-20 10:35   ` AngeloGioacchino Del Regno
2025-10-20  4:53 ` Johan Hovold [this message]
2025-10-20 10:35   ` [PATCH v3 06/14] iommu/mediatek: fix use-after-free on probe deferral AngeloGioacchino Del Regno
2025-10-25  9:18   ` Yong Wu (吴勇)
2025-10-20  4:53 ` [PATCH v3 07/14] iommu/mediatek: simplify dt parsing error handling Johan Hovold
2025-10-20 10:35   ` AngeloGioacchino Del Regno
2025-10-25  9:18   ` Yong Wu (吴勇)
2025-10-20  4:53 ` [PATCH v3 08/14] iommu/mediatek-v1: fix device leak on probe_device() Johan Hovold
2025-10-20 10:35   ` AngeloGioacchino Del Regno
2025-10-20  4:53 ` [PATCH v3 09/14] iommu/mediatek-v1: fix device leaks on probe() Johan Hovold
2025-10-20 10:35   ` AngeloGioacchino Del Regno
2025-10-20  4:53 ` [PATCH v3 10/14] iommu/mediatek-v1: add missing larb count sanity check Johan Hovold
2025-10-20 10:35   ` AngeloGioacchino Del Regno
2025-10-20  4:53 ` [PATCH v3 11/14] iommu/omap: fix device leaks on probe_device() Johan Hovold
2025-10-20  4:53 ` [PATCH v3 12/14] iommu/omap: simplify probe_device() error handling Johan Hovold
2025-10-20  4:53 ` [PATCH v3 13/14] iommu/sun50i: fix device leak on of_xlate() Johan Hovold
2025-10-20  4:53 ` [PATCH v3 14/14] iommu/tegra: fix device leak on probe_device() Johan Hovold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251020045318.30690-7-johan@kernel.org \
    --to=johan@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=iommu@lists.linux.dev \
    --cc=j@jannau.net \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=matthias.bgg@gmail.com \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=robin.murphy@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=sven@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=vdumpa@nvidia.com \
    --cc=wens@csie.org \
    --cc=will@kernel.org \
    --cc=yong.wu@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).