mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / fs / proc / base.c
blobeeb81d9648c672bfea4f4233fe7b51642b4b5a2c
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/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
66 #include <linux/mm.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cgroup.h>
79 #include <linux/cpuset.h>
80 #include <linux/audit.h>
81 #include <linux/poll.h>
82 #include <linux/nsproxy.h>
83 #include <linux/oom.h>
84 #include <linux/elf.h>
85 #include <linux/pid_namespace.h>
86 #include <linux/user_namespace.h>
87 #include <linux/fs_struct.h>
88 #include <linux/slab.h>
89 #include <linux/sched/autogroup.h>
90 #include <linux/sched/mm.h>
91 #include <linux/sched/coredump.h>
92 #include <linux/sched/debug.h>
93 #include <linux/sched/stat.h>
94 #include <linux/flex_array.h>
95 #include <linux/posix-timers.h>
96 #ifdef CONFIG_HARDWALL
97 #include <asm/hardwall.h>
98 #endif
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;
116 static u8 nlink_tgid;
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 } )
150 * Count the number of hardlinks for the pid_entry table, excluding the .
151 * and .. links.
153 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
154 unsigned int n)
156 unsigned int i;
157 unsigned int count;
159 count = 2;
160 for (i = 0; i < n; ++i) {
161 if (S_ISDIR(entries[i].mode))
162 ++count;
165 return count;
168 static int get_task_root(struct task_struct *task, struct path *root)
170 int result = -ENOENT;
172 task_lock(task);
173 if (task->fs) {
174 get_fs_root(task->fs, root);
175 result = 0;
177 task_unlock(task);
178 return result;
181 static int proc_cwd_link(struct dentry *dentry, struct path *path)
183 struct task_struct *task = get_proc_task(d_inode(dentry));
184 int result = -ENOENT;
186 if (task) {
187 task_lock(task);
188 if (task->fs) {
189 get_fs_pwd(task->fs, path);
190 result = 0;
192 task_unlock(task);
193 put_task_struct(task);
195 return result;
198 static int proc_root_link(struct dentry *dentry, struct path *path)
200 struct task_struct *task = get_proc_task(d_inode(dentry));
201 int result = -ENOENT;
203 if (task) {
204 result = get_task_root(task, path);
205 put_task_struct(task);
207 return result;
210 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
211 size_t _count, loff_t *pos)
213 struct task_struct *tsk;
214 struct mm_struct *mm;
215 char *page;
216 unsigned long count = _count;
217 unsigned long arg_start, arg_end, env_start, env_end;
218 unsigned long len1, len2, len;
219 unsigned long p;
220 char c;
221 ssize_t rv;
223 BUG_ON(*pos < 0);
225 tsk = get_proc_task(file_inode(file));
226 if (!tsk)
227 return -ESRCH;
228 mm = get_task_mm(tsk);
229 put_task_struct(tsk);
230 if (!mm)
231 return 0;
232 /* Check if process spawned far enough to have cmdline. */
233 if (!mm->env_end) {
234 rv = 0;
235 goto out_mmput;
238 page = (char *)__get_free_page(GFP_KERNEL);
239 if (!page) {
240 rv = -ENOMEM;
241 goto out_mmput;
244 down_read(&mm->mmap_sem);
245 arg_start = mm->arg_start;
246 arg_end = mm->arg_end;
247 env_start = mm->env_start;
248 env_end = mm->env_end;
249 up_read(&mm->mmap_sem);
251 BUG_ON(arg_start > arg_end);
252 BUG_ON(env_start > env_end);
254 len1 = arg_end - arg_start;
255 len2 = env_end - env_start;
257 /* Empty ARGV. */
258 if (len1 == 0) {
259 rv = 0;
260 goto out_free_page;
263 * Inherently racy -- command line shares address space
264 * with code and data.
266 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
267 if (rv <= 0)
268 goto out_free_page;
270 rv = 0;
272 if (c == '\0') {
273 /* Command line (set of strings) occupies whole ARGV. */
274 if (len1 <= *pos)
275 goto out_free_page;
277 p = arg_start + *pos;
278 len = len1 - *pos;
279 while (count > 0 && len > 0) {
280 unsigned int _count;
281 int nr_read;
283 _count = min3(count, len, PAGE_SIZE);
284 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
285 if (nr_read < 0)
286 rv = nr_read;
287 if (nr_read <= 0)
288 goto out_free_page;
290 if (copy_to_user(buf, page, nr_read)) {
291 rv = -EFAULT;
292 goto out_free_page;
295 p += nr_read;
296 len -= nr_read;
297 buf += nr_read;
298 count -= nr_read;
299 rv += nr_read;
301 } else {
303 * Command line (1 string) occupies ARGV and
304 * extends into ENVP.
306 struct {
307 unsigned long p;
308 unsigned long len;
309 } cmdline[2] = {
310 { .p = arg_start, .len = len1 },
311 { .p = env_start, .len = len2 },
313 loff_t pos1 = *pos;
314 unsigned int i;
316 i = 0;
317 while (i < 2 && pos1 >= cmdline[i].len) {
318 pos1 -= cmdline[i].len;
319 i++;
321 while (i < 2) {
322 p = cmdline[i].p + pos1;
323 len = cmdline[i].len - pos1;
324 while (count > 0 && len > 0) {
325 unsigned int _count, l;
326 int nr_read;
327 bool final;
329 _count = min3(count, len, PAGE_SIZE);
330 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
331 if (nr_read < 0)
332 rv = nr_read;
333 if (nr_read <= 0)
334 goto out_free_page;
337 * Command line can be shorter than whole ARGV
338 * even if last "marker" byte says it is not.
340 final = false;
341 l = strnlen(page, nr_read);
342 if (l < nr_read) {
343 nr_read = l;
344 final = true;
347 if (copy_to_user(buf, page, nr_read)) {
348 rv = -EFAULT;
349 goto out_free_page;
352 p += nr_read;
353 len -= nr_read;
354 buf += nr_read;
355 count -= nr_read;
356 rv += nr_read;
358 if (final)
359 goto out_free_page;
362 /* Only first chunk can be read partially. */
363 pos1 = 0;
364 i++;
368 out_free_page:
369 free_page((unsigned long)page);
370 out_mmput:
371 mmput(mm);
372 if (rv > 0)
373 *pos += rv;
374 return rv;
377 static const struct file_operations proc_pid_cmdline_ops = {
378 .read = proc_pid_cmdline_read,
379 .llseek = generic_file_llseek,
382 #ifdef CONFIG_KALLSYMS
384 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
385 * Returns the resolved symbol. If that fails, simply return the address.
387 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
388 struct pid *pid, struct task_struct *task)
390 unsigned long wchan;
391 char symname[KSYM_NAME_LEN];
393 wchan = get_wchan(task);
395 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
396 && !lookup_symbol_name(wchan, symname))
397 seq_printf(m, "%s", symname);
398 else
399 seq_putc(m, '0');
401 return 0;
403 #endif /* CONFIG_KALLSYMS */
405 static int lock_trace(struct task_struct *task)
407 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
408 if (err)
409 return err;
410 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
411 mutex_unlock(&task->signal->cred_guard_mutex);
412 return -EPERM;
414 return 0;
417 static void unlock_trace(struct task_struct *task)
419 mutex_unlock(&task->signal->cred_guard_mutex);
422 #ifdef CONFIG_STACKTRACE
424 #define MAX_STACK_TRACE_DEPTH 64
426 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
427 struct pid *pid, struct task_struct *task)
429 struct stack_trace trace;
430 unsigned long *entries;
431 int err;
432 int i;
435 * The ability to racily run the kernel stack unwinder on a running task
436 * and then observe the unwinder output is scary; while it is useful for
437 * debugging kernel issues, it can also allow an attacker to leak kernel
438 * stack contents.
439 * Doing this in a manner that is at least safe from races would require
440 * some work to ensure that the remote task can not be scheduled; and
441 * even then, this would still expose the unwinder as local attack
442 * surface.
443 * Therefore, this interface is restricted to root.
445 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
446 return -EACCES;
448 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
449 if (!entries)
450 return -ENOMEM;
452 trace.nr_entries = 0;
453 trace.max_entries = MAX_STACK_TRACE_DEPTH;
454 trace.entries = entries;
455 trace.skip = 0;
457 err = lock_trace(task);
458 if (!err) {
459 save_stack_trace_tsk(task, &trace);
461 for (i = 0; i < trace.nr_entries; i++) {
462 seq_printf(m, "[<%pK>] %pB\n",
463 (void *)entries[i], (void *)entries[i]);
465 unlock_trace(task);
467 kfree(entries);
469 return err;
471 #endif
473 #ifdef CONFIG_SCHED_INFO
475 * Provides /proc/PID/schedstat
477 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
478 struct pid *pid, struct task_struct *task)
480 if (unlikely(!sched_info_on()))
481 seq_printf(m, "0 0 0\n");
482 else
483 seq_printf(m, "%llu %llu %lu\n",
484 (unsigned long long)task->se.sum_exec_runtime,
485 (unsigned long long)task->sched_info.run_delay,
486 task->sched_info.pcount);
488 return 0;
490 #endif
492 #ifdef CONFIG_LATENCYTOP
493 static int lstats_show_proc(struct seq_file *m, void *v)
495 int i;
496 struct inode *inode = m->private;
497 struct task_struct *task = get_proc_task(inode);
499 if (!task)
500 return -ESRCH;
501 seq_puts(m, "Latency Top version : v0.1\n");
502 for (i = 0; i < 32; i++) {
503 struct latency_record *lr = &task->latency_record[i];
504 if (lr->backtrace[0]) {
505 int q;
506 seq_printf(m, "%i %li %li",
507 lr->count, lr->time, lr->max);
508 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
509 unsigned long bt = lr->backtrace[q];
510 if (!bt)
511 break;
512 if (bt == ULONG_MAX)
513 break;
514 seq_printf(m, " %ps", (void *)bt);
516 seq_putc(m, '\n');
520 put_task_struct(task);
521 return 0;
524 static int lstats_open(struct inode *inode, struct file *file)
526 return single_open(file, lstats_show_proc, inode);
529 static ssize_t lstats_write(struct file *file, const char __user *buf,
530 size_t count, loff_t *offs)
532 struct task_struct *task = get_proc_task(file_inode(file));
534 if (!task)
535 return -ESRCH;
536 clear_all_latency_tracing(task);
537 put_task_struct(task);
539 return count;
542 static const struct file_operations proc_lstats_operations = {
543 .open = lstats_open,
544 .read = seq_read,
545 .write = lstats_write,
546 .llseek = seq_lseek,
547 .release = single_release,
550 #endif
552 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
553 struct pid *pid, struct task_struct *task)
555 unsigned long totalpages = totalram_pages + total_swap_pages;
556 unsigned long points = 0;
558 points = oom_badness(task, NULL, NULL, totalpages) *
559 1000 / totalpages;
560 seq_printf(m, "%lu\n", points);
562 return 0;
565 struct limit_names {
566 const char *name;
567 const char *unit;
570 static const struct limit_names lnames[RLIM_NLIMITS] = {
571 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
572 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
573 [RLIMIT_DATA] = {"Max data size", "bytes"},
574 [RLIMIT_STACK] = {"Max stack size", "bytes"},
575 [RLIMIT_CORE] = {"Max core file size", "bytes"},
576 [RLIMIT_RSS] = {"Max resident set", "bytes"},
577 [RLIMIT_NPROC] = {"Max processes", "processes"},
578 [RLIMIT_NOFILE] = {"Max open files", "files"},
579 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
580 [RLIMIT_AS] = {"Max address space", "bytes"},
581 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
582 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
583 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
584 [RLIMIT_NICE] = {"Max nice priority", NULL},
585 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
586 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
589 /* Display limits for a process */
590 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
591 struct pid *pid, struct task_struct *task)
593 unsigned int i;
594 unsigned long flags;
596 struct rlimit rlim[RLIM_NLIMITS];
598 if (!lock_task_sighand(task, &flags))
599 return 0;
600 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
601 unlock_task_sighand(task, &flags);
604 * print the file header
606 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
607 "Limit", "Soft Limit", "Hard Limit", "Units");
609 for (i = 0; i < RLIM_NLIMITS; i++) {
610 if (rlim[i].rlim_cur == RLIM_INFINITY)
611 seq_printf(m, "%-25s %-20s ",
612 lnames[i].name, "unlimited");
613 else
614 seq_printf(m, "%-25s %-20lu ",
615 lnames[i].name, rlim[i].rlim_cur);
617 if (rlim[i].rlim_max == RLIM_INFINITY)
618 seq_printf(m, "%-20s ", "unlimited");
619 else
620 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
622 if (lnames[i].unit)
623 seq_printf(m, "%-10s\n", lnames[i].unit);
624 else
625 seq_putc(m, '\n');
628 return 0;
631 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
632 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
633 struct pid *pid, struct task_struct *task)
635 long nr;
636 unsigned long args[6], sp, pc;
637 int res;
639 res = lock_trace(task);
640 if (res)
641 return res;
643 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
644 seq_puts(m, "running\n");
645 else if (nr < 0)
646 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
647 else
648 seq_printf(m,
649 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
651 args[0], args[1], args[2], args[3], args[4], args[5],
652 sp, pc);
653 unlock_trace(task);
655 return 0;
657 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
659 /************************************************************************/
660 /* Here the fs part begins */
661 /************************************************************************/
663 /* permission checks */
664 static int proc_fd_access_allowed(struct inode *inode)
666 struct task_struct *task;
667 int allowed = 0;
668 /* Allow access to a task's file descriptors if it is us or we
669 * may use ptrace attach to the process and find out that
670 * information.
672 task = get_proc_task(inode);
673 if (task) {
674 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
675 put_task_struct(task);
677 return allowed;
680 int proc_setattr(struct dentry *dentry, struct iattr *attr)
682 int error;
683 struct inode *inode = d_inode(dentry);
685 if (attr->ia_valid & ATTR_MODE)
686 return -EPERM;
688 error = setattr_prepare(dentry, attr);
689 if (error)
690 return error;
692 setattr_copy(inode, attr);
693 mark_inode_dirty(inode);
694 return 0;
698 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
699 * or euid/egid (for hide_pid_min=2)?
701 static bool has_pid_permissions(struct pid_namespace *pid,
702 struct task_struct *task,
703 int hide_pid_min)
705 if (pid->hide_pid < hide_pid_min)
706 return true;
707 if (in_group_p(pid->pid_gid))
708 return true;
709 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
713 static int proc_pid_permission(struct inode *inode, int mask)
715 struct pid_namespace *pid = inode->i_sb->s_fs_info;
716 struct task_struct *task;
717 bool has_perms;
719 task = get_proc_task(inode);
720 if (!task)
721 return -ESRCH;
722 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
723 put_task_struct(task);
725 if (!has_perms) {
726 if (pid->hide_pid == HIDEPID_INVISIBLE) {
728 * Let's make getdents(), stat(), and open()
729 * consistent with each other. If a process
730 * may not stat() a file, it shouldn't be seen
731 * in procfs at all.
733 return -ENOENT;
736 return -EPERM;
738 return generic_permission(inode, mask);
743 static const struct inode_operations proc_def_inode_operations = {
744 .setattr = proc_setattr,
747 static int proc_single_show(struct seq_file *m, void *v)
749 struct inode *inode = m->private;
750 struct pid_namespace *ns;
751 struct pid *pid;
752 struct task_struct *task;
753 int ret;
755 ns = inode->i_sb->s_fs_info;
756 pid = proc_pid(inode);
757 task = get_pid_task(pid, PIDTYPE_PID);
758 if (!task)
759 return -ESRCH;
761 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
763 put_task_struct(task);
764 return ret;
767 static int proc_single_open(struct inode *inode, struct file *filp)
769 return single_open(filp, proc_single_show, inode);
772 static const struct file_operations proc_single_file_operations = {
773 .open = proc_single_open,
774 .read = seq_read,
775 .llseek = seq_lseek,
776 .release = single_release,
780 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
782 struct task_struct *task = get_proc_task(inode);
783 struct mm_struct *mm = ERR_PTR(-ESRCH);
785 if (task) {
786 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
787 put_task_struct(task);
789 if (!IS_ERR_OR_NULL(mm)) {
790 /* ensure this mm_struct can't be freed */
791 mmgrab(mm);
792 /* but do not pin its memory */
793 mmput(mm);
797 return mm;
800 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
802 struct mm_struct *mm = proc_mem_open(inode, mode);
804 if (IS_ERR(mm))
805 return PTR_ERR(mm);
807 file->private_data = mm;
808 return 0;
811 static int mem_open(struct inode *inode, struct file *file)
813 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
815 /* OK to pass negative loff_t, we can catch out-of-range */
816 file->f_mode |= FMODE_UNSIGNED_OFFSET;
818 return ret;
821 static ssize_t mem_rw(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos, int write)
824 struct mm_struct *mm = file->private_data;
825 unsigned long addr = *ppos;
826 ssize_t copied;
827 char *page;
828 unsigned int flags;
830 if (!mm)
831 return 0;
833 page = (char *)__get_free_page(GFP_KERNEL);
834 if (!page)
835 return -ENOMEM;
837 copied = 0;
838 if (!mmget_not_zero(mm))
839 goto free;
841 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
843 while (count > 0) {
844 int this_len = min_t(int, count, PAGE_SIZE);
846 if (write && copy_from_user(page, buf, this_len)) {
847 copied = -EFAULT;
848 break;
851 this_len = access_remote_vm(mm, addr, page, this_len, flags);
852 if (!this_len) {
853 if (!copied)
854 copied = -EIO;
855 break;
858 if (!write && copy_to_user(buf, page, this_len)) {
859 copied = -EFAULT;
860 break;
863 buf += this_len;
864 addr += this_len;
865 copied += this_len;
866 count -= this_len;
868 *ppos = addr;
870 mmput(mm);
871 free:
872 free_page((unsigned long) page);
873 return copied;
876 static ssize_t mem_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
879 return mem_rw(file, buf, count, ppos, 0);
882 static ssize_t mem_write(struct file *file, const char __user *buf,
883 size_t count, loff_t *ppos)
885 return mem_rw(file, (char __user*)buf, count, ppos, 1);
888 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
890 switch (orig) {
891 case 0:
892 file->f_pos = offset;
893 break;
894 case 1:
895 file->f_pos += offset;
896 break;
897 default:
898 return -EINVAL;
900 force_successful_syscall_return();
901 return file->f_pos;
904 static int mem_release(struct inode *inode, struct file *file)
906 struct mm_struct *mm = file->private_data;
907 if (mm)
908 mmdrop(mm);
909 return 0;
912 static const struct file_operations proc_mem_operations = {
913 .llseek = mem_lseek,
914 .read = mem_read,
915 .write = mem_write,
916 .open = mem_open,
917 .release = mem_release,
920 static int environ_open(struct inode *inode, struct file *file)
922 return __mem_open(inode, file, PTRACE_MODE_READ);
925 static ssize_t environ_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
928 char *page;
929 unsigned long src = *ppos;
930 int ret = 0;
931 struct mm_struct *mm = file->private_data;
932 unsigned long env_start, env_end;
934 /* Ensure the process spawned far enough to have an environment. */
935 if (!mm || !mm->env_end)
936 return 0;
938 page = (char *)__get_free_page(GFP_KERNEL);
939 if (!page)
940 return -ENOMEM;
942 ret = 0;
943 if (!mmget_not_zero(mm))
944 goto free;
946 down_read(&mm->mmap_sem);
947 env_start = mm->env_start;
948 env_end = mm->env_end;
949 up_read(&mm->mmap_sem);
951 while (count > 0) {
952 size_t this_len, max_len;
953 int retval;
955 if (src >= (env_end - env_start))
956 break;
958 this_len = env_end - (env_start + src);
960 max_len = min_t(size_t, PAGE_SIZE, count);
961 this_len = min(max_len, this_len);
963 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
965 if (retval <= 0) {
966 ret = retval;
967 break;
970 if (copy_to_user(buf, page, retval)) {
971 ret = -EFAULT;
972 break;
975 ret += retval;
976 src += retval;
977 buf += retval;
978 count -= retval;
980 *ppos = src;
981 mmput(mm);
983 free:
984 free_page((unsigned long) page);
985 return ret;
988 static const struct file_operations proc_environ_operations = {
989 .open = environ_open,
990 .read = environ_read,
991 .llseek = generic_file_llseek,
992 .release = mem_release,
995 static int auxv_open(struct inode *inode, struct file *file)
997 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1000 static ssize_t auxv_read(struct file *file, char __user *buf,
1001 size_t count, loff_t *ppos)
1003 struct mm_struct *mm = file->private_data;
1004 unsigned int nwords = 0;
1006 if (!mm)
1007 return 0;
1008 do {
1009 nwords += 2;
1010 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1011 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1012 nwords * sizeof(mm->saved_auxv[0]));
1015 static const struct file_operations proc_auxv_operations = {
1016 .open = auxv_open,
1017 .read = auxv_read,
1018 .llseek = generic_file_llseek,
1019 .release = mem_release,
1022 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1023 loff_t *ppos)
1025 struct task_struct *task = get_proc_task(file_inode(file));
1026 char buffer[PROC_NUMBUF];
1027 int oom_adj = OOM_ADJUST_MIN;
1028 size_t len;
1030 if (!task)
1031 return -ESRCH;
1032 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1033 oom_adj = OOM_ADJUST_MAX;
1034 else
1035 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1036 OOM_SCORE_ADJ_MAX;
1037 put_task_struct(task);
1038 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1039 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1042 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1044 struct mm_struct *mm = NULL;
1045 struct task_struct *task;
1046 int err = 0;
1048 task = get_proc_task(file_inode(file));
1049 if (!task)
1050 return -ESRCH;
1052 mutex_lock(&oom_adj_mutex);
1053 if (legacy) {
1054 if (oom_adj < task->signal->oom_score_adj &&
1055 !capable(CAP_SYS_RESOURCE)) {
1056 err = -EACCES;
1057 goto err_unlock;
1060 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1061 * /proc/pid/oom_score_adj instead.
1063 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1064 current->comm, task_pid_nr(current), task_pid_nr(task),
1065 task_pid_nr(task));
1066 } else {
1067 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1068 !capable(CAP_SYS_RESOURCE)) {
1069 err = -EACCES;
1070 goto err_unlock;
1075 * Make sure we will check other processes sharing the mm if this is
1076 * not vfrok which wants its own oom_score_adj.
1077 * pin the mm so it doesn't go away and get reused after task_unlock
1079 if (!task->vfork_done) {
1080 struct task_struct *p = find_lock_task_mm(task);
1082 if (p) {
1083 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1084 mm = p->mm;
1085 mmgrab(mm);
1087 task_unlock(p);
1091 task->signal->oom_score_adj = oom_adj;
1092 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1093 task->signal->oom_score_adj_min = (short)oom_adj;
1094 trace_oom_score_adj_update(task);
1096 if (mm) {
1097 struct task_struct *p;
1099 rcu_read_lock();
1100 for_each_process(p) {
1101 if (same_thread_group(task, p))
1102 continue;
1104 /* do not touch kernel threads or the global init */
1105 if (p->flags & PF_KTHREAD || is_global_init(p))
1106 continue;
1108 task_lock(p);
1109 if (!p->vfork_done && process_shares_mm(p, mm)) {
1110 p->signal->oom_score_adj = oom_adj;
1111 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1112 p->signal->oom_score_adj_min = (short)oom_adj;
1114 task_unlock(p);
1116 rcu_read_unlock();
1117 mmdrop(mm);
1119 err_unlock:
1120 mutex_unlock(&oom_adj_mutex);
1121 put_task_struct(task);
1122 return err;
1126 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1127 * kernels. The effective policy is defined by oom_score_adj, which has a
1128 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1129 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1130 * Processes that become oom disabled via oom_adj will still be oom disabled
1131 * with this implementation.
1133 * oom_adj cannot be removed since existing userspace binaries use it.
1135 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1136 size_t count, loff_t *ppos)
1138 char buffer[PROC_NUMBUF];
1139 int oom_adj;
1140 int err;
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
1145 if (copy_from_user(buffer, buf, count)) {
1146 err = -EFAULT;
1147 goto out;
1150 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1151 if (err)
1152 goto out;
1153 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1154 oom_adj != OOM_DISABLE) {
1155 err = -EINVAL;
1156 goto out;
1160 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1161 * value is always attainable.
1163 if (oom_adj == OOM_ADJUST_MAX)
1164 oom_adj = OOM_SCORE_ADJ_MAX;
1165 else
1166 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1168 err = __set_oom_adj(file, oom_adj, true);
1169 out:
1170 return err < 0 ? err : count;
1173 static const struct file_operations proc_oom_adj_operations = {
1174 .read = oom_adj_read,
1175 .write = oom_adj_write,
1176 .llseek = generic_file_llseek,
1179 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1180 size_t count, loff_t *ppos)
1182 struct task_struct *task = get_proc_task(file_inode(file));
1183 char buffer[PROC_NUMBUF];
1184 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1185 size_t len;
1187 if (!task)
1188 return -ESRCH;
1189 oom_score_adj = task->signal->oom_score_adj;
1190 put_task_struct(task);
1191 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1192 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1195 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1196 size_t count, loff_t *ppos)
1198 char buffer[PROC_NUMBUF];
1199 int oom_score_adj;
1200 int err;
1202 memset(buffer, 0, sizeof(buffer));
1203 if (count > sizeof(buffer) - 1)
1204 count = sizeof(buffer) - 1;
1205 if (copy_from_user(buffer, buf, count)) {
1206 err = -EFAULT;
1207 goto out;
1210 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1211 if (err)
1212 goto out;
1213 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1214 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1215 err = -EINVAL;
1216 goto out;
1219 err = __set_oom_adj(file, oom_score_adj, false);
1220 out:
1221 return err < 0 ? err : count;
1224 static const struct file_operations proc_oom_score_adj_operations = {
1225 .read = oom_score_adj_read,
1226 .write = oom_score_adj_write,
1227 .llseek = default_llseek,
1230 #ifdef CONFIG_AUDITSYSCALL
1231 #define TMPBUFLEN 11
1232 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1233 size_t count, loff_t *ppos)
1235 struct inode * inode = file_inode(file);
1236 struct task_struct *task = get_proc_task(inode);
1237 ssize_t length;
1238 char tmpbuf[TMPBUFLEN];
1240 if (!task)
1241 return -ESRCH;
1242 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1243 from_kuid(file->f_cred->user_ns,
1244 audit_get_loginuid(task)));
1245 put_task_struct(task);
1246 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1249 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1250 size_t count, loff_t *ppos)
1252 struct inode * inode = file_inode(file);
1253 uid_t loginuid;
1254 kuid_t kloginuid;
1255 int rv;
1257 rcu_read_lock();
1258 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1259 rcu_read_unlock();
1260 return -EPERM;
1262 rcu_read_unlock();
1264 if (*ppos != 0) {
1265 /* No partial writes. */
1266 return -EINVAL;
1269 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1270 if (rv < 0)
1271 return rv;
1273 /* is userspace tring to explicitly UNSET the loginuid? */
1274 if (loginuid == AUDIT_UID_UNSET) {
1275 kloginuid = INVALID_UID;
1276 } else {
1277 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1278 if (!uid_valid(kloginuid))
1279 return -EINVAL;
1282 rv = audit_set_loginuid(kloginuid);
1283 if (rv < 0)
1284 return rv;
1285 return count;
1288 static const struct file_operations proc_loginuid_operations = {
1289 .read = proc_loginuid_read,
1290 .write = proc_loginuid_write,
1291 .llseek = generic_file_llseek,
1294 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1295 size_t count, loff_t *ppos)
1297 struct inode * inode = file_inode(file);
1298 struct task_struct *task = get_proc_task(inode);
1299 ssize_t length;
1300 char tmpbuf[TMPBUFLEN];
1302 if (!task)
1303 return -ESRCH;
1304 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1305 audit_get_sessionid(task));
1306 put_task_struct(task);
1307 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1310 static const struct file_operations proc_sessionid_operations = {
1311 .read = proc_sessionid_read,
1312 .llseek = generic_file_llseek,
1314 #endif
1316 #ifdef CONFIG_FAULT_INJECTION
1317 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1318 size_t count, loff_t *ppos)
1320 struct task_struct *task = get_proc_task(file_inode(file));
1321 char buffer[PROC_NUMBUF];
1322 size_t len;
1323 int make_it_fail;
1325 if (!task)
1326 return -ESRCH;
1327 make_it_fail = task->make_it_fail;
1328 put_task_struct(task);
1330 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1332 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1335 static ssize_t proc_fault_inject_write(struct file * file,
1336 const char __user * buf, size_t count, loff_t *ppos)
1338 struct task_struct *task;
1339 char buffer[PROC_NUMBUF];
1340 int make_it_fail;
1341 int rv;
1343 if (!capable(CAP_SYS_RESOURCE))
1344 return -EPERM;
1345 memset(buffer, 0, sizeof(buffer));
1346 if (count > sizeof(buffer) - 1)
1347 count = sizeof(buffer) - 1;
1348 if (copy_from_user(buffer, buf, count))
1349 return -EFAULT;
1350 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1351 if (rv < 0)
1352 return rv;
1353 if (make_it_fail < 0 || make_it_fail > 1)
1354 return -EINVAL;
1356 task = get_proc_task(file_inode(file));
1357 if (!task)
1358 return -ESRCH;
1359 task->make_it_fail = make_it_fail;
1360 put_task_struct(task);
1362 return count;
1365 static const struct file_operations proc_fault_inject_operations = {
1366 .read = proc_fault_inject_read,
1367 .write = proc_fault_inject_write,
1368 .llseek = generic_file_llseek,
1371 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1372 size_t count, loff_t *ppos)
1374 struct task_struct *task;
1375 int err;
1376 unsigned int n;
1378 err = kstrtouint_from_user(buf, count, 0, &n);
1379 if (err)
1380 return err;
1382 task = get_proc_task(file_inode(file));
1383 if (!task)
1384 return -ESRCH;
1385 WRITE_ONCE(task->fail_nth, n);
1386 put_task_struct(task);
1388 return count;
1391 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1392 size_t count, loff_t *ppos)
1394 struct task_struct *task;
1395 char numbuf[PROC_NUMBUF];
1396 ssize_t len;
1398 task = get_proc_task(file_inode(file));
1399 if (!task)
1400 return -ESRCH;
1401 len = snprintf(numbuf, sizeof(numbuf), "%u\n",
1402 READ_ONCE(task->fail_nth));
1403 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
1404 put_task_struct(task);
1406 return len;
1409 static const struct file_operations proc_fail_nth_operations = {
1410 .read = proc_fail_nth_read,
1411 .write = proc_fail_nth_write,
1413 #endif
1416 #ifdef CONFIG_SCHED_DEBUG
1418 * Print out various scheduling related per-task fields:
1420 static int sched_show(struct seq_file *m, void *v)
1422 struct inode *inode = m->private;
1423 struct pid_namespace *ns = inode->i_sb->s_fs_info;
1424 struct task_struct *p;
1426 p = get_proc_task(inode);
1427 if (!p)
1428 return -ESRCH;
1429 proc_sched_show_task(p, ns, m);
1431 put_task_struct(p);
1433 return 0;
1436 static ssize_t
1437 sched_write(struct file *file, const char __user *buf,
1438 size_t count, loff_t *offset)
1440 struct inode *inode = file_inode(file);
1441 struct task_struct *p;
1443 p = get_proc_task(inode);
1444 if (!p)
1445 return -ESRCH;
1446 proc_sched_set_task(p);
1448 put_task_struct(p);
1450 return count;
1453 static int sched_open(struct inode *inode, struct file *filp)
1455 return single_open(filp, sched_show, inode);
1458 static const struct file_operations proc_pid_sched_operations = {
1459 .open = sched_open,
1460 .read = seq_read,
1461 .write = sched_write,
1462 .llseek = seq_lseek,
1463 .release = single_release,
1466 #endif
1468 #ifdef CONFIG_SCHED_AUTOGROUP
1470 * Print out autogroup related information:
1472 static int sched_autogroup_show(struct seq_file *m, void *v)
1474 struct inode *inode = m->private;
1475 struct task_struct *p;
1477 p = get_proc_task(inode);
1478 if (!p)
1479 return -ESRCH;
1480 proc_sched_autogroup_show_task(p, m);
1482 put_task_struct(p);
1484 return 0;
1487 static ssize_t
1488 sched_autogroup_write(struct file *file, const char __user *buf,
1489 size_t count, loff_t *offset)
1491 struct inode *inode = file_inode(file);
1492 struct task_struct *p;
1493 char buffer[PROC_NUMBUF];
1494 int nice;
1495 int err;
1497 memset(buffer, 0, sizeof(buffer));
1498 if (count > sizeof(buffer) - 1)
1499 count = sizeof(buffer) - 1;
1500 if (copy_from_user(buffer, buf, count))
1501 return -EFAULT;
1503 err = kstrtoint(strstrip(buffer), 0, &nice);
1504 if (err < 0)
1505 return err;
1507 p = get_proc_task(inode);
1508 if (!p)
1509 return -ESRCH;
1511 err = proc_sched_autogroup_set_nice(p, nice);
1512 if (err)
1513 count = err;
1515 put_task_struct(p);
1517 return count;
1520 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1522 int ret;
1524 ret = single_open(filp, sched_autogroup_show, NULL);
1525 if (!ret) {
1526 struct seq_file *m = filp->private_data;
1528 m->private = inode;
1530 return ret;
1533 static const struct file_operations proc_pid_sched_autogroup_operations = {
1534 .open = sched_autogroup_open,
1535 .read = seq_read,
1536 .write = sched_autogroup_write,
1537 .llseek = seq_lseek,
1538 .release = single_release,
1541 #endif /* CONFIG_SCHED_AUTOGROUP */
1543 static ssize_t comm_write(struct file *file, const char __user *buf,
1544 size_t count, loff_t *offset)
1546 struct inode *inode = file_inode(file);
1547 struct task_struct *p;
1548 char buffer[TASK_COMM_LEN];
1549 const size_t maxlen = sizeof(buffer) - 1;
1551 memset(buffer, 0, sizeof(buffer));
1552 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1553 return -EFAULT;
1555 p = get_proc_task(inode);
1556 if (!p)
1557 return -ESRCH;
1559 if (same_thread_group(current, p))
1560 set_task_comm(p, buffer);
1561 else
1562 count = -EINVAL;
1564 put_task_struct(p);
1566 return count;
1569 static int comm_show(struct seq_file *m, void *v)
1571 struct inode *inode = m->private;
1572 struct task_struct *p;
1574 p = get_proc_task(inode);
1575 if (!p)
1576 return -ESRCH;
1578 task_lock(p);
1579 seq_printf(m, "%s\n", p->comm);
1580 task_unlock(p);
1582 put_task_struct(p);
1584 return 0;
1587 static int comm_open(struct inode *inode, struct file *filp)
1589 return single_open(filp, comm_show, inode);
1592 static const struct file_operations proc_pid_set_comm_operations = {
1593 .open = comm_open,
1594 .read = seq_read,
1595 .write = comm_write,
1596 .llseek = seq_lseek,
1597 .release = single_release,
1600 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1602 struct task_struct *task;
1603 struct file *exe_file;
1605 task = get_proc_task(d_inode(dentry));
1606 if (!task)
1607 return -ENOENT;
1608 exe_file = get_task_exe_file(task);
1609 put_task_struct(task);
1610 if (exe_file) {
1611 *exe_path = exe_file->f_path;
1612 path_get(&exe_file->f_path);
1613 fput(exe_file);
1614 return 0;
1615 } else
1616 return -ENOENT;
1619 static const char *proc_pid_get_link(struct dentry *dentry,
1620 struct inode *inode,
1621 struct delayed_call *done)
1623 struct path path;
1624 int error = -EACCES;
1626 if (!dentry)
1627 return ERR_PTR(-ECHILD);
1629 /* Are we allowed to snoop on the tasks file descriptors? */
1630 if (!proc_fd_access_allowed(inode))
1631 goto out;
1633 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1634 if (error)
1635 goto out;
1637 nd_jump_link(&path);
1638 return NULL;
1639 out:
1640 return ERR_PTR(error);
1643 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1645 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1646 char *pathname;
1647 int len;
1649 if (!tmp)
1650 return -ENOMEM;
1652 pathname = d_path(path, tmp, PAGE_SIZE);
1653 len = PTR_ERR(pathname);
1654 if (IS_ERR(pathname))
1655 goto out;
1656 len = tmp + PAGE_SIZE - 1 - pathname;
1658 if (len > buflen)
1659 len = buflen;
1660 if (copy_to_user(buffer, pathname, len))
1661 len = -EFAULT;
1662 out:
1663 free_page((unsigned long)tmp);
1664 return len;
1667 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1669 int error = -EACCES;
1670 struct inode *inode = d_inode(dentry);
1671 struct path path;
1673 /* Are we allowed to snoop on the tasks file descriptors? */
1674 if (!proc_fd_access_allowed(inode))
1675 goto out;
1677 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1678 if (error)
1679 goto out;
1681 error = do_proc_readlink(&path, buffer, buflen);
1682 path_put(&path);
1683 out:
1684 return error;
1687 const struct inode_operations proc_pid_link_inode_operations = {
1688 .readlink = proc_pid_readlink,
1689 .get_link = proc_pid_get_link,
1690 .setattr = proc_setattr,
1694 /* building an inode */
1696 void task_dump_owner(struct task_struct *task, mode_t mode,
1697 kuid_t *ruid, kgid_t *rgid)
1699 /* Depending on the state of dumpable compute who should own a
1700 * proc file for a task.
1702 const struct cred *cred;
1703 kuid_t uid;
1704 kgid_t gid;
1706 if (unlikely(task->flags & PF_KTHREAD)) {
1707 *ruid = GLOBAL_ROOT_UID;
1708 *rgid = GLOBAL_ROOT_GID;
1709 return;
1712 /* Default to the tasks effective ownership */
1713 rcu_read_lock();
1714 cred = __task_cred(task);
1715 uid = cred->euid;
1716 gid = cred->egid;
1717 rcu_read_unlock();
1720 * Before the /proc/pid/status file was created the only way to read
1721 * the effective uid of a /process was to stat /proc/pid. Reading
1722 * /proc/pid/status is slow enough that procps and other packages
1723 * kept stating /proc/pid. To keep the rules in /proc simple I have
1724 * made this apply to all per process world readable and executable
1725 * directories.
1727 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1728 struct mm_struct *mm;
1729 task_lock(task);
1730 mm = task->mm;
1731 /* Make non-dumpable tasks owned by some root */
1732 if (mm) {
1733 if (get_dumpable(mm) != SUID_DUMP_USER) {
1734 struct user_namespace *user_ns = mm->user_ns;
1736 uid = make_kuid(user_ns, 0);
1737 if (!uid_valid(uid))
1738 uid = GLOBAL_ROOT_UID;
1740 gid = make_kgid(user_ns, 0);
1741 if (!gid_valid(gid))
1742 gid = GLOBAL_ROOT_GID;
1744 } else {
1745 uid = GLOBAL_ROOT_UID;
1746 gid = GLOBAL_ROOT_GID;
1748 task_unlock(task);
1750 *ruid = uid;
1751 *rgid = gid;
1754 struct inode *proc_pid_make_inode(struct super_block * sb,
1755 struct task_struct *task, umode_t mode)
1757 struct inode * inode;
1758 struct proc_inode *ei;
1760 /* We need a new inode */
1762 inode = new_inode(sb);
1763 if (!inode)
1764 goto out;
1766 /* Common stuff */
1767 ei = PROC_I(inode);
1768 inode->i_mode = mode;
1769 inode->i_ino = get_next_ino();
1770 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1771 inode->i_op = &proc_def_inode_operations;
1774 * grab the reference to task.
1776 ei->pid = get_task_pid(task, PIDTYPE_PID);
1777 if (!ei->pid)
1778 goto out_unlock;
1780 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1781 security_task_to_inode(task, inode);
1783 out:
1784 return inode;
1786 out_unlock:
1787 iput(inode);
1788 return NULL;
1791 int pid_getattr(const struct path *path, struct kstat *stat,
1792 u32 request_mask, unsigned int query_flags)
1794 struct inode *inode = d_inode(path->dentry);
1795 struct task_struct *task;
1796 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
1798 generic_fillattr(inode, stat);
1800 rcu_read_lock();
1801 stat->uid = GLOBAL_ROOT_UID;
1802 stat->gid = GLOBAL_ROOT_GID;
1803 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1804 if (task) {
1805 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
1806 rcu_read_unlock();
1808 * This doesn't prevent learning whether PID exists,
1809 * it only makes getattr() consistent with readdir().
1811 return -ENOENT;
1813 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
1815 rcu_read_unlock();
1816 return 0;
1819 /* dentry stuff */
1822 * Exceptional case: normally we are not allowed to unhash a busy
1823 * directory. In this case, however, we can do it - no aliasing problems
1824 * due to the way we treat inodes.
1826 * Rewrite the inode's ownerships here because the owning task may have
1827 * performed a setuid(), etc.
1830 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1832 struct inode *inode;
1833 struct task_struct *task;
1835 if (flags & LOOKUP_RCU)
1836 return -ECHILD;
1838 inode = d_inode(dentry);
1839 task = get_proc_task(inode);
1841 if (task) {
1842 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1844 inode->i_mode &= ~(S_ISUID | S_ISGID);
1845 security_task_to_inode(task, inode);
1846 put_task_struct(task);
1847 return 1;
1849 return 0;
1852 static inline bool proc_inode_is_dead(struct inode *inode)
1854 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1857 int pid_delete_dentry(const struct dentry *dentry)
1859 /* Is the task we represent dead?
1860 * If so, then don't put the dentry on the lru list,
1861 * kill it immediately.
1863 return proc_inode_is_dead(d_inode(dentry));
1866 const struct dentry_operations pid_dentry_operations =
1868 .d_revalidate = pid_revalidate,
1869 .d_delete = pid_delete_dentry,
1872 /* Lookups */
1875 * Fill a directory entry.
1877 * If possible create the dcache entry and derive our inode number and
1878 * file type from dcache entry.
1880 * Since all of the proc inode numbers are dynamically generated, the inode
1881 * numbers do not exist until the inode is cache. This means creating the
1882 * the dcache entry in readdir is necessary to keep the inode numbers
1883 * reported by readdir in sync with the inode numbers reported
1884 * by stat.
1886 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1887 const char *name, int len,
1888 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1890 struct dentry *child, *dir = file->f_path.dentry;
1891 struct qstr qname = QSTR_INIT(name, len);
1892 struct inode *inode;
1893 unsigned type;
1894 ino_t ino;
1896 child = d_hash_and_lookup(dir, &qname);
1897 if (!child) {
1898 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1899 child = d_alloc_parallel(dir, &qname, &wq);
1900 if (IS_ERR(child))
1901 goto end_instantiate;
1902 if (d_in_lookup(child)) {
1903 int err = instantiate(d_inode(dir), child, task, ptr);
1904 d_lookup_done(child);
1905 if (err < 0) {
1906 dput(child);
1907 goto end_instantiate;
1911 inode = d_inode(child);
1912 ino = inode->i_ino;
1913 type = inode->i_mode >> 12;
1914 dput(child);
1915 return dir_emit(ctx, name, len, ino, type);
1917 end_instantiate:
1918 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1922 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1923 * which represent vma start and end addresses.
1925 static int dname_to_vma_addr(struct dentry *dentry,
1926 unsigned long *start, unsigned long *end)
1928 const char *str = dentry->d_name.name;
1929 unsigned long long sval, eval;
1930 unsigned int len;
1932 len = _parse_integer(str, 16, &sval);
1933 if (len & KSTRTOX_OVERFLOW)
1934 return -EINVAL;
1935 if (sval != (unsigned long)sval)
1936 return -EINVAL;
1937 str += len;
1939 if (*str != '-')
1940 return -EINVAL;
1941 str++;
1943 len = _parse_integer(str, 16, &eval);
1944 if (len & KSTRTOX_OVERFLOW)
1945 return -EINVAL;
1946 if (eval != (unsigned long)eval)
1947 return -EINVAL;
1948 str += len;
1950 if (*str != '\0')
1951 return -EINVAL;
1953 *start = sval;
1954 *end = eval;
1956 return 0;
1959 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1961 unsigned long vm_start, vm_end;
1962 bool exact_vma_exists = false;
1963 struct mm_struct *mm = NULL;
1964 struct task_struct *task;
1965 struct inode *inode;
1966 int status = 0;
1968 if (flags & LOOKUP_RCU)
1969 return -ECHILD;
1971 inode = d_inode(dentry);
1972 task = get_proc_task(inode);
1973 if (!task)
1974 goto out_notask;
1976 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1977 if (IS_ERR_OR_NULL(mm))
1978 goto out;
1980 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1981 down_read(&mm->mmap_sem);
1982 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1983 up_read(&mm->mmap_sem);
1986 mmput(mm);
1988 if (exact_vma_exists) {
1989 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1991 security_task_to_inode(task, inode);
1992 status = 1;
1995 out:
1996 put_task_struct(task);
1998 out_notask:
1999 return status;
2002 static const struct dentry_operations tid_map_files_dentry_operations = {
2003 .d_revalidate = map_files_d_revalidate,
2004 .d_delete = pid_delete_dentry,
2007 static int map_files_get_link(struct dentry *dentry, struct path *path)
2009 unsigned long vm_start, vm_end;
2010 struct vm_area_struct *vma;
2011 struct task_struct *task;
2012 struct mm_struct *mm;
2013 int rc;
2015 rc = -ENOENT;
2016 task = get_proc_task(d_inode(dentry));
2017 if (!task)
2018 goto out;
2020 mm = get_task_mm(task);
2021 put_task_struct(task);
2022 if (!mm)
2023 goto out;
2025 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2026 if (rc)
2027 goto out_mmput;
2029 rc = -ENOENT;
2030 down_read(&mm->mmap_sem);
2031 vma = find_exact_vma(mm, vm_start, vm_end);
2032 if (vma && vma->vm_file) {
2033 *path = vma->vm_file->f_path;
2034 path_get(path);
2035 rc = 0;
2037 up_read(&mm->mmap_sem);
2039 out_mmput:
2040 mmput(mm);
2041 out:
2042 return rc;
2045 struct map_files_info {
2046 fmode_t mode;
2047 unsigned int len;
2048 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2052 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2053 * symlinks may be used to bypass permissions on ancestor directories in the
2054 * path to the file in question.
2056 static const char *
2057 proc_map_files_get_link(struct dentry *dentry,
2058 struct inode *inode,
2059 struct delayed_call *done)
2061 if (!capable(CAP_SYS_ADMIN))
2062 return ERR_PTR(-EPERM);
2064 return proc_pid_get_link(dentry, inode, done);
2068 * Identical to proc_pid_link_inode_operations except for get_link()
2070 static const struct inode_operations proc_map_files_link_inode_operations = {
2071 .readlink = proc_pid_readlink,
2072 .get_link = proc_map_files_get_link,
2073 .setattr = proc_setattr,
2076 static int
2077 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2078 struct task_struct *task, const void *ptr)
2080 fmode_t mode = (fmode_t)(unsigned long)ptr;
2081 struct proc_inode *ei;
2082 struct inode *inode;
2084 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2085 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2086 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2087 if (!inode)
2088 return -ENOENT;
2090 ei = PROC_I(inode);
2091 ei->op.proc_get_link = map_files_get_link;
2093 inode->i_op = &proc_map_files_link_inode_operations;
2094 inode->i_size = 64;
2096 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2097 d_add(dentry, inode);
2099 return 0;
2102 static struct dentry *proc_map_files_lookup(struct inode *dir,
2103 struct dentry *dentry, unsigned int flags)
2105 unsigned long vm_start, vm_end;
2106 struct vm_area_struct *vma;
2107 struct task_struct *task;
2108 int result;
2109 struct mm_struct *mm;
2111 result = -ENOENT;
2112 task = get_proc_task(dir);
2113 if (!task)
2114 goto out;
2116 result = -EACCES;
2117 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2118 goto out_put_task;
2120 result = -ENOENT;
2121 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2122 goto out_put_task;
2124 mm = get_task_mm(task);
2125 if (!mm)
2126 goto out_put_task;
2128 down_read(&mm->mmap_sem);
2129 vma = find_exact_vma(mm, vm_start, vm_end);
2130 if (!vma)
2131 goto out_no_vma;
2133 if (vma->vm_file)
2134 result = proc_map_files_instantiate(dir, dentry, task,
2135 (void *)(unsigned long)vma->vm_file->f_mode);
2137 out_no_vma:
2138 up_read(&mm->mmap_sem);
2139 mmput(mm);
2140 out_put_task:
2141 put_task_struct(task);
2142 out:
2143 return ERR_PTR(result);
2146 static const struct inode_operations proc_map_files_inode_operations = {
2147 .lookup = proc_map_files_lookup,
2148 .permission = proc_fd_permission,
2149 .setattr = proc_setattr,
2152 static int
2153 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2155 struct vm_area_struct *vma;
2156 struct task_struct *task;
2157 struct mm_struct *mm;
2158 unsigned long nr_files, pos, i;
2159 struct flex_array *fa = NULL;
2160 struct map_files_info info;
2161 struct map_files_info *p;
2162 int ret;
2164 ret = -ENOENT;
2165 task = get_proc_task(file_inode(file));
2166 if (!task)
2167 goto out;
2169 ret = -EACCES;
2170 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2171 goto out_put_task;
2173 ret = 0;
2174 if (!dir_emit_dots(file, ctx))
2175 goto out_put_task;
2177 mm = get_task_mm(task);
2178 if (!mm)
2179 goto out_put_task;
2180 down_read(&mm->mmap_sem);
2182 nr_files = 0;
2185 * We need two passes here:
2187 * 1) Collect vmas of mapped files with mmap_sem taken
2188 * 2) Release mmap_sem and instantiate entries
2190 * otherwise we get lockdep complained, since filldir()
2191 * routine might require mmap_sem taken in might_fault().
2194 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2195 if (vma->vm_file && ++pos > ctx->pos)
2196 nr_files++;
2199 if (nr_files) {
2200 fa = flex_array_alloc(sizeof(info), nr_files,
2201 GFP_KERNEL);
2202 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2203 GFP_KERNEL)) {
2204 ret = -ENOMEM;
2205 if (fa)
2206 flex_array_free(fa);
2207 up_read(&mm->mmap_sem);
2208 mmput(mm);
2209 goto out_put_task;
2211 for (i = 0, vma = mm->mmap, pos = 2; vma;
2212 vma = vma->vm_next) {
2213 if (!vma->vm_file)
2214 continue;
2215 if (++pos <= ctx->pos)
2216 continue;
2218 info.mode = vma->vm_file->f_mode;
2219 info.len = snprintf(info.name,
2220 sizeof(info.name), "%lx-%lx",
2221 vma->vm_start, vma->vm_end);
2222 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2223 BUG();
2226 up_read(&mm->mmap_sem);
2228 for (i = 0; i < nr_files; i++) {
2229 p = flex_array_get(fa, i);
2230 if (!proc_fill_cache(file, ctx,
2231 p->name, p->len,
2232 proc_map_files_instantiate,
2233 task,
2234 (void *)(unsigned long)p->mode))
2235 break;
2236 ctx->pos++;
2238 if (fa)
2239 flex_array_free(fa);
2240 mmput(mm);
2242 out_put_task:
2243 put_task_struct(task);
2244 out:
2245 return ret;
2248 static const struct file_operations proc_map_files_operations = {
2249 .read = generic_read_dir,
2250 .iterate_shared = proc_map_files_readdir,
2251 .llseek = generic_file_llseek,
2254 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2255 struct timers_private {
2256 struct pid *pid;
2257 struct task_struct *task;
2258 struct sighand_struct *sighand;
2259 struct pid_namespace *ns;
2260 unsigned long flags;
2263 static void *timers_start(struct seq_file *m, loff_t *pos)
2265 struct timers_private *tp = m->private;
2267 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2268 if (!tp->task)
2269 return ERR_PTR(-ESRCH);
2271 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2272 if (!tp->sighand)
2273 return ERR_PTR(-ESRCH);
2275 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2278 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2280 struct timers_private *tp = m->private;
2281 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2284 static void timers_stop(struct seq_file *m, void *v)
2286 struct timers_private *tp = m->private;
2288 if (tp->sighand) {
2289 unlock_task_sighand(tp->task, &tp->flags);
2290 tp->sighand = NULL;
2293 if (tp->task) {
2294 put_task_struct(tp->task);
2295 tp->task = NULL;
2299 static int show_timer(struct seq_file *m, void *v)
2301 struct k_itimer *timer;
2302 struct timers_private *tp = m->private;
2303 int notify;
2304 static const char * const nstr[] = {
2305 [SIGEV_SIGNAL] = "signal",
2306 [SIGEV_NONE] = "none",
2307 [SIGEV_THREAD] = "thread",
2310 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2311 notify = timer->it_sigev_notify;
2313 seq_printf(m, "ID: %d\n", timer->it_id);
2314 seq_printf(m, "signal: %d/%p\n",
2315 timer->sigq->info.si_signo,
2316 timer->sigq->info.si_value.sival_ptr);
2317 seq_printf(m, "notify: %s/%s.%d\n",
2318 nstr[notify & ~SIGEV_THREAD_ID],
2319 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2320 pid_nr_ns(timer->it_pid, tp->ns));
2321 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2323 return 0;
2326 static const struct seq_operations proc_timers_seq_ops = {
2327 .start = timers_start,
2328 .next = timers_next,
2329 .stop = timers_stop,
2330 .show = show_timer,
2333 static int proc_timers_open(struct inode *inode, struct file *file)
2335 struct timers_private *tp;
2337 tp = __seq_open_private(file, &proc_timers_seq_ops,
2338 sizeof(struct timers_private));
2339 if (!tp)
2340 return -ENOMEM;
2342 tp->pid = proc_pid(inode);
2343 tp->ns = inode->i_sb->s_fs_info;
2344 return 0;
2347 static const struct file_operations proc_timers_operations = {
2348 .open = proc_timers_open,
2349 .read = seq_read,
2350 .llseek = seq_lseek,
2351 .release = seq_release_private,
2353 #endif
2355 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2356 size_t count, loff_t *offset)
2358 struct inode *inode = file_inode(file);
2359 struct task_struct *p;
2360 u64 slack_ns;
2361 int err;
2363 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2364 if (err < 0)
2365 return err;
2367 p = get_proc_task(inode);
2368 if (!p)
2369 return -ESRCH;
2371 if (p != current) {
2372 if (!capable(CAP_SYS_NICE)) {
2373 count = -EPERM;
2374 goto out;
2377 err = security_task_setscheduler(p);
2378 if (err) {
2379 count = err;
2380 goto out;
2384 task_lock(p);
2385 if (slack_ns == 0)
2386 p->timer_slack_ns = p->default_timer_slack_ns;
2387 else
2388 p->timer_slack_ns = slack_ns;
2389 task_unlock(p);
2391 out:
2392 put_task_struct(p);
2394 return count;
2397 static int timerslack_ns_show(struct seq_file *m, void *v)
2399 struct inode *inode = m->private;
2400 struct task_struct *p;
2401 int err = 0;
2403 p = get_proc_task(inode);
2404 if (!p)
2405 return -ESRCH;
2407 if (p != current) {
2409 if (!capable(CAP_SYS_NICE)) {
2410 err = -EPERM;
2411 goto out;
2413 err = security_task_getscheduler(p);
2414 if (err)
2415 goto out;
2418 task_lock(p);
2419 seq_printf(m, "%llu\n", p->timer_slack_ns);
2420 task_unlock(p);
2422 out:
2423 put_task_struct(p);
2425 return err;
2428 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2430 return single_open(filp, timerslack_ns_show, inode);
2433 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2434 .open = timerslack_ns_open,
2435 .read = seq_read,
2436 .write = timerslack_ns_write,
2437 .llseek = seq_lseek,
2438 .release = single_release,
2441 static int proc_pident_instantiate(struct inode *dir,
2442 struct dentry *dentry, struct task_struct *task, const void *ptr)
2444 const struct pid_entry *p = ptr;
2445 struct inode *inode;
2446 struct proc_inode *ei;
2448 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2449 if (!inode)
2450 goto out;
2452 ei = PROC_I(inode);
2453 if (S_ISDIR(inode->i_mode))
2454 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2455 if (p->iop)
2456 inode->i_op = p->iop;
2457 if (p->fop)
2458 inode->i_fop = p->fop;
2459 ei->op = p->op;
2460 d_set_d_op(dentry, &pid_dentry_operations);
2461 d_add(dentry, inode);
2462 /* Close the race of the process dying before we return the dentry */
2463 if (pid_revalidate(dentry, 0))
2464 return 0;
2465 out:
2466 return -ENOENT;
2469 static struct dentry *proc_pident_lookup(struct inode *dir,
2470 struct dentry *dentry,
2471 const struct pid_entry *ents,
2472 unsigned int nents)
2474 int error;
2475 struct task_struct *task = get_proc_task(dir);
2476 const struct pid_entry *p, *last;
2478 error = -ENOENT;
2480 if (!task)
2481 goto out_no_task;
2484 * Yes, it does not scale. And it should not. Don't add
2485 * new entries into /proc/<tgid>/ without very good reasons.
2487 last = &ents[nents];
2488 for (p = ents; p < last; p++) {
2489 if (p->len != dentry->d_name.len)
2490 continue;
2491 if (!memcmp(dentry->d_name.name, p->name, p->len))
2492 break;
2494 if (p >= last)
2495 goto out;
2497 error = proc_pident_instantiate(dir, dentry, task, p);
2498 out:
2499 put_task_struct(task);
2500 out_no_task:
2501 return ERR_PTR(error);
2504 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2505 const struct pid_entry *ents, unsigned int nents)
2507 struct task_struct *task = get_proc_task(file_inode(file));
2508 const struct pid_entry *p;
2510 if (!task)
2511 return -ENOENT;
2513 if (!dir_emit_dots(file, ctx))
2514 goto out;
2516 if (ctx->pos >= nents + 2)
2517 goto out;
2519 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2520 if (!proc_fill_cache(file, ctx, p->name, p->len,
2521 proc_pident_instantiate, task, p))
2522 break;
2523 ctx->pos++;
2525 out:
2526 put_task_struct(task);
2527 return 0;
2530 #ifdef CONFIG_SECURITY
2531 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2532 size_t count, loff_t *ppos)
2534 struct inode * inode = file_inode(file);
2535 char *p = NULL;
2536 ssize_t length;
2537 struct task_struct *task = get_proc_task(inode);
2539 if (!task)
2540 return -ESRCH;
2542 length = security_getprocattr(task,
2543 (char*)file->f_path.dentry->d_name.name,
2544 &p);
2545 put_task_struct(task);
2546 if (length > 0)
2547 length = simple_read_from_buffer(buf, count, ppos, p, length);
2548 kfree(p);
2549 return length;
2552 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2553 size_t count, loff_t *ppos)
2555 struct inode * inode = file_inode(file);
2556 void *page;
2557 ssize_t length;
2558 struct task_struct *task = get_proc_task(inode);
2560 length = -ESRCH;
2561 if (!task)
2562 goto out_no_task;
2564 /* A task may only write its own attributes. */
2565 length = -EACCES;
2566 if (current != task)
2567 goto out;
2569 if (count > PAGE_SIZE)
2570 count = PAGE_SIZE;
2572 /* No partial writes. */
2573 length = -EINVAL;
2574 if (*ppos != 0)
2575 goto out;
2577 page = memdup_user(buf, count);
2578 if (IS_ERR(page)) {
2579 length = PTR_ERR(page);
2580 goto out;
2583 /* Guard against adverse ptrace interaction */
2584 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2585 if (length < 0)
2586 goto out_free;
2588 length = security_setprocattr(file->f_path.dentry->d_name.name,
2589 page, count);
2590 mutex_unlock(&current->signal->cred_guard_mutex);
2591 out_free:
2592 kfree(page);
2593 out:
2594 put_task_struct(task);
2595 out_no_task:
2596 return length;
2599 static const struct file_operations proc_pid_attr_operations = {
2600 .read = proc_pid_attr_read,
2601 .write = proc_pid_attr_write,
2602 .llseek = generic_file_llseek,
2605 static const struct pid_entry attr_dir_stuff[] = {
2606 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2607 REG("prev", S_IRUGO, proc_pid_attr_operations),
2608 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2609 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2610 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2611 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2614 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2616 return proc_pident_readdir(file, ctx,
2617 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2620 static const struct file_operations proc_attr_dir_operations = {
2621 .read = generic_read_dir,
2622 .iterate_shared = proc_attr_dir_readdir,
2623 .llseek = generic_file_llseek,
2626 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2627 struct dentry *dentry, unsigned int flags)
2629 return proc_pident_lookup(dir, dentry,
2630 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2633 static const struct inode_operations proc_attr_dir_inode_operations = {
2634 .lookup = proc_attr_dir_lookup,
2635 .getattr = pid_getattr,
2636 .setattr = proc_setattr,
2639 #endif
2641 #ifdef CONFIG_ELF_CORE
2642 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2643 size_t count, loff_t *ppos)
2645 struct task_struct *task = get_proc_task(file_inode(file));
2646 struct mm_struct *mm;
2647 char buffer[PROC_NUMBUF];
2648 size_t len;
2649 int ret;
2651 if (!task)
2652 return -ESRCH;
2654 ret = 0;
2655 mm = get_task_mm(task);
2656 if (mm) {
2657 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2658 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2659 MMF_DUMP_FILTER_SHIFT));
2660 mmput(mm);
2661 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2664 put_task_struct(task);
2666 return ret;
2669 static ssize_t proc_coredump_filter_write(struct file *file,
2670 const char __user *buf,
2671 size_t count,
2672 loff_t *ppos)
2674 struct task_struct *task;
2675 struct mm_struct *mm;
2676 unsigned int val;
2677 int ret;
2678 int i;
2679 unsigned long mask;
2681 ret = kstrtouint_from_user(buf, count, 0, &val);
2682 if (ret < 0)
2683 return ret;
2685 ret = -ESRCH;
2686 task = get_proc_task(file_inode(file));
2687 if (!task)
2688 goto out_no_task;
2690 mm = get_task_mm(task);
2691 if (!mm)
2692 goto out_no_mm;
2693 ret = 0;
2695 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2696 if (val & mask)
2697 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2698 else
2699 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2702 mmput(mm);
2703 out_no_mm:
2704 put_task_struct(task);
2705 out_no_task:
2706 if (ret < 0)
2707 return ret;
2708 return count;
2711 static const struct file_operations proc_coredump_filter_operations = {
2712 .read = proc_coredump_filter_read,
2713 .write = proc_coredump_filter_write,
2714 .llseek = generic_file_llseek,
2716 #endif
2718 #ifdef CONFIG_TASK_IO_ACCOUNTING
2719 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2721 struct task_io_accounting acct = task->ioac;
2722 unsigned long flags;
2723 int result;
2725 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2726 if (result)
2727 return result;
2729 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2730 result = -EACCES;
2731 goto out_unlock;
2734 if (whole && lock_task_sighand(task, &flags)) {
2735 struct task_struct *t = task;
2737 task_io_accounting_add(&acct, &task->signal->ioac);
2738 while_each_thread(task, t)
2739 task_io_accounting_add(&acct, &t->ioac);
2741 unlock_task_sighand(task, &flags);
2743 seq_printf(m,
2744 "rchar: %llu\n"
2745 "wchar: %llu\n"
2746 "syscr: %llu\n"
2747 "syscw: %llu\n"
2748 "read_bytes: %llu\n"
2749 "write_bytes: %llu\n"
2750 "cancelled_write_bytes: %llu\n",
2751 (unsigned long long)acct.rchar,
2752 (unsigned long long)acct.wchar,
2753 (unsigned long long)acct.syscr,
2754 (unsigned long long)acct.syscw,
2755 (unsigned long long)acct.read_bytes,
2756 (unsigned long long)acct.write_bytes,
2757 (unsigned long long)acct.cancelled_write_bytes);
2758 result = 0;
2760 out_unlock:
2761 mutex_unlock(&task->signal->cred_guard_mutex);
2762 return result;
2765 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2766 struct pid *pid, struct task_struct *task)
2768 return do_io_accounting(task, m, 0);
2771 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2772 struct pid *pid, struct task_struct *task)
2774 return do_io_accounting(task, m, 1);
2776 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2778 #ifdef CONFIG_USER_NS
2779 static int proc_id_map_open(struct inode *inode, struct file *file,
2780 const struct seq_operations *seq_ops)
2782 struct user_namespace *ns = NULL;
2783 struct task_struct *task;
2784 struct seq_file *seq;
2785 int ret = -EINVAL;
2787 task = get_proc_task(inode);
2788 if (task) {
2789 rcu_read_lock();
2790 ns = get_user_ns(task_cred_xxx(task, user_ns));
2791 rcu_read_unlock();
2792 put_task_struct(task);
2794 if (!ns)
2795 goto err;
2797 ret = seq_open(file, seq_ops);
2798 if (ret)
2799 goto err_put_ns;
2801 seq = file->private_data;
2802 seq->private = ns;
2804 return 0;
2805 err_put_ns:
2806 put_user_ns(ns);
2807 err:
2808 return ret;
2811 static int proc_id_map_release(struct inode *inode, struct file *file)
2813 struct seq_file *seq = file->private_data;
2814 struct user_namespace *ns = seq->private;
2815 put_user_ns(ns);
2816 return seq_release(inode, file);
2819 static int proc_uid_map_open(struct inode *inode, struct file *file)
2821 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2824 static int proc_gid_map_open(struct inode *inode, struct file *file)
2826 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2829 static int proc_projid_map_open(struct inode *inode, struct file *file)
2831 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2834 static const struct file_operations proc_uid_map_operations = {
2835 .open = proc_uid_map_open,
2836 .write = proc_uid_map_write,
2837 .read = seq_read,
2838 .llseek = seq_lseek,
2839 .release = proc_id_map_release,
2842 static const struct file_operations proc_gid_map_operations = {
2843 .open = proc_gid_map_open,
2844 .write = proc_gid_map_write,
2845 .read = seq_read,
2846 .llseek = seq_lseek,
2847 .release = proc_id_map_release,
2850 static const struct file_operations proc_projid_map_operations = {
2851 .open = proc_projid_map_open,
2852 .write = proc_projid_map_write,
2853 .read = seq_read,
2854 .llseek = seq_lseek,
2855 .release = proc_id_map_release,
2858 static int proc_setgroups_open(struct inode *inode, struct file *file)
2860 struct user_namespace *ns = NULL;
2861 struct task_struct *task;
2862 int ret;
2864 ret = -ESRCH;
2865 task = get_proc_task(inode);
2866 if (task) {
2867 rcu_read_lock();
2868 ns = get_user_ns(task_cred_xxx(task, user_ns));
2869 rcu_read_unlock();
2870 put_task_struct(task);
2872 if (!ns)
2873 goto err;
2875 if (file->f_mode & FMODE_WRITE) {
2876 ret = -EACCES;
2877 if (!ns_capable(ns, CAP_SYS_ADMIN))
2878 goto err_put_ns;
2881 ret = single_open(file, &proc_setgroups_show, ns);
2882 if (ret)
2883 goto err_put_ns;
2885 return 0;
2886 err_put_ns:
2887 put_user_ns(ns);
2888 err:
2889 return ret;
2892 static int proc_setgroups_release(struct inode *inode, struct file *file)
2894 struct seq_file *seq = file->private_data;
2895 struct user_namespace *ns = seq->private;
2896 int ret = single_release(inode, file);
2897 put_user_ns(ns);
2898 return ret;
2901 static const struct file_operations proc_setgroups_operations = {
2902 .open = proc_setgroups_open,
2903 .write = proc_setgroups_write,
2904 .read = seq_read,
2905 .llseek = seq_lseek,
2906 .release = proc_setgroups_release,
2908 #endif /* CONFIG_USER_NS */
2910 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2911 struct pid *pid, struct task_struct *task)
2913 int err = lock_trace(task);
2914 if (!err) {
2915 seq_printf(m, "%08x\n", task->personality);
2916 unlock_trace(task);
2918 return err;
2921 #ifdef CONFIG_LIVEPATCH
2922 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2923 struct pid *pid, struct task_struct *task)
2925 seq_printf(m, "%d\n", task->patch_state);
2926 return 0;
2928 #endif /* CONFIG_LIVEPATCH */
2931 * Thread groups
2933 static const struct file_operations proc_task_operations;
2934 static const struct inode_operations proc_task_inode_operations;
2936 static const struct pid_entry tgid_base_stuff[] = {
2937 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2938 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2939 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2940 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2941 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2942 #ifdef CONFIG_NET
2943 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2944 #endif
2945 REG("environ", S_IRUSR, proc_environ_operations),
2946 REG("auxv", S_IRUSR, proc_auxv_operations),
2947 ONE("status", S_IRUGO, proc_pid_status),
2948 ONE("personality", S_IRUSR, proc_pid_personality),
2949 ONE("limits", S_IRUGO, proc_pid_limits),
2950 #ifdef CONFIG_SCHED_DEBUG
2951 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2952 #endif
2953 #ifdef CONFIG_SCHED_AUTOGROUP
2954 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2955 #endif
2956 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2957 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2958 ONE("syscall", S_IRUSR, proc_pid_syscall),
2959 #endif
2960 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2961 ONE("stat", S_IRUGO, proc_tgid_stat),
2962 ONE("statm", S_IRUGO, proc_pid_statm),
2963 REG("maps", S_IRUGO, proc_pid_maps_operations),
2964 #ifdef CONFIG_NUMA
2965 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2966 #endif
2967 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2968 LNK("cwd", proc_cwd_link),
2969 LNK("root", proc_root_link),
2970 LNK("exe", proc_exe_link),
2971 REG("mounts", S_IRUGO, proc_mounts_operations),
2972 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2973 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2974 #ifdef CONFIG_PROC_PAGE_MONITOR
2975 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2976 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2977 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
2978 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2979 #endif
2980 #ifdef CONFIG_SECURITY
2981 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2982 #endif
2983 #ifdef CONFIG_KALLSYMS
2984 ONE("wchan", S_IRUGO, proc_pid_wchan),
2985 #endif
2986 #ifdef CONFIG_STACKTRACE
2987 ONE("stack", S_IRUSR, proc_pid_stack),
2988 #endif
2989 #ifdef CONFIG_SCHED_INFO
2990 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2991 #endif
2992 #ifdef CONFIG_LATENCYTOP
2993 REG("latency", S_IRUGO, proc_lstats_operations),
2994 #endif
2995 #ifdef CONFIG_PROC_PID_CPUSET
2996 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2997 #endif
2998 #ifdef CONFIG_CGROUPS
2999 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3000 #endif
3001 ONE("oom_score", S_IRUGO, proc_oom_score),
3002 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3003 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3004 #ifdef CONFIG_AUDITSYSCALL
3005 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3006 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3007 #endif
3008 #ifdef CONFIG_FAULT_INJECTION
3009 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3010 REG("fail-nth", 0644, proc_fail_nth_operations),
3011 #endif
3012 #ifdef CONFIG_ELF_CORE
3013 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3014 #endif
3015 #ifdef CONFIG_TASK_IO_ACCOUNTING
3016 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3017 #endif
3018 #ifdef CONFIG_HARDWALL
3019 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3020 #endif
3021 #ifdef CONFIG_USER_NS
3022 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3023 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3024 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3025 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3026 #endif
3027 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3028 REG("timers", S_IRUGO, proc_timers_operations),
3029 #endif
3030 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3031 #ifdef CONFIG_LIVEPATCH
3032 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3033 #endif
3036 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3038 return proc_pident_readdir(file, ctx,
3039 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3042 static const struct file_operations proc_tgid_base_operations = {
3043 .read = generic_read_dir,
3044 .iterate_shared = proc_tgid_base_readdir,
3045 .llseek = generic_file_llseek,
3048 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3050 return proc_pident_lookup(dir, dentry,
3051 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3054 static const struct inode_operations proc_tgid_base_inode_operations = {
3055 .lookup = proc_tgid_base_lookup,
3056 .getattr = pid_getattr,
3057 .setattr = proc_setattr,
3058 .permission = proc_pid_permission,
3061 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3063 struct dentry *dentry, *leader, *dir;
3064 char buf[PROC_NUMBUF];
3065 struct qstr name;
3067 name.name = buf;
3068 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3069 /* no ->d_hash() rejects on procfs */
3070 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3071 if (dentry) {
3072 d_invalidate(dentry);
3073 dput(dentry);
3076 if (pid == tgid)
3077 return;
3079 name.name = buf;
3080 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3081 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3082 if (!leader)
3083 goto out;
3085 name.name = "task";
3086 name.len = strlen(name.name);
3087 dir = d_hash_and_lookup(leader, &name);
3088 if (!dir)
3089 goto out_put_leader;
3091 name.name = buf;
3092 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3093 dentry = d_hash_and_lookup(dir, &name);
3094 if (dentry) {
3095 d_invalidate(dentry);
3096 dput(dentry);
3099 dput(dir);
3100 out_put_leader:
3101 dput(leader);
3102 out:
3103 return;
3107 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3108 * @task: task that should be flushed.
3110 * When flushing dentries from proc, one needs to flush them from global
3111 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3112 * in. This call is supposed to do all of this job.
3114 * Looks in the dcache for
3115 * /proc/@pid
3116 * /proc/@tgid/task/@pid
3117 * if either directory is present flushes it and all of it'ts children
3118 * from the dcache.
3120 * It is safe and reasonable to cache /proc entries for a task until
3121 * that task exits. After that they just clog up the dcache with
3122 * useless entries, possibly causing useful dcache entries to be
3123 * flushed instead. This routine is proved to flush those useless
3124 * dcache entries at process exit time.
3126 * NOTE: This routine is just an optimization so it does not guarantee
3127 * that no dcache entries will exist at process exit time it
3128 * just makes it very unlikely that any will persist.
3131 void proc_flush_task(struct task_struct *task)
3133 int i;
3134 struct pid *pid, *tgid;
3135 struct upid *upid;
3137 pid = task_pid(task);
3138 tgid = task_tgid(task);
3140 for (i = 0; i <= pid->level; i++) {
3141 upid = &pid->numbers[i];
3142 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3143 tgid->numbers[i].nr);
3147 static int proc_pid_instantiate(struct inode *dir,
3148 struct dentry * dentry,
3149 struct task_struct *task, const void *ptr)
3151 struct inode *inode;
3153 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3154 if (!inode)
3155 goto out;
3157 inode->i_op = &proc_tgid_base_inode_operations;
3158 inode->i_fop = &proc_tgid_base_operations;
3159 inode->i_flags|=S_IMMUTABLE;
3161 set_nlink(inode, nlink_tgid);
3163 d_set_d_op(dentry, &pid_dentry_operations);
3165 d_add(dentry, inode);
3166 /* Close the race of the process dying before we return the dentry */
3167 if (pid_revalidate(dentry, 0))
3168 return 0;
3169 out:
3170 return -ENOENT;
3173 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3175 int result = -ENOENT;
3176 struct task_struct *task;
3177 unsigned tgid;
3178 struct pid_namespace *ns;
3180 tgid = name_to_int(&dentry->d_name);
3181 if (tgid == ~0U)
3182 goto out;
3184 ns = dentry->d_sb->s_fs_info;
3185 rcu_read_lock();
3186 task = find_task_by_pid_ns(tgid, ns);
3187 if (task)
3188 get_task_struct(task);
3189 rcu_read_unlock();
3190 if (!task)
3191 goto out;
3193 result = proc_pid_instantiate(dir, dentry, task, NULL);
3194 put_task_struct(task);
3195 out:
3196 return ERR_PTR(result);
3200 * Find the first task with tgid >= tgid
3203 struct tgid_iter {
3204 unsigned int tgid;
3205 struct task_struct *task;
3207 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3209 struct pid *pid;
3211 if (iter.task)
3212 put_task_struct(iter.task);
3213 rcu_read_lock();
3214 retry:
3215 iter.task = NULL;
3216 pid = find_ge_pid(iter.tgid, ns);
3217 if (pid) {
3218 iter.tgid = pid_nr_ns(pid, ns);
3219 iter.task = pid_task(pid, PIDTYPE_PID);
3220 /* What we to know is if the pid we have find is the
3221 * pid of a thread_group_leader. Testing for task
3222 * being a thread_group_leader is the obvious thing
3223 * todo but there is a window when it fails, due to
3224 * the pid transfer logic in de_thread.
3226 * So we perform the straight forward test of seeing
3227 * if the pid we have found is the pid of a thread
3228 * group leader, and don't worry if the task we have
3229 * found doesn't happen to be a thread group leader.
3230 * As we don't care in the case of readdir.
3232 if (!iter.task || !has_group_leader_pid(iter.task)) {
3233 iter.tgid += 1;
3234 goto retry;
3236 get_task_struct(iter.task);
3238 rcu_read_unlock();
3239 return iter;
3242 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3244 /* for the /proc/ directory itself, after non-process stuff has been done */
3245 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3247 struct tgid_iter iter;
3248 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3249 loff_t pos = ctx->pos;
3251 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3252 return 0;
3254 if (pos == TGID_OFFSET - 2) {
3255 struct inode *inode = d_inode(ns->proc_self);
3256 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3257 return 0;
3258 ctx->pos = pos = pos + 1;
3260 if (pos == TGID_OFFSET - 1) {
3261 struct inode *inode = d_inode(ns->proc_thread_self);
3262 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3263 return 0;
3264 ctx->pos = pos = pos + 1;
3266 iter.tgid = pos - TGID_OFFSET;
3267 iter.task = NULL;
3268 for (iter = next_tgid(ns, iter);
3269 iter.task;
3270 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3271 char name[PROC_NUMBUF];
3272 int len;
3274 cond_resched();
3275 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
3276 continue;
3278 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3279 ctx->pos = iter.tgid + TGID_OFFSET;
3280 if (!proc_fill_cache(file, ctx, name, len,
3281 proc_pid_instantiate, iter.task, NULL)) {
3282 put_task_struct(iter.task);
3283 return 0;
3286 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3287 return 0;
3291 * proc_tid_comm_permission is a special permission function exclusively
3292 * used for the node /proc/<pid>/task/<tid>/comm.
3293 * It bypasses generic permission checks in the case where a task of the same
3294 * task group attempts to access the node.
3295 * The rationale behind this is that glibc and bionic access this node for
3296 * cross thread naming (pthread_set/getname_np(!self)). However, if
3297 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3298 * which locks out the cross thread naming implementation.
3299 * This function makes sure that the node is always accessible for members of
3300 * same thread group.
3302 static int proc_tid_comm_permission(struct inode *inode, int mask)
3304 bool is_same_tgroup;
3305 struct task_struct *task;
3307 task = get_proc_task(inode);
3308 if (!task)
3309 return -ESRCH;
3310 is_same_tgroup = same_thread_group(current, task);
3311 put_task_struct(task);
3313 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3314 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3315 * read or written by the members of the corresponding
3316 * thread group.
3318 return 0;
3321 return generic_permission(inode, mask);
3324 static const struct inode_operations proc_tid_comm_inode_operations = {
3325 .permission = proc_tid_comm_permission,
3329 * Tasks
3331 static const struct pid_entry tid_base_stuff[] = {
3332 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3333 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3334 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3335 #ifdef CONFIG_NET
3336 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3337 #endif
3338 REG("environ", S_IRUSR, proc_environ_operations),
3339 REG("auxv", S_IRUSR, proc_auxv_operations),
3340 ONE("status", S_IRUGO, proc_pid_status),
3341 ONE("personality", S_IRUSR, proc_pid_personality),
3342 ONE("limits", S_IRUGO, proc_pid_limits),
3343 #ifdef CONFIG_SCHED_DEBUG
3344 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3345 #endif
3346 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3347 &proc_tid_comm_inode_operations,
3348 &proc_pid_set_comm_operations, {}),
3349 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3350 ONE("syscall", S_IRUSR, proc_pid_syscall),
3351 #endif
3352 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3353 ONE("stat", S_IRUGO, proc_tid_stat),
3354 ONE("statm", S_IRUGO, proc_pid_statm),
3355 REG("maps", S_IRUGO, proc_tid_maps_operations),
3356 #ifdef CONFIG_PROC_CHILDREN
3357 REG("children", S_IRUGO, proc_tid_children_operations),
3358 #endif
3359 #ifdef CONFIG_NUMA
3360 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3361 #endif
3362 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3363 LNK("cwd", proc_cwd_link),
3364 LNK("root", proc_root_link),
3365 LNK("exe", proc_exe_link),
3366 REG("mounts", S_IRUGO, proc_mounts_operations),
3367 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3368 #ifdef CONFIG_PROC_PAGE_MONITOR
3369 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3370 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3371 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3372 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3373 #endif
3374 #ifdef CONFIG_SECURITY
3375 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3376 #endif
3377 #ifdef CONFIG_KALLSYMS
3378 ONE("wchan", S_IRUGO, proc_pid_wchan),
3379 #endif
3380 #ifdef CONFIG_STACKTRACE
3381 ONE("stack", S_IRUSR, proc_pid_stack),
3382 #endif
3383 #ifdef CONFIG_SCHED_INFO
3384 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3385 #endif
3386 #ifdef CONFIG_LATENCYTOP
3387 REG("latency", S_IRUGO, proc_lstats_operations),
3388 #endif
3389 #ifdef CONFIG_PROC_PID_CPUSET
3390 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3391 #endif
3392 #ifdef CONFIG_CGROUPS
3393 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3394 #endif
3395 ONE("oom_score", S_IRUGO, proc_oom_score),
3396 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3397 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3398 #ifdef CONFIG_AUDITSYSCALL
3399 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3400 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3401 #endif
3402 #ifdef CONFIG_FAULT_INJECTION
3403 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3404 REG("fail-nth", 0644, proc_fail_nth_operations),
3405 #endif
3406 #ifdef CONFIG_TASK_IO_ACCOUNTING
3407 ONE("io", S_IRUSR, proc_tid_io_accounting),
3408 #endif
3409 #ifdef CONFIG_HARDWALL
3410 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3411 #endif
3412 #ifdef CONFIG_USER_NS
3413 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3414 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3415 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3416 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3417 #endif
3418 #ifdef CONFIG_LIVEPATCH
3419 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3420 #endif
3423 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3425 return proc_pident_readdir(file, ctx,
3426 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3429 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3431 return proc_pident_lookup(dir, dentry,
3432 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3435 static const struct file_operations proc_tid_base_operations = {
3436 .read = generic_read_dir,
3437 .iterate_shared = proc_tid_base_readdir,
3438 .llseek = generic_file_llseek,
3441 static const struct inode_operations proc_tid_base_inode_operations = {
3442 .lookup = proc_tid_base_lookup,
3443 .getattr = pid_getattr,
3444 .setattr = proc_setattr,
3447 static int proc_task_instantiate(struct inode *dir,
3448 struct dentry *dentry, struct task_struct *task, const void *ptr)
3450 struct inode *inode;
3451 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3453 if (!inode)
3454 goto out;
3455 inode->i_op = &proc_tid_base_inode_operations;
3456 inode->i_fop = &proc_tid_base_operations;
3457 inode->i_flags|=S_IMMUTABLE;
3459 set_nlink(inode, nlink_tid);
3461 d_set_d_op(dentry, &pid_dentry_operations);
3463 d_add(dentry, inode);
3464 /* Close the race of the process dying before we return the dentry */
3465 if (pid_revalidate(dentry, 0))
3466 return 0;
3467 out:
3468 return -ENOENT;
3471 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3473 int result = -ENOENT;
3474 struct task_struct *task;
3475 struct task_struct *leader = get_proc_task(dir);
3476 unsigned tid;
3477 struct pid_namespace *ns;
3479 if (!leader)
3480 goto out_no_task;
3482 tid = name_to_int(&dentry->d_name);
3483 if (tid == ~0U)
3484 goto out;
3486 ns = dentry->d_sb->s_fs_info;
3487 rcu_read_lock();
3488 task = find_task_by_pid_ns(tid, ns);
3489 if (task)
3490 get_task_struct(task);
3491 rcu_read_unlock();
3492 if (!task)
3493 goto out;
3494 if (!same_thread_group(leader, task))
3495 goto out_drop_task;
3497 result = proc_task_instantiate(dir, dentry, task, NULL);
3498 out_drop_task:
3499 put_task_struct(task);
3500 out:
3501 put_task_struct(leader);
3502 out_no_task:
3503 return ERR_PTR(result);
3507 * Find the first tid of a thread group to return to user space.
3509 * Usually this is just the thread group leader, but if the users
3510 * buffer was too small or there was a seek into the middle of the
3511 * directory we have more work todo.
3513 * In the case of a short read we start with find_task_by_pid.
3515 * In the case of a seek we start with the leader and walk nr
3516 * threads past it.
3518 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3519 struct pid_namespace *ns)
3521 struct task_struct *pos, *task;
3522 unsigned long nr = f_pos;
3524 if (nr != f_pos) /* 32bit overflow? */
3525 return NULL;
3527 rcu_read_lock();
3528 task = pid_task(pid, PIDTYPE_PID);
3529 if (!task)
3530 goto fail;
3532 /* Attempt to start with the tid of a thread */
3533 if (tid && nr) {
3534 pos = find_task_by_pid_ns(tid, ns);
3535 if (pos && same_thread_group(pos, task))
3536 goto found;
3539 /* If nr exceeds the number of threads there is nothing todo */
3540 if (nr >= get_nr_threads(task))
3541 goto fail;
3543 /* If we haven't found our starting place yet start
3544 * with the leader and walk nr threads forward.
3546 pos = task = task->group_leader;
3547 do {
3548 if (!nr--)
3549 goto found;
3550 } while_each_thread(task, pos);
3551 fail:
3552 pos = NULL;
3553 goto out;
3554 found:
3555 get_task_struct(pos);
3556 out:
3557 rcu_read_unlock();
3558 return pos;
3562 * Find the next thread in the thread list.
3563 * Return NULL if there is an error or no next thread.
3565 * The reference to the input task_struct is released.
3567 static struct task_struct *next_tid(struct task_struct *start)
3569 struct task_struct *pos = NULL;
3570 rcu_read_lock();
3571 if (pid_alive(start)) {
3572 pos = next_thread(start);
3573 if (thread_group_leader(pos))
3574 pos = NULL;
3575 else
3576 get_task_struct(pos);
3578 rcu_read_unlock();
3579 put_task_struct(start);
3580 return pos;
3583 /* for the /proc/TGID/task/ directories */
3584 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3586 struct inode *inode = file_inode(file);
3587 struct task_struct *task;
3588 struct pid_namespace *ns;
3589 int tid;
3591 if (proc_inode_is_dead(inode))
3592 return -ENOENT;
3594 if (!dir_emit_dots(file, ctx))
3595 return 0;
3597 /* f_version caches the tgid value that the last readdir call couldn't
3598 * return. lseek aka telldir automagically resets f_version to 0.
3600 ns = inode->i_sb->s_fs_info;
3601 tid = (int)file->f_version;
3602 file->f_version = 0;
3603 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3604 task;
3605 task = next_tid(task), ctx->pos++) {
3606 char name[PROC_NUMBUF];
3607 int len;
3608 tid = task_pid_nr_ns(task, ns);
3609 len = snprintf(name, sizeof(name), "%d", tid);
3610 if (!proc_fill_cache(file, ctx, name, len,
3611 proc_task_instantiate, task, NULL)) {
3612 /* returning this tgid failed, save it as the first
3613 * pid for the next readir call */
3614 file->f_version = (u64)tid;
3615 put_task_struct(task);
3616 break;
3620 return 0;
3623 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3624 u32 request_mask, unsigned int query_flags)
3626 struct inode *inode = d_inode(path->dentry);
3627 struct task_struct *p = get_proc_task(inode);
3628 generic_fillattr(inode, stat);
3630 if (p) {
3631 stat->nlink += get_nr_threads(p);
3632 put_task_struct(p);
3635 return 0;
3638 static const struct inode_operations proc_task_inode_operations = {
3639 .lookup = proc_task_lookup,
3640 .getattr = proc_task_getattr,
3641 .setattr = proc_setattr,
3642 .permission = proc_pid_permission,
3645 static const struct file_operations proc_task_operations = {
3646 .read = generic_read_dir,
3647 .iterate_shared = proc_task_readdir,
3648 .llseek = generic_file_llseek,
3651 void __init set_proc_pid_nlink(void)
3653 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3654 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));