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