Greg Kroah-Hartman | 619daee | 2018-01-22 16:18:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 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 | * |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 9 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/fs.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 13 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 16 | #include <linux/slab.h> |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 17 | #include <linux/user_namespace.h> |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 18 | #include <linux/fs_context.h> |
| 19 | #include <net/net_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 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 26 | static int sysfs_get_tree(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 28 | struct kernfs_fs_context *kfc = fc->fs_private; |
| 29 | int ret; |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 30 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 31 | ret = kernfs_get_tree(fc); |
| 32 | if (ret) |
| 33 | return ret; |
| 34 | |
| 35 | if (kfc->new_sb_created) |
| 36 | fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE; |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static void sysfs_fs_context_free(struct fs_context *fc) |
| 41 | { |
| 42 | struct kernfs_fs_context *kfc = fc->fs_private; |
| 43 | |
| 44 | if (kfc->ns_tag) |
| 45 | kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag); |
| 46 | kernfs_free_fs_context(fc); |
| 47 | kfree(kfc); |
| 48 | } |
| 49 | |
| 50 | static const struct fs_context_operations sysfs_fs_context_ops = { |
| 51 | .free = sysfs_fs_context_free, |
| 52 | .get_tree = sysfs_get_tree, |
| 53 | }; |
| 54 | |
| 55 | static int sysfs_init_fs_context(struct fs_context *fc) |
| 56 | { |
| 57 | struct kernfs_fs_context *kfc; |
| 58 | struct net *netns; |
| 59 | |
| 60 | if (!(fc->sb_flags & SB_KERNMOUNT)) { |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 61 | if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET)) |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 62 | return -EPERM; |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 63 | } |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 64 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 65 | kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL); |
| 66 | if (!kfc) |
| 67 | return -ENOMEM; |
Eric W. Biederman | 90f8572 | 2015-06-29 14:42:03 -0500 | [diff] [blame] | 68 | |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 69 | kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); |
| 70 | kfc->root = sysfs_root; |
| 71 | kfc->magic = SYSFS_MAGIC; |
| 72 | fc->fs_private = kfc; |
| 73 | fc->ops = &sysfs_fs_context_ops; |
Al Viro | ab81dab | 2019-03-16 09:45:42 -0400 | [diff] [blame] | 74 | if (netns) { |
| 75 | if (fc->user_ns) |
| 76 | put_user_ns(fc->user_ns); |
| 77 | fc->user_ns = get_user_ns(netns->user_ns); |
| 78 | } |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 79 | fc->global = true; |
| 80 | return 0; |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 83 | static void sysfs_kill_sb(struct super_block *sb) |
| 84 | { |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 85 | void *ns = (void *)kernfs_super_ns(sb); |
| 86 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 87 | kernfs_kill_sb(sb); |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 88 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static struct file_system_type sysfs_fs_type = { |
David Howells | 23bf1b6 | 2018-11-01 23:07:26 +0000 | [diff] [blame] | 92 | .name = "sysfs", |
| 93 | .init_fs_context = sysfs_init_fs_context, |
| 94 | .kill_sb = sysfs_kill_sb, |
| 95 | .fs_flags = FS_USERNS_MOUNT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | int __init sysfs_init(void) |
| 99 | { |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 100 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Tejun Heo | 555724a | 2014-05-12 13:56:27 -0400 | [diff] [blame] | 102 | sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, |
| 103 | NULL); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 104 | if (IS_ERR(sysfs_root)) |
| 105 | return PTR_ERR(sysfs_root); |
| 106 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 107 | sysfs_root_kn = sysfs_root->kn; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | err = register_filesystem(&sysfs_fs_type); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 110 | if (err) { |
| 111 | kernfs_destroy_root(sysfs_root); |
| 112 | return err; |
| 113 | } |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 114 | |
| 115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |