root.c 4.9 KB

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