xref: /linux/fs/proc/root.c (revision 8f8d5745bb520c76b81abef4a2cb3023d0313bfd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/proc/root.c
4  *
5  *  Copyright (C) 1991, 1992 Linus Torvalds
6  *
7  *  proc root directory handling functions
8  */
9 
10 #include <linux/uaccess.h>
11 
12 #include <linux/errno.h>
13 #include <linux/time.h>
14 #include <linux/proc_fs.h>
15 #include <linux/stat.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/sched/stat.h>
19 #include <linux/module.h>
20 #include <linux/bitops.h>
21 #include <linux/user_namespace.h>
22 #include <linux/fs_context.h>
23 #include <linux/mount.h>
24 #include <linux/pid_namespace.h>
25 #include <linux/fs_parser.h>
26 #include <linux/cred.h>
27 #include <linux/magic.h>
28 #include <linux/slab.h>
29 
30 #include "internal.h"
31 
32 struct proc_fs_context {
33 	struct pid_namespace	*pid_ns;
34 	unsigned int		mask;
35 	int			hidepid;
36 	int			gid;
37 };
38 
39 enum proc_param {
40 	Opt_gid,
41 	Opt_hidepid,
42 };
43 
44 static const struct fs_parameter_spec proc_param_specs[] = {
45 	fsparam_u32("gid",	Opt_gid),
46 	fsparam_u32("hidepid",	Opt_hidepid),
47 	{}
48 };
49 
50 static const struct fs_parameter_description proc_fs_parameters = {
51 	.name		= "proc",
52 	.specs		= proc_param_specs,
53 };
54 
55 static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
56 {
57 	struct proc_fs_context *ctx = fc->fs_private;
58 	struct fs_parse_result result;
59 	int opt;
60 
61 	opt = fs_parse(fc, &proc_fs_parameters, param, &result);
62 	if (opt < 0)
63 		return opt;
64 
65 	switch (opt) {
66 	case Opt_gid:
67 		ctx->gid = result.uint_32;
68 		break;
69 
70 	case Opt_hidepid:
71 		ctx->hidepid = result.uint_32;
72 		if (ctx->hidepid < HIDEPID_OFF ||
73 		    ctx->hidepid > HIDEPID_INVISIBLE)
74 			return invalf(fc, "proc: hidepid value must be between 0 and 2.\n");
75 		break;
76 
77 	default:
78 		return -EINVAL;
79 	}
80 
81 	ctx->mask |= 1 << opt;
82 	return 0;
83 }
84 
85 static void proc_apply_options(struct super_block *s,
86 			       struct fs_context *fc,
87 			       struct pid_namespace *pid_ns,
88 			       struct user_namespace *user_ns)
89 {
90 	struct proc_fs_context *ctx = fc->fs_private;
91 
92 	if (ctx->mask & (1 << Opt_gid))
93 		pid_ns->pid_gid = make_kgid(user_ns, ctx->gid);
94 	if (ctx->mask & (1 << Opt_hidepid))
95 		pid_ns->hide_pid = ctx->hidepid;
96 }
97 
98 static int proc_fill_super(struct super_block *s, struct fs_context *fc)
99 {
100 	struct pid_namespace *pid_ns = get_pid_ns(s->s_fs_info);
101 	struct inode *root_inode;
102 	int ret;
103 
104 	proc_apply_options(s, fc, pid_ns, current_user_ns());
105 
106 	/* User space would break if executables or devices appear on proc */
107 	s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
108 	s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
109 	s->s_blocksize = 1024;
110 	s->s_blocksize_bits = 10;
111 	s->s_magic = PROC_SUPER_MAGIC;
112 	s->s_op = &proc_sops;
113 	s->s_time_gran = 1;
114 
115 	/*
116 	 * procfs isn't actually a stacking filesystem; however, there is
117 	 * too much magic going on inside it to permit stacking things on
118 	 * top of it
119 	 */
120 	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
121 
122 	/* procfs dentries and inodes don't require IO to create */
123 	s->s_shrink.seeks = 0;
124 
125 	pde_get(&proc_root);
126 	root_inode = proc_get_inode(s, &proc_root);
127 	if (!root_inode) {
128 		pr_err("proc_fill_super: get root inode failed\n");
129 		return -ENOMEM;
130 	}
131 
132 	s->s_root = d_make_root(root_inode);
133 	if (!s->s_root) {
134 		pr_err("proc_fill_super: allocate dentry failed\n");
135 		return -ENOMEM;
136 	}
137 
138 	ret = proc_setup_self(s);
139 	if (ret) {
140 		return ret;
141 	}
142 	return proc_setup_thread_self(s);
143 }
144 
145 static int proc_reconfigure(struct fs_context *fc)
146 {
147 	struct super_block *sb = fc->root->d_sb;
148 	struct pid_namespace *pid = sb->s_fs_info;
149 
150 	sync_filesystem(sb);
151 
152 	proc_apply_options(sb, fc, pid, current_user_ns());
153 	return 0;
154 }
155 
156 static int proc_get_tree(struct fs_context *fc)
157 {
158 	struct proc_fs_context *ctx = fc->fs_private;
159 
160 	put_user_ns(fc->user_ns);
161 	fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
162 	fc->s_fs_info = ctx->pid_ns;
163 	return vfs_get_super(fc, vfs_get_keyed_super, proc_fill_super);
164 }
165 
166 static void proc_fs_context_free(struct fs_context *fc)
167 {
168 	struct proc_fs_context *ctx = fc->fs_private;
169 
170 	if (ctx->pid_ns)
171 		put_pid_ns(ctx->pid_ns);
172 	kfree(ctx);
173 }
174 
175 static const struct fs_context_operations proc_fs_context_ops = {
176 	.free		= proc_fs_context_free,
177 	.parse_param	= proc_parse_param,
178 	.get_tree	= proc_get_tree,
179 	.reconfigure	= proc_reconfigure,
180 };
181 
182 static int proc_init_fs_context(struct fs_context *fc)
183 {
184 	struct proc_fs_context *ctx;
185 
186 	ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL);
187 	if (!ctx)
188 		return -ENOMEM;
189 
190 	ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
191 	fc->fs_private = ctx;
192 	fc->ops = &proc_fs_context_ops;
193 	return 0;
194 }
195 
196 static void proc_kill_sb(struct super_block *sb)
197 {
198 	struct pid_namespace *ns;
199 
200 	ns = (struct pid_namespace *)sb->s_fs_info;
201 	if (ns->proc_self)
202 		dput(ns->proc_self);
203 	if (ns->proc_thread_self)
204 		dput(ns->proc_thread_self);
205 	kill_anon_super(sb);
206 	put_pid_ns(ns);
207 }
208 
209 static struct file_system_type proc_fs_type = {
210 	.name			= "proc",
211 	.init_fs_context	= proc_init_fs_context,
212 	.parameters		= &proc_fs_parameters,
213 	.kill_sb		= proc_kill_sb,
214 	.fs_flags		= FS_USERNS_MOUNT,
215 };
216 
217 void __init proc_root_init(void)
218 {
219 	proc_init_kmemcache();
220 	set_proc_pid_nlink();
221 	proc_self_init();
222 	proc_thread_self_init();
223 	proc_symlink("mounts", NULL, "self/mounts");
224 
225 	proc_net_init();
226 	proc_mkdir("fs", NULL);
227 	proc_mkdir("driver", NULL);
228 	proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
229 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
230 	/* just give it a mountpoint */
231 	proc_create_mount_point("openprom");
232 #endif
233 	proc_tty_init();
234 	proc_mkdir("bus", NULL);
235 	proc_sys_init();
236 
237 	register_filesystem(&proc_fs_type);
238 }
239 
240 static int proc_root_getattr(const struct path *path, struct kstat *stat,
241 			     u32 request_mask, unsigned int query_flags)
242 {
243 	generic_fillattr(d_inode(path->dentry), stat);
244 	stat->nlink = proc_root.nlink + nr_processes();
245 	return 0;
246 }
247 
248 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
249 {
250 	if (!proc_pid_lookup(dentry, flags))
251 		return NULL;
252 
253 	return proc_lookup(dir, dentry, flags);
254 }
255 
256 static int proc_root_readdir(struct file *file, struct dir_context *ctx)
257 {
258 	if (ctx->pos < FIRST_PROCESS_ENTRY) {
259 		int error = proc_readdir(file, ctx);
260 		if (unlikely(error <= 0))
261 			return error;
262 		ctx->pos = FIRST_PROCESS_ENTRY;
263 	}
264 
265 	return proc_pid_readdir(file, ctx);
266 }
267 
268 /*
269  * The root /proc directory is special, as it has the
270  * <pid> directories. Thus we don't use the generic
271  * directory handling functions for that..
272  */
273 static const struct file_operations proc_root_operations = {
274 	.read		 = generic_read_dir,
275 	.iterate_shared	 = proc_root_readdir,
276 	.llseek		= generic_file_llseek,
277 };
278 
279 /*
280  * proc root can do almost nothing..
281  */
282 static const struct inode_operations proc_root_inode_operations = {
283 	.lookup		= proc_root_lookup,
284 	.getattr	= proc_root_getattr,
285 };
286 
287 /*
288  * This is the root "inode" in the /proc tree..
289  */
290 struct proc_dir_entry proc_root = {
291 	.low_ino	= PROC_ROOT_INO,
292 	.namelen	= 5,
293 	.mode		= S_IFDIR | S_IRUGO | S_IXUGO,
294 	.nlink		= 2,
295 	.refcnt		= REFCOUNT_INIT(1),
296 	.proc_iops	= &proc_root_inode_operations,
297 	.proc_fops	= &proc_root_operations,
298 	.parent		= &proc_root,
299 	.subdir		= RB_ROOT,
300 	.name		= "/proc",
301 };
302 
303 int pid_ns_prepare_proc(struct pid_namespace *ns)
304 {
305 	struct proc_fs_context *ctx;
306 	struct fs_context *fc;
307 	struct vfsmount *mnt;
308 
309 	fc = fs_context_for_mount(&proc_fs_type, SB_KERNMOUNT);
310 	if (IS_ERR(fc))
311 		return PTR_ERR(fc);
312 
313 	if (fc->user_ns != ns->user_ns) {
314 		put_user_ns(fc->user_ns);
315 		fc->user_ns = get_user_ns(ns->user_ns);
316 	}
317 
318 	ctx = fc->fs_private;
319 	if (ctx->pid_ns != ns) {
320 		put_pid_ns(ctx->pid_ns);
321 		get_pid_ns(ns);
322 		ctx->pid_ns = ns;
323 	}
324 
325 	mnt = fc_mount(fc);
326 	put_fs_context(fc);
327 	if (IS_ERR(mnt))
328 		return PTR_ERR(mnt);
329 
330 	ns->proc_mnt = mnt;
331 	return 0;
332 }
333 
334 void pid_ns_release_proc(struct pid_namespace *ns)
335 {
336 	kern_unmount(ns->proc_mnt);
337 }
338