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