fuse: HANDLE_KILLPRIV_V2 does not kill suid/sgid on fallocate() and copy_file_range()
Subsystems (2)
Hi!
While adding FUSE_HANDLE_KILLPRIV_V2 support to a go-fuse based server, I noticed that with it negotiated, fallocate() and copy_file_range() by a caller without CAP_FSETID no longer clear the setuid and setgid bits of the file they modify, and that the server has no way to learn that it should clear them itself.
The kernel makes the decision, but drops it on the way. fuse_file_fallocate() and __fuse_copy_file_range() both call file_modified() before sending the request, which ends in fuse_setattr() with ATTR_KILL_SUID and ATTR_KILL_SGID. Since commit 8981bdfda744 ("fuse: don't send ATTR_MODE to kill suid/sgid for handle_killpriv_v2"), fuse_setattr() drops those when fc->handle_killpriv_v2 is set, expecting the server to kill the bits when the WRITE which follows arrives. But no WRITE follows FUSE_FALLOCATE or FUSE_COPY_FILE_RANGE, and neither carries such a flag. All the server sees is an empty SETATTR before the request, which it cannot tell apart from the one it gets when only ATTR_KILL_PRIV was asked for.
For "fallocate -l 8192 f" run by an unprivileged user, on a file with mode 06777 which they may write to, the server receives:
GETXATTR "security.capability" -> ENODATA
SETATTR valid=0
FALLOCATE offset=0 length=8192 mode=0
and the file keeps mode 06777. Without FUSE_HANDLE_KILLPRIV_V2, the same command clears both bits, since fuse_setattr() then turns the request into a SETATTR of the mode. I verified the fallocate() case. The copy_file_range() case is from reading the code, as the server I tested does not implement FUSE_COPY_FILE_RANGE.
This matters beyond the mode looking wrong: fallocate() with FALLOC_FL_ZERO_RANGE or FALLOC_FL_PUNCH_HOLE, and copy_file_range() even more so, change what the file holds. So somebody who may write to a setuid file can replace its contents and keep the setuid bit, which is what killing the bits is there to prevent.
It looks like an interaction between changes rather than a choice. Commit fe0da9c09b2d ("fuse: copy_file_range needs to strip setuid bits and update timestamps") made copy_file_range() kill the bits in 2019, which stopped working on FUSE_HANDLE_KILLPRIV_V2 connections with 8981bdfda744 in 2020. Commit 4a6f278d4827 ("fuse: add file_modified() to fallocate") added the same to fallocate() in 2022, which takes effect only without FUSE_HANDLE_KILLPRIV_V2. As far as I can tell from its source, virtiofsd with --killpriv-v2 has the same gap: it drops CAP_FSETID for write, open and setattr when asked to, and calls fallocate() and copy_file_range() as they are.
I think we should pass the decision with the request, as for WRITE and OPEN. For FUSE_FALLOCATE, a new FUSE_FALLOCATE_KILL_SUIDGID could go into the currently unused fuse_fallocate_in.padding. For FUSE_COPY_FILE_RANGE, it would need a new field, or a bit in fuse_copy_file_range_in.flags behind a new INIT flag: servers pass .flags on to copy_file_range(2) as it is (virtiofsd does), which fails with EINVAL on any bit, so a new bit there cannot be sent to a server which did not ask for it.
What do you think?
Mitar
Signature · 3 lines
https://mitar.tnode.com/ https://noc.social/@mitar https://bsky.app/profile/mitar.bsky.social
Quoted 60 lines · Mitar
Hi!
While adding
FUSE_HANDLE_KILLPRIV_V2support to a go-fuse based server, I noticed that with it negotiated,fallocate()andcopy_file_range()by a caller withoutCAP_FSETIDno longer clear the setuid and setgid bits of the file they modify, and that the server has no way to learn that it should clear them itself.The kernel makes the decision, but drops it on the way.
fuse_file_fallocate()and__fuse_copy_file_range()both callfile_modified()before sending the request, which ends infuse_setattr()withATTR_KILL_SUIDandATTR_KILL_SGID. Since commit8981bdfda744("fuse: don't sendATTR_MODEto kill suid/sgid forhandle_killpriv_v2"),fuse_setattr()drops those whenfc->handle_killpriv_v2is set, expecting the server to kill the bits when the WRITE which follows arrives. But no WRITE followsFUSE_FALLOCATEorFUSE_COPY_FILE_RANGE, and neither carries such a flag. All the server sees is an empty SETATTR before the request, which it cannot tell apart from the one it gets when onlyATTR_KILL_PRIVwas asked for.For "fallocate -l 8192 f" run by an unprivileged user, on a file with mode 06777 which they may write to, the server receives:
GETXATTR "security.capability" -> ENODATA
SETATTR valid=0
FALLOCATE offset=0 length=8192 mode=0and the file keeps mode 06777. Without
FUSE_HANDLE_KILLPRIV_V2, the same command clears both bits, sincefuse_setattr()then turns the request into a SETATTR of the mode. I verified thefallocate()case. Thecopy_file_range()case is from reading the code, as the server I tested does not implementFUSE_COPY_FILE_RANGE.This matters beyond the mode looking wrong:
fallocate()withFALLOC_FL_ZERO_RANGEorFALLOC_FL_PUNCH_HOLE, andcopy_file_range()even more so, change what the file holds. So somebody who may write to a setuid file can replace its contents and keep the setuid bit, which is what killing the bits is there to prevent.It looks like an interaction between changes rather than a choice. Commit
fe0da9c09b2d("fuse:copy_file_rangeneeds to strip setuid bits and update timestamps") madecopy_file_range()kill the bits in 2019, which stopped working onFUSE_HANDLE_KILLPRIV_V2connections with8981bdfda744in 2020. Commit4a6f278d4827("fuse: addfile_modified()to fallocate") added the same tofallocate()in 2022, which takes effect only withoutFUSE_HANDLE_KILLPRIV_V2. As far as I can tell from its source, virtiofsd with --killpriv-v2 has the same gap: it dropsCAP_FSETIDfor write, open and setattr when asked to, and callsfallocate()andcopy_file_range()as they are.I think we should pass the decision with the request, as for WRITE and OPEN. For
FUSE_FALLOCATE, a newFUSE_FALLOCATE_KILL_SUIDGIDcould go into the currently unused fuse_fallocate_in.padding. ForFUSE_COPY_FILE_RANGE, it would need a new field, or a bit in fuse_copy_file_range_in.flags behind a new INIT flag: servers pass .flags on tocopy_file_range(2)as it is (virtiofsd does), which fails with EINVAL on any bit, so a new bit there cannot be sent to a server which did not ask for it.
Quoted 2 lines
What do you think?
I had actually noticed this with recent test additions in libfuse and had to add workarounds (on the day exactly one month ago). Unfortunately had been too busy to post the kernel patch yet.
libfuse commitsdaa6cb7f6996 — "test: check the KILLPRIV_V2 kill-suidgid flags reach the filesystem"
and
60ab8c0c35de ("test: check a write through passthrough_hp clears suid/sgid")
When testing the latter commit I had noticed the issue.
Could you try the attached patch? Right now totally untested, I can test and send it out earliest during the weekend.
Thanks,
Bernd
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.
--- 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
@@ -2143,7 +2143,7 @@ int fuse_flush_times(struct inode *inode, struct fuse_file *ff)* and the actual truncation by hand. */ int fuse_do_setattr (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);
@@ -2233,6 +2233,13 @@ int fuse_do_setattr(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 ;
@@ -2329,6 +2336,7 @@ static int fuse_setattr(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))
@@ -2338,6 +2346,7 @@ static int fuse_setattr(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 );
@@ -2370,7 +2379,7 @@ static int fuse_setattr(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
@@ -2708,7 +2708,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
@@ -1190,7 +1190,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 (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);
Hi!
Awesome! I looked at the patch and this is all probably above my head, but I think the patch should also include some protocol text describing the flag-only SETATTR, because this is now a new behavior, no? And probably we should also do a minor version bump so servers can tell that they can rely on this behavior and decide to request CAP_HANDLE_KILLPRIV_V2. For example, in our case, we do not request CAP_HANDLE_KILLPRIV_V2 now because we are aware of this issue with fallocate() andcopy_file_range().
Mitar
Quoted 87 lines · Bernd Schubert
Quoted 60 lines · Mitar
Hi!
While adding
FUSE_HANDLE_KILLPRIV_V2support to a go-fuse based server, I noticed that with it negotiated,fallocate()andcopy_file_range()by a caller withoutCAP_FSETIDno longer clear the setuid and setgid bits of the file they modify, and that the server has no way to learn that it should clear them itself.The kernel makes the decision, but drops it on the way.
fuse_file_fallocate()and__fuse_copy_file_range()both callfile_modified()before sending the request, which ends infuse_setattr()withATTR_KILL_SUIDandATTR_KILL_SGID. Since commit8981bdfda744("fuse: don't sendATTR_MODEto kill suid/sgid forhandle_killpriv_v2"),fuse_setattr()drops those whenfc->handle_killpriv_v2is set, expecting the server to kill the bits when the WRITE which follows arrives. But no WRITE followsFUSE_FALLOCATEorFUSE_COPY_FILE_RANGE, and neither carries such a flag. All the server sees is an empty SETATTR before the request, which it cannot tell apart from the one it gets when onlyATTR_KILL_PRIVwas asked for.For "fallocate -l 8192 f" run by an unprivileged user, on a file with mode 06777 which they may write to, the server receives:
GETXATTR "security.capability" -> ENODATA
SETATTR valid=0
FALLOCATE offset=0 length=8192 mode=0and the file keeps mode 06777. Without
FUSE_HANDLE_KILLPRIV_V2, the same command clears both bits, sincefuse_setattr()then turns the request into a SETATTR of the mode. I verified thefallocate()case. Thecopy_file_range()case is from reading the code, as the server I tested does not implementFUSE_COPY_FILE_RANGE.This matters beyond the mode looking wrong:
fallocate()withFALLOC_FL_ZERO_RANGEorFALLOC_FL_PUNCH_HOLE, andcopy_file_range()even more so, change what the file holds. So somebody who may write to a setuid file can replace its contents and keep the setuid bit, which is what killing the bits is there to prevent.It looks like an interaction between changes rather than a choice. Commit
fe0da9c09b2d("fuse:copy_file_rangeneeds to strip setuid bits and update timestamps") madecopy_file_range()kill the bits in 2019, which stopped working onFUSE_HANDLE_KILLPRIV_V2connections with8981bdfda744in 2020. Commit4a6f278d4827("fuse: addfile_modified()to fallocate") added the same tofallocate()in 2022, which takes effect only withoutFUSE_HANDLE_KILLPRIV_V2. As far as I can tell from its source, virtiofsd with --killpriv-v2 has the same gap: it dropsCAP_FSETIDfor write, open and setattr when asked to, and callsfallocate()andcopy_file_range()as they are.I think we should pass the decision with the request, as for WRITE and OPEN. For
FUSE_FALLOCATE, a newFUSE_FALLOCATE_KILL_SUIDGIDcould go into the currently unused fuse_fallocate_in.padding. ForFUSE_COPY_FILE_RANGE, it would need a new field, or a bit in fuse_copy_file_range_in.flags behind a new INIT flag: servers pass .flags on tocopy_file_range(2)as it is (virtiofsd does), which fails with EINVAL on any bit, so a new bit there cannot be sent to a server which did not ask for it.Quoted 2 lines
What do you think?
I had actually noticed this with recent test additions in libfuse and had to add workarounds (on the day exactly one month ago). Unfortunately had been too busy to post the kernel patch yet.
libfuse commits
daa6cb7f6996— "test: check theKILLPRIV_V2kill-suidgid flags reach the filesystem"and
60ab8c0c35de("test: check a write throughpassthrough_hpclears suid/sgid")When testing the latter commit I had noticed the issue.
Could you try the attached patch? Right now totally untested, I can test and send it out earliest during the weekend.
Thanks,
Bernd
Signature · 3 lines
https://mitar.tnode.com/ https://noc.social/@mitar https://bsky.app/profile/mitar.bsky.social
Hi!
Not sure what the process is here, but how to get this patch further?
Mitar
Quoted 110 lines · Mitar
Hi!
Awesome! I looked at the patch and this is all probably above my head, but I think the patch should also include some protocol text describing the flag-only SETATTR, because this is now a new behavior, no? And probably we should also do a minor version bump so servers can tell that they can rely on this behavior and decide to request
CAP_HANDLE_KILLPRIV_V2. For example, in our case, we do not requestCAP_HANDLE_KILLPRIV_V2now because we are aware of this issue withfallocate()andcopy_file_range().Mitar
Quoted 78 lines · Bernd Schubert
Quoted 60 lines · Mitar
Hi!
While adding
FUSE_HANDLE_KILLPRIV_V2support to a go-fuse based server, I noticed that with it negotiated,fallocate()andcopy_file_range()by a caller withoutCAP_FSETIDno longer clear the setuid and setgid bits of the file they modify, and that the server has no way to learn that it should clear them itself.The kernel makes the decision, but drops it on the way.
fuse_file_fallocate()and__fuse_copy_file_range()both callfile_modified()before sending the request, which ends infuse_setattr()withATTR_KILL_SUIDandATTR_KILL_SGID. Since commit8981bdfda744("fuse: don't sendATTR_MODEto kill suid/sgid forhandle_killpriv_v2"),fuse_setattr()drops those whenfc->handle_killpriv_v2is set, expecting the server to kill the bits when the WRITE which follows arrives. But no WRITE followsFUSE_FALLOCATEorFUSE_COPY_FILE_RANGE, and neither carries such a flag. All the server sees is an empty SETATTR before the request, which it cannot tell apart from the one it gets when onlyATTR_KILL_PRIVwas asked for.For "fallocate -l 8192 f" run by an unprivileged user, on a file with mode 06777 which they may write to, the server receives:
GETXATTR "security.capability" -> ENODATA
SETATTR valid=0
FALLOCATE offset=0 length=8192 mode=0and the file keeps mode 06777. Without
FUSE_HANDLE_KILLPRIV_V2, the same command clears both bits, sincefuse_setattr()then turns the request into a SETATTR of the mode. I verified thefallocate()case. Thecopy_file_range()case is from reading the code, as the server I tested does not implementFUSE_COPY_FILE_RANGE.This matters beyond the mode looking wrong:
fallocate()withFALLOC_FL_ZERO_RANGEorFALLOC_FL_PUNCH_HOLE, andcopy_file_range()even more so, change what the file holds. So somebody who may write to a setuid file can replace its contents and keep the setuid bit, which is what killing the bits is there to prevent.It looks like an interaction between changes rather than a choice. Commit
fe0da9c09b2d("fuse:copy_file_rangeneeds to strip setuid bits and update timestamps") madecopy_file_range()kill the bits in 2019, which stopped working onFUSE_HANDLE_KILLPRIV_V2connections with8981bdfda744in 2020. Commit4a6f278d4827("fuse: addfile_modified()to fallocate") added the same tofallocate()in 2022, which takes effect only withoutFUSE_HANDLE_KILLPRIV_V2. As far as I can tell from its source, virtiofsd with --killpriv-v2 has the same gap: it dropsCAP_FSETIDfor write, open and setattr when asked to, and callsfallocate()andcopy_file_range()as they are.I think we should pass the decision with the request, as for WRITE and OPEN. For
FUSE_FALLOCATE, a newFUSE_FALLOCATE_KILL_SUIDGIDcould go into the currently unused fuse_fallocate_in.padding. ForFUSE_COPY_FILE_RANGE, it would need a new field, or a bit in fuse_copy_file_range_in.flags behind a new INIT flag: servers pass .flags on tocopy_file_range(2)as it is (virtiofsd does), which fails with EINVAL on any bit, so a new bit there cannot be sent to a server which did not ask for it.Quoted 2 lines
What do you think?
I had actually noticed this with recent test additions in libfuse and had to add workarounds (on the day exactly one month ago). Unfortunately had been too busy to post the kernel patch yet.
libfuse commits
daa6cb7f6996— "test: check theKILLPRIV_V2kill-suidgid flags reach the filesystem"and
60ab8c0c35de("test: check a write throughpassthrough_hpclears suid/sgid")Quoted 9 lines
When testing the latter commit I had noticed the issue.
Could you try the attached patch? Right now totally untested, I can test and send it out earliest during the weekend.
Thanks,
Bernd--
https://mitar.tnode.com/
https://noc.social/@mitar
https://bsky.app/profile/mitar.bsky.social
Signature · 3 lines
https://mitar.tnode.com/ https://noc.social/@mitar https://bsky.app/profile/mitar.bsky.social
Miklos Szeredi
maintainerQuoted 1 line · Mitar
Not sure what the process is here, but how to get this patch further?
Posting it as a proper patch (e.g. git send-email)
Thanks,
Miklos
Quoted 5 lines · Miklos Szeredi
Quoted 1 line · Mitar
Not sure what the process is here, but how to get this patch further?
Posting it as a proper patch (e.g. git send-email)
I will do that later (Friday earliest), I just didn't come to it yet.
Thanks,
Bernd