Skip to contentSkip to conversation

drm/v3d: bound CPU-job copy-query writes to the destination BO

PATCH Superseded Michael Bommarito 4 participants 8 messages Jun 14 – Jun 15

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover drm/v3d: bound CPU-job copy-query writes to the destination BO

The V3D_SUBMIT_CPU COPY_TIMESTAMP_QUERY and COPY_PERFORMANCE_QUERY extensions take a destination offset, per-query stride and query count from userspace and consume them at job exec without checking the write extent against the destination BO. A render-node user (DRM_RENDER_ALLOW, no master, no capability) can drive a controlled-offset out-of-bounds write past the BO's vmap mapping; the timestamp variant also reads each result from an unchecked offset into the second BO.

Patch 1 validates the full write extent against the BO size before the job is queued, using check_*_overflow() so a u32 product cannot wrap. Patch 2 adds a KUnit reproducer the maintainer may take or drop.

Reproduced under KASAN via the KUnit over a shmem-backed BO: the trigger faults on stock and is rejected at submit on patched; two in-bounds controls pass on both.

Michael Bommarito (2):
drm/v3d: validate copy-query buffer bounds against destination BO size drm/v3d: add KUnit reproducer for the copy-query out-of-bounds write

 drivers/gpu/drm/v3d/Kconfig                |  10 ++
 drivers/gpu/drm/v3d/v3d_copy_query_kunit.c | 172 +++++++++++++++++++++
 drivers/gpu/drm/v3d/v3d_drv.h              |   3 +
 drivers/gpu/drm/v3d/v3d_sched.c            |   4 +
 drivers/gpu/drm/v3d/v3d_submit.c           |  86 +++++++++++
 5 files changed, 275 insertions(+)
 create mode 100644 drivers/gpu/drm/v3d/v3d_copy_query_kunit.c

base-commit: 5200f5f493f79f14bbdc349e402a40dfb32f23c8

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 drm/v3d: validate copy-query buffer bounds against destination BO size

The V3D_SUBMIT_CPU COPY_TIMESTAMP_QUERY and COPY_PERFORMANCE_QUERY extensions store a user-supplied destination offset, per-query stride and query count on the job and consume them at exec without checking that offset + (count - 1) * stride + per-query write size stays inside the destination BO. The copy then writes counter values and the availability bit past the BO's vmap mapping; the timestamp variant also reads each result from an unchecked offset into the second BO. A render-node user (DRM_RENDER_ALLOW, no master, no capability) controls the offset.

Validate the full write extent against the destination BO size once the BOs are looked up, before the job is queued, rejecting overflow or out-of-range geometry with -EINVAL; the per-query write size is computed with check_*_overflow() so a u32 product cannot wrap the bound, and the timestamp source offsets are bounded in the same pass. A KUnit reproducer follows.

Fixes 6745f3e44a20 ("drm/v3d: Create a CPU job extension to copy timestamp query to a buffer")
Assisted-by Claude:claude-opus-4-8

---
Reproduced under KASAN via a KUnit (patch 2) driving the real v3d_copy_query_results() over a shmem-backed BO; a copy offset at the BO size writes past the one-page vmap mapping:

  BUG: KASAN: vmalloc-out-of-bounds in v3d_copy_query_results+0x807/0x900

The trigger faults on stock and is rejected at submit time on the patched tree; two in-bounds controls pass on both. The performance-query copy shares the bound; its write values come from perfmon counters and were checked by source review rather than this hardware-free run.

 drivers/gpu/drm/v3d/v3d_submit.c | 86 ++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +86 -0
index ee4512db294b3..23e19dacfdce2 100644
@@ -1246,6 +1246,88 @@ static const unsigned int cpu_job_bo_handle_count[] = {
 	[V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
 };
 
+/* Reject offset + (count - 1) * stride + write_size if it leaves the BO. */
+static int
+v3d_check_copy_extent(struct drm_device *dev, size_t bo_size,
+		      u32 offset, u32 stride, u32 count, u32 write_size)
+{
+	u32 span, last;
+
+	if (!count)
+		return 0;
+
+	if (check_mul_overflow(stride, count - 1, &span) ||
+	    check_add_overflow(span, write_size, &span) ||
+	    check_add_overflow(span, offset, &last) ||
+	    last > bo_size) {
+		drm_dbg(dev, "CPU job copy buffer exceeds the destination BO.\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/* Bound the copy-query CPU-job writes; the exec-time copy does not. */
+static int
+v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job)
+{
+	struct drm_device *dev = &job->base.v3d->drm;
+	struct v3d_copy_query_results_info *copy = &job->copy;
+	u32 elem = copy->do_64bit ? sizeof(u64) : sizeof(u32);
+	struct v3d_bo *bo, *timestamp;
+	u32 slots, write_size;
+	int i;
+
+	switch (job->job_type) {
+	case V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY:
+		bo = to_v3d_bo(job->base.bo[0]);
+		timestamp = to_v3d_bo(job->base.bo[1]);
+
+		slots = copy->availability_bit ? 2 : 1;
+		if (check_mul_overflow(slots, elem, &write_size))
+			return -EINVAL;
+
+		if (v3d_check_copy_extent(dev, bo->base.base.size, copy->offset,
+					  copy->stride, job->timestamp_query.count,
+					  write_size))
+			return -EINVAL;
+
+		for (i = 0; i < job->timestamp_query.count; i++) {
+			u32 end;
+
+			if (check_add_overflow(job->timestamp_query.queries[i].offset,
+					       (u32)sizeof(u64), &end) ||
+			    end > timestamp->base.base.size) {
+				drm_dbg(dev, "CPU job timestamp query offset exceeds the BO.\n");
+				return -EINVAL;
+			}
+		}
+		return 0;
+	case V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY:
+		bo = to_v3d_bo(job->base.bo[0]);
+
+		if (check_mul_overflow(job->performance_query.nperfmons,
+				       (u32)DRM_V3D_MAX_PERF_COUNTERS, &slots))
+			return -EINVAL;
+		if (copy->availability_bit) {
+			u32 avail_slots;
+
+			if (check_add_overflow(job->performance_query.ncounters,
+					       1u, &avail_slots))
+				return -EINVAL;
+			slots = max(slots, avail_slots);
+		}
+		if (check_mul_overflow(slots, elem, &write_size))
+			return -EINVAL;
+
+		return v3d_check_copy_extent(dev, bo->base.base.size, copy->offset,
+					     copy->stride, job->performance_query.count,
+					     write_size);
+	default:
+		return 0;
+	}
+}
+
 /**
  * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D.
  * @dev: DRM device
@@ -1317,6 +1399,10 @@ v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
 		if (ret)
 			goto fail;
 
+		ret = v3d_cpu_job_check_copy_bounds(cpu_job);
+		if (ret)
+			goto fail;
+
 		ret = v3d_lock_bo_reservations(&cpu_job->base, &acquire_ctx);
 		if (ret)
 			goto fail;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] drm/v3d: validate copy-query buffer bounds against destination BO size

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

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

Pre-existing issues:
- [Critical] Missing bounds checking for TIMESTAMP_QUERY, RESET_TIMESTAMP_QUERY, and INDIRECT_CSD CPU jobs allows out-of-bounds kernel memory reads and writes.
- [High] Structurally flawed error handling in v3d_submit_cpu_ioctl leading to Use-After-Free and NULL pointer dereferences.
- [High] Missing validation of vmap() return value leads to NULL pointer dereferences in CPU job handlers.
- [Medium] Passing duplicate BO handles causes a vmap memory leak in v3d_copy_query_results().
--

commit 18b6364dfc49658f5028ca1c3ffd50cc3b4cd3e7
Author: Michael Bommarito <[email protected]>

drm/v3d: validate copy-query buffer bounds against destination BO size

This commit adds bounds checking to the copy timestamp query and copy performance query extensions. It verifies that the user-supplied destination offset, stride, and query count do not exceed the destination buffer object bounds before queuing the job.

Quoted 3 lines
drivers/gpu/drm/v3d/v3d_submit.c +0 -0

[ ... ]

Quoted 3 lines
drivers/gpu/drm/v3d/v3d_submit.c +3 -0
+static int
+v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job)
+{

[ ... ]

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1327

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_submit.c +2 -0
+	default:
+		return 0;

[Severity: Critical]
This is a pre-existing issue, but does returning 0 here leave other job types vulnerable to out-of-bounds accesses?

The new validation explicitly ignores timestamp query, reset timestamp query, and indirect csd jobs. These unvalidated job types also use user-provided offsets to access kernel memory.

For example, in drivers/gpu/drm/v3d/v3d_sched.c:v3d_timestamp_query():

    value_addr = ((u8 *)bo->vaddr) + timestamp_query->queries[i].offset;
    *((u64 *)value_addr) = i == 0 ? ktime_get_ns() : 0ull;

And in v3d_rewrite_csd_job_wg_counts_from_indirect():

    wg_counts = (uint32_t *)(bo->vaddr + indirect_csd->offset);
    ...
    u32 uniform_idx = indirect_csd->wg_uniform_offsets[i];
    ((uint32_t *)indirect->vaddr)[uniform_idx] = wg_counts[i];

Can an attacker provide arbitrary offsets to achieve out-of-bounds reads and writes in kernel memory through these unvalidated job types?

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_submit.c +2 -0
+	}
+}

[ ... ]

Quoted 1 line
drivers/gpu/drm/v3d/v3d_submit.c +0 -0
@@ -1317,6 +1399,10 @@ v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,

[ ... ]

drivers/gpu/drm/v3d/v3d_submit.c v3d_submit_cpu_ioctl(struct drm_device *dev, void *data, · line 1404

Quoted 6 lines
drivers/gpu/drm/v3d/v3d_submit.c +3 -0
 		if (ret)
 			goto fail;
 
+		ret = v3d_cpu_job_check_copy_bounds(cpu_job);
+		if (ret)
+			goto fail;

[Severity: High]
This is a pre-existing issue, but does this error handling path cause a use-after-free or NULL pointer dereference?

If a failure occurs and the code jumps to the fail block in v3d_submit_cpu_ioctl(), cpu_job is cleaned up:

fail:

    v3d_job_cleanup((void *)cpu_job);
    ...
    kvfree(cpu_job->timestamp_query.queries);

If v3d_job_init() failed earlier, v3d_job_deallocate(&cpu_job) frees cpu_job and sets the pointer to NULL, making the kvfree() dereference a NULL pointer.

If a failure occurs after v3d_job_init() (like the new bounds check), v3d_job_cleanup(cpu_job) drops the kref to 0, freeing the cpu_job struct. The subsequent kvfree() then accesses the freed struct.

Is it safe to access cpu_job->timestamp_query.queries after cpu_job has been cleaned up or deallocated?

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for bo->vaddr to be NULL when accessed in CPU job handlers?

In drivers/gpu/drm/v3d/v3d_bo.c:v3d_get_bo_vaddr():

    bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP,
                     pgprot_writecombine(PAGE_KERNEL));

The result of vmap() is assigned without a NULL check. If memory allocation fails, bo->vaddr becomes NULL.

Later, job handlers unconditionally use it, for example in v3d_timestamp_query():

    value_addr = ((u8 *)bo->vaddr) + timestamp_query->queries[i].offset;
    *((u64 *)value_addr) = i == 0 ? ktime_get_ns() : 0ull;

Will this lead to unvalidated writes to a NULL base address under memory pressure?

[Severity: Medium]
This is a pre-existing issue, but does v3d_copy_query_results() leak memory if the same buffer object handle is passed multiple times?

If a user submits a copy timestamp query job and passes the same buffer object handle for both the destination buffer and the timestamp buffer, job->base.bo[0] and job->base.bo[1] resolve to the same struct v3d_bo object.

In drivers/gpu/drm/v3d/v3d_sched.c:v3d_copy_query_results():

    v3d_get_bo_vaddr(bo);
    v3d_get_bo_vaddr(timestamp);

The second call will overwrite bo->vaddr with a new vmap() allocation, leaking the first virtual memory area mapping. Could this allow userspace to leak vmalloc space?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH 1/2] drm/v3d: validate copy-query buffer bounds against destination BO size

In reply to Michael Bommarito

Hi Michael,

Quoted 1 line · Michael Bommarito

The V3D_SUBMIT_CPU COPY_TIMESTAMP_QUERY and COPY_PERFORMANCE_QUERY

If you are doing it for V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY and V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY, you should also do it for V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY and V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY.

Quoted 38 lines

extensions store a user-supplied destination offset, per-query stride and query count on the job and consume them at exec without checking that offset + (count - 1) * stride + per-query write size stays inside the destination BO. The copy then writes counter values and the availability bit past the BO's vmap mapping; the timestamp variant also reads each result from an unchecked offset into the second BO. A render-node user (DRM_RENDER_ALLOW, no master, no capability) controls the offset.

Validate the full write extent against the destination BO size once the BOs are looked up, before the job is queued, rejecting overflow or out-of-range geometry with -EINVAL; the per-query write size is computed with check_*_overflow() so a u32 product cannot wrap the bound, and the timestamp source offsets are bounded in the same pass. A KUnit reproducer follows.

Fixes 6745f3e44a20 ("drm/v3d: Create a CPU job extension to copy timestamp query to a buffer")
Assisted-by Claude:claude-opus-4-8

---
Reproduced under KASAN via a KUnit (patch 2) driving the real v3d_copy_query_results() over a shmem-backed BO; a copy offset at the BO size writes past the one-page vmap mapping:

   BUG: KASAN: vmalloc-out-of-bounds in v3d_copy_query_results+0x807/0x900

The trigger faults on stock and is rejected at submit time on the patched tree; two in-bounds controls pass on both. The performance-query copy shares the bound; its write values come from perfmon counters and were checked by source review rather than this hardware-free run.

  drivers/gpu/drm/v3d/v3d_submit.c | 86 ++++++++++++++++++++++++++++++++
  1 file changed, 86 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +0 -0
index ee4512db294b3..23e19dacfdce2 100644

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1255

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_submit.c +7 -0
@@ -1246,6 +1246,88 @@ static const unsigned int cpu_job_bo_handle_count[] = {
  	[V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1,
  };
 
+/* Reject offset + (count - 1) * stride + write_size if it leaves the BO. */
+static int
+v3d_check_copy_extent(struct drm_device *dev, size_t bo_size,
+		      u32 offset, u32 stride, u32 count, u32 write_size)
+{
+	u32 span, last;
+

You don't need the variable "span".

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_submit.c +5 -0
+	if (!count)
+		return 0;
+
+	if (check_mul_overflow(stride, count - 1, &span) ||
+	    check_add_overflow(span, write_size, &span) ||

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1270

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_submit.c +10 -0
+	    check_add_overflow(span, offset, &last) ||
+	    last > bo_size) {
+		drm_dbg(dev, "CPU job copy buffer exceeds the destination BO.\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+/* Bound the copy-query CPU-job writes; the exec-time copy does not. */

This comment is a bit confusing.

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1272

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_submit.c +2 -0
+static int
+v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job)

How about "v3d_cpu_job_bounds_check"?

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_submit.c +5 -0
+{
+	struct drm_device *dev = &job->base.v3d->drm;
+	struct v3d_copy_query_results_info *copy = &job->copy;
+	u32 elem = copy->do_64bit ? sizeof(u64) : sizeof(u32);
+	struct v3d_bo *bo, *timestamp;

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1287

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_submit.c +10 -0
+	u32 slots, write_size;
+	int i;
+
+	switch (job->job_type) {
+	case V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY:
+		bo = to_v3d_bo(job->base.bo[0]);
+		timestamp = to_v3d_bo(job->base.bo[1]);
+
+		slots = copy->availability_bit ? 2 : 1;
+		if (check_mul_overflow(slots, elem, &write_size))

How could this ever overflow? slots is 1 or 2; elem is 4 or 8. The maximum product is 2 * 8 = 16. That cannot overflow any integer type.

Quoted 8 lines
drivers/gpu/drm/v3d/v3d_submit.c +8 -0
+			return -EINVAL;
+
+		if (v3d_check_copy_extent(dev, bo->base.base.size, copy->offset,
+					  copy->stride, job->timestamp_query.count,
+					  write_size))
+			return -EINVAL;
+
+		for (i = 0; i < job->timestamp_query.count; i++) {

drivers/gpu/drm/v3d/v3d_submit.c static const unsigned int cpu_job_bo_handle_count[] = { · line 1305

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_submit.c +10 -0
+			u32 end;
+
+			if (check_add_overflow(job->timestamp_query.queries[i].offset,
+					       (u32)sizeof(u64), &end) ||
+			    end > timestamp->base.base.size) {
+				drm_dbg(dev, "CPU job timestamp query offset exceeds the BO.\n");
+				return -EINVAL;
+			}
+		}
+		return 0;

I have the impression that this function is over complicating something simple. This could be as simple as:

dst = to_v3d_bo(job->base.bo[0]);
src = to_v3d_bo(job->base.bo[1]);

for (i = 0; i < tquery->count; i++) {
    if (tquery->queries[i].offset + sizeof(u64) > src->base.base.size)
        goto err_range;
}

write_size = (copy->availability_bit ? 2 : 1) * (u64)elem; return v3d_check_copy_extent(dev, dst->base.base.size,

                             copy->offset, copy->stride,
                             tquery->count, write_size);

The same logic of simplification also apply to V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY (with its own caveats).

About your second patch (the KUnit reproducer), it's a nice test to reproduce this issue, but for me, it's a no-go in terms of upstreaming. This is a very simple issue, not related to any v3d functional regression. So, I'd prefer not to take the KUnit test to the branch. Regarding this fix, a v2 addressing the issues pointed out is welcome.

Thanks for your patch.

Best regards,
- Maíra

#4 lore raw

Re: [PATCH 1/2] drm/v3d: validate copy-query buffer bounds against destination BO size

Quoted 4 lines · Maíra Canal

If you are doing it for V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY and V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY, you should also do it for V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY and V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY.

OK, will do!

Quoted 4 lines

How could this ever overflow? slots is 1 or 2; elem is 4 or 8. The maximum product is 2 * 8 = 16. That cannot overflow any integer type.
...

     if (tquery->queries[i].offset + sizeof(u64) > src->base.base.size)

You were definitely right about the first check being overkill, but I think we do need to think about 32-bit for this spot. Let me know in v2 if I missed something

Quoted 5 lines

About your second patch (the KUnit reproducer), it's a nice test to reproduce this issue, but for me, it's a no-go in terms of upstreaming. This is a very simple issue, not related to any v3d functional regression. So, I'd prefer not to take the KUnit test to the branch. Regarding this fix, a v2 addressing the issues pointed out is welcome.

Sounds good. I am trying to send them by default after getting yelled at by Greg for not including unit tests in every patch he sees :)

V2 coming up soon

Thanks,
Mike

#5 lore raw

PATCH 2/2 drm/v3d: add KUnit reproducer for the copy-query out-of-bounds write

In reply to Michael Bommarito

Add a KUnit suite (CONFIG_DRM_V3D_COPY_QUERY_KUNIT_TEST, builds under COMPILE_TEST) that drives the real v3d_copy_query_results() CPU-job handler over a shmem-backed BO and exercises the destination-buffer bounds the previous patch adds. Under KASAN the trigger case reproduces the vmalloc-out-of-bounds write on the stock tree and passes once the bounds patch is applied; two in-bounds controls pass on both trees.

Assisted-by Claude:claude-opus-4-8

---
The COPY_TIMESTAMP_QUERY path is hardware-free: with an unsignalled query the result read is skipped and only the availability bit is written, so the copy runs on CPU memory alone. The trigger uses a copy offset at the BO size, so the write lands past the one-page vmap mapping and KASAN (CONFIG_KASAN_VMALLOC) reports a vmalloc-out-of-bounds write; the two in-bounds controls prove the synthesised setup is not itself the cause. With patch 1 applied the trigger is rejected by the submit-time gate (exposed for the test) and the copy never runs. x86_64, COMPILE_TEST; the write is plain CPU memory and not architecture specific.

 drivers/gpu/drm/v3d/Kconfig                |  10 ++
 drivers/gpu/drm/v3d/v3d_copy_query_kunit.c | 172 +++++++++++++++++++++
 drivers/gpu/drm/v3d/v3d_drv.h              |   3 +
 drivers/gpu/drm/v3d/v3d_sched.c            |   4 +
 drivers/gpu/drm/v3d/v3d_submit.c           |   2 +-
 5 files changed, 190 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/v3d/v3d_copy_query_kunit.c
drivers/gpu/drm/v3d/Kconfig +10 -0
index ce62c5908e1db..f293eb95a73c8 100644
@@ -11,3 +11,13 @@ config DRM_V3D
 	  Choose this option if you have a system that has a Broadcom
 	  V3D 3.x or newer GPUs. SoCs supported include the BCM2711,
 	  BCM7268 and BCM7278.
+
+config DRM_V3D_COPY_QUERY_KUNIT_TEST
+	bool "KUnit test for V3D CPU-job copy-query bounds"
+	depends on DRM_V3D && KUNIT=y
+	select DRM_KUNIT_TEST_HELPERS
+	help
+	  Builds a KUnit suite that drives the V3D CPU-job copy-timestamp-query
+	  handler over a real shmem-backed BO to exercise the destination-buffer
+	  bounds. Used to reproduce the copy-query out-of-bounds write under
+	  KASAN. If in doubt, say N.
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +167 -0

167 lines not on this page · raw message

drivers/gpu/drm/v3d/v3d_drv.h +3 -0
index 6a3cad9334398..a4b772385070f 100644
@@ -583,6 +583,9 @@ int v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
 			 struct drm_file *file_priv);
 int v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
 			 struct drm_file *file_priv);
+/* Exposed for the copy-query KUnit; its presence marks the bounds patch applied. */
+#define V3D_CPU_JOB_COPY_BOUNDS_CHECKED 1
+int v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job);
 
 /* v3d_irq.c */
 int v3d_irq_init(struct v3d_dev *v3d);
drivers/gpu/drm/v3d/v3d_sched.c +4 -0
index 1855ef5b3b5fe..7884863376702 100644
@@ -901,3 +901,7 @@ v3d_sched_fini(struct v3d_dev *v3d)
 			drm_sched_fini(&v3d->queue[q].sched);
 	}
 }
+
+#if IS_ENABLED(CONFIG_DRM_V3D_COPY_QUERY_KUNIT_TEST)
+#include "v3d_copy_query_kunit.c"
+#endif
drivers/gpu/drm/v3d/v3d_submit.c +1 -1
index 23e19dacfdce2..ec0b94ada73ff 100644
@@ -1268,7 +1268,7 @@ v3d_check_copy_extent(struct drm_device *dev, size_t bo_size,
 }
 
 /* Bound the copy-query CPU-job writes; the exec-time copy does not. */
-static int
+int
 v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job)
 {
 	struct drm_device *dev = &job->base.v3d->drm;
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 2/2] drm/v3d: add KUnit reproducer for the copy-query out-of-bounds write

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

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

Pre-existing issues:
- [Critical] Missing bounds validation for CPU jobs other than COPY operations allows arbitrary out-of-bounds memory accesses.
- [High] Missing NULL check for vmap() return value causes NULL pointer dereference across all CPU job handlers.
- [High] Missing alignment checks for userspace-provided offsets in CPU copy jobs trigger fatal alignment faults on ARM architectures.
- [High] Unbounded memory allocation from userspace-controlled counts in CPU job submission.
- [High] Repeated calls to vmap() leak kernel vmap space when the same BO is supplied multiple times.
--

commit 3ca0a930a22512f0b8cf72cfb659ccbf12f82409
Author: Michael Bommarito <[email protected]>

drm/v3d: add KUnit reproducer for the copy-query out-of-bounds write

Add a KUnit suite (CONFIG_DRM_V3D_COPY_QUERY_KUNIT_TEST, builds under COMPILE_TEST) that drives the real v3d_copy_query_results() CPU-job handler over a shmem-backed BO and exercises the destination-buffer bounds the previous patch adds. Under KASAN the trigger case reproduces the vmalloc-out-of-bounds write on the stock tree and passes once the bounds patch is applied; two in-bounds controls pass on both trees.

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +0 -0
new file mode 100644
index 0000000000000..e7a795ed2a3ff

[ ... ]

Quoted 3 lines
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +3 -0
+static void v3d_copy_test_run(struct kunit *test, u32 offset, u32 stride,
+			      u32 count)
+{

[ ... ]

drivers/gpu/drm/v3d/v3d_copy_query_kunit.c line 97

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +2 -0
+	c->bo_array[0] = &c->dst->base.base;
+	c->bo_array[1] = &c->ts->base.base;

[Severity: High]
This is a pre-existing issue, but does the driver handle cases where the same buffer object is passed multiple times in a single job?

If userspace passes the same BO handle for both the source and timestamp arrays, the CPU job handler calls v3d_get_bo_vaddr() twice on the identical buffer object.

In drivers/gpu/drm/v3d/v3d_bo.c:v3d_get_bo_vaddr(), the mapping is unconditionally overwritten:

bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP,

                 pgprot_writecombine(PAGE_KERNEL));

Does the second call to vmap() overwrite the pointer from the first without unmapping it, permanently leaking the first vmap mapping and potentially exhausting virtual memory space?

[ ... ]

drivers/gpu/drm/v3d/v3d_copy_query_kunit.c line 111

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +2 -0
+	c->job.timestamp_query.queries = c->queries;
+	c->job.timestamp_query.count = count;

[Severity: High]
This is a pre-existing issue, but does the ioctl submission path validate the bounds of this user-provided count?

During CPU job submission in drivers/gpu/drm/v3d/v3d_submit.c:v3d_get_cpu_timestamp_query_params(), the unbounded userspace count is passed directly to the memory allocator:

query_info->queries = kvmalloc_objs(struct v3d_timestamp_query,

                                    timestamp.count);

If an unprivileged user provides an extremely large count value, could this attempt a massive virtual memory allocation, exhausting system memory and triggering the OOM killer?

[ ... ]

drivers/gpu/drm/v3d/v3d_copy_query_kunit.c line 132

Quoted 1 line
drivers/gpu/drm/v3d/v3d_copy_query_kunit.c new file +1 -0
+	v3d_copy_query_results(&c->job);

[Severity: High]
This is a pre-existing issue, but is it possible for the CPU job handlers to dereference a NULL pointer if mapping fails?

In drivers/gpu/drm/v3d/v3d_bo.c:v3d_get_bo_vaddr(), the code maps the buffer:

bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP,

                 pgprot_writecombine(PAGE_KERNEL));

If vmap() returns NULL under memory pressure, is this safely handled? Handlers like v3d_copy_query_results() in v3d_sched.c immediately compute addresses using this pointer:

data = ((u8 *)bo->vaddr) + copy->offset;

Could this result in a NULL pointer dereference and kernel crash?

[Severity: High]
This is also a pre-existing issue, but does this copy handler properly align memory accesses for userspace-controlled offsets?

In drivers/gpu/drm/v3d/v3d_sched.c:v3d_copy_query_results(), the driver computes memory addresses directly from the userspace offsets and performs 64-bit operations:

query_addr = ((u8 *)timestamp->vaddr) + queries[i].offset; write_to_buffer(data, 0, copy->do_64bit, *((u64 *)query_addr));

Since V3D hardware typically runs on ARM architectures that strictly require 4-byte or 8-byte alignment for 64-bit load/store instructions, could an unaligned userspace offset trigger a fatal alignment fault and kernel panic?

Quoted 1 line

+}

[ ... ]

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_submit.c +0 -0
index 23e19dacfdce2..ec0b94ada73ff 100644

[ ... ]

drivers/gpu/drm/v3d/v3d_submit.c v3d_check_copy_extent(struct drm_device *dev, size_t bo_size, · line 1273

Quoted 5 lines

/* Bound the copy-query CPU-job writes; the exec-time copy does not. */

drivers/gpu/drm/v3d/v3d_submit.c +1 -1
-static int
+int
 v3d_cpu_job_check_copy_bounds(struct v3d_cpu_job *job)
 {

[Severity: Critical]
This is a pre-existing issue, but does this function properly validate bounds for all CPU job types?

For CPU jobs other than COPY operations (like TIMESTAMP_QUERY or RESET_TIMESTAMP_QUERY), the switch statement defaults to returning 0 without validation:

drivers/gpu/drm/v3d/v3d_submit.c:v3d_cpu_job_check_copy_bounds() {
        ...
        default:
                return 0;
}

Could a malicious user exploit this by passing arbitrary out-of-bounds offsets? If the offset isn't checked, handlers like v3d_timestamp_query() might add it directly to bo->vaddr and perform out-of-bounds memory writes.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH 2/2] drm/v3d: add KUnit reproducer for the copy-query out-of-bounds write

In reply to Michael Bommarito

Hi Michael,

drivers/gpu/drm/v3d/Kconfig config DRM_V3D · line 23

Quoted 9 lines · Michael Bommarito
drivers/gpu/drm/v3d/Kconfig +9 -0
+config DRM_V3D_COPY_QUERY_KUNIT_TEST
+	bool "KUnit test for V3D CPU-job copy-query bounds"
+	depends on DRM_V3D && KUNIT=y
+	select DRM_KUNIT_TEST_HELPERS
+	help
+	  Builds a KUnit suite that drives the V3D CPU-job copy-timestamp-query
+	  handler over a real shmem-backed BO to exercise the destination-buffer
+	  bounds. Used to reproduce the copy-query out-of-bounds write under
+	  KASAN. If in doubt, say N.

Could you hide this option from the Kconfig frontend when KUNIT_ALL_TESTS is enabled?

E.g.:
bool "KUnit test for V3D CPU-job copy-query bounds" if !KUNIT_ALL_TESTS

From Documentation/dev-tools/kunit/style.rst:
"This Kconfig entry must:
<snip>
* be visible only if ``CONFIG_KUNIT_ALL_TESTS`` is not enabled."

- Julian Braha