1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.h"
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tid __ro_after_init;
114 static u8 nlink_tgid __ro_after_init;
120 const struct inode_operations *iop;
121 const struct file_operations *fop;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
134 #define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136 #define LNK(NAME, get_link) \
137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
138 &proc_pid_link_inode_operations, NULL, \
139 { .proc_get_link = get_link } )
140 #define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142 #define ONE(NAME, MODE, show) \
143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
145 { .proc_show = show } )
148 * Count the number of hardlinks for the pid_entry table, excluding the .
151 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
166 static int get_task_root(struct task_struct *task, struct path *root)
168 int result = -ENOENT;
172 get_fs_root(task->fs, root);
179 static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 struct task_struct *task = get_proc_task(d_inode(dentry));
182 int result = -ENOENT;
187 get_fs_pwd(task->fs, path);
191 put_task_struct(task);
196 static int proc_root_link(struct dentry *dentry, struct path *path)
198 struct task_struct *task = get_proc_task(d_inode(dentry));
199 int result = -ENOENT;
202 result = get_task_root(task, path);
203 put_task_struct(task);
208 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
209 size_t count, loff_t *ppos)
211 unsigned long arg_start, arg_end, env_start, env_end;
212 unsigned long pos, len;
215 /* Check if process spawned far enough to have cmdline. */
219 spin_lock(&mm->arg_lock);
220 arg_start = mm->arg_start;
221 arg_end = mm->arg_end;
222 env_start = mm->env_start;
223 env_end = mm->env_end;
224 spin_unlock(&mm->arg_lock);
226 if (arg_start >= arg_end)
230 * We have traditionally allowed the user to re-write
231 * the argument strings and overflow the end result
232 * into the environment section. But only do that if
233 * the environment area is contiguous to the arguments.
235 if (env_start != arg_end || env_start >= env_end)
236 env_start = env_end = arg_end;
238 /* .. and limit it to a maximum of one page of slop */
239 if (env_end >= arg_end + PAGE_SIZE)
240 env_end = arg_end + PAGE_SIZE - 1;
242 /* We're not going to care if "*ppos" has high bits set */
243 pos = arg_start + *ppos;
245 /* .. but we do check the result is in the proper range */
246 if (pos < arg_start || pos >= env_end)
249 /* .. and we never go past env_end */
250 if (env_end - pos < count)
251 count = env_end - pos;
253 page = (char *)__get_free_page(GFP_KERNEL);
260 size_t size = min_t(size_t, PAGE_SIZE, count);
264 * Are we already starting past the official end?
265 * We always include the last byte that is *supposed*
268 offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
270 got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
275 /* Don't walk past a NUL character once you hit arg_end */
276 if (pos + got >= arg_end) {
280 * If we started before 'arg_end' but ended up
281 * at or after it, we start the NUL character
282 * check at arg_end-1 (where we expect the normal
285 * NOTE! This is smaller than 'got', because
286 * pos + got >= arg_end
289 n = arg_end - pos - 1;
291 /* Cut off at first NUL after 'n' */
292 got = n + strnlen(page+n, offset+got-n);
297 /* Include the NUL if it existed */
302 got -= copy_to_user(buf, page+offset, got);
303 if (unlikely(!got)) {
314 free_page((unsigned long)page);
318 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
319 size_t count, loff_t *pos)
321 struct mm_struct *mm;
324 mm = get_task_mm(tsk);
328 ret = get_mm_cmdline(mm, buf, count, pos);
333 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
334 size_t count, loff_t *pos)
336 struct task_struct *tsk;
341 tsk = get_proc_task(file_inode(file));
344 ret = get_task_cmdline(tsk, buf, count, pos);
345 put_task_struct(tsk);
351 static const struct file_operations proc_pid_cmdline_ops = {
352 .read = proc_pid_cmdline_read,
353 .llseek = generic_file_llseek,
356 #ifdef CONFIG_KALLSYMS
358 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
359 * Returns the resolved symbol. If that fails, simply return the address.
361 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
362 struct pid *pid, struct task_struct *task)
365 char symname[KSYM_NAME_LEN];
367 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
370 wchan = get_wchan(task);
371 if (wchan && !lookup_symbol_name(wchan, symname)) {
372 seq_puts(m, symname);
380 #endif /* CONFIG_KALLSYMS */
382 static int lock_trace(struct task_struct *task)
384 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
387 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
388 mutex_unlock(&task->signal->cred_guard_mutex);
394 static void unlock_trace(struct task_struct *task)
396 mutex_unlock(&task->signal->cred_guard_mutex);
399 #ifdef CONFIG_STACKTRACE
401 #define MAX_STACK_TRACE_DEPTH 64
403 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
404 struct pid *pid, struct task_struct *task)
406 struct stack_trace trace;
407 unsigned long *entries;
411 * The ability to racily run the kernel stack unwinder on a running task
412 * and then observe the unwinder output is scary; while it is useful for
413 * debugging kernel issues, it can also allow an attacker to leak kernel
415 * Doing this in a manner that is at least safe from races would require
416 * some work to ensure that the remote task can not be scheduled; and
417 * even then, this would still expose the unwinder as local attack
419 * Therefore, this interface is restricted to root.
421 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
424 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
429 trace.nr_entries = 0;
430 trace.max_entries = MAX_STACK_TRACE_DEPTH;
431 trace.entries = entries;
434 err = lock_trace(task);
438 save_stack_trace_tsk(task, &trace);
440 for (i = 0; i < trace.nr_entries; i++) {
441 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
451 #ifdef CONFIG_SCHED_INFO
453 * Provides /proc/PID/schedstat
455 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456 struct pid *pid, struct task_struct *task)
458 if (unlikely(!sched_info_on()))
459 seq_printf(m, "0 0 0\n");
461 seq_printf(m, "%llu %llu %lu\n",
462 (unsigned long long)task->se.sum_exec_runtime,
463 (unsigned long long)task->sched_info.run_delay,
464 task->sched_info.pcount);
470 #ifdef CONFIG_LATENCYTOP
471 static int lstats_show_proc(struct seq_file *m, void *v)
474 struct inode *inode = m->private;
475 struct task_struct *task = get_proc_task(inode);
479 seq_puts(m, "Latency Top version : v0.1\n");
480 for (i = 0; i < LT_SAVECOUNT; i++) {
481 struct latency_record *lr = &task->latency_record[i];
482 if (lr->backtrace[0]) {
484 seq_printf(m, "%i %li %li",
485 lr->count, lr->time, lr->max);
486 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
487 unsigned long bt = lr->backtrace[q];
492 seq_printf(m, " %ps", (void *)bt);
498 put_task_struct(task);
502 static int lstats_open(struct inode *inode, struct file *file)
504 return single_open(file, lstats_show_proc, inode);
507 static ssize_t lstats_write(struct file *file, const char __user *buf,
508 size_t count, loff_t *offs)
510 struct task_struct *task = get_proc_task(file_inode(file));
514 clear_all_latency_tracing(task);
515 put_task_struct(task);
520 static const struct file_operations proc_lstats_operations = {
523 .write = lstats_write,
525 .release = single_release,
530 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
531 struct pid *pid, struct task_struct *task)
533 unsigned long totalpages = totalram_pages + total_swap_pages;
534 unsigned long points = 0;
536 points = oom_badness(task, NULL, NULL, totalpages) *
538 seq_printf(m, "%lu\n", points);
548 static const struct limit_names lnames[RLIM_NLIMITS] = {
549 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
550 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
551 [RLIMIT_DATA] = {"Max data size", "bytes"},
552 [RLIMIT_STACK] = {"Max stack size", "bytes"},
553 [RLIMIT_CORE] = {"Max core file size", "bytes"},
554 [RLIMIT_RSS] = {"Max resident set", "bytes"},
555 [RLIMIT_NPROC] = {"Max processes", "processes"},
556 [RLIMIT_NOFILE] = {"Max open files", "files"},
557 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
558 [RLIMIT_AS] = {"Max address space", "bytes"},
559 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
560 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
561 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
562 [RLIMIT_NICE] = {"Max nice priority", NULL},
563 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
564 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
567 /* Display limits for a process */
568 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
569 struct pid *pid, struct task_struct *task)
574 struct rlimit rlim[RLIM_NLIMITS];
576 if (!lock_task_sighand(task, &flags))
578 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
579 unlock_task_sighand(task, &flags);
582 * print the file header
584 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
585 "Limit", "Soft Limit", "Hard Limit", "Units");
587 for (i = 0; i < RLIM_NLIMITS; i++) {
588 if (rlim[i].rlim_cur == RLIM_INFINITY)
589 seq_printf(m, "%-25s %-20s ",
590 lnames[i].name, "unlimited");
592 seq_printf(m, "%-25s %-20lu ",
593 lnames[i].name, rlim[i].rlim_cur);
595 if (rlim[i].rlim_max == RLIM_INFINITY)
596 seq_printf(m, "%-20s ", "unlimited");
598 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
601 seq_printf(m, "%-10s\n", lnames[i].unit);
609 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
610 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
611 struct pid *pid, struct task_struct *task)
614 unsigned long args[6], sp, pc;
617 res = lock_trace(task);
621 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
622 seq_puts(m, "running\n");
624 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
627 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
629 args[0], args[1], args[2], args[3], args[4], args[5],
635 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
637 /************************************************************************/
638 /* Here the fs part begins */
639 /************************************************************************/
641 /* permission checks */
642 static int proc_fd_access_allowed(struct inode *inode)
644 struct task_struct *task;
646 /* Allow access to a task's file descriptors if it is us or we
647 * may use ptrace attach to the process and find out that
650 task = get_proc_task(inode);
652 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
653 put_task_struct(task);
658 int proc_setattr(struct dentry *dentry, struct iattr *attr)
661 struct inode *inode = d_inode(dentry);
663 if (attr->ia_valid & ATTR_MODE)
666 error = setattr_prepare(dentry, attr);
670 setattr_copy(inode, attr);
671 mark_inode_dirty(inode);
676 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
677 * or euid/egid (for hide_pid_min=2)?
679 static bool has_pid_permissions(struct pid_namespace *pid,
680 struct task_struct *task,
683 if (pid->hide_pid < hide_pid_min)
685 if (in_group_p(pid->pid_gid))
687 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
691 static int proc_pid_permission(struct inode *inode, int mask)
693 struct pid_namespace *pid = proc_pid_ns(inode);
694 struct task_struct *task;
697 task = get_proc_task(inode);
700 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
701 put_task_struct(task);
704 if (pid->hide_pid == HIDEPID_INVISIBLE) {
706 * Let's make getdents(), stat(), and open()
707 * consistent with each other. If a process
708 * may not stat() a file, it shouldn't be seen
716 return generic_permission(inode, mask);
721 static const struct inode_operations proc_def_inode_operations = {
722 .setattr = proc_setattr,
725 static int proc_single_show(struct seq_file *m, void *v)
727 struct inode *inode = m->private;
728 struct pid_namespace *ns = proc_pid_ns(inode);
729 struct pid *pid = proc_pid(inode);
730 struct task_struct *task;
733 task = get_pid_task(pid, PIDTYPE_PID);
737 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
739 put_task_struct(task);
743 static int proc_single_open(struct inode *inode, struct file *filp)
745 return single_open(filp, proc_single_show, inode);
748 static const struct file_operations proc_single_file_operations = {
749 .open = proc_single_open,
752 .release = single_release,
756 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
758 struct task_struct *task = get_proc_task(inode);
759 struct mm_struct *mm = ERR_PTR(-ESRCH);
762 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
763 put_task_struct(task);
765 if (!IS_ERR_OR_NULL(mm)) {
766 /* ensure this mm_struct can't be freed */
768 /* but do not pin its memory */
776 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
778 struct mm_struct *mm = proc_mem_open(inode, mode);
783 file->private_data = mm;
787 static int mem_open(struct inode *inode, struct file *file)
789 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
791 /* OK to pass negative loff_t, we can catch out-of-range */
792 file->f_mode |= FMODE_UNSIGNED_OFFSET;
797 static ssize_t mem_rw(struct file *file, char __user *buf,
798 size_t count, loff_t *ppos, int write)
800 struct mm_struct *mm = file->private_data;
801 unsigned long addr = *ppos;
809 page = (char *)__get_free_page(GFP_KERNEL);
814 if (!mmget_not_zero(mm))
817 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
820 int this_len = min_t(int, count, PAGE_SIZE);
822 if (write && copy_from_user(page, buf, this_len)) {
827 this_len = access_remote_vm(mm, addr, page, this_len, flags);
834 if (!write && copy_to_user(buf, page, this_len)) {
848 free_page((unsigned long) page);
852 static ssize_t mem_read(struct file *file, char __user *buf,
853 size_t count, loff_t *ppos)
855 return mem_rw(file, buf, count, ppos, 0);
858 static ssize_t mem_write(struct file *file, const char __user *buf,
859 size_t count, loff_t *ppos)
861 return mem_rw(file, (char __user*)buf, count, ppos, 1);
864 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
868 file->f_pos = offset;
871 file->f_pos += offset;
876 force_successful_syscall_return();
880 static int mem_release(struct inode *inode, struct file *file)
882 struct mm_struct *mm = file->private_data;
888 static const struct file_operations proc_mem_operations = {
893 .release = mem_release,
896 static int environ_open(struct inode *inode, struct file *file)
898 return __mem_open(inode, file, PTRACE_MODE_READ);
901 static ssize_t environ_read(struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
905 unsigned long src = *ppos;
907 struct mm_struct *mm = file->private_data;
908 unsigned long env_start, env_end;
910 /* Ensure the process spawned far enough to have an environment. */
911 if (!mm || !mm->env_end)
914 page = (char *)__get_free_page(GFP_KERNEL);
919 if (!mmget_not_zero(mm))
922 spin_lock(&mm->arg_lock);
923 env_start = mm->env_start;
924 env_end = mm->env_end;
925 spin_unlock(&mm->arg_lock);
928 size_t this_len, max_len;
931 if (src >= (env_end - env_start))
934 this_len = env_end - (env_start + src);
936 max_len = min_t(size_t, PAGE_SIZE, count);
937 this_len = min(max_len, this_len);
939 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
946 if (copy_to_user(buf, page, retval)) {
960 free_page((unsigned long) page);
964 static const struct file_operations proc_environ_operations = {
965 .open = environ_open,
966 .read = environ_read,
967 .llseek = generic_file_llseek,
968 .release = mem_release,
971 static int auxv_open(struct inode *inode, struct file *file)
973 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
976 static ssize_t auxv_read(struct file *file, char __user *buf,
977 size_t count, loff_t *ppos)
979 struct mm_struct *mm = file->private_data;
980 unsigned int nwords = 0;
986 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
987 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
988 nwords * sizeof(mm->saved_auxv[0]));
991 static const struct file_operations proc_auxv_operations = {
994 .llseek = generic_file_llseek,
995 .release = mem_release,
998 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1001 struct task_struct *task = get_proc_task(file_inode(file));
1002 char buffer[PROC_NUMBUF];
1003 int oom_adj = OOM_ADJUST_MIN;
1008 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1009 oom_adj = OOM_ADJUST_MAX;
1011 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1013 put_task_struct(task);
1014 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1015 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1018 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1020 static DEFINE_MUTEX(oom_adj_mutex);
1021 struct mm_struct *mm = NULL;
1022 struct task_struct *task;
1025 task = get_proc_task(file_inode(file));
1029 mutex_lock(&oom_adj_mutex);
1031 if (oom_adj < task->signal->oom_score_adj &&
1032 !capable(CAP_SYS_RESOURCE)) {
1037 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1038 * /proc/pid/oom_score_adj instead.
1040 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1041 current->comm, task_pid_nr(current), task_pid_nr(task),
1044 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1045 !capable(CAP_SYS_RESOURCE)) {
1052 * Make sure we will check other processes sharing the mm if this is
1053 * not vfrok which wants its own oom_score_adj.
1054 * pin the mm so it doesn't go away and get reused after task_unlock
1056 if (!task->vfork_done) {
1057 struct task_struct *p = find_lock_task_mm(task);
1060 if (atomic_read(&p->mm->mm_users) > 1) {
1068 task->signal->oom_score_adj = oom_adj;
1069 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1070 task->signal->oom_score_adj_min = (short)oom_adj;
1071 trace_oom_score_adj_update(task);
1074 struct task_struct *p;
1077 for_each_process(p) {
1078 if (same_thread_group(task, p))
1081 /* do not touch kernel threads or the global init */
1082 if (p->flags & PF_KTHREAD || is_global_init(p))
1086 if (!p->vfork_done && process_shares_mm(p, mm)) {
1087 p->signal->oom_score_adj = oom_adj;
1088 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1089 p->signal->oom_score_adj_min = (short)oom_adj;
1097 mutex_unlock(&oom_adj_mutex);
1098 put_task_struct(task);
1103 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1104 * kernels. The effective policy is defined by oom_score_adj, which has a
1105 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1106 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1107 * Processes that become oom disabled via oom_adj will still be oom disabled
1108 * with this implementation.
1110 * oom_adj cannot be removed since existing userspace binaries use it.
1112 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1113 size_t count, loff_t *ppos)
1115 char buffer[PROC_NUMBUF];
1119 memset(buffer, 0, sizeof(buffer));
1120 if (count > sizeof(buffer) - 1)
1121 count = sizeof(buffer) - 1;
1122 if (copy_from_user(buffer, buf, count)) {
1127 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1130 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1131 oom_adj != OOM_DISABLE) {
1137 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1138 * value is always attainable.
1140 if (oom_adj == OOM_ADJUST_MAX)
1141 oom_adj = OOM_SCORE_ADJ_MAX;
1143 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1145 err = __set_oom_adj(file, oom_adj, true);
1147 return err < 0 ? err : count;
1150 static const struct file_operations proc_oom_adj_operations = {
1151 .read = oom_adj_read,
1152 .write = oom_adj_write,
1153 .llseek = generic_file_llseek,
1156 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1157 size_t count, loff_t *ppos)
1159 struct task_struct *task = get_proc_task(file_inode(file));
1160 char buffer[PROC_NUMBUF];
1161 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1166 oom_score_adj = task->signal->oom_score_adj;
1167 put_task_struct(task);
1168 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1169 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1172 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1173 size_t count, loff_t *ppos)
1175 char buffer[PROC_NUMBUF];
1179 memset(buffer, 0, sizeof(buffer));
1180 if (count > sizeof(buffer) - 1)
1181 count = sizeof(buffer) - 1;
1182 if (copy_from_user(buffer, buf, count)) {
1187 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1190 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1191 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1196 err = __set_oom_adj(file, oom_score_adj, false);
1198 return err < 0 ? err : count;
1201 static const struct file_operations proc_oom_score_adj_operations = {
1202 .read = oom_score_adj_read,
1203 .write = oom_score_adj_write,
1204 .llseek = default_llseek,
1207 #ifdef CONFIG_AUDITSYSCALL
1208 #define TMPBUFLEN 11
1209 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1210 size_t count, loff_t *ppos)
1212 struct inode * inode = file_inode(file);
1213 struct task_struct *task = get_proc_task(inode);
1215 char tmpbuf[TMPBUFLEN];
1219 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1220 from_kuid(file->f_cred->user_ns,
1221 audit_get_loginuid(task)));
1222 put_task_struct(task);
1223 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1226 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1227 size_t count, loff_t *ppos)
1229 struct inode * inode = file_inode(file);
1235 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1242 /* No partial writes. */
1246 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1250 /* is userspace tring to explicitly UNSET the loginuid? */
1251 if (loginuid == AUDIT_UID_UNSET) {
1252 kloginuid = INVALID_UID;
1254 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1255 if (!uid_valid(kloginuid))
1259 rv = audit_set_loginuid(kloginuid);
1265 static const struct file_operations proc_loginuid_operations = {
1266 .read = proc_loginuid_read,
1267 .write = proc_loginuid_write,
1268 .llseek = generic_file_llseek,
1271 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1272 size_t count, loff_t *ppos)
1274 struct inode * inode = file_inode(file);
1275 struct task_struct *task = get_proc_task(inode);
1277 char tmpbuf[TMPBUFLEN];
1281 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1282 audit_get_sessionid(task));
1283 put_task_struct(task);
1284 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1287 static const struct file_operations proc_sessionid_operations = {
1288 .read = proc_sessionid_read,
1289 .llseek = generic_file_llseek,
1293 #ifdef CONFIG_FAULT_INJECTION
1294 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1295 size_t count, loff_t *ppos)
1297 struct task_struct *task = get_proc_task(file_inode(file));
1298 char buffer[PROC_NUMBUF];
1304 make_it_fail = task->make_it_fail;
1305 put_task_struct(task);
1307 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1309 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1312 static ssize_t proc_fault_inject_write(struct file * file,
1313 const char __user * buf, size_t count, loff_t *ppos)
1315 struct task_struct *task;
1316 char buffer[PROC_NUMBUF];
1320 if (!capable(CAP_SYS_RESOURCE))
1322 memset(buffer, 0, sizeof(buffer));
1323 if (count > sizeof(buffer) - 1)
1324 count = sizeof(buffer) - 1;
1325 if (copy_from_user(buffer, buf, count))
1327 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1330 if (make_it_fail < 0 || make_it_fail > 1)
1333 task = get_proc_task(file_inode(file));
1336 task->make_it_fail = make_it_fail;
1337 put_task_struct(task);
1342 static const struct file_operations proc_fault_inject_operations = {
1343 .read = proc_fault_inject_read,
1344 .write = proc_fault_inject_write,
1345 .llseek = generic_file_llseek,
1348 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1349 size_t count, loff_t *ppos)
1351 struct task_struct *task;
1355 err = kstrtouint_from_user(buf, count, 0, &n);
1359 task = get_proc_task(file_inode(file));
1363 put_task_struct(task);
1368 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1369 size_t count, loff_t *ppos)
1371 struct task_struct *task;
1372 char numbuf[PROC_NUMBUF];
1375 task = get_proc_task(file_inode(file));
1378 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1379 put_task_struct(task);
1380 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1383 static const struct file_operations proc_fail_nth_operations = {
1384 .read = proc_fail_nth_read,
1385 .write = proc_fail_nth_write,
1390 #ifdef CONFIG_SCHED_DEBUG
1392 * Print out various scheduling related per-task fields:
1394 static int sched_show(struct seq_file *m, void *v)
1396 struct inode *inode = m->private;
1397 struct pid_namespace *ns = proc_pid_ns(inode);
1398 struct task_struct *p;
1400 p = get_proc_task(inode);
1403 proc_sched_show_task(p, ns, m);
1411 sched_write(struct file *file, const char __user *buf,
1412 size_t count, loff_t *offset)
1414 struct inode *inode = file_inode(file);
1415 struct task_struct *p;
1417 p = get_proc_task(inode);
1420 proc_sched_set_task(p);
1427 static int sched_open(struct inode *inode, struct file *filp)
1429 return single_open(filp, sched_show, inode);
1432 static const struct file_operations proc_pid_sched_operations = {
1435 .write = sched_write,
1436 .llseek = seq_lseek,
1437 .release = single_release,
1442 #ifdef CONFIG_SCHED_AUTOGROUP
1444 * Print out autogroup related information:
1446 static int sched_autogroup_show(struct seq_file *m, void *v)
1448 struct inode *inode = m->private;
1449 struct task_struct *p;
1451 p = get_proc_task(inode);
1454 proc_sched_autogroup_show_task(p, m);
1462 sched_autogroup_write(struct file *file, const char __user *buf,
1463 size_t count, loff_t *offset)
1465 struct inode *inode = file_inode(file);
1466 struct task_struct *p;
1467 char buffer[PROC_NUMBUF];
1471 memset(buffer, 0, sizeof(buffer));
1472 if (count > sizeof(buffer) - 1)
1473 count = sizeof(buffer) - 1;
1474 if (copy_from_user(buffer, buf, count))
1477 err = kstrtoint(strstrip(buffer), 0, &nice);
1481 p = get_proc_task(inode);
1485 err = proc_sched_autogroup_set_nice(p, nice);
1494 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1498 ret = single_open(filp, sched_autogroup_show, NULL);
1500 struct seq_file *m = filp->private_data;
1507 static const struct file_operations proc_pid_sched_autogroup_operations = {
1508 .open = sched_autogroup_open,
1510 .write = sched_autogroup_write,
1511 .llseek = seq_lseek,
1512 .release = single_release,
1515 #endif /* CONFIG_SCHED_AUTOGROUP */
1517 static ssize_t comm_write(struct file *file, const char __user *buf,
1518 size_t count, loff_t *offset)
1520 struct inode *inode = file_inode(file);
1521 struct task_struct *p;
1522 char buffer[TASK_COMM_LEN];
1523 const size_t maxlen = sizeof(buffer) - 1;
1525 memset(buffer, 0, sizeof(buffer));
1526 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1529 p = get_proc_task(inode);
1533 if (same_thread_group(current, p))
1534 set_task_comm(p, buffer);
1543 static int comm_show(struct seq_file *m, void *v)
1545 struct inode *inode = m->private;
1546 struct task_struct *p;
1548 p = get_proc_task(inode);
1552 proc_task_name(m, p, false);
1560 static int comm_open(struct inode *inode, struct file *filp)
1562 return single_open(filp, comm_show, inode);
1565 static const struct file_operations proc_pid_set_comm_operations = {
1568 .write = comm_write,
1569 .llseek = seq_lseek,
1570 .release = single_release,
1573 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1575 struct task_struct *task;
1576 struct file *exe_file;
1578 task = get_proc_task(d_inode(dentry));
1581 exe_file = get_task_exe_file(task);
1582 put_task_struct(task);
1584 *exe_path = exe_file->f_path;
1585 path_get(&exe_file->f_path);
1592 static const char *proc_pid_get_link(struct dentry *dentry,
1593 struct inode *inode,
1594 struct delayed_call *done)
1597 int error = -EACCES;
1600 return ERR_PTR(-ECHILD);
1602 /* Are we allowed to snoop on the tasks file descriptors? */
1603 if (!proc_fd_access_allowed(inode))
1606 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1610 nd_jump_link(&path);
1613 return ERR_PTR(error);
1616 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1618 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1625 pathname = d_path(path, tmp, PAGE_SIZE);
1626 len = PTR_ERR(pathname);
1627 if (IS_ERR(pathname))
1629 len = tmp + PAGE_SIZE - 1 - pathname;
1633 if (copy_to_user(buffer, pathname, len))
1636 free_page((unsigned long)tmp);
1640 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1642 int error = -EACCES;
1643 struct inode *inode = d_inode(dentry);
1646 /* Are we allowed to snoop on the tasks file descriptors? */
1647 if (!proc_fd_access_allowed(inode))
1650 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1654 error = do_proc_readlink(&path, buffer, buflen);
1660 const struct inode_operations proc_pid_link_inode_operations = {
1661 .readlink = proc_pid_readlink,
1662 .get_link = proc_pid_get_link,
1663 .setattr = proc_setattr,
1667 /* building an inode */
1669 void task_dump_owner(struct task_struct *task, umode_t mode,
1670 kuid_t *ruid, kgid_t *rgid)
1672 /* Depending on the state of dumpable compute who should own a
1673 * proc file for a task.
1675 const struct cred *cred;
1679 if (unlikely(task->flags & PF_KTHREAD)) {
1680 *ruid = GLOBAL_ROOT_UID;
1681 *rgid = GLOBAL_ROOT_GID;
1685 /* Default to the tasks effective ownership */
1687 cred = __task_cred(task);
1693 * Before the /proc/pid/status file was created the only way to read
1694 * the effective uid of a /process was to stat /proc/pid. Reading
1695 * /proc/pid/status is slow enough that procps and other packages
1696 * kept stating /proc/pid. To keep the rules in /proc simple I have
1697 * made this apply to all per process world readable and executable
1700 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1701 struct mm_struct *mm;
1704 /* Make non-dumpable tasks owned by some root */
1706 if (get_dumpable(mm) != SUID_DUMP_USER) {
1707 struct user_namespace *user_ns = mm->user_ns;
1709 uid = make_kuid(user_ns, 0);
1710 if (!uid_valid(uid))
1711 uid = GLOBAL_ROOT_UID;
1713 gid = make_kgid(user_ns, 0);
1714 if (!gid_valid(gid))
1715 gid = GLOBAL_ROOT_GID;
1718 uid = GLOBAL_ROOT_UID;
1719 gid = GLOBAL_ROOT_GID;
1727 struct inode *proc_pid_make_inode(struct super_block * sb,
1728 struct task_struct *task, umode_t mode)
1730 struct inode * inode;
1731 struct proc_inode *ei;
1733 /* We need a new inode */
1735 inode = new_inode(sb);
1741 inode->i_mode = mode;
1742 inode->i_ino = get_next_ino();
1743 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1744 inode->i_op = &proc_def_inode_operations;
1747 * grab the reference to task.
1749 ei->pid = get_task_pid(task, PIDTYPE_PID);
1753 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1754 security_task_to_inode(task, inode);
1764 int pid_getattr(const struct path *path, struct kstat *stat,
1765 u32 request_mask, unsigned int query_flags)
1767 struct inode *inode = d_inode(path->dentry);
1768 struct pid_namespace *pid = proc_pid_ns(inode);
1769 struct task_struct *task;
1771 generic_fillattr(inode, stat);
1773 stat->uid = GLOBAL_ROOT_UID;
1774 stat->gid = GLOBAL_ROOT_GID;
1776 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1778 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1781 * This doesn't prevent learning whether PID exists,
1782 * it only makes getattr() consistent with readdir().
1786 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1795 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1797 void pid_update_inode(struct task_struct *task, struct inode *inode)
1799 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1801 inode->i_mode &= ~(S_ISUID | S_ISGID);
1802 security_task_to_inode(task, inode);
1806 * Rewrite the inode's ownerships here because the owning task may have
1807 * performed a setuid(), etc.
1810 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1812 struct inode *inode;
1813 struct task_struct *task;
1815 if (flags & LOOKUP_RCU)
1818 inode = d_inode(dentry);
1819 task = get_proc_task(inode);
1822 pid_update_inode(task, inode);
1823 put_task_struct(task);
1829 static inline bool proc_inode_is_dead(struct inode *inode)
1831 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1834 int pid_delete_dentry(const struct dentry *dentry)
1836 /* Is the task we represent dead?
1837 * If so, then don't put the dentry on the lru list,
1838 * kill it immediately.
1840 return proc_inode_is_dead(d_inode(dentry));
1843 const struct dentry_operations pid_dentry_operations =
1845 .d_revalidate = pid_revalidate,
1846 .d_delete = pid_delete_dentry,
1852 * Fill a directory entry.
1854 * If possible create the dcache entry and derive our inode number and
1855 * file type from dcache entry.
1857 * Since all of the proc inode numbers are dynamically generated, the inode
1858 * numbers do not exist until the inode is cache. This means creating the
1859 * the dcache entry in readdir is necessary to keep the inode numbers
1860 * reported by readdir in sync with the inode numbers reported
1863 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1864 const char *name, unsigned int len,
1865 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1867 struct dentry *child, *dir = file->f_path.dentry;
1868 struct qstr qname = QSTR_INIT(name, len);
1869 struct inode *inode;
1870 unsigned type = DT_UNKNOWN;
1873 child = d_hash_and_lookup(dir, &qname);
1875 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1876 child = d_alloc_parallel(dir, &qname, &wq);
1878 goto end_instantiate;
1879 if (d_in_lookup(child)) {
1881 res = instantiate(child, task, ptr);
1882 d_lookup_done(child);
1883 if (unlikely(res)) {
1887 goto end_instantiate;
1891 inode = d_inode(child);
1893 type = inode->i_mode >> 12;
1896 return dir_emit(ctx, name, len, ino, type);
1900 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1901 * which represent vma start and end addresses.
1903 static int dname_to_vma_addr(struct dentry *dentry,
1904 unsigned long *start, unsigned long *end)
1906 const char *str = dentry->d_name.name;
1907 unsigned long long sval, eval;
1910 if (str[0] == '0' && str[1] != '-')
1912 len = _parse_integer(str, 16, &sval);
1913 if (len & KSTRTOX_OVERFLOW)
1915 if (sval != (unsigned long)sval)
1923 if (str[0] == '0' && str[1])
1925 len = _parse_integer(str, 16, &eval);
1926 if (len & KSTRTOX_OVERFLOW)
1928 if (eval != (unsigned long)eval)
1941 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1943 unsigned long vm_start, vm_end;
1944 bool exact_vma_exists = false;
1945 struct mm_struct *mm = NULL;
1946 struct task_struct *task;
1947 struct inode *inode;
1950 if (flags & LOOKUP_RCU)
1953 inode = d_inode(dentry);
1954 task = get_proc_task(inode);
1958 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1959 if (IS_ERR_OR_NULL(mm))
1962 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1963 status = down_read_killable(&mm->mmap_sem);
1965 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1967 up_read(&mm->mmap_sem);
1973 if (exact_vma_exists) {
1974 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1976 security_task_to_inode(task, inode);
1981 put_task_struct(task);
1987 static const struct dentry_operations tid_map_files_dentry_operations = {
1988 .d_revalidate = map_files_d_revalidate,
1989 .d_delete = pid_delete_dentry,
1992 static int map_files_get_link(struct dentry *dentry, struct path *path)
1994 unsigned long vm_start, vm_end;
1995 struct vm_area_struct *vma;
1996 struct task_struct *task;
1997 struct mm_struct *mm;
2001 task = get_proc_task(d_inode(dentry));
2005 mm = get_task_mm(task);
2006 put_task_struct(task);
2010 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2014 rc = down_read_killable(&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 result = ERR_PTR(-EINTR);
2115 if (down_read_killable(&mm->mmap_sem))
2118 result = ERR_PTR(-ENOENT);
2119 vma = find_exact_vma(mm, vm_start, vm_end);
2124 result = proc_map_files_instantiate(dentry, task,
2125 (void *)(unsigned long)vma->vm_file->f_mode);
2128 up_read(&mm->mmap_sem);
2132 put_task_struct(task);
2137 static const struct inode_operations proc_map_files_inode_operations = {
2138 .lookup = proc_map_files_lookup,
2139 .permission = proc_fd_permission,
2140 .setattr = proc_setattr,
2144 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2146 struct vm_area_struct *vma;
2147 struct task_struct *task;
2148 struct mm_struct *mm;
2149 unsigned long nr_files, pos, i;
2150 struct flex_array *fa = NULL;
2151 struct map_files_info info;
2152 struct map_files_info *p;
2156 task = get_proc_task(file_inode(file));
2161 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2165 if (!dir_emit_dots(file, ctx))
2168 mm = get_task_mm(task);
2172 ret = down_read_killable(&mm->mmap_sem);
2181 * We need two passes here:
2183 * 1) Collect vmas of mapped files with mmap_sem taken
2184 * 2) Release mmap_sem and instantiate entries
2186 * otherwise we get lockdep complained, since filldir()
2187 * routine might require mmap_sem taken in might_fault().
2190 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2191 if (vma->vm_file && ++pos > ctx->pos)
2196 fa = flex_array_alloc(sizeof(info), nr_files,
2198 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2202 flex_array_free(fa);
2203 up_read(&mm->mmap_sem);
2207 for (i = 0, vma = mm->mmap, pos = 2; vma;
2208 vma = vma->vm_next) {
2211 if (++pos <= ctx->pos)
2214 info.start = vma->vm_start;
2215 info.end = vma->vm_end;
2216 info.mode = vma->vm_file->f_mode;
2217 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2221 up_read(&mm->mmap_sem);
2224 for (i = 0; i < nr_files; i++) {
2225 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2228 p = flex_array_get(fa, i);
2229 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2230 if (!proc_fill_cache(file, ctx,
2232 proc_map_files_instantiate,
2234 (void *)(unsigned long)p->mode))
2239 flex_array_free(fa);
2242 put_task_struct(task);
2247 static const struct file_operations proc_map_files_operations = {
2248 .read = generic_read_dir,
2249 .iterate_shared = proc_map_files_readdir,
2250 .llseek = generic_file_llseek,
2253 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2254 struct timers_private {
2256 struct task_struct *task;
2257 struct sighand_struct *sighand;
2258 struct pid_namespace *ns;
2259 unsigned long flags;
2262 static void *timers_start(struct seq_file *m, loff_t *pos)
2264 struct timers_private *tp = m->private;
2266 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2268 return ERR_PTR(-ESRCH);
2270 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2272 return ERR_PTR(-ESRCH);
2274 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2277 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2279 struct timers_private *tp = m->private;
2280 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2283 static void timers_stop(struct seq_file *m, void *v)
2285 struct timers_private *tp = m->private;
2288 unlock_task_sighand(tp->task, &tp->flags);
2293 put_task_struct(tp->task);
2298 static int show_timer(struct seq_file *m, void *v)
2300 struct k_itimer *timer;
2301 struct timers_private *tp = m->private;
2303 static const char * const nstr[] = {
2304 [SIGEV_SIGNAL] = "signal",
2305 [SIGEV_NONE] = "none",
2306 [SIGEV_THREAD] = "thread",
2309 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2310 notify = timer->it_sigev_notify;
2312 seq_printf(m, "ID: %d\n", timer->it_id);
2313 seq_printf(m, "signal: %d/%px\n",
2314 timer->sigq->info.si_signo,
2315 timer->sigq->info.si_value.sival_ptr);
2316 seq_printf(m, "notify: %s/%s.%d\n",
2317 nstr[notify & ~SIGEV_THREAD_ID],
2318 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2319 pid_nr_ns(timer->it_pid, tp->ns));
2320 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2325 static const struct seq_operations proc_timers_seq_ops = {
2326 .start = timers_start,
2327 .next = timers_next,
2328 .stop = timers_stop,
2332 static int proc_timers_open(struct inode *inode, struct file *file)
2334 struct timers_private *tp;
2336 tp = __seq_open_private(file, &proc_timers_seq_ops,
2337 sizeof(struct timers_private));
2341 tp->pid = proc_pid(inode);
2342 tp->ns = proc_pid_ns(inode);
2346 static const struct file_operations proc_timers_operations = {
2347 .open = proc_timers_open,
2349 .llseek = seq_lseek,
2350 .release = seq_release_private,
2354 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2355 size_t count, loff_t *offset)
2357 struct inode *inode = file_inode(file);
2358 struct task_struct *p;
2362 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2366 p = get_proc_task(inode);
2371 if (!capable(CAP_SYS_NICE)) {
2376 err = security_task_setscheduler(p);
2385 p->timer_slack_ns = p->default_timer_slack_ns;
2387 p->timer_slack_ns = slack_ns;
2396 static int timerslack_ns_show(struct seq_file *m, void *v)
2398 struct inode *inode = m->private;
2399 struct task_struct *p;
2402 p = get_proc_task(inode);
2408 if (!capable(CAP_SYS_NICE)) {
2412 err = security_task_getscheduler(p);
2418 seq_printf(m, "%llu\n", p->timer_slack_ns);
2427 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2429 return single_open(filp, timerslack_ns_show, inode);
2432 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2433 .open = timerslack_ns_open,
2435 .write = timerslack_ns_write,
2436 .llseek = seq_lseek,
2437 .release = single_release,
2440 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2441 struct task_struct *task, const void *ptr)
2443 const struct pid_entry *p = ptr;
2444 struct inode *inode;
2445 struct proc_inode *ei;
2447 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2449 return ERR_PTR(-ENOENT);
2452 if (S_ISDIR(inode->i_mode))
2453 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2455 inode->i_op = p->iop;
2457 inode->i_fop = p->fop;
2459 pid_update_inode(task, inode);
2460 d_set_d_op(dentry, &pid_dentry_operations);
2461 return d_splice_alias(inode, dentry);
2464 static struct dentry *proc_pident_lookup(struct inode *dir,
2465 struct dentry *dentry,
2466 const struct pid_entry *ents,
2469 struct task_struct *task = get_proc_task(dir);
2470 const struct pid_entry *p, *last;
2471 struct dentry *res = ERR_PTR(-ENOENT);
2477 * Yes, it does not scale. And it should not. Don't add
2478 * new entries into /proc/<tgid>/ without very good reasons.
2480 last = &ents[nents];
2481 for (p = ents; p < last; p++) {
2482 if (p->len != dentry->d_name.len)
2484 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2485 res = proc_pident_instantiate(dentry, task, p);
2489 put_task_struct(task);
2494 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2495 const struct pid_entry *ents, unsigned int nents)
2497 struct task_struct *task = get_proc_task(file_inode(file));
2498 const struct pid_entry *p;
2503 if (!dir_emit_dots(file, ctx))
2506 if (ctx->pos >= nents + 2)
2509 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2510 if (!proc_fill_cache(file, ctx, p->name, p->len,
2511 proc_pident_instantiate, task, p))
2516 put_task_struct(task);
2520 #ifdef CONFIG_SECURITY
2521 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2522 size_t count, loff_t *ppos)
2524 struct inode * inode = file_inode(file);
2527 struct task_struct *task = get_proc_task(inode);
2532 length = security_getprocattr(task,
2533 (char*)file->f_path.dentry->d_name.name,
2535 put_task_struct(task);
2537 length = simple_read_from_buffer(buf, count, ppos, p, length);
2542 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2543 size_t count, loff_t *ppos)
2545 struct inode * inode = file_inode(file);
2546 struct task_struct *task;
2551 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2556 /* A task may only write its own attributes. */
2557 if (current != task) {
2561 /* Prevent changes to overridden credentials. */
2562 if (current_cred() != current_real_cred()) {
2568 if (count > PAGE_SIZE)
2571 /* No partial writes. */
2575 page = memdup_user(buf, count);
2581 /* Guard against adverse ptrace interaction */
2582 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2586 rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
2587 mutex_unlock(¤t->signal->cred_guard_mutex);
2594 static const struct file_operations proc_pid_attr_operations = {
2595 .read = proc_pid_attr_read,
2596 .write = proc_pid_attr_write,
2597 .llseek = generic_file_llseek,
2600 static const struct pid_entry attr_dir_stuff[] = {
2601 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2602 REG("prev", S_IRUGO, proc_pid_attr_operations),
2603 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2604 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2605 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2606 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2609 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2611 return proc_pident_readdir(file, ctx,
2612 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2615 static const struct file_operations proc_attr_dir_operations = {
2616 .read = generic_read_dir,
2617 .iterate_shared = proc_attr_dir_readdir,
2618 .llseek = generic_file_llseek,
2621 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2622 struct dentry *dentry, unsigned int flags)
2624 return proc_pident_lookup(dir, dentry,
2625 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2628 static const struct inode_operations proc_attr_dir_inode_operations = {
2629 .lookup = proc_attr_dir_lookup,
2630 .getattr = pid_getattr,
2631 .setattr = proc_setattr,
2636 #ifdef CONFIG_ELF_CORE
2637 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2638 size_t count, loff_t *ppos)
2640 struct task_struct *task = get_proc_task(file_inode(file));
2641 struct mm_struct *mm;
2642 char buffer[PROC_NUMBUF];
2650 mm = get_task_mm(task);
2652 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2653 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2654 MMF_DUMP_FILTER_SHIFT));
2656 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2659 put_task_struct(task);
2664 static ssize_t proc_coredump_filter_write(struct file *file,
2665 const char __user *buf,
2669 struct task_struct *task;
2670 struct mm_struct *mm;
2676 ret = kstrtouint_from_user(buf, count, 0, &val);
2681 task = get_proc_task(file_inode(file));
2685 mm = get_task_mm(task);
2690 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2692 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2694 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2699 put_task_struct(task);
2706 static const struct file_operations proc_coredump_filter_operations = {
2707 .read = proc_coredump_filter_read,
2708 .write = proc_coredump_filter_write,
2709 .llseek = generic_file_llseek,
2713 #ifdef CONFIG_TASK_IO_ACCOUNTING
2714 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2716 struct task_io_accounting acct = task->ioac;
2717 unsigned long flags;
2720 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2724 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2729 if (whole && lock_task_sighand(task, &flags)) {
2730 struct task_struct *t = task;
2732 task_io_accounting_add(&acct, &task->signal->ioac);
2733 while_each_thread(task, t)
2734 task_io_accounting_add(&acct, &t->ioac);
2736 unlock_task_sighand(task, &flags);
2743 "read_bytes: %llu\n"
2744 "write_bytes: %llu\n"
2745 "cancelled_write_bytes: %llu\n",
2746 (unsigned long long)acct.rchar,
2747 (unsigned long long)acct.wchar,
2748 (unsigned long long)acct.syscr,
2749 (unsigned long long)acct.syscw,
2750 (unsigned long long)acct.read_bytes,
2751 (unsigned long long)acct.write_bytes,
2752 (unsigned long long)acct.cancelled_write_bytes);
2756 mutex_unlock(&task->signal->cred_guard_mutex);
2760 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2761 struct pid *pid, struct task_struct *task)
2763 return do_io_accounting(task, m, 0);
2766 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2767 struct pid *pid, struct task_struct *task)
2769 return do_io_accounting(task, m, 1);
2771 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2773 #ifdef CONFIG_USER_NS
2774 static int proc_id_map_open(struct inode *inode, struct file *file,
2775 const struct seq_operations *seq_ops)
2777 struct user_namespace *ns = NULL;
2778 struct task_struct *task;
2779 struct seq_file *seq;
2782 task = get_proc_task(inode);
2785 ns = get_user_ns(task_cred_xxx(task, user_ns));
2787 put_task_struct(task);
2792 ret = seq_open(file, seq_ops);
2796 seq = file->private_data;
2806 static int proc_id_map_release(struct inode *inode, struct file *file)
2808 struct seq_file *seq = file->private_data;
2809 struct user_namespace *ns = seq->private;
2811 return seq_release(inode, file);
2814 static int proc_uid_map_open(struct inode *inode, struct file *file)
2816 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2819 static int proc_gid_map_open(struct inode *inode, struct file *file)
2821 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2824 static int proc_projid_map_open(struct inode *inode, struct file *file)
2826 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2829 static const struct file_operations proc_uid_map_operations = {
2830 .open = proc_uid_map_open,
2831 .write = proc_uid_map_write,
2833 .llseek = seq_lseek,
2834 .release = proc_id_map_release,
2837 static const struct file_operations proc_gid_map_operations = {
2838 .open = proc_gid_map_open,
2839 .write = proc_gid_map_write,
2841 .llseek = seq_lseek,
2842 .release = proc_id_map_release,
2845 static const struct file_operations proc_projid_map_operations = {
2846 .open = proc_projid_map_open,
2847 .write = proc_projid_map_write,
2849 .llseek = seq_lseek,
2850 .release = proc_id_map_release,
2853 static int proc_setgroups_open(struct inode *inode, struct file *file)
2855 struct user_namespace *ns = NULL;
2856 struct task_struct *task;
2860 task = get_proc_task(inode);
2863 ns = get_user_ns(task_cred_xxx(task, user_ns));
2865 put_task_struct(task);
2870 if (file->f_mode & FMODE_WRITE) {
2872 if (!ns_capable(ns, CAP_SYS_ADMIN))
2876 ret = single_open(file, &proc_setgroups_show, ns);
2887 static int proc_setgroups_release(struct inode *inode, struct file *file)
2889 struct seq_file *seq = file->private_data;
2890 struct user_namespace *ns = seq->private;
2891 int ret = single_release(inode, file);
2896 static const struct file_operations proc_setgroups_operations = {
2897 .open = proc_setgroups_open,
2898 .write = proc_setgroups_write,
2900 .llseek = seq_lseek,
2901 .release = proc_setgroups_release,
2903 #endif /* CONFIG_USER_NS */
2905 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2906 struct pid *pid, struct task_struct *task)
2908 int err = lock_trace(task);
2910 seq_printf(m, "%08x\n", task->personality);
2916 #ifdef CONFIG_LIVEPATCH
2917 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2918 struct pid *pid, struct task_struct *task)
2920 seq_printf(m, "%d\n", task->patch_state);
2923 #endif /* CONFIG_LIVEPATCH */
2928 static const struct file_operations proc_task_operations;
2929 static const struct inode_operations proc_task_inode_operations;
2931 static const struct pid_entry tgid_base_stuff[] = {
2932 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2933 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2934 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2935 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2936 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2938 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2940 REG("environ", S_IRUSR, proc_environ_operations),
2941 REG("auxv", S_IRUSR, proc_auxv_operations),
2942 ONE("status", S_IRUGO, proc_pid_status),
2943 ONE("personality", S_IRUSR, proc_pid_personality),
2944 ONE("limits", S_IRUGO, proc_pid_limits),
2945 #ifdef CONFIG_SCHED_DEBUG
2946 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2948 #ifdef CONFIG_SCHED_AUTOGROUP
2949 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2951 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2952 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2953 ONE("syscall", S_IRUSR, proc_pid_syscall),
2955 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2956 ONE("stat", S_IRUGO, proc_tgid_stat),
2957 ONE("statm", S_IRUGO, proc_pid_statm),
2958 REG("maps", S_IRUGO, proc_pid_maps_operations),
2960 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2962 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2963 LNK("cwd", proc_cwd_link),
2964 LNK("root", proc_root_link),
2965 LNK("exe", proc_exe_link),
2966 REG("mounts", S_IRUGO, proc_mounts_operations),
2967 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2968 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2969 #ifdef CONFIG_PROC_PAGE_MONITOR
2970 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2971 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2972 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2973 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2975 #ifdef CONFIG_SECURITY
2976 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2978 #ifdef CONFIG_KALLSYMS
2979 ONE("wchan", S_IRUGO, proc_pid_wchan),
2981 #ifdef CONFIG_STACKTRACE
2982 ONE("stack", S_IRUSR, proc_pid_stack),
2984 #ifdef CONFIG_SCHED_INFO
2985 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2987 #ifdef CONFIG_LATENCYTOP
2988 REG("latency", S_IRUGO, proc_lstats_operations),
2990 #ifdef CONFIG_PROC_PID_CPUSET
2991 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2993 #ifdef CONFIG_CGROUPS
2994 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2996 ONE("oom_score", S_IRUGO, proc_oom_score),
2997 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2998 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2999 #ifdef CONFIG_AUDITSYSCALL
3000 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3001 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3003 #ifdef CONFIG_FAULT_INJECTION
3004 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3005 REG("fail-nth", 0644, proc_fail_nth_operations),
3007 #ifdef CONFIG_ELF_CORE
3008 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3010 #ifdef CONFIG_TASK_IO_ACCOUNTING
3011 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3013 #ifdef CONFIG_USER_NS
3014 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3015 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3016 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3017 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3019 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3020 REG("timers", S_IRUGO, proc_timers_operations),
3022 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3023 #ifdef CONFIG_LIVEPATCH
3024 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3028 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3030 return proc_pident_readdir(file, ctx,
3031 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3034 static const struct file_operations proc_tgid_base_operations = {
3035 .read = generic_read_dir,
3036 .iterate_shared = proc_tgid_base_readdir,
3037 .llseek = generic_file_llseek,
3040 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3042 return proc_pident_lookup(dir, dentry,
3043 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3046 static const struct inode_operations proc_tgid_base_inode_operations = {
3047 .lookup = proc_tgid_base_lookup,
3048 .getattr = pid_getattr,
3049 .setattr = proc_setattr,
3050 .permission = proc_pid_permission,
3053 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3055 struct dentry *dentry, *leader, *dir;
3060 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3061 /* no ->d_hash() rejects on procfs */
3062 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3064 d_invalidate(dentry);
3072 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3073 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3078 name.len = strlen(name.name);
3079 dir = d_hash_and_lookup(leader, &name);
3081 goto out_put_leader;
3084 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3085 dentry = d_hash_and_lookup(dir, &name);
3087 d_invalidate(dentry);
3099 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3100 * @task: task that should be flushed.
3102 * When flushing dentries from proc, one needs to flush them from global
3103 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3104 * in. This call is supposed to do all of this job.
3106 * Looks in the dcache for
3108 * /proc/@tgid/task/@pid
3109 * if either directory is present flushes it and all of it'ts children
3112 * It is safe and reasonable to cache /proc entries for a task until
3113 * that task exits. After that they just clog up the dcache with
3114 * useless entries, possibly causing useful dcache entries to be
3115 * flushed instead. This routine is proved to flush those useless
3116 * dcache entries at process exit time.
3118 * NOTE: This routine is just an optimization so it does not guarantee
3119 * that no dcache entries will exist at process exit time it
3120 * just makes it very unlikely that any will persist.
3123 void proc_flush_task(struct task_struct *task)
3126 struct pid *pid, *tgid;
3129 pid = task_pid(task);
3130 tgid = task_tgid(task);
3132 for (i = 0; i <= pid->level; i++) {
3133 upid = &pid->numbers[i];
3134 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3135 tgid->numbers[i].nr);
3139 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3140 struct task_struct *task, const void *ptr)
3142 struct inode *inode;
3144 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3146 return ERR_PTR(-ENOENT);
3148 inode->i_op = &proc_tgid_base_inode_operations;
3149 inode->i_fop = &proc_tgid_base_operations;
3150 inode->i_flags|=S_IMMUTABLE;
3152 set_nlink(inode, nlink_tgid);
3153 pid_update_inode(task, inode);
3155 d_set_d_op(dentry, &pid_dentry_operations);
3156 return d_splice_alias(inode, dentry);
3159 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3161 struct task_struct *task;
3163 struct pid_namespace *ns;
3164 struct dentry *result = ERR_PTR(-ENOENT);
3166 tgid = name_to_int(&dentry->d_name);
3170 ns = dentry->d_sb->s_fs_info;
3172 task = find_task_by_pid_ns(tgid, ns);
3174 get_task_struct(task);
3179 result = proc_pid_instantiate(dentry, task, NULL);
3180 put_task_struct(task);
3186 * Find the first task with tgid >= tgid
3191 struct task_struct *task;
3193 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3198 put_task_struct(iter.task);
3202 pid = find_ge_pid(iter.tgid, ns);
3204 iter.tgid = pid_nr_ns(pid, ns);
3205 iter.task = pid_task(pid, PIDTYPE_PID);
3206 /* What we to know is if the pid we have find is the
3207 * pid of a thread_group_leader. Testing for task
3208 * being a thread_group_leader is the obvious thing
3209 * todo but there is a window when it fails, due to
3210 * the pid transfer logic in de_thread.
3212 * So we perform the straight forward test of seeing
3213 * if the pid we have found is the pid of a thread
3214 * group leader, and don't worry if the task we have
3215 * found doesn't happen to be a thread group leader.
3216 * As we don't care in the case of readdir.
3218 if (!iter.task || !has_group_leader_pid(iter.task)) {
3222 get_task_struct(iter.task);
3228 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3230 /* for the /proc/ directory itself, after non-process stuff has been done */
3231 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3233 struct tgid_iter iter;
3234 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3235 loff_t pos = ctx->pos;
3237 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3240 if (pos == TGID_OFFSET - 2) {
3241 struct inode *inode = d_inode(ns->proc_self);
3242 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3244 ctx->pos = pos = pos + 1;
3246 if (pos == TGID_OFFSET - 1) {
3247 struct inode *inode = d_inode(ns->proc_thread_self);
3248 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3250 ctx->pos = pos = pos + 1;
3252 iter.tgid = pos - TGID_OFFSET;
3254 for (iter = next_tgid(ns, iter);
3256 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3261 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3264 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3265 ctx->pos = iter.tgid + TGID_OFFSET;
3266 if (!proc_fill_cache(file, ctx, name, len,
3267 proc_pid_instantiate, iter.task, NULL)) {
3268 put_task_struct(iter.task);
3272 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3277 * proc_tid_comm_permission is a special permission function exclusively
3278 * used for the node /proc/<pid>/task/<tid>/comm.
3279 * It bypasses generic permission checks in the case where a task of the same
3280 * task group attempts to access the node.
3281 * The rationale behind this is that glibc and bionic access this node for
3282 * cross thread naming (pthread_set/getname_np(!self)). However, if
3283 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3284 * which locks out the cross thread naming implementation.
3285 * This function makes sure that the node is always accessible for members of
3286 * same thread group.
3288 static int proc_tid_comm_permission(struct inode *inode, int mask)
3290 bool is_same_tgroup;
3291 struct task_struct *task;
3293 task = get_proc_task(inode);
3296 is_same_tgroup = same_thread_group(current, task);
3297 put_task_struct(task);
3299 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3300 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3301 * read or written by the members of the corresponding
3307 return generic_permission(inode, mask);
3310 static const struct inode_operations proc_tid_comm_inode_operations = {
3311 .permission = proc_tid_comm_permission,
3317 static const struct pid_entry tid_base_stuff[] = {
3318 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3319 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3320 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3322 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3324 REG("environ", S_IRUSR, proc_environ_operations),
3325 REG("auxv", S_IRUSR, proc_auxv_operations),
3326 ONE("status", S_IRUGO, proc_pid_status),
3327 ONE("personality", S_IRUSR, proc_pid_personality),
3328 ONE("limits", S_IRUGO, proc_pid_limits),
3329 #ifdef CONFIG_SCHED_DEBUG
3330 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3332 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3333 &proc_tid_comm_inode_operations,
3334 &proc_pid_set_comm_operations, {}),
3335 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3336 ONE("syscall", S_IRUSR, proc_pid_syscall),
3338 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3339 ONE("stat", S_IRUGO, proc_tid_stat),
3340 ONE("statm", S_IRUGO, proc_pid_statm),
3341 REG("maps", S_IRUGO, proc_pid_maps_operations),
3342 #ifdef CONFIG_PROC_CHILDREN
3343 REG("children", S_IRUGO, proc_tid_children_operations),
3346 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3348 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3349 LNK("cwd", proc_cwd_link),
3350 LNK("root", proc_root_link),
3351 LNK("exe", proc_exe_link),
3352 REG("mounts", S_IRUGO, proc_mounts_operations),
3353 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3354 #ifdef CONFIG_PROC_PAGE_MONITOR
3355 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3356 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3357 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3358 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3360 #ifdef CONFIG_SECURITY
3361 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3363 #ifdef CONFIG_KALLSYMS
3364 ONE("wchan", S_IRUGO, proc_pid_wchan),
3366 #ifdef CONFIG_STACKTRACE
3367 ONE("stack", S_IRUSR, proc_pid_stack),
3369 #ifdef CONFIG_SCHED_INFO
3370 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3372 #ifdef CONFIG_LATENCYTOP
3373 REG("latency", S_IRUGO, proc_lstats_operations),
3375 #ifdef CONFIG_PROC_PID_CPUSET
3376 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3378 #ifdef CONFIG_CGROUPS
3379 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3381 ONE("oom_score", S_IRUGO, proc_oom_score),
3382 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3383 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3384 #ifdef CONFIG_AUDITSYSCALL
3385 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3386 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3388 #ifdef CONFIG_FAULT_INJECTION
3389 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3390 REG("fail-nth", 0644, proc_fail_nth_operations),
3392 #ifdef CONFIG_TASK_IO_ACCOUNTING
3393 ONE("io", S_IRUSR, proc_tid_io_accounting),
3395 #ifdef CONFIG_USER_NS
3396 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3397 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3398 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3399 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3401 #ifdef CONFIG_LIVEPATCH
3402 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3406 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3408 return proc_pident_readdir(file, ctx,
3409 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3412 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3414 return proc_pident_lookup(dir, dentry,
3415 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3418 static const struct file_operations proc_tid_base_operations = {
3419 .read = generic_read_dir,
3420 .iterate_shared = proc_tid_base_readdir,
3421 .llseek = generic_file_llseek,
3424 static const struct inode_operations proc_tid_base_inode_operations = {
3425 .lookup = proc_tid_base_lookup,
3426 .getattr = pid_getattr,
3427 .setattr = proc_setattr,
3430 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3431 struct task_struct *task, const void *ptr)
3433 struct inode *inode;
3434 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3436 return ERR_PTR(-ENOENT);
3438 inode->i_op = &proc_tid_base_inode_operations;
3439 inode->i_fop = &proc_tid_base_operations;
3440 inode->i_flags |= S_IMMUTABLE;
3442 set_nlink(inode, nlink_tid);
3443 pid_update_inode(task, inode);
3445 d_set_d_op(dentry, &pid_dentry_operations);
3446 return d_splice_alias(inode, dentry);
3449 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3451 struct task_struct *task;
3452 struct task_struct *leader = get_proc_task(dir);
3454 struct pid_namespace *ns;
3455 struct dentry *result = ERR_PTR(-ENOENT);
3460 tid = name_to_int(&dentry->d_name);
3464 ns = dentry->d_sb->s_fs_info;
3466 task = find_task_by_pid_ns(tid, ns);
3468 get_task_struct(task);
3472 if (!same_thread_group(leader, task))
3475 result = proc_task_instantiate(dentry, task, NULL);
3477 put_task_struct(task);
3479 put_task_struct(leader);
3485 * Find the first tid of a thread group to return to user space.
3487 * Usually this is just the thread group leader, but if the users
3488 * buffer was too small or there was a seek into the middle of the
3489 * directory we have more work todo.
3491 * In the case of a short read we start with find_task_by_pid.
3493 * In the case of a seek we start with the leader and walk nr
3496 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3497 struct pid_namespace *ns)
3499 struct task_struct *pos, *task;
3500 unsigned long nr = f_pos;
3502 if (nr != f_pos) /* 32bit overflow? */
3506 task = pid_task(pid, PIDTYPE_PID);
3510 /* Attempt to start with the tid of a thread */
3512 pos = find_task_by_pid_ns(tid, ns);
3513 if (pos && same_thread_group(pos, task))
3517 /* If nr exceeds the number of threads there is nothing todo */
3518 if (nr >= get_nr_threads(task))
3521 /* If we haven't found our starting place yet start
3522 * with the leader and walk nr threads forward.
3524 pos = task = task->group_leader;
3528 } while_each_thread(task, pos);
3533 get_task_struct(pos);
3540 * Find the next thread in the thread list.
3541 * Return NULL if there is an error or no next thread.
3543 * The reference to the input task_struct is released.
3545 static struct task_struct *next_tid(struct task_struct *start)
3547 struct task_struct *pos = NULL;
3549 if (pid_alive(start)) {
3550 pos = next_thread(start);
3551 if (thread_group_leader(pos))
3554 get_task_struct(pos);
3557 put_task_struct(start);
3561 /* for the /proc/TGID/task/ directories */
3562 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3564 struct inode *inode = file_inode(file);
3565 struct task_struct *task;
3566 struct pid_namespace *ns;
3569 if (proc_inode_is_dead(inode))
3572 if (!dir_emit_dots(file, ctx))
3575 /* f_version caches the tgid value that the last readdir call couldn't
3576 * return. lseek aka telldir automagically resets f_version to 0.
3578 ns = proc_pid_ns(inode);
3579 tid = (int)file->f_version;
3580 file->f_version = 0;
3581 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3583 task = next_tid(task), ctx->pos++) {
3586 tid = task_pid_nr_ns(task, ns);
3587 len = snprintf(name, sizeof(name), "%u", tid);
3588 if (!proc_fill_cache(file, ctx, name, len,
3589 proc_task_instantiate, task, NULL)) {
3590 /* returning this tgid failed, save it as the first
3591 * pid for the next readir call */
3592 file->f_version = (u64)tid;
3593 put_task_struct(task);
3601 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3602 u32 request_mask, unsigned int query_flags)
3604 struct inode *inode = d_inode(path->dentry);
3605 struct task_struct *p = get_proc_task(inode);
3606 generic_fillattr(inode, stat);
3609 stat->nlink += get_nr_threads(p);
3616 static const struct inode_operations proc_task_inode_operations = {
3617 .lookup = proc_task_lookup,
3618 .getattr = proc_task_getattr,
3619 .setattr = proc_setattr,
3620 .permission = proc_pid_permission,
3623 static const struct file_operations proc_task_operations = {
3624 .read = generic_read_dir,
3625 .iterate_shared = proc_task_readdir,
3626 .llseek = generic_file_llseek,
3629 void __init set_proc_pid_nlink(void)
3631 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3632 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));