root.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. int err;
  108. proc_init_inodecache();
  109. set_proc_pid_nlink();
  110. err = register_filesystem(&proc_fs_type);
  111. if (err)
  112. return;
  113. proc_self_init();
  114. proc_thread_self_init();
  115. proc_symlink("mounts", NULL, "self/mounts");
  116. proc_net_init();
  117. #ifdef CONFIG_SYSVIPC
  118. proc_mkdir("sysvipc", NULL);
  119. #endif
  120. proc_mkdir("fs", NULL);
  121. proc_mkdir("driver", NULL);
  122. proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
  123. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  124. /* just give it a mountpoint */
  125. proc_create_mount_point("openprom");
  126. #endif
  127. proc_tty_init();
  128. proc_mkdir("bus", NULL);
  129. proc_sys_init();
  130. }
  131. static int proc_root_getattr(const struct path *path, struct kstat *stat,
  132. u32 request_mask, unsigned int query_flags)
  133. {
  134. generic_fillattr(d_inode(path->dentry), stat);
  135. stat->nlink = proc_root.nlink + nr_processes();
  136. return 0;
  137. }
  138. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
  139. {
  140. if (!proc_pid_lookup(dir, dentry, flags))
  141. return NULL;
  142. return proc_lookup(dir, dentry, flags);
  143. }
  144. static int proc_root_readdir(struct file *file, struct dir_context *ctx)
  145. {
  146. if (ctx->pos < FIRST_PROCESS_ENTRY) {
  147. int error = proc_readdir(file, ctx);
  148. if (unlikely(error <= 0))
  149. return error;
  150. ctx->pos = FIRST_PROCESS_ENTRY;
  151. }
  152. return proc_pid_readdir(file, ctx);
  153. }
  154. /*
  155. * The root /proc directory is special, as it has the
  156. * <pid> directories. Thus we don't use the generic
  157. * directory handling functions for that..
  158. */
  159. static const struct file_operations proc_root_operations = {
  160. .read = generic_read_dir,
  161. .iterate_shared = proc_root_readdir,
  162. .llseek = generic_file_llseek,
  163. };
  164. /*
  165. * proc root can do almost nothing..
  166. */
  167. static const struct inode_operations proc_root_inode_operations = {
  168. .lookup = proc_root_lookup,
  169. .getattr = proc_root_getattr,
  170. };
  171. /*
  172. * This is the root "inode" in the /proc tree..
  173. */
  174. struct proc_dir_entry proc_root = {
  175. .low_ino = PROC_ROOT_INO,
  176. .namelen = 5,
  177. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  178. .nlink = 2,
  179. .count = ATOMIC_INIT(1),
  180. .proc_iops = &proc_root_inode_operations,
  181. .proc_fops = &proc_root_operations,
  182. .parent = &proc_root,
  183. .subdir = RB_ROOT_CACHED,
  184. .name = "/proc",
  185. };
  186. int pid_ns_prepare_proc(struct pid_namespace *ns)
  187. {
  188. struct vfsmount *mnt;
  189. mnt = kern_mount_data(&proc_fs_type, ns);
  190. if (IS_ERR(mnt))
  191. return PTR_ERR(mnt);
  192. ns->proc_mnt = mnt;
  193. return 0;
  194. }
  195. void pid_ns_release_proc(struct pid_namespace *ns)
  196. {
  197. kern_unmount(ns->proc_mnt);
  198. }