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 /* We're not going to care if "*ppos" has high bits set */
239 pos = arg_start + *ppos;
241 /* .. but we do check the result is in the proper range */
242 if (pos < arg_start || pos >= env_end)
245 /* .. and we never go past env_end */
246 if (env_end - pos < count)
247 count = env_end - pos;
249 page = (char *)__get_free_page(GFP_KERNEL);
256 size_t size = min_t(size_t, PAGE_SIZE, count);
258 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
262 /* Don't walk past a NUL character once you hit arg_end */
263 if (pos + got >= arg_end) {
267 * If we started before 'arg_end' but ended up
268 * at or after it, we start the NUL character
269 * check at arg_end-1 (where we expect the normal
272 * NOTE! This is smaller than 'got', because
273 * pos + got >= arg_end
276 n = arg_end - pos - 1;
278 /* Cut off at first NUL after 'n' */
279 got = n + strnlen(page+n, got-n);
284 got -= copy_to_user(buf, page, got);
285 if (unlikely(!got)) {
296 free_page((unsigned long)page);
300 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
301 size_t count, loff_t *pos)
303 struct mm_struct *mm;
306 mm = get_task_mm(tsk);
310 ret = get_mm_cmdline(mm, buf, count, pos);
315 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
316 size_t count, loff_t *pos)
318 struct task_struct *tsk;
323 tsk = get_proc_task(file_inode(file));
326 ret = get_task_cmdline(tsk, buf, count, pos);
327 put_task_struct(tsk);
333 static const struct file_operations proc_pid_cmdline_ops = {
334 .read = proc_pid_cmdline_read,
335 .llseek = generic_file_llseek,
338 #ifdef CONFIG_KALLSYMS
340 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
341 * Returns the resolved symbol. If that fails, simply return the address.
343 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
344 struct pid *pid, struct task_struct *task)
347 char symname[KSYM_NAME_LEN];
349 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
352 wchan = get_wchan(task);
353 if (wchan && !lookup_symbol_name(wchan, symname)) {
354 seq_puts(m, symname);
362 #endif /* CONFIG_KALLSYMS */
364 static int lock_trace(struct task_struct *task)
366 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
369 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
370 mutex_unlock(&task->signal->cred_guard_mutex);
376 static void unlock_trace(struct task_struct *task)
378 mutex_unlock(&task->signal->cred_guard_mutex);
381 #ifdef CONFIG_STACKTRACE
383 #define MAX_STACK_TRACE_DEPTH 64
385 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
386 struct pid *pid, struct task_struct *task)
388 struct stack_trace trace;
389 unsigned long *entries;
392 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
397 trace.nr_entries = 0;
398 trace.max_entries = MAX_STACK_TRACE_DEPTH;
399 trace.entries = entries;
402 err = lock_trace(task);
406 save_stack_trace_tsk(task, &trace);
408 for (i = 0; i < trace.nr_entries; i++) {
409 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
419 #ifdef CONFIG_SCHED_INFO
421 * Provides /proc/PID/schedstat
423 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
424 struct pid *pid, struct task_struct *task)
426 if (unlikely(!sched_info_on()))
427 seq_printf(m, "0 0 0\n");
429 seq_printf(m, "%llu %llu %lu\n",
430 (unsigned long long)task->se.sum_exec_runtime,
431 (unsigned long long)task->sched_info.run_delay,
432 task->sched_info.pcount);
438 #ifdef CONFIG_LATENCYTOP
439 static int lstats_show_proc(struct seq_file *m, void *v)
442 struct inode *inode = m->private;
443 struct task_struct *task = get_proc_task(inode);
447 seq_puts(m, "Latency Top version : v0.1\n");
448 for (i = 0; i < 32; i++) {
449 struct latency_record *lr = &task->latency_record[i];
450 if (lr->backtrace[0]) {
452 seq_printf(m, "%i %li %li",
453 lr->count, lr->time, lr->max);
454 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
455 unsigned long bt = lr->backtrace[q];
460 seq_printf(m, " %ps", (void *)bt);
466 put_task_struct(task);
470 static int lstats_open(struct inode *inode, struct file *file)
472 return single_open(file, lstats_show_proc, inode);
475 static ssize_t lstats_write(struct file *file, const char __user *buf,
476 size_t count, loff_t *offs)
478 struct task_struct *task = get_proc_task(file_inode(file));
482 clear_all_latency_tracing(task);
483 put_task_struct(task);
488 static const struct file_operations proc_lstats_operations = {
491 .write = lstats_write,
493 .release = single_release,
498 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
499 struct pid *pid, struct task_struct *task)
501 unsigned long totalpages = totalram_pages + total_swap_pages;
502 unsigned long points = 0;
504 points = oom_badness(task, NULL, NULL, totalpages) *
506 seq_printf(m, "%lu\n", points);
516 static const struct limit_names lnames[RLIM_NLIMITS] = {
517 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
518 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
519 [RLIMIT_DATA] = {"Max data size", "bytes"},
520 [RLIMIT_STACK] = {"Max stack size", "bytes"},
521 [RLIMIT_CORE] = {"Max core file size", "bytes"},
522 [RLIMIT_RSS] = {"Max resident set", "bytes"},
523 [RLIMIT_NPROC] = {"Max processes", "processes"},
524 [RLIMIT_NOFILE] = {"Max open files", "files"},
525 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
526 [RLIMIT_AS] = {"Max address space", "bytes"},
527 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
528 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
529 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
530 [RLIMIT_NICE] = {"Max nice priority", NULL},
531 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
532 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
535 /* Display limits for a process */
536 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
537 struct pid *pid, struct task_struct *task)
542 struct rlimit rlim[RLIM_NLIMITS];
544 if (!lock_task_sighand(task, &flags))
546 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
547 unlock_task_sighand(task, &flags);
550 * print the file header
552 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
553 "Limit", "Soft Limit", "Hard Limit", "Units");
555 for (i = 0; i < RLIM_NLIMITS; i++) {
556 if (rlim[i].rlim_cur == RLIM_INFINITY)
557 seq_printf(m, "%-25s %-20s ",
558 lnames[i].name, "unlimited");
560 seq_printf(m, "%-25s %-20lu ",
561 lnames[i].name, rlim[i].rlim_cur);
563 if (rlim[i].rlim_max == RLIM_INFINITY)
564 seq_printf(m, "%-20s ", "unlimited");
566 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
569 seq_printf(m, "%-10s\n", lnames[i].unit);
577 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
578 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
579 struct pid *pid, struct task_struct *task)
582 unsigned long args[6], sp, pc;
585 res = lock_trace(task);
589 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
590 seq_puts(m, "running\n");
592 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
595 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
597 args[0], args[1], args[2], args[3], args[4], args[5],
603 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
605 /************************************************************************/
606 /* Here the fs part begins */
607 /************************************************************************/
609 /* permission checks */
610 static int proc_fd_access_allowed(struct inode *inode)
612 struct task_struct *task;
614 /* Allow access to a task's file descriptors if it is us or we
615 * may use ptrace attach to the process and find out that
618 task = get_proc_task(inode);
620 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
621 put_task_struct(task);
626 int proc_setattr(struct dentry *dentry, struct iattr *attr)
629 struct inode *inode = d_inode(dentry);
631 if (attr->ia_valid & ATTR_MODE)
634 error = setattr_prepare(dentry, attr);
638 setattr_copy(inode, attr);
639 mark_inode_dirty(inode);
644 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
645 * or euid/egid (for hide_pid_min=2)?
647 static bool has_pid_permissions(struct pid_namespace *pid,
648 struct task_struct *task,
651 if (pid->hide_pid < hide_pid_min)
653 if (in_group_p(pid->pid_gid))
655 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
659 static int proc_pid_permission(struct inode *inode, int mask)
661 struct pid_namespace *pid = proc_pid_ns(inode);
662 struct task_struct *task;
665 task = get_proc_task(inode);
668 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
669 put_task_struct(task);
672 if (pid->hide_pid == HIDEPID_INVISIBLE) {
674 * Let's make getdents(), stat(), and open()
675 * consistent with each other. If a process
676 * may not stat() a file, it shouldn't be seen
684 return generic_permission(inode, mask);
689 static const struct inode_operations proc_def_inode_operations = {
690 .setattr = proc_setattr,
693 static int proc_single_show(struct seq_file *m, void *v)
695 struct inode *inode = m->private;
696 struct pid_namespace *ns = proc_pid_ns(inode);
697 struct pid *pid = proc_pid(inode);
698 struct task_struct *task;
701 task = get_pid_task(pid, PIDTYPE_PID);
705 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
707 put_task_struct(task);
711 static int proc_single_open(struct inode *inode, struct file *filp)
713 return single_open(filp, proc_single_show, inode);
716 static const struct file_operations proc_single_file_operations = {
717 .open = proc_single_open,
720 .release = single_release,
724 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
726 struct task_struct *task = get_proc_task(inode);
727 struct mm_struct *mm = ERR_PTR(-ESRCH);
730 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
731 put_task_struct(task);
733 if (!IS_ERR_OR_NULL(mm)) {
734 /* ensure this mm_struct can't be freed */
736 /* but do not pin its memory */
744 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
746 struct mm_struct *mm = proc_mem_open(inode, mode);
751 file->private_data = mm;
755 static int mem_open(struct inode *inode, struct file *file)
757 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
759 /* OK to pass negative loff_t, we can catch out-of-range */
760 file->f_mode |= FMODE_UNSIGNED_OFFSET;
765 static ssize_t mem_rw(struct file *file, char __user *buf,
766 size_t count, loff_t *ppos, int write)
768 struct mm_struct *mm = file->private_data;
769 unsigned long addr = *ppos;
777 page = (char *)__get_free_page(GFP_KERNEL);
782 if (!mmget_not_zero(mm))
785 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
788 int this_len = min_t(int, count, PAGE_SIZE);
790 if (write && copy_from_user(page, buf, this_len)) {
795 this_len = access_remote_vm(mm, addr, page, this_len, flags);
802 if (!write && copy_to_user(buf, page, this_len)) {
816 free_page((unsigned long) page);
820 static ssize_t mem_read(struct file *file, char __user *buf,
821 size_t count, loff_t *ppos)
823 return mem_rw(file, buf, count, ppos, 0);
826 static ssize_t mem_write(struct file *file, const char __user *buf,
827 size_t count, loff_t *ppos)
829 return mem_rw(file, (char __user*)buf, count, ppos, 1);
832 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
836 file->f_pos = offset;
839 file->f_pos += offset;
844 force_successful_syscall_return();
848 static int mem_release(struct inode *inode, struct file *file)
850 struct mm_struct *mm = file->private_data;
856 static const struct file_operations proc_mem_operations = {
861 .release = mem_release,
864 static int environ_open(struct inode *inode, struct file *file)
866 return __mem_open(inode, file, PTRACE_MODE_READ);
869 static ssize_t environ_read(struct file *file, char __user *buf,
870 size_t count, loff_t *ppos)
873 unsigned long src = *ppos;
875 struct mm_struct *mm = file->private_data;
876 unsigned long env_start, env_end;
878 /* Ensure the process spawned far enough to have an environment. */
879 if (!mm || !mm->env_end)
882 page = (char *)__get_free_page(GFP_KERNEL);
887 if (!mmget_not_zero(mm))
890 spin_lock(&mm->arg_lock);
891 env_start = mm->env_start;
892 env_end = mm->env_end;
893 spin_unlock(&mm->arg_lock);
896 size_t this_len, max_len;
899 if (src >= (env_end - env_start))
902 this_len = env_end - (env_start + src);
904 max_len = min_t(size_t, PAGE_SIZE, count);
905 this_len = min(max_len, this_len);
907 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
914 if (copy_to_user(buf, page, retval)) {
928 free_page((unsigned long) page);
932 static const struct file_operations proc_environ_operations = {
933 .open = environ_open,
934 .read = environ_read,
935 .llseek = generic_file_llseek,
936 .release = mem_release,
939 static int auxv_open(struct inode *inode, struct file *file)
941 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
944 static ssize_t auxv_read(struct file *file, char __user *buf,
945 size_t count, loff_t *ppos)
947 struct mm_struct *mm = file->private_data;
948 unsigned int nwords = 0;
954 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
955 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
956 nwords * sizeof(mm->saved_auxv[0]));
959 static const struct file_operations proc_auxv_operations = {
962 .llseek = generic_file_llseek,
963 .release = mem_release,
966 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
969 struct task_struct *task = get_proc_task(file_inode(file));
970 char buffer[PROC_NUMBUF];
971 int oom_adj = OOM_ADJUST_MIN;
976 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
977 oom_adj = OOM_ADJUST_MAX;
979 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
981 put_task_struct(task);
982 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
983 return simple_read_from_buffer(buf, count, ppos, buffer, len);
986 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
988 static DEFINE_MUTEX(oom_adj_mutex);
989 struct mm_struct *mm = NULL;
990 struct task_struct *task;
993 task = get_proc_task(file_inode(file));
997 mutex_lock(&oom_adj_mutex);
999 if (oom_adj < task->signal->oom_score_adj &&
1000 !capable(CAP_SYS_RESOURCE)) {
1005 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1006 * /proc/pid/oom_score_adj instead.
1008 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1009 current->comm, task_pid_nr(current), task_pid_nr(task),
1012 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1013 !capable(CAP_SYS_RESOURCE)) {
1020 * Make sure we will check other processes sharing the mm if this is
1021 * not vfrok which wants its own oom_score_adj.
1022 * pin the mm so it doesn't go away and get reused after task_unlock
1024 if (!task->vfork_done) {
1025 struct task_struct *p = find_lock_task_mm(task);
1028 if (atomic_read(&p->mm->mm_users) > 1) {
1036 task->signal->oom_score_adj = oom_adj;
1037 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1038 task->signal->oom_score_adj_min = (short)oom_adj;
1039 trace_oom_score_adj_update(task);
1042 struct task_struct *p;
1045 for_each_process(p) {
1046 if (same_thread_group(task, p))
1049 /* do not touch kernel threads or the global init */
1050 if (p->flags & PF_KTHREAD || is_global_init(p))
1054 if (!p->vfork_done && process_shares_mm(p, mm)) {
1055 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1056 task_pid_nr(p), p->comm,
1057 p->signal->oom_score_adj, oom_adj,
1058 task_pid_nr(task), task->comm);
1059 p->signal->oom_score_adj = oom_adj;
1060 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1061 p->signal->oom_score_adj_min = (short)oom_adj;
1069 mutex_unlock(&oom_adj_mutex);
1070 put_task_struct(task);
1075 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1076 * kernels. The effective policy is defined by oom_score_adj, which has a
1077 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1078 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1079 * Processes that become oom disabled via oom_adj will still be oom disabled
1080 * with this implementation.
1082 * oom_adj cannot be removed since existing userspace binaries use it.
1084 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1085 size_t count, loff_t *ppos)
1087 char buffer[PROC_NUMBUF];
1091 memset(buffer, 0, sizeof(buffer));
1092 if (count > sizeof(buffer) - 1)
1093 count = sizeof(buffer) - 1;
1094 if (copy_from_user(buffer, buf, count)) {
1099 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1102 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1103 oom_adj != OOM_DISABLE) {
1109 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1110 * value is always attainable.
1112 if (oom_adj == OOM_ADJUST_MAX)
1113 oom_adj = OOM_SCORE_ADJ_MAX;
1115 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1117 err = __set_oom_adj(file, oom_adj, true);
1119 return err < 0 ? err : count;
1122 static const struct file_operations proc_oom_adj_operations = {
1123 .read = oom_adj_read,
1124 .write = oom_adj_write,
1125 .llseek = generic_file_llseek,
1128 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1129 size_t count, loff_t *ppos)
1131 struct task_struct *task = get_proc_task(file_inode(file));
1132 char buffer[PROC_NUMBUF];
1133 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1138 oom_score_adj = task->signal->oom_score_adj;
1139 put_task_struct(task);
1140 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1141 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1144 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1145 size_t count, loff_t *ppos)
1147 char buffer[PROC_NUMBUF];
1151 memset(buffer, 0, sizeof(buffer));
1152 if (count > sizeof(buffer) - 1)
1153 count = sizeof(buffer) - 1;
1154 if (copy_from_user(buffer, buf, count)) {
1159 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1162 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1163 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1168 err = __set_oom_adj(file, oom_score_adj, false);
1170 return err < 0 ? err : count;
1173 static const struct file_operations proc_oom_score_adj_operations = {
1174 .read = oom_score_adj_read,
1175 .write = oom_score_adj_write,
1176 .llseek = default_llseek,
1179 #ifdef CONFIG_AUDITSYSCALL
1180 #define TMPBUFLEN 11
1181 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1182 size_t count, loff_t *ppos)
1184 struct inode * inode = file_inode(file);
1185 struct task_struct *task = get_proc_task(inode);
1187 char tmpbuf[TMPBUFLEN];
1191 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1192 from_kuid(file->f_cred->user_ns,
1193 audit_get_loginuid(task)));
1194 put_task_struct(task);
1195 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1198 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1199 size_t count, loff_t *ppos)
1201 struct inode * inode = file_inode(file);
1207 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1214 /* No partial writes. */
1218 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1222 /* is userspace tring to explicitly UNSET the loginuid? */
1223 if (loginuid == AUDIT_UID_UNSET) {
1224 kloginuid = INVALID_UID;
1226 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1227 if (!uid_valid(kloginuid))
1231 rv = audit_set_loginuid(kloginuid);
1237 static const struct file_operations proc_loginuid_operations = {
1238 .read = proc_loginuid_read,
1239 .write = proc_loginuid_write,
1240 .llseek = generic_file_llseek,
1243 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1244 size_t count, loff_t *ppos)
1246 struct inode * inode = file_inode(file);
1247 struct task_struct *task = get_proc_task(inode);
1249 char tmpbuf[TMPBUFLEN];
1253 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1254 audit_get_sessionid(task));
1255 put_task_struct(task);
1256 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1259 static const struct file_operations proc_sessionid_operations = {
1260 .read = proc_sessionid_read,
1261 .llseek = generic_file_llseek,
1265 #ifdef CONFIG_FAULT_INJECTION
1266 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1267 size_t count, loff_t *ppos)
1269 struct task_struct *task = get_proc_task(file_inode(file));
1270 char buffer[PROC_NUMBUF];
1276 make_it_fail = task->make_it_fail;
1277 put_task_struct(task);
1279 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1281 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1284 static ssize_t proc_fault_inject_write(struct file * file,
1285 const char __user * buf, size_t count, loff_t *ppos)
1287 struct task_struct *task;
1288 char buffer[PROC_NUMBUF];
1292 if (!capable(CAP_SYS_RESOURCE))
1294 memset(buffer, 0, sizeof(buffer));
1295 if (count > sizeof(buffer) - 1)
1296 count = sizeof(buffer) - 1;
1297 if (copy_from_user(buffer, buf, count))
1299 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1302 if (make_it_fail < 0 || make_it_fail > 1)
1305 task = get_proc_task(file_inode(file));
1308 task->make_it_fail = make_it_fail;
1309 put_task_struct(task);
1314 static const struct file_operations proc_fault_inject_operations = {
1315 .read = proc_fault_inject_read,
1316 .write = proc_fault_inject_write,
1317 .llseek = generic_file_llseek,
1320 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1321 size_t count, loff_t *ppos)
1323 struct task_struct *task;
1327 err = kstrtouint_from_user(buf, count, 0, &n);
1331 task = get_proc_task(file_inode(file));
1335 put_task_struct(task);
1340 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1341 size_t count, loff_t *ppos)
1343 struct task_struct *task;
1344 char numbuf[PROC_NUMBUF];
1347 task = get_proc_task(file_inode(file));
1350 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1351 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
1352 put_task_struct(task);
1357 static const struct file_operations proc_fail_nth_operations = {
1358 .read = proc_fail_nth_read,
1359 .write = proc_fail_nth_write,
1364 #ifdef CONFIG_SCHED_DEBUG
1366 * Print out various scheduling related per-task fields:
1368 static int sched_show(struct seq_file *m, void *v)
1370 struct inode *inode = m->private;
1371 struct pid_namespace *ns = proc_pid_ns(inode);
1372 struct task_struct *p;
1374 p = get_proc_task(inode);
1377 proc_sched_show_task(p, ns, m);
1385 sched_write(struct file *file, const char __user *buf,
1386 size_t count, loff_t *offset)
1388 struct inode *inode = file_inode(file);
1389 struct task_struct *p;
1391 p = get_proc_task(inode);
1394 proc_sched_set_task(p);
1401 static int sched_open(struct inode *inode, struct file *filp)
1403 return single_open(filp, sched_show, inode);
1406 static const struct file_operations proc_pid_sched_operations = {
1409 .write = sched_write,
1410 .llseek = seq_lseek,
1411 .release = single_release,
1416 #ifdef CONFIG_SCHED_AUTOGROUP
1418 * Print out autogroup related information:
1420 static int sched_autogroup_show(struct seq_file *m, void *v)
1422 struct inode *inode = m->private;
1423 struct task_struct *p;
1425 p = get_proc_task(inode);
1428 proc_sched_autogroup_show_task(p, m);
1436 sched_autogroup_write(struct file *file, const char __user *buf,
1437 size_t count, loff_t *offset)
1439 struct inode *inode = file_inode(file);
1440 struct task_struct *p;
1441 char buffer[PROC_NUMBUF];
1445 memset(buffer, 0, sizeof(buffer));
1446 if (count > sizeof(buffer) - 1)
1447 count = sizeof(buffer) - 1;
1448 if (copy_from_user(buffer, buf, count))
1451 err = kstrtoint(strstrip(buffer), 0, &nice);
1455 p = get_proc_task(inode);
1459 err = proc_sched_autogroup_set_nice(p, nice);
1468 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1472 ret = single_open(filp, sched_autogroup_show, NULL);
1474 struct seq_file *m = filp->private_data;
1481 static const struct file_operations proc_pid_sched_autogroup_operations = {
1482 .open = sched_autogroup_open,
1484 .write = sched_autogroup_write,
1485 .llseek = seq_lseek,
1486 .release = single_release,
1489 #endif /* CONFIG_SCHED_AUTOGROUP */
1491 static ssize_t comm_write(struct file *file, const char __user *buf,
1492 size_t count, loff_t *offset)
1494 struct inode *inode = file_inode(file);
1495 struct task_struct *p;
1496 char buffer[TASK_COMM_LEN];
1497 const size_t maxlen = sizeof(buffer) - 1;
1499 memset(buffer, 0, sizeof(buffer));
1500 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1503 p = get_proc_task(inode);
1507 if (same_thread_group(current, p))
1508 set_task_comm(p, buffer);
1517 static int comm_show(struct seq_file *m, void *v)
1519 struct inode *inode = m->private;
1520 struct task_struct *p;
1522 p = get_proc_task(inode);
1526 proc_task_name(m, p, false);
1534 static int comm_open(struct inode *inode, struct file *filp)
1536 return single_open(filp, comm_show, inode);
1539 static const struct file_operations proc_pid_set_comm_operations = {
1542 .write = comm_write,
1543 .llseek = seq_lseek,
1544 .release = single_release,
1547 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1549 struct task_struct *task;
1550 struct file *exe_file;
1552 task = get_proc_task(d_inode(dentry));
1555 exe_file = get_task_exe_file(task);
1556 put_task_struct(task);
1558 *exe_path = exe_file->f_path;
1559 path_get(&exe_file->f_path);
1566 static const char *proc_pid_get_link(struct dentry *dentry,
1567 struct inode *inode,
1568 struct delayed_call *done)
1571 int error = -EACCES;
1574 return ERR_PTR(-ECHILD);
1576 /* Are we allowed to snoop on the tasks file descriptors? */
1577 if (!proc_fd_access_allowed(inode))
1580 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1584 nd_jump_link(&path);
1587 return ERR_PTR(error);
1590 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1592 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1599 pathname = d_path(path, tmp, PAGE_SIZE);
1600 len = PTR_ERR(pathname);
1601 if (IS_ERR(pathname))
1603 len = tmp + PAGE_SIZE - 1 - pathname;
1607 if (copy_to_user(buffer, pathname, len))
1610 free_page((unsigned long)tmp);
1614 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1616 int error = -EACCES;
1617 struct inode *inode = d_inode(dentry);
1620 /* Are we allowed to snoop on the tasks file descriptors? */
1621 if (!proc_fd_access_allowed(inode))
1624 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1628 error = do_proc_readlink(&path, buffer, buflen);
1634 const struct inode_operations proc_pid_link_inode_operations = {
1635 .readlink = proc_pid_readlink,
1636 .get_link = proc_pid_get_link,
1637 .setattr = proc_setattr,
1641 /* building an inode */
1643 void task_dump_owner(struct task_struct *task, umode_t mode,
1644 kuid_t *ruid, kgid_t *rgid)
1646 /* Depending on the state of dumpable compute who should own a
1647 * proc file for a task.
1649 const struct cred *cred;
1653 if (unlikely(task->flags & PF_KTHREAD)) {
1654 *ruid = GLOBAL_ROOT_UID;
1655 *rgid = GLOBAL_ROOT_GID;
1659 /* Default to the tasks effective ownership */
1661 cred = __task_cred(task);
1667 * Before the /proc/pid/status file was created the only way to read
1668 * the effective uid of a /process was to stat /proc/pid. Reading
1669 * /proc/pid/status is slow enough that procps and other packages
1670 * kept stating /proc/pid. To keep the rules in /proc simple I have
1671 * made this apply to all per process world readable and executable
1674 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1675 struct mm_struct *mm;
1678 /* Make non-dumpable tasks owned by some root */
1680 if (get_dumpable(mm) != SUID_DUMP_USER) {
1681 struct user_namespace *user_ns = mm->user_ns;
1683 uid = make_kuid(user_ns, 0);
1684 if (!uid_valid(uid))
1685 uid = GLOBAL_ROOT_UID;
1687 gid = make_kgid(user_ns, 0);
1688 if (!gid_valid(gid))
1689 gid = GLOBAL_ROOT_GID;
1692 uid = GLOBAL_ROOT_UID;
1693 gid = GLOBAL_ROOT_GID;
1701 struct inode *proc_pid_make_inode(struct super_block * sb,
1702 struct task_struct *task, umode_t mode)
1704 struct inode * inode;
1705 struct proc_inode *ei;
1707 /* We need a new inode */
1709 inode = new_inode(sb);
1715 inode->i_mode = mode;
1716 inode->i_ino = get_next_ino();
1717 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1718 inode->i_op = &proc_def_inode_operations;
1721 * grab the reference to task.
1723 ei->pid = get_task_pid(task, PIDTYPE_PID);
1727 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1728 security_task_to_inode(task, inode);
1738 int pid_getattr(const struct path *path, struct kstat *stat,
1739 u32 request_mask, unsigned int query_flags)
1741 struct inode *inode = d_inode(path->dentry);
1742 struct pid_namespace *pid = proc_pid_ns(inode);
1743 struct task_struct *task;
1745 generic_fillattr(inode, stat);
1747 stat->uid = GLOBAL_ROOT_UID;
1748 stat->gid = GLOBAL_ROOT_GID;
1750 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1752 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1755 * This doesn't prevent learning whether PID exists,
1756 * it only makes getattr() consistent with readdir().
1760 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1769 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1771 void pid_update_inode(struct task_struct *task, struct inode *inode)
1773 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1775 inode->i_mode &= ~(S_ISUID | S_ISGID);
1776 security_task_to_inode(task, inode);
1780 * Rewrite the inode's ownerships here because the owning task may have
1781 * performed a setuid(), etc.
1784 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1786 struct inode *inode;
1787 struct task_struct *task;
1789 if (flags & LOOKUP_RCU)
1792 inode = d_inode(dentry);
1793 task = get_proc_task(inode);
1796 pid_update_inode(task, inode);
1797 put_task_struct(task);
1803 static inline bool proc_inode_is_dead(struct inode *inode)
1805 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1808 int pid_delete_dentry(const struct dentry *dentry)
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1814 return proc_inode_is_dead(d_inode(dentry));
1817 const struct dentry_operations pid_dentry_operations =
1819 .d_revalidate = pid_revalidate,
1820 .d_delete = pid_delete_dentry,
1826 * Fill a directory entry.
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1837 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1838 const char *name, unsigned int len,
1839 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1841 struct dentry *child, *dir = file->f_path.dentry;
1842 struct qstr qname = QSTR_INIT(name, len);
1843 struct inode *inode;
1844 unsigned type = DT_UNKNOWN;
1847 child = d_hash_and_lookup(dir, &qname);
1849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1850 child = d_alloc_parallel(dir, &qname, &wq);
1852 goto end_instantiate;
1853 if (d_in_lookup(child)) {
1855 res = instantiate(child, task, ptr);
1856 d_lookup_done(child);
1857 if (unlikely(res)) {
1861 goto end_instantiate;
1865 inode = d_inode(child);
1867 type = inode->i_mode >> 12;
1870 return dir_emit(ctx, name, len, ino, type);
1874 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1875 * which represent vma start and end addresses.
1877 static int dname_to_vma_addr(struct dentry *dentry,
1878 unsigned long *start, unsigned long *end)
1880 const char *str = dentry->d_name.name;
1881 unsigned long long sval, eval;
1884 if (str[0] == '0' && str[1] != '-')
1886 len = _parse_integer(str, 16, &sval);
1887 if (len & KSTRTOX_OVERFLOW)
1889 if (sval != (unsigned long)sval)
1897 if (str[0] == '0' && str[1])
1899 len = _parse_integer(str, 16, &eval);
1900 if (len & KSTRTOX_OVERFLOW)
1902 if (eval != (unsigned long)eval)
1915 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1917 unsigned long vm_start, vm_end;
1918 bool exact_vma_exists = false;
1919 struct mm_struct *mm = NULL;
1920 struct task_struct *task;
1921 struct inode *inode;
1924 if (flags & LOOKUP_RCU)
1927 inode = d_inode(dentry);
1928 task = get_proc_task(inode);
1932 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1933 if (IS_ERR_OR_NULL(mm))
1936 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1937 down_read(&mm->mmap_sem);
1938 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1939 up_read(&mm->mmap_sem);
1944 if (exact_vma_exists) {
1945 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1947 security_task_to_inode(task, inode);
1952 put_task_struct(task);
1958 static const struct dentry_operations tid_map_files_dentry_operations = {
1959 .d_revalidate = map_files_d_revalidate,
1960 .d_delete = pid_delete_dentry,
1963 static int map_files_get_link(struct dentry *dentry, struct path *path)
1965 unsigned long vm_start, vm_end;
1966 struct vm_area_struct *vma;
1967 struct task_struct *task;
1968 struct mm_struct *mm;
1972 task = get_proc_task(d_inode(dentry));
1976 mm = get_task_mm(task);
1977 put_task_struct(task);
1981 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1986 down_read(&mm->mmap_sem);
1987 vma = find_exact_vma(mm, vm_start, vm_end);
1988 if (vma && vma->vm_file) {
1989 *path = vma->vm_file->f_path;
1993 up_read(&mm->mmap_sem);
2001 struct map_files_info {
2002 unsigned long start;
2008 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2009 * symlinks may be used to bypass permissions on ancestor directories in the
2010 * path to the file in question.
2013 proc_map_files_get_link(struct dentry *dentry,
2014 struct inode *inode,
2015 struct delayed_call *done)
2017 if (!capable(CAP_SYS_ADMIN))
2018 return ERR_PTR(-EPERM);
2020 return proc_pid_get_link(dentry, inode, done);
2024 * Identical to proc_pid_link_inode_operations except for get_link()
2026 static const struct inode_operations proc_map_files_link_inode_operations = {
2027 .readlink = proc_pid_readlink,
2028 .get_link = proc_map_files_get_link,
2029 .setattr = proc_setattr,
2032 static struct dentry *
2033 proc_map_files_instantiate(struct dentry *dentry,
2034 struct task_struct *task, const void *ptr)
2036 fmode_t mode = (fmode_t)(unsigned long)ptr;
2037 struct proc_inode *ei;
2038 struct inode *inode;
2040 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2041 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2042 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2044 return ERR_PTR(-ENOENT);
2047 ei->op.proc_get_link = map_files_get_link;
2049 inode->i_op = &proc_map_files_link_inode_operations;
2052 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2053 return d_splice_alias(inode, dentry);
2056 static struct dentry *proc_map_files_lookup(struct inode *dir,
2057 struct dentry *dentry, unsigned int flags)
2059 unsigned long vm_start, vm_end;
2060 struct vm_area_struct *vma;
2061 struct task_struct *task;
2062 struct dentry *result;
2063 struct mm_struct *mm;
2065 result = ERR_PTR(-ENOENT);
2066 task = get_proc_task(dir);
2070 result = ERR_PTR(-EACCES);
2071 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2074 result = ERR_PTR(-ENOENT);
2075 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2078 mm = get_task_mm(task);
2082 down_read(&mm->mmap_sem);
2083 vma = find_exact_vma(mm, vm_start, vm_end);
2088 result = proc_map_files_instantiate(dentry, task,
2089 (void *)(unsigned long)vma->vm_file->f_mode);
2092 up_read(&mm->mmap_sem);
2095 put_task_struct(task);
2100 static const struct inode_operations proc_map_files_inode_operations = {
2101 .lookup = proc_map_files_lookup,
2102 .permission = proc_fd_permission,
2103 .setattr = proc_setattr,
2107 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct mm_struct *mm;
2112 unsigned long nr_files, pos, i;
2113 struct flex_array *fa = NULL;
2114 struct map_files_info info;
2115 struct map_files_info *p;
2119 task = get_proc_task(file_inode(file));
2124 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2128 if (!dir_emit_dots(file, ctx))
2131 mm = get_task_mm(task);
2134 down_read(&mm->mmap_sem);
2139 * We need two passes here:
2141 * 1) Collect vmas of mapped files with mmap_sem taken
2142 * 2) Release mmap_sem and instantiate entries
2144 * otherwise we get lockdep complained, since filldir()
2145 * routine might require mmap_sem taken in might_fault().
2148 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2149 if (vma->vm_file && ++pos > ctx->pos)
2154 fa = flex_array_alloc(sizeof(info), nr_files,
2156 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2160 flex_array_free(fa);
2161 up_read(&mm->mmap_sem);
2165 for (i = 0, vma = mm->mmap, pos = 2; vma;
2166 vma = vma->vm_next) {
2169 if (++pos <= ctx->pos)
2172 info.start = vma->vm_start;
2173 info.end = vma->vm_end;
2174 info.mode = vma->vm_file->f_mode;
2175 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2179 up_read(&mm->mmap_sem);
2182 for (i = 0; i < nr_files; i++) {
2183 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2186 p = flex_array_get(fa, i);
2187 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2188 if (!proc_fill_cache(file, ctx,
2190 proc_map_files_instantiate,
2192 (void *)(unsigned long)p->mode))
2197 flex_array_free(fa);
2200 put_task_struct(task);
2205 static const struct file_operations proc_map_files_operations = {
2206 .read = generic_read_dir,
2207 .iterate_shared = proc_map_files_readdir,
2208 .llseek = generic_file_llseek,
2211 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2212 struct timers_private {
2214 struct task_struct *task;
2215 struct sighand_struct *sighand;
2216 struct pid_namespace *ns;
2217 unsigned long flags;
2220 static void *timers_start(struct seq_file *m, loff_t *pos)
2222 struct timers_private *tp = m->private;
2224 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2226 return ERR_PTR(-ESRCH);
2228 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2230 return ERR_PTR(-ESRCH);
2232 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2235 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2237 struct timers_private *tp = m->private;
2238 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2241 static void timers_stop(struct seq_file *m, void *v)
2243 struct timers_private *tp = m->private;
2246 unlock_task_sighand(tp->task, &tp->flags);
2251 put_task_struct(tp->task);
2256 static int show_timer(struct seq_file *m, void *v)
2258 struct k_itimer *timer;
2259 struct timers_private *tp = m->private;
2261 static const char * const nstr[] = {
2262 [SIGEV_SIGNAL] = "signal",
2263 [SIGEV_NONE] = "none",
2264 [SIGEV_THREAD] = "thread",
2267 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2268 notify = timer->it_sigev_notify;
2270 seq_printf(m, "ID: %d\n", timer->it_id);
2271 seq_printf(m, "signal: %d/%px\n",
2272 timer->sigq->info.si_signo,
2273 timer->sigq->info.si_value.sival_ptr);
2274 seq_printf(m, "notify: %s/%s.%d\n",
2275 nstr[notify & ~SIGEV_THREAD_ID],
2276 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2277 pid_nr_ns(timer->it_pid, tp->ns));
2278 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2283 static const struct seq_operations proc_timers_seq_ops = {
2284 .start = timers_start,
2285 .next = timers_next,
2286 .stop = timers_stop,
2290 static int proc_timers_open(struct inode *inode, struct file *file)
2292 struct timers_private *tp;
2294 tp = __seq_open_private(file, &proc_timers_seq_ops,
2295 sizeof(struct timers_private));
2299 tp->pid = proc_pid(inode);
2300 tp->ns = proc_pid_ns(inode);
2304 static const struct file_operations proc_timers_operations = {
2305 .open = proc_timers_open,
2307 .llseek = seq_lseek,
2308 .release = seq_release_private,
2312 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2313 size_t count, loff_t *offset)
2315 struct inode *inode = file_inode(file);
2316 struct task_struct *p;
2320 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2324 p = get_proc_task(inode);
2329 if (!capable(CAP_SYS_NICE)) {
2334 err = security_task_setscheduler(p);
2343 p->timer_slack_ns = p->default_timer_slack_ns;
2345 p->timer_slack_ns = slack_ns;
2354 static int timerslack_ns_show(struct seq_file *m, void *v)
2356 struct inode *inode = m->private;
2357 struct task_struct *p;
2360 p = get_proc_task(inode);
2366 if (!capable(CAP_SYS_NICE)) {
2370 err = security_task_getscheduler(p);
2376 seq_printf(m, "%llu\n", p->timer_slack_ns);
2385 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2387 return single_open(filp, timerslack_ns_show, inode);
2390 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2391 .open = timerslack_ns_open,
2393 .write = timerslack_ns_write,
2394 .llseek = seq_lseek,
2395 .release = single_release,
2398 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2399 struct task_struct *task, const void *ptr)
2401 const struct pid_entry *p = ptr;
2402 struct inode *inode;
2403 struct proc_inode *ei;
2405 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2407 return ERR_PTR(-ENOENT);
2410 if (S_ISDIR(inode->i_mode))
2411 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2413 inode->i_op = p->iop;
2415 inode->i_fop = p->fop;
2417 pid_update_inode(task, inode);
2418 d_set_d_op(dentry, &pid_dentry_operations);
2419 return d_splice_alias(inode, dentry);
2422 static struct dentry *proc_pident_lookup(struct inode *dir,
2423 struct dentry *dentry,
2424 const struct pid_entry *ents,
2427 struct task_struct *task = get_proc_task(dir);
2428 const struct pid_entry *p, *last;
2429 struct dentry *res = ERR_PTR(-ENOENT);
2435 * Yes, it does not scale. And it should not. Don't add
2436 * new entries into /proc/<tgid>/ without very good reasons.
2438 last = &ents[nents];
2439 for (p = ents; p < last; p++) {
2440 if (p->len != dentry->d_name.len)
2442 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2443 res = proc_pident_instantiate(dentry, task, p);
2447 put_task_struct(task);
2452 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2453 const struct pid_entry *ents, unsigned int nents)
2455 struct task_struct *task = get_proc_task(file_inode(file));
2456 const struct pid_entry *p;
2461 if (!dir_emit_dots(file, ctx))
2464 if (ctx->pos >= nents + 2)
2467 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2468 if (!proc_fill_cache(file, ctx, p->name, p->len,
2469 proc_pident_instantiate, task, p))
2474 put_task_struct(task);
2478 #ifdef CONFIG_SECURITY
2479 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2480 size_t count, loff_t *ppos)
2482 struct inode * inode = file_inode(file);
2485 struct task_struct *task = get_proc_task(inode);
2490 length = security_getprocattr(task,
2491 (char*)file->f_path.dentry->d_name.name,
2493 put_task_struct(task);
2495 length = simple_read_from_buffer(buf, count, ppos, p, length);
2500 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2501 size_t count, loff_t *ppos)
2503 struct inode * inode = file_inode(file);
2506 struct task_struct *task = get_proc_task(inode);
2512 /* A task may only write its own attributes. */
2514 if (current != task)
2517 if (count > PAGE_SIZE)
2520 /* No partial writes. */
2525 page = memdup_user(buf, count);
2527 length = PTR_ERR(page);
2531 /* Guard against adverse ptrace interaction */
2532 length = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2536 length = security_setprocattr(file->f_path.dentry->d_name.name,
2538 mutex_unlock(¤t->signal->cred_guard_mutex);
2542 put_task_struct(task);
2547 static const struct file_operations proc_pid_attr_operations = {
2548 .read = proc_pid_attr_read,
2549 .write = proc_pid_attr_write,
2550 .llseek = generic_file_llseek,
2553 static const struct pid_entry attr_dir_stuff[] = {
2554 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2555 REG("prev", S_IRUGO, proc_pid_attr_operations),
2556 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2557 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2558 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2559 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2562 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2564 return proc_pident_readdir(file, ctx,
2565 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2568 static const struct file_operations proc_attr_dir_operations = {
2569 .read = generic_read_dir,
2570 .iterate_shared = proc_attr_dir_readdir,
2571 .llseek = generic_file_llseek,
2574 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2575 struct dentry *dentry, unsigned int flags)
2577 return proc_pident_lookup(dir, dentry,
2578 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2581 static const struct inode_operations proc_attr_dir_inode_operations = {
2582 .lookup = proc_attr_dir_lookup,
2583 .getattr = pid_getattr,
2584 .setattr = proc_setattr,
2589 #ifdef CONFIG_ELF_CORE
2590 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2591 size_t count, loff_t *ppos)
2593 struct task_struct *task = get_proc_task(file_inode(file));
2594 struct mm_struct *mm;
2595 char buffer[PROC_NUMBUF];
2603 mm = get_task_mm(task);
2605 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2606 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2607 MMF_DUMP_FILTER_SHIFT));
2609 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2612 put_task_struct(task);
2617 static ssize_t proc_coredump_filter_write(struct file *file,
2618 const char __user *buf,
2622 struct task_struct *task;
2623 struct mm_struct *mm;
2629 ret = kstrtouint_from_user(buf, count, 0, &val);
2634 task = get_proc_task(file_inode(file));
2638 mm = get_task_mm(task);
2643 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2645 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2647 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2652 put_task_struct(task);
2659 static const struct file_operations proc_coredump_filter_operations = {
2660 .read = proc_coredump_filter_read,
2661 .write = proc_coredump_filter_write,
2662 .llseek = generic_file_llseek,
2666 #ifdef CONFIG_TASK_IO_ACCOUNTING
2667 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2669 struct task_io_accounting acct = task->ioac;
2670 unsigned long flags;
2673 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2677 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2682 if (whole && lock_task_sighand(task, &flags)) {
2683 struct task_struct *t = task;
2685 task_io_accounting_add(&acct, &task->signal->ioac);
2686 while_each_thread(task, t)
2687 task_io_accounting_add(&acct, &t->ioac);
2689 unlock_task_sighand(task, &flags);
2696 "read_bytes: %llu\n"
2697 "write_bytes: %llu\n"
2698 "cancelled_write_bytes: %llu\n",
2699 (unsigned long long)acct.rchar,
2700 (unsigned long long)acct.wchar,
2701 (unsigned long long)acct.syscr,
2702 (unsigned long long)acct.syscw,
2703 (unsigned long long)acct.read_bytes,
2704 (unsigned long long)acct.write_bytes,
2705 (unsigned long long)acct.cancelled_write_bytes);
2709 mutex_unlock(&task->signal->cred_guard_mutex);
2713 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2714 struct pid *pid, struct task_struct *task)
2716 return do_io_accounting(task, m, 0);
2719 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2720 struct pid *pid, struct task_struct *task)
2722 return do_io_accounting(task, m, 1);
2724 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2726 #ifdef CONFIG_USER_NS
2727 static int proc_id_map_open(struct inode *inode, struct file *file,
2728 const struct seq_operations *seq_ops)
2730 struct user_namespace *ns = NULL;
2731 struct task_struct *task;
2732 struct seq_file *seq;
2735 task = get_proc_task(inode);
2738 ns = get_user_ns(task_cred_xxx(task, user_ns));
2740 put_task_struct(task);
2745 ret = seq_open(file, seq_ops);
2749 seq = file->private_data;
2759 static int proc_id_map_release(struct inode *inode, struct file *file)
2761 struct seq_file *seq = file->private_data;
2762 struct user_namespace *ns = seq->private;
2764 return seq_release(inode, file);
2767 static int proc_uid_map_open(struct inode *inode, struct file *file)
2769 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2772 static int proc_gid_map_open(struct inode *inode, struct file *file)
2774 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2777 static int proc_projid_map_open(struct inode *inode, struct file *file)
2779 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2782 static const struct file_operations proc_uid_map_operations = {
2783 .open = proc_uid_map_open,
2784 .write = proc_uid_map_write,
2786 .llseek = seq_lseek,
2787 .release = proc_id_map_release,
2790 static const struct file_operations proc_gid_map_operations = {
2791 .open = proc_gid_map_open,
2792 .write = proc_gid_map_write,
2794 .llseek = seq_lseek,
2795 .release = proc_id_map_release,
2798 static const struct file_operations proc_projid_map_operations = {
2799 .open = proc_projid_map_open,
2800 .write = proc_projid_map_write,
2802 .llseek = seq_lseek,
2803 .release = proc_id_map_release,
2806 static int proc_setgroups_open(struct inode *inode, struct file *file)
2808 struct user_namespace *ns = NULL;
2809 struct task_struct *task;
2813 task = get_proc_task(inode);
2816 ns = get_user_ns(task_cred_xxx(task, user_ns));
2818 put_task_struct(task);
2823 if (file->f_mode & FMODE_WRITE) {
2825 if (!ns_capable(ns, CAP_SYS_ADMIN))
2829 ret = single_open(file, &proc_setgroups_show, ns);
2840 static int proc_setgroups_release(struct inode *inode, struct file *file)
2842 struct seq_file *seq = file->private_data;
2843 struct user_namespace *ns = seq->private;
2844 int ret = single_release(inode, file);
2849 static const struct file_operations proc_setgroups_operations = {
2850 .open = proc_setgroups_open,
2851 .write = proc_setgroups_write,
2853 .llseek = seq_lseek,
2854 .release = proc_setgroups_release,
2856 #endif /* CONFIG_USER_NS */
2858 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2859 struct pid *pid, struct task_struct *task)
2861 int err = lock_trace(task);
2863 seq_printf(m, "%08x\n", task->personality);
2869 #ifdef CONFIG_LIVEPATCH
2870 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2871 struct pid *pid, struct task_struct *task)
2873 seq_printf(m, "%d\n", task->patch_state);
2876 #endif /* CONFIG_LIVEPATCH */
2881 static const struct file_operations proc_task_operations;
2882 static const struct inode_operations proc_task_inode_operations;
2884 static const struct pid_entry tgid_base_stuff[] = {
2885 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2886 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2887 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2888 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2889 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2891 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2893 REG("environ", S_IRUSR, proc_environ_operations),
2894 REG("auxv", S_IRUSR, proc_auxv_operations),
2895 ONE("status", S_IRUGO, proc_pid_status),
2896 ONE("personality", S_IRUSR, proc_pid_personality),
2897 ONE("limits", S_IRUGO, proc_pid_limits),
2898 #ifdef CONFIG_SCHED_DEBUG
2899 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2901 #ifdef CONFIG_SCHED_AUTOGROUP
2902 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2904 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2905 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2906 ONE("syscall", S_IRUSR, proc_pid_syscall),
2908 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2909 ONE("stat", S_IRUGO, proc_tgid_stat),
2910 ONE("statm", S_IRUGO, proc_pid_statm),
2911 REG("maps", S_IRUGO, proc_pid_maps_operations),
2913 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2915 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2916 LNK("cwd", proc_cwd_link),
2917 LNK("root", proc_root_link),
2918 LNK("exe", proc_exe_link),
2919 REG("mounts", S_IRUGO, proc_mounts_operations),
2920 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2921 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2922 #ifdef CONFIG_PROC_PAGE_MONITOR
2923 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2924 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2925 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2926 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2928 #ifdef CONFIG_SECURITY
2929 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2931 #ifdef CONFIG_KALLSYMS
2932 ONE("wchan", S_IRUGO, proc_pid_wchan),
2934 #ifdef CONFIG_STACKTRACE
2935 ONE("stack", S_IRUSR, proc_pid_stack),
2937 #ifdef CONFIG_SCHED_INFO
2938 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2940 #ifdef CONFIG_LATENCYTOP
2941 REG("latency", S_IRUGO, proc_lstats_operations),
2943 #ifdef CONFIG_PROC_PID_CPUSET
2944 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2946 #ifdef CONFIG_CGROUPS
2947 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2949 ONE("oom_score", S_IRUGO, proc_oom_score),
2950 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2951 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2952 #ifdef CONFIG_AUDITSYSCALL
2953 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2954 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2956 #ifdef CONFIG_FAULT_INJECTION
2957 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2958 REG("fail-nth", 0644, proc_fail_nth_operations),
2960 #ifdef CONFIG_ELF_CORE
2961 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2963 #ifdef CONFIG_TASK_IO_ACCOUNTING
2964 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2966 #ifdef CONFIG_USER_NS
2967 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2968 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2969 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2970 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2972 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2973 REG("timers", S_IRUGO, proc_timers_operations),
2975 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2976 #ifdef CONFIG_LIVEPATCH
2977 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2981 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2983 return proc_pident_readdir(file, ctx,
2984 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2987 static const struct file_operations proc_tgid_base_operations = {
2988 .read = generic_read_dir,
2989 .iterate_shared = proc_tgid_base_readdir,
2990 .llseek = generic_file_llseek,
2993 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2995 return proc_pident_lookup(dir, dentry,
2996 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2999 static const struct inode_operations proc_tgid_base_inode_operations = {
3000 .lookup = proc_tgid_base_lookup,
3001 .getattr = pid_getattr,
3002 .setattr = proc_setattr,
3003 .permission = proc_pid_permission,
3006 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3008 struct dentry *dentry, *leader, *dir;
3013 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3014 /* no ->d_hash() rejects on procfs */
3015 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3017 d_invalidate(dentry);
3025 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3026 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3031 name.len = strlen(name.name);
3032 dir = d_hash_and_lookup(leader, &name);
3034 goto out_put_leader;
3037 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3038 dentry = d_hash_and_lookup(dir, &name);
3040 d_invalidate(dentry);
3052 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3053 * @task: task that should be flushed.
3055 * When flushing dentries from proc, one needs to flush them from global
3056 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3057 * in. This call is supposed to do all of this job.
3059 * Looks in the dcache for
3061 * /proc/@tgid/task/@pid
3062 * if either directory is present flushes it and all of it'ts children
3065 * It is safe and reasonable to cache /proc entries for a task until
3066 * that task exits. After that they just clog up the dcache with
3067 * useless entries, possibly causing useful dcache entries to be
3068 * flushed instead. This routine is proved to flush those useless
3069 * dcache entries at process exit time.
3071 * NOTE: This routine is just an optimization so it does not guarantee
3072 * that no dcache entries will exist at process exit time it
3073 * just makes it very unlikely that any will persist.
3076 void proc_flush_task(struct task_struct *task)
3079 struct pid *pid, *tgid;
3082 pid = task_pid(task);
3083 tgid = task_tgid(task);
3085 for (i = 0; i <= pid->level; i++) {
3086 upid = &pid->numbers[i];
3087 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3088 tgid->numbers[i].nr);
3092 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3093 struct task_struct *task, const void *ptr)
3095 struct inode *inode;
3097 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3099 return ERR_PTR(-ENOENT);
3101 inode->i_op = &proc_tgid_base_inode_operations;
3102 inode->i_fop = &proc_tgid_base_operations;
3103 inode->i_flags|=S_IMMUTABLE;
3105 set_nlink(inode, nlink_tgid);
3106 pid_update_inode(task, inode);
3108 d_set_d_op(dentry, &pid_dentry_operations);
3109 return d_splice_alias(inode, dentry);
3112 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3114 struct task_struct *task;
3116 struct pid_namespace *ns;
3117 struct dentry *result = ERR_PTR(-ENOENT);
3119 tgid = name_to_int(&dentry->d_name);
3123 ns = dentry->d_sb->s_fs_info;
3125 task = find_task_by_pid_ns(tgid, ns);
3127 get_task_struct(task);
3132 result = proc_pid_instantiate(dentry, task, NULL);
3133 put_task_struct(task);
3139 * Find the first task with tgid >= tgid
3144 struct task_struct *task;
3146 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3151 put_task_struct(iter.task);
3155 pid = find_ge_pid(iter.tgid, ns);
3157 iter.tgid = pid_nr_ns(pid, ns);
3158 iter.task = pid_task(pid, PIDTYPE_PID);
3159 /* What we to know is if the pid we have find is the
3160 * pid of a thread_group_leader. Testing for task
3161 * being a thread_group_leader is the obvious thing
3162 * todo but there is a window when it fails, due to
3163 * the pid transfer logic in de_thread.
3165 * So we perform the straight forward test of seeing
3166 * if the pid we have found is the pid of a thread
3167 * group leader, and don't worry if the task we have
3168 * found doesn't happen to be a thread group leader.
3169 * As we don't care in the case of readdir.
3171 if (!iter.task || !has_group_leader_pid(iter.task)) {
3175 get_task_struct(iter.task);
3181 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3183 /* for the /proc/ directory itself, after non-process stuff has been done */
3184 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3186 struct tgid_iter iter;
3187 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3188 loff_t pos = ctx->pos;
3190 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3193 if (pos == TGID_OFFSET - 2) {
3194 struct inode *inode = d_inode(ns->proc_self);
3195 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3197 ctx->pos = pos = pos + 1;
3199 if (pos == TGID_OFFSET - 1) {
3200 struct inode *inode = d_inode(ns->proc_thread_self);
3201 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3203 ctx->pos = pos = pos + 1;
3205 iter.tgid = pos - TGID_OFFSET;
3207 for (iter = next_tgid(ns, iter);
3209 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3214 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3217 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3218 ctx->pos = iter.tgid + TGID_OFFSET;
3219 if (!proc_fill_cache(file, ctx, name, len,
3220 proc_pid_instantiate, iter.task, NULL)) {
3221 put_task_struct(iter.task);
3225 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3230 * proc_tid_comm_permission is a special permission function exclusively
3231 * used for the node /proc/<pid>/task/<tid>/comm.
3232 * It bypasses generic permission checks in the case where a task of the same
3233 * task group attempts to access the node.
3234 * The rationale behind this is that glibc and bionic access this node for
3235 * cross thread naming (pthread_set/getname_np(!self)). However, if
3236 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3237 * which locks out the cross thread naming implementation.
3238 * This function makes sure that the node is always accessible for members of
3239 * same thread group.
3241 static int proc_tid_comm_permission(struct inode *inode, int mask)
3243 bool is_same_tgroup;
3244 struct task_struct *task;
3246 task = get_proc_task(inode);
3249 is_same_tgroup = same_thread_group(current, task);
3250 put_task_struct(task);
3252 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3253 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3254 * read or written by the members of the corresponding
3260 return generic_permission(inode, mask);
3263 static const struct inode_operations proc_tid_comm_inode_operations = {
3264 .permission = proc_tid_comm_permission,
3270 static const struct pid_entry tid_base_stuff[] = {
3271 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3272 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3273 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3275 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3277 REG("environ", S_IRUSR, proc_environ_operations),
3278 REG("auxv", S_IRUSR, proc_auxv_operations),
3279 ONE("status", S_IRUGO, proc_pid_status),
3280 ONE("personality", S_IRUSR, proc_pid_personality),
3281 ONE("limits", S_IRUGO, proc_pid_limits),
3282 #ifdef CONFIG_SCHED_DEBUG
3283 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3285 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3286 &proc_tid_comm_inode_operations,
3287 &proc_pid_set_comm_operations, {}),
3288 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3289 ONE("syscall", S_IRUSR, proc_pid_syscall),
3291 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3292 ONE("stat", S_IRUGO, proc_tid_stat),
3293 ONE("statm", S_IRUGO, proc_pid_statm),
3294 REG("maps", S_IRUGO, proc_tid_maps_operations),
3295 #ifdef CONFIG_PROC_CHILDREN
3296 REG("children", S_IRUGO, proc_tid_children_operations),
3299 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3301 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3302 LNK("cwd", proc_cwd_link),
3303 LNK("root", proc_root_link),
3304 LNK("exe", proc_exe_link),
3305 REG("mounts", S_IRUGO, proc_mounts_operations),
3306 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3307 #ifdef CONFIG_PROC_PAGE_MONITOR
3308 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3309 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3310 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3311 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3313 #ifdef CONFIG_SECURITY
3314 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3316 #ifdef CONFIG_KALLSYMS
3317 ONE("wchan", S_IRUGO, proc_pid_wchan),
3319 #ifdef CONFIG_STACKTRACE
3320 ONE("stack", S_IRUSR, proc_pid_stack),
3322 #ifdef CONFIG_SCHED_INFO
3323 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3325 #ifdef CONFIG_LATENCYTOP
3326 REG("latency", S_IRUGO, proc_lstats_operations),
3328 #ifdef CONFIG_PROC_PID_CPUSET
3329 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3331 #ifdef CONFIG_CGROUPS
3332 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3334 ONE("oom_score", S_IRUGO, proc_oom_score),
3335 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3336 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3337 #ifdef CONFIG_AUDITSYSCALL
3338 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3339 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3341 #ifdef CONFIG_FAULT_INJECTION
3342 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3343 REG("fail-nth", 0644, proc_fail_nth_operations),
3345 #ifdef CONFIG_TASK_IO_ACCOUNTING
3346 ONE("io", S_IRUSR, proc_tid_io_accounting),
3348 #ifdef CONFIG_USER_NS
3349 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3350 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3351 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3352 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3354 #ifdef CONFIG_LIVEPATCH
3355 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3359 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3361 return proc_pident_readdir(file, ctx,
3362 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3365 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3367 return proc_pident_lookup(dir, dentry,
3368 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3371 static const struct file_operations proc_tid_base_operations = {
3372 .read = generic_read_dir,
3373 .iterate_shared = proc_tid_base_readdir,
3374 .llseek = generic_file_llseek,
3377 static const struct inode_operations proc_tid_base_inode_operations = {
3378 .lookup = proc_tid_base_lookup,
3379 .getattr = pid_getattr,
3380 .setattr = proc_setattr,
3383 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3384 struct task_struct *task, const void *ptr)
3386 struct inode *inode;
3387 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3389 return ERR_PTR(-ENOENT);
3391 inode->i_op = &proc_tid_base_inode_operations;
3392 inode->i_fop = &proc_tid_base_operations;
3393 inode->i_flags |= S_IMMUTABLE;
3395 set_nlink(inode, nlink_tid);
3396 pid_update_inode(task, inode);
3398 d_set_d_op(dentry, &pid_dentry_operations);
3399 return d_splice_alias(inode, dentry);
3402 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3404 struct task_struct *task;
3405 struct task_struct *leader = get_proc_task(dir);
3407 struct pid_namespace *ns;
3408 struct dentry *result = ERR_PTR(-ENOENT);
3413 tid = name_to_int(&dentry->d_name);
3417 ns = dentry->d_sb->s_fs_info;
3419 task = find_task_by_pid_ns(tid, ns);
3421 get_task_struct(task);
3425 if (!same_thread_group(leader, task))
3428 result = proc_task_instantiate(dentry, task, NULL);
3430 put_task_struct(task);
3432 put_task_struct(leader);
3438 * Find the first tid of a thread group to return to user space.
3440 * Usually this is just the thread group leader, but if the users
3441 * buffer was too small or there was a seek into the middle of the
3442 * directory we have more work todo.
3444 * In the case of a short read we start with find_task_by_pid.
3446 * In the case of a seek we start with the leader and walk nr
3449 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3450 struct pid_namespace *ns)
3452 struct task_struct *pos, *task;
3453 unsigned long nr = f_pos;
3455 if (nr != f_pos) /* 32bit overflow? */
3459 task = pid_task(pid, PIDTYPE_PID);
3463 /* Attempt to start with the tid of a thread */
3465 pos = find_task_by_pid_ns(tid, ns);
3466 if (pos && same_thread_group(pos, task))
3470 /* If nr exceeds the number of threads there is nothing todo */
3471 if (nr >= get_nr_threads(task))
3474 /* If we haven't found our starting place yet start
3475 * with the leader and walk nr threads forward.
3477 pos = task = task->group_leader;
3481 } while_each_thread(task, pos);
3486 get_task_struct(pos);
3493 * Find the next thread in the thread list.
3494 * Return NULL if there is an error or no next thread.
3496 * The reference to the input task_struct is released.
3498 static struct task_struct *next_tid(struct task_struct *start)
3500 struct task_struct *pos = NULL;
3502 if (pid_alive(start)) {
3503 pos = next_thread(start);
3504 if (thread_group_leader(pos))
3507 get_task_struct(pos);
3510 put_task_struct(start);
3514 /* for the /proc/TGID/task/ directories */
3515 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3517 struct inode *inode = file_inode(file);
3518 struct task_struct *task;
3519 struct pid_namespace *ns;
3522 if (proc_inode_is_dead(inode))
3525 if (!dir_emit_dots(file, ctx))
3528 /* f_version caches the tgid value that the last readdir call couldn't
3529 * return. lseek aka telldir automagically resets f_version to 0.
3531 ns = proc_pid_ns(inode);
3532 tid = (int)file->f_version;
3533 file->f_version = 0;
3534 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3536 task = next_tid(task), ctx->pos++) {
3539 tid = task_pid_nr_ns(task, ns);
3540 len = snprintf(name, sizeof(name), "%u", tid);
3541 if (!proc_fill_cache(file, ctx, name, len,
3542 proc_task_instantiate, task, NULL)) {
3543 /* returning this tgid failed, save it as the first
3544 * pid for the next readir call */
3545 file->f_version = (u64)tid;
3546 put_task_struct(task);
3554 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3555 u32 request_mask, unsigned int query_flags)
3557 struct inode *inode = d_inode(path->dentry);
3558 struct task_struct *p = get_proc_task(inode);
3559 generic_fillattr(inode, stat);
3562 stat->nlink += get_nr_threads(p);
3569 static const struct inode_operations proc_task_inode_operations = {
3570 .lookup = proc_task_lookup,
3571 .getattr = proc_task_getattr,
3572 .setattr = proc_setattr,
3573 .permission = proc_pid_permission,
3576 static const struct file_operations proc_task_operations = {
3577 .read = generic_read_dir,
3578 .iterate_shared = proc_task_readdir,
3579 .llseek = generic_file_llseek,
3582 void __init set_proc_pid_nlink(void)
3584 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3585 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));