Skip to contentSkip to conversation

drm/i915: Prevent use-after-free in invalidate_range_start callback

PATCH Applied Michał Winiarski 4 participants 4 messages Feb 3, 2015 – Feb 5, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915: Prevent use-after-free in invalidate_range_start callback

It's possible for invalidate_range_start mmu notifier callback to race against userptr object release. If the gem object was released prior to obtaining the spinlock in invalidate_range_start we're hitting null pointer dereference.

Testcase: igt/gem_userptr_blits/stress-mm-invalidate-close
Testcase: igt/gem_userptr_blits/stress-mm-invalidate-close-overlap

---
 drivers/gpu/drm/i915/i915_gem_userptr.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +11 -2
index d182058..64b8802 100644
@@ -113,7 +113,10 @@ restart:
 			continue;
 
 		obj = mo->obj;
-		drm_gem_object_reference(&obj->base);
+
+		if (!kref_get_unless_zero(&obj->base.refcount))
+			continue;
+
 		spin_unlock(&mn->lock);
 
 		cancel_userptr(obj);
@@ -149,7 +152,13 @@ static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
 			it = interval_tree_iter_first(&mn->objects, start, end);
 		if (it != NULL) {
 			obj = container_of(it, struct i915_mmu_object, it)->obj;
-			drm_gem_object_reference(&obj->base);
+
+			if (!kref_get_unless_zero(&obj->base.refcount)) {
+				spin_unlock(&mn->lock);
+				serial = 0;
+				continue;
+			}
+
 			serial = mn->serial;
 		}
 		spin_unlock(&mn->lock);
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Michał Winiarski

It's possible for invalidate_range_start mmu notifier callback to race against userptr object release. If the gem object was released prior to obtaining the spinlock in invalidate_range_start we're hitting null pointer dereference.

Testcase: igt/gem_userptr_blits/stress-mm-invalidate-close*

Though I would personally remove the extra newline and add an extra comment instead:

Quoted 20 lines
---
 drivers/gpu/drm/i915/i915_gem_userptr.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +3 -1
index d182058..64b8802 100644
@@ -113,7 +113,10 @@ restart:
 			continue;
 
 		obj = mo->obj;
-		drm_gem_object_reference(&obj->base);
+		if (!kref_get_unless_zero(&obj->base.refcount))
+			continue;
+
 		spin_unlock(&mn->lock);
 
 		cancel_userptr(obj);

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 152

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +0 -1
@@ -149,7 +152,13 @@ static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
 			it = interval_tree_iter_first(&mn->objects, start, end);
 		if (it != NULL) {
 			obj = container_of(it, struct i915_mmu_object, it)->obj;
-			drm_gem_object_reference(&obj->base);

/* The mmu_object is released late when
* destroying the GEM object so it is entirely
* possible to gain a reference on an object
* in the process of being freed since our
* serialisation is via the spinlock and not the
* struct_mutex - and consequently use it
* after it is freed and then double free it. */

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 164

Quoted 9 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +6 -0
+			if (!kref_get_unless_zero(&obj->base.refcount)) {
+				spin_unlock(&mn->lock);
+				serial = 0;
+				continue;
+			}
+
 			serial = mn->serial;
 		}
 		spin_unlock(&mn->lock);

-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw
Quoted 10 lines · Chris Wilson
Quoted 8 lines · Michał Winiarski

It's possible for invalidate_range_start mmu notifier callback to race against userptr object release. If the gem object was released prior to obtaining the spinlock in invalidate_range_start we're hitting null pointer dereference.

Testcase: igt/gem_userptr_blits/stress-mm-invalidate-close*

Since it blows up in the real world already also

and for Jani. And Jani can add the comment while applying, I like it - explaining that kind of weak ref stuff is always good. -Daniel

Quoted 52 lines

Though I would personally remove the extra newline and add an extra comment instead:

Quoted 20 lines
---
 drivers/gpu/drm/i915/i915_gem_userptr.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +3 -1
index d182058..64b8802 100644
@@ -113,7 +113,10 @@ restart:
 			continue;
 
 		obj = mo->obj;
-		drm_gem_object_reference(&obj->base);
+		if (!kref_get_unless_zero(&obj->base.refcount))
+			continue;
+
 		spin_unlock(&mn->lock);
 
 		cancel_userptr(obj);

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 152

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +0 -1
@@ -149,7 +152,13 @@ static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
 			it = interval_tree_iter_first(&mn->objects, start, end);
 		if (it != NULL) {
 			obj = container_of(it, struct i915_mmu_object, it)->obj;
-			drm_gem_object_reference(&obj->base);

/* The mmu_object is released late when
* destroying the GEM object so it is entirely
* possible to gain a reference on an object
* in the process of being freed since our
* serialisation is via the spinlock and not the
* struct_mutex - and consequently use it
* after it is freed and then double free it. */

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 164

Quoted 9 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +6 -0
+			if (!kref_get_unless_zero(&obj->base.refcount)) {
+				spin_unlock(&mn->lock);
+				serial = 0;
+				continue;
+			}
+
 			serial = mn->serial;
 		}
 		spin_unlock(&mn->lock);

-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
maintainer Applied
#3 lore raw
Quoted 10 lines · Daniel Vetter
Quoted 9 lines · Chris Wilson
Quoted 8 lines · Michał Winiarski

It's possible for invalidate_range_start mmu notifier callback to race against userptr object release. If the gem object was released prior to obtaining the spinlock in invalidate_range_start we're hitting null pointer dereference.

Testcase: igt/gem_userptr_blits/stress-mm-invalidate-close*

Quoted 8 lines
Quoted 1 line

Since it blows up in the real world already also

and for Jani. And Jani can add the comment while applying, I like it - explaining that kind of weak ref stuff is always good.

Pushed to drm-intel-next-fixes (and therefore headed for 3.20) with Chris' comment added.

BR,
Jani.

Quoted 59 lines

-Daniel

Quoted 52 lines

Though I would personally remove the extra newline and add an extra comment instead:

Quoted 20 lines
---
 drivers/gpu/drm/i915/i915_gem_userptr.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_gem_userptr.c +3 -1
index d182058..64b8802 100644
@@ -113,7 +113,10 @@ restart:
 			continue;
 
 		obj = mo->obj;
-		drm_gem_object_reference(&obj->base);
+		if (!kref_get_unless_zero(&obj->base.refcount))
+			continue;
+
 		spin_unlock(&mn->lock);
 
 		cancel_userptr(obj);

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 152

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +0 -1
@@ -149,7 +152,13 @@ static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn,
 			it = interval_tree_iter_first(&mn->objects, start, end);
 		if (it != NULL) {
 			obj = container_of(it, struct i915_mmu_object, it)->obj;
-			drm_gem_object_reference(&obj->base);

/* The mmu_object is released late when
* destroying the GEM object so it is entirely
* possible to gain a reference on an object
* in the process of being freed since our
* serialisation is via the spinlock and not the
* struct_mutex - and consequently use it
* after it is freed and then double free it. */

drivers/gpu/drm/i915/i915_gem_userptr.c static void i915_gem_userptr_mn_invalidate_range_start(struct mmu_notifier *_mn, · line 164

Quoted 9 lines
drivers/gpu/drm/i915/i915_gem_userptr.c +6 -0
+			if (!kref_get_unless_zero(&obj->base.refcount)) {
+				spin_unlock(&mn->lock);
+				serial = 0;
+				continue;
+			}
+
 			serial = mn->serial;
 		}
 		spin_unlock(&mn->lock);

-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Signature · 5 lines
Jani Nikula, Intel Open Source Technology Center
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx