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