Skip to contentSkip to conversation

iommu: Fix shift-out-of-bounds in iova_bitmap_offset_to_index()

PATCH Superseded Qasim Ijaz 3 participants 4 messages Jan 12, 2025 – Jan 13, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 iommu: Fix shift-out-of-bounds in iova_bitmap_offset_to_index()

This patch resolves a UBSAN shift-out-of-bounds issue in iova_bitmap_offset_to_index() where shifting the constant "1" (of type int) by bitmap->mapped.pgshift (an unsigned long value) could result in undefined behavior.

The constant "1" defaults to a 32-bit "int", and when "pgshift" exceeds 31 (e.g., pgshift = 63) the shift operation overflows, as the result cannot be represented in a 32-bit type.

To resolve this, the constant is updated to "1UL", promoting it to an unsigned long type to match the operand's type.

---
 drivers/iommu/iommufd/iova_bitmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iommufd/iova_bitmap.c +1 -1
index ab665cf38ef4..39a86a4a1d3a 100644
@@ -130,7 +130,7 @@ struct iova_bitmap {
 static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
 						 unsigned long iova)
 {
-	unsigned long pgsize = 1 << bitmap->mapped.pgshift;
+	unsigned long pgsize = 1UL << bitmap->mapped.pgshift;
 
 	return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
 }
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Qasim Ijaz

This patch resolves a UBSAN shift-out-of-bounds issue in

Avoid the 'this patch' e.g. Resolve a UBSAN shift-out-of-bonds (...)

The Subject component part could also be a bit more specific e.g.

iommufd/iova_bitmap: Fix shift-out-of-bounds in iova_bitmap_offset_to_index()

Quoted 6 lines

iova_bitmap_offset_to_index() where shifting the constant "1" (of type int) by bitmap->mapped.pgshift (an unsigned long value) could result in undefined behavior.

The constant "1" defaults to a 32-bit "int", and when "pgshift" exceeds 31 (e.g., pgshift = 63) the shift operation overflows, as the result cannot be represented in a 32-bit type.

Quoted 6 lines

To resolve this, the constant is updated to "1UL", promoting it to an unsigned long type to match the operand's type.

With those two nits:

Quoted 17 lines
---
 drivers/iommu/iommufd/iova_bitmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/iommufd/iova_bitmap.c +1 -1
index ab665cf38ef4..39a86a4a1d3a 100644
@@ -130,7 +130,7 @@ struct iova_bitmap {
 static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
 						 unsigned long iova)
 {
-	unsigned long pgsize = 1 << bitmap->mapped.pgshift;
+	unsigned long pgsize = 1UL << bitmap->mapped.pgshift;
 
 	return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
 }
#2 lore raw
Quoted 18 lines · Joao Martins
Quoted 1 line · Qasim Ijaz

This patch resolves a UBSAN shift-out-of-bounds issue in

Avoid the 'this patch' e.g. Resolve a UBSAN shift-out-of-bonds (...)

The Subject component part could also be a bit more specific e.g.

iommufd/iova_bitmap: Fix shift-out-of-bounds in iova_bitmap_offset_to_index()

Quoted 9 lines

iova_bitmap_offset_to_index() where shifting the constant "1" (of type int) by bitmap->mapped.pgshift (an unsigned long value) could result in undefined behavior.

The constant "1" defaults to a 32-bit "int", and when "pgshift" exceeds 31 (e.g., pgshift = 63) the shift operation overflows, as the result cannot be represented in a 32-bit type.

To resolve this, the constant is updated to "1UL", promoting it to an unsigned long type to match the operand's type.

Quoted 7 lines
Quoted 3 lines

With those two nits:

It needs a fixes line too

Jason

#3 lore raw
Quoted 23 lines · Jason Gunthorpe
Quoted 22 lines · Joao Martins
Quoted 1 line · Qasim Ijaz

This patch resolves a UBSAN shift-out-of-bounds issue in

Avoid the 'this patch' e.g. Resolve a UBSAN shift-out-of-bonds (...)

The Subject component part could also be a bit more specific e.g.

iommufd/iova_bitmap: Fix shift-out-of-bounds in iova_bitmap_offset_to_index()

Quoted 12 lines

iova_bitmap_offset_to_index() where shifting the constant "1" (of type int) by bitmap->mapped.pgshift (an unsigned long value) could result in undefined behavior.

The constant "1" defaults to a 32-bit "int", and when "pgshift" exceeds 31 (e.g., pgshift = 63) the shift operation overflows, as the result cannot be represented in a 32-bit type.

To resolve this, the constant is updated to "1UL", promoting it to an unsigned long type to match the operand's type.

Quoted 5 lines
Quoted 3 lines

With those two nits:

It needs a fixes line too

It should be

Fixes 495c06d82ba ("vfio: Add an IOVA bitmap support")

Joao