Skip to contentSkip to conversation

tee: avoid accessing dma-buf shared memory after put

PATCH In review Hongyan Xu 3 participants 4 messages Aug 6 – Nov 9

Subsystems (2)
lore
Share & tools
lore

Hongyan Xu

author
#0 lore raw

PATCH 1/1 tee: avoid accessing dma-buf shared memory after put

param_from_user_memref() obtains a temporary reference to the dma-buf tee_shm wrapper by ID. If closing the shared-memory file descriptor races with the ioctl, this temporary reference can be the wrapper's last one. tee_shm_put() then frees the enclosing tee_shm_dmabuf_ref, but the function still reads ref->parent_shm and ref->offset afterwards.

Save the parent pointer and offset before dropping the wrapper reference. The additional parent reference keeps the parent shared memory alive for the parameter list as before.

Fixes 146bf4e75eca ("tee: new ioctl to a register tee_shm from a dmabuf file descriptor")
---
 drivers/tee/tee_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/tee/tee_core.c +5 -3
index 1aac50c7c1de..489c39d5b35f 100644
@@ -397,6 +397,7 @@ static int param_from_user_memref(struct tee_context *ctx,
 		}
 
 		if (shm->flags & TEE_SHM_DMA_BUF) {
+			struct tee_shm *parent_shm;
 			struct tee_shm_dmabuf_ref *ref;
 
 			ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -409,10 +410,11 @@ static int param_from_user_memref(struct tee_context *ctx,
 				 * list instead of the shm we got with
 				 * tee_shm_get_from_id() above.
 				 */
-				refcount_inc(&ref->parent_shm->refcount);
-				tee_shm_put(shm);
-				shm = ref->parent_shm;
+				parent_shm = ref->parent_shm;
 				offs = ref->offset;
+				refcount_inc(&parent_shm->refcount);
+				tee_shm_put(shm);
+				shm = parent_shm;
 			}
 		}
 	} else if (ctx->cap_memref_null) {
Signature · 1 line
2.50.1.windows.1
#1 lore raw

Hi,

Quoted 46 lines · Hongyan Xu

param_from_user_memref() obtains a temporary reference to the dma-buf tee_shm wrapper by ID. If closing the shared-memory file descriptor races with the ioctl, this temporary reference can be the wrapper's last one. tee_shm_put() then frees the enclosing tee_shm_dmabuf_ref, but the function still reads ref->parent_shm and ref->offset afterwards.

Save the parent pointer and offset before dropping the wrapper reference. The additional parent reference keeps the parent shared memory alive for the parameter list as before.

Fixes 146bf4e75eca ("tee: new ioctl to a register tee_shm from a dmabuf file descriptor")
---
 drivers/tee/tee_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/tee/tee_core.c +5 -4
index 1aac50c7c1de..489c39d5b35f 100644
@@ -397,6 +397,7 @@ static int param_from_user_memref(struct tee_context *ctx,
                }
 
                if (shm->flags & TEE_SHM_DMA_BUF) {
+                       struct tee_shm *parent_shm;
                        struct tee_shm_dmabuf_ref *ref;
 
                        ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -409,10 +410,11 @@ static int param_from_user_memref(struct tee_context *ctx,
                                 * list instead of the shm we got with
                                 * tee_shm_get_from_id() above.
                                 */
-                               refcount_inc(&ref->parent_shm->refcount);
-                               tee_shm_put(shm);
-                               shm = ref->parent_shm;
+                               parent_shm = ref->parent_shm;
                                offs = ref->offset;
+                               refcount_inc(&parent_shm->refcount);
+                               tee_shm_put(shm);
+                               shm = parent_shm;
                        }
                }
        } else if (ctx->cap_memref_null) {
--
Quoted 2 lines

2.50.1.windows.1

Looks good. Sumit, do you agree?

Cheers,
Jens

reviewer Reviewed-by
#2 lore raw
Quoted 45 lines · Jens Wiklander

Hi,

Quoted 42 lines · Hongyan Xu

param_from_user_memref() obtains a temporary reference to the dma-buf tee_shm wrapper by ID. If closing the shared-memory file descriptor races with the ioctl, this temporary reference can be the wrapper's last one. tee_shm_put() then frees the enclosing tee_shm_dmabuf_ref, but the function still reads ref->parent_shm and ref->offset afterwards.

Save the parent pointer and offset before dropping the wrapper reference. The additional parent reference keeps the parent shared memory alive for the parameter list as before.

Fixes 146bf4e75eca ("tee: new ioctl to a register tee_shm from a dmabuf file descriptor")
---
 drivers/tee/tee_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/tee/tee_core.c +5 -3
index 1aac50c7c1de..489c39d5b35f 100644
@@ -397,6 +397,7 @@ static int param_from_user_memref(struct tee_context *ctx,
                }
 
                if (shm->flags & TEE_SHM_DMA_BUF) {
+                       struct tee_shm *parent_shm;
                        struct tee_shm_dmabuf_ref *ref;
 
                        ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -409,10 +410,11 @@ static int param_from_user_memref(struct tee_context *ctx,
                                 * list instead of the shm we got with
                                 * tee_shm_get_from_id() above.
                                 */
-                               refcount_inc(&ref->parent_shm->refcount);
-                               tee_shm_put(shm);
-                               shm = ref->parent_shm;
+                               parent_shm = ref->parent_shm;
                                offs = ref->offset;
+                               refcount_inc(&parent_shm->refcount);
+                               tee_shm_put(shm);
+                               shm = parent_shm;
Quoted 9 lines
Quoted 6 lines
drivers/tee/tee_core.c +0 -1
                        }
                }
        } else if (ctx->cap_memref_null) {
--

2.50.1.windows.1

Looks good. Sumit, do you agree?

Sounds reasonable to me, FWIW:

-Sumit

#3 lore raw
Quoted 57 lines · Sumit Garg
Quoted 45 lines · Jens Wiklander

Hi,

Quoted 42 lines · Hongyan Xu

param_from_user_memref() obtains a temporary reference to the dma-buf tee_shm wrapper by ID. If closing the shared-memory file descriptor races with the ioctl, this temporary reference can be the wrapper's last one. tee_shm_put() then frees the enclosing tee_shm_dmabuf_ref, but the function still reads ref->parent_shm and ref->offset afterwards.

Save the parent pointer and offset before dropping the wrapper reference. The additional parent reference keeps the parent shared memory alive for the parameter list as before.

Fixes 146bf4e75eca ("tee: new ioctl to a register tee_shm from a dmabuf file descriptor")
---
 drivers/tee/tee_core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/tee/tee_core.c +5 -3
index 1aac50c7c1de..489c39d5b35f 100644
@@ -397,6 +397,7 @@ static int param_from_user_memref(struct tee_context *ctx,
                }
 
                if (shm->flags & TEE_SHM_DMA_BUF) {
+                       struct tee_shm *parent_shm;
                        struct tee_shm_dmabuf_ref *ref;
 
                        ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -409,10 +410,11 @@ static int param_from_user_memref(struct tee_context *ctx,
                                 * list instead of the shm we got with
                                 * tee_shm_get_from_id() above.
                                 */
-                               refcount_inc(&ref->parent_shm->refcount);
-                               tee_shm_put(shm);
-                               shm = ref->parent_shm;
+                               parent_shm = ref->parent_shm;
                                offs = ref->offset;
+                               refcount_inc(&parent_shm->refcount);
+                               tee_shm_put(shm);
+                               shm = parent_shm;
Quoted 9 lines
Quoted 6 lines
drivers/tee/tee_core.c +0 -1
                        }
                }
        } else if (ctx->cap_memref_null) {
--

2.50.1.windows.1

Looks good. Sumit, do you agree?

Quoted 3 lines

Sounds reasonable to me, FWIW:

Good. I'm picking this up.

Cheers,
Jens