Skip to contentSkip to conversation

drm/virtio: fix undefined behavior in capset shift calculation

PATCH In review Alexey Simakov 2 participants 2 messages Dec 8, 2025 – Dec 11, 2025

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

PATCH 1/1 drm/virtio: fix undefined behavior in capset shift calculation

According to MAX_CAPSET_ID, possible values of vgdev->capsets[i].id range from 0 to 63. Since the intermediate calculation uses type int, shifting by values larger than the bit width of int is undefined behavior as per the C language standard.

Explicitly cast the left-shift operand to ULL to prevent undefined behavior during the calculation.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 1925d6a7e0f4 ("drm/virtio: implement context init: track valid capabilities in a mask")
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -1
index 1c15cbf326b7..2e0ec7590ac2 100644
@@ -104,7 +104,7 @@ static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
 			return;
 		}
 
-		vgdev->capset_id_mask |= 1 << vgdev->capsets[i].id;
+		vgdev->capset_id_mask |= 1ULL << vgdev->capsets[i].id;
 		DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
 			 i, vgdev->capsets[i].id,
 			 vgdev->capsets[i].max_version,
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Alexey Simakov

According to MAX_CAPSET_ID, possible values of vgdev->capsets[i].id range from 0 to 63. Since the intermediate calculation uses type int, shifting by values larger than the bit width of int is undefined behavior as per the C language standard.

Explicitly cast the left-shift operand to ULL to prevent undefined behavior during the calculation.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes 1925d6a7e0f4 ("drm/virtio: implement context init: track valid capabilities in a mask")
---
 drivers/gpu/drm/virtio/virtgpu_kms.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_kms.c +0 -0
index 1c15cbf326b7..2e0ec7590ac2 100644

drivers/gpu/drm/virtio/virtgpu_kms.c static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev, · line 110

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_kms.c +1 -1
@@ -104,7 +104,7 @@ static void virtio_gpu_get_capsets(struct virtio_gpu_device *vgdev,
 			return;
 		}
 
-		vgdev->capset_id_mask |= 1 << vgdev->capsets[i].id;
+		vgdev->capset_id_mask |= 1ULL << vgdev->capsets[i].id;
 		DRM_INFO("cap set %d: id %d, max-version %d, max-size %d\n",
 			 i, vgdev->capsets[i].id,
 			 vgdev->capsets[i].max_version,
Signature · 2 lines
Best regards,
Dmitry