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