Skip to contentSkip to conversation

iommu/rockchip: Check of_find_device_by_node() return value

PATCH In review Haotian Zhang 2 participants 2 messages Dec 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/rockchip: Check of_find_device_by_node() return value

rk_iommu_of_xlate() calls of_find_device_by_node() without verifying it is non-NULL, which can lead to a NULL pointer dereference and crash.

Add a check for a NULL return from of_find_device_by_node() and return -ENODEV upon failure.

Fixes 5fd577c3eac3 ("iommu/rockchip: Use OF_IOMMU to attach devices automatically")
---
 drivers/iommu/rockchip-iommu.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iommu/rockchip-iommu.c +2 -0
index 0861dd469bd8..e59d0617574b 100644
@@ -1153,6 +1153,8 @@ static int rk_iommu_of_xlate(struct device *dev,
 	struct rk_iommudata *data;
 
 	iommu_dev = of_find_device_by_node(args->np);
+	if (!iommu_dev)
+		return -ENODEV;
 
 	data = devm_kzalloc(&iommu_dev->dev, sizeof(*data), GFP_KERNEL);
 	if (!data)
Signature · 1 line
2.50.1.windows.1
reviewer
#1 lore raw
Quoted 3 lines · Haotian Zhang

rk_iommu_of_xlate() calls of_find_device_by_node() without verifying it is non-NULL, which can lead to a NULL pointer dereference and crash.

No, it can't. Please look at how of_xlate is called - at this point we are looking up the IOMMU device, which has a driver bound to it, which has registered ops against this of_node that are the only way to reach this function in the first place. If the device could have somehow disappeared from underneath an active driver, this potential crash is the least of the kernel's worries...

Thanks,
Robin.

Quoted 22 lines

Add a check for a NULL return from of_find_device_by_node() and return -ENODEV upon failure.

Fixes 5fd577c3eac3 ("iommu/rockchip: Use OF_IOMMU to attach devices automatically")
---
  drivers/iommu/rockchip-iommu.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/iommu/rockchip-iommu.c +2 -0
index 0861dd469bd8..e59d0617574b 100644
@@ -1153,6 +1153,8 @@ static int rk_iommu_of_xlate(struct device *dev,
  	struct rk_iommudata *data;
 
  	iommu_dev = of_find_device_by_node(args->np);
+	if (!iommu_dev)
+		return -ENODEV;
 
  	data = devm_kzalloc(&iommu_dev->dev, sizeof(*data), GFP_KERNEL);
  	if (!data)