1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cache.h>
79 #include <linux/cgroup.h>
80 #include <linux/cpuset.h>
81 #include <linux/audit.h>
82 #include <linux/poll.h>
83 #include <linux/nsproxy.h>
84 #include <linux/oom.h>
85 #include <linux/elf.h>
86 #include <linux/pid_namespace.h>
87 #include <linux/user_namespace.h>
88 #include <linux/fs_struct.h>
89 #include <linux/slab.h>
90 #include <linux/sched/autogroup.h>
91 #include <linux/sched/mm.h>
92 #include <linux/sched/coredump.h>
93 #include <linux/sched/debug.h>
94 #include <linux/sched/stat.h>
95 #include <linux/flex_array.h>
96 #include <linux/posix-timers.h>
97 #include <trace/events/oom.h>
101 #include "../../lib/kstrtox.h"
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
113 static u8 nlink_tid __ro_after_init;
114 static u8 nlink_tgid __ro_after_init;
120 const struct inode_operations *iop;
121 const struct file_operations *fop;
125 #define NOD(NAME, MODE, IOP, FOP, OP) { \
127 .len = sizeof(NAME) - 1, \
134 #define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136 #define LNK(NAME, get_link) \
137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
138 &proc_pid_link_inode_operations, NULL, \
139 { .proc_get_link = get_link } )
140 #define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
142 #define ONE(NAME, MODE, show) \
143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
145 { .proc_show = show } )
148 * Count the number of hardlinks for the pid_entry table, excluding the .
151 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
166 static int get_task_root(struct task_struct *task, struct path *root)
168 int result = -ENOENT;
172 get_fs_root(task->fs, root);
179 static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 struct task_struct *task = get_proc_task(d_inode(dentry));
182 int result = -ENOENT;
187 get_fs_pwd(task->fs, path);
191 put_task_struct(task);
196 static int proc_root_link(struct dentry *dentry, struct path *path)
198 struct task_struct *task = get_proc_task(d_inode(dentry));
199 int result = -ENOENT;
202 result = get_task_root(task, path);
203 put_task_struct(task);
208 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
209 size_t count, loff_t *ppos)
211 unsigned long arg_start, arg_end;
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 spin_unlock(&mm->arg_lock);
224 if (arg_start >= arg_end)
227 /* We're not going to care if "*ppos" has high bits set */
228 /* .. but we do check the result is in the proper range */
229 pos = arg_start + *ppos;
230 if (pos < arg_start || pos >= arg_end)
232 if (count > arg_end - pos)
233 count = arg_end - pos;
235 page = (char *)__get_free_page(GFP_KERNEL);
242 size_t size = min_t(size_t, PAGE_SIZE, count);
244 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
247 got -= copy_to_user(buf, page, got);
248 if (unlikely(!got)) {
259 free_page((unsigned long)page);
263 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
264 size_t count, loff_t *pos)
266 struct mm_struct *mm;
269 mm = get_task_mm(tsk);
273 ret = get_mm_cmdline(mm, buf, count, pos);
278 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
279 size_t count, loff_t *pos)
281 struct task_struct *tsk;
286 tsk = get_proc_task(file_inode(file));
289 ret = get_task_cmdline(tsk, buf, count, pos);
290 put_task_struct(tsk);
296 static const struct file_operations proc_pid_cmdline_ops = {
297 .read = proc_pid_cmdline_read,
298 .llseek = generic_file_llseek,
301 #ifdef CONFIG_KALLSYMS
303 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
304 * Returns the resolved symbol. If that fails, simply return the address.
306 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
307 struct pid *pid, struct task_struct *task)
310 char symname[KSYM_NAME_LEN];
312 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
315 wchan = get_wchan(task);
316 if (wchan && !lookup_symbol_name(wchan, symname)) {
317 seq_puts(m, symname);
325 #endif /* CONFIG_KALLSYMS */
327 static int lock_trace(struct task_struct *task)
329 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
332 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
333 mutex_unlock(&task->signal->cred_guard_mutex);
339 static void unlock_trace(struct task_struct *task)
341 mutex_unlock(&task->signal->cred_guard_mutex);
344 #ifdef CONFIG_STACKTRACE
346 #define MAX_STACK_TRACE_DEPTH 64
348 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
349 struct pid *pid, struct task_struct *task)
351 struct stack_trace trace;
352 unsigned long *entries;
356 * The ability to racily run the kernel stack unwinder on a running task
357 * and then observe the unwinder output is scary; while it is useful for
358 * debugging kernel issues, it can also allow an attacker to leak kernel
360 * Doing this in a manner that is at least safe from races would require
361 * some work to ensure that the remote task can not be scheduled; and
362 * even then, this would still expose the unwinder as local attack
364 * Therefore, this interface is restricted to root.
366 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
369 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
374 trace.nr_entries = 0;
375 trace.max_entries = MAX_STACK_TRACE_DEPTH;
376 trace.entries = entries;
379 err = lock_trace(task);
383 save_stack_trace_tsk(task, &trace);
385 for (i = 0; i < trace.nr_entries; i++) {
386 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
396 #ifdef CONFIG_SCHED_INFO
398 * Provides /proc/PID/schedstat
400 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
401 struct pid *pid, struct task_struct *task)
403 if (unlikely(!sched_info_on()))
404 seq_printf(m, "0 0 0\n");
406 seq_printf(m, "%llu %llu %lu\n",
407 (unsigned long long)task->se.sum_exec_runtime,
408 (unsigned long long)task->sched_info.run_delay,
409 task->sched_info.pcount);
415 #ifdef CONFIG_LATENCYTOP
416 static int lstats_show_proc(struct seq_file *m, void *v)
419 struct inode *inode = m->private;
420 struct task_struct *task = get_proc_task(inode);
424 seq_puts(m, "Latency Top version : v0.1\n");
425 for (i = 0; i < LT_SAVECOUNT; i++) {
426 struct latency_record *lr = &task->latency_record[i];
427 if (lr->backtrace[0]) {
429 seq_printf(m, "%i %li %li",
430 lr->count, lr->time, lr->max);
431 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
432 unsigned long bt = lr->backtrace[q];
437 seq_printf(m, " %ps", (void *)bt);
443 put_task_struct(task);
447 static int lstats_open(struct inode *inode, struct file *file)
449 return single_open(file, lstats_show_proc, inode);
452 static ssize_t lstats_write(struct file *file, const char __user *buf,
453 size_t count, loff_t *offs)
455 struct task_struct *task = get_proc_task(file_inode(file));
459 clear_all_latency_tracing(task);
460 put_task_struct(task);
465 static const struct file_operations proc_lstats_operations = {
468 .write = lstats_write,
470 .release = single_release,
475 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
476 struct pid *pid, struct task_struct *task)
478 unsigned long totalpages = totalram_pages + total_swap_pages;
479 unsigned long points = 0;
481 points = oom_badness(task, NULL, NULL, totalpages) *
483 seq_printf(m, "%lu\n", points);
493 static const struct limit_names lnames[RLIM_NLIMITS] = {
494 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
495 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
496 [RLIMIT_DATA] = {"Max data size", "bytes"},
497 [RLIMIT_STACK] = {"Max stack size", "bytes"},
498 [RLIMIT_CORE] = {"Max core file size", "bytes"},
499 [RLIMIT_RSS] = {"Max resident set", "bytes"},
500 [RLIMIT_NPROC] = {"Max processes", "processes"},
501 [RLIMIT_NOFILE] = {"Max open files", "files"},
502 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
503 [RLIMIT_AS] = {"Max address space", "bytes"},
504 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
505 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
506 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
507 [RLIMIT_NICE] = {"Max nice priority", NULL},
508 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
509 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
512 /* Display limits for a process */
513 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
514 struct pid *pid, struct task_struct *task)
519 struct rlimit rlim[RLIM_NLIMITS];
521 if (!lock_task_sighand(task, &flags))
523 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
524 unlock_task_sighand(task, &flags);
527 * print the file header
529 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
530 "Limit", "Soft Limit", "Hard Limit", "Units");
532 for (i = 0; i < RLIM_NLIMITS; i++) {
533 if (rlim[i].rlim_cur == RLIM_INFINITY)
534 seq_printf(m, "%-25s %-20s ",
535 lnames[i].name, "unlimited");
537 seq_printf(m, "%-25s %-20lu ",
538 lnames[i].name, rlim[i].rlim_cur);
540 if (rlim[i].rlim_max == RLIM_INFINITY)
541 seq_printf(m, "%-20s ", "unlimited");
543 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
546 seq_printf(m, "%-10s\n", lnames[i].unit);
554 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
555 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
556 struct pid *pid, struct task_struct *task)
559 unsigned long args[6], sp, pc;
562 res = lock_trace(task);
566 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
567 seq_puts(m, "running\n");
569 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
572 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
574 args[0], args[1], args[2], args[3], args[4], args[5],
580 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
582 /************************************************************************/
583 /* Here the fs part begins */
584 /************************************************************************/
586 /* permission checks */
587 static int proc_fd_access_allowed(struct inode *inode)
589 struct task_struct *task;
591 /* Allow access to a task's file descriptors if it is us or we
592 * may use ptrace attach to the process and find out that
595 task = get_proc_task(inode);
597 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
598 put_task_struct(task);
603 int proc_setattr(struct dentry *dentry, struct iattr *attr)
606 struct inode *inode = d_inode(dentry);
608 if (attr->ia_valid & ATTR_MODE)
611 error = setattr_prepare(dentry, attr);
615 setattr_copy(inode, attr);
616 mark_inode_dirty(inode);
621 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
622 * or euid/egid (for hide_pid_min=2)?
624 static bool has_pid_permissions(struct pid_namespace *pid,
625 struct task_struct *task,
628 if (pid->hide_pid < hide_pid_min)
630 if (in_group_p(pid->pid_gid))
632 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
636 static int proc_pid_permission(struct inode *inode, int mask)
638 struct pid_namespace *pid = proc_pid_ns(inode);
639 struct task_struct *task;
642 task = get_proc_task(inode);
645 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
646 put_task_struct(task);
649 if (pid->hide_pid == HIDEPID_INVISIBLE) {
651 * Let's make getdents(), stat(), and open()
652 * consistent with each other. If a process
653 * may not stat() a file, it shouldn't be seen
661 return generic_permission(inode, mask);
666 static const struct inode_operations proc_def_inode_operations = {
667 .setattr = proc_setattr,
670 static int proc_single_show(struct seq_file *m, void *v)
672 struct inode *inode = m->private;
673 struct pid_namespace *ns = proc_pid_ns(inode);
674 struct pid *pid = proc_pid(inode);
675 struct task_struct *task;
678 task = get_pid_task(pid, PIDTYPE_PID);
682 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
684 put_task_struct(task);
688 static int proc_single_open(struct inode *inode, struct file *filp)
690 return single_open(filp, proc_single_show, inode);
693 static const struct file_operations proc_single_file_operations = {
694 .open = proc_single_open,
697 .release = single_release,
701 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
703 struct task_struct *task = get_proc_task(inode);
704 struct mm_struct *mm = ERR_PTR(-ESRCH);
707 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
708 put_task_struct(task);
710 if (!IS_ERR_OR_NULL(mm)) {
711 /* ensure this mm_struct can't be freed */
713 /* but do not pin its memory */
721 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
723 struct mm_struct *mm = proc_mem_open(inode, mode);
728 file->private_data = mm;
732 static int mem_open(struct inode *inode, struct file *file)
734 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
736 /* OK to pass negative loff_t, we can catch out-of-range */
737 file->f_mode |= FMODE_UNSIGNED_OFFSET;
742 static ssize_t mem_rw(struct file *file, char __user *buf,
743 size_t count, loff_t *ppos, int write)
745 struct mm_struct *mm = file->private_data;
746 unsigned long addr = *ppos;
754 page = (char *)__get_free_page(GFP_KERNEL);
759 if (!mmget_not_zero(mm))
762 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
765 int this_len = min_t(int, count, PAGE_SIZE);
767 if (write && copy_from_user(page, buf, this_len)) {
772 this_len = access_remote_vm(mm, addr, page, this_len, flags);
779 if (!write && copy_to_user(buf, page, this_len)) {
793 free_page((unsigned long) page);
797 static ssize_t mem_read(struct file *file, char __user *buf,
798 size_t count, loff_t *ppos)
800 return mem_rw(file, buf, count, ppos, 0);
803 static ssize_t mem_write(struct file *file, const char __user *buf,
804 size_t count, loff_t *ppos)
806 return mem_rw(file, (char __user*)buf, count, ppos, 1);
809 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
813 file->f_pos = offset;
816 file->f_pos += offset;
821 force_successful_syscall_return();
825 static int mem_release(struct inode *inode, struct file *file)
827 struct mm_struct *mm = file->private_data;
833 static const struct file_operations proc_mem_operations = {
838 .release = mem_release,
841 static int environ_open(struct inode *inode, struct file *file)
843 return __mem_open(inode, file, PTRACE_MODE_READ);
846 static ssize_t environ_read(struct file *file, char __user *buf,
847 size_t count, loff_t *ppos)
850 unsigned long src = *ppos;
852 struct mm_struct *mm = file->private_data;
853 unsigned long env_start, env_end;
855 /* Ensure the process spawned far enough to have an environment. */
856 if (!mm || !mm->env_end)
859 page = (char *)__get_free_page(GFP_KERNEL);
864 if (!mmget_not_zero(mm))
867 spin_lock(&mm->arg_lock);
868 env_start = mm->env_start;
869 env_end = mm->env_end;
870 spin_unlock(&mm->arg_lock);
873 size_t this_len, max_len;
876 if (src >= (env_end - env_start))
879 this_len = env_end - (env_start + src);
881 max_len = min_t(size_t, PAGE_SIZE, count);
882 this_len = min(max_len, this_len);
884 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
891 if (copy_to_user(buf, page, retval)) {
905 free_page((unsigned long) page);
909 static const struct file_operations proc_environ_operations = {
910 .open = environ_open,
911 .read = environ_read,
912 .llseek = generic_file_llseek,
913 .release = mem_release,
916 static int auxv_open(struct inode *inode, struct file *file)
918 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
921 static ssize_t auxv_read(struct file *file, char __user *buf,
922 size_t count, loff_t *ppos)
924 struct mm_struct *mm = file->private_data;
925 unsigned int nwords = 0;
931 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
932 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
933 nwords * sizeof(mm->saved_auxv[0]));
936 static const struct file_operations proc_auxv_operations = {
939 .llseek = generic_file_llseek,
940 .release = mem_release,
943 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
946 struct task_struct *task = get_proc_task(file_inode(file));
947 char buffer[PROC_NUMBUF];
948 int oom_adj = OOM_ADJUST_MIN;
953 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
954 oom_adj = OOM_ADJUST_MAX;
956 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
958 put_task_struct(task);
959 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
960 return simple_read_from_buffer(buf, count, ppos, buffer, len);
963 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
965 static DEFINE_MUTEX(oom_adj_mutex);
966 struct mm_struct *mm = NULL;
967 struct task_struct *task;
970 task = get_proc_task(file_inode(file));
974 mutex_lock(&oom_adj_mutex);
976 if (oom_adj < task->signal->oom_score_adj &&
977 !capable(CAP_SYS_RESOURCE)) {
982 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
983 * /proc/pid/oom_score_adj instead.
985 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
986 current->comm, task_pid_nr(current), task_pid_nr(task),
989 if ((short)oom_adj < task->signal->oom_score_adj_min &&
990 !capable(CAP_SYS_RESOURCE)) {
997 * Make sure we will check other processes sharing the mm if this is
998 * not vfrok which wants its own oom_score_adj.
999 * pin the mm so it doesn't go away and get reused after task_unlock
1001 if (!task->vfork_done) {
1002 struct task_struct *p = find_lock_task_mm(task);
1005 if (atomic_read(&p->mm->mm_users) > 1) {
1013 task->signal->oom_score_adj = oom_adj;
1014 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1015 task->signal->oom_score_adj_min = (short)oom_adj;
1016 trace_oom_score_adj_update(task);
1019 struct task_struct *p;
1022 for_each_process(p) {
1023 if (same_thread_group(task, p))
1026 /* do not touch kernel threads or the global init */
1027 if (p->flags & PF_KTHREAD || is_global_init(p))
1031 if (!p->vfork_done && process_shares_mm(p, mm)) {
1032 p->signal->oom_score_adj = oom_adj;
1033 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1034 p->signal->oom_score_adj_min = (short)oom_adj;
1042 mutex_unlock(&oom_adj_mutex);
1043 put_task_struct(task);
1048 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1049 * kernels. The effective policy is defined by oom_score_adj, which has a
1050 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1051 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1052 * Processes that become oom disabled via oom_adj will still be oom disabled
1053 * with this implementation.
1055 * oom_adj cannot be removed since existing userspace binaries use it.
1057 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1058 size_t count, loff_t *ppos)
1060 char buffer[PROC_NUMBUF];
1064 memset(buffer, 0, sizeof(buffer));
1065 if (count > sizeof(buffer) - 1)
1066 count = sizeof(buffer) - 1;
1067 if (copy_from_user(buffer, buf, count)) {
1072 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1075 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1076 oom_adj != OOM_DISABLE) {
1082 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1083 * value is always attainable.
1085 if (oom_adj == OOM_ADJUST_MAX)
1086 oom_adj = OOM_SCORE_ADJ_MAX;
1088 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1090 err = __set_oom_adj(file, oom_adj, true);
1092 return err < 0 ? err : count;
1095 static const struct file_operations proc_oom_adj_operations = {
1096 .read = oom_adj_read,
1097 .write = oom_adj_write,
1098 .llseek = generic_file_llseek,
1101 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1102 size_t count, loff_t *ppos)
1104 struct task_struct *task = get_proc_task(file_inode(file));
1105 char buffer[PROC_NUMBUF];
1106 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1111 oom_score_adj = task->signal->oom_score_adj;
1112 put_task_struct(task);
1113 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1114 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1117 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1118 size_t count, loff_t *ppos)
1120 char buffer[PROC_NUMBUF];
1124 memset(buffer, 0, sizeof(buffer));
1125 if (count > sizeof(buffer) - 1)
1126 count = sizeof(buffer) - 1;
1127 if (copy_from_user(buffer, buf, count)) {
1132 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1135 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1136 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1141 err = __set_oom_adj(file, oom_score_adj, false);
1143 return err < 0 ? err : count;
1146 static const struct file_operations proc_oom_score_adj_operations = {
1147 .read = oom_score_adj_read,
1148 .write = oom_score_adj_write,
1149 .llseek = default_llseek,
1152 #ifdef CONFIG_AUDITSYSCALL
1153 #define TMPBUFLEN 11
1154 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1155 size_t count, loff_t *ppos)
1157 struct inode * inode = file_inode(file);
1158 struct task_struct *task = get_proc_task(inode);
1160 char tmpbuf[TMPBUFLEN];
1164 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1165 from_kuid(file->f_cred->user_ns,
1166 audit_get_loginuid(task)));
1167 put_task_struct(task);
1168 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1171 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1172 size_t count, loff_t *ppos)
1174 struct inode * inode = file_inode(file);
1180 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1187 /* No partial writes. */
1191 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1195 /* is userspace tring to explicitly UNSET the loginuid? */
1196 if (loginuid == AUDIT_UID_UNSET) {
1197 kloginuid = INVALID_UID;
1199 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1200 if (!uid_valid(kloginuid))
1204 rv = audit_set_loginuid(kloginuid);
1210 static const struct file_operations proc_loginuid_operations = {
1211 .read = proc_loginuid_read,
1212 .write = proc_loginuid_write,
1213 .llseek = generic_file_llseek,
1216 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1217 size_t count, loff_t *ppos)
1219 struct inode * inode = file_inode(file);
1220 struct task_struct *task = get_proc_task(inode);
1222 char tmpbuf[TMPBUFLEN];
1226 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1227 audit_get_sessionid(task));
1228 put_task_struct(task);
1229 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1232 static const struct file_operations proc_sessionid_operations = {
1233 .read = proc_sessionid_read,
1234 .llseek = generic_file_llseek,
1238 #ifdef CONFIG_FAULT_INJECTION
1239 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1240 size_t count, loff_t *ppos)
1242 struct task_struct *task = get_proc_task(file_inode(file));
1243 char buffer[PROC_NUMBUF];
1249 make_it_fail = task->make_it_fail;
1250 put_task_struct(task);
1252 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1254 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1257 static ssize_t proc_fault_inject_write(struct file * file,
1258 const char __user * buf, size_t count, loff_t *ppos)
1260 struct task_struct *task;
1261 char buffer[PROC_NUMBUF];
1265 if (!capable(CAP_SYS_RESOURCE))
1267 memset(buffer, 0, sizeof(buffer));
1268 if (count > sizeof(buffer) - 1)
1269 count = sizeof(buffer) - 1;
1270 if (copy_from_user(buffer, buf, count))
1272 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1275 if (make_it_fail < 0 || make_it_fail > 1)
1278 task = get_proc_task(file_inode(file));
1281 task->make_it_fail = make_it_fail;
1282 put_task_struct(task);
1287 static const struct file_operations proc_fault_inject_operations = {
1288 .read = proc_fault_inject_read,
1289 .write = proc_fault_inject_write,
1290 .llseek = generic_file_llseek,
1293 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1294 size_t count, loff_t *ppos)
1296 struct task_struct *task;
1300 err = kstrtouint_from_user(buf, count, 0, &n);
1304 task = get_proc_task(file_inode(file));
1308 put_task_struct(task);
1313 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1314 size_t count, loff_t *ppos)
1316 struct task_struct *task;
1317 char numbuf[PROC_NUMBUF];
1320 task = get_proc_task(file_inode(file));
1323 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1324 put_task_struct(task);
1325 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1328 static const struct file_operations proc_fail_nth_operations = {
1329 .read = proc_fail_nth_read,
1330 .write = proc_fail_nth_write,
1335 #ifdef CONFIG_SCHED_DEBUG
1337 * Print out various scheduling related per-task fields:
1339 static int sched_show(struct seq_file *m, void *v)
1341 struct inode *inode = m->private;
1342 struct pid_namespace *ns = proc_pid_ns(inode);
1343 struct task_struct *p;
1345 p = get_proc_task(inode);
1348 proc_sched_show_task(p, ns, m);
1356 sched_write(struct file *file, const char __user *buf,
1357 size_t count, loff_t *offset)
1359 struct inode *inode = file_inode(file);
1360 struct task_struct *p;
1362 p = get_proc_task(inode);
1365 proc_sched_set_task(p);
1372 static int sched_open(struct inode *inode, struct file *filp)
1374 return single_open(filp, sched_show, inode);
1377 static const struct file_operations proc_pid_sched_operations = {
1380 .write = sched_write,
1381 .llseek = seq_lseek,
1382 .release = single_release,
1387 #ifdef CONFIG_SCHED_AUTOGROUP
1389 * Print out autogroup related information:
1391 static int sched_autogroup_show(struct seq_file *m, void *v)
1393 struct inode *inode = m->private;
1394 struct task_struct *p;
1396 p = get_proc_task(inode);
1399 proc_sched_autogroup_show_task(p, m);
1407 sched_autogroup_write(struct file *file, const char __user *buf,
1408 size_t count, loff_t *offset)
1410 struct inode *inode = file_inode(file);
1411 struct task_struct *p;
1412 char buffer[PROC_NUMBUF];
1416 memset(buffer, 0, sizeof(buffer));
1417 if (count > sizeof(buffer) - 1)
1418 count = sizeof(buffer) - 1;
1419 if (copy_from_user(buffer, buf, count))
1422 err = kstrtoint(strstrip(buffer), 0, &nice);
1426 p = get_proc_task(inode);
1430 err = proc_sched_autogroup_set_nice(p, nice);
1439 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1443 ret = single_open(filp, sched_autogroup_show, NULL);
1445 struct seq_file *m = filp->private_data;
1452 static const struct file_operations proc_pid_sched_autogroup_operations = {
1453 .open = sched_autogroup_open,
1455 .write = sched_autogroup_write,
1456 .llseek = seq_lseek,
1457 .release = single_release,
1460 #endif /* CONFIG_SCHED_AUTOGROUP */
1462 static ssize_t comm_write(struct file *file, const char __user *buf,
1463 size_t count, loff_t *offset)
1465 struct inode *inode = file_inode(file);
1466 struct task_struct *p;
1467 char buffer[TASK_COMM_LEN];
1468 const size_t maxlen = sizeof(buffer) - 1;
1470 memset(buffer, 0, sizeof(buffer));
1471 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1474 p = get_proc_task(inode);
1478 if (same_thread_group(current, p))
1479 set_task_comm(p, buffer);
1488 static int comm_show(struct seq_file *m, void *v)
1490 struct inode *inode = m->private;
1491 struct task_struct *p;
1493 p = get_proc_task(inode);
1497 proc_task_name(m, p, false);
1505 static int comm_open(struct inode *inode, struct file *filp)
1507 return single_open(filp, comm_show, inode);
1510 static const struct file_operations proc_pid_set_comm_operations = {
1513 .write = comm_write,
1514 .llseek = seq_lseek,
1515 .release = single_release,
1518 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1520 struct task_struct *task;
1521 struct file *exe_file;
1523 task = get_proc_task(d_inode(dentry));
1526 exe_file = get_task_exe_file(task);
1527 put_task_struct(task);
1529 *exe_path = exe_file->f_path;
1530 path_get(&exe_file->f_path);
1537 static const char *proc_pid_get_link(struct dentry *dentry,
1538 struct inode *inode,
1539 struct delayed_call *done)
1542 int error = -EACCES;
1545 return ERR_PTR(-ECHILD);
1547 /* Are we allowed to snoop on the tasks file descriptors? */
1548 if (!proc_fd_access_allowed(inode))
1551 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1555 nd_jump_link(&path);
1558 return ERR_PTR(error);
1561 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1563 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1570 pathname = d_path(path, tmp, PAGE_SIZE);
1571 len = PTR_ERR(pathname);
1572 if (IS_ERR(pathname))
1574 len = tmp + PAGE_SIZE - 1 - pathname;
1578 if (copy_to_user(buffer, pathname, len))
1581 free_page((unsigned long)tmp);
1585 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1587 int error = -EACCES;
1588 struct inode *inode = d_inode(dentry);
1591 /* Are we allowed to snoop on the tasks file descriptors? */
1592 if (!proc_fd_access_allowed(inode))
1595 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1599 error = do_proc_readlink(&path, buffer, buflen);
1605 const struct inode_operations proc_pid_link_inode_operations = {
1606 .readlink = proc_pid_readlink,
1607 .get_link = proc_pid_get_link,
1608 .setattr = proc_setattr,
1612 /* building an inode */
1614 void task_dump_owner(struct task_struct *task, umode_t mode,
1615 kuid_t *ruid, kgid_t *rgid)
1617 /* Depending on the state of dumpable compute who should own a
1618 * proc file for a task.
1620 const struct cred *cred;
1624 if (unlikely(task->flags & PF_KTHREAD)) {
1625 *ruid = GLOBAL_ROOT_UID;
1626 *rgid = GLOBAL_ROOT_GID;
1630 /* Default to the tasks effective ownership */
1632 cred = __task_cred(task);
1638 * Before the /proc/pid/status file was created the only way to read
1639 * the effective uid of a /process was to stat /proc/pid. Reading
1640 * /proc/pid/status is slow enough that procps and other packages
1641 * kept stating /proc/pid. To keep the rules in /proc simple I have
1642 * made this apply to all per process world readable and executable
1645 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1646 struct mm_struct *mm;
1649 /* Make non-dumpable tasks owned by some root */
1651 if (get_dumpable(mm) != SUID_DUMP_USER) {
1652 struct user_namespace *user_ns = mm->user_ns;
1654 uid = make_kuid(user_ns, 0);
1655 if (!uid_valid(uid))
1656 uid = GLOBAL_ROOT_UID;
1658 gid = make_kgid(user_ns, 0);
1659 if (!gid_valid(gid))
1660 gid = GLOBAL_ROOT_GID;
1663 uid = GLOBAL_ROOT_UID;
1664 gid = GLOBAL_ROOT_GID;
1672 struct inode *proc_pid_make_inode(struct super_block * sb,
1673 struct task_struct *task, umode_t mode)
1675 struct inode * inode;
1676 struct proc_inode *ei;
1678 /* We need a new inode */
1680 inode = new_inode(sb);
1686 inode->i_mode = mode;
1687 inode->i_ino = get_next_ino();
1688 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1689 inode->i_op = &proc_def_inode_operations;
1692 * grab the reference to task.
1694 ei->pid = get_task_pid(task, PIDTYPE_PID);
1698 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1699 security_task_to_inode(task, inode);
1709 int pid_getattr(const struct path *path, struct kstat *stat,
1710 u32 request_mask, unsigned int query_flags)
1712 struct inode *inode = d_inode(path->dentry);
1713 struct pid_namespace *pid = proc_pid_ns(inode);
1714 struct task_struct *task;
1716 generic_fillattr(inode, stat);
1718 stat->uid = GLOBAL_ROOT_UID;
1719 stat->gid = GLOBAL_ROOT_GID;
1721 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1723 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1726 * This doesn't prevent learning whether PID exists,
1727 * it only makes getattr() consistent with readdir().
1731 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1740 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1742 void pid_update_inode(struct task_struct *task, struct inode *inode)
1744 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1746 inode->i_mode &= ~(S_ISUID | S_ISGID);
1747 security_task_to_inode(task, inode);
1751 * Rewrite the inode's ownerships here because the owning task may have
1752 * performed a setuid(), etc.
1755 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1757 struct inode *inode;
1758 struct task_struct *task;
1760 if (flags & LOOKUP_RCU)
1763 inode = d_inode(dentry);
1764 task = get_proc_task(inode);
1767 pid_update_inode(task, inode);
1768 put_task_struct(task);
1774 static inline bool proc_inode_is_dead(struct inode *inode)
1776 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1779 int pid_delete_dentry(const struct dentry *dentry)
1781 /* Is the task we represent dead?
1782 * If so, then don't put the dentry on the lru list,
1783 * kill it immediately.
1785 return proc_inode_is_dead(d_inode(dentry));
1788 const struct dentry_operations pid_dentry_operations =
1790 .d_revalidate = pid_revalidate,
1791 .d_delete = pid_delete_dentry,
1797 * Fill a directory entry.
1799 * If possible create the dcache entry and derive our inode number and
1800 * file type from dcache entry.
1802 * Since all of the proc inode numbers are dynamically generated, the inode
1803 * numbers do not exist until the inode is cache. This means creating the
1804 * the dcache entry in readdir is necessary to keep the inode numbers
1805 * reported by readdir in sync with the inode numbers reported
1808 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1809 const char *name, unsigned int len,
1810 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1812 struct dentry *child, *dir = file->f_path.dentry;
1813 struct qstr qname = QSTR_INIT(name, len);
1814 struct inode *inode;
1815 unsigned type = DT_UNKNOWN;
1818 child = d_hash_and_lookup(dir, &qname);
1820 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1821 child = d_alloc_parallel(dir, &qname, &wq);
1823 goto end_instantiate;
1824 if (d_in_lookup(child)) {
1826 res = instantiate(child, task, ptr);
1827 d_lookup_done(child);
1828 if (unlikely(res)) {
1832 goto end_instantiate;
1836 inode = d_inode(child);
1838 type = inode->i_mode >> 12;
1841 return dir_emit(ctx, name, len, ino, type);
1845 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1846 * which represent vma start and end addresses.
1848 static int dname_to_vma_addr(struct dentry *dentry,
1849 unsigned long *start, unsigned long *end)
1851 const char *str = dentry->d_name.name;
1852 unsigned long long sval, eval;
1855 if (str[0] == '0' && str[1] != '-')
1857 len = _parse_integer(str, 16, &sval);
1858 if (len & KSTRTOX_OVERFLOW)
1860 if (sval != (unsigned long)sval)
1868 if (str[0] == '0' && str[1])
1870 len = _parse_integer(str, 16, &eval);
1871 if (len & KSTRTOX_OVERFLOW)
1873 if (eval != (unsigned long)eval)
1886 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1888 unsigned long vm_start, vm_end;
1889 bool exact_vma_exists = false;
1890 struct mm_struct *mm = NULL;
1891 struct task_struct *task;
1892 struct inode *inode;
1895 if (flags & LOOKUP_RCU)
1898 inode = d_inode(dentry);
1899 task = get_proc_task(inode);
1903 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1904 if (IS_ERR_OR_NULL(mm))
1907 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1908 status = down_read_killable(&mm->mmap_sem);
1910 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1912 up_read(&mm->mmap_sem);
1918 if (exact_vma_exists) {
1919 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1921 security_task_to_inode(task, inode);
1926 put_task_struct(task);
1932 static const struct dentry_operations tid_map_files_dentry_operations = {
1933 .d_revalidate = map_files_d_revalidate,
1934 .d_delete = pid_delete_dentry,
1937 static int map_files_get_link(struct dentry *dentry, struct path *path)
1939 unsigned long vm_start, vm_end;
1940 struct vm_area_struct *vma;
1941 struct task_struct *task;
1942 struct mm_struct *mm;
1946 task = get_proc_task(d_inode(dentry));
1950 mm = get_task_mm(task);
1951 put_task_struct(task);
1955 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1959 rc = down_read_killable(&mm->mmap_sem);
1964 vma = find_exact_vma(mm, vm_start, vm_end);
1965 if (vma && vma->vm_file) {
1966 *path = vma->vm_file->f_path;
1970 up_read(&mm->mmap_sem);
1978 struct map_files_info {
1979 unsigned long start;
1985 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1986 * symlinks may be used to bypass permissions on ancestor directories in the
1987 * path to the file in question.
1990 proc_map_files_get_link(struct dentry *dentry,
1991 struct inode *inode,
1992 struct delayed_call *done)
1994 if (!capable(CAP_SYS_ADMIN))
1995 return ERR_PTR(-EPERM);
1997 return proc_pid_get_link(dentry, inode, done);
2001 * Identical to proc_pid_link_inode_operations except for get_link()
2003 static const struct inode_operations proc_map_files_link_inode_operations = {
2004 .readlink = proc_pid_readlink,
2005 .get_link = proc_map_files_get_link,
2006 .setattr = proc_setattr,
2009 static struct dentry *
2010 proc_map_files_instantiate(struct dentry *dentry,
2011 struct task_struct *task, const void *ptr)
2013 fmode_t mode = (fmode_t)(unsigned long)ptr;
2014 struct proc_inode *ei;
2015 struct inode *inode;
2017 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2018 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2019 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2021 return ERR_PTR(-ENOENT);
2024 ei->op.proc_get_link = map_files_get_link;
2026 inode->i_op = &proc_map_files_link_inode_operations;
2029 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2030 return d_splice_alias(inode, dentry);
2033 static struct dentry *proc_map_files_lookup(struct inode *dir,
2034 struct dentry *dentry, unsigned int flags)
2036 unsigned long vm_start, vm_end;
2037 struct vm_area_struct *vma;
2038 struct task_struct *task;
2039 struct dentry *result;
2040 struct mm_struct *mm;
2042 result = ERR_PTR(-ENOENT);
2043 task = get_proc_task(dir);
2047 result = ERR_PTR(-EACCES);
2048 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2051 result = ERR_PTR(-ENOENT);
2052 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2055 mm = get_task_mm(task);
2059 result = ERR_PTR(-EINTR);
2060 if (down_read_killable(&mm->mmap_sem))
2063 result = ERR_PTR(-ENOENT);
2064 vma = find_exact_vma(mm, vm_start, vm_end);
2069 result = proc_map_files_instantiate(dentry, task,
2070 (void *)(unsigned long)vma->vm_file->f_mode);
2073 up_read(&mm->mmap_sem);
2077 put_task_struct(task);
2082 static const struct inode_operations proc_map_files_inode_operations = {
2083 .lookup = proc_map_files_lookup,
2084 .permission = proc_fd_permission,
2085 .setattr = proc_setattr,
2089 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2091 struct vm_area_struct *vma;
2092 struct task_struct *task;
2093 struct mm_struct *mm;
2094 unsigned long nr_files, pos, i;
2095 struct flex_array *fa = NULL;
2096 struct map_files_info info;
2097 struct map_files_info *p;
2101 task = get_proc_task(file_inode(file));
2106 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2110 if (!dir_emit_dots(file, ctx))
2113 mm = get_task_mm(task);
2117 ret = down_read_killable(&mm->mmap_sem);
2126 * We need two passes here:
2128 * 1) Collect vmas of mapped files with mmap_sem taken
2129 * 2) Release mmap_sem and instantiate entries
2131 * otherwise we get lockdep complained, since filldir()
2132 * routine might require mmap_sem taken in might_fault().
2135 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2136 if (vma->vm_file && ++pos > ctx->pos)
2141 fa = flex_array_alloc(sizeof(info), nr_files,
2143 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2147 flex_array_free(fa);
2148 up_read(&mm->mmap_sem);
2152 for (i = 0, vma = mm->mmap, pos = 2; vma;
2153 vma = vma->vm_next) {
2156 if (++pos <= ctx->pos)
2159 info.start = vma->vm_start;
2160 info.end = vma->vm_end;
2161 info.mode = vma->vm_file->f_mode;
2162 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2166 up_read(&mm->mmap_sem);
2169 for (i = 0; i < nr_files; i++) {
2170 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2173 p = flex_array_get(fa, i);
2174 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2175 if (!proc_fill_cache(file, ctx,
2177 proc_map_files_instantiate,
2179 (void *)(unsigned long)p->mode))
2184 flex_array_free(fa);
2187 put_task_struct(task);
2192 static const struct file_operations proc_map_files_operations = {
2193 .read = generic_read_dir,
2194 .iterate_shared = proc_map_files_readdir,
2195 .llseek = generic_file_llseek,
2198 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2199 struct timers_private {
2201 struct task_struct *task;
2202 struct sighand_struct *sighand;
2203 struct pid_namespace *ns;
2204 unsigned long flags;
2207 static void *timers_start(struct seq_file *m, loff_t *pos)
2209 struct timers_private *tp = m->private;
2211 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2213 return ERR_PTR(-ESRCH);
2215 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2217 return ERR_PTR(-ESRCH);
2219 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2222 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2224 struct timers_private *tp = m->private;
2225 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2228 static void timers_stop(struct seq_file *m, void *v)
2230 struct timers_private *tp = m->private;
2233 unlock_task_sighand(tp->task, &tp->flags);
2238 put_task_struct(tp->task);
2243 static int show_timer(struct seq_file *m, void *v)
2245 struct k_itimer *timer;
2246 struct timers_private *tp = m->private;
2248 static const char * const nstr[] = {
2249 [SIGEV_SIGNAL] = "signal",
2250 [SIGEV_NONE] = "none",
2251 [SIGEV_THREAD] = "thread",
2254 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2255 notify = timer->it_sigev_notify;
2257 seq_printf(m, "ID: %d\n", timer->it_id);
2258 seq_printf(m, "signal: %d/%px\n",
2259 timer->sigq->info.si_signo,
2260 timer->sigq->info.si_value.sival_ptr);
2261 seq_printf(m, "notify: %s/%s.%d\n",
2262 nstr[notify & ~SIGEV_THREAD_ID],
2263 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2264 pid_nr_ns(timer->it_pid, tp->ns));
2265 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2270 static const struct seq_operations proc_timers_seq_ops = {
2271 .start = timers_start,
2272 .next = timers_next,
2273 .stop = timers_stop,
2277 static int proc_timers_open(struct inode *inode, struct file *file)
2279 struct timers_private *tp;
2281 tp = __seq_open_private(file, &proc_timers_seq_ops,
2282 sizeof(struct timers_private));
2286 tp->pid = proc_pid(inode);
2287 tp->ns = proc_pid_ns(inode);
2291 static const struct file_operations proc_timers_operations = {
2292 .open = proc_timers_open,
2294 .llseek = seq_lseek,
2295 .release = seq_release_private,
2299 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2300 size_t count, loff_t *offset)
2302 struct inode *inode = file_inode(file);
2303 struct task_struct *p;
2307 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2311 p = get_proc_task(inode);
2316 if (!capable(CAP_SYS_NICE)) {
2321 err = security_task_setscheduler(p);
2330 p->timer_slack_ns = p->default_timer_slack_ns;
2332 p->timer_slack_ns = slack_ns;
2341 static int timerslack_ns_show(struct seq_file *m, void *v)
2343 struct inode *inode = m->private;
2344 struct task_struct *p;
2347 p = get_proc_task(inode);
2353 if (!capable(CAP_SYS_NICE)) {
2357 err = security_task_getscheduler(p);
2363 seq_printf(m, "%llu\n", p->timer_slack_ns);
2372 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2374 return single_open(filp, timerslack_ns_show, inode);
2377 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2378 .open = timerslack_ns_open,
2380 .write = timerslack_ns_write,
2381 .llseek = seq_lseek,
2382 .release = single_release,
2385 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2386 struct task_struct *task, const void *ptr)
2388 const struct pid_entry *p = ptr;
2389 struct inode *inode;
2390 struct proc_inode *ei;
2392 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2394 return ERR_PTR(-ENOENT);
2397 if (S_ISDIR(inode->i_mode))
2398 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2400 inode->i_op = p->iop;
2402 inode->i_fop = p->fop;
2404 pid_update_inode(task, inode);
2405 d_set_d_op(dentry, &pid_dentry_operations);
2406 return d_splice_alias(inode, dentry);
2409 static struct dentry *proc_pident_lookup(struct inode *dir,
2410 struct dentry *dentry,
2411 const struct pid_entry *ents,
2414 struct task_struct *task = get_proc_task(dir);
2415 const struct pid_entry *p, *last;
2416 struct dentry *res = ERR_PTR(-ENOENT);
2422 * Yes, it does not scale. And it should not. Don't add
2423 * new entries into /proc/<tgid>/ without very good reasons.
2425 last = &ents[nents];
2426 for (p = ents; p < last; p++) {
2427 if (p->len != dentry->d_name.len)
2429 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2430 res = proc_pident_instantiate(dentry, task, p);
2434 put_task_struct(task);
2439 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2440 const struct pid_entry *ents, unsigned int nents)
2442 struct task_struct *task = get_proc_task(file_inode(file));
2443 const struct pid_entry *p;
2448 if (!dir_emit_dots(file, ctx))
2451 if (ctx->pos >= nents + 2)
2454 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2455 if (!proc_fill_cache(file, ctx, p->name, p->len,
2456 proc_pident_instantiate, task, p))
2461 put_task_struct(task);
2465 #ifdef CONFIG_SECURITY
2466 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2467 size_t count, loff_t *ppos)
2469 struct inode * inode = file_inode(file);
2472 struct task_struct *task = get_proc_task(inode);
2477 length = security_getprocattr(task,
2478 (char*)file->f_path.dentry->d_name.name,
2480 put_task_struct(task);
2482 length = simple_read_from_buffer(buf, count, ppos, p, length);
2487 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2488 size_t count, loff_t *ppos)
2490 struct inode * inode = file_inode(file);
2491 struct task_struct *task;
2496 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2501 /* A task may only write its own attributes. */
2502 if (current != task) {
2506 /* Prevent changes to overridden credentials. */
2507 if (current_cred() != current_real_cred()) {
2513 if (count > PAGE_SIZE)
2516 /* No partial writes. */
2520 page = memdup_user(buf, count);
2526 /* Guard against adverse ptrace interaction */
2527 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2531 rv = security_setprocattr(file->f_path.dentry->d_name.name, page, count);
2532 mutex_unlock(¤t->signal->cred_guard_mutex);
2539 static const struct file_operations proc_pid_attr_operations = {
2540 .read = proc_pid_attr_read,
2541 .write = proc_pid_attr_write,
2542 .llseek = generic_file_llseek,
2545 static const struct pid_entry attr_dir_stuff[] = {
2546 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2547 REG("prev", S_IRUGO, proc_pid_attr_operations),
2548 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2549 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2550 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2551 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2554 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2556 return proc_pident_readdir(file, ctx,
2557 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2560 static const struct file_operations proc_attr_dir_operations = {
2561 .read = generic_read_dir,
2562 .iterate_shared = proc_attr_dir_readdir,
2563 .llseek = generic_file_llseek,
2566 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2567 struct dentry *dentry, unsigned int flags)
2569 return proc_pident_lookup(dir, dentry,
2570 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2573 static const struct inode_operations proc_attr_dir_inode_operations = {
2574 .lookup = proc_attr_dir_lookup,
2575 .getattr = pid_getattr,
2576 .setattr = proc_setattr,
2581 #ifdef CONFIG_ELF_CORE
2582 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2583 size_t count, loff_t *ppos)
2585 struct task_struct *task = get_proc_task(file_inode(file));
2586 struct mm_struct *mm;
2587 char buffer[PROC_NUMBUF];
2595 mm = get_task_mm(task);
2597 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2598 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2599 MMF_DUMP_FILTER_SHIFT));
2601 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2604 put_task_struct(task);
2609 static ssize_t proc_coredump_filter_write(struct file *file,
2610 const char __user *buf,
2614 struct task_struct *task;
2615 struct mm_struct *mm;
2621 ret = kstrtouint_from_user(buf, count, 0, &val);
2626 task = get_proc_task(file_inode(file));
2630 mm = get_task_mm(task);
2635 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2637 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2639 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2644 put_task_struct(task);
2651 static const struct file_operations proc_coredump_filter_operations = {
2652 .read = proc_coredump_filter_read,
2653 .write = proc_coredump_filter_write,
2654 .llseek = generic_file_llseek,
2658 #ifdef CONFIG_TASK_IO_ACCOUNTING
2659 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2661 struct task_io_accounting acct = task->ioac;
2662 unsigned long flags;
2665 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2669 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2674 if (whole && lock_task_sighand(task, &flags)) {
2675 struct task_struct *t = task;
2677 task_io_accounting_add(&acct, &task->signal->ioac);
2678 while_each_thread(task, t)
2679 task_io_accounting_add(&acct, &t->ioac);
2681 unlock_task_sighand(task, &flags);
2688 "read_bytes: %llu\n"
2689 "write_bytes: %llu\n"
2690 "cancelled_write_bytes: %llu\n",
2691 (unsigned long long)acct.rchar,
2692 (unsigned long long)acct.wchar,
2693 (unsigned long long)acct.syscr,
2694 (unsigned long long)acct.syscw,
2695 (unsigned long long)acct.read_bytes,
2696 (unsigned long long)acct.write_bytes,
2697 (unsigned long long)acct.cancelled_write_bytes);
2701 mutex_unlock(&task->signal->cred_guard_mutex);
2705 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2706 struct pid *pid, struct task_struct *task)
2708 return do_io_accounting(task, m, 0);
2711 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2712 struct pid *pid, struct task_struct *task)
2714 return do_io_accounting(task, m, 1);
2716 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2718 #ifdef CONFIG_USER_NS
2719 static int proc_id_map_open(struct inode *inode, struct file *file,
2720 const struct seq_operations *seq_ops)
2722 struct user_namespace *ns = NULL;
2723 struct task_struct *task;
2724 struct seq_file *seq;
2727 task = get_proc_task(inode);
2730 ns = get_user_ns(task_cred_xxx(task, user_ns));
2732 put_task_struct(task);
2737 ret = seq_open(file, seq_ops);
2741 seq = file->private_data;
2751 static int proc_id_map_release(struct inode *inode, struct file *file)
2753 struct seq_file *seq = file->private_data;
2754 struct user_namespace *ns = seq->private;
2756 return seq_release(inode, file);
2759 static int proc_uid_map_open(struct inode *inode, struct file *file)
2761 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2764 static int proc_gid_map_open(struct inode *inode, struct file *file)
2766 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2769 static int proc_projid_map_open(struct inode *inode, struct file *file)
2771 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2774 static const struct file_operations proc_uid_map_operations = {
2775 .open = proc_uid_map_open,
2776 .write = proc_uid_map_write,
2778 .llseek = seq_lseek,
2779 .release = proc_id_map_release,
2782 static const struct file_operations proc_gid_map_operations = {
2783 .open = proc_gid_map_open,
2784 .write = proc_gid_map_write,
2786 .llseek = seq_lseek,
2787 .release = proc_id_map_release,
2790 static const struct file_operations proc_projid_map_operations = {
2791 .open = proc_projid_map_open,
2792 .write = proc_projid_map_write,
2794 .llseek = seq_lseek,
2795 .release = proc_id_map_release,
2798 static int proc_setgroups_open(struct inode *inode, struct file *file)
2800 struct user_namespace *ns = NULL;
2801 struct task_struct *task;
2805 task = get_proc_task(inode);
2808 ns = get_user_ns(task_cred_xxx(task, user_ns));
2810 put_task_struct(task);
2815 if (file->f_mode & FMODE_WRITE) {
2817 if (!ns_capable(ns, CAP_SYS_ADMIN))
2821 ret = single_open(file, &proc_setgroups_show, ns);
2832 static int proc_setgroups_release(struct inode *inode, struct file *file)
2834 struct seq_file *seq = file->private_data;
2835 struct user_namespace *ns = seq->private;
2836 int ret = single_release(inode, file);
2841 static const struct file_operations proc_setgroups_operations = {
2842 .open = proc_setgroups_open,
2843 .write = proc_setgroups_write,
2845 .llseek = seq_lseek,
2846 .release = proc_setgroups_release,
2848 #endif /* CONFIG_USER_NS */
2850 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2851 struct pid *pid, struct task_struct *task)
2853 int err = lock_trace(task);
2855 seq_printf(m, "%08x\n", task->personality);
2861 #ifdef CONFIG_LIVEPATCH
2862 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2863 struct pid *pid, struct task_struct *task)
2865 seq_printf(m, "%d\n", task->patch_state);
2868 #endif /* CONFIG_LIVEPATCH */
2873 static const struct file_operations proc_task_operations;
2874 static const struct inode_operations proc_task_inode_operations;
2876 static const struct pid_entry tgid_base_stuff[] = {
2877 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2878 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2879 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2880 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2881 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2883 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2885 REG("environ", S_IRUSR, proc_environ_operations),
2886 REG("auxv", S_IRUSR, proc_auxv_operations),
2887 ONE("status", S_IRUGO, proc_pid_status),
2888 ONE("personality", S_IRUSR, proc_pid_personality),
2889 ONE("limits", S_IRUGO, proc_pid_limits),
2890 #ifdef CONFIG_SCHED_DEBUG
2891 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2893 #ifdef CONFIG_SCHED_AUTOGROUP
2894 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2896 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2897 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2898 ONE("syscall", S_IRUSR, proc_pid_syscall),
2900 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2901 ONE("stat", S_IRUGO, proc_tgid_stat),
2902 ONE("statm", S_IRUGO, proc_pid_statm),
2903 REG("maps", S_IRUGO, proc_pid_maps_operations),
2905 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2907 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2908 LNK("cwd", proc_cwd_link),
2909 LNK("root", proc_root_link),
2910 LNK("exe", proc_exe_link),
2911 REG("mounts", S_IRUGO, proc_mounts_operations),
2912 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2913 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2914 #ifdef CONFIG_PROC_PAGE_MONITOR
2915 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2916 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2917 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2918 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2920 #ifdef CONFIG_SECURITY
2921 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2923 #ifdef CONFIG_KALLSYMS
2924 ONE("wchan", S_IRUGO, proc_pid_wchan),
2926 #ifdef CONFIG_STACKTRACE
2927 ONE("stack", S_IRUSR, proc_pid_stack),
2929 #ifdef CONFIG_SCHED_INFO
2930 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2932 #ifdef CONFIG_LATENCYTOP
2933 REG("latency", S_IRUGO, proc_lstats_operations),
2935 #ifdef CONFIG_PROC_PID_CPUSET
2936 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2938 #ifdef CONFIG_CGROUPS
2939 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2941 ONE("oom_score", S_IRUGO, proc_oom_score),
2942 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2943 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2944 #ifdef CONFIG_AUDITSYSCALL
2945 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2946 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2948 #ifdef CONFIG_FAULT_INJECTION
2949 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2950 REG("fail-nth", 0644, proc_fail_nth_operations),
2952 #ifdef CONFIG_ELF_CORE
2953 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2955 #ifdef CONFIG_TASK_IO_ACCOUNTING
2956 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2958 #ifdef CONFIG_USER_NS
2959 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2960 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2961 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2962 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2964 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2965 REG("timers", S_IRUGO, proc_timers_operations),
2967 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2968 #ifdef CONFIG_LIVEPATCH
2969 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2973 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2975 return proc_pident_readdir(file, ctx,
2976 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2979 static const struct file_operations proc_tgid_base_operations = {
2980 .read = generic_read_dir,
2981 .iterate_shared = proc_tgid_base_readdir,
2982 .llseek = generic_file_llseek,
2985 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2987 return proc_pident_lookup(dir, dentry,
2988 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2991 static const struct inode_operations proc_tgid_base_inode_operations = {
2992 .lookup = proc_tgid_base_lookup,
2993 .getattr = pid_getattr,
2994 .setattr = proc_setattr,
2995 .permission = proc_pid_permission,
2998 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3000 struct dentry *dentry, *leader, *dir;
3005 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3006 /* no ->d_hash() rejects on procfs */
3007 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3009 d_invalidate(dentry);
3017 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
3018 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3023 name.len = strlen(name.name);
3024 dir = d_hash_and_lookup(leader, &name);
3026 goto out_put_leader;
3029 name.len = snprintf(buf, sizeof(buf), "%u", pid);
3030 dentry = d_hash_and_lookup(dir, &name);
3032 d_invalidate(dentry);
3044 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3045 * @task: task that should be flushed.
3047 * When flushing dentries from proc, one needs to flush them from global
3048 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3049 * in. This call is supposed to do all of this job.
3051 * Looks in the dcache for
3053 * /proc/@tgid/task/@pid
3054 * if either directory is present flushes it and all of it'ts children
3057 * It is safe and reasonable to cache /proc entries for a task until
3058 * that task exits. After that they just clog up the dcache with
3059 * useless entries, possibly causing useful dcache entries to be
3060 * flushed instead. This routine is proved to flush those useless
3061 * dcache entries at process exit time.
3063 * NOTE: This routine is just an optimization so it does not guarantee
3064 * that no dcache entries will exist at process exit time it
3065 * just makes it very unlikely that any will persist.
3068 void proc_flush_task(struct task_struct *task)
3071 struct pid *pid, *tgid;
3074 pid = task_pid(task);
3075 tgid = task_tgid(task);
3077 for (i = 0; i <= pid->level; i++) {
3078 upid = &pid->numbers[i];
3079 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3080 tgid->numbers[i].nr);
3084 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3085 struct task_struct *task, const void *ptr)
3087 struct inode *inode;
3089 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3091 return ERR_PTR(-ENOENT);
3093 inode->i_op = &proc_tgid_base_inode_operations;
3094 inode->i_fop = &proc_tgid_base_operations;
3095 inode->i_flags|=S_IMMUTABLE;
3097 set_nlink(inode, nlink_tgid);
3098 pid_update_inode(task, inode);
3100 d_set_d_op(dentry, &pid_dentry_operations);
3101 return d_splice_alias(inode, dentry);
3104 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3106 struct task_struct *task;
3108 struct pid_namespace *ns;
3109 struct dentry *result = ERR_PTR(-ENOENT);
3111 tgid = name_to_int(&dentry->d_name);
3115 ns = dentry->d_sb->s_fs_info;
3117 task = find_task_by_pid_ns(tgid, ns);
3119 get_task_struct(task);
3124 result = proc_pid_instantiate(dentry, task, NULL);
3125 put_task_struct(task);
3131 * Find the first task with tgid >= tgid
3136 struct task_struct *task;
3138 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3143 put_task_struct(iter.task);
3147 pid = find_ge_pid(iter.tgid, ns);
3149 iter.tgid = pid_nr_ns(pid, ns);
3150 iter.task = pid_task(pid, PIDTYPE_PID);
3151 /* What we to know is if the pid we have find is the
3152 * pid of a thread_group_leader. Testing for task
3153 * being a thread_group_leader is the obvious thing
3154 * todo but there is a window when it fails, due to
3155 * the pid transfer logic in de_thread.
3157 * So we perform the straight forward test of seeing
3158 * if the pid we have found is the pid of a thread
3159 * group leader, and don't worry if the task we have
3160 * found doesn't happen to be a thread group leader.
3161 * As we don't care in the case of readdir.
3163 if (!iter.task || !has_group_leader_pid(iter.task)) {
3167 get_task_struct(iter.task);
3173 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3175 /* for the /proc/ directory itself, after non-process stuff has been done */
3176 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3178 struct tgid_iter iter;
3179 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3180 loff_t pos = ctx->pos;
3182 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3185 if (pos == TGID_OFFSET - 2) {
3186 struct inode *inode = d_inode(ns->proc_self);
3187 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3189 ctx->pos = pos = pos + 1;
3191 if (pos == TGID_OFFSET - 1) {
3192 struct inode *inode = d_inode(ns->proc_thread_self);
3193 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3195 ctx->pos = pos = pos + 1;
3197 iter.tgid = pos - TGID_OFFSET;
3199 for (iter = next_tgid(ns, iter);
3201 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3206 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3209 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3210 ctx->pos = iter.tgid + TGID_OFFSET;
3211 if (!proc_fill_cache(file, ctx, name, len,
3212 proc_pid_instantiate, iter.task, NULL)) {
3213 put_task_struct(iter.task);
3217 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3222 * proc_tid_comm_permission is a special permission function exclusively
3223 * used for the node /proc/<pid>/task/<tid>/comm.
3224 * It bypasses generic permission checks in the case where a task of the same
3225 * task group attempts to access the node.
3226 * The rationale behind this is that glibc and bionic access this node for
3227 * cross thread naming (pthread_set/getname_np(!self)). However, if
3228 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3229 * which locks out the cross thread naming implementation.
3230 * This function makes sure that the node is always accessible for members of
3231 * same thread group.
3233 static int proc_tid_comm_permission(struct inode *inode, int mask)
3235 bool is_same_tgroup;
3236 struct task_struct *task;
3238 task = get_proc_task(inode);
3241 is_same_tgroup = same_thread_group(current, task);
3242 put_task_struct(task);
3244 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3245 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3246 * read or written by the members of the corresponding
3252 return generic_permission(inode, mask);
3255 static const struct inode_operations proc_tid_comm_inode_operations = {
3256 .permission = proc_tid_comm_permission,
3262 static const struct pid_entry tid_base_stuff[] = {
3263 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3264 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3265 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3267 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3269 REG("environ", S_IRUSR, proc_environ_operations),
3270 REG("auxv", S_IRUSR, proc_auxv_operations),
3271 ONE("status", S_IRUGO, proc_pid_status),
3272 ONE("personality", S_IRUSR, proc_pid_personality),
3273 ONE("limits", S_IRUGO, proc_pid_limits),
3274 #ifdef CONFIG_SCHED_DEBUG
3275 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3277 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3278 &proc_tid_comm_inode_operations,
3279 &proc_pid_set_comm_operations, {}),
3280 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3281 ONE("syscall", S_IRUSR, proc_pid_syscall),
3283 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3284 ONE("stat", S_IRUGO, proc_tid_stat),
3285 ONE("statm", S_IRUGO, proc_pid_statm),
3286 REG("maps", S_IRUGO, proc_pid_maps_operations),
3287 #ifdef CONFIG_PROC_CHILDREN
3288 REG("children", S_IRUGO, proc_tid_children_operations),
3291 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3293 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3294 LNK("cwd", proc_cwd_link),
3295 LNK("root", proc_root_link),
3296 LNK("exe", proc_exe_link),
3297 REG("mounts", S_IRUGO, proc_mounts_operations),
3298 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3299 #ifdef CONFIG_PROC_PAGE_MONITOR
3300 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3301 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3302 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3303 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3305 #ifdef CONFIG_SECURITY
3306 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3308 #ifdef CONFIG_KALLSYMS
3309 ONE("wchan", S_IRUGO, proc_pid_wchan),
3311 #ifdef CONFIG_STACKTRACE
3312 ONE("stack", S_IRUSR, proc_pid_stack),
3314 #ifdef CONFIG_SCHED_INFO
3315 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3317 #ifdef CONFIG_LATENCYTOP
3318 REG("latency", S_IRUGO, proc_lstats_operations),
3320 #ifdef CONFIG_PROC_PID_CPUSET
3321 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3323 #ifdef CONFIG_CGROUPS
3324 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3326 ONE("oom_score", S_IRUGO, proc_oom_score),
3327 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3328 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3329 #ifdef CONFIG_AUDITSYSCALL
3330 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3331 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3333 #ifdef CONFIG_FAULT_INJECTION
3334 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3335 REG("fail-nth", 0644, proc_fail_nth_operations),
3337 #ifdef CONFIG_TASK_IO_ACCOUNTING
3338 ONE("io", S_IRUSR, proc_tid_io_accounting),
3340 #ifdef CONFIG_USER_NS
3341 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3342 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3343 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3344 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3346 #ifdef CONFIG_LIVEPATCH
3347 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3351 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3353 return proc_pident_readdir(file, ctx,
3354 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3357 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3359 return proc_pident_lookup(dir, dentry,
3360 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3363 static const struct file_operations proc_tid_base_operations = {
3364 .read = generic_read_dir,
3365 .iterate_shared = proc_tid_base_readdir,
3366 .llseek = generic_file_llseek,
3369 static const struct inode_operations proc_tid_base_inode_operations = {
3370 .lookup = proc_tid_base_lookup,
3371 .getattr = pid_getattr,
3372 .setattr = proc_setattr,
3375 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3376 struct task_struct *task, const void *ptr)
3378 struct inode *inode;
3379 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3381 return ERR_PTR(-ENOENT);
3383 inode->i_op = &proc_tid_base_inode_operations;
3384 inode->i_fop = &proc_tid_base_operations;
3385 inode->i_flags |= S_IMMUTABLE;
3387 set_nlink(inode, nlink_tid);
3388 pid_update_inode(task, inode);
3390 d_set_d_op(dentry, &pid_dentry_operations);
3391 return d_splice_alias(inode, dentry);
3394 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3396 struct task_struct *task;
3397 struct task_struct *leader = get_proc_task(dir);
3399 struct pid_namespace *ns;
3400 struct dentry *result = ERR_PTR(-ENOENT);
3405 tid = name_to_int(&dentry->d_name);
3409 ns = dentry->d_sb->s_fs_info;
3411 task = find_task_by_pid_ns(tid, ns);
3413 get_task_struct(task);
3417 if (!same_thread_group(leader, task))
3420 result = proc_task_instantiate(dentry, task, NULL);
3422 put_task_struct(task);
3424 put_task_struct(leader);
3430 * Find the first tid of a thread group to return to user space.
3432 * Usually this is just the thread group leader, but if the users
3433 * buffer was too small or there was a seek into the middle of the
3434 * directory we have more work todo.
3436 * In the case of a short read we start with find_task_by_pid.
3438 * In the case of a seek we start with the leader and walk nr
3441 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3442 struct pid_namespace *ns)
3444 struct task_struct *pos, *task;
3445 unsigned long nr = f_pos;
3447 if (nr != f_pos) /* 32bit overflow? */
3451 task = pid_task(pid, PIDTYPE_PID);
3455 /* Attempt to start with the tid of a thread */
3457 pos = find_task_by_pid_ns(tid, ns);
3458 if (pos && same_thread_group(pos, task))
3462 /* If nr exceeds the number of threads there is nothing todo */
3463 if (nr >= get_nr_threads(task))
3466 /* If we haven't found our starting place yet start
3467 * with the leader and walk nr threads forward.
3469 pos = task = task->group_leader;
3473 } while_each_thread(task, pos);
3478 get_task_struct(pos);
3485 * Find the next thread in the thread list.
3486 * Return NULL if there is an error or no next thread.
3488 * The reference to the input task_struct is released.
3490 static struct task_struct *next_tid(struct task_struct *start)
3492 struct task_struct *pos = NULL;
3494 if (pid_alive(start)) {
3495 pos = next_thread(start);
3496 if (thread_group_leader(pos))
3499 get_task_struct(pos);
3502 put_task_struct(start);
3506 /* for the /proc/TGID/task/ directories */
3507 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3509 struct inode *inode = file_inode(file);
3510 struct task_struct *task;
3511 struct pid_namespace *ns;
3514 if (proc_inode_is_dead(inode))
3517 if (!dir_emit_dots(file, ctx))
3520 /* f_version caches the tgid value that the last readdir call couldn't
3521 * return. lseek aka telldir automagically resets f_version to 0.
3523 ns = proc_pid_ns(inode);
3524 tid = (int)file->f_version;
3525 file->f_version = 0;
3526 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3528 task = next_tid(task), ctx->pos++) {
3531 tid = task_pid_nr_ns(task, ns);
3532 len = snprintf(name, sizeof(name), "%u", tid);
3533 if (!proc_fill_cache(file, ctx, name, len,
3534 proc_task_instantiate, task, NULL)) {
3535 /* returning this tgid failed, save it as the first
3536 * pid for the next readir call */
3537 file->f_version = (u64)tid;
3538 put_task_struct(task);
3546 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3547 u32 request_mask, unsigned int query_flags)
3549 struct inode *inode = d_inode(path->dentry);
3550 struct task_struct *p = get_proc_task(inode);
3551 generic_fillattr(inode, stat);
3554 stat->nlink += get_nr_threads(p);
3561 static const struct inode_operations proc_task_inode_operations = {
3562 .lookup = proc_task_lookup,
3563 .getattr = proc_task_getattr,
3564 .setattr = proc_setattr,
3565 .permission = proc_pid_permission,
3568 static const struct file_operations proc_task_operations = {
3569 .read = generic_read_dir,
3570 .iterate_shared = proc_task_readdir,
3571 .llseek = generic_file_llseek,
3574 void __init set_proc_pid_nlink(void)
3576 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3577 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));