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/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/tracehook.h>
78 #include <linux/printk.h>
79 #include <linux/cache.h>
80 #include <linux/cgroup.h>
81 #include <linux/cpuset.h>
82 #include <linux/audit.h>
83 #include <linux/poll.h>
84 #include <linux/nsproxy.h>
85 #include <linux/oom.h>
86 #include <linux/elf.h>
87 #include <linux/pid_namespace.h>
88 #include <linux/user_namespace.h>
89 #include <linux/fs_struct.h>
90 #include <linux/slab.h>
91 #include <linux/sched/autogroup.h>
92 #include <linux/sched/mm.h>
93 #include <linux/sched/coredump.h>
94 #include <linux/sched/debug.h>
95 #include <linux/sched/stat.h>
96 #include <linux/posix-timers.h>
97 #include <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)
619 struct syscall_info info;
620 u64 *args = &info.data.args[0];
623 res = lock_trace(task);
627 if (task_current_syscall(task, &info))
628 seq_puts(m, "running\n");
629 else if (info.data.nr < 0)
630 seq_printf(m, "%d 0x%llx 0x%llx\n",
631 info.data.nr, info.sp, info.data.instruction_pointer);
634 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
636 args[0], args[1], args[2], args[3], args[4], args[5],
637 info.sp, info.data.instruction_pointer);
642 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
644 /************************************************************************/
645 /* Here the fs part begins */
646 /************************************************************************/
648 /* permission checks */
649 static int proc_fd_access_allowed(struct inode *inode)
651 struct task_struct *task;
653 /* Allow access to a task's file descriptors if it is us or we
654 * may use ptrace attach to the process and find out that
657 task = get_proc_task(inode);
659 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
660 put_task_struct(task);
665 int proc_setattr(struct dentry *dentry, struct iattr *attr)
668 struct inode *inode = d_inode(dentry);
670 if (attr->ia_valid & ATTR_MODE)
673 error = setattr_prepare(dentry, attr);
677 setattr_copy(inode, attr);
678 mark_inode_dirty(inode);
683 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
684 * or euid/egid (for hide_pid_min=2)?
686 static bool has_pid_permissions(struct pid_namespace *pid,
687 struct task_struct *task,
690 if (pid->hide_pid < hide_pid_min)
692 if (in_group_p(pid->pid_gid))
694 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
698 static int proc_pid_permission(struct inode *inode, int mask)
700 struct pid_namespace *pid = proc_pid_ns(inode);
701 struct task_struct *task;
704 task = get_proc_task(inode);
707 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
708 put_task_struct(task);
711 if (pid->hide_pid == HIDEPID_INVISIBLE) {
713 * Let's make getdents(), stat(), and open()
714 * consistent with each other. If a process
715 * may not stat() a file, it shouldn't be seen
723 return generic_permission(inode, mask);
728 static const struct inode_operations proc_def_inode_operations = {
729 .setattr = proc_setattr,
732 static int proc_single_show(struct seq_file *m, void *v)
734 struct inode *inode = m->private;
735 struct pid_namespace *ns = proc_pid_ns(inode);
736 struct pid *pid = proc_pid(inode);
737 struct task_struct *task;
740 task = get_pid_task(pid, PIDTYPE_PID);
744 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
746 put_task_struct(task);
750 static int proc_single_open(struct inode *inode, struct file *filp)
752 return single_open(filp, proc_single_show, inode);
755 static const struct file_operations proc_single_file_operations = {
756 .open = proc_single_open,
759 .release = single_release,
763 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
765 struct task_struct *task = get_proc_task(inode);
766 struct mm_struct *mm = ERR_PTR(-ESRCH);
769 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
770 put_task_struct(task);
772 if (!IS_ERR_OR_NULL(mm)) {
773 /* ensure this mm_struct can't be freed */
775 /* but do not pin its memory */
783 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
785 struct mm_struct *mm = proc_mem_open(inode, mode);
790 file->private_data = mm;
794 static int mem_open(struct inode *inode, struct file *file)
796 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
798 /* OK to pass negative loff_t, we can catch out-of-range */
799 file->f_mode |= FMODE_UNSIGNED_OFFSET;
804 static ssize_t mem_rw(struct file *file, char __user *buf,
805 size_t count, loff_t *ppos, int write)
807 struct mm_struct *mm = file->private_data;
808 unsigned long addr = *ppos;
816 page = (char *)__get_free_page(GFP_KERNEL);
821 if (!mmget_not_zero(mm))
824 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
827 int this_len = min_t(int, count, PAGE_SIZE);
829 if (write && copy_from_user(page, buf, this_len)) {
834 this_len = access_remote_vm(mm, addr, page, this_len, flags);
841 if (!write && copy_to_user(buf, page, this_len)) {
855 free_page((unsigned long) page);
859 static ssize_t mem_read(struct file *file, char __user *buf,
860 size_t count, loff_t *ppos)
862 return mem_rw(file, buf, count, ppos, 0);
865 static ssize_t mem_write(struct file *file, const char __user *buf,
866 size_t count, loff_t *ppos)
868 return mem_rw(file, (char __user*)buf, count, ppos, 1);
871 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
875 file->f_pos = offset;
878 file->f_pos += offset;
883 force_successful_syscall_return();
887 static int mem_release(struct inode *inode, struct file *file)
889 struct mm_struct *mm = file->private_data;
895 static const struct file_operations proc_mem_operations = {
900 .release = mem_release,
903 static int environ_open(struct inode *inode, struct file *file)
905 return __mem_open(inode, file, PTRACE_MODE_READ);
908 static ssize_t environ_read(struct file *file, char __user *buf,
909 size_t count, loff_t *ppos)
912 unsigned long src = *ppos;
914 struct mm_struct *mm = file->private_data;
915 unsigned long env_start, env_end;
917 /* Ensure the process spawned far enough to have an environment. */
918 if (!mm || !mm->env_end)
921 page = (char *)__get_free_page(GFP_KERNEL);
926 if (!mmget_not_zero(mm))
929 spin_lock(&mm->arg_lock);
930 env_start = mm->env_start;
931 env_end = mm->env_end;
932 spin_unlock(&mm->arg_lock);
935 size_t this_len, max_len;
938 if (src >= (env_end - env_start))
941 this_len = env_end - (env_start + src);
943 max_len = min_t(size_t, PAGE_SIZE, count);
944 this_len = min(max_len, this_len);
946 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
953 if (copy_to_user(buf, page, retval)) {
967 free_page((unsigned long) page);
971 static const struct file_operations proc_environ_operations = {
972 .open = environ_open,
973 .read = environ_read,
974 .llseek = generic_file_llseek,
975 .release = mem_release,
978 static int auxv_open(struct inode *inode, struct file *file)
980 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
983 static ssize_t auxv_read(struct file *file, char __user *buf,
984 size_t count, loff_t *ppos)
986 struct mm_struct *mm = file->private_data;
987 unsigned int nwords = 0;
993 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
994 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
995 nwords * sizeof(mm->saved_auxv[0]));
998 static const struct file_operations proc_auxv_operations = {
1001 .llseek = generic_file_llseek,
1002 .release = mem_release,
1005 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1008 struct task_struct *task = get_proc_task(file_inode(file));
1009 char buffer[PROC_NUMBUF];
1010 int oom_adj = OOM_ADJUST_MIN;
1015 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1016 oom_adj = OOM_ADJUST_MAX;
1018 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1020 put_task_struct(task);
1021 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1022 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1025 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1027 static DEFINE_MUTEX(oom_adj_mutex);
1028 struct mm_struct *mm = NULL;
1029 struct task_struct *task;
1032 task = get_proc_task(file_inode(file));
1036 mutex_lock(&oom_adj_mutex);
1038 if (oom_adj < task->signal->oom_score_adj &&
1039 !capable(CAP_SYS_RESOURCE)) {
1044 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1045 * /proc/pid/oom_score_adj instead.
1047 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1048 current->comm, task_pid_nr(current), task_pid_nr(task),
1051 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1052 !capable(CAP_SYS_RESOURCE)) {
1059 * Make sure we will check other processes sharing the mm if this is
1060 * not vfrok which wants its own oom_score_adj.
1061 * pin the mm so it doesn't go away and get reused after task_unlock
1063 if (!task->vfork_done) {
1064 struct task_struct *p = find_lock_task_mm(task);
1067 if (atomic_read(&p->mm->mm_users) > 1) {
1075 task->signal->oom_score_adj = oom_adj;
1076 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1077 task->signal->oom_score_adj_min = (short)oom_adj;
1078 trace_oom_score_adj_update(task);
1081 struct task_struct *p;
1084 for_each_process(p) {
1085 if (same_thread_group(task, p))
1088 /* do not touch kernel threads or the global init */
1089 if (p->flags & PF_KTHREAD || is_global_init(p))
1093 if (!p->vfork_done && process_shares_mm(p, mm)) {
1094 p->signal->oom_score_adj = oom_adj;
1095 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1096 p->signal->oom_score_adj_min = (short)oom_adj;
1104 mutex_unlock(&oom_adj_mutex);
1105 put_task_struct(task);
1110 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1111 * kernels. The effective policy is defined by oom_score_adj, which has a
1112 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1113 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1114 * Processes that become oom disabled via oom_adj will still be oom disabled
1115 * with this implementation.
1117 * oom_adj cannot be removed since existing userspace binaries use it.
1119 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1120 size_t count, loff_t *ppos)
1122 char buffer[PROC_NUMBUF];
1126 memset(buffer, 0, sizeof(buffer));
1127 if (count > sizeof(buffer) - 1)
1128 count = sizeof(buffer) - 1;
1129 if (copy_from_user(buffer, buf, count)) {
1134 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1137 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1138 oom_adj != OOM_DISABLE) {
1144 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1145 * value is always attainable.
1147 if (oom_adj == OOM_ADJUST_MAX)
1148 oom_adj = OOM_SCORE_ADJ_MAX;
1150 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1152 err = __set_oom_adj(file, oom_adj, true);
1154 return err < 0 ? err : count;
1157 static const struct file_operations proc_oom_adj_operations = {
1158 .read = oom_adj_read,
1159 .write = oom_adj_write,
1160 .llseek = generic_file_llseek,
1163 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1164 size_t count, loff_t *ppos)
1166 struct task_struct *task = get_proc_task(file_inode(file));
1167 char buffer[PROC_NUMBUF];
1168 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1173 oom_score_adj = task->signal->oom_score_adj;
1174 put_task_struct(task);
1175 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1176 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1179 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1180 size_t count, loff_t *ppos)
1182 char buffer[PROC_NUMBUF];
1186 memset(buffer, 0, sizeof(buffer));
1187 if (count > sizeof(buffer) - 1)
1188 count = sizeof(buffer) - 1;
1189 if (copy_from_user(buffer, buf, count)) {
1194 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1197 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1198 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1203 err = __set_oom_adj(file, oom_score_adj, false);
1205 return err < 0 ? err : count;
1208 static const struct file_operations proc_oom_score_adj_operations = {
1209 .read = oom_score_adj_read,
1210 .write = oom_score_adj_write,
1211 .llseek = default_llseek,
1215 #define TMPBUFLEN 11
1216 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1217 size_t count, loff_t *ppos)
1219 struct inode * inode = file_inode(file);
1220 struct task_struct *task = get_proc_task(inode);
1222 char tmpbuf[TMPBUFLEN];
1226 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1227 from_kuid(file->f_cred->user_ns,
1228 audit_get_loginuid(task)));
1229 put_task_struct(task);
1230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1233 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1234 size_t count, loff_t *ppos)
1236 struct inode * inode = file_inode(file);
1242 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1249 /* No partial writes. */
1253 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1257 /* is userspace tring to explicitly UNSET the loginuid? */
1258 if (loginuid == AUDIT_UID_UNSET) {
1259 kloginuid = INVALID_UID;
1261 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1262 if (!uid_valid(kloginuid))
1266 rv = audit_set_loginuid(kloginuid);
1272 static const struct file_operations proc_loginuid_operations = {
1273 .read = proc_loginuid_read,
1274 .write = proc_loginuid_write,
1275 .llseek = generic_file_llseek,
1278 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1279 size_t count, loff_t *ppos)
1281 struct inode * inode = file_inode(file);
1282 struct task_struct *task = get_proc_task(inode);
1284 char tmpbuf[TMPBUFLEN];
1288 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1289 audit_get_sessionid(task));
1290 put_task_struct(task);
1291 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1294 static const struct file_operations proc_sessionid_operations = {
1295 .read = proc_sessionid_read,
1296 .llseek = generic_file_llseek,
1300 #ifdef CONFIG_FAULT_INJECTION
1301 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1302 size_t count, loff_t *ppos)
1304 struct task_struct *task = get_proc_task(file_inode(file));
1305 char buffer[PROC_NUMBUF];
1311 make_it_fail = task->make_it_fail;
1312 put_task_struct(task);
1314 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1316 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1319 static ssize_t proc_fault_inject_write(struct file * file,
1320 const char __user * buf, size_t count, loff_t *ppos)
1322 struct task_struct *task;
1323 char buffer[PROC_NUMBUF];
1327 if (!capable(CAP_SYS_RESOURCE))
1329 memset(buffer, 0, sizeof(buffer));
1330 if (count > sizeof(buffer) - 1)
1331 count = sizeof(buffer) - 1;
1332 if (copy_from_user(buffer, buf, count))
1334 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1337 if (make_it_fail < 0 || make_it_fail > 1)
1340 task = get_proc_task(file_inode(file));
1343 task->make_it_fail = make_it_fail;
1344 put_task_struct(task);
1349 static const struct file_operations proc_fault_inject_operations = {
1350 .read = proc_fault_inject_read,
1351 .write = proc_fault_inject_write,
1352 .llseek = generic_file_llseek,
1355 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1356 size_t count, loff_t *ppos)
1358 struct task_struct *task;
1362 err = kstrtouint_from_user(buf, count, 0, &n);
1366 task = get_proc_task(file_inode(file));
1370 put_task_struct(task);
1375 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1376 size_t count, loff_t *ppos)
1378 struct task_struct *task;
1379 char numbuf[PROC_NUMBUF];
1382 task = get_proc_task(file_inode(file));
1385 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1386 put_task_struct(task);
1387 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1390 static const struct file_operations proc_fail_nth_operations = {
1391 .read = proc_fail_nth_read,
1392 .write = proc_fail_nth_write,
1397 #ifdef CONFIG_SCHED_DEBUG
1399 * Print out various scheduling related per-task fields:
1401 static int sched_show(struct seq_file *m, void *v)
1403 struct inode *inode = m->private;
1404 struct pid_namespace *ns = proc_pid_ns(inode);
1405 struct task_struct *p;
1407 p = get_proc_task(inode);
1410 proc_sched_show_task(p, ns, m);
1418 sched_write(struct file *file, const char __user *buf,
1419 size_t count, loff_t *offset)
1421 struct inode *inode = file_inode(file);
1422 struct task_struct *p;
1424 p = get_proc_task(inode);
1427 proc_sched_set_task(p);
1434 static int sched_open(struct inode *inode, struct file *filp)
1436 return single_open(filp, sched_show, inode);
1439 static const struct file_operations proc_pid_sched_operations = {
1442 .write = sched_write,
1443 .llseek = seq_lseek,
1444 .release = single_release,
1449 #ifdef CONFIG_SCHED_AUTOGROUP
1451 * Print out autogroup related information:
1453 static int sched_autogroup_show(struct seq_file *m, void *v)
1455 struct inode *inode = m->private;
1456 struct task_struct *p;
1458 p = get_proc_task(inode);
1461 proc_sched_autogroup_show_task(p, m);
1469 sched_autogroup_write(struct file *file, const char __user *buf,
1470 size_t count, loff_t *offset)
1472 struct inode *inode = file_inode(file);
1473 struct task_struct *p;
1474 char buffer[PROC_NUMBUF];
1478 memset(buffer, 0, sizeof(buffer));
1479 if (count > sizeof(buffer) - 1)
1480 count = sizeof(buffer) - 1;
1481 if (copy_from_user(buffer, buf, count))
1484 err = kstrtoint(strstrip(buffer), 0, &nice);
1488 p = get_proc_task(inode);
1492 err = proc_sched_autogroup_set_nice(p, nice);
1501 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1505 ret = single_open(filp, sched_autogroup_show, NULL);
1507 struct seq_file *m = filp->private_data;
1514 static const struct file_operations proc_pid_sched_autogroup_operations = {
1515 .open = sched_autogroup_open,
1517 .write = sched_autogroup_write,
1518 .llseek = seq_lseek,
1519 .release = single_release,
1522 #endif /* CONFIG_SCHED_AUTOGROUP */
1524 static ssize_t comm_write(struct file *file, const char __user *buf,
1525 size_t count, loff_t *offset)
1527 struct inode *inode = file_inode(file);
1528 struct task_struct *p;
1529 char buffer[TASK_COMM_LEN];
1530 const size_t maxlen = sizeof(buffer) - 1;
1532 memset(buffer, 0, sizeof(buffer));
1533 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1536 p = get_proc_task(inode);
1540 if (same_thread_group(current, p))
1541 set_task_comm(p, buffer);
1550 static int comm_show(struct seq_file *m, void *v)
1552 struct inode *inode = m->private;
1553 struct task_struct *p;
1555 p = get_proc_task(inode);
1559 proc_task_name(m, p, false);
1567 static int comm_open(struct inode *inode, struct file *filp)
1569 return single_open(filp, comm_show, inode);
1572 static const struct file_operations proc_pid_set_comm_operations = {
1575 .write = comm_write,
1576 .llseek = seq_lseek,
1577 .release = single_release,
1580 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1582 struct task_struct *task;
1583 struct file *exe_file;
1585 task = get_proc_task(d_inode(dentry));
1588 exe_file = get_task_exe_file(task);
1589 put_task_struct(task);
1591 *exe_path = exe_file->f_path;
1592 path_get(&exe_file->f_path);
1599 static const char *proc_pid_get_link(struct dentry *dentry,
1600 struct inode *inode,
1601 struct delayed_call *done)
1604 int error = -EACCES;
1607 return ERR_PTR(-ECHILD);
1609 /* Are we allowed to snoop on the tasks file descriptors? */
1610 if (!proc_fd_access_allowed(inode))
1613 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1617 nd_jump_link(&path);
1620 return ERR_PTR(error);
1623 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1625 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1632 pathname = d_path(path, tmp, PAGE_SIZE);
1633 len = PTR_ERR(pathname);
1634 if (IS_ERR(pathname))
1636 len = tmp + PAGE_SIZE - 1 - pathname;
1640 if (copy_to_user(buffer, pathname, len))
1643 free_page((unsigned long)tmp);
1647 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1649 int error = -EACCES;
1650 struct inode *inode = d_inode(dentry);
1653 /* Are we allowed to snoop on the tasks file descriptors? */
1654 if (!proc_fd_access_allowed(inode))
1657 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1661 error = do_proc_readlink(&path, buffer, buflen);
1667 const struct inode_operations proc_pid_link_inode_operations = {
1668 .readlink = proc_pid_readlink,
1669 .get_link = proc_pid_get_link,
1670 .setattr = proc_setattr,
1674 /* building an inode */
1676 void task_dump_owner(struct task_struct *task, umode_t mode,
1677 kuid_t *ruid, kgid_t *rgid)
1679 /* Depending on the state of dumpable compute who should own a
1680 * proc file for a task.
1682 const struct cred *cred;
1686 if (unlikely(task->flags & PF_KTHREAD)) {
1687 *ruid = GLOBAL_ROOT_UID;
1688 *rgid = GLOBAL_ROOT_GID;
1692 /* Default to the tasks effective ownership */
1694 cred = __task_cred(task);
1700 * Before the /proc/pid/status file was created the only way to read
1701 * the effective uid of a /process was to stat /proc/pid. Reading
1702 * /proc/pid/status is slow enough that procps and other packages
1703 * kept stating /proc/pid. To keep the rules in /proc simple I have
1704 * made this apply to all per process world readable and executable
1707 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1708 struct mm_struct *mm;
1711 /* Make non-dumpable tasks owned by some root */
1713 if (get_dumpable(mm) != SUID_DUMP_USER) {
1714 struct user_namespace *user_ns = mm->user_ns;
1716 uid = make_kuid(user_ns, 0);
1717 if (!uid_valid(uid))
1718 uid = GLOBAL_ROOT_UID;
1720 gid = make_kgid(user_ns, 0);
1721 if (!gid_valid(gid))
1722 gid = GLOBAL_ROOT_GID;
1725 uid = GLOBAL_ROOT_UID;
1726 gid = GLOBAL_ROOT_GID;
1734 struct inode *proc_pid_make_inode(struct super_block * sb,
1735 struct task_struct *task, umode_t mode)
1737 struct inode * inode;
1738 struct proc_inode *ei;
1740 /* We need a new inode */
1742 inode = new_inode(sb);
1748 inode->i_mode = mode;
1749 inode->i_ino = get_next_ino();
1750 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1751 inode->i_op = &proc_def_inode_operations;
1754 * grab the reference to task.
1756 ei->pid = get_task_pid(task, PIDTYPE_PID);
1760 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1761 security_task_to_inode(task, inode);
1771 int pid_getattr(const struct path *path, struct kstat *stat,
1772 u32 request_mask, unsigned int query_flags)
1774 struct inode *inode = d_inode(path->dentry);
1775 struct pid_namespace *pid = proc_pid_ns(inode);
1776 struct task_struct *task;
1778 generic_fillattr(inode, stat);
1780 stat->uid = GLOBAL_ROOT_UID;
1781 stat->gid = GLOBAL_ROOT_GID;
1783 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1785 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1788 * This doesn't prevent learning whether PID exists,
1789 * it only makes getattr() consistent with readdir().
1793 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1802 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1804 void pid_update_inode(struct task_struct *task, struct inode *inode)
1806 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1808 inode->i_mode &= ~(S_ISUID | S_ISGID);
1809 security_task_to_inode(task, inode);
1813 * Rewrite the inode's ownerships here because the owning task may have
1814 * performed a setuid(), etc.
1817 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1819 struct inode *inode;
1820 struct task_struct *task;
1822 if (flags & LOOKUP_RCU)
1825 inode = d_inode(dentry);
1826 task = get_proc_task(inode);
1829 pid_update_inode(task, inode);
1830 put_task_struct(task);
1836 static inline bool proc_inode_is_dead(struct inode *inode)
1838 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1841 int pid_delete_dentry(const struct dentry *dentry)
1843 /* Is the task we represent dead?
1844 * If so, then don't put the dentry on the lru list,
1845 * kill it immediately.
1847 return proc_inode_is_dead(d_inode(dentry));
1850 const struct dentry_operations pid_dentry_operations =
1852 .d_revalidate = pid_revalidate,
1853 .d_delete = pid_delete_dentry,
1859 * Fill a directory entry.
1861 * If possible create the dcache entry and derive our inode number and
1862 * file type from dcache entry.
1864 * Since all of the proc inode numbers are dynamically generated, the inode
1865 * numbers do not exist until the inode is cache. This means creating the
1866 * the dcache entry in readdir is necessary to keep the inode numbers
1867 * reported by readdir in sync with the inode numbers reported
1870 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1871 const char *name, unsigned int len,
1872 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1874 struct dentry *child, *dir = file->f_path.dentry;
1875 struct qstr qname = QSTR_INIT(name, len);
1876 struct inode *inode;
1877 unsigned type = DT_UNKNOWN;
1880 child = d_hash_and_lookup(dir, &qname);
1882 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1883 child = d_alloc_parallel(dir, &qname, &wq);
1885 goto end_instantiate;
1886 if (d_in_lookup(child)) {
1888 res = instantiate(child, task, ptr);
1889 d_lookup_done(child);
1890 if (unlikely(res)) {
1894 goto end_instantiate;
1898 inode = d_inode(child);
1900 type = inode->i_mode >> 12;
1903 return dir_emit(ctx, name, len, ino, type);
1907 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1908 * which represent vma start and end addresses.
1910 static int dname_to_vma_addr(struct dentry *dentry,
1911 unsigned long *start, unsigned long *end)
1913 const char *str = dentry->d_name.name;
1914 unsigned long long sval, eval;
1917 if (str[0] == '0' && str[1] != '-')
1919 len = _parse_integer(str, 16, &sval);
1920 if (len & KSTRTOX_OVERFLOW)
1922 if (sval != (unsigned long)sval)
1930 if (str[0] == '0' && str[1])
1932 len = _parse_integer(str, 16, &eval);
1933 if (len & KSTRTOX_OVERFLOW)
1935 if (eval != (unsigned long)eval)
1948 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1950 unsigned long vm_start, vm_end;
1951 bool exact_vma_exists = false;
1952 struct mm_struct *mm = NULL;
1953 struct task_struct *task;
1954 struct inode *inode;
1957 if (flags & LOOKUP_RCU)
1960 inode = d_inode(dentry);
1961 task = get_proc_task(inode);
1965 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1966 if (IS_ERR_OR_NULL(mm))
1969 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1970 down_read(&mm->mmap_sem);
1971 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1972 up_read(&mm->mmap_sem);
1977 if (exact_vma_exists) {
1978 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1980 security_task_to_inode(task, inode);
1985 put_task_struct(task);
1991 static const struct dentry_operations tid_map_files_dentry_operations = {
1992 .d_revalidate = map_files_d_revalidate,
1993 .d_delete = pid_delete_dentry,
1996 static int map_files_get_link(struct dentry *dentry, struct path *path)
1998 unsigned long vm_start, vm_end;
1999 struct vm_area_struct *vma;
2000 struct task_struct *task;
2001 struct mm_struct *mm;
2005 task = get_proc_task(d_inode(dentry));
2009 mm = get_task_mm(task);
2010 put_task_struct(task);
2014 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2019 down_read(&mm->mmap_sem);
2020 vma = find_exact_vma(mm, vm_start, vm_end);
2021 if (vma && vma->vm_file) {
2022 *path = vma->vm_file->f_path;
2026 up_read(&mm->mmap_sem);
2034 struct map_files_info {
2035 unsigned long start;
2041 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2042 * symlinks may be used to bypass permissions on ancestor directories in the
2043 * path to the file in question.
2046 proc_map_files_get_link(struct dentry *dentry,
2047 struct inode *inode,
2048 struct delayed_call *done)
2050 if (!capable(CAP_SYS_ADMIN))
2051 return ERR_PTR(-EPERM);
2053 return proc_pid_get_link(dentry, inode, done);
2057 * Identical to proc_pid_link_inode_operations except for get_link()
2059 static const struct inode_operations proc_map_files_link_inode_operations = {
2060 .readlink = proc_pid_readlink,
2061 .get_link = proc_map_files_get_link,
2062 .setattr = proc_setattr,
2065 static struct dentry *
2066 proc_map_files_instantiate(struct dentry *dentry,
2067 struct task_struct *task, const void *ptr)
2069 fmode_t mode = (fmode_t)(unsigned long)ptr;
2070 struct proc_inode *ei;
2071 struct inode *inode;
2073 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2074 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2075 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2077 return ERR_PTR(-ENOENT);
2080 ei->op.proc_get_link = map_files_get_link;
2082 inode->i_op = &proc_map_files_link_inode_operations;
2085 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2086 return d_splice_alias(inode, dentry);
2089 static struct dentry *proc_map_files_lookup(struct inode *dir,
2090 struct dentry *dentry, unsigned int flags)
2092 unsigned long vm_start, vm_end;
2093 struct vm_area_struct *vma;
2094 struct task_struct *task;
2095 struct dentry *result;
2096 struct mm_struct *mm;
2098 result = ERR_PTR(-ENOENT);
2099 task = get_proc_task(dir);
2103 result = ERR_PTR(-EACCES);
2104 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2107 result = ERR_PTR(-ENOENT);
2108 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2111 mm = get_task_mm(task);
2115 down_read(&mm->mmap_sem);
2116 vma = find_exact_vma(mm, vm_start, vm_end);
2121 result = proc_map_files_instantiate(dentry, task,
2122 (void *)(unsigned long)vma->vm_file->f_mode);
2125 up_read(&mm->mmap_sem);
2128 put_task_struct(task);
2133 static const struct inode_operations proc_map_files_inode_operations = {
2134 .lookup = proc_map_files_lookup,
2135 .permission = proc_fd_permission,
2136 .setattr = proc_setattr,
2140 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2142 struct vm_area_struct *vma;
2143 struct task_struct *task;
2144 struct mm_struct *mm;
2145 unsigned long nr_files, pos, i;
2146 GENRADIX(struct map_files_info) fa;
2147 struct map_files_info *p;
2153 task = get_proc_task(file_inode(file));
2158 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2162 if (!dir_emit_dots(file, ctx))
2165 mm = get_task_mm(task);
2168 down_read(&mm->mmap_sem);
2173 * We need two passes here:
2175 * 1) Collect vmas of mapped files with mmap_sem taken
2176 * 2) Release mmap_sem and instantiate entries
2178 * otherwise we get lockdep complained, since filldir()
2179 * routine might require mmap_sem taken in might_fault().
2182 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2185 if (++pos <= ctx->pos)
2188 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2191 up_read(&mm->mmap_sem);
2196 p->start = vma->vm_start;
2197 p->end = vma->vm_end;
2198 p->mode = vma->vm_file->f_mode;
2200 up_read(&mm->mmap_sem);
2203 for (i = 0; i < nr_files; i++) {
2204 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2207 p = genradix_ptr(&fa, i);
2208 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2209 if (!proc_fill_cache(file, ctx,
2211 proc_map_files_instantiate,
2213 (void *)(unsigned long)p->mode))
2219 put_task_struct(task);
2225 static const struct file_operations proc_map_files_operations = {
2226 .read = generic_read_dir,
2227 .iterate_shared = proc_map_files_readdir,
2228 .llseek = generic_file_llseek,
2231 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2232 struct timers_private {
2234 struct task_struct *task;
2235 struct sighand_struct *sighand;
2236 struct pid_namespace *ns;
2237 unsigned long flags;
2240 static void *timers_start(struct seq_file *m, loff_t *pos)
2242 struct timers_private *tp = m->private;
2244 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2246 return ERR_PTR(-ESRCH);
2248 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2250 return ERR_PTR(-ESRCH);
2252 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2255 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2257 struct timers_private *tp = m->private;
2258 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2261 static void timers_stop(struct seq_file *m, void *v)
2263 struct timers_private *tp = m->private;
2266 unlock_task_sighand(tp->task, &tp->flags);
2271 put_task_struct(tp->task);
2276 static int show_timer(struct seq_file *m, void *v)
2278 struct k_itimer *timer;
2279 struct timers_private *tp = m->private;
2281 static const char * const nstr[] = {
2282 [SIGEV_SIGNAL] = "signal",
2283 [SIGEV_NONE] = "none",
2284 [SIGEV_THREAD] = "thread",
2287 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2288 notify = timer->it_sigev_notify;
2290 seq_printf(m, "ID: %d\n", timer->it_id);
2291 seq_printf(m, "signal: %d/%px\n",
2292 timer->sigq->info.si_signo,
2293 timer->sigq->info.si_value.sival_ptr);
2294 seq_printf(m, "notify: %s/%s.%d\n",
2295 nstr[notify & ~SIGEV_THREAD_ID],
2296 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2297 pid_nr_ns(timer->it_pid, tp->ns));
2298 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2303 static const struct seq_operations proc_timers_seq_ops = {
2304 .start = timers_start,
2305 .next = timers_next,
2306 .stop = timers_stop,
2310 static int proc_timers_open(struct inode *inode, struct file *file)
2312 struct timers_private *tp;
2314 tp = __seq_open_private(file, &proc_timers_seq_ops,
2315 sizeof(struct timers_private));
2319 tp->pid = proc_pid(inode);
2320 tp->ns = proc_pid_ns(inode);
2324 static const struct file_operations proc_timers_operations = {
2325 .open = proc_timers_open,
2327 .llseek = seq_lseek,
2328 .release = seq_release_private,
2332 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2333 size_t count, loff_t *offset)
2335 struct inode *inode = file_inode(file);
2336 struct task_struct *p;
2340 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2344 p = get_proc_task(inode);
2350 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2357 err = security_task_setscheduler(p);
2366 p->timer_slack_ns = p->default_timer_slack_ns;
2368 p->timer_slack_ns = slack_ns;
2377 static int timerslack_ns_show(struct seq_file *m, void *v)
2379 struct inode *inode = m->private;
2380 struct task_struct *p;
2383 p = get_proc_task(inode);
2389 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2396 err = security_task_getscheduler(p);
2402 seq_printf(m, "%llu\n", p->timer_slack_ns);
2411 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2413 return single_open(filp, timerslack_ns_show, inode);
2416 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2417 .open = timerslack_ns_open,
2419 .write = timerslack_ns_write,
2420 .llseek = seq_lseek,
2421 .release = single_release,
2424 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2425 struct task_struct *task, const void *ptr)
2427 const struct pid_entry *p = ptr;
2428 struct inode *inode;
2429 struct proc_inode *ei;
2431 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2433 return ERR_PTR(-ENOENT);
2436 if (S_ISDIR(inode->i_mode))
2437 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2439 inode->i_op = p->iop;
2441 inode->i_fop = p->fop;
2443 pid_update_inode(task, inode);
2444 d_set_d_op(dentry, &pid_dentry_operations);
2445 return d_splice_alias(inode, dentry);
2448 static struct dentry *proc_pident_lookup(struct inode *dir,
2449 struct dentry *dentry,
2450 const struct pid_entry *p,
2451 const struct pid_entry *end)
2453 struct task_struct *task = get_proc_task(dir);
2454 struct dentry *res = ERR_PTR(-ENOENT);
2460 * Yes, it does not scale. And it should not. Don't add
2461 * new entries into /proc/<tgid>/ without very good reasons.
2463 for (; p < end; p++) {
2464 if (p->len != dentry->d_name.len)
2466 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2467 res = proc_pident_instantiate(dentry, task, p);
2471 put_task_struct(task);
2476 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2477 const struct pid_entry *ents, unsigned int nents)
2479 struct task_struct *task = get_proc_task(file_inode(file));
2480 const struct pid_entry *p;
2485 if (!dir_emit_dots(file, ctx))
2488 if (ctx->pos >= nents + 2)
2491 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2492 if (!proc_fill_cache(file, ctx, p->name, p->len,
2493 proc_pident_instantiate, task, p))
2498 put_task_struct(task);
2502 #ifdef CONFIG_SECURITY
2503 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2504 size_t count, loff_t *ppos)
2506 struct inode * inode = file_inode(file);
2509 struct task_struct *task = get_proc_task(inode);
2514 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2515 (char*)file->f_path.dentry->d_name.name,
2517 put_task_struct(task);
2519 length = simple_read_from_buffer(buf, count, ppos, p, length);
2524 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2525 size_t count, loff_t *ppos)
2527 struct inode * inode = file_inode(file);
2528 struct task_struct *task;
2533 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2538 /* A task may only write its own attributes. */
2539 if (current != task) {
2545 if (count > PAGE_SIZE)
2548 /* No partial writes. */
2552 page = memdup_user(buf, count);
2558 /* Guard against adverse ptrace interaction */
2559 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2563 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2564 file->f_path.dentry->d_name.name, page,
2566 mutex_unlock(¤t->signal->cred_guard_mutex);
2573 static const struct file_operations proc_pid_attr_operations = {
2574 .read = proc_pid_attr_read,
2575 .write = proc_pid_attr_write,
2576 .llseek = generic_file_llseek,
2579 #define LSM_DIR_OPS(LSM) \
2580 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2581 struct dir_context *ctx) \
2583 return proc_pident_readdir(filp, ctx, \
2584 LSM##_attr_dir_stuff, \
2585 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2588 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2589 .read = generic_read_dir, \
2590 .iterate = proc_##LSM##_attr_dir_iterate, \
2591 .llseek = default_llseek, \
2594 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2595 struct dentry *dentry, unsigned int flags) \
2597 return proc_pident_lookup(dir, dentry, \
2598 LSM##_attr_dir_stuff, \
2599 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2602 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2603 .lookup = proc_##LSM##_attr_dir_lookup, \
2604 .getattr = pid_getattr, \
2605 .setattr = proc_setattr, \
2608 #ifdef CONFIG_SECURITY_SMACK
2609 static const struct pid_entry smack_attr_dir_stuff[] = {
2610 ATTR("smack", "current", 0666),
2615 static const struct pid_entry attr_dir_stuff[] = {
2616 ATTR(NULL, "current", 0666),
2617 ATTR(NULL, "prev", 0444),
2618 ATTR(NULL, "exec", 0666),
2619 ATTR(NULL, "fscreate", 0666),
2620 ATTR(NULL, "keycreate", 0666),
2621 ATTR(NULL, "sockcreate", 0666),
2622 #ifdef CONFIG_SECURITY_SMACK
2624 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2628 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2630 return proc_pident_readdir(file, ctx,
2631 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2634 static const struct file_operations proc_attr_dir_operations = {
2635 .read = generic_read_dir,
2636 .iterate_shared = proc_attr_dir_readdir,
2637 .llseek = generic_file_llseek,
2640 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2641 struct dentry *dentry, unsigned int flags)
2643 return proc_pident_lookup(dir, dentry,
2645 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
2648 static const struct inode_operations proc_attr_dir_inode_operations = {
2649 .lookup = proc_attr_dir_lookup,
2650 .getattr = pid_getattr,
2651 .setattr = proc_setattr,
2656 #ifdef CONFIG_ELF_CORE
2657 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2658 size_t count, loff_t *ppos)
2660 struct task_struct *task = get_proc_task(file_inode(file));
2661 struct mm_struct *mm;
2662 char buffer[PROC_NUMBUF];
2670 mm = get_task_mm(task);
2672 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2673 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2674 MMF_DUMP_FILTER_SHIFT));
2676 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2679 put_task_struct(task);
2684 static ssize_t proc_coredump_filter_write(struct file *file,
2685 const char __user *buf,
2689 struct task_struct *task;
2690 struct mm_struct *mm;
2696 ret = kstrtouint_from_user(buf, count, 0, &val);
2701 task = get_proc_task(file_inode(file));
2705 mm = get_task_mm(task);
2710 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2712 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2714 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2719 put_task_struct(task);
2726 static const struct file_operations proc_coredump_filter_operations = {
2727 .read = proc_coredump_filter_read,
2728 .write = proc_coredump_filter_write,
2729 .llseek = generic_file_llseek,
2733 #ifdef CONFIG_TASK_IO_ACCOUNTING
2734 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2736 struct task_io_accounting acct = task->ioac;
2737 unsigned long flags;
2740 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2744 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2749 if (whole && lock_task_sighand(task, &flags)) {
2750 struct task_struct *t = task;
2752 task_io_accounting_add(&acct, &task->signal->ioac);
2753 while_each_thread(task, t)
2754 task_io_accounting_add(&acct, &t->ioac);
2756 unlock_task_sighand(task, &flags);
2763 "read_bytes: %llu\n"
2764 "write_bytes: %llu\n"
2765 "cancelled_write_bytes: %llu\n",
2766 (unsigned long long)acct.rchar,
2767 (unsigned long long)acct.wchar,
2768 (unsigned long long)acct.syscr,
2769 (unsigned long long)acct.syscw,
2770 (unsigned long long)acct.read_bytes,
2771 (unsigned long long)acct.write_bytes,
2772 (unsigned long long)acct.cancelled_write_bytes);
2776 mutex_unlock(&task->signal->cred_guard_mutex);
2780 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2781 struct pid *pid, struct task_struct *task)
2783 return do_io_accounting(task, m, 0);
2786 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2787 struct pid *pid, struct task_struct *task)
2789 return do_io_accounting(task, m, 1);
2791 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2793 #ifdef CONFIG_USER_NS
2794 static int proc_id_map_open(struct inode *inode, struct file *file,
2795 const struct seq_operations *seq_ops)
2797 struct user_namespace *ns = NULL;
2798 struct task_struct *task;
2799 struct seq_file *seq;
2802 task = get_proc_task(inode);
2805 ns = get_user_ns(task_cred_xxx(task, user_ns));
2807 put_task_struct(task);
2812 ret = seq_open(file, seq_ops);
2816 seq = file->private_data;
2826 static int proc_id_map_release(struct inode *inode, struct file *file)
2828 struct seq_file *seq = file->private_data;
2829 struct user_namespace *ns = seq->private;
2831 return seq_release(inode, file);
2834 static int proc_uid_map_open(struct inode *inode, struct file *file)
2836 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2839 static int proc_gid_map_open(struct inode *inode, struct file *file)
2841 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2844 static int proc_projid_map_open(struct inode *inode, struct file *file)
2846 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2849 static const struct file_operations proc_uid_map_operations = {
2850 .open = proc_uid_map_open,
2851 .write = proc_uid_map_write,
2853 .llseek = seq_lseek,
2854 .release = proc_id_map_release,
2857 static const struct file_operations proc_gid_map_operations = {
2858 .open = proc_gid_map_open,
2859 .write = proc_gid_map_write,
2861 .llseek = seq_lseek,
2862 .release = proc_id_map_release,
2865 static const struct file_operations proc_projid_map_operations = {
2866 .open = proc_projid_map_open,
2867 .write = proc_projid_map_write,
2869 .llseek = seq_lseek,
2870 .release = proc_id_map_release,
2873 static int proc_setgroups_open(struct inode *inode, struct file *file)
2875 struct user_namespace *ns = NULL;
2876 struct task_struct *task;
2880 task = get_proc_task(inode);
2883 ns = get_user_ns(task_cred_xxx(task, user_ns));
2885 put_task_struct(task);
2890 if (file->f_mode & FMODE_WRITE) {
2892 if (!ns_capable(ns, CAP_SYS_ADMIN))
2896 ret = single_open(file, &proc_setgroups_show, ns);
2907 static int proc_setgroups_release(struct inode *inode, struct file *file)
2909 struct seq_file *seq = file->private_data;
2910 struct user_namespace *ns = seq->private;
2911 int ret = single_release(inode, file);
2916 static const struct file_operations proc_setgroups_operations = {
2917 .open = proc_setgroups_open,
2918 .write = proc_setgroups_write,
2920 .llseek = seq_lseek,
2921 .release = proc_setgroups_release,
2923 #endif /* CONFIG_USER_NS */
2925 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2926 struct pid *pid, struct task_struct *task)
2928 int err = lock_trace(task);
2930 seq_printf(m, "%08x\n", task->personality);
2936 #ifdef CONFIG_LIVEPATCH
2937 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2938 struct pid *pid, struct task_struct *task)
2940 seq_printf(m, "%d\n", task->patch_state);
2943 #endif /* CONFIG_LIVEPATCH */
2945 #ifdef CONFIG_STACKLEAK_METRICS
2946 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2947 struct pid *pid, struct task_struct *task)
2949 unsigned long prev_depth = THREAD_SIZE -
2950 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2951 unsigned long depth = THREAD_SIZE -
2952 (task->lowest_stack & (THREAD_SIZE - 1));
2954 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2958 #endif /* CONFIG_STACKLEAK_METRICS */
2963 static const struct file_operations proc_task_operations;
2964 static const struct inode_operations proc_task_inode_operations;
2966 static const struct pid_entry tgid_base_stuff[] = {
2967 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2968 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2969 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2970 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2971 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2973 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2975 REG("environ", S_IRUSR, proc_environ_operations),
2976 REG("auxv", S_IRUSR, proc_auxv_operations),
2977 ONE("status", S_IRUGO, proc_pid_status),
2978 ONE("personality", S_IRUSR, proc_pid_personality),
2979 ONE("limits", S_IRUGO, proc_pid_limits),
2980 #ifdef CONFIG_SCHED_DEBUG
2981 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2983 #ifdef CONFIG_SCHED_AUTOGROUP
2984 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2986 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2987 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2988 ONE("syscall", S_IRUSR, proc_pid_syscall),
2990 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2991 ONE("stat", S_IRUGO, proc_tgid_stat),
2992 ONE("statm", S_IRUGO, proc_pid_statm),
2993 REG("maps", S_IRUGO, proc_pid_maps_operations),
2995 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2997 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2998 LNK("cwd", proc_cwd_link),
2999 LNK("root", proc_root_link),
3000 LNK("exe", proc_exe_link),
3001 REG("mounts", S_IRUGO, proc_mounts_operations),
3002 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3003 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3004 #ifdef CONFIG_PROC_PAGE_MONITOR
3005 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3006 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3007 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3008 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3010 #ifdef CONFIG_SECURITY
3011 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3013 #ifdef CONFIG_KALLSYMS
3014 ONE("wchan", S_IRUGO, proc_pid_wchan),
3016 #ifdef CONFIG_STACKTRACE
3017 ONE("stack", S_IRUSR, proc_pid_stack),
3019 #ifdef CONFIG_SCHED_INFO
3020 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3022 #ifdef CONFIG_LATENCYTOP
3023 REG("latency", S_IRUGO, proc_lstats_operations),
3025 #ifdef CONFIG_PROC_PID_CPUSET
3026 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3028 #ifdef CONFIG_CGROUPS
3029 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3031 ONE("oom_score", S_IRUGO, proc_oom_score),
3032 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3033 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3035 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3036 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3038 #ifdef CONFIG_FAULT_INJECTION
3039 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3040 REG("fail-nth", 0644, proc_fail_nth_operations),
3042 #ifdef CONFIG_ELF_CORE
3043 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3045 #ifdef CONFIG_TASK_IO_ACCOUNTING
3046 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3048 #ifdef CONFIG_USER_NS
3049 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3050 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3051 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3052 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3054 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3055 REG("timers", S_IRUGO, proc_timers_operations),
3057 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3058 #ifdef CONFIG_LIVEPATCH
3059 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3061 #ifdef CONFIG_STACKLEAK_METRICS
3062 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3066 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3068 return proc_pident_readdir(file, ctx,
3069 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3072 static const struct file_operations proc_tgid_base_operations = {
3073 .read = generic_read_dir,
3074 .iterate_shared = proc_tgid_base_readdir,
3075 .llseek = generic_file_llseek,
3078 struct pid *tgid_pidfd_to_pid(const struct file *file)
3080 if (!d_is_dir(file->f_path.dentry) ||
3081 (file->f_op != &proc_tgid_base_operations))
3082 return ERR_PTR(-EBADF);
3084 return proc_pid(file_inode(file));
3087 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3089 return proc_pident_lookup(dir, dentry,
3091 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
3094 static const struct inode_operations proc_tgid_base_inode_operations = {
3095 .lookup = proc_tgid_base_lookup,
3096 .getattr = pid_getattr,
3097 .setattr = proc_setattr,
3098 .permission = proc_pid_permission,
3101 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3103 struct dentry *dentry, *leader, *dir;
3108 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3109 /* no ->d_hash() rejects on procfs */
3110 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3112 d_invalidate(dentry);
3120 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3121 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3126 name.len = strlen(name.name);
3127 dir = d_hash_and_lookup(leader, &name);
3129 goto out_put_leader;
3132 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3133 dentry = d_hash_and_lookup(dir, &name);
3135 d_invalidate(dentry);
3147 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3148 * @task: task that should be flushed.
3150 * When flushing dentries from proc, one needs to flush them from global
3151 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3152 * in. This call is supposed to do all of this job.
3154 * Looks in the dcache for
3156 * /proc/@tgid/task/@pid
3157 * if either directory is present flushes it and all of it'ts children
3160 * It is safe and reasonable to cache /proc entries for a task until
3161 * that task exits. After that they just clog up the dcache with
3162 * useless entries, possibly causing useful dcache entries to be
3163 * flushed instead. This routine is proved to flush those useless
3164 * dcache entries at process exit time.
3166 * NOTE: This routine is just an optimization so it does not guarantee
3167 * that no dcache entries will exist at process exit time it
3168 * just makes it very unlikely that any will persist.
3171 void proc_flush_task(struct task_struct *task)
3174 struct pid *pid, *tgid;
3177 pid = task_pid(task);
3178 tgid = task_tgid(task);
3180 for (i = 0; i <= pid->level; i++) {
3181 upid = &pid->numbers[i];
3182 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3183 tgid->numbers[i].nr);
3187 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3188 struct task_struct *task, const void *ptr)
3190 struct inode *inode;
3192 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3194 return ERR_PTR(-ENOENT);
3196 inode->i_op = &proc_tgid_base_inode_operations;
3197 inode->i_fop = &proc_tgid_base_operations;
3198 inode->i_flags|=S_IMMUTABLE;
3200 set_nlink(inode, nlink_tgid);
3201 pid_update_inode(task, inode);
3203 d_set_d_op(dentry, &pid_dentry_operations);
3204 return d_splice_alias(inode, dentry);
3207 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3209 struct task_struct *task;
3211 struct pid_namespace *ns;
3212 struct dentry *result = ERR_PTR(-ENOENT);
3214 tgid = name_to_int(&dentry->d_name);
3218 ns = dentry->d_sb->s_fs_info;
3220 task = find_task_by_pid_ns(tgid, ns);
3222 get_task_struct(task);
3227 result = proc_pid_instantiate(dentry, task, NULL);
3228 put_task_struct(task);
3234 * Find the first task with tgid >= tgid
3239 struct task_struct *task;
3241 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3246 put_task_struct(iter.task);
3250 pid = find_ge_pid(iter.tgid, ns);
3252 iter.tgid = pid_nr_ns(pid, ns);
3253 iter.task = pid_task(pid, PIDTYPE_PID);
3254 /* What we to know is if the pid we have find is the
3255 * pid of a thread_group_leader. Testing for task
3256 * being a thread_group_leader is the obvious thing
3257 * todo but there is a window when it fails, due to
3258 * the pid transfer logic in de_thread.
3260 * So we perform the straight forward test of seeing
3261 * if the pid we have found is the pid of a thread
3262 * group leader, and don't worry if the task we have
3263 * found doesn't happen to be a thread group leader.
3264 * As we don't care in the case of readdir.
3266 if (!iter.task || !has_group_leader_pid(iter.task)) {
3270 get_task_struct(iter.task);
3276 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3278 /* for the /proc/ directory itself, after non-process stuff has been done */
3279 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3281 struct tgid_iter iter;
3282 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3283 loff_t pos = ctx->pos;
3285 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3288 if (pos == TGID_OFFSET - 2) {
3289 struct inode *inode = d_inode(ns->proc_self);
3290 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3292 ctx->pos = pos = pos + 1;
3294 if (pos == TGID_OFFSET - 1) {
3295 struct inode *inode = d_inode(ns->proc_thread_self);
3296 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3298 ctx->pos = pos = pos + 1;
3300 iter.tgid = pos - TGID_OFFSET;
3302 for (iter = next_tgid(ns, iter);
3304 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3309 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3312 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3313 ctx->pos = iter.tgid + TGID_OFFSET;
3314 if (!proc_fill_cache(file, ctx, name, len,
3315 proc_pid_instantiate, iter.task, NULL)) {
3316 put_task_struct(iter.task);
3320 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3325 * proc_tid_comm_permission is a special permission function exclusively
3326 * used for the node /proc/<pid>/task/<tid>/comm.
3327 * It bypasses generic permission checks in the case where a task of the same
3328 * task group attempts to access the node.
3329 * The rationale behind this is that glibc and bionic access this node for
3330 * cross thread naming (pthread_set/getname_np(!self)). However, if
3331 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3332 * which locks out the cross thread naming implementation.
3333 * This function makes sure that the node is always accessible for members of
3334 * same thread group.
3336 static int proc_tid_comm_permission(struct inode *inode, int mask)
3338 bool is_same_tgroup;
3339 struct task_struct *task;
3341 task = get_proc_task(inode);
3344 is_same_tgroup = same_thread_group(current, task);
3345 put_task_struct(task);
3347 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3348 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3349 * read or written by the members of the corresponding
3355 return generic_permission(inode, mask);
3358 static const struct inode_operations proc_tid_comm_inode_operations = {
3359 .permission = proc_tid_comm_permission,
3365 static const struct pid_entry tid_base_stuff[] = {
3366 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3367 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3368 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3370 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3372 REG("environ", S_IRUSR, proc_environ_operations),
3373 REG("auxv", S_IRUSR, proc_auxv_operations),
3374 ONE("status", S_IRUGO, proc_pid_status),
3375 ONE("personality", S_IRUSR, proc_pid_personality),
3376 ONE("limits", S_IRUGO, proc_pid_limits),
3377 #ifdef CONFIG_SCHED_DEBUG
3378 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3380 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3381 &proc_tid_comm_inode_operations,
3382 &proc_pid_set_comm_operations, {}),
3383 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3384 ONE("syscall", S_IRUSR, proc_pid_syscall),
3386 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3387 ONE("stat", S_IRUGO, proc_tid_stat),
3388 ONE("statm", S_IRUGO, proc_pid_statm),
3389 REG("maps", S_IRUGO, proc_pid_maps_operations),
3390 #ifdef CONFIG_PROC_CHILDREN
3391 REG("children", S_IRUGO, proc_tid_children_operations),
3394 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3396 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3397 LNK("cwd", proc_cwd_link),
3398 LNK("root", proc_root_link),
3399 LNK("exe", proc_exe_link),
3400 REG("mounts", S_IRUGO, proc_mounts_operations),
3401 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3402 #ifdef CONFIG_PROC_PAGE_MONITOR
3403 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3404 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3405 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3406 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3408 #ifdef CONFIG_SECURITY
3409 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3411 #ifdef CONFIG_KALLSYMS
3412 ONE("wchan", S_IRUGO, proc_pid_wchan),
3414 #ifdef CONFIG_STACKTRACE
3415 ONE("stack", S_IRUSR, proc_pid_stack),
3417 #ifdef CONFIG_SCHED_INFO
3418 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3420 #ifdef CONFIG_LATENCYTOP
3421 REG("latency", S_IRUGO, proc_lstats_operations),
3423 #ifdef CONFIG_PROC_PID_CPUSET
3424 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3426 #ifdef CONFIG_CGROUPS
3427 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3429 ONE("oom_score", S_IRUGO, proc_oom_score),
3430 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3431 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3433 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3434 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3436 #ifdef CONFIG_FAULT_INJECTION
3437 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3438 REG("fail-nth", 0644, proc_fail_nth_operations),
3440 #ifdef CONFIG_TASK_IO_ACCOUNTING
3441 ONE("io", S_IRUSR, proc_tid_io_accounting),
3443 #ifdef CONFIG_USER_NS
3444 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3445 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3446 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3447 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3449 #ifdef CONFIG_LIVEPATCH
3450 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3454 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3456 return proc_pident_readdir(file, ctx,
3457 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3460 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3462 return proc_pident_lookup(dir, dentry,
3464 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
3467 static const struct file_operations proc_tid_base_operations = {
3468 .read = generic_read_dir,
3469 .iterate_shared = proc_tid_base_readdir,
3470 .llseek = generic_file_llseek,
3473 static const struct inode_operations proc_tid_base_inode_operations = {
3474 .lookup = proc_tid_base_lookup,
3475 .getattr = pid_getattr,
3476 .setattr = proc_setattr,
3479 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3480 struct task_struct *task, const void *ptr)
3482 struct inode *inode;
3483 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3485 return ERR_PTR(-ENOENT);
3487 inode->i_op = &proc_tid_base_inode_operations;
3488 inode->i_fop = &proc_tid_base_operations;
3489 inode->i_flags |= S_IMMUTABLE;
3491 set_nlink(inode, nlink_tid);
3492 pid_update_inode(task, inode);
3494 d_set_d_op(dentry, &pid_dentry_operations);
3495 return d_splice_alias(inode, dentry);
3498 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3500 struct task_struct *task;
3501 struct task_struct *leader = get_proc_task(dir);
3503 struct pid_namespace *ns;
3504 struct dentry *result = ERR_PTR(-ENOENT);
3509 tid = name_to_int(&dentry->d_name);
3513 ns = dentry->d_sb->s_fs_info;
3515 task = find_task_by_pid_ns(tid, ns);
3517 get_task_struct(task);
3521 if (!same_thread_group(leader, task))
3524 result = proc_task_instantiate(dentry, task, NULL);
3526 put_task_struct(task);
3528 put_task_struct(leader);
3534 * Find the first tid of a thread group to return to user space.
3536 * Usually this is just the thread group leader, but if the users
3537 * buffer was too small or there was a seek into the middle of the
3538 * directory we have more work todo.
3540 * In the case of a short read we start with find_task_by_pid.
3542 * In the case of a seek we start with the leader and walk nr
3545 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3546 struct pid_namespace *ns)
3548 struct task_struct *pos, *task;
3549 unsigned long nr = f_pos;
3551 if (nr != f_pos) /* 32bit overflow? */
3555 task = pid_task(pid, PIDTYPE_PID);
3559 /* Attempt to start with the tid of a thread */
3561 pos = find_task_by_pid_ns(tid, ns);
3562 if (pos && same_thread_group(pos, task))
3566 /* If nr exceeds the number of threads there is nothing todo */
3567 if (nr >= get_nr_threads(task))
3570 /* If we haven't found our starting place yet start
3571 * with the leader and walk nr threads forward.
3573 pos = task = task->group_leader;
3577 } while_each_thread(task, pos);
3582 get_task_struct(pos);
3589 * Find the next thread in the thread list.
3590 * Return NULL if there is an error or no next thread.
3592 * The reference to the input task_struct is released.
3594 static struct task_struct *next_tid(struct task_struct *start)
3596 struct task_struct *pos = NULL;
3598 if (pid_alive(start)) {
3599 pos = next_thread(start);
3600 if (thread_group_leader(pos))
3603 get_task_struct(pos);
3606 put_task_struct(start);
3610 /* for the /proc/TGID/task/ directories */
3611 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3613 struct inode *inode = file_inode(file);
3614 struct task_struct *task;
3615 struct pid_namespace *ns;
3618 if (proc_inode_is_dead(inode))
3621 if (!dir_emit_dots(file, ctx))
3624 /* f_version caches the tgid value that the last readdir call couldn't
3625 * return. lseek aka telldir automagically resets f_version to 0.
3627 ns = proc_pid_ns(inode);
3628 tid = (int)file->f_version;
3629 file->f_version = 0;
3630 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3632 task = next_tid(task), ctx->pos++) {
3635 tid = task_pid_nr_ns(task, ns);
3636 len = snprintf(name, sizeof(name), "%u", tid);
3637 if (!proc_fill_cache(file, ctx, name, len,
3638 proc_task_instantiate, task, NULL)) {
3639 /* returning this tgid failed, save it as the first
3640 * pid for the next readir call */
3641 file->f_version = (u64)tid;
3642 put_task_struct(task);
3650 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3651 u32 request_mask, unsigned int query_flags)
3653 struct inode *inode = d_inode(path->dentry);
3654 struct task_struct *p = get_proc_task(inode);
3655 generic_fillattr(inode, stat);
3658 stat->nlink += get_nr_threads(p);
3665 static const struct inode_operations proc_task_inode_operations = {
3666 .lookup = proc_task_lookup,
3667 .getattr = proc_task_getattr,
3668 .setattr = proc_setattr,
3669 .permission = proc_pid_permission,
3672 static const struct file_operations proc_task_operations = {
3673 .read = generic_read_dir,
3674 .iterate_shared = proc_task_readdir,
3675 .llseek = generic_file_llseek,
3678 void __init set_proc_pid_nlink(void)
3680 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3681 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));