Skip to contentSkip to conversation

tee: handle lookup of shm with reference count 0

PATCH New Jens Wiklander 1 participant 1 message Dec 28, 2021 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tee: handle lookup of shm with reference count 0

commit dfd0743f1d9ea76931510ed150334d571fbab49d upstream.

Since the tee subsystem does not keep a strong reference to its idle shared memory buffers, it races with other threads that try to destroy a shared memory through a close of its dma-buf fd or by unmapping the memory.

In tee_shm_get_from_id() when a lookup in teedev->idr has been successful, it is possible that the tee_shm is in the dma-buf teardown path, but that path is blocked by the teedev mutex. Since we don't have an API to tell if the tee_shm is in the dma-buf teardown path or not we must find another way of detecting this condition.

Fix this by doing the reference counting directly on the tee_shm using a new refcount_t refcount field. dma-buf is replaced by using anon_inode_getfd() instead, this separates the life-cycle of the underlying file from the tee_shm. tee_shm_put() is updated to hold the mutex when decreasing the refcount to 0 and then remove the tee_shm from teedev->idr before releasing the mutex. This means that the tee_shm can never be found unless it has a refcount larger than 0.

Fixes 967c9cca2cc5 ("tee: generic TEE subsystem")
[JW: backported to 4.14-stable]
---
 drivers/tee/tee_private.h |   4 +-
 drivers/tee/tee_shm.c     | 155 +++++++++++++++-----------------------
 2 files changed, 63 insertions(+), 96 deletions(-)
drivers/tee/tee_private.h +2 -2
index 21cb6be8bce9..4ef325f782d1 100644
@@ -31,7 +31,7 @@ struct tee_device;
  * @paddr:	physical address of the shared memory
  * @kaddr:	virtual address of the shared memory
  * @size:	size of shared memory
- * @dmabuf:	dmabuf used to for exporting to user space
+ * @refcount:	reference counter
  * @flags:	defined by TEE_SHM_* in tee_drv.h
  * @id:		unique id of a shared memory object on this device
  */
@@ -42,7 +42,7 @@ struct tee_shm {
 	phys_addr_t paddr;
 	void *kaddr;
 	size_t size;
-	struct dma_buf *dmabuf;
+	refcount_t refcount;
 	u32 flags;
 	int id;
 };
drivers/tee/tee_shm.c +61 -94

230 lines not on this page · raw message

Signature · 1 line
2.31.1