Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2
Subsystems (2)
cover Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2
With FUSE_HANDLE_KILLPRIV_V2, an unprivileged fallocate(), copy_file_range() or passthrough write leaves the suid/sgid bits set, because the kernel never asks the server to clear them. Mitar reported this in [1], and Bernd posted a fix in [2].
Patch 1 is Bernd's fix, rebased on the most recent pull of FUSE fixes[3] in Linus' tree, otherwise unchanged.
Patch 2 bumps the FUSE protocol minor version to 48, so a server can tell whether the kernel has the fix before it asks for FUSE_HANDLE_KILLPRIV_V2.
Patch 3 adds a selftest. It runs a raw /dev/fuse server and checks which kill requests reach it.
I messed up my email sending just now, leaving out the self-test patch, so trying it again.
[1] https://lore.kernel.org/fuse-devel/CAKLmikMhOYpGEw6uCqX_Bm0JPEg-4FhdJMNa9+tgtw3-Kf+eZg@mail.gmail.com/
[2] https://lore.kernel.org/fuse-devel/[email protected]/
[3] https://lore.kernel.org/fuse-devel/179520930874.792202.7768867709024262369.pr-tracker-bot@kernel.org/
Bernd Schubert (1):
fuse: kill suid/sgid on fallocate and copy_file_range with KILLPRIV_V2
Han-Wen Nienhuys (2):
fuse: bump protocol minor version to 48 selftests/fuse: test FATTR_KILL_SUIDGID from file_remove_privs()
fs/fuse/dir.c | 13 +- fs/fuse/file.c | 3 +- fs/fuse/fuse_i.h | 2 +- include/uapi/linux/fuse.h | 6 +- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 + .../filesystems/fuse/killpriv_v2_test.c | 359 ++++++++++++++++++ 7 files changed, 381 insertions(+), 5 deletions(-) create mode 100644 tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c
Signature · 1 line
2.56.1
PATCH 1/3 fuse: kill suid/sgid on fallocate and copy_file_range with KILLPRIV_V2
From: Bernd Schubert <[email protected]>
With FUSE_HANDLE_KILLPRIV_V2, fuse_setattr() drops the mode change that file_remove_privs() asks for and relies on the server to clear the bits when it handles the following WRITE request (FUSE_WRITE_KILL_SUIDGID). fallocate() and copy_file_range() send no WRITE, and the SETATTR sent from file_modified() carries no kill flag. A caller without CAP_FSETID can therefore change the contents of a setuid file while the setuid bit stays set; write() on the same file clears it.
Send FATTR_KILL_SUIDGID in that SETATTR. The VFS sets ATTR_KILL_SUID and ATTR_KILL_SGID only after its own CAP_FSETID check, so capable() is not tested again.
This affects every caller of file_remove_privs() and kiocb_modified(), not only fallocate and copy_file_range: passthrough writes (backing_file_write_iter()), ext_map writes, DAX writes, and ordinary cached writes (fuse_perform_write()). Before, their SETATTRs went out with valid == 0, so no requests are added. On an ordinary write the server is now asked twice, in the SETATTR and again by FUSE_WRITE_KILL_SUIDGID on the WRITE, which then finds the bits already cleared. This happens once: the SETATTR reply updates the cached mode, so later writes find nothing to kill.
[HWN: rebased onto v7.4-rc3, extended the commit message]
--- fs/fuse/dir.c | 13 +++++++++++-- fs/fuse/file.c | 3 ++- fs/fuse/fuse_i.h | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-)
fs/fuse/dir.c +11 -2
@@ -2148,7 +2148,7 @@ int fuse_flush_times(struct inode *inode, struct fuse_file *ff)* and the actual truncation by hand. */ int fuse_do_setattr (const struct mnt_idmap *idmap,struct dentry *dentry, -struct iattr *attr,struct file *file) +struct iattr *attr,struct file *file,bool kill_suidgid) {struct inode *inode =d_inode (dentry);struct fuse_mount *fm =get_fuse_mount (inode);
@@ -2241,6 +2241,13 @@ int fuse_do_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, attr->ia_valid & (ATTR_UID | ATTR_GID)) inarg.valid |=FATTR_KILL_SUIDGID ; +/* +* fuse_setattr() dropped the mode change for the kill request. The VFS +* already applied the CAP_FSETID rule, so no capable() test here. +*/ +if (fc->handle_killpriv_v2 && kill_suidgid) + inarg.valid |=FATTR_KILL_SUIDGID ; +if (attr->ia_valid &ATTR_SIZE ) {/* For mandatory locking in truncate */ inarg.valid |=FATTR_LOCKOWNER ;
@@ -2337,6 +2344,7 @@ static int fuse_setattr(const struct mnt_idmap *idmap, struct dentry *entry,struct inode *inode =d_inode (entry);struct fuse_conn *fc =get_fuse_conn (inode);struct file *file = (attr->ia_valid &ATTR_FILE ) ? attr->ia_file :NULL ; +bool kill_suidgid = false;int ret;if (fuse_is_bad (inode))
@@ -2346,6 +2354,7 @@ static int fuse_setattr(const struct mnt_idmap *idmap, struct dentry *entry,return -EACCES ;if (attr->ia_valid & (ATTR_KILL_SUID |ATTR_KILL_SGID )) { + kill_suidgid = true; attr->ia_valid &= ~(ATTR_KILL_SUID |ATTR_KILL_SGID |ATTR_MODE );
@@ -2378,7 +2387,7 @@ static int fuse_setattr(const struct mnt_idmap *idmap, struct dentry *entry,if (!attr->ia_valid)return 0 ; - ret =fuse_do_setattr (idmap, entry, attr, file); + ret =fuse_do_setattr (idmap, entry, attr, file, kill_suidgid);if (!ret) {/* * If filesystem supports acls it may have updated acl xattrs in
fs/fuse/file.c +2 -1
@@ -2766,7 +2766,8 @@ static void fuse_do_truncate(struct file *file) attr.ia_file = file; attr.ia_valid |=ATTR_FILE ; -fuse_do_setattr (file_mnt_idmap (file),file_dentry (file), &attr, file); +fuse_do_setattr (file_mnt_idmap (file),file_dentry (file), &attr, file, + false); }static inline loff_t fuse_round_up (struct fuse_conn *fc,loff_t off)
fs/fuse/fuse_i.h +1 -1
@@ -1250,7 +1250,7 @@ int fuse_flush_times(struct inode *inode, struct fuse_file *ff);int fuse_write_inode (struct inode *inode,struct writeback_control *wbc);int fuse_do_setattr (const struct mnt_idmap *idmap,struct dentry *dentry, -struct iattr *attr,struct file *file); +struct iattr *attr,struct file *file,bool kill_suidgid);void fuse_unlock_inode (struct inode *inode,bool locked);bool fuse_lock_inode (struct inode *inode);
Signature · 1 line
2.56.1
PATCH 2/3 fuse: bump protocol minor version to 48
In reply to Han-Wen NienhuysThe commit "fuse: kill suid/sgid on fallocate and copy_file_range with KILLPRIV_V2" fixes a bug with FUSE_HANDLE_KILLPRIV_V2: the kernel did not tell the server to clear suid/sgid after fallocate, copy_file_range and passthrough writes.
A server cannot detect this bug by itself. All it sees is an empty FUSE_SETATTR, and the kernel also sends one when only security.capability must be removed.
Bump the minor version, so a server can check whether the kernel has the fix before it asks for FUSE_HANDLE_KILLPRIV_V2, for example when it also uses passthrough.
This is not meant for stable. On older kernels, a FUSE server should use a workaround instead.
--- include/uapi/linux/fuse.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/uapi/linux/fuse.h +5 -1
@@ -258,6 +258,10 @@* - add FUSE_NOTIFY_BACKING_MAP, fuse_notify_backing_map_out, fuse_extent * - add FUSE_BACKING_MAP_CREATE, FUSE_BACKING_MAP_CYCLIC * - extend the meaning of FUSE_HAS_INODE_DAX and FUSE_ATTR_DAX +* +* 7.48 +* - with FUSE_HANDLE_KILLPRIV_V2, send FATTR_KILL_SUIDGID in the SETATTR +* that precedes fallocate, copy_file_range and passthrough writes */ #ifndef _LINUX_FUSE_H
@@ -293,7 +297,7 @@#define FUSE_KERNEL_VERSION 7/** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 47 +#define FUSE_KERNEL_MINOR_VERSION 48/** The node ID of the root inode */ #define FUSE_ROOT_ID 1
Signature · 1 line
2.56.1
PATCH 3/3 selftests/fuse: test FATTR_KILL_SUIDGID from file_remove_privs()
In reply to Han-Wen NienhuysAdd tests for commit "fuse: kill suid/sgid on fallocate and copy_file_range with KILLPRIV_V2".
With FUSE_HANDLE_KILLPRIV_V2, when a caller without CAP_FSETID calls fallocate() or copy_file_range(), the kernel must ask the server to clear suid/sgid. These calls send no FUSE_WRITE, so the request comes as FATTR_KILL_SUIDGID on the FUSE_SETATTR from file_remove_privs().
The tests also check that sgid stays if the file is not group-executable, that a caller with CAP_FSETID keeps both bits, and that only the first of two writes sends the kill in a SETATTR.
--- .../selftests/filesystems/fuse/.gitignore | 1 + .../selftests/filesystems/fuse/Makefile | 2 + .../filesystems/fuse/killpriv_v2_test.c | 359 ++++++++++++++++++ 3 files changed, 362 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
@@ -6,3 +6,4 @@ write_extend_eof_test fuse_acl_cache_test fuse_symlink_cache_test fuse_readdir_cache_test +killpriv_v2_test
tools/testing/selftests/filesystems/fuse/Makefile +2 -0
@@ -7,11 +7,13 @@ TEST_GEN_PROGS += write_extend_eof_test TEST_GEN_PROGS += fuse_acl_cache_test TEST_GEN_PROGS += fuse_symlink_cache_test TEST_GEN_PROGS += fuse_readdir_cache_test +TEST_GEN_PROGS += killpriv_v2_test TEST_GEN_FILES := fuse_mnt include ../../lib.mk $(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread +$(OUTPUT)/killpriv_v2_test: LDLIBS += -lpthread VAR_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) ifeq ($(VAR_CFLAGS),)
tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c new file +359 -0
359 lines not on this page · raw message
Signature · 1 line
2.56.1
Hi!
Thank you for posting this, and thanks to Bernd for the fix. The minor version bump is what I hoped for: with it a server can tell whether it may ask for FUSE_HANDLE_KILLPRIV_V2.
One small suggestion for the 7.48 entry in patch 2, which should not delay the series: as the commit message of patch 1 says, the SETATTR is sent before every caller of file_remove_privs(), not only before fallocate, copy_file_range and passthrough writes. It is also a new kind of SETATTR, with FATTR_KILL_SUIDGID as its only attribute, so a server which honors the flag only together with FATTR_SIZE (as virtiofsd does) ignores it. Maybe something like:
- with FUSE_HANDLE_KILLPRIV_V2, send FATTR_KILL_SUIDGID in the SETATTR from file_remove_privs(), possibly as its only attribute
Mitar
Signature · 3 lines
https://mitar.tnode.com/ https://noc.social/@mitar https://bsky.app/profile/mitar.bsky.social