root.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/proc/root.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * proc root directory handling functions
  8. */
  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/sched/stat.h>
  16. #include <linux/module.h>
  17. #include <linux/bitops.h>
  18. #include <linux/user_namespace.h>
  19. #include <linux/fs_context.h>
  20. #include <linux/mount.h>
  21. #include <linux/pid_namespace.h>
  22. #include <linux/fs_parser.h>
  23. #include <linux/cred.h>
  24. #include <linux/magic.h>
  25. #include <linux/slab.h>
  26. #include "internal.h"
  27. struct proc_fs_context {
  28. struct pid_namespace *pid_ns;
  29. unsigned int mask;
  30. enum proc_hidepid hidepid;
  31. int gid;
  32. enum proc_pidonly pidonly;
  33. };
  34. enum proc_param {
  35. Opt_gid,
  36. Opt_hidepid,
  37. Opt_subset,
  38. };
  39. static const struct fs_parameter_spec proc_fs_parameters[] = {
  40. fsparam_u32("gid", Opt_gid),
  41. fsparam_string("hidepid", Opt_hidepid),
  42. fsparam_string("subset", Opt_subset),
  43. {}
  44. };
  45. static inline int valid_hidepid(unsigned int value)
  46. {
  47. return (value == HIDEPID_OFF ||
  48. value == HIDEPID_NO_ACCESS ||
  49. value == HIDEPID_INVISIBLE ||
  50. value == HIDEPID_NOT_PTRACEABLE);
  51. }
  52. static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param)
  53. {
  54. struct proc_fs_context *ctx = fc->fs_private;
  55. struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid);
  56. struct fs_parse_result result;
  57. int base = (unsigned long)hidepid_u32_spec.data;
  58. if (param->type != fs_value_is_string)
  59. return invalf(fc, "proc: unexpected type of hidepid value\n");
  60. if (!kstrtouint(param->string, base, &result.uint_32)) {
  61. if (!valid_hidepid(result.uint_32))
  62. return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
  63. ctx->hidepid = result.uint_32;
  64. return 0;
  65. }
  66. if (!strcmp(param->string, "off"))
  67. ctx->hidepid = HIDEPID_OFF;
  68. else if (!strcmp(param->string, "noaccess"))
  69. ctx->hidepid = HIDEPID_NO_ACCESS;
  70. else if (!strcmp(param->string, "invisible"))
  71. ctx->hidepid = HIDEPID_INVISIBLE;
  72. else if (!strcmp(param->string, "ptraceable"))
  73. ctx->hidepid = HIDEPID_NOT_PTRACEABLE;
  74. else
  75. return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
  76. return 0;
  77. }
  78. static int proc_parse_subset_param(struct fs_context *fc, char *value)
  79. {
  80. struct proc_fs_context *ctx = fc->fs_private;
  81. while (value) {
  82. char *ptr = strchr(value, ',');
  83. if (ptr != NULL)
  84. *ptr++ = '\0';
  85. if (*value != '\0') {
  86. if (!strcmp(value, "pid")) {
  87. ctx->pidonly = PROC_PIDONLY_ON;
  88. } else {
  89. return invalf(fc, "proc: unsupported subset option - %s\n", value);
  90. }
  91. }
  92. value = ptr;
  93. }
  94. return 0;
  95. }
  96. static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
  97. {
  98. struct proc_fs_context *ctx = fc->fs_private;
  99. struct fs_parse_result result;
  100. int opt;
  101. opt = fs_parse(fc, proc_fs_parameters, param, &result);
  102. if (opt < 0)
  103. return opt;
  104. switch (opt) {
  105. case Opt_gid:
  106. ctx->gid = result.uint_32;
  107. break;
  108. case Opt_hidepid:
  109. if (proc_parse_hidepid_param(fc, param))
  110. return -EINVAL;
  111. break;
  112. case Opt_subset:
  113. if (proc_parse_subset_param(fc, param->string) < 0)
  114. return -EINVAL;
  115. break;
  116. default:
  117. return -EINVAL;
  118. }
  119. ctx->mask |= 1 << opt;
  120. return 0;
  121. }
  122. static void proc_apply_options(struct proc_fs_info *fs_info,
  123. struct fs_context *fc,
  124. struct user_namespace *user_ns)
  125. {
  126. struct proc_fs_context *ctx = fc->fs_private;
  127. if (ctx->mask & (1 << Opt_gid))
  128. fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
  129. if (ctx->mask & (1 << Opt_hidepid))
  130. fs_info->hide_pid = ctx->hidepid;
  131. if (ctx->mask & (1 << Opt_subset))
  132. fs_info->pidonly = ctx->pidonly;
  133. }
  134. static int proc_fill_super(struct super_block *s, struct fs_context *fc)
  135. {
  136. struct proc_fs_context *ctx = fc->fs_private;
  137. struct inode *root_inode;
  138. struct proc_fs_info *fs_info;
  139. int ret;
  140. fs_info = kzalloc(sizeof(*fs_info), GFP_KERNEL);
  141. if (!fs_info)
  142. return -ENOMEM;
  143. fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
  144. proc_apply_options(fs_info, fc, current_user_ns());
  145. /* User space would break if executables or devices appear on proc */
  146. s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
  147. s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
  148. s->s_blocksize = 1024;
  149. s->s_blocksize_bits = 10;
  150. s->s_magic = PROC_SUPER_MAGIC;
  151. s->s_op = &proc_sops;
  152. s->s_time_gran = 1;
  153. s->s_fs_info = fs_info;
  154. /*
  155. * procfs isn't actually a stacking filesystem; however, there is
  156. * too much magic going on inside it to permit stacking things on
  157. * top of it
  158. */
  159. s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  160. /* procfs dentries and inodes don't require IO to create */
  161. s->s_shrink.seeks = 0;
  162. pde_get(&proc_root);
  163. root_inode = proc_get_inode(s, &proc_root);
  164. if (!root_inode) {
  165. pr_err("proc_fill_super: get root inode failed\n");
  166. return -ENOMEM;
  167. }
  168. s->s_root = d_make_root(root_inode);
  169. if (!s->s_root) {
  170. pr_err("proc_fill_super: allocate dentry failed\n");
  171. return -ENOMEM;
  172. }
  173. ret = proc_setup_self(s);
  174. if (ret) {
  175. return ret;
  176. }
  177. return proc_setup_thread_self(s);
  178. }
  179. static int proc_reconfigure(struct fs_context *fc)
  180. {
  181. struct super_block *sb = fc->root->d_sb;
  182. struct proc_fs_info *fs_info = proc_sb_info(sb);
  183. sync_filesystem(sb);
  184. proc_apply_options(fs_info, fc, current_user_ns());
  185. return 0;
  186. }
  187. static int proc_get_tree(struct fs_context *fc)
  188. {
  189. return get_tree_nodev(fc, proc_fill_super);
  190. }
  191. static void proc_fs_context_free(struct fs_context *fc)
  192. {
  193. struct proc_fs_context *ctx = fc->fs_private;
  194. put_pid_ns(ctx->pid_ns);
  195. kfree(ctx);
  196. }
  197. static const struct fs_context_operations proc_fs_context_ops = {
  198. .free = proc_fs_context_free,
  199. .parse_param = proc_parse_param,
  200. .get_tree = proc_get_tree,
  201. .reconfigure = proc_reconfigure,
  202. };
  203. static int proc_init_fs_context(struct fs_context *fc)
  204. {
  205. struct proc_fs_context *ctx;
  206. ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL);
  207. if (!ctx)
  208. return -ENOMEM;
  209. ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
  210. put_user_ns(fc->user_ns);
  211. fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
  212. fc->fs_private = ctx;
  213. fc->ops = &proc_fs_context_ops;
  214. return 0;
  215. }
  216. static void proc_kill_sb(struct super_block *sb)
  217. {
  218. struct proc_fs_info *fs_info = proc_sb_info(sb);
  219. if (!fs_info) {
  220. kill_anon_super(sb);
  221. return;
  222. }
  223. dput(fs_info->proc_self);
  224. dput(fs_info->proc_thread_self);
  225. kill_anon_super(sb);
  226. put_pid_ns(fs_info->pid_ns);
  227. kfree(fs_info);
  228. }
  229. static struct file_system_type proc_fs_type = {
  230. .name = "proc",
  231. .init_fs_context = proc_init_fs_context,
  232. .parameters = proc_fs_parameters,
  233. .kill_sb = proc_kill_sb,
  234. .fs_flags = FS_USERNS_MOUNT | FS_DISALLOW_NOTIFY_PERM,
  235. };
  236. void __init proc_root_init(void)
  237. {
  238. proc_init_kmemcache();
  239. set_proc_pid_nlink();
  240. proc_self_init();
  241. proc_thread_self_init();
  242. proc_symlink("mounts", NULL, "self/mounts");
  243. proc_net_init();
  244. proc_mkdir("fs", NULL);
  245. proc_mkdir("driver", NULL);
  246. proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
  247. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  248. /* just give it a mountpoint */
  249. proc_create_mount_point("openprom");
  250. #endif
  251. proc_tty_init();
  252. proc_mkdir("bus", NULL);
  253. proc_sys_init();
  254. /*
  255. * Last things last. It is not like userspace processes eager
  256. * to open /proc files exist at this point but register last
  257. * anyway.
  258. */
  259. register_filesystem(&proc_fs_type);
  260. }
  261. static int proc_root_getattr(struct user_namespace *mnt_userns,
  262. const struct path *path, struct kstat *stat,
  263. u32 request_mask, unsigned int query_flags)
  264. {
  265. generic_fillattr(&init_user_ns, d_inode(path->dentry), stat);
  266. stat->nlink = proc_root.nlink + nr_processes();
  267. return 0;
  268. }
  269. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
  270. {
  271. if (!proc_pid_lookup(dentry, flags))
  272. return NULL;
  273. return proc_lookup(dir, dentry, flags);
  274. }
  275. static int proc_root_readdir(struct file *file, struct dir_context *ctx)
  276. {
  277. if (ctx->pos < FIRST_PROCESS_ENTRY) {
  278. int error = proc_readdir(file, ctx);
  279. if (unlikely(error <= 0))
  280. return error;
  281. ctx->pos = FIRST_PROCESS_ENTRY;
  282. }
  283. return proc_pid_readdir(file, ctx);
  284. }
  285. /*
  286. * The root /proc directory is special, as it has the
  287. * <pid> directories. Thus we don't use the generic
  288. * directory handling functions for that..
  289. */
  290. static const struct file_operations proc_root_operations = {
  291. .read = generic_read_dir,
  292. .iterate_shared = proc_root_readdir,
  293. .llseek = generic_file_llseek,
  294. };
  295. /*
  296. * proc root can do almost nothing..
  297. */
  298. static const struct inode_operations proc_root_inode_operations = {
  299. .lookup = proc_root_lookup,
  300. .getattr = proc_root_getattr,
  301. };
  302. /*
  303. * This is the root "inode" in the /proc tree..
  304. */
  305. struct proc_dir_entry proc_root = {
  306. .low_ino = PROC_ROOT_INO,
  307. .namelen = 5,
  308. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  309. .nlink = 2,
  310. .refcnt = REFCOUNT_INIT(1),
  311. .proc_iops = &proc_root_inode_operations,
  312. .proc_dir_ops = &proc_root_operations,
  313. .parent = &proc_root,
  314. .subdir = RB_ROOT,
  315. .name = "/proc",
  316. };