accel/amdxdna: Fix use-after-free of mm_struct in job scheduler
Lizhi Hou
maintainerPATCH 1/1 accel/amdxdna: Fix use-after-free of mm_struct in job scheduler
amdxdna_cmd_submit() stores current->mm in job->mm without holding any reference. aie2_sched_job_run() later access job->mm from the DRM scheduler worker thread. With only a raw pointer and no structural reference, the mm_struct can be freed before the scheduler runs the job.
Fix this by calling mmgrab() to hold a structural mm_count reference for the lifetime of the job, paired with mmdrop() in every cleanup path.
--- drivers/accel/amdxdna/amdxdna_ctx.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/accel/amdxdna/amdxdna_ctx.c +4 -0
@@ -577,6 +577,7 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job)amdxdna_arg_bos_put (job);amdxdna_gem_put_obj (job->cmd_bo);dma_fence_put (job->fence); +mmdrop (job->mm); } int amdxdna_cmd_submit(struct amdxdna_client *client,
@@ -642,6 +643,7 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, job->hwctx = hwctx; job->mm = current->mm; +mmgrab (job->mm); job->fence =amdxdna_fence_create (hwctx);if (!job->fence) {
@@ -676,6 +678,8 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, cmd_put:amdxdna_gem_put_obj (job->cmd_bo); free_job: +if (job->mm) +mmdrop (job->mm);kfree (job);return ret; }
Signature · 1 line
2.34.1
Quoted 10 lines · Lizhi Hou
amdxdna_cmd_submit()storescurrent->mminjob->mmwithout holding any reference.aie2_sched_job_run()later accessjob->mmfrom the DRM scheduler worker thread. With only a raw pointer and no structural reference, themm_structcan be freed before the scheduler runs the job.Fix this by calling
mmgrab()to hold a structuralmm_countreference for the lifetime of the job, paired withmmdrop()in every cleanup path.Fixes aac243092b70 ("accel/amdxdna: Add command execution")Signed-off-by Lizhi Hou <[email protected]>
Quoted 33 lines
--- drivers/accel/amdxdna/amdxdna_ctx.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/accel/amdxdna/amdxdna_ctx.c +4
-0@@ -577,6 +577,7 @@ void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job)amdxdna_arg_bos_put (job);amdxdna_gem_put_obj (job->cmd_bo);dma_fence_put (job->fence); +mmdrop (job->mm); } int amdxdna_cmd_submit(struct amdxdna_client *client,@@ -642,6 +643,7 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, job->hwctx = hwctx; job->mm = current->mm; +mmgrab (job->mm); job->fence =amdxdna_fence_create (hwctx);if (!job->fence) {@@ -676,6 +678,8 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, cmd_put:amdxdna_gem_put_obj (job->cmd_bo); free_job: +if (job->mm) +mmdrop (job->mm);kfree (job);return ret; }
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Job scheduler tracks current->mm instead of client->mm, enabling cross-process DMA vulnerabilities when file descriptors are shared. --
--- Patch [1]: [PATCH V1] accel/amdxdna: Fix use-after-free of mm_struct in job scheduler --- commit 97cf038e79109413737ecf07de35445a6294ab64
Author: Lizhi Hou <[email protected]>
accel/amdxdna: Fix use-after-free of mm_struct in job scheduler
This commit fixes a use-after-free of mm_struct in the job scheduler by calling mmgrab to hold a structural reference for the lifetime of the job, paired with mmdrop in cleanup paths.
Quoted 4 lines
drivers/accel/amdxdna/amdxdna_ctx.c +0
-0
[ ... ]
drivers/accel/amdxdna/amdxdna_ctx.c int amdxdna_cmd_submit(struct amdxdna_client *client, · line 602
Quoted 5 lines
drivers/accel/amdxdna/amdxdna_ctx.c +1
-0@@ -598,6 +599,7 @@ int amdxdna_cmd_submit(struct amdxdna_client *client, job->hwctx = hwctx; job->mm = current->mm; +mmgrab (job->mm);
[Severity: Critical]
This is a pre-existing issue and was not introduced by the patch under review, but should this use client->mm instead of current->mm?
If the file descriptor is shared across processes (for example, via fork), using current->mm binds the hardware job liveness check to the ioctl caller rather than the PASID owner.
Could this lead to cross-process DMA if Process A opens the device, binds IOMMU SVA to its mm (allocating PASID X), forks Process B, and then exits? The IOMMU would free PASID X, which could be reallocated to a new Process C.
If Process B then calls the submit ioctl, amdxdna_cmd_submit() assigns Process B's mm to job->mm. Since Process B is still alive, the liveness check in aie2_sched_job_run() would pass, and the hardware might access Process C's memory under Process B's control using the recycled PASID.
Quoted 2 lines
drivers/accel/amdxdna/amdxdna_ctx.c +0
-0job->fence =amdxdna_fence_create (hwctx);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1