Skip to contentSkip to conversation

drm/virtio: fix 0-byte blob ZERO_SIZE_PTR, GEM leak, and context_init rollback

PATCH In review Hui Peng 3 participants 3 messages Sep 19 – Sep 20

Subsystems (4)
lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 drm/virtio: fix 0-byte blob ZERO_SIZE_PTR, GEM leak, and context_init rollback

Fix multiple issues in virtio-gpu VRAM mapping, blob creation, and context initialization:

1. In virtio_gpu_vram_mmap(), set vram->map_state = STATE_ERR if deferred virtio_gpu_vram_map() fails so callers do not treat an unmapped VRAM node as valid.
2. In verify_blob(), reject rc_blob->size == 0 or size values that wrap PAGE_ALIGN() to prevent kvmalloc_array(0, ...) from returning ZERO_SIZE_PTR.
3. In virtio_gpu_gem_create(), virtio_gpu_resource_create_ioctl(), and virtio_gpu_resource_create_blob_ioctl(), use drm_gem_object_put() instead of drm_gem_object_release() on error so the BO's free callback is properly invoked.
4. In virtio_gpu_context_init_ioctl(), stage context parameters into local variables and only commit them to vfpriv after all parameters and ring_idx_mask have been validated.

Fixes 16845c5d5409 ("drm/virtio: implement blob resources: implement vram object")
Fixes 85c83ea915ed ("drm/virtio: implement context init: allocate an array of fence contexts")
Assisted-by LLM

---

drivers/gpu/drm/virtio/virtgpu_gem.c +1 -1
index 66c3f6f74e9c..d2f0b8a3f172 100644
@@ -45,7 +45,7 @@ static int virtio_gpu_gem_create(struct drm_file *file,
 
 	ret = drm_gem_handle_create(file, &obj->base.base, &handle);
 	if (ret) {
-		drm_gem_object_release(&obj->base.base);
+		drm_gem_object_put(&obj->base.base);
 		return ret;
 	}
 
drivers/gpu/drm/virtio/virtgpu_ioctl.c +40 -16

136 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_vram.c +5 -2
index 5b4a3ab81cd5..aaeae38afc0e 100644
@@ -248,7 +248,10 @@ void virtio_gpu_vram_map_deferred(struct virtio_gpu_object_vram *vram)
 		return;
 
 	mutex_lock(&map_lock);
-	if (!drm_mm_node_allocated(&vram->vram_node))
-		virtio_gpu_vram_map(&vram->base);
+	if (!drm_mm_node_allocated(&vram->vram_node)) {
+		vram->map_state = STATE_INITIALIZING;
+		if (virtio_gpu_vram_map(&vram->base))
+			vram->map_state = STATE_ERR;
+	}
 	mutex_unlock(&map_lock);
 }
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Fix multiple issues in virtio-gpu VRAM mapping, blob creation, and context initialization:

1. In virtio_gpu_vram_mmap(), set vram->map_state = STATE_ERR if deferred virtio_gpu_vram_map() fails so callers do not treat an unmapped VRAM node as valid.
2. In verify_blob(), reject rc_blob->size == 0 or size values that wrap PAGE_ALIGN() to prevent kvmalloc_array(0, ...) from returning ZERO_SIZE_PTR.
3. In virtio_gpu_gem_create(), virtio_gpu_resource_create_ioctl(), and virtio_gpu_resource_create_blob_ioctl(), use drm_gem_object_put() instead of drm_gem_object_release() on error so the BO's free callback is properly invoked.
4. In virtio_gpu_context_init_ioctl(), stage context parameters into local variables and only commit them to vfpriv after all parameters and ring_idx_mask have been validated.

Quoted 4 lines
Fixes 16845c5d5409 ("drm/virtio: implement blob resources: implement vram object")
Fixes 85c83ea915ed ("drm/virtio: implement context init: allocate an array of fence contexts")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Hui Peng
Quoted 17 lines · Hui Peng

Fix multiple issues in virtio-gpu VRAM mapping, blob creation, and context initialization:

1. In virtio_gpu_vram_mmap(), set vram->map_state = STATE_ERR if deferred virtio_gpu_vram_map() fails so callers do not treat an unmapped VRAM node as valid.
2. In verify_blob(), reject rc_blob->size == 0 or size values that wrap PAGE_ALIGN() to prevent kvmalloc_array(0, ...) from returning ZERO_SIZE_PTR.
3. In virtio_gpu_gem_create(), virtio_gpu_resource_create_ioctl(), and virtio_gpu_resource_create_blob_ioctl(), use drm_gem_object_put() instead of drm_gem_object_release() on error so the BO's free callback is properly invoked.
4. In virtio_gpu_context_init_ioctl(), stage context parameters into local variables and only commit them to vfpriv after all parameters and ring_idx_mask have been validated.

Quoted 5 lines
Fixes 16845c5d5409 ("drm/virtio: implement blob resources: implement vram object")
Fixes 85c83ea915ed ("drm/virtio: implement context init: allocate an array of fence contexts")
Assisted-by LLM

---

Please split this patch to have one fix per one patch and make a v2. The same for your other patch.

Signature · 2 lines
Best regards,
Dmitry