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