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