Skip to contentSkip to conversation

fuse: fix use-after-free in fuse_create_open() error path

PATCH In review Jimmy Zuber 2 participants 3 messages Oct 26 – Nov 18 FUSE filesystem core

lore
Share & tools
lore
#0 lore raw

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.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
Assisted-by Claude:claude-opus-5-5 [Claude-Code]

---
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
index 28516a1760e24..4c64a4f812395 100644
@@ -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
#1 lore raw

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jimmy Zuber
Quoted 53 lines · Jimmy Zuber

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.

Fixes 2fbe4586cff3 ("fuse: add support for opening dax device as backing")
Assisted-by Claude:claude-opus-5-5 [Claude-Code]

---
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
index 28516a1760e24..4c64a4f812395 100644
@@ -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!