dma-buf: Move dma_buf_release() from fops to dentry_ops
Subsystems (3)
Sumit Semwal
maintainerPATCH 1/1 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/
--- drivers/dma-buf/dma-buf.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/dma-buf/dma-buf.c +7 -6
@@ -54,8 +54,11 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) dentry->d_name.name, ret > 0 ? name : ""); } +static void dma_buf_release (struct dentry *dentry); +static const struct dentry_operations dma_buf_dentry_ops = { .d_dname = dmabuffs_dname, + .d_release = dma_buf_release, };static struct vfsmount *dma_buf_mnt;
@@ -77,14 +80,14 @@ static struct file_system_type dma_buf_fs_type = { .kill_sb = kill_anon_super, }; -static int dma_buf_release (struct inode *inode,struct file *file) +static void dma_buf_release (struct dentry *dentry) {struct dma_buf *dmabuf; -if (!is_dma_buf_file (file)) -return -EINVAL ; +if (dentry->d_op != &dma_buf_dentry_ops) +return ; - dmabuf = file->private_data; + dmabuf = dentry->d_fsdata;BUG_ON (dmabuf->vmapping_counter);
@@ -110,7 +113,6 @@ static int dma_buf_release(struct inode *inode, struct file *file)module_put (dmabuf->owner);kfree (dmabuf->name);kfree (dmabuf); -return 0 ; }static int dma_buf_mmap_internal (struct file *file,struct vm_area_struct *vma)
@@ -412,7 +414,6 @@ static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) }static const struct file_operations dma_buf_fops = { - .release = dma_buf_release, .mmap = dma_buf_mmap_internal, .llseek = dma_buf_llseek, .poll = dma_buf_poll,
Signature · 1 line
2.27.0
Quoted 27 lines · Sumit Semwal
Charan Teja reported a 'use-after-free' in
dmabuffs_dname[1], which happens if thedma_buf_release()is called while the userspace is accessing thedma_bufpseudo fs'sdmabuffs_dname()in another process, anddma_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 thefile_operations'release(), we can tie it to thedentry_operations'd_release(), which will be called when the last ref to the dentry is removed.The path exercised by
__fput()callsf_op->release()first, and then calls dput, which eventually callsd_op->d_release().In the 'normal' case, when no userspace access is happening via
dma_bufpseudo 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()fromfops->release()tod_ops->d_release().Many thanks to Arnd for his FS insights :)
Quoted 8 lines
[1]: https://lore.kernel.org/patchwork/patch/1238278/
Fixes bb2bb9030425 ("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]>Signed-off-by Sumit Semwal <[email protected]>
The patch looks correct to me.
Obviously this should still be verified against the original report if possible.
Quoted 8 lines
drivers/dma-buf/dma-buf.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)drivers/dma-buf/dma-buf.c +0
-0
drivers/dma-buf/dma-buf.c static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) · line 62
Quoted 9 lines
drivers/dma-buf/dma-buf.c +3
-0@@ -54,8 +54,11 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) dentry->d_name.name, ret > 0 ? name : ""); } +static void dma_buf_release (struct dentry *dentry); +static const struct dentry_operations dma_buf_dentry_ops = { .d_dname = dmabuffs_dname, + .d_release = dma_buf_release, };
I'd suggest rearranging the file to avoid the forward declaration, even if it makes it a little harder to review the change, the resulting code will remain organized more logically.
Quoted 4 lines
drivers/dma-buf/dma-buf.c +0
-0static struct vfsmount *dma_buf_mnt;@@ -77,14 +80,14 @@ static struct file_system_type dma_buf_fs_type = { .kill_sb = kill_anon_super, };
drivers/dma-buf/dma-buf.c static struct file_system_type dma_buf_fs_type = { · line 88
Quoted 10 lines
drivers/dma-buf/dma-buf.c +3
-3-static int dma_buf_release (struct inode *inode,struct file *file) +static void dma_buf_release (struct dentry *dentry) {struct dma_buf *dmabuf; -if (!is_dma_buf_file (file)) -return -EINVAL ; +if (dentry->d_op != &dma_buf_dentry_ops) +return ;
I think the check here is redundant and it's clearer without it.
Arnd
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Sumit Semwal
maintainerQuoted 38 lines · Arnd Bergmann
Quoted 27 lines · Sumit Semwal
Charan Teja reported a 'use-after-free' in
dmabuffs_dname[1], which happens if thedma_buf_release()is called while the userspace is accessing thedma_bufpseudo fs'sdmabuffs_dname()in another process, anddma_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 thefile_operations'release(), we can tie it to thedentry_operations'd_release(), which will be called when the last ref to the dentry is removed.The path exercised by
__fput()callsf_op->release()first, and then calls dput, which eventually callsd_op->d_release().In the 'normal' case, when no userspace access is happening via
dma_bufpseudo 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()fromfops->release()tod_ops->d_release().Many thanks to Arnd for his FS insights :)
Quoted 8 lines
[1]: https://lore.kernel.org/patchwork/patch/1238278/
Fixes bb2bb9030425 ("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]>Signed-off-by Sumit Semwal <[email protected]>
Quoted 5 lines
The patch looks correct to me.
Reviewed-by Arnd Bergmann <[email protected]>Obviously this should still be verified against the original report if possible.
Thanks, Arnd!
Quoted 12 lines
Quoted 12 lines
drivers/dma-buf/dma-buf.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)drivers/dma-buf/dma-buf.c +0
-0@@ -54,8 +54,11 @@ static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) dentry->d_name.name, ret > 0 ? name : ""); }
Quoted 10 lines
drivers/dma-buf/dma-buf.c static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) · line 62
Quoted 6 lines
drivers/dma-buf/dma-buf.c +3
-0+static void dma_buf_release (struct dentry *dentry); +static const struct dentry_operations dma_buf_dentry_ops = { .d_dname = dmabuffs_dname, + .d_release = dma_buf_release, };I'd suggest rearranging the file to avoid the forward declaration, even if it makes it a little harder to review the change, the resulting code will remain organized more logically.
Got it, will update it in v2.
Quoted 11 lines
Quoted 11 lines
drivers/dma-buf/dma-buf.c +1
-1static struct vfsmount *dma_buf_mnt;@@ -77,14 +80,14 @@ static struct file_system_type dma_buf_fs_type = { .kill_sb = kill_anon_super, }; -static int dma_buf_release (struct inode *inode,struct file *file) +static void dma_buf_release (struct dentry *dentry) {struct dma_buf *dmabuf;
Quoted 6 lines
drivers/dma-buf/dma-buf.c static struct file_system_type dma_buf_fs_type = { · line 88
Quoted 4 lines
drivers/dma-buf/dma-buf.c +2
-2-if (!is_dma_buf_file (file)) -return -EINVAL ; +if (dentry->d_op != &dma_buf_dentry_ops) +return ;I think the check here is redundant and it's clearer without it.
Ok, will remove.
Quoted 2 lines
Arnd
Best,
Sumit.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel