root.c 4.8 KB

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