Skip to content

Commit 555724a

Browse files
htejungregkh
authored andcommitted
kernfs, sysfs, cgroup: restrict extra perm check on open to sysfs
The kernfs open method - kernfs_fop_open() - inherited extra permission checks from sysfs. While the vfs layer allows ignoring the read/write permissions checks if the issuer has CAP_DAC_OVERRIDE, sysfs explicitly denied open regardless of the cap if the file doesn't have any of the UGO perms of the requested access or doesn't implement the requested operation. It can be debated whether this was a good idea or not but the behavior is too subtle and dangerous to change at this point. After cgroup got converted to kernfs, this extra perm check also got applied to cgroup breaking libcgroup which opens write-only files with O_RDWR as root. This patch gates the extra open permission check with a new flag KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK and enables it for sysfs. For sysfs, nothing changes. For cgroup, root now can perform any operation regardless of the permissions as it was before kernfs conversion. Note that kernfs still fails unimplemented operations with -EINVAL. While at it, add comments explaining KERNFS_ROOT flags. Signed-off-by: Tejun Heo <tj@kernel.org> Reported-by: Andrey Wagin <avagin@gmail.com> Tested-by: Andrey Wagin <avagin@gmail.com> Cc: Li Zefan <lizefan@huawei.com> References: http://lkml.kernel.org/g/CANaxB-xUm3rJ-Cbp72q-rQJO5mZe1qK6qXsQM=vh0U8upJ44+A@mail.gmail.com Fixes: 2bd59d4 ("cgroup: convert to kernfs") Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d1db0ee commit 555724a

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎fs/kernfs/file.c‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ static void kernfs_put_open_node(struct kernfs_node *kn,
610610
static int kernfs_fop_open(struct inode *inode, struct file *file)
611611
{
612612
struct kernfs_node *kn = file->f_path.dentry->d_fsdata;
613+
struct kernfs_root *root = kernfs_root(kn);
613614
const struct kernfs_ops *ops;
614615
struct kernfs_open_file *of;
615616
bool has_read, has_write, has_mmap;
@@ -624,14 +625,16 @@ static int kernfs_fop_open(struct inode *inode, struct file *file)
624625
has_write = ops->write || ops->mmap;
625626
has_mmap = ops->mmap;
626627

627-
/* check perms and supported operations */
628-
if ((file->f_mode & FMODE_WRITE) &&
629-
(!(inode->i_mode & S_IWUGO) || !has_write))
630-
goto err_out;
628+
/* see the flag definition for details */
629+
if (root->flags & KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK) {
630+
if ((file->f_mode & FMODE_WRITE) &&
631+
(!(inode->i_mode & S_IWUGO) || !has_write))
632+
goto err_out;
631633

632-
if ((file->f_mode & FMODE_READ) &&
633-
(!(inode->i_mode & S_IRUGO) || !has_read))
634-
goto err_out;
634+
if ((file->f_mode & FMODE_READ) &&
635+
(!(inode->i_mode & S_IRUGO) || !has_read))
636+
goto err_out;
637+
}
635638

636639
/* allocate a kernfs_open_file for the file */
637640
error = -ENOMEM;

‎fs/sysfs/mount.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ int __init sysfs_init(void)
6363
{
6464
int err;
6565

66-
sysfs_root = kernfs_create_root(NULL, 0, NULL);
66+
sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
67+
NULL);
6768
if (IS_ERR(sysfs_root))
6869
return PTR_ERR(sysfs_root);
6970

‎include/linux/kernfs.h‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,24 @@ enum kernfs_node_flag {
5050

5151
/* @flags for kernfs_create_root() */
5252
enum kernfs_root_flag {
53-
KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
53+
/*
54+
* kernfs_nodes are created in the deactivated state and invisible.
55+
* They require explicit kernfs_activate() to become visible. This
56+
* can be used to make related nodes become visible atomically
57+
* after all nodes are created successfully.
58+
*/
59+
KERNFS_ROOT_CREATE_DEACTIVATED = 0x0001,
60+
61+
/*
62+
* For regular flies, if the opener has CAP_DAC_OVERRIDE, open(2)
63+
* succeeds regardless of the RW permissions. sysfs had an extra
64+
* layer of enforcement where open(2) fails with -EACCES regardless
65+
* of CAP_DAC_OVERRIDE if the permission doesn't have the
66+
* respective read or write access at all (none of S_IRUGO or
67+
* S_IWUGO) or the respective operation isn't implemented. The
68+
* following flag enables that behavior.
69+
*/
70+
KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
5471
};
5572

5673
/* type-specific structures for kernfs_node union members */

0 commit comments

Comments
 (0)