Skip to contentSkip to conversation

drm/qxl: fix vmalloc OOB write, surface size overflow, and BO reloc leaks

PATCH New Hui Peng 2 participants 2 messages Sep 19

Subsystems (4)
lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 drm/qxl: fix vmalloc OOB write, surface size overflow, and BO reloc leaks

Fix multiple memory safety and resource management bugs in the QXL ioctl and buffer object paths:

1. In qxl_bo_kmap_atomic_page(), page_offset is already a byte offset (reloc_info->dst_offset & PAGE_MASK), yet the fallback kptr and ttm_bo_vmap paths multiply page_offset by PAGE_SIZE a second time, causing a +16 MiB out-of-bounds kernel vmalloc write when applying relocations in qxl_process_single_command(). Add page_offset directly and validate reloc.dst_offset against dst_bo->tbo.base.size and cmd->command_size.
2. In qxl_alloc_surf_ioctl(), param->stride * param->height is computed using 32-bit signed arithmetic and param->stride == INT_MIN overflows on negation, allowing a 4 GiB surface to wrap to a 4 KiB GEM BO. Use check_mul_overflow() and check_add_overflow() with size_t.
3. In qxl_process_single_command(), prevent overwriting the union qxl_release_info header at offset 0 of cmd_bo, and reserve/unreserve non-command dst_bo buffers around apply_reloc()/apply_surf_reloc().
4. In qxl_bo_create(), reject size == 0 or size > ULONG_MAX - PAGE_SIZE + 1 before roundup(), and in qxl_bo_check_id(), deallocate bo->surface_id if qxl_hw_surface_alloc() fails.

Fixes f64122c1f6ad ("drm: add new QXL driver. (v1.4)")
Assisted-by LLM

---

drivers/gpu/drm/qxl/qxl_drv.h +1 -1
index cc02b5f10ad9..4978208ce80b 100644
@@ -297,7 +297,7 @@ int qxl_destroy_monitors_object(struct qxl_device *qdev);
 /* qxl_gem.c */
 void qxl_gem_init(struct qxl_device *qdev);
 void qxl_gem_fini(struct qxl_device *qdev);
-int qxl_gem_object_create(struct qxl_device *qdev, int size,
+int qxl_gem_object_create(struct qxl_device *qdev, size_t size,
 			  int alignment, int initial_domain,
 			  bool discardable, bool kernel,
 			  struct qxl_surface *surf,
drivers/gpu/drm/qxl/qxl_gem.c +2 -2
index 4939b57a2a48..bcd4d0b6c4fc 100644
@@ -43,7 +43,7 @@ void qxl_gem_object_free(struct drm_gem_object *gobj)
 	ttm_bo_fini(tbo);
 }
 
-int qxl_gem_object_create(struct qxl_device *qdev, int size,
+int qxl_gem_object_create(struct qxl_device *qdev, size_t size,
 			  int alignment, int initial_domain,
 			  bool discardable, bool kernel,
 			  struct qxl_surface *surf,
@@ -60,7 +60,7 @@ int qxl_gem_object_create(struct qxl_device *qdev, int size,
 	if (r) {
 		if (r != -ERESTARTSYS)
 			DRM_ERROR(
-			"Failed to allocate GEM object (%d, %d, %u, %d)\n",
+			"Failed to allocate GEM object (%zu, %d, %u, %d)\n",
 				  size, initial_domain, alignment, r);
 		return r;
 	}
drivers/gpu/drm/qxl/qxl_ioctl.c +37 -7

109 lines not on this page · raw message

drivers/gpu/drm/qxl/qxl_object.c +9 -6
index 313f6c30cac8..d54d5b4a6f68 100644
@@ -116,6 +116,8 @@ int qxl_bo_create(struct qxl_device *qdev, unsigned long size,
 	else
 		type = ttm_bo_type_device;
 	*bo_ptr = NULL;
+	if (size == 0 || size > ULONG_MAX - PAGE_SIZE + 1)
+		return -EINVAL;
 	bo = kzalloc_obj(struct qxl_bo);
 	if (bo == NULL)
 		return -ENOMEM;
@@ -165,10 +167,8 @@ int qxl_bo_vmap_locked(struct qxl_bo *bo, struct iosys_map *map)
 	}
 
 	r = ttm_bo_vmap(&bo->tbo, &bo->map);
-	if (r) {
-		qxl_bo_unpin_locked(bo);
+	if (r)
 		return r;
-	}
 	bo->map_count = 1;
 
 	/* TODO: Remove kptr in favor of map everywhere. */
@@ -223,7 +223,7 @@ void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
 	return io_mapping_map_atomic_wc(map, offset + page_offset);
 fallback:
 	if (bo->kptr) {
-		rptr = bo->kptr + (page_offset * PAGE_SIZE);
+		rptr = bo->kptr + page_offset;
 		return rptr;
 	}
 
@@ -232,7 +232,7 @@ void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
 		return NULL;
 	rptr = bo_map.vaddr; /* TODO: Use mapping abstraction properly */
 
-	rptr += page_offset * PAGE_SIZE;
+	rptr += page_offset;
 	return rptr;
 }
 
@@ -395,8 +395,11 @@ int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo)
 			return ret;
 
 		ret = qxl_hw_surface_alloc(qdev, bo);
-		if (ret)
+		if (ret) {
+			qxl_surface_id_dealloc(qdev, bo->surface_id);
+			bo->surface_id = 0;
 			return ret;
+		}
 	}
 	return 0;
 }
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] drm/qxl: Silent failure in apply_reloc allows bypassing hardware relocations - [Medium] drm/qxl…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] drm/qxl: Silent failure in apply_reloc allows bypassing hardware relocations
- [Medium] drm/qxl: Meaningful code and commit message mismatch on BO reservation --

commit 19602cc9bae945e0036cb9d92dfb3b6be072f8de
Author: Hui Peng <[email protected]>

drm/qxl: fix vmalloc OOB write, surface size overflow, and BO reloc leaks

This commit addresses multiple memory safety and resource management issues in the QXL driver, including out-of-bounds kernel vmalloc writes during relocation processing, integer overflows in surface size calculations, and resource leaks.

Quoted 3 lines

3. In qxl_process_single_command(), prevent overwriting the union qxl_release_info header at offset 0 of cmd_bo, and reserve/unreserve non-command dst_bo buffers around apply_reloc()/apply_surf_reloc().

[Severity: Medium]
The commit message mentions adding logic to reserve/unreserve non-command dst_bo buffers around apply_reloc() and apply_surf_reloc(). Is this description accurate?

Looking at the relocation loop in qxl_process_single_command(), there are no local reserve or unreserve calls added by this patch.

It seems qxlhw_handle_to_bo() adds dst_bo to the release object, which is then batch-reserved by qxl_release_reserve_list() before the loop. The buffers appear to be safely reserved already, but does the commit message need an update to reflect the actual batch reservation mechanism used?

drivers/gpu/drm/qxl/qxl_ioctl.c apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info) · line 93

Quoted 10 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +2 -0
index 591b026ceff9c..6bb609bc6a7ea 100644
@@ -89,6 +89,8 @@ apply_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
 	void *reloc_page;
 
 	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
+	if (!reloc_page)
+		return;

[Severity: High]
Does returning early without an error code here allow unpatched commands to reach the hardware?

If qxl_bo_kmap_atomic_page() fails (for example, due to vmalloc exhaustion when a user specifies a dumb buffer for dst_bo), this silently aborts the relocation.

Since the caller loop in qxl_process_single_command() assumes success:

    for (i = 0; i < cmd->relocs_num; ++i) {
        if (reloc_info[i].type == QXL_RELOC_TYPE_BO)
            apply_reloc(qdev, &reloc_info[i]);
        ...
    }

could this allow a user to bypass validation and submit unpatched, user-controlled physical addresses to the virtual GPU? Should these apply functions return an error code to properly abort the ioctl?

drivers/gpu/drm/qxl/qxl_ioctl.c apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info) · line 111

Quoted 9 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +2 -0
 	*(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev,
 											      info->src_bo,
 											      info->src_offset);
@@ -105,6 +107,8 @@ apply_surf_reloc(struct qxl_device *qdev, struct qxl_reloc_info *info)
 		id = info->src_bo->surface_id;
 
 	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
+	if (!reloc_page)
+		return;

[Severity: High]
Similar to apply_reloc() above, this early return silently ignores the map failure for surface relocations, leading to the same risk of submitting a corrupted command to the hardware.

Quoted 3 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +0 -0
 	*(uint32_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = id;
 	qxl_bo_kunmap_atomic_page(qdev, info->dst_bo, reloc_page);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1