]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/proc/base.c
KVM: arm64: vgic-v3: Add ICV_IAR1_EL1 handler
[mirror_ubuntu-zesty-kernel.git] / fs / proc / base.c
1 /*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
14 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
50 #include <linux/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
65 #include <linux/mm.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
92 #endif
93 #include <trace/events/oom.h>
94 #include "internal.h"
95 #include "fd.h"
96
97 /* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
107 static u8 nlink_tid;
108 static u8 nlink_tgid;
109
110 struct pid_entry {
111 const char *name;
112 unsigned int len;
113 umode_t mode;
114 const struct inode_operations *iop;
115 const struct file_operations *fop;
116 union proc_op op;
117 };
118
119 #define NOD(NAME, MODE, IOP, FOP, OP) { \
120 .name = (NAME), \
121 .len = sizeof(NAME) - 1, \
122 .mode = MODE, \
123 .iop = IOP, \
124 .fop = FOP, \
125 .op = OP, \
126 }
127
128 #define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130 #define LNK(NAME, get_link) \
131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
132 &proc_pid_link_inode_operations, NULL, \
133 { .proc_get_link = get_link } )
134 #define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
136 #define ONE(NAME, MODE, show) \
137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
139 { .proc_show = show } )
140
141 /*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
145 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
146 unsigned int n)
147 {
148 unsigned int i;
149 unsigned int count;
150
151 count = 2;
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158 }
159
160 static int get_task_root(struct task_struct *task, struct path *root)
161 {
162 int result = -ENOENT;
163
164 task_lock(task);
165 if (task->fs) {
166 get_fs_root(task->fs, root);
167 result = 0;
168 }
169 task_unlock(task);
170 return result;
171 }
172
173 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 {
175 struct task_struct *task = get_proc_task(d_inode(dentry));
176 int result = -ENOENT;
177
178 if (task) {
179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
185 put_task_struct(task);
186 }
187 return result;
188 }
189
190 static int proc_root_link(struct dentry *dentry, struct path *path)
191 {
192 struct task_struct *task = get_proc_task(d_inode(dentry));
193 int result = -ENOENT;
194
195 if (task) {
196 result = get_task_root(task, path);
197 put_task_struct(task);
198 }
199 return result;
200 }
201
202 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
203 size_t _count, loff_t *pos)
204 {
205 struct task_struct *tsk;
206 struct mm_struct *mm;
207 char *page;
208 unsigned long count = _count;
209 unsigned long arg_start, arg_end, env_start, env_end;
210 unsigned long len1, len2, len;
211 unsigned long p;
212 char c;
213 ssize_t rv;
214
215 BUG_ON(*pos < 0);
216
217 tsk = get_proc_task(file_inode(file));
218 if (!tsk)
219 return -ESRCH;
220 mm = get_task_mm(tsk);
221 put_task_struct(tsk);
222 if (!mm)
223 return 0;
224 /* Check if process spawned far enough to have cmdline. */
225 if (!mm->env_end) {
226 rv = 0;
227 goto out_mmput;
228 }
229
230 page = (char *)__get_free_page(GFP_TEMPORARY);
231 if (!page) {
232 rv = -ENOMEM;
233 goto out_mmput;
234 }
235
236 down_read(&mm->mmap_sem);
237 arg_start = mm->arg_start;
238 arg_end = mm->arg_end;
239 env_start = mm->env_start;
240 env_end = mm->env_end;
241 up_read(&mm->mmap_sem);
242
243 BUG_ON(arg_start > arg_end);
244 BUG_ON(env_start > env_end);
245
246 len1 = arg_end - arg_start;
247 len2 = env_end - env_start;
248
249 /* Empty ARGV. */
250 if (len1 == 0) {
251 rv = 0;
252 goto out_free_page;
253 }
254 /*
255 * Inherently racy -- command line shares address space
256 * with code and data.
257 */
258 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
259 if (rv <= 0)
260 goto out_free_page;
261
262 rv = 0;
263
264 if (c == '\0') {
265 /* Command line (set of strings) occupies whole ARGV. */
266 if (len1 <= *pos)
267 goto out_free_page;
268
269 p = arg_start + *pos;
270 len = len1 - *pos;
271 while (count > 0 && len > 0) {
272 unsigned int _count;
273 int nr_read;
274
275 _count = min3(count, len, PAGE_SIZE);
276 nr_read = access_remote_vm(mm, p, page, _count, 0);
277 if (nr_read < 0)
278 rv = nr_read;
279 if (nr_read <= 0)
280 goto out_free_page;
281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
291 rv += nr_read;
292 }
293 } else {
294 /*
295 * Command line (1 string) occupies ARGV and maybe
296 * extends into ENVP.
297 */
298 if (len1 + len2 <= *pos)
299 goto skip_argv_envp;
300 if (len1 <= *pos)
301 goto skip_argv;
302
303 p = arg_start + *pos;
304 len = len1 - *pos;
305 while (count > 0 && len > 0) {
306 unsigned int _count, l;
307 int nr_read;
308 bool final;
309
310 _count = min3(count, len, PAGE_SIZE);
311 nr_read = access_remote_vm(mm, p, page, _count, 0);
312 if (nr_read < 0)
313 rv = nr_read;
314 if (nr_read <= 0)
315 goto out_free_page;
316
317 /*
318 * Command line can be shorter than whole ARGV
319 * even if last "marker" byte says it is not.
320 */
321 final = false;
322 l = strnlen(page, nr_read);
323 if (l < nr_read) {
324 nr_read = l;
325 final = true;
326 }
327
328 if (copy_to_user(buf, page, nr_read)) {
329 rv = -EFAULT;
330 goto out_free_page;
331 }
332
333 p += nr_read;
334 len -= nr_read;
335 buf += nr_read;
336 count -= nr_read;
337 rv += nr_read;
338
339 if (final)
340 goto out_free_page;
341 }
342 skip_argv:
343 /*
344 * Command line (1 string) occupies ARGV and
345 * extends into ENVP.
346 */
347 if (len1 <= *pos) {
348 p = env_start + *pos - len1;
349 len = len1 + len2 - *pos;
350 } else {
351 p = env_start;
352 len = len2;
353 }
354 while (count > 0 && len > 0) {
355 unsigned int _count, l;
356 int nr_read;
357 bool final;
358
359 _count = min3(count, len, PAGE_SIZE);
360 nr_read = access_remote_vm(mm, p, page, _count, 0);
361 if (nr_read < 0)
362 rv = nr_read;
363 if (nr_read <= 0)
364 goto out_free_page;
365
366 /* Find EOS. */
367 final = false;
368 l = strnlen(page, nr_read);
369 if (l < nr_read) {
370 nr_read = l;
371 final = true;
372 }
373
374 if (copy_to_user(buf, page, nr_read)) {
375 rv = -EFAULT;
376 goto out_free_page;
377 }
378
379 p += nr_read;
380 len -= nr_read;
381 buf += nr_read;
382 count -= nr_read;
383 rv += nr_read;
384
385 if (final)
386 goto out_free_page;
387 }
388 skip_argv_envp:
389 ;
390 }
391
392 out_free_page:
393 free_page((unsigned long)page);
394 out_mmput:
395 mmput(mm);
396 if (rv > 0)
397 *pos += rv;
398 return rv;
399 }
400
401 static const struct file_operations proc_pid_cmdline_ops = {
402 .read = proc_pid_cmdline_read,
403 .llseek = generic_file_llseek,
404 };
405
406 #ifdef CONFIG_KALLSYMS
407 /*
408 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
409 * Returns the resolved symbol. If that fails, simply return the address.
410 */
411 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
412 struct pid *pid, struct task_struct *task)
413 {
414 unsigned long wchan;
415 char symname[KSYM_NAME_LEN];
416
417 wchan = get_wchan(task);
418
419 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
420 && !lookup_symbol_name(wchan, symname))
421 seq_printf(m, "%s", symname);
422 else
423 seq_putc(m, '0');
424
425 return 0;
426 }
427 #endif /* CONFIG_KALLSYMS */
428
429 static int lock_trace(struct task_struct *task)
430 {
431 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
432 if (err)
433 return err;
434 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
435 mutex_unlock(&task->signal->cred_guard_mutex);
436 return -EPERM;
437 }
438 return 0;
439 }
440
441 static void unlock_trace(struct task_struct *task)
442 {
443 mutex_unlock(&task->signal->cred_guard_mutex);
444 }
445
446 #ifdef CONFIG_STACKTRACE
447
448 #define MAX_STACK_TRACE_DEPTH 64
449
450 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
451 struct pid *pid, struct task_struct *task)
452 {
453 struct stack_trace trace;
454 unsigned long *entries;
455 int err;
456 int i;
457
458 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
459 if (!entries)
460 return -ENOMEM;
461
462 trace.nr_entries = 0;
463 trace.max_entries = MAX_STACK_TRACE_DEPTH;
464 trace.entries = entries;
465 trace.skip = 0;
466
467 err = lock_trace(task);
468 if (!err) {
469 save_stack_trace_tsk(task, &trace);
470
471 for (i = 0; i < trace.nr_entries; i++) {
472 seq_printf(m, "[<%pK>] %pB\n",
473 (void *)entries[i], (void *)entries[i]);
474 }
475 unlock_trace(task);
476 }
477 kfree(entries);
478
479 return err;
480 }
481 #endif
482
483 #ifdef CONFIG_SCHED_INFO
484 /*
485 * Provides /proc/PID/schedstat
486 */
487 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
488 struct pid *pid, struct task_struct *task)
489 {
490 if (unlikely(!sched_info_on()))
491 seq_printf(m, "0 0 0\n");
492 else
493 seq_printf(m, "%llu %llu %lu\n",
494 (unsigned long long)task->se.sum_exec_runtime,
495 (unsigned long long)task->sched_info.run_delay,
496 task->sched_info.pcount);
497
498 return 0;
499 }
500 #endif
501
502 #ifdef CONFIG_LATENCYTOP
503 static int lstats_show_proc(struct seq_file *m, void *v)
504 {
505 int i;
506 struct inode *inode = m->private;
507 struct task_struct *task = get_proc_task(inode);
508
509 if (!task)
510 return -ESRCH;
511 seq_puts(m, "Latency Top version : v0.1\n");
512 for (i = 0; i < 32; i++) {
513 struct latency_record *lr = &task->latency_record[i];
514 if (lr->backtrace[0]) {
515 int q;
516 seq_printf(m, "%i %li %li",
517 lr->count, lr->time, lr->max);
518 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
519 unsigned long bt = lr->backtrace[q];
520 if (!bt)
521 break;
522 if (bt == ULONG_MAX)
523 break;
524 seq_printf(m, " %ps", (void *)bt);
525 }
526 seq_putc(m, '\n');
527 }
528
529 }
530 put_task_struct(task);
531 return 0;
532 }
533
534 static int lstats_open(struct inode *inode, struct file *file)
535 {
536 return single_open(file, lstats_show_proc, inode);
537 }
538
539 static ssize_t lstats_write(struct file *file, const char __user *buf,
540 size_t count, loff_t *offs)
541 {
542 struct task_struct *task = get_proc_task(file_inode(file));
543
544 if (!task)
545 return -ESRCH;
546 clear_all_latency_tracing(task);
547 put_task_struct(task);
548
549 return count;
550 }
551
552 static const struct file_operations proc_lstats_operations = {
553 .open = lstats_open,
554 .read = seq_read,
555 .write = lstats_write,
556 .llseek = seq_lseek,
557 .release = single_release,
558 };
559
560 #endif
561
562 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
563 struct pid *pid, struct task_struct *task)
564 {
565 unsigned long totalpages = totalram_pages + total_swap_pages;
566 unsigned long points = 0;
567
568 points = oom_badness(task, NULL, NULL, totalpages) *
569 1000 / totalpages;
570 seq_printf(m, "%lu\n", points);
571
572 return 0;
573 }
574
575 struct limit_names {
576 const char *name;
577 const char *unit;
578 };
579
580 static const struct limit_names lnames[RLIM_NLIMITS] = {
581 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
582 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
583 [RLIMIT_DATA] = {"Max data size", "bytes"},
584 [RLIMIT_STACK] = {"Max stack size", "bytes"},
585 [RLIMIT_CORE] = {"Max core file size", "bytes"},
586 [RLIMIT_RSS] = {"Max resident set", "bytes"},
587 [RLIMIT_NPROC] = {"Max processes", "processes"},
588 [RLIMIT_NOFILE] = {"Max open files", "files"},
589 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
590 [RLIMIT_AS] = {"Max address space", "bytes"},
591 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
592 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
593 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
594 [RLIMIT_NICE] = {"Max nice priority", NULL},
595 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
596 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
597 };
598
599 /* Display limits for a process */
600 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
601 struct pid *pid, struct task_struct *task)
602 {
603 unsigned int i;
604 unsigned long flags;
605
606 struct rlimit rlim[RLIM_NLIMITS];
607
608 if (!lock_task_sighand(task, &flags))
609 return 0;
610 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
611 unlock_task_sighand(task, &flags);
612
613 /*
614 * print the file header
615 */
616 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
617 "Limit", "Soft Limit", "Hard Limit", "Units");
618
619 for (i = 0; i < RLIM_NLIMITS; i++) {
620 if (rlim[i].rlim_cur == RLIM_INFINITY)
621 seq_printf(m, "%-25s %-20s ",
622 lnames[i].name, "unlimited");
623 else
624 seq_printf(m, "%-25s %-20lu ",
625 lnames[i].name, rlim[i].rlim_cur);
626
627 if (rlim[i].rlim_max == RLIM_INFINITY)
628 seq_printf(m, "%-20s ", "unlimited");
629 else
630 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
631
632 if (lnames[i].unit)
633 seq_printf(m, "%-10s\n", lnames[i].unit);
634 else
635 seq_putc(m, '\n');
636 }
637
638 return 0;
639 }
640
641 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
642 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
643 struct pid *pid, struct task_struct *task)
644 {
645 long nr;
646 unsigned long args[6], sp, pc;
647 int res;
648
649 res = lock_trace(task);
650 if (res)
651 return res;
652
653 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
654 seq_puts(m, "running\n");
655 else if (nr < 0)
656 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
657 else
658 seq_printf(m,
659 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
660 nr,
661 args[0], args[1], args[2], args[3], args[4], args[5],
662 sp, pc);
663 unlock_trace(task);
664
665 return 0;
666 }
667 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
668
669 /************************************************************************/
670 /* Here the fs part begins */
671 /************************************************************************/
672
673 /* permission checks */
674 static int proc_fd_access_allowed(struct inode *inode)
675 {
676 struct task_struct *task;
677 int allowed = 0;
678 /* Allow access to a task's file descriptors if it is us or we
679 * may use ptrace attach to the process and find out that
680 * information.
681 */
682 task = get_proc_task(inode);
683 if (task) {
684 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
685 put_task_struct(task);
686 }
687 return allowed;
688 }
689
690 int proc_setattr(struct dentry *dentry, struct iattr *attr)
691 {
692 int error;
693 struct inode *inode = d_inode(dentry);
694 struct user_namespace *s_user_ns;
695
696 if (attr->ia_valid & ATTR_MODE)
697 return -EPERM;
698
699 /* Don't let anyone mess with weird proc files */
700 s_user_ns = inode->i_sb->s_user_ns;
701 if (!kuid_has_mapping(s_user_ns, inode->i_uid) ||
702 !kgid_has_mapping(s_user_ns, inode->i_gid))
703 return -EPERM;
704
705 error = setattr_prepare(dentry, attr);
706 if (error)
707 return error;
708
709 setattr_copy(inode, attr);
710 mark_inode_dirty(inode);
711 return 0;
712 }
713
714 /*
715 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
716 * or euid/egid (for hide_pid_min=2)?
717 */
718 static bool has_pid_permissions(struct pid_namespace *pid,
719 struct task_struct *task,
720 int hide_pid_min)
721 {
722 if (pid->hide_pid < hide_pid_min)
723 return true;
724 if (in_group_p(pid->pid_gid))
725 return true;
726 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
727 }
728
729
730 static int proc_pid_permission(struct inode *inode, int mask)
731 {
732 struct pid_namespace *pid = inode->i_sb->s_fs_info;
733 struct task_struct *task;
734 bool has_perms;
735
736 task = get_proc_task(inode);
737 if (!task)
738 return -ESRCH;
739 has_perms = has_pid_permissions(pid, task, 1);
740 put_task_struct(task);
741
742 if (!has_perms) {
743 if (pid->hide_pid == 2) {
744 /*
745 * Let's make getdents(), stat(), and open()
746 * consistent with each other. If a process
747 * may not stat() a file, it shouldn't be seen
748 * in procfs at all.
749 */
750 return -ENOENT;
751 }
752
753 return -EPERM;
754 }
755 return generic_permission(inode, mask);
756 }
757
758
759
760 static const struct inode_operations proc_def_inode_operations = {
761 .setattr = proc_setattr,
762 };
763
764 static int proc_single_show(struct seq_file *m, void *v)
765 {
766 struct inode *inode = m->private;
767 struct pid_namespace *ns;
768 struct pid *pid;
769 struct task_struct *task;
770 int ret;
771
772 ns = inode->i_sb->s_fs_info;
773 pid = proc_pid(inode);
774 task = get_pid_task(pid, PIDTYPE_PID);
775 if (!task)
776 return -ESRCH;
777
778 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
779
780 put_task_struct(task);
781 return ret;
782 }
783
784 static int proc_single_open(struct inode *inode, struct file *filp)
785 {
786 return single_open(filp, proc_single_show, inode);
787 }
788
789 static const struct file_operations proc_single_file_operations = {
790 .open = proc_single_open,
791 .read = seq_read,
792 .llseek = seq_lseek,
793 .release = single_release,
794 };
795
796
797 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
798 {
799 struct task_struct *task = get_proc_task(inode);
800 struct mm_struct *mm = ERR_PTR(-ESRCH);
801
802 if (task) {
803 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
804 put_task_struct(task);
805
806 if (!IS_ERR_OR_NULL(mm)) {
807 /* ensure this mm_struct can't be freed */
808 atomic_inc(&mm->mm_count);
809 /* but do not pin its memory */
810 mmput(mm);
811 }
812 }
813
814 return mm;
815 }
816
817 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
818 {
819 struct mm_struct *mm = proc_mem_open(inode, mode);
820
821 if (IS_ERR(mm))
822 return PTR_ERR(mm);
823
824 file->private_data = mm;
825 return 0;
826 }
827
828 static int mem_open(struct inode *inode, struct file *file)
829 {
830 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
831
832 /* OK to pass negative loff_t, we can catch out-of-range */
833 file->f_mode |= FMODE_UNSIGNED_OFFSET;
834
835 return ret;
836 }
837
838 static ssize_t mem_rw(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos, int write)
840 {
841 struct mm_struct *mm = file->private_data;
842 unsigned long addr = *ppos;
843 ssize_t copied;
844 char *page;
845 unsigned int flags;
846
847 if (!mm)
848 return 0;
849
850 page = (char *)__get_free_page(GFP_TEMPORARY);
851 if (!page)
852 return -ENOMEM;
853
854 copied = 0;
855 if (!atomic_inc_not_zero(&mm->mm_users))
856 goto free;
857
858 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
859 flags = FOLL_FORCE;
860 if (write)
861 flags |= FOLL_WRITE;
862
863 while (count > 0) {
864 int this_len = min_t(int, count, PAGE_SIZE);
865
866 if (write && copy_from_user(page, buf, this_len)) {
867 copied = -EFAULT;
868 break;
869 }
870
871 this_len = access_remote_vm(mm, addr, page, this_len, flags);
872 if (!this_len) {
873 if (!copied)
874 copied = -EIO;
875 break;
876 }
877
878 if (!write && copy_to_user(buf, page, this_len)) {
879 copied = -EFAULT;
880 break;
881 }
882
883 buf += this_len;
884 addr += this_len;
885 copied += this_len;
886 count -= this_len;
887 }
888 *ppos = addr;
889
890 mmput(mm);
891 free:
892 free_page((unsigned long) page);
893 return copied;
894 }
895
896 static ssize_t mem_read(struct file *file, char __user *buf,
897 size_t count, loff_t *ppos)
898 {
899 return mem_rw(file, buf, count, ppos, 0);
900 }
901
902 static ssize_t mem_write(struct file *file, const char __user *buf,
903 size_t count, loff_t *ppos)
904 {
905 return mem_rw(file, (char __user*)buf, count, ppos, 1);
906 }
907
908 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
909 {
910 switch (orig) {
911 case 0:
912 file->f_pos = offset;
913 break;
914 case 1:
915 file->f_pos += offset;
916 break;
917 default:
918 return -EINVAL;
919 }
920 force_successful_syscall_return();
921 return file->f_pos;
922 }
923
924 static int mem_release(struct inode *inode, struct file *file)
925 {
926 struct mm_struct *mm = file->private_data;
927 if (mm)
928 mmdrop(mm);
929 return 0;
930 }
931
932 static const struct file_operations proc_mem_operations = {
933 .llseek = mem_lseek,
934 .read = mem_read,
935 .write = mem_write,
936 .open = mem_open,
937 .release = mem_release,
938 };
939
940 static int environ_open(struct inode *inode, struct file *file)
941 {
942 return __mem_open(inode, file, PTRACE_MODE_READ);
943 }
944
945 static ssize_t environ_read(struct file *file, char __user *buf,
946 size_t count, loff_t *ppos)
947 {
948 char *page;
949 unsigned long src = *ppos;
950 int ret = 0;
951 struct mm_struct *mm = file->private_data;
952 unsigned long env_start, env_end;
953
954 /* Ensure the process spawned far enough to have an environment. */
955 if (!mm || !mm->env_end)
956 return 0;
957
958 page = (char *)__get_free_page(GFP_TEMPORARY);
959 if (!page)
960 return -ENOMEM;
961
962 ret = 0;
963 if (!atomic_inc_not_zero(&mm->mm_users))
964 goto free;
965
966 down_read(&mm->mmap_sem);
967 env_start = mm->env_start;
968 env_end = mm->env_end;
969 up_read(&mm->mmap_sem);
970
971 while (count > 0) {
972 size_t this_len, max_len;
973 int retval;
974
975 if (src >= (env_end - env_start))
976 break;
977
978 this_len = env_end - (env_start + src);
979
980 max_len = min_t(size_t, PAGE_SIZE, count);
981 this_len = min(max_len, this_len);
982
983 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
984
985 if (retval <= 0) {
986 ret = retval;
987 break;
988 }
989
990 if (copy_to_user(buf, page, retval)) {
991 ret = -EFAULT;
992 break;
993 }
994
995 ret += retval;
996 src += retval;
997 buf += retval;
998 count -= retval;
999 }
1000 *ppos = src;
1001 mmput(mm);
1002
1003 free:
1004 free_page((unsigned long) page);
1005 return ret;
1006 }
1007
1008 static const struct file_operations proc_environ_operations = {
1009 .open = environ_open,
1010 .read = environ_read,
1011 .llseek = generic_file_llseek,
1012 .release = mem_release,
1013 };
1014
1015 static int auxv_open(struct inode *inode, struct file *file)
1016 {
1017 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1018 }
1019
1020 static ssize_t auxv_read(struct file *file, char __user *buf,
1021 size_t count, loff_t *ppos)
1022 {
1023 struct mm_struct *mm = file->private_data;
1024 unsigned int nwords = 0;
1025
1026 if (!mm)
1027 return 0;
1028 do {
1029 nwords += 2;
1030 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1031 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1032 nwords * sizeof(mm->saved_auxv[0]));
1033 }
1034
1035 static const struct file_operations proc_auxv_operations = {
1036 .open = auxv_open,
1037 .read = auxv_read,
1038 .llseek = generic_file_llseek,
1039 .release = mem_release,
1040 };
1041
1042 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1043 loff_t *ppos)
1044 {
1045 struct task_struct *task = get_proc_task(file_inode(file));
1046 char buffer[PROC_NUMBUF];
1047 int oom_adj = OOM_ADJUST_MIN;
1048 size_t len;
1049
1050 if (!task)
1051 return -ESRCH;
1052 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1053 oom_adj = OOM_ADJUST_MAX;
1054 else
1055 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1056 OOM_SCORE_ADJ_MAX;
1057 put_task_struct(task);
1058 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1059 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1060 }
1061
1062 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1063 {
1064 static DEFINE_MUTEX(oom_adj_mutex);
1065 struct mm_struct *mm = NULL;
1066 struct task_struct *task;
1067 int err = 0;
1068
1069 task = get_proc_task(file_inode(file));
1070 if (!task)
1071 return -ESRCH;
1072
1073 mutex_lock(&oom_adj_mutex);
1074 if (legacy) {
1075 if (oom_adj < task->signal->oom_score_adj &&
1076 !capable(CAP_SYS_RESOURCE)) {
1077 err = -EACCES;
1078 goto err_unlock;
1079 }
1080 /*
1081 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1082 * /proc/pid/oom_score_adj instead.
1083 */
1084 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1085 current->comm, task_pid_nr(current), task_pid_nr(task),
1086 task_pid_nr(task));
1087 } else {
1088 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1089 !capable(CAP_SYS_RESOURCE)) {
1090 err = -EACCES;
1091 goto err_unlock;
1092 }
1093 }
1094
1095 /*
1096 * Make sure we will check other processes sharing the mm if this is
1097 * not vfrok which wants its own oom_score_adj.
1098 * pin the mm so it doesn't go away and get reused after task_unlock
1099 */
1100 if (!task->vfork_done) {
1101 struct task_struct *p = find_lock_task_mm(task);
1102
1103 if (p) {
1104 if (atomic_read(&p->mm->mm_users) > 1) {
1105 mm = p->mm;
1106 atomic_inc(&mm->mm_count);
1107 }
1108 task_unlock(p);
1109 }
1110 }
1111
1112 task->signal->oom_score_adj = oom_adj;
1113 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1114 task->signal->oom_score_adj_min = (short)oom_adj;
1115 trace_oom_score_adj_update(task);
1116
1117 if (mm) {
1118 struct task_struct *p;
1119
1120 rcu_read_lock();
1121 for_each_process(p) {
1122 if (same_thread_group(task, p))
1123 continue;
1124
1125 /* do not touch kernel threads or the global init */
1126 if (p->flags & PF_KTHREAD || is_global_init(p))
1127 continue;
1128
1129 task_lock(p);
1130 if (!p->vfork_done && process_shares_mm(p, mm)) {
1131 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1132 task_pid_nr(p), p->comm,
1133 p->signal->oom_score_adj, oom_adj,
1134 task_pid_nr(task), task->comm);
1135 p->signal->oom_score_adj = oom_adj;
1136 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1137 p->signal->oom_score_adj_min = (short)oom_adj;
1138 }
1139 task_unlock(p);
1140 }
1141 rcu_read_unlock();
1142 mmdrop(mm);
1143 }
1144 err_unlock:
1145 mutex_unlock(&oom_adj_mutex);
1146 put_task_struct(task);
1147 return err;
1148 }
1149
1150 /*
1151 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1152 * kernels. The effective policy is defined by oom_score_adj, which has a
1153 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1154 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1155 * Processes that become oom disabled via oom_adj will still be oom disabled
1156 * with this implementation.
1157 *
1158 * oom_adj cannot be removed since existing userspace binaries use it.
1159 */
1160 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1161 size_t count, loff_t *ppos)
1162 {
1163 char buffer[PROC_NUMBUF];
1164 int oom_adj;
1165 int err;
1166
1167 memset(buffer, 0, sizeof(buffer));
1168 if (count > sizeof(buffer) - 1)
1169 count = sizeof(buffer) - 1;
1170 if (copy_from_user(buffer, buf, count)) {
1171 err = -EFAULT;
1172 goto out;
1173 }
1174
1175 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1176 if (err)
1177 goto out;
1178 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1179 oom_adj != OOM_DISABLE) {
1180 err = -EINVAL;
1181 goto out;
1182 }
1183
1184 /*
1185 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1186 * value is always attainable.
1187 */
1188 if (oom_adj == OOM_ADJUST_MAX)
1189 oom_adj = OOM_SCORE_ADJ_MAX;
1190 else
1191 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1192
1193 err = __set_oom_adj(file, oom_adj, true);
1194 out:
1195 return err < 0 ? err : count;
1196 }
1197
1198 static const struct file_operations proc_oom_adj_operations = {
1199 .read = oom_adj_read,
1200 .write = oom_adj_write,
1201 .llseek = generic_file_llseek,
1202 };
1203
1204 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1205 size_t count, loff_t *ppos)
1206 {
1207 struct task_struct *task = get_proc_task(file_inode(file));
1208 char buffer[PROC_NUMBUF];
1209 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1210 size_t len;
1211
1212 if (!task)
1213 return -ESRCH;
1214 oom_score_adj = task->signal->oom_score_adj;
1215 put_task_struct(task);
1216 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1217 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1218 }
1219
1220 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1221 size_t count, loff_t *ppos)
1222 {
1223 char buffer[PROC_NUMBUF];
1224 int oom_score_adj;
1225 int err;
1226
1227 memset(buffer, 0, sizeof(buffer));
1228 if (count > sizeof(buffer) - 1)
1229 count = sizeof(buffer) - 1;
1230 if (copy_from_user(buffer, buf, count)) {
1231 err = -EFAULT;
1232 goto out;
1233 }
1234
1235 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1236 if (err)
1237 goto out;
1238 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1239 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1240 err = -EINVAL;
1241 goto out;
1242 }
1243
1244 err = __set_oom_adj(file, oom_score_adj, false);
1245 out:
1246 return err < 0 ? err : count;
1247 }
1248
1249 static const struct file_operations proc_oom_score_adj_operations = {
1250 .read = oom_score_adj_read,
1251 .write = oom_score_adj_write,
1252 .llseek = default_llseek,
1253 };
1254
1255 #ifdef CONFIG_AUDITSYSCALL
1256 #define TMPBUFLEN 11
1257 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1258 size_t count, loff_t *ppos)
1259 {
1260 struct inode * inode = file_inode(file);
1261 struct task_struct *task = get_proc_task(inode);
1262 ssize_t length;
1263 char tmpbuf[TMPBUFLEN];
1264
1265 if (!task)
1266 return -ESRCH;
1267 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1268 from_kuid(file->f_cred->user_ns,
1269 audit_get_loginuid(task)));
1270 put_task_struct(task);
1271 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1272 }
1273
1274 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1275 size_t count, loff_t *ppos)
1276 {
1277 struct inode * inode = file_inode(file);
1278 uid_t loginuid;
1279 kuid_t kloginuid;
1280 int rv;
1281
1282 rcu_read_lock();
1283 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1284 rcu_read_unlock();
1285 return -EPERM;
1286 }
1287 rcu_read_unlock();
1288
1289 if (*ppos != 0) {
1290 /* No partial writes. */
1291 return -EINVAL;
1292 }
1293
1294 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1295 if (rv < 0)
1296 return rv;
1297
1298 /* is userspace tring to explicitly UNSET the loginuid? */
1299 if (loginuid == AUDIT_UID_UNSET) {
1300 kloginuid = INVALID_UID;
1301 } else {
1302 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1303 if (!uid_valid(kloginuid))
1304 return -EINVAL;
1305 }
1306
1307 rv = audit_set_loginuid(kloginuid);
1308 if (rv < 0)
1309 return rv;
1310 return count;
1311 }
1312
1313 static const struct file_operations proc_loginuid_operations = {
1314 .read = proc_loginuid_read,
1315 .write = proc_loginuid_write,
1316 .llseek = generic_file_llseek,
1317 };
1318
1319 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1320 size_t count, loff_t *ppos)
1321 {
1322 struct inode * inode = file_inode(file);
1323 struct task_struct *task = get_proc_task(inode);
1324 ssize_t length;
1325 char tmpbuf[TMPBUFLEN];
1326
1327 if (!task)
1328 return -ESRCH;
1329 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1330 audit_get_sessionid(task));
1331 put_task_struct(task);
1332 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1333 }
1334
1335 static const struct file_operations proc_sessionid_operations = {
1336 .read = proc_sessionid_read,
1337 .llseek = generic_file_llseek,
1338 };
1339 #endif
1340
1341 #ifdef CONFIG_FAULT_INJECTION
1342 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1343 size_t count, loff_t *ppos)
1344 {
1345 struct task_struct *task = get_proc_task(file_inode(file));
1346 char buffer[PROC_NUMBUF];
1347 size_t len;
1348 int make_it_fail;
1349
1350 if (!task)
1351 return -ESRCH;
1352 make_it_fail = task->make_it_fail;
1353 put_task_struct(task);
1354
1355 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1356
1357 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1358 }
1359
1360 static ssize_t proc_fault_inject_write(struct file * file,
1361 const char __user * buf, size_t count, loff_t *ppos)
1362 {
1363 struct task_struct *task;
1364 char buffer[PROC_NUMBUF];
1365 int make_it_fail;
1366 int rv;
1367
1368 if (!capable(CAP_SYS_RESOURCE))
1369 return -EPERM;
1370 memset(buffer, 0, sizeof(buffer));
1371 if (count > sizeof(buffer) - 1)
1372 count = sizeof(buffer) - 1;
1373 if (copy_from_user(buffer, buf, count))
1374 return -EFAULT;
1375 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1376 if (rv < 0)
1377 return rv;
1378 if (make_it_fail < 0 || make_it_fail > 1)
1379 return -EINVAL;
1380
1381 task = get_proc_task(file_inode(file));
1382 if (!task)
1383 return -ESRCH;
1384 task->make_it_fail = make_it_fail;
1385 put_task_struct(task);
1386
1387 return count;
1388 }
1389
1390 static const struct file_operations proc_fault_inject_operations = {
1391 .read = proc_fault_inject_read,
1392 .write = proc_fault_inject_write,
1393 .llseek = generic_file_llseek,
1394 };
1395 #endif
1396
1397
1398 #ifdef CONFIG_SCHED_DEBUG
1399 /*
1400 * Print out various scheduling related per-task fields:
1401 */
1402 static int sched_show(struct seq_file *m, void *v)
1403 {
1404 struct inode *inode = m->private;
1405 struct task_struct *p;
1406
1407 p = get_proc_task(inode);
1408 if (!p)
1409 return -ESRCH;
1410 proc_sched_show_task(p, m);
1411
1412 put_task_struct(p);
1413
1414 return 0;
1415 }
1416
1417 static ssize_t
1418 sched_write(struct file *file, const char __user *buf,
1419 size_t count, loff_t *offset)
1420 {
1421 struct inode *inode = file_inode(file);
1422 struct task_struct *p;
1423
1424 p = get_proc_task(inode);
1425 if (!p)
1426 return -ESRCH;
1427 proc_sched_set_task(p);
1428
1429 put_task_struct(p);
1430
1431 return count;
1432 }
1433
1434 static int sched_open(struct inode *inode, struct file *filp)
1435 {
1436 return single_open(filp, sched_show, inode);
1437 }
1438
1439 static const struct file_operations proc_pid_sched_operations = {
1440 .open = sched_open,
1441 .read = seq_read,
1442 .write = sched_write,
1443 .llseek = seq_lseek,
1444 .release = single_release,
1445 };
1446
1447 #endif
1448
1449 #ifdef CONFIG_SCHED_AUTOGROUP
1450 /*
1451 * Print out autogroup related information:
1452 */
1453 static int sched_autogroup_show(struct seq_file *m, void *v)
1454 {
1455 struct inode *inode = m->private;
1456 struct task_struct *p;
1457
1458 p = get_proc_task(inode);
1459 if (!p)
1460 return -ESRCH;
1461 proc_sched_autogroup_show_task(p, m);
1462
1463 put_task_struct(p);
1464
1465 return 0;
1466 }
1467
1468 static ssize_t
1469 sched_autogroup_write(struct file *file, const char __user *buf,
1470 size_t count, loff_t *offset)
1471 {
1472 struct inode *inode = file_inode(file);
1473 struct task_struct *p;
1474 char buffer[PROC_NUMBUF];
1475 int nice;
1476 int err;
1477
1478 memset(buffer, 0, sizeof(buffer));
1479 if (count > sizeof(buffer) - 1)
1480 count = sizeof(buffer) - 1;
1481 if (copy_from_user(buffer, buf, count))
1482 return -EFAULT;
1483
1484 err = kstrtoint(strstrip(buffer), 0, &nice);
1485 if (err < 0)
1486 return err;
1487
1488 p = get_proc_task(inode);
1489 if (!p)
1490 return -ESRCH;
1491
1492 err = proc_sched_autogroup_set_nice(p, nice);
1493 if (err)
1494 count = err;
1495
1496 put_task_struct(p);
1497
1498 return count;
1499 }
1500
1501 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1502 {
1503 int ret;
1504
1505 ret = single_open(filp, sched_autogroup_show, NULL);
1506 if (!ret) {
1507 struct seq_file *m = filp->private_data;
1508
1509 m->private = inode;
1510 }
1511 return ret;
1512 }
1513
1514 static const struct file_operations proc_pid_sched_autogroup_operations = {
1515 .open = sched_autogroup_open,
1516 .read = seq_read,
1517 .write = sched_autogroup_write,
1518 .llseek = seq_lseek,
1519 .release = single_release,
1520 };
1521
1522 #endif /* CONFIG_SCHED_AUTOGROUP */
1523
1524 static ssize_t comm_write(struct file *file, const char __user *buf,
1525 size_t count, loff_t *offset)
1526 {
1527 struct inode *inode = file_inode(file);
1528 struct task_struct *p;
1529 char buffer[TASK_COMM_LEN];
1530 const size_t maxlen = sizeof(buffer) - 1;
1531
1532 memset(buffer, 0, sizeof(buffer));
1533 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1534 return -EFAULT;
1535
1536 p = get_proc_task(inode);
1537 if (!p)
1538 return -ESRCH;
1539
1540 if (same_thread_group(current, p))
1541 set_task_comm(p, buffer);
1542 else
1543 count = -EINVAL;
1544
1545 put_task_struct(p);
1546
1547 return count;
1548 }
1549
1550 static int comm_show(struct seq_file *m, void *v)
1551 {
1552 struct inode *inode = m->private;
1553 struct task_struct *p;
1554
1555 p = get_proc_task(inode);
1556 if (!p)
1557 return -ESRCH;
1558
1559 task_lock(p);
1560 seq_printf(m, "%s\n", p->comm);
1561 task_unlock(p);
1562
1563 put_task_struct(p);
1564
1565 return 0;
1566 }
1567
1568 static int comm_open(struct inode *inode, struct file *filp)
1569 {
1570 return single_open(filp, comm_show, inode);
1571 }
1572
1573 static const struct file_operations proc_pid_set_comm_operations = {
1574 .open = comm_open,
1575 .read = seq_read,
1576 .write = comm_write,
1577 .llseek = seq_lseek,
1578 .release = single_release,
1579 };
1580
1581 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1582 {
1583 struct task_struct *task;
1584 struct file *exe_file;
1585
1586 task = get_proc_task(d_inode(dentry));
1587 if (!task)
1588 return -ENOENT;
1589 exe_file = get_task_exe_file(task);
1590 put_task_struct(task);
1591 if (exe_file) {
1592 *exe_path = exe_file->f_path;
1593 path_get(&exe_file->f_path);
1594 fput(exe_file);
1595 return 0;
1596 } else
1597 return -ENOENT;
1598 }
1599
1600 static const char *proc_pid_get_link(struct dentry *dentry,
1601 struct inode *inode,
1602 struct delayed_call *done)
1603 {
1604 struct path path;
1605 int error = -EACCES;
1606
1607 if (!dentry)
1608 return ERR_PTR(-ECHILD);
1609
1610 /* Are we allowed to snoop on the tasks file descriptors? */
1611 if (!proc_fd_access_allowed(inode))
1612 goto out;
1613
1614 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1615 if (error)
1616 goto out;
1617
1618 nd_jump_link(&path);
1619 return NULL;
1620 out:
1621 return ERR_PTR(error);
1622 }
1623
1624 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1625 {
1626 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1627 char *pathname;
1628 int len;
1629
1630 if (!tmp)
1631 return -ENOMEM;
1632
1633 pathname = d_path(path, tmp, PAGE_SIZE);
1634 len = PTR_ERR(pathname);
1635 if (IS_ERR(pathname))
1636 goto out;
1637 len = tmp + PAGE_SIZE - 1 - pathname;
1638
1639 if (len > buflen)
1640 len = buflen;
1641 if (copy_to_user(buffer, pathname, len))
1642 len = -EFAULT;
1643 out:
1644 free_page((unsigned long)tmp);
1645 return len;
1646 }
1647
1648 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1649 {
1650 int error = -EACCES;
1651 struct inode *inode = d_inode(dentry);
1652 struct path path;
1653
1654 /* Are we allowed to snoop on the tasks file descriptors? */
1655 if (!proc_fd_access_allowed(inode))
1656 goto out;
1657
1658 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1659 if (error)
1660 goto out;
1661
1662 error = do_proc_readlink(&path, buffer, buflen);
1663 path_put(&path);
1664 out:
1665 return error;
1666 }
1667
1668 const struct inode_operations proc_pid_link_inode_operations = {
1669 .readlink = proc_pid_readlink,
1670 .get_link = proc_pid_get_link,
1671 .setattr = proc_setattr,
1672 };
1673
1674
1675 /* building an inode */
1676
1677 struct inode *proc_pid_make_inode(struct super_block * sb,
1678 struct task_struct *task, umode_t mode)
1679 {
1680 struct inode * inode;
1681 struct proc_inode *ei;
1682 const struct cred *cred;
1683
1684 /* We need a new inode */
1685
1686 inode = new_inode(sb);
1687 if (!inode)
1688 goto out;
1689
1690 /* Common stuff */
1691 ei = PROC_I(inode);
1692 inode->i_mode = mode;
1693 inode->i_ino = get_next_ino();
1694 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
1695 inode->i_op = &proc_def_inode_operations;
1696
1697 /*
1698 * grab the reference to task.
1699 */
1700 ei->pid = get_task_pid(task, PIDTYPE_PID);
1701 if (!ei->pid)
1702 goto out_unlock;
1703
1704 if (task_dumpable(task)) {
1705 rcu_read_lock();
1706 cred = __task_cred(task);
1707 inode->i_uid = cred->euid;
1708 inode->i_gid = cred->egid;
1709 rcu_read_unlock();
1710 }
1711 security_task_to_inode(task, inode);
1712
1713 out:
1714 return inode;
1715
1716 out_unlock:
1717 iput(inode);
1718 return NULL;
1719 }
1720
1721 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1722 {
1723 struct inode *inode = d_inode(dentry);
1724 struct task_struct *task;
1725 const struct cred *cred;
1726 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1727
1728 generic_fillattr(inode, stat);
1729
1730 rcu_read_lock();
1731 stat->uid = GLOBAL_ROOT_UID;
1732 stat->gid = GLOBAL_ROOT_GID;
1733 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1734 if (task) {
1735 if (!has_pid_permissions(pid, task, 2)) {
1736 rcu_read_unlock();
1737 /*
1738 * This doesn't prevent learning whether PID exists,
1739 * it only makes getattr() consistent with readdir().
1740 */
1741 return -ENOENT;
1742 }
1743 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1744 task_dumpable(task)) {
1745 cred = __task_cred(task);
1746 stat->uid = cred->euid;
1747 stat->gid = cred->egid;
1748 }
1749 }
1750 rcu_read_unlock();
1751 return 0;
1752 }
1753
1754 /* dentry stuff */
1755
1756 /*
1757 * Exceptional case: normally we are not allowed to unhash a busy
1758 * directory. In this case, however, we can do it - no aliasing problems
1759 * due to the way we treat inodes.
1760 *
1761 * Rewrite the inode's ownerships here because the owning task may have
1762 * performed a setuid(), etc.
1763 *
1764 * Before the /proc/pid/status file was created the only way to read
1765 * the effective uid of a /process was to stat /proc/pid. Reading
1766 * /proc/pid/status is slow enough that procps and other packages
1767 * kept stating /proc/pid. To keep the rules in /proc simple I have
1768 * made this apply to all per process world readable and executable
1769 * directories.
1770 */
1771 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1772 {
1773 struct inode *inode;
1774 struct task_struct *task;
1775 const struct cred *cred;
1776
1777 if (flags & LOOKUP_RCU)
1778 return -ECHILD;
1779
1780 inode = d_inode(dentry);
1781 task = get_proc_task(inode);
1782
1783 if (task) {
1784 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1785 task_dumpable(task)) {
1786 rcu_read_lock();
1787 cred = __task_cred(task);
1788 inode->i_uid = cred->euid;
1789 inode->i_gid = cred->egid;
1790 rcu_read_unlock();
1791 } else {
1792 inode->i_uid = GLOBAL_ROOT_UID;
1793 inode->i_gid = GLOBAL_ROOT_GID;
1794 }
1795 inode->i_mode &= ~(S_ISUID | S_ISGID);
1796 security_task_to_inode(task, inode);
1797 put_task_struct(task);
1798 return 1;
1799 }
1800 return 0;
1801 }
1802
1803 static inline bool proc_inode_is_dead(struct inode *inode)
1804 {
1805 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1806 }
1807
1808 int pid_delete_dentry(const struct dentry *dentry)
1809 {
1810 /* Is the task we represent dead?
1811 * If so, then don't put the dentry on the lru list,
1812 * kill it immediately.
1813 */
1814 return proc_inode_is_dead(d_inode(dentry));
1815 }
1816
1817 const struct dentry_operations pid_dentry_operations =
1818 {
1819 .d_revalidate = pid_revalidate,
1820 .d_delete = pid_delete_dentry,
1821 };
1822
1823 /* Lookups */
1824
1825 /*
1826 * Fill a directory entry.
1827 *
1828 * If possible create the dcache entry and derive our inode number and
1829 * file type from dcache entry.
1830 *
1831 * Since all of the proc inode numbers are dynamically generated, the inode
1832 * numbers do not exist until the inode is cache. This means creating the
1833 * the dcache entry in readdir is necessary to keep the inode numbers
1834 * reported by readdir in sync with the inode numbers reported
1835 * by stat.
1836 */
1837 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1838 const char *name, int len,
1839 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1840 {
1841 struct dentry *child, *dir = file->f_path.dentry;
1842 struct qstr qname = QSTR_INIT(name, len);
1843 struct inode *inode;
1844 unsigned type;
1845 ino_t ino;
1846
1847 child = d_hash_and_lookup(dir, &qname);
1848 if (!child) {
1849 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1850 child = d_alloc_parallel(dir, &qname, &wq);
1851 if (IS_ERR(child))
1852 goto end_instantiate;
1853 if (d_in_lookup(child)) {
1854 int err = instantiate(d_inode(dir), child, task, ptr);
1855 d_lookup_done(child);
1856 if (err < 0) {
1857 dput(child);
1858 goto end_instantiate;
1859 }
1860 }
1861 }
1862 inode = d_inode(child);
1863 ino = inode->i_ino;
1864 type = inode->i_mode >> 12;
1865 dput(child);
1866 return dir_emit(ctx, name, len, ino, type);
1867
1868 end_instantiate:
1869 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1870 }
1871
1872 /*
1873 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1874 * which represent vma start and end addresses.
1875 */
1876 static int dname_to_vma_addr(struct dentry *dentry,
1877 unsigned long *start, unsigned long *end)
1878 {
1879 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1880 return -EINVAL;
1881
1882 return 0;
1883 }
1884
1885 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1886 {
1887 unsigned long vm_start, vm_end;
1888 bool exact_vma_exists = false;
1889 struct mm_struct *mm = NULL;
1890 struct task_struct *task;
1891 const struct cred *cred;
1892 struct inode *inode;
1893 int status = 0;
1894
1895 if (flags & LOOKUP_RCU)
1896 return -ECHILD;
1897
1898 inode = d_inode(dentry);
1899 task = get_proc_task(inode);
1900 if (!task)
1901 goto out_notask;
1902
1903 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1904 if (IS_ERR_OR_NULL(mm))
1905 goto out;
1906
1907 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1908 down_read(&mm->mmap_sem);
1909 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1910 up_read(&mm->mmap_sem);
1911 }
1912
1913 mmput(mm);
1914
1915 if (exact_vma_exists) {
1916 if (task_dumpable(task)) {
1917 rcu_read_lock();
1918 cred = __task_cred(task);
1919 inode->i_uid = cred->euid;
1920 inode->i_gid = cred->egid;
1921 rcu_read_unlock();
1922 } else {
1923 inode->i_uid = GLOBAL_ROOT_UID;
1924 inode->i_gid = GLOBAL_ROOT_GID;
1925 }
1926 security_task_to_inode(task, inode);
1927 status = 1;
1928 }
1929
1930 out:
1931 put_task_struct(task);
1932
1933 out_notask:
1934 return status;
1935 }
1936
1937 static const struct dentry_operations tid_map_files_dentry_operations = {
1938 .d_revalidate = map_files_d_revalidate,
1939 .d_delete = pid_delete_dentry,
1940 };
1941
1942 static int map_files_get_link(struct dentry *dentry, struct path *path)
1943 {
1944 unsigned long vm_start, vm_end;
1945 struct vm_area_struct *vma;
1946 struct task_struct *task;
1947 struct mm_struct *mm;
1948 int rc;
1949
1950 rc = -ENOENT;
1951 task = get_proc_task(d_inode(dentry));
1952 if (!task)
1953 goto out;
1954
1955 mm = get_task_mm(task);
1956 put_task_struct(task);
1957 if (!mm)
1958 goto out;
1959
1960 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1961 if (rc)
1962 goto out_mmput;
1963
1964 rc = -ENOENT;
1965 down_read(&mm->mmap_sem);
1966 vma = find_exact_vma(mm, vm_start, vm_end);
1967 if (vma && vma->vm_file) {
1968 *path = vma_pr_or_file(vma)->f_path;
1969 path_get(path);
1970 rc = 0;
1971 }
1972 up_read(&mm->mmap_sem);
1973
1974 out_mmput:
1975 mmput(mm);
1976 out:
1977 return rc;
1978 }
1979
1980 struct map_files_info {
1981 fmode_t mode;
1982 unsigned int len;
1983 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1984 };
1985
1986 /*
1987 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1988 * symlinks may be used to bypass permissions on ancestor directories in the
1989 * path to the file in question.
1990 */
1991 static const char *
1992 proc_map_files_get_link(struct dentry *dentry,
1993 struct inode *inode,
1994 struct delayed_call *done)
1995 {
1996 if (!capable(CAP_SYS_ADMIN))
1997 return ERR_PTR(-EPERM);
1998
1999 return proc_pid_get_link(dentry, inode, done);
2000 }
2001
2002 /*
2003 * Identical to proc_pid_link_inode_operations except for get_link()
2004 */
2005 static const struct inode_operations proc_map_files_link_inode_operations = {
2006 .readlink = proc_pid_readlink,
2007 .get_link = proc_map_files_get_link,
2008 .setattr = proc_setattr,
2009 };
2010
2011 static int
2012 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2013 struct task_struct *task, const void *ptr)
2014 {
2015 fmode_t mode = (fmode_t)(unsigned long)ptr;
2016 struct proc_inode *ei;
2017 struct inode *inode;
2018
2019 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2020 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2021 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2022 if (!inode)
2023 return -ENOENT;
2024
2025 ei = PROC_I(inode);
2026 ei->op.proc_get_link = map_files_get_link;
2027
2028 inode->i_op = &proc_map_files_link_inode_operations;
2029 inode->i_size = 64;
2030
2031 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2032 d_add(dentry, inode);
2033
2034 return 0;
2035 }
2036
2037 static struct dentry *proc_map_files_lookup(struct inode *dir,
2038 struct dentry *dentry, unsigned int flags)
2039 {
2040 unsigned long vm_start, vm_end;
2041 struct vm_area_struct *vma;
2042 struct task_struct *task;
2043 int result;
2044 struct mm_struct *mm;
2045
2046 result = -ENOENT;
2047 task = get_proc_task(dir);
2048 if (!task)
2049 goto out;
2050
2051 result = -EACCES;
2052 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2053 goto out_put_task;
2054
2055 result = -ENOENT;
2056 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2057 goto out_put_task;
2058
2059 mm = get_task_mm(task);
2060 if (!mm)
2061 goto out_put_task;
2062
2063 down_read(&mm->mmap_sem);
2064 vma = find_exact_vma(mm, vm_start, vm_end);
2065 if (!vma)
2066 goto out_no_vma;
2067
2068 if (vma->vm_file)
2069 result = proc_map_files_instantiate(dir, dentry, task,
2070 (void *)(unsigned long)vma->vm_file->f_mode);
2071
2072 out_no_vma:
2073 up_read(&mm->mmap_sem);
2074 mmput(mm);
2075 out_put_task:
2076 put_task_struct(task);
2077 out:
2078 return ERR_PTR(result);
2079 }
2080
2081 static const struct inode_operations proc_map_files_inode_operations = {
2082 .lookup = proc_map_files_lookup,
2083 .permission = proc_fd_permission,
2084 .setattr = proc_setattr,
2085 };
2086
2087 static int
2088 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2089 {
2090 struct vm_area_struct *vma;
2091 struct task_struct *task;
2092 struct mm_struct *mm;
2093 unsigned long nr_files, pos, i;
2094 struct flex_array *fa = NULL;
2095 struct map_files_info info;
2096 struct map_files_info *p;
2097 int ret;
2098
2099 ret = -ENOENT;
2100 task = get_proc_task(file_inode(file));
2101 if (!task)
2102 goto out;
2103
2104 ret = -EACCES;
2105 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2106 goto out_put_task;
2107
2108 ret = 0;
2109 if (!dir_emit_dots(file, ctx))
2110 goto out_put_task;
2111
2112 mm = get_task_mm(task);
2113 if (!mm)
2114 goto out_put_task;
2115 down_read(&mm->mmap_sem);
2116
2117 nr_files = 0;
2118
2119 /*
2120 * We need two passes here:
2121 *
2122 * 1) Collect vmas of mapped files with mmap_sem taken
2123 * 2) Release mmap_sem and instantiate entries
2124 *
2125 * otherwise we get lockdep complained, since filldir()
2126 * routine might require mmap_sem taken in might_fault().
2127 */
2128
2129 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2130 if (vma->vm_file && ++pos > ctx->pos)
2131 nr_files++;
2132 }
2133
2134 if (nr_files) {
2135 fa = flex_array_alloc(sizeof(info), nr_files,
2136 GFP_KERNEL);
2137 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2138 GFP_KERNEL)) {
2139 ret = -ENOMEM;
2140 if (fa)
2141 flex_array_free(fa);
2142 up_read(&mm->mmap_sem);
2143 mmput(mm);
2144 goto out_put_task;
2145 }
2146 for (i = 0, vma = mm->mmap, pos = 2; vma;
2147 vma = vma->vm_next) {
2148 if (!vma->vm_file)
2149 continue;
2150 if (++pos <= ctx->pos)
2151 continue;
2152
2153 info.mode = vma->vm_file->f_mode;
2154 info.len = snprintf(info.name,
2155 sizeof(info.name), "%lx-%lx",
2156 vma->vm_start, vma->vm_end);
2157 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2158 BUG();
2159 }
2160 }
2161 up_read(&mm->mmap_sem);
2162
2163 for (i = 0; i < nr_files; i++) {
2164 p = flex_array_get(fa, i);
2165 if (!proc_fill_cache(file, ctx,
2166 p->name, p->len,
2167 proc_map_files_instantiate,
2168 task,
2169 (void *)(unsigned long)p->mode))
2170 break;
2171 ctx->pos++;
2172 }
2173 if (fa)
2174 flex_array_free(fa);
2175 mmput(mm);
2176
2177 out_put_task:
2178 put_task_struct(task);
2179 out:
2180 return ret;
2181 }
2182
2183 static const struct file_operations proc_map_files_operations = {
2184 .read = generic_read_dir,
2185 .iterate_shared = proc_map_files_readdir,
2186 .llseek = generic_file_llseek,
2187 };
2188
2189 #ifdef CONFIG_CHECKPOINT_RESTORE
2190 struct timers_private {
2191 struct pid *pid;
2192 struct task_struct *task;
2193 struct sighand_struct *sighand;
2194 struct pid_namespace *ns;
2195 unsigned long flags;
2196 };
2197
2198 static void *timers_start(struct seq_file *m, loff_t *pos)
2199 {
2200 struct timers_private *tp = m->private;
2201
2202 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2203 if (!tp->task)
2204 return ERR_PTR(-ESRCH);
2205
2206 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2207 if (!tp->sighand)
2208 return ERR_PTR(-ESRCH);
2209
2210 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2211 }
2212
2213 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2214 {
2215 struct timers_private *tp = m->private;
2216 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2217 }
2218
2219 static void timers_stop(struct seq_file *m, void *v)
2220 {
2221 struct timers_private *tp = m->private;
2222
2223 if (tp->sighand) {
2224 unlock_task_sighand(tp->task, &tp->flags);
2225 tp->sighand = NULL;
2226 }
2227
2228 if (tp->task) {
2229 put_task_struct(tp->task);
2230 tp->task = NULL;
2231 }
2232 }
2233
2234 static int show_timer(struct seq_file *m, void *v)
2235 {
2236 struct k_itimer *timer;
2237 struct timers_private *tp = m->private;
2238 int notify;
2239 static const char * const nstr[] = {
2240 [SIGEV_SIGNAL] = "signal",
2241 [SIGEV_NONE] = "none",
2242 [SIGEV_THREAD] = "thread",
2243 };
2244
2245 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2246 notify = timer->it_sigev_notify;
2247
2248 seq_printf(m, "ID: %d\n", timer->it_id);
2249 seq_printf(m, "signal: %d/%p\n",
2250 timer->sigq->info.si_signo,
2251 timer->sigq->info.si_value.sival_ptr);
2252 seq_printf(m, "notify: %s/%s.%d\n",
2253 nstr[notify & ~SIGEV_THREAD_ID],
2254 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2255 pid_nr_ns(timer->it_pid, tp->ns));
2256 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2257
2258 return 0;
2259 }
2260
2261 static const struct seq_operations proc_timers_seq_ops = {
2262 .start = timers_start,
2263 .next = timers_next,
2264 .stop = timers_stop,
2265 .show = show_timer,
2266 };
2267
2268 static int proc_timers_open(struct inode *inode, struct file *file)
2269 {
2270 struct timers_private *tp;
2271
2272 tp = __seq_open_private(file, &proc_timers_seq_ops,
2273 sizeof(struct timers_private));
2274 if (!tp)
2275 return -ENOMEM;
2276
2277 tp->pid = proc_pid(inode);
2278 tp->ns = inode->i_sb->s_fs_info;
2279 return 0;
2280 }
2281
2282 static const struct file_operations proc_timers_operations = {
2283 .open = proc_timers_open,
2284 .read = seq_read,
2285 .llseek = seq_lseek,
2286 .release = seq_release_private,
2287 };
2288 #endif
2289
2290 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2291 size_t count, loff_t *offset)
2292 {
2293 struct inode *inode = file_inode(file);
2294 struct task_struct *p;
2295 u64 slack_ns;
2296 int err;
2297
2298 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2299 if (err < 0)
2300 return err;
2301
2302 p = get_proc_task(inode);
2303 if (!p)
2304 return -ESRCH;
2305
2306 if (p != current) {
2307 if (!capable(CAP_SYS_NICE)) {
2308 count = -EPERM;
2309 goto out;
2310 }
2311
2312 err = security_task_setscheduler(p);
2313 if (err) {
2314 count = err;
2315 goto out;
2316 }
2317 }
2318
2319 task_lock(p);
2320 if (slack_ns == 0)
2321 p->timer_slack_ns = p->default_timer_slack_ns;
2322 else
2323 p->timer_slack_ns = slack_ns;
2324 task_unlock(p);
2325
2326 out:
2327 put_task_struct(p);
2328
2329 return count;
2330 }
2331
2332 static int timerslack_ns_show(struct seq_file *m, void *v)
2333 {
2334 struct inode *inode = m->private;
2335 struct task_struct *p;
2336 int err = 0;
2337
2338 p = get_proc_task(inode);
2339 if (!p)
2340 return -ESRCH;
2341
2342 if (p != current) {
2343
2344 if (!capable(CAP_SYS_NICE)) {
2345 err = -EPERM;
2346 goto out;
2347 }
2348 err = security_task_getscheduler(p);
2349 if (err)
2350 goto out;
2351 }
2352
2353 task_lock(p);
2354 seq_printf(m, "%llu\n", p->timer_slack_ns);
2355 task_unlock(p);
2356
2357 out:
2358 put_task_struct(p);
2359
2360 return err;
2361 }
2362
2363 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2364 {
2365 return single_open(filp, timerslack_ns_show, inode);
2366 }
2367
2368 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2369 .open = timerslack_ns_open,
2370 .read = seq_read,
2371 .write = timerslack_ns_write,
2372 .llseek = seq_lseek,
2373 .release = single_release,
2374 };
2375
2376 static int proc_pident_instantiate(struct inode *dir,
2377 struct dentry *dentry, struct task_struct *task, const void *ptr)
2378 {
2379 const struct pid_entry *p = ptr;
2380 struct inode *inode;
2381 struct proc_inode *ei;
2382
2383 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
2384 if (!inode)
2385 goto out;
2386
2387 ei = PROC_I(inode);
2388 if (S_ISDIR(inode->i_mode))
2389 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2390 if (p->iop)
2391 inode->i_op = p->iop;
2392 if (p->fop)
2393 inode->i_fop = p->fop;
2394 ei->op = p->op;
2395 d_set_d_op(dentry, &pid_dentry_operations);
2396 d_add(dentry, inode);
2397 /* Close the race of the process dying before we return the dentry */
2398 if (pid_revalidate(dentry, 0))
2399 return 0;
2400 out:
2401 return -ENOENT;
2402 }
2403
2404 static struct dentry *proc_pident_lookup(struct inode *dir,
2405 struct dentry *dentry,
2406 const struct pid_entry *ents,
2407 unsigned int nents)
2408 {
2409 int error;
2410 struct task_struct *task = get_proc_task(dir);
2411 const struct pid_entry *p, *last;
2412
2413 error = -ENOENT;
2414
2415 if (!task)
2416 goto out_no_task;
2417
2418 /*
2419 * Yes, it does not scale. And it should not. Don't add
2420 * new entries into /proc/<tgid>/ without very good reasons.
2421 */
2422 last = &ents[nents];
2423 for (p = ents; p < last; p++) {
2424 if (p->len != dentry->d_name.len)
2425 continue;
2426 if (!memcmp(dentry->d_name.name, p->name, p->len))
2427 break;
2428 }
2429 if (p >= last)
2430 goto out;
2431
2432 error = proc_pident_instantiate(dir, dentry, task, p);
2433 out:
2434 put_task_struct(task);
2435 out_no_task:
2436 return ERR_PTR(error);
2437 }
2438
2439 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2440 const struct pid_entry *ents, unsigned int nents)
2441 {
2442 struct task_struct *task = get_proc_task(file_inode(file));
2443 const struct pid_entry *p;
2444
2445 if (!task)
2446 return -ENOENT;
2447
2448 if (!dir_emit_dots(file, ctx))
2449 goto out;
2450
2451 if (ctx->pos >= nents + 2)
2452 goto out;
2453
2454 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2455 if (!proc_fill_cache(file, ctx, p->name, p->len,
2456 proc_pident_instantiate, task, p))
2457 break;
2458 ctx->pos++;
2459 }
2460 out:
2461 put_task_struct(task);
2462 return 0;
2463 }
2464
2465 #ifdef CONFIG_SECURITY
2466 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2467 size_t count, loff_t *ppos)
2468 {
2469 struct inode * inode = file_inode(file);
2470 char *p = NULL;
2471 ssize_t length;
2472 struct task_struct *task = get_proc_task(inode);
2473
2474 if (!task)
2475 return -ESRCH;
2476
2477 length = security_getprocattr(task,
2478 (char*)file->f_path.dentry->d_name.name,
2479 &p);
2480 put_task_struct(task);
2481 if (length > 0)
2482 length = simple_read_from_buffer(buf, count, ppos, p, length);
2483 kfree(p);
2484 return length;
2485 }
2486
2487 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2488 size_t count, loff_t *ppos)
2489 {
2490 struct inode * inode = file_inode(file);
2491 void *page;
2492 ssize_t length;
2493 struct task_struct *task = get_proc_task(inode);
2494
2495 length = -ESRCH;
2496 if (!task)
2497 goto out_no_task;
2498 if (count > PAGE_SIZE)
2499 count = PAGE_SIZE;
2500
2501 /* No partial writes. */
2502 length = -EINVAL;
2503 if (*ppos != 0)
2504 goto out;
2505
2506 page = memdup_user(buf, count);
2507 if (IS_ERR(page)) {
2508 length = PTR_ERR(page);
2509 goto out;
2510 }
2511
2512 /* Guard against adverse ptrace interaction */
2513 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2514 if (length < 0)
2515 goto out_free;
2516
2517 length = security_setprocattr(task,
2518 (char*)file->f_path.dentry->d_name.name,
2519 page, count);
2520 mutex_unlock(&task->signal->cred_guard_mutex);
2521 out_free:
2522 kfree(page);
2523 out:
2524 put_task_struct(task);
2525 out_no_task:
2526 return length;
2527 }
2528
2529 static const struct file_operations proc_pid_attr_operations = {
2530 .read = proc_pid_attr_read,
2531 .write = proc_pid_attr_write,
2532 .llseek = generic_file_llseek,
2533 };
2534
2535 static const struct pid_entry attr_dir_stuff[] = {
2536 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2537 REG("prev", S_IRUGO, proc_pid_attr_operations),
2538 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2539 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2540 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2541 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2542 };
2543
2544 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2545 {
2546 return proc_pident_readdir(file, ctx,
2547 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2548 }
2549
2550 static const struct file_operations proc_attr_dir_operations = {
2551 .read = generic_read_dir,
2552 .iterate_shared = proc_attr_dir_readdir,
2553 .llseek = generic_file_llseek,
2554 };
2555
2556 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2557 struct dentry *dentry, unsigned int flags)
2558 {
2559 return proc_pident_lookup(dir, dentry,
2560 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2561 }
2562
2563 static const struct inode_operations proc_attr_dir_inode_operations = {
2564 .lookup = proc_attr_dir_lookup,
2565 .getattr = pid_getattr,
2566 .setattr = proc_setattr,
2567 };
2568
2569 #endif
2570
2571 #ifdef CONFIG_ELF_CORE
2572 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2573 size_t count, loff_t *ppos)
2574 {
2575 struct task_struct *task = get_proc_task(file_inode(file));
2576 struct mm_struct *mm;
2577 char buffer[PROC_NUMBUF];
2578 size_t len;
2579 int ret;
2580
2581 if (!task)
2582 return -ESRCH;
2583
2584 ret = 0;
2585 mm = get_task_mm(task);
2586 if (mm) {
2587 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2588 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2589 MMF_DUMP_FILTER_SHIFT));
2590 mmput(mm);
2591 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2592 }
2593
2594 put_task_struct(task);
2595
2596 return ret;
2597 }
2598
2599 static ssize_t proc_coredump_filter_write(struct file *file,
2600 const char __user *buf,
2601 size_t count,
2602 loff_t *ppos)
2603 {
2604 struct task_struct *task;
2605 struct mm_struct *mm;
2606 unsigned int val;
2607 int ret;
2608 int i;
2609 unsigned long mask;
2610
2611 ret = kstrtouint_from_user(buf, count, 0, &val);
2612 if (ret < 0)
2613 return ret;
2614
2615 ret = -ESRCH;
2616 task = get_proc_task(file_inode(file));
2617 if (!task)
2618 goto out_no_task;
2619
2620 mm = get_task_mm(task);
2621 if (!mm)
2622 goto out_no_mm;
2623 ret = 0;
2624
2625 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2626 if (val & mask)
2627 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2628 else
2629 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2630 }
2631
2632 mmput(mm);
2633 out_no_mm:
2634 put_task_struct(task);
2635 out_no_task:
2636 if (ret < 0)
2637 return ret;
2638 return count;
2639 }
2640
2641 static const struct file_operations proc_coredump_filter_operations = {
2642 .read = proc_coredump_filter_read,
2643 .write = proc_coredump_filter_write,
2644 .llseek = generic_file_llseek,
2645 };
2646 #endif
2647
2648 #ifdef CONFIG_TASK_IO_ACCOUNTING
2649 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2650 {
2651 struct task_io_accounting acct = task->ioac;
2652 unsigned long flags;
2653 int result;
2654
2655 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2656 if (result)
2657 return result;
2658
2659 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2660 result = -EACCES;
2661 goto out_unlock;
2662 }
2663
2664 if (whole && lock_task_sighand(task, &flags)) {
2665 struct task_struct *t = task;
2666
2667 task_io_accounting_add(&acct, &task->signal->ioac);
2668 while_each_thread(task, t)
2669 task_io_accounting_add(&acct, &t->ioac);
2670
2671 unlock_task_sighand(task, &flags);
2672 }
2673 seq_printf(m,
2674 "rchar: %llu\n"
2675 "wchar: %llu\n"
2676 "syscr: %llu\n"
2677 "syscw: %llu\n"
2678 "read_bytes: %llu\n"
2679 "write_bytes: %llu\n"
2680 "cancelled_write_bytes: %llu\n",
2681 (unsigned long long)acct.rchar,
2682 (unsigned long long)acct.wchar,
2683 (unsigned long long)acct.syscr,
2684 (unsigned long long)acct.syscw,
2685 (unsigned long long)acct.read_bytes,
2686 (unsigned long long)acct.write_bytes,
2687 (unsigned long long)acct.cancelled_write_bytes);
2688 result = 0;
2689
2690 out_unlock:
2691 mutex_unlock(&task->signal->cred_guard_mutex);
2692 return result;
2693 }
2694
2695 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2696 struct pid *pid, struct task_struct *task)
2697 {
2698 return do_io_accounting(task, m, 0);
2699 }
2700
2701 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2702 struct pid *pid, struct task_struct *task)
2703 {
2704 return do_io_accounting(task, m, 1);
2705 }
2706 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2707
2708 #ifdef CONFIG_USER_NS
2709 static int proc_id_map_open(struct inode *inode, struct file *file,
2710 const struct seq_operations *seq_ops)
2711 {
2712 struct user_namespace *ns = NULL;
2713 struct task_struct *task;
2714 struct seq_file *seq;
2715 int ret = -EINVAL;
2716
2717 task = get_proc_task(inode);
2718 if (task) {
2719 rcu_read_lock();
2720 ns = get_user_ns(task_cred_xxx(task, user_ns));
2721 rcu_read_unlock();
2722 put_task_struct(task);
2723 }
2724 if (!ns)
2725 goto err;
2726
2727 ret = seq_open(file, seq_ops);
2728 if (ret)
2729 goto err_put_ns;
2730
2731 seq = file->private_data;
2732 seq->private = ns;
2733
2734 return 0;
2735 err_put_ns:
2736 put_user_ns(ns);
2737 err:
2738 return ret;
2739 }
2740
2741 static int proc_id_map_release(struct inode *inode, struct file *file)
2742 {
2743 struct seq_file *seq = file->private_data;
2744 struct user_namespace *ns = seq->private;
2745 put_user_ns(ns);
2746 return seq_release(inode, file);
2747 }
2748
2749 static int proc_uid_map_open(struct inode *inode, struct file *file)
2750 {
2751 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2752 }
2753
2754 static int proc_gid_map_open(struct inode *inode, struct file *file)
2755 {
2756 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2757 }
2758
2759 static int proc_projid_map_open(struct inode *inode, struct file *file)
2760 {
2761 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2762 }
2763
2764 static const struct file_operations proc_uid_map_operations = {
2765 .open = proc_uid_map_open,
2766 .write = proc_uid_map_write,
2767 .read = seq_read,
2768 .llseek = seq_lseek,
2769 .release = proc_id_map_release,
2770 };
2771
2772 static const struct file_operations proc_gid_map_operations = {
2773 .open = proc_gid_map_open,
2774 .write = proc_gid_map_write,
2775 .read = seq_read,
2776 .llseek = seq_lseek,
2777 .release = proc_id_map_release,
2778 };
2779
2780 static const struct file_operations proc_projid_map_operations = {
2781 .open = proc_projid_map_open,
2782 .write = proc_projid_map_write,
2783 .read = seq_read,
2784 .llseek = seq_lseek,
2785 .release = proc_id_map_release,
2786 };
2787
2788 static int proc_setgroups_open(struct inode *inode, struct file *file)
2789 {
2790 struct user_namespace *ns = NULL;
2791 struct task_struct *task;
2792 int ret;
2793
2794 ret = -ESRCH;
2795 task = get_proc_task(inode);
2796 if (task) {
2797 rcu_read_lock();
2798 ns = get_user_ns(task_cred_xxx(task, user_ns));
2799 rcu_read_unlock();
2800 put_task_struct(task);
2801 }
2802 if (!ns)
2803 goto err;
2804
2805 if (file->f_mode & FMODE_WRITE) {
2806 ret = -EACCES;
2807 if (!ns_capable(ns, CAP_SYS_ADMIN))
2808 goto err_put_ns;
2809 }
2810
2811 ret = single_open(file, &proc_setgroups_show, ns);
2812 if (ret)
2813 goto err_put_ns;
2814
2815 return 0;
2816 err_put_ns:
2817 put_user_ns(ns);
2818 err:
2819 return ret;
2820 }
2821
2822 static int proc_setgroups_release(struct inode *inode, struct file *file)
2823 {
2824 struct seq_file *seq = file->private_data;
2825 struct user_namespace *ns = seq->private;
2826 int ret = single_release(inode, file);
2827 put_user_ns(ns);
2828 return ret;
2829 }
2830
2831 static const struct file_operations proc_setgroups_operations = {
2832 .open = proc_setgroups_open,
2833 .write = proc_setgroups_write,
2834 .read = seq_read,
2835 .llseek = seq_lseek,
2836 .release = proc_setgroups_release,
2837 };
2838 #endif /* CONFIG_USER_NS */
2839
2840 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2841 struct pid *pid, struct task_struct *task)
2842 {
2843 int err = lock_trace(task);
2844 if (!err) {
2845 seq_printf(m, "%08x\n", task->personality);
2846 unlock_trace(task);
2847 }
2848 return err;
2849 }
2850
2851 /*
2852 * Thread groups
2853 */
2854 static const struct file_operations proc_task_operations;
2855 static const struct inode_operations proc_task_inode_operations;
2856
2857 static const struct pid_entry tgid_base_stuff[] = {
2858 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2859 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2860 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2861 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2862 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2863 #ifdef CONFIG_NET
2864 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2865 #endif
2866 REG("environ", S_IRUSR, proc_environ_operations),
2867 REG("auxv", S_IRUSR, proc_auxv_operations),
2868 ONE("status", S_IRUGO, proc_pid_status),
2869 ONE("personality", S_IRUSR, proc_pid_personality),
2870 ONE("limits", S_IRUGO, proc_pid_limits),
2871 #ifdef CONFIG_SCHED_DEBUG
2872 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2873 #endif
2874 #ifdef CONFIG_SCHED_AUTOGROUP
2875 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2876 #endif
2877 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2878 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2879 ONE("syscall", S_IRUSR, proc_pid_syscall),
2880 #endif
2881 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2882 ONE("stat", S_IRUGO, proc_tgid_stat),
2883 ONE("statm", S_IRUGO, proc_pid_statm),
2884 REG("maps", S_IRUGO, proc_pid_maps_operations),
2885 #ifdef CONFIG_NUMA
2886 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2887 #endif
2888 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2889 LNK("cwd", proc_cwd_link),
2890 LNK("root", proc_root_link),
2891 LNK("exe", proc_exe_link),
2892 REG("mounts", S_IRUGO, proc_mounts_operations),
2893 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2894 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2895 #ifdef CONFIG_PROC_PAGE_MONITOR
2896 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2897 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2898 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2899 #endif
2900 #ifdef CONFIG_SECURITY
2901 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2902 #endif
2903 #ifdef CONFIG_KALLSYMS
2904 ONE("wchan", S_IRUGO, proc_pid_wchan),
2905 #endif
2906 #ifdef CONFIG_STACKTRACE
2907 ONE("stack", S_IRUSR, proc_pid_stack),
2908 #endif
2909 #ifdef CONFIG_SCHED_INFO
2910 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2911 #endif
2912 #ifdef CONFIG_LATENCYTOP
2913 REG("latency", S_IRUGO, proc_lstats_operations),
2914 #endif
2915 #ifdef CONFIG_PROC_PID_CPUSET
2916 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2917 #endif
2918 #ifdef CONFIG_CGROUPS
2919 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2920 #endif
2921 ONE("oom_score", S_IRUGO, proc_oom_score),
2922 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2923 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2924 #ifdef CONFIG_AUDITSYSCALL
2925 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2926 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2927 #endif
2928 #ifdef CONFIG_FAULT_INJECTION
2929 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2930 #endif
2931 #ifdef CONFIG_ELF_CORE
2932 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2933 #endif
2934 #ifdef CONFIG_TASK_IO_ACCOUNTING
2935 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2936 #endif
2937 #ifdef CONFIG_HARDWALL
2938 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2939 #endif
2940 #ifdef CONFIG_USER_NS
2941 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2942 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2943 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2944 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2945 #endif
2946 #ifdef CONFIG_CHECKPOINT_RESTORE
2947 REG("timers", S_IRUGO, proc_timers_operations),
2948 #endif
2949 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
2950 };
2951
2952 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2953 {
2954 return proc_pident_readdir(file, ctx,
2955 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2956 }
2957
2958 static const struct file_operations proc_tgid_base_operations = {
2959 .read = generic_read_dir,
2960 .iterate_shared = proc_tgid_base_readdir,
2961 .llseek = generic_file_llseek,
2962 };
2963
2964 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2965 {
2966 return proc_pident_lookup(dir, dentry,
2967 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2968 }
2969
2970 static const struct inode_operations proc_tgid_base_inode_operations = {
2971 .lookup = proc_tgid_base_lookup,
2972 .getattr = pid_getattr,
2973 .setattr = proc_setattr,
2974 .permission = proc_pid_permission,
2975 };
2976
2977 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2978 {
2979 struct dentry *dentry, *leader, *dir;
2980 char buf[PROC_NUMBUF];
2981 struct qstr name;
2982
2983 name.name = buf;
2984 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2985 /* no ->d_hash() rejects on procfs */
2986 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2987 if (dentry) {
2988 d_invalidate(dentry);
2989 dput(dentry);
2990 }
2991
2992 if (pid == tgid)
2993 return;
2994
2995 name.name = buf;
2996 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2997 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2998 if (!leader)
2999 goto out;
3000
3001 name.name = "task";
3002 name.len = strlen(name.name);
3003 dir = d_hash_and_lookup(leader, &name);
3004 if (!dir)
3005 goto out_put_leader;
3006
3007 name.name = buf;
3008 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3009 dentry = d_hash_and_lookup(dir, &name);
3010 if (dentry) {
3011 d_invalidate(dentry);
3012 dput(dentry);
3013 }
3014
3015 dput(dir);
3016 out_put_leader:
3017 dput(leader);
3018 out:
3019 return;
3020 }
3021
3022 /**
3023 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3024 * @task: task that should be flushed.
3025 *
3026 * When flushing dentries from proc, one needs to flush them from global
3027 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3028 * in. This call is supposed to do all of this job.
3029 *
3030 * Looks in the dcache for
3031 * /proc/@pid
3032 * /proc/@tgid/task/@pid
3033 * if either directory is present flushes it and all of it'ts children
3034 * from the dcache.
3035 *
3036 * It is safe and reasonable to cache /proc entries for a task until
3037 * that task exits. After that they just clog up the dcache with
3038 * useless entries, possibly causing useful dcache entries to be
3039 * flushed instead. This routine is proved to flush those useless
3040 * dcache entries at process exit time.
3041 *
3042 * NOTE: This routine is just an optimization so it does not guarantee
3043 * that no dcache entries will exist at process exit time it
3044 * just makes it very unlikely that any will persist.
3045 */
3046
3047 void proc_flush_task(struct task_struct *task)
3048 {
3049 int i;
3050 struct pid *pid, *tgid;
3051 struct upid *upid;
3052
3053 pid = task_pid(task);
3054 tgid = task_tgid(task);
3055
3056 for (i = 0; i <= pid->level; i++) {
3057 upid = &pid->numbers[i];
3058 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3059 tgid->numbers[i].nr);
3060 }
3061 }
3062
3063 static int proc_pid_instantiate(struct inode *dir,
3064 struct dentry * dentry,
3065 struct task_struct *task, const void *ptr)
3066 {
3067 struct inode *inode;
3068
3069 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3070 if (!inode)
3071 goto out;
3072
3073 inode->i_op = &proc_tgid_base_inode_operations;
3074 inode->i_fop = &proc_tgid_base_operations;
3075 inode->i_flags|=S_IMMUTABLE;
3076
3077 set_nlink(inode, nlink_tgid);
3078
3079 d_set_d_op(dentry, &pid_dentry_operations);
3080
3081 d_add(dentry, inode);
3082 /* Close the race of the process dying before we return the dentry */
3083 if (pid_revalidate(dentry, 0))
3084 return 0;
3085 out:
3086 return -ENOENT;
3087 }
3088
3089 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3090 {
3091 int result = -ENOENT;
3092 struct task_struct *task;
3093 unsigned tgid;
3094 struct pid_namespace *ns;
3095
3096 tgid = name_to_int(&dentry->d_name);
3097 if (tgid == ~0U)
3098 goto out;
3099
3100 ns = dentry->d_sb->s_fs_info;
3101 rcu_read_lock();
3102 task = find_task_by_pid_ns(tgid, ns);
3103 if (task)
3104 get_task_struct(task);
3105 rcu_read_unlock();
3106 if (!task)
3107 goto out;
3108
3109 result = proc_pid_instantiate(dir, dentry, task, NULL);
3110 put_task_struct(task);
3111 out:
3112 return ERR_PTR(result);
3113 }
3114
3115 /*
3116 * Find the first task with tgid >= tgid
3117 *
3118 */
3119 struct tgid_iter {
3120 unsigned int tgid;
3121 struct task_struct *task;
3122 };
3123 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3124 {
3125 struct pid *pid;
3126
3127 if (iter.task)
3128 put_task_struct(iter.task);
3129 rcu_read_lock();
3130 retry:
3131 iter.task = NULL;
3132 pid = find_ge_pid(iter.tgid, ns);
3133 if (pid) {
3134 iter.tgid = pid_nr_ns(pid, ns);
3135 iter.task = pid_task(pid, PIDTYPE_PID);
3136 /* What we to know is if the pid we have find is the
3137 * pid of a thread_group_leader. Testing for task
3138 * being a thread_group_leader is the obvious thing
3139 * todo but there is a window when it fails, due to
3140 * the pid transfer logic in de_thread.
3141 *
3142 * So we perform the straight forward test of seeing
3143 * if the pid we have found is the pid of a thread
3144 * group leader, and don't worry if the task we have
3145 * found doesn't happen to be a thread group leader.
3146 * As we don't care in the case of readdir.
3147 */
3148 if (!iter.task || !has_group_leader_pid(iter.task)) {
3149 iter.tgid += 1;
3150 goto retry;
3151 }
3152 get_task_struct(iter.task);
3153 }
3154 rcu_read_unlock();
3155 return iter;
3156 }
3157
3158 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3159
3160 /* for the /proc/ directory itself, after non-process stuff has been done */
3161 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3162 {
3163 struct tgid_iter iter;
3164 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3165 loff_t pos = ctx->pos;
3166
3167 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3168 return 0;
3169
3170 if (pos == TGID_OFFSET - 2) {
3171 struct inode *inode = d_inode(ns->proc_self);
3172 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3173 return 0;
3174 ctx->pos = pos = pos + 1;
3175 }
3176 if (pos == TGID_OFFSET - 1) {
3177 struct inode *inode = d_inode(ns->proc_thread_self);
3178 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3179 return 0;
3180 ctx->pos = pos = pos + 1;
3181 }
3182 iter.tgid = pos - TGID_OFFSET;
3183 iter.task = NULL;
3184 for (iter = next_tgid(ns, iter);
3185 iter.task;
3186 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3187 char name[PROC_NUMBUF];
3188 int len;
3189
3190 cond_resched();
3191 if (!has_pid_permissions(ns, iter.task, 2))
3192 continue;
3193
3194 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3195 ctx->pos = iter.tgid + TGID_OFFSET;
3196 if (!proc_fill_cache(file, ctx, name, len,
3197 proc_pid_instantiate, iter.task, NULL)) {
3198 put_task_struct(iter.task);
3199 return 0;
3200 }
3201 }
3202 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3203 return 0;
3204 }
3205
3206 /*
3207 * proc_tid_comm_permission is a special permission function exclusively
3208 * used for the node /proc/<pid>/task/<tid>/comm.
3209 * It bypasses generic permission checks in the case where a task of the same
3210 * task group attempts to access the node.
3211 * The rationale behind this is that glibc and bionic access this node for
3212 * cross thread naming (pthread_set/getname_np(!self)). However, if
3213 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3214 * which locks out the cross thread naming implementation.
3215 * This function makes sure that the node is always accessible for members of
3216 * same thread group.
3217 */
3218 static int proc_tid_comm_permission(struct inode *inode, int mask)
3219 {
3220 bool is_same_tgroup;
3221 struct task_struct *task;
3222
3223 task = get_proc_task(inode);
3224 if (!task)
3225 return -ESRCH;
3226 is_same_tgroup = same_thread_group(current, task);
3227 put_task_struct(task);
3228
3229 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3230 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3231 * read or written by the members of the corresponding
3232 * thread group.
3233 */
3234 return 0;
3235 }
3236
3237 return generic_permission(inode, mask);
3238 }
3239
3240 static const struct inode_operations proc_tid_comm_inode_operations = {
3241 .permission = proc_tid_comm_permission,
3242 };
3243
3244 /*
3245 * Tasks
3246 */
3247 static const struct pid_entry tid_base_stuff[] = {
3248 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3249 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3250 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3251 #ifdef CONFIG_NET
3252 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3253 #endif
3254 REG("environ", S_IRUSR, proc_environ_operations),
3255 REG("auxv", S_IRUSR, proc_auxv_operations),
3256 ONE("status", S_IRUGO, proc_pid_status),
3257 ONE("personality", S_IRUSR, proc_pid_personality),
3258 ONE("limits", S_IRUGO, proc_pid_limits),
3259 #ifdef CONFIG_SCHED_DEBUG
3260 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3261 #endif
3262 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3263 &proc_tid_comm_inode_operations,
3264 &proc_pid_set_comm_operations, {}),
3265 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3266 ONE("syscall", S_IRUSR, proc_pid_syscall),
3267 #endif
3268 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3269 ONE("stat", S_IRUGO, proc_tid_stat),
3270 ONE("statm", S_IRUGO, proc_pid_statm),
3271 REG("maps", S_IRUGO, proc_tid_maps_operations),
3272 #ifdef CONFIG_PROC_CHILDREN
3273 REG("children", S_IRUGO, proc_tid_children_operations),
3274 #endif
3275 #ifdef CONFIG_NUMA
3276 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3277 #endif
3278 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3279 LNK("cwd", proc_cwd_link),
3280 LNK("root", proc_root_link),
3281 LNK("exe", proc_exe_link),
3282 REG("mounts", S_IRUGO, proc_mounts_operations),
3283 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3284 #ifdef CONFIG_PROC_PAGE_MONITOR
3285 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3286 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3287 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3288 #endif
3289 #ifdef CONFIG_SECURITY
3290 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3291 #endif
3292 #ifdef CONFIG_KALLSYMS
3293 ONE("wchan", S_IRUGO, proc_pid_wchan),
3294 #endif
3295 #ifdef CONFIG_STACKTRACE
3296 ONE("stack", S_IRUSR, proc_pid_stack),
3297 #endif
3298 #ifdef CONFIG_SCHED_INFO
3299 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3300 #endif
3301 #ifdef CONFIG_LATENCYTOP
3302 REG("latency", S_IRUGO, proc_lstats_operations),
3303 #endif
3304 #ifdef CONFIG_PROC_PID_CPUSET
3305 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3306 #endif
3307 #ifdef CONFIG_CGROUPS
3308 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3309 #endif
3310 ONE("oom_score", S_IRUGO, proc_oom_score),
3311 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3312 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3313 #ifdef CONFIG_AUDITSYSCALL
3314 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3315 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3316 #endif
3317 #ifdef CONFIG_FAULT_INJECTION
3318 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3319 #endif
3320 #ifdef CONFIG_TASK_IO_ACCOUNTING
3321 ONE("io", S_IRUSR, proc_tid_io_accounting),
3322 #endif
3323 #ifdef CONFIG_HARDWALL
3324 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3325 #endif
3326 #ifdef CONFIG_USER_NS
3327 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3328 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3329 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3330 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3331 #endif
3332 };
3333
3334 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3335 {
3336 return proc_pident_readdir(file, ctx,
3337 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3338 }
3339
3340 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3341 {
3342 return proc_pident_lookup(dir, dentry,
3343 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3344 }
3345
3346 static const struct file_operations proc_tid_base_operations = {
3347 .read = generic_read_dir,
3348 .iterate_shared = proc_tid_base_readdir,
3349 .llseek = generic_file_llseek,
3350 };
3351
3352 static const struct inode_operations proc_tid_base_inode_operations = {
3353 .lookup = proc_tid_base_lookup,
3354 .getattr = pid_getattr,
3355 .setattr = proc_setattr,
3356 };
3357
3358 static int proc_task_instantiate(struct inode *dir,
3359 struct dentry *dentry, struct task_struct *task, const void *ptr)
3360 {
3361 struct inode *inode;
3362 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3363
3364 if (!inode)
3365 goto out;
3366 inode->i_op = &proc_tid_base_inode_operations;
3367 inode->i_fop = &proc_tid_base_operations;
3368 inode->i_flags|=S_IMMUTABLE;
3369
3370 set_nlink(inode, nlink_tid);
3371
3372 d_set_d_op(dentry, &pid_dentry_operations);
3373
3374 d_add(dentry, inode);
3375 /* Close the race of the process dying before we return the dentry */
3376 if (pid_revalidate(dentry, 0))
3377 return 0;
3378 out:
3379 return -ENOENT;
3380 }
3381
3382 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3383 {
3384 int result = -ENOENT;
3385 struct task_struct *task;
3386 struct task_struct *leader = get_proc_task(dir);
3387 unsigned tid;
3388 struct pid_namespace *ns;
3389
3390 if (!leader)
3391 goto out_no_task;
3392
3393 tid = name_to_int(&dentry->d_name);
3394 if (tid == ~0U)
3395 goto out;
3396
3397 ns = dentry->d_sb->s_fs_info;
3398 rcu_read_lock();
3399 task = find_task_by_pid_ns(tid, ns);
3400 if (task)
3401 get_task_struct(task);
3402 rcu_read_unlock();
3403 if (!task)
3404 goto out;
3405 if (!same_thread_group(leader, task))
3406 goto out_drop_task;
3407
3408 result = proc_task_instantiate(dir, dentry, task, NULL);
3409 out_drop_task:
3410 put_task_struct(task);
3411 out:
3412 put_task_struct(leader);
3413 out_no_task:
3414 return ERR_PTR(result);
3415 }
3416
3417 /*
3418 * Find the first tid of a thread group to return to user space.
3419 *
3420 * Usually this is just the thread group leader, but if the users
3421 * buffer was too small or there was a seek into the middle of the
3422 * directory we have more work todo.
3423 *
3424 * In the case of a short read we start with find_task_by_pid.
3425 *
3426 * In the case of a seek we start with the leader and walk nr
3427 * threads past it.
3428 */
3429 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3430 struct pid_namespace *ns)
3431 {
3432 struct task_struct *pos, *task;
3433 unsigned long nr = f_pos;
3434
3435 if (nr != f_pos) /* 32bit overflow? */
3436 return NULL;
3437
3438 rcu_read_lock();
3439 task = pid_task(pid, PIDTYPE_PID);
3440 if (!task)
3441 goto fail;
3442
3443 /* Attempt to start with the tid of a thread */
3444 if (tid && nr) {
3445 pos = find_task_by_pid_ns(tid, ns);
3446 if (pos && same_thread_group(pos, task))
3447 goto found;
3448 }
3449
3450 /* If nr exceeds the number of threads there is nothing todo */
3451 if (nr >= get_nr_threads(task))
3452 goto fail;
3453
3454 /* If we haven't found our starting place yet start
3455 * with the leader and walk nr threads forward.
3456 */
3457 pos = task = task->group_leader;
3458 do {
3459 if (!nr--)
3460 goto found;
3461 } while_each_thread(task, pos);
3462 fail:
3463 pos = NULL;
3464 goto out;
3465 found:
3466 get_task_struct(pos);
3467 out:
3468 rcu_read_unlock();
3469 return pos;
3470 }
3471
3472 /*
3473 * Find the next thread in the thread list.
3474 * Return NULL if there is an error or no next thread.
3475 *
3476 * The reference to the input task_struct is released.
3477 */
3478 static struct task_struct *next_tid(struct task_struct *start)
3479 {
3480 struct task_struct *pos = NULL;
3481 rcu_read_lock();
3482 if (pid_alive(start)) {
3483 pos = next_thread(start);
3484 if (thread_group_leader(pos))
3485 pos = NULL;
3486 else
3487 get_task_struct(pos);
3488 }
3489 rcu_read_unlock();
3490 put_task_struct(start);
3491 return pos;
3492 }
3493
3494 /* for the /proc/TGID/task/ directories */
3495 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3496 {
3497 struct inode *inode = file_inode(file);
3498 struct task_struct *task;
3499 struct pid_namespace *ns;
3500 int tid;
3501
3502 if (proc_inode_is_dead(inode))
3503 return -ENOENT;
3504
3505 if (!dir_emit_dots(file, ctx))
3506 return 0;
3507
3508 /* f_version caches the tgid value that the last readdir call couldn't
3509 * return. lseek aka telldir automagically resets f_version to 0.
3510 */
3511 ns = inode->i_sb->s_fs_info;
3512 tid = (int)file->f_version;
3513 file->f_version = 0;
3514 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3515 task;
3516 task = next_tid(task), ctx->pos++) {
3517 char name[PROC_NUMBUF];
3518 int len;
3519 tid = task_pid_nr_ns(task, ns);
3520 len = snprintf(name, sizeof(name), "%d", tid);
3521 if (!proc_fill_cache(file, ctx, name, len,
3522 proc_task_instantiate, task, NULL)) {
3523 /* returning this tgid failed, save it as the first
3524 * pid for the next readir call */
3525 file->f_version = (u64)tid;
3526 put_task_struct(task);
3527 break;
3528 }
3529 }
3530
3531 return 0;
3532 }
3533
3534 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3535 {
3536 struct inode *inode = d_inode(dentry);
3537 struct task_struct *p = get_proc_task(inode);
3538 generic_fillattr(inode, stat);
3539
3540 if (p) {
3541 stat->nlink += get_nr_threads(p);
3542 put_task_struct(p);
3543 }
3544
3545 return 0;
3546 }
3547
3548 static const struct inode_operations proc_task_inode_operations = {
3549 .lookup = proc_task_lookup,
3550 .getattr = proc_task_getattr,
3551 .setattr = proc_setattr,
3552 .permission = proc_pid_permission,
3553 };
3554
3555 static const struct file_operations proc_task_operations = {
3556 .read = generic_read_dir,
3557 .iterate_shared = proc_task_readdir,
3558 .llseek = generic_file_llseek,
3559 };
3560
3561 void __init set_proc_pid_nlink(void)
3562 {
3563 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3564 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3565 }