root.c 4.9 KB

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