Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Bin Wang | 6b8fbde | 2012-12-06 17:08:56 +0800 | [diff] [blame] | 13 | #define DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/fs.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 16 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 19 | #include <linux/user_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "sysfs.h" |
| 22 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 23 | static struct kernfs_root *sysfs_root; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 24 | struct kernfs_node *sysfs_root_kn; |
Tejun Heo | 061447a | 2013-11-28 14:54:39 -0500 | [diff] [blame] | 25 | |
Al Viro | d0e46f8 | 2010-07-26 13:30:36 +0400 | [diff] [blame] | 26 | static struct dentry *sysfs_mount(struct file_system_type *fs_type, |
| 27 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 29 | struct dentry *root; |
| 30 | void *ns; |
Li Zefan | fed95ba | 2014-02-25 19:28:44 +0800 | [diff] [blame] | 31 | bool new_sb; |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 32 | |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 33 | if (!(flags & MS_KERNMOUNT)) { |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 34 | if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET)) |
| 35 | return ERR_PTR(-EPERM); |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 36 | } |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 37 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 38 | ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 39 | root = kernfs_mount_ns(fs_type, flags, sysfs_root, |
| 40 | SYSFS_MAGIC, &new_sb, ns); |
Li Zefan | fed95ba | 2014-02-25 19:28:44 +0800 | [diff] [blame] | 41 | if (IS_ERR(root) || !new_sb) |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 42 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
Eric W. Biederman | 90f8572 | 2015-06-29 14:42:03 -0500 | [diff] [blame] | 43 | else if (new_sb) |
| 44 | /* Userspace would break if executables appear on sysfs */ |
| 45 | root->d_sb->s_iflags |= SB_I_NOEXEC; |
| 46 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 47 | return root; |
| 48 | } |
| 49 | |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 50 | static void sysfs_kill_sb(struct super_block *sb) |
| 51 | { |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 52 | void *ns = (void *)kernfs_super_ns(sb); |
| 53 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 54 | kernfs_kill_sb(sb); |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 55 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | static struct file_system_type sysfs_fs_type = { |
| 59 | .name = "sysfs", |
Al Viro | d0e46f8 | 2010-07-26 13:30:36 +0400 | [diff] [blame] | 60 | .mount = sysfs_mount, |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 61 | .kill_sb = sysfs_kill_sb, |
Eric W. Biederman | 1b852bc | 2015-05-08 23:22:29 -0500 | [diff] [blame] | 62 | .fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | int __init sysfs_init(void) |
| 66 | { |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 67 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Tejun Heo | 555724a | 2014-05-12 13:56:27 -0400 | [diff] [blame] | 69 | sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, |
| 70 | NULL); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 71 | if (IS_ERR(sysfs_root)) |
| 72 | return PTR_ERR(sysfs_root); |
| 73 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 74 | sysfs_root_kn = sysfs_root->kn; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | err = register_filesystem(&sysfs_fs_type); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 77 | if (err) { |
| 78 | kernfs_destroy_root(sysfs_root); |
| 79 | return err; |
| 80 | } |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 81 | |
| 82 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |