Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/proc/root.c
0004  *
0005  *  Copyright (C) 1991, 1992 Linus Torvalds
0006  *
0007  *  proc root directory handling functions
0008  */
0009 #include <linux/errno.h>
0010 #include <linux/time.h>
0011 #include <linux/proc_fs.h>
0012 #include <linux/stat.h>
0013 #include <linux/init.h>
0014 #include <linux/sched.h>
0015 #include <linux/sched/stat.h>
0016 #include <linux/module.h>
0017 #include <linux/bitops.h>
0018 #include <linux/user_namespace.h>
0019 #include <linux/fs_context.h>
0020 #include <linux/mount.h>
0021 #include <linux/pid_namespace.h>
0022 #include <linux/fs_parser.h>
0023 #include <linux/cred.h>
0024 #include <linux/magic.h>
0025 #include <linux/slab.h>
0026 
0027 #include "internal.h"
0028 
0029 struct proc_fs_context {
0030     struct pid_namespace    *pid_ns;
0031     unsigned int        mask;
0032     enum proc_hidepid   hidepid;
0033     int         gid;
0034     enum proc_pidonly   pidonly;
0035 };
0036 
0037 enum proc_param {
0038     Opt_gid,
0039     Opt_hidepid,
0040     Opt_subset,
0041 };
0042 
0043 static const struct fs_parameter_spec proc_fs_parameters[] = {
0044     fsparam_u32("gid",  Opt_gid),
0045     fsparam_string("hidepid",   Opt_hidepid),
0046     fsparam_string("subset",    Opt_subset),
0047     {}
0048 };
0049 
0050 static inline int valid_hidepid(unsigned int value)
0051 {
0052     return (value == HIDEPID_OFF ||
0053         value == HIDEPID_NO_ACCESS ||
0054         value == HIDEPID_INVISIBLE ||
0055         value == HIDEPID_NOT_PTRACEABLE);
0056 }
0057 
0058 static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param)
0059 {
0060     struct proc_fs_context *ctx = fc->fs_private;
0061     struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid);
0062     struct fs_parse_result result;
0063     int base = (unsigned long)hidepid_u32_spec.data;
0064 
0065     if (param->type != fs_value_is_string)
0066         return invalf(fc, "proc: unexpected type of hidepid value\n");
0067 
0068     if (!kstrtouint(param->string, base, &result.uint_32)) {
0069         if (!valid_hidepid(result.uint_32))
0070             return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
0071         ctx->hidepid = result.uint_32;
0072         return 0;
0073     }
0074 
0075     if (!strcmp(param->string, "off"))
0076         ctx->hidepid = HIDEPID_OFF;
0077     else if (!strcmp(param->string, "noaccess"))
0078         ctx->hidepid = HIDEPID_NO_ACCESS;
0079     else if (!strcmp(param->string, "invisible"))
0080         ctx->hidepid = HIDEPID_INVISIBLE;
0081     else if (!strcmp(param->string, "ptraceable"))
0082         ctx->hidepid = HIDEPID_NOT_PTRACEABLE;
0083     else
0084         return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
0085 
0086     return 0;
0087 }
0088 
0089 static int proc_parse_subset_param(struct fs_context *fc, char *value)
0090 {
0091     struct proc_fs_context *ctx = fc->fs_private;
0092 
0093     while (value) {
0094         char *ptr = strchr(value, ',');
0095 
0096         if (ptr != NULL)
0097             *ptr++ = '\0';
0098 
0099         if (*value != '\0') {
0100             if (!strcmp(value, "pid")) {
0101                 ctx->pidonly = PROC_PIDONLY_ON;
0102             } else {
0103                 return invalf(fc, "proc: unsupported subset option - %s\n", value);
0104             }
0105         }
0106         value = ptr;
0107     }
0108 
0109     return 0;
0110 }
0111 
0112 static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
0113 {
0114     struct proc_fs_context *ctx = fc->fs_private;
0115     struct fs_parse_result result;
0116     int opt;
0117 
0118     opt = fs_parse(fc, proc_fs_parameters, param, &result);
0119     if (opt < 0)
0120         return opt;
0121 
0122     switch (opt) {
0123     case Opt_gid:
0124         ctx->gid = result.uint_32;
0125         break;
0126 
0127     case Opt_hidepid:
0128         if (proc_parse_hidepid_param(fc, param))
0129             return -EINVAL;
0130         break;
0131 
0132     case Opt_subset:
0133         if (proc_parse_subset_param(fc, param->string) < 0)
0134             return -EINVAL;
0135         break;
0136 
0137     default:
0138         return -EINVAL;
0139     }
0140 
0141     ctx->mask |= 1 << opt;
0142     return 0;
0143 }
0144 
0145 static void proc_apply_options(struct proc_fs_info *fs_info,
0146                    struct fs_context *fc,
0147                    struct user_namespace *user_ns)
0148 {
0149     struct proc_fs_context *ctx = fc->fs_private;
0150 
0151     if (ctx->mask & (1 << Opt_gid))
0152         fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
0153     if (ctx->mask & (1 << Opt_hidepid))
0154         fs_info->hide_pid = ctx->hidepid;
0155     if (ctx->mask & (1 << Opt_subset))
0156         fs_info->pidonly = ctx->pidonly;
0157 }
0158 
0159 static int proc_fill_super(struct super_block *s, struct fs_context *fc)
0160 {
0161     struct proc_fs_context *ctx = fc->fs_private;
0162     struct inode *root_inode;
0163     struct proc_fs_info *fs_info;
0164     int ret;
0165 
0166     fs_info = kzalloc(sizeof(*fs_info), GFP_KERNEL);
0167     if (!fs_info)
0168         return -ENOMEM;
0169 
0170     fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
0171     proc_apply_options(fs_info, fc, current_user_ns());
0172 
0173     /* User space would break if executables or devices appear on proc */
0174     s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
0175     s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
0176     s->s_blocksize = 1024;
0177     s->s_blocksize_bits = 10;
0178     s->s_magic = PROC_SUPER_MAGIC;
0179     s->s_op = &proc_sops;
0180     s->s_time_gran = 1;
0181     s->s_fs_info = fs_info;
0182 
0183     /*
0184      * procfs isn't actually a stacking filesystem; however, there is
0185      * too much magic going on inside it to permit stacking things on
0186      * top of it
0187      */
0188     s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
0189 
0190     /* procfs dentries and inodes don't require IO to create */
0191     s->s_shrink.seeks = 0;
0192 
0193     pde_get(&proc_root);
0194     root_inode = proc_get_inode(s, &proc_root);
0195     if (!root_inode) {
0196         pr_err("proc_fill_super: get root inode failed\n");
0197         return -ENOMEM;
0198     }
0199 
0200     s->s_root = d_make_root(root_inode);
0201     if (!s->s_root) {
0202         pr_err("proc_fill_super: allocate dentry failed\n");
0203         return -ENOMEM;
0204     }
0205 
0206     ret = proc_setup_self(s);
0207     if (ret) {
0208         return ret;
0209     }
0210     return proc_setup_thread_self(s);
0211 }
0212 
0213 static int proc_reconfigure(struct fs_context *fc)
0214 {
0215     struct super_block *sb = fc->root->d_sb;
0216     struct proc_fs_info *fs_info = proc_sb_info(sb);
0217 
0218     sync_filesystem(sb);
0219 
0220     proc_apply_options(fs_info, fc, current_user_ns());
0221     return 0;
0222 }
0223 
0224 static int proc_get_tree(struct fs_context *fc)
0225 {
0226     return get_tree_nodev(fc, proc_fill_super);
0227 }
0228 
0229 static void proc_fs_context_free(struct fs_context *fc)
0230 {
0231     struct proc_fs_context *ctx = fc->fs_private;
0232 
0233     put_pid_ns(ctx->pid_ns);
0234     kfree(ctx);
0235 }
0236 
0237 static const struct fs_context_operations proc_fs_context_ops = {
0238     .free       = proc_fs_context_free,
0239     .parse_param    = proc_parse_param,
0240     .get_tree   = proc_get_tree,
0241     .reconfigure    = proc_reconfigure,
0242 };
0243 
0244 static int proc_init_fs_context(struct fs_context *fc)
0245 {
0246     struct proc_fs_context *ctx;
0247 
0248     ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL);
0249     if (!ctx)
0250         return -ENOMEM;
0251 
0252     ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
0253     put_user_ns(fc->user_ns);
0254     fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
0255     fc->fs_private = ctx;
0256     fc->ops = &proc_fs_context_ops;
0257     return 0;
0258 }
0259 
0260 static void proc_kill_sb(struct super_block *sb)
0261 {
0262     struct proc_fs_info *fs_info = proc_sb_info(sb);
0263 
0264     if (!fs_info) {
0265         kill_anon_super(sb);
0266         return;
0267     }
0268 
0269     dput(fs_info->proc_self);
0270     dput(fs_info->proc_thread_self);
0271 
0272     kill_anon_super(sb);
0273     put_pid_ns(fs_info->pid_ns);
0274     kfree(fs_info);
0275 }
0276 
0277 static struct file_system_type proc_fs_type = {
0278     .name           = "proc",
0279     .init_fs_context    = proc_init_fs_context,
0280     .parameters     = proc_fs_parameters,
0281     .kill_sb        = proc_kill_sb,
0282     .fs_flags       = FS_USERNS_MOUNT | FS_DISALLOW_NOTIFY_PERM,
0283 };
0284 
0285 void __init proc_root_init(void)
0286 {
0287     proc_init_kmemcache();
0288     set_proc_pid_nlink();
0289     proc_self_init();
0290     proc_thread_self_init();
0291     proc_symlink("mounts", NULL, "self/mounts");
0292 
0293     proc_net_init();
0294     proc_mkdir("fs", NULL);
0295     proc_mkdir("driver", NULL);
0296     proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
0297 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
0298     /* just give it a mountpoint */
0299     proc_create_mount_point("openprom");
0300 #endif
0301     proc_tty_init();
0302     proc_mkdir("bus", NULL);
0303     proc_sys_init();
0304 
0305     /*
0306      * Last things last. It is not like userspace processes eager
0307      * to open /proc files exist at this point but register last
0308      * anyway.
0309      */
0310     register_filesystem(&proc_fs_type);
0311 }
0312 
0313 static int proc_root_getattr(struct user_namespace *mnt_userns,
0314                  const struct path *path, struct kstat *stat,
0315                  u32 request_mask, unsigned int query_flags)
0316 {
0317     generic_fillattr(&init_user_ns, d_inode(path->dentry), stat);
0318     stat->nlink = proc_root.nlink + nr_processes();
0319     return 0;
0320 }
0321 
0322 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
0323 {
0324     if (!proc_pid_lookup(dentry, flags))
0325         return NULL;
0326 
0327     return proc_lookup(dir, dentry, flags);
0328 }
0329 
0330 static int proc_root_readdir(struct file *file, struct dir_context *ctx)
0331 {
0332     if (ctx->pos < FIRST_PROCESS_ENTRY) {
0333         int error = proc_readdir(file, ctx);
0334         if (unlikely(error <= 0))
0335             return error;
0336         ctx->pos = FIRST_PROCESS_ENTRY;
0337     }
0338 
0339     return proc_pid_readdir(file, ctx);
0340 }
0341 
0342 /*
0343  * The root /proc directory is special, as it has the
0344  * <pid> directories. Thus we don't use the generic
0345  * directory handling functions for that..
0346  */
0347 static const struct file_operations proc_root_operations = {
0348     .read        = generic_read_dir,
0349     .iterate_shared  = proc_root_readdir,
0350     .llseek     = generic_file_llseek,
0351 };
0352 
0353 /*
0354  * proc root can do almost nothing..
0355  */
0356 static const struct inode_operations proc_root_inode_operations = {
0357     .lookup     = proc_root_lookup,
0358     .getattr    = proc_root_getattr,
0359 };
0360 
0361 /*
0362  * This is the root "inode" in the /proc tree..
0363  */
0364 struct proc_dir_entry proc_root = {
0365     .low_ino    = PROC_ROOT_INO, 
0366     .namelen    = 5, 
0367     .mode       = S_IFDIR | S_IRUGO | S_IXUGO, 
0368     .nlink      = 2, 
0369     .refcnt     = REFCOUNT_INIT(1),
0370     .proc_iops  = &proc_root_inode_operations, 
0371     .proc_dir_ops   = &proc_root_operations,
0372     .parent     = &proc_root,
0373     .subdir     = RB_ROOT,
0374     .name       = "/proc",
0375 };