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