fuse: fix use-after-free in fuse_create_open() error path
PATCH 1/1 fuse: fix use-after-free in fuse_create_open() error path
An open can still fail after fuse_finish_open() has succeeded. For example, an O_DIRECT open fails with -EINVAL if the file has no ->direct_IO method. By then the VFS has marked the file as open, so it releases the file itself, which calls fuse_release().
fuse_create_open() also frees the fuse_file whenever finish_open() fails, so fuse_release() then uses the freed fuse_file.
This happens when a file is created with O_DIRECT on a DAX inode that is opened for passthrough, because fuse_dax_aops has no ->direct_IO.
Only free the fuse_file in fuse_create_open() if the file was not opened.
---
Found by Sashiko while reviewing the FUSE_OPEN_ATOMIC series (see Link). To reproduce it without a DAX device, I removed ->direct_IO from fuse_file_aops and ran the open under UML with KASAN:
BUG: KASAN: slab-use-after-free in fuse_file_release+0x31/0x123 ... fuse_file_release+0x31/0x123 fuse_release_common+0x65/0x71 fuse_release+0x7f/0x8a
With this patch the open fails with -EINVAL and KASAN reports nothing.
fs/fuse/dir.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
fs/fuse/dir.c +5 -2
@@ -917,8 +917,11 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, err =finish_open (file, entry, fuse_finish_open); }if (err) { - fi =get_fuse_inode (inode); -fuse_sync_release (fi, ff, flags); +/* fput() releases a file that finish_open() opened */ +if (!(file->f_mode &FMODE_OPENED )) { + fi =get_fuse_inode (inode); +fuse_sync_release (fi, ff, flags); + } }else {if (fm->fc->atomic_o_trunc && trunc)truncate_pagecache (inode,0 );
base-commit: 9b1da7b4a4e197072e630c3b85d4100a399752ff
Signature · 1 line
2.50.1
Hi Miklos,
Gentle ping on this fix. The bug comes from commit 2fbe4586cff3 ("fuse:
add support for opening dax device as backing"), which is in 7.4-rc1. The patch applies to fuse for-next at 7.4-rc1.
The FUSE_OPEN_ATOMIC series depends on this patch.
Thanks,
Jimmy
Quoted 53 lines · Jimmy Zuber
An open can still fail after
fuse_finish_open()has succeeded. For example, anO_DIRECTopen fails with-EINVALif the file has no ->direct_IO method. By then the VFS has marked the file as open, so it releases the file itself, which callsfuse_release().
fuse_create_open()also frees thefuse_filewheneverfinish_open()fails, sofuse_release()then uses the freedfuse_file.This happens when a file is created with
O_DIRECTon a DAX inode that is opened for passthrough, becausefuse_dax_aopshas no ->direct_IO.Only free the
fuse_fileinfuse_create_open()if the file was not opened.Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")Assisted-by Claude:claude-opus-5-5 [Claude-Code]Signed-off-by Jimmy Zuber <[email protected]>---
Found by Sashiko while reviewing theFUSE_OPEN_ATOMICseries (see Link). To reproduce it without a DAX device, I removed ->direct_IO fromfuse_file_aopsand ran the open under UML with KASAN:BUG: KASAN: slab-use-after-free in fuse_file_release+0x31/0x123 ... fuse_file_release+0x31/0x123 fuse_release_common+0x65/0x71 fuse_release+0x7f/0x8aWith this patch the open fails with
-EINVALand KASAN reports nothing.fs/fuse/dir.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)fs/fuse/dir.c +5
-2@@ -917,8 +917,11 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir, err =finish_open (file, entry, fuse_finish_open); }if (err) { - fi =get_fuse_inode (inode); -fuse_sync_release (fi, ff, flags); +/* fput() releases a file that finish_open() opened */ +if (!(file->f_mode &FMODE_OPENED )) { + fi =get_fuse_inode (inode); +fuse_sync_release (fi, ff, flags); + } }else {if (fm->fc->atomic_o_trunc && trunc)truncate_pagecache (inode,0 );
Quoted 1 line
base-commit:
9b1da7b4a4e197072e630c3b85d4100a399752ff
This is super hard to see, I wonder if we should add a check in fuse_finish_open()
+ if ((file->f_flags & O_DIRECT) && !file->f_mapping->a_ops->direct_IO)
+ return -EINVAL;
+
and avoid the condition in fuse_create_open(). Or have both for safety.
Anyway, thanks for fixing it!