overlayfs: check the capability before cred overridden
PATCH v2 1/1 overlayfs: check the capability before cred overridden
We found that it return success when we set IMMUTABLE_FL flag to a file in docker even though the docker didn't have the capability CAP_LINUX_IMMUTABLE.
The commit d1d04ef8572b ("ovl: stack file ops") and dab5ca8fd9dd ("ovl: add lsattr/chattr support") implemented chattr operations on a regular overlay file. ovl_real_ioctl() overridden the current process's subjective credentials with ofs->creator_cred which have the capability CAP_LINUX_IMMUTABLE so that it will return success in vfs_ioctl()->cap_capable().
Fix this by checking the capability before cred overriden. And here we only care about APPEND_FL and IMMUTABLE_FL, so get these information from inode.
Changes since v1:
- remove S_DIRSYNC since ovl_copyflags() does not copy FS_DIRSYNC_FL, pointed out by Amir Goldstein.
--- fs/overlayfs/file.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
fs/overlayfs/file.c +28 -0
@@ -11,6 +11,7 @@#include <linux/mount.h> #include <linux/xattr.h> #include <linux/uio.h> +#include <linux/uaccess.h> #include "overlayfs.h" static char ovl_whatisit (struct inode *inode,struct inode *realinode)
@@ -372,10 +373,28 @@ static long ovl_real_ioctl(struct file *file, unsigned int cmd,return ret; } +static unsigned int ovl_get_inode_flags (struct inode *inode) +{ +unsigned int flags = inode->i_flags; +unsigned int ovl_iflags =0 ; + +if (flags &S_SYNC ) + ovl_iflags |=FS_SYNC_FL ; +if (flags &S_APPEND ) + ovl_iflags |=FS_APPEND_FL ; +if (flags &S_IMMUTABLE ) + ovl_iflags |=FS_IMMUTABLE_FL ; +if (flags &S_NOATIME ) + ovl_iflags |=FS_NOATIME_FL ; + +return ovl_iflags; +} +static long ovl_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {long ret;struct inode *inode =file_inode (file); +unsigned int flags;switch (cmd) {case FS_IOC_GETFLAGS :
@@ -386,6 +405,15 @@ static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)if (!inode_owner_or_capable (inode))return -EACCES ; +if (get_user (flags, (int __user *) arg)) +return -EFAULT ; + +/* Check the capability before cred overridden */ +if ((flags ^ovl_get_inode_flags (inode)) & (FS_APPEND_FL |FS_IMMUTABLE_FL )) { +if (!capable (CAP_LINUX_IMMUTABLE )) +return -EPERM ; + } + ret =mnt_want_write_file (file);if (ret)return ret;
Signature · 1 line
2.19.1.856.g8858448bb
Miklos Szeredi
maintainerQuoted 16 lines · Jiufei Xue
We found that it return success when we set
IMMUTABLE_FLflag to a file in docker even though the docker didn't have the capabilityCAP_LINUX_IMMUTABLE.The commit
d1d04ef8572b("ovl: stack file ops") anddab5ca8fd9dd("ovl: add lsattr/chattr support") implemented chattr operations on a regular overlay file.ovl_real_ioctl()overridden the current process's subjective credentials withofs->creator_credwhich have the capabilityCAP_LINUX_IMMUTABLEso that it will return success in vfs_ioctl()->cap_capable().Fix this by checking the capability before cred overriden. And here we only care about
APPEND_FLandIMMUTABLE_FL, so get these information from inode.
Quoted 5 lines
Changes since v1:
- removeS_DIRSYNCsinceovl_copyflags()does not copyFS_DIRSYNC_FL, pointed out by Amir Goldstein.Signed-off-by Jiufei Xue <[email protected]>
Thanks. Applied with modification, please see my vfs.git#overlayfs-next tree.
Miklos