Skip to contentSkip to conversation

fuse: expose backing files in the daemon's fd table

PATCH In review Matan Cohen 3 participants 3 messages Apr 24 – Apr 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse: expose backing files in the daemon's fd table

FUSE passthrough backing files are currently not installed in the daemon's fd table, making them invisible to lsof(8) and /proc/<pid>/fd. This complicates debugging and is the reason passthrough currently requires CAP_SYS_ADMIN.

Install each backing file into the daemon's fd table on BACKING_OPEN and close it on BACKING_CLOSE. Add an fd field to struct fuse_backing to carry the installed fd number.

BACKING_CLOSE always runs in the daemon's ioctl context, so close_fd() targets the correct fd table directly.

To handle fds that remain open at connection teardown (e.g. on umount or connection abort without an explicit BACKING_CLOSE), store a reference to the daemon's task_struct in fc->daemon_task on the first BACKING_OPEN. During teardown, fuse_backing_files_free() schedules a task_work callback on the daemon task that calls close_fd() from the daemon's own context. If the daemon has already exited (task_work_add returns -ESRCH), its fd table was already cleaned up on exit.

This is a prerequisite for relaxing the CAP_SYS_ADMIN requirement on FUSE passthrough; a follow-up patch will drop the capability check now that backing fds are accountable via /proc/<pid>/fd.

---
 fs/fuse/backing.c                             |  97 +++++-
 fs/fuse/fuse_i.h                              |  15 +
 .../selftests/filesystems/fuse/.gitignore     |   1 +
 .../selftests/filesystems/fuse/Makefile       |   2 +-
 .../filesystems/fuse/fuse_passthrough_test.c  | 297 ++++++++++++++++++
 5 files changed, 405 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_passthrough_test.c
fs/fuse/backing.c +91 -6
index d95dfa48483f0..9c4ac0388491c 100644
@@ -8,6 +8,8 @@
 #include "fuse_i.h"
 
 #include <linux/file.h>
+#include <linux/fdtable.h>
+#include <linux/task_work.h>
 
 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb)
 {
@@ -20,6 +22,7 @@ static void fuse_backing_free(struct fuse_backing *fb)
 {
 	pr_debug("%s: fb=0x%p\n", __func__, fb);
 
+	WARN_ON_ONCE(fb->fd >= 0);
 	if (fb->file)
 		fput(fb->file);
 	put_cred(fb->cred);
@@ -64,19 +67,63 @@ static struct fuse_backing *fuse_backing_id_remove(struct fuse_conn *fc,
 	return fb;
 }
 
+struct fuse_backing_close_work {
+	struct callback_head cb;
+	int fd;
+};
+
+static void fuse_backing_close_fd(struct callback_head *cb)
+{
+	struct fuse_backing_close_work *w =
+		container_of(cb, struct fuse_backing_close_work, cb);
+	close_fd(w->fd);
+	kfree(w);
+}
+
 static int fuse_backing_id_free(int id, void *p, void *data)
 {
+	struct fuse_conn *fc = data;
 	struct fuse_backing *fb = p;
 
 	WARN_ON_ONCE(refcount_read(&fb->count) != 1);
+
+	if (fb->fd >= 0 && fc->daemon_task) {
+		struct fuse_backing_close_work *w;
+
+		w = kmalloc_obj(*w, GFP_ATOMIC);
+		if (w) {
+			init_task_work(&w->cb, fuse_backing_close_fd);
+			w->fd = fb->fd;
+			/*
+			 * Schedule close_fd() to run in the daemon's context.
+			 * TWA_RESUME fires on the daemon's next return to
+			 * userspace -- in practice immediately after its
+			 * blocked read() unblocks with an error at teardown.
+			 * -ESRCH means the daemon already exited and closed
+			 * all its fds; nothing to do.
+			 */
+			if (task_work_add(fc->daemon_task, &w->cb, TWA_RESUME))
+				kfree(w);
+		} else {
+			pr_warn_ratelimited("fuse: failed to close backing fd %d on teardown\n",
+					    fb->fd);
+		}
+		fb->fd = -1;
+	}
+
 	fuse_backing_free(fb);
 	return 0;
 }
 
 void fuse_backing_files_free(struct fuse_conn *fc)
 {
-	idr_for_each(&fc->backing_files_map, fuse_backing_id_free, NULL);
+	idr_for_each(&fc->backing_files_map, fuse_backing_id_free, fc);
 	idr_destroy(&fc->backing_files_map);
+
+	if (fc->daemon_task) {
+		put_task_struct(fc->daemon_task);
+		fc->daemon_task = NULL;
+	}
 }
 
 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
@@ -117,21 +164,50 @@ int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map)
 	if (!fb)
 		goto out_fput;
 
-	fb->file = file;
+	/*
+	 * Capture the daemon's task on the first BACKING_OPEN so that
+	 * fuse_backing_files_free() can schedule fd closes via task_work
+	 * during teardown, even when teardown runs outside daemon context.
+	 */
+	if (!fc->daemon_task) {
+		spin_lock(&fc->lock);
+		if (!fc->daemon_task)
+			fc->daemon_task = get_task_struct(current);
+		spin_unlock(&fc->lock);
+	}
+
+	fb->file = file;	/* fget_raw ref transferred */
+	fb->fd = -1;
 	fb->cred = prepare_creds();
+	if (!fb->cred) {
+		res = -ENOMEM;
+		goto out_free;
+	}
 	refcount_set(&fb->count, 1);
+	fb->fd = get_unused_fd_flags(O_CLOEXEC);
+	if (fb->fd < 0) {
+		res = fb->fd;
+		goto out_free;
+	}
+	get_file(file);
+	fd_install(fb->fd, file);
 
 	res = fuse_backing_id_alloc(fc, fb);
-	if (res < 0) {
-		fuse_backing_free(fb);
-		fb = NULL;
-	}
+	if (res < 0)
+		goto out_close_fd;
 
 out:
 	pr_debug("%s: fb=0x%p, ret=%i\n", __func__, fb, res);
 
 	return res;
 
+out_close_fd:
+	close_fd(fb->fd);
+	fb->fd = -1;
+out_free:
+	fuse_backing_free(fb);
+	fb = NULL;
+	goto out;
 out_fput:
 	fput(file);
 	goto out;
@@ -158,6 +234,15 @@ int fuse_backing_close(struct fuse_conn *fc, int backing_id)
 	if (!fb)
 		goto out;
 
+	/*
+	 * Close the fd installed in the daemon's fd table on BACKING_OPEN.
+	 * BACKING_CLOSE always runs in the daemon's ioctl context, so
+	 * close_fd() targets the correct fd table.
+	 */
+	if (fb->fd >= 0) {
+		close_fd(fb->fd);
+		fb->fd = -1;
+	}
 	fuse_backing_put(fb);
 	err = 0;
 out:
fs/fuse/fuse_i.h +15 -0
index 17423d4e3cfa6..0edda751747a2 100644
@@ -107,6 +107,14 @@ struct fuse_submount_lookup {
 struct fuse_backing {
 	struct file *file;
 	struct cred *cred;
+	/**
+	 * fd: fd installed in the daemon's fd table on BACKING_OPEN, or -1.
+	 * Valid only while this entry is in fc->backing_files_map.
+	 * Closed by BACKING_CLOSE (daemon ioctl context) or by a task_work
+	 * callback scheduled on fc->daemon_task during connection teardown.
+	 * Must be -1 before fuse_backing_free() is called.
+	 */
+	int fd;
 
 	/** refcount */
 	refcount_t count;
@@ -979,6 +987,13 @@ struct fuse_conn {
 #ifdef CONFIG_FUSE_PASSTHROUGH
 	/** IDR for backing files ids */
 	struct idr backing_files_map;
+	/**
+	 * daemon_task: task_struct of the daemon, held with a reference.
+	 * Used during connection teardown to schedule task_work that closes
+	 * any remaining backing fds in the daemon's fd table.  Set on the
+	 * first BACKING_OPEN ioctl; NULL if no backing files were registered.
+	 */
+	struct task_struct *daemon_task;
 #endif
 
 #ifdef CONFIG_FUSE_IO_URING
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
index 3e72e742d08e8..ef1ff428aa7a6 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 fuse_mnt
 fusectl_test
+fuse_passthrough_test
tools/testing/selftests/filesystems/fuse/Makefile +1 -1
index 612aad69a93aa..494042ebc4f2c 100644
@@ -2,7 +2,7 @@
 
 CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
 
-TEST_GEN_PROGS := fusectl_test
+TEST_GEN_PROGS := fusectl_test fuse_passthrough_test
 TEST_GEN_FILES := fuse_mnt
 
 include ../../lib.mk
tools/testing/selftests/filesystems/fuse/fuse_passthrough_test.c new file +297 -0

297 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw

[removing irrelevant lists]

Hi Matan!

Quoted 12 lines · Matan Cohen

FUSE passthrough backing files are currently not installed in the daemon's fd table, making them invisible to lsof(8) and /proc/<pid>/fd. This complicates debugging and is the reason passthrough currently requires CAP_SYS_ADMIN.

Install each backing file into the daemon's fd table on BACKING_OPEN and close it on BACKING_CLOSE. Add an fd field to struct fuse_backing to carry the installed fd number.

BACKING_CLOSE always runs in the daemon's ioctl context, so close_fd() targets the correct fd table directly.

Quoted 8 lines

To handle fds that remain open at connection teardown (e.g. on umount or connection abort without an explicit BACKING_CLOSE), store a reference to the daemon's task_struct in fc->daemon_task on the first BACKING_OPEN. During teardown, fuse_backing_files_free() schedules a task_work callback on the daemon task that calls close_fd() from the daemon's own context. If the daemon has already exited (task_work_add returns -ESRCH), its fd table was already cleaned up on exit.

I am sorry but both the approach and the implementation are wrong for several different reasons, so I will not start to list them.

Please follow this discussion:
https://lore.kernel.org/linux-fsdevel/CAJfpegvaCTxS_wC6EGFfh3Gim5DEgOtuju=_=qCsouzkCRvJog@mail.gmail.com/

It does not end with a definite decision on the best way to export backing files to lsof, but it does pave the general direction the the solution should take.

Quoted 3 lines

This is a prerequisite for relaxing the CAP_SYS_ADMIN requirement on FUSE passthrough; a follow-up patch will drop the capability check now that backing fds are accountable via /proc/<pid>/fd.

This is one prerequisite, not all of them. See "Resource Accounting and Visibility" section in Documentation/filesystems/fuse/fuse-passthrough.rst

Please CC me and the new fuse-devel list on future attempts to address this issue. There is no need to CC LKML and selftest maintainers.

Thanks,
Amir.

#2 lore raw
In reply to Matan Cohen
kernel test robot noticed the following build errors:

Hi Matan,

kernel test robot noticed the following build errors:

[auto build test ERROR on v7.0]
[also build test ERROR on linus/master next-20260427] [cannot apply to mszeredi-fuse/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Matan-Cohen/fuse-expose-backing-files-in-the-daemon-s-fd-table/20260426-193147
base:   v7.0
patch link:    https://lore.kernel.org/r/20260424144553.6241-1-matan%40matanco.space
patch subject: [PATCH] fuse: expose backing files in the daemon's fd table
config: nios2-allmodconfig (https://download.01.org/0day-ci/archive/20260428/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260428/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "task_work_add" [fs/fuse/fuse.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki