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