vfs: make immutable files actually immutable
Subsystems (13)
v6 cover vfs: make immutable files actually immutable
Hi all,
The chattr(1) manpage has this to say about the immutable bit that system administrators can set on files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
Given the clause about how the file 'cannot be modified', it is surprising that programs holding writable file descriptors can continue to write to and truncate files after the immutable flag has been set, but they cannot call other things such as utimes, fallocate, unlink, link, setxattr, or reflink.
Since the immutable flag is only settable by administrators, resolve this inconsistent behavior in favor of the documented behavior -- once the flag is set, the file cannot be modified, period. We presume that administrators must be trusted to know what they're doing, and that cutting off programs with writable fds will probably break them.
Therefore, add immutability checks to the relevant VFS functions, then refactor the SETFLAGS and FSSETXATTR implementations to use common argument checking functions so that we can then force pagefaults on all the file data when setting immutability.
Note that various distro manpages points out the inconsistent behavior of the various Linux filesystems w.r.t. immutable. This fixes all that.
I also discovered that userspace programs can write and create writable memory mappings to active swap files. This is extremely bad because this allows anyone with write privileges to corrupt system memory. The final patch in this series closes off that hole, at least for swap files.
If you're going to start using this mess, you probably ought to just pull from my git trees, which are linked below.
This has been lightly tested with fstests. Enjoy! Comments and questions are, as always, welcome.
--D
kernel git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfs-linux.git/log/?h=immutable-files
fstests git tree:
https://git.kernel.org/cgit/linux/kernel/git/djwong/xfstests-dev.git/log/?h=immutable-files
PATCH v6 1/4 mm/fs: don't allow writes to immutable files
From: Darrick J. Wong <[email protected]>
The chattr manpage has this to say about immutable files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
Once the flag is set, it is enforced for quite a few file operations, such as fallocate, fpunch, fzero, rm, touch, open, etc. However, we don't check for immutability when doing a write(), a PROT_WRITE mmap(), a truncate(), or a write to a previously established mmap.
If a program has an open write fd to a file that the administrator subsequently marks immutable, the program still can change the file contents. Weird!
The ability to write to an immutable file does not follow the manpage promise that immutable files cannot be modified. Worse yet it's inconsistent with the behavior of other syscalls which don't allow modifications of immutable files.
Therefore, add the necessary checks to make the write, mmap, and truncate behavior consistent with what the manpage says and consistent with other syscalls on filesystems which support IMMUTABLE.
--- fs/attr.c | 13 ++++++------- mm/filemap.c | 3 +++ mm/memory.c | 4 ++++ mm/mmap.c | 8 ++++++-- 4 files changed, 19 insertions(+), 9 deletions(-)
fs/attr.c +6 -7
@@ -233,19 +233,18 @@ int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **deWARN_ON_ONCE (!inode_is_locked (inode)); -if (ia_valid & (ATTR_MODE |ATTR_UID |ATTR_GID |ATTR_TIMES_SET )) { -if (IS_IMMUTABLE (inode) ||IS_APPEND (inode)) -return -EPERM ; - } +if (IS_IMMUTABLE (inode)) +return -EPERM ; + +if ((ia_valid & (ATTR_MODE |ATTR_UID |ATTR_GID |ATTR_TIMES_SET )) && +IS_APPEND (inode)) +return -EPERM ;/* * If utimes(2) and friends are called with times == NULL (or both * times are UTIME_NOW), then we need to check for write permission */ if (ia_valid &ATTR_TOUCH ) { -if (IS_IMMUTABLE (inode)) -return -EPERM ; -if (!inode_owner_or_capable (inode)) { error =inode_permission (inode,MAY_WRITE );if (error)
mm/filemap.c +3 -0
@@ -2935,6 +2935,9 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)loff_t count;int ret; +if (IS_IMMUTABLE (inode)) +return -EPERM ; +if (!iov_iter_count (from))return 0 ;
mm/memory.c +4 -0
@@ -2235,6 +2235,10 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) vmf->flags =FAULT_FLAG_WRITE |FAULT_FLAG_MKWRITE ; +if (vmf->vma->vm_file && +IS_IMMUTABLE (vmf->vma->vm_file->f_mapping->host)) +return VM_FAULT_SIGBUS ; + ret = vmf->vma->vm_ops->page_mkwrite (vmf);/* Restore original flags so that caller is not surprised */ vmf->flags = old_flags;
mm/mmap.c +6 -2
@@ -1483,8 +1483,12 @@ unsigned long do_mmap(struct file *file, unsigned long addr,case MAP_SHARED_VALIDATE :if (flags & ~flags_mask)return -EOPNOTSUPP ; -if ((prot&PROT_WRITE ) && !(file->f_mode&FMODE_WRITE )) -return -EACCES ; +if (prot &PROT_WRITE ) { +if (!(file->f_mode &FMODE_WRITE )) +return -EACCES ; +if (IS_IMMUTABLE (file->f_mapping->host)) +return -EPERM ; + }/* * Make sure we don't allow writing to an append-only
PATCH v6 2/4 vfs: flush and wait for io when setting the immutable flag via SETFLAGS
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
When we're using FS_IOC_SETFLAGS to set the immutable flag on a file, we need to ensure that userspace can't continue to write the file after the file becomes immutable. To make that happen, we have to flush all the dirty pagecache pages to disk to ensure that we can fail a page fault on a mmap'd region, wait for pending directio to complete, and hope the caller locked out any new writes by holding the inode lock.
--- fs/inode.c | 21 +++++++++++++++++++-- include/linux/fs.h | 11 +++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-)
fs/inode.c +19 -2
@@ -2193,7 +2193,8 @@ EXPORT_SYMBOL(current_time);/* * Generic function to check FS_IOC_SETFLAGS values and reject any invalid -* configurations. +* configurations. Once we're done, prepare the inode for whatever changes +* are coming down the pipeline. * * Note: the caller should be holding i_mutex, or else be sure that they have * exclusive access to the inode structure.
@@ -2201,6 +2202,8 @@ EXPORT_SYMBOL(current_time);int vfs_ioc_setflags_prepare (struct inode *inode,unsigned int oldflags,unsigned int flags) { +int ret; +/* * The IMMUTABLE and APPEND_ONLY flags can only be changed by * the relevant capability.
@@ -2211,7 +2214,21 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags, !capable(CAP_LINUX_IMMUTABLE))return -EPERM ; -return 0 ; +/* +* Now that we're done checking the new flags, flush all pending IO and +* dirty mappings before setting S_IMMUTABLE on an inode via +* FS_IOC_SETFLAGS. If the flush fails we'll clear the flag before +* returning error. +*/ +if (!S_ISREG (inode->i_mode) ||IS_IMMUTABLE (inode) || + !(flags &FS_IMMUTABLE_FL )) +return 0 ; + +inode_set_flags (inode,S_IMMUTABLE ,S_IMMUTABLE ); + ret =inode_drain_writes (inode); +if (ret) +inode_set_flags (inode,0 ,S_IMMUTABLE ); +return ret; }EXPORT_SYMBOL (vfs_ioc_setflags_prepare);
include/linux/fs.h +11 -0
@@ -3567,4 +3567,15 @@ static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags) fa->fsx_xflags = xflags; } +/* +* Flush file data before changing attributes. Caller must hold any locks +* required to prevent further writes to this file until we're done setting +* flags. +*/ +static inline int inode_drain_writes (struct inode *inode) +{ +inode_dio_wait (inode); +return filemap_write_and_wait (inode->i_mapping); +} +#endif /* _LINUX_FS_H */
PATCH v6 3/4 vfs: flush and wait for io when setting the immutable flag via FSSETXATTR
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
When we're using FS_IOC_FSSETXATTR to set the immutable flag on a file, we need to ensure that userspace can't continue to write the file after the file becomes immutable. To make that happen, we have to flush all the dirty pagecache pages to disk to ensure that we can fail a page fault on a mmap'd region, wait for pending directio to complete, and hope the caller locked out any new writes by holding the inode lock.
XFS has more complex locking than other FSSETXATTR implementations so we have to keep the checking and preparation code in different functions.
--- fs/btrfs/ioctl.c | 2 + fs/ext4/ioctl.c | 2 + fs/f2fs/file.c | 2 + fs/inode.c | 31 +++++++++++++++++++++++ fs/xfs/xfs_ioctl.c | 71 +++++++++++++++++++++++++++++++++++++++------------- include/linux/fs.h | 3 ++ 6 files changed, 90 insertions(+), 21 deletions(-)
fs/btrfs/ioctl.c +1 -1
@@ -420,7 +420,7 @@ static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)simple_fill_fsxattr (&old_fa,btrfs_inode_flags_to_xflags (binode->flags)); - ret =vfs_ioc_fssetxattr_check (inode, &old_fa, &fa); + ret =vfs_ioc_fssetxattr_prepare (inode, &old_fa, &fa);if (ret) goto out_unlock;
fs/ext4/ioctl.c +1 -1
@@ -1109,7 +1109,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)inode_lock (inode);ext4_fill_fsxattr (inode, &old_fa); - err =vfs_ioc_fssetxattr_check (inode, &old_fa, &fa); + err =vfs_ioc_fssetxattr_prepare (inode, &old_fa, &fa);if (err) goto out; flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
fs/f2fs/file.c +1 -1
@@ -2825,7 +2825,7 @@ static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)inode_lock (inode);f2fs_fill_fsxattr (inode, &old_fa); - err =vfs_ioc_fssetxattr_check (inode, &old_fa, &fa); + err =vfs_ioc_fssetxattr_prepare (inode, &old_fa, &fa);if (err) goto out; flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
fs/inode.c +31 -0
@@ -2293,3 +2293,34 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,return 0 ; }EXPORT_SYMBOL (vfs_ioc_fssetxattr_check); + +/* +* Generic function to check FS_IOC_FSSETXATTR values and reject any invalid +* configurations. If none are found, flush all pending IO and dirty mappings +* before setting S_IMMUTABLE on an inode. If the flush fails we'll clear the +* flag before returning error. +* +* Note: the caller must hold whatever locks are necessary to block any other +* threads from starting a write to the file. +*/ +int vfs_ioc_fssetxattr_prepare (struct inode *inode, +const struct fsxattr *old_fa, +struct fsxattr *fa) +{ +int ret; + + ret =vfs_ioc_fssetxattr_check (inode, old_fa, fa); +if (ret) +return ret; + +if (!S_ISREG (inode->i_mode) ||IS_IMMUTABLE (inode) || + !(fa->fsx_xflags &FS_XFLAG_IMMUTABLE )) +return 0 ; + +inode_set_flags (inode,S_IMMUTABLE ,S_IMMUTABLE ); + ret =inode_drain_writes (inode); +if (ret) +inode_set_flags (inode,0 ,S_IMMUTABLE ); +return ret; +} +EXPORT_SYMBOL (vfs_ioc_fssetxattr_prepare);
fs/xfs/xfs_ioctl.c +53 -18
122 lines not on this page · raw message
include/linux/fs.h +3 -0
@@ -3560,6 +3560,9 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,int vfs_ioc_fssetxattr_check (struct inode *inode,const struct fsxattr *old_fa,struct fsxattr *fa); +int vfs_ioc_fssetxattr_prepare (struct inode *inode, +const struct fsxattr *old_fa, +struct fsxattr *fa);static inline void simple_fill_fsxattr (struct fsxattr *fa,__u32 xflags) {
PATCH v6 4/4 vfs: don't allow most setxattr to immutable files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
The chattr manpage has this to say about immutable files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
However, we don't actually check the immutable flag in the setattr code, which means that we can update inode flags and project ids and extent size hints on supposedly immutable files. Therefore, reject setflags and fssetxattr calls on an immutable file if the file is immutable and will remain that way.
--- fs/inode.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
fs/inode.c +27 -0
@@ -2214,6 +2214,14 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags, !capable(CAP_LINUX_IMMUTABLE))return -EPERM ; +/* +* We aren't allowed to change any other flags if the immutable flag is +* already set and is not being unset. +*/ +if ((oldflags &FS_IMMUTABLE_FL ) && (flags &FS_IMMUTABLE_FL ) && + oldflags != flags) +return -EPERM ; +/* * Now that we're done checking the new flags, flush all pending IO and * dirty mappings before setting S_IMMUTABLE on an inode via
@@ -2284,6 +2292,25 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa, !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))return -EINVAL ; +/* +* We aren't allowed to change any fields if the immutable flag is +* already set and is not being unset. +*/ +if ((old_fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && + (fa->fsx_xflags &FS_XFLAG_IMMUTABLE )) { +if (old_fa->fsx_xflags != fa->fsx_xflags) +return -EPERM ; +if (old_fa->fsx_projid != fa->fsx_projid) +return -EPERM ; +if ((fa->fsx_xflags & (FS_XFLAG_EXTSIZE | +FS_XFLAG_EXTSZINHERIT )) && + old_fa->fsx_extsize != fa->fsx_extsize) +return -EPERM ; +if ((old_fa->fsx_xflags &FS_XFLAG_COWEXTSIZE ) && + old_fa->fsx_cowextsize != fa->fsx_cowextsize) +return -EPERM ; + } +/* Extent size hints of zero turn off the flags. */ if (fa->fsx_extsize ==0 ) fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |FS_XFLAG_EXTSZINHERIT );
Re: [PATCH 4/4] vfs: don't allow most setxattr to immutable files
Quoted 54 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The chattr manpage has this to say about immutable files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
However, we don't actually check the immutable flag in the setattr code, which means that we can update inode flags and project ids and extent size hints on supposedly immutable files. Therefore, reject setflags and fssetxattr calls on an immutable file if the file is immutable and will remain that way.
Signed-off-by Darrick J. Wong <[email protected]>--- fs/inode.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)fs/inode.c +17
-0@@ -2214,6 +2214,14 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags, !capable(CAP_LINUX_IMMUTABLE))return -EPERM ; +/* +* We aren't allowed to change any other flags if the immutable flag is +* already set and is not being unset. +*/ +if ((oldflags &FS_IMMUTABLE_FL ) && (flags &FS_IMMUTABLE_FL ) && + oldflags != flags) +return -EPERM ; +/* * Now that we're done checking the new flags, flush all pending IO and * dirty mappings before setting S_IMMUTABLE on an inode via @@ -2284,6 +2292,25 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa, !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))return -EINVAL ; +/* +* We aren't allowed to change any fields if the immutable flag is +* already set and is not being unset. +*/ +if ((old_fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && + (fa->fsx_xflags &FS_XFLAG_IMMUTABLE )) { +if (old_fa->fsx_xflags != fa->fsx_xflags) +return -EPERM ; +if (old_fa->fsx_projid != fa->fsx_projid)
fs/inode.c int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa, · line 2313
Quoted 10 lines
fs/inode.c +10
-0+return -EPERM ; +if ((fa->fsx_xflags & (FS_XFLAG_EXTSIZE | +FS_XFLAG_EXTSZINHERIT )) && + old_fa->fsx_extsize != fa->fsx_extsize) +return -EPERM ; +if ((old_fa->fsx_xflags &FS_XFLAG_COWEXTSIZE ) && + old_fa->fsx_cowextsize != fa->fsx_cowextsize) +return -EPERM ; + } +
I would like to reject this for the sheer effort on my eyes, but I'll try harder to rationalize.
How about memcmp(fa, old_fa, offsetof(struct fsxattr, fsx_pad))?
Would be more robust to future struct fsxattr changes and generally more easy on the eyes.
Sure, there is the possibility of userspace passing uninitialized fsx_extsize/fsx_cowextsize without setting the flag, but is that a real concern for the very few tools that are used to chattr? Those tools, when asked to set an attribute, will first get struct fsxattr from fs, then change the requested attr and set the fsxattr struct. So IMO the chances of this causing any regression or unexpected behavior are ridiculously low.
Thanks,
Amir.
PATCH v2 4/4 vfs: don't allow most setxattr to immutable files
In reply to Darrick J. WongFrom: Darrick J. Wong <[email protected]>
The chattr manpage has this to say about immutable files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
However, we don't actually check the immutable flag in the setattr code, which means that we can update inode flags and project ids and extent size hints on supposedly immutable files. Therefore, reject setflags and fssetxattr calls on an immutable file if the file is immutable and will remain that way.
---
v2: use memcmp instead of open coding a bunch of checks
--- fs/inode.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
fs/inode.c +17 -0
@@ -2214,6 +2214,14 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags, !capable(CAP_LINUX_IMMUTABLE))return -EPERM ; +/* +* We aren't allowed to change any other flags if the immutable flag is +* already set and is not being unset. +*/ +if ((oldflags &FS_IMMUTABLE_FL ) && (flags &FS_IMMUTABLE_FL ) && + oldflags != flags) +return -EPERM ; +/* * Now that we're done checking the new flags, flush all pending IO and * dirty mappings before setting S_IMMUTABLE on an inode via
@@ -2284,6 +2292,15 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa, !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))return -EINVAL ; +/* +* We aren't allowed to change any fields if the immutable flag is +* already set and is not being unset. +*/ +if ((old_fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && + (fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && +memcmp (fa, old_fa, offsetof(struct fsxattr , fsx_pad))) +return -EPERM ; +/* Extent size hints of zero turn off the flags. */ if (fa->fsx_extsize ==0 ) fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |FS_XFLAG_EXTSZINHERIT );
Re: [PATCH v2 4/4] vfs: don't allow most setxattr to immutable files
Quoted 16 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
The chattr manpage has this to say about immutable files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
However, we don't actually check the immutable flag in the setattr code, which means that we can update inode flags and project ids and extent size hints on supposedly immutable files. Therefore, reject setflags and fssetxattr calls on an immutable file if the file is immutable and will remain that way.
Quoted 3 lines
Signed-off-by Darrick J. Wong <[email protected]>---
v2: use memcmp instead of open coding a bunch of checks
Thanks,
Quoted 39 lines
--- fs/inode.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)fs/inode.c +17
-0@@ -2214,6 +2214,14 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags, !capable(CAP_LINUX_IMMUTABLE))return -EPERM ; +/* +* We aren't allowed to change any other flags if the immutable flag is +* already set and is not being unset. +*/ +if ((oldflags &FS_IMMUTABLE_FL ) && (flags &FS_IMMUTABLE_FL ) && + oldflags != flags) +return -EPERM ; +/* * Now that we're done checking the new flags, flush all pending IO and * dirty mappings before setting S_IMMUTABLE on an inode via @@ -2284,6 +2292,15 @@ int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa, !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))return -EINVAL ; +/* +* We aren't allowed to change any fields if the immutable flag is +* already set and is not being unset. +*/ +if ((old_fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && + (fa->fsx_xflags &FS_XFLAG_IMMUTABLE ) && +memcmp (fa, old_fa, offsetof(struct fsxattr , fsx_pad))) +return -EPERM ; +/* Extent size hints of zero turn off the flags. */ if (fa->fsx_extsize ==0 ) fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |FS_XFLAG_EXTSZINHERIT );
Quoted 16 lines · Darrick J. Wong
Hi all,
The chattr(1) manpage has this to say about the immutable bit that system administrators can set on files:
"A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file, most of the file's metadata can not be modified, and the file can not be opened in write mode."
Given the clause about how the file 'cannot be modified', it is surprising that programs holding writable file descriptors can continue to write to and truncate files after the immutable flag has been set, but they cannot call other things such as utimes, fallocate, unlink, link, setxattr, or reflink.
Quoted 6 lines
Since the immutable flag is only settable by administrators, resolve this inconsistent behavior in favor of the documented behavior -- once the flag is set, the file cannot be modified, period. We presume that administrators must be trusted to know what they're doing, and that cutting off programs with writable fds will probably break them.
This effort sounds very logical to me and sound. But are we allowed to do it? IE: Is it not breaking ABI. I do agree previous ABI was evil but are we allowed to break it?
I would not mind breaking it if %99.99 of the time the immutable bit was actually set manually by a human administrator. But what if there are automated systems that set it relying on the current behaviour?
For example I have a very distant and vague recollection of a massive camera capture system, that was DMAing directly to file (splice). And setting the immutable bit right away on start. Then once the capture is done (capture file recycled) the file becomes immutable. Such program is now broken. Who's fault is it?
I'm totally not sure and maybe you are right. But have you made a survey of the majority of immutable uses, and are positive that the guys are not broken after this change?
For me this is kind of scary. Yes I am known to be a SW coward ;-)
Thanks
Boaz