proc_namespace.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/proc_namespace.c - handling of /proc/<pid>/{mounts,mountinfo,mountstats}
  4. *
  5. * In fact, that's a piece of procfs; it's *almost* isolated from
  6. * the rest of fs/proc, but has rather close relationships with
  7. * fs/namespace.c, thus here instead of fs/proc
  8. *
  9. */
  10. #include <linux/mnt_namespace.h>
  11. #include <linux/nsproxy.h>
  12. #include <linux/security.h>
  13. #include <linux/fs_struct.h>
  14. #include <linux/sched/task.h>
  15. #include "proc/internal.h" /* only for get_proc_task() in ->open() */
  16. #include "pnode.h"
  17. #include "internal.h"
  18. static __poll_t mounts_poll(struct file *file, poll_table *wait)
  19. {
  20. struct seq_file *m = file->private_data;
  21. struct proc_mounts *p = m->private;
  22. struct mnt_namespace *ns = p->ns;
  23. __poll_t res = EPOLLIN | EPOLLRDNORM;
  24. int event;
  25. poll_wait(file, &p->ns->poll, wait);
  26. event = READ_ONCE(ns->event);
  27. if (m->poll_event != event) {
  28. m->poll_event = event;
  29. res |= EPOLLERR | EPOLLPRI;
  30. }
  31. return res;
  32. }
  33. struct proc_fs_opts {
  34. int flag;
  35. const char *str;
  36. };
  37. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  38. {
  39. static const struct proc_fs_opts fs_opts[] = {
  40. { SB_SYNCHRONOUS, ",sync" },
  41. { SB_DIRSYNC, ",dirsync" },
  42. { SB_MANDLOCK, ",mand" },
  43. { SB_LAZYTIME, ",lazytime" },
  44. { 0, NULL }
  45. };
  46. const struct proc_fs_opts *fs_infop;
  47. for (fs_infop = fs_opts; fs_infop->flag; fs_infop++) {
  48. if (sb->s_flags & fs_infop->flag)
  49. seq_puts(m, fs_infop->str);
  50. }
  51. return security_sb_show_options(m, sb);
  52. }
  53. static void show_vfsmnt_opts(struct seq_file *m, struct vfsmount *mnt)
  54. {
  55. static const struct proc_fs_opts mnt_opts[] = {
  56. { MNT_NOSUID, ",nosuid" },
  57. { MNT_NODEV, ",nodev" },
  58. { MNT_NOEXEC, ",noexec" },
  59. { MNT_NOATIME, ",noatime" },
  60. { MNT_NODIRATIME, ",nodiratime" },
  61. { MNT_RELATIME, ",relatime" },
  62. { MNT_NOSYMFOLLOW, ",nosymfollow" },
  63. { 0, NULL }
  64. };
  65. const struct proc_fs_opts *fs_infop;
  66. for (fs_infop = mnt_opts; fs_infop->flag; fs_infop++) {
  67. if (mnt->mnt_flags & fs_infop->flag)
  68. seq_puts(m, fs_infop->str);
  69. }
  70. if (is_idmapped_mnt(mnt))
  71. seq_puts(m, ",idmapped");
  72. }
  73. static inline void mangle(struct seq_file *m, const char *s)
  74. {
  75. seq_escape(m, s, " \t\n\\#");
  76. }
  77. static void show_type(struct seq_file *m, struct super_block *sb)
  78. {
  79. mangle(m, sb->s_type->name);
  80. if (sb->s_subtype) {
  81. seq_putc(m, '.');
  82. mangle(m, sb->s_subtype);
  83. }
  84. }
  85. static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
  86. {
  87. struct proc_mounts *p = m->private;
  88. struct mount *r = real_mount(mnt);
  89. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  90. struct super_block *sb = mnt_path.dentry->d_sb;
  91. int err;
  92. if (sb->s_op->show_devname) {
  93. err = sb->s_op->show_devname(m, mnt_path.dentry);
  94. if (err)
  95. goto out;
  96. } else {
  97. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  98. }
  99. seq_putc(m, ' ');
  100. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  101. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  102. if (err)
  103. goto out;
  104. seq_putc(m, ' ');
  105. show_type(m, sb);
  106. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  107. err = show_sb_opts(m, sb);
  108. if (err)
  109. goto out;
  110. show_vfsmnt_opts(m, mnt);
  111. if (sb->s_op->show_options)
  112. err = sb->s_op->show_options(m, mnt_path.dentry);
  113. seq_puts(m, " 0 0\n");
  114. out:
  115. return err;
  116. }
  117. static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
  118. {
  119. struct proc_mounts *p = m->private;
  120. struct mount *r = real_mount(mnt);
  121. struct super_block *sb = mnt->mnt_sb;
  122. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  123. int err;
  124. seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
  125. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  126. err = show_path(m, mnt->mnt_root);
  127. if (err)
  128. goto out;
  129. seq_putc(m, ' ');
  130. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  131. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  132. if (err)
  133. goto out;
  134. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  135. show_vfsmnt_opts(m, mnt);
  136. /* Tagged fields ("foo:X" or "bar") */
  137. if (IS_MNT_SHARED(r))
  138. seq_printf(m, " shared:%i", r->mnt_group_id);
  139. if (IS_MNT_SLAVE(r)) {
  140. int master = r->mnt_master->mnt_group_id;
  141. int dom = get_dominating_id(r, &p->root);
  142. seq_printf(m, " master:%i", master);
  143. if (dom && dom != master)
  144. seq_printf(m, " propagate_from:%i", dom);
  145. }
  146. if (IS_MNT_UNBINDABLE(r))
  147. seq_puts(m, " unbindable");
  148. /* Filesystem specific data */
  149. seq_puts(m, " - ");
  150. show_type(m, sb);
  151. seq_putc(m, ' ');
  152. if (sb->s_op->show_devname) {
  153. err = sb->s_op->show_devname(m, mnt->mnt_root);
  154. if (err)
  155. goto out;
  156. } else {
  157. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  158. }
  159. seq_puts(m, sb_rdonly(sb) ? " ro" : " rw");
  160. err = show_sb_opts(m, sb);
  161. if (err)
  162. goto out;
  163. if (sb->s_op->show_options)
  164. err = sb->s_op->show_options(m, mnt->mnt_root);
  165. seq_putc(m, '\n');
  166. out:
  167. return err;
  168. }
  169. static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
  170. {
  171. struct proc_mounts *p = m->private;
  172. struct mount *r = real_mount(mnt);
  173. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  174. struct super_block *sb = mnt_path.dentry->d_sb;
  175. int err;
  176. /* device */
  177. if (sb->s_op->show_devname) {
  178. seq_puts(m, "device ");
  179. err = sb->s_op->show_devname(m, mnt_path.dentry);
  180. if (err)
  181. goto out;
  182. } else {
  183. if (r->mnt_devname) {
  184. seq_puts(m, "device ");
  185. mangle(m, r->mnt_devname);
  186. } else
  187. seq_puts(m, "no device");
  188. }
  189. /* mount point */
  190. seq_puts(m, " mounted on ");
  191. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  192. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  193. if (err)
  194. goto out;
  195. seq_putc(m, ' ');
  196. /* file system type */
  197. seq_puts(m, "with fstype ");
  198. show_type(m, sb);
  199. /* optional statistics */
  200. if (sb->s_op->show_stats) {
  201. seq_putc(m, ' ');
  202. err = sb->s_op->show_stats(m, mnt_path.dentry);
  203. }
  204. seq_putc(m, '\n');
  205. out:
  206. return err;
  207. }
  208. static int mounts_open_common(struct inode *inode, struct file *file,
  209. int (*show)(struct seq_file *, struct vfsmount *))
  210. {
  211. struct task_struct *task = get_proc_task(inode);
  212. struct nsproxy *nsp;
  213. struct mnt_namespace *ns = NULL;
  214. struct path root;
  215. struct proc_mounts *p;
  216. struct seq_file *m;
  217. int ret = -EINVAL;
  218. if (!task)
  219. goto err;
  220. task_lock(task);
  221. nsp = task->nsproxy;
  222. if (!nsp || !nsp->mnt_ns) {
  223. task_unlock(task);
  224. put_task_struct(task);
  225. goto err;
  226. }
  227. ns = nsp->mnt_ns;
  228. get_mnt_ns(ns);
  229. if (!task->fs) {
  230. task_unlock(task);
  231. put_task_struct(task);
  232. ret = -ENOENT;
  233. goto err_put_ns;
  234. }
  235. get_fs_root(task->fs, &root);
  236. task_unlock(task);
  237. put_task_struct(task);
  238. ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
  239. if (ret)
  240. goto err_put_path;
  241. m = file->private_data;
  242. m->poll_event = ns->event;
  243. p = m->private;
  244. p->ns = ns;
  245. p->root = root;
  246. p->show = show;
  247. return 0;
  248. err_put_path:
  249. path_put(&root);
  250. err_put_ns:
  251. put_mnt_ns(ns);
  252. err:
  253. return ret;
  254. }
  255. static int mounts_release(struct inode *inode, struct file *file)
  256. {
  257. struct seq_file *m = file->private_data;
  258. struct proc_mounts *p = m->private;
  259. path_put(&p->root);
  260. put_mnt_ns(p->ns);
  261. return seq_release_private(inode, file);
  262. }
  263. static int mounts_open(struct inode *inode, struct file *file)
  264. {
  265. return mounts_open_common(inode, file, show_vfsmnt);
  266. }
  267. static int mountinfo_open(struct inode *inode, struct file *file)
  268. {
  269. return mounts_open_common(inode, file, show_mountinfo);
  270. }
  271. static int mountstats_open(struct inode *inode, struct file *file)
  272. {
  273. return mounts_open_common(inode, file, show_vfsstat);
  274. }
  275. const struct file_operations proc_mounts_operations = {
  276. .open = mounts_open,
  277. .read_iter = seq_read_iter,
  278. .splice_read = copy_splice_read,
  279. .llseek = seq_lseek,
  280. .release = mounts_release,
  281. .poll = mounts_poll,
  282. };
  283. const struct file_operations proc_mountinfo_operations = {
  284. .open = mountinfo_open,
  285. .read_iter = seq_read_iter,
  286. .splice_read = copy_splice_read,
  287. .llseek = seq_lseek,
  288. .release = mounts_release,
  289. .poll = mounts_poll,
  290. };
  291. const struct file_operations proc_mountstats_operations = {
  292. .open = mountstats_open,
  293. .read_iter = seq_read_iter,
  294. .splice_read = copy_splice_read,
  295. .llseek = seq_lseek,
  296. .release = mounts_release,
  297. };