root.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. static int proc_test_super(struct super_block *sb, void *data)
  23. {
  24. return sb->s_fs_info == data;
  25. }
  26. static int proc_set_super(struct super_block *sb, void *data)
  27. {
  28. int err = set_anon_super(sb, NULL);
  29. if (!err) {
  30. struct pid_namespace *ns = (struct pid_namespace *)data;
  31. sb->s_fs_info = get_pid_ns(ns);
  32. }
  33. return err;
  34. }
  35. enum {
  36. Opt_gid, Opt_hidepid, Opt_err,
  37. };
  38. static const match_table_t tokens = {
  39. {Opt_hidepid, "hidepid=%u"},
  40. {Opt_gid, "gid=%u"},
  41. {Opt_err, NULL},
  42. };
  43. static int proc_parse_options(char *options, struct pid_namespace *pid)
  44. {
  45. char *p;
  46. substring_t args[MAX_OPT_ARGS];
  47. int option;
  48. if (!options)
  49. return 1;
  50. while ((p = strsep(&options, ",")) != NULL) {
  51. int token;
  52. if (!*p)
  53. continue;
  54. args[0].to = args[0].from = NULL;
  55. token = match_token(p, tokens, args);
  56. switch (token) {
  57. case Opt_gid:
  58. if (match_int(&args[0], &option))
  59. return 0;
  60. pid->pid_gid = make_kgid(current_user_ns(), option);
  61. break;
  62. case Opt_hidepid:
  63. if (match_int(&args[0], &option))
  64. return 0;
  65. if (option < 0 || option > 2) {
  66. pr_err("proc: hidepid value must be between 0 and 2.\n");
  67. return 0;
  68. }
  69. pid->hide_pid = option;
  70. break;
  71. default:
  72. pr_err("proc: unrecognized mount option \"%s\" "
  73. "or missing value\n", p);
  74. return 0;
  75. }
  76. }
  77. return 1;
  78. }
  79. int proc_remount(struct super_block *sb, int *flags, char *data)
  80. {
  81. struct pid_namespace *pid = sb->s_fs_info;
  82. sync_filesystem(sb);
  83. return !proc_parse_options(data, pid);
  84. }
  85. static struct dentry *proc_mount(struct file_system_type *fs_type,
  86. int flags, const char *dev_name, void *data)
  87. {
  88. int err;
  89. struct super_block *sb;
  90. struct pid_namespace *ns;
  91. char *options;
  92. if (flags & MS_KERNMOUNT) {
  93. ns = (struct pid_namespace *)data;
  94. options = NULL;
  95. } else {
  96. ns = task_active_pid_ns(current);
  97. options = data;
  98. /* Does the mounter have privilege over the pid namespace? */
  99. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  100. return ERR_PTR(-EPERM);
  101. }
  102. sb = sget(fs_type, proc_test_super, proc_set_super, flags, ns);
  103. if (IS_ERR(sb))
  104. return ERR_CAST(sb);
  105. /*
  106. * procfs isn't actually a stacking filesystem; however, there is
  107. * too much magic going on inside it to permit stacking things on
  108. * top of it
  109. */
  110. sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  111. if (!proc_parse_options(options, ns)) {
  112. deactivate_locked_super(sb);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. if (!sb->s_root) {
  116. err = proc_fill_super(sb);
  117. if (err) {
  118. deactivate_locked_super(sb);
  119. return ERR_PTR(err);
  120. }
  121. sb->s_flags |= MS_ACTIVE;
  122. /* User space would break if executables appear on proc */
  123. sb->s_iflags |= SB_I_NOEXEC;
  124. }
  125. return dget(sb->s_root);
  126. }
  127. static void proc_kill_sb(struct super_block *sb)
  128. {
  129. struct pid_namespace *ns;
  130. ns = (struct pid_namespace *)sb->s_fs_info;
  131. if (ns->proc_self)
  132. dput(ns->proc_self);
  133. if (ns->proc_thread_self)
  134. dput(ns->proc_thread_self);
  135. kill_anon_super(sb);
  136. put_pid_ns(ns);
  137. }
  138. static struct file_system_type proc_fs_type = {
  139. .name = "proc",
  140. .mount = proc_mount,
  141. .kill_sb = proc_kill_sb,
  142. .fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
  143. };
  144. void __init proc_root_init(void)
  145. {
  146. int err;
  147. proc_init_inodecache();
  148. err = register_filesystem(&proc_fs_type);
  149. if (err)
  150. return;
  151. proc_self_init();
  152. proc_thread_self_init();
  153. proc_symlink("mounts", NULL, "self/mounts");
  154. proc_net_init();
  155. #ifdef CONFIG_SYSVIPC
  156. proc_mkdir("sysvipc", NULL);
  157. #endif
  158. proc_mkdir("fs", NULL);
  159. proc_mkdir("driver", NULL);
  160. proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
  161. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  162. /* just give it a mountpoint */
  163. proc_create_mount_point("openprom");
  164. #endif
  165. proc_tty_init();
  166. proc_mkdir("bus", NULL);
  167. proc_sys_init();
  168. }
  169. static int proc_root_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat
  170. )
  171. {
  172. generic_fillattr(d_inode(dentry), stat);
  173. stat->nlink = proc_root.nlink + nr_processes();
  174. return 0;
  175. }
  176. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
  177. {
  178. if (!proc_pid_lookup(dir, dentry, flags))
  179. return NULL;
  180. return proc_lookup(dir, dentry, flags);
  181. }
  182. static int proc_root_readdir(struct file *file, struct dir_context *ctx)
  183. {
  184. if (ctx->pos < FIRST_PROCESS_ENTRY) {
  185. int error = proc_readdir(file, ctx);
  186. if (unlikely(error <= 0))
  187. return error;
  188. ctx->pos = FIRST_PROCESS_ENTRY;
  189. }
  190. return proc_pid_readdir(file, ctx);
  191. }
  192. /*
  193. * The root /proc directory is special, as it has the
  194. * <pid> directories. Thus we don't use the generic
  195. * directory handling functions for that..
  196. */
  197. static const struct file_operations proc_root_operations = {
  198. .read = generic_read_dir,
  199. .iterate = proc_root_readdir,
  200. .llseek = default_llseek,
  201. };
  202. /*
  203. * proc root can do almost nothing..
  204. */
  205. static const struct inode_operations proc_root_inode_operations = {
  206. .lookup = proc_root_lookup,
  207. .getattr = proc_root_getattr,
  208. };
  209. /*
  210. * This is the root "inode" in the /proc tree..
  211. */
  212. struct proc_dir_entry proc_root = {
  213. .low_ino = PROC_ROOT_INO,
  214. .namelen = 5,
  215. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  216. .nlink = 2,
  217. .count = ATOMIC_INIT(1),
  218. .proc_iops = &proc_root_inode_operations,
  219. .proc_fops = &proc_root_operations,
  220. .parent = &proc_root,
  221. .subdir = RB_ROOT,
  222. .name = "/proc",
  223. };
  224. int pid_ns_prepare_proc(struct pid_namespace *ns)
  225. {
  226. struct vfsmount *mnt;
  227. mnt = kern_mount_data(&proc_fs_type, ns);
  228. if (IS_ERR(mnt))
  229. return PTR_ERR(mnt);
  230. ns->proc_mnt = mnt;
  231. return 0;
  232. }
  233. void pid_ns_release_proc(struct pid_namespace *ns)
  234. {
  235. kern_unmount(ns->proc_mnt);
  236. }