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