Skip to contentSkip to conversation

Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2

PATCH New Han-Wen Nienhuys 1 participant 3 messages Nov 22 FUSE filesystem core

lore
Share & tools
lore
#0 lore raw

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.

[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 (1):
fuse: bump protocol minor version to 48

 fs/fuse/dir.c             | 13 +++++++++++--
 fs/fuse/file.c            |  3 ++-
 fs/fuse/fuse_i.h          |  2 +-
 include/uapi/linux/fuse.h |  6 +++++-
 4 files changed, 19 insertions(+), 5 deletions(-)
Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/2 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
index a04d93d5fc44..ae62b975a1bc 100644
@@ -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
index 993a1d2b3151..efd35218f613 100644
@@ -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
index e0992fcbcc55..6ed2d71dc9d1 100644
@@ -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
#2 lore raw

PATCH 2/2 fuse: bump protocol minor version to 48

In reply to Han-Wen Nienhuys

The 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.

Assisted-by Claude:claude-opus-5-5
---
 include/uapi/linux/fuse.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/uapi/linux/fuse.h +5 -1
index cb9e9554f0ef..6f6baded766b 100644
@@ -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