1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/proc/root.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * 7 * proc root directory handling functions 8 */ 9 #include <linux/errno.h> 10 #include <linux/time.h> 11 #include <linux/proc_fs.h> 12 #include <linux/stat.h> 13 #include <linux/init.h> 14 #include <linux/sched.h> 15 #include <linux/sched/stat.h> 16 #include <linux/module.h> 17 #include <linux/bitops.h> 18 #include <linux/user_namespace.h> 19 #include <linux/fs_context.h> 20 #include <linux/mount.h> 21 #include <linux/pid_namespace.h> 22 #include <linux/fs_parser.h> 23 #include <linux/cred.h> 24 #include <linux/magic.h> 25 #include <linux/slab.h> 26 27 #include "internal.h" 28 29 struct proc_fs_context { 30 struct pid_namespace *pid_ns; 31 unsigned int mask; 32 enum proc_hidepid hidepid; 33 int gid; 34 enum proc_pidonly pidonly; 35 }; 36 37 enum proc_param { 38 Opt_gid, 39 Opt_hidepid, 40 Opt_subset, 41 Opt_pidns, 42 }; 43 44 static const struct fs_parameter_spec proc_fs_parameters[] = { 45 fsparam_u32("gid", Opt_gid), 46 fsparam_string("hidepid", Opt_hidepid), 47 fsparam_string("subset", Opt_subset), 48 fsparam_file_or_string("pidns", Opt_pidns), 49 {} 50 }; 51 52 static inline int valid_hidepid(unsigned int value) 53 { 54 return (value == HIDEPID_OFF || 55 value == HIDEPID_NO_ACCESS || 56 value == HIDEPID_INVISIBLE || 57 value == HIDEPID_NOT_PTRACEABLE); 58 } 59 60 static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param) 61 { 62 struct proc_fs_context *ctx = fc->fs_private; 63 struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid); 64 struct fs_parse_result result; 65 int base = (unsigned long)hidepid_u32_spec.data; 66 67 if (param->type != fs_value_is_string) 68 return invalf(fc, "proc: unexpected type of hidepid value\n"); 69 70 if (!kstrtouint(param->string, base, &result.uint_32)) { 71 if (!valid_hidepid(result.uint_32)) 72 return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string); 73 ctx->hidepid = result.uint_32; 74 return 0; 75 } 76 77 if (!strcmp(param->string, "off")) 78 ctx->hidepid = HIDEPID_OFF; 79 else if (!strcmp(param->string, "noaccess")) 80 ctx->hidepid = HIDEPID_NO_ACCESS; 81 else if (!strcmp(param->string, "invisible")) 82 ctx->hidepid = HIDEPID_INVISIBLE; 83 else if (!strcmp(param->string, "ptraceable")) 84 ctx->hidepid = HIDEPID_NOT_PTRACEABLE; 85 else 86 return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string); 87 88 return 0; 89 } 90 91 static int proc_parse_subset_param(struct fs_context *fc, char *value) 92 { 93 struct proc_fs_context *ctx = fc->fs_private; 94 95 while (value) { 96 char *ptr = strchr(value, ','); 97 98 if (ptr != NULL) 99 *ptr++ = '\0'; 100 101 if (*value != '\0') { 102 if (!strcmp(value, "pid")) { 103 ctx->pidonly = PROC_PIDONLY_ON; 104 } else { 105 return invalf(fc, "proc: unsupported subset option - %s\n", value); 106 } 107 } 108 value = ptr; 109 } 110 111 return 0; 112 } 113 114 #ifdef CONFIG_PID_NS 115 static int proc_parse_pidns_param(struct fs_context *fc, 116 struct fs_parameter *param, 117 struct fs_parse_result *result) 118 { 119 struct proc_fs_context *ctx = fc->fs_private; 120 struct pid_namespace *target, *active = task_active_pid_ns(current); 121 struct ns_common *ns; 122 struct file *ns_filp __free(fput) = NULL; 123 124 switch (param->type) { 125 case fs_value_is_file: 126 /* came through fsconfig, steal the file reference */ 127 ns_filp = no_free_ptr(param->file); 128 break; 129 case fs_value_is_string: 130 ns_filp = filp_open(param->string, O_RDONLY, 0); 131 break; 132 default: 133 WARN_ON_ONCE(true); 134 break; 135 } 136 if (!ns_filp) 137 ns_filp = ERR_PTR(-EBADF); 138 if (IS_ERR(ns_filp)) { 139 errorfc(fc, "could not get file from pidns argument"); 140 return PTR_ERR(ns_filp); 141 } 142 143 if (!proc_ns_file(ns_filp)) 144 return invalfc(fc, "pidns argument is not an nsfs file"); 145 ns = get_proc_ns(file_inode(ns_filp)); 146 if (ns->ns_type != CLONE_NEWPID) 147 return invalfc(fc, "pidns argument is not a pidns file"); 148 target = container_of(ns, struct pid_namespace, ns); 149 150 /* 151 * pidns= is shorthand for joining the pidns to get a fsopen fd, so the 152 * permission model should be the same as pidns_install(). 153 */ 154 if (!ns_capable(target->user_ns, CAP_SYS_ADMIN)) { 155 errorfc(fc, "insufficient permissions to set pidns"); 156 return -EPERM; 157 } 158 if (!pidns_is_ancestor(target, active)) 159 return invalfc(fc, "cannot set pidns to non-descendant pidns"); 160 161 put_pid_ns(ctx->pid_ns); 162 ctx->pid_ns = get_pid_ns(target); 163 put_user_ns(fc->user_ns); 164 fc->user_ns = get_user_ns(ctx->pid_ns->user_ns); 165 return 0; 166 } 167 #endif /* CONFIG_PID_NS */ 168 169 static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param) 170 { 171 struct proc_fs_context *ctx = fc->fs_private; 172 struct fs_parse_result result; 173 int opt, err; 174 175 opt = fs_parse(fc, proc_fs_parameters, param, &result); 176 if (opt < 0) 177 return opt; 178 179 switch (opt) { 180 case Opt_gid: 181 ctx->gid = result.uint_32; 182 break; 183 184 case Opt_hidepid: 185 err = proc_parse_hidepid_param(fc, param); 186 if (err) 187 return err; 188 break; 189 190 case Opt_subset: 191 err = proc_parse_subset_param(fc, param->string); 192 if (err) 193 return err; 194 break; 195 196 case Opt_pidns: 197 #ifdef CONFIG_PID_NS 198 /* 199 * We would have to RCU-protect every proc_pid_ns() or 200 * proc_sb_info() access if we allowed this to be reconfigured 201 * for an existing procfs instance. Luckily, procfs instances 202 * are cheap to create, and mount-beneath would let you 203 * atomically replace an instance even with overmounts. 204 */ 205 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 206 errorfc(fc, "cannot reconfigure pidns for existing procfs"); 207 return -EBUSY; 208 } 209 err = proc_parse_pidns_param(fc, param, &result); 210 if (err) 211 return err; 212 break; 213 #else 214 errorfc(fc, "pidns mount flag not supported on this system"); 215 return -EOPNOTSUPP; 216 #endif 217 218 default: 219 return -EINVAL; 220 } 221 222 ctx->mask |= 1 << opt; 223 return 0; 224 } 225 226 static int proc_apply_options(struct proc_fs_info *fs_info, 227 struct fs_context *fc, 228 struct user_namespace *user_ns) 229 { 230 struct proc_fs_context *ctx = fc->fs_private; 231 232 if ((ctx->mask & (1 << Opt_subset)) && 233 fc->purpose == FS_CONTEXT_FOR_RECONFIGURE && 234 ctx->pidonly != fs_info->pidonly) 235 return invalf(fc, "proc: subset=pid cannot be changed\n"); 236 237 if (ctx->mask & (1 << Opt_gid)) 238 fs_info->pid_gid = make_kgid(user_ns, ctx->gid); 239 if (ctx->mask & (1 << Opt_hidepid)) 240 fs_info->hide_pid = ctx->hidepid; 241 if (ctx->mask & (1 << Opt_subset)) 242 fs_info->pidonly = ctx->pidonly; 243 if (ctx->mask & (1 << Opt_pidns) && 244 !WARN_ON_ONCE(fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)) { 245 put_pid_ns(fs_info->pid_ns); 246 fs_info->pid_ns = get_pid_ns(ctx->pid_ns); 247 } 248 return 0; 249 } 250 251 static int proc_fill_super(struct super_block *s, struct fs_context *fc) 252 { 253 struct proc_fs_context *ctx = fc->fs_private; 254 struct inode *root_inode; 255 struct proc_fs_info *fs_info; 256 int ret; 257 258 fs_info = kzalloc_obj(*fs_info); 259 if (!fs_info) 260 return -ENOMEM; 261 262 fs_info->pid_ns = get_pid_ns(ctx->pid_ns); 263 fs_info->mounter_cred = get_cred(fc->cred); 264 ret = proc_apply_options(fs_info, fc, current_user_ns()); 265 if (ret) 266 return ret; 267 268 /* User space would break if executables or devices appear on proc */ 269 s->s_iflags |= SB_I_NOEXEC | SB_I_NODEV; 270 s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC; 271 s->s_blocksize = 1024; 272 s->s_blocksize_bits = 10; 273 s->s_magic = PROC_SUPER_MAGIC; 274 s->s_op = &proc_sops; 275 s->s_time_gran = 1; 276 s->s_fs_info = fs_info; 277 278 if (fs_info->pidonly == PROC_PIDONLY_ON) 279 s->s_iflags |= SB_I_RESTRICTED_VARIANT; 280 281 /* 282 * procfs isn't actually a stacking filesystem; however, there is 283 * too much magic going on inside it to permit stacking things on 284 * top of it 285 */ 286 s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH; 287 288 /* procfs dentries and inodes don't require IO to create */ 289 s->s_shrink->seeks = 0; 290 291 pde_get(&proc_root); 292 root_inode = proc_get_inode(s, &proc_root); 293 if (!root_inode) { 294 pr_err("proc_fill_super: get root inode failed\n"); 295 return -ENOMEM; 296 } 297 298 s->s_root = d_make_root(root_inode); 299 if (!s->s_root) { 300 pr_err("proc_fill_super: allocate dentry failed\n"); 301 return -ENOMEM; 302 } 303 304 ret = proc_setup_self(s); 305 if (ret) { 306 return ret; 307 } 308 return proc_setup_thread_self(s); 309 } 310 311 static int proc_reconfigure(struct fs_context *fc) 312 { 313 struct super_block *sb = fc->root->d_sb; 314 struct proc_fs_info *fs_info = proc_sb_info(sb); 315 316 sync_filesystem(sb); 317 318 return proc_apply_options(fs_info, fc, current_user_ns()); 319 } 320 321 static int proc_get_tree(struct fs_context *fc) 322 { 323 return get_tree_nodev(fc, proc_fill_super); 324 } 325 326 static void proc_fs_context_free(struct fs_context *fc) 327 { 328 struct proc_fs_context *ctx = fc->fs_private; 329 330 put_pid_ns(ctx->pid_ns); 331 kfree(ctx); 332 } 333 334 static const struct fs_context_operations proc_fs_context_ops = { 335 .free = proc_fs_context_free, 336 .parse_param = proc_parse_param, 337 .get_tree = proc_get_tree, 338 .reconfigure = proc_reconfigure, 339 }; 340 341 static int proc_init_fs_context(struct fs_context *fc) 342 { 343 struct proc_fs_context *ctx; 344 345 ctx = kzalloc_obj(struct proc_fs_context); 346 if (!ctx) 347 return -ENOMEM; 348 349 ctx->pid_ns = get_pid_ns(task_active_pid_ns(current)); 350 put_user_ns(fc->user_ns); 351 fc->user_ns = get_user_ns(ctx->pid_ns->user_ns); 352 fc->fs_private = ctx; 353 fc->ops = &proc_fs_context_ops; 354 return 0; 355 } 356 357 static void proc_kill_sb(struct super_block *sb) 358 { 359 struct proc_fs_info *fs_info = proc_sb_info(sb); 360 361 kill_anon_super(sb); 362 if (fs_info) { 363 put_pid_ns(fs_info->pid_ns); 364 put_cred(fs_info->mounter_cred); 365 kfree_rcu(fs_info, rcu); 366 } 367 } 368 369 static struct file_system_type proc_fs_type = { 370 .name = "proc", 371 .init_fs_context = proc_init_fs_context, 372 .parameters = proc_fs_parameters, 373 .kill_sb = proc_kill_sb, 374 .fs_flags = FS_USERNS_MOUNT | FS_USERNS_MOUNT_RESTRICTED | FS_DISALLOW_NOTIFY_PERM, 375 }; 376 377 void __init proc_root_init(void) 378 { 379 proc_init_kmemcache(); 380 set_proc_pid_nlink(); 381 proc_self_init(); 382 proc_thread_self_init(); 383 proc_symlink("mounts", NULL, "self/mounts"); 384 385 proc_net_init(); 386 proc_mkdir("fs", NULL); 387 proc_mkdir("driver", NULL); 388 proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */ 389 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE) 390 /* just give it a mountpoint */ 391 proc_create_mount_point("openprom"); 392 #endif 393 proc_tty_init(); 394 proc_mkdir("bus", NULL); 395 proc_sys_init(); 396 397 /* 398 * Last things last. It is not like userspace processes eager 399 * to open /proc files exist at this point but register last 400 * anyway. 401 */ 402 register_filesystem(&proc_fs_type); 403 } 404 405 static int proc_root_getattr(struct mnt_idmap *idmap, 406 const struct path *path, struct kstat *stat, 407 u32 request_mask, unsigned int query_flags) 408 { 409 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry), 410 stat); 411 stat->nlink = proc_root.nlink + nr_processes(); 412 return 0; 413 } 414 415 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags) 416 { 417 if (!proc_pid_lookup(dentry, flags)) 418 return NULL; 419 420 return proc_lookup(dir, dentry, flags); 421 } 422 423 static int proc_root_readdir(struct file *file, struct dir_context *ctx) 424 { 425 if (ctx->pos < FIRST_PROCESS_ENTRY) { 426 int error = proc_readdir(file, ctx); 427 if (unlikely(error <= 0)) 428 return error; 429 ctx->pos = FIRST_PROCESS_ENTRY; 430 } 431 432 return proc_pid_readdir(file, ctx); 433 } 434 435 /* 436 * The root /proc directory is special, as it has the 437 * <pid> directories. Thus we don't use the generic 438 * directory handling functions for that.. 439 */ 440 static const struct file_operations proc_root_operations = { 441 .read = generic_read_dir, 442 .iterate_shared = proc_root_readdir, 443 .llseek = generic_file_llseek, 444 }; 445 446 /* 447 * proc root can do almost nothing.. 448 */ 449 static const struct inode_operations proc_root_inode_operations = { 450 .lookup = proc_root_lookup, 451 .getattr = proc_root_getattr, 452 }; 453 454 /* 455 * This is the root "inode" in the /proc tree.. 456 */ 457 struct proc_dir_entry proc_root = { 458 .low_ino = PROCFS_ROOT_INO, 459 .namelen = 5, 460 .mode = S_IFDIR | S_IRUGO | S_IXUGO, 461 .nlink = 2, 462 .refcnt = REFCOUNT_INIT(1), 463 .proc_iops = &proc_root_inode_operations, 464 .proc_dir_ops = &proc_root_operations, 465 .parent = &proc_root, 466 .subdir = RB_ROOT, 467 .name = "/proc", 468 }; 469