proc_namespace.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * fs/proc_namespace.c - handling of /proc/<pid>/{mounts,mountinfo,mountstats}
  3. *
  4. * In fact, that's a piece of procfs; it's *almost* isolated from
  5. * the rest of fs/proc, but has rather close relationships with
  6. * fs/namespace.c, thus here instead of fs/proc
  7. *
  8. */
  9. #include <linux/mnt_namespace.h>
  10. #include <linux/nsproxy.h>
  11. #include <linux/security.h>
  12. #include <linux/fs_struct.h>
  13. #include "proc/internal.h" /* only for get_proc_task() in ->open() */
  14. #include "pnode.h"
  15. #include "internal.h"
  16. static unsigned mounts_poll(struct file *file, poll_table *wait)
  17. {
  18. struct seq_file *m = file->private_data;
  19. struct proc_mounts *p = m->private;
  20. struct mnt_namespace *ns = p->ns;
  21. unsigned res = POLLIN | POLLRDNORM;
  22. poll_wait(file, &p->ns->poll, wait);
  23. br_read_lock(&vfsmount_lock);
  24. if (m->poll_event != ns->event) {
  25. m->poll_event = ns->event;
  26. res |= POLLERR | POLLPRI;
  27. }
  28. br_read_unlock(&vfsmount_lock);
  29. return res;
  30. }
  31. struct proc_fs_info {
  32. int flag;
  33. const char *str;
  34. };
  35. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  36. {
  37. static const struct proc_fs_info fs_info[] = {
  38. { MS_SYNCHRONOUS, ",sync" },
  39. { MS_DIRSYNC, ",dirsync" },
  40. { MS_MANDLOCK, ",mand" },
  41. { 0, NULL }
  42. };
  43. const struct proc_fs_info *fs_infop;
  44. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  45. if (sb->s_flags & fs_infop->flag)
  46. seq_puts(m, fs_infop->str);
  47. }
  48. return security_sb_show_options(m, sb);
  49. }
  50. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  51. {
  52. static const struct proc_fs_info mnt_info[] = {
  53. { MNT_NOSUID, ",nosuid" },
  54. { MNT_NODEV, ",nodev" },
  55. { MNT_NOEXEC, ",noexec" },
  56. { MNT_NOATIME, ",noatime" },
  57. { MNT_NODIRATIME, ",nodiratime" },
  58. { MNT_RELATIME, ",relatime" },
  59. { 0, NULL }
  60. };
  61. const struct proc_fs_info *fs_infop;
  62. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  63. if (mnt->mnt_flags & fs_infop->flag)
  64. seq_puts(m, fs_infop->str);
  65. }
  66. }
  67. static inline void mangle(struct seq_file *m, const char *s)
  68. {
  69. seq_escape(m, s, " \t\n\\");
  70. }
  71. static void show_type(struct seq_file *m, struct super_block *sb)
  72. {
  73. mangle(m, sb->s_type->name);
  74. if (sb->s_subtype && sb->s_subtype[0]) {
  75. seq_putc(m, '.');
  76. mangle(m, sb->s_subtype);
  77. }
  78. }
  79. static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
  80. {
  81. struct proc_mounts *p = m->private;
  82. struct mount *r = real_mount(mnt);
  83. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  84. struct super_block *sb = mnt_path.dentry->d_sb;
  85. int err;
  86. if (sb->s_op->show_devname) {
  87. err = sb->s_op->show_devname(m, mnt_path.dentry);
  88. if (err)
  89. goto out;
  90. } else {
  91. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  92. }
  93. seq_putc(m, ' ');
  94. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  95. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  96. if (err)
  97. goto out;
  98. seq_putc(m, ' ');
  99. show_type(m, sb);
  100. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  101. err = show_sb_opts(m, sb);
  102. if (err)
  103. goto out;
  104. show_mnt_opts(m, mnt);
  105. if (sb->s_op->show_options2)
  106. err = sb->s_op->show_options2(mnt, m, mnt_path.dentry);
  107. else if (sb->s_op->show_options)
  108. err = sb->s_op->show_options(m, mnt_path.dentry);
  109. seq_puts(m, " 0 0\n");
  110. out:
  111. return err;
  112. }
  113. static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
  114. {
  115. struct proc_mounts *p = m->private;
  116. struct mount *r = real_mount(mnt);
  117. struct super_block *sb = mnt->mnt_sb;
  118. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  119. int err;
  120. seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
  121. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  122. if (sb->s_op->show_path) {
  123. err = sb->s_op->show_path(m, mnt->mnt_root);
  124. if (err)
  125. goto out;
  126. } else {
  127. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  128. }
  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_mnt_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->s_flags & MS_RDONLY ? " ro" : " rw");
  160. err = show_sb_opts(m, sb);
  161. if (err)
  162. goto out;
  163. if (sb->s_op->show_options2) {
  164. err = sb->s_op->show_options2(mnt, m, mnt->mnt_root);
  165. } else 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) {
  225. rcu_read_unlock();
  226. put_task_struct(task);
  227. goto err;
  228. }
  229. ns = nsp->mnt_ns;
  230. if (!ns) {
  231. rcu_read_unlock();
  232. put_task_struct(task);
  233. goto err;
  234. }
  235. get_mnt_ns(ns);
  236. if (!task->fs) {
  237. task_unlock(task);
  238. put_task_struct(task);
  239. ret = -ENOENT;
  240. goto err_put_ns;
  241. }
  242. get_fs_root(task->fs, &root);
  243. task_unlock(task);
  244. put_task_struct(task);
  245. ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
  246. if (ret)
  247. goto err_put_path;
  248. m = file->private_data;
  249. m->poll_event = ns->event;
  250. p = m->private;
  251. p->ns = ns;
  252. p->root = root;
  253. p->show = show;
  254. return 0;
  255. err_put_path:
  256. path_put(&root);
  257. err_put_ns:
  258. put_mnt_ns(ns);
  259. err:
  260. return ret;
  261. }
  262. static int mounts_release(struct inode *inode, struct file *file)
  263. {
  264. struct seq_file *m = file->private_data;
  265. struct proc_mounts *p = m->private;
  266. path_put(&p->root);
  267. put_mnt_ns(p->ns);
  268. return seq_release_private(inode, file);
  269. }
  270. static int mounts_open(struct inode *inode, struct file *file)
  271. {
  272. return mounts_open_common(inode, file, show_vfsmnt);
  273. }
  274. static int mountinfo_open(struct inode *inode, struct file *file)
  275. {
  276. return mounts_open_common(inode, file, show_mountinfo);
  277. }
  278. static int mountstats_open(struct inode *inode, struct file *file)
  279. {
  280. return mounts_open_common(inode, file, show_vfsstat);
  281. }
  282. const struct file_operations proc_mounts_operations = {
  283. .open = mounts_open,
  284. .read = seq_read,
  285. .llseek = seq_lseek,
  286. .release = mounts_release,
  287. .poll = mounts_poll,
  288. };
  289. const struct file_operations proc_mountinfo_operations = {
  290. .open = mountinfo_open,
  291. .read = seq_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 = seq_read,
  299. .llseek = seq_lseek,
  300. .release = mounts_release,
  301. };