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.
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.
45 * Smaps information related to shared, private, clean and dirty pages.
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 } )
146 #define ATTR(LSM, NAME, MODE) \
147 NOD(NAME, (S_IFREG|(MODE)), \
148 NULL, &proc_pid_attr_operations, \
152 * Count the number of hardlinks for the pid_entry table, excluding the .
155 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
162 for (i = 0; i < n; ++i) {
163 if (S_ISDIR(entries[i].mode))
170 static int get_task_root(struct task_struct *task, struct path *root)
172 int result = -ENOENT;
176 get_fs_root(task->fs, root);
183 static int proc_cwd_link(struct dentry *dentry, struct path *path)
185 struct task_struct *task = get_proc_task(d_inode(dentry));
186 int result = -ENOENT;
191 get_fs_pwd(task->fs, path);
195 put_task_struct(task);
200 static int proc_root_link(struct dentry *dentry, struct path *path)
202 struct task_struct *task = get_proc_task(d_inode(dentry));
203 int result = -ENOENT;
206 result = get_task_root(task, path);
207 put_task_struct(task);
212 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
213 size_t count, loff_t *ppos)
215 unsigned long arg_start, arg_end, env_start, env_end;
216 unsigned long pos, len;
219 /* Check if process spawned far enough to have cmdline. */
223 spin_lock(&mm->arg_lock);
224 arg_start = mm->arg_start;
225 arg_end = mm->arg_end;
226 env_start = mm->env_start;
227 env_end = mm->env_end;
228 spin_unlock(&mm->arg_lock);
230 if (arg_start >= arg_end)
234 * We have traditionally allowed the user to re-write
235 * the argument strings and overflow the end result
236 * into the environment section. But only do that if
237 * the environment area is contiguous to the arguments.
239 if (env_start != arg_end || env_start >= env_end)
240 env_start = env_end = arg_end;
242 /* .. and limit it to a maximum of one page of slop */
243 if (env_end >= arg_end + PAGE_SIZE)
244 env_end = arg_end + PAGE_SIZE - 1;
246 /* We're not going to care if "*ppos" has high bits set */
247 pos = arg_start + *ppos;
249 /* .. but we do check the result is in the proper range */
250 if (pos < arg_start || pos >= env_end)
253 /* .. and we never go past env_end */
254 if (env_end - pos < count)
255 count = env_end - pos;
257 page = (char *)__get_free_page(GFP_KERNEL);
264 size_t size = min_t(size_t, PAGE_SIZE, count);
268 * Are we already starting past the official end?
269 * We always include the last byte that is *supposed*
272 offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
274 got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
279 /* Don't walk past a NUL character once you hit arg_end */
280 if (pos + got >= arg_end) {
284 * If we started before 'arg_end' but ended up
285 * at or after it, we start the NUL character
286 * check at arg_end-1 (where we expect the normal
289 * NOTE! This is smaller than 'got', because
290 * pos + got >= arg_end
293 n = arg_end - pos - 1;
295 /* Cut off at first NUL after 'n' */
296 got = n + strnlen(page+n, offset+got-n);
301 /* Include the NUL if it existed */
306 got -= copy_to_user(buf, page+offset, got);
307 if (unlikely(!got)) {
318 free_page((unsigned long)page);
322 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
323 size_t count, loff_t *pos)
325 struct mm_struct *mm;
328 mm = get_task_mm(tsk);
332 ret = get_mm_cmdline(mm, buf, count, pos);
337 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
338 size_t count, loff_t *pos)
340 struct task_struct *tsk;
345 tsk = get_proc_task(file_inode(file));
348 ret = get_task_cmdline(tsk, buf, count, pos);
349 put_task_struct(tsk);
355 static const struct file_operations proc_pid_cmdline_ops = {
356 .read = proc_pid_cmdline_read,
357 .llseek = generic_file_llseek,
360 #ifdef CONFIG_KALLSYMS
362 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
363 * Returns the resolved symbol. If that fails, simply return the address.
365 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
366 struct pid *pid, struct task_struct *task)
369 char symname[KSYM_NAME_LEN];
371 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
374 wchan = get_wchan(task);
375 if (wchan && !lookup_symbol_name(wchan, symname)) {
376 seq_puts(m, symname);
384 #endif /* CONFIG_KALLSYMS */
386 static int lock_trace(struct task_struct *task)
388 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
391 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
392 mutex_unlock(&task->signal->cred_guard_mutex);
398 static void unlock_trace(struct task_struct *task)
400 mutex_unlock(&task->signal->cred_guard_mutex);
403 #ifdef CONFIG_STACKTRACE
405 #define MAX_STACK_TRACE_DEPTH 64
407 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
408 struct pid *pid, struct task_struct *task)
410 struct stack_trace trace;
411 unsigned long *entries;
415 * The ability to racily run the kernel stack unwinder on a running task
416 * and then observe the unwinder output is scary; while it is useful for
417 * debugging kernel issues, it can also allow an attacker to leak kernel
419 * Doing this in a manner that is at least safe from races would require
420 * some work to ensure that the remote task can not be scheduled; and
421 * even then, this would still expose the unwinder as local attack
423 * Therefore, this interface is restricted to root.
425 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
428 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
433 trace.nr_entries = 0;
434 trace.max_entries = MAX_STACK_TRACE_DEPTH;
435 trace.entries = entries;
438 err = lock_trace(task);
442 save_stack_trace_tsk(task, &trace);
444 for (i = 0; i < trace.nr_entries; i++) {
445 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
455 #ifdef CONFIG_SCHED_INFO
457 * Provides /proc/PID/schedstat
459 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
460 struct pid *pid, struct task_struct *task)
462 if (unlikely(!sched_info_on()))
463 seq_puts(m, "0 0 0\n");
465 seq_printf(m, "%llu %llu %lu\n",
466 (unsigned long long)task->se.sum_exec_runtime,
467 (unsigned long long)task->sched_info.run_delay,
468 task->sched_info.pcount);
474 #ifdef CONFIG_LATENCYTOP
475 static int lstats_show_proc(struct seq_file *m, void *v)
478 struct inode *inode = m->private;
479 struct task_struct *task = get_proc_task(inode);
483 seq_puts(m, "Latency Top version : v0.1\n");
484 for (i = 0; i < LT_SAVECOUNT; i++) {
485 struct latency_record *lr = &task->latency_record[i];
486 if (lr->backtrace[0]) {
488 seq_printf(m, "%i %li %li",
489 lr->count, lr->time, lr->max);
490 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
491 unsigned long bt = lr->backtrace[q];
496 seq_printf(m, " %ps", (void *)bt);
502 put_task_struct(task);
506 static int lstats_open(struct inode *inode, struct file *file)
508 return single_open(file, lstats_show_proc, inode);
511 static ssize_t lstats_write(struct file *file, const char __user *buf,
512 size_t count, loff_t *offs)
514 struct task_struct *task = get_proc_task(file_inode(file));
518 clear_all_latency_tracing(task);
519 put_task_struct(task);
524 static const struct file_operations proc_lstats_operations = {
527 .write = lstats_write,
529 .release = single_release,
534 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
535 struct pid *pid, struct task_struct *task)
537 unsigned long totalpages = totalram_pages() + total_swap_pages;
538 unsigned long points = 0;
540 points = oom_badness(task, NULL, NULL, totalpages) *
542 seq_printf(m, "%lu\n", points);
552 static const struct limit_names lnames[RLIM_NLIMITS] = {
553 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
554 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
555 [RLIMIT_DATA] = {"Max data size", "bytes"},
556 [RLIMIT_STACK] = {"Max stack size", "bytes"},
557 [RLIMIT_CORE] = {"Max core file size", "bytes"},
558 [RLIMIT_RSS] = {"Max resident set", "bytes"},
559 [RLIMIT_NPROC] = {"Max processes", "processes"},
560 [RLIMIT_NOFILE] = {"Max open files", "files"},
561 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
562 [RLIMIT_AS] = {"Max address space", "bytes"},
563 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
564 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
565 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
566 [RLIMIT_NICE] = {"Max nice priority", NULL},
567 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
568 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
571 /* Display limits for a process */
572 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
573 struct pid *pid, struct task_struct *task)
578 struct rlimit rlim[RLIM_NLIMITS];
580 if (!lock_task_sighand(task, &flags))
582 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
583 unlock_task_sighand(task, &flags);
586 * print the file header
593 for (i = 0; i < RLIM_NLIMITS; i++) {
594 if (rlim[i].rlim_cur == RLIM_INFINITY)
595 seq_printf(m, "%-25s %-20s ",
596 lnames[i].name, "unlimited");
598 seq_printf(m, "%-25s %-20lu ",
599 lnames[i].name, rlim[i].rlim_cur);
601 if (rlim[i].rlim_max == RLIM_INFINITY)
602 seq_printf(m, "%-20s ", "unlimited");
604 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
607 seq_printf(m, "%-10s\n", lnames[i].unit);
615 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
616 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
617 struct pid *pid, struct task_struct *task)
620 unsigned long args[6], sp, pc;
623 res = lock_trace(task);
627 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
628 seq_puts(m, "running\n");
630 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
633 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
635 args[0], args[1], args[2], args[3], args[4], args[5],
641 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
643 /************************************************************************/
644 /* Here the fs part begins */
645 /************************************************************************/
647 /* permission checks */
648 static int proc_fd_access_allowed(struct inode *inode)
650 struct task_struct *task;
652 /* Allow access to a task's file descriptors if it is us or we
653 * may use ptrace attach to the process and find out that
656 task = get_proc_task(inode);
658 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
659 put_task_struct(task);
664 int proc_setattr(struct dentry *dentry, struct iattr *attr)
667 struct inode *inode = d_inode(dentry);
669 if (attr->ia_valid & ATTR_MODE)
672 error = setattr_prepare(dentry, attr);
676 setattr_copy(inode, attr);
677 mark_inode_dirty(inode);
682 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
683 * or euid/egid (for hide_pid_min=2)?
685 static bool has_pid_permissions(struct pid_namespace *pid,
686 struct task_struct *task,
689 if (pid->hide_pid < hide_pid_min)
691 if (in_group_p(pid->pid_gid))
693 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
697 static int proc_pid_permission(struct inode *inode, int mask)
699 struct pid_namespace *pid = proc_pid_ns(inode);
700 struct task_struct *task;
703 task = get_proc_task(inode);
706 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
707 put_task_struct(task);
710 if (pid->hide_pid == HIDEPID_INVISIBLE) {
712 * Let's make getdents(), stat(), and open()
713 * consistent with each other. If a process
714 * may not stat() a file, it shouldn't be seen
722 return generic_permission(inode, mask);
727 static const struct inode_operations proc_def_inode_operations = {
728 .setattr = proc_setattr,
731 static int proc_single_show(struct seq_file *m, void *v)
733 struct inode *inode = m->private;
734 struct pid_namespace *ns = proc_pid_ns(inode);
735 struct pid *pid = proc_pid(inode);
736 struct task_struct *task;
739 task = get_pid_task(pid, PIDTYPE_PID);
743 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
745 put_task_struct(task);
749 static int proc_single_open(struct inode *inode, struct file *filp)
751 return single_open(filp, proc_single_show, inode);
754 static const struct file_operations proc_single_file_operations = {
755 .open = proc_single_open,
758 .release = single_release,
762 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
764 struct task_struct *task = get_proc_task(inode);
765 struct mm_struct *mm = ERR_PTR(-ESRCH);
768 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
769 put_task_struct(task);
771 if (!IS_ERR_OR_NULL(mm)) {
772 /* ensure this mm_struct can't be freed */
774 /* but do not pin its memory */
782 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
784 struct mm_struct *mm = proc_mem_open(inode, mode);
789 file->private_data = mm;
793 static int mem_open(struct inode *inode, struct file *file)
795 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
797 /* OK to pass negative loff_t, we can catch out-of-range */
798 file->f_mode |= FMODE_UNSIGNED_OFFSET;
803 static ssize_t mem_rw(struct file *file, char __user *buf,
804 size_t count, loff_t *ppos, int write)
806 struct mm_struct *mm = file->private_data;
807 unsigned long addr = *ppos;
815 page = (char *)__get_free_page(GFP_KERNEL);
820 if (!mmget_not_zero(mm))
823 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
826 int this_len = min_t(int, count, PAGE_SIZE);
828 if (write && copy_from_user(page, buf, this_len)) {
833 this_len = access_remote_vm(mm, addr, page, this_len, flags);
840 if (!write && copy_to_user(buf, page, this_len)) {
854 free_page((unsigned long) page);
858 static ssize_t mem_read(struct file *file, char __user *buf,
859 size_t count, loff_t *ppos)
861 return mem_rw(file, buf, count, ppos, 0);
864 static ssize_t mem_write(struct file *file, const char __user *buf,
865 size_t count, loff_t *ppos)
867 return mem_rw(file, (char __user*)buf, count, ppos, 1);
870 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
874 file->f_pos = offset;
877 file->f_pos += offset;
882 force_successful_syscall_return();
886 static int mem_release(struct inode *inode, struct file *file)
888 struct mm_struct *mm = file->private_data;
894 static const struct file_operations proc_mem_operations = {
899 .release = mem_release,
902 static int environ_open(struct inode *inode, struct file *file)
904 return __mem_open(inode, file, PTRACE_MODE_READ);
907 static ssize_t environ_read(struct file *file, char __user *buf,
908 size_t count, loff_t *ppos)
911 unsigned long src = *ppos;
913 struct mm_struct *mm = file->private_data;
914 unsigned long env_start, env_end;
916 /* Ensure the process spawned far enough to have an environment. */
917 if (!mm || !mm->env_end)
920 page = (char *)__get_free_page(GFP_KERNEL);
925 if (!mmget_not_zero(mm))
928 spin_lock(&mm->arg_lock);
929 env_start = mm->env_start;
930 env_end = mm->env_end;
931 spin_unlock(&mm->arg_lock);
934 size_t this_len, max_len;
937 if (src >= (env_end - env_start))
940 this_len = env_end - (env_start + src);
942 max_len = min_t(size_t, PAGE_SIZE, count);
943 this_len = min(max_len, this_len);
945 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
952 if (copy_to_user(buf, page, retval)) {
966 free_page((unsigned long) page);
970 static const struct file_operations proc_environ_operations = {
971 .open = environ_open,
972 .read = environ_read,
973 .llseek = generic_file_llseek,
974 .release = mem_release,
977 static int auxv_open(struct inode *inode, struct file *file)
979 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
982 static ssize_t auxv_read(struct file *file, char __user *buf,
983 size_t count, loff_t *ppos)
985 struct mm_struct *mm = file->private_data;
986 unsigned int nwords = 0;
992 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
993 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
994 nwords * sizeof(mm->saved_auxv[0]));
997 static const struct file_operations proc_auxv_operations = {
1000 .llseek = generic_file_llseek,
1001 .release = mem_release,
1004 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1007 struct task_struct *task = get_proc_task(file_inode(file));
1008 char buffer[PROC_NUMBUF];
1009 int oom_adj = OOM_ADJUST_MIN;
1014 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1015 oom_adj = OOM_ADJUST_MAX;
1017 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1019 put_task_struct(task);
1020 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1021 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1024 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1026 static DEFINE_MUTEX(oom_adj_mutex);
1027 struct mm_struct *mm = NULL;
1028 struct task_struct *task;
1031 task = get_proc_task(file_inode(file));
1035 mutex_lock(&oom_adj_mutex);
1037 if (oom_adj < task->signal->oom_score_adj &&
1038 !capable(CAP_SYS_RESOURCE)) {
1043 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1044 * /proc/pid/oom_score_adj instead.
1046 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1047 current->comm, task_pid_nr(current), task_pid_nr(task),
1050 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1051 !capable(CAP_SYS_RESOURCE)) {
1058 * Make sure we will check other processes sharing the mm if this is
1059 * not vfrok which wants its own oom_score_adj.
1060 * pin the mm so it doesn't go away and get reused after task_unlock
1062 if (!task->vfork_done) {
1063 struct task_struct *p = find_lock_task_mm(task);
1066 if (atomic_read(&p->mm->mm_users) > 1) {
1074 task->signal->oom_score_adj = oom_adj;
1075 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1076 task->signal->oom_score_adj_min = (short)oom_adj;
1077 trace_oom_score_adj_update(task);
1080 struct task_struct *p;
1083 for_each_process(p) {
1084 if (same_thread_group(task, p))
1087 /* do not touch kernel threads or the global init */
1088 if (p->flags & PF_KTHREAD || is_global_init(p))
1092 if (!p->vfork_done && process_shares_mm(p, mm)) {
1093 p->signal->oom_score_adj = oom_adj;
1094 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1095 p->signal->oom_score_adj_min = (short)oom_adj;
1103 mutex_unlock(&oom_adj_mutex);
1104 put_task_struct(task);
1109 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1110 * kernels. The effective policy is defined by oom_score_adj, which has a
1111 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1112 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1113 * Processes that become oom disabled via oom_adj will still be oom disabled
1114 * with this implementation.
1116 * oom_adj cannot be removed since existing userspace binaries use it.
1118 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1119 size_t count, loff_t *ppos)
1121 char buffer[PROC_NUMBUF];
1125 memset(buffer, 0, sizeof(buffer));
1126 if (count > sizeof(buffer) - 1)
1127 count = sizeof(buffer) - 1;
1128 if (copy_from_user(buffer, buf, count)) {
1133 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1136 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1137 oom_adj != OOM_DISABLE) {
1143 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1144 * value is always attainable.
1146 if (oom_adj == OOM_ADJUST_MAX)
1147 oom_adj = OOM_SCORE_ADJ_MAX;
1149 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1151 err = __set_oom_adj(file, oom_adj, true);
1153 return err < 0 ? err : count;
1156 static const struct file_operations proc_oom_adj_operations = {
1157 .read = oom_adj_read,
1158 .write = oom_adj_write,
1159 .llseek = generic_file_llseek,
1162 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1163 size_t count, loff_t *ppos)
1165 struct task_struct *task = get_proc_task(file_inode(file));
1166 char buffer[PROC_NUMBUF];
1167 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1172 oom_score_adj = task->signal->oom_score_adj;
1173 put_task_struct(task);
1174 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1175 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1178 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1179 size_t count, loff_t *ppos)
1181 char buffer[PROC_NUMBUF];
1185 memset(buffer, 0, sizeof(buffer));
1186 if (count > sizeof(buffer) - 1)
1187 count = sizeof(buffer) - 1;
1188 if (copy_from_user(buffer, buf, count)) {
1193 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1196 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1197 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1202 err = __set_oom_adj(file, oom_score_adj, false);
1204 return err < 0 ? err : count;
1207 static const struct file_operations proc_oom_score_adj_operations = {
1208 .read = oom_score_adj_read,
1209 .write = oom_score_adj_write,
1210 .llseek = default_llseek,
1214 #define TMPBUFLEN 11
1215 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1216 size_t count, loff_t *ppos)
1218 struct inode * inode = file_inode(file);
1219 struct task_struct *task = get_proc_task(inode);
1221 char tmpbuf[TMPBUFLEN];
1225 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1226 from_kuid(file->f_cred->user_ns,
1227 audit_get_loginuid(task)));
1228 put_task_struct(task);
1229 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1232 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1233 size_t count, loff_t *ppos)
1235 struct inode * inode = file_inode(file);
1241 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1248 /* No partial writes. */
1252 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1256 /* is userspace tring to explicitly UNSET the loginuid? */
1257 if (loginuid == AUDIT_UID_UNSET) {
1258 kloginuid = INVALID_UID;
1260 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1261 if (!uid_valid(kloginuid))
1265 rv = audit_set_loginuid(kloginuid);
1271 static const struct file_operations proc_loginuid_operations = {
1272 .read = proc_loginuid_read,
1273 .write = proc_loginuid_write,
1274 .llseek = generic_file_llseek,
1277 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1278 size_t count, loff_t *ppos)
1280 struct inode * inode = file_inode(file);
1281 struct task_struct *task = get_proc_task(inode);
1283 char tmpbuf[TMPBUFLEN];
1287 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1288 audit_get_sessionid(task));
1289 put_task_struct(task);
1290 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1293 static const struct file_operations proc_sessionid_operations = {
1294 .read = proc_sessionid_read,
1295 .llseek = generic_file_llseek,
1299 #ifdef CONFIG_FAULT_INJECTION
1300 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1301 size_t count, loff_t *ppos)
1303 struct task_struct *task = get_proc_task(file_inode(file));
1304 char buffer[PROC_NUMBUF];
1310 make_it_fail = task->make_it_fail;
1311 put_task_struct(task);
1313 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1315 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1318 static ssize_t proc_fault_inject_write(struct file * file,
1319 const char __user * buf, size_t count, loff_t *ppos)
1321 struct task_struct *task;
1322 char buffer[PROC_NUMBUF];
1326 if (!capable(CAP_SYS_RESOURCE))
1328 memset(buffer, 0, sizeof(buffer));
1329 if (count > sizeof(buffer) - 1)
1330 count = sizeof(buffer) - 1;
1331 if (copy_from_user(buffer, buf, count))
1333 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1336 if (make_it_fail < 0 || make_it_fail > 1)
1339 task = get_proc_task(file_inode(file));
1342 task->make_it_fail = make_it_fail;
1343 put_task_struct(task);
1348 static const struct file_operations proc_fault_inject_operations = {
1349 .read = proc_fault_inject_read,
1350 .write = proc_fault_inject_write,
1351 .llseek = generic_file_llseek,
1354 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1355 size_t count, loff_t *ppos)
1357 struct task_struct *task;
1361 err = kstrtouint_from_user(buf, count, 0, &n);
1365 task = get_proc_task(file_inode(file));
1369 put_task_struct(task);
1374 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1375 size_t count, loff_t *ppos)
1377 struct task_struct *task;
1378 char numbuf[PROC_NUMBUF];
1381 task = get_proc_task(file_inode(file));
1384 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1385 put_task_struct(task);
1386 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1389 static const struct file_operations proc_fail_nth_operations = {
1390 .read = proc_fail_nth_read,
1391 .write = proc_fail_nth_write,
1396 #ifdef CONFIG_SCHED_DEBUG
1398 * Print out various scheduling related per-task fields:
1400 static int sched_show(struct seq_file *m, void *v)
1402 struct inode *inode = m->private;
1403 struct pid_namespace *ns = proc_pid_ns(inode);
1404 struct task_struct *p;
1406 p = get_proc_task(inode);
1409 proc_sched_show_task(p, ns, m);
1417 sched_write(struct file *file, const char __user *buf,
1418 size_t count, loff_t *offset)
1420 struct inode *inode = file_inode(file);
1421 struct task_struct *p;
1423 p = get_proc_task(inode);
1426 proc_sched_set_task(p);
1433 static int sched_open(struct inode *inode, struct file *filp)
1435 return single_open(filp, sched_show, inode);
1438 static const struct file_operations proc_pid_sched_operations = {
1441 .write = sched_write,
1442 .llseek = seq_lseek,
1443 .release = single_release,
1448 #ifdef CONFIG_SCHED_AUTOGROUP
1450 * Print out autogroup related information:
1452 static int sched_autogroup_show(struct seq_file *m, void *v)
1454 struct inode *inode = m->private;
1455 struct task_struct *p;
1457 p = get_proc_task(inode);
1460 proc_sched_autogroup_show_task(p, m);
1468 sched_autogroup_write(struct file *file, const char __user *buf,
1469 size_t count, loff_t *offset)
1471 struct inode *inode = file_inode(file);
1472 struct task_struct *p;
1473 char buffer[PROC_NUMBUF];
1477 memset(buffer, 0, sizeof(buffer));
1478 if (count > sizeof(buffer) - 1)
1479 count = sizeof(buffer) - 1;
1480 if (copy_from_user(buffer, buf, count))
1483 err = kstrtoint(strstrip(buffer), 0, &nice);
1487 p = get_proc_task(inode);
1491 err = proc_sched_autogroup_set_nice(p, nice);
1500 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1504 ret = single_open(filp, sched_autogroup_show, NULL);
1506 struct seq_file *m = filp->private_data;
1513 static const struct file_operations proc_pid_sched_autogroup_operations = {
1514 .open = sched_autogroup_open,
1516 .write = sched_autogroup_write,
1517 .llseek = seq_lseek,
1518 .release = single_release,
1521 #endif /* CONFIG_SCHED_AUTOGROUP */
1523 static ssize_t comm_write(struct file *file, const char __user *buf,
1524 size_t count, loff_t *offset)
1526 struct inode *inode = file_inode(file);
1527 struct task_struct *p;
1528 char buffer[TASK_COMM_LEN];
1529 const size_t maxlen = sizeof(buffer) - 1;
1531 memset(buffer, 0, sizeof(buffer));
1532 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1535 p = get_proc_task(inode);
1539 if (same_thread_group(current, p))
1540 set_task_comm(p, buffer);
1549 static int comm_show(struct seq_file *m, void *v)
1551 struct inode *inode = m->private;
1552 struct task_struct *p;
1554 p = get_proc_task(inode);
1558 proc_task_name(m, p, false);
1566 static int comm_open(struct inode *inode, struct file *filp)
1568 return single_open(filp, comm_show, inode);
1571 static const struct file_operations proc_pid_set_comm_operations = {
1574 .write = comm_write,
1575 .llseek = seq_lseek,
1576 .release = single_release,
1579 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1581 struct task_struct *task;
1582 struct file *exe_file;
1584 task = get_proc_task(d_inode(dentry));
1587 exe_file = get_task_exe_file(task);
1588 put_task_struct(task);
1590 *exe_path = exe_file->f_path;
1591 path_get(&exe_file->f_path);
1598 static const char *proc_pid_get_link(struct dentry *dentry,
1599 struct inode *inode,
1600 struct delayed_call *done)
1603 int error = -EACCES;
1606 return ERR_PTR(-ECHILD);
1608 /* Are we allowed to snoop on the tasks file descriptors? */
1609 if (!proc_fd_access_allowed(inode))
1612 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1616 nd_jump_link(&path);
1619 return ERR_PTR(error);
1622 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1624 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1631 pathname = d_path(path, tmp, PAGE_SIZE);
1632 len = PTR_ERR(pathname);
1633 if (IS_ERR(pathname))
1635 len = tmp + PAGE_SIZE - 1 - pathname;
1639 if (copy_to_user(buffer, pathname, len))
1642 free_page((unsigned long)tmp);
1646 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1648 int error = -EACCES;
1649 struct inode *inode = d_inode(dentry);
1652 /* Are we allowed to snoop on the tasks file descriptors? */
1653 if (!proc_fd_access_allowed(inode))
1656 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1660 error = do_proc_readlink(&path, buffer, buflen);
1666 const struct inode_operations proc_pid_link_inode_operations = {
1667 .readlink = proc_pid_readlink,
1668 .get_link = proc_pid_get_link,
1669 .setattr = proc_setattr,
1673 /* building an inode */
1675 void task_dump_owner(struct task_struct *task, umode_t mode,
1676 kuid_t *ruid, kgid_t *rgid)
1678 /* Depending on the state of dumpable compute who should own a
1679 * proc file for a task.
1681 const struct cred *cred;
1685 if (unlikely(task->flags & PF_KTHREAD)) {
1686 *ruid = GLOBAL_ROOT_UID;
1687 *rgid = GLOBAL_ROOT_GID;
1691 /* Default to the tasks effective ownership */
1693 cred = __task_cred(task);
1699 * Before the /proc/pid/status file was created the only way to read
1700 * the effective uid of a /process was to stat /proc/pid. Reading
1701 * /proc/pid/status is slow enough that procps and other packages
1702 * kept stating /proc/pid. To keep the rules in /proc simple I have
1703 * made this apply to all per process world readable and executable
1706 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1707 struct mm_struct *mm;
1710 /* Make non-dumpable tasks owned by some root */
1712 if (get_dumpable(mm) != SUID_DUMP_USER) {
1713 struct user_namespace *user_ns = mm->user_ns;
1715 uid = make_kuid(user_ns, 0);
1716 if (!uid_valid(uid))
1717 uid = GLOBAL_ROOT_UID;
1719 gid = make_kgid(user_ns, 0);
1720 if (!gid_valid(gid))
1721 gid = GLOBAL_ROOT_GID;
1724 uid = GLOBAL_ROOT_UID;
1725 gid = GLOBAL_ROOT_GID;
1733 struct inode *proc_pid_make_inode(struct super_block * sb,
1734 struct task_struct *task, umode_t mode)
1736 struct inode * inode;
1737 struct proc_inode *ei;
1739 /* We need a new inode */
1741 inode = new_inode(sb);
1747 inode->i_mode = mode;
1748 inode->i_ino = get_next_ino();
1749 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1750 inode->i_op = &proc_def_inode_operations;
1753 * grab the reference to task.
1755 ei->pid = get_task_pid(task, PIDTYPE_PID);
1759 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1760 security_task_to_inode(task, inode);
1770 int pid_getattr(const struct path *path, struct kstat *stat,
1771 u32 request_mask, unsigned int query_flags)
1773 struct inode *inode = d_inode(path->dentry);
1774 struct pid_namespace *pid = proc_pid_ns(inode);
1775 struct task_struct *task;
1777 generic_fillattr(inode, stat);
1779 stat->uid = GLOBAL_ROOT_UID;
1780 stat->gid = GLOBAL_ROOT_GID;
1782 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1784 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1787 * This doesn't prevent learning whether PID exists,
1788 * it only makes getattr() consistent with readdir().
1792 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1801 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1803 void pid_update_inode(struct task_struct *task, struct inode *inode)
1805 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1807 inode->i_mode &= ~(S_ISUID | S_ISGID);
1808 security_task_to_inode(task, inode);
1812 * Rewrite the inode's ownerships here because the owning task may have
1813 * performed a setuid(), etc.
1816 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1818 struct inode *inode;
1819 struct task_struct *task;
1821 if (flags & LOOKUP_RCU)
1824 inode = d_inode(dentry);
1825 task = get_proc_task(inode);
1828 pid_update_inode(task, inode);
1829 put_task_struct(task);
1835 static inline bool proc_inode_is_dead(struct inode *inode)
1837 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1840 int pid_delete_dentry(const struct dentry *dentry)
1842 /* Is the task we represent dead?
1843 * If so, then don't put the dentry on the lru list,
1844 * kill it immediately.
1846 return proc_inode_is_dead(d_inode(dentry));
1849 const struct dentry_operations pid_dentry_operations =
1851 .d_revalidate = pid_revalidate,
1852 .d_delete = pid_delete_dentry,
1858 * Fill a directory entry.
1860 * If possible create the dcache entry and derive our inode number and
1861 * file type from dcache entry.
1863 * Since all of the proc inode numbers are dynamically generated, the inode
1864 * numbers do not exist until the inode is cache. This means creating the
1865 * the dcache entry in readdir is necessary to keep the inode numbers
1866 * reported by readdir in sync with the inode numbers reported
1869 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1870 const char *name, unsigned int len,
1871 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1873 struct dentry *child, *dir = file->f_path.dentry;
1874 struct qstr qname = QSTR_INIT(name, len);
1875 struct inode *inode;
1876 unsigned type = DT_UNKNOWN;
1879 child = d_hash_and_lookup(dir, &qname);
1881 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1882 child = d_alloc_parallel(dir, &qname, &wq);
1884 goto end_instantiate;
1885 if (d_in_lookup(child)) {
1887 res = instantiate(child, task, ptr);
1888 d_lookup_done(child);
1889 if (unlikely(res)) {
1893 goto end_instantiate;
1897 inode = d_inode(child);
1899 type = inode->i_mode >> 12;
1902 return dir_emit(ctx, name, len, ino, type);
1906 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1907 * which represent vma start and end addresses.
1909 static int dname_to_vma_addr(struct dentry *dentry,
1910 unsigned long *start, unsigned long *end)
1912 const char *str = dentry->d_name.name;
1913 unsigned long long sval, eval;
1916 if (str[0] == '0' && str[1] != '-')
1918 len = _parse_integer(str, 16, &sval);
1919 if (len & KSTRTOX_OVERFLOW)
1921 if (sval != (unsigned long)sval)
1929 if (str[0] == '0' && str[1])
1931 len = _parse_integer(str, 16, &eval);
1932 if (len & KSTRTOX_OVERFLOW)
1934 if (eval != (unsigned long)eval)
1947 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1949 unsigned long vm_start, vm_end;
1950 bool exact_vma_exists = false;
1951 struct mm_struct *mm = NULL;
1952 struct task_struct *task;
1953 struct inode *inode;
1956 if (flags & LOOKUP_RCU)
1959 inode = d_inode(dentry);
1960 task = get_proc_task(inode);
1964 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1965 if (IS_ERR_OR_NULL(mm))
1968 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1969 down_read(&mm->mmap_sem);
1970 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1971 up_read(&mm->mmap_sem);
1976 if (exact_vma_exists) {
1977 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1979 security_task_to_inode(task, inode);
1984 put_task_struct(task);
1990 static const struct dentry_operations tid_map_files_dentry_operations = {
1991 .d_revalidate = map_files_d_revalidate,
1992 .d_delete = pid_delete_dentry,
1995 static int map_files_get_link(struct dentry *dentry, struct path *path)
1997 unsigned long vm_start, vm_end;
1998 struct vm_area_struct *vma;
1999 struct task_struct *task;
2000 struct mm_struct *mm;
2004 task = get_proc_task(d_inode(dentry));
2008 mm = get_task_mm(task);
2009 put_task_struct(task);
2013 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2018 down_read(&mm->mmap_sem);
2019 vma = find_exact_vma(mm, vm_start, vm_end);
2020 if (vma && vma->vm_file) {
2021 *path = vma->vm_file->f_path;
2025 up_read(&mm->mmap_sem);
2033 struct map_files_info {
2034 unsigned long start;
2040 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2041 * symlinks may be used to bypass permissions on ancestor directories in the
2042 * path to the file in question.
2045 proc_map_files_get_link(struct dentry *dentry,
2046 struct inode *inode,
2047 struct delayed_call *done)
2049 if (!capable(CAP_SYS_ADMIN))
2050 return ERR_PTR(-EPERM);
2052 return proc_pid_get_link(dentry, inode, done);
2056 * Identical to proc_pid_link_inode_operations except for get_link()
2058 static const struct inode_operations proc_map_files_link_inode_operations = {
2059 .readlink = proc_pid_readlink,
2060 .get_link = proc_map_files_get_link,
2061 .setattr = proc_setattr,
2064 static struct dentry *
2065 proc_map_files_instantiate(struct dentry *dentry,
2066 struct task_struct *task, const void *ptr)
2068 fmode_t mode = (fmode_t)(unsigned long)ptr;
2069 struct proc_inode *ei;
2070 struct inode *inode;
2072 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2073 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2074 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2076 return ERR_PTR(-ENOENT);
2079 ei->op.proc_get_link = map_files_get_link;
2081 inode->i_op = &proc_map_files_link_inode_operations;
2084 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2085 return d_splice_alias(inode, dentry);
2088 static struct dentry *proc_map_files_lookup(struct inode *dir,
2089 struct dentry *dentry, unsigned int flags)
2091 unsigned long vm_start, vm_end;
2092 struct vm_area_struct *vma;
2093 struct task_struct *task;
2094 struct dentry *result;
2095 struct mm_struct *mm;
2097 result = ERR_PTR(-ENOENT);
2098 task = get_proc_task(dir);
2102 result = ERR_PTR(-EACCES);
2103 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2106 result = ERR_PTR(-ENOENT);
2107 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2110 mm = get_task_mm(task);
2114 down_read(&mm->mmap_sem);
2115 vma = find_exact_vma(mm, vm_start, vm_end);
2120 result = proc_map_files_instantiate(dentry, task,
2121 (void *)(unsigned long)vma->vm_file->f_mode);
2124 up_read(&mm->mmap_sem);
2127 put_task_struct(task);
2132 static const struct inode_operations proc_map_files_inode_operations = {
2133 .lookup = proc_map_files_lookup,
2134 .permission = proc_fd_permission,
2135 .setattr = proc_setattr,
2139 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2141 struct vm_area_struct *vma;
2142 struct task_struct *task;
2143 struct mm_struct *mm;
2144 unsigned long nr_files, pos, i;
2145 struct flex_array *fa = NULL;
2146 struct map_files_info info;
2147 struct map_files_info *p;
2151 task = get_proc_task(file_inode(file));
2156 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2160 if (!dir_emit_dots(file, ctx))
2163 mm = get_task_mm(task);
2166 down_read(&mm->mmap_sem);
2171 * We need two passes here:
2173 * 1) Collect vmas of mapped files with mmap_sem taken
2174 * 2) Release mmap_sem and instantiate entries
2176 * otherwise we get lockdep complained, since filldir()
2177 * routine might require mmap_sem taken in might_fault().
2180 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2181 if (vma->vm_file && ++pos > ctx->pos)
2186 fa = flex_array_alloc(sizeof(info), nr_files,
2188 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2192 flex_array_free(fa);
2193 up_read(&mm->mmap_sem);
2197 for (i = 0, vma = mm->mmap, pos = 2; vma;
2198 vma = vma->vm_next) {
2201 if (++pos <= ctx->pos)
2204 info.start = vma->vm_start;
2205 info.end = vma->vm_end;
2206 info.mode = vma->vm_file->f_mode;
2207 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2211 up_read(&mm->mmap_sem);
2214 for (i = 0; i < nr_files; i++) {
2215 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2218 p = flex_array_get(fa, i);
2219 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2220 if (!proc_fill_cache(file, ctx,
2222 proc_map_files_instantiate,
2224 (void *)(unsigned long)p->mode))
2229 flex_array_free(fa);
2232 put_task_struct(task);
2237 static const struct file_operations proc_map_files_operations = {
2238 .read = generic_read_dir,
2239 .iterate_shared = proc_map_files_readdir,
2240 .llseek = generic_file_llseek,
2243 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2244 struct timers_private {
2246 struct task_struct *task;
2247 struct sighand_struct *sighand;
2248 struct pid_namespace *ns;
2249 unsigned long flags;
2252 static void *timers_start(struct seq_file *m, loff_t *pos)
2254 struct timers_private *tp = m->private;
2256 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2258 return ERR_PTR(-ESRCH);
2260 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2262 return ERR_PTR(-ESRCH);
2264 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2267 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2269 struct timers_private *tp = m->private;
2270 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2273 static void timers_stop(struct seq_file *m, void *v)
2275 struct timers_private *tp = m->private;
2278 unlock_task_sighand(tp->task, &tp->flags);
2283 put_task_struct(tp->task);
2288 static int show_timer(struct seq_file *m, void *v)
2290 struct k_itimer *timer;
2291 struct timers_private *tp = m->private;
2293 static const char * const nstr[] = {
2294 [SIGEV_SIGNAL] = "signal",
2295 [SIGEV_NONE] = "none",
2296 [SIGEV_THREAD] = "thread",
2299 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2300 notify = timer->it_sigev_notify;
2302 seq_printf(m, "ID: %d\n", timer->it_id);
2303 seq_printf(m, "signal: %d/%px\n",
2304 timer->sigq->info.si_signo,
2305 timer->sigq->info.si_value.sival_ptr);
2306 seq_printf(m, "notify: %s/%s.%d\n",
2307 nstr[notify & ~SIGEV_THREAD_ID],
2308 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2309 pid_nr_ns(timer->it_pid, tp->ns));
2310 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2315 static const struct seq_operations proc_timers_seq_ops = {
2316 .start = timers_start,
2317 .next = timers_next,
2318 .stop = timers_stop,
2322 static int proc_timers_open(struct inode *inode, struct file *file)
2324 struct timers_private *tp;
2326 tp = __seq_open_private(file, &proc_timers_seq_ops,
2327 sizeof(struct timers_private));
2331 tp->pid = proc_pid(inode);
2332 tp->ns = proc_pid_ns(inode);
2336 static const struct file_operations proc_timers_operations = {
2337 .open = proc_timers_open,
2339 .llseek = seq_lseek,
2340 .release = seq_release_private,
2344 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2345 size_t count, loff_t *offset)
2347 struct inode *inode = file_inode(file);
2348 struct task_struct *p;
2352 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2356 p = get_proc_task(inode);
2362 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2369 err = security_task_setscheduler(p);
2378 p->timer_slack_ns = p->default_timer_slack_ns;
2380 p->timer_slack_ns = slack_ns;
2389 static int timerslack_ns_show(struct seq_file *m, void *v)
2391 struct inode *inode = m->private;
2392 struct task_struct *p;
2395 p = get_proc_task(inode);
2401 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2408 err = security_task_getscheduler(p);
2414 seq_printf(m, "%llu\n", p->timer_slack_ns);
2423 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2425 return single_open(filp, timerslack_ns_show, inode);
2428 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2429 .open = timerslack_ns_open,
2431 .write = timerslack_ns_write,
2432 .llseek = seq_lseek,
2433 .release = single_release,
2436 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2437 struct task_struct *task, const void *ptr)
2439 const struct pid_entry *p = ptr;
2440 struct inode *inode;
2441 struct proc_inode *ei;
2443 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2445 return ERR_PTR(-ENOENT);
2448 if (S_ISDIR(inode->i_mode))
2449 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2451 inode->i_op = p->iop;
2453 inode->i_fop = p->fop;
2455 pid_update_inode(task, inode);
2456 d_set_d_op(dentry, &pid_dentry_operations);
2457 return d_splice_alias(inode, dentry);
2460 static struct dentry *proc_pident_lookup(struct inode *dir,
2461 struct dentry *dentry,
2462 const struct pid_entry *ents,
2465 struct task_struct *task = get_proc_task(dir);
2466 const struct pid_entry *p, *last;
2467 struct dentry *res = ERR_PTR(-ENOENT);
2473 * Yes, it does not scale. And it should not. Don't add
2474 * new entries into /proc/<tgid>/ without very good reasons.
2476 last = &ents[nents];
2477 for (p = ents; p < last; p++) {
2478 if (p->len != dentry->d_name.len)
2480 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2481 res = proc_pident_instantiate(dentry, task, p);
2485 put_task_struct(task);
2490 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2491 const struct pid_entry *ents, unsigned int nents)
2493 struct task_struct *task = get_proc_task(file_inode(file));
2494 const struct pid_entry *p;
2499 if (!dir_emit_dots(file, ctx))
2502 if (ctx->pos >= nents + 2)
2505 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2506 if (!proc_fill_cache(file, ctx, p->name, p->len,
2507 proc_pident_instantiate, task, p))
2512 put_task_struct(task);
2516 #ifdef CONFIG_SECURITY
2517 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2518 size_t count, loff_t *ppos)
2520 struct inode * inode = file_inode(file);
2523 struct task_struct *task = get_proc_task(inode);
2528 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2529 (char*)file->f_path.dentry->d_name.name,
2531 put_task_struct(task);
2533 length = simple_read_from_buffer(buf, count, ppos, p, length);
2538 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2539 size_t count, loff_t *ppos)
2541 struct inode * inode = file_inode(file);
2542 struct task_struct *task;
2547 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2552 /* A task may only write its own attributes. */
2553 if (current != task) {
2559 if (count > PAGE_SIZE)
2562 /* No partial writes. */
2566 page = memdup_user(buf, count);
2572 /* Guard against adverse ptrace interaction */
2573 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2577 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2578 file->f_path.dentry->d_name.name, page,
2580 mutex_unlock(¤t->signal->cred_guard_mutex);
2587 static const struct file_operations proc_pid_attr_operations = {
2588 .read = proc_pid_attr_read,
2589 .write = proc_pid_attr_write,
2590 .llseek = generic_file_llseek,
2593 #define LSM_DIR_OPS(LSM) \
2594 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2595 struct dir_context *ctx) \
2597 return proc_pident_readdir(filp, ctx, \
2598 LSM##_attr_dir_stuff, \
2599 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2602 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2603 .read = generic_read_dir, \
2604 .iterate = proc_##LSM##_attr_dir_iterate, \
2605 .llseek = default_llseek, \
2608 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2609 struct dentry *dentry, unsigned int flags) \
2611 return proc_pident_lookup(dir, dentry, \
2612 LSM##_attr_dir_stuff, \
2613 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2616 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2617 .lookup = proc_##LSM##_attr_dir_lookup, \
2618 .getattr = pid_getattr, \
2619 .setattr = proc_setattr, \
2622 #ifdef CONFIG_SECURITY_SMACK
2623 static const struct pid_entry smack_attr_dir_stuff[] = {
2624 ATTR("smack", "current", 0666),
2629 static const struct pid_entry attr_dir_stuff[] = {
2630 ATTR(NULL, "current", 0666),
2631 ATTR(NULL, "prev", 0444),
2632 ATTR(NULL, "exec", 0666),
2633 ATTR(NULL, "fscreate", 0666),
2634 ATTR(NULL, "keycreate", 0666),
2635 ATTR(NULL, "sockcreate", 0666),
2636 #ifdef CONFIG_SECURITY_SMACK
2638 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2642 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2644 return proc_pident_readdir(file, ctx,
2645 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2648 static const struct file_operations proc_attr_dir_operations = {
2649 .read = generic_read_dir,
2650 .iterate_shared = proc_attr_dir_readdir,
2651 .llseek = generic_file_llseek,
2654 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2655 struct dentry *dentry, unsigned int flags)
2657 return proc_pident_lookup(dir, dentry,
2658 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2661 static const struct inode_operations proc_attr_dir_inode_operations = {
2662 .lookup = proc_attr_dir_lookup,
2663 .getattr = pid_getattr,
2664 .setattr = proc_setattr,
2669 #ifdef CONFIG_ELF_CORE
2670 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2671 size_t count, loff_t *ppos)
2673 struct task_struct *task = get_proc_task(file_inode(file));
2674 struct mm_struct *mm;
2675 char buffer[PROC_NUMBUF];
2683 mm = get_task_mm(task);
2685 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2686 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2687 MMF_DUMP_FILTER_SHIFT));
2689 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2692 put_task_struct(task);
2697 static ssize_t proc_coredump_filter_write(struct file *file,
2698 const char __user *buf,
2702 struct task_struct *task;
2703 struct mm_struct *mm;
2709 ret = kstrtouint_from_user(buf, count, 0, &val);
2714 task = get_proc_task(file_inode(file));
2718 mm = get_task_mm(task);
2723 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2725 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2727 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2732 put_task_struct(task);
2739 static const struct file_operations proc_coredump_filter_operations = {
2740 .read = proc_coredump_filter_read,
2741 .write = proc_coredump_filter_write,
2742 .llseek = generic_file_llseek,
2746 #ifdef CONFIG_TASK_IO_ACCOUNTING
2747 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2749 struct task_io_accounting acct = task->ioac;
2750 unsigned long flags;
2753 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2757 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2762 if (whole && lock_task_sighand(task, &flags)) {
2763 struct task_struct *t = task;
2765 task_io_accounting_add(&acct, &task->signal->ioac);
2766 while_each_thread(task, t)
2767 task_io_accounting_add(&acct, &t->ioac);
2769 unlock_task_sighand(task, &flags);
2776 "read_bytes: %llu\n"
2777 "write_bytes: %llu\n"
2778 "cancelled_write_bytes: %llu\n",
2779 (unsigned long long)acct.rchar,
2780 (unsigned long long)acct.wchar,
2781 (unsigned long long)acct.syscr,
2782 (unsigned long long)acct.syscw,
2783 (unsigned long long)acct.read_bytes,
2784 (unsigned long long)acct.write_bytes,
2785 (unsigned long long)acct.cancelled_write_bytes);
2789 mutex_unlock(&task->signal->cred_guard_mutex);
2793 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2794 struct pid *pid, struct task_struct *task)
2796 return do_io_accounting(task, m, 0);
2799 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2800 struct pid *pid, struct task_struct *task)
2802 return do_io_accounting(task, m, 1);
2804 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2806 #ifdef CONFIG_USER_NS
2807 static int proc_id_map_open(struct inode *inode, struct file *file,
2808 const struct seq_operations *seq_ops)
2810 struct user_namespace *ns = NULL;
2811 struct task_struct *task;
2812 struct seq_file *seq;
2815 task = get_proc_task(inode);
2818 ns = get_user_ns(task_cred_xxx(task, user_ns));
2820 put_task_struct(task);
2825 ret = seq_open(file, seq_ops);
2829 seq = file->private_data;
2839 static int proc_id_map_release(struct inode *inode, struct file *file)
2841 struct seq_file *seq = file->private_data;
2842 struct user_namespace *ns = seq->private;
2844 return seq_release(inode, file);
2847 static int proc_uid_map_open(struct inode *inode, struct file *file)
2849 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2852 static int proc_gid_map_open(struct inode *inode, struct file *file)
2854 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2857 static int proc_projid_map_open(struct inode *inode, struct file *file)
2859 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2862 static const struct file_operations proc_uid_map_operations = {
2863 .open = proc_uid_map_open,
2864 .write = proc_uid_map_write,
2866 .llseek = seq_lseek,
2867 .release = proc_id_map_release,
2870 static const struct file_operations proc_gid_map_operations = {
2871 .open = proc_gid_map_open,
2872 .write = proc_gid_map_write,
2874 .llseek = seq_lseek,
2875 .release = proc_id_map_release,
2878 static const struct file_operations proc_projid_map_operations = {
2879 .open = proc_projid_map_open,
2880 .write = proc_projid_map_write,
2882 .llseek = seq_lseek,
2883 .release = proc_id_map_release,
2886 static int proc_setgroups_open(struct inode *inode, struct file *file)
2888 struct user_namespace *ns = NULL;
2889 struct task_struct *task;
2893 task = get_proc_task(inode);
2896 ns = get_user_ns(task_cred_xxx(task, user_ns));
2898 put_task_struct(task);
2903 if (file->f_mode & FMODE_WRITE) {
2905 if (!ns_capable(ns, CAP_SYS_ADMIN))
2909 ret = single_open(file, &proc_setgroups_show, ns);
2920 static int proc_setgroups_release(struct inode *inode, struct file *file)
2922 struct seq_file *seq = file->private_data;
2923 struct user_namespace *ns = seq->private;
2924 int ret = single_release(inode, file);
2929 static const struct file_operations proc_setgroups_operations = {
2930 .open = proc_setgroups_open,
2931 .write = proc_setgroups_write,
2933 .llseek = seq_lseek,
2934 .release = proc_setgroups_release,
2936 #endif /* CONFIG_USER_NS */
2938 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2939 struct pid *pid, struct task_struct *task)
2941 int err = lock_trace(task);
2943 seq_printf(m, "%08x\n", task->personality);
2949 #ifdef CONFIG_LIVEPATCH
2950 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2951 struct pid *pid, struct task_struct *task)
2953 seq_printf(m, "%d\n", task->patch_state);
2956 #endif /* CONFIG_LIVEPATCH */
2958 #ifdef CONFIG_STACKLEAK_METRICS
2959 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2960 struct pid *pid, struct task_struct *task)
2962 unsigned long prev_depth = THREAD_SIZE -
2963 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2964 unsigned long depth = THREAD_SIZE -
2965 (task->lowest_stack & (THREAD_SIZE - 1));
2967 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2971 #endif /* CONFIG_STACKLEAK_METRICS */
2976 static const struct file_operations proc_task_operations;
2977 static const struct inode_operations proc_task_inode_operations;
2979 static const struct pid_entry tgid_base_stuff[] = {
2980 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2981 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2982 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2983 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2984 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2986 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2988 REG("environ", S_IRUSR, proc_environ_operations),
2989 REG("auxv", S_IRUSR, proc_auxv_operations),
2990 ONE("status", S_IRUGO, proc_pid_status),
2991 ONE("personality", S_IRUSR, proc_pid_personality),
2992 ONE("limits", S_IRUGO, proc_pid_limits),
2993 #ifdef CONFIG_SCHED_DEBUG
2994 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2996 #ifdef CONFIG_SCHED_AUTOGROUP
2997 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2999 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3000 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3001 ONE("syscall", S_IRUSR, proc_pid_syscall),
3003 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3004 ONE("stat", S_IRUGO, proc_tgid_stat),
3005 ONE("statm", S_IRUGO, proc_pid_statm),
3006 REG("maps", S_IRUGO, proc_pid_maps_operations),
3008 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3010 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3011 LNK("cwd", proc_cwd_link),
3012 LNK("root", proc_root_link),
3013 LNK("exe", proc_exe_link),
3014 REG("mounts", S_IRUGO, proc_mounts_operations),
3015 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3016 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3017 #ifdef CONFIG_PROC_PAGE_MONITOR
3018 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3019 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3020 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3021 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3023 #ifdef CONFIG_SECURITY
3024 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3026 #ifdef CONFIG_KALLSYMS
3027 ONE("wchan", S_IRUGO, proc_pid_wchan),
3029 #ifdef CONFIG_STACKTRACE
3030 ONE("stack", S_IRUSR, proc_pid_stack),
3032 #ifdef CONFIG_SCHED_INFO
3033 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3035 #ifdef CONFIG_LATENCYTOP
3036 REG("latency", S_IRUGO, proc_lstats_operations),
3038 #ifdef CONFIG_PROC_PID_CPUSET
3039 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3041 #ifdef CONFIG_CGROUPS
3042 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3044 ONE("oom_score", S_IRUGO, proc_oom_score),
3045 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3046 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3048 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3049 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3051 #ifdef CONFIG_FAULT_INJECTION
3052 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3053 REG("fail-nth", 0644, proc_fail_nth_operations),
3055 #ifdef CONFIG_ELF_CORE
3056 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3058 #ifdef CONFIG_TASK_IO_ACCOUNTING
3059 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3061 #ifdef CONFIG_USER_NS
3062 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3063 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3064 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3065 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3067 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3068 REG("timers", S_IRUGO, proc_timers_operations),
3070 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3071 #ifdef CONFIG_LIVEPATCH
3072 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3074 #ifdef CONFIG_STACKLEAK_METRICS
3075 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3079 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3081 return proc_pident_readdir(file, ctx,
3082 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3085 static const struct file_operations proc_tgid_base_operations = {
3086 .read = generic_read_dir,
3087 .iterate_shared = proc_tgid_base_readdir,
3088 .llseek = generic_file_llseek,
3091 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3093 return proc_pident_lookup(dir, dentry,
3094 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3097 static const struct inode_operations proc_tgid_base_inode_operations = {
3098 .lookup = proc_tgid_base_lookup,
3099 .getattr = pid_getattr,
3100 .setattr = proc_setattr,
3101 .permission = proc_pid_permission,
3104 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3106 struct dentry *dentry, *leader, *dir;
3111 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3112 /* no ->d_hash() rejects on procfs */
3113 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3115 d_invalidate(dentry);
3123 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3124 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3129 name.len = strlen(name.name);
3130 dir = d_hash_and_lookup(leader, &name);
3132 goto out_put_leader;
3135 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3136 dentry = d_hash_and_lookup(dir, &name);
3138 d_invalidate(dentry);
3150 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3151 * @task: task that should be flushed.
3153 * When flushing dentries from proc, one needs to flush them from global
3154 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3155 * in. This call is supposed to do all of this job.
3157 * Looks in the dcache for
3159 * /proc/@tgid/task/@pid
3160 * if either directory is present flushes it and all of it'ts children
3163 * It is safe and reasonable to cache /proc entries for a task until
3164 * that task exits. After that they just clog up the dcache with
3165 * useless entries, possibly causing useful dcache entries to be
3166 * flushed instead. This routine is proved to flush those useless
3167 * dcache entries at process exit time.
3169 * NOTE: This routine is just an optimization so it does not guarantee
3170 * that no dcache entries will exist at process exit time it
3171 * just makes it very unlikely that any will persist.
3174 void proc_flush_task(struct task_struct *task)
3177 struct pid *pid, *tgid;
3180 pid = task_pid(task);
3181 tgid = task_tgid(task);
3183 for (i = 0; i <= pid->level; i++) {
3184 upid = &pid->numbers[i];
3185 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3186 tgid->numbers[i].nr);
3190 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3191 struct task_struct *task, const void *ptr)
3193 struct inode *inode;
3195 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3197 return ERR_PTR(-ENOENT);
3199 inode->i_op = &proc_tgid_base_inode_operations;
3200 inode->i_fop = &proc_tgid_base_operations;
3201 inode->i_flags|=S_IMMUTABLE;
3203 set_nlink(inode, nlink_tgid);
3204 pid_update_inode(task, inode);
3206 d_set_d_op(dentry, &pid_dentry_operations);
3207 return d_splice_alias(inode, dentry);
3210 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3212 struct task_struct *task;
3214 struct pid_namespace *ns;
3215 struct dentry *result = ERR_PTR(-ENOENT);
3217 tgid = name_to_int(&dentry->d_name);
3221 ns = dentry->d_sb->s_fs_info;
3223 task = find_task_by_pid_ns(tgid, ns);
3225 get_task_struct(task);
3230 result = proc_pid_instantiate(dentry, task, NULL);
3231 put_task_struct(task);
3237 * Find the first task with tgid >= tgid
3242 struct task_struct *task;
3244 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3249 put_task_struct(iter.task);
3253 pid = find_ge_pid(iter.tgid, ns);
3255 iter.tgid = pid_nr_ns(pid, ns);
3256 iter.task = pid_task(pid, PIDTYPE_PID);
3257 /* What we to know is if the pid we have find is the
3258 * pid of a thread_group_leader. Testing for task
3259 * being a thread_group_leader is the obvious thing
3260 * todo but there is a window when it fails, due to
3261 * the pid transfer logic in de_thread.
3263 * So we perform the straight forward test of seeing
3264 * if the pid we have found is the pid of a thread
3265 * group leader, and don't worry if the task we have
3266 * found doesn't happen to be a thread group leader.
3267 * As we don't care in the case of readdir.
3269 if (!iter.task || !has_group_leader_pid(iter.task)) {
3273 get_task_struct(iter.task);
3279 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3281 /* for the /proc/ directory itself, after non-process stuff has been done */
3282 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3284 struct tgid_iter iter;
3285 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3286 loff_t pos = ctx->pos;
3288 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3291 if (pos == TGID_OFFSET - 2) {
3292 struct inode *inode = d_inode(ns->proc_self);
3293 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3295 ctx->pos = pos = pos + 1;
3297 if (pos == TGID_OFFSET - 1) {
3298 struct inode *inode = d_inode(ns->proc_thread_self);
3299 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3301 ctx->pos = pos = pos + 1;
3303 iter.tgid = pos - TGID_OFFSET;
3305 for (iter = next_tgid(ns, iter);
3307 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3312 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3315 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3316 ctx->pos = iter.tgid + TGID_OFFSET;
3317 if (!proc_fill_cache(file, ctx, name, len,
3318 proc_pid_instantiate, iter.task, NULL)) {
3319 put_task_struct(iter.task);
3323 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3328 * proc_tid_comm_permission is a special permission function exclusively
3329 * used for the node /proc/<pid>/task/<tid>/comm.
3330 * It bypasses generic permission checks in the case where a task of the same
3331 * task group attempts to access the node.
3332 * The rationale behind this is that glibc and bionic access this node for
3333 * cross thread naming (pthread_set/getname_np(!self)). However, if
3334 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3335 * which locks out the cross thread naming implementation.
3336 * This function makes sure that the node is always accessible for members of
3337 * same thread group.
3339 static int proc_tid_comm_permission(struct inode *inode, int mask)
3341 bool is_same_tgroup;
3342 struct task_struct *task;
3344 task = get_proc_task(inode);
3347 is_same_tgroup = same_thread_group(current, task);
3348 put_task_struct(task);
3350 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3351 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3352 * read or written by the members of the corresponding
3358 return generic_permission(inode, mask);
3361 static const struct inode_operations proc_tid_comm_inode_operations = {
3362 .permission = proc_tid_comm_permission,
3368 static const struct pid_entry tid_base_stuff[] = {
3369 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3370 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3371 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3373 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3375 REG("environ", S_IRUSR, proc_environ_operations),
3376 REG("auxv", S_IRUSR, proc_auxv_operations),
3377 ONE("status", S_IRUGO, proc_pid_status),
3378 ONE("personality", S_IRUSR, proc_pid_personality),
3379 ONE("limits", S_IRUGO, proc_pid_limits),
3380 #ifdef CONFIG_SCHED_DEBUG
3381 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3383 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3384 &proc_tid_comm_inode_operations,
3385 &proc_pid_set_comm_operations, {}),
3386 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3387 ONE("syscall", S_IRUSR, proc_pid_syscall),
3389 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3390 ONE("stat", S_IRUGO, proc_tid_stat),
3391 ONE("statm", S_IRUGO, proc_pid_statm),
3392 REG("maps", S_IRUGO, proc_pid_maps_operations),
3393 #ifdef CONFIG_PROC_CHILDREN
3394 REG("children", S_IRUGO, proc_tid_children_operations),
3397 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3399 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3400 LNK("cwd", proc_cwd_link),
3401 LNK("root", proc_root_link),
3402 LNK("exe", proc_exe_link),
3403 REG("mounts", S_IRUGO, proc_mounts_operations),
3404 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3405 #ifdef CONFIG_PROC_PAGE_MONITOR
3406 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3407 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3408 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3409 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3411 #ifdef CONFIG_SECURITY
3412 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3414 #ifdef CONFIG_KALLSYMS
3415 ONE("wchan", S_IRUGO, proc_pid_wchan),
3417 #ifdef CONFIG_STACKTRACE
3418 ONE("stack", S_IRUSR, proc_pid_stack),
3420 #ifdef CONFIG_SCHED_INFO
3421 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3423 #ifdef CONFIG_LATENCYTOP
3424 REG("latency", S_IRUGO, proc_lstats_operations),
3426 #ifdef CONFIG_PROC_PID_CPUSET
3427 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3429 #ifdef CONFIG_CGROUPS
3430 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3432 ONE("oom_score", S_IRUGO, proc_oom_score),
3433 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3434 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3436 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3437 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3439 #ifdef CONFIG_FAULT_INJECTION
3440 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3441 REG("fail-nth", 0644, proc_fail_nth_operations),
3443 #ifdef CONFIG_TASK_IO_ACCOUNTING
3444 ONE("io", S_IRUSR, proc_tid_io_accounting),
3446 #ifdef CONFIG_USER_NS
3447 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3448 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3449 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3450 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3452 #ifdef CONFIG_LIVEPATCH
3453 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3457 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3459 return proc_pident_readdir(file, ctx,
3460 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3463 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3465 return proc_pident_lookup(dir, dentry,
3466 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3469 static const struct file_operations proc_tid_base_operations = {
3470 .read = generic_read_dir,
3471 .iterate_shared = proc_tid_base_readdir,
3472 .llseek = generic_file_llseek,
3475 static const struct inode_operations proc_tid_base_inode_operations = {
3476 .lookup = proc_tid_base_lookup,
3477 .getattr = pid_getattr,
3478 .setattr = proc_setattr,
3481 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3482 struct task_struct *task, const void *ptr)
3484 struct inode *inode;
3485 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3487 return ERR_PTR(-ENOENT);
3489 inode->i_op = &proc_tid_base_inode_operations;
3490 inode->i_fop = &proc_tid_base_operations;
3491 inode->i_flags |= S_IMMUTABLE;
3493 set_nlink(inode, nlink_tid);
3494 pid_update_inode(task, inode);
3496 d_set_d_op(dentry, &pid_dentry_operations);
3497 return d_splice_alias(inode, dentry);
3500 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3502 struct task_struct *task;
3503 struct task_struct *leader = get_proc_task(dir);
3505 struct pid_namespace *ns;
3506 struct dentry *result = ERR_PTR(-ENOENT);
3511 tid = name_to_int(&dentry->d_name);
3515 ns = dentry->d_sb->s_fs_info;
3517 task = find_task_by_pid_ns(tid, ns);
3519 get_task_struct(task);
3523 if (!same_thread_group(leader, task))
3526 result = proc_task_instantiate(dentry, task, NULL);
3528 put_task_struct(task);
3530 put_task_struct(leader);
3536 * Find the first tid of a thread group to return to user space.
3538 * Usually this is just the thread group leader, but if the users
3539 * buffer was too small or there was a seek into the middle of the
3540 * directory we have more work todo.
3542 * In the case of a short read we start with find_task_by_pid.
3544 * In the case of a seek we start with the leader and walk nr
3547 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3548 struct pid_namespace *ns)
3550 struct task_struct *pos, *task;
3551 unsigned long nr = f_pos;
3553 if (nr != f_pos) /* 32bit overflow? */
3557 task = pid_task(pid, PIDTYPE_PID);
3561 /* Attempt to start with the tid of a thread */
3563 pos = find_task_by_pid_ns(tid, ns);
3564 if (pos && same_thread_group(pos, task))
3568 /* If nr exceeds the number of threads there is nothing todo */
3569 if (nr >= get_nr_threads(task))
3572 /* If we haven't found our starting place yet start
3573 * with the leader and walk nr threads forward.
3575 pos = task = task->group_leader;
3579 } while_each_thread(task, pos);
3584 get_task_struct(pos);
3591 * Find the next thread in the thread list.
3592 * Return NULL if there is an error or no next thread.
3594 * The reference to the input task_struct is released.
3596 static struct task_struct *next_tid(struct task_struct *start)
3598 struct task_struct *pos = NULL;
3600 if (pid_alive(start)) {
3601 pos = next_thread(start);
3602 if (thread_group_leader(pos))
3605 get_task_struct(pos);
3608 put_task_struct(start);
3612 /* for the /proc/TGID/task/ directories */
3613 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3615 struct inode *inode = file_inode(file);
3616 struct task_struct *task;
3617 struct pid_namespace *ns;
3620 if (proc_inode_is_dead(inode))
3623 if (!dir_emit_dots(file, ctx))
3626 /* f_version caches the tgid value that the last readdir call couldn't
3627 * return. lseek aka telldir automagically resets f_version to 0.
3629 ns = proc_pid_ns(inode);
3630 tid = (int)file->f_version;
3631 file->f_version = 0;
3632 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3634 task = next_tid(task), ctx->pos++) {
3637 tid = task_pid_nr_ns(task, ns);
3638 len = snprintf(name, sizeof(name), "%u", tid);
3639 if (!proc_fill_cache(file, ctx, name, len,
3640 proc_task_instantiate, task, NULL)) {
3641 /* returning this tgid failed, save it as the first
3642 * pid for the next readir call */
3643 file->f_version = (u64)tid;
3644 put_task_struct(task);
3652 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3653 u32 request_mask, unsigned int query_flags)
3655 struct inode *inode = d_inode(path->dentry);
3656 struct task_struct *p = get_proc_task(inode);
3657 generic_fillattr(inode, stat);
3660 stat->nlink += get_nr_threads(p);
3667 static const struct inode_operations proc_task_inode_operations = {
3668 .lookup = proc_task_lookup,
3669 .getattr = proc_task_getattr,
3670 .setattr = proc_setattr,
3671 .permission = proc_pid_permission,
3674 static const struct file_operations proc_task_operations = {
3675 .read = generic_read_dir,
3676 .iterate_shared = proc_task_readdir,
3677 .llseek = generic_file_llseek,
3680 void __init set_proc_pid_nlink(void)
3682 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3683 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));