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