* [PATCH 1/3] fuse: kill suid/sgid on fallocate and copy_file_range with KILLPRIV_V2
2026-11-22 13:29 [PATCH 0/3] Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2 Han-Wen Nienhuys
@ 2026-11-22 13:29 ` Han-Wen Nienhuys
2026-11-22 13:29 ` [PATCH 2/3] fuse: bump protocol minor version to 48 Han-Wen Nienhuys
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Han-Wen Nienhuys @ 2026-11-22 13:29 UTC (permalink / raw)
To: amir73il, miklos
Cc: fuse-devel, linux-fsdevel, mmitar, bernd, Han-Wen Nienhuys
From: Bernd Schubert <bernd@bsbernd.com>
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]
Fixes: 8981bdfda744 ("fuse: don't send ATTR_MODE to kill suid/sgid for handle_killpriv_v2")
Reported-by: Mitar <mmitar@gmail.com>
Closes: https://lore.kernel.org/fuse-devel/CAKLmikMhOYpGEw6uCqX_Bm0JPEg-4FhdJMNa9+tgtw3-Kf+eZg@mail.gmail.com/
Signed-off-by: Bernd Schubert <bernd@bsbernd.com>
Link: https://lore.kernel.org/fuse-devel/e75cf549-07a6-4095-bbf8-8caff7a3338b@bsbernd.com/
Signed-off-by: Han-Wen Nienhuys <hanwenn@gmail.com>
---
fs/fuse/dir.c | 13 +++++++++++--
fs/fuse/file.c | 3 ++-
fs/fuse/fuse_i.h | 2 +-
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index a04d93d5fc44..ae62b975a1bc 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -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
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 993a1d2b3151..efd35218f613 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -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)
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index e0992fcbcc55..6ed2d71dc9d1 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -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);
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 3/3] selftests/fuse: test FATTR_KILL_SUIDGID from file_remove_privs()
2026-11-22 13:29 [PATCH 0/3] Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2 Han-Wen Nienhuys
2026-11-22 13:29 ` [PATCH 1/3] fuse: kill suid/sgid on fallocate and copy_file_range " Han-Wen Nienhuys
2026-11-22 13:29 ` [PATCH 2/3] fuse: bump protocol minor version to 48 Han-Wen Nienhuys
@ 2026-11-22 13:29 ` Han-Wen Nienhuys
2026-11-23 8:13 ` [PATCH 0/3] Subject: [PATCH 0/3] fuse: kill suid/sgid on fallocate, copy_file_range and passthrough writes with KILLPRIV_V2 Mitar
3 siblings, 0 replies; 5+ messages in thread
From: Han-Wen Nienhuys @ 2026-11-22 13:29 UTC (permalink / raw)
To: amir73il, miklos
Cc: fuse-devel, linux-fsdevel, mmitar, bernd, Han-Wen Nienhuys
Add 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.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Han-Wen Nienhuys <hanwenn@gmail.com>
---
.../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
diff --git a/tools/testing/selftests/filesystems/fuse/.gitignore b/tools/testing/selftests/filesystems/fuse/.gitignore
index 7c787313e69b..02ff7a1cbc16 100644
--- a/tools/testing/selftests/filesystems/fuse/.gitignore
+++ b/tools/testing/selftests/filesystems/fuse/.gitignore
@@ -6,3 +6,4 @@ write_extend_eof_test
fuse_acl_cache_test
fuse_symlink_cache_test
fuse_readdir_cache_test
+killpriv_v2_test
diff --git a/tools/testing/selftests/filesystems/fuse/Makefile b/tools/testing/selftests/filesystems/fuse/Makefile
index e3ae3acff113..89560f1ad12d 100644
--- a/tools/testing/selftests/filesystems/fuse/Makefile
+++ b/tools/testing/selftests/filesystems/fuse/Makefile
@@ -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),)
diff --git a/tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c b/tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c
new file mode 100644
index 000000000000..41d65ff64e22
--- /dev/null
+++ b/tools/testing/selftests/filesystems/fuse/killpriv_v2_test.c
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * With FUSE_HANDLE_KILLPRIV_V2 the server clears suid/sgid when the kernel
+ * asks it to. Paths that modify data without sending FUSE_WRITE (fallocate,
+ * copy_file_range, passthrough writes) go through file_remove_privs(), which
+ * must result in a FUSE_SETATTR carrying FATTR_KILL_SUIDGID.
+ *
+ * This is a raw /dev/fuse server holding a single file; its mode is the
+ * server's, so the test checks what the server was asked to do.
+ *
+ * Requires root to mount fuse.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/uio.h>
+#include <linux/capability.h>
+#include <linux/fuse.h>
+
+#include "../../kselftest_harness.h"
+
+#define FUSE_ROOT_ID 1
+#define FILE_INO 2
+#define MAX_WRITE (128 * 1024)
+
+struct server {
+ int fd;
+ int killpriv_v2; /* negotiated */
+ uint32_t mode; /* the file's mode, S_IFREG included */
+ uint64_t size;
+ int kill_setattr; /* SETATTRs with FATTR_KILL_SUIDGID */
+ int kill_write; /* WRITEs with FUSE_WRITE_KILL_SUIDGID */
+};
+
+static void reply(int fd, uint64_t unique, int error, void *data, size_t len)
+{
+ struct fuse_out_header oh = {
+ .len = sizeof(oh) + (data ? len : 0),
+ .error = error,
+ .unique = unique,
+ };
+ struct iovec iov[2] = { { &oh, sizeof(oh) }, { data, len } };
+
+ /* Errors here are teardown races (device closed on unmount); ignore. */
+ (void)writev(fd, iov, data ? 2 : 1);
+}
+
+static void fill_attr(struct fuse_attr *a, uint64_t ino, uint32_t mode,
+ uint64_t size)
+{
+ memset(a, 0, sizeof(*a));
+ a->ino = ino;
+ a->mode = mode;
+ a->nlink = 1;
+ a->size = size;
+ a->blksize = sysconf(_SC_PAGESIZE);
+}
+
+/* The VFS rule the server must follow: suid always, sgid if group-exec. */
+static void kill_suidgid(struct server *s)
+{
+ s->mode &= ~S_ISUID;
+ if (s->mode & 0010)
+ s->mode &= ~S_ISGID;
+}
+
+static void *server_thread(void *arg)
+{
+ struct server *s = arg;
+ static char buf[MAX_WRITE + 4096];
+
+ for (;;) {
+ ssize_t n = read(s->fd, buf, sizeof(buf));
+ struct fuse_in_header *ih = (void *)buf;
+ int root;
+
+ if (n < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return NULL; /* device closed on unmount */
+ }
+ if (n < (ssize_t)sizeof(*ih))
+ continue;
+ root = ih->nodeid == FUSE_ROOT_ID;
+
+ switch (ih->opcode) {
+ case FUSE_INIT: {
+ struct fuse_init_in *in = (void *)(ih + 1);
+ struct fuse_init_out out = {0};
+
+ out.major = FUSE_KERNEL_VERSION;
+ out.minor = FUSE_KERNEL_MINOR_VERSION;
+ out.max_readahead = in->max_readahead;
+ out.max_write = MAX_WRITE;
+ out.max_background = 16;
+ out.congestion_threshold = 12;
+ out.flags = in->flags & FUSE_HANDLE_KILLPRIV_V2;
+ s->killpriv_v2 = !!out.flags;
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_GETATTR: {
+ struct fuse_attr_out out = {0};
+
+ fill_attr(&out.attr, ih->nodeid,
+ root ? (S_IFDIR | 0755) : s->mode,
+ root ? 0 : s->size);
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_LOOKUP: {
+ struct fuse_entry_out out = {0};
+
+ out.nodeid = FILE_INO;
+ fill_attr(&out.attr, FILE_INO, s->mode, s->size);
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_OPEN:
+ case FUSE_OPENDIR: {
+ struct fuse_open_out out = {0};
+
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_WRITE: {
+ struct fuse_write_in *in = (void *)(ih + 1);
+ struct fuse_write_out out = {0};
+
+ if (in->write_flags & FUSE_WRITE_KILL_SUIDGID) {
+ s->kill_write++;
+ kill_suidgid(s);
+ }
+ if (in->offset + in->size > s->size)
+ s->size = in->offset + in->size;
+ out.size = in->size;
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_SETATTR: {
+ struct fuse_setattr_in *in = (void *)(ih + 1);
+ struct fuse_attr_out out = {0};
+
+ if (in->valid & FATTR_MODE)
+ s->mode = (s->mode & S_IFMT) |
+ (in->mode & 07777);
+ if (in->valid & FATTR_SIZE)
+ s->size = in->size;
+ if (in->valid & FATTR_KILL_SUIDGID) {
+ s->kill_setattr++;
+ kill_suidgid(s);
+ }
+ fill_attr(&out.attr, ih->nodeid, s->mode, s->size);
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_FALLOCATE: {
+ struct fuse_fallocate_in *in = (void *)(ih + 1);
+
+ if (in->offset + in->length > s->size)
+ s->size = in->offset + in->length;
+ reply(s->fd, ih->unique, 0, NULL, 0);
+ break;
+ }
+ case FUSE_COPY_FILE_RANGE_64: {
+ /*
+ * Succeed without copying: an error would make the
+ * kernel fall back to splice, which sends WRITEs.
+ */
+ struct fuse_copy_file_range_in *in = (void *)(ih + 1);
+ struct fuse_copy_file_range_out out = {
+ .bytes_copied = in->len,
+ };
+
+ if (in->off_out + in->len > s->size)
+ s->size = in->off_out + in->len;
+ reply(s->fd, ih->unique, 0, &out, sizeof(out));
+ break;
+ }
+ case FUSE_GETXATTR:
+ reply(s->fd, ih->unique, -ENODATA, NULL, 0);
+ break;
+ case FUSE_FLUSH:
+ case FUSE_RELEASE:
+ case FUSE_RELEASEDIR:
+ case FUSE_FSYNC:
+ case FUSE_ACCESS:
+ reply(s->fd, ih->unique, 0, NULL, 0);
+ break;
+ case FUSE_FORGET:
+ break;
+ default:
+ reply(s->fd, ih->unique, -EOPNOTSUPP, NULL, 0);
+ break;
+ }
+ }
+}
+
+/* Drop CAP_FSETID from this thread's effective set; the server keeps it. */
+static int drop_fsetid(void)
+{
+ struct __user_cap_header_struct hdr = {
+ .version = _LINUX_CAPABILITY_VERSION_3,
+ };
+ struct __user_cap_data_struct data[2];
+
+ if (syscall(SYS_capget, &hdr, data))
+ return -1;
+ data[CAP_TO_INDEX(CAP_FSETID)].effective &= ~CAP_TO_MASK(CAP_FSETID);
+ return syscall(SYS_capset, &hdr, data);
+}
+
+FIXTURE(fuse)
+{
+ struct server srv;
+ char dir[64];
+ char path[128];
+ int mounted;
+};
+
+FIXTURE_SETUP(fuse)
+{
+ char opts[128];
+ pthread_t t;
+ int fd;
+
+ self->srv.fd = -1;
+ if (geteuid() != 0)
+ SKIP(return, "need root to mount fuse");
+
+ self->srv.mode = S_IFREG | 06775;
+ self->srv.fd = open("/dev/fuse", O_RDWR);
+ ASSERT_GE(self->srv.fd, 0);
+
+ strcpy(self->dir, "/tmp/fuse_killpriv_XXXXXX");
+ ASSERT_NE(NULL, mkdtemp(self->dir));
+ snprintf(self->path, sizeof(self->path), "%s/file", self->dir);
+
+ snprintf(opts, sizeof(opts),
+ "fd=%d,rootmode=40000,user_id=0,group_id=0",
+ self->srv.fd);
+ ASSERT_EQ(0, mount("fuse", self->dir, "fuse", 0, opts));
+ self->mounted = 1;
+
+ ASSERT_EQ(0, pthread_create(&t, NULL, server_thread, &self->srv));
+ pthread_detach(t);
+
+ /* The first request completes INIT. */
+ fd = open(self->dir, O_RDONLY | O_DIRECTORY);
+ ASSERT_GE(fd, 0);
+ close(fd);
+ if (!self->srv.killpriv_v2)
+ SKIP(return, "kernel does not offer FUSE_HANDLE_KILLPRIV_V2");
+}
+
+FIXTURE_TEARDOWN(fuse)
+{
+ if (self->mounted)
+ umount2(self->dir, MNT_DETACH);
+ if (self->srv.fd >= 0)
+ close(self->srv.fd);
+ if (self->dir[0])
+ rmdir(self->dir);
+}
+
+/* fallocate() sends no FUSE_WRITE; the kill must come with SETATTR. */
+TEST_F(fuse, fallocate_unprivileged)
+{
+ int fd;
+
+ ASSERT_EQ(0, drop_fsetid());
+ fd = open(self->path, O_RDWR);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(0, fallocate(fd, 0, 0, 4096));
+ close(fd);
+
+ EXPECT_EQ(1, self->srv.kill_setattr);
+ EXPECT_EQ(S_IFREG | 0775, self->srv.mode);
+}
+
+/* Without group exec, sgid stays; the kernel still asks for suid. */
+TEST_F(fuse, fallocate_unprivileged_sgid_no_group_exec)
+{
+ int fd;
+
+ self->srv.mode = S_IFREG | 06765;
+ ASSERT_EQ(0, drop_fsetid());
+ fd = open(self->path, O_RDWR);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(0, fallocate(fd, 0, 0, 4096));
+ close(fd);
+
+ EXPECT_EQ(1, self->srv.kill_setattr);
+ EXPECT_EQ(S_IFREG | 02765, self->srv.mode);
+}
+
+/* With CAP_FSETID nothing is killed. */
+TEST_F(fuse, fallocate_privileged)
+{
+ int fd;
+
+ fd = open(self->path, O_RDWR);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(0, fallocate(fd, 0, 0, 4096));
+ close(fd);
+
+ EXPECT_EQ(0, self->srv.kill_setattr);
+ EXPECT_EQ(S_IFREG | 06775, self->srv.mode);
+}
+
+/* copy_file_range() sends no FUSE_WRITE either. */
+TEST_F(fuse, copy_file_range_unprivileged)
+{
+ loff_t in = 0, out = 4096;
+ int fd;
+
+ self->srv.size = 4096;
+ ASSERT_EQ(0, drop_fsetid());
+ fd = open(self->path, O_RDWR);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(4096, copy_file_range(fd, &in, fd, &out, 4096, 0));
+ close(fd);
+
+ EXPECT_EQ(1, self->srv.kill_setattr);
+ EXPECT_EQ(S_IFREG | 0775, self->srv.mode);
+}
+
+/*
+ * A write asks twice: in the SETATTR from file_remove_privs(), and with
+ * FUSE_WRITE_KILL_SUIDGID on the WRITE. The SETATTR reply updates the cached
+ * mode, so a second write sends no SETATTR; the WRITE flag depends only on
+ * CAP_FSETID and comes every time.
+ */
+TEST_F(fuse, write_unprivileged)
+{
+ int fd;
+
+ ASSERT_EQ(0, drop_fsetid());
+ fd = open(self->path, O_RDWR);
+ ASSERT_GE(fd, 0);
+ ASSERT_EQ(1, pwrite(fd, "x", 1, 0));
+ EXPECT_EQ(1, self->srv.kill_setattr);
+ EXPECT_EQ(1, self->srv.kill_write);
+ EXPECT_EQ(S_IFREG | 0775, self->srv.mode);
+
+ ASSERT_EQ(1, pwrite(fd, "y", 1, 1));
+ close(fd);
+ EXPECT_EQ(1, self->srv.kill_setattr);
+ EXPECT_EQ(2, self->srv.kill_write);
+}
+
+TEST_HARNESS_MAIN
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread