root.c 4.9 KB

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