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