Skip to contentSkip to conversation

dma-buf: fix stack corruption in dma_fence_chain_release

PATCH Lionel Landwerlin 3 participants 5 messages Aug 5, 2019 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Lionel Landwerlin

author Acked-by
#1 lore raw
Quoted 5 lines · Christian König

We can't free up the chain using recursion or we run into a stack overflow.

Manually free up the dangling chain nodes to avoid recursion.

That definitely makes sense to me, but I'm not an expert in RCU foo :/

I guess this deserves a

Fixes 7bf60c52e0 ("dma-buf: add new dma_fence_chain container v7")
Quoted 30 lines
---
  drivers/dma-buf/dma-fence-chain.c | 24 +++++++++++++++++++++++-
  1 file changed, 23 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-chain.c +18 -0
index b5089f64be2a..44a741677d25 100644
@@ -178,8 +178,30 @@ static bool dma_fence_chain_signaled(struct dma_fence *fence)
  static void dma_fence_chain_release(struct dma_fence *fence)
  {
  	struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+	struct dma_fence *prev;
+
+	/* Manually unlink the chain as much as possible to avoid recursion
+	 * and potential stack overflow.
+	 */
+	while ((prev = rcu_dereference_protected(chain->prev, true))) {
+		struct dma_fence_chain *prev_chain;
+
+		if (kref_read(&prev->refcount) > 1)
+		       break;
+
+		prev_chain = to_dma_fence_chain(prev);
+		if (!prev_chain)
+			break;
+
+		/* No need for atomic operations since we hold the last
+		 * reference to prev_chain.
+		 */

drivers/dma-buf/dma-fence-chain.c static bool dma_fence_chain_signaled(struct dma_fence *fence) · line 207

Quoted 10 lines
drivers/dma-buf/dma-fence-chain.c +5 -1
+		chain->prev = prev_chain->prev;
+		RCU_INIT_POINTER(prev_chain->prev, NULL);
+		dma_fence_put(prev);
+	}
+	dma_fence_put(prev);
 
-	dma_fence_put(rcu_dereference_protected(chain->prev, true));
  	dma_fence_put(chain->fence);
  	dma_fence_free(fence);
  }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
In reply to a message not in the archive

By any change, did you run into this with a CTS test whose name ends with ".chain" ? :)

-Lionel

Quoted 35 lines · Christian König

We can't free up the chain using recursion or we run into a stack overflow.

Manually free up the dangling chain nodes to avoid recursion.

---
  drivers/dma-buf/dma-fence-chain.c | 24 +++++++++++++++++++++++-
  1 file changed, 23 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-chain.c +18 -0
index b5089f64be2a..44a741677d25 100644
@@ -178,8 +178,30 @@ static bool dma_fence_chain_signaled(struct dma_fence *fence)
  static void dma_fence_chain_release(struct dma_fence *fence)
  {
  	struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+	struct dma_fence *prev;
+
+	/* Manually unlink the chain as much as possible to avoid recursion
+	 * and potential stack overflow.
+	 */
+	while ((prev = rcu_dereference_protected(chain->prev, true))) {
+		struct dma_fence_chain *prev_chain;
+
+		if (kref_read(&prev->refcount) > 1)
+		       break;
+
+		prev_chain = to_dma_fence_chain(prev);
+		if (!prev_chain)
+			break;
+
+		/* No need for atomic operations since we hold the last
+		 * reference to prev_chain.
+		 */

drivers/dma-buf/dma-fence-chain.c static bool dma_fence_chain_signaled(struct dma_fence *fence) · line 207

Quoted 10 lines
drivers/dma-buf/dma-fence-chain.c +5 -1
+		chain->prev = prev_chain->prev;
+		RCU_INIT_POINTER(prev_chain->prev, NULL);
+		dma_fence_put(prev);
+	}
+	dma_fence_put(prev);
 
-	dma_fence_put(rcu_dereference_protected(chain->prev, true));
  	dma_fence_put(chain->fence);
  	dma_fence_free(fence);
  }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Not even remotely :)I tested this with my own crafted code inside the kernel.

It's probably quite some hassle to actually trigger this problem from userspace and I only found it because I created a very very long sequence chain by accident.

Christian.

Am 05.08.19 um 14:03 schrieb Lionel Landwerlin:

Quoted 42 lines

By any change, did you run into this with a CTS test whose name ends with ".chain" ? :)

-Lionel

Quoted 37 lines · Christian König

We can't free up the chain using recursion or we run into a stack overflow.

Manually free up the dangling chain nodes to avoid recursion.

---
  drivers/dma-buf/dma-fence-chain.c | 24 +++++++++++++++++++++++-

  1 file changed, 23 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-chain.c +19 -0
index b5089f64be2a..44a741677d25 100644
@@ -178,8 +178,30 @@ static bool dma_fence_chain_signaled(struct 
 dma_fence *fence)
   static void dma_fence_chain_release(struct dma_fence *fence)
   {
       struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+    struct dma_fence *prev;
+
+    /* Manually unlink the chain as much as possible to avoid recursion
+     * and potential stack overflow.
+     */
+    while ((prev = rcu_dereference_protected(chain->prev, true))) {
+        struct dma_fence_chain *prev_chain;
+
+        if (kref_read(&prev->refcount) > 1)
+               break;
+
+        prev_chain = to_dma_fence_chain(prev);
+        if (!prev_chain)
+            break;
+
+        /* No need for atomic operations since we hold the last
+         * reference to prev_chain.
+         */
+        chain->prev = prev_chain->prev;
Quoted 10 lines
Quoted 8 lines
drivers/dma-buf/dma-fence-chain.c +4 -0
+        RCU_INIT_POINTER(prev_chain->prev, NULL);
+        dma_fence_put(prev);
+    }
+    dma_fence_put(prev);
   -    dma_fence_put(rcu_dereference_protected(chain->prev, true));
       dma_fence_put(chain->fence);
       dma_fence_free(fence);

  }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

That one test creates a 32k chain of fences I think. Anyway my kernel crash was unrelated ;)

-Lionel

Quoted 62 lines · Christian König

Not even remotely :)I tested this with my own crafted code inside the kernel.

It's probably quite some hassle to actually trigger this problem from userspace and I only found it because I created a very very long sequence chain by accident.

Christian.

Am 05.08.19 um 14:03 schrieb Lionel Landwerlin:

Quoted 53 lines

By any change, did you run into this with a CTS test whose name ends with ".chain" ? :)

-Lionel

Quoted 46 lines · Christian König

We can't free up the chain using recursion or we run into a stack overflow.

Manually free up the dangling chain nodes to avoid recursion.

---
  drivers/dma-buf/dma-fence-chain.c | 24 +++++++++++++++++++++++-

  1 file changed, 23 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-chain.c +3 -0
index b5089f64be2a..44a741677d25 100644
@@ -178,8 +178,30 @@ static bool dma_fence_chain_signaled(struct 
 dma_fence *fence)
   static void dma_fence_chain_release(struct dma_fence *fence)
   {
       struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+    struct dma_fence *prev;
+
+    /* Manually unlink the chain as much as possible to avoid 

recursion
+     * and potential stack overflow.
+     */
+    while ((prev = rcu_dereference_protected(chain->prev, true))) { +        struct dma_fence_chain *prev_chain;

drivers/dma-buf/dma-fence-chain.c +16 -0
+
+        if (kref_read(&prev->refcount) > 1)
+               break;
+
+        prev_chain = to_dma_fence_chain(prev);
+        if (!prev_chain)
+            break;
+
+        /* No need for atomic operations since we hold the last
+         * reference to prev_chain.
+         */
+        chain->prev = prev_chain->prev;
+        RCU_INIT_POINTER(prev_chain->prev, NULL);
+        dma_fence_put(prev);
+    }
+    dma_fence_put(prev);

  -    dma_fence_put(rcu_dereference_protected(chain->prev, true)); dma_fence_put(chain->fence); dma_fence_free(fence);
  }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 2 lines · Lionel Landwerlin

That one test creates a 32k chain of fences I think. Anyway my kernel crash was unrelated ;)

Hm I'd expect that with clever use of vgem fake fences we should be able to repro this bug here with an igt. Would be real nice, any takers? -Daniel

Quoted 77 lines

-Lionel

Quoted 68 lines · Christian König

Not even remotely :)I tested this with my own crafted code inside the kernel.

It's probably quite some hassle to actually trigger this problem from userspace and I only found it because I created a very very long sequence chain by accident.

Christian.

Am 05.08.19 um 14:03 schrieb Lionel Landwerlin:

Quoted 56 lines

By any change, did you run into this with a CTS test whose name ends with ".chain" ? :)

-Lionel

Quoted 48 lines · Christian König

We can't free up the chain using recursion or we run into a stack overflow.

Manually free up the dangling chain nodes to avoid recursion.

---
  drivers/dma-buf/dma-fence-chain.c | 24 +++++++++++++++++++++++-

  1 file changed, 23 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence-chain.c +3 -0
index b5089f64be2a..44a741677d25 100644
@@ -178,8 +178,30 @@ static bool dma_fence_chain_signaled(struct
 dma_fence *fence)
   static void dma_fence_chain_release(struct dma_fence *fence)
   {
       struct dma_fence_chain *chain = to_dma_fence_chain(fence);
+    struct dma_fence *prev;
+
+    /* Manually unlink the chain as much as possible to avoid

recursion
+     * and potential stack overflow.
+     */
+    while ((prev = rcu_dereference_protected(chain->prev, true))) { +        struct dma_fence_chain *prev_chain;

drivers/dma-buf/dma-fence-chain.c +16 -0
+
+        if (kref_read(&prev->refcount) > 1)
+               break;
+
+        prev_chain = to_dma_fence_chain(prev);
+        if (!prev_chain)
+            break;
+
+        /* No need for atomic operations since we hold the last
+         * reference to prev_chain.
+         */
+        chain->prev = prev_chain->prev;
+        RCU_INIT_POINTER(prev_chain->prev, NULL);
+        dma_fence_put(prev);
+    }
+    dma_fence_put(prev);

  -    dma_fence_put(rcu_dereference_protected(chain->prev, true)); dma_fence_put(chain->fence); dma_fence_free(fence);
  }

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel