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