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