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