]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/sysfs/mount.c
kernfs, sysfs, cgroup: restrict extra perm check on open to sysfs
[mirror_ubuntu-bionic-kernel.git] / fs / sysfs / mount.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
11 */
12
6b8fbde4 13#define DEBUG
1da177e4
LT
14
15#include <linux/fs.h>
16#include <linux/mount.h>
1da177e4 17#include <linux/init.h>
87a8ebd6 18#include <linux/user_namespace.h>
1da177e4
LT
19
20#include "sysfs.h"
21
ba7443bc 22static struct kernfs_root *sysfs_root;
324a56e1 23struct kernfs_node *sysfs_root_kn;
061447a4 24
d0e46f88
AV
25static struct dentry *sysfs_mount(struct file_system_type *fs_type,
26 int flags, const char *dev_name, void *data)
1da177e4 27{
4b93dc9b
TH
28 struct dentry *root;
29 void *ns;
fed95bab 30 bool new_sb;
9e7fdd25 31
7dc5dbc8
EB
32 if (!(flags & MS_KERNMOUNT)) {
33 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
34 return ERR_PTR(-EPERM);
35
c84a3b27
TH
36 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
37 return ERR_PTR(-EPERM);
7dc5dbc8 38 }
87a8ebd6 39
4b93dc9b 40 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
fed95bab
LZ
41 root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
42 if (IS_ERR(root) || !new_sb)
4b93dc9b
TH
43 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
44 return root;
45}
46
9e7fdd25 47static void sysfs_kill_sb(struct super_block *sb)
4b93dc9b 48{
a7560a01
TH
49 void *ns = (void *)kernfs_super_ns(sb);
50
4b93dc9b 51 kernfs_kill_sb(sb);
a7560a01 52 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
4b93dc9b
TH
53}
54
1da177e4
LT
55static struct file_system_type sysfs_fs_type = {
56 .name = "sysfs",
d0e46f88 57 .mount = sysfs_mount,
9e7fdd25 58 .kill_sb = sysfs_kill_sb,
4f326c00 59 .fs_flags = FS_USERNS_MOUNT,
1da177e4
LT
60};
61
4b93dc9b
TH
62int __init sysfs_init(void)
63{
64 int err;
e0bf68dd 65
555724a8
TH
66 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
67 NULL);
4b93dc9b
TH
68 if (IS_ERR(sysfs_root))
69 return PTR_ERR(sysfs_root);
70
324a56e1 71 sysfs_root_kn = sysfs_root->kn;
ba7443bc 72
1da177e4 73 err = register_filesystem(&sysfs_fs_type);
4b93dc9b
TH
74 if (err) {
75 kernfs_destroy_root(sysfs_root);
76 return err;
77 }
9e30cc95
TH
78
79 return 0;
1da177e4 80}