root.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. Opt_pidns,
  39. };
  40. static const struct fs_parameter_spec proc_fs_parameters[] = {
  41. fsparam_u32("gid", Opt_gid),
  42. fsparam_string("hidepid", Opt_hidepid),
  43. fsparam_string("subset", Opt_subset),
  44. fsparam_file_or_string("pidns", Opt_pidns),
  45. {}
  46. };
  47. static inline int valid_hidepid(unsigned int value)
  48. {
  49. return (value == HIDEPID_OFF ||
  50. value == HIDEPID_NO_ACCESS ||
  51. value == HIDEPID_INVISIBLE ||
  52. value == HIDEPID_NOT_PTRACEABLE);
  53. }
  54. static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param)
  55. {
  56. struct proc_fs_context *ctx = fc->fs_private;
  57. struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid);
  58. struct fs_parse_result result;
  59. int base = (unsigned long)hidepid_u32_spec.data;
  60. if (param->type != fs_value_is_string)
  61. return invalf(fc, "proc: unexpected type of hidepid value\n");
  62. if (!kstrtouint(param->string, base, &result.uint_32)) {
  63. if (!valid_hidepid(result.uint_32))
  64. return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
  65. ctx->hidepid = result.uint_32;
  66. return 0;
  67. }
  68. if (!strcmp(param->string, "off"))
  69. ctx->hidepid = HIDEPID_OFF;
  70. else if (!strcmp(param->string, "noaccess"))
  71. ctx->hidepid = HIDEPID_NO_ACCESS;
  72. else if (!strcmp(param->string, "invisible"))
  73. ctx->hidepid = HIDEPID_INVISIBLE;
  74. else if (!strcmp(param->string, "ptraceable"))
  75. ctx->hidepid = HIDEPID_NOT_PTRACEABLE;
  76. else
  77. return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
  78. return 0;
  79. }
  80. static int proc_parse_subset_param(struct fs_context *fc, char *value)
  81. {
  82. struct proc_fs_context *ctx = fc->fs_private;
  83. while (value) {
  84. char *ptr = strchr(value, ',');
  85. if (ptr != NULL)
  86. *ptr++ = '\0';
  87. if (*value != '\0') {
  88. if (!strcmp(value, "pid")) {
  89. ctx->pidonly = PROC_PIDONLY_ON;
  90. } else {
  91. return invalf(fc, "proc: unsupported subset option - %s\n", value);
  92. }
  93. }
  94. value = ptr;
  95. }
  96. return 0;
  97. }
  98. #ifdef CONFIG_PID_NS
  99. static int proc_parse_pidns_param(struct fs_context *fc,
  100. struct fs_parameter *param,
  101. struct fs_parse_result *result)
  102. {
  103. struct proc_fs_context *ctx = fc->fs_private;
  104. struct pid_namespace *target, *active = task_active_pid_ns(current);
  105. struct ns_common *ns;
  106. struct file *ns_filp __free(fput) = NULL;
  107. switch (param->type) {
  108. case fs_value_is_file:
  109. /* came through fsconfig, steal the file reference */
  110. ns_filp = no_free_ptr(param->file);
  111. break;
  112. case fs_value_is_string:
  113. ns_filp = filp_open(param->string, O_RDONLY, 0);
  114. break;
  115. default:
  116. WARN_ON_ONCE(true);
  117. break;
  118. }
  119. if (!ns_filp)
  120. ns_filp = ERR_PTR(-EBADF);
  121. if (IS_ERR(ns_filp)) {
  122. errorfc(fc, "could not get file from pidns argument");
  123. return PTR_ERR(ns_filp);
  124. }
  125. if (!proc_ns_file(ns_filp))
  126. return invalfc(fc, "pidns argument is not an nsfs file");
  127. ns = get_proc_ns(file_inode(ns_filp));
  128. if (ns->ns_type != CLONE_NEWPID)
  129. return invalfc(fc, "pidns argument is not a pidns file");
  130. target = container_of(ns, struct pid_namespace, ns);
  131. /*
  132. * pidns= is shorthand for joining the pidns to get a fsopen fd, so the
  133. * permission model should be the same as pidns_install().
  134. */
  135. if (!ns_capable(target->user_ns, CAP_SYS_ADMIN)) {
  136. errorfc(fc, "insufficient permissions to set pidns");
  137. return -EPERM;
  138. }
  139. if (!pidns_is_ancestor(target, active))
  140. return invalfc(fc, "cannot set pidns to non-descendant pidns");
  141. put_pid_ns(ctx->pid_ns);
  142. ctx->pid_ns = get_pid_ns(target);
  143. put_user_ns(fc->user_ns);
  144. fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
  145. return 0;
  146. }
  147. #endif /* CONFIG_PID_NS */
  148. static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
  149. {
  150. struct proc_fs_context *ctx = fc->fs_private;
  151. struct fs_parse_result result;
  152. int opt, err;
  153. opt = fs_parse(fc, proc_fs_parameters, param, &result);
  154. if (opt < 0)
  155. return opt;
  156. switch (opt) {
  157. case Opt_gid:
  158. ctx->gid = result.uint_32;
  159. break;
  160. case Opt_hidepid:
  161. err = proc_parse_hidepid_param(fc, param);
  162. if (err)
  163. return err;
  164. break;
  165. case Opt_subset:
  166. err = proc_parse_subset_param(fc, param->string);
  167. if (err)
  168. return err;
  169. break;
  170. case Opt_pidns:
  171. #ifdef CONFIG_PID_NS
  172. /*
  173. * We would have to RCU-protect every proc_pid_ns() or
  174. * proc_sb_info() access if we allowed this to be reconfigured
  175. * for an existing procfs instance. Luckily, procfs instances
  176. * are cheap to create, and mount-beneath would let you
  177. * atomically replace an instance even with overmounts.
  178. */
  179. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  180. errorfc(fc, "cannot reconfigure pidns for existing procfs");
  181. return -EBUSY;
  182. }
  183. err = proc_parse_pidns_param(fc, param, &result);
  184. if (err)
  185. return err;
  186. break;
  187. #else
  188. errorfc(fc, "pidns mount flag not supported on this system");
  189. return -EOPNOTSUPP;
  190. #endif
  191. default:
  192. return -EINVAL;
  193. }
  194. ctx->mask |= 1 << opt;
  195. return 0;
  196. }
  197. static void proc_apply_options(struct proc_fs_info *fs_info,
  198. struct fs_context *fc,
  199. struct user_namespace *user_ns)
  200. {
  201. struct proc_fs_context *ctx = fc->fs_private;
  202. if (ctx->mask & (1 << Opt_gid))
  203. fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
  204. if (ctx->mask & (1 << Opt_hidepid))
  205. fs_info->hide_pid = ctx->hidepid;
  206. if (ctx->mask & (1 << Opt_subset))
  207. fs_info->pidonly = ctx->pidonly;
  208. if (ctx->mask & (1 << Opt_pidns) &&
  209. !WARN_ON_ONCE(fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)) {
  210. put_pid_ns(fs_info->pid_ns);
  211. fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
  212. }
  213. }
  214. static int proc_fill_super(struct super_block *s, struct fs_context *fc)
  215. {
  216. struct proc_fs_context *ctx = fc->fs_private;
  217. struct inode *root_inode;
  218. struct proc_fs_info *fs_info;
  219. int ret;
  220. fs_info = kzalloc_obj(*fs_info);
  221. if (!fs_info)
  222. return -ENOMEM;
  223. fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
  224. proc_apply_options(fs_info, fc, current_user_ns());
  225. /* User space would break if executables or devices appear on proc */
  226. s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
  227. s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
  228. s->s_blocksize = 1024;
  229. s->s_blocksize_bits = 10;
  230. s->s_magic = PROC_SUPER_MAGIC;
  231. s->s_op = &proc_sops;
  232. s->s_time_gran = 1;
  233. s->s_fs_info = fs_info;
  234. /*
  235. * procfs isn't actually a stacking filesystem; however, there is
  236. * too much magic going on inside it to permit stacking things on
  237. * top of it
  238. */
  239. s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  240. /* procfs dentries and inodes don't require IO to create */
  241. s->s_shrink->seeks = 0;
  242. pde_get(&proc_root);
  243. root_inode = proc_get_inode(s, &proc_root);
  244. if (!root_inode) {
  245. pr_err("proc_fill_super: get root inode failed\n");
  246. return -ENOMEM;
  247. }
  248. s->s_root = d_make_root(root_inode);
  249. if (!s->s_root) {
  250. pr_err("proc_fill_super: allocate dentry failed\n");
  251. return -ENOMEM;
  252. }
  253. ret = proc_setup_self(s);
  254. if (ret) {
  255. return ret;
  256. }
  257. return proc_setup_thread_self(s);
  258. }
  259. static int proc_reconfigure(struct fs_context *fc)
  260. {
  261. struct super_block *sb = fc->root->d_sb;
  262. struct proc_fs_info *fs_info = proc_sb_info(sb);
  263. sync_filesystem(sb);
  264. proc_apply_options(fs_info, fc, current_user_ns());
  265. return 0;
  266. }
  267. static int proc_get_tree(struct fs_context *fc)
  268. {
  269. return get_tree_nodev(fc, proc_fill_super);
  270. }
  271. static void proc_fs_context_free(struct fs_context *fc)
  272. {
  273. struct proc_fs_context *ctx = fc->fs_private;
  274. put_pid_ns(ctx->pid_ns);
  275. kfree(ctx);
  276. }
  277. static const struct fs_context_operations proc_fs_context_ops = {
  278. .free = proc_fs_context_free,
  279. .parse_param = proc_parse_param,
  280. .get_tree = proc_get_tree,
  281. .reconfigure = proc_reconfigure,
  282. };
  283. static int proc_init_fs_context(struct fs_context *fc)
  284. {
  285. struct proc_fs_context *ctx;
  286. ctx = kzalloc_obj(struct proc_fs_context);
  287. if (!ctx)
  288. return -ENOMEM;
  289. ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
  290. put_user_ns(fc->user_ns);
  291. fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
  292. fc->fs_private = ctx;
  293. fc->ops = &proc_fs_context_ops;
  294. return 0;
  295. }
  296. static void proc_kill_sb(struct super_block *sb)
  297. {
  298. struct proc_fs_info *fs_info = proc_sb_info(sb);
  299. kill_anon_super(sb);
  300. if (fs_info) {
  301. put_pid_ns(fs_info->pid_ns);
  302. kfree_rcu(fs_info, rcu);
  303. }
  304. }
  305. static struct file_system_type proc_fs_type = {
  306. .name = "proc",
  307. .init_fs_context = proc_init_fs_context,
  308. .parameters = proc_fs_parameters,
  309. .kill_sb = proc_kill_sb,
  310. .fs_flags = FS_USERNS_MOUNT | FS_DISALLOW_NOTIFY_PERM,
  311. };
  312. void __init proc_root_init(void)
  313. {
  314. proc_init_kmemcache();
  315. set_proc_pid_nlink();
  316. proc_self_init();
  317. proc_thread_self_init();
  318. proc_symlink("mounts", NULL, "self/mounts");
  319. proc_net_init();
  320. proc_mkdir("fs", NULL);
  321. proc_mkdir("driver", NULL);
  322. proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
  323. #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  324. /* just give it a mountpoint */
  325. proc_create_mount_point("openprom");
  326. #endif
  327. proc_tty_init();
  328. proc_mkdir("bus", NULL);
  329. proc_sys_init();
  330. /*
  331. * Last things last. It is not like userspace processes eager
  332. * to open /proc files exist at this point but register last
  333. * anyway.
  334. */
  335. register_filesystem(&proc_fs_type);
  336. }
  337. static int proc_root_getattr(struct mnt_idmap *idmap,
  338. const struct path *path, struct kstat *stat,
  339. u32 request_mask, unsigned int query_flags)
  340. {
  341. generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry),
  342. stat);
  343. stat->nlink = proc_root.nlink + nr_processes();
  344. return 0;
  345. }
  346. static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
  347. {
  348. if (!proc_pid_lookup(dentry, flags))
  349. return NULL;
  350. return proc_lookup(dir, dentry, flags);
  351. }
  352. static int proc_root_readdir(struct file *file, struct dir_context *ctx)
  353. {
  354. if (ctx->pos < FIRST_PROCESS_ENTRY) {
  355. int error = proc_readdir(file, ctx);
  356. if (unlikely(error <= 0))
  357. return error;
  358. ctx->pos = FIRST_PROCESS_ENTRY;
  359. }
  360. return proc_pid_readdir(file, ctx);
  361. }
  362. /*
  363. * The root /proc directory is special, as it has the
  364. * <pid> directories. Thus we don't use the generic
  365. * directory handling functions for that..
  366. */
  367. static const struct file_operations proc_root_operations = {
  368. .read = generic_read_dir,
  369. .iterate_shared = proc_root_readdir,
  370. .llseek = generic_file_llseek,
  371. };
  372. /*
  373. * proc root can do almost nothing..
  374. */
  375. static const struct inode_operations proc_root_inode_operations = {
  376. .lookup = proc_root_lookup,
  377. .getattr = proc_root_getattr,
  378. };
  379. /*
  380. * This is the root "inode" in the /proc tree..
  381. */
  382. struct proc_dir_entry proc_root = {
  383. .low_ino = PROCFS_ROOT_INO,
  384. .namelen = 5,
  385. .mode = S_IFDIR | S_IRUGO | S_IXUGO,
  386. .nlink = 2,
  387. .refcnt = REFCOUNT_INIT(1),
  388. .proc_iops = &proc_root_inode_operations,
  389. .proc_dir_ops = &proc_root_operations,
  390. .parent = &proc_root,
  391. .subdir = RB_ROOT,
  392. .name = "/proc",
  393. };