Skip to contentSkip to conversation

gpu: host1x: Fix passing zero to ERR_PTR in host1x_iommu_attach()

PATCH In review Alper Ak 2 participants 2 messages Feb 9 – Feb 13

Subsystems (4)
lore
Share & tools
lore

Alper Ak

author
#0 lore raw

PATCH 1/1 gpu: host1x: Fix passing zero to ERR_PTR in host1x_iommu_attach()

When iommu_attach_group() returns -ENODEV, the code sets err to 0 but still falls through to the error path, returning ERR_PTR(0).

Returning ERR_PTR(0) evaluates to NULL and breaks the ERR_PTR/IS_ERR contract, causing the error to be silently ignored and potentially leading to NULL pointer dereferences by callers.

Fix this by returning NULL when err is zero, and ERR_PTR(err) only for actual error codes.

This issue was reported by the Smatch static analyzer.

Fixes 06867a362de0 ("gpu: host1x: Set DMA mask based on IOMMU setup")
---
 drivers/gpu/host1x/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/host1x/dev.c +1 -1
index 3f475f0e6545..46a570b861ac 100644
@@ -450,7 +450,7 @@ static struct iommu_domain *host1x_iommu_attach(struct host1x *host)
 	iommu_group_put(host->group);
 	host->group = NULL;
 
-	return ERR_PTR(err);
+	return err ? ERR_PTR(err) : NULL;
 }
 
 static int host1x_iommu_init(struct host1x *host)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 9 lines · Alper Ak

When iommu_attach_group() returns -ENODEV, the code sets err to 0 but still falls through to the error path, returning ERR_PTR(0).

Returning ERR_PTR(0) evaluates to NULL and breaks the ERR_PTR/IS_ERR contract, causing the error to be silently ignored and potentially leading to NULL pointer dereferences by callers.

Fix this by returning NULL when err is zero, and ERR_PTR(err) only for actual error codes.

This commit message doesn't make sense. First you say that the function has a bug because ERR_PTR(0) evaluates to NULL and can cause problems when callers don't handle NULL. Then you fix that by returning NULL explicitly.

While returning NULL through ERR_PTR(0) may not be very beautiful, this function is defined to return NULL in certain cases and so the behavior is correct.

Mikko

Quoted 26 lines

This issue was reported by the Smatch static analyzer.

Fixes 06867a362de0 ("gpu: host1x: Set DMA mask based on IOMMU setup")
---
 drivers/gpu/host1x/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/host1x/dev.c +1 -1
index 3f475f0e6545..46a570b861ac 100644
@@ -450,7 +450,7 @@ static struct iommu_domain *host1x_iommu_attach(struct host1x *host)
 	iommu_group_put(host->group);
 	host->group = NULL;
 
-	return ERR_PTR(err);
+	return err ? ERR_PTR(err) : NULL;
 }
 
 static int host1x_iommu_init(struct host1x *host)
Signature · 1 line
2.43.0