Skip to contentSkip to conversation

tee: shm: fix use-after-free via temporarily dropped reference

PATCH In review Jens Wiklander 2 participants 2 messages May 7, 2018 TEE

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tee: shm: fix use-after-free via temporarily dropped reference

From: Jann Horn <[email protected]>

Bump the file's refcount before moving the reference into the fd table, not afterwards. The old code could drop the file's refcount to zero for a short moment before calling get_file() via get_dma_buf().

This code can only be triggered on ARM systems that use Linaro's OP-TEE.

Fixes 967c9cca2cc5 ("tee: generic TEE subsystem")
---
 drivers/tee/tee_shm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tee/tee_shm.c +3 -2
index 556960a1bab3..07d3be6f0780 100644
@@ -360,9 +360,10 @@ int tee_shm_get_fd(struct tee_shm *shm)
 	if (!(shm->flags & TEE_SHM_DMA_BUF))
 		return -EINVAL;
 
+	get_dma_buf(shm->dmabuf);
 	fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
-	if (fd >= 0)
-		get_dma_buf(shm->dmabuf);
+	if (fd < 0)
+		dma_buf_put(shm->dmabuf);
 	return fd;
 }
 
Signature · 1 line
2.17.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Jens,

Quoted 11 lines · Jens Wiklander

From: Jann Horn <[email protected]>

Bump the file's refcount before moving the reference into the fd table, not afterwards. The old code could drop the file's refcount to zero for a short moment before calling get_file() via get_dma_buf().

This code can only be triggered on ARM systems that use Linaro's OP-TEE.

Fixes 967c9cca2cc5 ("tee: generic TEE subsystem")
Signature · 2 lines
WBR Volodymyr Babchuk aka lorc [+380976646013]
mailto: vlad.babchuk at gmail.com