Skip to contentSkip to conversation

accel/amdxdna: Fix use-after-free of mm_struct in job scheduler

PATCH In review Lizhi Hou 3 participants 3 messages Jul 16

Subsystems (2)
lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 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.

Fixes aac243092b70 ("accel/amdxdna: Add command execution")
---
 drivers/accel/amdxdna/amdxdna_ctx.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/accel/amdxdna/amdxdna_ctx.c +4 -0
index bdbd3db12a6c..31a414c3f0d9 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Lizhi Hou

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.

Fixes aac243092b70 ("accel/amdxdna: Add command execution")
Quoted 33 lines
---
  drivers/accel/amdxdna/amdxdna_ctx.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/accel/amdxdna/amdxdna_ctx.c +4 -0
index bdbd3db12a6c..31a414c3f0d9 100644
@@ -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;
  }
#2 lore raw
In reply to Lizhi Hou
Thank 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
index 855da8c79a1cd..ea560025ae79f 100644

[ ... ]

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 -0
 	job->fence = amdxdna_fence_create(hwctx);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1