blob: f3db82071cfbd5997bdb1393097e755ae730ea96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Bin Wang6b8fbde2012-12-06 17:08:56 +080013#define DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/fs.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080016#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070019#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
22
Tejun Heoba7443b2013-11-28 14:54:40 -050023static struct kernfs_root *sysfs_root;
Tejun Heo324a56e2013-12-11 14:11:53 -050024struct kernfs_node *sysfs_root_kn;
Tejun Heo061447a2013-11-28 14:54:39 -050025
Al Virod0e46f82010-07-26 13:30:36 +040026static struct dentry *sysfs_mount(struct file_system_type *fs_type,
27 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Tejun Heo4b93dc92013-11-28 14:54:43 -050029 struct dentry *root;
30 void *ns;
Li Zefanfed95ba2014-02-25 19:28:44 +080031 bool new_sb;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070032
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070033 if (!(flags & MS_KERNMOUNT)) {
Tejun Heoc84a3b22013-11-23 18:01:46 -050034 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
35 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070036 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070037
Tejun Heo4b93dc92013-11-28 14:54:43 -050038 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
Jianyu Zhanc9482a52014-04-26 15:40:28 +080039 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
40 SYSFS_MAGIC, &new_sb, ns);
Li Zefanfed95ba2014-02-25 19:28:44 +080041 if (IS_ERR(root) || !new_sb)
Tejun Heo4b93dc92013-11-28 14:54:43 -050042 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Eric W. Biederman90f85722015-06-29 14:42:03 -050043 else if (new_sb)
44 /* Userspace would break if executables appear on sysfs */
45 root->d_sb->s_iflags |= SB_I_NOEXEC;
46
Tejun Heo4b93dc92013-11-28 14:54:43 -050047 return root;
48}
49
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070050static void sysfs_kill_sb(struct super_block *sb)
51{
Tejun Heoa7560a02013-12-10 10:22:30 -050052 void *ns = (void *)kernfs_super_ns(sb);
53
Tejun Heo4b93dc92013-11-28 14:54:43 -050054 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050055 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050056}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct file_system_type sysfs_fs_type = {
59 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +040060 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070061 .kill_sb = sysfs_kill_sb,
Eric W. Biederman1b852bc2015-05-08 23:22:29 -050062 .fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65int __init sysfs_init(void)
66{
Tejun Heo9e30cc92013-11-28 14:54:38 -050067 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Tejun Heo555724a2014-05-12 13:56:27 -040069 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
70 NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -050071 if (IS_ERR(sysfs_root))
72 return PTR_ERR(sysfs_root);
73
Tejun Heo324a56e2013-12-11 14:11:53 -050074 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -050075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -050077 if (err) {
78 kernfs_destroy_root(sysfs_root);
79 return err;
80 }
Tejun Heo9e30cc92013-11-28 14:54:38 -050081
82 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}