Skip to content

Commit 4ab59c3

Browse files
committed
dma-buf: Move dma_buf_release() from fops to dentry_ops
Charan Teja reported a 'use-after-free' in dmabuffs_dname [1], which happens if the dma_buf_release() is called while the userspace is accessing the dma_buf pseudo fs's dmabuffs_dname() in another process, and dma_buf_release() releases the dmabuf object when the last reference to the struct file goes away. I discussed with Arnd Bergmann, and he suggested that rather than tying the dma_buf_release() to the file_operations' release(), we can tie it to the dentry_operations' d_release(), which will be called when the last ref to the dentry is removed. The path exercised by __fput() calls f_op->release() first, and then calls dput, which eventually calls d_op->d_release(). In the 'normal' case, when no userspace access is happening via dma_buf pseudo fs, there should be exactly one fd, file, dentry and inode, so closing the fd will kill of everything right away. In the presented case, the dentry's d_release() will be called only when the dentry's last ref is released. Therefore, lets move dma_buf_release() from fops->release() to d_ops->d_release() Many thanks to Arnd for his FS insights :) [1]: https://lore.kernel.org/patchwork/patch/1238278/ Fixes: bb2bb90 ("dma-buf: add DMA_BUF_SET_NAME ioctls") Reported-by: [email protected] Cc: <[email protected]> [5.3+] Cc: Arnd Bergmann <[email protected]> Reported-by: Charan Teja Reddy <[email protected]> Reviewed-by: Arnd Bergmann <[email protected]> Signed-off-by: Sumit Semwal <[email protected]> Tested-by: Charan Teja Reddy <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent dc5bdb6 commit 4ab59c3

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

drivers/dma-buf/dma-buf.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,11 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
5454
dentry->d_name.name, ret > 0 ? name : "");
5555
}
5656

57-
static const struct dentry_operations dma_buf_dentry_ops = {
58-
.d_dname = dmabuffs_dname,
59-
};
60-
61-
static struct vfsmount *dma_buf_mnt;
62-
63-
static int dma_buf_fs_init_context(struct fs_context *fc)
64-
{
65-
struct pseudo_fs_context *ctx;
66-
67-
ctx = init_pseudo(fc, DMA_BUF_MAGIC);
68-
if (!ctx)
69-
return -ENOMEM;
70-
ctx->dops = &dma_buf_dentry_ops;
71-
return 0;
72-
}
73-
74-
static struct file_system_type dma_buf_fs_type = {
75-
.name = "dmabuf",
76-
.init_fs_context = dma_buf_fs_init_context,
77-
.kill_sb = kill_anon_super,
78-
};
79-
80-
static int dma_buf_release(struct inode *inode, struct file *file)
57+
static void dma_buf_release(struct dentry *dentry)
8158
{
8259
struct dma_buf *dmabuf;
8360

84-
if (!is_dma_buf_file(file))
85-
return -EINVAL;
86-
87-
dmabuf = file->private_data;
61+
dmabuf = dentry->d_fsdata;
8862

8963
BUG_ON(dmabuf->vmapping_counter);
9064

@@ -110,9 +84,32 @@ static int dma_buf_release(struct inode *inode, struct file *file)
11084
module_put(dmabuf->owner);
11185
kfree(dmabuf->name);
11286
kfree(dmabuf);
87+
}
88+
89+
static const struct dentry_operations dma_buf_dentry_ops = {
90+
.d_dname = dmabuffs_dname,
91+
.d_release = dma_buf_release,
92+
};
93+
94+
static struct vfsmount *dma_buf_mnt;
95+
96+
static int dma_buf_fs_init_context(struct fs_context *fc)
97+
{
98+
struct pseudo_fs_context *ctx;
99+
100+
ctx = init_pseudo(fc, DMA_BUF_MAGIC);
101+
if (!ctx)
102+
return -ENOMEM;
103+
ctx->dops = &dma_buf_dentry_ops;
113104
return 0;
114105
}
115106

107+
static struct file_system_type dma_buf_fs_type = {
108+
.name = "dmabuf",
109+
.init_fs_context = dma_buf_fs_init_context,
110+
.kill_sb = kill_anon_super,
111+
};
112+
116113
static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
117114
{
118115
struct dma_buf *dmabuf;
@@ -412,7 +409,6 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
412409
}
413410

414411
static const struct file_operations dma_buf_fops = {
415-
.release = dma_buf_release,
416412
.mmap = dma_buf_mmap_internal,
417413
.llseek = dma_buf_llseek,
418414
.poll = dma_buf_poll,

0 commit comments

Comments
 (0)