proc_namespace.c 7.8 KB

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