Skip to contentSkip to conversation

etnaviv: Fix double-free in etna_bo_cache_free()

PATCH In review Marek Vasut 3 participants 3 messages Jun 1, 2019 – Jun 27, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 etnaviv: Fix double-free in etna_bo_cache_free()

The following situation can happen in a multithreaded OpenGL application. A BO is submitted from etna_cmd_stream #1 with flags set for read. A BO is submitted from etna_cmd_stream #2 with flags set for write. This triggers a flush on stream #1 and clears the BO's current_stream pointer. If at this point, stream #2 attempts to queue BO again, which does happen, the BO will be added to the submit list twice. The Linux kernel driver correctly detects this and warns about it with "BO at index %u already on submit list" kernel message.

However, when cleaning the BO cache in etna_bo_cache_free(), the BO which was submitted twice will also be free()d twice, this triggering a glibc double free detector.

The fix is easy, even if the BO does not have current_stream set, iterate over current streams' list of BOs before adding the BO to it and verify that the BO is not yet there.

Cc Christian Gmeiner <[email protected]>
Cc Lucas Stach <[email protected]>
---
 etnaviv/etnaviv_cmd_stream.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
etnaviv/etnaviv_cmd_stream.c +3 -5
index 7139c324..261777b0 100644
@@ -150,11 +150,7 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
 
 	pthread_mutex_lock(&idx_lock);
 
-	if (!bo->current_stream) {
-		idx = append_bo(stream, bo);
-		bo->current_stream = stream;
-		bo->idx = idx;
-	} else if (bo->current_stream == stream) {
+	if (bo->current_stream == stream) {
 		idx = bo->idx;
 	} else {
 		/* slow-path: */
@@ -165,6 +161,8 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
 			/* not found */
 			idx = append_bo(stream, bo);
 		}
+		bo->current_stream = stream;
+		bo->idx = idx;
 	}
 	pthread_mutex_unlock(&idx_lock);
 
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#1 lore raw

Am So., 2. Juni 2019 um 01:37 Uhr schrieb Marek Vasut <[email protected]>:

Quoted 14 lines

The following situation can happen in a multithreaded OpenGL application. A BO is submitted from etna_cmd_stream #1 with flags set for read. A BO is submitted from etna_cmd_stream #2 with flags set for write. This triggers a flush on stream #1 and clears the BO's current_stream pointer. If at this point, stream #2 attempts to queue BO again, which does happen, the BO will be added to the submit list twice. The Linux kernel driver correctly detects this and warns about it with "BO at index %u already on submit list" kernel message.

However, when cleaning the BO cache in etna_bo_cache_free(), the BO which was submitted twice will also be free()d twice, this triggering a glibc double free detector.

Quoted 7 lines

The fix is easy, even if the BO does not have current_stream set, iterate over current streams' list of BOs before adding the BO to it and verify that the BO is not yet there.

Cc Christian Gmeiner <[email protected]>
Cc Lucas Stach <[email protected]>

Will land this patch even libdrm parts got moved to mesa as it fixes a serious issue and somebody might use newest libdrm with mesa < 19.1.

Quoted 33 lines
---
 etnaviv/etnaviv_cmd_stream.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
etnaviv/etnaviv_cmd_stream.c +3 -6
index 7139c324..261777b0 100644
@@ -150,11 +150,7 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
 
        pthread_mutex_lock(&idx_lock);
 
-       if (!bo->current_stream) {
-               idx = append_bo(stream, bo);
-               bo->current_stream = stream;
-               bo->idx = idx;
-       } else if (bo->current_stream == stream) {
+       if (bo->current_stream == stream) {
                idx = bo->idx;
        } else {
                /* slow-path: */
@@ -165,6 +161,8 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
                        /* not found */
                        idx = append_bo(stream, bo);
                }
+               bo->current_stream = stream;
+               bo->idx = idx;
        }
        pthread_mutex_unlock(&idx_lock);
 
--

2.20.1

Signature · 9 lines
greets
--
Christian Gmeiner, MSc

https://christian-gmeiner.info/privacypolicy
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#2 lore raw
In reply to Marek Vasut
Quoted 17 lines · Marek Vasut

The following situation can happen in a multithreaded OpenGL application. A BO is submitted from etna_cmd_stream #1 with flags set for read. A BO is submitted from etna_cmd_stream #2 with flags set for write. This triggers a flush on stream #1 and clears the BO's current_stream pointer. If at this point, stream #2 attempts to queue BO again, which does happen, the BO will be added to the submit list twice. The Linux kernel driver correctly detects this and warns about it with "BO at index %u already on submit list" kernel message.

However, when cleaning the BO cache in etna_bo_cache_free(), the BO which was submitted twice will also be free()d twice, this triggering a glibc double free detector.

The fix is easy, even if the BO does not have current_stream set, iterate over current streams' list of BOs before adding the BO to it and verify that the BO is not yet there.

Quoted 6 lines
Cc Christian Gmeiner <[email protected]>
Cc Lucas Stach <[email protected]>
---
 etnaviv/etnaviv_cmd_stream.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Thanks for catching this.

Quoted 33 lines
etnaviv/etnaviv_cmd_stream.c +3 -5
index 7139c324..261777b0 100644
@@ -150,11 +150,7 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
 
 	pthread_mutex_lock(&idx_lock);
 
-	if (!bo->current_stream) {
-		idx = append_bo(stream, bo);
-		bo->current_stream = stream;
-		bo->idx = idx;
-	} else if (bo->current_stream == stream) {
+	if (bo->current_stream == stream) {
 		idx = bo->idx;
 	} else {
 		/* slow-path: */
@@ -165,6 +161,8 @@ static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
 			/* not found */
 			idx = append_bo(stream, bo);
 		}
+		bo->current_stream = stream;
+		bo->idx = idx;
 	}
 	pthread_mutex_unlock(&idx_lock);
 
Signature · 6 lines
2.20.1

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

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