Skip to contentSkip to conversation

drm/qxl: fix 32-bit overflow in the ALLOC_SURF size computation

PATCH In review Aldo Ariel Panzardo 2 participants 5 messages Aug 9 – Oct 24

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

PATCH v2 1/1 drm/qxl: fix 32-bit overflow in the ALLOC_SURF size computation

qxl_alloc_surf_ioctl() works out the backing size of a surface with

	actual_stride = param->stride < 0 ? -param->stride : param->stride;
	size = actual_stride * param->height + actual_stride;

where size and actual_stride are int and param->height is __u32. Every operand comes straight from userspace through DRM_IOCTL_QXL_ALLOC_SURF, which is DRM_AUTH, and the expression is evaluated modulo 2^32 with no overflow check.

The wrapped value is what reaches qxl_bo_create(), which only rounds it up to a page. The original width, height and stride are kept verbatim in bo->surf and are later handed to the device by qxl_hw_surface_alloc() together with the address of that undersized allocation, so the driver tells the host about a surface far larger than the memory backing it.

For example stride=4096, height=1048576 gives
4096 * (1048576 + 1) = 0x1_0000_1000, which truncates to 4096: a one-page buffer object described to the device as a 4 GiB surface. Measured on 6.12.101 by probing mmap() lengths against the resulting GEM object, the backing is 4096 bytes while the surface declared to the device is 4294967296 bytes.

Two smaller problems are fixed at the same time. Negating param->stride is undefined for INT_MIN, and unlike QXL_ALLOC there is no rejection of a zero-sized request.

Compute the size in u64, which cannot wrap for the u32 inputs, reject INT_MIN and zero dimensions, and bound the result so it still fits the int parameter of qxl_gem_object_create().

Fixes f64122c1f6ad ("drm: add new QXL driver. (v1.4)")

---
v2: compute the size in u64 instead of size_t, so the (height + 1)
addition cannot wrap on 32-bit either (reported by Sashiko AI review); the v1 <linux/overflow.h> include is no longer needed.

 drivers/gpu/drm/qxl/qxl_ioctl.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/gpu/drm/qxl/qxl_ioctl.c +14 -2
index cd1901d5c7c0..75abfc1db0f5 100644
@@ -385,12 +385,24 @@ int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, struct drm_file *fi
 	struct drm_qxl_alloc_surf *param = data;
 	int handle;
 	int ret;
-	int size, actual_stride;
+	int actual_stride;
+	u64 size;
 	struct qxl_surface surf;
 
 	/* work out size allocate bo with handle */
+	if (param->stride == INT_MIN)
+		return -EINVAL;
 	actual_stride = param->stride < 0 ? -param->stride : param->stride;
-	size = actual_stride * param->height + actual_stride;
+	if (!actual_stride || !param->width || !param->height)
+		return -EINVAL;
+	/*
+	 * size = actual_stride * (height + 1), computed in u64 so it cannot
+	 * wrap on any architecture, then bounded so it still fits the int
+	 * parameter of qxl_gem_object_create().
+	 */
+	size = (u64)actual_stride * ((u64)param->height + 1);
+	if (size > INT_MAX)
+		return -EINVAL;
 
 	surf.format = param->format;
 	surf.width = param->width;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The command relocation processing lacks bounds checking on dst_offset, allowing userspace to perform arbitrary writes to kernel or VRAM memory.
- [High] The size computation in qxl_alloc_surf_ioctl prevents integer overflows but fails to validate that the userspace-provided stride is mathematically sufficient for the width of the surface, leaving it vulnerable to undersized VRAM allocations. --

--- Patch [1]: [PATCH v2] drm/qxl: fix 32-bit overflow in the ALLOC_SURF size computation --- commit 0b850095b7d5d290c2b02556121eaeca53cced5b
Author: Aldo Ariel Panzardo <[email protected]>

drm/qxl: fix 32-bit overflow in the ALLOC_SURF size computation

This commit fixes a 32-bit integer overflow in the QXL surface allocation ioctl by computing the backing size as u64 and checking it against INT_MAX. It also rejects zero dimensions and handles an INT_MIN stride properly.

drivers/gpu/drm/qxl/qxl_ioctl.c

Quoted 4 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +0 -0
index 591b026ceff9c..5f84a4cf9631d 100644

[Severity: Critical]
This is a pre-existing issue, but does qxl_process_single_command() lack bounds checking on dst_offset? A userspace client could send a QXL_EXECBUFFER ioctl with a maliciously crafted dst_offset. qxl_process_single_command() copies this without validating it against dst_bo->tbo.base.size:

drivers/gpu/drm/qxl/qxl_ioctl.c:qxl_process_single_command() {
	...
	reloc_info[i].dst_offset = reloc.dst_offset;
	...
}

Later, apply_reloc() calls qxl_bo_kmap_atomic_page() with this out-of-bounds offset and writes the source BO's physical address to it:

drivers/gpu/drm/qxl/qxl_ioctl.c:apply_reloc() {
	...
	reloc_page = qxl_bo_kmap_atomic_page(qdev, info->dst_bo, info->dst_offset & PAGE_MASK);
	*(uint64_t *)(reloc_page + (info->dst_offset & ~PAGE_MASK)) = qxl_bo_physical_address(qdev, info->src_bo, info->src_offset);
	...
}

Could this allow arbitrary local privilege escalation or system crashes from any DRM_AUTH client via out-of-bounds writes?

Quoted 17 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +7 -2
@@ -386,12 +386,24 @@ int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, struct drm_file *fi
 	struct drm_qxl_alloc_surf *param = data;
 	int handle;
 	int ret;
-	int size, actual_stride;
+	int actual_stride;
+	u64 size;
 	struct qxl_surface surf;
 
 	/* work out size allocate bo with handle */
+	if (param->stride == INT_MIN)
+		return -EINVAL;
 	actual_stride = param->stride < 0 ? -param->stride : param->stride;
-	size = actual_stride * param->height + actual_stride;
+	if (!actual_stride || !param->width || !param->height)
+		return -EINVAL;
+	/*

drivers/gpu/drm/qxl/qxl_ioctl.c int qxl_alloc_surf_ioctl(struct drm_device *dev, void *data, struct drm_file *fi · line 409

Quoted 10 lines
drivers/gpu/drm/qxl/qxl_ioctl.c +7 -0
+	 * size = actual_stride * (height + 1), computed in u64 so it cannot
+	 * wrap on any architecture, then bounded so it still fits the int
+	 * parameter of qxl_gem_object_create().
+	 */
+	size = (u64)actual_stride * ((u64)param->height + 1);
+	if (size > INT_MAX)
+		return -EINVAL;
 
 	surf.format = param->format;
 	surf.width = param->width;

[Severity: High]
This isn't a bug introduced by this patch, but does this allocation validate that the stride is mathematically sufficient for the width?

If DRM_IOCTL_QXL_ALLOC_SURF is called with a huge param->width and a tiny actual_stride (e.g., 1), the size computation will pass the INT_MAX check, but fail to enforce stride >= width * bytes_per_pixel.

Since the width is passed directly to the QXL host device, could the host subsequently overrun the guest VRAM outside the undersized allocated buffer when processing the surface?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
In reply to Aldo Ariel Panzardo

Hi Dave, Gerd,

Gentle ping on this v2 -- it has been sitting for ~6 weeks. The fix promotes the backing-size computation to u64 to prevent the modulo-2^32 wrap that lets userspace create a surface whose declared size vastly exceeds its backing allocation.

Is there anything else needed, or can this go into drm-misc-fixes?

Thanks,
Aldo

Quoted 80 lines · Aldo Ariel Panzardo

qxl_alloc_surf_ioctl() works out the backing size of a surface with

        actual_stride = param->stride < 0 ? -param->stride : param->stride;
        size = actual_stride * param->height + actual_stride;

where size and actual_stride are int and param->height is __u32. Every operand comes straight from userspace through DRM_IOCTL_QXL_ALLOC_SURF, which is DRM_AUTH, and the expression is evaluated modulo 2^32 with no overflow check.

The wrapped value is what reaches qxl_bo_create(), which only rounds it up to a page. The original width, height and stride are kept verbatim in bo->surf and are later handed to the device by qxl_hw_surface_alloc() together with the address of that undersized allocation, so the driver tells the host about a surface far larger than the memory backing it.

For example stride=4096, height=1048576 gives
4096 * (1048576 + 1) = 0x1_0000_1000, which truncates to 4096: a one-page buffer object described to the device as a 4 GiB surface. Measured on 6.12.101 by probing mmap() lengths against the resulting GEM object, the backing is 4096 bytes while the surface declared to the device is 4294967296 bytes.

Two smaller problems are fixed at the same time. Negating param->stride is undefined for INT_MIN, and unlike QXL_ALLOC there is no rejection of a zero-sized request.

Compute the size in u64, which cannot wrap for the u32 inputs, reject INT_MIN and zero dimensions, and bound the result so it still fits the int parameter of qxl_gem_object_create().

Fixes f64122c1f6ad ("drm: add new QXL driver. (v1.4)")

---
v2: compute the size in u64 instead of size_t, so the (height + 1)
addition cannot wrap on 32-bit either (reported by Sashiko AI review); the v1 <linux/overflow.h> include is no longer needed.

 drivers/gpu/drm/qxl/qxl_ioctl.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/gpu/drm/qxl/qxl_ioctl.c +0 -0
index cd1901d5c7c0..75abfc1db0f5 100644
@@ -385,12 +385,24 @@ int qxl_alloc_surf_ioctl(struct drm_device *dev,

void *data, struct drm_file *fi

        struct drm_qxl_alloc_surf *param = data;
        int handle;
        int ret;
drivers/gpu/drm/qxl/qxl_ioctl.c +8 -2
-       int size, actual_stride;
+       int actual_stride;
+       u64 size;
        struct qxl_surface surf;
 
        /* work out size allocate bo with handle */
+       if (param->stride == INT_MIN)
+               return -EINVAL;
        actual_stride = param->stride < 0 ? -param->stride : param->stride;
-       size = actual_stride * param->height + actual_stride;
+       if (!actual_stride || !param->width || !param->height)
+               return -EINVAL;
+       /*
+        * size = actual_stride * (height + 1), computed in u64 so it

cannot

drivers/gpu/drm/qxl/qxl_ioctl.c +6 -1
+        * wrap on any architecture, then bounded so it still fits the int
+        * parameter of qxl_gem_object_create().
+        */
+       size = (u64)actual_stride * ((u64)param->height + 1);
+       if (size > INT_MAX)
+               return -EINVAL;
 
        surf.format = param->format;
        surf.width = param->width;
--

2.43.0

#3 lore raw
In reply to Aldo Ariel Panzardo

Hi Dave, Gerd,

Friendly ping on this v2, sent August 8. The original ALLOC_SURF integer overflow allows a DRM_AUTH client to create a surface backed by a one-page BO but described to the device as up to 4 GiB.

The v2 computes the size in u64 (addressing a Sashiko review note about 32-bit wrap on the height+1 addition).

Let me know if it needs any changes.

Thanks,
Aldo

author
#4 lore raw

Hi Dave, Gerd,

Following up on this v2.

Would an updated reproducer or additional testing help with the review? I'm happy to provide those or address any concerns with the approach.

Thanks,
Aldo

Quoted 13 lines · Aldo Ariel Panzardo

Hi Dave, Gerd,

Friendly ping on this v2, sent August 8. The original ALLOC_SURF integer overflow allows a DRM_AUTH client to create a surface backed by a one-page BO but described to the device as up to 4 GiB.

The v2 computes the size in u64 (addressing a Sashiko review note about 32-bit wrap on the height+1 addition).

Let me know if it needs any changes.

Thanks,
Aldo