Skip to contentSkip to conversation

drm/sched: Fix UAF in drm_sched_fence_get_timeline_name()

PATCH In review Rob Clark 4 participants 5 messages May 9, 2025 – May 12, 2025

Subsystems (3)
lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/sched: Fix UAF in drm_sched_fence_get_timeline_name()

From: Rob Clark <[email protected]>

The fence can outlive the sched, so it is not safe to dereference the sched in drm_sched_fence_get_timeline_name()

---
 drivers/gpu/drm/scheduler/sched_fence.c |  3 ++-
 include/drm/gpu_scheduler.h             | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_fence.c +2 -1
index e971528504a5..4e529c3ba6d4 100644
@@ -92,7 +92,7 @@ static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
 static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
 {
 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
-	return (const char *)fence->sched->name;
+	return fence->name;
 }
 
 static void drm_sched_fence_free_rcu(struct rcu_head *rcu)
@@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
 	unsigned seq;
 
 	fence->sched = entity->rq->sched;
+	fence->name  = fence->sched->name;
 	seq = atomic_inc_return(&entity->fence_seq);
 	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
 		       &fence->lock, entity->fence_context, seq);
include/drm/gpu_scheduler.h +11 -0
index 0ae108f6fcaf..d830ffe083f1 100644
@@ -295,6 +295,9 @@ struct drm_sched_fence {
         /**
          * @sched: the scheduler instance to which the job having this struct
          * belongs to.
+         *
+         * Some care must be taken as to where the sched is derefed, as the
+         * fence can outlive the sched.
          */
 	struct drm_gpu_scheduler	*sched;
         /**
@@ -305,6 +308,14 @@ struct drm_sched_fence {
          * @owner: job owner for debugging
          */
 	void				*owner;
+
+	/**
+	 * @name: the timeline name
+	 *
+	 * This comes from the @sched, but since the fence can outlive the
+	 * sched, we need to keep our own copy.
+	 */
+	const char			*name;
 };
 
 struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);
Signature · 1 line
2.49.0
#1 lore raw

Hi,

Quoted 4 lines · Rob Clark

From: Rob Clark <[email protected]>

The fence can outlive the sched, so it is not safe to dereference the sched in drm_sched_fence_get_timeline_name()

Thx for the fix. Looks correct to me. Some nits

Quoted 2 lines

This is clearly a bug. So please provide a Fixes: tag and +Cc the stable kernel.

Quoted 16 lines

---
 drivers/gpu/drm/scheduler/sched_fence.c |  3 ++- include/drm/gpu_scheduler.h             | 11 +++++++++++

 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_fence.c +0 -0
index e971528504a5..4e529c3ba6d4 100644
@@ -92,7 +92,7 @@ static const char
 *drm_sched_fence_get_driver_name(struct dma_fence *fence)

 static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
 {
  struct drm_sched_fence *fence = to_drm_sched_fence(f);

drivers/gpu/drm/scheduler/sched_fence.c static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence) · line 95

Quoted 2 lines
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
-	return (const char *)fence->sched->name;
+	return fence->name;

Adding an empty line while we're here already would be neat.

Quoted 21 lines

 }

 static void drm_sched_fence_free_rcu(struct rcu_head *rcu)

drivers/gpu/drm/scheduler/sched_fence.c +0 -0
@@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence
 *fence,
  	unsigned seq;


  fence->sched = entity->rq->sched;

drivers/gpu/drm/scheduler/sched_fence.c +1 -0
+	fence->name  = fence->sched->name;

  seq = atomic_inc_return(&entity->fence_seq); dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
         &fence->lock, entity->fence_context, seq);

include/drm/gpu_scheduler.h +0 -0
index 0ae108f6fcaf..d830ffe083f1 100644
@@ -295,6 +295,9 @@ struct drm_sched_fence {
          /**
           * @sched: the scheduler instance to which the job having
Quoted 7 lines

this struct
          * belongs to.
+         *
+         * Some care must be taken as to where the sched is derefed, as the
+         * fence can outlive the sched.
          */

Just briefly hinting at the lifetime is enough here. Every developer understands what that implicates.

"Might have a lifetime shorter than the owning &struct drm_sched_fence"

Thx
P.

Quoted 18 lines

  struct drm_gpu_scheduler *sched;
         /**

include/drm/gpu_scheduler.h +5 -0
@@ -305,6 +308,14 @@ struct drm_sched_fence {
           * @owner: job owner for debugging
           */
  	void				*owner;
+
+	/**
+	 * @name: the timeline name
+	 *
+	 * This comes from the @sched, but since the fence can

outlive the

include/drm/gpu_scheduler.h +3 -0
+	 * sched, we need to keep our own copy.
+	 */
+	const char			*name;

 };

 struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);

#2 lore raw
In reply to Rob Clark
Quoted 4 lines · Rob Clark

From: Rob Clark <[email protected]>

The fence can outlive the sched, so it is not safe to dereference the sched in drm_sched_fence_get_timeline_name()

Funny I've been working in the same problem space:

See https://lore.kernel.org/dri-devel/[email protected]/

Quoted 44 lines
---
  drivers/gpu/drm/scheduler/sched_fence.c |  3 ++-
  include/drm/gpu_scheduler.h             | 11 +++++++++++
  2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_fence.c +2 -1
index e971528504a5..4e529c3ba6d4 100644
@@ -92,7 +92,7 @@ static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
  static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
  {
  	struct drm_sched_fence *fence = to_drm_sched_fence(f);
-	return (const char *)fence->sched->name;
+	return fence->name;
  }
 
  static void drm_sched_fence_free_rcu(struct rcu_head *rcu)
@@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
  	unsigned seq;
 
  	fence->sched = entity->rq->sched;
+	fence->name  = fence->sched->name;
  	seq = atomic_inc_return(&entity->fence_seq);
  	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
  		       &fence->lock, entity->fence_context, seq);
include/drm/gpu_scheduler.h +3 -0
index 0ae108f6fcaf..d830ffe083f1 100644
@@ -295,6 +295,9 @@ struct drm_sched_fence {
          /**
           * @sched: the scheduler instance to which the job having this struct
           * belongs to.
+         *
+         * Some care must be taken as to where the sched is derefed, as the
+         * fence can outlive the sched.
           */
  	struct drm_gpu_scheduler	*sched;
          /**
@@ -305,6 +308,14 @@ struct drm_sched_fence {
           * @owner: job owner for debugging

include/drm/gpu_scheduler.h struct drm_sched_fence { · line 318

Quoted 10 lines
include/drm/gpu_scheduler.h +8 -0
           */
  	void				*owner;
+
+	/**
+	 * @name: the timeline name
+	 *
+	 * This comes from the @sched, but since the fence can outlive the
+	 * sched, we need to keep our own copy.
+	 */
+	const char			*name;

With drivers such as xe, fence->sched can indeed be freed, but so can sched->name, so it is not safe to store a copy of it. AFAICT only safe way is to simply give up on the real names for signalled fences.

Could you see if my series fixes the issue in your use case? I *think* by using the driver/timeline name wrappers I did catch all external access points and made them safe.

Regards,

Tvrtko

Quoted 3 lines
include/drm/gpu_scheduler.h +0 -0
  };
 
  struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f);
maintainer
#3 lore raw
In reply to Rob Clark
Quoted 25 lines · Rob Clark

From: Rob Clark <[email protected]>

The fence can outlive the sched, so it is not safe to dereference the sched in drm_sched_fence_get_timeline_name()

---
 drivers/gpu/drm/scheduler/sched_fence.c |  3 ++-
 include/drm/gpu_scheduler.h             | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
index e971528504a5..4e529c3ba6d4 100644
@@ -92,7 +92,7 @@ static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
 static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
 {
 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
-	return (const char *)fence->sched->name;
+	return fence->name;
 }
 
 static void drm_sched_fence_free_rcu(struct rcu_head *rcu)

drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_init(struct drm_sched_fence *fence, · line 229

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_fence.c +1 -0
@@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
 	unsigned seq;
 
 	fence->sched = entity->rq->sched;
+	fence->name  = fence->sched->name;

This requires sched->name to be a string in the .(ro)data section of the binary, or a string that the driver only ever frees after all fences of this scheduler have been freed.

Are we sure that those rules are documented and honored by existing drivers?

Otherwise, we might just fix one bug and create a more subtle one instead. :(

Quoted 18 lines
drivers/gpu/drm/scheduler/sched_fence.c +0 -0
 	seq = atomic_inc_return(&entity->fence_seq);
 	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
 		       &fence->lock, entity->fence_context, seq);
include/drm/gpu_scheduler.h +3 -0
index 0ae108f6fcaf..d830ffe083f1 100644
@@ -295,6 +295,9 @@ struct drm_sched_fence {
         /**
          * @sched: the scheduler instance to which the job having this struct
          * belongs to.
+         *
+         * Some care must be taken as to where the sched is derefed, as the
+         * fence can outlive the sched.
          */
 	struct drm_gpu_scheduler	*sched;
         /**

include/drm/gpu_scheduler.h struct drm_sched_fence { · line 316

Quoted 9 lines
include/drm/gpu_scheduler.h +6 -0
@@ -305,6 +308,14 @@ struct drm_sched_fence {
          * @owner: job owner for debugging
          */
 	void				*owner;
+
+	/**
+	 * @name: the timeline name
+	 *
+	 * This comes from the @sched, but since the fence can outlive the
+	 * sched, we need to keep our own copy.

It's our own copy of the pointer, not our own copy of the string. I think we should be clear about that.

Quoted 3 lines
include/drm/gpu_scheduler.h +2 -0
+	 */
+	const char			*name;
 };
author
#4 lore raw
Quoted 36 lines · Danilo Krummrich
Quoted 25 lines · Rob Clark

From: Rob Clark <[email protected]>

The fence can outlive the sched, so it is not safe to dereference the sched in drm_sched_fence_get_timeline_name()

---
 drivers/gpu/drm/scheduler/sched_fence.c |  3 ++-
 include/drm/gpu_scheduler.h             | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
index e971528504a5..4e529c3ba6d4 100644
@@ -92,7 +92,7 @@ static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
 static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
 {
      struct drm_sched_fence *fence = to_drm_sched_fence(f);
-     return (const char *)fence->sched->name;
+     return fence->name;
 }
 
 static void drm_sched_fence_free_rcu(struct rcu_head *rcu)

drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_init(struct drm_sched_fence *fence, · line 229

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_fence.c +1 -0
@@ -226,6 +226,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
      unsigned seq;
 
      fence->sched = entity->rq->sched;
+     fence->name  = fence->sched->name;

This requires sched->name to be a string in the .(ro)data section of the binary, or a string that the driver only ever frees after all fences of this scheduler have been freed.

Quoted 3 lines

Are we sure that those rules are documented and honored by existing drivers?

Otherwise, we might just fix one bug and create a more subtle one instead. :(

Agreed, but at least it is _less_ bad, and the alternative of refcnting the sched seemed pretty heavy handed :-(

I'll take a look at Tvrtko's series to see if that could help.

I suppose the alternative is to null out the sched ptr when the fence is signalled, and then return some generic name (ie "signalled" or something like that)?

BR,
-R

Quoted 35 lines
Quoted 19 lines
drivers/gpu/drm/scheduler/sched_fence.c +0 -0
      seq = atomic_inc_return(&entity->fence_seq);
      dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
                     &fence->lock, entity->fence_context, seq);
include/drm/gpu_scheduler.h +3 -0
index 0ae108f6fcaf..d830ffe083f1 100644
@@ -295,6 +295,9 @@ struct drm_sched_fence {
         /**
          * @sched: the scheduler instance to which the job having this struct
          * belongs to.
+         *
+         * Some care must be taken as to where the sched is derefed, as the
+         * fence can outlive the sched.
          */
      struct drm_gpu_scheduler        *sched;
         /**

include/drm/gpu_scheduler.h struct drm_sched_fence { · line 316

Quoted 9 lines
include/drm/gpu_scheduler.h +6 -0
@@ -305,6 +308,14 @@ struct drm_sched_fence {
          * @owner: job owner for debugging
          */
      void                            *owner;
+
+     /**
+      * @name: the timeline name
+      *
+      * This comes from the @sched, but since the fence can outlive the
+      * sched, we need to keep our own copy.

It's our own copy of the pointer, not our own copy of the string. I think we should be clear about that.

Quoted 3 lines
include/drm/gpu_scheduler.h +2 -0
+      */
+     const char                      *name;
 };