fuse: allow FUSE_SYNCFS for privileged userspace servers
Subsystems (4)
cover fuse: allow FUSE_SYNCFS for privileged userspace servers
FUSE_SYNCFS (propagating syncfs()/sync() to the server) is currently enabled only for virtiofs and fuseblk, since an untrusted server can stall sync(). But any FUSE filesystem may buffer data in the server that should reach storage on sync(); the only thing that should gate it is whether the mount was set up with host privilege. This series lets a plain /dev/fuse server opt in via a new FUSE_HAS_SYNCFS INIT flag, honored only for mounts owned by the initial user namespace. Patch 1 has the full rationale and the security argument.
Patch 1: the kernel change (UAPI flag + gating in process_init_reply()). Patch 2: a selftest that speaks the raw FUSE protocol over /dev/fuse, so
it can withhold the flag and directly observe whether the FUSE_SYNCFS opcode is forwarded -- covering the privileged, opt-out, and unprivileged-userns cases.
A matching libfuse change (FUSE_CAP_SYNCFS negotiation) will be sent to the libfuse project once the UAPI flag here is settled.
Testing: built and booted under QEMU (x86_64). The selftest passes all three cases. A separate end-to-end check on a FUSE_WRITEBACK_CACHE mount confirmed the point of the change: after write() the server had received 0 bytes (data dirty in the page cache), and after syncfs() it received the full buffered payload followed by FUSE_SYNCFS -- i.e. syncfs() flushes cached data to the server on a privileged mount.
Jimmy Zuber (2):
fuse: allow FUSE_SYNCFS for privileged userspace servers selftests/fuse: add test for FUSE_HAS_SYNCFS privilege gating
fs/fuse/inode.c | 16 + include/uapi/linux/fuse.h | 11 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 +- .../selftests/filesystems/fuse/test_syncfs.c | 318 ++++++++++++++++++ 5 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/test_syncfs.c
base-commit: 7d87a5a284bb34edb3f4e7e312ef403b3385a7b7
Signature · 1 line
2.50.1
PATCH 1/2 fuse: allow FUSE_SYNCFS for privileged userspace servers
Propagating syncfs()/sync() to a FUSE server via FUSE_SYNCFS lets the server flush its own cached or intermediate state when userspace asks the filesystem to sync. This is currently enabled only for virtiofs and fuseblk, because an untrusted server can use it to stall sync() indefinitely (see commit 2d82ab251ef0 ("virtiofs: propagate sync() to file server"), and commit d3906d8f3cee ("fuse: enable FUSE_SYNCFS for all fuseblk servers")). Both of those mount types require host privilege to set up, so the server is trusted not to abuse it.
There is nothing virtiofs- or block-specific about wanting to handle syncfs(), though. A plain /dev/fuse server is just as entitled to participate in the sync() path -- so that data it has buffered reaches stable storage when the user asks for it -- provided it is equally trusted. The relevant trust boundary is whether the mount was set up with host privilege.
Add an opt-in INIT flag, FUSE_HAS_SYNCFS, and enable propagation only when both:
- the server sets FUSE_HAS_SYNCFS in its INIT reply, and
- the mount is owned by the initial user namespace
(fc->user_ns == &init_user_ns).
The user namespace check is the key restriction. A regular fuse mount is mountable from a non-initial user namespace (FS_USERNS_MOUNT), where the server is untrusted; the VFS already marks such mounts with SB_I_UNTRUSTED_MOUNTER. Restricting FUSE_SYNCFS to init_user_ns mounts preserves the original DoS protection for unprivileged servers in full, while mirroring how virtiofs and fuseblk earn syncfs by construction (neither is mountable from a non-initial user namespace).
The flag is only advertised to servers whose mount is owned by the initial user namespace, so an unprivileged server is never invited to opt in (and is ignored by fuse_syncfs_enable() if it sets the flag anyway).
--- fs/fuse/inode.c | 16 ++++++++++++++++ include/uapi/linux/fuse.h | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-)
fs/fuse/inode.c +16 -0
@@ -1266,6 +1266,16 @@ struct fuse_init_args {struct fuse_mount *fm; }; +/* +* A server can stall syncfs()/sync(), so only honor FUSE_HAS_SYNCFS for +* mounts owned by the initial user namespace, i.e. set up with host +* privilege (like virtiofs and fuseblk). +*/ +static bool fuse_syncfs_enable (struct fuse_conn *fc,u64 flags) +{ +return (flags &FUSE_HAS_SYNCFS ) && fc->user_ns == &init_user_ns; +} +static void process_init_reply (struct fuse_args *args,int error) {struct fuse_init_args *ia =container_of (args,typeof (*ia), args);
@@ -1406,6 +1416,9 @@ static void process_init_reply(struct fuse_args *args, int error)if (flags &FUSE_REQUEST_TIMEOUT ) timeout = arg->request_timeout; + +if (fuse_syncfs_enable (fc, flags)) + fc->sync_fs =1 ; }else { ra_pages = fc->max_read /PAGE_SIZE ; fc->no_lock =1 ;
@@ -1473,6 +1486,9 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm) flags |=FUSE_SUBMOUNTS ;if (IS_ENABLED (CONFIG_FUSE_PASSTHROUGH )) flags |=FUSE_PASSTHROUGH ; +/* Only offered to host-privileged mounts; see fuse_syncfs_enable(). */ +if (fm->fc->user_ns == &init_user_ns) + flags |=FUSE_HAS_SYNCFS ;/* * This is just an information flag for fuse server. No need to check
include/uapi/linux/fuse.h +10 -1
@@ -240,6 +240,9 @@* - add FUSE_COPY_FILE_RANGE_64 * - add struct fuse_copy_file_range_out * - add FUSE_NOTIFY_PRUNE +* +* 7.46 +* - add FUSE_HAS_SYNCFS opt-in flag for privileged userspace servers */ #ifndef _LINUX_FUSE_H
@@ -275,7 +278,7 @@#define FUSE_KERNEL_VERSION 7/** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 45 +#define FUSE_KERNEL_MINOR_VERSION 46/** The node ID of the root inode */ #define FUSE_ROOT_ID 1
@@ -448,6 +451,11 @@ struct fuse_file_lock {* FUSE_OVER_IO_URING: Indicate that client supports io-uring * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests. * init_out.request_timeout contains the timeout (in secs) +* FUSE_HAS_SYNCFS: server requests that syncfs()/sync() be propagated as +* FUSE_SYNCFS requests. Only honored by the kernel for mounts +* owned by the initial user namespace (i.e. set up with real +* host privilege), since an untrusted server can use this to +* stall sync(). Unprivileged (user namespace) mounts ignore it. */ #define FUSE_ASYNC_READ (1 << 0)#define FUSE_POSIX_LOCKS (1 << 1)
@@ -495,6 +503,7 @@ struct fuse_file_lock {#define FUSE_ALLOW_IDMAP (1ULL << 40)#define FUSE_OVER_IO_URING (1ULL << 41)#define FUSE_REQUEST_TIMEOUT (1ULL << 42) +#define FUSE_HAS_SYNCFS (1ULL << 43)/** * CUSE INIT request/reply flags
Signature · 1 line
2.50.1
Miklos Szeredi
maintainerfs/fuse/inode.c struct fuse_init_args { · line 1277
Quoted 9 lines · Jimmy Zuber
fs/fuse/inode.c +9
-0+/* +* A server can stall syncfs()/sync(), so only honor FUSE_HAS_SYNCFS for +* mounts owned by the initial user namespace, i.e. set up with host +* privilege (like virtiofs and fuseblk). +*/ +static bool fuse_syncfs_enable (struct fuse_conn *fc,u64 flags) +{ +return (flags &FUSE_HAS_SYNCFS ) && fc->user_ns == &init_user_ns; +}
Sounds really easy to trick: start the server in the initial user ns,
then clone the mounter with a new user/mount namespace. Theinit_user_ns test will pass happily, since the server is running in the initial namespace.
Thanks,
Miklos
Quoted 4 lines · Miklos Szeredi
Sounds really easy to trick: start the server in the initial user ns,
then clone the mounter with a new user/mount namespace. Theinit_user_nstest will pass happily, since the server is running in the initial namespace.
Ah, the intention was to limit sync to sufficiently privileged FUSE setups. I missed that the initial user namespace is not equivalent to elevated permissions. I am thinking instead it would make sense to assert that the opener of /dev/fuse has CAP_SYS_ADMIN in the initial user namespace. They could then hand off the fd to a less privileged server, but that is the prerogative of that privileged user, so I think it satisfies the spirit of the DoS prevention requirement.
I will follow up shortly with a new version.
Thank you!
Jimmy
PATCH 2/2 selftests/fuse: add test for FUSE_HAS_SYNCFS privilege gating
In reply to Jimmy ZuberAdd a selftest that talks the raw FUSE protocol over /dev/fuse (rather than via libfuse, which negotiates INIT internally) so it can both choose whether to advertise FUSE_HAS_SYNCFS and directly observe whether a FUSE_SYNCFS opcode is forwarded by the kernel.
Three cases are covered:
T1: host-root mount, server sets FUSE_HAS_SYNCFS
-> FUSE_SYNCFS must reach the server.
T2: host-root mount, server does not opt in
-> FUSE_SYNCFS must not be sent (back-compat).
T3: unprivileged user-namespace mount, server sets FUSE_HAS_SYNCFS
-> kernel must still withhold FUSE_SYNCFS.T3 requires CONFIG_USER_NS and the ability to create an unprivileged user-namespace mount; it is skipped otherwise.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 +- .../selftests/filesystems/fuse/test_syncfs.c | 318 ++++++++++++++++++ 3 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/filesystems/fuse/test_syncfs.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only fuse_mnt fusectl_test +test_syncfs
tools/testing/selftests/filesystems/fuse/Makefile +1 -1
@@ -2,7 +2,7 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) -TEST_GEN_PROGS := fusectl_test +TEST_GEN_PROGS := fusectl_test test_syncfs TEST_GEN_FILES := fuse_mnt include ../../lib.mk
tools/testing/selftests/filesystems/fuse/test_syncfs.c new file +318 -0
318 lines not on this page · raw message
Signature · 1 line
2.50.1
v2 cover fuse: allow FUSE_SYNCFS for privileged userspace servers
In reply to Jimmy ZuberFUSE_SYNCFS (propagating syncfs()/sync() to the server) is currently enabled only for virtiofs and fuseblk, since an untrusted server can stall sync(). Any FUSE filesystem may buffer data in the server that ought to reach storage on sync(); the only thing that should gate it is whether the /dev/fuse opener is sufficiently privileged to be trusted to block sync.
This series lets a plain /dev/fuse server opt in via a new FUSE_HAS_SYNCFS INIT flag, honored only when the server opened /dev/fuse with CAP_SYS_ADMIN privilege in the initial user namespace -- checked with file_ns_capable() at mount time.
Patch 1: the kernel change (UAPI flag + privilege gating). Patch 2: a selftest that speaks the raw FUSE protocol over /dev/fuse, so
it can withhold the flag and directly observe whether the FUSE_SYNCFS opcode is forwarded.
A matching libfuse change (FUSE_CAP_SYNCFS negotiation) will be sent to the libfuse project once the UAPI flag here is settled.
Changes since v1 [1]:
- Gate on the privilege of the opener (CAP_SYS_ADMIN in init_user_ns at /dev/fuse open time, via file_ns_capable()) rather than on the mount's user namespace. Miklos pointed out that the v1 check (fc->user_ns == &init_user_ns) tested a property of the mount, not of the server that actually services -- and can stall -- the connection. Being in the initial user namespace is not itself a privilege (e.g. an ordinary sshfs mount qualifies). Checking the device opener's capability closes that gap.
- Selftest: add a case covering exactly that distinction -- a server in the initial user namespace that opened /dev/fuse without CAP_SYS_ADMIN -- which v1 would have wrongly allowed.
Testing: built and booted under QEMU. The selftest passes all four cases, including the new case above (verified that the kernel withholds FUSE_SYNCFS while it would have been granted under the v1 check).
[1] https://lore.kernel.org/[email protected]
Jimmy Zuber (2):
fuse: allow FUSE_SYNCFS for privileged userspace servers selftests/fuse: add test for FUSE_HAS_SYNCFS privilege gating
fs/fuse/fuse_i.h | 9 + fs/fuse/inode.c | 28 ++ include/uapi/linux/fuse.h | 12 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 +- .../selftests/filesystems/fuse/test_syncfs.c | 370 ++++++++++++++++++ 6 files changed, 420 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/test_syncfs.c
base-commit: 7d87a5a284bb34edb3f4e7e312ef403b3385a7b7
Signature · 1 line
2.50.1
PATCH v2 1/2 fuse: allow FUSE_SYNCFS for privileged userspace servers
Propagating syncfs()/sync() to a FUSE server via FUSE_SYNCFS lets the server flush its own cached or intermediate state when userspace asks the filesystem to sync. This is currently enabled only for virtiofs and fuseblk, because an untrusted server can use it to stall sync() indefinitely (see commit 2d82ab251ef0 ("virtiofs: propagate sync() to file server"), and commit d3906d8f3cee ("fuse: enable FUSE_SYNCFS for all fuseblk servers")). Both of those mount types require host privilege to set up, so the server is trusted not to abuse it.
There is nothing virtiofs- or block-specific about wanting to handle syncfs(), though. A plain /dev/fuse server is just as entitled to participate in the sync() path -- so that data it has buffered reaches stable storage when the user asks for it -- provided it is equally trusted.
The trust property that virtiofs and fuseblk satisfy is that neither can be mounted without CAP_SYS_ADMIN in the initial user namespace (neither sets FS_USERNS_MOUNT). A plain fuse mount does set FS_USERNS_MOUNT, so its existence guarantees no such privilege; the privilege has to be checked rather than assumed.
Add an opt-in INIT flag, FUSE_HAS_SYNCFS, and honor it only when the server opened /dev/fuse with CAP_SYS_ADMIN in the initial user namespace, recorded at mount time via file_ns_capable(). This is the same privilege that mounting virtiofs or fuseblk requires, applied to the process that actually services (and could stall) the connection. Checking the device opener's capability -- rather than the mount's user namespace -- avoids treating an unprivileged server that merely happens to run in the initial user namespace (e.g. a normal sshfs mount) as trusted.
The flag is only advertised to servers that pass this check, so an unprivileged server is never invited to opt in (and is ignored by fuse_syncfs_enable() if it sets the flag anyway).
--- fs/fuse/fuse_i.h | 9 +++++++++ fs/fuse/inode.c | 28 ++++++++++++++++++++++++++++ include/uapi/linux/fuse.h | 12 +++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-)
fs/fuse/fuse_i.h +9 -0
@@ -391,6 +391,7 @@ struct fuse_fs_context { bool no_control:1; bool no_force_umount:1; bool legacy_opts_show:1; + bool syncfs_capable:1;enum fuse_dax_mode dax_mode;unsigned int max_read;unsigned int blksize;
@@ -675,6 +676,14 @@ struct fuse_conn {/** @sync_fs: Propagate syncfs() to server */ unsigned int sync_fs:1; +/** +* @syncfs_capable: the privilege required to honor FUSE_HAS_SYNCFS was +* present when /dev/fuse was opened (CAP_SYS_ADMIN in the initial user +* namespace), i.e. the same privilege that mounting virtiofs/fuseblk +* requires. +*/ + unsigned int syncfs_capable:1; +/** @init_security: Initialize security xattrs when creating a new inode */ unsigned int init_security:1;
fs/fuse/inode.c +28 -0
@@ -800,6 +800,15 @@ static int fuse_opt_fd(struct fs_context *fsc, struct file *file)if (file->f_cred->user_ns != fsc->user_ns)return invalfc (fsc,"wrong user namespace for fuse device" ); +/* +* Record whether the server opened /dev/fuse with CAP_SYS_ADMIN in the +* initial user namespace -- the same privilege that mounting virtiofs +* or fuseblk requires. Only such servers are trusted to receive +* FUSE_SYNCFS (see fuse_syncfs_enable()). +*/ + ctx->syncfs_capable =file_ns_capable (file, &init_user_ns, +CAP_SYS_ADMIN ); + ctx->fud =fuse_dev_grab (file);return 0 ;
@@ -1266,6 +1275,16 @@ struct fuse_init_args {struct fuse_mount *fm; }; +/* +* A server can stall syncfs()/sync(), so only honor FUSE_HAS_SYNCFS for +* servers that opened /dev/fuse with CAP_SYS_ADMIN in the initial user +* namespace -- the same privilege required to mount virtiofs or fuseblk. +*/ +static bool fuse_syncfs_enable (struct fuse_conn *fc,u64 flags) +{ +return (flags &FUSE_HAS_SYNCFS ) && fc->syncfs_capable; +} +static void process_init_reply (struct fuse_args *args,int error) {struct fuse_init_args *ia =container_of (args,typeof (*ia), args);
@@ -1406,6 +1425,9 @@ static void process_init_reply(struct fuse_args *args, int error)if (flags &FUSE_REQUEST_TIMEOUT ) timeout = arg->request_timeout; + +if (fuse_syncfs_enable (fc, flags)) + fc->sync_fs =1 ; }else { ra_pages = fc->max_read /PAGE_SIZE ; fc->no_lock =1 ;
@@ -1473,6 +1495,11 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm) flags |=FUSE_SUBMOUNTS ;if (IS_ENABLED (CONFIG_FUSE_PASSTHROUGH )) flags |=FUSE_PASSTHROUGH ; +/* Only offered to sufficiently privileged servers; see +* fuse_syncfs_enable(). +*/ +if (fm->fc->syncfs_capable) + flags |=FUSE_HAS_SYNCFS ;/* * This is just an information flag for fuse server. No need to check
@@ -1766,6 +1793,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) fc->default_permissions = ctx->default_permissions; fc->allow_other = ctx->allow_other; + fc->syncfs_capable = ctx->syncfs_capable; fc->user_id = ctx->user_id; fc->group_id = ctx->group_id; fc->legacy_opts_show = ctx->legacy_opts_show;
include/uapi/linux/fuse.h +11 -1
@@ -240,6 +240,9 @@* - add FUSE_COPY_FILE_RANGE_64 * - add struct fuse_copy_file_range_out * - add FUSE_NOTIFY_PRUNE +* +* 7.46 +* - add FUSE_HAS_SYNCFS opt-in flag for privileged userspace servers */ #ifndef _LINUX_FUSE_H
@@ -275,7 +278,7 @@#define FUSE_KERNEL_VERSION 7/** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 45 +#define FUSE_KERNEL_MINOR_VERSION 46/** The node ID of the root inode */ #define FUSE_ROOT_ID 1
@@ -448,6 +451,12 @@ struct fuse_file_lock {* FUSE_OVER_IO_URING: Indicate that client supports io-uring * FUSE_REQUEST_TIMEOUT: kernel supports timing out requests. * init_out.request_timeout contains the timeout (in secs) +* FUSE_HAS_SYNCFS: server requests that syncfs()/sync() be propagated as +* FUSE_SYNCFS requests. Since an untrusted server can use this +* to stall sync(), it is only honored when /dev/fuse was opened +* with CAP_SYS_ADMIN in the initial user namespace (the same +* privilege that mounting virtiofs or fuseblk requires). +* Insufficiently privileged servers ignore it. */ #define FUSE_ASYNC_READ (1 << 0)#define FUSE_POSIX_LOCKS (1 << 1)
@@ -495,6 +504,7 @@ struct fuse_file_lock {#define FUSE_ALLOW_IDMAP (1ULL << 40)#define FUSE_OVER_IO_URING (1ULL << 41)#define FUSE_REQUEST_TIMEOUT (1ULL << 42) +#define FUSE_HAS_SYNCFS (1ULL << 43)/** * CUSE INIT request/reply flags
Signature · 1 line
2.50.1
PATCH v2 2/2 selftests/fuse: add test for FUSE_HAS_SYNCFS privilege gating
In reply to Jimmy ZuberAdd a selftest that talks the raw FUSE protocol over /dev/fuse (rather than via libfuse, which negotiates INIT internally) so it can both choose whether to advertise FUSE_HAS_SYNCFS and directly observe whether a FUSE_SYNCFS opcode is forwarded by the kernel.
Four cases are covered:
T1: host-root mount, server sets FUSE_HAS_SYNCFS
-> FUSE_SYNCFS must reach the server.
T2: host-root mount, server does not opt in
-> FUSE_SYNCFS must not be sent (back-compat).
T3: server opts in but opened /dev/fuse without CAP_SYS_ADMIN while still
in the initial user namespace
-> FUSE_SYNCFS must be withheld. This is the case that distinguishes
gating on the server's privilege from gating on the mount's user
namespace.
T4: unprivileged user-namespace mount, server sets FUSE_HAS_SYNCFS
-> FUSE_SYNCFS must be withheld.T4 requires CONFIG_USER_NS and the ability to create an unprivileged user-namespace mount; it is skipped otherwise.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 +- .../selftests/filesystems/fuse/test_syncfs.c | 370 ++++++++++++++++++ 3 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/filesystems/fuse/test_syncfs.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only fuse_mnt fusectl_test +test_syncfs
tools/testing/selftests/filesystems/fuse/Makefile +1 -1
@@ -2,7 +2,7 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) -TEST_GEN_PROGS := fusectl_test +TEST_GEN_PROGS := fusectl_test test_syncfs TEST_GEN_FILES := fuse_mnt include ../../lib.mk
tools/testing/selftests/filesystems/fuse/test_syncfs.c new file +370 -0
370 lines not on this page · raw message
Signature · 1 line
2.50.1
Hi Miklos,
Gentle ping on v2 - I've added CAP_SYS_ADMIN checks at /dev/fuse open time. I hope this addresses your concerns. Let me know if there's anything else you'd like changed.
Thank you,
Jimmy