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