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