Skip to contentSkip to conversation

drm/xe/vm: move xa_alloc to prevent UAF

PATCH Superseded Matthew Auld 3 participants 7 messages Sep 23, 2024 – Sep 25, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/xe/vm: move xa_alloc to prevent UAF

Evil user can guess the next id of the vm before the ioctl completes and then call vm destroy ioctl to trigger UAF since create ioctl is still referencing the same vm. Move the xa_alloc all the way to the end to prevent this.

Fixes dd08ebf6c352 ("drm/xe: Introduce a new DRM driver for Intel GPUs")
Cc Matthew Brost <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/xe/xe_vm.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +10 -12
index a3d7cb7cfd22..f7182ef3d8e6 100644
@@ -1765,12 +1765,6 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 	if (IS_ERR(vm))
 		return PTR_ERR(vm);
 
-	mutex_lock(&xef->vm.lock);
-	err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
-	mutex_unlock(&xef->vm.lock);
-	if (err)
-		goto err_close_and_put;
-
 	if (xe->info.has_asid) {
 		down_write(&xe->usm.lock);
 		err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
@@ -1778,12 +1772,11 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 				      &xe->usm.next_asid, GFP_KERNEL);
 		up_write(&xe->usm.lock);
 		if (err < 0)
-			goto err_free_id;
+			goto err_close_and_put;
 
 		vm->usm.asid = asid;
 	}
 
-	args->vm_id = id;
 	vm->xef = xe_file_get(xef);
 
 	/* Record BO memory for VM pagetable created against client */
@@ -1796,12 +1789,17 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 	args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
 #endif
 
-	return 0;
-
-err_free_id:
+	/* user id alloc must always be last in ioctl to prevent UAF */
 	mutex_lock(&xef->vm.lock);
-	xa_erase(&xef->vm.xa, id);
+	err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
 	mutex_unlock(&xef->vm.lock);
+	if (err)
+		goto err_close_and_put;
+
+	args->vm_id = id;
+
+	return 0;
+
 err_close_and_put:
 	xe_vm_close_and_put(vm);
 
Signature · 1 line
2.46.1
#1 lore raw

PATCH 2/2 drm/xe/queue: move xa_alloc to prevent UAF

Evil user can guess the next id of the queue before the ioctl completes and then call queue destroy ioctl to trigger UAF since create ioctl is still referencing the same queue. Move the xa_alloc all the way to the end to prevent this.

Fixes 2149ded63079 ("drm/xe: Fix use after free when client stats are captured")
Cc Matthew Brost <[email protected]>
---
 drivers/gpu/drm/xe/xe_exec_queue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_exec_queue.c +3 -1
index 7f28b7fc68d5..a1d4b9b0726e 100644
@@ -635,6 +635,9 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 		}
 	}
 
+	q->xef = xe_file_get(xef);
+
+	/* user id alloc must always be last in ioctl to prevent UAF */
 	mutex_lock(&xef->exec_queue.lock);
 	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
 	mutex_unlock(&xef->exec_queue.lock);
@@ -642,7 +645,6 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 		goto kill_exec_queue;
 
 	args->exec_queue_id = id;
-	q->xef = xe_file_get(xef);
 
 	return 0;
 
Signature · 1 line
2.46.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/xe/queue: move xa_alloc to prevent UAF

Quoted 17 lines · Matthew Auld

Evil user can guess the next id of the queue before the ioctl completes and then call queue destroy ioctl to trigger UAF since create ioctl is still referencing the same queue. Move the xa_alloc all the way to the end to prevent this.

Fixes 2149ded63079 ("drm/xe: Fix use after free when client stats are captured")
Cc Matthew Brost <[email protected]>
---
 drivers/gpu/drm/xe/xe_exec_queue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_exec_queue.c +0 -0
index 7f28b7fc68d5..a1d4b9b0726e 100644

drivers/gpu/drm/xe/xe_exec_queue.c int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data, · line 638

Quoted 4 lines
drivers/gpu/drm/xe/xe_exec_queue.c +1 -0
@@ -635,6 +635,9 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 		}
 	}
 
+	q->xef = xe_file_get(xef);

Same comment as last patch, this looks coreect shall I merge my series and then you rebase?

Matt

Quoted 16 lines
drivers/gpu/drm/xe/xe_exec_queue.c +2 -1
+
+	/* user id alloc must always be last in ioctl to prevent UAF */
 	mutex_lock(&xef->exec_queue.lock);
 	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
 	mutex_unlock(&xef->exec_queue.lock);
@@ -642,7 +645,6 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
 		goto kill_exec_queue;
 
 	args->exec_queue_id = id;
-	q->xef = xe_file_get(xef);
 
 	return 0;
 
Signature · 1 line
2.46.1
#3 lore raw

Re: [PATCH 2/2] drm/xe/queue: move xa_alloc to prevent UAF

Quoted 21 lines · Matthew Brost
Quoted 20 lines · Matthew Auld

Evil user can guess the next id of the queue before the ioctl completes and then call queue destroy ioctl to trigger UAF since create ioctl is still referencing the same queue. Move the xa_alloc all the way to the end to prevent this.

Fixes 2149ded63079 ("drm/xe: Fix use after free when client stats are captured")
Cc Matthew Brost <[email protected]>
---
  drivers/gpu/drm/xe/xe_exec_queue.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_exec_queue.c +0 -0
index 7f28b7fc68d5..a1d4b9b0726e 100644
@@ -635,6 +635,9 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
  		}
  	}
 
Quoted 4 lines

drivers/gpu/drm/xe/xe_exec_queue.c int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data, · line 638

Quoted 1 line
drivers/gpu/drm/xe/xe_exec_queue.c +1 -0
+	q->xef = xe_file_get(xef);

Same comment as last patch, this looks coreect shall I merge my series and then you rebase?

Yeah, sounds fine.

Quoted 19 lines

Matt

Quoted 16 lines
drivers/gpu/drm/xe/xe_exec_queue.c +2 -1
+
+	/* user id alloc must always be last in ioctl to prevent UAF */
  	mutex_lock(&xef->exec_queue.lock);
  	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
  	mutex_unlock(&xef->exec_queue.lock);
@@ -642,7 +645,6 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
  		goto kill_exec_queue;
 
  	args->exec_queue_id = id;
-	q->xef = xe_file_get(xef);
 
  	return 0;
 
Signature · 1 line
2.46.1
maintainer
#4 lore raw

Re: [PATCH 2/2] drm/xe/queue: move xa_alloc to prevent UAF

Quoted 26 lines · Matthew Auld
Quoted 13 lines · Matthew Brost
Quoted 12 lines · Matthew Auld

Evil user can guess the next id of the queue before the ioctl completes and then call queue destroy ioctl to trigger UAF since create ioctl is still referencing the same queue. Move the xa_alloc all the way to the end to prevent this.

Fixes 2149ded63079 ("drm/xe: Fix use after free when client stats are captured")
Cc Matthew Brost <[email protected]>
---
  drivers/gpu/drm/xe/xe_exec_queue.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 11 lines

drivers/gpu/drm/xe/xe_exec_queue.c int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data, · line 637

Quoted 8 lines
drivers/gpu/drm/xe/xe_exec_queue.c +1 -0
index 7f28b7fc68d5..a1d4b9b0726e 100644
@@ -635,6 +635,9 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
  		}
  	}
+	q->xef = xe_file_get(xef);

Same comment as last patch, this looks coreect shall I merge my series and then you rebase?

Quoted 2 lines

Yeah, sounds fine.

My series is merged, feel free to rebase at your convenience.

Matt

Quoted 16 lines
Quoted 16 lines

Matt

Quoted 13 lines
drivers/gpu/drm/xe/xe_exec_queue.c +2 -1
+
+	/* user id alloc must always be last in ioctl to prevent UAF */
  	mutex_lock(&xef->exec_queue.lock);
  	err = xa_alloc(&xef->exec_queue.xa, &id, q, xa_limit_32b, GFP_KERNEL);
  	mutex_unlock(&xef->exec_queue.lock);
@@ -642,7 +645,6 @@ int xe_exec_queue_create_ioctl(struct drm_device *dev, void *data,
  		goto kill_exec_queue;
  	args->exec_queue_id = id;
-	q->xef = xe_file_get(xef);
  	return 0;
Signature · 1 line
2.46.1
maintainer
#5 lore raw
In reply to Matthew Auld
Quoted 5 lines · Matthew Auld

Evil user can guess the next id of the vm before the ioctl completes and then call vm destroy ioctl to trigger UAF since create ioctl is still referencing the same vm. Move the xa_alloc all the way to the end to prevent this.

I think this is correct. Shall I merge this series [1] first and then you can rebase?

Matt

[1] https://patchwork.freedesktop.org/patch/615303/?series=138938&rev=1

Quoted 64 lines
Fixes dd08ebf6c352 ("drm/xe: Introduce a new DRM driver for Intel GPUs")
Cc Matthew Brost <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/xe/xe_vm.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +10 -12
index a3d7cb7cfd22..f7182ef3d8e6 100644
@@ -1765,12 +1765,6 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 	if (IS_ERR(vm))
 		return PTR_ERR(vm);
 
-	mutex_lock(&xef->vm.lock);
-	err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
-	mutex_unlock(&xef->vm.lock);
-	if (err)
-		goto err_close_and_put;
-
 	if (xe->info.has_asid) {
 		down_write(&xe->usm.lock);
 		err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
@@ -1778,12 +1772,11 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 				      &xe->usm.next_asid, GFP_KERNEL);
 		up_write(&xe->usm.lock);
 		if (err < 0)
-			goto err_free_id;
+			goto err_close_and_put;
 
 		vm->usm.asid = asid;
 	}
 
-	args->vm_id = id;
 	vm->xef = xe_file_get(xef);
 
 	/* Record BO memory for VM pagetable created against client */
@@ -1796,12 +1789,17 @@ int xe_vm_create_ioctl(struct drm_device *dev, void *data,
 	args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
 #endif
 
-	return 0;
-
-err_free_id:
+	/* user id alloc must always be last in ioctl to prevent UAF */
 	mutex_lock(&xef->vm.lock);
-	xa_erase(&xef->vm.xa, id);
+	err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
 	mutex_unlock(&xef->vm.lock);
+	if (err)
+		goto err_close_and_put;
+
+	args->vm_id = id;
+
+	return 0;
+
 err_close_and_put:
 	xe_vm_close_and_put(vm);
 
Signature · 1 line
2.46.1
#6 lore raw

✗ CI.Patch_applied: failure for series starting with [1/2] drm/xe/vm: move xa_alloc to prevent UAF

In reply to Matthew Auld
== Series Details ==

== Series Details ==

Series: series starting with [1/2] drm/xe/vm: move xa_alloc to prevent UAF
URL : https://patchwork.freedesktop.org/series/138995/
State : failure

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 88d592f72f7b drm-tip: 2024y-09m-25d-19h-23m-21s UTC integration manifest === git am output follows ===
error: patch failed: drivers/gpu/drm/xe/xe_vm.c:1765
error: drivers/gpu/drm/xe/xe_vm.c: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Applying: drm/xe/vm: move xa_alloc to prevent UAF
Patch failed at 0001 drm/xe/vm: move xa_alloc to prevent UAF
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".