Skip to content

Commit 289e5e0

Browse files
Jiufei Xuegregkh
authored andcommitted
ovl: check the capability before cred overridden
commit 98487de upstream. 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 d1d04ef ("ovl: stack file ops") and dab5ca8 ("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 overridden. And here we only care about APPEND_FL and IMMUTABLE_FL, so get these information from inode. [SzM: move check and call to underlying fs inside inode locked region to prevent two such calls from racing with each other] Signed-off-by: Jiufei Xue <jiufei.xue@linux.alibaba.com> Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Cc: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e73b09c commit 289e5e0

1 file changed

Lines changed: 61 additions & 18 deletions

File tree

‎fs/overlayfs/file.c‎

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/mount.h>
1212
#include <linux/xattr.h>
1313
#include <linux/uio.h>
14+
#include <linux/uaccess.h>
1415
#include "overlayfs.h"
1516

1617
static char ovl_whatisit(struct inode *inode, struct inode *realinode)
@@ -372,34 +373,76 @@ static long ovl_real_ioctl(struct file *file, unsigned int cmd,
372373
return ret;
373374
}
374375

375-
static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376+
static unsigned int ovl_get_inode_flags(struct inode *inode)
377+
{
378+
unsigned int flags = READ_ONCE(inode->i_flags);
379+
unsigned int ovl_iflags = 0;
380+
381+
if (flags & S_SYNC)
382+
ovl_iflags |= FS_SYNC_FL;
383+
if (flags & S_APPEND)
384+
ovl_iflags |= FS_APPEND_FL;
385+
if (flags & S_IMMUTABLE)
386+
ovl_iflags |= FS_IMMUTABLE_FL;
387+
if (flags & S_NOATIME)
388+
ovl_iflags |= FS_NOATIME_FL;
389+
390+
return ovl_iflags;
391+
}
392+
393+
static long ovl_ioctl_set_flags(struct file *file, unsigned long arg)
376394
{
377395
long ret;
378396
struct inode *inode = file_inode(file);
397+
unsigned int flags;
398+
unsigned int old_flags;
399+
400+
if (!inode_owner_or_capable(inode))
401+
return -EACCES;
402+
403+
if (get_user(flags, (int __user *) arg))
404+
return -EFAULT;
405+
406+
ret = mnt_want_write_file(file);
407+
if (ret)
408+
return ret;
409+
410+
inode_lock(inode);
411+
412+
/* Check the capability before cred override */
413+
ret = -EPERM;
414+
old_flags = ovl_get_inode_flags(inode);
415+
if (((flags ^ old_flags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
416+
!capable(CAP_LINUX_IMMUTABLE))
417+
goto unlock;
418+
419+
ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
420+
if (ret)
421+
goto unlock;
422+
423+
ret = ovl_real_ioctl(file, FS_IOC_SETFLAGS, arg);
424+
425+
ovl_copyflags(ovl_inode_real(inode), inode);
426+
unlock:
427+
inode_unlock(inode);
428+
429+
mnt_drop_write_file(file);
430+
431+
return ret;
432+
433+
}
434+
435+
static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
436+
{
437+
long ret;
379438

380439
switch (cmd) {
381440
case FS_IOC_GETFLAGS:
382441
ret = ovl_real_ioctl(file, cmd, arg);
383442
break;
384443

385444
case FS_IOC_SETFLAGS:
386-
if (!inode_owner_or_capable(inode))
387-
return -EACCES;
388-
389-
ret = mnt_want_write_file(file);
390-
if (ret)
391-
return ret;
392-
393-
ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
394-
if (!ret) {
395-
ret = ovl_real_ioctl(file, cmd, arg);
396-
397-
inode_lock(inode);
398-
ovl_copyflags(ovl_inode_real(inode), inode);
399-
inode_unlock(inode);
400-
}
401-
402-
mnt_drop_write_file(file);
445+
ret = ovl_ioctl_set_flags(file, arg);
403446
break;
404447

405448
default:

0 commit comments

Comments
 (0)