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 #include <linux/errno.h>
10 #include <linux/time.h>
11 #include <linux/proc_fs.h>
12 #include <linux/stat.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/sched/stat.h>
16 #include <linux/module.h>
17 #include <linux/bitops.h>
18 #include <linux/user_namespace.h>
19 #include <linux/fs_context.h>
20 #include <linux/mount.h>
21 #include <linux/pid_namespace.h>
22 #include <linux/fs_parser.h>
23 #include <linux/cred.h>
24 #include <linux/magic.h>
25 #include <linux/slab.h>
26
27 #include "internal.h"
28
29 struct proc_fs_context {
30 struct pid_namespace *pid_ns;
31 unsigned int mask;
32 enum proc_hidepid hidepid;
33 int gid;
34 enum proc_pidonly pidonly;
35 };
36
37 enum proc_param {
38 Opt_gid,
39 Opt_hidepid,
40 Opt_subset,
41 Opt_pidns,
42 };
43
44 static const struct fs_parameter_spec proc_fs_parameters[] = {
45 fsparam_u32("gid", Opt_gid),
46 fsparam_string("hidepid", Opt_hidepid),
47 fsparam_string("subset", Opt_subset),
48 fsparam_file_or_string("pidns", Opt_pidns),
49 {}
50 };
51
valid_hidepid(unsigned int value)52 static inline int valid_hidepid(unsigned int value)
53 {
54 return (value == HIDEPID_OFF ||
55 value == HIDEPID_NO_ACCESS ||
56 value == HIDEPID_INVISIBLE ||
57 value == HIDEPID_NOT_PTRACEABLE);
58 }
59
proc_parse_hidepid_param(struct fs_context * fc,struct fs_parameter * param)60 static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param)
61 {
62 struct proc_fs_context *ctx = fc->fs_private;
63 struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid);
64 struct fs_parse_result result;
65 int base = (unsigned long)hidepid_u32_spec.data;
66
67 if (param->type != fs_value_is_string)
68 return invalf(fc, "proc: unexpected type of hidepid value\n");
69
70 if (!kstrtouint(param->string, base, &result.uint_32)) {
71 if (!valid_hidepid(result.uint_32))
72 return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
73 ctx->hidepid = result.uint_32;
74 return 0;
75 }
76
77 if (!strcmp(param->string, "off"))
78 ctx->hidepid = HIDEPID_OFF;
79 else if (!strcmp(param->string, "noaccess"))
80 ctx->hidepid = HIDEPID_NO_ACCESS;
81 else if (!strcmp(param->string, "invisible"))
82 ctx->hidepid = HIDEPID_INVISIBLE;
83 else if (!strcmp(param->string, "ptraceable"))
84 ctx->hidepid = HIDEPID_NOT_PTRACEABLE;
85 else
86 return invalf(fc, "proc: unknown value of hidepid - %s\n", param->string);
87
88 return 0;
89 }
90
proc_parse_subset_param(struct fs_context * fc,char * value)91 static int proc_parse_subset_param(struct fs_context *fc, char *value)
92 {
93 struct proc_fs_context *ctx = fc->fs_private;
94
95 while (value) {
96 char *ptr = strchr(value, ',');
97
98 if (ptr != NULL)
99 *ptr++ = '\0';
100
101 if (*value != '\0') {
102 if (!strcmp(value, "pid")) {
103 ctx->pidonly = PROC_PIDONLY_ON;
104 } else {
105 return invalf(fc, "proc: unsupported subset option - %s\n", value);
106 }
107 }
108 value = ptr;
109 }
110
111 return 0;
112 }
113
114 #ifdef CONFIG_PID_NS
proc_parse_pidns_param(struct fs_context * fc,struct fs_parameter * param,struct fs_parse_result * result)115 static int proc_parse_pidns_param(struct fs_context *fc,
116 struct fs_parameter *param,
117 struct fs_parse_result *result)
118 {
119 struct proc_fs_context *ctx = fc->fs_private;
120 struct pid_namespace *target, *active = task_active_pid_ns(current);
121 struct ns_common *ns;
122 struct file *ns_filp __free(fput) = NULL;
123
124 switch (param->type) {
125 case fs_value_is_file:
126 /* came through fsconfig, steal the file reference */
127 ns_filp = no_free_ptr(param->file);
128 break;
129 case fs_value_is_string:
130 ns_filp = filp_open(param->string, O_RDONLY, 0);
131 break;
132 default:
133 WARN_ON_ONCE(true);
134 break;
135 }
136 if (!ns_filp)
137 ns_filp = ERR_PTR(-EBADF);
138 if (IS_ERR(ns_filp)) {
139 errorfc(fc, "could not get file from pidns argument");
140 return PTR_ERR(ns_filp);
141 }
142
143 if (!proc_ns_file(ns_filp))
144 return invalfc(fc, "pidns argument is not an nsfs file");
145 ns = get_proc_ns(file_inode(ns_filp));
146 if (ns->ns_type != CLONE_NEWPID)
147 return invalfc(fc, "pidns argument is not a pidns file");
148 target = container_of(ns, struct pid_namespace, ns);
149
150 /*
151 * pidns= is shorthand for joining the pidns to get a fsopen fd, so the
152 * permission model should be the same as pidns_install().
153 */
154 if (!ns_capable(target->user_ns, CAP_SYS_ADMIN)) {
155 errorfc(fc, "insufficient permissions to set pidns");
156 return -EPERM;
157 }
158 if (!pidns_is_ancestor(target, active))
159 return invalfc(fc, "cannot set pidns to non-descendant pidns");
160
161 put_pid_ns(ctx->pid_ns);
162 ctx->pid_ns = get_pid_ns(target);
163 put_user_ns(fc->user_ns);
164 fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
165 return 0;
166 }
167 #endif /* CONFIG_PID_NS */
168
proc_parse_param(struct fs_context * fc,struct fs_parameter * param)169 static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
170 {
171 struct proc_fs_context *ctx = fc->fs_private;
172 struct fs_parse_result result;
173 int opt, err;
174
175 opt = fs_parse(fc, proc_fs_parameters, param, &result);
176 if (opt < 0)
177 return opt;
178
179 switch (opt) {
180 case Opt_gid:
181 ctx->gid = result.uint_32;
182 break;
183
184 case Opt_hidepid:
185 err = proc_parse_hidepid_param(fc, param);
186 if (err)
187 return err;
188 break;
189
190 case Opt_subset:
191 err = proc_parse_subset_param(fc, param->string);
192 if (err)
193 return err;
194 break;
195
196 case Opt_pidns:
197 #ifdef CONFIG_PID_NS
198 /*
199 * We would have to RCU-protect every proc_pid_ns() or
200 * proc_sb_info() access if we allowed this to be reconfigured
201 * for an existing procfs instance. Luckily, procfs instances
202 * are cheap to create, and mount-beneath would let you
203 * atomically replace an instance even with overmounts.
204 */
205 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
206 errorfc(fc, "cannot reconfigure pidns for existing procfs");
207 return -EBUSY;
208 }
209 err = proc_parse_pidns_param(fc, param, &result);
210 if (err)
211 return err;
212 break;
213 #else
214 errorfc(fc, "pidns mount flag not supported on this system");
215 return -EOPNOTSUPP;
216 #endif
217
218 default:
219 return -EINVAL;
220 }
221
222 ctx->mask |= 1 << opt;
223 return 0;
224 }
225
proc_apply_options(struct proc_fs_info * fs_info,struct fs_context * fc,struct user_namespace * user_ns)226 static void proc_apply_options(struct proc_fs_info *fs_info,
227 struct fs_context *fc,
228 struct user_namespace *user_ns)
229 {
230 struct proc_fs_context *ctx = fc->fs_private;
231
232 if (ctx->mask & (1 << Opt_gid))
233 fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
234 if (ctx->mask & (1 << Opt_hidepid))
235 fs_info->hide_pid = ctx->hidepid;
236 if (ctx->mask & (1 << Opt_subset))
237 fs_info->pidonly = ctx->pidonly;
238 if (ctx->mask & (1 << Opt_pidns) &&
239 !WARN_ON_ONCE(fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)) {
240 put_pid_ns(fs_info->pid_ns);
241 fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
242 }
243 }
244
proc_fill_super(struct super_block * s,struct fs_context * fc)245 static int proc_fill_super(struct super_block *s, struct fs_context *fc)
246 {
247 struct proc_fs_context *ctx = fc->fs_private;
248 struct inode *root_inode;
249 struct proc_fs_info *fs_info;
250 int ret;
251
252 fs_info = kzalloc(sizeof(*fs_info), GFP_KERNEL);
253 if (!fs_info)
254 return -ENOMEM;
255
256 fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
257 proc_apply_options(fs_info, fc, current_user_ns());
258
259 /* User space would break if executables or devices appear on proc */
260 s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
261 s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
262 s->s_blocksize = 1024;
263 s->s_blocksize_bits = 10;
264 s->s_magic = PROC_SUPER_MAGIC;
265 s->s_op = &proc_sops;
266 s->s_time_gran = 1;
267 s->s_fs_info = fs_info;
268
269 /*
270 * procfs isn't actually a stacking filesystem; however, there is
271 * too much magic going on inside it to permit stacking things on
272 * top of it
273 */
274 s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
275
276 /* procfs dentries and inodes don't require IO to create */
277 s->s_shrink->seeks = 0;
278
279 pde_get(&proc_root);
280 root_inode = proc_get_inode(s, &proc_root);
281 if (!root_inode) {
282 pr_err("proc_fill_super: get root inode failed\n");
283 return -ENOMEM;
284 }
285
286 s->s_root = d_make_root(root_inode);
287 if (!s->s_root) {
288 pr_err("proc_fill_super: allocate dentry failed\n");
289 return -ENOMEM;
290 }
291
292 ret = proc_setup_self(s);
293 if (ret) {
294 return ret;
295 }
296 return proc_setup_thread_self(s);
297 }
298
proc_reconfigure(struct fs_context * fc)299 static int proc_reconfigure(struct fs_context *fc)
300 {
301 struct super_block *sb = fc->root->d_sb;
302 struct proc_fs_info *fs_info = proc_sb_info(sb);
303
304 sync_filesystem(sb);
305
306 proc_apply_options(fs_info, fc, current_user_ns());
307 return 0;
308 }
309
proc_get_tree(struct fs_context * fc)310 static int proc_get_tree(struct fs_context *fc)
311 {
312 return get_tree_nodev(fc, proc_fill_super);
313 }
314
proc_fs_context_free(struct fs_context * fc)315 static void proc_fs_context_free(struct fs_context *fc)
316 {
317 struct proc_fs_context *ctx = fc->fs_private;
318
319 put_pid_ns(ctx->pid_ns);
320 kfree(ctx);
321 }
322
323 static const struct fs_context_operations proc_fs_context_ops = {
324 .free = proc_fs_context_free,
325 .parse_param = proc_parse_param,
326 .get_tree = proc_get_tree,
327 .reconfigure = proc_reconfigure,
328 };
329
proc_init_fs_context(struct fs_context * fc)330 static int proc_init_fs_context(struct fs_context *fc)
331 {
332 struct proc_fs_context *ctx;
333
334 ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL);
335 if (!ctx)
336 return -ENOMEM;
337
338 ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
339 put_user_ns(fc->user_ns);
340 fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
341 fc->fs_private = ctx;
342 fc->ops = &proc_fs_context_ops;
343 return 0;
344 }
345
proc_kill_sb(struct super_block * sb)346 static void proc_kill_sb(struct super_block *sb)
347 {
348 struct proc_fs_info *fs_info = proc_sb_info(sb);
349
350 if (!fs_info) {
351 kill_anon_super(sb);
352 return;
353 }
354
355 dput(fs_info->proc_self);
356 dput(fs_info->proc_thread_self);
357
358 kill_anon_super(sb);
359 put_pid_ns(fs_info->pid_ns);
360 kfree_rcu(fs_info, rcu);
361 }
362
363 static struct file_system_type proc_fs_type = {
364 .name = "proc",
365 .init_fs_context = proc_init_fs_context,
366 .parameters = proc_fs_parameters,
367 .kill_sb = proc_kill_sb,
368 .fs_flags = FS_USERNS_MOUNT | FS_DISALLOW_NOTIFY_PERM,
369 };
370
proc_root_init(void)371 void __init proc_root_init(void)
372 {
373 proc_init_kmemcache();
374 set_proc_pid_nlink();
375 proc_self_init();
376 proc_thread_self_init();
377 proc_symlink("mounts", NULL, "self/mounts");
378
379 proc_net_init();
380 proc_mkdir("fs", NULL);
381 proc_mkdir("driver", NULL);
382 proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
383 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
384 /* just give it a mountpoint */
385 proc_create_mount_point("openprom");
386 #endif
387 proc_tty_init();
388 proc_mkdir("bus", NULL);
389 proc_sys_init();
390
391 /*
392 * Last things last. It is not like userspace processes eager
393 * to open /proc files exist at this point but register last
394 * anyway.
395 */
396 register_filesystem(&proc_fs_type);
397 }
398
proc_root_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)399 static int proc_root_getattr(struct mnt_idmap *idmap,
400 const struct path *path, struct kstat *stat,
401 u32 request_mask, unsigned int query_flags)
402 {
403 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry),
404 stat);
405 stat->nlink = proc_root.nlink + nr_processes();
406 return 0;
407 }
408
proc_root_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)409 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
410 {
411 if (!proc_pid_lookup(dentry, flags))
412 return NULL;
413
414 return proc_lookup(dir, dentry, flags);
415 }
416
proc_root_readdir(struct file * file,struct dir_context * ctx)417 static int proc_root_readdir(struct file *file, struct dir_context *ctx)
418 {
419 if (ctx->pos < FIRST_PROCESS_ENTRY) {
420 int error = proc_readdir(file, ctx);
421 if (unlikely(error <= 0))
422 return error;
423 ctx->pos = FIRST_PROCESS_ENTRY;
424 }
425
426 return proc_pid_readdir(file, ctx);
427 }
428
429 /*
430 * The root /proc directory is special, as it has the
431 * <pid> directories. Thus we don't use the generic
432 * directory handling functions for that..
433 */
434 static const struct file_operations proc_root_operations = {
435 .read = generic_read_dir,
436 .iterate_shared = proc_root_readdir,
437 .llseek = generic_file_llseek,
438 };
439
440 /*
441 * proc root can do almost nothing..
442 */
443 static const struct inode_operations proc_root_inode_operations = {
444 .lookup = proc_root_lookup,
445 .getattr = proc_root_getattr,
446 };
447
448 /*
449 * This is the root "inode" in the /proc tree..
450 */
451 struct proc_dir_entry proc_root = {
452 .low_ino = PROCFS_ROOT_INO,
453 .namelen = 5,
454 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
455 .nlink = 2,
456 .refcnt = REFCOUNT_INIT(1),
457 .proc_iops = &proc_root_inode_operations,
458 .proc_dir_ops = &proc_root_operations,
459 .parent = &proc_root,
460 .subdir = RB_ROOT,
461 .name = "/proc",
462 };
463