0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/fs.h>
0013 #include <linux/magic.h>
0014 #include <linux/mount.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/user_namespace.h>
0018 #include <linux/fs_context.h>
0019 #include <net/net_namespace.h>
0020
0021 #include "sysfs.h"
0022
0023 static struct kernfs_root *sysfs_root;
0024 struct kernfs_node *sysfs_root_kn;
0025
0026 static int sysfs_get_tree(struct fs_context *fc)
0027 {
0028 struct kernfs_fs_context *kfc = fc->fs_private;
0029 int ret;
0030
0031 ret = kernfs_get_tree(fc);
0032 if (ret)
0033 return ret;
0034
0035 if (kfc->new_sb_created)
0036 fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
0037 return 0;
0038 }
0039
0040 static void sysfs_fs_context_free(struct fs_context *fc)
0041 {
0042 struct kernfs_fs_context *kfc = fc->fs_private;
0043
0044 if (kfc->ns_tag)
0045 kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
0046 kernfs_free_fs_context(fc);
0047 kfree(kfc);
0048 }
0049
0050 static const struct fs_context_operations sysfs_fs_context_ops = {
0051 .free = sysfs_fs_context_free,
0052 .get_tree = sysfs_get_tree,
0053 };
0054
0055 static int sysfs_init_fs_context(struct fs_context *fc)
0056 {
0057 struct kernfs_fs_context *kfc;
0058 struct net *netns;
0059
0060 if (!(fc->sb_flags & SB_KERNMOUNT)) {
0061 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
0062 return -EPERM;
0063 }
0064
0065 kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
0066 if (!kfc)
0067 return -ENOMEM;
0068
0069 kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
0070 kfc->root = sysfs_root;
0071 kfc->magic = SYSFS_MAGIC;
0072 fc->fs_private = kfc;
0073 fc->ops = &sysfs_fs_context_ops;
0074 if (netns) {
0075 put_user_ns(fc->user_ns);
0076 fc->user_ns = get_user_ns(netns->user_ns);
0077 }
0078 fc->global = true;
0079 return 0;
0080 }
0081
0082 static void sysfs_kill_sb(struct super_block *sb)
0083 {
0084 void *ns = (void *)kernfs_super_ns(sb);
0085
0086 kernfs_kill_sb(sb);
0087 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
0088 }
0089
0090 static struct file_system_type sysfs_fs_type = {
0091 .name = "sysfs",
0092 .init_fs_context = sysfs_init_fs_context,
0093 .kill_sb = sysfs_kill_sb,
0094 .fs_flags = FS_USERNS_MOUNT,
0095 };
0096
0097 int __init sysfs_init(void)
0098 {
0099 int err;
0100
0101 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
0102 NULL);
0103 if (IS_ERR(sysfs_root))
0104 return PTR_ERR(sysfs_root);
0105
0106 sysfs_root_kn = kernfs_root_to_node(sysfs_root);
0107
0108 err = register_filesystem(&sysfs_fs_type);
0109 if (err) {
0110 kernfs_destroy_root(sysfs_root);
0111 return err;
0112 }
0113
0114 return 0;
0115 }