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