1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
45 * Smaps information related to shared, private, clean and dirty pages.
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.h"
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tid __ro_after_init;
114 static u8 nlink_tgid __ro_after_init;
120 const struct inode_operations *iop;
121 const struct file_operations *fop;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
134 #define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136 #define LNK(NAME, get_link) \
137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
138 &proc_pid_link_inode_operations, NULL, \
139 { .proc_get_link = get_link } )
140 #define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142 #define ONE(NAME, MODE, show) \
143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
145 { .proc_show = show } )
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
589 for (i = 0; i < RLIM_NLIMITS; i++) {
590 if (rlim[i].rlim_cur == RLIM_INFINITY)
591 seq_printf(m, "%-25s %-20s ",
592 lnames[i].name, "unlimited");
594 seq_printf(m, "%-25s %-20lu ",
595 lnames[i].name, rlim[i].rlim_cur);
597 if (rlim[i].rlim_max == RLIM_INFINITY)
598 seq_printf(m, "%-20s ", "unlimited");
600 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
603 seq_printf(m, "%-10s\n", lnames[i].unit);
611 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
612 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
613 struct pid *pid, struct task_struct *task)
616 unsigned long args[6], sp, pc;
619 res = lock_trace(task);
623 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
624 seq_puts(m, "running\n");
626 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
629 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
631 args[0], args[1], args[2], args[3], args[4], args[5],
637 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
639 /************************************************************************/
640 /* Here the fs part begins */
641 /************************************************************************/
643 /* permission checks */
644 static int proc_fd_access_allowed(struct inode *inode)
646 struct task_struct *task;
648 /* Allow access to a task's file descriptors if it is us or we
649 * may use ptrace attach to the process and find out that
652 task = get_proc_task(inode);
654 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
655 put_task_struct(task);
660 int proc_setattr(struct dentry *dentry, struct iattr *attr)
663 struct inode *inode = d_inode(dentry);
665 if (attr->ia_valid & ATTR_MODE)
668 error = setattr_prepare(dentry, attr);
672 setattr_copy(inode, attr);
673 mark_inode_dirty(inode);
678 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
679 * or euid/egid (for hide_pid_min=2)?
681 static bool has_pid_permissions(struct pid_namespace *pid,
682 struct task_struct *task,
685 if (pid->hide_pid < hide_pid_min)
687 if (in_group_p(pid->pid_gid))
689 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
693 static int proc_pid_permission(struct inode *inode, int mask)
695 struct pid_namespace *pid = proc_pid_ns(inode);
696 struct task_struct *task;
699 task = get_proc_task(inode);
702 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
703 put_task_struct(task);
706 if (pid->hide_pid == HIDEPID_INVISIBLE) {
708 * Let's make getdents(), stat(), and open()
709 * consistent with each other. If a process
710 * may not stat() a file, it shouldn't be seen
718 return generic_permission(inode, mask);
723 static const struct inode_operations proc_def_inode_operations = {
724 .setattr = proc_setattr,
727 static int proc_single_show(struct seq_file *m, void *v)
729 struct inode *inode = m->private;
730 struct pid_namespace *ns = proc_pid_ns(inode);
731 struct pid *pid = proc_pid(inode);
732 struct task_struct *task;
735 task = get_pid_task(pid, PIDTYPE_PID);
739 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
741 put_task_struct(task);
745 static int proc_single_open(struct inode *inode, struct file *filp)
747 return single_open(filp, proc_single_show, inode);
750 static const struct file_operations proc_single_file_operations = {
751 .open = proc_single_open,
754 .release = single_release,
758 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
760 struct task_struct *task = get_proc_task(inode);
761 struct mm_struct *mm = ERR_PTR(-ESRCH);
764 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
765 put_task_struct(task);
767 if (!IS_ERR_OR_NULL(mm)) {
768 /* ensure this mm_struct can't be freed */
770 /* but do not pin its memory */
778 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
780 struct mm_struct *mm = proc_mem_open(inode, mode);
785 file->private_data = mm;
789 static int mem_open(struct inode *inode, struct file *file)
791 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
793 /* OK to pass negative loff_t, we can catch out-of-range */
794 file->f_mode |= FMODE_UNSIGNED_OFFSET;
799 static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
802 struct mm_struct *mm = file->private_data;
803 unsigned long addr = *ppos;
811 page = (char *)__get_free_page(GFP_KERNEL);
816 if (!mmget_not_zero(mm))
819 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
822 int this_len = min_t(int, count, PAGE_SIZE);
824 if (write && copy_from_user(page, buf, this_len)) {
829 this_len = access_remote_vm(mm, addr, page, this_len, flags);
836 if (!write && copy_to_user(buf, page, this_len)) {
850 free_page((unsigned long) page);
854 static ssize_t mem_read(struct file *file, char __user *buf,
855 size_t count, loff_t *ppos)
857 return mem_rw(file, buf, count, ppos, 0);
860 static ssize_t mem_write(struct file *file, const char __user *buf,
861 size_t count, loff_t *ppos)
863 return mem_rw(file, (char __user*)buf, count, ppos, 1);
866 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
870 file->f_pos = offset;
873 file->f_pos += offset;
878 force_successful_syscall_return();
882 static int mem_release(struct inode *inode, struct file *file)
884 struct mm_struct *mm = file->private_data;
890 static const struct file_operations proc_mem_operations = {
895 .release = mem_release,
898 static int environ_open(struct inode *inode, struct file *file)
900 return __mem_open(inode, file, PTRACE_MODE_READ);
903 static ssize_t environ_read(struct file *file, char __user *buf,
904 size_t count, loff_t *ppos)
907 unsigned long src = *ppos;
909 struct mm_struct *mm = file->private_data;
910 unsigned long env_start, env_end;
912 /* Ensure the process spawned far enough to have an environment. */
913 if (!mm || !mm->env_end)
916 page = (char *)__get_free_page(GFP_KERNEL);
921 if (!mmget_not_zero(mm))
924 spin_lock(&mm->arg_lock);
925 env_start = mm->env_start;
926 env_end = mm->env_end;
927 spin_unlock(&mm->arg_lock);
930 size_t this_len, max_len;
933 if (src >= (env_end - env_start))
936 this_len = env_end - (env_start + src);
938 max_len = min_t(size_t, PAGE_SIZE, count);
939 this_len = min(max_len, this_len);
941 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
948 if (copy_to_user(buf, page, retval)) {
962 free_page((unsigned long) page);
966 static const struct file_operations proc_environ_operations = {
967 .open = environ_open,
968 .read = environ_read,
969 .llseek = generic_file_llseek,
970 .release = mem_release,
973 static int auxv_open(struct inode *inode, struct file *file)
975 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
978 static ssize_t auxv_read(struct file *file, char __user *buf,
979 size_t count, loff_t *ppos)
981 struct mm_struct *mm = file->private_data;
982 unsigned int nwords = 0;
988 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
989 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
990 nwords * sizeof(mm->saved_auxv[0]));
993 static const struct file_operations proc_auxv_operations = {
996 .llseek = generic_file_llseek,
997 .release = mem_release,
1000 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1003 struct task_struct *task = get_proc_task(file_inode(file));
1004 char buffer[PROC_NUMBUF];
1005 int oom_adj = OOM_ADJUST_MIN;
1010 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1011 oom_adj = OOM_ADJUST_MAX;
1013 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1015 put_task_struct(task);
1016 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1017 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1020 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1022 static DEFINE_MUTEX(oom_adj_mutex);
1023 struct mm_struct *mm = NULL;
1024 struct task_struct *task;
1027 task = get_proc_task(file_inode(file));
1031 mutex_lock(&oom_adj_mutex);
1033 if (oom_adj < task->signal->oom_score_adj &&
1034 !capable(CAP_SYS_RESOURCE)) {
1039 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1040 * /proc/pid/oom_score_adj instead.
1042 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1043 current->comm, task_pid_nr(current), task_pid_nr(task),
1046 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1047 !capable(CAP_SYS_RESOURCE)) {
1054 * Make sure we will check other processes sharing the mm if this is
1055 * not vfrok which wants its own oom_score_adj.
1056 * pin the mm so it doesn't go away and get reused after task_unlock
1058 if (!task->vfork_done) {
1059 struct task_struct *p = find_lock_task_mm(task);
1062 if (atomic_read(&p->mm->mm_users) > 1) {
1070 task->signal->oom_score_adj = oom_adj;
1071 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1072 task->signal->oom_score_adj_min = (short)oom_adj;
1073 trace_oom_score_adj_update(task);
1076 struct task_struct *p;
1079 for_each_process(p) {
1080 if (same_thread_group(task, p))
1083 /* do not touch kernel threads or the global init */
1084 if (p->flags & PF_KTHREAD || is_global_init(p))
1088 if (!p->vfork_done && process_shares_mm(p, mm)) {
1089 p->signal->oom_score_adj = oom_adj;
1090 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1091 p->signal->oom_score_adj_min = (short)oom_adj;
1099 mutex_unlock(&oom_adj_mutex);
1100 put_task_struct(task);
1105 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1106 * kernels. The effective policy is defined by oom_score_adj, which has a
1107 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1108 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1109 * Processes that become oom disabled via oom_adj will still be oom disabled
1110 * with this implementation.
1112 * oom_adj cannot be removed since existing userspace binaries use it.
1114 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1115 size_t count, loff_t *ppos)
1117 char buffer[PROC_NUMBUF];
1121 memset(buffer, 0, sizeof(buffer));
1122 if (count > sizeof(buffer) - 1)
1123 count = sizeof(buffer) - 1;
1124 if (copy_from_user(buffer, buf, count)) {
1129 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1132 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1133 oom_adj != OOM_DISABLE) {
1139 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1140 * value is always attainable.
1142 if (oom_adj == OOM_ADJUST_MAX)
1143 oom_adj = OOM_SCORE_ADJ_MAX;
1145 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1147 err = __set_oom_adj(file, oom_adj, true);
1149 return err < 0 ? err : count;
1152 static const struct file_operations proc_oom_adj_operations = {
1153 .read = oom_adj_read,
1154 .write = oom_adj_write,
1155 .llseek = generic_file_llseek,
1158 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1159 size_t count, loff_t *ppos)
1161 struct task_struct *task = get_proc_task(file_inode(file));
1162 char buffer[PROC_NUMBUF];
1163 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1168 oom_score_adj = task->signal->oom_score_adj;
1169 put_task_struct(task);
1170 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1171 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1174 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1175 size_t count, loff_t *ppos)
1177 char buffer[PROC_NUMBUF];
1181 memset(buffer, 0, sizeof(buffer));
1182 if (count > sizeof(buffer) - 1)
1183 count = sizeof(buffer) - 1;
1184 if (copy_from_user(buffer, buf, count)) {
1189 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1192 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1193 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1198 err = __set_oom_adj(file, oom_score_adj, false);
1200 return err < 0 ? err : count;
1203 static const struct file_operations proc_oom_score_adj_operations = {
1204 .read = oom_score_adj_read,
1205 .write = oom_score_adj_write,
1206 .llseek = default_llseek,
1209 #ifdef CONFIG_AUDITSYSCALL
1210 #define TMPBUFLEN 11
1211 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1212 size_t count, loff_t *ppos)
1214 struct inode * inode = file_inode(file);
1215 struct task_struct *task = get_proc_task(inode);
1217 char tmpbuf[TMPBUFLEN];
1221 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1222 from_kuid(file->f_cred->user_ns,
1223 audit_get_loginuid(task)));
1224 put_task_struct(task);
1225 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1228 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1229 size_t count, loff_t *ppos)
1231 struct inode * inode = file_inode(file);
1237 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1244 /* No partial writes. */
1248 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1252 /* is userspace tring to explicitly UNSET the loginuid? */
1253 if (loginuid == AUDIT_UID_UNSET) {
1254 kloginuid = INVALID_UID;
1256 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1257 if (!uid_valid(kloginuid))
1261 rv = audit_set_loginuid(kloginuid);
1267 static const struct file_operations proc_loginuid_operations = {
1268 .read = proc_loginuid_read,
1269 .write = proc_loginuid_write,
1270 .llseek = generic_file_llseek,
1273 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1274 size_t count, loff_t *ppos)
1276 struct inode * inode = file_inode(file);
1277 struct task_struct *task = get_proc_task(inode);
1279 char tmpbuf[TMPBUFLEN];
1283 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1284 audit_get_sessionid(task));
1285 put_task_struct(task);
1286 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1289 static const struct file_operations proc_sessionid_operations = {
1290 .read = proc_sessionid_read,
1291 .llseek = generic_file_llseek,
1295 #ifdef CONFIG_FAULT_INJECTION
1296 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1297 size_t count, loff_t *ppos)
1299 struct task_struct *task = get_proc_task(file_inode(file));
1300 char buffer[PROC_NUMBUF];
1306 make_it_fail = task->make_it_fail;
1307 put_task_struct(task);
1309 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1311 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1314 static ssize_t proc_fault_inject_write(struct file * file,
1315 const char __user * buf, size_t count, loff_t *ppos)
1317 struct task_struct *task;
1318 char buffer[PROC_NUMBUF];
1322 if (!capable(CAP_SYS_RESOURCE))
1324 memset(buffer, 0, sizeof(buffer));
1325 if (count > sizeof(buffer) - 1)
1326 count = sizeof(buffer) - 1;
1327 if (copy_from_user(buffer, buf, count))
1329 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1332 if (make_it_fail < 0 || make_it_fail > 1)
1335 task = get_proc_task(file_inode(file));
1338 task->make_it_fail = make_it_fail;
1339 put_task_struct(task);
1344 static const struct file_operations proc_fault_inject_operations = {
1345 .read = proc_fault_inject_read,
1346 .write = proc_fault_inject_write,
1347 .llseek = generic_file_llseek,
1350 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *ppos)
1353 struct task_struct *task;
1357 err = kstrtouint_from_user(buf, count, 0, &n);
1361 task = get_proc_task(file_inode(file));
1365 put_task_struct(task);
1370 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1371 size_t count, loff_t *ppos)
1373 struct task_struct *task;
1374 char numbuf[PROC_NUMBUF];
1377 task = get_proc_task(file_inode(file));
1380 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1381 put_task_struct(task);
1382 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1385 static const struct file_operations proc_fail_nth_operations = {
1386 .read = proc_fail_nth_read,
1387 .write = proc_fail_nth_write,
1392 #ifdef CONFIG_SCHED_DEBUG
1394 * Print out various scheduling related per-task fields:
1396 static int sched_show(struct seq_file *m, void *v)
1398 struct inode *inode = m->private;
1399 struct pid_namespace *ns = proc_pid_ns(inode);
1400 struct task_struct *p;
1402 p = get_proc_task(inode);
1405 proc_sched_show_task(p, ns, m);
1413 sched_write(struct file *file, const char __user *buf,
1414 size_t count, loff_t *offset)
1416 struct inode *inode = file_inode(file);
1417 struct task_struct *p;
1419 p = get_proc_task(inode);
1422 proc_sched_set_task(p);
1429 static int sched_open(struct inode *inode, struct file *filp)
1431 return single_open(filp, sched_show, inode);
1434 static const struct file_operations proc_pid_sched_operations = {
1437 .write = sched_write,
1438 .llseek = seq_lseek,
1439 .release = single_release,
1444 #ifdef CONFIG_SCHED_AUTOGROUP
1446 * Print out autogroup related information:
1448 static int sched_autogroup_show(struct seq_file *m, void *v)
1450 struct inode *inode = m->private;
1451 struct task_struct *p;
1453 p = get_proc_task(inode);
1456 proc_sched_autogroup_show_task(p, m);
1464 sched_autogroup_write(struct file *file, const char __user *buf,
1465 size_t count, loff_t *offset)
1467 struct inode *inode = file_inode(file);
1468 struct task_struct *p;
1469 char buffer[PROC_NUMBUF];
1473 memset(buffer, 0, sizeof(buffer));
1474 if (count > sizeof(buffer) - 1)
1475 count = sizeof(buffer) - 1;
1476 if (copy_from_user(buffer, buf, count))
1479 err = kstrtoint(strstrip(buffer), 0, &nice);
1483 p = get_proc_task(inode);
1487 err = proc_sched_autogroup_set_nice(p, nice);
1496 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1500 ret = single_open(filp, sched_autogroup_show, NULL);
1502 struct seq_file *m = filp->private_data;
1509 static const struct file_operations proc_pid_sched_autogroup_operations = {
1510 .open = sched_autogroup_open,
1512 .write = sched_autogroup_write,
1513 .llseek = seq_lseek,
1514 .release = single_release,
1517 #endif /* CONFIG_SCHED_AUTOGROUP */
1519 static ssize_t comm_write(struct file *file, const char __user *buf,
1520 size_t count, loff_t *offset)
1522 struct inode *inode = file_inode(file);
1523 struct task_struct *p;
1524 char buffer[TASK_COMM_LEN];
1525 const size_t maxlen = sizeof(buffer) - 1;
1527 memset(buffer, 0, sizeof(buffer));
1528 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1531 p = get_proc_task(inode);
1535 if (same_thread_group(current, p))
1536 set_task_comm(p, buffer);
1545 static int comm_show(struct seq_file *m, void *v)
1547 struct inode *inode = m->private;
1548 struct task_struct *p;
1550 p = get_proc_task(inode);
1554 proc_task_name(m, p, false);
1562 static int comm_open(struct inode *inode, struct file *filp)
1564 return single_open(filp, comm_show, inode);
1567 static const struct file_operations proc_pid_set_comm_operations = {
1570 .write = comm_write,
1571 .llseek = seq_lseek,
1572 .release = single_release,
1575 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1577 struct task_struct *task;
1578 struct file *exe_file;
1580 task = get_proc_task(d_inode(dentry));
1583 exe_file = get_task_exe_file(task);
1584 put_task_struct(task);
1586 *exe_path = exe_file->f_path;
1587 path_get(&exe_file->f_path);
1594 static const char *proc_pid_get_link(struct dentry *dentry,
1595 struct inode *inode,
1596 struct delayed_call *done)
1599 int error = -EACCES;
1602 return ERR_PTR(-ECHILD);
1604 /* Are we allowed to snoop on the tasks file descriptors? */
1605 if (!proc_fd_access_allowed(inode))
1608 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1612 nd_jump_link(&path);
1615 return ERR_PTR(error);
1618 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1620 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1627 pathname = d_path(path, tmp, PAGE_SIZE);
1628 len = PTR_ERR(pathname);
1629 if (IS_ERR(pathname))
1631 len = tmp + PAGE_SIZE - 1 - pathname;
1635 if (copy_to_user(buffer, pathname, len))
1638 free_page((unsigned long)tmp);
1642 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1644 int error = -EACCES;
1645 struct inode *inode = d_inode(dentry);
1648 /* Are we allowed to snoop on the tasks file descriptors? */
1649 if (!proc_fd_access_allowed(inode))
1652 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1656 error = do_proc_readlink(&path, buffer, buflen);
1662 const struct inode_operations proc_pid_link_inode_operations = {
1663 .readlink = proc_pid_readlink,
1664 .get_link = proc_pid_get_link,
1665 .setattr = proc_setattr,
1669 /* building an inode */
1671 void task_dump_owner(struct task_struct *task, umode_t mode,
1672 kuid_t *ruid, kgid_t *rgid)
1674 /* Depending on the state of dumpable compute who should own a
1675 * proc file for a task.
1677 const struct cred *cred;
1681 if (unlikely(task->flags & PF_KTHREAD)) {
1682 *ruid = GLOBAL_ROOT_UID;
1683 *rgid = GLOBAL_ROOT_GID;
1687 /* Default to the tasks effective ownership */
1689 cred = __task_cred(task);
1695 * Before the /proc/pid/status file was created the only way to read
1696 * the effective uid of a /process was to stat /proc/pid. Reading
1697 * /proc/pid/status is slow enough that procps and other packages
1698 * kept stating /proc/pid. To keep the rules in /proc simple I have
1699 * made this apply to all per process world readable and executable
1702 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1703 struct mm_struct *mm;
1706 /* Make non-dumpable tasks owned by some root */
1708 if (get_dumpable(mm) != SUID_DUMP_USER) {
1709 struct user_namespace *user_ns = mm->user_ns;
1711 uid = make_kuid(user_ns, 0);
1712 if (!uid_valid(uid))
1713 uid = GLOBAL_ROOT_UID;
1715 gid = make_kgid(user_ns, 0);
1716 if (!gid_valid(gid))
1717 gid = GLOBAL_ROOT_GID;
1720 uid = GLOBAL_ROOT_UID;
1721 gid = GLOBAL_ROOT_GID;
1729 struct inode *proc_pid_make_inode(struct super_block * sb,
1730 struct task_struct *task, umode_t mode)
1732 struct inode * inode;
1733 struct proc_inode *ei;
1735 /* We need a new inode */
1737 inode = new_inode(sb);
1743 inode->i_mode = mode;
1744 inode->i_ino = get_next_ino();
1745 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1746 inode->i_op = &proc_def_inode_operations;
1749 * grab the reference to task.
1751 ei->pid = get_task_pid(task, PIDTYPE_PID);
1755 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1756 security_task_to_inode(task, inode);
1766 int pid_getattr(const struct path *path, struct kstat *stat,
1767 u32 request_mask, unsigned int query_flags)
1769 struct inode *inode = d_inode(path->dentry);
1770 struct pid_namespace *pid = proc_pid_ns(inode);
1771 struct task_struct *task;
1773 generic_fillattr(inode, stat);
1775 stat->uid = GLOBAL_ROOT_UID;
1776 stat->gid = GLOBAL_ROOT_GID;
1778 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1780 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1783 * This doesn't prevent learning whether PID exists,
1784 * it only makes getattr() consistent with readdir().
1788 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1797 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1799 void pid_update_inode(struct task_struct *task, struct inode *inode)
1801 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1803 inode->i_mode &= ~(S_ISUID | S_ISGID);
1804 security_task_to_inode(task, inode);
1808 * Rewrite the inode's ownerships here because the owning task may have
1809 * performed a setuid(), etc.
1812 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1814 struct inode *inode;
1815 struct task_struct *task;
1817 if (flags & LOOKUP_RCU)
1820 inode = d_inode(dentry);
1821 task = get_proc_task(inode);
1824 pid_update_inode(task, inode);
1825 put_task_struct(task);
1831 static inline bool proc_inode_is_dead(struct inode *inode)
1833 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1836 int pid_delete_dentry(const struct dentry *dentry)
1838 /* Is the task we represent dead?
1839 * If so, then don't put the dentry on the lru list,
1840 * kill it immediately.
1842 return proc_inode_is_dead(d_inode(dentry));
1845 const struct dentry_operations pid_dentry_operations =
1847 .d_revalidate = pid_revalidate,
1848 .d_delete = pid_delete_dentry,
1854 * Fill a directory entry.
1856 * If possible create the dcache entry and derive our inode number and
1857 * file type from dcache entry.
1859 * Since all of the proc inode numbers are dynamically generated, the inode
1860 * numbers do not exist until the inode is cache. This means creating the
1861 * the dcache entry in readdir is necessary to keep the inode numbers
1862 * reported by readdir in sync with the inode numbers reported
1865 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1866 const char *name, unsigned int len,
1867 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1869 struct dentry *child, *dir = file->f_path.dentry;
1870 struct qstr qname = QSTR_INIT(name, len);
1871 struct inode *inode;
1872 unsigned type = DT_UNKNOWN;
1875 child = d_hash_and_lookup(dir, &qname);
1877 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1878 child = d_alloc_parallel(dir, &qname, &wq);
1880 goto end_instantiate;
1881 if (d_in_lookup(child)) {
1883 res = instantiate(child, task, ptr);
1884 d_lookup_done(child);
1885 if (unlikely(res)) {
1889 goto end_instantiate;
1893 inode = d_inode(child);
1895 type = inode->i_mode >> 12;
1898 return dir_emit(ctx, name, len, ino, type);
1902 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1903 * which represent vma start and end addresses.
1905 static int dname_to_vma_addr(struct dentry *dentry,
1906 unsigned long *start, unsigned long *end)
1908 const char *str = dentry->d_name.name;
1909 unsigned long long sval, eval;
1912 if (str[0] == '0' && str[1] != '-')
1914 len = _parse_integer(str, 16, &sval);
1915 if (len & KSTRTOX_OVERFLOW)
1917 if (sval != (unsigned long)sval)
1925 if (str[0] == '0' && str[1])
1927 len = _parse_integer(str, 16, &eval);
1928 if (len & KSTRTOX_OVERFLOW)
1930 if (eval != (unsigned long)eval)
1943 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1945 unsigned long vm_start, vm_end;
1946 bool exact_vma_exists = false;
1947 struct mm_struct *mm = NULL;
1948 struct task_struct *task;
1949 struct inode *inode;
1952 if (flags & LOOKUP_RCU)
1955 inode = d_inode(dentry);
1956 task = get_proc_task(inode);
1960 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1961 if (IS_ERR_OR_NULL(mm))
1964 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1965 down_read(&mm->mmap_sem);
1966 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1967 up_read(&mm->mmap_sem);
1972 if (exact_vma_exists) {
1973 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1975 security_task_to_inode(task, inode);
1980 put_task_struct(task);
1986 static const struct dentry_operations tid_map_files_dentry_operations = {
1987 .d_revalidate = map_files_d_revalidate,
1988 .d_delete = pid_delete_dentry,
1991 static int map_files_get_link(struct dentry *dentry, struct path *path)
1993 unsigned long vm_start, vm_end;
1994 struct vm_area_struct *vma;
1995 struct task_struct *task;
1996 struct mm_struct *mm;
2000 task = get_proc_task(d_inode(dentry));
2004 mm = get_task_mm(task);
2005 put_task_struct(task);
2009 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2014 down_read(&mm->mmap_sem);
2015 vma = find_exact_vma(mm, vm_start, vm_end);
2016 if (vma && vma->vm_file) {
2017 *path = vma->vm_file->f_path;
2021 up_read(&mm->mmap_sem);
2029 struct map_files_info {
2030 unsigned long start;
2036 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2037 * symlinks may be used to bypass permissions on ancestor directories in the
2038 * path to the file in question.
2041 proc_map_files_get_link(struct dentry *dentry,
2042 struct inode *inode,
2043 struct delayed_call *done)
2045 if (!capable(CAP_SYS_ADMIN))
2046 return ERR_PTR(-EPERM);
2048 return proc_pid_get_link(dentry, inode, done);
2052 * Identical to proc_pid_link_inode_operations except for get_link()
2054 static const struct inode_operations proc_map_files_link_inode_operations = {
2055 .readlink = proc_pid_readlink,
2056 .get_link = proc_map_files_get_link,
2057 .setattr = proc_setattr,
2060 static struct dentry *
2061 proc_map_files_instantiate(struct dentry *dentry,
2062 struct task_struct *task, const void *ptr)
2064 fmode_t mode = (fmode_t)(unsigned long)ptr;
2065 struct proc_inode *ei;
2066 struct inode *inode;
2068 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2069 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2070 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2072 return ERR_PTR(-ENOENT);
2075 ei->op.proc_get_link = map_files_get_link;
2077 inode->i_op = &proc_map_files_link_inode_operations;
2080 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2081 return d_splice_alias(inode, dentry);
2084 static struct dentry *proc_map_files_lookup(struct inode *dir,
2085 struct dentry *dentry, unsigned int flags)
2087 unsigned long vm_start, vm_end;
2088 struct vm_area_struct *vma;
2089 struct task_struct *task;
2090 struct dentry *result;
2091 struct mm_struct *mm;
2093 result = ERR_PTR(-ENOENT);
2094 task = get_proc_task(dir);
2098 result = ERR_PTR(-EACCES);
2099 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2102 result = ERR_PTR(-ENOENT);
2103 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2106 mm = get_task_mm(task);
2110 down_read(&mm->mmap_sem);
2111 vma = find_exact_vma(mm, vm_start, vm_end);
2116 result = proc_map_files_instantiate(dentry, task,
2117 (void *)(unsigned long)vma->vm_file->f_mode);
2120 up_read(&mm->mmap_sem);
2123 put_task_struct(task);
2128 static const struct inode_operations proc_map_files_inode_operations = {
2129 .lookup = proc_map_files_lookup,
2130 .permission = proc_fd_permission,
2131 .setattr = proc_setattr,
2135 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2137 struct vm_area_struct *vma;
2138 struct task_struct *task;
2139 struct mm_struct *mm;
2140 unsigned long nr_files, pos, i;
2141 struct flex_array *fa = NULL;
2142 struct map_files_info info;
2143 struct map_files_info *p;
2147 task = get_proc_task(file_inode(file));
2152 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2156 if (!dir_emit_dots(file, ctx))
2159 mm = get_task_mm(task);
2162 down_read(&mm->mmap_sem);
2167 * We need two passes here:
2169 * 1) Collect vmas of mapped files with mmap_sem taken
2170 * 2) Release mmap_sem and instantiate entries
2172 * otherwise we get lockdep complained, since filldir()
2173 * routine might require mmap_sem taken in might_fault().
2176 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2177 if (vma->vm_file && ++pos > ctx->pos)
2182 fa = flex_array_alloc(sizeof(info), nr_files,
2184 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2188 flex_array_free(fa);
2189 up_read(&mm->mmap_sem);
2193 for (i = 0, vma = mm->mmap, pos = 2; vma;
2194 vma = vma->vm_next) {
2197 if (++pos <= ctx->pos)
2200 info.start = vma->vm_start;
2201 info.end = vma->vm_end;
2202 info.mode = vma->vm_file->f_mode;
2203 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2207 up_read(&mm->mmap_sem);
2210 for (i = 0; i < nr_files; i++) {
2211 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2214 p = flex_array_get(fa, i);
2215 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2216 if (!proc_fill_cache(file, ctx,
2218 proc_map_files_instantiate,
2220 (void *)(unsigned long)p->mode))
2225 flex_array_free(fa);
2228 put_task_struct(task);
2233 static const struct file_operations proc_map_files_operations = {
2234 .read = generic_read_dir,
2235 .iterate_shared = proc_map_files_readdir,
2236 .llseek = generic_file_llseek,
2239 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2240 struct timers_private {
2242 struct task_struct *task;
2243 struct sighand_struct *sighand;
2244 struct pid_namespace *ns;
2245 unsigned long flags;
2248 static void *timers_start(struct seq_file *m, loff_t *pos)
2250 struct timers_private *tp = m->private;
2252 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2254 return ERR_PTR(-ESRCH);
2256 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2258 return ERR_PTR(-ESRCH);
2260 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2263 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2265 struct timers_private *tp = m->private;
2266 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2269 static void timers_stop(struct seq_file *m, void *v)
2271 struct timers_private *tp = m->private;
2274 unlock_task_sighand(tp->task, &tp->flags);
2279 put_task_struct(tp->task);
2284 static int show_timer(struct seq_file *m, void *v)
2286 struct k_itimer *timer;
2287 struct timers_private *tp = m->private;
2289 static const char * const nstr[] = {
2290 [SIGEV_SIGNAL] = "signal",
2291 [SIGEV_NONE] = "none",
2292 [SIGEV_THREAD] = "thread",
2295 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2296 notify = timer->it_sigev_notify;
2298 seq_printf(m, "ID: %d\n", timer->it_id);
2299 seq_printf(m, "signal: %d/%px\n",
2300 timer->sigq->info.si_signo,
2301 timer->sigq->info.si_value.sival_ptr);
2302 seq_printf(m, "notify: %s/%s.%d\n",
2303 nstr[notify & ~SIGEV_THREAD_ID],
2304 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2305 pid_nr_ns(timer->it_pid, tp->ns));
2306 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2311 static const struct seq_operations proc_timers_seq_ops = {
2312 .start = timers_start,
2313 .next = timers_next,
2314 .stop = timers_stop,
2318 static int proc_timers_open(struct inode *inode, struct file *file)
2320 struct timers_private *tp;
2322 tp = __seq_open_private(file, &proc_timers_seq_ops,
2323 sizeof(struct timers_private));
2327 tp->pid = proc_pid(inode);
2328 tp->ns = proc_pid_ns(inode);
2332 static const struct file_operations proc_timers_operations = {
2333 .open = proc_timers_open,
2335 .llseek = seq_lseek,
2336 .release = seq_release_private,
2340 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2341 size_t count, loff_t *offset)
2343 struct inode *inode = file_inode(file);
2344 struct task_struct *p;
2348 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2352 p = get_proc_task(inode);
2358 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2365 err = security_task_setscheduler(p);
2374 p->timer_slack_ns = p->default_timer_slack_ns;
2376 p->timer_slack_ns = slack_ns;
2385 static int timerslack_ns_show(struct seq_file *m, void *v)
2387 struct inode *inode = m->private;
2388 struct task_struct *p;
2391 p = get_proc_task(inode);
2397 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2404 err = security_task_getscheduler(p);
2410 seq_printf(m, "%llu\n", p->timer_slack_ns);
2419 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2421 return single_open(filp, timerslack_ns_show, inode);
2424 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2425 .open = timerslack_ns_open,
2427 .write = timerslack_ns_write,
2428 .llseek = seq_lseek,
2429 .release = single_release,
2432 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2433 struct task_struct *task, const void *ptr)
2435 const struct pid_entry *p = ptr;
2436 struct inode *inode;
2437 struct proc_inode *ei;
2439 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2441 return ERR_PTR(-ENOENT);
2444 if (S_ISDIR(inode->i_mode))
2445 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2447 inode->i_op = p->iop;
2449 inode->i_fop = p->fop;
2451 pid_update_inode(task, inode);
2452 d_set_d_op(dentry, &pid_dentry_operations);
2453 return d_splice_alias(inode, dentry);
2456 static struct dentry *proc_pident_lookup(struct inode *dir,
2457 struct dentry *dentry,
2458 const struct pid_entry *ents,
2461 struct task_struct *task = get_proc_task(dir);
2462 const struct pid_entry *p, *last;
2463 struct dentry *res = ERR_PTR(-ENOENT);
2469 * Yes, it does not scale. And it should not. Don't add
2470 * new entries into /proc/<tgid>/ without very good reasons.
2472 last = &ents[nents];
2473 for (p = ents; p < last; p++) {
2474 if (p->len != dentry->d_name.len)
2476 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2477 res = proc_pident_instantiate(dentry, task, p);
2481 put_task_struct(task);
2486 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2487 const struct pid_entry *ents, unsigned int nents)
2489 struct task_struct *task = get_proc_task(file_inode(file));
2490 const struct pid_entry *p;
2495 if (!dir_emit_dots(file, ctx))
2498 if (ctx->pos >= nents + 2)
2501 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2502 if (!proc_fill_cache(file, ctx, p->name, p->len,
2503 proc_pident_instantiate, task, p))
2508 put_task_struct(task);
2512 #ifdef CONFIG_SECURITY
2513 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2514 size_t count, loff_t *ppos)
2516 struct inode * inode = file_inode(file);
2519 struct task_struct *task = get_proc_task(inode);
2524 length = security_getprocattr(task,
2525 (char*)file->f_path.dentry->d_name.name,
2527 put_task_struct(task);
2529 length = simple_read_from_buffer(buf, count, ppos, p, length);
2534 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2535 size_t count, loff_t *ppos)
2537 struct inode * inode = file_inode(file);
2538 struct task_struct *task;
2543 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2548 /* A task may only write its own attributes. */
2549 if (current != task) {
2555 if (count > PAGE_SIZE)
2558 /* No partial writes. */
2562 page = memdup_user(buf, count);
2568 /* Guard against adverse ptrace interaction */
2569 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2573 rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
2574 mutex_unlock(¤t->signal->cred_guard_mutex);
2581 static const struct file_operations proc_pid_attr_operations = {
2582 .read = proc_pid_attr_read,
2583 .write = proc_pid_attr_write,
2584 .llseek = generic_file_llseek,
2587 static const struct pid_entry attr_dir_stuff[] = {
2588 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2589 REG("prev", S_IRUGO, proc_pid_attr_operations),
2590 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2591 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2592 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2593 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2596 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2598 return proc_pident_readdir(file, ctx,
2599 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2602 static const struct file_operations proc_attr_dir_operations = {
2603 .read = generic_read_dir,
2604 .iterate_shared = proc_attr_dir_readdir,
2605 .llseek = generic_file_llseek,
2608 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2609 struct dentry *dentry, unsigned int flags)
2611 return proc_pident_lookup(dir, dentry,
2612 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2615 static const struct inode_operations proc_attr_dir_inode_operations = {
2616 .lookup = proc_attr_dir_lookup,
2617 .getattr = pid_getattr,
2618 .setattr = proc_setattr,
2623 #ifdef CONFIG_ELF_CORE
2624 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2625 size_t count, loff_t *ppos)
2627 struct task_struct *task = get_proc_task(file_inode(file));
2628 struct mm_struct *mm;
2629 char buffer[PROC_NUMBUF];
2637 mm = get_task_mm(task);
2639 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2640 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2641 MMF_DUMP_FILTER_SHIFT));
2643 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2646 put_task_struct(task);
2651 static ssize_t proc_coredump_filter_write(struct file *file,
2652 const char __user *buf,
2656 struct task_struct *task;
2657 struct mm_struct *mm;
2663 ret = kstrtouint_from_user(buf, count, 0, &val);
2668 task = get_proc_task(file_inode(file));
2672 mm = get_task_mm(task);
2677 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2679 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2681 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2686 put_task_struct(task);
2693 static const struct file_operations proc_coredump_filter_operations = {
2694 .read = proc_coredump_filter_read,
2695 .write = proc_coredump_filter_write,
2696 .llseek = generic_file_llseek,
2700 #ifdef CONFIG_TASK_IO_ACCOUNTING
2701 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2703 struct task_io_accounting acct = task->ioac;
2704 unsigned long flags;
2707 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2711 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2716 if (whole && lock_task_sighand(task, &flags)) {
2717 struct task_struct *t = task;
2719 task_io_accounting_add(&acct, &task->signal->ioac);
2720 while_each_thread(task, t)
2721 task_io_accounting_add(&acct, &t->ioac);
2723 unlock_task_sighand(task, &flags);
2730 "read_bytes: %llu\n"
2731 "write_bytes: %llu\n"
2732 "cancelled_write_bytes: %llu\n",
2733 (unsigned long long)acct.rchar,
2734 (unsigned long long)acct.wchar,
2735 (unsigned long long)acct.syscr,
2736 (unsigned long long)acct.syscw,
2737 (unsigned long long)acct.read_bytes,
2738 (unsigned long long)acct.write_bytes,
2739 (unsigned long long)acct.cancelled_write_bytes);
2743 mutex_unlock(&task->signal->cred_guard_mutex);
2747 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2748 struct pid *pid, struct task_struct *task)
2750 return do_io_accounting(task, m, 0);
2753 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2754 struct pid *pid, struct task_struct *task)
2756 return do_io_accounting(task, m, 1);
2758 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2760 #ifdef CONFIG_USER_NS
2761 static int proc_id_map_open(struct inode *inode, struct file *file,
2762 const struct seq_operations *seq_ops)
2764 struct user_namespace *ns = NULL;
2765 struct task_struct *task;
2766 struct seq_file *seq;
2769 task = get_proc_task(inode);
2772 ns = get_user_ns(task_cred_xxx(task, user_ns));
2774 put_task_struct(task);
2779 ret = seq_open(file, seq_ops);
2783 seq = file->private_data;
2793 static int proc_id_map_release(struct inode *inode, struct file *file)
2795 struct seq_file *seq = file->private_data;
2796 struct user_namespace *ns = seq->private;
2798 return seq_release(inode, file);
2801 static int proc_uid_map_open(struct inode *inode, struct file *file)
2803 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2806 static int proc_gid_map_open(struct inode *inode, struct file *file)
2808 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2811 static int proc_projid_map_open(struct inode *inode, struct file *file)
2813 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2816 static const struct file_operations proc_uid_map_operations = {
2817 .open = proc_uid_map_open,
2818 .write = proc_uid_map_write,
2820 .llseek = seq_lseek,
2821 .release = proc_id_map_release,
2824 static const struct file_operations proc_gid_map_operations = {
2825 .open = proc_gid_map_open,
2826 .write = proc_gid_map_write,
2828 .llseek = seq_lseek,
2829 .release = proc_id_map_release,
2832 static const struct file_operations proc_projid_map_operations = {
2833 .open = proc_projid_map_open,
2834 .write = proc_projid_map_write,
2836 .llseek = seq_lseek,
2837 .release = proc_id_map_release,
2840 static int proc_setgroups_open(struct inode *inode, struct file *file)
2842 struct user_namespace *ns = NULL;
2843 struct task_struct *task;
2847 task = get_proc_task(inode);
2850 ns = get_user_ns(task_cred_xxx(task, user_ns));
2852 put_task_struct(task);
2857 if (file->f_mode & FMODE_WRITE) {
2859 if (!ns_capable(ns, CAP_SYS_ADMIN))
2863 ret = single_open(file, &proc_setgroups_show, ns);
2874 static int proc_setgroups_release(struct inode *inode, struct file *file)
2876 struct seq_file *seq = file->private_data;
2877 struct user_namespace *ns = seq->private;
2878 int ret = single_release(inode, file);
2883 static const struct file_operations proc_setgroups_operations = {
2884 .open = proc_setgroups_open,
2885 .write = proc_setgroups_write,
2887 .llseek = seq_lseek,
2888 .release = proc_setgroups_release,
2890 #endif /* CONFIG_USER_NS */
2892 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2893 struct pid *pid, struct task_struct *task)
2895 int err = lock_trace(task);
2897 seq_printf(m, "%08x\n", task->personality);
2903 #ifdef CONFIG_LIVEPATCH
2904 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2905 struct pid *pid, struct task_struct *task)
2907 seq_printf(m, "%d\n", task->patch_state);
2910 #endif /* CONFIG_LIVEPATCH */
2912 #ifdef CONFIG_STACKLEAK_METRICS
2913 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2914 struct pid *pid, struct task_struct *task)
2916 unsigned long prev_depth = THREAD_SIZE -
2917 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2918 unsigned long depth = THREAD_SIZE -
2919 (task->lowest_stack & (THREAD_SIZE - 1));
2921 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2925 #endif /* CONFIG_STACKLEAK_METRICS */
2930 static const struct file_operations proc_task_operations;
2931 static const struct inode_operations proc_task_inode_operations;
2933 static const struct pid_entry tgid_base_stuff[] = {
2934 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2935 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2936 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2937 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2938 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2940 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2942 REG("environ", S_IRUSR, proc_environ_operations),
2943 REG("auxv", S_IRUSR, proc_auxv_operations),
2944 ONE("status", S_IRUGO, proc_pid_status),
2945 ONE("personality", S_IRUSR, proc_pid_personality),
2946 ONE("limits", S_IRUGO, proc_pid_limits),
2947 #ifdef CONFIG_SCHED_DEBUG
2948 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2950 #ifdef CONFIG_SCHED_AUTOGROUP
2951 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2953 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2954 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2955 ONE("syscall", S_IRUSR, proc_pid_syscall),
2957 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2958 ONE("stat", S_IRUGO, proc_tgid_stat),
2959 ONE("statm", S_IRUGO, proc_pid_statm),
2960 REG("maps", S_IRUGO, proc_pid_maps_operations),
2962 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2964 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2965 LNK("cwd", proc_cwd_link),
2966 LNK("root", proc_root_link),
2967 LNK("exe", proc_exe_link),
2968 REG("mounts", S_IRUGO, proc_mounts_operations),
2969 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2970 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2971 #ifdef CONFIG_PROC_PAGE_MONITOR
2972 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2973 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2974 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2975 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2977 #ifdef CONFIG_SECURITY
2978 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2980 #ifdef CONFIG_KALLSYMS
2981 ONE("wchan", S_IRUGO, proc_pid_wchan),
2983 #ifdef CONFIG_STACKTRACE
2984 ONE("stack", S_IRUSR, proc_pid_stack),
2986 #ifdef CONFIG_SCHED_INFO
2987 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2989 #ifdef CONFIG_LATENCYTOP
2990 REG("latency", S_IRUGO, proc_lstats_operations),
2992 #ifdef CONFIG_PROC_PID_CPUSET
2993 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2995 #ifdef CONFIG_CGROUPS
2996 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2998 ONE("oom_score", S_IRUGO, proc_oom_score),
2999 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3000 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3001 #ifdef CONFIG_AUDITSYSCALL
3002 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3003 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3005 #ifdef CONFIG_FAULT_INJECTION
3006 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3007 REG("fail-nth", 0644, proc_fail_nth_operations),
3009 #ifdef CONFIG_ELF_CORE
3010 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3012 #ifdef CONFIG_TASK_IO_ACCOUNTING
3013 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3015 #ifdef CONFIG_USER_NS
3016 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3017 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3018 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3019 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3021 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3022 REG("timers", S_IRUGO, proc_timers_operations),
3024 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3025 #ifdef CONFIG_LIVEPATCH
3026 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3028 #ifdef CONFIG_STACKLEAK_METRICS
3029 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3033 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3035 return proc_pident_readdir(file, ctx,
3036 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3039 static const struct file_operations proc_tgid_base_operations = {
3040 .read = generic_read_dir,
3041 .iterate_shared = proc_tgid_base_readdir,
3042 .llseek = generic_file_llseek,
3045 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3047 return proc_pident_lookup(dir, dentry,
3048 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3051 static const struct inode_operations proc_tgid_base_inode_operations = {
3052 .lookup = proc_tgid_base_lookup,
3053 .getattr = pid_getattr,
3054 .setattr = proc_setattr,
3055 .permission = proc_pid_permission,
3058 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3060 struct dentry *dentry, *leader, *dir;
3065 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3066 /* no ->d_hash() rejects on procfs */
3067 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3069 d_invalidate(dentry);
3077 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3078 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3083 name.len = strlen(name.name);
3084 dir = d_hash_and_lookup(leader, &name);
3086 goto out_put_leader;
3089 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3090 dentry = d_hash_and_lookup(dir, &name);
3092 d_invalidate(dentry);
3104 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3105 * @task: task that should be flushed.
3107 * When flushing dentries from proc, one needs to flush them from global
3108 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3109 * in. This call is supposed to do all of this job.
3111 * Looks in the dcache for
3113 * /proc/@tgid/task/@pid
3114 * if either directory is present flushes it and all of it'ts children
3117 * It is safe and reasonable to cache /proc entries for a task until
3118 * that task exits. After that they just clog up the dcache with
3119 * useless entries, possibly causing useful dcache entries to be
3120 * flushed instead. This routine is proved to flush those useless
3121 * dcache entries at process exit time.
3123 * NOTE: This routine is just an optimization so it does not guarantee
3124 * that no dcache entries will exist at process exit time it
3125 * just makes it very unlikely that any will persist.
3128 void proc_flush_task(struct task_struct *task)
3131 struct pid *pid, *tgid;
3134 pid = task_pid(task);
3135 tgid = task_tgid(task);
3137 for (i = 0; i <= pid->level; i++) {
3138 upid = &pid->numbers[i];
3139 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3140 tgid->numbers[i].nr);
3144 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3145 struct task_struct *task, const void *ptr)
3147 struct inode *inode;
3149 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3151 return ERR_PTR(-ENOENT);
3153 inode->i_op = &proc_tgid_base_inode_operations;
3154 inode->i_fop = &proc_tgid_base_operations;
3155 inode->i_flags|=S_IMMUTABLE;
3157 set_nlink(inode, nlink_tgid);
3158 pid_update_inode(task, inode);
3160 d_set_d_op(dentry, &pid_dentry_operations);
3161 return d_splice_alias(inode, dentry);
3164 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3166 struct task_struct *task;
3168 struct pid_namespace *ns;
3169 struct dentry *result = ERR_PTR(-ENOENT);
3171 tgid = name_to_int(&dentry->d_name);
3175 ns = dentry->d_sb->s_fs_info;
3177 task = find_task_by_pid_ns(tgid, ns);
3179 get_task_struct(task);
3184 result = proc_pid_instantiate(dentry, task, NULL);
3185 put_task_struct(task);
3191 * Find the first task with tgid >= tgid
3196 struct task_struct *task;
3198 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3203 put_task_struct(iter.task);
3207 pid = find_ge_pid(iter.tgid, ns);
3209 iter.tgid = pid_nr_ns(pid, ns);
3210 iter.task = pid_task(pid, PIDTYPE_PID);
3211 /* What we to know is if the pid we have find is the
3212 * pid of a thread_group_leader. Testing for task
3213 * being a thread_group_leader is the obvious thing
3214 * todo but there is a window when it fails, due to
3215 * the pid transfer logic in de_thread.
3217 * So we perform the straight forward test of seeing
3218 * if the pid we have found is the pid of a thread
3219 * group leader, and don't worry if the task we have
3220 * found doesn't happen to be a thread group leader.
3221 * As we don't care in the case of readdir.
3223 if (!iter.task || !has_group_leader_pid(iter.task)) {
3227 get_task_struct(iter.task);
3233 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3235 /* for the /proc/ directory itself, after non-process stuff has been done */
3236 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3238 struct tgid_iter iter;
3239 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3240 loff_t pos = ctx->pos;
3242 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3245 if (pos == TGID_OFFSET - 2) {
3246 struct inode *inode = d_inode(ns->proc_self);
3247 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3249 ctx->pos = pos = pos + 1;
3251 if (pos == TGID_OFFSET - 1) {
3252 struct inode *inode = d_inode(ns->proc_thread_self);
3253 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3255 ctx->pos = pos = pos + 1;
3257 iter.tgid = pos - TGID_OFFSET;
3259 for (iter = next_tgid(ns, iter);
3261 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3266 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3269 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3270 ctx->pos = iter.tgid + TGID_OFFSET;
3271 if (!proc_fill_cache(file, ctx, name, len,
3272 proc_pid_instantiate, iter.task, NULL)) {
3273 put_task_struct(iter.task);
3277 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3282 * proc_tid_comm_permission is a special permission function exclusively
3283 * used for the node /proc/<pid>/task/<tid>/comm.
3284 * It bypasses generic permission checks in the case where a task of the same
3285 * task group attempts to access the node.
3286 * The rationale behind this is that glibc and bionic access this node for
3287 * cross thread naming (pthread_set/getname_np(!self)). However, if
3288 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3289 * which locks out the cross thread naming implementation.
3290 * This function makes sure that the node is always accessible for members of
3291 * same thread group.
3293 static int proc_tid_comm_permission(struct inode *inode, int mask)
3295 bool is_same_tgroup;
3296 struct task_struct *task;
3298 task = get_proc_task(inode);
3301 is_same_tgroup = same_thread_group(current, task);
3302 put_task_struct(task);
3304 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3305 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3306 * read or written by the members of the corresponding
3312 return generic_permission(inode, mask);
3315 static const struct inode_operations proc_tid_comm_inode_operations = {
3316 .permission = proc_tid_comm_permission,
3322 static const struct pid_entry tid_base_stuff[] = {
3323 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3324 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3325 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3327 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3329 REG("environ", S_IRUSR, proc_environ_operations),
3330 REG("auxv", S_IRUSR, proc_auxv_operations),
3331 ONE("status", S_IRUGO, proc_pid_status),
3332 ONE("personality", S_IRUSR, proc_pid_personality),
3333 ONE("limits", S_IRUGO, proc_pid_limits),
3334 #ifdef CONFIG_SCHED_DEBUG
3335 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3337 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3338 &proc_tid_comm_inode_operations,
3339 &proc_pid_set_comm_operations, {}),
3340 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3341 ONE("syscall", S_IRUSR, proc_pid_syscall),
3343 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3344 ONE("stat", S_IRUGO, proc_tid_stat),
3345 ONE("statm", S_IRUGO, proc_pid_statm),
3346 REG("maps", S_IRUGO, proc_pid_maps_operations),
3347 #ifdef CONFIG_PROC_CHILDREN
3348 REG("children", S_IRUGO, proc_tid_children_operations),
3351 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3353 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3354 LNK("cwd", proc_cwd_link),
3355 LNK("root", proc_root_link),
3356 LNK("exe", proc_exe_link),
3357 REG("mounts", S_IRUGO, proc_mounts_operations),
3358 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3359 #ifdef CONFIG_PROC_PAGE_MONITOR
3360 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3361 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3362 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3363 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3365 #ifdef CONFIG_SECURITY
3366 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3368 #ifdef CONFIG_KALLSYMS
3369 ONE("wchan", S_IRUGO, proc_pid_wchan),
3371 #ifdef CONFIG_STACKTRACE
3372 ONE("stack", S_IRUSR, proc_pid_stack),
3374 #ifdef CONFIG_SCHED_INFO
3375 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3377 #ifdef CONFIG_LATENCYTOP
3378 REG("latency", S_IRUGO, proc_lstats_operations),
3380 #ifdef CONFIG_PROC_PID_CPUSET
3381 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3383 #ifdef CONFIG_CGROUPS
3384 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3386 ONE("oom_score", S_IRUGO, proc_oom_score),
3387 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3388 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3389 #ifdef CONFIG_AUDITSYSCALL
3390 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3391 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3393 #ifdef CONFIG_FAULT_INJECTION
3394 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3395 REG("fail-nth", 0644, proc_fail_nth_operations),
3397 #ifdef CONFIG_TASK_IO_ACCOUNTING
3398 ONE("io", S_IRUSR, proc_tid_io_accounting),
3400 #ifdef CONFIG_USER_NS
3401 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3402 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3403 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3404 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3406 #ifdef CONFIG_LIVEPATCH
3407 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3411 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3413 return proc_pident_readdir(file, ctx,
3414 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3417 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3419 return proc_pident_lookup(dir, dentry,
3420 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3423 static const struct file_operations proc_tid_base_operations = {
3424 .read = generic_read_dir,
3425 .iterate_shared = proc_tid_base_readdir,
3426 .llseek = generic_file_llseek,
3429 static const struct inode_operations proc_tid_base_inode_operations = {
3430 .lookup = proc_tid_base_lookup,
3431 .getattr = pid_getattr,
3432 .setattr = proc_setattr,
3435 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3436 struct task_struct *task, const void *ptr)
3438 struct inode *inode;
3439 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3441 return ERR_PTR(-ENOENT);
3443 inode->i_op = &proc_tid_base_inode_operations;
3444 inode->i_fop = &proc_tid_base_operations;
3445 inode->i_flags |= S_IMMUTABLE;
3447 set_nlink(inode, nlink_tid);
3448 pid_update_inode(task, inode);
3450 d_set_d_op(dentry, &pid_dentry_operations);
3451 return d_splice_alias(inode, dentry);
3454 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3456 struct task_struct *task;
3457 struct task_struct *leader = get_proc_task(dir);
3459 struct pid_namespace *ns;
3460 struct dentry *result = ERR_PTR(-ENOENT);
3465 tid = name_to_int(&dentry->d_name);
3469 ns = dentry->d_sb->s_fs_info;
3471 task = find_task_by_pid_ns(tid, ns);
3473 get_task_struct(task);
3477 if (!same_thread_group(leader, task))
3480 result = proc_task_instantiate(dentry, task, NULL);
3482 put_task_struct(task);
3484 put_task_struct(leader);
3490 * Find the first tid of a thread group to return to user space.
3492 * Usually this is just the thread group leader, but if the users
3493 * buffer was too small or there was a seek into the middle of the
3494 * directory we have more work todo.
3496 * In the case of a short read we start with find_task_by_pid.
3498 * In the case of a seek we start with the leader and walk nr
3501 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3502 struct pid_namespace *ns)
3504 struct task_struct *pos, *task;
3505 unsigned long nr = f_pos;
3507 if (nr != f_pos) /* 32bit overflow? */
3511 task = pid_task(pid, PIDTYPE_PID);
3515 /* Attempt to start with the tid of a thread */
3517 pos = find_task_by_pid_ns(tid, ns);
3518 if (pos && same_thread_group(pos, task))
3522 /* If nr exceeds the number of threads there is nothing todo */
3523 if (nr >= get_nr_threads(task))
3526 /* If we haven't found our starting place yet start
3527 * with the leader and walk nr threads forward.
3529 pos = task = task->group_leader;
3533 } while_each_thread(task, pos);
3538 get_task_struct(pos);
3545 * Find the next thread in the thread list.
3546 * Return NULL if there is an error or no next thread.
3548 * The reference to the input task_struct is released.
3550 static struct task_struct *next_tid(struct task_struct *start)
3552 struct task_struct *pos = NULL;
3554 if (pid_alive(start)) {
3555 pos = next_thread(start);
3556 if (thread_group_leader(pos))
3559 get_task_struct(pos);
3562 put_task_struct(start);
3566 /* for the /proc/TGID/task/ directories */
3567 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3569 struct inode *inode = file_inode(file);
3570 struct task_struct *task;
3571 struct pid_namespace *ns;
3574 if (proc_inode_is_dead(inode))
3577 if (!dir_emit_dots(file, ctx))
3580 /* f_version caches the tgid value that the last readdir call couldn't
3581 * return. lseek aka telldir automagically resets f_version to 0.
3583 ns = proc_pid_ns(inode);
3584 tid = (int)file->f_version;
3585 file->f_version = 0;
3586 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3588 task = next_tid(task), ctx->pos++) {
3591 tid = task_pid_nr_ns(task, ns);
3592 len = snprintf(name, sizeof(name), "%u", tid);
3593 if (!proc_fill_cache(file, ctx, name, len,
3594 proc_task_instantiate, task, NULL)) {
3595 /* returning this tgid failed, save it as the first
3596 * pid for the next readir call */
3597 file->f_version = (u64)tid;
3598 put_task_struct(task);
3606 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3607 u32 request_mask, unsigned int query_flags)
3609 struct inode *inode = d_inode(path->dentry);
3610 struct task_struct *p = get_proc_task(inode);
3611 generic_fillattr(inode, stat);
3614 stat->nlink += get_nr_threads(p);
3621 static const struct inode_operations proc_task_inode_operations = {
3622 .lookup = proc_task_lookup,
3623 .getattr = proc_task_getattr,
3624 .setattr = proc_setattr,
3625 .permission = proc_pid_permission,
3628 static const struct file_operations proc_task_operations = {
3629 .read = generic_read_dir,
3630 .iterate_shared = proc_task_readdir,
3631 .llseek = generic_file_llseek,
3634 void __init set_proc_pid_nlink(void)
3636 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3637 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));