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