Skip to content

Commit b0fe80c

Browse files
committed
drm/v3d: Fix use-after-free of CPU job query arrays on error path
The CPU job ioctl's fail label calls kvfree() on cpu_job's timestamp and performance query arrays after v3d_job_cleanup(), which drops the job's last reference and frees cpu_job. Reading cpu_job at that point is a use-after-free. Also, on the early v3d_job_init() failure path, it is a NULL dereference, since v3d_job_deallocate() zeroes the local pointer. In the success path, the arrays are released from the scheduler's .free_job callback, but on the error path, they are freed manually, as the job was never pushed to the scheduler. While the success path deals with this correctly, the fail path doesn't. On top of that, the manual kvfree() calls only free the array storage; they don't drm_syncobj_put() the per-query syncobjs that v3d_timestamp_query_info_free() and v3d_performance_query_info_free() release on the success path. So the same fail path that triggers the use-after-free also leaks one syncobj reference per query. Unify the CPU job teardown into the CPU job's kref destructor, mirroring v3d_render_job_free(). The scheduler's .free_job slot reverts to the generic v3d_sched_job_free() and the fail label drops the manual kvfree() calls, leaving a single teardown path that is reached from both the scheduler and the ioctl error path. That removes the use-after-free, the NULL dereference, and the syncobj leak by construction. Cc: stable@vger.kernel.org Fixes: 9ba0ff3 ("drm/v3d: Create a CPU job extension for the timestamp query job") Assisted-by: Claude:claude-opus-4.7 Reviewed-by: Iago Toral Quiroga <itoral@igalia.com> Link: https://patch.msgid.link/20260515-v3d-cpu-job-leaks-v1-1-7f147cbbf935@igalia.com Signed-off-by: Maíra Canal <mcanal@igalia.com>
1 parent 379e8f1 commit b0fe80c

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

drivers/gpu/drm/v3d/v3d_sched.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,6 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
125125
}
126126
}
127127

128-
static void
129-
v3d_cpu_job_free(struct drm_sched_job *sched_job)
130-
{
131-
struct v3d_cpu_job *job = to_cpu_job(sched_job);
132-
133-
v3d_timestamp_query_info_free(&job->timestamp_query,
134-
job->timestamp_query.count);
135-
136-
v3d_performance_query_info_free(&job->performance_query,
137-
job->performance_query.count);
138-
139-
v3d_job_cleanup(&job->base);
140-
}
141-
142128
static void
143129
v3d_switch_perfmon(struct v3d_dev *v3d, struct v3d_job *job)
144130
{
@@ -830,7 +816,7 @@ static const struct drm_sched_backend_ops v3d_cache_clean_sched_ops = {
830816

831817
static const struct drm_sched_backend_ops v3d_cpu_sched_ops = {
832818
.run_job = v3d_cpu_job_run,
833-
.free_job = v3d_cpu_job_free
819+
.free_job = v3d_sched_job_free
834820
};
835821

836822
static int

drivers/gpu/drm/v3d/v3d_submit.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ v3d_render_job_free(struct kref *ref)
123123
v3d_job_free(ref);
124124
}
125125

126+
static void
127+
v3d_cpu_job_free(struct kref *ref)
128+
{
129+
struct v3d_cpu_job *job = container_of(ref, struct v3d_cpu_job,
130+
base.refcount);
131+
132+
v3d_timestamp_query_info_free(&job->timestamp_query,
133+
job->timestamp_query.count);
134+
135+
v3d_performance_query_info_free(&job->performance_query,
136+
job->performance_query.count);
137+
138+
v3d_job_free(ref);
139+
}
140+
126141
void v3d_job_cleanup(struct v3d_job *job)
127142
{
128143
if (!job)
@@ -1302,7 +1317,7 @@ v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
13021317
trace_v3d_submit_cpu_ioctl(&v3d->drm, cpu_job->job_type);
13031318

13041319
ret = v3d_job_init(v3d, file_priv, &cpu_job->base,
1305-
v3d_job_free, 0, &se, V3D_CPU);
1320+
v3d_cpu_job_free, 0, &se, V3D_CPU);
13061321
if (ret) {
13071322
v3d_job_deallocate((void *)&cpu_job);
13081323
goto fail;
@@ -1385,8 +1400,6 @@ v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
13851400
v3d_job_cleanup((void *)csd_job);
13861401
v3d_job_cleanup(clean_job);
13871402
v3d_put_multisync_post_deps(&se);
1388-
kvfree(cpu_job->timestamp_query.queries);
1389-
kvfree(cpu_job->performance_query.queries);
13901403

13911404
return ret;
13921405
}

0 commit comments

Comments
 (0)