proc_namespace.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_mnt_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. }
  71. static inline void mangle(struct seq_file *m, const char *s)
  72. {
  73. seq_escape(m, s, " \t\n\\");
  74. }
  75. static void show_type(struct seq_file *m, struct super_block *sb)
  76. {
  77. mangle(m, sb->s_type->name);
  78. if (sb->s_subtype) {
  79. seq_putc(m, '.');
  80. mangle(m, sb->s_subtype);
  81. }
  82. }
  83. static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
  84. {
  85. struct proc_mounts *p = m->private;
  86. struct mount *r = real_mount(mnt);
  87. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  88. struct super_block *sb = mnt_path.dentry->d_sb;
  89. int err;
  90. if (sb->s_op->show_devname) {
  91. err = sb->s_op->show_devname(m, mnt_path.dentry);
  92. if (err)
  93. goto out;
  94. } else {
  95. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  96. }
  97. seq_putc(m, ' ');
  98. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  99. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  100. if (err)
  101. goto out;
  102. seq_putc(m, ' ');
  103. show_type(m, sb);
  104. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  105. err = show_sb_opts(m, sb);
  106. if (err)
  107. goto out;
  108. show_mnt_opts(m, mnt);
  109. if (sb->s_op->show_options)
  110. err = sb->s_op->show_options(m, mnt_path.dentry);
  111. seq_puts(m, " 0 0\n");
  112. out:
  113. return err;
  114. }
  115. static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
  116. {
  117. struct proc_mounts *p = m->private;
  118. struct mount *r = real_mount(mnt);
  119. struct super_block *sb = mnt->mnt_sb;
  120. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  121. int err;
  122. seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
  123. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  124. if (sb->s_op->show_path) {
  125. err = sb->s_op->show_path(m, mnt->mnt_root);
  126. if (err)
  127. goto out;
  128. } else {
  129. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  130. }
  131. seq_putc(m, ' ');
  132. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  133. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  134. if (err)
  135. goto out;
  136. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  137. show_mnt_opts(m, mnt);
  138. /* Tagged fields ("foo:X" or "bar") */
  139. if (IS_MNT_SHARED(r))
  140. seq_printf(m, " shared:%i", r->mnt_group_id);
  141. if (IS_MNT_SLAVE(r)) {
  142. int master = r->mnt_master->mnt_group_id;
  143. int dom = get_dominating_id(r, &p->root);
  144. seq_printf(m, " master:%i", master);
  145. if (dom && dom != master)
  146. seq_printf(m, " propagate_from:%i", dom);
  147. }
  148. if (IS_MNT_UNBINDABLE(r))
  149. seq_puts(m, " unbindable");
  150. /* Filesystem specific data */
  151. seq_puts(m, " - ");
  152. show_type(m, sb);
  153. seq_putc(m, ' ');
  154. if (sb->s_op->show_devname) {
  155. err = sb->s_op->show_devname(m, mnt->mnt_root);
  156. if (err)
  157. goto out;
  158. } else {
  159. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  160. }
  161. seq_puts(m, sb_rdonly(sb) ? " ro" : " rw");
  162. err = show_sb_opts(m, sb);
  163. if (err)
  164. goto out;
  165. if (sb->s_op->show_options)
  166. err = sb->s_op->show_options(m, mnt->mnt_root);
  167. seq_putc(m, '\n');
  168. out:
  169. return err;
  170. }
  171. static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
  172. {
  173. struct proc_mounts *p = m->private;
  174. struct mount *r = real_mount(mnt);
  175. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  176. struct super_block *sb = mnt_path.dentry->d_sb;
  177. int err;
  178. /* device */
  179. if (sb->s_op->show_devname) {
  180. seq_puts(m, "device ");
  181. err = sb->s_op->show_devname(m, mnt_path.dentry);
  182. if (err)
  183. goto out;
  184. } else {
  185. if (r->mnt_devname) {
  186. seq_puts(m, "device ");
  187. mangle(m, r->mnt_devname);
  188. } else
  189. seq_puts(m, "no device");
  190. }
  191. /* mount point */
  192. seq_puts(m, " mounted on ");
  193. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  194. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  195. if (err)
  196. goto out;
  197. seq_putc(m, ' ');
  198. /* file system type */
  199. seq_puts(m, "with fstype ");
  200. show_type(m, sb);
  201. /* optional statistics */
  202. if (sb->s_op->show_stats) {
  203. seq_putc(m, ' ');
  204. err = sb->s_op->show_stats(m, mnt_path.dentry);
  205. }
  206. seq_putc(m, '\n');
  207. out:
  208. return err;
  209. }
  210. static int mounts_open_common(struct inode *inode, struct file *file,
  211. int (*show)(struct seq_file *, struct vfsmount *))
  212. {
  213. struct task_struct *task = get_proc_task(inode);
  214. struct nsproxy *nsp;
  215. struct mnt_namespace *ns = NULL;
  216. struct path root;
  217. struct proc_mounts *p;
  218. struct seq_file *m;
  219. int ret = -EINVAL;
  220. if (!task)
  221. goto err;
  222. task_lock(task);
  223. nsp = task->nsproxy;
  224. if (!nsp || !nsp->mnt_ns) {
  225. task_unlock(task);
  226. put_task_struct(task);
  227. goto err;
  228. }
  229. ns = nsp->mnt_ns;
  230. get_mnt_ns(ns);
  231. if (!task->fs) {
  232. task_unlock(task);
  233. put_task_struct(task);
  234. ret = -ENOENT;
  235. goto err_put_ns;
  236. }
  237. get_fs_root(task->fs, &root);
  238. task_unlock(task);
  239. put_task_struct(task);
  240. ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
  241. if (ret)
  242. goto err_put_path;
  243. m = file->private_data;
  244. m->poll_event = ns->event;
  245. p = m->private;
  246. p->ns = ns;
  247. p->root = root;
  248. p->show = show;
  249. INIT_LIST_HEAD(&p->cursor.mnt_list);
  250. p->cursor.mnt.mnt_flags = MNT_CURSOR;
  251. return 0;
  252. err_put_path:
  253. path_put(&root);
  254. err_put_ns:
  255. put_mnt_ns(ns);
  256. err:
  257. return ret;
  258. }
  259. static int mounts_release(struct inode *inode, struct file *file)
  260. {
  261. struct seq_file *m = file->private_data;
  262. struct proc_mounts *p = m->private;
  263. path_put(&p->root);
  264. mnt_cursor_del(p->ns, &p->cursor);
  265. put_mnt_ns(p->ns);
  266. return seq_release_private(inode, file);
  267. }
  268. static int mounts_open(struct inode *inode, struct file *file)
  269. {
  270. return mounts_open_common(inode, file, show_vfsmnt);
  271. }
  272. static int mountinfo_open(struct inode *inode, struct file *file)
  273. {
  274. return mounts_open_common(inode, file, show_mountinfo);
  275. }
  276. static int mountstats_open(struct inode *inode, struct file *file)
  277. {
  278. return mounts_open_common(inode, file, show_vfsstat);
  279. }
  280. const struct file_operations proc_mounts_operations = {
  281. .open = mounts_open,
  282. .read_iter = seq_read_iter,
  283. .splice_read = generic_file_splice_read,
  284. .llseek = seq_lseek,
  285. .release = mounts_release,
  286. .poll = mounts_poll,
  287. };
  288. const struct file_operations proc_mountinfo_operations = {
  289. .open = mountinfo_open,
  290. .read_iter = seq_read_iter,
  291. .splice_read = generic_file_splice_read,
  292. .llseek = seq_lseek,
  293. .release = mounts_release,
  294. .poll = mounts_poll,
  295. };
  296. const struct file_operations proc_mountstats_operations = {
  297. .open = mountstats_open,
  298. .read_iter = seq_read_iter,
  299. .splice_read = generic_file_splice_read,
  300. .llseek = seq_lseek,
  301. .release = mounts_release,
  302. };