proc_namespace.c 8.7 KB

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