Blame view

fs/proc/root.c 8.69 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
  /*
   *  linux/fs/proc/root.c
   *
   *  Copyright (C) 1991, 1992 Linus Torvalds
   *
   *  proc root directory handling functions
   */
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
9
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
  
  #include <linux/errno.h>
  #include <linux/time.h>
  #include <linux/proc_fs.h>
  #include <linux/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/init.h>
914e26379   Al Viro   [PATCH] severing ...
16
  #include <linux/sched.h>
03441a348   Ingo Molnar   sched/headers: Pr...
17
  #include <linux/sched/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
  #include <linux/module.h>
  #include <linux/bitops.h>
87a8ebd63   Eric W. Biederman   userns: Restrict ...
20
  #include <linux/user_namespace.h>
66f592e2e   David Howells   proc: Add fs_cont...
21
  #include <linux/fs_context.h>
f6c7a1f34   Eric W. Biederman   [PATCH] proc: giv...
22
  #include <linux/mount.h>
07543f5c7   Pavel Emelyanov   pid namespaces: m...
23
  #include <linux/pid_namespace.h>
66f592e2e   David Howells   proc: Add fs_cont...
24
  #include <linux/fs_parser.h>
5b825c3af   Ingo Molnar   sched/headers: Pr...
25
  #include <linux/cred.h>
60a3c3a58   David Howells   procfs: Move proc...
26
  #include <linux/magic.h>
66f592e2e   David Howells   proc: Add fs_cont...
27
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

fee781e6c   Adrian Bunk   [PATCH] fs/proc/:...
29
  #include "internal.h"
66f592e2e   David Howells   proc: Add fs_cont...
30
31
32
  struct proc_fs_context {
  	struct pid_namespace	*pid_ns;
  	unsigned int		mask;
e61bb8b36   Alexey Gladkov   proc: use named e...
33
  	enum proc_hidepid	hidepid;
66f592e2e   David Howells   proc: Add fs_cont...
34
  	int			gid;
e61bb8b36   Alexey Gladkov   proc: use named e...
35
  	enum proc_pidonly	pidonly;
97412950b   Vasiliy Kulikov   procfs: parse mou...
36
  };
66f592e2e   David Howells   proc: Add fs_cont...
37
38
39
  enum proc_param {
  	Opt_gid,
  	Opt_hidepid,
6814ef2d9   Alexey Gladkov   proc: add option ...
40
  	Opt_subset,
97412950b   Vasiliy Kulikov   procfs: parse mou...
41
  };
d7167b149   Al Viro   fs_parse: fold fs...
42
  static const struct fs_parameter_spec proc_fs_parameters[] = {
66f592e2e   David Howells   proc: Add fs_cont...
43
  	fsparam_u32("gid",	Opt_gid),
1c6c4d112   Alexey Gladkov   proc: use human-r...
44
  	fsparam_string("hidepid",	Opt_hidepid),
6814ef2d9   Alexey Gladkov   proc: add option ...
45
  	fsparam_string("subset",	Opt_subset),
66f592e2e   David Howells   proc: Add fs_cont...
46
47
  	{}
  };
24a71ce5c   Alexey Gladkov   proc: instantiate...
48
49
50
51
52
53
54
  static inline int valid_hidepid(unsigned int value)
  {
  	return (value == HIDEPID_OFF ||
  		value == HIDEPID_NO_ACCESS ||
  		value == HIDEPID_INVISIBLE ||
  		value == HIDEPID_NOT_PTRACEABLE);
  }
1c6c4d112   Alexey Gladkov   proc: use human-r...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  static int proc_parse_hidepid_param(struct fs_context *fc, struct fs_parameter *param)
  {
  	struct proc_fs_context *ctx = fc->fs_private;
  	struct fs_parameter_spec hidepid_u32_spec = fsparam_u32("hidepid", Opt_hidepid);
  	struct fs_parse_result result;
  	int base = (unsigned long)hidepid_u32_spec.data;
  
  	if (param->type != fs_value_is_string)
  		return invalf(fc, "proc: unexpected type of hidepid value
  ");
  
  	if (!kstrtouint(param->string, base, &result.uint_32)) {
  		if (!valid_hidepid(result.uint_32))
  			return invalf(fc, "proc: unknown value of hidepid - %s
  ", param->string);
  		ctx->hidepid = result.uint_32;
  		return 0;
  	}
  
  	if (!strcmp(param->string, "off"))
  		ctx->hidepid = HIDEPID_OFF;
  	else if (!strcmp(param->string, "noaccess"))
  		ctx->hidepid = HIDEPID_NO_ACCESS;
  	else if (!strcmp(param->string, "invisible"))
  		ctx->hidepid = HIDEPID_INVISIBLE;
  	else if (!strcmp(param->string, "ptraceable"))
  		ctx->hidepid = HIDEPID_NOT_PTRACEABLE;
  	else
  		return invalf(fc, "proc: unknown value of hidepid - %s
  ", param->string);
  
  	return 0;
  }
6814ef2d9   Alexey Gladkov   proc: add option ...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  static int proc_parse_subset_param(struct fs_context *fc, char *value)
  {
  	struct proc_fs_context *ctx = fc->fs_private;
  
  	while (value) {
  		char *ptr = strchr(value, ',');
  
  		if (ptr != NULL)
  			*ptr++ = '\0';
  
  		if (*value != '\0') {
  			if (!strcmp(value, "pid")) {
  				ctx->pidonly = PROC_PIDONLY_ON;
  			} else {
  				return invalf(fc, "proc: unsupported subset option - %s
  ", value);
  			}
  		}
  		value = ptr;
  	}
  
  	return 0;
  }
66f592e2e   David Howells   proc: Add fs_cont...
111
  static int proc_parse_param(struct fs_context *fc, struct fs_parameter *param)
97412950b   Vasiliy Kulikov   procfs: parse mou...
112
  {
66f592e2e   David Howells   proc: Add fs_cont...
113
114
115
  	struct proc_fs_context *ctx = fc->fs_private;
  	struct fs_parse_result result;
  	int opt;
d7167b149   Al Viro   fs_parse: fold fs...
116
  	opt = fs_parse(fc, proc_fs_parameters, param, &result);
66f592e2e   David Howells   proc: Add fs_cont...
117
118
119
120
121
122
123
124
125
  	if (opt < 0)
  		return opt;
  
  	switch (opt) {
  	case Opt_gid:
  		ctx->gid = result.uint_32;
  		break;
  
  	case Opt_hidepid:
1c6c4d112   Alexey Gladkov   proc: use human-r...
126
127
  		if (proc_parse_hidepid_param(fc, param))
  			return -EINVAL;
66f592e2e   David Howells   proc: Add fs_cont...
128
  		break;
6814ef2d9   Alexey Gladkov   proc: add option ...
129
130
131
132
  	case Opt_subset:
  		if (proc_parse_subset_param(fc, param->string) < 0)
  			return -EINVAL;
  		break;
66f592e2e   David Howells   proc: Add fs_cont...
133
134
  	default:
  		return -EINVAL;
97412950b   Vasiliy Kulikov   procfs: parse mou...
135
  	}
66f592e2e   David Howells   proc: Add fs_cont...
136
137
  	ctx->mask |= 1 << opt;
  	return 0;
97412950b   Vasiliy Kulikov   procfs: parse mou...
138
  }
fa10fed30   Alexey Gladkov   proc: allow to mo...
139
  static void proc_apply_options(struct proc_fs_info *fs_info,
66f592e2e   David Howells   proc: Add fs_cont...
140
  			       struct fs_context *fc,
66f592e2e   David Howells   proc: Add fs_cont...
141
  			       struct user_namespace *user_ns)
97412950b   Vasiliy Kulikov   procfs: parse mou...
142
  {
66f592e2e   David Howells   proc: Add fs_cont...
143
144
145
  	struct proc_fs_context *ctx = fc->fs_private;
  
  	if (ctx->mask & (1 << Opt_gid))
fa10fed30   Alexey Gladkov   proc: allow to mo...
146
  		fs_info->pid_gid = make_kgid(user_ns, ctx->gid);
66f592e2e   David Howells   proc: Add fs_cont...
147
  	if (ctx->mask & (1 << Opt_hidepid))
fa10fed30   Alexey Gladkov   proc: allow to mo...
148
  		fs_info->hide_pid = ctx->hidepid;
6814ef2d9   Alexey Gladkov   proc: add option ...
149
150
  	if (ctx->mask & (1 << Opt_subset))
  		fs_info->pidonly = ctx->pidonly;
97412950b   Vasiliy Kulikov   procfs: parse mou...
151
  }
66f592e2e   David Howells   proc: Add fs_cont...
152
  static int proc_fill_super(struct super_block *s, struct fs_context *fc)
60a3c3a58   David Howells   procfs: Move proc...
153
  {
fa10fed30   Alexey Gladkov   proc: allow to mo...
154
  	struct proc_fs_context *ctx = fc->fs_private;
60a3c3a58   David Howells   procfs: Move proc...
155
  	struct inode *root_inode;
fa10fed30   Alexey Gladkov   proc: allow to mo...
156
  	struct proc_fs_info *fs_info;
60a3c3a58   David Howells   procfs: Move proc...
157
  	int ret;
fa10fed30   Alexey Gladkov   proc: allow to mo...
158
159
160
161
162
163
  	fs_info = kzalloc(sizeof(*fs_info), GFP_KERNEL);
  	if (!fs_info)
  		return -ENOMEM;
  
  	fs_info->pid_ns = get_pid_ns(ctx->pid_ns);
  	proc_apply_options(fs_info, fc, current_user_ns());
60a3c3a58   David Howells   procfs: Move proc...
164
165
166
167
168
169
170
171
172
  
  	/* User space would break if executables or devices appear on proc */
  	s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
  	s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
  	s->s_blocksize = 1024;
  	s->s_blocksize_bits = 10;
  	s->s_magic = PROC_SUPER_MAGIC;
  	s->s_op = &proc_sops;
  	s->s_time_gran = 1;
fa10fed30   Alexey Gladkov   proc: allow to mo...
173
  	s->s_fs_info = fs_info;
60a3c3a58   David Howells   procfs: Move proc...
174
175
176
177
178
179
180
  
  	/*
  	 * procfs isn't actually a stacking filesystem; however, there is
  	 * too much magic going on inside it to permit stacking things on
  	 * top of it
  	 */
  	s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
fa10fed30   Alexey Gladkov   proc: allow to mo...
181

60a3c3a58   David Howells   procfs: Move proc...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  	/* procfs dentries and inodes don't require IO to create */
  	s->s_shrink.seeks = 0;
  
  	pde_get(&proc_root);
  	root_inode = proc_get_inode(s, &proc_root);
  	if (!root_inode) {
  		pr_err("proc_fill_super: get root inode failed
  ");
  		return -ENOMEM;
  	}
  
  	s->s_root = d_make_root(root_inode);
  	if (!s->s_root) {
  		pr_err("proc_fill_super: allocate dentry failed
  ");
  		return -ENOMEM;
  	}
  
  	ret = proc_setup_self(s);
  	if (ret) {
  		return ret;
  	}
  	return proc_setup_thread_self(s);
  }
66f592e2e   David Howells   proc: Add fs_cont...
206
  static int proc_reconfigure(struct fs_context *fc)
97412950b   Vasiliy Kulikov   procfs: parse mou...
207
  {
66f592e2e   David Howells   proc: Add fs_cont...
208
  	struct super_block *sb = fc->root->d_sb;
fa10fed30   Alexey Gladkov   proc: allow to mo...
209
  	struct proc_fs_info *fs_info = proc_sb_info(sb);
02b9984d6   Theodore Ts'o   fs: push sync_fil...
210
211
  
  	sync_filesystem(sb);
66f592e2e   David Howells   proc: Add fs_cont...
212

fa10fed30   Alexey Gladkov   proc: allow to mo...
213
  	proc_apply_options(fs_info, fc, current_user_ns());
66f592e2e   David Howells   proc: Add fs_cont...
214
  	return 0;
97412950b   Vasiliy Kulikov   procfs: parse mou...
215
  }
66f592e2e   David Howells   proc: Add fs_cont...
216
  static int proc_get_tree(struct fs_context *fc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
  {
fa10fed30   Alexey Gladkov   proc: allow to mo...
218
  	return get_tree_nodev(fc, proc_fill_super);
66f592e2e   David Howells   proc: Add fs_cont...
219
  }
07543f5c7   Pavel Emelyanov   pid namespaces: m...
220

66f592e2e   David Howells   proc: Add fs_cont...
221
222
223
  static void proc_fs_context_free(struct fs_context *fc)
  {
  	struct proc_fs_context *ctx = fc->fs_private;
46cf047a9   Al Viro   procfs: set ->use...
224
  	put_pid_ns(ctx->pid_ns);
66f592e2e   David Howells   proc: Add fs_cont...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  	kfree(ctx);
  }
  
  static const struct fs_context_operations proc_fs_context_ops = {
  	.free		= proc_fs_context_free,
  	.parse_param	= proc_parse_param,
  	.get_tree	= proc_get_tree,
  	.reconfigure	= proc_reconfigure,
  };
  
  static int proc_init_fs_context(struct fs_context *fc)
  {
  	struct proc_fs_context *ctx;
  
  	ctx = kzalloc(sizeof(struct proc_fs_context), GFP_KERNEL);
  	if (!ctx)
  		return -ENOMEM;
07543f5c7   Pavel Emelyanov   pid namespaces: m...
242

66f592e2e   David Howells   proc: Add fs_cont...
243
  	ctx->pid_ns = get_pid_ns(task_active_pid_ns(current));
46cf047a9   Al Viro   procfs: set ->use...
244
245
  	put_user_ns(fc->user_ns);
  	fc->user_ns = get_user_ns(ctx->pid_ns->user_ns);
66f592e2e   David Howells   proc: Add fs_cont...
246
247
248
  	fc->fs_private = ctx;
  	fc->ops = &proc_fs_context_ops;
  	return 0;
07543f5c7   Pavel Emelyanov   pid namespaces: m...
249
250
251
252
  }
  
  static void proc_kill_sb(struct super_block *sb)
  {
fa10fed30   Alexey Gladkov   proc: allow to mo...
253
  	struct proc_fs_info *fs_info = proc_sb_info(sb);
07543f5c7   Pavel Emelyanov   pid namespaces: m...
254

058f2e4da   Alexey Gladkov   proc: s_fs_info m...
255
256
257
258
  	if (!fs_info) {
  		kill_anon_super(sb);
  		return;
  	}
4fa3b1c41   Eric W. Biederman   proc: Handle umou...
259

058f2e4da   Alexey Gladkov   proc: s_fs_info m...
260
261
  	dput(fs_info->proc_self);
  	dput(fs_info->proc_thread_self);
4fa3b1c41   Eric W. Biederman   proc: Handle umou...
262

fa10fed30   Alexey Gladkov   proc: allow to mo...
263
264
265
  	kill_anon_super(sb);
  	put_pid_ns(fs_info->pid_ns);
  	kfree(fs_info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  }
c2319540c   Alexey Dobriyan   proc: fix NULL ->...
267
  static struct file_system_type proc_fs_type = {
66f592e2e   David Howells   proc: Add fs_cont...
268
269
  	.name			= "proc",
  	.init_fs_context	= proc_init_fs_context,
d7167b149   Al Viro   fs_parse: fold fs...
270
  	.parameters		= proc_fs_parameters,
66f592e2e   David Howells   proc: Add fs_cont...
271
  	.kill_sb		= proc_kill_sb,
0b3b094ac   Jan Kara   fanotify: Disallo...
272
  	.fs_flags		= FS_USERNS_MOUNT | FS_DISALLOW_NOTIFY_PERM,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
  void __init proc_root_init(void)
  {
195b8cf06   Alexey Dobriyan   proc: move "struc...
276
  	proc_init_kmemcache();
1270dd8d9   Alexey Dobriyan   fs/proc: calculat...
277
  	set_proc_pid_nlink();
e656d8a6f   Eric W. Biederman   procfs: Use the p...
278
  	proc_self_init();
0097875bd   Eric W. Biederman   proc: Implement /...
279
  	proc_thread_self_init();
155134fef   Linus Torvalds   Revert "proc: Poi...
280
  	proc_symlink("mounts", NULL, "self/mounts");
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
281
282
  
  	proc_net_init();
36a5aeb87   Alexey Dobriyan   proc: remove proc...
283
  	proc_mkdir("fs", NULL);
928b4d8c8   Alexey Dobriyan   proc: remove proc...
284
  	proc_mkdir("driver", NULL);
eb6d38d54   Eric W. Biederman   proc: Allow creat...
285
  	proc_create_mount_point("fs/nfsd"); /* somewhere for the nfsd filesystem to be mounted */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
  #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
  	/* just give it a mountpoint */
eb6d38d54   Eric W. Biederman   proc: Allow creat...
288
  	proc_create_mount_point("openprom");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
  #endif
  	proc_tty_init();
9c37066d8   Alexey Dobriyan   proc: remove proc...
291
  	proc_mkdir("bus", NULL);
77b14db50   Eric W. Biederman   [PATCH] sysctl: r...
292
  	proc_sys_init();
1539d584e   Alexey Dobriyan   proc: register fi...
293
294
  
  	register_filesystem(&proc_fs_type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
  }
a528d35e8   David Howells   statx: Add a syst...
296
297
  static int proc_root_getattr(const struct path *path, struct kstat *stat,
  			     u32 request_mask, unsigned int query_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
  {
a528d35e8   David Howells   statx: Add a syst...
299
  	generic_fillattr(d_inode(path->dentry), stat);
76b6159ba   Al Viro   [PATCH] fix handl...
300
301
302
  	stat->nlink = proc_root.nlink + nr_processes();
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303

00cd8dd3b   Al Viro   stop passing name...
304
  static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
76b6159ba   Al Viro   [PATCH] fix handl...
305
  {
867aaccf1   Zhikang Zhang   proc: remove unus...
306
  	if (!proc_pid_lookup(dentry, flags))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
  		return NULL;
66f592e2e   David Howells   proc: Add fs_cont...
308

335eb5315   Alexey Dobriyan   proc: faster /pro...
309
  	return proc_lookup(dir, dentry, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
  }
f0c3b5093   Al Viro   [readdir] convert...
311
  static int proc_root_readdir(struct file *file, struct dir_context *ctx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
  {
f0c3b5093   Al Viro   [readdir] convert...
313
  	if (ctx->pos < FIRST_PROCESS_ENTRY) {
94fc5d9de   Richard Genoud   proc: return on p...
314
315
316
  		int error = proc_readdir(file, ctx);
  		if (unlikely(error <= 0))
  			return error;
f0c3b5093   Al Viro   [readdir] convert...
317
  		ctx->pos = FIRST_PROCESS_ENTRY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319

f0c3b5093   Al Viro   [readdir] convert...
320
  	return proc_pid_readdir(file, ctx);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
  }
  
  /*
   * The root /proc directory is special, as it has the
   * <pid> directories. Thus we don't use the generic
   * directory handling functions for that..
   */
00977a59b   Arjan van de Ven   [PATCH] mark stru...
328
  static const struct file_operations proc_root_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
  	.read		 = generic_read_dir,
f50752eaa   Al Viro   switch all procfs...
330
331
  	.iterate_shared	 = proc_root_readdir,
  	.llseek		= generic_file_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
333
334
335
336
  };
  
  /*
   * proc root can do almost nothing..
   */
c5ef1c42c   Arjan van de Ven   [PATCH] mark stru...
337
  static const struct inode_operations proc_root_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
  	.lookup		= proc_root_lookup,
76b6159ba   Al Viro   [PATCH] fix handl...
339
  	.getattr	= proc_root_getattr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
342
343
344
345
346
347
  };
  
  /*
   * This is the root "inode" in the /proc tree..
   */
  struct proc_dir_entry proc_root = {
  	.low_ino	= PROC_ROOT_INO, 
  	.namelen	= 5, 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
  	.mode		= S_IFDIR | S_IRUGO | S_IXUGO, 
  	.nlink		= 2, 
9cdd83e31   Alexey Dobriyan   proc: switch stru...
350
  	.refcnt		= REFCOUNT_INIT(1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
351
  	.proc_iops	= &proc_root_inode_operations, 
d56c0d45f   Alexey Dobriyan   proc: decouple pr...
352
  	.proc_dir_ops	= &proc_root_operations,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  	.parent		= &proc_root,
4f1134370   Alexey Dobriyan   proc: use slower ...
354
  	.subdir		= RB_ROOT,
24074a35c   David Howells   proc: Make inline...
355
  	.name		= "/proc",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
  };