Skip to content

Commit a19dae4

Browse files
Charan Teja Reddygregkh
authored andcommitted
dmabuf: fix use-after-free of dmabuf's file->f_inode
commit 05cd846 upstream. It is observed 'use-after-free' on the dmabuf's file->f_inode with the race between closing the dmabuf file and reading the dmabuf's debug info. Consider the below scenario where P1 is closing the dma_buf file and P2 is reading the dma_buf's debug info in the system: P1 P2 dma_buf_debug_show() dma_buf_put() __fput() file->f_op->release() dput() .... dentry_unlink_inode() iput(dentry->d_inode) (where the inode is freed) mutex_lock(&db_list.lock) read 'dma_buf->file->f_inode' (the same inode is freed by P1) mutex_unlock(&db_list.lock) dentry->d_op->d_release()--> dma_buf_release() ..... mutex_lock(&db_list.lock) removes the dmabuf from the list mutex_unlock(&db_list.lock) In the above scenario, when dma_buf_put() is called on a dma_buf, it first frees the dma_buf's file->f_inode(=dentry->d_inode) and then removes this dma_buf from the system db_list. In between P2 traversing the db_list tries to access this dma_buf's file->f_inode that was freed by P1 which is a use-after-free case. Since, __fput() calls f_op->release first and then later calls the d_op->d_release, move the dma_buf's db_list removal from d_release() to f_op->release(). This ensures that dma_buf's file->f_inode is not accessed after it is released. Cc: <stable@vger.kernel.org> # 5.4.x- Fixes: 4ab59c3 ("dma-buf: Move dma_buf_release() from fops to dentry_ops") Acked-by: Christian König <christian.koenig@amd.com> Signed-off-by: Charan Teja Reddy <charante@codeaurora.org> Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org> Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Link: https://patchwork.freedesktop.org/patch/msgid/1609857399-31549-1-git-send-email-charante@codeaurora.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6844bc3 commit a19dae4

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

‎drivers/dma-buf/dma-buf.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ static void dma_buf_release(struct dentry *dentry)
7676

7777
dmabuf->ops->release(dmabuf);
7878

79-
mutex_lock(&db_list.lock);
80-
list_del(&dmabuf->list_node);
81-
mutex_unlock(&db_list.lock);
82-
8379
if (dmabuf->resv == (struct dma_resv *)&dmabuf[1])
8480
dma_resv_fini(dmabuf->resv);
8581

@@ -88,6 +84,22 @@ static void dma_buf_release(struct dentry *dentry)
8884
kfree(dmabuf);
8985
}
9086

87+
static int dma_buf_file_release(struct inode *inode, struct file *file)
88+
{
89+
struct dma_buf *dmabuf;
90+
91+
if (!is_dma_buf_file(file))
92+
return -EINVAL;
93+
94+
dmabuf = file->private_data;
95+
96+
mutex_lock(&db_list.lock);
97+
list_del(&dmabuf->list_node);
98+
mutex_unlock(&db_list.lock);
99+
100+
return 0;
101+
}
102+
91103
static const struct dentry_operations dma_buf_dentry_ops = {
92104
.d_dname = dmabuffs_dname,
93105
.d_release = dma_buf_release,
@@ -413,6 +425,7 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
413425
}
414426

415427
static const struct file_operations dma_buf_fops = {
428+
.release = dma_buf_file_release,
416429
.mmap = dma_buf_mmap_internal,
417430
.llseek = dma_buf_llseek,
418431
.poll = dma_buf_poll,

0 commit comments

Comments
 (0)