Loading...
1/*
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.
11 */
12
13#define DEBUG
14
15#include <linux/fs.h>
16#include <linux/magic.h>
17#include <linux/mount.h>
18#include <linux/init.h>
19#include <linux/user_namespace.h>
20
21#include "sysfs.h"
22
23static struct kernfs_root *sysfs_root;
24struct kernfs_node *sysfs_root_kn;
25
26static struct dentry *sysfs_mount(struct file_system_type *fs_type,
27 int flags, const char *dev_name, void *data)
28{
29 struct dentry *root;
30 void *ns;
31 bool new_sb;
32
33 if (!(flags & MS_KERNMOUNT)) {
34 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
35 return ERR_PTR(-EPERM);
36
37 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
38 return ERR_PTR(-EPERM);
39 }
40
41 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
42 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
43 SYSFS_MAGIC, &new_sb, ns);
44 if (IS_ERR(root) || !new_sb)
45 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
46 return root;
47}
48
49static void sysfs_kill_sb(struct super_block *sb)
50{
51 void *ns = (void *)kernfs_super_ns(sb);
52
53 kernfs_kill_sb(sb);
54 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
55}
56
57static struct file_system_type sysfs_fs_type = {
58 .name = "sysfs",
59 .mount = sysfs_mount,
60 .kill_sb = sysfs_kill_sb,
61 .fs_flags = FS_USERNS_MOUNT,
62};
63
64int __init sysfs_init(void)
65{
66 int err;
67
68 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
69 NULL);
70 if (IS_ERR(sysfs_root))
71 return PTR_ERR(sysfs_root);
72
73 sysfs_root_kn = sysfs_root->kn;
74
75 err = register_filesystem(&sysfs_fs_type);
76 if (err) {
77 kernfs_destroy_root(sysfs_root);
78 return err;
79 }
80
81 return 0;
82}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
4 *
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 *
9 * Please see Documentation/filesystems/sysfs.txt for more information.
10 */
11
12#include <linux/fs.h>
13#include <linux/magic.h>
14#include <linux/mount.h>
15#include <linux/init.h>
16#include <linux/user_namespace.h>
17
18#include "sysfs.h"
19
20static struct kernfs_root *sysfs_root;
21struct kernfs_node *sysfs_root_kn;
22
23static struct dentry *sysfs_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *data)
25{
26 struct dentry *root;
27 void *ns;
28 bool new_sb = false;
29
30 if (!(flags & SB_KERNMOUNT)) {
31 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
32 return ERR_PTR(-EPERM);
33 }
34
35 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
36 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
37 SYSFS_MAGIC, &new_sb, ns);
38 if (!new_sb)
39 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
40 else if (!IS_ERR(root))
41 root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
42
43 return root;
44}
45
46static void sysfs_kill_sb(struct super_block *sb)
47{
48 void *ns = (void *)kernfs_super_ns(sb);
49
50 kernfs_kill_sb(sb);
51 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
52}
53
54static struct file_system_type sysfs_fs_type = {
55 .name = "sysfs",
56 .mount = sysfs_mount,
57 .kill_sb = sysfs_kill_sb,
58 .fs_flags = FS_USERNS_MOUNT,
59};
60
61int __init sysfs_init(void)
62{
63 int err;
64
65 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
66 NULL);
67 if (IS_ERR(sysfs_root))
68 return PTR_ERR(sysfs_root);
69
70 sysfs_root_kn = sysfs_root->kn;
71
72 err = register_filesystem(&sysfs_fs_type);
73 if (err) {
74 kernfs_destroy_root(sysfs_root);
75 return err;
76 }
77
78 return 0;
79}