root.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/uaccess.h>
  10. #include <linux/errno.h>
  11. #include <linux/time.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/stat.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/stat.h>
  17. #include <linux/module.h>
  18. #include <linux/bitops.h>
  19. #include <linux/user_namespace.h>
  20. #include <linux/mount.h>
  21. #include <linux/pid_namespace.h>
  22. #include <linux/parser.h>
  23. #include <linux/cred.h>
  24. #include "internal.h"
  25. enum {
  26. Opt_gid, Opt_hidepid, Opt_err,
  27. };
  28. static const match_table_t tokens = {
  29. {Opt_hidepid, "hidepid=%u"},
  30. {Opt_gid, "gid=%u"},
  31. {Opt_err, NULL},
  32. };
  33. int proc_parse_options(char *options, struct pid_namespace *pid)
  34. {
  35. char *p;
  36. substring_t args[MAX_OPT_ARGS];
  37. int option;
  38. if (!options)
  39. return 1;
  40. while ((p = strsep(&options, ",")) != NULL) {
  41. int token;
  42. if (!*p)
  43. continue;
  44. args[0].to = args[0].from = NULL;
  45. token = match_token(p, tokens, args);
  46. switch (token) {
  47. case Opt_gid:
  48. if (match_int(&args[0], &option))
  49. return 0;
  50. pid->pid_gid = make_kgid(current_user_ns(), option);
  51. break;
  52. case Opt_hidepid:
  53. if (match_int(&args[0], &option))
  54. return 0;
  55. if (option < HIDEPID_OFF ||
  56. option > HIDEPID_INVISIBLE) {
  57. pr_err("proc: hidepid value must be between 0 and 2.\n");
  58. return 0;
  59. }
  60. pid->hide_pid = option;
  61. break;
  62. default:
  63. pr_err("proc: unrecognized mount option \"%s\" "
  64. "or missing value\n", p);
  65. return 0;
  66. }
  67. }
  68. return 1;
  69. }
  70. int proc_remount(struct super_block *sb, int *flags, char *data)
  71. {
  72. struct pid_namespace *pid = sb->s_fs_info;
  73. sync_filesystem(sb);
  74. return !proc_parse_options(data, pid);
  75. }
  76. static struct dentry *proc_mount(struct file_system_type *fs_type,
  77. int flags, const char *dev_name, void *data)
  78. {
  79. struct pid_namespace *ns;
  80. if (flags & SB_KERNMOUNT) {
  81. ns = data;
  82. data = NULL;
  83. } else {
  84. ns = task_active_pid_ns(current);
  85. }
  86. return mount_ns(fs_type, flags, data, ns, ns->user_ns, proc_fill_super);
  87. }
  88. static void proc_kill_sb(struct super_block *sb)
  89. {
  90. struct pid_namespace *ns;
  91. ns = (struct pid_namespace *)sb->s_fs_info;
  92. if (ns->proc_self)
  93. dput(ns->proc_self);
  94. if (ns->proc_thread_self)
  95. dput(ns->proc_thread_self);
  96. kill_anon_super(sb);
  97. put_pid_ns(ns);
  98. }
  99. static struct file_system_type proc_fs_type = {
  100. .name = "proc",
  101. .mount = proc_mount,
  102. .kill_sb = proc_kill_sb,
  103. .fs_flags = FS_USERNS_MOUNT,
  104. };
  105. void __init proc_root_init(void)
  106. {
  107. proc_init_kmemcache();
  108. set_proc_pid_nlink();
  109. proc_self_init();
  110. proc_thread_self_init();
  111. proc_symlink("mounts", NULL, "self/mounts");
  112. proc_net_init();
  113. proc_mkdir("fs", NULL);
  114. proc_mkdir("driver", NULL);
  115. proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
  116. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  117. /* just give it a mountpoint */
  118. proc_create_mount_point("openprom");
  119. #endif
  120. proc_tty_init();
  121. proc_mkdir("bus", NULL);
  122. proc_sys_init();
  123. register_filesystem(&proc_fs_type);
  124. }
  125. static int proc_root_getattr(const struct path *path, struct kstat *stat,
  126. u32 request_mask, unsigned int query_flags)
  127. {
  128. generic_fillattr(d_inode(path->dentry), stat);
  129. stat->nlink = proc_root.nlink + nr_processes();
  130. return 0;
  131. }
  132. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
  133. {
  134. if (!proc_pid_lookup(dir, dentry, flags))
  135. return NULL;
  136. return proc_lookup(dir, dentry, flags);
  137. }
  138. static int proc_root_readdir(struct file *file, struct dir_context *ctx)
  139. {
  140. if (ctx->pos < FIRST_PROCESS_ENTRY) {
  141. int error = proc_readdir(file, ctx);
  142. if (unlikely(error <= 0))
  143. return error;
  144. ctx->pos = FIRST_PROCESS_ENTRY;
  145. }
  146. return proc_pid_readdir(file, ctx);
  147. }
  148. /*
  149. * The root /proc directory is special, as it has the
  150. * <pid> directories. Thus we don't use the generic
  151. * directory handling functions for that..
  152. */
  153. static const struct file_operations proc_root_operations = {
  154. .read = generic_read_dir,
  155. .iterate_shared = proc_root_readdir,
  156. .llseek = generic_file_llseek,
  157. };
  158. /*
  159. * proc root can do almost nothing..
  160. */
  161. static const struct inode_operations proc_root_inode_operations = {
  162. .lookup = proc_root_lookup,
  163. .getattr = proc_root_getattr,
  164. };
  165. /*
  166. * This is the root "inode" in the /proc tree..
  167. */
  168. struct proc_dir_entry proc_root = {
  169. .low_ino = PROC_ROOT_INO,
  170. .namelen = 5,
  171. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  172. .nlink = 2,
  173. .refcnt = REFCOUNT_INIT(1),
  174. .proc_iops = &proc_root_inode_operations,
  175. .proc_fops = &proc_root_operations,
  176. .parent = &proc_root,
  177. .subdir = RB_ROOT,
  178. .name = "/proc",
  179. };
  180. int pid_ns_prepare_proc(struct pid_namespace *ns)
  181. {
  182. struct vfsmount *mnt;
  183. mnt = kern_mount_data(&proc_fs_type, ns);
  184. if (IS_ERR(mnt))
  185. return PTR_ERR(mnt);
  186. ns->proc_mnt = mnt;
  187. return 0;
  188. }
  189. void pid_ns_release_proc(struct pid_namespace *ns)
  190. {
  191. kern_unmount(ns->proc_mnt);
  192. }