From: Jann Horn Date: Wed, 4 Apr 2018 19:03:21 +0000 (+0200) Subject: tee: shm: fix use-after-free via temporarily dropped reference X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=bca7faea5410d50d2a3be4b11688f02a2fb2ee95;p=GitHub%2Fmoto-9609%2Fandroid_kernel_motorola_exynos9610.git tee: shm: fix use-after-free via temporarily dropped reference commit bb765d1c331f62b59049d35607ed2e365802bef9 upstream. 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") Signed-off-by: Jann Horn Signed-off-by: Jens Wiklander Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/tee/tee_shm.c b/drivers/tee/tee_shm.c index 4bc7956cefc4..ea3ce4e17b85 100644 --- a/drivers/tee/tee_shm.c +++ b/drivers/tee/tee_shm.c @@ -203,9 +203,10 @@ int tee_shm_get_fd(struct tee_shm *shm) if ((shm->flags & req_flags) != req_flags) 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; }