xref: /openbmc/linux/fs/proc/base.c (revision b99b2588)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/proc/base.c
4  *
5  *  Copyright (C) 1991, 1992 Linus Torvalds
6  *
7  *  proc base directory handling functions
8  *
9  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10  *  Instead of using magical inumbers to determine the kind of object
11  *  we allocate and fill in-core inodes upon lookup. They don't even
12  *  go into icache. We cache the reference to task_struct upon lookup too.
13  *  Eventually it should become a filesystem in its own. We don't use the
14  *  rest of procfs anymore.
15  *
16  *
17  *  Changelog:
18  *  17-Jan-2005
19  *  Allan Bezerra
20  *  Bruna Moreira <bruna.moreira@indt.org.br>
21  *  Edjard Mota <edjard.mota@indt.org.br>
22  *  Ilias Biris <ilias.biris@indt.org.br>
23  *  Mauricio Lin <mauricio.lin@indt.org.br>
24  *
25  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26  *
27  *  A new process specific entry (smaps) included in /proc. It shows the
28  *  size of rss for each memory area. The maps entry lacks information
29  *  about physical memory size (rss) for each mapped file, i.e.,
30  *  rss information for executables and library files.
31  *  This additional information is useful for any tools that need to know
32  *  about physical memory consumption for a process specific library.
33  *
34  *  Changelog:
35  *  21-Feb-2005
36  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37  *  Pud inclusion in the page table walking.
38  *
39  *  ChangeLog:
40  *  10-Mar-2005
41  *  10LE Instituto Nokia de Tecnologia - INdT:
42  *  A better way to walks through the page table as suggested by Hugh Dickins.
43  *
44  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
45  *  Smaps information related to shared, private, clean and dirty pages.
46  *
47  *  Paul Mundt <paul.mundt@nokia.com>:
48  *  Overall revision about smaps.
49  */
50 
51 #include <linux/uaccess.h>
52 
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
67 #include <linux/mm.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/printk.h>
78 #include <linux/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/posix-timers.h>
96 #include <linux/time_namespace.h>
97 #include <linux/resctrl.h>
98 #include <linux/cn_proc.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
101 #include "fd.h"
102 
103 #include "../../lib/kstrtox.h"
104 
105 /* NOTE:
106  *	Implementing inode permission operations in /proc is almost
107  *	certainly an error.  Permission checks need to happen during
108  *	each system call not at open time.  The reason is that most of
109  *	what we wish to check for permissions in /proc varies at runtime.
110  *
111  *	The classic example of a problem is opening file descriptors
112  *	in /proc for a task before it execs a suid executable.
113  */
114 
115 static u8 nlink_tid __ro_after_init;
116 static u8 nlink_tgid __ro_after_init;
117 
118 struct pid_entry {
119 	const char *name;
120 	unsigned int len;
121 	umode_t mode;
122 	const struct inode_operations *iop;
123 	const struct file_operations *fop;
124 	union proc_op op;
125 };
126 
127 #define NOD(NAME, MODE, IOP, FOP, OP) {			\
128 	.name = (NAME),					\
129 	.len  = sizeof(NAME) - 1,			\
130 	.mode = MODE,					\
131 	.iop  = IOP,					\
132 	.fop  = FOP,					\
133 	.op   = OP,					\
134 }
135 
136 #define DIR(NAME, MODE, iops, fops)	\
137 	NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138 #define LNK(NAME, get_link)					\
139 	NOD(NAME, (S_IFLNK|S_IRWXUGO),				\
140 		&proc_pid_link_inode_operations, NULL,		\
141 		{ .proc_get_link = get_link } )
142 #define REG(NAME, MODE, fops)				\
143 	NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
144 #define ONE(NAME, MODE, show)				\
145 	NOD(NAME, (S_IFREG|(MODE)),			\
146 		NULL, &proc_single_file_operations,	\
147 		{ .proc_show = show } )
148 #define ATTR(LSM, NAME, MODE)				\
149 	NOD(NAME, (S_IFREG|(MODE)),			\
150 		NULL, &proc_pid_attr_operations,	\
151 		{ .lsm = LSM })
152 
153 /*
154  * Count the number of hardlinks for the pid_entry table, excluding the .
155  * and .. links.
156  */
157 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 	unsigned int n)
159 {
160 	unsigned int i;
161 	unsigned int count;
162 
163 	count = 2;
164 	for (i = 0; i < n; ++i) {
165 		if (S_ISDIR(entries[i].mode))
166 			++count;
167 	}
168 
169 	return count;
170 }
171 
172 static int get_task_root(struct task_struct *task, struct path *root)
173 {
174 	int result = -ENOENT;
175 
176 	task_lock(task);
177 	if (task->fs) {
178 		get_fs_root(task->fs, root);
179 		result = 0;
180 	}
181 	task_unlock(task);
182 	return result;
183 }
184 
185 static int proc_cwd_link(struct dentry *dentry, struct path *path)
186 {
187 	struct task_struct *task = get_proc_task(d_inode(dentry));
188 	int result = -ENOENT;
189 
190 	if (task) {
191 		task_lock(task);
192 		if (task->fs) {
193 			get_fs_pwd(task->fs, path);
194 			result = 0;
195 		}
196 		task_unlock(task);
197 		put_task_struct(task);
198 	}
199 	return result;
200 }
201 
202 static int proc_root_link(struct dentry *dentry, struct path *path)
203 {
204 	struct task_struct *task = get_proc_task(d_inode(dentry));
205 	int result = -ENOENT;
206 
207 	if (task) {
208 		result = get_task_root(task, path);
209 		put_task_struct(task);
210 	}
211 	return result;
212 }
213 
214 /*
215  * If the user used setproctitle(), we just get the string from
216  * user space at arg_start, and limit it to a maximum of one page.
217  */
218 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
219 				size_t count, unsigned long pos,
220 				unsigned long arg_start)
221 {
222 	char *page;
223 	int ret, got;
224 
225 	if (pos >= PAGE_SIZE)
226 		return 0;
227 
228 	page = (char *)__get_free_page(GFP_KERNEL);
229 	if (!page)
230 		return -ENOMEM;
231 
232 	ret = 0;
233 	got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
234 	if (got > 0) {
235 		int len = strnlen(page, got);
236 
237 		/* Include the NUL character if it was found */
238 		if (len < got)
239 			len++;
240 
241 		if (len > pos) {
242 			len -= pos;
243 			if (len > count)
244 				len = count;
245 			len -= copy_to_user(buf, page+pos, len);
246 			if (!len)
247 				len = -EFAULT;
248 			ret = len;
249 		}
250 	}
251 	free_page((unsigned long)page);
252 	return ret;
253 }
254 
255 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
256 			      size_t count, loff_t *ppos)
257 {
258 	unsigned long arg_start, arg_end, env_start, env_end;
259 	unsigned long pos, len;
260 	char *page, c;
261 
262 	/* Check if process spawned far enough to have cmdline. */
263 	if (!mm->env_end)
264 		return 0;
265 
266 	spin_lock(&mm->arg_lock);
267 	arg_start = mm->arg_start;
268 	arg_end = mm->arg_end;
269 	env_start = mm->env_start;
270 	env_end = mm->env_end;
271 	spin_unlock(&mm->arg_lock);
272 
273 	if (arg_start >= arg_end)
274 		return 0;
275 
276 	/*
277 	 * We allow setproctitle() to overwrite the argument
278 	 * strings, and overflow past the original end. But
279 	 * only when it overflows into the environment area.
280 	 */
281 	if (env_start != arg_end || env_end < env_start)
282 		env_start = env_end = arg_end;
283 	len = env_end - arg_start;
284 
285 	/* We're not going to care if "*ppos" has high bits set */
286 	pos = *ppos;
287 	if (pos >= len)
288 		return 0;
289 	if (count > len - pos)
290 		count = len - pos;
291 	if (!count)
292 		return 0;
293 
294 	/*
295 	 * Magical special case: if the argv[] end byte is not
296 	 * zero, the user has overwritten it with setproctitle(3).
297 	 *
298 	 * Possible future enhancement: do this only once when
299 	 * pos is 0, and set a flag in the 'struct file'.
300 	 */
301 	if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
302 		return get_mm_proctitle(mm, buf, count, pos, arg_start);
303 
304 	/*
305 	 * For the non-setproctitle() case we limit things strictly
306 	 * to the [arg_start, arg_end[ range.
307 	 */
308 	pos += arg_start;
309 	if (pos < arg_start || pos >= arg_end)
310 		return 0;
311 	if (count > arg_end - pos)
312 		count = arg_end - pos;
313 
314 	page = (char *)__get_free_page(GFP_KERNEL);
315 	if (!page)
316 		return -ENOMEM;
317 
318 	len = 0;
319 	while (count) {
320 		int got;
321 		size_t size = min_t(size_t, PAGE_SIZE, count);
322 
323 		got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
324 		if (got <= 0)
325 			break;
326 		got -= copy_to_user(buf, page, got);
327 		if (unlikely(!got)) {
328 			if (!len)
329 				len = -EFAULT;
330 			break;
331 		}
332 		pos += got;
333 		buf += got;
334 		len += got;
335 		count -= got;
336 	}
337 
338 	free_page((unsigned long)page);
339 	return len;
340 }
341 
342 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
343 				size_t count, loff_t *pos)
344 {
345 	struct mm_struct *mm;
346 	ssize_t ret;
347 
348 	mm = get_task_mm(tsk);
349 	if (!mm)
350 		return 0;
351 
352 	ret = get_mm_cmdline(mm, buf, count, pos);
353 	mmput(mm);
354 	return ret;
355 }
356 
357 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
358 				     size_t count, loff_t *pos)
359 {
360 	struct task_struct *tsk;
361 	ssize_t ret;
362 
363 	BUG_ON(*pos < 0);
364 
365 	tsk = get_proc_task(file_inode(file));
366 	if (!tsk)
367 		return -ESRCH;
368 	ret = get_task_cmdline(tsk, buf, count, pos);
369 	put_task_struct(tsk);
370 	if (ret > 0)
371 		*pos += ret;
372 	return ret;
373 }
374 
375 static const struct file_operations proc_pid_cmdline_ops = {
376 	.read	= proc_pid_cmdline_read,
377 	.llseek	= generic_file_llseek,
378 };
379 
380 #ifdef CONFIG_KALLSYMS
381 /*
382  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
383  * Returns the resolved symbol.  If that fails, simply return the address.
384  */
385 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
386 			  struct pid *pid, struct task_struct *task)
387 {
388 	unsigned long wchan;
389 	char symname[KSYM_NAME_LEN];
390 
391 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
392 		goto print0;
393 
394 	wchan = get_wchan(task);
395 	if (wchan && !lookup_symbol_name(wchan, symname)) {
396 		seq_puts(m, symname);
397 		return 0;
398 	}
399 
400 print0:
401 	seq_putc(m, '0');
402 	return 0;
403 }
404 #endif /* CONFIG_KALLSYMS */
405 
406 static int lock_trace(struct task_struct *task)
407 {
408 	int err = down_read_killable(&task->signal->exec_update_lock);
409 	if (err)
410 		return err;
411 	if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
412 		up_read(&task->signal->exec_update_lock);
413 		return -EPERM;
414 	}
415 	return 0;
416 }
417 
418 static void unlock_trace(struct task_struct *task)
419 {
420 	up_read(&task->signal->exec_update_lock);
421 }
422 
423 #ifdef CONFIG_STACKTRACE
424 
425 #define MAX_STACK_TRACE_DEPTH	64
426 
427 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
428 			  struct pid *pid, struct task_struct *task)
429 {
430 	unsigned long *entries;
431 	int err;
432 
433 	/*
434 	 * The ability to racily run the kernel stack unwinder on a running task
435 	 * and then observe the unwinder output is scary; while it is useful for
436 	 * debugging kernel issues, it can also allow an attacker to leak kernel
437 	 * stack contents.
438 	 * Doing this in a manner that is at least safe from races would require
439 	 * some work to ensure that the remote task can not be scheduled; and
440 	 * even then, this would still expose the unwinder as local attack
441 	 * surface.
442 	 * Therefore, this interface is restricted to root.
443 	 */
444 	if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
445 		return -EACCES;
446 
447 	entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
448 				GFP_KERNEL);
449 	if (!entries)
450 		return -ENOMEM;
451 
452 	err = lock_trace(task);
453 	if (!err) {
454 		unsigned int i, nr_entries;
455 
456 		nr_entries = stack_trace_save_tsk(task, entries,
457 						  MAX_STACK_TRACE_DEPTH, 0);
458 
459 		for (i = 0; i < nr_entries; i++) {
460 			seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
461 		}
462 
463 		unlock_trace(task);
464 	}
465 	kfree(entries);
466 
467 	return err;
468 }
469 #endif
470 
471 #ifdef CONFIG_SCHED_INFO
472 /*
473  * Provides /proc/PID/schedstat
474  */
475 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
476 			      struct pid *pid, struct task_struct *task)
477 {
478 	if (unlikely(!sched_info_on()))
479 		seq_puts(m, "0 0 0\n");
480 	else
481 		seq_printf(m, "%llu %llu %lu\n",
482 		   (unsigned long long)task->se.sum_exec_runtime,
483 		   (unsigned long long)task->sched_info.run_delay,
484 		   task->sched_info.pcount);
485 
486 	return 0;
487 }
488 #endif
489 
490 #ifdef CONFIG_LATENCYTOP
491 static int lstats_show_proc(struct seq_file *m, void *v)
492 {
493 	int i;
494 	struct inode *inode = m->private;
495 	struct task_struct *task = get_proc_task(inode);
496 
497 	if (!task)
498 		return -ESRCH;
499 	seq_puts(m, "Latency Top version : v0.1\n");
500 	for (i = 0; i < LT_SAVECOUNT; i++) {
501 		struct latency_record *lr = &task->latency_record[i];
502 		if (lr->backtrace[0]) {
503 			int q;
504 			seq_printf(m, "%i %li %li",
505 				   lr->count, lr->time, lr->max);
506 			for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
507 				unsigned long bt = lr->backtrace[q];
508 
509 				if (!bt)
510 					break;
511 				seq_printf(m, " %ps", (void *)bt);
512 			}
513 			seq_putc(m, '\n');
514 		}
515 
516 	}
517 	put_task_struct(task);
518 	return 0;
519 }
520 
521 static int lstats_open(struct inode *inode, struct file *file)
522 {
523 	return single_open(file, lstats_show_proc, inode);
524 }
525 
526 static ssize_t lstats_write(struct file *file, const char __user *buf,
527 			    size_t count, loff_t *offs)
528 {
529 	struct task_struct *task = get_proc_task(file_inode(file));
530 
531 	if (!task)
532 		return -ESRCH;
533 	clear_tsk_latency_tracing(task);
534 	put_task_struct(task);
535 
536 	return count;
537 }
538 
539 static const struct file_operations proc_lstats_operations = {
540 	.open		= lstats_open,
541 	.read		= seq_read,
542 	.write		= lstats_write,
543 	.llseek		= seq_lseek,
544 	.release	= single_release,
545 };
546 
547 #endif
548 
549 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
550 			  struct pid *pid, struct task_struct *task)
551 {
552 	unsigned long totalpages = totalram_pages() + total_swap_pages;
553 	unsigned long points = 0;
554 	long badness;
555 
556 	badness = oom_badness(task, totalpages);
557 	/*
558 	 * Special case OOM_SCORE_ADJ_MIN for all others scale the
559 	 * badness value into [0, 2000] range which we have been
560 	 * exporting for a long time so userspace might depend on it.
561 	 */
562 	if (badness != LONG_MIN)
563 		points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3;
564 
565 	seq_printf(m, "%lu\n", points);
566 
567 	return 0;
568 }
569 
570 struct limit_names {
571 	const char *name;
572 	const char *unit;
573 };
574 
575 static const struct limit_names lnames[RLIM_NLIMITS] = {
576 	[RLIMIT_CPU] = {"Max cpu time", "seconds"},
577 	[RLIMIT_FSIZE] = {"Max file size", "bytes"},
578 	[RLIMIT_DATA] = {"Max data size", "bytes"},
579 	[RLIMIT_STACK] = {"Max stack size", "bytes"},
580 	[RLIMIT_CORE] = {"Max core file size", "bytes"},
581 	[RLIMIT_RSS] = {"Max resident set", "bytes"},
582 	[RLIMIT_NPROC] = {"Max processes", "processes"},
583 	[RLIMIT_NOFILE] = {"Max open files", "files"},
584 	[RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
585 	[RLIMIT_AS] = {"Max address space", "bytes"},
586 	[RLIMIT_LOCKS] = {"Max file locks", "locks"},
587 	[RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
588 	[RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
589 	[RLIMIT_NICE] = {"Max nice priority", NULL},
590 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
591 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
592 };
593 
594 /* Display limits for a process */
595 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
596 			   struct pid *pid, struct task_struct *task)
597 {
598 	unsigned int i;
599 	unsigned long flags;
600 
601 	struct rlimit rlim[RLIM_NLIMITS];
602 
603 	if (!lock_task_sighand(task, &flags))
604 		return 0;
605 	memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
606 	unlock_task_sighand(task, &flags);
607 
608 	/*
609 	 * print the file header
610 	 */
611 	seq_puts(m, "Limit                     "
612 		"Soft Limit           "
613 		"Hard Limit           "
614 		"Units     \n");
615 
616 	for (i = 0; i < RLIM_NLIMITS; i++) {
617 		if (rlim[i].rlim_cur == RLIM_INFINITY)
618 			seq_printf(m, "%-25s %-20s ",
619 				   lnames[i].name, "unlimited");
620 		else
621 			seq_printf(m, "%-25s %-20lu ",
622 				   lnames[i].name, rlim[i].rlim_cur);
623 
624 		if (rlim[i].rlim_max == RLIM_INFINITY)
625 			seq_printf(m, "%-20s ", "unlimited");
626 		else
627 			seq_printf(m, "%-20lu ", rlim[i].rlim_max);
628 
629 		if (lnames[i].unit)
630 			seq_printf(m, "%-10s\n", lnames[i].unit);
631 		else
632 			seq_putc(m, '\n');
633 	}
634 
635 	return 0;
636 }
637 
638 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
639 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
640 			    struct pid *pid, struct task_struct *task)
641 {
642 	struct syscall_info info;
643 	u64 *args = &info.data.args[0];
644 	int res;
645 
646 	res = lock_trace(task);
647 	if (res)
648 		return res;
649 
650 	if (task_current_syscall(task, &info))
651 		seq_puts(m, "running\n");
652 	else if (info.data.nr < 0)
653 		seq_printf(m, "%d 0x%llx 0x%llx\n",
654 			   info.data.nr, info.sp, info.data.instruction_pointer);
655 	else
656 		seq_printf(m,
657 		       "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
658 		       info.data.nr,
659 		       args[0], args[1], args[2], args[3], args[4], args[5],
660 		       info.sp, info.data.instruction_pointer);
661 	unlock_trace(task);
662 
663 	return 0;
664 }
665 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
666 
667 /************************************************************************/
668 /*                       Here the fs part begins                        */
669 /************************************************************************/
670 
671 /* permission checks */
672 static bool proc_fd_access_allowed(struct inode *inode)
673 {
674 	struct task_struct *task;
675 	bool allowed = false;
676 	/* Allow access to a task's file descriptors if it is us or we
677 	 * may use ptrace attach to the process and find out that
678 	 * information.
679 	 */
680 	task = get_proc_task(inode);
681 	if (task) {
682 		allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
683 		put_task_struct(task);
684 	}
685 	return allowed;
686 }
687 
688 int proc_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
689 		 struct iattr *attr)
690 {
691 	int error;
692 	struct inode *inode = d_inode(dentry);
693 
694 	if (attr->ia_valid & ATTR_MODE)
695 		return -EPERM;
696 
697 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
698 	if (error)
699 		return error;
700 
701 	setattr_copy(&nop_mnt_idmap, inode, attr);
702 	return 0;
703 }
704 
705 /*
706  * May current process learn task's sched/cmdline info (for hide_pid_min=1)
707  * or euid/egid (for hide_pid_min=2)?
708  */
709 static bool has_pid_permissions(struct proc_fs_info *fs_info,
710 				 struct task_struct *task,
711 				 enum proc_hidepid hide_pid_min)
712 {
713 	/*
714 	 * If 'hidpid' mount option is set force a ptrace check,
715 	 * we indicate that we are using a filesystem syscall
716 	 * by passing PTRACE_MODE_READ_FSCREDS
717 	 */
718 	if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE)
719 		return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
720 
721 	if (fs_info->hide_pid < hide_pid_min)
722 		return true;
723 	if (in_group_p(fs_info->pid_gid))
724 		return true;
725 	return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
726 }
727 
728 
729 static int proc_pid_permission(struct mnt_idmap *idmap,
730 			       struct inode *inode, int mask)
731 {
732 	struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
733 	struct task_struct *task;
734 	bool has_perms;
735 
736 	task = get_proc_task(inode);
737 	if (!task)
738 		return -ESRCH;
739 	has_perms = has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS);
740 	put_task_struct(task);
741 
742 	if (!has_perms) {
743 		if (fs_info->hide_pid == HIDEPID_INVISIBLE) {
744 			/*
745 			 * Let's make getdents(), stat(), and open()
746 			 * consistent with each other.  If a process
747 			 * may not stat() a file, it shouldn't be seen
748 			 * in procfs at all.
749 			 */
750 			return -ENOENT;
751 		}
752 
753 		return -EPERM;
754 	}
755 	return generic_permission(&nop_mnt_idmap, inode, mask);
756 }
757 
758 
759 
760 static const struct inode_operations proc_def_inode_operations = {
761 	.setattr	= proc_setattr,
762 };
763 
764 static int proc_single_show(struct seq_file *m, void *v)
765 {
766 	struct inode *inode = m->private;
767 	struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
768 	struct pid *pid = proc_pid(inode);
769 	struct task_struct *task;
770 	int ret;
771 
772 	task = get_pid_task(pid, PIDTYPE_PID);
773 	if (!task)
774 		return -ESRCH;
775 
776 	ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
777 
778 	put_task_struct(task);
779 	return ret;
780 }
781 
782 static int proc_single_open(struct inode *inode, struct file *filp)
783 {
784 	return single_open(filp, proc_single_show, inode);
785 }
786 
787 static const struct file_operations proc_single_file_operations = {
788 	.open		= proc_single_open,
789 	.read		= seq_read,
790 	.llseek		= seq_lseek,
791 	.release	= single_release,
792 };
793 
794 
795 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
796 {
797 	struct task_struct *task = get_proc_task(inode);
798 	struct mm_struct *mm = ERR_PTR(-ESRCH);
799 
800 	if (task) {
801 		mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
802 		put_task_struct(task);
803 
804 		if (!IS_ERR_OR_NULL(mm)) {
805 			/* ensure this mm_struct can't be freed */
806 			mmgrab(mm);
807 			/* but do not pin its memory */
808 			mmput(mm);
809 		}
810 	}
811 
812 	return mm;
813 }
814 
815 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
816 {
817 	struct mm_struct *mm = proc_mem_open(inode, mode);
818 
819 	if (IS_ERR(mm))
820 		return PTR_ERR(mm);
821 
822 	file->private_data = mm;
823 	return 0;
824 }
825 
826 static int mem_open(struct inode *inode, struct file *file)
827 {
828 	int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
829 
830 	/* OK to pass negative loff_t, we can catch out-of-range */
831 	file->f_mode |= FMODE_UNSIGNED_OFFSET;
832 
833 	return ret;
834 }
835 
836 static ssize_t mem_rw(struct file *file, char __user *buf,
837 			size_t count, loff_t *ppos, int write)
838 {
839 	struct mm_struct *mm = file->private_data;
840 	unsigned long addr = *ppos;
841 	ssize_t copied;
842 	char *page;
843 	unsigned int flags;
844 
845 	if (!mm)
846 		return 0;
847 
848 	page = (char *)__get_free_page(GFP_KERNEL);
849 	if (!page)
850 		return -ENOMEM;
851 
852 	copied = 0;
853 	if (!mmget_not_zero(mm))
854 		goto free;
855 
856 	flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
857 
858 	while (count > 0) {
859 		size_t this_len = min_t(size_t, count, PAGE_SIZE);
860 
861 		if (write && copy_from_user(page, buf, this_len)) {
862 			copied = -EFAULT;
863 			break;
864 		}
865 
866 		this_len = access_remote_vm(mm, addr, page, this_len, flags);
867 		if (!this_len) {
868 			if (!copied)
869 				copied = -EIO;
870 			break;
871 		}
872 
873 		if (!write && copy_to_user(buf, page, this_len)) {
874 			copied = -EFAULT;
875 			break;
876 		}
877 
878 		buf += this_len;
879 		addr += this_len;
880 		copied += this_len;
881 		count -= this_len;
882 	}
883 	*ppos = addr;
884 
885 	mmput(mm);
886 free:
887 	free_page((unsigned long) page);
888 	return copied;
889 }
890 
891 static ssize_t mem_read(struct file *file, char __user *buf,
892 			size_t count, loff_t *ppos)
893 {
894 	return mem_rw(file, buf, count, ppos, 0);
895 }
896 
897 static ssize_t mem_write(struct file *file, const char __user *buf,
898 			 size_t count, loff_t *ppos)
899 {
900 	return mem_rw(file, (char __user*)buf, count, ppos, 1);
901 }
902 
903 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
904 {
905 	switch (orig) {
906 	case 0:
907 		file->f_pos = offset;
908 		break;
909 	case 1:
910 		file->f_pos += offset;
911 		break;
912 	default:
913 		return -EINVAL;
914 	}
915 	force_successful_syscall_return();
916 	return file->f_pos;
917 }
918 
919 static int mem_release(struct inode *inode, struct file *file)
920 {
921 	struct mm_struct *mm = file->private_data;
922 	if (mm)
923 		mmdrop(mm);
924 	return 0;
925 }
926 
927 static const struct file_operations proc_mem_operations = {
928 	.llseek		= mem_lseek,
929 	.read		= mem_read,
930 	.write		= mem_write,
931 	.open		= mem_open,
932 	.release	= mem_release,
933 };
934 
935 static int environ_open(struct inode *inode, struct file *file)
936 {
937 	return __mem_open(inode, file, PTRACE_MODE_READ);
938 }
939 
940 static ssize_t environ_read(struct file *file, char __user *buf,
941 			size_t count, loff_t *ppos)
942 {
943 	char *page;
944 	unsigned long src = *ppos;
945 	int ret = 0;
946 	struct mm_struct *mm = file->private_data;
947 	unsigned long env_start, env_end;
948 
949 	/* Ensure the process spawned far enough to have an environment. */
950 	if (!mm || !mm->env_end)
951 		return 0;
952 
953 	page = (char *)__get_free_page(GFP_KERNEL);
954 	if (!page)
955 		return -ENOMEM;
956 
957 	ret = 0;
958 	if (!mmget_not_zero(mm))
959 		goto free;
960 
961 	spin_lock(&mm->arg_lock);
962 	env_start = mm->env_start;
963 	env_end = mm->env_end;
964 	spin_unlock(&mm->arg_lock);
965 
966 	while (count > 0) {
967 		size_t this_len, max_len;
968 		int retval;
969 
970 		if (src >= (env_end - env_start))
971 			break;
972 
973 		this_len = env_end - (env_start + src);
974 
975 		max_len = min_t(size_t, PAGE_SIZE, count);
976 		this_len = min(max_len, this_len);
977 
978 		retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
979 
980 		if (retval <= 0) {
981 			ret = retval;
982 			break;
983 		}
984 
985 		if (copy_to_user(buf, page, retval)) {
986 			ret = -EFAULT;
987 			break;
988 		}
989 
990 		ret += retval;
991 		src += retval;
992 		buf += retval;
993 		count -= retval;
994 	}
995 	*ppos = src;
996 	mmput(mm);
997 
998 free:
999 	free_page((unsigned long) page);
1000 	return ret;
1001 }
1002 
1003 static const struct file_operations proc_environ_operations = {
1004 	.open		= environ_open,
1005 	.read		= environ_read,
1006 	.llseek		= generic_file_llseek,
1007 	.release	= mem_release,
1008 };
1009 
1010 static int auxv_open(struct inode *inode, struct file *file)
1011 {
1012 	return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1013 }
1014 
1015 static ssize_t auxv_read(struct file *file, char __user *buf,
1016 			size_t count, loff_t *ppos)
1017 {
1018 	struct mm_struct *mm = file->private_data;
1019 	unsigned int nwords = 0;
1020 
1021 	if (!mm)
1022 		return 0;
1023 	do {
1024 		nwords += 2;
1025 	} while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1026 	return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1027 				       nwords * sizeof(mm->saved_auxv[0]));
1028 }
1029 
1030 static const struct file_operations proc_auxv_operations = {
1031 	.open		= auxv_open,
1032 	.read		= auxv_read,
1033 	.llseek		= generic_file_llseek,
1034 	.release	= mem_release,
1035 };
1036 
1037 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1038 			    loff_t *ppos)
1039 {
1040 	struct task_struct *task = get_proc_task(file_inode(file));
1041 	char buffer[PROC_NUMBUF];
1042 	int oom_adj = OOM_ADJUST_MIN;
1043 	size_t len;
1044 
1045 	if (!task)
1046 		return -ESRCH;
1047 	if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1048 		oom_adj = OOM_ADJUST_MAX;
1049 	else
1050 		oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1051 			  OOM_SCORE_ADJ_MAX;
1052 	put_task_struct(task);
1053 	if (oom_adj > OOM_ADJUST_MAX)
1054 		oom_adj = OOM_ADJUST_MAX;
1055 	len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1056 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1057 }
1058 
1059 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1060 {
1061 	struct mm_struct *mm = NULL;
1062 	struct task_struct *task;
1063 	int err = 0;
1064 
1065 	task = get_proc_task(file_inode(file));
1066 	if (!task)
1067 		return -ESRCH;
1068 
1069 	mutex_lock(&oom_adj_mutex);
1070 	if (legacy) {
1071 		if (oom_adj < task->signal->oom_score_adj &&
1072 				!capable(CAP_SYS_RESOURCE)) {
1073 			err = -EACCES;
1074 			goto err_unlock;
1075 		}
1076 		/*
1077 		 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1078 		 * /proc/pid/oom_score_adj instead.
1079 		 */
1080 		pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1081 			  current->comm, task_pid_nr(current), task_pid_nr(task),
1082 			  task_pid_nr(task));
1083 	} else {
1084 		if ((short)oom_adj < task->signal->oom_score_adj_min &&
1085 				!capable(CAP_SYS_RESOURCE)) {
1086 			err = -EACCES;
1087 			goto err_unlock;
1088 		}
1089 	}
1090 
1091 	/*
1092 	 * Make sure we will check other processes sharing the mm if this is
1093 	 * not vfrok which wants its own oom_score_adj.
1094 	 * pin the mm so it doesn't go away and get reused after task_unlock
1095 	 */
1096 	if (!task->vfork_done) {
1097 		struct task_struct *p = find_lock_task_mm(task);
1098 
1099 		if (p) {
1100 			if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1101 				mm = p->mm;
1102 				mmgrab(mm);
1103 			}
1104 			task_unlock(p);
1105 		}
1106 	}
1107 
1108 	task->signal->oom_score_adj = oom_adj;
1109 	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1110 		task->signal->oom_score_adj_min = (short)oom_adj;
1111 	trace_oom_score_adj_update(task);
1112 
1113 	if (mm) {
1114 		struct task_struct *p;
1115 
1116 		rcu_read_lock();
1117 		for_each_process(p) {
1118 			if (same_thread_group(task, p))
1119 				continue;
1120 
1121 			/* do not touch kernel threads or the global init */
1122 			if (p->flags & PF_KTHREAD || is_global_init(p))
1123 				continue;
1124 
1125 			task_lock(p);
1126 			if (!p->vfork_done && process_shares_mm(p, mm)) {
1127 				p->signal->oom_score_adj = oom_adj;
1128 				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1129 					p->signal->oom_score_adj_min = (short)oom_adj;
1130 			}
1131 			task_unlock(p);
1132 		}
1133 		rcu_read_unlock();
1134 		mmdrop(mm);
1135 	}
1136 err_unlock:
1137 	mutex_unlock(&oom_adj_mutex);
1138 	put_task_struct(task);
1139 	return err;
1140 }
1141 
1142 /*
1143  * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1144  * kernels.  The effective policy is defined by oom_score_adj, which has a
1145  * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1146  * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1147  * Processes that become oom disabled via oom_adj will still be oom disabled
1148  * with this implementation.
1149  *
1150  * oom_adj cannot be removed since existing userspace binaries use it.
1151  */
1152 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1153 			     size_t count, loff_t *ppos)
1154 {
1155 	char buffer[PROC_NUMBUF];
1156 	int oom_adj;
1157 	int err;
1158 
1159 	memset(buffer, 0, sizeof(buffer));
1160 	if (count > sizeof(buffer) - 1)
1161 		count = sizeof(buffer) - 1;
1162 	if (copy_from_user(buffer, buf, count)) {
1163 		err = -EFAULT;
1164 		goto out;
1165 	}
1166 
1167 	err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1168 	if (err)
1169 		goto out;
1170 	if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1171 	     oom_adj != OOM_DISABLE) {
1172 		err = -EINVAL;
1173 		goto out;
1174 	}
1175 
1176 	/*
1177 	 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1178 	 * value is always attainable.
1179 	 */
1180 	if (oom_adj == OOM_ADJUST_MAX)
1181 		oom_adj = OOM_SCORE_ADJ_MAX;
1182 	else
1183 		oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1184 
1185 	err = __set_oom_adj(file, oom_adj, true);
1186 out:
1187 	return err < 0 ? err : count;
1188 }
1189 
1190 static const struct file_operations proc_oom_adj_operations = {
1191 	.read		= oom_adj_read,
1192 	.write		= oom_adj_write,
1193 	.llseek		= generic_file_llseek,
1194 };
1195 
1196 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1197 					size_t count, loff_t *ppos)
1198 {
1199 	struct task_struct *task = get_proc_task(file_inode(file));
1200 	char buffer[PROC_NUMBUF];
1201 	short oom_score_adj = OOM_SCORE_ADJ_MIN;
1202 	size_t len;
1203 
1204 	if (!task)
1205 		return -ESRCH;
1206 	oom_score_adj = task->signal->oom_score_adj;
1207 	put_task_struct(task);
1208 	len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1209 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1210 }
1211 
1212 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1213 					size_t count, loff_t *ppos)
1214 {
1215 	char buffer[PROC_NUMBUF];
1216 	int oom_score_adj;
1217 	int err;
1218 
1219 	memset(buffer, 0, sizeof(buffer));
1220 	if (count > sizeof(buffer) - 1)
1221 		count = sizeof(buffer) - 1;
1222 	if (copy_from_user(buffer, buf, count)) {
1223 		err = -EFAULT;
1224 		goto out;
1225 	}
1226 
1227 	err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1228 	if (err)
1229 		goto out;
1230 	if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1231 			oom_score_adj > OOM_SCORE_ADJ_MAX) {
1232 		err = -EINVAL;
1233 		goto out;
1234 	}
1235 
1236 	err = __set_oom_adj(file, oom_score_adj, false);
1237 out:
1238 	return err < 0 ? err : count;
1239 }
1240 
1241 static const struct file_operations proc_oom_score_adj_operations = {
1242 	.read		= oom_score_adj_read,
1243 	.write		= oom_score_adj_write,
1244 	.llseek		= default_llseek,
1245 };
1246 
1247 #ifdef CONFIG_AUDIT
1248 #define TMPBUFLEN 11
1249 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1250 				  size_t count, loff_t *ppos)
1251 {
1252 	struct inode * inode = file_inode(file);
1253 	struct task_struct *task = get_proc_task(inode);
1254 	ssize_t length;
1255 	char tmpbuf[TMPBUFLEN];
1256 
1257 	if (!task)
1258 		return -ESRCH;
1259 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1260 			   from_kuid(file->f_cred->user_ns,
1261 				     audit_get_loginuid(task)));
1262 	put_task_struct(task);
1263 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1264 }
1265 
1266 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1267 				   size_t count, loff_t *ppos)
1268 {
1269 	struct inode * inode = file_inode(file);
1270 	uid_t loginuid;
1271 	kuid_t kloginuid;
1272 	int rv;
1273 
1274 	/* Don't let kthreads write their own loginuid */
1275 	if (current->flags & PF_KTHREAD)
1276 		return -EPERM;
1277 
1278 	rcu_read_lock();
1279 	if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1280 		rcu_read_unlock();
1281 		return -EPERM;
1282 	}
1283 	rcu_read_unlock();
1284 
1285 	if (*ppos != 0) {
1286 		/* No partial writes. */
1287 		return -EINVAL;
1288 	}
1289 
1290 	rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1291 	if (rv < 0)
1292 		return rv;
1293 
1294 	/* is userspace tring to explicitly UNSET the loginuid? */
1295 	if (loginuid == AUDIT_UID_UNSET) {
1296 		kloginuid = INVALID_UID;
1297 	} else {
1298 		kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1299 		if (!uid_valid(kloginuid))
1300 			return -EINVAL;
1301 	}
1302 
1303 	rv = audit_set_loginuid(kloginuid);
1304 	if (rv < 0)
1305 		return rv;
1306 	return count;
1307 }
1308 
1309 static const struct file_operations proc_loginuid_operations = {
1310 	.read		= proc_loginuid_read,
1311 	.write		= proc_loginuid_write,
1312 	.llseek		= generic_file_llseek,
1313 };
1314 
1315 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1316 				  size_t count, loff_t *ppos)
1317 {
1318 	struct inode * inode = file_inode(file);
1319 	struct task_struct *task = get_proc_task(inode);
1320 	ssize_t length;
1321 	char tmpbuf[TMPBUFLEN];
1322 
1323 	if (!task)
1324 		return -ESRCH;
1325 	length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1326 				audit_get_sessionid(task));
1327 	put_task_struct(task);
1328 	return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1329 }
1330 
1331 static const struct file_operations proc_sessionid_operations = {
1332 	.read		= proc_sessionid_read,
1333 	.llseek		= generic_file_llseek,
1334 };
1335 #endif
1336 
1337 #ifdef CONFIG_FAULT_INJECTION
1338 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1339 				      size_t count, loff_t *ppos)
1340 {
1341 	struct task_struct *task = get_proc_task(file_inode(file));
1342 	char buffer[PROC_NUMBUF];
1343 	size_t len;
1344 	int make_it_fail;
1345 
1346 	if (!task)
1347 		return -ESRCH;
1348 	make_it_fail = task->make_it_fail;
1349 	put_task_struct(task);
1350 
1351 	len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1352 
1353 	return simple_read_from_buffer(buf, count, ppos, buffer, len);
1354 }
1355 
1356 static ssize_t proc_fault_inject_write(struct file * file,
1357 			const char __user * buf, size_t count, loff_t *ppos)
1358 {
1359 	struct task_struct *task;
1360 	char buffer[PROC_NUMBUF];
1361 	int make_it_fail;
1362 	int rv;
1363 
1364 	if (!capable(CAP_SYS_RESOURCE))
1365 		return -EPERM;
1366 	memset(buffer, 0, sizeof(buffer));
1367 	if (count > sizeof(buffer) - 1)
1368 		count = sizeof(buffer) - 1;
1369 	if (copy_from_user(buffer, buf, count))
1370 		return -EFAULT;
1371 	rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1372 	if (rv < 0)
1373 		return rv;
1374 	if (make_it_fail < 0 || make_it_fail > 1)
1375 		return -EINVAL;
1376 
1377 	task = get_proc_task(file_inode(file));
1378 	if (!task)
1379 		return -ESRCH;
1380 	task->make_it_fail = make_it_fail;
1381 	put_task_struct(task);
1382 
1383 	return count;
1384 }
1385 
1386 static const struct file_operations proc_fault_inject_operations = {
1387 	.read		= proc_fault_inject_read,
1388 	.write		= proc_fault_inject_write,
1389 	.llseek		= generic_file_llseek,
1390 };
1391 
1392 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1393 				   size_t count, loff_t *ppos)
1394 {
1395 	struct task_struct *task;
1396 	int err;
1397 	unsigned int n;
1398 
1399 	err = kstrtouint_from_user(buf, count, 0, &n);
1400 	if (err)
1401 		return err;
1402 
1403 	task = get_proc_task(file_inode(file));
1404 	if (!task)
1405 		return -ESRCH;
1406 	task->fail_nth = n;
1407 	put_task_struct(task);
1408 
1409 	return count;
1410 }
1411 
1412 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1413 				  size_t count, loff_t *ppos)
1414 {
1415 	struct task_struct *task;
1416 	char numbuf[PROC_NUMBUF];
1417 	ssize_t len;
1418 
1419 	task = get_proc_task(file_inode(file));
1420 	if (!task)
1421 		return -ESRCH;
1422 	len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1423 	put_task_struct(task);
1424 	return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1425 }
1426 
1427 static const struct file_operations proc_fail_nth_operations = {
1428 	.read		= proc_fail_nth_read,
1429 	.write		= proc_fail_nth_write,
1430 };
1431 #endif
1432 
1433 
1434 #ifdef CONFIG_SCHED_DEBUG
1435 /*
1436  * Print out various scheduling related per-task fields:
1437  */
1438 static int sched_show(struct seq_file *m, void *v)
1439 {
1440 	struct inode *inode = m->private;
1441 	struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
1442 	struct task_struct *p;
1443 
1444 	p = get_proc_task(inode);
1445 	if (!p)
1446 		return -ESRCH;
1447 	proc_sched_show_task(p, ns, m);
1448 
1449 	put_task_struct(p);
1450 
1451 	return 0;
1452 }
1453 
1454 static ssize_t
1455 sched_write(struct file *file, const char __user *buf,
1456 	    size_t count, loff_t *offset)
1457 {
1458 	struct inode *inode = file_inode(file);
1459 	struct task_struct *p;
1460 
1461 	p = get_proc_task(inode);
1462 	if (!p)
1463 		return -ESRCH;
1464 	proc_sched_set_task(p);
1465 
1466 	put_task_struct(p);
1467 
1468 	return count;
1469 }
1470 
1471 static int sched_open(struct inode *inode, struct file *filp)
1472 {
1473 	return single_open(filp, sched_show, inode);
1474 }
1475 
1476 static const struct file_operations proc_pid_sched_operations = {
1477 	.open		= sched_open,
1478 	.read		= seq_read,
1479 	.write		= sched_write,
1480 	.llseek		= seq_lseek,
1481 	.release	= single_release,
1482 };
1483 
1484 #endif
1485 
1486 #ifdef CONFIG_SCHED_AUTOGROUP
1487 /*
1488  * Print out autogroup related information:
1489  */
1490 static int sched_autogroup_show(struct seq_file *m, void *v)
1491 {
1492 	struct inode *inode = m->private;
1493 	struct task_struct *p;
1494 
1495 	p = get_proc_task(inode);
1496 	if (!p)
1497 		return -ESRCH;
1498 	proc_sched_autogroup_show_task(p, m);
1499 
1500 	put_task_struct(p);
1501 
1502 	return 0;
1503 }
1504 
1505 static ssize_t
1506 sched_autogroup_write(struct file *file, const char __user *buf,
1507 	    size_t count, loff_t *offset)
1508 {
1509 	struct inode *inode = file_inode(file);
1510 	struct task_struct *p;
1511 	char buffer[PROC_NUMBUF];
1512 	int nice;
1513 	int err;
1514 
1515 	memset(buffer, 0, sizeof(buffer));
1516 	if (count > sizeof(buffer) - 1)
1517 		count = sizeof(buffer) - 1;
1518 	if (copy_from_user(buffer, buf, count))
1519 		return -EFAULT;
1520 
1521 	err = kstrtoint(strstrip(buffer), 0, &nice);
1522 	if (err < 0)
1523 		return err;
1524 
1525 	p = get_proc_task(inode);
1526 	if (!p)
1527 		return -ESRCH;
1528 
1529 	err = proc_sched_autogroup_set_nice(p, nice);
1530 	if (err)
1531 		count = err;
1532 
1533 	put_task_struct(p);
1534 
1535 	return count;
1536 }
1537 
1538 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1539 {
1540 	int ret;
1541 
1542 	ret = single_open(filp, sched_autogroup_show, NULL);
1543 	if (!ret) {
1544 		struct seq_file *m = filp->private_data;
1545 
1546 		m->private = inode;
1547 	}
1548 	return ret;
1549 }
1550 
1551 static const struct file_operations proc_pid_sched_autogroup_operations = {
1552 	.open		= sched_autogroup_open,
1553 	.read		= seq_read,
1554 	.write		= sched_autogroup_write,
1555 	.llseek		= seq_lseek,
1556 	.release	= single_release,
1557 };
1558 
1559 #endif /* CONFIG_SCHED_AUTOGROUP */
1560 
1561 #ifdef CONFIG_TIME_NS
1562 static int timens_offsets_show(struct seq_file *m, void *v)
1563 {
1564 	struct task_struct *p;
1565 
1566 	p = get_proc_task(file_inode(m->file));
1567 	if (!p)
1568 		return -ESRCH;
1569 	proc_timens_show_offsets(p, m);
1570 
1571 	put_task_struct(p);
1572 
1573 	return 0;
1574 }
1575 
1576 static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
1577 				    size_t count, loff_t *ppos)
1578 {
1579 	struct inode *inode = file_inode(file);
1580 	struct proc_timens_offset offsets[2];
1581 	char *kbuf = NULL, *pos, *next_line;
1582 	struct task_struct *p;
1583 	int ret, noffsets;
1584 
1585 	/* Only allow < page size writes at the beginning of the file */
1586 	if ((*ppos != 0) || (count >= PAGE_SIZE))
1587 		return -EINVAL;
1588 
1589 	/* Slurp in the user data */
1590 	kbuf = memdup_user_nul(buf, count);
1591 	if (IS_ERR(kbuf))
1592 		return PTR_ERR(kbuf);
1593 
1594 	/* Parse the user data */
1595 	ret = -EINVAL;
1596 	noffsets = 0;
1597 	for (pos = kbuf; pos; pos = next_line) {
1598 		struct proc_timens_offset *off = &offsets[noffsets];
1599 		char clock[10];
1600 		int err;
1601 
1602 		/* Find the end of line and ensure we don't look past it */
1603 		next_line = strchr(pos, '\n');
1604 		if (next_line) {
1605 			*next_line = '\0';
1606 			next_line++;
1607 			if (*next_line == '\0')
1608 				next_line = NULL;
1609 		}
1610 
1611 		err = sscanf(pos, "%9s %lld %lu", clock,
1612 				&off->val.tv_sec, &off->val.tv_nsec);
1613 		if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
1614 			goto out;
1615 
1616 		clock[sizeof(clock) - 1] = 0;
1617 		if (strcmp(clock, "monotonic") == 0 ||
1618 		    strcmp(clock, __stringify(CLOCK_MONOTONIC)) == 0)
1619 			off->clockid = CLOCK_MONOTONIC;
1620 		else if (strcmp(clock, "boottime") == 0 ||
1621 			 strcmp(clock, __stringify(CLOCK_BOOTTIME)) == 0)
1622 			off->clockid = CLOCK_BOOTTIME;
1623 		else
1624 			goto out;
1625 
1626 		noffsets++;
1627 		if (noffsets == ARRAY_SIZE(offsets)) {
1628 			if (next_line)
1629 				count = next_line - kbuf;
1630 			break;
1631 		}
1632 	}
1633 
1634 	ret = -ESRCH;
1635 	p = get_proc_task(inode);
1636 	if (!p)
1637 		goto out;
1638 	ret = proc_timens_set_offset(file, p, offsets, noffsets);
1639 	put_task_struct(p);
1640 	if (ret)
1641 		goto out;
1642 
1643 	ret = count;
1644 out:
1645 	kfree(kbuf);
1646 	return ret;
1647 }
1648 
1649 static int timens_offsets_open(struct inode *inode, struct file *filp)
1650 {
1651 	return single_open(filp, timens_offsets_show, inode);
1652 }
1653 
1654 static const struct file_operations proc_timens_offsets_operations = {
1655 	.open		= timens_offsets_open,
1656 	.read		= seq_read,
1657 	.write		= timens_offsets_write,
1658 	.llseek		= seq_lseek,
1659 	.release	= single_release,
1660 };
1661 #endif /* CONFIG_TIME_NS */
1662 
1663 static ssize_t comm_write(struct file *file, const char __user *buf,
1664 				size_t count, loff_t *offset)
1665 {
1666 	struct inode *inode = file_inode(file);
1667 	struct task_struct *p;
1668 	char buffer[TASK_COMM_LEN];
1669 	const size_t maxlen = sizeof(buffer) - 1;
1670 
1671 	memset(buffer, 0, sizeof(buffer));
1672 	if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1673 		return -EFAULT;
1674 
1675 	p = get_proc_task(inode);
1676 	if (!p)
1677 		return -ESRCH;
1678 
1679 	if (same_thread_group(current, p)) {
1680 		set_task_comm(p, buffer);
1681 		proc_comm_connector(p);
1682 	}
1683 	else
1684 		count = -EINVAL;
1685 
1686 	put_task_struct(p);
1687 
1688 	return count;
1689 }
1690 
1691 static int comm_show(struct seq_file *m, void *v)
1692 {
1693 	struct inode *inode = m->private;
1694 	struct task_struct *p;
1695 
1696 	p = get_proc_task(inode);
1697 	if (!p)
1698 		return -ESRCH;
1699 
1700 	proc_task_name(m, p, false);
1701 	seq_putc(m, '\n');
1702 
1703 	put_task_struct(p);
1704 
1705 	return 0;
1706 }
1707 
1708 static int comm_open(struct inode *inode, struct file *filp)
1709 {
1710 	return single_open(filp, comm_show, inode);
1711 }
1712 
1713 static const struct file_operations proc_pid_set_comm_operations = {
1714 	.open		= comm_open,
1715 	.read		= seq_read,
1716 	.write		= comm_write,
1717 	.llseek		= seq_lseek,
1718 	.release	= single_release,
1719 };
1720 
1721 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1722 {
1723 	struct task_struct *task;
1724 	struct file *exe_file;
1725 
1726 	task = get_proc_task(d_inode(dentry));
1727 	if (!task)
1728 		return -ENOENT;
1729 	exe_file = get_task_exe_file(task);
1730 	put_task_struct(task);
1731 	if (exe_file) {
1732 		*exe_path = exe_file->f_path;
1733 		path_get(&exe_file->f_path);
1734 		fput(exe_file);
1735 		return 0;
1736 	} else
1737 		return -ENOENT;
1738 }
1739 
1740 static const char *proc_pid_get_link(struct dentry *dentry,
1741 				     struct inode *inode,
1742 				     struct delayed_call *done)
1743 {
1744 	struct path path;
1745 	int error = -EACCES;
1746 
1747 	if (!dentry)
1748 		return ERR_PTR(-ECHILD);
1749 
1750 	/* Are we allowed to snoop on the tasks file descriptors? */
1751 	if (!proc_fd_access_allowed(inode))
1752 		goto out;
1753 
1754 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1755 	if (error)
1756 		goto out;
1757 
1758 	error = nd_jump_link(&path);
1759 out:
1760 	return ERR_PTR(error);
1761 }
1762 
1763 static int do_proc_readlink(const struct path *path, char __user *buffer, int buflen)
1764 {
1765 	char *tmp = kmalloc(PATH_MAX, GFP_KERNEL);
1766 	char *pathname;
1767 	int len;
1768 
1769 	if (!tmp)
1770 		return -ENOMEM;
1771 
1772 	pathname = d_path(path, tmp, PATH_MAX);
1773 	len = PTR_ERR(pathname);
1774 	if (IS_ERR(pathname))
1775 		goto out;
1776 	len = tmp + PATH_MAX - 1 - pathname;
1777 
1778 	if (len > buflen)
1779 		len = buflen;
1780 	if (copy_to_user(buffer, pathname, len))
1781 		len = -EFAULT;
1782  out:
1783 	kfree(tmp);
1784 	return len;
1785 }
1786 
1787 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1788 {
1789 	int error = -EACCES;
1790 	struct inode *inode = d_inode(dentry);
1791 	struct path path;
1792 
1793 	/* Are we allowed to snoop on the tasks file descriptors? */
1794 	if (!proc_fd_access_allowed(inode))
1795 		goto out;
1796 
1797 	error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1798 	if (error)
1799 		goto out;
1800 
1801 	error = do_proc_readlink(&path, buffer, buflen);
1802 	path_put(&path);
1803 out:
1804 	return error;
1805 }
1806 
1807 const struct inode_operations proc_pid_link_inode_operations = {
1808 	.readlink	= proc_pid_readlink,
1809 	.get_link	= proc_pid_get_link,
1810 	.setattr	= proc_setattr,
1811 };
1812 
1813 
1814 /* building an inode */
1815 
1816 void task_dump_owner(struct task_struct *task, umode_t mode,
1817 		     kuid_t *ruid, kgid_t *rgid)
1818 {
1819 	/* Depending on the state of dumpable compute who should own a
1820 	 * proc file for a task.
1821 	 */
1822 	const struct cred *cred;
1823 	kuid_t uid;
1824 	kgid_t gid;
1825 
1826 	if (unlikely(task->flags & PF_KTHREAD)) {
1827 		*ruid = GLOBAL_ROOT_UID;
1828 		*rgid = GLOBAL_ROOT_GID;
1829 		return;
1830 	}
1831 
1832 	/* Default to the tasks effective ownership */
1833 	rcu_read_lock();
1834 	cred = __task_cred(task);
1835 	uid = cred->euid;
1836 	gid = cred->egid;
1837 	rcu_read_unlock();
1838 
1839 	/*
1840 	 * Before the /proc/pid/status file was created the only way to read
1841 	 * the effective uid of a /process was to stat /proc/pid.  Reading
1842 	 * /proc/pid/status is slow enough that procps and other packages
1843 	 * kept stating /proc/pid.  To keep the rules in /proc simple I have
1844 	 * made this apply to all per process world readable and executable
1845 	 * directories.
1846 	 */
1847 	if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1848 		struct mm_struct *mm;
1849 		task_lock(task);
1850 		mm = task->mm;
1851 		/* Make non-dumpable tasks owned by some root */
1852 		if (mm) {
1853 			if (get_dumpable(mm) != SUID_DUMP_USER) {
1854 				struct user_namespace *user_ns = mm->user_ns;
1855 
1856 				uid = make_kuid(user_ns, 0);
1857 				if (!uid_valid(uid))
1858 					uid = GLOBAL_ROOT_UID;
1859 
1860 				gid = make_kgid(user_ns, 0);
1861 				if (!gid_valid(gid))
1862 					gid = GLOBAL_ROOT_GID;
1863 			}
1864 		} else {
1865 			uid = GLOBAL_ROOT_UID;
1866 			gid = GLOBAL_ROOT_GID;
1867 		}
1868 		task_unlock(task);
1869 	}
1870 	*ruid = uid;
1871 	*rgid = gid;
1872 }
1873 
1874 void proc_pid_evict_inode(struct proc_inode *ei)
1875 {
1876 	struct pid *pid = ei->pid;
1877 
1878 	if (S_ISDIR(ei->vfs_inode.i_mode)) {
1879 		spin_lock(&pid->lock);
1880 		hlist_del_init_rcu(&ei->sibling_inodes);
1881 		spin_unlock(&pid->lock);
1882 	}
1883 
1884 	put_pid(pid);
1885 }
1886 
1887 struct inode *proc_pid_make_inode(struct super_block *sb,
1888 				  struct task_struct *task, umode_t mode)
1889 {
1890 	struct inode * inode;
1891 	struct proc_inode *ei;
1892 	struct pid *pid;
1893 
1894 	/* We need a new inode */
1895 
1896 	inode = new_inode(sb);
1897 	if (!inode)
1898 		goto out;
1899 
1900 	/* Common stuff */
1901 	ei = PROC_I(inode);
1902 	inode->i_mode = mode;
1903 	inode->i_ino = get_next_ino();
1904 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1905 	inode->i_op = &proc_def_inode_operations;
1906 
1907 	/*
1908 	 * grab the reference to task.
1909 	 */
1910 	pid = get_task_pid(task, PIDTYPE_PID);
1911 	if (!pid)
1912 		goto out_unlock;
1913 
1914 	/* Let the pid remember us for quick removal */
1915 	ei->pid = pid;
1916 
1917 	task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1918 	security_task_to_inode(task, inode);
1919 
1920 out:
1921 	return inode;
1922 
1923 out_unlock:
1924 	iput(inode);
1925 	return NULL;
1926 }
1927 
1928 /*
1929  * Generating an inode and adding it into @pid->inodes, so that task will
1930  * invalidate inode's dentry before being released.
1931  *
1932  * This helper is used for creating dir-type entries under '/proc' and
1933  * '/proc/<tgid>/task'. Other entries(eg. fd, stat) under '/proc/<tgid>'
1934  * can be released by invalidating '/proc/<tgid>' dentry.
1935  * In theory, dentries under '/proc/<tgid>/task' can also be released by
1936  * invalidating '/proc/<tgid>' dentry, we reserve it to handle single
1937  * thread exiting situation: Any one of threads should invalidate its
1938  * '/proc/<tgid>/task/<pid>' dentry before released.
1939  */
1940 static struct inode *proc_pid_make_base_inode(struct super_block *sb,
1941 				struct task_struct *task, umode_t mode)
1942 {
1943 	struct inode *inode;
1944 	struct proc_inode *ei;
1945 	struct pid *pid;
1946 
1947 	inode = proc_pid_make_inode(sb, task, mode);
1948 	if (!inode)
1949 		return NULL;
1950 
1951 	/* Let proc_flush_pid find this directory inode */
1952 	ei = PROC_I(inode);
1953 	pid = ei->pid;
1954 	spin_lock(&pid->lock);
1955 	hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
1956 	spin_unlock(&pid->lock);
1957 
1958 	return inode;
1959 }
1960 
1961 int pid_getattr(struct mnt_idmap *idmap, const struct path *path,
1962 		struct kstat *stat, u32 request_mask, unsigned int query_flags)
1963 {
1964 	struct inode *inode = d_inode(path->dentry);
1965 	struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
1966 	struct task_struct *task;
1967 
1968 	generic_fillattr(&nop_mnt_idmap, inode, stat);
1969 
1970 	stat->uid = GLOBAL_ROOT_UID;
1971 	stat->gid = GLOBAL_ROOT_GID;
1972 	rcu_read_lock();
1973 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
1974 	if (task) {
1975 		if (!has_pid_permissions(fs_info, task, HIDEPID_INVISIBLE)) {
1976 			rcu_read_unlock();
1977 			/*
1978 			 * This doesn't prevent learning whether PID exists,
1979 			 * it only makes getattr() consistent with readdir().
1980 			 */
1981 			return -ENOENT;
1982 		}
1983 		task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1984 	}
1985 	rcu_read_unlock();
1986 	return 0;
1987 }
1988 
1989 /* dentry stuff */
1990 
1991 /*
1992  * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1993  */
1994 void pid_update_inode(struct task_struct *task, struct inode *inode)
1995 {
1996 	task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1997 
1998 	inode->i_mode &= ~(S_ISUID | S_ISGID);
1999 	security_task_to_inode(task, inode);
2000 }
2001 
2002 /*
2003  * Rewrite the inode's ownerships here because the owning task may have
2004  * performed a setuid(), etc.
2005  *
2006  */
2007 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
2008 {
2009 	struct inode *inode;
2010 	struct task_struct *task;
2011 	int ret = 0;
2012 
2013 	rcu_read_lock();
2014 	inode = d_inode_rcu(dentry);
2015 	if (!inode)
2016 		goto out;
2017 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
2018 
2019 	if (task) {
2020 		pid_update_inode(task, inode);
2021 		ret = 1;
2022 	}
2023 out:
2024 	rcu_read_unlock();
2025 	return ret;
2026 }
2027 
2028 static inline bool proc_inode_is_dead(struct inode *inode)
2029 {
2030 	return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
2031 }
2032 
2033 int pid_delete_dentry(const struct dentry *dentry)
2034 {
2035 	/* Is the task we represent dead?
2036 	 * If so, then don't put the dentry on the lru list,
2037 	 * kill it immediately.
2038 	 */
2039 	return proc_inode_is_dead(d_inode(dentry));
2040 }
2041 
2042 const struct dentry_operations pid_dentry_operations =
2043 {
2044 	.d_revalidate	= pid_revalidate,
2045 	.d_delete	= pid_delete_dentry,
2046 };
2047 
2048 /* Lookups */
2049 
2050 /*
2051  * Fill a directory entry.
2052  *
2053  * If possible create the dcache entry and derive our inode number and
2054  * file type from dcache entry.
2055  *
2056  * Since all of the proc inode numbers are dynamically generated, the inode
2057  * numbers do not exist until the inode is cache.  This means creating
2058  * the dcache entry in readdir is necessary to keep the inode numbers
2059  * reported by readdir in sync with the inode numbers reported
2060  * by stat.
2061  */
2062 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
2063 	const char *name, unsigned int len,
2064 	instantiate_t instantiate, struct task_struct *task, const void *ptr)
2065 {
2066 	struct dentry *child, *dir = file->f_path.dentry;
2067 	struct qstr qname = QSTR_INIT(name, len);
2068 	struct inode *inode;
2069 	unsigned type = DT_UNKNOWN;
2070 	ino_t ino = 1;
2071 
2072 	child = d_hash_and_lookup(dir, &qname);
2073 	if (!child) {
2074 		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2075 		child = d_alloc_parallel(dir, &qname, &wq);
2076 		if (IS_ERR(child))
2077 			goto end_instantiate;
2078 		if (d_in_lookup(child)) {
2079 			struct dentry *res;
2080 			res = instantiate(child, task, ptr);
2081 			d_lookup_done(child);
2082 			if (unlikely(res)) {
2083 				dput(child);
2084 				child = res;
2085 				if (IS_ERR(child))
2086 					goto end_instantiate;
2087 			}
2088 		}
2089 	}
2090 	inode = d_inode(child);
2091 	ino = inode->i_ino;
2092 	type = inode->i_mode >> 12;
2093 	dput(child);
2094 end_instantiate:
2095 	return dir_emit(ctx, name, len, ino, type);
2096 }
2097 
2098 /*
2099  * dname_to_vma_addr - maps a dentry name into two unsigned longs
2100  * which represent vma start and end addresses.
2101  */
2102 static int dname_to_vma_addr(struct dentry *dentry,
2103 			     unsigned long *start, unsigned long *end)
2104 {
2105 	const char *str = dentry->d_name.name;
2106 	unsigned long long sval, eval;
2107 	unsigned int len;
2108 
2109 	if (str[0] == '0' && str[1] != '-')
2110 		return -EINVAL;
2111 	len = _parse_integer(str, 16, &sval);
2112 	if (len & KSTRTOX_OVERFLOW)
2113 		return -EINVAL;
2114 	if (sval != (unsigned long)sval)
2115 		return -EINVAL;
2116 	str += len;
2117 
2118 	if (*str != '-')
2119 		return -EINVAL;
2120 	str++;
2121 
2122 	if (str[0] == '0' && str[1])
2123 		return -EINVAL;
2124 	len = _parse_integer(str, 16, &eval);
2125 	if (len & KSTRTOX_OVERFLOW)
2126 		return -EINVAL;
2127 	if (eval != (unsigned long)eval)
2128 		return -EINVAL;
2129 	str += len;
2130 
2131 	if (*str != '\0')
2132 		return -EINVAL;
2133 
2134 	*start = sval;
2135 	*end = eval;
2136 
2137 	return 0;
2138 }
2139 
2140 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2141 {
2142 	unsigned long vm_start, vm_end;
2143 	bool exact_vma_exists = false;
2144 	struct mm_struct *mm = NULL;
2145 	struct task_struct *task;
2146 	struct inode *inode;
2147 	int status = 0;
2148 
2149 	if (flags & LOOKUP_RCU)
2150 		return -ECHILD;
2151 
2152 	inode = d_inode(dentry);
2153 	task = get_proc_task(inode);
2154 	if (!task)
2155 		goto out_notask;
2156 
2157 	mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
2158 	if (IS_ERR_OR_NULL(mm))
2159 		goto out;
2160 
2161 	if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2162 		status = mmap_read_lock_killable(mm);
2163 		if (!status) {
2164 			exact_vma_exists = !!find_exact_vma(mm, vm_start,
2165 							    vm_end);
2166 			mmap_read_unlock(mm);
2167 		}
2168 	}
2169 
2170 	mmput(mm);
2171 
2172 	if (exact_vma_exists) {
2173 		task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2174 
2175 		security_task_to_inode(task, inode);
2176 		status = 1;
2177 	}
2178 
2179 out:
2180 	put_task_struct(task);
2181 
2182 out_notask:
2183 	return status;
2184 }
2185 
2186 static const struct dentry_operations tid_map_files_dentry_operations = {
2187 	.d_revalidate	= map_files_d_revalidate,
2188 	.d_delete	= pid_delete_dentry,
2189 };
2190 
2191 static int map_files_get_link(struct dentry *dentry, struct path *path)
2192 {
2193 	unsigned long vm_start, vm_end;
2194 	struct vm_area_struct *vma;
2195 	struct task_struct *task;
2196 	struct mm_struct *mm;
2197 	int rc;
2198 
2199 	rc = -ENOENT;
2200 	task = get_proc_task(d_inode(dentry));
2201 	if (!task)
2202 		goto out;
2203 
2204 	mm = get_task_mm(task);
2205 	put_task_struct(task);
2206 	if (!mm)
2207 		goto out;
2208 
2209 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2210 	if (rc)
2211 		goto out_mmput;
2212 
2213 	rc = mmap_read_lock_killable(mm);
2214 	if (rc)
2215 		goto out_mmput;
2216 
2217 	rc = -ENOENT;
2218 	vma = find_exact_vma(mm, vm_start, vm_end);
2219 	if (vma && vma->vm_file) {
2220 		*path = vma->vm_file->f_path;
2221 		path_get(path);
2222 		rc = 0;
2223 	}
2224 	mmap_read_unlock(mm);
2225 
2226 out_mmput:
2227 	mmput(mm);
2228 out:
2229 	return rc;
2230 }
2231 
2232 struct map_files_info {
2233 	unsigned long	start;
2234 	unsigned long	end;
2235 	fmode_t		mode;
2236 };
2237 
2238 /*
2239  * Only allow CAP_SYS_ADMIN and CAP_CHECKPOINT_RESTORE to follow the links, due
2240  * to concerns about how the symlinks may be used to bypass permissions on
2241  * ancestor directories in the path to the file in question.
2242  */
2243 static const char *
2244 proc_map_files_get_link(struct dentry *dentry,
2245 			struct inode *inode,
2246 		        struct delayed_call *done)
2247 {
2248 	if (!checkpoint_restore_ns_capable(&init_user_ns))
2249 		return ERR_PTR(-EPERM);
2250 
2251 	return proc_pid_get_link(dentry, inode, done);
2252 }
2253 
2254 /*
2255  * Identical to proc_pid_link_inode_operations except for get_link()
2256  */
2257 static const struct inode_operations proc_map_files_link_inode_operations = {
2258 	.readlink	= proc_pid_readlink,
2259 	.get_link	= proc_map_files_get_link,
2260 	.setattr	= proc_setattr,
2261 };
2262 
2263 static struct dentry *
2264 proc_map_files_instantiate(struct dentry *dentry,
2265 			   struct task_struct *task, const void *ptr)
2266 {
2267 	fmode_t mode = (fmode_t)(unsigned long)ptr;
2268 	struct proc_inode *ei;
2269 	struct inode *inode;
2270 
2271 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2272 				    ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2273 				    ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2274 	if (!inode)
2275 		return ERR_PTR(-ENOENT);
2276 
2277 	ei = PROC_I(inode);
2278 	ei->op.proc_get_link = map_files_get_link;
2279 
2280 	inode->i_op = &proc_map_files_link_inode_operations;
2281 	inode->i_size = 64;
2282 
2283 	d_set_d_op(dentry, &tid_map_files_dentry_operations);
2284 	return d_splice_alias(inode, dentry);
2285 }
2286 
2287 static struct dentry *proc_map_files_lookup(struct inode *dir,
2288 		struct dentry *dentry, unsigned int flags)
2289 {
2290 	unsigned long vm_start, vm_end;
2291 	struct vm_area_struct *vma;
2292 	struct task_struct *task;
2293 	struct dentry *result;
2294 	struct mm_struct *mm;
2295 
2296 	result = ERR_PTR(-ENOENT);
2297 	task = get_proc_task(dir);
2298 	if (!task)
2299 		goto out;
2300 
2301 	result = ERR_PTR(-EACCES);
2302 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2303 		goto out_put_task;
2304 
2305 	result = ERR_PTR(-ENOENT);
2306 	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2307 		goto out_put_task;
2308 
2309 	mm = get_task_mm(task);
2310 	if (!mm)
2311 		goto out_put_task;
2312 
2313 	result = ERR_PTR(-EINTR);
2314 	if (mmap_read_lock_killable(mm))
2315 		goto out_put_mm;
2316 
2317 	result = ERR_PTR(-ENOENT);
2318 	vma = find_exact_vma(mm, vm_start, vm_end);
2319 	if (!vma)
2320 		goto out_no_vma;
2321 
2322 	if (vma->vm_file)
2323 		result = proc_map_files_instantiate(dentry, task,
2324 				(void *)(unsigned long)vma->vm_file->f_mode);
2325 
2326 out_no_vma:
2327 	mmap_read_unlock(mm);
2328 out_put_mm:
2329 	mmput(mm);
2330 out_put_task:
2331 	put_task_struct(task);
2332 out:
2333 	return result;
2334 }
2335 
2336 static const struct inode_operations proc_map_files_inode_operations = {
2337 	.lookup		= proc_map_files_lookup,
2338 	.permission	= proc_fd_permission,
2339 	.setattr	= proc_setattr,
2340 };
2341 
2342 static int
2343 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2344 {
2345 	struct vm_area_struct *vma;
2346 	struct task_struct *task;
2347 	struct mm_struct *mm;
2348 	unsigned long nr_files, pos, i;
2349 	GENRADIX(struct map_files_info) fa;
2350 	struct map_files_info *p;
2351 	int ret;
2352 	struct vma_iterator vmi;
2353 
2354 	genradix_init(&fa);
2355 
2356 	ret = -ENOENT;
2357 	task = get_proc_task(file_inode(file));
2358 	if (!task)
2359 		goto out;
2360 
2361 	ret = -EACCES;
2362 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2363 		goto out_put_task;
2364 
2365 	ret = 0;
2366 	if (!dir_emit_dots(file, ctx))
2367 		goto out_put_task;
2368 
2369 	mm = get_task_mm(task);
2370 	if (!mm)
2371 		goto out_put_task;
2372 
2373 	ret = mmap_read_lock_killable(mm);
2374 	if (ret) {
2375 		mmput(mm);
2376 		goto out_put_task;
2377 	}
2378 
2379 	nr_files = 0;
2380 
2381 	/*
2382 	 * We need two passes here:
2383 	 *
2384 	 *  1) Collect vmas of mapped files with mmap_lock taken
2385 	 *  2) Release mmap_lock and instantiate entries
2386 	 *
2387 	 * otherwise we get lockdep complained, since filldir()
2388 	 * routine might require mmap_lock taken in might_fault().
2389 	 */
2390 
2391 	pos = 2;
2392 	vma_iter_init(&vmi, mm, 0);
2393 	for_each_vma(vmi, vma) {
2394 		if (!vma->vm_file)
2395 			continue;
2396 		if (++pos <= ctx->pos)
2397 			continue;
2398 
2399 		p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2400 		if (!p) {
2401 			ret = -ENOMEM;
2402 			mmap_read_unlock(mm);
2403 			mmput(mm);
2404 			goto out_put_task;
2405 		}
2406 
2407 		p->start = vma->vm_start;
2408 		p->end = vma->vm_end;
2409 		p->mode = vma->vm_file->f_mode;
2410 	}
2411 	mmap_read_unlock(mm);
2412 	mmput(mm);
2413 
2414 	for (i = 0; i < nr_files; i++) {
2415 		char buf[4 * sizeof(long) + 2];	/* max: %lx-%lx\0 */
2416 		unsigned int len;
2417 
2418 		p = genradix_ptr(&fa, i);
2419 		len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2420 		if (!proc_fill_cache(file, ctx,
2421 				      buf, len,
2422 				      proc_map_files_instantiate,
2423 				      task,
2424 				      (void *)(unsigned long)p->mode))
2425 			break;
2426 		ctx->pos++;
2427 	}
2428 
2429 out_put_task:
2430 	put_task_struct(task);
2431 out:
2432 	genradix_free(&fa);
2433 	return ret;
2434 }
2435 
2436 static const struct file_operations proc_map_files_operations = {
2437 	.read		= generic_read_dir,
2438 	.iterate_shared	= proc_map_files_readdir,
2439 	.llseek		= generic_file_llseek,
2440 };
2441 
2442 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2443 struct timers_private {
2444 	struct pid *pid;
2445 	struct task_struct *task;
2446 	struct sighand_struct *sighand;
2447 	struct pid_namespace *ns;
2448 	unsigned long flags;
2449 };
2450 
2451 static void *timers_start(struct seq_file *m, loff_t *pos)
2452 {
2453 	struct timers_private *tp = m->private;
2454 
2455 	tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2456 	if (!tp->task)
2457 		return ERR_PTR(-ESRCH);
2458 
2459 	tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2460 	if (!tp->sighand)
2461 		return ERR_PTR(-ESRCH);
2462 
2463 	return seq_list_start(&tp->task->signal->posix_timers, *pos);
2464 }
2465 
2466 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2467 {
2468 	struct timers_private *tp = m->private;
2469 	return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2470 }
2471 
2472 static void timers_stop(struct seq_file *m, void *v)
2473 {
2474 	struct timers_private *tp = m->private;
2475 
2476 	if (tp->sighand) {
2477 		unlock_task_sighand(tp->task, &tp->flags);
2478 		tp->sighand = NULL;
2479 	}
2480 
2481 	if (tp->task) {
2482 		put_task_struct(tp->task);
2483 		tp->task = NULL;
2484 	}
2485 }
2486 
2487 static int show_timer(struct seq_file *m, void *v)
2488 {
2489 	struct k_itimer *timer;
2490 	struct timers_private *tp = m->private;
2491 	int notify;
2492 	static const char * const nstr[] = {
2493 		[SIGEV_SIGNAL] = "signal",
2494 		[SIGEV_NONE] = "none",
2495 		[SIGEV_THREAD] = "thread",
2496 	};
2497 
2498 	timer = list_entry((struct list_head *)v, struct k_itimer, list);
2499 	notify = timer->it_sigev_notify;
2500 
2501 	seq_printf(m, "ID: %d\n", timer->it_id);
2502 	seq_printf(m, "signal: %d/%px\n",
2503 		   timer->sigq->info.si_signo,
2504 		   timer->sigq->info.si_value.sival_ptr);
2505 	seq_printf(m, "notify: %s/%s.%d\n",
2506 		   nstr[notify & ~SIGEV_THREAD_ID],
2507 		   (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2508 		   pid_nr_ns(timer->it_pid, tp->ns));
2509 	seq_printf(m, "ClockID: %d\n", timer->it_clock);
2510 
2511 	return 0;
2512 }
2513 
2514 static const struct seq_operations proc_timers_seq_ops = {
2515 	.start	= timers_start,
2516 	.next	= timers_next,
2517 	.stop	= timers_stop,
2518 	.show	= show_timer,
2519 };
2520 
2521 static int proc_timers_open(struct inode *inode, struct file *file)
2522 {
2523 	struct timers_private *tp;
2524 
2525 	tp = __seq_open_private(file, &proc_timers_seq_ops,
2526 			sizeof(struct timers_private));
2527 	if (!tp)
2528 		return -ENOMEM;
2529 
2530 	tp->pid = proc_pid(inode);
2531 	tp->ns = proc_pid_ns(inode->i_sb);
2532 	return 0;
2533 }
2534 
2535 static const struct file_operations proc_timers_operations = {
2536 	.open		= proc_timers_open,
2537 	.read		= seq_read,
2538 	.llseek		= seq_lseek,
2539 	.release	= seq_release_private,
2540 };
2541 #endif
2542 
2543 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2544 					size_t count, loff_t *offset)
2545 {
2546 	struct inode *inode = file_inode(file);
2547 	struct task_struct *p;
2548 	u64 slack_ns;
2549 	int err;
2550 
2551 	err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2552 	if (err < 0)
2553 		return err;
2554 
2555 	p = get_proc_task(inode);
2556 	if (!p)
2557 		return -ESRCH;
2558 
2559 	if (p != current) {
2560 		rcu_read_lock();
2561 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2562 			rcu_read_unlock();
2563 			count = -EPERM;
2564 			goto out;
2565 		}
2566 		rcu_read_unlock();
2567 
2568 		err = security_task_setscheduler(p);
2569 		if (err) {
2570 			count = err;
2571 			goto out;
2572 		}
2573 	}
2574 
2575 	task_lock(p);
2576 	if (slack_ns == 0)
2577 		p->timer_slack_ns = p->default_timer_slack_ns;
2578 	else
2579 		p->timer_slack_ns = slack_ns;
2580 	task_unlock(p);
2581 
2582 out:
2583 	put_task_struct(p);
2584 
2585 	return count;
2586 }
2587 
2588 static int timerslack_ns_show(struct seq_file *m, void *v)
2589 {
2590 	struct inode *inode = m->private;
2591 	struct task_struct *p;
2592 	int err = 0;
2593 
2594 	p = get_proc_task(inode);
2595 	if (!p)
2596 		return -ESRCH;
2597 
2598 	if (p != current) {
2599 		rcu_read_lock();
2600 		if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2601 			rcu_read_unlock();
2602 			err = -EPERM;
2603 			goto out;
2604 		}
2605 		rcu_read_unlock();
2606 
2607 		err = security_task_getscheduler(p);
2608 		if (err)
2609 			goto out;
2610 	}
2611 
2612 	task_lock(p);
2613 	seq_printf(m, "%llu\n", p->timer_slack_ns);
2614 	task_unlock(p);
2615 
2616 out:
2617 	put_task_struct(p);
2618 
2619 	return err;
2620 }
2621 
2622 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2623 {
2624 	return single_open(filp, timerslack_ns_show, inode);
2625 }
2626 
2627 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2628 	.open		= timerslack_ns_open,
2629 	.read		= seq_read,
2630 	.write		= timerslack_ns_write,
2631 	.llseek		= seq_lseek,
2632 	.release	= single_release,
2633 };
2634 
2635 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2636 	struct task_struct *task, const void *ptr)
2637 {
2638 	const struct pid_entry *p = ptr;
2639 	struct inode *inode;
2640 	struct proc_inode *ei;
2641 
2642 	inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2643 	if (!inode)
2644 		return ERR_PTR(-ENOENT);
2645 
2646 	ei = PROC_I(inode);
2647 	if (S_ISDIR(inode->i_mode))
2648 		set_nlink(inode, 2);	/* Use getattr to fix if necessary */
2649 	if (p->iop)
2650 		inode->i_op = p->iop;
2651 	if (p->fop)
2652 		inode->i_fop = p->fop;
2653 	ei->op = p->op;
2654 	pid_update_inode(task, inode);
2655 	d_set_d_op(dentry, &pid_dentry_operations);
2656 	return d_splice_alias(inode, dentry);
2657 }
2658 
2659 static struct dentry *proc_pident_lookup(struct inode *dir,
2660 					 struct dentry *dentry,
2661 					 const struct pid_entry *p,
2662 					 const struct pid_entry *end)
2663 {
2664 	struct task_struct *task = get_proc_task(dir);
2665 	struct dentry *res = ERR_PTR(-ENOENT);
2666 
2667 	if (!task)
2668 		goto out_no_task;
2669 
2670 	/*
2671 	 * Yes, it does not scale. And it should not. Don't add
2672 	 * new entries into /proc/<tgid>/ without very good reasons.
2673 	 */
2674 	for (; p < end; p++) {
2675 		if (p->len != dentry->d_name.len)
2676 			continue;
2677 		if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2678 			res = proc_pident_instantiate(dentry, task, p);
2679 			break;
2680 		}
2681 	}
2682 	put_task_struct(task);
2683 out_no_task:
2684 	return res;
2685 }
2686 
2687 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2688 		const struct pid_entry *ents, unsigned int nents)
2689 {
2690 	struct task_struct *task = get_proc_task(file_inode(file));
2691 	const struct pid_entry *p;
2692 
2693 	if (!task)
2694 		return -ENOENT;
2695 
2696 	if (!dir_emit_dots(file, ctx))
2697 		goto out;
2698 
2699 	if (ctx->pos >= nents + 2)
2700 		goto out;
2701 
2702 	for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2703 		if (!proc_fill_cache(file, ctx, p->name, p->len,
2704 				proc_pident_instantiate, task, p))
2705 			break;
2706 		ctx->pos++;
2707 	}
2708 out:
2709 	put_task_struct(task);
2710 	return 0;
2711 }
2712 
2713 #ifdef CONFIG_SECURITY
2714 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2715 {
2716 	file->private_data = NULL;
2717 	__mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2718 	return 0;
2719 }
2720 
2721 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2722 				  size_t count, loff_t *ppos)
2723 {
2724 	struct inode * inode = file_inode(file);
2725 	char *p = NULL;
2726 	ssize_t length;
2727 	struct task_struct *task = get_proc_task(inode);
2728 
2729 	if (!task)
2730 		return -ESRCH;
2731 
2732 	length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2733 				      file->f_path.dentry->d_name.name,
2734 				      &p);
2735 	put_task_struct(task);
2736 	if (length > 0)
2737 		length = simple_read_from_buffer(buf, count, ppos, p, length);
2738 	kfree(p);
2739 	return length;
2740 }
2741 
2742 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2743 				   size_t count, loff_t *ppos)
2744 {
2745 	struct inode * inode = file_inode(file);
2746 	struct task_struct *task;
2747 	void *page;
2748 	int rv;
2749 
2750 	/* A task may only write when it was the opener. */
2751 	if (file->private_data != current->mm)
2752 		return -EPERM;
2753 
2754 	rcu_read_lock();
2755 	task = pid_task(proc_pid(inode), PIDTYPE_PID);
2756 	if (!task) {
2757 		rcu_read_unlock();
2758 		return -ESRCH;
2759 	}
2760 	/* A task may only write its own attributes. */
2761 	if (current != task) {
2762 		rcu_read_unlock();
2763 		return -EACCES;
2764 	}
2765 	/* Prevent changes to overridden credentials. */
2766 	if (current_cred() != current_real_cred()) {
2767 		rcu_read_unlock();
2768 		return -EBUSY;
2769 	}
2770 	rcu_read_unlock();
2771 
2772 	if (count > PAGE_SIZE)
2773 		count = PAGE_SIZE;
2774 
2775 	/* No partial writes. */
2776 	if (*ppos != 0)
2777 		return -EINVAL;
2778 
2779 	page = memdup_user(buf, count);
2780 	if (IS_ERR(page)) {
2781 		rv = PTR_ERR(page);
2782 		goto out;
2783 	}
2784 
2785 	/* Guard against adverse ptrace interaction */
2786 	rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2787 	if (rv < 0)
2788 		goto out_free;
2789 
2790 	rv = security_setprocattr(PROC_I(inode)->op.lsm,
2791 				  file->f_path.dentry->d_name.name, page,
2792 				  count);
2793 	mutex_unlock(&current->signal->cred_guard_mutex);
2794 out_free:
2795 	kfree(page);
2796 out:
2797 	return rv;
2798 }
2799 
2800 static const struct file_operations proc_pid_attr_operations = {
2801 	.open		= proc_pid_attr_open,
2802 	.read		= proc_pid_attr_read,
2803 	.write		= proc_pid_attr_write,
2804 	.llseek		= generic_file_llseek,
2805 	.release	= mem_release,
2806 };
2807 
2808 #define LSM_DIR_OPS(LSM) \
2809 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2810 			     struct dir_context *ctx) \
2811 { \
2812 	return proc_pident_readdir(filp, ctx, \
2813 				   LSM##_attr_dir_stuff, \
2814 				   ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2815 } \
2816 \
2817 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2818 	.read		= generic_read_dir, \
2819 	.iterate	= proc_##LSM##_attr_dir_iterate, \
2820 	.llseek		= default_llseek, \
2821 }; \
2822 \
2823 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2824 				struct dentry *dentry, unsigned int flags) \
2825 { \
2826 	return proc_pident_lookup(dir, dentry, \
2827 				  LSM##_attr_dir_stuff, \
2828 				  LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2829 } \
2830 \
2831 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2832 	.lookup		= proc_##LSM##_attr_dir_lookup, \
2833 	.getattr	= pid_getattr, \
2834 	.setattr	= proc_setattr, \
2835 }
2836 
2837 #ifdef CONFIG_SECURITY_SMACK
2838 static const struct pid_entry smack_attr_dir_stuff[] = {
2839 	ATTR("smack", "current",	0666),
2840 };
2841 LSM_DIR_OPS(smack);
2842 #endif
2843 
2844 #ifdef CONFIG_SECURITY_APPARMOR
2845 static const struct pid_entry apparmor_attr_dir_stuff[] = {
2846 	ATTR("apparmor", "current",	0666),
2847 	ATTR("apparmor", "prev",	0444),
2848 	ATTR("apparmor", "exec",	0666),
2849 };
2850 LSM_DIR_OPS(apparmor);
2851 #endif
2852 
2853 static const struct pid_entry attr_dir_stuff[] = {
2854 	ATTR(NULL, "current",		0666),
2855 	ATTR(NULL, "prev",		0444),
2856 	ATTR(NULL, "exec",		0666),
2857 	ATTR(NULL, "fscreate",		0666),
2858 	ATTR(NULL, "keycreate",		0666),
2859 	ATTR(NULL, "sockcreate",	0666),
2860 #ifdef CONFIG_SECURITY_SMACK
2861 	DIR("smack",			0555,
2862 	    proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2863 #endif
2864 #ifdef CONFIG_SECURITY_APPARMOR
2865 	DIR("apparmor",			0555,
2866 	    proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
2867 #endif
2868 };
2869 
2870 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2871 {
2872 	return proc_pident_readdir(file, ctx,
2873 				   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2874 }
2875 
2876 static const struct file_operations proc_attr_dir_operations = {
2877 	.read		= generic_read_dir,
2878 	.iterate_shared	= proc_attr_dir_readdir,
2879 	.llseek		= generic_file_llseek,
2880 };
2881 
2882 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2883 				struct dentry *dentry, unsigned int flags)
2884 {
2885 	return proc_pident_lookup(dir, dentry,
2886 				  attr_dir_stuff,
2887 				  attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
2888 }
2889 
2890 static const struct inode_operations proc_attr_dir_inode_operations = {
2891 	.lookup		= proc_attr_dir_lookup,
2892 	.getattr	= pid_getattr,
2893 	.setattr	= proc_setattr,
2894 };
2895 
2896 #endif
2897 
2898 #ifdef CONFIG_ELF_CORE
2899 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2900 					 size_t count, loff_t *ppos)
2901 {
2902 	struct task_struct *task = get_proc_task(file_inode(file));
2903 	struct mm_struct *mm;
2904 	char buffer[PROC_NUMBUF];
2905 	size_t len;
2906 	int ret;
2907 
2908 	if (!task)
2909 		return -ESRCH;
2910 
2911 	ret = 0;
2912 	mm = get_task_mm(task);
2913 	if (mm) {
2914 		len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2915 			       ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2916 				MMF_DUMP_FILTER_SHIFT));
2917 		mmput(mm);
2918 		ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2919 	}
2920 
2921 	put_task_struct(task);
2922 
2923 	return ret;
2924 }
2925 
2926 static ssize_t proc_coredump_filter_write(struct file *file,
2927 					  const char __user *buf,
2928 					  size_t count,
2929 					  loff_t *ppos)
2930 {
2931 	struct task_struct *task;
2932 	struct mm_struct *mm;
2933 	unsigned int val;
2934 	int ret;
2935 	int i;
2936 	unsigned long mask;
2937 
2938 	ret = kstrtouint_from_user(buf, count, 0, &val);
2939 	if (ret < 0)
2940 		return ret;
2941 
2942 	ret = -ESRCH;
2943 	task = get_proc_task(file_inode(file));
2944 	if (!task)
2945 		goto out_no_task;
2946 
2947 	mm = get_task_mm(task);
2948 	if (!mm)
2949 		goto out_no_mm;
2950 	ret = 0;
2951 
2952 	for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2953 		if (val & mask)
2954 			set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2955 		else
2956 			clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2957 	}
2958 
2959 	mmput(mm);
2960  out_no_mm:
2961 	put_task_struct(task);
2962  out_no_task:
2963 	if (ret < 0)
2964 		return ret;
2965 	return count;
2966 }
2967 
2968 static const struct file_operations proc_coredump_filter_operations = {
2969 	.read		= proc_coredump_filter_read,
2970 	.write		= proc_coredump_filter_write,
2971 	.llseek		= generic_file_llseek,
2972 };
2973 #endif
2974 
2975 #ifdef CONFIG_TASK_IO_ACCOUNTING
2976 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2977 {
2978 	struct task_io_accounting acct = task->ioac;
2979 	unsigned long flags;
2980 	int result;
2981 
2982 	result = down_read_killable(&task->signal->exec_update_lock);
2983 	if (result)
2984 		return result;
2985 
2986 	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2987 		result = -EACCES;
2988 		goto out_unlock;
2989 	}
2990 
2991 	if (whole && lock_task_sighand(task, &flags)) {
2992 		struct task_struct *t = task;
2993 
2994 		task_io_accounting_add(&acct, &task->signal->ioac);
2995 		while_each_thread(task, t)
2996 			task_io_accounting_add(&acct, &t->ioac);
2997 
2998 		unlock_task_sighand(task, &flags);
2999 	}
3000 	seq_printf(m,
3001 		   "rchar: %llu\n"
3002 		   "wchar: %llu\n"
3003 		   "syscr: %llu\n"
3004 		   "syscw: %llu\n"
3005 		   "read_bytes: %llu\n"
3006 		   "write_bytes: %llu\n"
3007 		   "cancelled_write_bytes: %llu\n",
3008 		   (unsigned long long)acct.rchar,
3009 		   (unsigned long long)acct.wchar,
3010 		   (unsigned long long)acct.syscr,
3011 		   (unsigned long long)acct.syscw,
3012 		   (unsigned long long)acct.read_bytes,
3013 		   (unsigned long long)acct.write_bytes,
3014 		   (unsigned long long)acct.cancelled_write_bytes);
3015 	result = 0;
3016 
3017 out_unlock:
3018 	up_read(&task->signal->exec_update_lock);
3019 	return result;
3020 }
3021 
3022 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3023 				  struct pid *pid, struct task_struct *task)
3024 {
3025 	return do_io_accounting(task, m, 0);
3026 }
3027 
3028 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3029 				   struct pid *pid, struct task_struct *task)
3030 {
3031 	return do_io_accounting(task, m, 1);
3032 }
3033 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3034 
3035 #ifdef CONFIG_USER_NS
3036 static int proc_id_map_open(struct inode *inode, struct file *file,
3037 	const struct seq_operations *seq_ops)
3038 {
3039 	struct user_namespace *ns = NULL;
3040 	struct task_struct *task;
3041 	struct seq_file *seq;
3042 	int ret = -EINVAL;
3043 
3044 	task = get_proc_task(inode);
3045 	if (task) {
3046 		rcu_read_lock();
3047 		ns = get_user_ns(task_cred_xxx(task, user_ns));
3048 		rcu_read_unlock();
3049 		put_task_struct(task);
3050 	}
3051 	if (!ns)
3052 		goto err;
3053 
3054 	ret = seq_open(file, seq_ops);
3055 	if (ret)
3056 		goto err_put_ns;
3057 
3058 	seq = file->private_data;
3059 	seq->private = ns;
3060 
3061 	return 0;
3062 err_put_ns:
3063 	put_user_ns(ns);
3064 err:
3065 	return ret;
3066 }
3067 
3068 static int proc_id_map_release(struct inode *inode, struct file *file)
3069 {
3070 	struct seq_file *seq = file->private_data;
3071 	struct user_namespace *ns = seq->private;
3072 	put_user_ns(ns);
3073 	return seq_release(inode, file);
3074 }
3075 
3076 static int proc_uid_map_open(struct inode *inode, struct file *file)
3077 {
3078 	return proc_id_map_open(inode, file, &proc_uid_seq_operations);
3079 }
3080 
3081 static int proc_gid_map_open(struct inode *inode, struct file *file)
3082 {
3083 	return proc_id_map_open(inode, file, &proc_gid_seq_operations);
3084 }
3085 
3086 static int proc_projid_map_open(struct inode *inode, struct file *file)
3087 {
3088 	return proc_id_map_open(inode, file, &proc_projid_seq_operations);
3089 }
3090 
3091 static const struct file_operations proc_uid_map_operations = {
3092 	.open		= proc_uid_map_open,
3093 	.write		= proc_uid_map_write,
3094 	.read		= seq_read,
3095 	.llseek		= seq_lseek,
3096 	.release	= proc_id_map_release,
3097 };
3098 
3099 static const struct file_operations proc_gid_map_operations = {
3100 	.open		= proc_gid_map_open,
3101 	.write		= proc_gid_map_write,
3102 	.read		= seq_read,
3103 	.llseek		= seq_lseek,
3104 	.release	= proc_id_map_release,
3105 };
3106 
3107 static const struct file_operations proc_projid_map_operations = {
3108 	.open		= proc_projid_map_open,
3109 	.write		= proc_projid_map_write,
3110 	.read		= seq_read,
3111 	.llseek		= seq_lseek,
3112 	.release	= proc_id_map_release,
3113 };
3114 
3115 static int proc_setgroups_open(struct inode *inode, struct file *file)
3116 {
3117 	struct user_namespace *ns = NULL;
3118 	struct task_struct *task;
3119 	int ret;
3120 
3121 	ret = -ESRCH;
3122 	task = get_proc_task(inode);
3123 	if (task) {
3124 		rcu_read_lock();
3125 		ns = get_user_ns(task_cred_xxx(task, user_ns));
3126 		rcu_read_unlock();
3127 		put_task_struct(task);
3128 	}
3129 	if (!ns)
3130 		goto err;
3131 
3132 	if (file->f_mode & FMODE_WRITE) {
3133 		ret = -EACCES;
3134 		if (!ns_capable(ns, CAP_SYS_ADMIN))
3135 			goto err_put_ns;
3136 	}
3137 
3138 	ret = single_open(file, &proc_setgroups_show, ns);
3139 	if (ret)
3140 		goto err_put_ns;
3141 
3142 	return 0;
3143 err_put_ns:
3144 	put_user_ns(ns);
3145 err:
3146 	return ret;
3147 }
3148 
3149 static int proc_setgroups_release(struct inode *inode, struct file *file)
3150 {
3151 	struct seq_file *seq = file->private_data;
3152 	struct user_namespace *ns = seq->private;
3153 	int ret = single_release(inode, file);
3154 	put_user_ns(ns);
3155 	return ret;
3156 }
3157 
3158 static const struct file_operations proc_setgroups_operations = {
3159 	.open		= proc_setgroups_open,
3160 	.write		= proc_setgroups_write,
3161 	.read		= seq_read,
3162 	.llseek		= seq_lseek,
3163 	.release	= proc_setgroups_release,
3164 };
3165 #endif /* CONFIG_USER_NS */
3166 
3167 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3168 				struct pid *pid, struct task_struct *task)
3169 {
3170 	int err = lock_trace(task);
3171 	if (!err) {
3172 		seq_printf(m, "%08x\n", task->personality);
3173 		unlock_trace(task);
3174 	}
3175 	return err;
3176 }
3177 
3178 #ifdef CONFIG_LIVEPATCH
3179 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
3180 				struct pid *pid, struct task_struct *task)
3181 {
3182 	seq_printf(m, "%d\n", task->patch_state);
3183 	return 0;
3184 }
3185 #endif /* CONFIG_LIVEPATCH */
3186 
3187 #ifdef CONFIG_KSM
3188 static int proc_pid_ksm_merging_pages(struct seq_file *m, struct pid_namespace *ns,
3189 				struct pid *pid, struct task_struct *task)
3190 {
3191 	struct mm_struct *mm;
3192 
3193 	mm = get_task_mm(task);
3194 	if (mm) {
3195 		seq_printf(m, "%lu\n", mm->ksm_merging_pages);
3196 		mmput(mm);
3197 	}
3198 
3199 	return 0;
3200 }
3201 static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
3202 				struct pid *pid, struct task_struct *task)
3203 {
3204 	struct mm_struct *mm;
3205 
3206 	mm = get_task_mm(task);
3207 	if (mm) {
3208 		seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
3209 		mmput(mm);
3210 	}
3211 
3212 	return 0;
3213 }
3214 #endif /* CONFIG_KSM */
3215 
3216 #ifdef CONFIG_STACKLEAK_METRICS
3217 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3218 				struct pid *pid, struct task_struct *task)
3219 {
3220 	unsigned long prev_depth = THREAD_SIZE -
3221 				(task->prev_lowest_stack & (THREAD_SIZE - 1));
3222 	unsigned long depth = THREAD_SIZE -
3223 				(task->lowest_stack & (THREAD_SIZE - 1));
3224 
3225 	seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3226 							prev_depth, depth);
3227 	return 0;
3228 }
3229 #endif /* CONFIG_STACKLEAK_METRICS */
3230 
3231 /*
3232  * Thread groups
3233  */
3234 static const struct file_operations proc_task_operations;
3235 static const struct inode_operations proc_task_inode_operations;
3236 
3237 static const struct pid_entry tgid_base_stuff[] = {
3238 	DIR("task",       S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3239 	DIR("fd",         S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3240 	DIR("map_files",  S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3241 	DIR("fdinfo",     S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3242 	DIR("ns",	  S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3243 #ifdef CONFIG_NET
3244 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3245 #endif
3246 	REG("environ",    S_IRUSR, proc_environ_operations),
3247 	REG("auxv",       S_IRUSR, proc_auxv_operations),
3248 	ONE("status",     S_IRUGO, proc_pid_status),
3249 	ONE("personality", S_IRUSR, proc_pid_personality),
3250 	ONE("limits",	  S_IRUGO, proc_pid_limits),
3251 #ifdef CONFIG_SCHED_DEBUG
3252 	REG("sched",      S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3253 #endif
3254 #ifdef CONFIG_SCHED_AUTOGROUP
3255 	REG("autogroup",  S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3256 #endif
3257 #ifdef CONFIG_TIME_NS
3258 	REG("timens_offsets",  S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
3259 #endif
3260 	REG("comm",      S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3261 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3262 	ONE("syscall",    S_IRUSR, proc_pid_syscall),
3263 #endif
3264 	REG("cmdline",    S_IRUGO, proc_pid_cmdline_ops),
3265 	ONE("stat",       S_IRUGO, proc_tgid_stat),
3266 	ONE("statm",      S_IRUGO, proc_pid_statm),
3267 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
3268 #ifdef CONFIG_NUMA
3269 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
3270 #endif
3271 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
3272 	LNK("cwd",        proc_cwd_link),
3273 	LNK("root",       proc_root_link),
3274 	LNK("exe",        proc_exe_link),
3275 	REG("mounts",     S_IRUGO, proc_mounts_operations),
3276 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3277 	REG("mountstats", S_IRUSR, proc_mountstats_operations),
3278 #ifdef CONFIG_PROC_PAGE_MONITOR
3279 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3280 	REG("smaps",      S_IRUGO, proc_pid_smaps_operations),
3281 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3282 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
3283 #endif
3284 #ifdef CONFIG_SECURITY
3285 	DIR("attr",       S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3286 #endif
3287 #ifdef CONFIG_KALLSYMS
3288 	ONE("wchan",      S_IRUGO, proc_pid_wchan),
3289 #endif
3290 #ifdef CONFIG_STACKTRACE
3291 	ONE("stack",      S_IRUSR, proc_pid_stack),
3292 #endif
3293 #ifdef CONFIG_SCHED_INFO
3294 	ONE("schedstat",  S_IRUGO, proc_pid_schedstat),
3295 #endif
3296 #ifdef CONFIG_LATENCYTOP
3297 	REG("latency",  S_IRUGO, proc_lstats_operations),
3298 #endif
3299 #ifdef CONFIG_PROC_PID_CPUSET
3300 	ONE("cpuset",     S_IRUGO, proc_cpuset_show),
3301 #endif
3302 #ifdef CONFIG_CGROUPS
3303 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3304 #endif
3305 #ifdef CONFIG_PROC_CPU_RESCTRL
3306 	ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3307 #endif
3308 	ONE("oom_score",  S_IRUGO, proc_oom_score),
3309 	REG("oom_adj",    S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3310 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3311 #ifdef CONFIG_AUDIT
3312 	REG("loginuid",   S_IWUSR|S_IRUGO, proc_loginuid_operations),
3313 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3314 #endif
3315 #ifdef CONFIG_FAULT_INJECTION
3316 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3317 	REG("fail-nth", 0644, proc_fail_nth_operations),
3318 #endif
3319 #ifdef CONFIG_ELF_CORE
3320 	REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3321 #endif
3322 #ifdef CONFIG_TASK_IO_ACCOUNTING
3323 	ONE("io",	S_IRUSR, proc_tgid_io_accounting),
3324 #endif
3325 #ifdef CONFIG_USER_NS
3326 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
3327 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3328 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3329 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
3330 #endif
3331 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3332 	REG("timers",	  S_IRUGO, proc_timers_operations),
3333 #endif
3334 	REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3335 #ifdef CONFIG_LIVEPATCH
3336 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
3337 #endif
3338 #ifdef CONFIG_STACKLEAK_METRICS
3339 	ONE("stack_depth", S_IRUGO, proc_stack_depth),
3340 #endif
3341 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3342 	ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3343 #endif
3344 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
3345 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
3346 #endif
3347 #ifdef CONFIG_KSM
3348 	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
3349 	ONE("ksm_stat",  S_IRUSR, proc_pid_ksm_stat),
3350 #endif
3351 };
3352 
3353 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3354 {
3355 	return proc_pident_readdir(file, ctx,
3356 				   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3357 }
3358 
3359 static const struct file_operations proc_tgid_base_operations = {
3360 	.read		= generic_read_dir,
3361 	.iterate_shared	= proc_tgid_base_readdir,
3362 	.llseek		= generic_file_llseek,
3363 };
3364 
3365 struct pid *tgid_pidfd_to_pid(const struct file *file)
3366 {
3367 	if (file->f_op != &proc_tgid_base_operations)
3368 		return ERR_PTR(-EBADF);
3369 
3370 	return proc_pid(file_inode(file));
3371 }
3372 
3373 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3374 {
3375 	return proc_pident_lookup(dir, dentry,
3376 				  tgid_base_stuff,
3377 				  tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
3378 }
3379 
3380 static const struct inode_operations proc_tgid_base_inode_operations = {
3381 	.lookup		= proc_tgid_base_lookup,
3382 	.getattr	= pid_getattr,
3383 	.setattr	= proc_setattr,
3384 	.permission	= proc_pid_permission,
3385 };
3386 
3387 /**
3388  * proc_flush_pid -  Remove dcache entries for @pid from the /proc dcache.
3389  * @pid: pid that should be flushed.
3390  *
3391  * This function walks a list of inodes (that belong to any proc
3392  * filesystem) that are attached to the pid and flushes them from
3393  * the dentry cache.
3394  *
3395  * It is safe and reasonable to cache /proc entries for a task until
3396  * that task exits.  After that they just clog up the dcache with
3397  * useless entries, possibly causing useful dcache entries to be
3398  * flushed instead.  This routine is provided to flush those useless
3399  * dcache entries when a process is reaped.
3400  *
3401  * NOTE: This routine is just an optimization so it does not guarantee
3402  *       that no dcache entries will exist after a process is reaped
3403  *       it just makes it very unlikely that any will persist.
3404  */
3405 
3406 void proc_flush_pid(struct pid *pid)
3407 {
3408 	proc_invalidate_siblings_dcache(&pid->inodes, &pid->lock);
3409 }
3410 
3411 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3412 				   struct task_struct *task, const void *ptr)
3413 {
3414 	struct inode *inode;
3415 
3416 	inode = proc_pid_make_base_inode(dentry->d_sb, task,
3417 					 S_IFDIR | S_IRUGO | S_IXUGO);
3418 	if (!inode)
3419 		return ERR_PTR(-ENOENT);
3420 
3421 	inode->i_op = &proc_tgid_base_inode_operations;
3422 	inode->i_fop = &proc_tgid_base_operations;
3423 	inode->i_flags|=S_IMMUTABLE;
3424 
3425 	set_nlink(inode, nlink_tgid);
3426 	pid_update_inode(task, inode);
3427 
3428 	d_set_d_op(dentry, &pid_dentry_operations);
3429 	return d_splice_alias(inode, dentry);
3430 }
3431 
3432 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3433 {
3434 	struct task_struct *task;
3435 	unsigned tgid;
3436 	struct proc_fs_info *fs_info;
3437 	struct pid_namespace *ns;
3438 	struct dentry *result = ERR_PTR(-ENOENT);
3439 
3440 	tgid = name_to_int(&dentry->d_name);
3441 	if (tgid == ~0U)
3442 		goto out;
3443 
3444 	fs_info = proc_sb_info(dentry->d_sb);
3445 	ns = fs_info->pid_ns;
3446 	rcu_read_lock();
3447 	task = find_task_by_pid_ns(tgid, ns);
3448 	if (task)
3449 		get_task_struct(task);
3450 	rcu_read_unlock();
3451 	if (!task)
3452 		goto out;
3453 
3454 	/* Limit procfs to only ptraceable tasks */
3455 	if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE) {
3456 		if (!has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS))
3457 			goto out_put_task;
3458 	}
3459 
3460 	result = proc_pid_instantiate(dentry, task, NULL);
3461 out_put_task:
3462 	put_task_struct(task);
3463 out:
3464 	return result;
3465 }
3466 
3467 /*
3468  * Find the first task with tgid >= tgid
3469  *
3470  */
3471 struct tgid_iter {
3472 	unsigned int tgid;
3473 	struct task_struct *task;
3474 };
3475 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3476 {
3477 	struct pid *pid;
3478 
3479 	if (iter.task)
3480 		put_task_struct(iter.task);
3481 	rcu_read_lock();
3482 retry:
3483 	iter.task = NULL;
3484 	pid = find_ge_pid(iter.tgid, ns);
3485 	if (pid) {
3486 		iter.tgid = pid_nr_ns(pid, ns);
3487 		iter.task = pid_task(pid, PIDTYPE_TGID);
3488 		if (!iter.task) {
3489 			iter.tgid += 1;
3490 			goto retry;
3491 		}
3492 		get_task_struct(iter.task);
3493 	}
3494 	rcu_read_unlock();
3495 	return iter;
3496 }
3497 
3498 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3499 
3500 /* for the /proc/ directory itself, after non-process stuff has been done */
3501 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3502 {
3503 	struct tgid_iter iter;
3504 	struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb);
3505 	struct pid_namespace *ns = proc_pid_ns(file_inode(file)->i_sb);
3506 	loff_t pos = ctx->pos;
3507 
3508 	if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3509 		return 0;
3510 
3511 	if (pos == TGID_OFFSET - 2) {
3512 		struct inode *inode = d_inode(fs_info->proc_self);
3513 		if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3514 			return 0;
3515 		ctx->pos = pos = pos + 1;
3516 	}
3517 	if (pos == TGID_OFFSET - 1) {
3518 		struct inode *inode = d_inode(fs_info->proc_thread_self);
3519 		if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3520 			return 0;
3521 		ctx->pos = pos = pos + 1;
3522 	}
3523 	iter.tgid = pos - TGID_OFFSET;
3524 	iter.task = NULL;
3525 	for (iter = next_tgid(ns, iter);
3526 	     iter.task;
3527 	     iter.tgid += 1, iter = next_tgid(ns, iter)) {
3528 		char name[10 + 1];
3529 		unsigned int len;
3530 
3531 		cond_resched();
3532 		if (!has_pid_permissions(fs_info, iter.task, HIDEPID_INVISIBLE))
3533 			continue;
3534 
3535 		len = snprintf(name, sizeof(name), "%u", iter.tgid);
3536 		ctx->pos = iter.tgid + TGID_OFFSET;
3537 		if (!proc_fill_cache(file, ctx, name, len,
3538 				     proc_pid_instantiate, iter.task, NULL)) {
3539 			put_task_struct(iter.task);
3540 			return 0;
3541 		}
3542 	}
3543 	ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3544 	return 0;
3545 }
3546 
3547 /*
3548  * proc_tid_comm_permission is a special permission function exclusively
3549  * used for the node /proc/<pid>/task/<tid>/comm.
3550  * It bypasses generic permission checks in the case where a task of the same
3551  * task group attempts to access the node.
3552  * The rationale behind this is that glibc and bionic access this node for
3553  * cross thread naming (pthread_set/getname_np(!self)). However, if
3554  * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3555  * which locks out the cross thread naming implementation.
3556  * This function makes sure that the node is always accessible for members of
3557  * same thread group.
3558  */
3559 static int proc_tid_comm_permission(struct mnt_idmap *idmap,
3560 				    struct inode *inode, int mask)
3561 {
3562 	bool is_same_tgroup;
3563 	struct task_struct *task;
3564 
3565 	task = get_proc_task(inode);
3566 	if (!task)
3567 		return -ESRCH;
3568 	is_same_tgroup = same_thread_group(current, task);
3569 	put_task_struct(task);
3570 
3571 	if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3572 		/* This file (/proc/<pid>/task/<tid>/comm) can always be
3573 		 * read or written by the members of the corresponding
3574 		 * thread group.
3575 		 */
3576 		return 0;
3577 	}
3578 
3579 	return generic_permission(&nop_mnt_idmap, inode, mask);
3580 }
3581 
3582 static const struct inode_operations proc_tid_comm_inode_operations = {
3583 		.permission = proc_tid_comm_permission,
3584 };
3585 
3586 /*
3587  * Tasks
3588  */
3589 static const struct pid_entry tid_base_stuff[] = {
3590 	DIR("fd",        S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3591 	DIR("fdinfo",    S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3592 	DIR("ns",	 S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3593 #ifdef CONFIG_NET
3594 	DIR("net",        S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3595 #endif
3596 	REG("environ",   S_IRUSR, proc_environ_operations),
3597 	REG("auxv",      S_IRUSR, proc_auxv_operations),
3598 	ONE("status",    S_IRUGO, proc_pid_status),
3599 	ONE("personality", S_IRUSR, proc_pid_personality),
3600 	ONE("limits",	 S_IRUGO, proc_pid_limits),
3601 #ifdef CONFIG_SCHED_DEBUG
3602 	REG("sched",     S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3603 #endif
3604 	NOD("comm",      S_IFREG|S_IRUGO|S_IWUSR,
3605 			 &proc_tid_comm_inode_operations,
3606 			 &proc_pid_set_comm_operations, {}),
3607 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3608 	ONE("syscall",   S_IRUSR, proc_pid_syscall),
3609 #endif
3610 	REG("cmdline",   S_IRUGO, proc_pid_cmdline_ops),
3611 	ONE("stat",      S_IRUGO, proc_tid_stat),
3612 	ONE("statm",     S_IRUGO, proc_pid_statm),
3613 	REG("maps",      S_IRUGO, proc_pid_maps_operations),
3614 #ifdef CONFIG_PROC_CHILDREN
3615 	REG("children",  S_IRUGO, proc_tid_children_operations),
3616 #endif
3617 #ifdef CONFIG_NUMA
3618 	REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3619 #endif
3620 	REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
3621 	LNK("cwd",       proc_cwd_link),
3622 	LNK("root",      proc_root_link),
3623 	LNK("exe",       proc_exe_link),
3624 	REG("mounts",    S_IRUGO, proc_mounts_operations),
3625 	REG("mountinfo",  S_IRUGO, proc_mountinfo_operations),
3626 #ifdef CONFIG_PROC_PAGE_MONITOR
3627 	REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3628 	REG("smaps",     S_IRUGO, proc_pid_smaps_operations),
3629 	REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3630 	REG("pagemap",    S_IRUSR, proc_pagemap_operations),
3631 #endif
3632 #ifdef CONFIG_SECURITY
3633 	DIR("attr",      S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3634 #endif
3635 #ifdef CONFIG_KALLSYMS
3636 	ONE("wchan",     S_IRUGO, proc_pid_wchan),
3637 #endif
3638 #ifdef CONFIG_STACKTRACE
3639 	ONE("stack",      S_IRUSR, proc_pid_stack),
3640 #endif
3641 #ifdef CONFIG_SCHED_INFO
3642 	ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3643 #endif
3644 #ifdef CONFIG_LATENCYTOP
3645 	REG("latency",  S_IRUGO, proc_lstats_operations),
3646 #endif
3647 #ifdef CONFIG_PROC_PID_CPUSET
3648 	ONE("cpuset",    S_IRUGO, proc_cpuset_show),
3649 #endif
3650 #ifdef CONFIG_CGROUPS
3651 	ONE("cgroup",  S_IRUGO, proc_cgroup_show),
3652 #endif
3653 #ifdef CONFIG_PROC_CPU_RESCTRL
3654 	ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3655 #endif
3656 	ONE("oom_score", S_IRUGO, proc_oom_score),
3657 	REG("oom_adj",   S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3658 	REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3659 #ifdef CONFIG_AUDIT
3660 	REG("loginuid",  S_IWUSR|S_IRUGO, proc_loginuid_operations),
3661 	REG("sessionid",  S_IRUGO, proc_sessionid_operations),
3662 #endif
3663 #ifdef CONFIG_FAULT_INJECTION
3664 	REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3665 	REG("fail-nth", 0644, proc_fail_nth_operations),
3666 #endif
3667 #ifdef CONFIG_TASK_IO_ACCOUNTING
3668 	ONE("io",	S_IRUSR, proc_tid_io_accounting),
3669 #endif
3670 #ifdef CONFIG_USER_NS
3671 	REG("uid_map",    S_IRUGO|S_IWUSR, proc_uid_map_operations),
3672 	REG("gid_map",    S_IRUGO|S_IWUSR, proc_gid_map_operations),
3673 	REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3674 	REG("setgroups",  S_IRUGO|S_IWUSR, proc_setgroups_operations),
3675 #endif
3676 #ifdef CONFIG_LIVEPATCH
3677 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
3678 #endif
3679 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3680 	ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3681 #endif
3682 #ifdef CONFIG_SECCOMP_CACHE_DEBUG
3683 	ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
3684 #endif
3685 #ifdef CONFIG_KSM
3686 	ONE("ksm_merging_pages",  S_IRUSR, proc_pid_ksm_merging_pages),
3687 	ONE("ksm_stat",  S_IRUSR, proc_pid_ksm_stat),
3688 #endif
3689 };
3690 
3691 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3692 {
3693 	return proc_pident_readdir(file, ctx,
3694 				   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3695 }
3696 
3697 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3698 {
3699 	return proc_pident_lookup(dir, dentry,
3700 				  tid_base_stuff,
3701 				  tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
3702 }
3703 
3704 static const struct file_operations proc_tid_base_operations = {
3705 	.read		= generic_read_dir,
3706 	.iterate_shared	= proc_tid_base_readdir,
3707 	.llseek		= generic_file_llseek,
3708 };
3709 
3710 static const struct inode_operations proc_tid_base_inode_operations = {
3711 	.lookup		= proc_tid_base_lookup,
3712 	.getattr	= pid_getattr,
3713 	.setattr	= proc_setattr,
3714 };
3715 
3716 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3717 	struct task_struct *task, const void *ptr)
3718 {
3719 	struct inode *inode;
3720 	inode = proc_pid_make_base_inode(dentry->d_sb, task,
3721 					 S_IFDIR | S_IRUGO | S_IXUGO);
3722 	if (!inode)
3723 		return ERR_PTR(-ENOENT);
3724 
3725 	inode->i_op = &proc_tid_base_inode_operations;
3726 	inode->i_fop = &proc_tid_base_operations;
3727 	inode->i_flags |= S_IMMUTABLE;
3728 
3729 	set_nlink(inode, nlink_tid);
3730 	pid_update_inode(task, inode);
3731 
3732 	d_set_d_op(dentry, &pid_dentry_operations);
3733 	return d_splice_alias(inode, dentry);
3734 }
3735 
3736 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3737 {
3738 	struct task_struct *task;
3739 	struct task_struct *leader = get_proc_task(dir);
3740 	unsigned tid;
3741 	struct proc_fs_info *fs_info;
3742 	struct pid_namespace *ns;
3743 	struct dentry *result = ERR_PTR(-ENOENT);
3744 
3745 	if (!leader)
3746 		goto out_no_task;
3747 
3748 	tid = name_to_int(&dentry->d_name);
3749 	if (tid == ~0U)
3750 		goto out;
3751 
3752 	fs_info = proc_sb_info(dentry->d_sb);
3753 	ns = fs_info->pid_ns;
3754 	rcu_read_lock();
3755 	task = find_task_by_pid_ns(tid, ns);
3756 	if (task)
3757 		get_task_struct(task);
3758 	rcu_read_unlock();
3759 	if (!task)
3760 		goto out;
3761 	if (!same_thread_group(leader, task))
3762 		goto out_drop_task;
3763 
3764 	result = proc_task_instantiate(dentry, task, NULL);
3765 out_drop_task:
3766 	put_task_struct(task);
3767 out:
3768 	put_task_struct(leader);
3769 out_no_task:
3770 	return result;
3771 }
3772 
3773 /*
3774  * Find the first tid of a thread group to return to user space.
3775  *
3776  * Usually this is just the thread group leader, but if the users
3777  * buffer was too small or there was a seek into the middle of the
3778  * directory we have more work todo.
3779  *
3780  * In the case of a short read we start with find_task_by_pid.
3781  *
3782  * In the case of a seek we start with the leader and walk nr
3783  * threads past it.
3784  */
3785 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3786 					struct pid_namespace *ns)
3787 {
3788 	struct task_struct *pos, *task;
3789 	unsigned long nr = f_pos;
3790 
3791 	if (nr != f_pos)	/* 32bit overflow? */
3792 		return NULL;
3793 
3794 	rcu_read_lock();
3795 	task = pid_task(pid, PIDTYPE_PID);
3796 	if (!task)
3797 		goto fail;
3798 
3799 	/* Attempt to start with the tid of a thread */
3800 	if (tid && nr) {
3801 		pos = find_task_by_pid_ns(tid, ns);
3802 		if (pos && same_thread_group(pos, task))
3803 			goto found;
3804 	}
3805 
3806 	/* If nr exceeds the number of threads there is nothing todo */
3807 	if (nr >= get_nr_threads(task))
3808 		goto fail;
3809 
3810 	/* If we haven't found our starting place yet start
3811 	 * with the leader and walk nr threads forward.
3812 	 */
3813 	pos = task = task->group_leader;
3814 	do {
3815 		if (!nr--)
3816 			goto found;
3817 	} while_each_thread(task, pos);
3818 fail:
3819 	pos = NULL;
3820 	goto out;
3821 found:
3822 	get_task_struct(pos);
3823 out:
3824 	rcu_read_unlock();
3825 	return pos;
3826 }
3827 
3828 /*
3829  * Find the next thread in the thread list.
3830  * Return NULL if there is an error or no next thread.
3831  *
3832  * The reference to the input task_struct is released.
3833  */
3834 static struct task_struct *next_tid(struct task_struct *start)
3835 {
3836 	struct task_struct *pos = NULL;
3837 	rcu_read_lock();
3838 	if (pid_alive(start)) {
3839 		pos = next_thread(start);
3840 		if (thread_group_leader(pos))
3841 			pos = NULL;
3842 		else
3843 			get_task_struct(pos);
3844 	}
3845 	rcu_read_unlock();
3846 	put_task_struct(start);
3847 	return pos;
3848 }
3849 
3850 /* for the /proc/TGID/task/ directories */
3851 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3852 {
3853 	struct inode *inode = file_inode(file);
3854 	struct task_struct *task;
3855 	struct pid_namespace *ns;
3856 	int tid;
3857 
3858 	if (proc_inode_is_dead(inode))
3859 		return -ENOENT;
3860 
3861 	if (!dir_emit_dots(file, ctx))
3862 		return 0;
3863 
3864 	/* f_version caches the tgid value that the last readdir call couldn't
3865 	 * return. lseek aka telldir automagically resets f_version to 0.
3866 	 */
3867 	ns = proc_pid_ns(inode->i_sb);
3868 	tid = (int)file->f_version;
3869 	file->f_version = 0;
3870 	for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3871 	     task;
3872 	     task = next_tid(task), ctx->pos++) {
3873 		char name[10 + 1];
3874 		unsigned int len;
3875 
3876 		tid = task_pid_nr_ns(task, ns);
3877 		if (!tid)
3878 			continue;	/* The task has just exited. */
3879 		len = snprintf(name, sizeof(name), "%u", tid);
3880 		if (!proc_fill_cache(file, ctx, name, len,
3881 				proc_task_instantiate, task, NULL)) {
3882 			/* returning this tgid failed, save it as the first
3883 			 * pid for the next readir call */
3884 			file->f_version = (u64)tid;
3885 			put_task_struct(task);
3886 			break;
3887 		}
3888 	}
3889 
3890 	return 0;
3891 }
3892 
3893 static int proc_task_getattr(struct mnt_idmap *idmap,
3894 			     const struct path *path, struct kstat *stat,
3895 			     u32 request_mask, unsigned int query_flags)
3896 {
3897 	struct inode *inode = d_inode(path->dentry);
3898 	struct task_struct *p = get_proc_task(inode);
3899 	generic_fillattr(&nop_mnt_idmap, inode, stat);
3900 
3901 	if (p) {
3902 		stat->nlink += get_nr_threads(p);
3903 		put_task_struct(p);
3904 	}
3905 
3906 	return 0;
3907 }
3908 
3909 static const struct inode_operations proc_task_inode_operations = {
3910 	.lookup		= proc_task_lookup,
3911 	.getattr	= proc_task_getattr,
3912 	.setattr	= proc_setattr,
3913 	.permission	= proc_pid_permission,
3914 };
3915 
3916 static const struct file_operations proc_task_operations = {
3917 	.read		= generic_read_dir,
3918 	.iterate_shared	= proc_task_readdir,
3919 	.llseek		= generic_file_llseek,
3920 };
3921 
3922 void __init set_proc_pid_nlink(void)
3923 {
3924 	nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3925 	nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3926 }
3927