Skip to contentSkip to conversation

dma-buf/dma-fence: fix checking signaling bit for timeline and driver name v2

PATCH In review Christian König 3 participants 5 messages Sep 14 – Sep 15

Subsystems (5)
lore
Share & tools
lore

Christian König

author reviewer
#0 lore raw

PATCH 1/2 dma-buf/dma-fence: fix checking signaling bit for timeline and driver name v2

The patch "dma-buf: dma-fence: Fix potential NULL pointer dereference" changed the check to test for the ops pointer instead of the signaled bit to avoid a potential NULL dereference when the ops pointer has been cleared.

The problem is now that the ops pointer is cleared only when neither the release nor the wait callback is implemented and this isn't true for a lot of dma_fence implementations yet. So those implementations lost the RCU protection after signaling of the returned string resulting in potential use after free.

Add the signaling check additional to the ops pointer check so that we have both the protection against NULL dereference as well as the RCU protection after signaling for the returned string.

v2: improve comments to note RCU protection and explain why we check
both signaling state and ops pointer

Fixes 035219a760ed ("dma-buf: dma-fence: Fix potential NULL pointer dereference")
---
 drivers/dma-buf/dma-fence.c | 16 ++++++++++++++--
 include/linux/dma-fence.h   |  6 ++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +14 -2
index 05090fb0fd5ae..f52675f3ba03a 100644
@@ -1170,7 +1170,13 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (ops)
+
+	/*
+	 * Check both signaled state and ops pointer, we don't know which one is
+	 * loaded first and ops pointer is only set to NULL on newer
+	 * implementations.
+	 */
+	if (!dma_fence_test_signaled_flag(fence) && ops)
 		return (const char __rcu *)ops->get_driver_name(fence);
 	else
 		return (const char __rcu *)"detached-driver";
@@ -1203,7 +1209,13 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)
 
 	/* RCU protection is required for safe access to returned string */
 	ops = rcu_dereference(fence->ops);
-	if (ops)
+
+	/*
+	 * Check both signaled state and ops pointer, we don't know which one is
+	 * loaded first and ops pointer is only set to NULL on newer
+	 * implementations.
+	 */
+	if (!dma_fence_test_signaled_flag(fence) && ops)
 		return (const char __rcu *)ops->get_timeline_name(fence);
 	else
 		return (const char __rcu *)"signaled-timeline";
include/linux/dma-fence.h +6 -0
index 158cd609f1036..ffa99b930843e 100644
@@ -141,6 +141,9 @@ struct dma_fence_ops {
 	 * compute the name at runtime, without having it to store permanently
 	 * for each fence, or build a cache of some sort.
 	 *
+	 * The returned string is RCU protected and can be freed after the fence
+	 * signaled and a RCU grace period passed.
+	 *
 	 * This callback is mandatory.
 	 */
 	const char * (*get_driver_name)(struct dma_fence *fence);
@@ -153,6 +156,9 @@ struct dma_fence_ops {
 	 * having it to store permanently for each fence, or build a cache of
 	 * some sort.
 	 *
+	 * The returned string is RCU protected and can be freed after the fence
+	 * signaled and a RCU grace period passed.
+	 *
 	 * This callback is mandatory.
 	 */
 	const char * (*get_timeline_name)(struct dma_fence *fence);
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 2/2 drm/sched: document the RCU dependency

Tvrkos patches added RCU protection to the returned strings from get_timeline_name()/get_driver_name() callbacks of the dma_fence backends.

This fixed use after free problems for a couple of drivers, but we never documented the consequences for the drm_sched_fence.

Add a few words on the function documentation to note that we need an RCU grace period between signaling the last scheduler fence and scheduler teardown.

---
 drivers/gpu/drm/scheduler/sched_main.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +5 -0
index 6cb6f95464938..f73c5141b4e5c 100644
@@ -1203,6 +1203,11 @@ static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched)
  * is implemented, all jobs will be canceled through it and afterwards cleaned
  * up through &struct drm_sched_backend_ops.free_job. If cancel_job is not
  * implemented, memory could leak.
+ *
+ * The user must wait one RCU grace period between signaling the last hardware-
+ * fence and calling this function because the timeline name returned by
+ * scheduler fences must stay valid until all readers have exited their RCU read
+ * side critical section.
  */
 void drm_sched_fini(struct drm_gpu_scheduler *sched)
 {
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 2/2] drm/sched: document the RCU dependency

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:
- [High] The documented RCU grace period requirement for scheduler teardown is insufficient to prevent Use-After-Free, and the underlying UAF remains reachable from userspace. --

commit 5a72d906ede5cec7818892f2331889b7b0dd6420
Author: Christian König <[email protected]>

drm/sched: document the RCU dependency

This commit documents the requirement to wait for an RCU grace period between signaling the last hardware fence and tearing down the scheduler.

Quoted 4 lines

drivers/gpu/drm/scheduler/sched_main.c static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched) · line 1212

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_main.c +5 -0
@@ -1203,6 +1203,11 @@ static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched)
  * is implemented, all jobs will be canceled through it and afterwards cleaned
  * up through &struct drm_sched_backend_ops.free_job. If cancel_job is not
  * implemented, memory could leak.
+ *
+ * The user must wait one RCU grace period between signaling the last hardware-
+ * fence and calling this function because the timeline name returned by
+ * scheduler fences must stay valid until all readers have exited their RCU read
+ * side critical section.
  */
 void drm_sched_fini(struct drm_gpu_scheduler *sched)

[Severity: High]
This is a pre-existing issue, but is waiting a single RCU grace period before calling drm_sched_fini() actually sufficient to prevent a use-after-free here?

If a user submits a job to obtain a sync_file fd for the scheduler fence, and then the GPU queue or context is destroyed while jobs are pending (leaving fences unsignaled), drivers like xe will tear down the scheduler and often free the embedding structure without waiting for an RCU grace period:

drivers/gpu/drm/xe/xe_execlist.c:execlist_exec_queue_fini() {
    ...
    drm_sched_fini(&exl->sched);
    ...
    kfree(exl);
}

Since the fence doesn't detach from the scheduler, a subsequent userspace query via SYNC_IOC_FILE_INFO will enter a new RCU read-side critical section and access the timeline name on a fence that has outlived its scheduler:

drivers/dma-buf/sync_file.c:sync_fill_fence_info() {
    ...
    rcu_read_lock();
    ...
    timeline = dma_fence_timeline_name(fence);
    ...
}

Which then blindly dereferences the freed scheduler pointer:

drivers/gpu/drm/scheduler/sched_fence.c:drm_sched_fence_get_timeline_name() {
    ...
    return (const char *)fence->sched->name;
}

Even if the driver waited for an RCU grace period before calling drm_sched_fini(), couldn't new RCU readers still start querying the unsignaled fence through sync_file long after the scheduler is freed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Acked-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] drm/sched: document the RCU dependency

In reply to Christian König
Quoted 1 line · Christian König

Tvrkos patches added RCU protection to the returned strings from

*Tvrtko ;)

Anyways, which patches? Better add a commit ID?

Quoted 8 lines

get_timeline_name()/get_driver_name() callbacks of the dma_fence backends.

This fixed use after free problems for a couple of drivers, but we never documented the consequences for the drm_sched_fence.

Add a few words on the function documentation to note that we need an RCU grace period between signaling the last scheduler fence and

s/scheduler fence/hardware-fence

:D

Quoted 3 lines

scheduler teardown.

with the above:

Quoted 20 lines

---
 drivers/gpu/drm/scheduler/sched_main.c | 5 +++++

 1 file changed, 5 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +5 -0
index 6cb6f95464938..f73c5141b4e5c 100644
@@ -1203,6 +1203,11 @@ static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched)
   * is implemented, all jobs will be canceled through it and afterwards cleaned
   * up through &struct drm_sched_backend_ops.free_job. If cancel_job is not
   * implemented, memory could leak.
+ *
+ * The user must wait one RCU grace period between signaling the last hardware-
+ * fence and calling this function because the timeline name returned by
+ * scheduler fences must stay valid until all readers have exited their RCU read
+ * side critical section.
   */
  void drm_sched_fini(struct drm_gpu_scheduler *sched)
  {
Reviewed-by on v1 1/2
#4 lore raw
In reply to Christian König
Quoted 15 lines · Christian König

The patch "dma-buf: dma-fence: Fix potential NULL pointer dereference" changed the check to test for the ops pointer instead of the signaled bit to avoid a potential NULL dereference when the ops pointer has been cleared.

The problem is now that the ops pointer is cleared only when neither the release nor the wait callback is implemented and this isn't true for a lot of dma_fence implementations yet. So those implementations lost the RCU protection after signaling of the returned string resulting in potential use after free.

Add the signaling check additional to the ops pointer check so that we have both the protection against NULL dereference as well as the RCU protection after signaling for the returned string.

Quoted 8 lines

v2: improve comments to note RCU protection and explain why we check both signaling state and ops pointer

Fixes 035219a760ed ("dma-buf: dma-fence: Fix potential NULL pointer dereference")
Quoted 13 lines

---
 drivers/dma-buf/dma-fence.c | 16 ++++++++++++++-- include/linux/dma-fence.h   |  6 ++++++

 2 files changed, 20 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +0 -0
index 05090fb0fd5ae..f52675f3ba03a 100644
@@ -1170,7 +1170,13 @@ const char __rcu *dma_fence_driver_name(struct dma_fence *fence)


  /* RCU protection is required for safe access to returned string */ ops = rcu_dereference(fence->ops);

drivers/dma-buf/dma-fence.c const char __rcu *dma_fence_driver_name(struct dma_fence *fence) · line 1178

Quoted 7 lines
drivers/dma-buf/dma-fence.c +6 -1
-	if (ops)
+
+	/*
+	 * Check both signaled state and ops pointer, we don't know which one is
+	 * loaded first and ops pointer is only set to NULL on newer
+	 * implementations.
+	 */

nit: "we don't know" doesn't give the reader confidence that the ordering is right. I would more say "Load ordering is irrelevant because …".

P.