1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
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.
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>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
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.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
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>
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/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.h"
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tid __ro_after_init;
114 static u8 nlink_tgid __ro_after_init;
120 const struct inode_operations *iop;
121 const struct file_operations *fop;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
134 #define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136 #define LNK(NAME, get_link) \
137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
138 &proc_pid_link_inode_operations, NULL, \
139 { .proc_get_link = get_link } )
140 #define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142 #define ONE(NAME, MODE, show) \
143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
145 { .proc_show = show } )
148 * Count the number of hardlinks for the pid_entry table, excluding the .
151 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
166 static int get_task_root(struct task_struct *task, struct path *root)
168 int result = -ENOENT;
172 get_fs_root(task->fs, root);
179 static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 struct task_struct *task = get_proc_task(d_inode(dentry));
182 int result = -ENOENT;
187 get_fs_pwd(task->fs, path);
191 put_task_struct(task);
196 static int proc_root_link(struct dentry *dentry, struct path *path)
198 struct task_struct *task = get_proc_task(d_inode(dentry));
199 int result = -ENOENT;
202 result = get_task_root(task, path);
203 put_task_struct(task);
209 * If the user used setproctitle(), we just get the string from
210 * user space at arg_start, and limit it to a maximum of one page.
212 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
213 size_t count, unsigned long pos,
214 unsigned long arg_start)
219 if (pos >= PAGE_SIZE)
222 page = (char *)__get_free_page(GFP_KERNEL);
227 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
229 int len = strnlen(page, got);
231 /* Include the NUL character if it was found */
239 len -= copy_to_user(buf, page+pos, len);
245 free_page((unsigned long)page);
249 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
250 size_t count, loff_t *ppos)
252 unsigned long arg_start, arg_end, env_start, env_end;
253 unsigned long pos, len;
256 /* Check if process spawned far enough to have cmdline. */
260 spin_lock(&mm->arg_lock);
261 arg_start = mm->arg_start;
262 arg_end = mm->arg_end;
263 env_start = mm->env_start;
264 env_end = mm->env_end;
265 spin_unlock(&mm->arg_lock);
267 if (arg_start >= arg_end)
271 * We allow setproctitle() to overwrite the argument
272 * strings, and overflow past the original end. But
273 * only when it overflows into the environment area.
275 if (env_start != arg_end || env_end < env_start)
276 env_start = env_end = arg_end;
277 len = env_end - arg_start;
279 /* We're not going to care if "*ppos" has high bits set */
283 if (count > len - pos)
289 * Magical special case: if the argv[] end byte is not
290 * zero, the user has overwritten it with setproctitle(3).
292 * Possible future enhancement: do this only once when
293 * pos is 0, and set a flag in the 'struct file'.
295 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
296 return get_mm_proctitle(mm, buf, count, pos, arg_start);
299 * For the non-setproctitle() case we limit things strictly
300 * to the [arg_start, arg_end[ range.
303 if (pos < arg_start || pos >= arg_end)
305 if (count > arg_end - pos)
306 count = arg_end - pos;
308 page = (char *)__get_free_page(GFP_KERNEL);
315 size_t size = min_t(size_t, PAGE_SIZE, count);
317 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
320 got -= copy_to_user(buf, page, got);
321 if (unlikely(!got)) {
332 free_page((unsigned long)page);
336 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
337 size_t count, loff_t *pos)
339 struct mm_struct *mm;
342 mm = get_task_mm(tsk);
346 ret = get_mm_cmdline(mm, buf, count, pos);
351 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
352 size_t count, loff_t *pos)
354 struct task_struct *tsk;
359 tsk = get_proc_task(file_inode(file));
362 ret = get_task_cmdline(tsk, buf, count, pos);
363 put_task_struct(tsk);
369 static const struct file_operations proc_pid_cmdline_ops = {
370 .read = proc_pid_cmdline_read,
371 .llseek = generic_file_llseek,
374 #ifdef CONFIG_KALLSYMS
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
379 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
380 struct pid *pid, struct task_struct *task)
383 char symname[KSYM_NAME_LEN];
385 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
388 wchan = get_wchan(task);
389 if (wchan && !lookup_symbol_name(wchan, symname)) {
390 seq_puts(m, symname);
398 #endif /* CONFIG_KALLSYMS */
400 static int lock_trace(struct task_struct *task)
402 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
405 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
406 mutex_unlock(&task->signal->cred_guard_mutex);
412 static void unlock_trace(struct task_struct *task)
414 mutex_unlock(&task->signal->cred_guard_mutex);
417 #ifdef CONFIG_STACKTRACE
419 #define MAX_STACK_TRACE_DEPTH 64
421 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
422 struct pid *pid, struct task_struct *task)
424 struct stack_trace trace;
425 unsigned long *entries;
429 * The ability to racily run the kernel stack unwinder on a running task
430 * and then observe the unwinder output is scary; while it is useful for
431 * debugging kernel issues, it can also allow an attacker to leak kernel
433 * Doing this in a manner that is at least safe from races would require
434 * some work to ensure that the remote task can not be scheduled; and
435 * even then, this would still expose the unwinder as local attack
437 * Therefore, this interface is restricted to root.
439 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
442 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
447 trace.nr_entries = 0;
448 trace.max_entries = MAX_STACK_TRACE_DEPTH;
449 trace.entries = entries;
452 err = lock_trace(task);
456 save_stack_trace_tsk(task, &trace);
458 for (i = 0; i < trace.nr_entries; i++) {
459 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
469 #ifdef CONFIG_SCHED_INFO
471 * Provides /proc/PID/schedstat
473 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
474 struct pid *pid, struct task_struct *task)
476 if (unlikely(!sched_info_on()))
477 seq_printf(m, "0 0 0\n");
479 seq_printf(m, "%llu %llu %lu\n",
480 (unsigned long long)task->se.sum_exec_runtime,
481 (unsigned long long)task->sched_info.run_delay,
482 task->sched_info.pcount);
488 #ifdef CONFIG_LATENCYTOP
489 static int lstats_show_proc(struct seq_file *m, void *v)
492 struct inode *inode = m->private;
493 struct task_struct *task = get_proc_task(inode);
497 seq_puts(m, "Latency Top version : v0.1\n");
498 for (i = 0; i < LT_SAVECOUNT; i++) {
499 struct latency_record *lr = &task->latency_record[i];
500 if (lr->backtrace[0]) {
502 seq_printf(m, "%i %li %li",
503 lr->count, lr->time, lr->max);
504 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
505 unsigned long bt = lr->backtrace[q];
510 seq_printf(m, " %ps", (void *)bt);
516 put_task_struct(task);
520 static int lstats_open(struct inode *inode, struct file *file)
522 return single_open(file, lstats_show_proc, inode);
525 static ssize_t lstats_write(struct file *file, const char __user *buf,
526 size_t count, loff_t *offs)
528 struct task_struct *task = get_proc_task(file_inode(file));
532 clear_all_latency_tracing(task);
533 put_task_struct(task);
538 static const struct file_operations proc_lstats_operations = {
541 .write = lstats_write,
543 .release = single_release,
548 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
549 struct pid *pid, struct task_struct *task)
551 unsigned long totalpages = totalram_pages + total_swap_pages;
552 unsigned long points = 0;
554 points = oom_badness(task, NULL, NULL, totalpages) *
556 seq_printf(m, "%lu\n", points);
566 static const struct limit_names lnames[RLIM_NLIMITS] = {
567 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
568 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
569 [RLIMIT_DATA] = {"Max data size", "bytes"},
570 [RLIMIT_STACK] = {"Max stack size", "bytes"},
571 [RLIMIT_CORE] = {"Max core file size", "bytes"},
572 [RLIMIT_RSS] = {"Max resident set", "bytes"},
573 [RLIMIT_NPROC] = {"Max processes", "processes"},
574 [RLIMIT_NOFILE] = {"Max open files", "files"},
575 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
576 [RLIMIT_AS] = {"Max address space", "bytes"},
577 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
578 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
579 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
580 [RLIMIT_NICE] = {"Max nice priority", NULL},
581 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
582 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
585 /* Display limits for a process */
586 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
587 struct pid *pid, struct task_struct *task)
592 struct rlimit rlim[RLIM_NLIMITS];
594 if (!lock_task_sighand(task, &flags))
596 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
597 unlock_task_sighand(task, &flags);
600 * print the file header
602 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
603 "Limit", "Soft Limit", "Hard Limit", "Units");
605 for (i = 0; i < RLIM_NLIMITS; i++) {
606 if (rlim[i].rlim_cur == RLIM_INFINITY)
607 seq_printf(m, "%-25s %-20s ",
608 lnames[i].name, "unlimited");
610 seq_printf(m, "%-25s %-20lu ",
611 lnames[i].name, rlim[i].rlim_cur);
613 if (rlim[i].rlim_max == RLIM_INFINITY)
614 seq_printf(m, "%-20s ", "unlimited");
616 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
619 seq_printf(m, "%-10s\n", lnames[i].unit);
627 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
628 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
629 struct pid *pid, struct task_struct *task)
632 unsigned long args[6], sp, pc;
635 res = lock_trace(task);
639 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
640 seq_puts(m, "running\n");
642 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
645 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
647 args[0], args[1], args[2], args[3], args[4], args[5],
653 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
655 /************************************************************************/
656 /* Here the fs part begins */
657 /************************************************************************/
659 /* permission checks */
660 static int proc_fd_access_allowed(struct inode *inode)
662 struct task_struct *task;
664 /* Allow access to a task's file descriptors if it is us or we
665 * may use ptrace attach to the process and find out that
668 task = get_proc_task(inode);
670 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
671 put_task_struct(task);
676 int proc_setattr(struct dentry *dentry, struct iattr *attr)
679 struct inode *inode = d_inode(dentry);
681 if (attr->ia_valid & ATTR_MODE)
684 error = setattr_prepare(dentry, attr);
688 setattr_copy(inode, attr);
689 mark_inode_dirty(inode);
694 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
695 * or euid/egid (for hide_pid_min=2)?
697 static bool has_pid_permissions(struct pid_namespace *pid,
698 struct task_struct *task,
701 if (pid->hide_pid < hide_pid_min)
703 if (in_group_p(pid->pid_gid))
705 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
709 static int proc_pid_permission(struct inode *inode, int mask)
711 struct pid_namespace *pid = proc_pid_ns(inode);
712 struct task_struct *task;
715 task = get_proc_task(inode);
718 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
719 put_task_struct(task);
722 if (pid->hide_pid == HIDEPID_INVISIBLE) {
724 * Let's make getdents(), stat(), and open()
725 * consistent with each other. If a process
726 * may not stat() a file, it shouldn't be seen
734 return generic_permission(inode, mask);
739 static const struct inode_operations proc_def_inode_operations = {
740 .setattr = proc_setattr,
743 static int proc_single_show(struct seq_file *m, void *v)
745 struct inode *inode = m->private;
746 struct pid_namespace *ns = proc_pid_ns(inode);
747 struct pid *pid = proc_pid(inode);
748 struct task_struct *task;
751 task = get_pid_task(pid, PIDTYPE_PID);
755 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
757 put_task_struct(task);
761 static int proc_single_open(struct inode *inode, struct file *filp)
763 return single_open(filp, proc_single_show, inode);
766 static const struct file_operations proc_single_file_operations = {
767 .open = proc_single_open,
770 .release = single_release,
774 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
776 struct task_struct *task = get_proc_task(inode);
777 struct mm_struct *mm = ERR_PTR(-ESRCH);
780 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
781 put_task_struct(task);
783 if (!IS_ERR_OR_NULL(mm)) {
784 /* ensure this mm_struct can't be freed */
786 /* but do not pin its memory */
794 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
796 struct mm_struct *mm = proc_mem_open(inode, mode);
801 file->private_data = mm;
805 static int mem_open(struct inode *inode, struct file *file)
807 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
809 /* OK to pass negative loff_t, we can catch out-of-range */
810 file->f_mode |= FMODE_UNSIGNED_OFFSET;
815 static ssize_t mem_rw(struct file *file, char __user *buf,
816 size_t count, loff_t *ppos, int write)
818 struct mm_struct *mm = file->private_data;
819 unsigned long addr = *ppos;
827 page = (char *)__get_free_page(GFP_KERNEL);
832 if (!mmget_not_zero(mm))
835 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
838 size_t this_len = min_t(size_t, count, PAGE_SIZE);
840 if (write && copy_from_user(page, buf, this_len)) {
845 this_len = access_remote_vm(mm, addr, page, this_len, flags);
852 if (!write && copy_to_user(buf, page, this_len)) {
866 free_page((unsigned long) page);
870 static ssize_t mem_read(struct file *file, char __user *buf,
871 size_t count, loff_t *ppos)
873 return mem_rw(file, buf, count, ppos, 0);
876 static ssize_t mem_write(struct file *file, const char __user *buf,
877 size_t count, loff_t *ppos)
879 return mem_rw(file, (char __user*)buf, count, ppos, 1);
882 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
886 file->f_pos = offset;
889 file->f_pos += offset;
894 force_successful_syscall_return();
898 static int mem_release(struct inode *inode, struct file *file)
900 struct mm_struct *mm = file->private_data;
906 static const struct file_operations proc_mem_operations = {
911 .release = mem_release,
914 static int environ_open(struct inode *inode, struct file *file)
916 return __mem_open(inode, file, PTRACE_MODE_READ);
919 static ssize_t environ_read(struct file *file, char __user *buf,
920 size_t count, loff_t *ppos)
923 unsigned long src = *ppos;
925 struct mm_struct *mm = file->private_data;
926 unsigned long env_start, env_end;
928 /* Ensure the process spawned far enough to have an environment. */
929 if (!mm || !mm->env_end)
932 page = (char *)__get_free_page(GFP_KERNEL);
937 if (!mmget_not_zero(mm))
940 spin_lock(&mm->arg_lock);
941 env_start = mm->env_start;
942 env_end = mm->env_end;
943 spin_unlock(&mm->arg_lock);
946 size_t this_len, max_len;
949 if (src >= (env_end - env_start))
952 this_len = env_end - (env_start + src);
954 max_len = min_t(size_t, PAGE_SIZE, count);
955 this_len = min(max_len, this_len);
957 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
964 if (copy_to_user(buf, page, retval)) {
978 free_page((unsigned long) page);
982 static const struct file_operations proc_environ_operations = {
983 .open = environ_open,
984 .read = environ_read,
985 .llseek = generic_file_llseek,
986 .release = mem_release,
989 static int auxv_open(struct inode *inode, struct file *file)
991 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
994 static ssize_t auxv_read(struct file *file, char __user *buf,
995 size_t count, loff_t *ppos)
997 struct mm_struct *mm = file->private_data;
998 unsigned int nwords = 0;
1004 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1005 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1006 nwords * sizeof(mm->saved_auxv[0]));
1009 static const struct file_operations proc_auxv_operations = {
1012 .llseek = generic_file_llseek,
1013 .release = mem_release,
1016 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1019 struct task_struct *task = get_proc_task(file_inode(file));
1020 char buffer[PROC_NUMBUF];
1021 int oom_adj = OOM_ADJUST_MIN;
1026 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1027 oom_adj = OOM_ADJUST_MAX;
1029 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1031 put_task_struct(task);
1032 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1033 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1036 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1038 struct mm_struct *mm = NULL;
1039 struct task_struct *task;
1042 task = get_proc_task(file_inode(file));
1046 mutex_lock(&oom_adj_mutex);
1048 if (oom_adj < task->signal->oom_score_adj &&
1049 !capable(CAP_SYS_RESOURCE)) {
1054 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1055 * /proc/pid/oom_score_adj instead.
1057 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1058 current->comm, task_pid_nr(current), task_pid_nr(task),
1061 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1062 !capable(CAP_SYS_RESOURCE)) {
1069 * Make sure we will check other processes sharing the mm if this is
1070 * not vfrok which wants its own oom_score_adj.
1071 * pin the mm so it doesn't go away and get reused after task_unlock
1073 if (!task->vfork_done) {
1074 struct task_struct *p = find_lock_task_mm(task);
1077 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1085 task->signal->oom_score_adj = oom_adj;
1086 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1087 task->signal->oom_score_adj_min = (short)oom_adj;
1088 trace_oom_score_adj_update(task);
1091 struct task_struct *p;
1094 for_each_process(p) {
1095 if (same_thread_group(task, p))
1098 /* do not touch kernel threads or the global init */
1099 if (p->flags & PF_KTHREAD || is_global_init(p))
1103 if (!p->vfork_done && process_shares_mm(p, mm)) {
1104 p->signal->oom_score_adj = oom_adj;
1105 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1106 p->signal->oom_score_adj_min = (short)oom_adj;
1114 mutex_unlock(&oom_adj_mutex);
1115 put_task_struct(task);
1120 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1121 * kernels. The effective policy is defined by oom_score_adj, which has a
1122 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1123 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1124 * Processes that become oom disabled via oom_adj will still be oom disabled
1125 * with this implementation.
1127 * oom_adj cannot be removed since existing userspace binaries use it.
1129 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1130 size_t count, loff_t *ppos)
1132 char buffer[PROC_NUMBUF];
1136 memset(buffer, 0, sizeof(buffer));
1137 if (count > sizeof(buffer) - 1)
1138 count = sizeof(buffer) - 1;
1139 if (copy_from_user(buffer, buf, count)) {
1144 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1147 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1148 oom_adj != OOM_DISABLE) {
1154 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1155 * value is always attainable.
1157 if (oom_adj == OOM_ADJUST_MAX)
1158 oom_adj = OOM_SCORE_ADJ_MAX;
1160 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1162 err = __set_oom_adj(file, oom_adj, true);
1164 return err < 0 ? err : count;
1167 static const struct file_operations proc_oom_adj_operations = {
1168 .read = oom_adj_read,
1169 .write = oom_adj_write,
1170 .llseek = generic_file_llseek,
1173 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1174 size_t count, loff_t *ppos)
1176 struct task_struct *task = get_proc_task(file_inode(file));
1177 char buffer[PROC_NUMBUF];
1178 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1183 oom_score_adj = task->signal->oom_score_adj;
1184 put_task_struct(task);
1185 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1186 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1189 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1190 size_t count, loff_t *ppos)
1192 char buffer[PROC_NUMBUF];
1196 memset(buffer, 0, sizeof(buffer));
1197 if (count > sizeof(buffer) - 1)
1198 count = sizeof(buffer) - 1;
1199 if (copy_from_user(buffer, buf, count)) {
1204 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1207 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1208 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1213 err = __set_oom_adj(file, oom_score_adj, false);
1215 return err < 0 ? err : count;
1218 static const struct file_operations proc_oom_score_adj_operations = {
1219 .read = oom_score_adj_read,
1220 .write = oom_score_adj_write,
1221 .llseek = default_llseek,
1224 #ifdef CONFIG_AUDITSYSCALL
1225 #define TMPBUFLEN 11
1226 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1227 size_t count, loff_t *ppos)
1229 struct inode * inode = file_inode(file);
1230 struct task_struct *task = get_proc_task(inode);
1232 char tmpbuf[TMPBUFLEN];
1236 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1237 from_kuid(file->f_cred->user_ns,
1238 audit_get_loginuid(task)));
1239 put_task_struct(task);
1240 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1243 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1244 size_t count, loff_t *ppos)
1246 struct inode * inode = file_inode(file);
1252 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1259 /* No partial writes. */
1263 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1267 /* is userspace tring to explicitly UNSET the loginuid? */
1268 if (loginuid == AUDIT_UID_UNSET) {
1269 kloginuid = INVALID_UID;
1271 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1272 if (!uid_valid(kloginuid))
1276 rv = audit_set_loginuid(kloginuid);
1282 static const struct file_operations proc_loginuid_operations = {
1283 .read = proc_loginuid_read,
1284 .write = proc_loginuid_write,
1285 .llseek = generic_file_llseek,
1288 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1289 size_t count, loff_t *ppos)
1291 struct inode * inode = file_inode(file);
1292 struct task_struct *task = get_proc_task(inode);
1294 char tmpbuf[TMPBUFLEN];
1298 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1299 audit_get_sessionid(task));
1300 put_task_struct(task);
1301 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1304 static const struct file_operations proc_sessionid_operations = {
1305 .read = proc_sessionid_read,
1306 .llseek = generic_file_llseek,
1310 #ifdef CONFIG_FAULT_INJECTION
1311 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1314 struct task_struct *task = get_proc_task(file_inode(file));
1315 char buffer[PROC_NUMBUF];
1321 make_it_fail = task->make_it_fail;
1322 put_task_struct(task);
1324 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1326 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1329 static ssize_t proc_fault_inject_write(struct file * file,
1330 const char __user * buf, size_t count, loff_t *ppos)
1332 struct task_struct *task;
1333 char buffer[PROC_NUMBUF];
1337 if (!capable(CAP_SYS_RESOURCE))
1339 memset(buffer, 0, sizeof(buffer));
1340 if (count > sizeof(buffer) - 1)
1341 count = sizeof(buffer) - 1;
1342 if (copy_from_user(buffer, buf, count))
1344 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1347 if (make_it_fail < 0 || make_it_fail > 1)
1350 task = get_proc_task(file_inode(file));
1353 task->make_it_fail = make_it_fail;
1354 put_task_struct(task);
1359 static const struct file_operations proc_fault_inject_operations = {
1360 .read = proc_fault_inject_read,
1361 .write = proc_fault_inject_write,
1362 .llseek = generic_file_llseek,
1365 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1366 size_t count, loff_t *ppos)
1368 struct task_struct *task;
1372 err = kstrtouint_from_user(buf, count, 0, &n);
1376 task = get_proc_task(file_inode(file));
1380 put_task_struct(task);
1385 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1386 size_t count, loff_t *ppos)
1388 struct task_struct *task;
1389 char numbuf[PROC_NUMBUF];
1392 task = get_proc_task(file_inode(file));
1395 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1396 put_task_struct(task);
1397 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1400 static const struct file_operations proc_fail_nth_operations = {
1401 .read = proc_fail_nth_read,
1402 .write = proc_fail_nth_write,
1407 #ifdef CONFIG_SCHED_DEBUG
1409 * Print out various scheduling related per-task fields:
1411 static int sched_show(struct seq_file *m, void *v)
1413 struct inode *inode = m->private;
1414 struct pid_namespace *ns = proc_pid_ns(inode);
1415 struct task_struct *p;
1417 p = get_proc_task(inode);
1420 proc_sched_show_task(p, ns, m);
1428 sched_write(struct file *file, const char __user *buf,
1429 size_t count, loff_t *offset)
1431 struct inode *inode = file_inode(file);
1432 struct task_struct *p;
1434 p = get_proc_task(inode);
1437 proc_sched_set_task(p);
1444 static int sched_open(struct inode *inode, struct file *filp)
1446 return single_open(filp, sched_show, inode);
1449 static const struct file_operations proc_pid_sched_operations = {
1452 .write = sched_write,
1453 .llseek = seq_lseek,
1454 .release = single_release,
1459 #ifdef CONFIG_SCHED_AUTOGROUP
1461 * Print out autogroup related information:
1463 static int sched_autogroup_show(struct seq_file *m, void *v)
1465 struct inode *inode = m->private;
1466 struct task_struct *p;
1468 p = get_proc_task(inode);
1471 proc_sched_autogroup_show_task(p, m);
1479 sched_autogroup_write(struct file *file, const char __user *buf,
1480 size_t count, loff_t *offset)
1482 struct inode *inode = file_inode(file);
1483 struct task_struct *p;
1484 char buffer[PROC_NUMBUF];
1488 memset(buffer, 0, sizeof(buffer));
1489 if (count > sizeof(buffer) - 1)
1490 count = sizeof(buffer) - 1;
1491 if (copy_from_user(buffer, buf, count))
1494 err = kstrtoint(strstrip(buffer), 0, &nice);
1498 p = get_proc_task(inode);
1502 err = proc_sched_autogroup_set_nice(p, nice);
1511 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1515 ret = single_open(filp, sched_autogroup_show, NULL);
1517 struct seq_file *m = filp->private_data;
1524 static const struct file_operations proc_pid_sched_autogroup_operations = {
1525 .open = sched_autogroup_open,
1527 .write = sched_autogroup_write,
1528 .llseek = seq_lseek,
1529 .release = single_release,
1532 #endif /* CONFIG_SCHED_AUTOGROUP */
1534 static ssize_t comm_write(struct file *file, const char __user *buf,
1535 size_t count, loff_t *offset)
1537 struct inode *inode = file_inode(file);
1538 struct task_struct *p;
1539 char buffer[TASK_COMM_LEN];
1540 const size_t maxlen = sizeof(buffer) - 1;
1542 memset(buffer, 0, sizeof(buffer));
1543 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1546 p = get_proc_task(inode);
1550 if (same_thread_group(current, p))
1551 set_task_comm(p, buffer);
1560 static int comm_show(struct seq_file *m, void *v)
1562 struct inode *inode = m->private;
1563 struct task_struct *p;
1565 p = get_proc_task(inode);
1569 proc_task_name(m, p, false);
1577 static int comm_open(struct inode *inode, struct file *filp)
1579 return single_open(filp, comm_show, inode);
1582 static const struct file_operations proc_pid_set_comm_operations = {
1585 .write = comm_write,
1586 .llseek = seq_lseek,
1587 .release = single_release,
1590 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1592 struct task_struct *task;
1593 struct file *exe_file;
1595 task = get_proc_task(d_inode(dentry));
1598 exe_file = get_task_exe_file(task);
1599 put_task_struct(task);
1601 *exe_path = exe_file->f_path;
1602 path_get(&exe_file->f_path);
1609 static const char *proc_pid_get_link(struct dentry *dentry,
1610 struct inode *inode,
1611 struct delayed_call *done)
1614 int error = -EACCES;
1617 return ERR_PTR(-ECHILD);
1619 /* Are we allowed to snoop on the tasks file descriptors? */
1620 if (!proc_fd_access_allowed(inode))
1623 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1627 nd_jump_link(&path);
1630 return ERR_PTR(error);
1633 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1635 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1642 pathname = d_path(path, tmp, PAGE_SIZE);
1643 len = PTR_ERR(pathname);
1644 if (IS_ERR(pathname))
1646 len = tmp + PAGE_SIZE - 1 - pathname;
1650 if (copy_to_user(buffer, pathname, len))
1653 free_page((unsigned long)tmp);
1657 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1659 int error = -EACCES;
1660 struct inode *inode = d_inode(dentry);
1663 /* Are we allowed to snoop on the tasks file descriptors? */
1664 if (!proc_fd_access_allowed(inode))
1667 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1671 error = do_proc_readlink(&path, buffer, buflen);
1677 const struct inode_operations proc_pid_link_inode_operations = {
1678 .readlink = proc_pid_readlink,
1679 .get_link = proc_pid_get_link,
1680 .setattr = proc_setattr,
1684 /* building an inode */
1686 void task_dump_owner(struct task_struct *task, umode_t mode,
1687 kuid_t *ruid, kgid_t *rgid)
1689 /* Depending on the state of dumpable compute who should own a
1690 * proc file for a task.
1692 const struct cred *cred;
1696 if (unlikely(task->flags & PF_KTHREAD)) {
1697 *ruid = GLOBAL_ROOT_UID;
1698 *rgid = GLOBAL_ROOT_GID;
1702 /* Default to the tasks effective ownership */
1704 cred = __task_cred(task);
1710 * Before the /proc/pid/status file was created the only way to read
1711 * the effective uid of a /process was to stat /proc/pid. Reading
1712 * /proc/pid/status is slow enough that procps and other packages
1713 * kept stating /proc/pid. To keep the rules in /proc simple I have
1714 * made this apply to all per process world readable and executable
1717 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1718 struct mm_struct *mm;
1721 /* Make non-dumpable tasks owned by some root */
1723 if (get_dumpable(mm) != SUID_DUMP_USER) {
1724 struct user_namespace *user_ns = mm->user_ns;
1726 uid = make_kuid(user_ns, 0);
1727 if (!uid_valid(uid))
1728 uid = GLOBAL_ROOT_UID;
1730 gid = make_kgid(user_ns, 0);
1731 if (!gid_valid(gid))
1732 gid = GLOBAL_ROOT_GID;
1735 uid = GLOBAL_ROOT_UID;
1736 gid = GLOBAL_ROOT_GID;
1744 struct inode *proc_pid_make_inode(struct super_block * sb,
1745 struct task_struct *task, umode_t mode)
1747 struct inode * inode;
1748 struct proc_inode *ei;
1750 /* We need a new inode */
1752 inode = new_inode(sb);
1758 inode->i_mode = mode;
1759 inode->i_ino = get_next_ino();
1760 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1761 inode->i_op = &proc_def_inode_operations;
1764 * grab the reference to task.
1766 ei->pid = get_task_pid(task, PIDTYPE_PID);
1770 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1771 security_task_to_inode(task, inode);
1781 int pid_getattr(const struct path *path, struct kstat *stat,
1782 u32 request_mask, unsigned int query_flags)
1784 struct inode *inode = d_inode(path->dentry);
1785 struct pid_namespace *pid = proc_pid_ns(inode);
1786 struct task_struct *task;
1788 generic_fillattr(inode, stat);
1790 stat->uid = GLOBAL_ROOT_UID;
1791 stat->gid = GLOBAL_ROOT_GID;
1793 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1795 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1798 * This doesn't prevent learning whether PID exists,
1799 * it only makes getattr() consistent with readdir().
1803 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1812 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1814 void pid_update_inode(struct task_struct *task, struct inode *inode)
1816 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1818 inode->i_mode &= ~(S_ISUID | S_ISGID);
1819 security_task_to_inode(task, inode);
1823 * Rewrite the inode's ownerships here because the owning task may have
1824 * performed a setuid(), etc.
1827 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1829 struct inode *inode;
1830 struct task_struct *task;
1832 if (flags & LOOKUP_RCU)
1835 inode = d_inode(dentry);
1836 task = get_proc_task(inode);
1839 pid_update_inode(task, inode);
1840 put_task_struct(task);
1846 static inline bool proc_inode_is_dead(struct inode *inode)
1848 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1851 int pid_delete_dentry(const struct dentry *dentry)
1853 /* Is the task we represent dead?
1854 * If so, then don't put the dentry on the lru list,
1855 * kill it immediately.
1857 return proc_inode_is_dead(d_inode(dentry));
1860 const struct dentry_operations pid_dentry_operations =
1862 .d_revalidate = pid_revalidate,
1863 .d_delete = pid_delete_dentry,
1869 * Fill a directory entry.
1871 * If possible create the dcache entry and derive our inode number and
1872 * file type from dcache entry.
1874 * Since all of the proc inode numbers are dynamically generated, the inode
1875 * numbers do not exist until the inode is cache. This means creating the
1876 * the dcache entry in readdir is necessary to keep the inode numbers
1877 * reported by readdir in sync with the inode numbers reported
1880 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1881 const char *name, unsigned int len,
1882 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1884 struct dentry *child, *dir = file->f_path.dentry;
1885 struct qstr qname = QSTR_INIT(name, len);
1886 struct inode *inode;
1887 unsigned type = DT_UNKNOWN;
1890 child = d_hash_and_lookup(dir, &qname);
1892 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1893 child = d_alloc_parallel(dir, &qname, &wq);
1895 goto end_instantiate;
1896 if (d_in_lookup(child)) {
1898 res = instantiate(child, task, ptr);
1899 d_lookup_done(child);
1900 if (unlikely(res)) {
1904 goto end_instantiate;
1908 inode = d_inode(child);
1910 type = inode->i_mode >> 12;
1913 return dir_emit(ctx, name, len, ino, type);
1917 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1918 * which represent vma start and end addresses.
1920 static int dname_to_vma_addr(struct dentry *dentry,
1921 unsigned long *start, unsigned long *end)
1923 const char *str = dentry->d_name.name;
1924 unsigned long long sval, eval;
1927 if (str[0] == '0' && str[1] != '-')
1929 len = _parse_integer(str, 16, &sval);
1930 if (len & KSTRTOX_OVERFLOW)
1932 if (sval != (unsigned long)sval)
1940 if (str[0] == '0' && str[1])
1942 len = _parse_integer(str, 16, &eval);
1943 if (len & KSTRTOX_OVERFLOW)
1945 if (eval != (unsigned long)eval)
1958 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1960 unsigned long vm_start, vm_end;
1961 bool exact_vma_exists = false;
1962 struct mm_struct *mm = NULL;
1963 struct task_struct *task;
1964 struct inode *inode;
1967 if (flags & LOOKUP_RCU)
1970 inode = d_inode(dentry);
1971 task = get_proc_task(inode);
1975 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1976 if (IS_ERR_OR_NULL(mm))
1979 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1980 status = down_read_killable(&mm->mmap_sem);
1982 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1984 up_read(&mm->mmap_sem);
1990 if (exact_vma_exists) {
1991 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1993 security_task_to_inode(task, inode);
1998 put_task_struct(task);
2004 static const struct dentry_operations tid_map_files_dentry_operations = {
2005 .d_revalidate = map_files_d_revalidate,
2006 .d_delete = pid_delete_dentry,
2009 static int map_files_get_link(struct dentry *dentry, struct path *path)
2011 unsigned long vm_start, vm_end;
2012 struct vm_area_struct *vma;
2013 struct task_struct *task;
2014 struct mm_struct *mm;
2018 task = get_proc_task(d_inode(dentry));
2022 mm = get_task_mm(task);
2023 put_task_struct(task);
2027 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2031 rc = down_read_killable(&mm->mmap_sem);
2036 vma = find_exact_vma(mm, vm_start, vm_end);
2037 if (vma && vma->vm_file) {
2038 *path = vma->vm_file->f_path;
2042 up_read(&mm->mmap_sem);
2050 struct map_files_info {
2051 unsigned long start;
2057 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2058 * symlinks may be used to bypass permissions on ancestor directories in the
2059 * path to the file in question.
2062 proc_map_files_get_link(struct dentry *dentry,
2063 struct inode *inode,
2064 struct delayed_call *done)
2066 if (!capable(CAP_SYS_ADMIN))
2067 return ERR_PTR(-EPERM);
2069 return proc_pid_get_link(dentry, inode, done);
2073 * Identical to proc_pid_link_inode_operations except for get_link()
2075 static const struct inode_operations proc_map_files_link_inode_operations = {
2076 .readlink = proc_pid_readlink,
2077 .get_link = proc_map_files_get_link,
2078 .setattr = proc_setattr,
2081 static struct dentry *
2082 proc_map_files_instantiate(struct dentry *dentry,
2083 struct task_struct *task, const void *ptr)
2085 fmode_t mode = (fmode_t)(unsigned long)ptr;
2086 struct proc_inode *ei;
2087 struct inode *inode;
2089 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2090 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2091 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2093 return ERR_PTR(-ENOENT);
2096 ei->op.proc_get_link = map_files_get_link;
2098 inode->i_op = &proc_map_files_link_inode_operations;
2101 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2102 return d_splice_alias(inode, dentry);
2105 static struct dentry *proc_map_files_lookup(struct inode *dir,
2106 struct dentry *dentry, unsigned int flags)
2108 unsigned long vm_start, vm_end;
2109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct dentry *result;
2112 struct mm_struct *mm;
2114 result = ERR_PTR(-ENOENT);
2115 task = get_proc_task(dir);
2119 result = ERR_PTR(-EACCES);
2120 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2123 result = ERR_PTR(-ENOENT);
2124 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2127 mm = get_task_mm(task);
2131 result = ERR_PTR(-EINTR);
2132 if (down_read_killable(&mm->mmap_sem))
2135 result = ERR_PTR(-ENOENT);
2136 vma = find_exact_vma(mm, vm_start, vm_end);
2141 result = proc_map_files_instantiate(dentry, task,
2142 (void *)(unsigned long)vma->vm_file->f_mode);
2145 up_read(&mm->mmap_sem);
2149 put_task_struct(task);
2154 static const struct inode_operations proc_map_files_inode_operations = {
2155 .lookup = proc_map_files_lookup,
2156 .permission = proc_fd_permission,
2157 .setattr = proc_setattr,
2161 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2163 struct vm_area_struct *vma;
2164 struct task_struct *task;
2165 struct mm_struct *mm;
2166 unsigned long nr_files, pos, i;
2167 struct flex_array *fa = NULL;
2168 struct map_files_info info;
2169 struct map_files_info *p;
2173 task = get_proc_task(file_inode(file));
2178 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2182 if (!dir_emit_dots(file, ctx))
2185 mm = get_task_mm(task);
2189 ret = down_read_killable(&mm->mmap_sem);
2198 * We need two passes here:
2200 * 1) Collect vmas of mapped files with mmap_sem taken
2201 * 2) Release mmap_sem and instantiate entries
2203 * otherwise we get lockdep complained, since filldir()
2204 * routine might require mmap_sem taken in might_fault().
2207 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2208 if (vma->vm_file && ++pos > ctx->pos)
2213 fa = flex_array_alloc(sizeof(info), nr_files,
2215 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2219 flex_array_free(fa);
2220 up_read(&mm->mmap_sem);
2224 for (i = 0, vma = mm->mmap, pos = 2; vma;
2225 vma = vma->vm_next) {
2228 if (++pos <= ctx->pos)
2231 info.start = vma->vm_start;
2232 info.end = vma->vm_end;
2233 info.mode = vma->vm_file->f_mode;
2234 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2238 up_read(&mm->mmap_sem);
2241 for (i = 0; i < nr_files; i++) {
2242 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2245 p = flex_array_get(fa, i);
2246 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2247 if (!proc_fill_cache(file, ctx,
2249 proc_map_files_instantiate,
2251 (void *)(unsigned long)p->mode))
2256 flex_array_free(fa);
2259 put_task_struct(task);
2264 static const struct file_operations proc_map_files_operations = {
2265 .read = generic_read_dir,
2266 .iterate_shared = proc_map_files_readdir,
2267 .llseek = generic_file_llseek,
2270 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2271 struct timers_private {
2273 struct task_struct *task;
2274 struct sighand_struct *sighand;
2275 struct pid_namespace *ns;
2276 unsigned long flags;
2279 static void *timers_start(struct seq_file *m, loff_t *pos)
2281 struct timers_private *tp = m->private;
2283 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2285 return ERR_PTR(-ESRCH);
2287 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2289 return ERR_PTR(-ESRCH);
2291 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2294 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2296 struct timers_private *tp = m->private;
2297 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2300 static void timers_stop(struct seq_file *m, void *v)
2302 struct timers_private *tp = m->private;
2305 unlock_task_sighand(tp->task, &tp->flags);
2310 put_task_struct(tp->task);
2315 static int show_timer(struct seq_file *m, void *v)
2317 struct k_itimer *timer;
2318 struct timers_private *tp = m->private;
2320 static const char * const nstr[] = {
2321 [SIGEV_SIGNAL] = "signal",
2322 [SIGEV_NONE] = "none",
2323 [SIGEV_THREAD] = "thread",
2326 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2327 notify = timer->it_sigev_notify;
2329 seq_printf(m, "ID: %d\n", timer->it_id);
2330 seq_printf(m, "signal: %d/%px\n",
2331 timer->sigq->info.si_signo,
2332 timer->sigq->info.si_value.sival_ptr);
2333 seq_printf(m, "notify: %s/%s.%d\n",
2334 nstr[notify & ~SIGEV_THREAD_ID],
2335 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2336 pid_nr_ns(timer->it_pid, tp->ns));
2337 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2342 static const struct seq_operations proc_timers_seq_ops = {
2343 .start = timers_start,
2344 .next = timers_next,
2345 .stop = timers_stop,
2349 static int proc_timers_open(struct inode *inode, struct file *file)
2351 struct timers_private *tp;
2353 tp = __seq_open_private(file, &proc_timers_seq_ops,
2354 sizeof(struct timers_private));
2358 tp->pid = proc_pid(inode);
2359 tp->ns = proc_pid_ns(inode);
2363 static const struct file_operations proc_timers_operations = {
2364 .open = proc_timers_open,
2366 .llseek = seq_lseek,
2367 .release = seq_release_private,
2371 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2372 size_t count, loff_t *offset)
2374 struct inode *inode = file_inode(file);
2375 struct task_struct *p;
2379 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2383 p = get_proc_task(inode);
2388 if (!capable(CAP_SYS_NICE)) {
2393 err = security_task_setscheduler(p);
2402 p->timer_slack_ns = p->default_timer_slack_ns;
2404 p->timer_slack_ns = slack_ns;
2413 static int timerslack_ns_show(struct seq_file *m, void *v)
2415 struct inode *inode = m->private;
2416 struct task_struct *p;
2419 p = get_proc_task(inode);
2425 if (!capable(CAP_SYS_NICE)) {
2429 err = security_task_getscheduler(p);
2435 seq_printf(m, "%llu\n", p->timer_slack_ns);
2444 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2446 return single_open(filp, timerslack_ns_show, inode);
2449 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2450 .open = timerslack_ns_open,
2452 .write = timerslack_ns_write,
2453 .llseek = seq_lseek,
2454 .release = single_release,
2457 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2458 struct task_struct *task, const void *ptr)
2460 const struct pid_entry *p = ptr;
2461 struct inode *inode;
2462 struct proc_inode *ei;
2464 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2466 return ERR_PTR(-ENOENT);
2469 if (S_ISDIR(inode->i_mode))
2470 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2472 inode->i_op = p->iop;
2474 inode->i_fop = p->fop;
2476 pid_update_inode(task, inode);
2477 d_set_d_op(dentry, &pid_dentry_operations);
2478 return d_splice_alias(inode, dentry);
2481 static struct dentry *proc_pident_lookup(struct inode *dir,
2482 struct dentry *dentry,
2483 const struct pid_entry *ents,
2486 struct task_struct *task = get_proc_task(dir);
2487 const struct pid_entry *p, *last;
2488 struct dentry *res = ERR_PTR(-ENOENT);
2494 * Yes, it does not scale. And it should not. Don't add
2495 * new entries into /proc/<tgid>/ without very good reasons.
2497 last = &ents[nents];
2498 for (p = ents; p < last; p++) {
2499 if (p->len != dentry->d_name.len)
2501 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2502 res = proc_pident_instantiate(dentry, task, p);
2506 put_task_struct(task);
2511 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2512 const struct pid_entry *ents, unsigned int nents)
2514 struct task_struct *task = get_proc_task(file_inode(file));
2515 const struct pid_entry *p;
2520 if (!dir_emit_dots(file, ctx))
2523 if (ctx->pos >= nents + 2)
2526 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2527 if (!proc_fill_cache(file, ctx, p->name, p->len,
2528 proc_pident_instantiate, task, p))
2533 put_task_struct(task);
2537 #ifdef CONFIG_SECURITY
2538 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2540 file->private_data = NULL;
2541 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2545 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2546 size_t count, loff_t *ppos)
2548 struct inode * inode = file_inode(file);
2551 struct task_struct *task = get_proc_task(inode);
2556 length = security_getprocattr(task,
2557 (char*)file->f_path.dentry->d_name.name,
2559 put_task_struct(task);
2561 length = simple_read_from_buffer(buf, count, ppos, p, length);
2566 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2567 size_t count, loff_t *ppos)
2569 struct inode * inode = file_inode(file);
2570 struct task_struct *task;
2574 /* A task may only write when it was the opener. */
2575 if (file->private_data != current->mm)
2579 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2584 /* A task may only write its own attributes. */
2585 if (current != task) {
2589 /* Prevent changes to overridden credentials. */
2590 if (current_cred() != current_real_cred()) {
2596 if (count > PAGE_SIZE)
2599 /* No partial writes. */
2603 page = memdup_user(buf, count);
2609 /* Guard against adverse ptrace interaction */
2610 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2614 rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
2615 mutex_unlock(¤t->signal->cred_guard_mutex);
2622 static const struct file_operations proc_pid_attr_operations = {
2623 .open = proc_pid_attr_open,
2624 .read = proc_pid_attr_read,
2625 .write = proc_pid_attr_write,
2626 .llseek = generic_file_llseek,
2627 .release = mem_release,
2630 static const struct pid_entry attr_dir_stuff[] = {
2631 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2632 REG("prev", S_IRUGO, proc_pid_attr_operations),
2633 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2634 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2635 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2636 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2639 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2641 return proc_pident_readdir(file, ctx,
2642 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2645 static const struct file_operations proc_attr_dir_operations = {
2646 .read = generic_read_dir,
2647 .iterate_shared = proc_attr_dir_readdir,
2648 .llseek = generic_file_llseek,
2651 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2652 struct dentry *dentry, unsigned int flags)
2654 return proc_pident_lookup(dir, dentry,
2655 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2658 static const struct inode_operations proc_attr_dir_inode_operations = {
2659 .lookup = proc_attr_dir_lookup,
2660 .getattr = pid_getattr,
2661 .setattr = proc_setattr,
2666 #ifdef CONFIG_ELF_CORE
2667 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2668 size_t count, loff_t *ppos)
2670 struct task_struct *task = get_proc_task(file_inode(file));
2671 struct mm_struct *mm;
2672 char buffer[PROC_NUMBUF];
2680 mm = get_task_mm(task);
2682 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2683 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2684 MMF_DUMP_FILTER_SHIFT));
2686 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2689 put_task_struct(task);
2694 static ssize_t proc_coredump_filter_write(struct file *file,
2695 const char __user *buf,
2699 struct task_struct *task;
2700 struct mm_struct *mm;
2706 ret = kstrtouint_from_user(buf, count, 0, &val);
2711 task = get_proc_task(file_inode(file));
2715 mm = get_task_mm(task);
2720 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2722 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2724 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2729 put_task_struct(task);
2736 static const struct file_operations proc_coredump_filter_operations = {
2737 .read = proc_coredump_filter_read,
2738 .write = proc_coredump_filter_write,
2739 .llseek = generic_file_llseek,
2743 #ifdef CONFIG_TASK_IO_ACCOUNTING
2744 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2746 struct task_io_accounting acct = task->ioac;
2747 unsigned long flags;
2750 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2754 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2759 if (whole && lock_task_sighand(task, &flags)) {
2760 struct task_struct *t = task;
2762 task_io_accounting_add(&acct, &task->signal->ioac);
2763 while_each_thread(task, t)
2764 task_io_accounting_add(&acct, &t->ioac);
2766 unlock_task_sighand(task, &flags);
2773 "read_bytes: %llu\n"
2774 "write_bytes: %llu\n"
2775 "cancelled_write_bytes: %llu\n",
2776 (unsigned long long)acct.rchar,
2777 (unsigned long long)acct.wchar,
2778 (unsigned long long)acct.syscr,
2779 (unsigned long long)acct.syscw,
2780 (unsigned long long)acct.read_bytes,
2781 (unsigned long long)acct.write_bytes,
2782 (unsigned long long)acct.cancelled_write_bytes);
2786 mutex_unlock(&task->signal->cred_guard_mutex);
2790 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2791 struct pid *pid, struct task_struct *task)
2793 return do_io_accounting(task, m, 0);
2796 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2797 struct pid *pid, struct task_struct *task)
2799 return do_io_accounting(task, m, 1);
2801 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2803 #ifdef CONFIG_USER_NS
2804 static int proc_id_map_open(struct inode *inode, struct file *file,
2805 const struct seq_operations *seq_ops)
2807 struct user_namespace *ns = NULL;
2808 struct task_struct *task;
2809 struct seq_file *seq;
2812 task = get_proc_task(inode);
2815 ns = get_user_ns(task_cred_xxx(task, user_ns));
2817 put_task_struct(task);
2822 ret = seq_open(file, seq_ops);
2826 seq = file->private_data;
2836 static int proc_id_map_release(struct inode *inode, struct file *file)
2838 struct seq_file *seq = file->private_data;
2839 struct user_namespace *ns = seq->private;
2841 return seq_release(inode, file);
2844 static int proc_uid_map_open(struct inode *inode, struct file *file)
2846 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2849 static int proc_gid_map_open(struct inode *inode, struct file *file)
2851 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2854 static int proc_projid_map_open(struct inode *inode, struct file *file)
2856 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2859 static const struct file_operations proc_uid_map_operations = {
2860 .open = proc_uid_map_open,
2861 .write = proc_uid_map_write,
2863 .llseek = seq_lseek,
2864 .release = proc_id_map_release,
2867 static const struct file_operations proc_gid_map_operations = {
2868 .open = proc_gid_map_open,
2869 .write = proc_gid_map_write,
2871 .llseek = seq_lseek,
2872 .release = proc_id_map_release,
2875 static const struct file_operations proc_projid_map_operations = {
2876 .open = proc_projid_map_open,
2877 .write = proc_projid_map_write,
2879 .llseek = seq_lseek,
2880 .release = proc_id_map_release,
2883 static int proc_setgroups_open(struct inode *inode, struct file *file)
2885 struct user_namespace *ns = NULL;
2886 struct task_struct *task;
2890 task = get_proc_task(inode);
2893 ns = get_user_ns(task_cred_xxx(task, user_ns));
2895 put_task_struct(task);
2900 if (file->f_mode & FMODE_WRITE) {
2902 if (!ns_capable(ns, CAP_SYS_ADMIN))
2906 ret = single_open(file, &proc_setgroups_show, ns);
2917 static int proc_setgroups_release(struct inode *inode, struct file *file)
2919 struct seq_file *seq = file->private_data;
2920 struct user_namespace *ns = seq->private;
2921 int ret = single_release(inode, file);
2926 static const struct file_operations proc_setgroups_operations = {
2927 .open = proc_setgroups_open,
2928 .write = proc_setgroups_write,
2930 .llseek = seq_lseek,
2931 .release = proc_setgroups_release,
2933 #endif /* CONFIG_USER_NS */
2935 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2936 struct pid *pid, struct task_struct *task)
2938 int err = lock_trace(task);
2940 seq_printf(m, "%08x\n", task->personality);
2946 #ifdef CONFIG_LIVEPATCH
2947 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2948 struct pid *pid, struct task_struct *task)
2950 seq_printf(m, "%d\n", task->patch_state);
2953 #endif /* CONFIG_LIVEPATCH */
2958 static const struct file_operations proc_task_operations;
2959 static const struct inode_operations proc_task_inode_operations;
2961 static const struct pid_entry tgid_base_stuff[] = {
2962 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2963 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2964 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2965 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2966 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2968 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2970 REG("environ", S_IRUSR, proc_environ_operations),
2971 REG("auxv", S_IRUSR, proc_auxv_operations),
2972 ONE("status", S_IRUGO, proc_pid_status),
2973 ONE("personality", S_IRUSR, proc_pid_personality),
2974 ONE("limits", S_IRUGO, proc_pid_limits),
2975 #ifdef CONFIG_SCHED_DEBUG
2976 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2978 #ifdef CONFIG_SCHED_AUTOGROUP
2979 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2981 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2982 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2983 ONE("syscall", S_IRUSR, proc_pid_syscall),
2985 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2986 ONE("stat", S_IRUGO, proc_tgid_stat),
2987 ONE("statm", S_IRUGO, proc_pid_statm),
2988 REG("maps", S_IRUGO, proc_pid_maps_operations),
2990 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2992 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2993 LNK("cwd", proc_cwd_link),
2994 LNK("root", proc_root_link),
2995 LNK("exe", proc_exe_link),
2996 REG("mounts", S_IRUGO, proc_mounts_operations),
2997 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2998 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2999 #ifdef CONFIG_PROC_PAGE_MONITOR
3000 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3001 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3002 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3003 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3005 #ifdef CONFIG_SECURITY
3006 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3008 #ifdef CONFIG_KALLSYMS
3009 ONE("wchan", S_IRUGO, proc_pid_wchan),
3011 #ifdef CONFIG_STACKTRACE
3012 ONE("stack", S_IRUSR, proc_pid_stack),
3014 #ifdef CONFIG_SCHED_INFO
3015 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3017 #ifdef CONFIG_LATENCYTOP
3018 REG("latency", S_IRUGO, proc_lstats_operations),
3020 #ifdef CONFIG_PROC_PID_CPUSET
3021 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3023 #ifdef CONFIG_CGROUPS
3024 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3026 ONE("oom_score", S_IRUGO, proc_oom_score),
3027 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3028 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3029 #ifdef CONFIG_AUDITSYSCALL
3030 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3031 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3033 #ifdef CONFIG_FAULT_INJECTION
3034 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3035 REG("fail-nth", 0644, proc_fail_nth_operations),
3037 #ifdef CONFIG_ELF_CORE
3038 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3040 #ifdef CONFIG_TASK_IO_ACCOUNTING
3041 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3043 #ifdef CONFIG_USER_NS
3044 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3045 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3046 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3047 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3049 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3050 REG("timers", S_IRUGO, proc_timers_operations),
3052 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3053 #ifdef CONFIG_LIVEPATCH
3054 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3058 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3060 return proc_pident_readdir(file, ctx,
3061 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3064 static const struct file_operations proc_tgid_base_operations = {
3065 .read = generic_read_dir,
3066 .iterate_shared = proc_tgid_base_readdir,
3067 .llseek = generic_file_llseek,
3070 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3072 return proc_pident_lookup(dir, dentry,
3073 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3076 static const struct inode_operations proc_tgid_base_inode_operations = {
3077 .lookup = proc_tgid_base_lookup,
3078 .getattr = pid_getattr,
3079 .setattr = proc_setattr,
3080 .permission = proc_pid_permission,
3083 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3085 struct dentry *dentry, *leader, *dir;
3090 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3091 /* no ->d_hash() rejects on procfs */
3092 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3094 d_invalidate(dentry);
3102 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3103 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3108 name.len = strlen(name.name);
3109 dir = d_hash_and_lookup(leader, &name);
3111 goto out_put_leader;
3114 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3115 dentry = d_hash_and_lookup(dir, &name);
3117 d_invalidate(dentry);
3129 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3130 * @task: task that should be flushed.
3132 * When flushing dentries from proc, one needs to flush them from global
3133 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3134 * in. This call is supposed to do all of this job.
3136 * Looks in the dcache for
3138 * /proc/@tgid/task/@pid
3139 * if either directory is present flushes it and all of it'ts children
3142 * It is safe and reasonable to cache /proc entries for a task until
3143 * that task exits. After that they just clog up the dcache with
3144 * useless entries, possibly causing useful dcache entries to be
3145 * flushed instead. This routine is proved to flush those useless
3146 * dcache entries at process exit time.
3148 * NOTE: This routine is just an optimization so it does not guarantee
3149 * that no dcache entries will exist at process exit time it
3150 * just makes it very unlikely that any will persist.
3153 void proc_flush_task(struct task_struct *task)
3156 struct pid *pid, *tgid;
3159 pid = task_pid(task);
3160 tgid = task_tgid(task);
3162 for (i = 0; i <= pid->level; i++) {
3163 upid = &pid->numbers[i];
3164 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3165 tgid->numbers[i].nr);
3169 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3170 struct task_struct *task, const void *ptr)
3172 struct inode *inode;
3174 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3176 return ERR_PTR(-ENOENT);
3178 inode->i_op = &proc_tgid_base_inode_operations;
3179 inode->i_fop = &proc_tgid_base_operations;
3180 inode->i_flags|=S_IMMUTABLE;
3182 set_nlink(inode, nlink_tgid);
3183 pid_update_inode(task, inode);
3185 d_set_d_op(dentry, &pid_dentry_operations);
3186 return d_splice_alias(inode, dentry);
3189 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3191 struct task_struct *task;
3193 struct pid_namespace *ns;
3194 struct dentry *result = ERR_PTR(-ENOENT);
3196 tgid = name_to_int(&dentry->d_name);
3200 ns = dentry->d_sb->s_fs_info;
3202 task = find_task_by_pid_ns(tgid, ns);
3204 get_task_struct(task);
3209 result = proc_pid_instantiate(dentry, task, NULL);
3210 put_task_struct(task);
3216 * Find the first task with tgid >= tgid
3221 struct task_struct *task;
3223 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3228 put_task_struct(iter.task);
3232 pid = find_ge_pid(iter.tgid, ns);
3234 iter.tgid = pid_nr_ns(pid, ns);
3235 iter.task = pid_task(pid, PIDTYPE_PID);
3236 /* What we to know is if the pid we have find is the
3237 * pid of a thread_group_leader. Testing for task
3238 * being a thread_group_leader is the obvious thing
3239 * todo but there is a window when it fails, due to
3240 * the pid transfer logic in de_thread.
3242 * So we perform the straight forward test of seeing
3243 * if the pid we have found is the pid of a thread
3244 * group leader, and don't worry if the task we have
3245 * found doesn't happen to be a thread group leader.
3246 * As we don't care in the case of readdir.
3248 if (!iter.task || !has_group_leader_pid(iter.task)) {
3252 get_task_struct(iter.task);
3258 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3260 /* for the /proc/ directory itself, after non-process stuff has been done */
3261 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3263 struct tgid_iter iter;
3264 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3265 loff_t pos = ctx->pos;
3267 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3270 if (pos == TGID_OFFSET - 2) {
3271 struct inode *inode = d_inode(ns->proc_self);
3272 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3274 ctx->pos = pos = pos + 1;
3276 if (pos == TGID_OFFSET - 1) {
3277 struct inode *inode = d_inode(ns->proc_thread_self);
3278 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3280 ctx->pos = pos = pos + 1;
3282 iter.tgid = pos - TGID_OFFSET;
3284 for (iter = next_tgid(ns, iter);
3286 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3291 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3294 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3295 ctx->pos = iter.tgid + TGID_OFFSET;
3296 if (!proc_fill_cache(file, ctx, name, len,
3297 proc_pid_instantiate, iter.task, NULL)) {
3298 put_task_struct(iter.task);
3302 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3307 * proc_tid_comm_permission is a special permission function exclusively
3308 * used for the node /proc/<pid>/task/<tid>/comm.
3309 * It bypasses generic permission checks in the case where a task of the same
3310 * task group attempts to access the node.
3311 * The rationale behind this is that glibc and bionic access this node for
3312 * cross thread naming (pthread_set/getname_np(!self)). However, if
3313 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3314 * which locks out the cross thread naming implementation.
3315 * This function makes sure that the node is always accessible for members of
3316 * same thread group.
3318 static int proc_tid_comm_permission(struct inode *inode, int mask)
3320 bool is_same_tgroup;
3321 struct task_struct *task;
3323 task = get_proc_task(inode);
3326 is_same_tgroup = same_thread_group(current, task);
3327 put_task_struct(task);
3329 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3330 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3331 * read or written by the members of the corresponding
3337 return generic_permission(inode, mask);
3340 static const struct inode_operations proc_tid_comm_inode_operations = {
3341 .permission = proc_tid_comm_permission,
3347 static const struct pid_entry tid_base_stuff[] = {
3348 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3349 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3350 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3352 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3354 REG("environ", S_IRUSR, proc_environ_operations),
3355 REG("auxv", S_IRUSR, proc_auxv_operations),
3356 ONE("status", S_IRUGO, proc_pid_status),
3357 ONE("personality", S_IRUSR, proc_pid_personality),
3358 ONE("limits", S_IRUGO, proc_pid_limits),
3359 #ifdef CONFIG_SCHED_DEBUG
3360 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3362 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3363 &proc_tid_comm_inode_operations,
3364 &proc_pid_set_comm_operations, {}),
3365 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3366 ONE("syscall", S_IRUSR, proc_pid_syscall),
3368 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3369 ONE("stat", S_IRUGO, proc_tid_stat),
3370 ONE("statm", S_IRUGO, proc_pid_statm),
3371 REG("maps", S_IRUGO, proc_pid_maps_operations),
3372 #ifdef CONFIG_PROC_CHILDREN
3373 REG("children", S_IRUGO, proc_tid_children_operations),
3376 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3378 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3379 LNK("cwd", proc_cwd_link),
3380 LNK("root", proc_root_link),
3381 LNK("exe", proc_exe_link),
3382 REG("mounts", S_IRUGO, proc_mounts_operations),
3383 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3384 #ifdef CONFIG_PROC_PAGE_MONITOR
3385 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3386 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3387 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3388 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3390 #ifdef CONFIG_SECURITY
3391 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3393 #ifdef CONFIG_KALLSYMS
3394 ONE("wchan", S_IRUGO, proc_pid_wchan),
3396 #ifdef CONFIG_STACKTRACE
3397 ONE("stack", S_IRUSR, proc_pid_stack),
3399 #ifdef CONFIG_SCHED_INFO
3400 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3402 #ifdef CONFIG_LATENCYTOP
3403 REG("latency", S_IRUGO, proc_lstats_operations),
3405 #ifdef CONFIG_PROC_PID_CPUSET
3406 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3408 #ifdef CONFIG_CGROUPS
3409 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3411 ONE("oom_score", S_IRUGO, proc_oom_score),
3412 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3413 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3414 #ifdef CONFIG_AUDITSYSCALL
3415 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3416 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3418 #ifdef CONFIG_FAULT_INJECTION
3419 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3420 REG("fail-nth", 0644, proc_fail_nth_operations),
3422 #ifdef CONFIG_TASK_IO_ACCOUNTING
3423 ONE("io", S_IRUSR, proc_tid_io_accounting),
3425 #ifdef CONFIG_USER_NS
3426 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3427 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3428 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3429 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3431 #ifdef CONFIG_LIVEPATCH
3432 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3436 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3438 return proc_pident_readdir(file, ctx,
3439 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3442 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3444 return proc_pident_lookup(dir, dentry,
3445 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3448 static const struct file_operations proc_tid_base_operations = {
3449 .read = generic_read_dir,
3450 .iterate_shared = proc_tid_base_readdir,
3451 .llseek = generic_file_llseek,
3454 static const struct inode_operations proc_tid_base_inode_operations = {
3455 .lookup = proc_tid_base_lookup,
3456 .getattr = pid_getattr,
3457 .setattr = proc_setattr,
3460 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3461 struct task_struct *task, const void *ptr)
3463 struct inode *inode;
3464 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3466 return ERR_PTR(-ENOENT);
3468 inode->i_op = &proc_tid_base_inode_operations;
3469 inode->i_fop = &proc_tid_base_operations;
3470 inode->i_flags |= S_IMMUTABLE;
3472 set_nlink(inode, nlink_tid);
3473 pid_update_inode(task, inode);
3475 d_set_d_op(dentry, &pid_dentry_operations);
3476 return d_splice_alias(inode, dentry);
3479 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3481 struct task_struct *task;
3482 struct task_struct *leader = get_proc_task(dir);
3484 struct pid_namespace *ns;
3485 struct dentry *result = ERR_PTR(-ENOENT);
3490 tid = name_to_int(&dentry->d_name);
3494 ns = dentry->d_sb->s_fs_info;
3496 task = find_task_by_pid_ns(tid, ns);
3498 get_task_struct(task);
3502 if (!same_thread_group(leader, task))
3505 result = proc_task_instantiate(dentry, task, NULL);
3507 put_task_struct(task);
3509 put_task_struct(leader);
3515 * Find the first tid of a thread group to return to user space.
3517 * Usually this is just the thread group leader, but if the users
3518 * buffer was too small or there was a seek into the middle of the
3519 * directory we have more work todo.
3521 * In the case of a short read we start with find_task_by_pid.
3523 * In the case of a seek we start with the leader and walk nr
3526 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3527 struct pid_namespace *ns)
3529 struct task_struct *pos, *task;
3530 unsigned long nr = f_pos;
3532 if (nr != f_pos) /* 32bit overflow? */
3536 task = pid_task(pid, PIDTYPE_PID);
3540 /* Attempt to start with the tid of a thread */
3542 pos = find_task_by_pid_ns(tid, ns);
3543 if (pos && same_thread_group(pos, task))
3547 /* If nr exceeds the number of threads there is nothing todo */
3548 if (nr >= get_nr_threads(task))
3551 /* If we haven't found our starting place yet start
3552 * with the leader and walk nr threads forward.
3554 pos = task = task->group_leader;
3558 } while_each_thread(task, pos);
3563 get_task_struct(pos);
3570 * Find the next thread in the thread list.
3571 * Return NULL if there is an error or no next thread.
3573 * The reference to the input task_struct is released.
3575 static struct task_struct *next_tid(struct task_struct *start)
3577 struct task_struct *pos = NULL;
3579 if (pid_alive(start)) {
3580 pos = next_thread(start);
3581 if (thread_group_leader(pos))
3584 get_task_struct(pos);
3587 put_task_struct(start);
3591 /* for the /proc/TGID/task/ directories */
3592 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3594 struct inode *inode = file_inode(file);
3595 struct task_struct *task;
3596 struct pid_namespace *ns;
3599 if (proc_inode_is_dead(inode))
3602 if (!dir_emit_dots(file, ctx))
3605 /* f_version caches the tgid value that the last readdir call couldn't
3606 * return. lseek aka telldir automagically resets f_version to 0.
3608 ns = proc_pid_ns(inode);
3609 tid = (int)file->f_version;
3610 file->f_version = 0;
3611 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3613 task = next_tid(task), ctx->pos++) {
3616 tid = task_pid_nr_ns(task, ns);
3617 len = snprintf(name, sizeof(name), "%u", tid);
3618 if (!proc_fill_cache(file, ctx, name, len,
3619 proc_task_instantiate, task, NULL)) {
3620 /* returning this tgid failed, save it as the first
3621 * pid for the next readir call */
3622 file->f_version = (u64)tid;
3623 put_task_struct(task);
3631 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3632 u32 request_mask, unsigned int query_flags)
3634 struct inode *inode = d_inode(path->dentry);
3635 struct task_struct *p = get_proc_task(inode);
3636 generic_fillattr(inode, stat);
3639 stat->nlink += get_nr_threads(p);
3646 static const struct inode_operations proc_task_inode_operations = {
3647 .lookup = proc_task_lookup,
3648 .getattr = proc_task_getattr,
3649 .setattr = proc_setattr,
3650 .permission = proc_pid_permission,
3653 static const struct file_operations proc_task_operations = {
3654 .read = generic_read_dir,
3655 .iterate_shared = proc_task_readdir,
3656 .llseek = generic_file_llseek,
3659 void __init set_proc_pid_nlink(void)
3661 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3662 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));