gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / fs / proc / base.c
blob6042b646ab2797acaa50ad0023e932300a7a42af
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/proc/base.c
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.
17 * Changelog:
18 * 17-Jan-2005
19 * Allan Bezerra
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.
34 * Changelog:
35 * 21-Feb-2005
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
39 * ChangeLog:
40 * 10-Mar-2005
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
67 #include <linux/mm.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/tracehook.h>
78 #include <linux/printk.h>
79 #include <linux/cache.h>
80 #include <linux/cgroup.h>
81 #include <linux/cpuset.h>
82 #include <linux/audit.h>
83 #include <linux/poll.h>
84 #include <linux/nsproxy.h>
85 #include <linux/oom.h>
86 #include <linux/elf.h>
87 #include <linux/pid_namespace.h>
88 #include <linux/user_namespace.h>
89 #include <linux/fs_struct.h>
90 #include <linux/slab.h>
91 #include <linux/sched/autogroup.h>
92 #include <linux/sched/mm.h>
93 #include <linux/sched/coredump.h>
94 #include <linux/sched/debug.h>
95 #include <linux/sched/stat.h>
96 #include <linux/posix-timers.h>
97 #include <linux/time_namespace.h>
98 #include <linux/resctrl.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
101 #include "fd.h"
103 #include "../../lib/kstrtox.h"
105 /* NOTE:
106 * Implementing inode permission operations in /proc is almost
107 * certainly an error. Permission checks need to happen during
108 * each system call not at open time. The reason is that most of
109 * what we wish to check for permissions in /proc varies at runtime.
111 * The classic example of a problem is opening file descriptors
112 * in /proc for a task before it execs a suid executable.
115 static u8 nlink_tid __ro_after_init;
116 static u8 nlink_tgid __ro_after_init;
118 struct pid_entry {
119 const char *name;
120 unsigned int len;
121 umode_t mode;
122 const struct inode_operations *iop;
123 const struct file_operations *fop;
124 union proc_op op;
127 #define NOD(NAME, MODE, IOP, FOP, OP) { \
128 .name = (NAME), \
129 .len = sizeof(NAME) - 1, \
130 .mode = MODE, \
131 .iop = IOP, \
132 .fop = FOP, \
133 .op = OP, \
136 #define DIR(NAME, MODE, iops, fops) \
137 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138 #define LNK(NAME, get_link) \
139 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
140 &proc_pid_link_inode_operations, NULL, \
141 { .proc_get_link = get_link } )
142 #define REG(NAME, MODE, fops) \
143 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
144 #define ONE(NAME, MODE, show) \
145 NOD(NAME, (S_IFREG|(MODE)), \
146 NULL, &proc_single_file_operations, \
147 { .proc_show = show } )
148 #define ATTR(LSM, NAME, MODE) \
149 NOD(NAME, (S_IFREG|(MODE)), \
150 NULL, &proc_pid_attr_operations, \
151 { .lsm = LSM })
154 * Count the number of hardlinks for the pid_entry table, excluding the .
155 * and .. links.
157 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
158 unsigned int n)
160 unsigned int i;
161 unsigned int count;
163 count = 2;
164 for (i = 0; i < n; ++i) {
165 if (S_ISDIR(entries[i].mode))
166 ++count;
169 return count;
172 static int get_task_root(struct task_struct *task, struct path *root)
174 int result = -ENOENT;
176 task_lock(task);
177 if (task->fs) {
178 get_fs_root(task->fs, root);
179 result = 0;
181 task_unlock(task);
182 return result;
185 static int proc_cwd_link(struct dentry *dentry, struct path *path)
187 struct task_struct *task = get_proc_task(d_inode(dentry));
188 int result = -ENOENT;
190 if (task) {
191 task_lock(task);
192 if (task->fs) {
193 get_fs_pwd(task->fs, path);
194 result = 0;
196 task_unlock(task);
197 put_task_struct(task);
199 return result;
202 static int proc_root_link(struct dentry *dentry, struct path *path)
204 struct task_struct *task = get_proc_task(d_inode(dentry));
205 int result = -ENOENT;
207 if (task) {
208 result = get_task_root(task, path);
209 put_task_struct(task);
211 return result;
215 * If the user used setproctitle(), we just get the string from
216 * user space at arg_start, and limit it to a maximum of one page.
218 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
219 size_t count, unsigned long pos,
220 unsigned long arg_start)
222 char *page;
223 int ret, got;
225 if (pos >= PAGE_SIZE)
226 return 0;
228 page = (char *)__get_free_page(GFP_KERNEL);
229 if (!page)
230 return -ENOMEM;
232 ret = 0;
233 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
234 if (got > 0) {
235 int len = strnlen(page, got);
237 /* Include the NUL character if it was found */
238 if (len < got)
239 len++;
241 if (len > pos) {
242 len -= pos;
243 if (len > count)
244 len = count;
245 len -= copy_to_user(buf, page+pos, len);
246 if (!len)
247 len = -EFAULT;
248 ret = len;
251 free_page((unsigned long)page);
252 return ret;
255 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
256 size_t count, loff_t *ppos)
258 unsigned long arg_start, arg_end, env_start, env_end;
259 unsigned long pos, len;
260 char *page, c;
262 /* Check if process spawned far enough to have cmdline. */
263 if (!mm->env_end)
264 return 0;
266 spin_lock(&mm->arg_lock);
267 arg_start = mm->arg_start;
268 arg_end = mm->arg_end;
269 env_start = mm->env_start;
270 env_end = mm->env_end;
271 spin_unlock(&mm->arg_lock);
273 if (arg_start >= arg_end)
274 return 0;
277 * We allow setproctitle() to overwrite the argument
278 * strings, and overflow past the original end. But
279 * only when it overflows into the environment area.
281 if (env_start != arg_end || env_end < env_start)
282 env_start = env_end = arg_end;
283 len = env_end - arg_start;
285 /* We're not going to care if "*ppos" has high bits set */
286 pos = *ppos;
287 if (pos >= len)
288 return 0;
289 if (count > len - pos)
290 count = len - pos;
291 if (!count)
292 return 0;
295 * Magical special case: if the argv[] end byte is not
296 * zero, the user has overwritten it with setproctitle(3).
298 * Possible future enhancement: do this only once when
299 * pos is 0, and set a flag in the 'struct file'.
301 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
302 return get_mm_proctitle(mm, buf, count, pos, arg_start);
305 * For the non-setproctitle() case we limit things strictly
306 * to the [arg_start, arg_end[ range.
308 pos += arg_start;
309 if (pos < arg_start || pos >= arg_end)
310 return 0;
311 if (count > arg_end - pos)
312 count = arg_end - pos;
314 page = (char *)__get_free_page(GFP_KERNEL);
315 if (!page)
316 return -ENOMEM;
318 len = 0;
319 while (count) {
320 int got;
321 size_t size = min_t(size_t, PAGE_SIZE, count);
323 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
324 if (got <= 0)
325 break;
326 got -= copy_to_user(buf, page, got);
327 if (unlikely(!got)) {
328 if (!len)
329 len = -EFAULT;
330 break;
332 pos += got;
333 buf += got;
334 len += got;
335 count -= got;
338 free_page((unsigned long)page);
339 return len;
342 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
343 size_t count, loff_t *pos)
345 struct mm_struct *mm;
346 ssize_t ret;
348 mm = get_task_mm(tsk);
349 if (!mm)
350 return 0;
352 ret = get_mm_cmdline(mm, buf, count, pos);
353 mmput(mm);
354 return ret;
357 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
358 size_t count, loff_t *pos)
360 struct task_struct *tsk;
361 ssize_t ret;
363 BUG_ON(*pos < 0);
365 tsk = get_proc_task(file_inode(file));
366 if (!tsk)
367 return -ESRCH;
368 ret = get_task_cmdline(tsk, buf, count, pos);
369 put_task_struct(tsk);
370 if (ret > 0)
371 *pos += ret;
372 return ret;
375 static const struct file_operations proc_pid_cmdline_ops = {
376 .read = proc_pid_cmdline_read,
377 .llseek = generic_file_llseek,
380 #ifdef CONFIG_KALLSYMS
382 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
383 * Returns the resolved symbol. If that fails, simply return the address.
385 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
386 struct pid *pid, struct task_struct *task)
388 unsigned long wchan;
389 char symname[KSYM_NAME_LEN];
391 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
392 goto print0;
394 wchan = get_wchan(task);
395 if (wchan && !lookup_symbol_name(wchan, symname)) {
396 seq_puts(m, symname);
397 return 0;
400 print0:
401 seq_putc(m, '0');
402 return 0;
404 #endif /* CONFIG_KALLSYMS */
406 static int lock_trace(struct task_struct *task)
408 int err = mutex_lock_killable(&task->signal->exec_update_mutex);
409 if (err)
410 return err;
411 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
412 mutex_unlock(&task->signal->exec_update_mutex);
413 return -EPERM;
415 return 0;
418 static void unlock_trace(struct task_struct *task)
420 mutex_unlock(&task->signal->exec_update_mutex);
423 #ifdef CONFIG_STACKTRACE
425 #define MAX_STACK_TRACE_DEPTH 64
427 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
428 struct pid *pid, struct task_struct *task)
430 unsigned long *entries;
431 int err;
434 * The ability to racily run the kernel stack unwinder on a running task
435 * and then observe the unwinder output is scary; while it is useful for
436 * debugging kernel issues, it can also allow an attacker to leak kernel
437 * stack contents.
438 * Doing this in a manner that is at least safe from races would require
439 * some work to ensure that the remote task can not be scheduled; and
440 * even then, this would still expose the unwinder as local attack
441 * surface.
442 * Therefore, this interface is restricted to root.
444 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
445 return -EACCES;
447 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
448 GFP_KERNEL);
449 if (!entries)
450 return -ENOMEM;
452 err = lock_trace(task);
453 if (!err) {
454 unsigned int i, nr_entries;
456 nr_entries = stack_trace_save_tsk(task, entries,
457 MAX_STACK_TRACE_DEPTH, 0);
459 for (i = 0; i < nr_entries; i++) {
460 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
463 unlock_trace(task);
465 kfree(entries);
467 return err;
469 #endif
471 #ifdef CONFIG_SCHED_INFO
473 * Provides /proc/PID/schedstat
475 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
476 struct pid *pid, struct task_struct *task)
478 if (unlikely(!sched_info_on()))
479 seq_puts(m, "0 0 0\n");
480 else
481 seq_printf(m, "%llu %llu %lu\n",
482 (unsigned long long)task->se.sum_exec_runtime,
483 (unsigned long long)task->sched_info.run_delay,
484 task->sched_info.pcount);
486 return 0;
488 #endif
490 #ifdef CONFIG_LATENCYTOP
491 static int lstats_show_proc(struct seq_file *m, void *v)
493 int i;
494 struct inode *inode = m->private;
495 struct task_struct *task = get_proc_task(inode);
497 if (!task)
498 return -ESRCH;
499 seq_puts(m, "Latency Top version : v0.1\n");
500 for (i = 0; i < LT_SAVECOUNT; i++) {
501 struct latency_record *lr = &task->latency_record[i];
502 if (lr->backtrace[0]) {
503 int q;
504 seq_printf(m, "%i %li %li",
505 lr->count, lr->time, lr->max);
506 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
507 unsigned long bt = lr->backtrace[q];
509 if (!bt)
510 break;
511 seq_printf(m, " %ps", (void *)bt);
513 seq_putc(m, '\n');
517 put_task_struct(task);
518 return 0;
521 static int lstats_open(struct inode *inode, struct file *file)
523 return single_open(file, lstats_show_proc, inode);
526 static ssize_t lstats_write(struct file *file, const char __user *buf,
527 size_t count, loff_t *offs)
529 struct task_struct *task = get_proc_task(file_inode(file));
531 if (!task)
532 return -ESRCH;
533 clear_tsk_latency_tracing(task);
534 put_task_struct(task);
536 return count;
539 static const struct file_operations proc_lstats_operations = {
540 .open = lstats_open,
541 .read = seq_read,
542 .write = lstats_write,
543 .llseek = seq_lseek,
544 .release = single_release,
547 #endif
549 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
550 struct pid *pid, struct task_struct *task)
552 unsigned long totalpages = totalram_pages() + total_swap_pages;
553 unsigned long points = 0;
555 points = oom_badness(task, totalpages) * 1000 / totalpages;
556 seq_printf(m, "%lu\n", points);
558 return 0;
561 struct limit_names {
562 const char *name;
563 const char *unit;
566 static const struct limit_names lnames[RLIM_NLIMITS] = {
567 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
568 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
569 [RLIMIT_DATA] = {"Max data size", "bytes"},
570 [RLIMIT_STACK] = {"Max stack size", "bytes"},
571 [RLIMIT_CORE] = {"Max core file size", "bytes"},
572 [RLIMIT_RSS] = {"Max resident set", "bytes"},
573 [RLIMIT_NPROC] = {"Max processes", "processes"},
574 [RLIMIT_NOFILE] = {"Max open files", "files"},
575 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
576 [RLIMIT_AS] = {"Max address space", "bytes"},
577 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
578 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
579 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
580 [RLIMIT_NICE] = {"Max nice priority", NULL},
581 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
582 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
585 /* Display limits for a process */
586 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
587 struct pid *pid, struct task_struct *task)
589 unsigned int i;
590 unsigned long flags;
592 struct rlimit rlim[RLIM_NLIMITS];
594 if (!lock_task_sighand(task, &flags))
595 return 0;
596 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
597 unlock_task_sighand(task, &flags);
600 * print the file header
602 seq_puts(m, "Limit "
603 "Soft Limit "
604 "Hard Limit "
605 "Units \n");
607 for (i = 0; i < RLIM_NLIMITS; i++) {
608 if (rlim[i].rlim_cur == RLIM_INFINITY)
609 seq_printf(m, "%-25s %-20s ",
610 lnames[i].name, "unlimited");
611 else
612 seq_printf(m, "%-25s %-20lu ",
613 lnames[i].name, rlim[i].rlim_cur);
615 if (rlim[i].rlim_max == RLIM_INFINITY)
616 seq_printf(m, "%-20s ", "unlimited");
617 else
618 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
620 if (lnames[i].unit)
621 seq_printf(m, "%-10s\n", lnames[i].unit);
622 else
623 seq_putc(m, '\n');
626 return 0;
629 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
630 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
631 struct pid *pid, struct task_struct *task)
633 struct syscall_info info;
634 u64 *args = &info.data.args[0];
635 int res;
637 res = lock_trace(task);
638 if (res)
639 return res;
641 if (task_current_syscall(task, &info))
642 seq_puts(m, "running\n");
643 else if (info.data.nr < 0)
644 seq_printf(m, "%d 0x%llx 0x%llx\n",
645 info.data.nr, info.sp, info.data.instruction_pointer);
646 else
647 seq_printf(m,
648 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
649 info.data.nr,
650 args[0], args[1], args[2], args[3], args[4], args[5],
651 info.sp, info.data.instruction_pointer);
652 unlock_trace(task);
654 return 0;
656 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
658 /************************************************************************/
659 /* Here the fs part begins */
660 /************************************************************************/
662 /* permission checks */
663 static int proc_fd_access_allowed(struct inode *inode)
665 struct task_struct *task;
666 int allowed = 0;
667 /* Allow access to a task's file descriptors if it is us or we
668 * may use ptrace attach to the process and find out that
669 * information.
671 task = get_proc_task(inode);
672 if (task) {
673 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
674 put_task_struct(task);
676 return allowed;
679 int proc_setattr(struct dentry *dentry, struct iattr *attr)
681 int error;
682 struct inode *inode = d_inode(dentry);
684 if (attr->ia_valid & ATTR_MODE)
685 return -EPERM;
687 error = setattr_prepare(dentry, attr);
688 if (error)
689 return error;
691 setattr_copy(inode, attr);
692 mark_inode_dirty(inode);
693 return 0;
697 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
698 * or euid/egid (for hide_pid_min=2)?
700 static bool has_pid_permissions(struct pid_namespace *pid,
701 struct task_struct *task,
702 int hide_pid_min)
704 if (pid->hide_pid < hide_pid_min)
705 return true;
706 if (in_group_p(pid->pid_gid))
707 return true;
708 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
712 static int proc_pid_permission(struct inode *inode, int mask)
714 struct pid_namespace *pid = proc_pid_ns(inode);
715 struct task_struct *task;
716 bool has_perms;
718 task = get_proc_task(inode);
719 if (!task)
720 return -ESRCH;
721 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
722 put_task_struct(task);
724 if (!has_perms) {
725 if (pid->hide_pid == HIDEPID_INVISIBLE) {
727 * Let's make getdents(), stat(), and open()
728 * consistent with each other. If a process
729 * may not stat() a file, it shouldn't be seen
730 * in procfs at all.
732 return -ENOENT;
735 return -EPERM;
737 return generic_permission(inode, mask);
742 static const struct inode_operations proc_def_inode_operations = {
743 .setattr = proc_setattr,
746 static int proc_single_show(struct seq_file *m, void *v)
748 struct inode *inode = m->private;
749 struct pid_namespace *ns = proc_pid_ns(inode);
750 struct pid *pid = proc_pid(inode);
751 struct task_struct *task;
752 int ret;
754 task = get_pid_task(pid, PIDTYPE_PID);
755 if (!task)
756 return -ESRCH;
758 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
760 put_task_struct(task);
761 return ret;
764 static int proc_single_open(struct inode *inode, struct file *filp)
766 return single_open(filp, proc_single_show, inode);
769 static const struct file_operations proc_single_file_operations = {
770 .open = proc_single_open,
771 .read = seq_read,
772 .llseek = seq_lseek,
773 .release = single_release,
777 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
779 struct task_struct *task = get_proc_task(inode);
780 struct mm_struct *mm = ERR_PTR(-ESRCH);
782 if (task) {
783 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
784 put_task_struct(task);
786 if (!IS_ERR_OR_NULL(mm)) {
787 /* ensure this mm_struct can't be freed */
788 mmgrab(mm);
789 /* but do not pin its memory */
790 mmput(mm);
794 return mm;
797 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
799 struct mm_struct *mm = proc_mem_open(inode, mode);
801 if (IS_ERR(mm))
802 return PTR_ERR(mm);
804 file->private_data = mm;
805 return 0;
808 static int mem_open(struct inode *inode, struct file *file)
810 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
812 /* OK to pass negative loff_t, we can catch out-of-range */
813 file->f_mode |= FMODE_UNSIGNED_OFFSET;
815 return ret;
818 static ssize_t mem_rw(struct file *file, char __user *buf,
819 size_t count, loff_t *ppos, int write)
821 struct mm_struct *mm = file->private_data;
822 unsigned long addr = *ppos;
823 ssize_t copied;
824 char *page;
825 unsigned int flags;
827 if (!mm)
828 return 0;
830 page = (char *)__get_free_page(GFP_KERNEL);
831 if (!page)
832 return -ENOMEM;
834 copied = 0;
835 if (!mmget_not_zero(mm))
836 goto free;
838 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
840 while (count > 0) {
841 int this_len = min_t(int, count, PAGE_SIZE);
843 if (write && copy_from_user(page, buf, this_len)) {
844 copied = -EFAULT;
845 break;
848 this_len = access_remote_vm(mm, addr, page, this_len, flags);
849 if (!this_len) {
850 if (!copied)
851 copied = -EIO;
852 break;
855 if (!write && copy_to_user(buf, page, this_len)) {
856 copied = -EFAULT;
857 break;
860 buf += this_len;
861 addr += this_len;
862 copied += this_len;
863 count -= this_len;
865 *ppos = addr;
867 mmput(mm);
868 free:
869 free_page((unsigned long) page);
870 return copied;
873 static ssize_t mem_read(struct file *file, char __user *buf,
874 size_t count, loff_t *ppos)
876 return mem_rw(file, buf, count, ppos, 0);
879 static ssize_t mem_write(struct file *file, const char __user *buf,
880 size_t count, loff_t *ppos)
882 return mem_rw(file, (char __user*)buf, count, ppos, 1);
885 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
887 switch (orig) {
888 case 0:
889 file->f_pos = offset;
890 break;
891 case 1:
892 file->f_pos += offset;
893 break;
894 default:
895 return -EINVAL;
897 force_successful_syscall_return();
898 return file->f_pos;
901 static int mem_release(struct inode *inode, struct file *file)
903 struct mm_struct *mm = file->private_data;
904 if (mm)
905 mmdrop(mm);
906 return 0;
909 static const struct file_operations proc_mem_operations = {
910 .llseek = mem_lseek,
911 .read = mem_read,
912 .write = mem_write,
913 .open = mem_open,
914 .release = mem_release,
917 static int environ_open(struct inode *inode, struct file *file)
919 return __mem_open(inode, file, PTRACE_MODE_READ);
922 static ssize_t environ_read(struct file *file, char __user *buf,
923 size_t count, loff_t *ppos)
925 char *page;
926 unsigned long src = *ppos;
927 int ret = 0;
928 struct mm_struct *mm = file->private_data;
929 unsigned long env_start, env_end;
931 /* Ensure the process spawned far enough to have an environment. */
932 if (!mm || !mm->env_end)
933 return 0;
935 page = (char *)__get_free_page(GFP_KERNEL);
936 if (!page)
937 return -ENOMEM;
939 ret = 0;
940 if (!mmget_not_zero(mm))
941 goto free;
943 spin_lock(&mm->arg_lock);
944 env_start = mm->env_start;
945 env_end = mm->env_end;
946 spin_unlock(&mm->arg_lock);
948 while (count > 0) {
949 size_t this_len, max_len;
950 int retval;
952 if (src >= (env_end - env_start))
953 break;
955 this_len = env_end - (env_start + src);
957 max_len = min_t(size_t, PAGE_SIZE, count);
958 this_len = min(max_len, this_len);
960 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
962 if (retval <= 0) {
963 ret = retval;
964 break;
967 if (copy_to_user(buf, page, retval)) {
968 ret = -EFAULT;
969 break;
972 ret += retval;
973 src += retval;
974 buf += retval;
975 count -= retval;
977 *ppos = src;
978 mmput(mm);
980 free:
981 free_page((unsigned long) page);
982 return ret;
985 static const struct file_operations proc_environ_operations = {
986 .open = environ_open,
987 .read = environ_read,
988 .llseek = generic_file_llseek,
989 .release = mem_release,
992 static int auxv_open(struct inode *inode, struct file *file)
994 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
997 static ssize_t auxv_read(struct file *file, char __user *buf,
998 size_t count, loff_t *ppos)
1000 struct mm_struct *mm = file->private_data;
1001 unsigned int nwords = 0;
1003 if (!mm)
1004 return 0;
1005 do {
1006 nwords += 2;
1007 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1008 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1009 nwords * sizeof(mm->saved_auxv[0]));
1012 static const struct file_operations proc_auxv_operations = {
1013 .open = auxv_open,
1014 .read = auxv_read,
1015 .llseek = generic_file_llseek,
1016 .release = mem_release,
1019 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1020 loff_t *ppos)
1022 struct task_struct *task = get_proc_task(file_inode(file));
1023 char buffer[PROC_NUMBUF];
1024 int oom_adj = OOM_ADJUST_MIN;
1025 size_t len;
1027 if (!task)
1028 return -ESRCH;
1029 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1030 oom_adj = OOM_ADJUST_MAX;
1031 else
1032 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1033 OOM_SCORE_ADJ_MAX;
1034 put_task_struct(task);
1035 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1036 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1039 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1041 static DEFINE_MUTEX(oom_adj_mutex);
1042 struct mm_struct *mm = NULL;
1043 struct task_struct *task;
1044 int err = 0;
1046 task = get_proc_task(file_inode(file));
1047 if (!task)
1048 return -ESRCH;
1050 mutex_lock(&oom_adj_mutex);
1051 if (legacy) {
1052 if (oom_adj < task->signal->oom_score_adj &&
1053 !capable(CAP_SYS_RESOURCE)) {
1054 err = -EACCES;
1055 goto err_unlock;
1058 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1059 * /proc/pid/oom_score_adj instead.
1061 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1062 current->comm, task_pid_nr(current), task_pid_nr(task),
1063 task_pid_nr(task));
1064 } else {
1065 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1066 !capable(CAP_SYS_RESOURCE)) {
1067 err = -EACCES;
1068 goto err_unlock;
1073 * Make sure we will check other processes sharing the mm if this is
1074 * not vfrok which wants its own oom_score_adj.
1075 * pin the mm so it doesn't go away and get reused after task_unlock
1077 if (!task->vfork_done) {
1078 struct task_struct *p = find_lock_task_mm(task);
1080 if (p) {
1081 if (atomic_read(&p->mm->mm_users) > 1) {
1082 mm = p->mm;
1083 mmgrab(mm);
1085 task_unlock(p);
1089 task->signal->oom_score_adj = oom_adj;
1090 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1091 task->signal->oom_score_adj_min = (short)oom_adj;
1092 trace_oom_score_adj_update(task);
1094 if (mm) {
1095 struct task_struct *p;
1097 rcu_read_lock();
1098 for_each_process(p) {
1099 if (same_thread_group(task, p))
1100 continue;
1102 /* do not touch kernel threads or the global init */
1103 if (p->flags & PF_KTHREAD || is_global_init(p))
1104 continue;
1106 task_lock(p);
1107 if (!p->vfork_done && process_shares_mm(p, mm)) {
1108 p->signal->oom_score_adj = oom_adj;
1109 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1110 p->signal->oom_score_adj_min = (short)oom_adj;
1112 task_unlock(p);
1114 rcu_read_unlock();
1115 mmdrop(mm);
1117 err_unlock:
1118 mutex_unlock(&oom_adj_mutex);
1119 put_task_struct(task);
1120 return err;
1124 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1125 * kernels. The effective policy is defined by oom_score_adj, which has a
1126 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1127 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1128 * Processes that become oom disabled via oom_adj will still be oom disabled
1129 * with this implementation.
1131 * oom_adj cannot be removed since existing userspace binaries use it.
1133 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1134 size_t count, loff_t *ppos)
1136 char buffer[PROC_NUMBUF];
1137 int oom_adj;
1138 int err;
1140 memset(buffer, 0, sizeof(buffer));
1141 if (count > sizeof(buffer) - 1)
1142 count = sizeof(buffer) - 1;
1143 if (copy_from_user(buffer, buf, count)) {
1144 err = -EFAULT;
1145 goto out;
1148 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1149 if (err)
1150 goto out;
1151 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1152 oom_adj != OOM_DISABLE) {
1153 err = -EINVAL;
1154 goto out;
1158 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1159 * value is always attainable.
1161 if (oom_adj == OOM_ADJUST_MAX)
1162 oom_adj = OOM_SCORE_ADJ_MAX;
1163 else
1164 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1166 err = __set_oom_adj(file, oom_adj, true);
1167 out:
1168 return err < 0 ? err : count;
1171 static const struct file_operations proc_oom_adj_operations = {
1172 .read = oom_adj_read,
1173 .write = oom_adj_write,
1174 .llseek = generic_file_llseek,
1177 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1178 size_t count, loff_t *ppos)
1180 struct task_struct *task = get_proc_task(file_inode(file));
1181 char buffer[PROC_NUMBUF];
1182 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1183 size_t len;
1185 if (!task)
1186 return -ESRCH;
1187 oom_score_adj = task->signal->oom_score_adj;
1188 put_task_struct(task);
1189 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1190 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1193 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1194 size_t count, loff_t *ppos)
1196 char buffer[PROC_NUMBUF];
1197 int oom_score_adj;
1198 int err;
1200 memset(buffer, 0, sizeof(buffer));
1201 if (count > sizeof(buffer) - 1)
1202 count = sizeof(buffer) - 1;
1203 if (copy_from_user(buffer, buf, count)) {
1204 err = -EFAULT;
1205 goto out;
1208 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1209 if (err)
1210 goto out;
1211 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1212 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1213 err = -EINVAL;
1214 goto out;
1217 err = __set_oom_adj(file, oom_score_adj, false);
1218 out:
1219 return err < 0 ? err : count;
1222 static const struct file_operations proc_oom_score_adj_operations = {
1223 .read = oom_score_adj_read,
1224 .write = oom_score_adj_write,
1225 .llseek = default_llseek,
1228 #ifdef CONFIG_AUDIT
1229 #define TMPBUFLEN 11
1230 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1231 size_t count, loff_t *ppos)
1233 struct inode * inode = file_inode(file);
1234 struct task_struct *task = get_proc_task(inode);
1235 ssize_t length;
1236 char tmpbuf[TMPBUFLEN];
1238 if (!task)
1239 return -ESRCH;
1240 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1241 from_kuid(file->f_cred->user_ns,
1242 audit_get_loginuid(task)));
1243 put_task_struct(task);
1244 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1247 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1248 size_t count, loff_t *ppos)
1250 struct inode * inode = file_inode(file);
1251 uid_t loginuid;
1252 kuid_t kloginuid;
1253 int rv;
1255 rcu_read_lock();
1256 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1257 rcu_read_unlock();
1258 return -EPERM;
1260 rcu_read_unlock();
1262 if (*ppos != 0) {
1263 /* No partial writes. */
1264 return -EINVAL;
1267 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1268 if (rv < 0)
1269 return rv;
1271 /* is userspace tring to explicitly UNSET the loginuid? */
1272 if (loginuid == AUDIT_UID_UNSET) {
1273 kloginuid = INVALID_UID;
1274 } else {
1275 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1276 if (!uid_valid(kloginuid))
1277 return -EINVAL;
1280 rv = audit_set_loginuid(kloginuid);
1281 if (rv < 0)
1282 return rv;
1283 return count;
1286 static const struct file_operations proc_loginuid_operations = {
1287 .read = proc_loginuid_read,
1288 .write = proc_loginuid_write,
1289 .llseek = generic_file_llseek,
1292 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1293 size_t count, loff_t *ppos)
1295 struct inode * inode = file_inode(file);
1296 struct task_struct *task = get_proc_task(inode);
1297 ssize_t length;
1298 char tmpbuf[TMPBUFLEN];
1300 if (!task)
1301 return -ESRCH;
1302 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1303 audit_get_sessionid(task));
1304 put_task_struct(task);
1305 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1308 static const struct file_operations proc_sessionid_operations = {
1309 .read = proc_sessionid_read,
1310 .llseek = generic_file_llseek,
1312 #endif
1314 #ifdef CONFIG_FAULT_INJECTION
1315 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1316 size_t count, loff_t *ppos)
1318 struct task_struct *task = get_proc_task(file_inode(file));
1319 char buffer[PROC_NUMBUF];
1320 size_t len;
1321 int make_it_fail;
1323 if (!task)
1324 return -ESRCH;
1325 make_it_fail = task->make_it_fail;
1326 put_task_struct(task);
1328 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1330 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1333 static ssize_t proc_fault_inject_write(struct file * file,
1334 const char __user * buf, size_t count, loff_t *ppos)
1336 struct task_struct *task;
1337 char buffer[PROC_NUMBUF];
1338 int make_it_fail;
1339 int rv;
1341 if (!capable(CAP_SYS_RESOURCE))
1342 return -EPERM;
1343 memset(buffer, 0, sizeof(buffer));
1344 if (count > sizeof(buffer) - 1)
1345 count = sizeof(buffer) - 1;
1346 if (copy_from_user(buffer, buf, count))
1347 return -EFAULT;
1348 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1349 if (rv < 0)
1350 return rv;
1351 if (make_it_fail < 0 || make_it_fail > 1)
1352 return -EINVAL;
1354 task = get_proc_task(file_inode(file));
1355 if (!task)
1356 return -ESRCH;
1357 task->make_it_fail = make_it_fail;
1358 put_task_struct(task);
1360 return count;
1363 static const struct file_operations proc_fault_inject_operations = {
1364 .read = proc_fault_inject_read,
1365 .write = proc_fault_inject_write,
1366 .llseek = generic_file_llseek,
1369 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1370 size_t count, loff_t *ppos)
1372 struct task_struct *task;
1373 int err;
1374 unsigned int n;
1376 err = kstrtouint_from_user(buf, count, 0, &n);
1377 if (err)
1378 return err;
1380 task = get_proc_task(file_inode(file));
1381 if (!task)
1382 return -ESRCH;
1383 task->fail_nth = n;
1384 put_task_struct(task);
1386 return count;
1389 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1390 size_t count, loff_t *ppos)
1392 struct task_struct *task;
1393 char numbuf[PROC_NUMBUF];
1394 ssize_t len;
1396 task = get_proc_task(file_inode(file));
1397 if (!task)
1398 return -ESRCH;
1399 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1400 put_task_struct(task);
1401 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1404 static const struct file_operations proc_fail_nth_operations = {
1405 .read = proc_fail_nth_read,
1406 .write = proc_fail_nth_write,
1408 #endif
1411 #ifdef CONFIG_SCHED_DEBUG
1413 * Print out various scheduling related per-task fields:
1415 static int sched_show(struct seq_file *m, void *v)
1417 struct inode *inode = m->private;
1418 struct pid_namespace *ns = proc_pid_ns(inode);
1419 struct task_struct *p;
1421 p = get_proc_task(inode);
1422 if (!p)
1423 return -ESRCH;
1424 proc_sched_show_task(p, ns, m);
1426 put_task_struct(p);
1428 return 0;
1431 static ssize_t
1432 sched_write(struct file *file, const char __user *buf,
1433 size_t count, loff_t *offset)
1435 struct inode *inode = file_inode(file);
1436 struct task_struct *p;
1438 p = get_proc_task(inode);
1439 if (!p)
1440 return -ESRCH;
1441 proc_sched_set_task(p);
1443 put_task_struct(p);
1445 return count;
1448 static int sched_open(struct inode *inode, struct file *filp)
1450 return single_open(filp, sched_show, inode);
1453 static const struct file_operations proc_pid_sched_operations = {
1454 .open = sched_open,
1455 .read = seq_read,
1456 .write = sched_write,
1457 .llseek = seq_lseek,
1458 .release = single_release,
1461 #endif
1463 #ifdef CONFIG_SCHED_AUTOGROUP
1465 * Print out autogroup related information:
1467 static int sched_autogroup_show(struct seq_file *m, void *v)
1469 struct inode *inode = m->private;
1470 struct task_struct *p;
1472 p = get_proc_task(inode);
1473 if (!p)
1474 return -ESRCH;
1475 proc_sched_autogroup_show_task(p, m);
1477 put_task_struct(p);
1479 return 0;
1482 static ssize_t
1483 sched_autogroup_write(struct file *file, const char __user *buf,
1484 size_t count, loff_t *offset)
1486 struct inode *inode = file_inode(file);
1487 struct task_struct *p;
1488 char buffer[PROC_NUMBUF];
1489 int nice;
1490 int err;
1492 memset(buffer, 0, sizeof(buffer));
1493 if (count > sizeof(buffer) - 1)
1494 count = sizeof(buffer) - 1;
1495 if (copy_from_user(buffer, buf, count))
1496 return -EFAULT;
1498 err = kstrtoint(strstrip(buffer), 0, &nice);
1499 if (err < 0)
1500 return err;
1502 p = get_proc_task(inode);
1503 if (!p)
1504 return -ESRCH;
1506 err = proc_sched_autogroup_set_nice(p, nice);
1507 if (err)
1508 count = err;
1510 put_task_struct(p);
1512 return count;
1515 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1517 int ret;
1519 ret = single_open(filp, sched_autogroup_show, NULL);
1520 if (!ret) {
1521 struct seq_file *m = filp->private_data;
1523 m->private = inode;
1525 return ret;
1528 static const struct file_operations proc_pid_sched_autogroup_operations = {
1529 .open = sched_autogroup_open,
1530 .read = seq_read,
1531 .write = sched_autogroup_write,
1532 .llseek = seq_lseek,
1533 .release = single_release,
1536 #endif /* CONFIG_SCHED_AUTOGROUP */
1538 #ifdef CONFIG_TIME_NS
1539 static int timens_offsets_show(struct seq_file *m, void *v)
1541 struct task_struct *p;
1543 p = get_proc_task(file_inode(m->file));
1544 if (!p)
1545 return -ESRCH;
1546 proc_timens_show_offsets(p, m);
1548 put_task_struct(p);
1550 return 0;
1553 static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
1554 size_t count, loff_t *ppos)
1556 struct inode *inode = file_inode(file);
1557 struct proc_timens_offset offsets[2];
1558 char *kbuf = NULL, *pos, *next_line;
1559 struct task_struct *p;
1560 int ret, noffsets;
1562 /* Only allow < page size writes at the beginning of the file */
1563 if ((*ppos != 0) || (count >= PAGE_SIZE))
1564 return -EINVAL;
1566 /* Slurp in the user data */
1567 kbuf = memdup_user_nul(buf, count);
1568 if (IS_ERR(kbuf))
1569 return PTR_ERR(kbuf);
1571 /* Parse the user data */
1572 ret = -EINVAL;
1573 noffsets = 0;
1574 for (pos = kbuf; pos; pos = next_line) {
1575 struct proc_timens_offset *off = &offsets[noffsets];
1576 int err;
1578 /* Find the end of line and ensure we don't look past it */
1579 next_line = strchr(pos, '\n');
1580 if (next_line) {
1581 *next_line = '\0';
1582 next_line++;
1583 if (*next_line == '\0')
1584 next_line = NULL;
1587 err = sscanf(pos, "%u %lld %lu", &off->clockid,
1588 &off->val.tv_sec, &off->val.tv_nsec);
1589 if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
1590 goto out;
1591 noffsets++;
1592 if (noffsets == ARRAY_SIZE(offsets)) {
1593 if (next_line)
1594 count = next_line - kbuf;
1595 break;
1599 ret = -ESRCH;
1600 p = get_proc_task(inode);
1601 if (!p)
1602 goto out;
1603 ret = proc_timens_set_offset(file, p, offsets, noffsets);
1604 put_task_struct(p);
1605 if (ret)
1606 goto out;
1608 ret = count;
1609 out:
1610 kfree(kbuf);
1611 return ret;
1614 static int timens_offsets_open(struct inode *inode, struct file *filp)
1616 return single_open(filp, timens_offsets_show, inode);
1619 static const struct file_operations proc_timens_offsets_operations = {
1620 .open = timens_offsets_open,
1621 .read = seq_read,
1622 .write = timens_offsets_write,
1623 .llseek = seq_lseek,
1624 .release = single_release,
1626 #endif /* CONFIG_TIME_NS */
1628 static ssize_t comm_write(struct file *file, const char __user *buf,
1629 size_t count, loff_t *offset)
1631 struct inode *inode = file_inode(file);
1632 struct task_struct *p;
1633 char buffer[TASK_COMM_LEN];
1634 const size_t maxlen = sizeof(buffer) - 1;
1636 memset(buffer, 0, sizeof(buffer));
1637 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1638 return -EFAULT;
1640 p = get_proc_task(inode);
1641 if (!p)
1642 return -ESRCH;
1644 if (same_thread_group(current, p))
1645 set_task_comm(p, buffer);
1646 else
1647 count = -EINVAL;
1649 put_task_struct(p);
1651 return count;
1654 static int comm_show(struct seq_file *m, void *v)
1656 struct inode *inode = m->private;
1657 struct task_struct *p;
1659 p = get_proc_task(inode);
1660 if (!p)
1661 return -ESRCH;
1663 proc_task_name(m, p, false);
1664 seq_putc(m, '\n');
1666 put_task_struct(p);
1668 return 0;
1671 static int comm_open(struct inode *inode, struct file *filp)
1673 return single_open(filp, comm_show, inode);
1676 static const struct file_operations proc_pid_set_comm_operations = {
1677 .open = comm_open,
1678 .read = seq_read,
1679 .write = comm_write,
1680 .llseek = seq_lseek,
1681 .release = single_release,
1684 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1686 struct task_struct *task;
1687 struct file *exe_file;
1689 task = get_proc_task(d_inode(dentry));
1690 if (!task)
1691 return -ENOENT;
1692 exe_file = get_task_exe_file(task);
1693 put_task_struct(task);
1694 if (exe_file) {
1695 *exe_path = exe_file->f_path;
1696 path_get(&exe_file->f_path);
1697 fput(exe_file);
1698 return 0;
1699 } else
1700 return -ENOENT;
1703 static const char *proc_pid_get_link(struct dentry *dentry,
1704 struct inode *inode,
1705 struct delayed_call *done)
1707 struct path path;
1708 int error = -EACCES;
1710 if (!dentry)
1711 return ERR_PTR(-ECHILD);
1713 /* Are we allowed to snoop on the tasks file descriptors? */
1714 if (!proc_fd_access_allowed(inode))
1715 goto out;
1717 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1718 if (error)
1719 goto out;
1721 error = nd_jump_link(&path);
1722 out:
1723 return ERR_PTR(error);
1726 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1728 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1729 char *pathname;
1730 int len;
1732 if (!tmp)
1733 return -ENOMEM;
1735 pathname = d_path(path, tmp, PAGE_SIZE);
1736 len = PTR_ERR(pathname);
1737 if (IS_ERR(pathname))
1738 goto out;
1739 len = tmp + PAGE_SIZE - 1 - pathname;
1741 if (len > buflen)
1742 len = buflen;
1743 if (copy_to_user(buffer, pathname, len))
1744 len = -EFAULT;
1745 out:
1746 free_page((unsigned long)tmp);
1747 return len;
1750 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1752 int error = -EACCES;
1753 struct inode *inode = d_inode(dentry);
1754 struct path path;
1756 /* Are we allowed to snoop on the tasks file descriptors? */
1757 if (!proc_fd_access_allowed(inode))
1758 goto out;
1760 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1761 if (error)
1762 goto out;
1764 error = do_proc_readlink(&path, buffer, buflen);
1765 path_put(&path);
1766 out:
1767 return error;
1770 const struct inode_operations proc_pid_link_inode_operations = {
1771 .readlink = proc_pid_readlink,
1772 .get_link = proc_pid_get_link,
1773 .setattr = proc_setattr,
1777 /* building an inode */
1779 void task_dump_owner(struct task_struct *task, umode_t mode,
1780 kuid_t *ruid, kgid_t *rgid)
1782 /* Depending on the state of dumpable compute who should own a
1783 * proc file for a task.
1785 const struct cred *cred;
1786 kuid_t uid;
1787 kgid_t gid;
1789 if (unlikely(task->flags & PF_KTHREAD)) {
1790 *ruid = GLOBAL_ROOT_UID;
1791 *rgid = GLOBAL_ROOT_GID;
1792 return;
1795 /* Default to the tasks effective ownership */
1796 rcu_read_lock();
1797 cred = __task_cred(task);
1798 uid = cred->euid;
1799 gid = cred->egid;
1800 rcu_read_unlock();
1803 * Before the /proc/pid/status file was created the only way to read
1804 * the effective uid of a /process was to stat /proc/pid. Reading
1805 * /proc/pid/status is slow enough that procps and other packages
1806 * kept stating /proc/pid. To keep the rules in /proc simple I have
1807 * made this apply to all per process world readable and executable
1808 * directories.
1810 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1811 struct mm_struct *mm;
1812 task_lock(task);
1813 mm = task->mm;
1814 /* Make non-dumpable tasks owned by some root */
1815 if (mm) {
1816 if (get_dumpable(mm) != SUID_DUMP_USER) {
1817 struct user_namespace *user_ns = mm->user_ns;
1819 uid = make_kuid(user_ns, 0);
1820 if (!uid_valid(uid))
1821 uid = GLOBAL_ROOT_UID;
1823 gid = make_kgid(user_ns, 0);
1824 if (!gid_valid(gid))
1825 gid = GLOBAL_ROOT_GID;
1827 } else {
1828 uid = GLOBAL_ROOT_UID;
1829 gid = GLOBAL_ROOT_GID;
1831 task_unlock(task);
1833 *ruid = uid;
1834 *rgid = gid;
1837 void proc_pid_evict_inode(struct proc_inode *ei)
1839 struct pid *pid = ei->pid;
1841 if (S_ISDIR(ei->vfs_inode.i_mode)) {
1842 spin_lock(&pid->lock);
1843 hlist_del_init_rcu(&ei->sibling_inodes);
1844 spin_unlock(&pid->lock);
1847 put_pid(pid);
1850 struct inode *proc_pid_make_inode(struct super_block * sb,
1851 struct task_struct *task, umode_t mode)
1853 struct inode * inode;
1854 struct proc_inode *ei;
1855 struct pid *pid;
1857 /* We need a new inode */
1859 inode = new_inode(sb);
1860 if (!inode)
1861 goto out;
1863 /* Common stuff */
1864 ei = PROC_I(inode);
1865 inode->i_mode = mode;
1866 inode->i_ino = get_next_ino();
1867 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1868 inode->i_op = &proc_def_inode_operations;
1871 * grab the reference to task.
1873 pid = get_task_pid(task, PIDTYPE_PID);
1874 if (!pid)
1875 goto out_unlock;
1877 /* Let the pid remember us for quick removal */
1878 ei->pid = pid;
1879 if (S_ISDIR(mode)) {
1880 spin_lock(&pid->lock);
1881 hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
1882 spin_unlock(&pid->lock);
1885 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1886 security_task_to_inode(task, inode);
1888 out:
1889 return inode;
1891 out_unlock:
1892 iput(inode);
1893 return NULL;
1896 int pid_getattr(const struct path *path, struct kstat *stat,
1897 u32 request_mask, unsigned int query_flags)
1899 struct inode *inode = d_inode(path->dentry);
1900 struct pid_namespace *pid = proc_pid_ns(inode);
1901 struct task_struct *task;
1903 generic_fillattr(inode, stat);
1905 stat->uid = GLOBAL_ROOT_UID;
1906 stat->gid = GLOBAL_ROOT_GID;
1907 rcu_read_lock();
1908 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1909 if (task) {
1910 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1911 rcu_read_unlock();
1913 * This doesn't prevent learning whether PID exists,
1914 * it only makes getattr() consistent with readdir().
1916 return -ENOENT;
1918 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1920 rcu_read_unlock();
1921 return 0;
1924 /* dentry stuff */
1927 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1929 void pid_update_inode(struct task_struct *task, struct inode *inode)
1931 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1933 inode->i_mode &= ~(S_ISUID | S_ISGID);
1934 security_task_to_inode(task, inode);
1938 * Rewrite the inode's ownerships here because the owning task may have
1939 * performed a setuid(), etc.
1942 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
1944 struct inode *inode;
1945 struct task_struct *task;
1947 if (flags & LOOKUP_RCU)
1948 return -ECHILD;
1950 inode = d_inode(dentry);
1951 task = get_proc_task(inode);
1953 if (task) {
1954 pid_update_inode(task, inode);
1955 put_task_struct(task);
1956 return 1;
1958 return 0;
1961 static inline bool proc_inode_is_dead(struct inode *inode)
1963 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1966 int pid_delete_dentry(const struct dentry *dentry)
1968 /* Is the task we represent dead?
1969 * If so, then don't put the dentry on the lru list,
1970 * kill it immediately.
1972 return proc_inode_is_dead(d_inode(dentry));
1975 const struct dentry_operations pid_dentry_operations =
1977 .d_revalidate = pid_revalidate,
1978 .d_delete = pid_delete_dentry,
1981 /* Lookups */
1984 * Fill a directory entry.
1986 * If possible create the dcache entry and derive our inode number and
1987 * file type from dcache entry.
1989 * Since all of the proc inode numbers are dynamically generated, the inode
1990 * numbers do not exist until the inode is cache. This means creating the
1991 * the dcache entry in readdir is necessary to keep the inode numbers
1992 * reported by readdir in sync with the inode numbers reported
1993 * by stat.
1995 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1996 const char *name, unsigned int len,
1997 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1999 struct dentry *child, *dir = file->f_path.dentry;
2000 struct qstr qname = QSTR_INIT(name, len);
2001 struct inode *inode;
2002 unsigned type = DT_UNKNOWN;
2003 ino_t ino = 1;
2005 child = d_hash_and_lookup(dir, &qname);
2006 if (!child) {
2007 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2008 child = d_alloc_parallel(dir, &qname, &wq);
2009 if (IS_ERR(child))
2010 goto end_instantiate;
2011 if (d_in_lookup(child)) {
2012 struct dentry *res;
2013 res = instantiate(child, task, ptr);
2014 d_lookup_done(child);
2015 if (unlikely(res)) {
2016 dput(child);
2017 child = res;
2018 if (IS_ERR(child))
2019 goto end_instantiate;
2023 inode = d_inode(child);
2024 ino = inode->i_ino;
2025 type = inode->i_mode >> 12;
2026 dput(child);
2027 end_instantiate:
2028 return dir_emit(ctx, name, len, ino, type);
2032 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2033 * which represent vma start and end addresses.
2035 static int dname_to_vma_addr(struct dentry *dentry,
2036 unsigned long *start, unsigned long *end)
2038 const char *str = dentry->d_name.name;
2039 unsigned long long sval, eval;
2040 unsigned int len;
2042 if (str[0] == '0' && str[1] != '-')
2043 return -EINVAL;
2044 len = _parse_integer(str, 16, &sval);
2045 if (len & KSTRTOX_OVERFLOW)
2046 return -EINVAL;
2047 if (sval != (unsigned long)sval)
2048 return -EINVAL;
2049 str += len;
2051 if (*str != '-')
2052 return -EINVAL;
2053 str++;
2055 if (str[0] == '0' && str[1])
2056 return -EINVAL;
2057 len = _parse_integer(str, 16, &eval);
2058 if (len & KSTRTOX_OVERFLOW)
2059 return -EINVAL;
2060 if (eval != (unsigned long)eval)
2061 return -EINVAL;
2062 str += len;
2064 if (*str != '\0')
2065 return -EINVAL;
2067 *start = sval;
2068 *end = eval;
2070 return 0;
2073 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2075 unsigned long vm_start, vm_end;
2076 bool exact_vma_exists = false;
2077 struct mm_struct *mm = NULL;
2078 struct task_struct *task;
2079 struct inode *inode;
2080 int status = 0;
2082 if (flags & LOOKUP_RCU)
2083 return -ECHILD;
2085 inode = d_inode(dentry);
2086 task = get_proc_task(inode);
2087 if (!task)
2088 goto out_notask;
2090 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
2091 if (IS_ERR_OR_NULL(mm))
2092 goto out;
2094 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2095 status = down_read_killable(&mm->mmap_sem);
2096 if (!status) {
2097 exact_vma_exists = !!find_exact_vma(mm, vm_start,
2098 vm_end);
2099 up_read(&mm->mmap_sem);
2103 mmput(mm);
2105 if (exact_vma_exists) {
2106 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2108 security_task_to_inode(task, inode);
2109 status = 1;
2112 out:
2113 put_task_struct(task);
2115 out_notask:
2116 return status;
2119 static const struct dentry_operations tid_map_files_dentry_operations = {
2120 .d_revalidate = map_files_d_revalidate,
2121 .d_delete = pid_delete_dentry,
2124 static int map_files_get_link(struct dentry *dentry, struct path *path)
2126 unsigned long vm_start, vm_end;
2127 struct vm_area_struct *vma;
2128 struct task_struct *task;
2129 struct mm_struct *mm;
2130 int rc;
2132 rc = -ENOENT;
2133 task = get_proc_task(d_inode(dentry));
2134 if (!task)
2135 goto out;
2137 mm = get_task_mm(task);
2138 put_task_struct(task);
2139 if (!mm)
2140 goto out;
2142 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2143 if (rc)
2144 goto out_mmput;
2146 rc = down_read_killable(&mm->mmap_sem);
2147 if (rc)
2148 goto out_mmput;
2150 rc = -ENOENT;
2151 vma = find_exact_vma(mm, vm_start, vm_end);
2152 if (vma && vma->vm_file) {
2153 *path = vma->vm_file->f_path;
2154 path_get(path);
2155 rc = 0;
2157 up_read(&mm->mmap_sem);
2159 out_mmput:
2160 mmput(mm);
2161 out:
2162 return rc;
2165 struct map_files_info {
2166 unsigned long start;
2167 unsigned long end;
2168 fmode_t mode;
2172 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2173 * symlinks may be used to bypass permissions on ancestor directories in the
2174 * path to the file in question.
2176 static const char *
2177 proc_map_files_get_link(struct dentry *dentry,
2178 struct inode *inode,
2179 struct delayed_call *done)
2181 if (!capable(CAP_SYS_ADMIN))
2182 return ERR_PTR(-EPERM);
2184 return proc_pid_get_link(dentry, inode, done);
2188 * Identical to proc_pid_link_inode_operations except for get_link()
2190 static const struct inode_operations proc_map_files_link_inode_operations = {
2191 .readlink = proc_pid_readlink,
2192 .get_link = proc_map_files_get_link,
2193 .setattr = proc_setattr,
2196 static struct dentry *
2197 proc_map_files_instantiate(struct dentry *dentry,
2198 struct task_struct *task, const void *ptr)
2200 fmode_t mode = (fmode_t)(unsigned long)ptr;
2201 struct proc_inode *ei;
2202 struct inode *inode;
2204 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2205 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2206 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2207 if (!inode)
2208 return ERR_PTR(-ENOENT);
2210 ei = PROC_I(inode);
2211 ei->op.proc_get_link = map_files_get_link;
2213 inode->i_op = &proc_map_files_link_inode_operations;
2214 inode->i_size = 64;
2216 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2217 return d_splice_alias(inode, dentry);
2220 static struct dentry *proc_map_files_lookup(struct inode *dir,
2221 struct dentry *dentry, unsigned int flags)
2223 unsigned long vm_start, vm_end;
2224 struct vm_area_struct *vma;
2225 struct task_struct *task;
2226 struct dentry *result;
2227 struct mm_struct *mm;
2229 result = ERR_PTR(-ENOENT);
2230 task = get_proc_task(dir);
2231 if (!task)
2232 goto out;
2234 result = ERR_PTR(-EACCES);
2235 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2236 goto out_put_task;
2238 result = ERR_PTR(-ENOENT);
2239 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2240 goto out_put_task;
2242 mm = get_task_mm(task);
2243 if (!mm)
2244 goto out_put_task;
2246 result = ERR_PTR(-EINTR);
2247 if (down_read_killable(&mm->mmap_sem))
2248 goto out_put_mm;
2250 result = ERR_PTR(-ENOENT);
2251 vma = find_exact_vma(mm, vm_start, vm_end);
2252 if (!vma)
2253 goto out_no_vma;
2255 if (vma->vm_file)
2256 result = proc_map_files_instantiate(dentry, task,
2257 (void *)(unsigned long)vma->vm_file->f_mode);
2259 out_no_vma:
2260 up_read(&mm->mmap_sem);
2261 out_put_mm:
2262 mmput(mm);
2263 out_put_task:
2264 put_task_struct(task);
2265 out:
2266 return result;
2269 static const struct inode_operations proc_map_files_inode_operations = {
2270 .lookup = proc_map_files_lookup,
2271 .permission = proc_fd_permission,
2272 .setattr = proc_setattr,
2275 static int
2276 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2278 struct vm_area_struct *vma;
2279 struct task_struct *task;
2280 struct mm_struct *mm;
2281 unsigned long nr_files, pos, i;
2282 GENRADIX(struct map_files_info) fa;
2283 struct map_files_info *p;
2284 int ret;
2286 genradix_init(&fa);
2288 ret = -ENOENT;
2289 task = get_proc_task(file_inode(file));
2290 if (!task)
2291 goto out;
2293 ret = -EACCES;
2294 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2295 goto out_put_task;
2297 ret = 0;
2298 if (!dir_emit_dots(file, ctx))
2299 goto out_put_task;
2301 mm = get_task_mm(task);
2302 if (!mm)
2303 goto out_put_task;
2305 ret = down_read_killable(&mm->mmap_sem);
2306 if (ret) {
2307 mmput(mm);
2308 goto out_put_task;
2311 nr_files = 0;
2314 * We need two passes here:
2316 * 1) Collect vmas of mapped files with mmap_sem taken
2317 * 2) Release mmap_sem and instantiate entries
2319 * otherwise we get lockdep complained, since filldir()
2320 * routine might require mmap_sem taken in might_fault().
2323 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2324 if (!vma->vm_file)
2325 continue;
2326 if (++pos <= ctx->pos)
2327 continue;
2329 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2330 if (!p) {
2331 ret = -ENOMEM;
2332 up_read(&mm->mmap_sem);
2333 mmput(mm);
2334 goto out_put_task;
2337 p->start = vma->vm_start;
2338 p->end = vma->vm_end;
2339 p->mode = vma->vm_file->f_mode;
2341 up_read(&mm->mmap_sem);
2342 mmput(mm);
2344 for (i = 0; i < nr_files; i++) {
2345 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2346 unsigned int len;
2348 p = genradix_ptr(&fa, i);
2349 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2350 if (!proc_fill_cache(file, ctx,
2351 buf, len,
2352 proc_map_files_instantiate,
2353 task,
2354 (void *)(unsigned long)p->mode))
2355 break;
2356 ctx->pos++;
2359 out_put_task:
2360 put_task_struct(task);
2361 out:
2362 genradix_free(&fa);
2363 return ret;
2366 static const struct file_operations proc_map_files_operations = {
2367 .read = generic_read_dir,
2368 .iterate_shared = proc_map_files_readdir,
2369 .llseek = generic_file_llseek,
2372 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2373 struct timers_private {
2374 struct pid *pid;
2375 struct task_struct *task;
2376 struct sighand_struct *sighand;
2377 struct pid_namespace *ns;
2378 unsigned long flags;
2381 static void *timers_start(struct seq_file *m, loff_t *pos)
2383 struct timers_private *tp = m->private;
2385 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2386 if (!tp->task)
2387 return ERR_PTR(-ESRCH);
2389 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2390 if (!tp->sighand)
2391 return ERR_PTR(-ESRCH);
2393 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2396 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2398 struct timers_private *tp = m->private;
2399 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2402 static void timers_stop(struct seq_file *m, void *v)
2404 struct timers_private *tp = m->private;
2406 if (tp->sighand) {
2407 unlock_task_sighand(tp->task, &tp->flags);
2408 tp->sighand = NULL;
2411 if (tp->task) {
2412 put_task_struct(tp->task);
2413 tp->task = NULL;
2417 static int show_timer(struct seq_file *m, void *v)
2419 struct k_itimer *timer;
2420 struct timers_private *tp = m->private;
2421 int notify;
2422 static const char * const nstr[] = {
2423 [SIGEV_SIGNAL] = "signal",
2424 [SIGEV_NONE] = "none",
2425 [SIGEV_THREAD] = "thread",
2428 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2429 notify = timer->it_sigev_notify;
2431 seq_printf(m, "ID: %d\n", timer->it_id);
2432 seq_printf(m, "signal: %d/%px\n",
2433 timer->sigq->info.si_signo,
2434 timer->sigq->info.si_value.sival_ptr);
2435 seq_printf(m, "notify: %s/%s.%d\n",
2436 nstr[notify & ~SIGEV_THREAD_ID],
2437 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2438 pid_nr_ns(timer->it_pid, tp->ns));
2439 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2441 return 0;
2444 static const struct seq_operations proc_timers_seq_ops = {
2445 .start = timers_start,
2446 .next = timers_next,
2447 .stop = timers_stop,
2448 .show = show_timer,
2451 static int proc_timers_open(struct inode *inode, struct file *file)
2453 struct timers_private *tp;
2455 tp = __seq_open_private(file, &proc_timers_seq_ops,
2456 sizeof(struct timers_private));
2457 if (!tp)
2458 return -ENOMEM;
2460 tp->pid = proc_pid(inode);
2461 tp->ns = proc_pid_ns(inode);
2462 return 0;
2465 static const struct file_operations proc_timers_operations = {
2466 .open = proc_timers_open,
2467 .read = seq_read,
2468 .llseek = seq_lseek,
2469 .release = seq_release_private,
2471 #endif
2473 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2474 size_t count, loff_t *offset)
2476 struct inode *inode = file_inode(file);
2477 struct task_struct *p;
2478 u64 slack_ns;
2479 int err;
2481 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2482 if (err < 0)
2483 return err;
2485 p = get_proc_task(inode);
2486 if (!p)
2487 return -ESRCH;
2489 if (p != current) {
2490 rcu_read_lock();
2491 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2492 rcu_read_unlock();
2493 count = -EPERM;
2494 goto out;
2496 rcu_read_unlock();
2498 err = security_task_setscheduler(p);
2499 if (err) {
2500 count = err;
2501 goto out;
2505 task_lock(p);
2506 if (slack_ns == 0)
2507 p->timer_slack_ns = p->default_timer_slack_ns;
2508 else
2509 p->timer_slack_ns = slack_ns;
2510 task_unlock(p);
2512 out:
2513 put_task_struct(p);
2515 return count;
2518 static int timerslack_ns_show(struct seq_file *m, void *v)
2520 struct inode *inode = m->private;
2521 struct task_struct *p;
2522 int err = 0;
2524 p = get_proc_task(inode);
2525 if (!p)
2526 return -ESRCH;
2528 if (p != current) {
2529 rcu_read_lock();
2530 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2531 rcu_read_unlock();
2532 err = -EPERM;
2533 goto out;
2535 rcu_read_unlock();
2537 err = security_task_getscheduler(p);
2538 if (err)
2539 goto out;
2542 task_lock(p);
2543 seq_printf(m, "%llu\n", p->timer_slack_ns);
2544 task_unlock(p);
2546 out:
2547 put_task_struct(p);
2549 return err;
2552 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2554 return single_open(filp, timerslack_ns_show, inode);
2557 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2558 .open = timerslack_ns_open,
2559 .read = seq_read,
2560 .write = timerslack_ns_write,
2561 .llseek = seq_lseek,
2562 .release = single_release,
2565 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2566 struct task_struct *task, const void *ptr)
2568 const struct pid_entry *p = ptr;
2569 struct inode *inode;
2570 struct proc_inode *ei;
2572 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2573 if (!inode)
2574 return ERR_PTR(-ENOENT);
2576 ei = PROC_I(inode);
2577 if (S_ISDIR(inode->i_mode))
2578 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2579 if (p->iop)
2580 inode->i_op = p->iop;
2581 if (p->fop)
2582 inode->i_fop = p->fop;
2583 ei->op = p->op;
2584 pid_update_inode(task, inode);
2585 d_set_d_op(dentry, &pid_dentry_operations);
2586 return d_splice_alias(inode, dentry);
2589 static struct dentry *proc_pident_lookup(struct inode *dir,
2590 struct dentry *dentry,
2591 const struct pid_entry *p,
2592 const struct pid_entry *end)
2594 struct task_struct *task = get_proc_task(dir);
2595 struct dentry *res = ERR_PTR(-ENOENT);
2597 if (!task)
2598 goto out_no_task;
2601 * Yes, it does not scale. And it should not. Don't add
2602 * new entries into /proc/<tgid>/ without very good reasons.
2604 for (; p < end; p++) {
2605 if (p->len != dentry->d_name.len)
2606 continue;
2607 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2608 res = proc_pident_instantiate(dentry, task, p);
2609 break;
2612 put_task_struct(task);
2613 out_no_task:
2614 return res;
2617 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2618 const struct pid_entry *ents, unsigned int nents)
2620 struct task_struct *task = get_proc_task(file_inode(file));
2621 const struct pid_entry *p;
2623 if (!task)
2624 return -ENOENT;
2626 if (!dir_emit_dots(file, ctx))
2627 goto out;
2629 if (ctx->pos >= nents + 2)
2630 goto out;
2632 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2633 if (!proc_fill_cache(file, ctx, p->name, p->len,
2634 proc_pident_instantiate, task, p))
2635 break;
2636 ctx->pos++;
2638 out:
2639 put_task_struct(task);
2640 return 0;
2643 #ifdef CONFIG_SECURITY
2644 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2645 size_t count, loff_t *ppos)
2647 struct inode * inode = file_inode(file);
2648 char *p = NULL;
2649 ssize_t length;
2650 struct task_struct *task = get_proc_task(inode);
2652 if (!task)
2653 return -ESRCH;
2655 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2656 (char*)file->f_path.dentry->d_name.name,
2657 &p);
2658 put_task_struct(task);
2659 if (length > 0)
2660 length = simple_read_from_buffer(buf, count, ppos, p, length);
2661 kfree(p);
2662 return length;
2665 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2666 size_t count, loff_t *ppos)
2668 struct inode * inode = file_inode(file);
2669 struct task_struct *task;
2670 void *page;
2671 int rv;
2673 rcu_read_lock();
2674 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2675 if (!task) {
2676 rcu_read_unlock();
2677 return -ESRCH;
2679 /* A task may only write its own attributes. */
2680 if (current != task) {
2681 rcu_read_unlock();
2682 return -EACCES;
2684 /* Prevent changes to overridden credentials. */
2685 if (current_cred() != current_real_cred()) {
2686 rcu_read_unlock();
2687 return -EBUSY;
2689 rcu_read_unlock();
2691 if (count > PAGE_SIZE)
2692 count = PAGE_SIZE;
2694 /* No partial writes. */
2695 if (*ppos != 0)
2696 return -EINVAL;
2698 page = memdup_user(buf, count);
2699 if (IS_ERR(page)) {
2700 rv = PTR_ERR(page);
2701 goto out;
2704 /* Guard against adverse ptrace interaction */
2705 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2706 if (rv < 0)
2707 goto out_free;
2709 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2710 file->f_path.dentry->d_name.name, page,
2711 count);
2712 mutex_unlock(&current->signal->cred_guard_mutex);
2713 out_free:
2714 kfree(page);
2715 out:
2716 return rv;
2719 static const struct file_operations proc_pid_attr_operations = {
2720 .read = proc_pid_attr_read,
2721 .write = proc_pid_attr_write,
2722 .llseek = generic_file_llseek,
2725 #define LSM_DIR_OPS(LSM) \
2726 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2727 struct dir_context *ctx) \
2729 return proc_pident_readdir(filp, ctx, \
2730 LSM##_attr_dir_stuff, \
2731 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2734 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2735 .read = generic_read_dir, \
2736 .iterate = proc_##LSM##_attr_dir_iterate, \
2737 .llseek = default_llseek, \
2738 }; \
2740 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2741 struct dentry *dentry, unsigned int flags) \
2743 return proc_pident_lookup(dir, dentry, \
2744 LSM##_attr_dir_stuff, \
2745 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2748 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2749 .lookup = proc_##LSM##_attr_dir_lookup, \
2750 .getattr = pid_getattr, \
2751 .setattr = proc_setattr, \
2754 #ifdef CONFIG_SECURITY_SMACK
2755 static const struct pid_entry smack_attr_dir_stuff[] = {
2756 ATTR("smack", "current", 0666),
2758 LSM_DIR_OPS(smack);
2759 #endif
2761 static const struct pid_entry attr_dir_stuff[] = {
2762 ATTR(NULL, "current", 0666),
2763 ATTR(NULL, "prev", 0444),
2764 ATTR(NULL, "exec", 0666),
2765 ATTR(NULL, "fscreate", 0666),
2766 ATTR(NULL, "keycreate", 0666),
2767 ATTR(NULL, "sockcreate", 0666),
2768 #ifdef CONFIG_SECURITY_SMACK
2769 DIR("smack", 0555,
2770 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2771 #endif
2774 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2776 return proc_pident_readdir(file, ctx,
2777 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2780 static const struct file_operations proc_attr_dir_operations = {
2781 .read = generic_read_dir,
2782 .iterate_shared = proc_attr_dir_readdir,
2783 .llseek = generic_file_llseek,
2786 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2787 struct dentry *dentry, unsigned int flags)
2789 return proc_pident_lookup(dir, dentry,
2790 attr_dir_stuff,
2791 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
2794 static const struct inode_operations proc_attr_dir_inode_operations = {
2795 .lookup = proc_attr_dir_lookup,
2796 .getattr = pid_getattr,
2797 .setattr = proc_setattr,
2800 #endif
2802 #ifdef CONFIG_ELF_CORE
2803 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2804 size_t count, loff_t *ppos)
2806 struct task_struct *task = get_proc_task(file_inode(file));
2807 struct mm_struct *mm;
2808 char buffer[PROC_NUMBUF];
2809 size_t len;
2810 int ret;
2812 if (!task)
2813 return -ESRCH;
2815 ret = 0;
2816 mm = get_task_mm(task);
2817 if (mm) {
2818 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2819 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2820 MMF_DUMP_FILTER_SHIFT));
2821 mmput(mm);
2822 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2825 put_task_struct(task);
2827 return ret;
2830 static ssize_t proc_coredump_filter_write(struct file *file,
2831 const char __user *buf,
2832 size_t count,
2833 loff_t *ppos)
2835 struct task_struct *task;
2836 struct mm_struct *mm;
2837 unsigned int val;
2838 int ret;
2839 int i;
2840 unsigned long mask;
2842 ret = kstrtouint_from_user(buf, count, 0, &val);
2843 if (ret < 0)
2844 return ret;
2846 ret = -ESRCH;
2847 task = get_proc_task(file_inode(file));
2848 if (!task)
2849 goto out_no_task;
2851 mm = get_task_mm(task);
2852 if (!mm)
2853 goto out_no_mm;
2854 ret = 0;
2856 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2857 if (val & mask)
2858 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2859 else
2860 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2863 mmput(mm);
2864 out_no_mm:
2865 put_task_struct(task);
2866 out_no_task:
2867 if (ret < 0)
2868 return ret;
2869 return count;
2872 static const struct file_operations proc_coredump_filter_operations = {
2873 .read = proc_coredump_filter_read,
2874 .write = proc_coredump_filter_write,
2875 .llseek = generic_file_llseek,
2877 #endif
2879 #ifdef CONFIG_TASK_IO_ACCOUNTING
2880 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2882 struct task_io_accounting acct = task->ioac;
2883 unsigned long flags;
2884 int result;
2886 result = mutex_lock_killable(&task->signal->exec_update_mutex);
2887 if (result)
2888 return result;
2890 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2891 result = -EACCES;
2892 goto out_unlock;
2895 if (whole && lock_task_sighand(task, &flags)) {
2896 struct task_struct *t = task;
2898 task_io_accounting_add(&acct, &task->signal->ioac);
2899 while_each_thread(task, t)
2900 task_io_accounting_add(&acct, &t->ioac);
2902 unlock_task_sighand(task, &flags);
2904 seq_printf(m,
2905 "rchar: %llu\n"
2906 "wchar: %llu\n"
2907 "syscr: %llu\n"
2908 "syscw: %llu\n"
2909 "read_bytes: %llu\n"
2910 "write_bytes: %llu\n"
2911 "cancelled_write_bytes: %llu\n",
2912 (unsigned long long)acct.rchar,
2913 (unsigned long long)acct.wchar,
2914 (unsigned long long)acct.syscr,
2915 (unsigned long long)acct.syscw,
2916 (unsigned long long)acct.read_bytes,
2917 (unsigned long long)acct.write_bytes,
2918 (unsigned long long)acct.cancelled_write_bytes);
2919 result = 0;
2921 out_unlock:
2922 mutex_unlock(&task->signal->exec_update_mutex);
2923 return result;
2926 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2927 struct pid *pid, struct task_struct *task)
2929 return do_io_accounting(task, m, 0);
2932 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2933 struct pid *pid, struct task_struct *task)
2935 return do_io_accounting(task, m, 1);
2937 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2939 #ifdef CONFIG_USER_NS
2940 static int proc_id_map_open(struct inode *inode, struct file *file,
2941 const struct seq_operations *seq_ops)
2943 struct user_namespace *ns = NULL;
2944 struct task_struct *task;
2945 struct seq_file *seq;
2946 int ret = -EINVAL;
2948 task = get_proc_task(inode);
2949 if (task) {
2950 rcu_read_lock();
2951 ns = get_user_ns(task_cred_xxx(task, user_ns));
2952 rcu_read_unlock();
2953 put_task_struct(task);
2955 if (!ns)
2956 goto err;
2958 ret = seq_open(file, seq_ops);
2959 if (ret)
2960 goto err_put_ns;
2962 seq = file->private_data;
2963 seq->private = ns;
2965 return 0;
2966 err_put_ns:
2967 put_user_ns(ns);
2968 err:
2969 return ret;
2972 static int proc_id_map_release(struct inode *inode, struct file *file)
2974 struct seq_file *seq = file->private_data;
2975 struct user_namespace *ns = seq->private;
2976 put_user_ns(ns);
2977 return seq_release(inode, file);
2980 static int proc_uid_map_open(struct inode *inode, struct file *file)
2982 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2985 static int proc_gid_map_open(struct inode *inode, struct file *file)
2987 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2990 static int proc_projid_map_open(struct inode *inode, struct file *file)
2992 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2995 static const struct file_operations proc_uid_map_operations = {
2996 .open = proc_uid_map_open,
2997 .write = proc_uid_map_write,
2998 .read = seq_read,
2999 .llseek = seq_lseek,
3000 .release = proc_id_map_release,
3003 static const struct file_operations proc_gid_map_operations = {
3004 .open = proc_gid_map_open,
3005 .write = proc_gid_map_write,
3006 .read = seq_read,
3007 .llseek = seq_lseek,
3008 .release = proc_id_map_release,
3011 static const struct file_operations proc_projid_map_operations = {
3012 .open = proc_projid_map_open,
3013 .write = proc_projid_map_write,
3014 .read = seq_read,
3015 .llseek = seq_lseek,
3016 .release = proc_id_map_release,
3019 static int proc_setgroups_open(struct inode *inode, struct file *file)
3021 struct user_namespace *ns = NULL;
3022 struct task_struct *task;
3023 int ret;
3025 ret = -ESRCH;
3026 task = get_proc_task(inode);
3027 if (task) {
3028 rcu_read_lock();
3029 ns = get_user_ns(task_cred_xxx(task, user_ns));
3030 rcu_read_unlock();
3031 put_task_struct(task);
3033 if (!ns)
3034 goto err;
3036 if (file->f_mode & FMODE_WRITE) {
3037 ret = -EACCES;
3038 if (!ns_capable(ns, CAP_SYS_ADMIN))
3039 goto err_put_ns;
3042 ret = single_open(file, &proc_setgroups_show, ns);
3043 if (ret)
3044 goto err_put_ns;
3046 return 0;
3047 err_put_ns:
3048 put_user_ns(ns);
3049 err:
3050 return ret;
3053 static int proc_setgroups_release(struct inode *inode, struct file *file)
3055 struct seq_file *seq = file->private_data;
3056 struct user_namespace *ns = seq->private;
3057 int ret = single_release(inode, file);
3058 put_user_ns(ns);
3059 return ret;
3062 static const struct file_operations proc_setgroups_operations = {
3063 .open = proc_setgroups_open,
3064 .write = proc_setgroups_write,
3065 .read = seq_read,
3066 .llseek = seq_lseek,
3067 .release = proc_setgroups_release,
3069 #endif /* CONFIG_USER_NS */
3071 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3072 struct pid *pid, struct task_struct *task)
3074 int err = lock_trace(task);
3075 if (!err) {
3076 seq_printf(m, "%08x\n", task->personality);
3077 unlock_trace(task);
3079 return err;
3082 #ifdef CONFIG_LIVEPATCH
3083 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
3084 struct pid *pid, struct task_struct *task)
3086 seq_printf(m, "%d\n", task->patch_state);
3087 return 0;
3089 #endif /* CONFIG_LIVEPATCH */
3091 #ifdef CONFIG_STACKLEAK_METRICS
3092 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3093 struct pid *pid, struct task_struct *task)
3095 unsigned long prev_depth = THREAD_SIZE -
3096 (task->prev_lowest_stack & (THREAD_SIZE - 1));
3097 unsigned long depth = THREAD_SIZE -
3098 (task->lowest_stack & (THREAD_SIZE - 1));
3100 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3101 prev_depth, depth);
3102 return 0;
3104 #endif /* CONFIG_STACKLEAK_METRICS */
3107 * Thread groups
3109 static const struct file_operations proc_task_operations;
3110 static const struct inode_operations proc_task_inode_operations;
3112 static const struct pid_entry tgid_base_stuff[] = {
3113 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3114 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3115 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3116 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3117 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3118 #ifdef CONFIG_NET
3119 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3120 #endif
3121 REG("environ", S_IRUSR, proc_environ_operations),
3122 REG("auxv", S_IRUSR, proc_auxv_operations),
3123 ONE("status", S_IRUGO, proc_pid_status),
3124 ONE("personality", S_IRUSR, proc_pid_personality),
3125 ONE("limits", S_IRUGO, proc_pid_limits),
3126 #ifdef CONFIG_SCHED_DEBUG
3127 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3128 #endif
3129 #ifdef CONFIG_SCHED_AUTOGROUP
3130 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3131 #endif
3132 #ifdef CONFIG_TIME_NS
3133 REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
3134 #endif
3135 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3136 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3137 ONE("syscall", S_IRUSR, proc_pid_syscall),
3138 #endif
3139 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3140 ONE("stat", S_IRUGO, proc_tgid_stat),
3141 ONE("statm", S_IRUGO, proc_pid_statm),
3142 REG("maps", S_IRUGO, proc_pid_maps_operations),
3143 #ifdef CONFIG_NUMA
3144 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3145 #endif
3146 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3147 LNK("cwd", proc_cwd_link),
3148 LNK("root", proc_root_link),
3149 LNK("exe", proc_exe_link),
3150 REG("mounts", S_IRUGO, proc_mounts_operations),
3151 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3152 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3153 #ifdef CONFIG_PROC_PAGE_MONITOR
3154 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3155 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3156 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3157 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3158 #endif
3159 #ifdef CONFIG_SECURITY
3160 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3161 #endif
3162 #ifdef CONFIG_KALLSYMS
3163 ONE("wchan", S_IRUGO, proc_pid_wchan),
3164 #endif
3165 #ifdef CONFIG_STACKTRACE
3166 ONE("stack", S_IRUSR, proc_pid_stack),
3167 #endif
3168 #ifdef CONFIG_SCHED_INFO
3169 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3170 #endif
3171 #ifdef CONFIG_LATENCYTOP
3172 REG("latency", S_IRUGO, proc_lstats_operations),
3173 #endif
3174 #ifdef CONFIG_PROC_PID_CPUSET
3175 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3176 #endif
3177 #ifdef CONFIG_CGROUPS
3178 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3179 #endif
3180 #ifdef CONFIG_PROC_CPU_RESCTRL
3181 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3182 #endif
3183 ONE("oom_score", S_IRUGO, proc_oom_score),
3184 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3185 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3186 #ifdef CONFIG_AUDIT
3187 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3188 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3189 #endif
3190 #ifdef CONFIG_FAULT_INJECTION
3191 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3192 REG("fail-nth", 0644, proc_fail_nth_operations),
3193 #endif
3194 #ifdef CONFIG_ELF_CORE
3195 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3196 #endif
3197 #ifdef CONFIG_TASK_IO_ACCOUNTING
3198 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3199 #endif
3200 #ifdef CONFIG_USER_NS
3201 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3202 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3203 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3204 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3205 #endif
3206 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3207 REG("timers", S_IRUGO, proc_timers_operations),
3208 #endif
3209 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3210 #ifdef CONFIG_LIVEPATCH
3211 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3212 #endif
3213 #ifdef CONFIG_STACKLEAK_METRICS
3214 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3215 #endif
3216 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3217 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3218 #endif
3221 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3223 return proc_pident_readdir(file, ctx,
3224 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3227 static const struct file_operations proc_tgid_base_operations = {
3228 .read = generic_read_dir,
3229 .iterate_shared = proc_tgid_base_readdir,
3230 .llseek = generic_file_llseek,
3233 struct pid *tgid_pidfd_to_pid(const struct file *file)
3235 if (file->f_op != &proc_tgid_base_operations)
3236 return ERR_PTR(-EBADF);
3238 return proc_pid(file_inode(file));
3241 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3243 return proc_pident_lookup(dir, dentry,
3244 tgid_base_stuff,
3245 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
3248 static const struct inode_operations proc_tgid_base_inode_operations = {
3249 .lookup = proc_tgid_base_lookup,
3250 .getattr = pid_getattr,
3251 .setattr = proc_setattr,
3252 .permission = proc_pid_permission,
3256 * proc_flush_pid - Remove dcache entries for @pid from the /proc dcache.
3257 * @pid: pid that should be flushed.
3259 * This function walks a list of inodes (that belong to any proc
3260 * filesystem) that are attached to the pid and flushes them from
3261 * the dentry cache.
3263 * It is safe and reasonable to cache /proc entries for a task until
3264 * that task exits. After that they just clog up the dcache with
3265 * useless entries, possibly causing useful dcache entries to be
3266 * flushed instead. This routine is provided to flush those useless
3267 * dcache entries when a process is reaped.
3269 * NOTE: This routine is just an optimization so it does not guarantee
3270 * that no dcache entries will exist after a process is reaped
3271 * it just makes it very unlikely that any will persist.
3274 void proc_flush_pid(struct pid *pid)
3276 proc_invalidate_siblings_dcache(&pid->inodes, &pid->lock);
3277 put_pid(pid);
3280 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3281 struct task_struct *task, const void *ptr)
3283 struct inode *inode;
3285 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3286 if (!inode)
3287 return ERR_PTR(-ENOENT);
3289 inode->i_op = &proc_tgid_base_inode_operations;
3290 inode->i_fop = &proc_tgid_base_operations;
3291 inode->i_flags|=S_IMMUTABLE;
3293 set_nlink(inode, nlink_tgid);
3294 pid_update_inode(task, inode);
3296 d_set_d_op(dentry, &pid_dentry_operations);
3297 return d_splice_alias(inode, dentry);
3300 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3302 struct task_struct *task;
3303 unsigned tgid;
3304 struct pid_namespace *ns;
3305 struct dentry *result = ERR_PTR(-ENOENT);
3307 tgid = name_to_int(&dentry->d_name);
3308 if (tgid == ~0U)
3309 goto out;
3311 ns = dentry->d_sb->s_fs_info;
3312 rcu_read_lock();
3313 task = find_task_by_pid_ns(tgid, ns);
3314 if (task)
3315 get_task_struct(task);
3316 rcu_read_unlock();
3317 if (!task)
3318 goto out;
3320 result = proc_pid_instantiate(dentry, task, NULL);
3321 put_task_struct(task);
3322 out:
3323 return result;
3327 * Find the first task with tgid >= tgid
3330 struct tgid_iter {
3331 unsigned int tgid;
3332 struct task_struct *task;
3334 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3336 struct pid *pid;
3338 if (iter.task)
3339 put_task_struct(iter.task);
3340 rcu_read_lock();
3341 retry:
3342 iter.task = NULL;
3343 pid = find_ge_pid(iter.tgid, ns);
3344 if (pid) {
3345 iter.tgid = pid_nr_ns(pid, ns);
3346 iter.task = pid_task(pid, PIDTYPE_PID);
3347 /* What we to know is if the pid we have find is the
3348 * pid of a thread_group_leader. Testing for task
3349 * being a thread_group_leader is the obvious thing
3350 * todo but there is a window when it fails, due to
3351 * the pid transfer logic in de_thread.
3353 * So we perform the straight forward test of seeing
3354 * if the pid we have found is the pid of a thread
3355 * group leader, and don't worry if the task we have
3356 * found doesn't happen to be a thread group leader.
3357 * As we don't care in the case of readdir.
3359 if (!iter.task || !has_group_leader_pid(iter.task)) {
3360 iter.tgid += 1;
3361 goto retry;
3363 get_task_struct(iter.task);
3365 rcu_read_unlock();
3366 return iter;
3369 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3371 /* for the /proc/ directory itself, after non-process stuff has been done */
3372 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3374 struct tgid_iter iter;
3375 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
3376 loff_t pos = ctx->pos;
3378 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3379 return 0;
3381 if (pos == TGID_OFFSET - 2) {
3382 struct inode *inode = d_inode(ns->proc_self);
3383 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3384 return 0;
3385 ctx->pos = pos = pos + 1;
3387 if (pos == TGID_OFFSET - 1) {
3388 struct inode *inode = d_inode(ns->proc_thread_self);
3389 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3390 return 0;
3391 ctx->pos = pos = pos + 1;
3393 iter.tgid = pos - TGID_OFFSET;
3394 iter.task = NULL;
3395 for (iter = next_tgid(ns, iter);
3396 iter.task;
3397 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3398 char name[10 + 1];
3399 unsigned int len;
3401 cond_resched();
3402 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3403 continue;
3405 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3406 ctx->pos = iter.tgid + TGID_OFFSET;
3407 if (!proc_fill_cache(file, ctx, name, len,
3408 proc_pid_instantiate, iter.task, NULL)) {
3409 put_task_struct(iter.task);
3410 return 0;
3413 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3414 return 0;
3418 * proc_tid_comm_permission is a special permission function exclusively
3419 * used for the node /proc/<pid>/task/<tid>/comm.
3420 * It bypasses generic permission checks in the case where a task of the same
3421 * task group attempts to access the node.
3422 * The rationale behind this is that glibc and bionic access this node for
3423 * cross thread naming (pthread_set/getname_np(!self)). However, if
3424 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3425 * which locks out the cross thread naming implementation.
3426 * This function makes sure that the node is always accessible for members of
3427 * same thread group.
3429 static int proc_tid_comm_permission(struct inode *inode, int mask)
3431 bool is_same_tgroup;
3432 struct task_struct *task;
3434 task = get_proc_task(inode);
3435 if (!task)
3436 return -ESRCH;
3437 is_same_tgroup = same_thread_group(current, task);
3438 put_task_struct(task);
3440 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3441 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3442 * read or written by the members of the corresponding
3443 * thread group.
3445 return 0;
3448 return generic_permission(inode, mask);
3451 static const struct inode_operations proc_tid_comm_inode_operations = {
3452 .permission = proc_tid_comm_permission,
3456 * Tasks
3458 static const struct pid_entry tid_base_stuff[] = {
3459 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3460 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3461 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3462 #ifdef CONFIG_NET
3463 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3464 #endif
3465 REG("environ", S_IRUSR, proc_environ_operations),
3466 REG("auxv", S_IRUSR, proc_auxv_operations),
3467 ONE("status", S_IRUGO, proc_pid_status),
3468 ONE("personality", S_IRUSR, proc_pid_personality),
3469 ONE("limits", S_IRUGO, proc_pid_limits),
3470 #ifdef CONFIG_SCHED_DEBUG
3471 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3472 #endif
3473 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3474 &proc_tid_comm_inode_operations,
3475 &proc_pid_set_comm_operations, {}),
3476 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3477 ONE("syscall", S_IRUSR, proc_pid_syscall),
3478 #endif
3479 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3480 ONE("stat", S_IRUGO, proc_tid_stat),
3481 ONE("statm", S_IRUGO, proc_pid_statm),
3482 REG("maps", S_IRUGO, proc_pid_maps_operations),
3483 #ifdef CONFIG_PROC_CHILDREN
3484 REG("children", S_IRUGO, proc_tid_children_operations),
3485 #endif
3486 #ifdef CONFIG_NUMA
3487 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3488 #endif
3489 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3490 LNK("cwd", proc_cwd_link),
3491 LNK("root", proc_root_link),
3492 LNK("exe", proc_exe_link),
3493 REG("mounts", S_IRUGO, proc_mounts_operations),
3494 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3495 #ifdef CONFIG_PROC_PAGE_MONITOR
3496 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3497 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3498 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3499 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3500 #endif
3501 #ifdef CONFIG_SECURITY
3502 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3503 #endif
3504 #ifdef CONFIG_KALLSYMS
3505 ONE("wchan", S_IRUGO, proc_pid_wchan),
3506 #endif
3507 #ifdef CONFIG_STACKTRACE
3508 ONE("stack", S_IRUSR, proc_pid_stack),
3509 #endif
3510 #ifdef CONFIG_SCHED_INFO
3511 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3512 #endif
3513 #ifdef CONFIG_LATENCYTOP
3514 REG("latency", S_IRUGO, proc_lstats_operations),
3515 #endif
3516 #ifdef CONFIG_PROC_PID_CPUSET
3517 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3518 #endif
3519 #ifdef CONFIG_CGROUPS
3520 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3521 #endif
3522 #ifdef CONFIG_PROC_CPU_RESCTRL
3523 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3524 #endif
3525 ONE("oom_score", S_IRUGO, proc_oom_score),
3526 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3527 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3528 #ifdef CONFIG_AUDIT
3529 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3530 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3531 #endif
3532 #ifdef CONFIG_FAULT_INJECTION
3533 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3534 REG("fail-nth", 0644, proc_fail_nth_operations),
3535 #endif
3536 #ifdef CONFIG_TASK_IO_ACCOUNTING
3537 ONE("io", S_IRUSR, proc_tid_io_accounting),
3538 #endif
3539 #ifdef CONFIG_USER_NS
3540 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3541 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3542 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3543 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3544 #endif
3545 #ifdef CONFIG_LIVEPATCH
3546 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3547 #endif
3548 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3549 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3550 #endif
3553 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3555 return proc_pident_readdir(file, ctx,
3556 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3559 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3561 return proc_pident_lookup(dir, dentry,
3562 tid_base_stuff,
3563 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
3566 static const struct file_operations proc_tid_base_operations = {
3567 .read = generic_read_dir,
3568 .iterate_shared = proc_tid_base_readdir,
3569 .llseek = generic_file_llseek,
3572 static const struct inode_operations proc_tid_base_inode_operations = {
3573 .lookup = proc_tid_base_lookup,
3574 .getattr = pid_getattr,
3575 .setattr = proc_setattr,
3578 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3579 struct task_struct *task, const void *ptr)
3581 struct inode *inode;
3582 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3583 if (!inode)
3584 return ERR_PTR(-ENOENT);
3586 inode->i_op = &proc_tid_base_inode_operations;
3587 inode->i_fop = &proc_tid_base_operations;
3588 inode->i_flags |= S_IMMUTABLE;
3590 set_nlink(inode, nlink_tid);
3591 pid_update_inode(task, inode);
3593 d_set_d_op(dentry, &pid_dentry_operations);
3594 return d_splice_alias(inode, dentry);
3597 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3599 struct task_struct *task;
3600 struct task_struct *leader = get_proc_task(dir);
3601 unsigned tid;
3602 struct pid_namespace *ns;
3603 struct dentry *result = ERR_PTR(-ENOENT);
3605 if (!leader)
3606 goto out_no_task;
3608 tid = name_to_int(&dentry->d_name);
3609 if (tid == ~0U)
3610 goto out;
3612 ns = dentry->d_sb->s_fs_info;
3613 rcu_read_lock();
3614 task = find_task_by_pid_ns(tid, ns);
3615 if (task)
3616 get_task_struct(task);
3617 rcu_read_unlock();
3618 if (!task)
3619 goto out;
3620 if (!same_thread_group(leader, task))
3621 goto out_drop_task;
3623 result = proc_task_instantiate(dentry, task, NULL);
3624 out_drop_task:
3625 put_task_struct(task);
3626 out:
3627 put_task_struct(leader);
3628 out_no_task:
3629 return result;
3633 * Find the first tid of a thread group to return to user space.
3635 * Usually this is just the thread group leader, but if the users
3636 * buffer was too small or there was a seek into the middle of the
3637 * directory we have more work todo.
3639 * In the case of a short read we start with find_task_by_pid.
3641 * In the case of a seek we start with the leader and walk nr
3642 * threads past it.
3644 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3645 struct pid_namespace *ns)
3647 struct task_struct *pos, *task;
3648 unsigned long nr = f_pos;
3650 if (nr != f_pos) /* 32bit overflow? */
3651 return NULL;
3653 rcu_read_lock();
3654 task = pid_task(pid, PIDTYPE_PID);
3655 if (!task)
3656 goto fail;
3658 /* Attempt to start with the tid of a thread */
3659 if (tid && nr) {
3660 pos = find_task_by_pid_ns(tid, ns);
3661 if (pos && same_thread_group(pos, task))
3662 goto found;
3665 /* If nr exceeds the number of threads there is nothing todo */
3666 if (nr >= get_nr_threads(task))
3667 goto fail;
3669 /* If we haven't found our starting place yet start
3670 * with the leader and walk nr threads forward.
3672 pos = task = task->group_leader;
3673 do {
3674 if (!nr--)
3675 goto found;
3676 } while_each_thread(task, pos);
3677 fail:
3678 pos = NULL;
3679 goto out;
3680 found:
3681 get_task_struct(pos);
3682 out:
3683 rcu_read_unlock();
3684 return pos;
3688 * Find the next thread in the thread list.
3689 * Return NULL if there is an error or no next thread.
3691 * The reference to the input task_struct is released.
3693 static struct task_struct *next_tid(struct task_struct *start)
3695 struct task_struct *pos = NULL;
3696 rcu_read_lock();
3697 if (pid_alive(start)) {
3698 pos = next_thread(start);
3699 if (thread_group_leader(pos))
3700 pos = NULL;
3701 else
3702 get_task_struct(pos);
3704 rcu_read_unlock();
3705 put_task_struct(start);
3706 return pos;
3709 /* for the /proc/TGID/task/ directories */
3710 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3712 struct inode *inode = file_inode(file);
3713 struct task_struct *task;
3714 struct pid_namespace *ns;
3715 int tid;
3717 if (proc_inode_is_dead(inode))
3718 return -ENOENT;
3720 if (!dir_emit_dots(file, ctx))
3721 return 0;
3723 /* f_version caches the tgid value that the last readdir call couldn't
3724 * return. lseek aka telldir automagically resets f_version to 0.
3726 ns = proc_pid_ns(inode);
3727 tid = (int)file->f_version;
3728 file->f_version = 0;
3729 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3730 task;
3731 task = next_tid(task), ctx->pos++) {
3732 char name[10 + 1];
3733 unsigned int len;
3734 tid = task_pid_nr_ns(task, ns);
3735 len = snprintf(name, sizeof(name), "%u", tid);
3736 if (!proc_fill_cache(file, ctx, name, len,
3737 proc_task_instantiate, task, NULL)) {
3738 /* returning this tgid failed, save it as the first
3739 * pid for the next readir call */
3740 file->f_version = (u64)tid;
3741 put_task_struct(task);
3742 break;
3746 return 0;
3749 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3750 u32 request_mask, unsigned int query_flags)
3752 struct inode *inode = d_inode(path->dentry);
3753 struct task_struct *p = get_proc_task(inode);
3754 generic_fillattr(inode, stat);
3756 if (p) {
3757 stat->nlink += get_nr_threads(p);
3758 put_task_struct(p);
3761 return 0;
3764 static const struct inode_operations proc_task_inode_operations = {
3765 .lookup = proc_task_lookup,
3766 .getattr = proc_task_getattr,
3767 .setattr = proc_setattr,
3768 .permission = proc_pid_permission,
3771 static const struct file_operations proc_task_operations = {
3772 .read = generic_read_dir,
3773 .iterate_shared = proc_task_readdir,
3774 .llseek = generic_file_llseek,
3777 void __init set_proc_pid_nlink(void)
3779 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3780 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));