Skip to contentSkip to conversation

iommu/sva: fix signedness bug in iommu_sva_alloc_pasid()

PATCH In review Dan Carpenter 5 participants 6 messages Apr 6, 2023 – Jul 14, 2023

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

PATCH 1/1 iommu/sva: fix signedness bug in iommu_sva_alloc_pasid()

The ida_alloc_range() function returns negative error codes on error. On success it returns values in the min to max range (inclusive). It never returns more then INT_MAX even if "max" is higher. It never returns values in the 0 to (min - 1) range.

The bug is that "min" is an unsigned int so negative error codes will be promoted to high positive values errors treated as success.

Fixes 1a14bf0fc7ed ("iommu/sva: Use GFP_KERNEL for pasid allocation")
---
 drivers/iommu/iommu-sva.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommu-sva.c +2 -1
index c4ab3c457fbc..6e4f1ba1c148 100644
@@ -33,8 +33,9 @@ static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t ma
 	}
 
 	ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_KERNEL);
-	if (ret < min)
+	if (ret < 0)
 		goto out;
+
 	mm->pasid = ret;
 	ret = 0;
 out:
Signature · 1 line
2.39.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Dan Carpenter

The ida_alloc_range() function returns negative error codes on error. On success it returns values in the min to max range (inclusive). It never returns more then INT_MAX even if "max" is higher. It never returns values in the 0 to (min - 1) range.

The bug is that "min" is an unsigned int so negative error codes will be promoted to high positive values errors treated as success.

Fixes 1a14bf0fc7ed ("iommu/sva: Use GFP_KERNEL for pasid allocation")

Best regards,
baolu

#2 lore raw
In reply to Dan Carpenter

Ping!

regards,
dan carpenter

Quoted 31 lines · Dan Carpenter

The ida_alloc_range() function returns negative error codes on error. On success it returns values in the min to max range (inclusive). It never returns more then INT_MAX even if "max" is higher. It never returns values in the 0 to (min - 1) range.

The bug is that "min" is an unsigned int so negative error codes will be promoted to high positive values errors treated as success.

Fixes 1a14bf0fc7ed ("iommu/sva: Use GFP_KERNEL for pasid allocation")
---
 drivers/iommu/iommu-sva.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommu-sva.c +2 -1
index c4ab3c457fbc..6e4f1ba1c148 100644
@@ -33,8 +33,9 @@ static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t ma
 	}
 
 	ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_KERNEL);
-	if (ret < min)
+	if (ret < 0)
 		goto out;
+
 	mm->pasid = ret;
 	ret = 0;
 out:
Signature · 1 line
2.39.1
maintainer
#3 lore raw
Quoted 1 line · Dan Carpenter

Ping!

Ah, Joerg isn't on CC. Fixing that now...

Will

Quoted 32 lines · Dan Carpenter
Quoted 31 lines

The ida_alloc_range() function returns negative error codes on error. On success it returns values in the min to max range (inclusive). It never returns more then INT_MAX even if "max" is higher. It never returns values in the 0 to (min - 1) range.

The bug is that "min" is an unsigned int so negative error codes will be promoted to high positive values errors treated as success.

Fixes 1a14bf0fc7ed ("iommu/sva: Use GFP_KERNEL for pasid allocation")
---
 drivers/iommu/iommu-sva.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/iommu-sva.c +2 -1
index c4ab3c457fbc..6e4f1ba1c148 100644
@@ -33,8 +33,9 @@ static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t ma
 	}
 
 	ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_KERNEL);
-	if (ret < min)
+	if (ret < 0)
 		goto out;
+
 	mm->pasid = ret;
 	ret = 0;
 out:
Signature · 1 line
2.39.1
#4 lore raw
Quoted 5 lines · Will Deacon
Quoted 1 line · Dan Carpenter

Ping!

Ah, Joerg isn't on CC. Fixing that now...

Sorry! I use a script to send patches and for a while if the patch had a Fixes tag then my script would remove the top name listed from get_maintainer.pl. Doh!

I should probably resend this?

regards,
dan carpenter

maintainer
#5 lore raw
Quoted 1 line · Dan Carpenter

I should probably resend this?

No, just applied it, thanks.