proc: maps protection
[safe/jmp/linux-2.6] / 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/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include "internal.h"
78
79 /* NOTE:
80  *      Implementing inode permission operations in /proc is almost
81  *      certainly an error.  Permission checks need to happen during
82  *      each system call not at open time.  The reason is that most of
83  *      what we wish to check for permissions in /proc varies at runtime.
84  *
85  *      The classic example of a problem is opening file descriptors
86  *      in /proc for a task before it execs a suid executable.
87  */
88
89
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
92
93 struct pid_entry {
94         int len;
95         char *name;
96         mode_t mode;
97         const struct inode_operations *iop;
98         const struct file_operations *fop;
99         union proc_op op;
100 };
101
102 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
103         .len  = sizeof(NAME) - 1,                       \
104         .name = (NAME),                                 \
105         .mode = MODE,                                   \
106         .iop  = IOP,                                    \
107         .fop  = FOP,                                    \
108         .op   = OP,                                     \
109 }
110
111 #define DIR(NAME, MODE, OTYPE)                                                  \
112         NOD(NAME, (S_IFDIR|(MODE)),                                             \
113                 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,   \
114                 {} )
115 #define LNK(NAME, OTYPE)                                        \
116         NOD(NAME, (S_IFLNK|S_IRWXUGO),                          \
117                 &proc_pid_link_inode_operations, NULL,          \
118                 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE)                          \
120         NOD(NAME, (S_IFREG|(MODE)), NULL,               \
121                 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE)                          \
123         NOD(NAME, (S_IFREG|(MODE)),                     \
124                 NULL, &proc_info_file_operations,       \
125                 { .proc_read = &proc_##OTYPE } )
126
127 int maps_protect;
128 EXPORT_SYMBOL(maps_protect);
129
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 {
132         struct fs_struct *fs;
133         task_lock(task);
134         fs = task->fs;
135         if(fs)
136                 atomic_inc(&fs->count);
137         task_unlock(task);
138         return fs;
139 }
140
141 static int get_nr_threads(struct task_struct *tsk)
142 {
143         /* Must be called with the rcu_read_lock held */
144         unsigned long flags;
145         int count = 0;
146
147         if (lock_task_sighand(tsk, &flags)) {
148                 count = atomic_read(&tsk->signal->count);
149                 unlock_task_sighand(tsk, &flags);
150         }
151         return count;
152 }
153
154 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 {
156         struct task_struct *task = get_proc_task(inode);
157         struct fs_struct *fs = NULL;
158         int result = -ENOENT;
159
160         if (task) {
161                 fs = get_fs_struct(task);
162                 put_task_struct(task);
163         }
164         if (fs) {
165                 read_lock(&fs->lock);
166                 *mnt = mntget(fs->pwdmnt);
167                 *dentry = dget(fs->pwd);
168                 read_unlock(&fs->lock);
169                 result = 0;
170                 put_fs_struct(fs);
171         }
172         return result;
173 }
174
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 {
177         struct task_struct *task = get_proc_task(inode);
178         struct fs_struct *fs = NULL;
179         int result = -ENOENT;
180
181         if (task) {
182                 fs = get_fs_struct(task);
183                 put_task_struct(task);
184         }
185         if (fs) {
186                 read_lock(&fs->lock);
187                 *mnt = mntget(fs->rootmnt);
188                 *dentry = dget(fs->root);
189                 read_unlock(&fs->lock);
190                 result = 0;
191                 put_fs_struct(fs);
192         }
193         return result;
194 }
195
196 #define MAY_PTRACE(task) \
197         (task == current || \
198         (task->parent == current && \
199         (task->ptrace & PT_PTRACED) && \
200          (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201          security_ptrace(current,task) == 0))
202
203 static int proc_pid_environ(struct task_struct *task, char * buffer)
204 {
205         int res = 0;
206         struct mm_struct *mm = get_task_mm(task);
207         if (mm) {
208                 unsigned int len = mm->env_end - mm->env_start;
209                 if (len > PAGE_SIZE)
210                         len = PAGE_SIZE;
211                 res = access_process_vm(task, mm->env_start, buffer, len, 0);
212                 if (!ptrace_may_attach(task))
213                         res = -ESRCH;
214                 mmput(mm);
215         }
216         return res;
217 }
218
219 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
220 {
221         int res = 0;
222         unsigned int len;
223         struct mm_struct *mm = get_task_mm(task);
224         if (!mm)
225                 goto out;
226         if (!mm->arg_end)
227                 goto out_mm;    /* Shh! No looking before we're done */
228
229         len = mm->arg_end - mm->arg_start;
230  
231         if (len > PAGE_SIZE)
232                 len = PAGE_SIZE;
233  
234         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235
236         // If the nul at the end of args has been overwritten, then
237         // assume application is using setproctitle(3).
238         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239                 len = strnlen(buffer, res);
240                 if (len < res) {
241                     res = len;
242                 } else {
243                         len = mm->env_end - mm->env_start;
244                         if (len > PAGE_SIZE - res)
245                                 len = PAGE_SIZE - res;
246                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247                         res = strnlen(buffer, res);
248                 }
249         }
250 out_mm:
251         mmput(mm);
252 out:
253         return res;
254 }
255
256 static int proc_pid_auxv(struct task_struct *task, char *buffer)
257 {
258         int res = 0;
259         struct mm_struct *mm = get_task_mm(task);
260         if (mm) {
261                 unsigned int nwords = 0;
262                 do
263                         nwords += 2;
264                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265                 res = nwords * sizeof(mm->saved_auxv[0]);
266                 if (res > PAGE_SIZE)
267                         res = PAGE_SIZE;
268                 memcpy(buffer, mm->saved_auxv, res);
269                 mmput(mm);
270         }
271         return res;
272 }
273
274
275 #ifdef CONFIG_KALLSYMS
276 /*
277  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278  * Returns the resolved symbol.  If that fails, simply return the address.
279  */
280 static int proc_pid_wchan(struct task_struct *task, char *buffer)
281 {
282         char *modname;
283         const char *sym_name;
284         unsigned long wchan, size, offset;
285         char namebuf[KSYM_NAME_LEN+1];
286
287         wchan = get_wchan(task);
288
289         sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
290         if (sym_name)
291                 return sprintf(buffer, "%s", sym_name);
292         return sprintf(buffer, "%lu", wchan);
293 }
294 #endif /* CONFIG_KALLSYMS */
295
296 #ifdef CONFIG_SCHEDSTATS
297 /*
298  * Provides /proc/PID/schedstat
299  */
300 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
301 {
302         return sprintf(buffer, "%lu %lu %lu\n",
303                         task->sched_info.cpu_time,
304                         task->sched_info.run_delay,
305                         task->sched_info.pcnt);
306 }
307 #endif
308
309 /* The badness from the OOM killer */
310 unsigned long badness(struct task_struct *p, unsigned long uptime);
311 static int proc_oom_score(struct task_struct *task, char *buffer)
312 {
313         unsigned long points;
314         struct timespec uptime;
315
316         do_posix_clock_monotonic_gettime(&uptime);
317         points = badness(task, uptime.tv_sec);
318         return sprintf(buffer, "%lu\n", points);
319 }
320
321 /************************************************************************/
322 /*                       Here the fs part begins                        */
323 /************************************************************************/
324
325 /* permission checks */
326 static int proc_fd_access_allowed(struct inode *inode)
327 {
328         struct task_struct *task;
329         int allowed = 0;
330         /* Allow access to a task's file descriptors if it is us or we
331          * may use ptrace attach to the process and find out that
332          * information.
333          */
334         task = get_proc_task(inode);
335         if (task) {
336                 allowed = ptrace_may_attach(task);
337                 put_task_struct(task);
338         }
339         return allowed;
340 }
341
342 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
343 {
344         int error;
345         struct inode *inode = dentry->d_inode;
346
347         if (attr->ia_valid & ATTR_MODE)
348                 return -EPERM;
349
350         error = inode_change_ok(inode, attr);
351         if (!error) {
352                 error = security_inode_setattr(dentry, attr);
353                 if (!error)
354                         error = inode_setattr(inode, attr);
355         }
356         return error;
357 }
358
359 static const struct inode_operations proc_def_inode_operations = {
360         .setattr        = proc_setattr,
361 };
362
363 extern struct seq_operations mounts_op;
364 struct proc_mounts {
365         struct seq_file m;
366         int event;
367 };
368
369 static int mounts_open(struct inode *inode, struct file *file)
370 {
371         struct task_struct *task = get_proc_task(inode);
372         struct mnt_namespace *ns = NULL;
373         struct proc_mounts *p;
374         int ret = -EINVAL;
375
376         if (task) {
377                 task_lock(task);
378                 if (task->nsproxy) {
379                         ns = task->nsproxy->mnt_ns;
380                         if (ns)
381                                 get_mnt_ns(ns);
382                 }
383                 task_unlock(task);
384                 put_task_struct(task);
385         }
386
387         if (ns) {
388                 ret = -ENOMEM;
389                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
390                 if (p) {
391                         file->private_data = &p->m;
392                         ret = seq_open(file, &mounts_op);
393                         if (!ret) {
394                                 p->m.private = ns;
395                                 p->event = ns->event;
396                                 return 0;
397                         }
398                         kfree(p);
399                 }
400                 put_mnt_ns(ns);
401         }
402         return ret;
403 }
404
405 static int mounts_release(struct inode *inode, struct file *file)
406 {
407         struct seq_file *m = file->private_data;
408         struct mnt_namespace *ns = m->private;
409         put_mnt_ns(ns);
410         return seq_release(inode, file);
411 }
412
413 static unsigned mounts_poll(struct file *file, poll_table *wait)
414 {
415         struct proc_mounts *p = file->private_data;
416         struct mnt_namespace *ns = p->m.private;
417         unsigned res = 0;
418
419         poll_wait(file, &ns->poll, wait);
420
421         spin_lock(&vfsmount_lock);
422         if (p->event != ns->event) {
423                 p->event = ns->event;
424                 res = POLLERR;
425         }
426         spin_unlock(&vfsmount_lock);
427
428         return res;
429 }
430
431 static const struct file_operations proc_mounts_operations = {
432         .open           = mounts_open,
433         .read           = seq_read,
434         .llseek         = seq_lseek,
435         .release        = mounts_release,
436         .poll           = mounts_poll,
437 };
438
439 extern struct seq_operations mountstats_op;
440 static int mountstats_open(struct inode *inode, struct file *file)
441 {
442         int ret = seq_open(file, &mountstats_op);
443
444         if (!ret) {
445                 struct seq_file *m = file->private_data;
446                 struct mnt_namespace *mnt_ns = NULL;
447                 struct task_struct *task = get_proc_task(inode);
448
449                 if (task) {
450                         task_lock(task);
451                         if (task->nsproxy)
452                                 mnt_ns = task->nsproxy->mnt_ns;
453                         if (mnt_ns)
454                                 get_mnt_ns(mnt_ns);
455                         task_unlock(task);
456                         put_task_struct(task);
457                 }
458
459                 if (mnt_ns)
460                         m->private = mnt_ns;
461                 else {
462                         seq_release(inode, file);
463                         ret = -EINVAL;
464                 }
465         }
466         return ret;
467 }
468
469 static const struct file_operations proc_mountstats_operations = {
470         .open           = mountstats_open,
471         .read           = seq_read,
472         .llseek         = seq_lseek,
473         .release        = mounts_release,
474 };
475
476 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
477
478 static ssize_t proc_info_read(struct file * file, char __user * buf,
479                           size_t count, loff_t *ppos)
480 {
481         struct inode * inode = file->f_path.dentry->d_inode;
482         unsigned long page;
483         ssize_t length;
484         struct task_struct *task = get_proc_task(inode);
485
486         length = -ESRCH;
487         if (!task)
488                 goto out_no_task;
489
490         if (count > PROC_BLOCK_SIZE)
491                 count = PROC_BLOCK_SIZE;
492
493         length = -ENOMEM;
494         if (!(page = __get_free_page(GFP_KERNEL)))
495                 goto out;
496
497         length = PROC_I(inode)->op.proc_read(task, (char*)page);
498
499         if (length >= 0)
500                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
501         free_page(page);
502 out:
503         put_task_struct(task);
504 out_no_task:
505         return length;
506 }
507
508 static const struct file_operations proc_info_file_operations = {
509         .read           = proc_info_read,
510 };
511
512 static int mem_open(struct inode* inode, struct file* file)
513 {
514         file->private_data = (void*)((long)current->self_exec_id);
515         return 0;
516 }
517
518 static ssize_t mem_read(struct file * file, char __user * buf,
519                         size_t count, loff_t *ppos)
520 {
521         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
522         char *page;
523         unsigned long src = *ppos;
524         int ret = -ESRCH;
525         struct mm_struct *mm;
526
527         if (!task)
528                 goto out_no_task;
529
530         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
531                 goto out;
532
533         ret = -ENOMEM;
534         page = (char *)__get_free_page(GFP_USER);
535         if (!page)
536                 goto out;
537
538         ret = 0;
539  
540         mm = get_task_mm(task);
541         if (!mm)
542                 goto out_free;
543
544         ret = -EIO;
545  
546         if (file->private_data != (void*)((long)current->self_exec_id))
547                 goto out_put;
548
549         ret = 0;
550  
551         while (count > 0) {
552                 int this_len, retval;
553
554                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
555                 retval = access_process_vm(task, src, page, this_len, 0);
556                 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
557                         if (!ret)
558                                 ret = -EIO;
559                         break;
560                 }
561
562                 if (copy_to_user(buf, page, retval)) {
563                         ret = -EFAULT;
564                         break;
565                 }
566  
567                 ret += retval;
568                 src += retval;
569                 buf += retval;
570                 count -= retval;
571         }
572         *ppos = src;
573
574 out_put:
575         mmput(mm);
576 out_free:
577         free_page((unsigned long) page);
578 out:
579         put_task_struct(task);
580 out_no_task:
581         return ret;
582 }
583
584 #define mem_write NULL
585
586 #ifndef mem_write
587 /* This is a security hazard */
588 static ssize_t mem_write(struct file * file, const char __user *buf,
589                          size_t count, loff_t *ppos)
590 {
591         int copied;
592         char *page;
593         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
594         unsigned long dst = *ppos;
595
596         copied = -ESRCH;
597         if (!task)
598                 goto out_no_task;
599
600         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
601                 goto out;
602
603         copied = -ENOMEM;
604         page = (char *)__get_free_page(GFP_USER);
605         if (!page)
606                 goto out;
607
608         copied = 0;
609         while (count > 0) {
610                 int this_len, retval;
611
612                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
613                 if (copy_from_user(page, buf, this_len)) {
614                         copied = -EFAULT;
615                         break;
616                 }
617                 retval = access_process_vm(task, dst, page, this_len, 1);
618                 if (!retval) {
619                         if (!copied)
620                                 copied = -EIO;
621                         break;
622                 }
623                 copied += retval;
624                 buf += retval;
625                 dst += retval;
626                 count -= retval;                        
627         }
628         *ppos = dst;
629         free_page((unsigned long) page);
630 out:
631         put_task_struct(task);
632 out_no_task:
633         return copied;
634 }
635 #endif
636
637 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
638 {
639         switch (orig) {
640         case 0:
641                 file->f_pos = offset;
642                 break;
643         case 1:
644                 file->f_pos += offset;
645                 break;
646         default:
647                 return -EINVAL;
648         }
649         force_successful_syscall_return();
650         return file->f_pos;
651 }
652
653 static const struct file_operations proc_mem_operations = {
654         .llseek         = mem_lseek,
655         .read           = mem_read,
656         .write          = mem_write,
657         .open           = mem_open,
658 };
659
660 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
661                                 size_t count, loff_t *ppos)
662 {
663         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
664         char buffer[PROC_NUMBUF];
665         size_t len;
666         int oom_adjust;
667         loff_t __ppos = *ppos;
668
669         if (!task)
670                 return -ESRCH;
671         oom_adjust = task->oomkilladj;
672         put_task_struct(task);
673
674         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
675         if (__ppos >= len)
676                 return 0;
677         if (count > len-__ppos)
678                 count = len-__ppos;
679         if (copy_to_user(buf, buffer + __ppos, count))
680                 return -EFAULT;
681         *ppos = __ppos + count;
682         return count;
683 }
684
685 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
686                                 size_t count, loff_t *ppos)
687 {
688         struct task_struct *task;
689         char buffer[PROC_NUMBUF], *end;
690         int oom_adjust;
691
692         memset(buffer, 0, sizeof(buffer));
693         if (count > sizeof(buffer) - 1)
694                 count = sizeof(buffer) - 1;
695         if (copy_from_user(buffer, buf, count))
696                 return -EFAULT;
697         oom_adjust = simple_strtol(buffer, &end, 0);
698         if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
699              oom_adjust != OOM_DISABLE)
700                 return -EINVAL;
701         if (*end == '\n')
702                 end++;
703         task = get_proc_task(file->f_path.dentry->d_inode);
704         if (!task)
705                 return -ESRCH;
706         if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
707                 put_task_struct(task);
708                 return -EACCES;
709         }
710         task->oomkilladj = oom_adjust;
711         put_task_struct(task);
712         if (end - buffer == 0)
713                 return -EIO;
714         return end - buffer;
715 }
716
717 static const struct file_operations proc_oom_adjust_operations = {
718         .read           = oom_adjust_read,
719         .write          = oom_adjust_write,
720 };
721
722 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
723                                 size_t count, loff_t *ppos)
724 {
725         struct task_struct *task;
726         char buffer[PROC_NUMBUF], *end;
727         struct mm_struct *mm;
728
729         memset(buffer, 0, sizeof(buffer));
730         if (count > sizeof(buffer) - 1)
731                 count = sizeof(buffer) - 1;
732         if (copy_from_user(buffer, buf, count))
733                 return -EFAULT;
734         if (!simple_strtol(buffer, &end, 0))
735                 return -EINVAL;
736         if (*end == '\n')
737                 end++;
738         task = get_proc_task(file->f_path.dentry->d_inode);
739         if (!task)
740                 return -ESRCH;
741         mm = get_task_mm(task);
742         if (mm) {
743                 clear_refs_smap(mm);
744                 mmput(mm);
745         }
746         put_task_struct(task);
747         if (end - buffer == 0)
748                 return -EIO;
749         return end - buffer;
750 }
751
752 static struct file_operations proc_clear_refs_operations = {
753         .write          = clear_refs_write,
754 };
755
756 #ifdef CONFIG_AUDITSYSCALL
757 #define TMPBUFLEN 21
758 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
759                                   size_t count, loff_t *ppos)
760 {
761         struct inode * inode = file->f_path.dentry->d_inode;
762         struct task_struct *task = get_proc_task(inode);
763         ssize_t length;
764         char tmpbuf[TMPBUFLEN];
765
766         if (!task)
767                 return -ESRCH;
768         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
769                                 audit_get_loginuid(task->audit_context));
770         put_task_struct(task);
771         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
772 }
773
774 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
775                                    size_t count, loff_t *ppos)
776 {
777         struct inode * inode = file->f_path.dentry->d_inode;
778         char *page, *tmp;
779         ssize_t length;
780         uid_t loginuid;
781
782         if (!capable(CAP_AUDIT_CONTROL))
783                 return -EPERM;
784
785         if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
786                 return -EPERM;
787
788         if (count >= PAGE_SIZE)
789                 count = PAGE_SIZE - 1;
790
791         if (*ppos != 0) {
792                 /* No partial writes. */
793                 return -EINVAL;
794         }
795         page = (char*)__get_free_page(GFP_USER);
796         if (!page)
797                 return -ENOMEM;
798         length = -EFAULT;
799         if (copy_from_user(page, buf, count))
800                 goto out_free_page;
801
802         page[count] = '\0';
803         loginuid = simple_strtoul(page, &tmp, 10);
804         if (tmp == page) {
805                 length = -EINVAL;
806                 goto out_free_page;
807
808         }
809         length = audit_set_loginuid(current, loginuid);
810         if (likely(length == 0))
811                 length = count;
812
813 out_free_page:
814         free_page((unsigned long) page);
815         return length;
816 }
817
818 static const struct file_operations proc_loginuid_operations = {
819         .read           = proc_loginuid_read,
820         .write          = proc_loginuid_write,
821 };
822 #endif
823
824 #ifdef CONFIG_SECCOMP
825 static ssize_t seccomp_read(struct file *file, char __user *buf,
826                             size_t count, loff_t *ppos)
827 {
828         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
829         char __buf[20];
830         loff_t __ppos = *ppos;
831         size_t len;
832
833         if (!tsk)
834                 return -ESRCH;
835         /* no need to print the trailing zero, so use only len */
836         len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
837         put_task_struct(tsk);
838         if (__ppos >= len)
839                 return 0;
840         if (count > len - __ppos)
841                 count = len - __ppos;
842         if (copy_to_user(buf, __buf + __ppos, count))
843                 return -EFAULT;
844         *ppos = __ppos + count;
845         return count;
846 }
847
848 static ssize_t seccomp_write(struct file *file, const char __user *buf,
849                              size_t count, loff_t *ppos)
850 {
851         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
852         char __buf[20], *end;
853         unsigned int seccomp_mode;
854         ssize_t result;
855
856         result = -ESRCH;
857         if (!tsk)
858                 goto out_no_task;
859
860         /* can set it only once to be even more secure */
861         result = -EPERM;
862         if (unlikely(tsk->seccomp.mode))
863                 goto out;
864
865         result = -EFAULT;
866         memset(__buf, 0, sizeof(__buf));
867         count = min(count, sizeof(__buf) - 1);
868         if (copy_from_user(__buf, buf, count))
869                 goto out;
870
871         seccomp_mode = simple_strtoul(__buf, &end, 0);
872         if (*end == '\n')
873                 end++;
874         result = -EINVAL;
875         if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
876                 tsk->seccomp.mode = seccomp_mode;
877                 set_tsk_thread_flag(tsk, TIF_SECCOMP);
878         } else
879                 goto out;
880         result = -EIO;
881         if (unlikely(!(end - __buf)))
882                 goto out;
883         result = end - __buf;
884 out:
885         put_task_struct(tsk);
886 out_no_task:
887         return result;
888 }
889
890 static const struct file_operations proc_seccomp_operations = {
891         .read           = seccomp_read,
892         .write          = seccomp_write,
893 };
894 #endif /* CONFIG_SECCOMP */
895
896 #ifdef CONFIG_FAULT_INJECTION
897 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
898                                       size_t count, loff_t *ppos)
899 {
900         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
901         char buffer[PROC_NUMBUF];
902         size_t len;
903         int make_it_fail;
904         loff_t __ppos = *ppos;
905
906         if (!task)
907                 return -ESRCH;
908         make_it_fail = task->make_it_fail;
909         put_task_struct(task);
910
911         len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
912         if (__ppos >= len)
913                 return 0;
914         if (count > len-__ppos)
915                 count = len-__ppos;
916         if (copy_to_user(buf, buffer + __ppos, count))
917                 return -EFAULT;
918         *ppos = __ppos + count;
919         return count;
920 }
921
922 static ssize_t proc_fault_inject_write(struct file * file,
923                         const char __user * buf, size_t count, loff_t *ppos)
924 {
925         struct task_struct *task;
926         char buffer[PROC_NUMBUF], *end;
927         int make_it_fail;
928
929         if (!capable(CAP_SYS_RESOURCE))
930                 return -EPERM;
931         memset(buffer, 0, sizeof(buffer));
932         if (count > sizeof(buffer) - 1)
933                 count = sizeof(buffer) - 1;
934         if (copy_from_user(buffer, buf, count))
935                 return -EFAULT;
936         make_it_fail = simple_strtol(buffer, &end, 0);
937         if (*end == '\n')
938                 end++;
939         task = get_proc_task(file->f_dentry->d_inode);
940         if (!task)
941                 return -ESRCH;
942         task->make_it_fail = make_it_fail;
943         put_task_struct(task);
944         if (end - buffer == 0)
945                 return -EIO;
946         return end - buffer;
947 }
948
949 static const struct file_operations proc_fault_inject_operations = {
950         .read           = proc_fault_inject_read,
951         .write          = proc_fault_inject_write,
952 };
953 #endif
954
955 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
956 {
957         struct inode *inode = dentry->d_inode;
958         int error = -EACCES;
959
960         /* We don't need a base pointer in the /proc filesystem */
961         path_release(nd);
962
963         /* Are we allowed to snoop on the tasks file descriptors? */
964         if (!proc_fd_access_allowed(inode))
965                 goto out;
966
967         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968         nd->last_type = LAST_BIND;
969 out:
970         return ERR_PTR(error);
971 }
972
973 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974                             char __user *buffer, int buflen)
975 {
976         struct inode * inode;
977         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
978         int len;
979
980         if (!tmp)
981                 return -ENOMEM;
982                 
983         inode = dentry->d_inode;
984         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
985         len = PTR_ERR(path);
986         if (IS_ERR(path))
987                 goto out;
988         len = tmp + PAGE_SIZE - 1 - path;
989
990         if (len > buflen)
991                 len = buflen;
992         if (copy_to_user(buffer, path, len))
993                 len = -EFAULT;
994  out:
995         free_page((unsigned long)tmp);
996         return len;
997 }
998
999 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000 {
1001         int error = -EACCES;
1002         struct inode *inode = dentry->d_inode;
1003         struct dentry *de;
1004         struct vfsmount *mnt = NULL;
1005
1006         /* Are we allowed to snoop on the tasks file descriptors? */
1007         if (!proc_fd_access_allowed(inode))
1008                 goto out;
1009
1010         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1011         if (error)
1012                 goto out;
1013
1014         error = do_proc_readlink(de, mnt, buffer, buflen);
1015         dput(de);
1016         mntput(mnt);
1017 out:
1018         return error;
1019 }
1020
1021 static const struct inode_operations proc_pid_link_inode_operations = {
1022         .readlink       = proc_pid_readlink,
1023         .follow_link    = proc_pid_follow_link,
1024         .setattr        = proc_setattr,
1025 };
1026
1027
1028 /* building an inode */
1029
1030 static int task_dumpable(struct task_struct *task)
1031 {
1032         int dumpable = 0;
1033         struct mm_struct *mm;
1034
1035         task_lock(task);
1036         mm = task->mm;
1037         if (mm)
1038                 dumpable = mm->dumpable;
1039         task_unlock(task);
1040         if(dumpable == 1)
1041                 return 1;
1042         return 0;
1043 }
1044
1045
1046 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1047 {
1048         struct inode * inode;
1049         struct proc_inode *ei;
1050
1051         /* We need a new inode */
1052
1053         inode = new_inode(sb);
1054         if (!inode)
1055                 goto out;
1056
1057         /* Common stuff */
1058         ei = PROC_I(inode);
1059         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060         inode->i_op = &proc_def_inode_operations;
1061
1062         /*
1063          * grab the reference to task.
1064          */
1065         ei->pid = get_task_pid(task, PIDTYPE_PID);
1066         if (!ei->pid)
1067                 goto out_unlock;
1068
1069         inode->i_uid = 0;
1070         inode->i_gid = 0;
1071         if (task_dumpable(task)) {
1072                 inode->i_uid = task->euid;
1073                 inode->i_gid = task->egid;
1074         }
1075         security_task_to_inode(task, inode);
1076
1077 out:
1078         return inode;
1079
1080 out_unlock:
1081         iput(inode);
1082         return NULL;
1083 }
1084
1085 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1086 {
1087         struct inode *inode = dentry->d_inode;
1088         struct task_struct *task;
1089         generic_fillattr(inode, stat);
1090
1091         rcu_read_lock();
1092         stat->uid = 0;
1093         stat->gid = 0;
1094         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1095         if (task) {
1096                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097                     task_dumpable(task)) {
1098                         stat->uid = task->euid;
1099                         stat->gid = task->egid;
1100                 }
1101         }
1102         rcu_read_unlock();
1103         return 0;
1104 }
1105
1106 /* dentry stuff */
1107
1108 /*
1109  *      Exceptional case: normally we are not allowed to unhash a busy
1110  * directory. In this case, however, we can do it - no aliasing problems
1111  * due to the way we treat inodes.
1112  *
1113  * Rewrite the inode's ownerships here because the owning task may have
1114  * performed a setuid(), etc.
1115  *
1116  * Before the /proc/pid/status file was created the only way to read
1117  * the effective uid of a /process was to stat /proc/pid.  Reading
1118  * /proc/pid/status is slow enough that procps and other packages
1119  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1120  * made this apply to all per process world readable and executable
1121  * directories.
1122  */
1123 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1124 {
1125         struct inode *inode = dentry->d_inode;
1126         struct task_struct *task = get_proc_task(inode);
1127         if (task) {
1128                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129                     task_dumpable(task)) {
1130                         inode->i_uid = task->euid;
1131                         inode->i_gid = task->egid;
1132                 } else {
1133                         inode->i_uid = 0;
1134                         inode->i_gid = 0;
1135                 }
1136                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137                 security_task_to_inode(task, inode);
1138                 put_task_struct(task);
1139                 return 1;
1140         }
1141         d_drop(dentry);
1142         return 0;
1143 }
1144
1145 static int pid_delete_dentry(struct dentry * dentry)
1146 {
1147         /* Is the task we represent dead?
1148          * If so, then don't put the dentry on the lru list,
1149          * kill it immediately.
1150          */
1151         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1152 }
1153
1154 static struct dentry_operations pid_dentry_operations =
1155 {
1156         .d_revalidate   = pid_revalidate,
1157         .d_delete       = pid_delete_dentry,
1158 };
1159
1160 /* Lookups */
1161
1162 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1163
1164 /*
1165  * Fill a directory entry.
1166  *
1167  * If possible create the dcache entry and derive our inode number and
1168  * file type from dcache entry.
1169  *
1170  * Since all of the proc inode numbers are dynamically generated, the inode
1171  * numbers do not exist until the inode is cache.  This means creating the
1172  * the dcache entry in readdir is necessary to keep the inode numbers
1173  * reported by readdir in sync with the inode numbers reported
1174  * by stat.
1175  */
1176 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1177         char *name, int len,
1178         instantiate_t instantiate, struct task_struct *task, void *ptr)
1179 {
1180         struct dentry *child, *dir = filp->f_path.dentry;
1181         struct inode *inode;
1182         struct qstr qname;
1183         ino_t ino = 0;
1184         unsigned type = DT_UNKNOWN;
1185
1186         qname.name = name;
1187         qname.len  = len;
1188         qname.hash = full_name_hash(name, len);
1189
1190         child = d_lookup(dir, &qname);
1191         if (!child) {
1192                 struct dentry *new;
1193                 new = d_alloc(dir, &qname);
1194                 if (new) {
1195                         child = instantiate(dir->d_inode, new, task, ptr);
1196                         if (child)
1197                                 dput(new);
1198                         else
1199                                 child = new;
1200                 }
1201         }
1202         if (!child || IS_ERR(child) || !child->d_inode)
1203                 goto end_instantiate;
1204         inode = child->d_inode;
1205         if (inode) {
1206                 ino = inode->i_ino;
1207                 type = inode->i_mode >> 12;
1208         }
1209         dput(child);
1210 end_instantiate:
1211         if (!ino)
1212                 ino = find_inode_number(dir, &qname);
1213         if (!ino)
1214                 ino = 1;
1215         return filldir(dirent, name, len, filp->f_pos, ino, type);
1216 }
1217
1218 static unsigned name_to_int(struct dentry *dentry)
1219 {
1220         const char *name = dentry->d_name.name;
1221         int len = dentry->d_name.len;
1222         unsigned n = 0;
1223
1224         if (len > 1 && *name == '0')
1225                 goto out;
1226         while (len-- > 0) {
1227                 unsigned c = *name++ - '0';
1228                 if (c > 9)
1229                         goto out;
1230                 if (n >= (~0U-9)/10)
1231                         goto out;
1232                 n *= 10;
1233                 n += c;
1234         }
1235         return n;
1236 out:
1237         return ~0U;
1238 }
1239
1240 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1241 {
1242         struct task_struct *task = get_proc_task(inode);
1243         struct files_struct *files = NULL;
1244         struct file *file;
1245         int fd = proc_fd(inode);
1246
1247         if (task) {
1248                 files = get_files_struct(task);
1249                 put_task_struct(task);
1250         }
1251         if (files) {
1252                 /*
1253                  * We are not taking a ref to the file structure, so we must
1254                  * hold ->file_lock.
1255                  */
1256                 spin_lock(&files->file_lock);
1257                 file = fcheck_files(files, fd);
1258                 if (file) {
1259                         *mnt = mntget(file->f_path.mnt);
1260                         *dentry = dget(file->f_path.dentry);
1261                         spin_unlock(&files->file_lock);
1262                         put_files_struct(files);
1263                         return 0;
1264                 }
1265                 spin_unlock(&files->file_lock);
1266                 put_files_struct(files);
1267         }
1268         return -ENOENT;
1269 }
1270
1271 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1272 {
1273         struct inode *inode = dentry->d_inode;
1274         struct task_struct *task = get_proc_task(inode);
1275         int fd = proc_fd(inode);
1276         struct files_struct *files;
1277
1278         if (task) {
1279                 files = get_files_struct(task);
1280                 if (files) {
1281                         rcu_read_lock();
1282                         if (fcheck_files(files, fd)) {
1283                                 rcu_read_unlock();
1284                                 put_files_struct(files);
1285                                 if (task_dumpable(task)) {
1286                                         inode->i_uid = task->euid;
1287                                         inode->i_gid = task->egid;
1288                                 } else {
1289                                         inode->i_uid = 0;
1290                                         inode->i_gid = 0;
1291                                 }
1292                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1293                                 security_task_to_inode(task, inode);
1294                                 put_task_struct(task);
1295                                 return 1;
1296                         }
1297                         rcu_read_unlock();
1298                         put_files_struct(files);
1299                 }
1300                 put_task_struct(task);
1301         }
1302         d_drop(dentry);
1303         return 0;
1304 }
1305
1306 static struct dentry_operations tid_fd_dentry_operations =
1307 {
1308         .d_revalidate   = tid_fd_revalidate,
1309         .d_delete       = pid_delete_dentry,
1310 };
1311
1312 static struct dentry *proc_fd_instantiate(struct inode *dir,
1313         struct dentry *dentry, struct task_struct *task, void *ptr)
1314 {
1315         unsigned fd = *(unsigned *)ptr;
1316         struct file *file;
1317         struct files_struct *files;
1318         struct inode *inode;
1319         struct proc_inode *ei;
1320         struct dentry *error = ERR_PTR(-ENOENT);
1321
1322         inode = proc_pid_make_inode(dir->i_sb, task);
1323         if (!inode)
1324                 goto out;
1325         ei = PROC_I(inode);
1326         ei->fd = fd;
1327         files = get_files_struct(task);
1328         if (!files)
1329                 goto out_iput;
1330         inode->i_mode = S_IFLNK;
1331
1332         /*
1333          * We are not taking a ref to the file structure, so we must
1334          * hold ->file_lock.
1335          */
1336         spin_lock(&files->file_lock);
1337         file = fcheck_files(files, fd);
1338         if (!file)
1339                 goto out_unlock;
1340         if (file->f_mode & 1)
1341                 inode->i_mode |= S_IRUSR | S_IXUSR;
1342         if (file->f_mode & 2)
1343                 inode->i_mode |= S_IWUSR | S_IXUSR;
1344         spin_unlock(&files->file_lock);
1345         put_files_struct(files);
1346
1347         inode->i_op = &proc_pid_link_inode_operations;
1348         inode->i_size = 64;
1349         ei->op.proc_get_link = proc_fd_link;
1350         dentry->d_op = &tid_fd_dentry_operations;
1351         d_add(dentry, inode);
1352         /* Close the race of the process dying before we return the dentry */
1353         if (tid_fd_revalidate(dentry, NULL))
1354                 error = NULL;
1355
1356  out:
1357         return error;
1358 out_unlock:
1359         spin_unlock(&files->file_lock);
1360         put_files_struct(files);
1361 out_iput:
1362         iput(inode);
1363         goto out;
1364 }
1365
1366 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1367 {
1368         struct task_struct *task = get_proc_task(dir);
1369         unsigned fd = name_to_int(dentry);
1370         struct dentry *result = ERR_PTR(-ENOENT);
1371
1372         if (!task)
1373                 goto out_no_task;
1374         if (fd == ~0U)
1375                 goto out;
1376
1377         result = proc_fd_instantiate(dir, dentry, task, &fd);
1378 out:
1379         put_task_struct(task);
1380 out_no_task:
1381         return result;
1382 }
1383
1384 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1385         struct task_struct *task, int fd)
1386 {
1387         char name[PROC_NUMBUF];
1388         int len = snprintf(name, sizeof(name), "%d", fd);
1389         return proc_fill_cache(filp, dirent, filldir, name, len,
1390                                 proc_fd_instantiate, task, &fd);
1391 }
1392
1393 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1394 {
1395         struct dentry *dentry = filp->f_path.dentry;
1396         struct inode *inode = dentry->d_inode;
1397         struct task_struct *p = get_proc_task(inode);
1398         unsigned int fd, tid, ino;
1399         int retval;
1400         struct files_struct * files;
1401         struct fdtable *fdt;
1402
1403         retval = -ENOENT;
1404         if (!p)
1405                 goto out_no_task;
1406         retval = 0;
1407         tid = p->pid;
1408
1409         fd = filp->f_pos;
1410         switch (fd) {
1411                 case 0:
1412                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1413                                 goto out;
1414                         filp->f_pos++;
1415                 case 1:
1416                         ino = parent_ino(dentry);
1417                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1418                                 goto out;
1419                         filp->f_pos++;
1420                 default:
1421                         files = get_files_struct(p);
1422                         if (!files)
1423                                 goto out;
1424                         rcu_read_lock();
1425                         fdt = files_fdtable(files);
1426                         for (fd = filp->f_pos-2;
1427                              fd < fdt->max_fds;
1428                              fd++, filp->f_pos++) {
1429
1430                                 if (!fcheck_files(files, fd))
1431                                         continue;
1432                                 rcu_read_unlock();
1433
1434                                 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1435                                         rcu_read_lock();
1436                                         break;
1437                                 }
1438                                 rcu_read_lock();
1439                         }
1440                         rcu_read_unlock();
1441                         put_files_struct(files);
1442         }
1443 out:
1444         put_task_struct(p);
1445 out_no_task:
1446         return retval;
1447 }
1448
1449 static const struct file_operations proc_fd_operations = {
1450         .read           = generic_read_dir,
1451         .readdir        = proc_readfd,
1452 };
1453
1454 /*
1455  * /proc/pid/fd needs a special permission handler so that a process can still
1456  * access /proc/self/fd after it has executed a setuid().
1457  */
1458 static int proc_fd_permission(struct inode *inode, int mask,
1459                                 struct nameidata *nd)
1460 {
1461         int rv;
1462
1463         rv = generic_permission(inode, mask, NULL);
1464         if (rv == 0)
1465                 return 0;
1466         if (task_pid(current) == proc_pid(inode))
1467                 rv = 0;
1468         return rv;
1469 }
1470
1471 /*
1472  * proc directories can do almost nothing..
1473  */
1474 static const struct inode_operations proc_fd_inode_operations = {
1475         .lookup         = proc_lookupfd,
1476         .permission     = proc_fd_permission,
1477         .setattr        = proc_setattr,
1478 };
1479
1480 static struct dentry *proc_pident_instantiate(struct inode *dir,
1481         struct dentry *dentry, struct task_struct *task, void *ptr)
1482 {
1483         struct pid_entry *p = ptr;
1484         struct inode *inode;
1485         struct proc_inode *ei;
1486         struct dentry *error = ERR_PTR(-EINVAL);
1487
1488         inode = proc_pid_make_inode(dir->i_sb, task);
1489         if (!inode)
1490                 goto out;
1491
1492         ei = PROC_I(inode);
1493         inode->i_mode = p->mode;
1494         if (S_ISDIR(inode->i_mode))
1495                 inode->i_nlink = 2;     /* Use getattr to fix if necessary */
1496         if (p->iop)
1497                 inode->i_op = p->iop;
1498         if (p->fop)
1499                 inode->i_fop = p->fop;
1500         ei->op = p->op;
1501         dentry->d_op = &pid_dentry_operations;
1502         d_add(dentry, inode);
1503         /* Close the race of the process dying before we return the dentry */
1504         if (pid_revalidate(dentry, NULL))
1505                 error = NULL;
1506 out:
1507         return error;
1508 }
1509
1510 static struct dentry *proc_pident_lookup(struct inode *dir, 
1511                                          struct dentry *dentry,
1512                                          struct pid_entry *ents,
1513                                          unsigned int nents)
1514 {
1515         struct inode *inode;
1516         struct dentry *error;
1517         struct task_struct *task = get_proc_task(dir);
1518         struct pid_entry *p, *last;
1519
1520         error = ERR_PTR(-ENOENT);
1521         inode = NULL;
1522
1523         if (!task)
1524                 goto out_no_task;
1525
1526         /*
1527          * Yes, it does not scale. And it should not. Don't add
1528          * new entries into /proc/<tgid>/ without very good reasons.
1529          */
1530         last = &ents[nents - 1];
1531         for (p = ents; p <= last; p++) {
1532                 if (p->len != dentry->d_name.len)
1533                         continue;
1534                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1535                         break;
1536         }
1537         if (p > last)
1538                 goto out;
1539
1540         error = proc_pident_instantiate(dir, dentry, task, p);
1541 out:
1542         put_task_struct(task);
1543 out_no_task:
1544         return error;
1545 }
1546
1547 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1548         struct task_struct *task, struct pid_entry *p)
1549 {
1550         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1551                                 proc_pident_instantiate, task, p);
1552 }
1553
1554 static int proc_pident_readdir(struct file *filp,
1555                 void *dirent, filldir_t filldir,
1556                 struct pid_entry *ents, unsigned int nents)
1557 {
1558         int i;
1559         int pid;
1560         struct dentry *dentry = filp->f_path.dentry;
1561         struct inode *inode = dentry->d_inode;
1562         struct task_struct *task = get_proc_task(inode);
1563         struct pid_entry *p, *last;
1564         ino_t ino;
1565         int ret;
1566
1567         ret = -ENOENT;
1568         if (!task)
1569                 goto out_no_task;
1570
1571         ret = 0;
1572         pid = task->pid;
1573         i = filp->f_pos;
1574         switch (i) {
1575         case 0:
1576                 ino = inode->i_ino;
1577                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1578                         goto out;
1579                 i++;
1580                 filp->f_pos++;
1581                 /* fall through */
1582         case 1:
1583                 ino = parent_ino(dentry);
1584                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1585                         goto out;
1586                 i++;
1587                 filp->f_pos++;
1588                 /* fall through */
1589         default:
1590                 i -= 2;
1591                 if (i >= nents) {
1592                         ret = 1;
1593                         goto out;
1594                 }
1595                 p = ents + i;
1596                 last = &ents[nents - 1];
1597                 while (p <= last) {
1598                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1599                                 goto out;
1600                         filp->f_pos++;
1601                         p++;
1602                 }
1603         }
1604
1605         ret = 1;
1606 out:
1607         put_task_struct(task);
1608 out_no_task:
1609         return ret;
1610 }
1611
1612 #ifdef CONFIG_SECURITY
1613 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1614                                   size_t count, loff_t *ppos)
1615 {
1616         struct inode * inode = file->f_path.dentry->d_inode;
1617         char *p = NULL;
1618         ssize_t length;
1619         struct task_struct *task = get_proc_task(inode);
1620
1621         if (!task)
1622                 return -ESRCH;
1623
1624         length = security_getprocattr(task,
1625                                       (char*)file->f_path.dentry->d_name.name,
1626                                       &p);
1627         put_task_struct(task);
1628         if (length > 0)
1629                 length = simple_read_from_buffer(buf, count, ppos, p, length);
1630         kfree(p);
1631         return length;
1632 }
1633
1634 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1635                                    size_t count, loff_t *ppos)
1636 {
1637         struct inode * inode = file->f_path.dentry->d_inode;
1638         char *page;
1639         ssize_t length;
1640         struct task_struct *task = get_proc_task(inode);
1641
1642         length = -ESRCH;
1643         if (!task)
1644                 goto out_no_task;
1645         if (count > PAGE_SIZE)
1646                 count = PAGE_SIZE;
1647
1648         /* No partial writes. */
1649         length = -EINVAL;
1650         if (*ppos != 0)
1651                 goto out;
1652
1653         length = -ENOMEM;
1654         page = (char*)__get_free_page(GFP_USER);
1655         if (!page)
1656                 goto out;
1657
1658         length = -EFAULT;
1659         if (copy_from_user(page, buf, count))
1660                 goto out_free;
1661
1662         length = security_setprocattr(task,
1663                                       (char*)file->f_path.dentry->d_name.name,
1664                                       (void*)page, count);
1665 out_free:
1666         free_page((unsigned long) page);
1667 out:
1668         put_task_struct(task);
1669 out_no_task:
1670         return length;
1671 }
1672
1673 static const struct file_operations proc_pid_attr_operations = {
1674         .read           = proc_pid_attr_read,
1675         .write          = proc_pid_attr_write,
1676 };
1677
1678 static struct pid_entry attr_dir_stuff[] = {
1679         REG("current",    S_IRUGO|S_IWUGO, pid_attr),
1680         REG("prev",       S_IRUGO,         pid_attr),
1681         REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
1682         REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
1683         REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
1684         REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1685 };
1686
1687 static int proc_attr_dir_readdir(struct file * filp,
1688                              void * dirent, filldir_t filldir)
1689 {
1690         return proc_pident_readdir(filp,dirent,filldir,
1691                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1692 }
1693
1694 static const struct file_operations proc_attr_dir_operations = {
1695         .read           = generic_read_dir,
1696         .readdir        = proc_attr_dir_readdir,
1697 };
1698
1699 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1700                                 struct dentry *dentry, struct nameidata *nd)
1701 {
1702         return proc_pident_lookup(dir, dentry,
1703                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1704 }
1705
1706 static const struct inode_operations proc_attr_dir_inode_operations = {
1707         .lookup         = proc_attr_dir_lookup,
1708         .getattr        = pid_getattr,
1709         .setattr        = proc_setattr,
1710 };
1711
1712 #endif
1713
1714 /*
1715  * /proc/self:
1716  */
1717 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1718                               int buflen)
1719 {
1720         char tmp[PROC_NUMBUF];
1721         sprintf(tmp, "%d", current->tgid);
1722         return vfs_readlink(dentry,buffer,buflen,tmp);
1723 }
1724
1725 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1726 {
1727         char tmp[PROC_NUMBUF];
1728         sprintf(tmp, "%d", current->tgid);
1729         return ERR_PTR(vfs_follow_link(nd,tmp));
1730 }
1731
1732 static const struct inode_operations proc_self_inode_operations = {
1733         .readlink       = proc_self_readlink,
1734         .follow_link    = proc_self_follow_link,
1735 };
1736
1737 /*
1738  * proc base
1739  *
1740  * These are the directory entries in the root directory of /proc
1741  * that properly belong to the /proc filesystem, as they describe
1742  * describe something that is process related.
1743  */
1744 static struct pid_entry proc_base_stuff[] = {
1745         NOD("self", S_IFLNK|S_IRWXUGO,
1746                 &proc_self_inode_operations, NULL, {}),
1747 };
1748
1749 /*
1750  *      Exceptional case: normally we are not allowed to unhash a busy
1751  * directory. In this case, however, we can do it - no aliasing problems
1752  * due to the way we treat inodes.
1753  */
1754 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1755 {
1756         struct inode *inode = dentry->d_inode;
1757         struct task_struct *task = get_proc_task(inode);
1758         if (task) {
1759                 put_task_struct(task);
1760                 return 1;
1761         }
1762         d_drop(dentry);
1763         return 0;
1764 }
1765
1766 static struct dentry_operations proc_base_dentry_operations =
1767 {
1768         .d_revalidate   = proc_base_revalidate,
1769         .d_delete       = pid_delete_dentry,
1770 };
1771
1772 static struct dentry *proc_base_instantiate(struct inode *dir,
1773         struct dentry *dentry, struct task_struct *task, void *ptr)
1774 {
1775         struct pid_entry *p = ptr;
1776         struct inode *inode;
1777         struct proc_inode *ei;
1778         struct dentry *error = ERR_PTR(-EINVAL);
1779
1780         /* Allocate the inode */
1781         error = ERR_PTR(-ENOMEM);
1782         inode = new_inode(dir->i_sb);
1783         if (!inode)
1784                 goto out;
1785
1786         /* Initialize the inode */
1787         ei = PROC_I(inode);
1788         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1789
1790         /*
1791          * grab the reference to the task.
1792          */
1793         ei->pid = get_task_pid(task, PIDTYPE_PID);
1794         if (!ei->pid)
1795                 goto out_iput;
1796
1797         inode->i_uid = 0;
1798         inode->i_gid = 0;
1799         inode->i_mode = p->mode;
1800         if (S_ISDIR(inode->i_mode))
1801                 inode->i_nlink = 2;
1802         if (S_ISLNK(inode->i_mode))
1803                 inode->i_size = 64;
1804         if (p->iop)
1805                 inode->i_op = p->iop;
1806         if (p->fop)
1807                 inode->i_fop = p->fop;
1808         ei->op = p->op;
1809         dentry->d_op = &proc_base_dentry_operations;
1810         d_add(dentry, inode);
1811         error = NULL;
1812 out:
1813         return error;
1814 out_iput:
1815         iput(inode);
1816         goto out;
1817 }
1818
1819 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1820 {
1821         struct dentry *error;
1822         struct task_struct *task = get_proc_task(dir);
1823         struct pid_entry *p, *last;
1824
1825         error = ERR_PTR(-ENOENT);
1826
1827         if (!task)
1828                 goto out_no_task;
1829
1830         /* Lookup the directory entry */
1831         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1832         for (p = proc_base_stuff; p <= last; p++) {
1833                 if (p->len != dentry->d_name.len)
1834                         continue;
1835                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1836                         break;
1837         }
1838         if (p > last)
1839                 goto out;
1840
1841         error = proc_base_instantiate(dir, dentry, task, p);
1842
1843 out:
1844         put_task_struct(task);
1845 out_no_task:
1846         return error;
1847 }
1848
1849 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1850         struct task_struct *task, struct pid_entry *p)
1851 {
1852         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1853                                 proc_base_instantiate, task, p);
1854 }
1855
1856 #ifdef CONFIG_TASK_IO_ACCOUNTING
1857 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1858 {
1859         return sprintf(buffer,
1860 #ifdef CONFIG_TASK_XACCT
1861                         "rchar: %llu\n"
1862                         "wchar: %llu\n"
1863                         "syscr: %llu\n"
1864                         "syscw: %llu\n"
1865 #endif
1866                         "read_bytes: %llu\n"
1867                         "write_bytes: %llu\n"
1868                         "cancelled_write_bytes: %llu\n",
1869 #ifdef CONFIG_TASK_XACCT
1870                         (unsigned long long)task->rchar,
1871                         (unsigned long long)task->wchar,
1872                         (unsigned long long)task->syscr,
1873                         (unsigned long long)task->syscw,
1874 #endif
1875                         (unsigned long long)task->ioac.read_bytes,
1876                         (unsigned long long)task->ioac.write_bytes,
1877                         (unsigned long long)task->ioac.cancelled_write_bytes);
1878 }
1879 #endif
1880
1881 /*
1882  * Thread groups
1883  */
1884 static const struct file_operations proc_task_operations;
1885 static const struct inode_operations proc_task_inode_operations;
1886
1887 static struct pid_entry tgid_base_stuff[] = {
1888         DIR("task",       S_IRUGO|S_IXUGO, task),
1889         DIR("fd",         S_IRUSR|S_IXUSR, fd),
1890         INF("environ",    S_IRUSR, pid_environ),
1891         INF("auxv",       S_IRUSR, pid_auxv),
1892         INF("status",     S_IRUGO, pid_status),
1893         INF("cmdline",    S_IRUGO, pid_cmdline),
1894         INF("stat",       S_IRUGO, tgid_stat),
1895         INF("statm",      S_IRUGO, pid_statm),
1896         REG("maps",       S_IRUGO, maps),
1897 #ifdef CONFIG_NUMA
1898         REG("numa_maps",  S_IRUGO, numa_maps),
1899 #endif
1900         REG("mem",        S_IRUSR|S_IWUSR, mem),
1901 #ifdef CONFIG_SECCOMP
1902         REG("seccomp",    S_IRUSR|S_IWUSR, seccomp),
1903 #endif
1904         LNK("cwd",        cwd),
1905         LNK("root",       root),
1906         LNK("exe",        exe),
1907         REG("mounts",     S_IRUGO, mounts),
1908         REG("mountstats", S_IRUSR, mountstats),
1909 #ifdef CONFIG_MMU
1910         REG("clear_refs", S_IWUSR, clear_refs),
1911         REG("smaps",      S_IRUGO, smaps),
1912 #endif
1913 #ifdef CONFIG_SECURITY
1914         DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
1915 #endif
1916 #ifdef CONFIG_KALLSYMS
1917         INF("wchan",      S_IRUGO, pid_wchan),
1918 #endif
1919 #ifdef CONFIG_SCHEDSTATS
1920         INF("schedstat",  S_IRUGO, pid_schedstat),
1921 #endif
1922 #ifdef CONFIG_CPUSETS
1923         REG("cpuset",     S_IRUGO, cpuset),
1924 #endif
1925         INF("oom_score",  S_IRUGO, oom_score),
1926         REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
1927 #ifdef CONFIG_AUDITSYSCALL
1928         REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
1929 #endif
1930 #ifdef CONFIG_FAULT_INJECTION
1931         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1932 #endif
1933 #ifdef CONFIG_TASK_IO_ACCOUNTING
1934         INF("io",       S_IRUGO, pid_io_accounting),
1935 #endif
1936 };
1937
1938 static int proc_tgid_base_readdir(struct file * filp,
1939                              void * dirent, filldir_t filldir)
1940 {
1941         return proc_pident_readdir(filp,dirent,filldir,
1942                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1943 }
1944
1945 static const struct file_operations proc_tgid_base_operations = {
1946         .read           = generic_read_dir,
1947         .readdir        = proc_tgid_base_readdir,
1948 };
1949
1950 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1951         return proc_pident_lookup(dir, dentry,
1952                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1953 }
1954
1955 static const struct inode_operations proc_tgid_base_inode_operations = {
1956         .lookup         = proc_tgid_base_lookup,
1957         .getattr        = pid_getattr,
1958         .setattr        = proc_setattr,
1959 };
1960
1961 /**
1962  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
1963  *
1964  * @task: task that should be flushed.
1965  *
1966  * Looks in the dcache for
1967  * /proc/@pid
1968  * /proc/@tgid/task/@pid
1969  * if either directory is present flushes it and all of it'ts children
1970  * from the dcache.
1971  *
1972  * It is safe and reasonable to cache /proc entries for a task until
1973  * that task exits.  After that they just clog up the dcache with
1974  * useless entries, possibly causing useful dcache entries to be
1975  * flushed instead.  This routine is proved to flush those useless
1976  * dcache entries at process exit time.
1977  *
1978  * NOTE: This routine is just an optimization so it does not guarantee
1979  *       that no dcache entries will exist at process exit time it
1980  *       just makes it very unlikely that any will persist.
1981  */
1982 void proc_flush_task(struct task_struct *task)
1983 {
1984         struct dentry *dentry, *leader, *dir;
1985         char buf[PROC_NUMBUF];
1986         struct qstr name;
1987
1988         name.name = buf;
1989         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1990         dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1991         if (dentry) {
1992                 shrink_dcache_parent(dentry);
1993                 d_drop(dentry);
1994                 dput(dentry);
1995         }
1996
1997         if (thread_group_leader(task))
1998                 goto out;
1999
2000         name.name = buf;
2001         name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2002         leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2003         if (!leader)
2004                 goto out;
2005
2006         name.name = "task";
2007         name.len = strlen(name.name);
2008         dir = d_hash_and_lookup(leader, &name);
2009         if (!dir)
2010                 goto out_put_leader;
2011
2012         name.name = buf;
2013         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2014         dentry = d_hash_and_lookup(dir, &name);
2015         if (dentry) {
2016                 shrink_dcache_parent(dentry);
2017                 d_drop(dentry);
2018                 dput(dentry);
2019         }
2020
2021         dput(dir);
2022 out_put_leader:
2023         dput(leader);
2024 out:
2025         return;
2026 }
2027
2028 static struct dentry *proc_pid_instantiate(struct inode *dir,
2029                                            struct dentry * dentry,
2030                                            struct task_struct *task, void *ptr)
2031 {
2032         struct dentry *error = ERR_PTR(-ENOENT);
2033         struct inode *inode;
2034
2035         inode = proc_pid_make_inode(dir->i_sb, task);
2036         if (!inode)
2037                 goto out;
2038
2039         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2040         inode->i_op = &proc_tgid_base_inode_operations;
2041         inode->i_fop = &proc_tgid_base_operations;
2042         inode->i_flags|=S_IMMUTABLE;
2043         inode->i_nlink = 4;
2044 #ifdef CONFIG_SECURITY
2045         inode->i_nlink += 1;
2046 #endif
2047
2048         dentry->d_op = &pid_dentry_operations;
2049
2050         d_add(dentry, inode);
2051         /* Close the race of the process dying before we return the dentry */
2052         if (pid_revalidate(dentry, NULL))
2053                 error = NULL;
2054 out:
2055         return error;
2056 }
2057
2058 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2059 {
2060         struct dentry *result = ERR_PTR(-ENOENT);
2061         struct task_struct *task;
2062         unsigned tgid;
2063
2064         result = proc_base_lookup(dir, dentry);
2065         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2066                 goto out;
2067
2068         tgid = name_to_int(dentry);
2069         if (tgid == ~0U)
2070                 goto out;
2071
2072         rcu_read_lock();
2073         task = find_task_by_pid(tgid);
2074         if (task)
2075                 get_task_struct(task);
2076         rcu_read_unlock();
2077         if (!task)
2078                 goto out;
2079
2080         result = proc_pid_instantiate(dir, dentry, task, NULL);
2081         put_task_struct(task);
2082 out:
2083         return result;
2084 }
2085
2086 /*
2087  * Find the first task with tgid >= tgid
2088  *
2089  */
2090 static struct task_struct *next_tgid(unsigned int tgid)
2091 {
2092         struct task_struct *task;
2093         struct pid *pid;
2094
2095         rcu_read_lock();
2096 retry:
2097         task = NULL;
2098         pid = find_ge_pid(tgid);
2099         if (pid) {
2100                 tgid = pid->nr + 1;
2101                 task = pid_task(pid, PIDTYPE_PID);
2102                 /* What we to know is if the pid we have find is the
2103                  * pid of a thread_group_leader.  Testing for task
2104                  * being a thread_group_leader is the obvious thing
2105                  * todo but there is a window when it fails, due to
2106                  * the pid transfer logic in de_thread.
2107                  *
2108                  * So we perform the straight forward test of seeing
2109                  * if the pid we have found is the pid of a thread
2110                  * group leader, and don't worry if the task we have
2111                  * found doesn't happen to be a thread group leader.
2112                  * As we don't care in the case of readdir.
2113                  */
2114                 if (!task || !has_group_leader_pid(task))
2115                         goto retry;
2116                 get_task_struct(task);
2117         }
2118         rcu_read_unlock();
2119         return task;
2120 }
2121
2122 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2123
2124 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2125         struct task_struct *task, int tgid)
2126 {
2127         char name[PROC_NUMBUF];
2128         int len = snprintf(name, sizeof(name), "%d", tgid);
2129         return proc_fill_cache(filp, dirent, filldir, name, len,
2130                                 proc_pid_instantiate, task, NULL);
2131 }
2132
2133 /* for the /proc/ directory itself, after non-process stuff has been done */
2134 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2135 {
2136         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2137         struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2138         struct task_struct *task;
2139         int tgid;
2140
2141         if (!reaper)
2142                 goto out_no_task;
2143
2144         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2145                 struct pid_entry *p = &proc_base_stuff[nr];
2146                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2147                         goto out;
2148         }
2149
2150         tgid = filp->f_pos - TGID_OFFSET;
2151         for (task = next_tgid(tgid);
2152              task;
2153              put_task_struct(task), task = next_tgid(tgid + 1)) {
2154                 tgid = task->pid;
2155                 filp->f_pos = tgid + TGID_OFFSET;
2156                 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2157                         put_task_struct(task);
2158                         goto out;
2159                 }
2160         }
2161         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2162 out:
2163         put_task_struct(reaper);
2164 out_no_task:
2165         return 0;
2166 }
2167
2168 /*
2169  * Tasks
2170  */
2171 static struct pid_entry tid_base_stuff[] = {
2172         DIR("fd",        S_IRUSR|S_IXUSR, fd),
2173         INF("environ",   S_IRUSR, pid_environ),
2174         INF("auxv",      S_IRUSR, pid_auxv),
2175         INF("status",    S_IRUGO, pid_status),
2176         INF("cmdline",   S_IRUGO, pid_cmdline),
2177         INF("stat",      S_IRUGO, tid_stat),
2178         INF("statm",     S_IRUGO, pid_statm),
2179         REG("maps",      S_IRUGO, maps),
2180 #ifdef CONFIG_NUMA
2181         REG("numa_maps", S_IRUGO, numa_maps),
2182 #endif
2183         REG("mem",       S_IRUSR|S_IWUSR, mem),
2184 #ifdef CONFIG_SECCOMP
2185         REG("seccomp",   S_IRUSR|S_IWUSR, seccomp),
2186 #endif
2187         LNK("cwd",       cwd),
2188         LNK("root",      root),
2189         LNK("exe",       exe),
2190         REG("mounts",    S_IRUGO, mounts),
2191 #ifdef CONFIG_MMU
2192         REG("clear_refs", S_IWUSR, clear_refs),
2193         REG("smaps",     S_IRUGO, smaps),
2194 #endif
2195 #ifdef CONFIG_SECURITY
2196         DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
2197 #endif
2198 #ifdef CONFIG_KALLSYMS
2199         INF("wchan",     S_IRUGO, pid_wchan),
2200 #endif
2201 #ifdef CONFIG_SCHEDSTATS
2202         INF("schedstat", S_IRUGO, pid_schedstat),
2203 #endif
2204 #ifdef CONFIG_CPUSETS
2205         REG("cpuset",    S_IRUGO, cpuset),
2206 #endif
2207         INF("oom_score", S_IRUGO, oom_score),
2208         REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
2209 #ifdef CONFIG_AUDITSYSCALL
2210         REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
2211 #endif
2212 #ifdef CONFIG_FAULT_INJECTION
2213         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2214 #endif
2215 };
2216
2217 static int proc_tid_base_readdir(struct file * filp,
2218                              void * dirent, filldir_t filldir)
2219 {
2220         return proc_pident_readdir(filp,dirent,filldir,
2221                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2222 }
2223
2224 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2225         return proc_pident_lookup(dir, dentry,
2226                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2227 }
2228
2229 static const struct file_operations proc_tid_base_operations = {
2230         .read           = generic_read_dir,
2231         .readdir        = proc_tid_base_readdir,
2232 };
2233
2234 static const struct inode_operations proc_tid_base_inode_operations = {
2235         .lookup         = proc_tid_base_lookup,
2236         .getattr        = pid_getattr,
2237         .setattr        = proc_setattr,
2238 };
2239
2240 static struct dentry *proc_task_instantiate(struct inode *dir,
2241         struct dentry *dentry, struct task_struct *task, void *ptr)
2242 {
2243         struct dentry *error = ERR_PTR(-ENOENT);
2244         struct inode *inode;
2245         inode = proc_pid_make_inode(dir->i_sb, task);
2246
2247         if (!inode)
2248                 goto out;
2249         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2250         inode->i_op = &proc_tid_base_inode_operations;
2251         inode->i_fop = &proc_tid_base_operations;
2252         inode->i_flags|=S_IMMUTABLE;
2253         inode->i_nlink = 3;
2254 #ifdef CONFIG_SECURITY
2255         inode->i_nlink += 1;
2256 #endif
2257
2258         dentry->d_op = &pid_dentry_operations;
2259
2260         d_add(dentry, inode);
2261         /* Close the race of the process dying before we return the dentry */
2262         if (pid_revalidate(dentry, NULL))
2263                 error = NULL;
2264 out:
2265         return error;
2266 }
2267
2268 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2269 {
2270         struct dentry *result = ERR_PTR(-ENOENT);
2271         struct task_struct *task;
2272         struct task_struct *leader = get_proc_task(dir);
2273         unsigned tid;
2274
2275         if (!leader)
2276                 goto out_no_task;
2277
2278         tid = name_to_int(dentry);
2279         if (tid == ~0U)
2280                 goto out;
2281
2282         rcu_read_lock();
2283         task = find_task_by_pid(tid);
2284         if (task)
2285                 get_task_struct(task);
2286         rcu_read_unlock();
2287         if (!task)
2288                 goto out;
2289         if (leader->tgid != task->tgid)
2290                 goto out_drop_task;
2291
2292         result = proc_task_instantiate(dir, dentry, task, NULL);
2293 out_drop_task:
2294         put_task_struct(task);
2295 out:
2296         put_task_struct(leader);
2297 out_no_task:
2298         return result;
2299 }
2300
2301 /*
2302  * Find the first tid of a thread group to return to user space.
2303  *
2304  * Usually this is just the thread group leader, but if the users
2305  * buffer was too small or there was a seek into the middle of the
2306  * directory we have more work todo.
2307  *
2308  * In the case of a short read we start with find_task_by_pid.
2309  *
2310  * In the case of a seek we start with the leader and walk nr
2311  * threads past it.
2312  */
2313 static struct task_struct *first_tid(struct task_struct *leader,
2314                                         int tid, int nr)
2315 {
2316         struct task_struct *pos;
2317
2318         rcu_read_lock();
2319         /* Attempt to start with the pid of a thread */
2320         if (tid && (nr > 0)) {
2321                 pos = find_task_by_pid(tid);
2322                 if (pos && (pos->group_leader == leader))
2323                         goto found;
2324         }
2325
2326         /* If nr exceeds the number of threads there is nothing todo */
2327         pos = NULL;
2328         if (nr && nr >= get_nr_threads(leader))
2329                 goto out;
2330
2331         /* If we haven't found our starting place yet start
2332          * with the leader and walk nr threads forward.
2333          */
2334         for (pos = leader; nr > 0; --nr) {
2335                 pos = next_thread(pos);
2336                 if (pos == leader) {
2337                         pos = NULL;
2338                         goto out;
2339                 }
2340         }
2341 found:
2342         get_task_struct(pos);
2343 out:
2344         rcu_read_unlock();
2345         return pos;
2346 }
2347
2348 /*
2349  * Find the next thread in the thread list.
2350  * Return NULL if there is an error or no next thread.
2351  *
2352  * The reference to the input task_struct is released.
2353  */
2354 static struct task_struct *next_tid(struct task_struct *start)
2355 {
2356         struct task_struct *pos = NULL;
2357         rcu_read_lock();
2358         if (pid_alive(start)) {
2359                 pos = next_thread(start);
2360                 if (thread_group_leader(pos))
2361                         pos = NULL;
2362                 else
2363                         get_task_struct(pos);
2364         }
2365         rcu_read_unlock();
2366         put_task_struct(start);
2367         return pos;
2368 }
2369
2370 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2371         struct task_struct *task, int tid)
2372 {
2373         char name[PROC_NUMBUF];
2374         int len = snprintf(name, sizeof(name), "%d", tid);
2375         return proc_fill_cache(filp, dirent, filldir, name, len,
2376                                 proc_task_instantiate, task, NULL);
2377 }
2378
2379 /* for the /proc/TGID/task/ directories */
2380 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2381 {
2382         struct dentry *dentry = filp->f_path.dentry;
2383         struct inode *inode = dentry->d_inode;
2384         struct task_struct *leader = NULL;
2385         struct task_struct *task;
2386         int retval = -ENOENT;
2387         ino_t ino;
2388         int tid;
2389         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2390
2391         task = get_proc_task(inode);
2392         if (!task)
2393                 goto out_no_task;
2394         rcu_read_lock();
2395         if (pid_alive(task)) {
2396                 leader = task->group_leader;
2397                 get_task_struct(leader);
2398         }
2399         rcu_read_unlock();
2400         put_task_struct(task);
2401         if (!leader)
2402                 goto out_no_task;
2403         retval = 0;
2404
2405         switch (pos) {
2406         case 0:
2407                 ino = inode->i_ino;
2408                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2409                         goto out;
2410                 pos++;
2411                 /* fall through */
2412         case 1:
2413                 ino = parent_ino(dentry);
2414                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2415                         goto out;
2416                 pos++;
2417                 /* fall through */
2418         }
2419
2420         /* f_version caches the tgid value that the last readdir call couldn't
2421          * return. lseek aka telldir automagically resets f_version to 0.
2422          */
2423         tid = filp->f_version;
2424         filp->f_version = 0;
2425         for (task = first_tid(leader, tid, pos - 2);
2426              task;
2427              task = next_tid(task), pos++) {
2428                 tid = task->pid;
2429                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2430                         /* returning this tgid failed, save it as the first
2431                          * pid for the next readir call */
2432                         filp->f_version = tid;
2433                         put_task_struct(task);
2434                         break;
2435                 }
2436         }
2437 out:
2438         filp->f_pos = pos;
2439         put_task_struct(leader);
2440 out_no_task:
2441         return retval;
2442 }
2443
2444 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2445 {
2446         struct inode *inode = dentry->d_inode;
2447         struct task_struct *p = get_proc_task(inode);
2448         generic_fillattr(inode, stat);
2449
2450         if (p) {
2451                 rcu_read_lock();
2452                 stat->nlink += get_nr_threads(p);
2453                 rcu_read_unlock();
2454                 put_task_struct(p);
2455         }
2456
2457         return 0;
2458 }
2459
2460 static const struct inode_operations proc_task_inode_operations = {
2461         .lookup         = proc_task_lookup,
2462         .getattr        = proc_task_getattr,
2463         .setattr        = proc_setattr,
2464 };
2465
2466 static const struct file_operations proc_task_operations = {
2467         .read           = generic_read_dir,
2468         .readdir        = proc_task_readdir,
2469 };