blob: 05ace70d051a87ff2f5cfd28d76e7fa6551b48bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#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>
Al Viro5addc5d2005-11-07 17:15:49 -050073#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070074#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070075#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "internal.h"
77
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070078/* NOTE:
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
83 *
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
86 */
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric W. Biederman8578cea2006-06-26 00:25:54 -070089/* Worst case buffer size needed for holding an integer. */
Andrew Morton0187f872006-10-17 00:09:41 -070090#define PROC_NUMBUF 13
Eric W. Biederman8578cea2006-06-26 00:25:54 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int len;
94 char *name;
95 mode_t mode;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070096 struct inode_operations *iop;
97 struct file_operations *fop;
98 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Eric W. Biederman61a28782006-10-02 02:18:49 -0700101#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700102 .len = sizeof(NAME) - 1, \
103 .name = (NAME), \
104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
108}
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700126static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700129 task_lock(task);
130 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if(fs)
132 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133 task_unlock(task);
134 return fs;
135}
136
Eric W. Biederman99f89552006-06-26 00:25:55 -0700137static int get_nr_threads(struct task_struct *tsk)
138{
139 /* Must be called with the rcu_read_lock held */
140 unsigned long flags;
141 int count = 0;
142
143 if (lock_task_sighand(tsk, &flags)) {
144 count = atomic_read(&tsk->signal->count);
145 unlock_task_sighand(tsk, &flags);
146 }
147 return count;
148}
149
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700150static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
151{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700152 struct task_struct *task = get_proc_task(inode);
153 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700155
156 if (task) {
157 fs = get_fs_struct(task);
158 put_task_struct(task);
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (fs) {
161 read_lock(&fs->lock);
162 *mnt = mntget(fs->pwdmnt);
163 *dentry = dget(fs->pwd);
164 read_unlock(&fs->lock);
165 result = 0;
166 put_fs_struct(fs);
167 }
168 return result;
169}
170
171static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
172{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
178 fs = get_fs_struct(task);
179 put_task_struct(task);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (fs) {
182 read_lock(&fs->lock);
183 *mnt = mntget(fs->rootmnt);
184 *dentry = dget(fs->root);
185 read_unlock(&fs->lock);
186 result = 0;
187 put_fs_struct(fs);
188 }
189 return result;
190}
191
192#define MAY_PTRACE(task) \
193 (task == current || \
194 (task->parent == current && \
195 (task->ptrace & PT_PTRACED) && \
196 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
197 security_ptrace(current,task) == 0))
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int proc_pid_environ(struct task_struct *task, char * buffer)
200{
201 int res = 0;
202 struct mm_struct *mm = get_task_mm(task);
203 if (mm) {
204 unsigned int len = mm->env_end - mm->env_start;
205 if (len > PAGE_SIZE)
206 len = PAGE_SIZE;
207 res = access_process_vm(task, mm->env_start, buffer, len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700208 if (!ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 res = -ESRCH;
210 mmput(mm);
211 }
212 return res;
213}
214
215static int proc_pid_cmdline(struct task_struct *task, char * buffer)
216{
217 int res = 0;
218 unsigned int len;
219 struct mm_struct *mm = get_task_mm(task);
220 if (!mm)
221 goto out;
222 if (!mm->arg_end)
223 goto out_mm; /* Shh! No looking before we're done */
224
225 len = mm->arg_end - mm->arg_start;
226
227 if (len > PAGE_SIZE)
228 len = PAGE_SIZE;
229
230 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
231
232 // If the nul at the end of args has been overwritten, then
233 // assume application is using setproctitle(3).
234 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
235 len = strnlen(buffer, res);
236 if (len < res) {
237 res = len;
238 } else {
239 len = mm->env_end - mm->env_start;
240 if (len > PAGE_SIZE - res)
241 len = PAGE_SIZE - res;
242 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
243 res = strnlen(buffer, res);
244 }
245 }
246out_mm:
247 mmput(mm);
248out:
249 return res;
250}
251
252static int proc_pid_auxv(struct task_struct *task, char *buffer)
253{
254 int res = 0;
255 struct mm_struct *mm = get_task_mm(task);
256 if (mm) {
257 unsigned int nwords = 0;
258 do
259 nwords += 2;
260 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
261 res = nwords * sizeof(mm->saved_auxv[0]);
262 if (res > PAGE_SIZE)
263 res = PAGE_SIZE;
264 memcpy(buffer, mm->saved_auxv, res);
265 mmput(mm);
266 }
267 return res;
268}
269
270
271#ifdef CONFIG_KALLSYMS
272/*
273 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
274 * Returns the resolved symbol. If that fails, simply return the address.
275 */
276static int proc_pid_wchan(struct task_struct *task, char *buffer)
277{
278 char *modname;
279 const char *sym_name;
280 unsigned long wchan, size, offset;
281 char namebuf[KSYM_NAME_LEN+1];
282
283 wchan = get_wchan(task);
284
285 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
286 if (sym_name)
287 return sprintf(buffer, "%s", sym_name);
288 return sprintf(buffer, "%lu", wchan);
289}
290#endif /* CONFIG_KALLSYMS */
291
292#ifdef CONFIG_SCHEDSTATS
293/*
294 * Provides /proc/PID/schedstat
295 */
296static int proc_pid_schedstat(struct task_struct *task, char *buffer)
297{
298 return sprintf(buffer, "%lu %lu %lu\n",
299 task->sched_info.cpu_time,
300 task->sched_info.run_delay,
301 task->sched_info.pcnt);
302}
303#endif
304
305/* The badness from the OOM killer */
306unsigned long badness(struct task_struct *p, unsigned long uptime);
307static int proc_oom_score(struct task_struct *task, char *buffer)
308{
309 unsigned long points;
310 struct timespec uptime;
311
312 do_posix_clock_monotonic_gettime(&uptime);
313 points = badness(task, uptime.tv_sec);
314 return sprintf(buffer, "%lu\n", points);
315}
316
317/************************************************************************/
318/* Here the fs part begins */
319/************************************************************************/
320
321/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700322static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700324 struct task_struct *task;
325 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700326 /* Allow access to a task's file descriptors if it is us or we
327 * may use ptrace attach to the process and find out that
328 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700329 */
330 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700331 if (task) {
332 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700333 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700334 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700335 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700338static int proc_setattr(struct dentry *dentry, struct iattr *attr)
339{
340 int error;
341 struct inode *inode = dentry->d_inode;
342
343 if (attr->ia_valid & ATTR_MODE)
344 return -EPERM;
345
346 error = inode_change_ok(inode, attr);
347 if (!error) {
348 error = security_inode_setattr(dentry, attr);
349 if (!error)
350 error = inode_setattr(inode, attr);
351 }
352 return error;
353}
354
355static struct inode_operations proc_def_inode_operations = {
356 .setattr = proc_setattr,
357};
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359extern struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500360struct proc_mounts {
361 struct seq_file m;
362 int event;
363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int mounts_open(struct inode *inode, struct file *file)
366{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700367 struct task_struct *task = get_proc_task(inode);
368 struct namespace *namespace = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500369 struct proc_mounts *p;
370 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Eric W. Biederman99f89552006-06-26 00:25:55 -0700372 if (task) {
373 task_lock(task);
Serge E. Hallyn1651e142006-10-02 02:18:08 -0700374 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700375 if (namespace)
376 get_namespace(namespace);
377 task_unlock(task);
378 put_task_struct(task);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Al Viro5addc5d2005-11-07 17:15:49 -0500381 if (namespace) {
382 ret = -ENOMEM;
383 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
384 if (p) {
385 file->private_data = &p->m;
386 ret = seq_open(file, &mounts_op);
387 if (!ret) {
388 p->m.private = namespace;
389 p->event = namespace->event;
390 return 0;
391 }
392 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Al Viro5addc5d2005-11-07 17:15:49 -0500394 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 return ret;
397}
398
399static int mounts_release(struct inode *inode, struct file *file)
400{
401 struct seq_file *m = file->private_data;
402 struct namespace *namespace = m->private;
403 put_namespace(namespace);
404 return seq_release(inode, file);
405}
406
Al Viro5addc5d2005-11-07 17:15:49 -0500407static unsigned mounts_poll(struct file *file, poll_table *wait)
408{
409 struct proc_mounts *p = file->private_data;
410 struct namespace *ns = p->m.private;
411 unsigned res = 0;
412
413 poll_wait(file, &ns->poll, wait);
414
415 spin_lock(&vfsmount_lock);
416 if (p->event != ns->event) {
417 p->event = ns->event;
418 res = POLLERR;
419 }
420 spin_unlock(&vfsmount_lock);
421
422 return res;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static struct file_operations proc_mounts_operations = {
426 .open = mounts_open,
427 .read = seq_read,
428 .llseek = seq_lseek,
429 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500430 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
Chuck Leverb4629fe2006-03-20 13:44:12 -0500433extern struct seq_operations mountstats_op;
434static int mountstats_open(struct inode *inode, struct file *file)
435{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500436 int ret = seq_open(file, &mountstats_op);
437
438 if (!ret) {
439 struct seq_file *m = file->private_data;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700440 struct namespace *namespace = NULL;
441 struct task_struct *task = get_proc_task(inode);
442
443 if (task) {
444 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800445 if (task->nsproxy)
446 namespace = task->nsproxy->namespace;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700447 if (namespace)
448 get_namespace(namespace);
449 task_unlock(task);
450 put_task_struct(task);
451 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500452
453 if (namespace)
454 m->private = namespace;
455 else {
456 seq_release(inode, file);
457 ret = -EINVAL;
458 }
459 }
460 return ret;
461}
462
463static struct file_operations proc_mountstats_operations = {
464 .open = mountstats_open,
465 .read = seq_read,
466 .llseek = seq_lseek,
467 .release = mounts_release,
468};
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
471
472static ssize_t proc_info_read(struct file * file, char __user * buf,
473 size_t count, loff_t *ppos)
474{
475 struct inode * inode = file->f_dentry->d_inode;
476 unsigned long page;
477 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700478 struct task_struct *task = get_proc_task(inode);
479
480 length = -ESRCH;
481 if (!task)
482 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (count > PROC_BLOCK_SIZE)
485 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700486
487 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 length = PROC_I(inode)->op.proc_read(task, (char*)page);
492
493 if (length >= 0)
494 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
495 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700496out:
497 put_task_struct(task);
498out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return length;
500}
501
502static struct file_operations proc_info_file_operations = {
503 .read = proc_info_read,
504};
505
506static int mem_open(struct inode* inode, struct file* file)
507{
508 file->private_data = (void*)((long)current->self_exec_id);
509 return 0;
510}
511
512static ssize_t mem_read(struct file * file, char __user * buf,
513 size_t count, loff_t *ppos)
514{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700515 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 char *page;
517 unsigned long src = *ppos;
518 int ret = -ESRCH;
519 struct mm_struct *mm;
520
Eric W. Biederman99f89552006-06-26 00:25:55 -0700521 if (!task)
522 goto out_no_task;
523
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700524 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto out;
526
527 ret = -ENOMEM;
528 page = (char *)__get_free_page(GFP_USER);
529 if (!page)
530 goto out;
531
532 ret = 0;
533
534 mm = get_task_mm(task);
535 if (!mm)
536 goto out_free;
537
538 ret = -EIO;
539
540 if (file->private_data != (void*)((long)current->self_exec_id))
541 goto out_put;
542
543 ret = 0;
544
545 while (count > 0) {
546 int this_len, retval;
547
548 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
549 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700550 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!ret)
552 ret = -EIO;
553 break;
554 }
555
556 if (copy_to_user(buf, page, retval)) {
557 ret = -EFAULT;
558 break;
559 }
560
561 ret += retval;
562 src += retval;
563 buf += retval;
564 count -= retval;
565 }
566 *ppos = src;
567
568out_put:
569 mmput(mm);
570out_free:
571 free_page((unsigned long) page);
572out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700573 put_task_struct(task);
574out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return ret;
576}
577
578#define mem_write NULL
579
580#ifndef mem_write
581/* This is a security hazard */
582static ssize_t mem_write(struct file * file, const char * buf,
583 size_t count, loff_t *ppos)
584{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700585 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 char *page;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700587 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long dst = *ppos;
589
Eric W. Biederman99f89552006-06-26 00:25:55 -0700590 copied = -ESRCH;
591 if (!task)
592 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric W. Biederman99f89552006-06-26 00:25:55 -0700594 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
595 goto out;
596
597 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 page = (char *)__get_free_page(GFP_USER);
599 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700602 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 while (count > 0) {
604 int this_len, retval;
605
606 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
607 if (copy_from_user(page, buf, this_len)) {
608 copied = -EFAULT;
609 break;
610 }
611 retval = access_process_vm(task, dst, page, this_len, 1);
612 if (!retval) {
613 if (!copied)
614 copied = -EIO;
615 break;
616 }
617 copied += retval;
618 buf += retval;
619 dst += retval;
620 count -= retval;
621 }
622 *ppos = dst;
623 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700624out:
625 put_task_struct(task);
626out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return copied;
628}
629#endif
630
631static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
632{
633 switch (orig) {
634 case 0:
635 file->f_pos = offset;
636 break;
637 case 1:
638 file->f_pos += offset;
639 break;
640 default:
641 return -EINVAL;
642 }
643 force_successful_syscall_return();
644 return file->f_pos;
645}
646
647static struct file_operations proc_mem_operations = {
648 .llseek = mem_lseek,
649 .read = mem_read,
650 .write = mem_write,
651 .open = mem_open,
652};
653
654static ssize_t oom_adjust_read(struct file *file, char __user *buf,
655 size_t count, loff_t *ppos)
656{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700657 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700658 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700660 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 loff_t __ppos = *ppos;
662
Eric W. Biederman99f89552006-06-26 00:25:55 -0700663 if (!task)
664 return -ESRCH;
665 oom_adjust = task->oomkilladj;
666 put_task_struct(task);
667
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700668 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (__ppos >= len)
670 return 0;
671 if (count > len-__ppos)
672 count = len-__ppos;
673 if (copy_to_user(buf, buffer + __ppos, count))
674 return -EFAULT;
675 *ppos = __ppos + count;
676 return count;
677}
678
679static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
681{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700682 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700683 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int oom_adjust;
685
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (copy_from_user(buffer, buf, count))
690 return -EFAULT;
691 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700692 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
693 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695 if (*end == '\n')
696 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700697 task = get_proc_task(file->f_dentry->d_inode);
698 if (!task)
699 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800700 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
701 put_task_struct(task);
702 return -EACCES;
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700705 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (end - buffer == 0)
707 return -EIO;
708 return end - buffer;
709}
710
711static struct file_operations proc_oom_adjust_operations = {
712 .read = oom_adjust_read,
713 .write = oom_adjust_write,
714};
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#ifdef CONFIG_AUDITSYSCALL
717#define TMPBUFLEN 21
718static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
719 size_t count, loff_t *ppos)
720{
721 struct inode * inode = file->f_dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700722 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ssize_t length;
724 char tmpbuf[TMPBUFLEN];
725
Eric W. Biederman99f89552006-06-26 00:25:55 -0700726 if (!task)
727 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
729 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700730 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
732}
733
734static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
735 size_t count, loff_t *ppos)
736{
737 struct inode * inode = file->f_dentry->d_inode;
738 char *page, *tmp;
739 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 uid_t loginuid;
741
742 if (!capable(CAP_AUDIT_CONTROL))
743 return -EPERM;
744
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700745 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EPERM;
747
Al Viroe0182902006-05-18 08:28:02 -0400748 if (count >= PAGE_SIZE)
749 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (*ppos != 0) {
752 /* No partial writes. */
753 return -EINVAL;
754 }
755 page = (char*)__get_free_page(GFP_USER);
756 if (!page)
757 return -ENOMEM;
758 length = -EFAULT;
759 if (copy_from_user(page, buf, count))
760 goto out_free_page;
761
Al Viroe0182902006-05-18 08:28:02 -0400762 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 loginuid = simple_strtoul(page, &tmp, 10);
764 if (tmp == page) {
765 length = -EINVAL;
766 goto out_free_page;
767
768 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700769 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (likely(length == 0))
771 length = count;
772
773out_free_page:
774 free_page((unsigned long) page);
775 return length;
776}
777
778static struct file_operations proc_loginuid_operations = {
779 .read = proc_loginuid_read,
780 .write = proc_loginuid_write,
781};
782#endif
783
784#ifdef CONFIG_SECCOMP
785static ssize_t seccomp_read(struct file *file, char __user *buf,
786 size_t count, loff_t *ppos)
787{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700788 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 char __buf[20];
790 loff_t __ppos = *ppos;
791 size_t len;
792
Eric W. Biederman99f89552006-06-26 00:25:55 -0700793 if (!tsk)
794 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* no need to print the trailing zero, so use only len */
796 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700797 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (__ppos >= len)
799 return 0;
800 if (count > len - __ppos)
801 count = len - __ppos;
802 if (copy_to_user(buf, __buf + __ppos, count))
803 return -EFAULT;
804 *ppos = __ppos + count;
805 return count;
806}
807
808static ssize_t seccomp_write(struct file *file, const char __user *buf,
809 size_t count, loff_t *ppos)
810{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700811 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 char __buf[20], *end;
813 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700814 ssize_t result;
815
816 result = -ESRCH;
817 if (!tsk)
818 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700821 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Eric W. Biederman99f89552006-06-26 00:25:55 -0700825 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 memset(__buf, 0, sizeof(__buf));
827 count = min(count, sizeof(__buf) - 1);
828 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700829 goto out;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 seccomp_mode = simple_strtoul(__buf, &end, 0);
832 if (*end == '\n')
833 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700834 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
836 tsk->seccomp.mode = seccomp_mode;
837 set_tsk_thread_flag(tsk, TIF_SECCOMP);
838 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700839 goto out;
840 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700842 goto out;
843 result = end - __buf;
844out:
845 put_task_struct(tsk);
846out_no_task:
847 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static struct file_operations proc_seccomp_operations = {
851 .read = seccomp_read,
852 .write = seccomp_write,
853};
854#endif /* CONFIG_SECCOMP */
855
Al Viro008b1502005-08-20 00:17:39 +0100856static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct inode *inode = dentry->d_inode;
859 int error = -EACCES;
860
861 /* We don't need a base pointer in the /proc filesystem */
862 path_release(nd);
863
Eric W. Biederman778c1142006-06-26 00:25:58 -0700864 /* Are we allowed to snoop on the tasks file descriptors? */
865 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
869 nd->last_type = LAST_BIND;
870out:
Al Viro008b1502005-08-20 00:17:39 +0100871 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
875 char __user *buffer, int buflen)
876{
877 struct inode * inode;
878 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
879 int len;
880
881 if (!tmp)
882 return -ENOMEM;
883
884 inode = dentry->d_inode;
885 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
886 len = PTR_ERR(path);
887 if (IS_ERR(path))
888 goto out;
889 len = tmp + PAGE_SIZE - 1 - path;
890
891 if (len > buflen)
892 len = buflen;
893 if (copy_to_user(buffer, path, len))
894 len = -EFAULT;
895 out:
896 free_page((unsigned long)tmp);
897 return len;
898}
899
900static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
901{
902 int error = -EACCES;
903 struct inode *inode = dentry->d_inode;
904 struct dentry *de;
905 struct vfsmount *mnt = NULL;
906
Eric W. Biederman778c1142006-06-26 00:25:58 -0700907 /* Are we allowed to snoop on the tasks file descriptors? */
908 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
912 if (error)
913 goto out;
914
915 error = do_proc_readlink(de, mnt, buffer, buflen);
916 dput(de);
917 mntput(mnt);
918out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return error;
920}
921
922static struct inode_operations proc_pid_link_inode_operations = {
923 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700924 .follow_link = proc_pid_follow_link,
925 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926};
927
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700928
929/* building an inode */
930
931static int task_dumpable(struct task_struct *task)
932{
933 int dumpable = 0;
934 struct mm_struct *mm;
935
936 task_lock(task);
937 mm = task->mm;
938 if (mm)
939 dumpable = mm->dumpable;
940 task_unlock(task);
941 if(dumpable == 1)
942 return 1;
943 return 0;
944}
945
946
Eric W. Biederman61a28782006-10-02 02:18:49 -0700947static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700948{
949 struct inode * inode;
950 struct proc_inode *ei;
951
952 /* We need a new inode */
953
954 inode = new_inode(sb);
955 if (!inode)
956 goto out;
957
958 /* Common stuff */
959 ei = PROC_I(inode);
960 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700961 inode->i_op = &proc_def_inode_operations;
962
963 /*
964 * grab the reference to task.
965 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -0700966 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -0700967 if (!ei->pid)
968 goto out_unlock;
969
970 inode->i_uid = 0;
971 inode->i_gid = 0;
972 if (task_dumpable(task)) {
973 inode->i_uid = task->euid;
974 inode->i_gid = task->egid;
975 }
976 security_task_to_inode(task, inode);
977
978out:
979 return inode;
980
981out_unlock:
982 iput(inode);
983 return NULL;
984}
985
986static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
987{
988 struct inode *inode = dentry->d_inode;
989 struct task_struct *task;
990 generic_fillattr(inode, stat);
991
992 rcu_read_lock();
993 stat->uid = 0;
994 stat->gid = 0;
995 task = pid_task(proc_pid(inode), PIDTYPE_PID);
996 if (task) {
997 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
998 task_dumpable(task)) {
999 stat->uid = task->euid;
1000 stat->gid = task->egid;
1001 }
1002 }
1003 rcu_read_unlock();
1004 return 0;
1005}
1006
1007/* dentry stuff */
1008
1009/*
1010 * Exceptional case: normally we are not allowed to unhash a busy
1011 * directory. In this case, however, we can do it - no aliasing problems
1012 * due to the way we treat inodes.
1013 *
1014 * Rewrite the inode's ownerships here because the owning task may have
1015 * performed a setuid(), etc.
1016 *
1017 * Before the /proc/pid/status file was created the only way to read
1018 * the effective uid of a /process was to stat /proc/pid. Reading
1019 * /proc/pid/status is slow enough that procps and other packages
1020 * kept stating /proc/pid. To keep the rules in /proc simple I have
1021 * made this apply to all per process world readable and executable
1022 * directories.
1023 */
1024static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1025{
1026 struct inode *inode = dentry->d_inode;
1027 struct task_struct *task = get_proc_task(inode);
1028 if (task) {
1029 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1030 task_dumpable(task)) {
1031 inode->i_uid = task->euid;
1032 inode->i_gid = task->egid;
1033 } else {
1034 inode->i_uid = 0;
1035 inode->i_gid = 0;
1036 }
1037 inode->i_mode &= ~(S_ISUID | S_ISGID);
1038 security_task_to_inode(task, inode);
1039 put_task_struct(task);
1040 return 1;
1041 }
1042 d_drop(dentry);
1043 return 0;
1044}
1045
1046static int pid_delete_dentry(struct dentry * dentry)
1047{
1048 /* Is the task we represent dead?
1049 * If so, then don't put the dentry on the lru list,
1050 * kill it immediately.
1051 */
1052 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1053}
1054
1055static struct dentry_operations pid_dentry_operations =
1056{
1057 .d_revalidate = pid_revalidate,
1058 .d_delete = pid_delete_dentry,
1059};
1060
1061/* Lookups */
1062
Eric W. Biederman61a28782006-10-02 02:18:49 -07001063typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1064
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001065/*
1066 * Fill a directory entry.
1067 *
1068 * If possible create the dcache entry and derive our inode number and
1069 * file type from dcache entry.
1070 *
1071 * Since all of the proc inode numbers are dynamically generated, the inode
1072 * numbers do not exist until the inode is cache. This means creating the
1073 * the dcache entry in readdir is necessary to keep the inode numbers
1074 * reported by readdir in sync with the inode numbers reported
1075 * by stat.
1076 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001077static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1078 char *name, int len,
1079 instantiate_t instantiate, struct task_struct *task, void *ptr)
1080{
1081 struct dentry *child, *dir = filp->f_dentry;
1082 struct inode *inode;
1083 struct qstr qname;
1084 ino_t ino = 0;
1085 unsigned type = DT_UNKNOWN;
1086
1087 qname.name = name;
1088 qname.len = len;
1089 qname.hash = full_name_hash(name, len);
1090
1091 child = d_lookup(dir, &qname);
1092 if (!child) {
1093 struct dentry *new;
1094 new = d_alloc(dir, &qname);
1095 if (new) {
1096 child = instantiate(dir->d_inode, new, task, ptr);
1097 if (child)
1098 dput(new);
1099 else
1100 child = new;
1101 }
1102 }
1103 if (!child || IS_ERR(child) || !child->d_inode)
1104 goto end_instantiate;
1105 inode = child->d_inode;
1106 if (inode) {
1107 ino = inode->i_ino;
1108 type = inode->i_mode >> 12;
1109 }
1110 dput(child);
1111end_instantiate:
1112 if (!ino)
1113 ino = find_inode_number(dir, &qname);
1114 if (!ino)
1115 ino = 1;
1116 return filldir(dirent, name, len, filp->f_pos, ino, type);
1117}
1118
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001119static unsigned name_to_int(struct dentry *dentry)
1120{
1121 const char *name = dentry->d_name.name;
1122 int len = dentry->d_name.len;
1123 unsigned n = 0;
1124
1125 if (len > 1 && *name == '0')
1126 goto out;
1127 while (len-- > 0) {
1128 unsigned c = *name++ - '0';
1129 if (c > 9)
1130 goto out;
1131 if (n >= (~0U-9)/10)
1132 goto out;
1133 n *= 10;
1134 n += c;
1135 }
1136 return n;
1137out:
1138 return ~0U;
1139}
1140
1141static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1142{
1143 struct task_struct *task = get_proc_task(inode);
1144 struct files_struct *files = NULL;
1145 struct file *file;
1146 int fd = proc_fd(inode);
1147
1148 if (task) {
1149 files = get_files_struct(task);
1150 put_task_struct(task);
1151 }
1152 if (files) {
1153 /*
1154 * We are not taking a ref to the file structure, so we must
1155 * hold ->file_lock.
1156 */
1157 spin_lock(&files->file_lock);
1158 file = fcheck_files(files, fd);
1159 if (file) {
1160 *mnt = mntget(file->f_vfsmnt);
1161 *dentry = dget(file->f_dentry);
1162 spin_unlock(&files->file_lock);
1163 put_files_struct(files);
1164 return 0;
1165 }
1166 spin_unlock(&files->file_lock);
1167 put_files_struct(files);
1168 }
1169 return -ENOENT;
1170}
1171
1172static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1173{
1174 struct inode *inode = dentry->d_inode;
1175 struct task_struct *task = get_proc_task(inode);
1176 int fd = proc_fd(inode);
1177 struct files_struct *files;
1178
1179 if (task) {
1180 files = get_files_struct(task);
1181 if (files) {
1182 rcu_read_lock();
1183 if (fcheck_files(files, fd)) {
1184 rcu_read_unlock();
1185 put_files_struct(files);
1186 if (task_dumpable(task)) {
1187 inode->i_uid = task->euid;
1188 inode->i_gid = task->egid;
1189 } else {
1190 inode->i_uid = 0;
1191 inode->i_gid = 0;
1192 }
1193 inode->i_mode &= ~(S_ISUID | S_ISGID);
1194 security_task_to_inode(task, inode);
1195 put_task_struct(task);
1196 return 1;
1197 }
1198 rcu_read_unlock();
1199 put_files_struct(files);
1200 }
1201 put_task_struct(task);
1202 }
1203 d_drop(dentry);
1204 return 0;
1205}
1206
1207static struct dentry_operations tid_fd_dentry_operations =
1208{
1209 .d_revalidate = tid_fd_revalidate,
1210 .d_delete = pid_delete_dentry,
1211};
1212
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001213static struct dentry *proc_fd_instantiate(struct inode *dir,
1214 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001215{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001216 unsigned fd = *(unsigned *)ptr;
1217 struct file *file;
1218 struct files_struct *files;
1219 struct inode *inode;
1220 struct proc_inode *ei;
1221 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001222
Eric W. Biederman61a28782006-10-02 02:18:49 -07001223 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001224 if (!inode)
1225 goto out;
1226 ei = PROC_I(inode);
1227 ei->fd = fd;
1228 files = get_files_struct(task);
1229 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001230 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001231 inode->i_mode = S_IFLNK;
1232
1233 /*
1234 * We are not taking a ref to the file structure, so we must
1235 * hold ->file_lock.
1236 */
1237 spin_lock(&files->file_lock);
1238 file = fcheck_files(files, fd);
1239 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001240 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001241 if (file->f_mode & 1)
1242 inode->i_mode |= S_IRUSR | S_IXUSR;
1243 if (file->f_mode & 2)
1244 inode->i_mode |= S_IWUSR | S_IXUSR;
1245 spin_unlock(&files->file_lock);
1246 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001247
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001248 inode->i_op = &proc_pid_link_inode_operations;
1249 inode->i_size = 64;
1250 ei->op.proc_get_link = proc_fd_link;
1251 dentry->d_op = &tid_fd_dentry_operations;
1252 d_add(dentry, inode);
1253 /* Close the race of the process dying before we return the dentry */
1254 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001255 error = NULL;
1256
1257 out:
1258 return error;
1259out_unlock:
1260 spin_unlock(&files->file_lock);
1261 put_files_struct(files);
1262out_iput:
1263 iput(inode);
1264 goto out;
1265}
1266
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001267static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1268{
1269 struct task_struct *task = get_proc_task(dir);
1270 unsigned fd = name_to_int(dentry);
1271 struct dentry *result = ERR_PTR(-ENOENT);
1272
1273 if (!task)
1274 goto out_no_task;
1275 if (fd == ~0U)
1276 goto out;
1277
1278 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001279out:
1280 put_task_struct(task);
1281out_no_task:
1282 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001283}
1284
Eric W. Biederman61a28782006-10-02 02:18:49 -07001285static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1286 struct task_struct *task, int fd)
1287{
1288 char name[PROC_NUMBUF];
1289 int len = snprintf(name, sizeof(name), "%d", fd);
1290 return proc_fill_cache(filp, dirent, filldir, name, len,
1291 proc_fd_instantiate, task, &fd);
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1295{
Eric W. Biederman56347082006-06-26 00:25:40 -07001296 struct dentry *dentry = filp->f_dentry;
1297 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001298 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned int fd, tid, ino;
1300 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001302 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001305 if (!p)
1306 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 retval = 0;
1308 tid = p->pid;
1309
1310 fd = filp->f_pos;
1311 switch (fd) {
1312 case 0:
1313 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1314 goto out;
1315 filp->f_pos++;
1316 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001317 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1319 goto out;
1320 filp->f_pos++;
1321 default:
1322 files = get_files_struct(p);
1323 if (!files)
1324 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001325 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001326 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001328 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (!fcheck_files(files, fd))
1332 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Eric W. Biederman61a28782006-10-02 02:18:49 -07001335 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001336 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
1338 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001339 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001341 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 put_files_struct(files);
1343 }
1344out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001345 put_task_struct(p);
1346out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return retval;
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static struct file_operations proc_fd_operations = {
1351 .read = generic_read_dir,
1352 .readdir = proc_readfd,
1353};
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/*
1356 * proc directories can do almost nothing..
1357 */
1358static struct inode_operations proc_fd_inode_operations = {
1359 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001360 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361};
1362
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001363static struct dentry *proc_pident_instantiate(struct inode *dir,
1364 struct dentry *dentry, struct task_struct *task, void *ptr)
1365{
1366 struct pid_entry *p = ptr;
1367 struct inode *inode;
1368 struct proc_inode *ei;
1369 struct dentry *error = ERR_PTR(-EINVAL);
1370
Eric W. Biederman61a28782006-10-02 02:18:49 -07001371 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001372 if (!inode)
1373 goto out;
1374
1375 ei = PROC_I(inode);
1376 inode->i_mode = p->mode;
1377 if (S_ISDIR(inode->i_mode))
1378 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1379 if (p->iop)
1380 inode->i_op = p->iop;
1381 if (p->fop)
1382 inode->i_fop = p->fop;
1383 ei->op = p->op;
1384 dentry->d_op = &pid_dentry_operations;
1385 d_add(dentry, inode);
1386 /* Close the race of the process dying before we return the dentry */
1387 if (pid_revalidate(dentry, NULL))
1388 error = NULL;
1389out:
1390 return error;
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static struct dentry *proc_pident_lookup(struct inode *dir,
1394 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001395 struct pid_entry *ents,
1396 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001399 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001400 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001401 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001403 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 inode = NULL;
1405
Eric W. Biederman99f89552006-06-26 00:25:55 -07001406 if (!task)
1407 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001409 /*
1410 * Yes, it does not scale. And it should not. Don't add
1411 * new entries into /proc/<tgid>/ without very good reasons.
1412 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001413 last = &ents[nents - 1];
1414 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (p->len != dentry->d_name.len)
1416 continue;
1417 if (!memcmp(dentry->d_name.name, p->name, p->len))
1418 break;
1419 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001420 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto out;
1422
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001423 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001425 put_task_struct(task);
1426out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001427 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Eric W. Biederman61a28782006-10-02 02:18:49 -07001430static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1431 struct task_struct *task, struct pid_entry *p)
1432{
1433 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1434 proc_pident_instantiate, task, p);
1435}
1436
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001437static int proc_pident_readdir(struct file *filp,
1438 void *dirent, filldir_t filldir,
1439 struct pid_entry *ents, unsigned int nents)
1440{
1441 int i;
1442 int pid;
1443 struct dentry *dentry = filp->f_dentry;
1444 struct inode *inode = dentry->d_inode;
1445 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001446 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001447 ino_t ino;
1448 int ret;
1449
1450 ret = -ENOENT;
1451 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001452 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001453
1454 ret = 0;
1455 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001456 i = filp->f_pos;
1457 switch (i) {
1458 case 0:
1459 ino = inode->i_ino;
1460 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1461 goto out;
1462 i++;
1463 filp->f_pos++;
1464 /* fall through */
1465 case 1:
1466 ino = parent_ino(dentry);
1467 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1468 goto out;
1469 i++;
1470 filp->f_pos++;
1471 /* fall through */
1472 default:
1473 i -= 2;
1474 if (i >= nents) {
1475 ret = 1;
1476 goto out;
1477 }
1478 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001479 last = &ents[nents - 1];
1480 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001481 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001482 goto out;
1483 filp->f_pos++;
1484 p++;
1485 }
1486 }
1487
1488 ret = 1;
1489out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001490 put_task_struct(task);
1491out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001492 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001496static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1497 size_t count, loff_t *ppos)
1498{
1499 struct inode * inode = file->f_dentry->d_inode;
1500 unsigned long page;
1501 ssize_t length;
1502 struct task_struct *task = get_proc_task(inode);
1503
1504 length = -ESRCH;
1505 if (!task)
1506 goto out_no_task;
1507
1508 if (count > PAGE_SIZE)
1509 count = PAGE_SIZE;
1510 length = -ENOMEM;
1511 if (!(page = __get_free_page(GFP_KERNEL)))
1512 goto out;
1513
1514 length = security_getprocattr(task,
1515 (char*)file->f_dentry->d_name.name,
1516 (void*)page, count);
1517 if (length >= 0)
1518 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1519 free_page(page);
1520out:
1521 put_task_struct(task);
1522out_no_task:
1523 return length;
1524}
1525
1526static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1527 size_t count, loff_t *ppos)
1528{
1529 struct inode * inode = file->f_dentry->d_inode;
1530 char *page;
1531 ssize_t length;
1532 struct task_struct *task = get_proc_task(inode);
1533
1534 length = -ESRCH;
1535 if (!task)
1536 goto out_no_task;
1537 if (count > PAGE_SIZE)
1538 count = PAGE_SIZE;
1539
1540 /* No partial writes. */
1541 length = -EINVAL;
1542 if (*ppos != 0)
1543 goto out;
1544
1545 length = -ENOMEM;
1546 page = (char*)__get_free_page(GFP_USER);
1547 if (!page)
1548 goto out;
1549
1550 length = -EFAULT;
1551 if (copy_from_user(page, buf, count))
1552 goto out_free;
1553
1554 length = security_setprocattr(task,
1555 (char*)file->f_dentry->d_name.name,
1556 (void*)page, count);
1557out_free:
1558 free_page((unsigned long) page);
1559out:
1560 put_task_struct(task);
1561out_no_task:
1562 return length;
1563}
1564
1565static struct file_operations proc_pid_attr_operations = {
1566 .read = proc_pid_attr_read,
1567 .write = proc_pid_attr_write,
1568};
1569
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001570static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001571 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1572 REG("prev", S_IRUGO, pid_attr),
1573 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1574 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1575 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1576 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001577};
1578
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001579static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 void * dirent, filldir_t filldir)
1581{
1582 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001583 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001586static struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001588 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589};
1590
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001591static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct dentry *dentry, struct nameidata *nd)
1593{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001594 return proc_pident_lookup(dir, dentry,
1595 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001598static struct inode_operations proc_attr_dir_inode_operations = {
1599 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001600 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001601 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602};
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604#endif
1605
1606/*
1607 * /proc/self:
1608 */
1609static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1610 int buflen)
1611{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001612 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 sprintf(tmp, "%d", current->tgid);
1614 return vfs_readlink(dentry,buffer,buflen,tmp);
1615}
1616
Al Viro008b1502005-08-20 00:17:39 +01001617static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001619 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001621 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624static struct inode_operations proc_self_inode_operations = {
1625 .readlink = proc_self_readlink,
1626 .follow_link = proc_self_follow_link,
1627};
1628
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001629/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001630 * proc base
1631 *
1632 * These are the directory entries in the root directory of /proc
1633 * that properly belong to the /proc filesystem, as they describe
1634 * describe something that is process related.
1635 */
1636static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001637 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001638 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001639};
1640
1641/*
1642 * Exceptional case: normally we are not allowed to unhash a busy
1643 * directory. In this case, however, we can do it - no aliasing problems
1644 * due to the way we treat inodes.
1645 */
1646static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1647{
1648 struct inode *inode = dentry->d_inode;
1649 struct task_struct *task = get_proc_task(inode);
1650 if (task) {
1651 put_task_struct(task);
1652 return 1;
1653 }
1654 d_drop(dentry);
1655 return 0;
1656}
1657
1658static struct dentry_operations proc_base_dentry_operations =
1659{
1660 .d_revalidate = proc_base_revalidate,
1661 .d_delete = pid_delete_dentry,
1662};
1663
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001664static struct dentry *proc_base_instantiate(struct inode *dir,
1665 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001666{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001667 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001668 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001669 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001670 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001671
1672 /* Allocate the inode */
1673 error = ERR_PTR(-ENOMEM);
1674 inode = new_inode(dir->i_sb);
1675 if (!inode)
1676 goto out;
1677
1678 /* Initialize the inode */
1679 ei = PROC_I(inode);
1680 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001681
1682 /*
1683 * grab the reference to the task.
1684 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001685 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001686 if (!ei->pid)
1687 goto out_iput;
1688
1689 inode->i_uid = 0;
1690 inode->i_gid = 0;
1691 inode->i_mode = p->mode;
1692 if (S_ISDIR(inode->i_mode))
1693 inode->i_nlink = 2;
1694 if (S_ISLNK(inode->i_mode))
1695 inode->i_size = 64;
1696 if (p->iop)
1697 inode->i_op = p->iop;
1698 if (p->fop)
1699 inode->i_fop = p->fop;
1700 ei->op = p->op;
1701 dentry->d_op = &proc_base_dentry_operations;
1702 d_add(dentry, inode);
1703 error = NULL;
1704out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001705 return error;
1706out_iput:
1707 iput(inode);
1708 goto out;
1709}
1710
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001711static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1712{
1713 struct dentry *error;
1714 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001715 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001716
1717 error = ERR_PTR(-ENOENT);
1718
1719 if (!task)
1720 goto out_no_task;
1721
1722 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001723 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1724 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001725 if (p->len != dentry->d_name.len)
1726 continue;
1727 if (!memcmp(dentry->d_name.name, p->name, p->len))
1728 break;
1729 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001730 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001731 goto out;
1732
1733 error = proc_base_instantiate(dir, dentry, task, p);
1734
1735out:
1736 put_task_struct(task);
1737out_no_task:
1738 return error;
1739}
1740
Eric W. Biederman61a28782006-10-02 02:18:49 -07001741static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1742 struct task_struct *task, struct pid_entry *p)
1743{
1744 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1745 proc_base_instantiate, task, p);
1746}
1747
Eric W. Biederman801199c2006-10-02 02:18:48 -07001748/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749 * Thread groups
1750 */
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001751static struct file_operations proc_task_operations;
1752static struct inode_operations proc_task_inode_operations;
1753
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001754static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001755 DIR("task", S_IRUGO|S_IXUGO, task),
1756 DIR("fd", S_IRUSR|S_IXUSR, fd),
1757 INF("environ", S_IRUSR, pid_environ),
1758 INF("auxv", S_IRUSR, pid_auxv),
1759 INF("status", S_IRUGO, pid_status),
1760 INF("cmdline", S_IRUGO, pid_cmdline),
1761 INF("stat", S_IRUGO, tgid_stat),
1762 INF("statm", S_IRUGO, pid_statm),
1763 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001765 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001766#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001767 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001769 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001771 LNK("cwd", cwd),
1772 LNK("root", root),
1773 LNK("exe", exe),
1774 REG("mounts", S_IRUGO, mounts),
1775 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07001777 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778#endif
1779#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001780 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781#endif
1782#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001783 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001784#endif
1785#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001786 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787#endif
1788#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001789 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001790#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001791 INF("oom_score", S_IRUGO, oom_score),
1792 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001793#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001794 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001795#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001796};
1797
1798static int proc_tgid_base_readdir(struct file * filp,
1799 void * dirent, filldir_t filldir)
1800{
1801 return proc_pident_readdir(filp,dirent,filldir,
1802 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1803}
1804
1805static struct file_operations proc_tgid_base_operations = {
1806 .read = generic_read_dir,
1807 .readdir = proc_tgid_base_readdir,
1808};
1809
1810static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001811 return proc_pident_lookup(dir, dentry,
1812 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813}
1814
1815static struct inode_operations proc_tgid_base_inode_operations = {
1816 .lookup = proc_tgid_base_lookup,
1817 .getattr = pid_getattr,
1818 .setattr = proc_setattr,
1819};
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001822 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001824 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001826 * Looks in the dcache for
1827 * /proc/@pid
1828 * /proc/@tgid/task/@pid
1829 * if either directory is present flushes it and all of it'ts children
1830 * from the dcache.
1831 *
1832 * It is safe and reasonable to cache /proc entries for a task until
1833 * that task exits. After that they just clog up the dcache with
1834 * useless entries, possibly causing useful dcache entries to be
1835 * flushed instead. This routine is proved to flush those useless
1836 * dcache entries at process exit time.
1837 *
1838 * NOTE: This routine is just an optimization so it does not guarantee
1839 * that no dcache entries will exist at process exit time it
1840 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001842void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001844 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001845 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001846 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Eric W. Biederman48e64842006-06-26 00:25:48 -07001848 name.name = buf;
1849 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1850 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1851 if (dentry) {
1852 shrink_dcache_parent(dentry);
1853 d_drop(dentry);
1854 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Eric W. Biederman48e64842006-06-26 00:25:48 -07001857 if (thread_group_leader(task))
1858 goto out;
1859
1860 name.name = buf;
1861 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1862 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1863 if (!leader)
1864 goto out;
1865
1866 name.name = "task";
1867 name.len = strlen(name.name);
1868 dir = d_hash_and_lookup(leader, &name);
1869 if (!dir)
1870 goto out_put_leader;
1871
1872 name.name = buf;
1873 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1874 dentry = d_hash_and_lookup(dir, &name);
1875 if (dentry) {
1876 shrink_dcache_parent(dentry);
1877 d_drop(dentry);
1878 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001880
1881 dput(dir);
1882out_put_leader:
1883 dput(leader);
1884out:
1885 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001888struct dentry *proc_pid_instantiate(struct inode *dir,
1889 struct dentry * dentry, struct task_struct *task, void *ptr)
1890{
1891 struct dentry *error = ERR_PTR(-ENOENT);
1892 struct inode *inode;
1893
Eric W. Biederman61a28782006-10-02 02:18:49 -07001894 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001895 if (!inode)
1896 goto out;
1897
1898 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1899 inode->i_op = &proc_tgid_base_inode_operations;
1900 inode->i_fop = &proc_tgid_base_operations;
1901 inode->i_flags|=S_IMMUTABLE;
1902 inode->i_nlink = 4;
1903#ifdef CONFIG_SECURITY
1904 inode->i_nlink += 1;
1905#endif
1906
1907 dentry->d_op = &pid_dentry_operations;
1908
1909 d_add(dentry, inode);
1910 /* Close the race of the process dying before we return the dentry */
1911 if (pid_revalidate(dentry, NULL))
1912 error = NULL;
1913out:
1914 return error;
1915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1918{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001919 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Eric W. Biederman801199c2006-10-02 02:18:48 -07001923 result = proc_base_lookup(dir, dentry);
1924 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1925 goto out;
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 tgid = name_to_int(dentry);
1928 if (tgid == ~0U)
1929 goto out;
1930
Eric W. Biedermande758732006-06-26 00:25:51 -07001931 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 task = find_task_by_pid(tgid);
1933 if (task)
1934 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07001935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!task)
1937 goto out;
1938
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001939 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07001940 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001942 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001946 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001949static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001951 struct task_struct *task;
1952 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001954 rcu_read_lock();
1955retry:
1956 task = NULL;
1957 pid = find_ge_pid(tgid);
1958 if (pid) {
1959 tgid = pid->nr + 1;
1960 task = pid_task(pid, PIDTYPE_PID);
1961 /* What we to know is if the pid we have find is the
1962 * pid of a thread_group_leader. Testing for task
1963 * being a thread_group_leader is the obvious thing
1964 * todo but there is a window when it fails, due to
1965 * the pid transfer logic in de_thread.
1966 *
1967 * So we perform the straight forward test of seeing
1968 * if the pid we have found is the pid of a thread
1969 * group leader, and don't worry if the task we have
1970 * found doesn't happen to be a thread group leader.
1971 * As we don't care in the case of readdir.
1972 */
1973 if (!task || !has_group_leader_pid(task))
1974 goto retry;
1975 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07001977 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07001978 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001981#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Eric W. Biederman61a28782006-10-02 02:18:49 -07001983static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1984 struct task_struct *task, int tgid)
1985{
1986 char name[PROC_NUMBUF];
1987 int len = snprintf(name, sizeof(name), "%d", tgid);
1988 return proc_fill_cache(filp, dirent, filldir, name, len,
1989 proc_pid_instantiate, task, NULL);
1990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/* for the /proc/ directory itself, after non-process stuff has been done */
1993int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001996 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07001997 struct task_struct *task;
1998 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Eric W. Biederman61a28782006-10-02 02:18:49 -07002000 if (!reaper)
2001 goto out_no_task;
2002
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002003 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002004 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002005 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002009 tgid = filp->f_pos - TGID_OFFSET;
2010 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002011 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002012 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002013 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002014 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002015 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002016 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002017 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002020 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2021out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002022 put_task_struct(reaper);
2023out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025}
2026
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002027/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002028 * Tasks
2029 */
2030static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002031 DIR("fd", S_IRUSR|S_IXUSR, fd),
2032 INF("environ", S_IRUSR, pid_environ),
2033 INF("auxv", S_IRUSR, pid_auxv),
2034 INF("status", S_IRUGO, pid_status),
2035 INF("cmdline", S_IRUGO, pid_cmdline),
2036 INF("stat", S_IRUGO, tid_stat),
2037 INF("statm", S_IRUGO, pid_statm),
2038 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002039#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002040 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002041#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002042 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002044 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002045#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002046 LNK("cwd", cwd),
2047 LNK("root", root),
2048 LNK("exe", exe),
2049 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002050#ifdef CONFIG_MMU
Eric W. Biederman61a28782006-10-02 02:18:49 -07002051 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002052#endif
2053#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002054 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002055#endif
2056#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002057 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002058#endif
2059#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002060 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002061#endif
2062#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002063 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002065 INF("oom_score", S_IRUGO, oom_score),
2066 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002068 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002069#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002070};
2071
2072static int proc_tid_base_readdir(struct file * filp,
2073 void * dirent, filldir_t filldir)
2074{
2075 return proc_pident_readdir(filp,dirent,filldir,
2076 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2077}
2078
2079static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002080 return proc_pident_lookup(dir, dentry,
2081 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002082}
2083
2084static struct file_operations proc_tid_base_operations = {
2085 .read = generic_read_dir,
2086 .readdir = proc_tid_base_readdir,
2087};
2088
2089static struct inode_operations proc_tid_base_inode_operations = {
2090 .lookup = proc_tid_base_lookup,
2091 .getattr = pid_getattr,
2092 .setattr = proc_setattr,
2093};
2094
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002095static struct dentry *proc_task_instantiate(struct inode *dir,
2096 struct dentry *dentry, struct task_struct *task, void *ptr)
2097{
2098 struct dentry *error = ERR_PTR(-ENOENT);
2099 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002100 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002101
2102 if (!inode)
2103 goto out;
2104 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2105 inode->i_op = &proc_tid_base_inode_operations;
2106 inode->i_fop = &proc_tid_base_operations;
2107 inode->i_flags|=S_IMMUTABLE;
2108 inode->i_nlink = 3;
2109#ifdef CONFIG_SECURITY
2110 inode->i_nlink += 1;
2111#endif
2112
2113 dentry->d_op = &pid_dentry_operations;
2114
2115 d_add(dentry, inode);
2116 /* Close the race of the process dying before we return the dentry */
2117 if (pid_revalidate(dentry, NULL))
2118 error = NULL;
2119out:
2120 return error;
2121}
2122
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002123static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2124{
2125 struct dentry *result = ERR_PTR(-ENOENT);
2126 struct task_struct *task;
2127 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128 unsigned tid;
2129
2130 if (!leader)
2131 goto out_no_task;
2132
2133 tid = name_to_int(dentry);
2134 if (tid == ~0U)
2135 goto out;
2136
2137 rcu_read_lock();
2138 task = find_task_by_pid(tid);
2139 if (task)
2140 get_task_struct(task);
2141 rcu_read_unlock();
2142 if (!task)
2143 goto out;
2144 if (leader->tgid != task->tgid)
2145 goto out_drop_task;
2146
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002147 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002148out_drop_task:
2149 put_task_struct(task);
2150out:
2151 put_task_struct(leader);
2152out_no_task:
2153 return result;
2154}
2155
2156/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002157 * Find the first tid of a thread group to return to user space.
2158 *
2159 * Usually this is just the thread group leader, but if the users
2160 * buffer was too small or there was a seek into the middle of the
2161 * directory we have more work todo.
2162 *
2163 * In the case of a short read we start with find_task_by_pid.
2164 *
2165 * In the case of a seek we start with the leader and walk nr
2166 * threads past it.
2167 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002168static struct task_struct *first_tid(struct task_struct *leader,
2169 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002170{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002171 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002172
Eric W. Biedermancc288732006-06-26 00:26:01 -07002173 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002174 /* Attempt to start with the pid of a thread */
2175 if (tid && (nr > 0)) {
2176 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002177 if (pos && (pos->group_leader == leader))
2178 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002179 }
2180
2181 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002182 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002183 if (nr && nr >= get_nr_threads(leader))
2184 goto out;
2185
2186 /* If we haven't found our starting place yet start
2187 * with the leader and walk nr threads forward.
2188 */
2189 for (pos = leader; nr > 0; --nr) {
2190 pos = next_thread(pos);
2191 if (pos == leader) {
2192 pos = NULL;
2193 goto out;
2194 }
2195 }
2196found:
2197 get_task_struct(pos);
2198out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002199 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002200 return pos;
2201}
2202
2203/*
2204 * Find the next thread in the thread list.
2205 * Return NULL if there is an error or no next thread.
2206 *
2207 * The reference to the input task_struct is released.
2208 */
2209static struct task_struct *next_tid(struct task_struct *start)
2210{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002211 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002212 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002213 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002214 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002215 if (thread_group_leader(pos))
2216 pos = NULL;
2217 else
2218 get_task_struct(pos);
2219 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002220 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002221 put_task_struct(start);
2222 return pos;
2223}
2224
Eric W. Biederman61a28782006-10-02 02:18:49 -07002225static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2226 struct task_struct *task, int tid)
2227{
2228 char name[PROC_NUMBUF];
2229 int len = snprintf(name, sizeof(name), "%d", tid);
2230 return proc_fill_cache(filp, dirent, filldir, name, len,
2231 proc_task_instantiate, task, NULL);
2232}
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234/* for the /proc/TGID/task/ directories */
2235static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct dentry *dentry = filp->f_dentry;
2238 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002239 struct task_struct *leader = get_proc_task(inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002240 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int retval = -ENOENT;
2242 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002243 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2245
Eric W. Biederman99f89552006-06-26 00:25:55 -07002246 if (!leader)
2247 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 retval = 0;
2249
2250 switch (pos) {
2251 case 0:
2252 ino = inode->i_ino;
2253 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2254 goto out;
2255 pos++;
2256 /* fall through */
2257 case 1:
2258 ino = parent_ino(dentry);
2259 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2260 goto out;
2261 pos++;
2262 /* fall through */
2263 }
2264
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002265 /* f_version caches the tgid value that the last readdir call couldn't
2266 * return. lseek aka telldir automagically resets f_version to 0.
2267 */
2268 tid = filp->f_version;
2269 filp->f_version = 0;
2270 for (task = first_tid(leader, tid, pos - 2);
2271 task;
2272 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002273 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002274 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002275 /* returning this tgid failed, save it as the first
2276 * pid for the next readir call */
2277 filp->f_version = tid;
2278 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282out:
2283 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002284 put_task_struct(leader);
2285out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return retval;
2287}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002288
2289static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2290{
2291 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002292 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002293 generic_fillattr(inode, stat);
2294
Eric W. Biederman99f89552006-06-26 00:25:55 -07002295 if (p) {
2296 rcu_read_lock();
2297 stat->nlink += get_nr_threads(p);
2298 rcu_read_unlock();
2299 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002300 }
2301
2302 return 0;
2303}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002304
2305static struct inode_operations proc_task_inode_operations = {
2306 .lookup = proc_task_lookup,
2307 .getattr = proc_task_getattr,
2308 .setattr = proc_setattr,
2309};
2310
2311static struct file_operations proc_task_operations = {
2312 .read = generic_read_dir,
2313 .readdir = proc_task_readdir,
2314};