blob: ec158dd02b3ac2b758fbbf527820c1f0f83a486a [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>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#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;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080096 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080097 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070098 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
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800355static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700356 .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);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800368 struct mnt_namespace *ns = 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);
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800374 if (task->nsproxy) {
375 ns = task->nsproxy->mnt_ns;
376 if (ns)
377 get_mnt_ns(ns);
378 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700379 task_unlock(task);
380 put_task_struct(task);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800383 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500384 ret = -ENOMEM;
385 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
386 if (p) {
387 file->private_data = &p->m;
388 ret = seq_open(file, &mounts_op);
389 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800390 p->m.private = ns;
391 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500392 return 0;
393 }
394 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800396 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 return ret;
399}
400
401static int mounts_release(struct inode *inode, struct file *file)
402{
403 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800404 struct mnt_namespace *ns = m->private;
405 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return seq_release(inode, file);
407}
408
Al Viro5addc5d2005-11-07 17:15:49 -0500409static unsigned mounts_poll(struct file *file, poll_table *wait)
410{
411 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800412 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500413 unsigned res = 0;
414
415 poll_wait(file, &ns->poll, wait);
416
417 spin_lock(&vfsmount_lock);
418 if (p->event != ns->event) {
419 p->event = ns->event;
420 res = POLLERR;
421 }
422 spin_unlock(&vfsmount_lock);
423
424 return res;
425}
426
Arjan van de Ven00977a52007-02-12 00:55:34 -0800427static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .open = mounts_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500432 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Chuck Leverb4629fe2006-03-20 13:44:12 -0500435extern struct seq_operations mountstats_op;
436static int mountstats_open(struct inode *inode, struct file *file)
437{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500438 int ret = seq_open(file, &mountstats_op);
439
440 if (!ret) {
441 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800442 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700443 struct task_struct *task = get_proc_task(inode);
444
445 if (task) {
446 task_lock(task);
Vasily Tarasov701e0542006-11-25 11:09:22 -0800447 if (task->nsproxy)
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800448 mnt_ns = task->nsproxy->mnt_ns;
449 if (mnt_ns)
450 get_mnt_ns(mnt_ns);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700451 task_unlock(task);
452 put_task_struct(task);
453 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500454
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800455 if (mnt_ns)
456 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500457 else {
458 seq_release(inode, file);
459 ret = -EINVAL;
460 }
461 }
462 return ret;
463}
464
Arjan van de Ven00977a52007-02-12 00:55:34 -0800465static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500466 .open = mountstats_open,
467 .read = seq_read,
468 .llseek = seq_lseek,
469 .release = mounts_release,
470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
473
474static ssize_t proc_info_read(struct file * file, char __user * buf,
475 size_t count, loff_t *ppos)
476{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800477 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned long page;
479 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700480 struct task_struct *task = get_proc_task(inode);
481
482 length = -ESRCH;
483 if (!task)
484 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (count > PROC_BLOCK_SIZE)
487 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700488
489 length = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!(page = __get_free_page(GFP_KERNEL)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 length = PROC_I(inode)->op.proc_read(task, (char*)page);
494
495 if (length >= 0)
496 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
497 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700498out:
499 put_task_struct(task);
500out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return length;
502}
503
Arjan van de Ven00977a52007-02-12 00:55:34 -0800504static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .read = proc_info_read,
506};
507
508static int mem_open(struct inode* inode, struct file* file)
509{
510 file->private_data = (void*)((long)current->self_exec_id);
511 return 0;
512}
513
514static ssize_t mem_read(struct file * file, char __user * buf,
515 size_t count, loff_t *ppos)
516{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800517 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 char *page;
519 unsigned long src = *ppos;
520 int ret = -ESRCH;
521 struct mm_struct *mm;
522
Eric W. Biederman99f89552006-06-26 00:25:55 -0700523 if (!task)
524 goto out_no_task;
525
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700526 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto out;
528
529 ret = -ENOMEM;
530 page = (char *)__get_free_page(GFP_USER);
531 if (!page)
532 goto out;
533
534 ret = 0;
535
536 mm = get_task_mm(task);
537 if (!mm)
538 goto out_free;
539
540 ret = -EIO;
541
542 if (file->private_data != (void*)((long)current->self_exec_id))
543 goto out_put;
544
545 ret = 0;
546
547 while (count > 0) {
548 int this_len, retval;
549
550 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
551 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700552 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!ret)
554 ret = -EIO;
555 break;
556 }
557
558 if (copy_to_user(buf, page, retval)) {
559 ret = -EFAULT;
560 break;
561 }
562
563 ret += retval;
564 src += retval;
565 buf += retval;
566 count -= retval;
567 }
568 *ppos = src;
569
570out_put:
571 mmput(mm);
572out_free:
573 free_page((unsigned long) page);
574out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700575 put_task_struct(task);
576out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return ret;
578}
579
580#define mem_write NULL
581
582#ifndef mem_write
583/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800584static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 size_t count, loff_t *ppos)
586{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700587 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800589 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 unsigned long dst = *ppos;
591
Eric W. Biederman99f89552006-06-26 00:25:55 -0700592 copied = -ESRCH;
593 if (!task)
594 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Eric W. Biederman99f89552006-06-26 00:25:55 -0700596 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
597 goto out;
598
599 copied = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 page = (char *)__get_free_page(GFP_USER);
601 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700604 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 while (count > 0) {
606 int this_len, retval;
607
608 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
609 if (copy_from_user(page, buf, this_len)) {
610 copied = -EFAULT;
611 break;
612 }
613 retval = access_process_vm(task, dst, page, this_len, 1);
614 if (!retval) {
615 if (!copied)
616 copied = -EIO;
617 break;
618 }
619 copied += retval;
620 buf += retval;
621 dst += retval;
622 count -= retval;
623 }
624 *ppos = dst;
625 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700626out:
627 put_task_struct(task);
628out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return copied;
630}
631#endif
632
633static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
634{
635 switch (orig) {
636 case 0:
637 file->f_pos = offset;
638 break;
639 case 1:
640 file->f_pos += offset;
641 break;
642 default:
643 return -EINVAL;
644 }
645 force_successful_syscall_return();
646 return file->f_pos;
647}
648
Arjan van de Ven00977a52007-02-12 00:55:34 -0800649static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .llseek = mem_lseek,
651 .read = mem_read,
652 .write = mem_write,
653 .open = mem_open,
654};
655
656static ssize_t oom_adjust_read(struct file *file, char __user *buf,
657 size_t count, loff_t *ppos)
658{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800659 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700660 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700662 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 loff_t __ppos = *ppos;
664
Eric W. Biederman99f89552006-06-26 00:25:55 -0700665 if (!task)
666 return -ESRCH;
667 oom_adjust = task->oomkilladj;
668 put_task_struct(task);
669
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700670 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (__ppos >= len)
672 return 0;
673 if (count > len-__ppos)
674 count = len-__ppos;
675 if (copy_to_user(buf, buffer + __ppos, count))
676 return -EFAULT;
677 *ppos = __ppos + count;
678 return count;
679}
680
681static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
682 size_t count, loff_t *ppos)
683{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700684 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700685 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int oom_adjust;
687
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700688 memset(buffer, 0, sizeof(buffer));
689 if (count > sizeof(buffer) - 1)
690 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (copy_from_user(buffer, buf, count))
692 return -EFAULT;
693 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700694 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
695 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697 if (*end == '\n')
698 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800699 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700700 if (!task)
701 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800702 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
703 put_task_struct(task);
704 return -EACCES;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700707 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (end - buffer == 0)
709 return -EIO;
710 return end - buffer;
711}
712
Arjan van de Ven00977a52007-02-12 00:55:34 -0800713static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 .read = oom_adjust_read,
715 .write = oom_adjust_write,
716};
717
David Rientjesb813e932007-05-06 14:49:24 -0700718static ssize_t clear_refs_write(struct file *file, const char __user *buf,
719 size_t count, loff_t *ppos)
720{
721 struct task_struct *task;
722 char buffer[PROC_NUMBUF], *end;
723 struct mm_struct *mm;
724
725 memset(buffer, 0, sizeof(buffer));
726 if (count > sizeof(buffer) - 1)
727 count = sizeof(buffer) - 1;
728 if (copy_from_user(buffer, buf, count))
729 return -EFAULT;
730 if (!simple_strtol(buffer, &end, 0))
731 return -EINVAL;
732 if (*end == '\n')
733 end++;
734 task = get_proc_task(file->f_path.dentry->d_inode);
735 if (!task)
736 return -ESRCH;
737 mm = get_task_mm(task);
738 if (mm) {
739 clear_refs_smap(mm);
740 mmput(mm);
741 }
742 put_task_struct(task);
743 if (end - buffer == 0)
744 return -EIO;
745 return end - buffer;
746}
747
748static struct file_operations proc_clear_refs_operations = {
749 .write = clear_refs_write,
750};
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#ifdef CONFIG_AUDITSYSCALL
753#define TMPBUFLEN 21
754static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
755 size_t count, loff_t *ppos)
756{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800757 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700758 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ssize_t length;
760 char tmpbuf[TMPBUFLEN];
761
Eric W. Biederman99f89552006-06-26 00:25:55 -0700762 if (!task)
763 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
765 audit_get_loginuid(task->audit_context));
Eric W. Biederman99f89552006-06-26 00:25:55 -0700766 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
768}
769
770static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
771 size_t count, loff_t *ppos)
772{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800773 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 char *page, *tmp;
775 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 uid_t loginuid;
777
778 if (!capable(CAP_AUDIT_CONTROL))
779 return -EPERM;
780
Eric W. Biederman13b41b02006-06-26 00:25:56 -0700781 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EPERM;
783
Al Viroe0182902006-05-18 08:28:02 -0400784 if (count >= PAGE_SIZE)
785 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (*ppos != 0) {
788 /* No partial writes. */
789 return -EINVAL;
790 }
791 page = (char*)__get_free_page(GFP_USER);
792 if (!page)
793 return -ENOMEM;
794 length = -EFAULT;
795 if (copy_from_user(page, buf, count))
796 goto out_free_page;
797
Al Viroe0182902006-05-18 08:28:02 -0400798 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 loginuid = simple_strtoul(page, &tmp, 10);
800 if (tmp == page) {
801 length = -EINVAL;
802 goto out_free_page;
803
804 }
Eric W. Biederman99f89552006-06-26 00:25:55 -0700805 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (likely(length == 0))
807 length = count;
808
809out_free_page:
810 free_page((unsigned long) page);
811 return length;
812}
813
Arjan van de Ven00977a52007-02-12 00:55:34 -0800814static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 .read = proc_loginuid_read,
816 .write = proc_loginuid_write,
817};
818#endif
819
820#ifdef CONFIG_SECCOMP
821static ssize_t seccomp_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos)
823{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700824 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 char __buf[20];
826 loff_t __ppos = *ppos;
827 size_t len;
828
Eric W. Biederman99f89552006-06-26 00:25:55 -0700829 if (!tsk)
830 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* no need to print the trailing zero, so use only len */
832 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700833 put_task_struct(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (__ppos >= len)
835 return 0;
836 if (count > len - __ppos)
837 count = len - __ppos;
838 if (copy_to_user(buf, __buf + __ppos, count))
839 return -EFAULT;
840 *ppos = __ppos + count;
841 return count;
842}
843
844static ssize_t seccomp_write(struct file *file, const char __user *buf,
845 size_t count, loff_t *ppos)
846{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700847 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 char __buf[20], *end;
849 unsigned int seccomp_mode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700850 ssize_t result;
851
852 result = -ESRCH;
853 if (!tsk)
854 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* can set it only once to be even more secure */
Eric W. Biederman99f89552006-06-26 00:25:55 -0700857 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (unlikely(tsk->seccomp.mode))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700859 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Eric W. Biederman99f89552006-06-26 00:25:55 -0700861 result = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 memset(__buf, 0, sizeof(__buf));
863 count = min(count, sizeof(__buf) - 1);
864 if (copy_from_user(__buf, buf, count))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700865 goto out;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 seccomp_mode = simple_strtoul(__buf, &end, 0);
868 if (*end == '\n')
869 end++;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700870 result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
872 tsk->seccomp.mode = seccomp_mode;
873 set_tsk_thread_flag(tsk, TIF_SECCOMP);
874 } else
Eric W. Biederman99f89552006-06-26 00:25:55 -0700875 goto out;
876 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (unlikely(!(end - __buf)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700878 goto out;
879 result = end - __buf;
880out:
881 put_task_struct(tsk);
882out_no_task:
883 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Arjan van de Ven00977a52007-02-12 00:55:34 -0800886static const struct file_operations proc_seccomp_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 .read = seccomp_read,
888 .write = seccomp_write,
889};
890#endif /* CONFIG_SECCOMP */
891
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800892#ifdef CONFIG_FAULT_INJECTION
893static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
894 size_t count, loff_t *ppos)
895{
896 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
897 char buffer[PROC_NUMBUF];
898 size_t len;
899 int make_it_fail;
900 loff_t __ppos = *ppos;
901
902 if (!task)
903 return -ESRCH;
904 make_it_fail = task->make_it_fail;
905 put_task_struct(task);
906
907 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
908 if (__ppos >= len)
909 return 0;
910 if (count > len-__ppos)
911 count = len-__ppos;
912 if (copy_to_user(buf, buffer + __ppos, count))
913 return -EFAULT;
914 *ppos = __ppos + count;
915 return count;
916}
917
918static ssize_t proc_fault_inject_write(struct file * file,
919 const char __user * buf, size_t count, loff_t *ppos)
920{
921 struct task_struct *task;
922 char buffer[PROC_NUMBUF], *end;
923 int make_it_fail;
924
925 if (!capable(CAP_SYS_RESOURCE))
926 return -EPERM;
927 memset(buffer, 0, sizeof(buffer));
928 if (count > sizeof(buffer) - 1)
929 count = sizeof(buffer) - 1;
930 if (copy_from_user(buffer, buf, count))
931 return -EFAULT;
932 make_it_fail = simple_strtol(buffer, &end, 0);
933 if (*end == '\n')
934 end++;
935 task = get_proc_task(file->f_dentry->d_inode);
936 if (!task)
937 return -ESRCH;
938 task->make_it_fail = make_it_fail;
939 put_task_struct(task);
940 if (end - buffer == 0)
941 return -EIO;
942 return end - buffer;
943}
944
Arjan van de Ven00977a52007-02-12 00:55:34 -0800945static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800946 .read = proc_fault_inject_read,
947 .write = proc_fault_inject_write,
948};
949#endif
950
Al Viro008b1502005-08-20 00:17:39 +0100951static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 struct inode *inode = dentry->d_inode;
954 int error = -EACCES;
955
956 /* We don't need a base pointer in the /proc filesystem */
957 path_release(nd);
958
Eric W. Biederman778c1142006-06-26 00:25:58 -0700959 /* Are we allowed to snoop on the tasks file descriptors? */
960 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
964 nd->last_type = LAST_BIND;
965out:
Al Viro008b1502005-08-20 00:17:39 +0100966 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
970 char __user *buffer, int buflen)
971{
972 struct inode * inode;
973 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
974 int len;
975
976 if (!tmp)
977 return -ENOMEM;
978
979 inode = dentry->d_inode;
980 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
981 len = PTR_ERR(path);
982 if (IS_ERR(path))
983 goto out;
984 len = tmp + PAGE_SIZE - 1 - path;
985
986 if (len > buflen)
987 len = buflen;
988 if (copy_to_user(buffer, path, len))
989 len = -EFAULT;
990 out:
991 free_page((unsigned long)tmp);
992 return len;
993}
994
995static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
996{
997 int error = -EACCES;
998 struct inode *inode = dentry->d_inode;
999 struct dentry *de;
1000 struct vfsmount *mnt = NULL;
1001
Eric W. Biederman778c1142006-06-26 00:25:58 -07001002 /* Are we allowed to snoop on the tasks file descriptors? */
1003 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1007 if (error)
1008 goto out;
1009
1010 error = do_proc_readlink(de, mnt, buffer, buflen);
1011 dput(de);
1012 mntput(mnt);
1013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return error;
1015}
1016
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001017static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001019 .follow_link = proc_pid_follow_link,
1020 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021};
1022
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001023
1024/* building an inode */
1025
1026static int task_dumpable(struct task_struct *task)
1027{
1028 int dumpable = 0;
1029 struct mm_struct *mm;
1030
1031 task_lock(task);
1032 mm = task->mm;
1033 if (mm)
1034 dumpable = mm->dumpable;
1035 task_unlock(task);
1036 if(dumpable == 1)
1037 return 1;
1038 return 0;
1039}
1040
1041
Eric W. Biederman61a28782006-10-02 02:18:49 -07001042static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001043{
1044 struct inode * inode;
1045 struct proc_inode *ei;
1046
1047 /* We need a new inode */
1048
1049 inode = new_inode(sb);
1050 if (!inode)
1051 goto out;
1052
1053 /* Common stuff */
1054 ei = PROC_I(inode);
1055 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001056 inode->i_op = &proc_def_inode_operations;
1057
1058 /*
1059 * grab the reference to task.
1060 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001061 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001062 if (!ei->pid)
1063 goto out_unlock;
1064
1065 inode->i_uid = 0;
1066 inode->i_gid = 0;
1067 if (task_dumpable(task)) {
1068 inode->i_uid = task->euid;
1069 inode->i_gid = task->egid;
1070 }
1071 security_task_to_inode(task, inode);
1072
1073out:
1074 return inode;
1075
1076out_unlock:
1077 iput(inode);
1078 return NULL;
1079}
1080
1081static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1082{
1083 struct inode *inode = dentry->d_inode;
1084 struct task_struct *task;
1085 generic_fillattr(inode, stat);
1086
1087 rcu_read_lock();
1088 stat->uid = 0;
1089 stat->gid = 0;
1090 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1091 if (task) {
1092 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1093 task_dumpable(task)) {
1094 stat->uid = task->euid;
1095 stat->gid = task->egid;
1096 }
1097 }
1098 rcu_read_unlock();
1099 return 0;
1100}
1101
1102/* dentry stuff */
1103
1104/*
1105 * Exceptional case: normally we are not allowed to unhash a busy
1106 * directory. In this case, however, we can do it - no aliasing problems
1107 * due to the way we treat inodes.
1108 *
1109 * Rewrite the inode's ownerships here because the owning task may have
1110 * performed a setuid(), etc.
1111 *
1112 * Before the /proc/pid/status file was created the only way to read
1113 * the effective uid of a /process was to stat /proc/pid. Reading
1114 * /proc/pid/status is slow enough that procps and other packages
1115 * kept stating /proc/pid. To keep the rules in /proc simple I have
1116 * made this apply to all per process world readable and executable
1117 * directories.
1118 */
1119static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1120{
1121 struct inode *inode = dentry->d_inode;
1122 struct task_struct *task = get_proc_task(inode);
1123 if (task) {
1124 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1125 task_dumpable(task)) {
1126 inode->i_uid = task->euid;
1127 inode->i_gid = task->egid;
1128 } else {
1129 inode->i_uid = 0;
1130 inode->i_gid = 0;
1131 }
1132 inode->i_mode &= ~(S_ISUID | S_ISGID);
1133 security_task_to_inode(task, inode);
1134 put_task_struct(task);
1135 return 1;
1136 }
1137 d_drop(dentry);
1138 return 0;
1139}
1140
1141static int pid_delete_dentry(struct dentry * dentry)
1142{
1143 /* Is the task we represent dead?
1144 * If so, then don't put the dentry on the lru list,
1145 * kill it immediately.
1146 */
1147 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1148}
1149
1150static struct dentry_operations pid_dentry_operations =
1151{
1152 .d_revalidate = pid_revalidate,
1153 .d_delete = pid_delete_dentry,
1154};
1155
1156/* Lookups */
1157
Eric W. Biederman61a28782006-10-02 02:18:49 -07001158typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1159
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001160/*
1161 * Fill a directory entry.
1162 *
1163 * If possible create the dcache entry and derive our inode number and
1164 * file type from dcache entry.
1165 *
1166 * Since all of the proc inode numbers are dynamically generated, the inode
1167 * numbers do not exist until the inode is cache. This means creating the
1168 * the dcache entry in readdir is necessary to keep the inode numbers
1169 * reported by readdir in sync with the inode numbers reported
1170 * by stat.
1171 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001172static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1173 char *name, int len,
1174 instantiate_t instantiate, struct task_struct *task, void *ptr)
1175{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001176 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001177 struct inode *inode;
1178 struct qstr qname;
1179 ino_t ino = 0;
1180 unsigned type = DT_UNKNOWN;
1181
1182 qname.name = name;
1183 qname.len = len;
1184 qname.hash = full_name_hash(name, len);
1185
1186 child = d_lookup(dir, &qname);
1187 if (!child) {
1188 struct dentry *new;
1189 new = d_alloc(dir, &qname);
1190 if (new) {
1191 child = instantiate(dir->d_inode, new, task, ptr);
1192 if (child)
1193 dput(new);
1194 else
1195 child = new;
1196 }
1197 }
1198 if (!child || IS_ERR(child) || !child->d_inode)
1199 goto end_instantiate;
1200 inode = child->d_inode;
1201 if (inode) {
1202 ino = inode->i_ino;
1203 type = inode->i_mode >> 12;
1204 }
1205 dput(child);
1206end_instantiate:
1207 if (!ino)
1208 ino = find_inode_number(dir, &qname);
1209 if (!ino)
1210 ino = 1;
1211 return filldir(dirent, name, len, filp->f_pos, ino, type);
1212}
1213
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001214static unsigned name_to_int(struct dentry *dentry)
1215{
1216 const char *name = dentry->d_name.name;
1217 int len = dentry->d_name.len;
1218 unsigned n = 0;
1219
1220 if (len > 1 && *name == '0')
1221 goto out;
1222 while (len-- > 0) {
1223 unsigned c = *name++ - '0';
1224 if (c > 9)
1225 goto out;
1226 if (n >= (~0U-9)/10)
1227 goto out;
1228 n *= 10;
1229 n += c;
1230 }
1231 return n;
1232out:
1233 return ~0U;
1234}
1235
1236static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1237{
1238 struct task_struct *task = get_proc_task(inode);
1239 struct files_struct *files = NULL;
1240 struct file *file;
1241 int fd = proc_fd(inode);
1242
1243 if (task) {
1244 files = get_files_struct(task);
1245 put_task_struct(task);
1246 }
1247 if (files) {
1248 /*
1249 * We are not taking a ref to the file structure, so we must
1250 * hold ->file_lock.
1251 */
1252 spin_lock(&files->file_lock);
1253 file = fcheck_files(files, fd);
1254 if (file) {
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001255 *mnt = mntget(file->f_path.mnt);
1256 *dentry = dget(file->f_path.dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001257 spin_unlock(&files->file_lock);
1258 put_files_struct(files);
1259 return 0;
1260 }
1261 spin_unlock(&files->file_lock);
1262 put_files_struct(files);
1263 }
1264 return -ENOENT;
1265}
1266
1267static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1268{
1269 struct inode *inode = dentry->d_inode;
1270 struct task_struct *task = get_proc_task(inode);
1271 int fd = proc_fd(inode);
1272 struct files_struct *files;
1273
1274 if (task) {
1275 files = get_files_struct(task);
1276 if (files) {
1277 rcu_read_lock();
1278 if (fcheck_files(files, fd)) {
1279 rcu_read_unlock();
1280 put_files_struct(files);
1281 if (task_dumpable(task)) {
1282 inode->i_uid = task->euid;
1283 inode->i_gid = task->egid;
1284 } else {
1285 inode->i_uid = 0;
1286 inode->i_gid = 0;
1287 }
1288 inode->i_mode &= ~(S_ISUID | S_ISGID);
1289 security_task_to_inode(task, inode);
1290 put_task_struct(task);
1291 return 1;
1292 }
1293 rcu_read_unlock();
1294 put_files_struct(files);
1295 }
1296 put_task_struct(task);
1297 }
1298 d_drop(dentry);
1299 return 0;
1300}
1301
1302static struct dentry_operations tid_fd_dentry_operations =
1303{
1304 .d_revalidate = tid_fd_revalidate,
1305 .d_delete = pid_delete_dentry,
1306};
1307
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001308static struct dentry *proc_fd_instantiate(struct inode *dir,
1309 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001310{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001311 unsigned fd = *(unsigned *)ptr;
1312 struct file *file;
1313 struct files_struct *files;
1314 struct inode *inode;
1315 struct proc_inode *ei;
1316 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001317
Eric W. Biederman61a28782006-10-02 02:18:49 -07001318 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001319 if (!inode)
1320 goto out;
1321 ei = PROC_I(inode);
1322 ei->fd = fd;
1323 files = get_files_struct(task);
1324 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001325 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001326 inode->i_mode = S_IFLNK;
1327
1328 /*
1329 * We are not taking a ref to the file structure, so we must
1330 * hold ->file_lock.
1331 */
1332 spin_lock(&files->file_lock);
1333 file = fcheck_files(files, fd);
1334 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001335 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001336 if (file->f_mode & 1)
1337 inode->i_mode |= S_IRUSR | S_IXUSR;
1338 if (file->f_mode & 2)
1339 inode->i_mode |= S_IWUSR | S_IXUSR;
1340 spin_unlock(&files->file_lock);
1341 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001342
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001343 inode->i_op = &proc_pid_link_inode_operations;
1344 inode->i_size = 64;
1345 ei->op.proc_get_link = proc_fd_link;
1346 dentry->d_op = &tid_fd_dentry_operations;
1347 d_add(dentry, inode);
1348 /* Close the race of the process dying before we return the dentry */
1349 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001350 error = NULL;
1351
1352 out:
1353 return error;
1354out_unlock:
1355 spin_unlock(&files->file_lock);
1356 put_files_struct(files);
1357out_iput:
1358 iput(inode);
1359 goto out;
1360}
1361
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001362static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1363{
1364 struct task_struct *task = get_proc_task(dir);
1365 unsigned fd = name_to_int(dentry);
1366 struct dentry *result = ERR_PTR(-ENOENT);
1367
1368 if (!task)
1369 goto out_no_task;
1370 if (fd == ~0U)
1371 goto out;
1372
1373 result = proc_fd_instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001374out:
1375 put_task_struct(task);
1376out_no_task:
1377 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001378}
1379
Eric W. Biederman61a28782006-10-02 02:18:49 -07001380static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1381 struct task_struct *task, int fd)
1382{
1383 char name[PROC_NUMBUF];
1384 int len = snprintf(name, sizeof(name), "%d", fd);
1385 return proc_fill_cache(filp, dirent, filldir, name, len,
1386 proc_fd_instantiate, task, &fd);
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1390{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001391 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001392 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001393 struct task_struct *p = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned int fd, tid, ino;
1395 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001397 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001400 if (!p)
1401 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 retval = 0;
1403 tid = p->pid;
1404
1405 fd = filp->f_pos;
1406 switch (fd) {
1407 case 0:
1408 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1409 goto out;
1410 filp->f_pos++;
1411 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001412 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1414 goto out;
1415 filp->f_pos++;
1416 default:
1417 files = get_files_struct(p);
1418 if (!files)
1419 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001420 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001421 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001423 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 fd++, filp->f_pos++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (!fcheck_files(files, fd))
1427 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001428 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Eric W. Biederman61a28782006-10-02 02:18:49 -07001430 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001431 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001434 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001436 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 put_files_struct(files);
1438 }
1439out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001440 put_task_struct(p);
1441out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return retval;
1443}
1444
Arjan van de Ven00977a52007-02-12 00:55:34 -08001445static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 .read = generic_read_dir,
1447 .readdir = proc_readfd,
1448};
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/*
1451 * proc directories can do almost nothing..
1452 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001453static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 .lookup = proc_lookupfd,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001455 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456};
1457
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001458static struct dentry *proc_pident_instantiate(struct inode *dir,
1459 struct dentry *dentry, struct task_struct *task, void *ptr)
1460{
1461 struct pid_entry *p = ptr;
1462 struct inode *inode;
1463 struct proc_inode *ei;
1464 struct dentry *error = ERR_PTR(-EINVAL);
1465
Eric W. Biederman61a28782006-10-02 02:18:49 -07001466 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001467 if (!inode)
1468 goto out;
1469
1470 ei = PROC_I(inode);
1471 inode->i_mode = p->mode;
1472 if (S_ISDIR(inode->i_mode))
1473 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1474 if (p->iop)
1475 inode->i_op = p->iop;
1476 if (p->fop)
1477 inode->i_fop = p->fop;
1478 ei->op = p->op;
1479 dentry->d_op = &pid_dentry_operations;
1480 d_add(dentry, inode);
1481 /* Close the race of the process dying before we return the dentry */
1482 if (pid_revalidate(dentry, NULL))
1483 error = NULL;
1484out:
1485 return error;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static struct dentry *proc_pident_lookup(struct inode *dir,
1489 struct dentry *dentry,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001490 struct pid_entry *ents,
1491 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001494 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001495 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001496 struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001498 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 inode = NULL;
1500
Eric W. Biederman99f89552006-06-26 00:25:55 -07001501 if (!task)
1502 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001504 /*
1505 * Yes, it does not scale. And it should not. Don't add
1506 * new entries into /proc/<tgid>/ without very good reasons.
1507 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001508 last = &ents[nents - 1];
1509 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (p->len != dentry->d_name.len)
1511 continue;
1512 if (!memcmp(dentry->d_name.name, p->name, p->len))
1513 break;
1514 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001515 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 goto out;
1517
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001518 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001520 put_task_struct(task);
1521out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001522 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Eric W. Biederman61a28782006-10-02 02:18:49 -07001525static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1526 struct task_struct *task, struct pid_entry *p)
1527{
1528 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1529 proc_pident_instantiate, task, p);
1530}
1531
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001532static int proc_pident_readdir(struct file *filp,
1533 void *dirent, filldir_t filldir,
1534 struct pid_entry *ents, unsigned int nents)
1535{
1536 int i;
1537 int pid;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001538 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001539 struct inode *inode = dentry->d_inode;
1540 struct task_struct *task = get_proc_task(inode);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001541 struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542 ino_t ino;
1543 int ret;
1544
1545 ret = -ENOENT;
1546 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001547 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548
1549 ret = 0;
1550 pid = task->pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001551 i = filp->f_pos;
1552 switch (i) {
1553 case 0:
1554 ino = inode->i_ino;
1555 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1556 goto out;
1557 i++;
1558 filp->f_pos++;
1559 /* fall through */
1560 case 1:
1561 ino = parent_ino(dentry);
1562 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1563 goto out;
1564 i++;
1565 filp->f_pos++;
1566 /* fall through */
1567 default:
1568 i -= 2;
1569 if (i >= nents) {
1570 ret = 1;
1571 goto out;
1572 }
1573 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001574 last = &ents[nents - 1];
1575 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001576 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001577 goto out;
1578 filp->f_pos++;
1579 p++;
1580 }
1581 }
1582
1583 ret = 1;
1584out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001585 put_task_struct(task);
1586out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001591static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1592 size_t count, loff_t *ppos)
1593{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001594 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001595 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001596 ssize_t length;
1597 struct task_struct *task = get_proc_task(inode);
1598
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001600 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001601
1602 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001603 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001604 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001605 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001606 if (length > 0)
1607 length = simple_read_from_buffer(buf, count, ppos, p, length);
1608 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001609 return length;
1610}
1611
1612static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1613 size_t count, loff_t *ppos)
1614{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001615 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001616 char *page;
1617 ssize_t length;
1618 struct task_struct *task = get_proc_task(inode);
1619
1620 length = -ESRCH;
1621 if (!task)
1622 goto out_no_task;
1623 if (count > PAGE_SIZE)
1624 count = PAGE_SIZE;
1625
1626 /* No partial writes. */
1627 length = -EINVAL;
1628 if (*ppos != 0)
1629 goto out;
1630
1631 length = -ENOMEM;
1632 page = (char*)__get_free_page(GFP_USER);
1633 if (!page)
1634 goto out;
1635
1636 length = -EFAULT;
1637 if (copy_from_user(page, buf, count))
1638 goto out_free;
1639
1640 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001641 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642 (void*)page, count);
1643out_free:
1644 free_page((unsigned long) page);
1645out:
1646 put_task_struct(task);
1647out_no_task:
1648 return length;
1649}
1650
Arjan van de Ven00977a52007-02-12 00:55:34 -08001651static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001652 .read = proc_pid_attr_read,
1653 .write = proc_pid_attr_write,
1654};
1655
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001656static struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001657 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1658 REG("prev", S_IRUGO, pid_attr),
1659 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1660 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1661 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1662 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001663};
1664
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001665static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 void * dirent, filldir_t filldir)
1667{
1668 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001669 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Arjan van de Ven00977a52007-02-12 00:55:34 -08001672static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001674 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675};
1676
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001677static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct dentry *dentry, struct nameidata *nd)
1679{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001680 return proc_pident_lookup(dir, dentry,
1681 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001684static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001685 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07001686 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001687 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688};
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690#endif
1691
1692/*
1693 * /proc/self:
1694 */
1695static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1696 int buflen)
1697{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001698 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 sprintf(tmp, "%d", current->tgid);
1700 return vfs_readlink(dentry,buffer,buflen,tmp);
1701}
1702
Al Viro008b1502005-08-20 00:17:39 +01001703static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001705 char tmp[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sprintf(tmp, "%d", current->tgid);
Al Viro008b1502005-08-20 00:17:39 +01001707 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001710static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 .readlink = proc_self_readlink,
1712 .follow_link = proc_self_follow_link,
1713};
1714
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001715/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07001716 * proc base
1717 *
1718 * These are the directory entries in the root directory of /proc
1719 * that properly belong to the /proc filesystem, as they describe
1720 * describe something that is process related.
1721 */
1722static struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001723 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07001724 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07001725};
1726
1727/*
1728 * Exceptional case: normally we are not allowed to unhash a busy
1729 * directory. In this case, however, we can do it - no aliasing problems
1730 * due to the way we treat inodes.
1731 */
1732static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1733{
1734 struct inode *inode = dentry->d_inode;
1735 struct task_struct *task = get_proc_task(inode);
1736 if (task) {
1737 put_task_struct(task);
1738 return 1;
1739 }
1740 d_drop(dentry);
1741 return 0;
1742}
1743
1744static struct dentry_operations proc_base_dentry_operations =
1745{
1746 .d_revalidate = proc_base_revalidate,
1747 .d_delete = pid_delete_dentry,
1748};
1749
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001750static struct dentry *proc_base_instantiate(struct inode *dir,
1751 struct dentry *dentry, struct task_struct *task, void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07001752{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001753 struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001754 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001755 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001756 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001757
1758 /* Allocate the inode */
1759 error = ERR_PTR(-ENOMEM);
1760 inode = new_inode(dir->i_sb);
1761 if (!inode)
1762 goto out;
1763
1764 /* Initialize the inode */
1765 ei = PROC_I(inode);
1766 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07001767
1768 /*
1769 * grab the reference to the task.
1770 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001771 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07001772 if (!ei->pid)
1773 goto out_iput;
1774
1775 inode->i_uid = 0;
1776 inode->i_gid = 0;
1777 inode->i_mode = p->mode;
1778 if (S_ISDIR(inode->i_mode))
1779 inode->i_nlink = 2;
1780 if (S_ISLNK(inode->i_mode))
1781 inode->i_size = 64;
1782 if (p->iop)
1783 inode->i_op = p->iop;
1784 if (p->fop)
1785 inode->i_fop = p->fop;
1786 ei->op = p->op;
1787 dentry->d_op = &proc_base_dentry_operations;
1788 d_add(dentry, inode);
1789 error = NULL;
1790out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07001791 return error;
1792out_iput:
1793 iput(inode);
1794 goto out;
1795}
1796
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001797static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1798{
1799 struct dentry *error;
1800 struct task_struct *task = get_proc_task(dir);
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001801 struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001802
1803 error = ERR_PTR(-ENOENT);
1804
1805 if (!task)
1806 goto out_no_task;
1807
1808 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001809 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1810 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001811 if (p->len != dentry->d_name.len)
1812 continue;
1813 if (!memcmp(dentry->d_name.name, p->name, p->len))
1814 break;
1815 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001816 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001817 goto out;
1818
1819 error = proc_base_instantiate(dir, dentry, task, p);
1820
1821out:
1822 put_task_struct(task);
1823out_no_task:
1824 return error;
1825}
1826
Eric W. Biederman61a28782006-10-02 02:18:49 -07001827static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1828 struct task_struct *task, struct pid_entry *p)
1829{
1830 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1831 proc_base_instantiate, task, p);
1832}
1833
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001834#ifdef CONFIG_TASK_IO_ACCOUNTING
1835static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1836{
1837 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001838#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001839 "rchar: %llu\n"
1840 "wchar: %llu\n"
1841 "syscr: %llu\n"
1842 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001843#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001844 "read_bytes: %llu\n"
1845 "write_bytes: %llu\n"
1846 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001847#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001848 (unsigned long long)task->rchar,
1849 (unsigned long long)task->wchar,
1850 (unsigned long long)task->syscr,
1851 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08001852#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001853 (unsigned long long)task->ioac.read_bytes,
1854 (unsigned long long)task->ioac.write_bytes,
1855 (unsigned long long)task->ioac.cancelled_write_bytes);
1856}
1857#endif
1858
Eric W. Biederman801199c2006-10-02 02:18:48 -07001859/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001860 * Thread groups
1861 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08001862static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001863static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001864
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001865static struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001866 DIR("task", S_IRUGO|S_IXUGO, task),
1867 DIR("fd", S_IRUSR|S_IXUSR, fd),
1868 INF("environ", S_IRUSR, pid_environ),
1869 INF("auxv", S_IRUSR, pid_auxv),
1870 INF("status", S_IRUGO, pid_status),
1871 INF("cmdline", S_IRUGO, pid_cmdline),
1872 INF("stat", S_IRUGO, tgid_stat),
1873 INF("statm", S_IRUGO, pid_statm),
1874 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001878 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001879#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07001880 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001881#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001882 LNK("cwd", cwd),
1883 LNK("root", root),
1884 LNK("exe", exe),
1885 REG("mounts", S_IRUGO, mounts),
1886 REG("mountstats", S_IRUSR, mountstats),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001887#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07001888 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07001889 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001890#endif
1891#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001892 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001893#endif
1894#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001895 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001896#endif
1897#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001899#endif
1900#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07001901 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001902#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07001903 INF("oom_score", S_IRUGO, oom_score),
1904 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001905#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07001906 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001907#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001908#ifdef CONFIG_FAULT_INJECTION
1909 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1910#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08001911#ifdef CONFIG_TASK_IO_ACCOUNTING
1912 INF("io", S_IRUGO, pid_io_accounting),
1913#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001914};
1915
1916static int proc_tgid_base_readdir(struct file * filp,
1917 void * dirent, filldir_t filldir)
1918{
1919 return proc_pident_readdir(filp,dirent,filldir,
1920 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1921}
1922
Arjan van de Ven00977a52007-02-12 00:55:34 -08001923static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001924 .read = generic_read_dir,
1925 .readdir = proc_tgid_base_readdir,
1926};
1927
1928static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001929 return proc_pident_lookup(dir, dentry,
1930 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001931}
1932
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001933static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001934 .lookup = proc_tgid_base_lookup,
1935 .getattr = pid_getattr,
1936 .setattr = proc_setattr,
1937};
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/**
Eric W. Biederman48e64842006-06-26 00:25:48 -07001940 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001942 * @task: task that should be flushed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
Eric W. Biederman48e64842006-06-26 00:25:48 -07001944 * Looks in the dcache for
1945 * /proc/@pid
1946 * /proc/@tgid/task/@pid
1947 * if either directory is present flushes it and all of it'ts children
1948 * from the dcache.
1949 *
1950 * It is safe and reasonable to cache /proc entries for a task until
1951 * that task exits. After that they just clog up the dcache with
1952 * useless entries, possibly causing useful dcache entries to be
1953 * flushed instead. This routine is proved to flush those useless
1954 * dcache entries at process exit time.
1955 *
1956 * NOTE: This routine is just an optimization so it does not guarantee
1957 * that no dcache entries will exist at process exit time it
1958 * just makes it very unlikely that any will persist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Eric W. Biederman48e64842006-06-26 00:25:48 -07001960void proc_flush_task(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Eric W. Biederman48e64842006-06-26 00:25:48 -07001962 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001963 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07001964 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Eric W. Biederman48e64842006-06-26 00:25:48 -07001966 name.name = buf;
1967 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1968 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1969 if (dentry) {
1970 shrink_dcache_parent(dentry);
1971 d_drop(dentry);
1972 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Eric W. Biederman48e64842006-06-26 00:25:48 -07001975 if (thread_group_leader(task))
1976 goto out;
1977
1978 name.name = buf;
1979 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1980 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1981 if (!leader)
1982 goto out;
1983
1984 name.name = "task";
1985 name.len = strlen(name.name);
1986 dir = d_hash_and_lookup(leader, &name);
1987 if (!dir)
1988 goto out_put_leader;
1989
1990 name.name = buf;
1991 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1992 dentry = d_hash_and_lookup(dir, &name);
1993 if (dentry) {
1994 shrink_dcache_parent(dentry);
1995 d_drop(dentry);
1996 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07001998
1999 dput(dir);
2000out_put_leader:
2001 dput(leader);
2002out:
2003 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Adrian Bunk9711ef92006-12-06 20:38:31 -08002006static struct dentry *proc_pid_instantiate(struct inode *dir,
2007 struct dentry * dentry,
2008 struct task_struct *task, void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002009{
2010 struct dentry *error = ERR_PTR(-ENOENT);
2011 struct inode *inode;
2012
Eric W. Biederman61a28782006-10-02 02:18:49 -07002013 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002014 if (!inode)
2015 goto out;
2016
2017 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2018 inode->i_op = &proc_tgid_base_inode_operations;
2019 inode->i_fop = &proc_tgid_base_operations;
2020 inode->i_flags|=S_IMMUTABLE;
2021 inode->i_nlink = 4;
2022#ifdef CONFIG_SECURITY
2023 inode->i_nlink += 1;
2024#endif
2025
2026 dentry->d_op = &pid_dentry_operations;
2027
2028 d_add(dentry, inode);
2029 /* Close the race of the process dying before we return the dentry */
2030 if (pid_revalidate(dentry, NULL))
2031 error = NULL;
2032out:
2033 return error;
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2037{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002038 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 unsigned tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Eric W. Biederman801199c2006-10-02 02:18:48 -07002042 result = proc_base_lookup(dir, dentry);
2043 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2044 goto out;
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 tgid = name_to_int(dentry);
2047 if (tgid == ~0U)
2048 goto out;
2049
Eric W. Biedermande758732006-06-26 00:25:51 -07002050 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 task = find_task_by_pid(tgid);
2052 if (task)
2053 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002054 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (!task)
2056 goto out;
2057
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002058 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002059 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002061 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002065 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 */
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002068static struct task_struct *next_tgid(unsigned int tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002070 struct task_struct *task;
2071 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002073 rcu_read_lock();
2074retry:
2075 task = NULL;
2076 pid = find_ge_pid(tgid);
2077 if (pid) {
2078 tgid = pid->nr + 1;
2079 task = pid_task(pid, PIDTYPE_PID);
2080 /* What we to know is if the pid we have find is the
2081 * pid of a thread_group_leader. Testing for task
2082 * being a thread_group_leader is the obvious thing
2083 * todo but there is a window when it fails, due to
2084 * the pid transfer logic in de_thread.
2085 *
2086 * So we perform the straight forward test of seeing
2087 * if the pid we have found is the pid of a thread
2088 * group leader, and don't worry if the task we have
2089 * found doesn't happen to be a thread group leader.
2090 * As we don't care in the case of readdir.
2091 */
2092 if (!task || !has_group_leader_pid(task))
2093 goto retry;
2094 get_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002096 rcu_read_unlock();
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002097 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002100#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Eric W. Biederman61a28782006-10-02 02:18:49 -07002102static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2103 struct task_struct *task, int tgid)
2104{
2105 char name[PROC_NUMBUF];
2106 int len = snprintf(name, sizeof(name), "%d", tgid);
2107 return proc_fill_cache(filp, dirent, filldir, name, len,
2108 proc_pid_instantiate, task, NULL);
2109}
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111/* for the /proc/ directory itself, after non-process stuff has been done */
2112int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2113{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002115 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002116 struct task_struct *task;
2117 int tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Eric W. Biederman61a28782006-10-02 02:18:49 -07002119 if (!reaper)
2120 goto out_no_task;
2121
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002122 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric W. Biederman801199c2006-10-02 02:18:48 -07002123 struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002124 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002125 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002128 tgid = filp->f_pos - TGID_OFFSET;
2129 for (task = next_tgid(tgid);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002130 task;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002131 put_task_struct(task), task = next_tgid(tgid + 1)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002132 tgid = task->pid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002133 filp->f_pos = tgid + TGID_OFFSET;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002134 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002135 put_task_struct(task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002139 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2140out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002141 put_task_struct(reaper);
2142out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002146/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002147 * Tasks
2148 */
2149static struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002150 DIR("fd", S_IRUSR|S_IXUSR, fd),
2151 INF("environ", S_IRUSR, pid_environ),
2152 INF("auxv", S_IRUSR, pid_auxv),
2153 INF("status", S_IRUGO, pid_status),
2154 INF("cmdline", S_IRUGO, pid_cmdline),
2155 INF("stat", S_IRUGO, tid_stat),
2156 INF("statm", S_IRUGO, pid_statm),
2157 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002158#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002159 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002160#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002161 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002162#ifdef CONFIG_SECCOMP
Eric W. Biederman61a28782006-10-02 02:18:49 -07002163 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002164#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002165 LNK("cwd", cwd),
2166 LNK("root", root),
2167 LNK("exe", exe),
2168 REG("mounts", S_IRUGO, mounts),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002169#ifdef CONFIG_MMU
David Rientjesb813e932007-05-06 14:49:24 -07002170 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002171 REG("smaps", S_IRUGO, smaps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002172#endif
2173#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002174 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002175#endif
2176#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002177 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002178#endif
2179#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002180 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002181#endif
2182#ifdef CONFIG_CPUSETS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002183 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002184#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002185 INF("oom_score", S_IRUGO, oom_score),
2186 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002187#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002188 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002189#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002190#ifdef CONFIG_FAULT_INJECTION
2191 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2192#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002193};
2194
2195static int proc_tid_base_readdir(struct file * filp,
2196 void * dirent, filldir_t filldir)
2197{
2198 return proc_pident_readdir(filp,dirent,filldir,
2199 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2200}
2201
2202static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002203 return proc_pident_lookup(dir, dentry,
2204 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205}
2206
Arjan van de Ven00977a52007-02-12 00:55:34 -08002207static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002208 .read = generic_read_dir,
2209 .readdir = proc_tid_base_readdir,
2210};
2211
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002212static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213 .lookup = proc_tid_base_lookup,
2214 .getattr = pid_getattr,
2215 .setattr = proc_setattr,
2216};
2217
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002218static struct dentry *proc_task_instantiate(struct inode *dir,
2219 struct dentry *dentry, struct task_struct *task, void *ptr)
2220{
2221 struct dentry *error = ERR_PTR(-ENOENT);
2222 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002223 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002224
2225 if (!inode)
2226 goto out;
2227 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2228 inode->i_op = &proc_tid_base_inode_operations;
2229 inode->i_fop = &proc_tid_base_operations;
2230 inode->i_flags|=S_IMMUTABLE;
2231 inode->i_nlink = 3;
2232#ifdef CONFIG_SECURITY
2233 inode->i_nlink += 1;
2234#endif
2235
2236 dentry->d_op = &pid_dentry_operations;
2237
2238 d_add(dentry, inode);
2239 /* Close the race of the process dying before we return the dentry */
2240 if (pid_revalidate(dentry, NULL))
2241 error = NULL;
2242out:
2243 return error;
2244}
2245
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002246static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2247{
2248 struct dentry *result = ERR_PTR(-ENOENT);
2249 struct task_struct *task;
2250 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002251 unsigned tid;
2252
2253 if (!leader)
2254 goto out_no_task;
2255
2256 tid = name_to_int(dentry);
2257 if (tid == ~0U)
2258 goto out;
2259
2260 rcu_read_lock();
2261 task = find_task_by_pid(tid);
2262 if (task)
2263 get_task_struct(task);
2264 rcu_read_unlock();
2265 if (!task)
2266 goto out;
2267 if (leader->tgid != task->tgid)
2268 goto out_drop_task;
2269
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002270 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002271out_drop_task:
2272 put_task_struct(task);
2273out:
2274 put_task_struct(leader);
2275out_no_task:
2276 return result;
2277}
2278
2279/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002280 * Find the first tid of a thread group to return to user space.
2281 *
2282 * Usually this is just the thread group leader, but if the users
2283 * buffer was too small or there was a seek into the middle of the
2284 * directory we have more work todo.
2285 *
2286 * In the case of a short read we start with find_task_by_pid.
2287 *
2288 * In the case of a seek we start with the leader and walk nr
2289 * threads past it.
2290 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002291static struct task_struct *first_tid(struct task_struct *leader,
2292 int tid, int nr)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002293{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002294 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002295
Eric W. Biedermancc288732006-06-26 00:26:01 -07002296 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002297 /* Attempt to start with the pid of a thread */
2298 if (tid && (nr > 0)) {
2299 pos = find_task_by_pid(tid);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002300 if (pos && (pos->group_leader == leader))
2301 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002302 }
2303
2304 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002305 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002306 if (nr && nr >= get_nr_threads(leader))
2307 goto out;
2308
2309 /* If we haven't found our starting place yet start
2310 * with the leader and walk nr threads forward.
2311 */
2312 for (pos = leader; nr > 0; --nr) {
2313 pos = next_thread(pos);
2314 if (pos == leader) {
2315 pos = NULL;
2316 goto out;
2317 }
2318 }
2319found:
2320 get_task_struct(pos);
2321out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002322 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002323 return pos;
2324}
2325
2326/*
2327 * Find the next thread in the thread list.
2328 * Return NULL if there is an error or no next thread.
2329 *
2330 * The reference to the input task_struct is released.
2331 */
2332static struct task_struct *next_tid(struct task_struct *start)
2333{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002334 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002335 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002336 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002337 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002338 if (thread_group_leader(pos))
2339 pos = NULL;
2340 else
2341 get_task_struct(pos);
2342 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002343 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002344 put_task_struct(start);
2345 return pos;
2346}
2347
Eric W. Biederman61a28782006-10-02 02:18:49 -07002348static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2349 struct task_struct *task, int tid)
2350{
2351 char name[PROC_NUMBUF];
2352 int len = snprintf(name, sizeof(name), "%d", tid);
2353 return proc_fill_cache(filp, dirent, filldir, name, len,
2354 proc_task_instantiate, task, NULL);
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357/* for the /proc/TGID/task/ directories */
2358static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2359{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002360 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002362 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002363 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 int retval = -ENOENT;
2365 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002366 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2368
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002369 task = get_proc_task(inode);
2370 if (!task)
2371 goto out_no_task;
2372 rcu_read_lock();
2373 if (pid_alive(task)) {
2374 leader = task->group_leader;
2375 get_task_struct(leader);
2376 }
2377 rcu_read_unlock();
2378 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002379 if (!leader)
2380 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 retval = 0;
2382
2383 switch (pos) {
2384 case 0:
2385 ino = inode->i_ino;
2386 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2387 goto out;
2388 pos++;
2389 /* fall through */
2390 case 1:
2391 ino = parent_ino(dentry);
2392 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2393 goto out;
2394 pos++;
2395 /* fall through */
2396 }
2397
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002398 /* f_version caches the tgid value that the last readdir call couldn't
2399 * return. lseek aka telldir automagically resets f_version to 0.
2400 */
2401 tid = filp->f_version;
2402 filp->f_version = 0;
2403 for (task = first_tid(leader, tid, pos - 2);
2404 task;
2405 task = next_tid(task), pos++) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002406 tid = task->pid;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002407 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002408 /* returning this tgid failed, save it as the first
2409 * pid for the next readir call */
2410 filp->f_version = tid;
2411 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415out:
2416 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002417 put_task_struct(leader);
2418out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return retval;
2420}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002421
2422static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2423{
2424 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002425 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002426 generic_fillattr(inode, stat);
2427
Eric W. Biederman99f89552006-06-26 00:25:55 -07002428 if (p) {
2429 rcu_read_lock();
2430 stat->nlink += get_nr_threads(p);
2431 rcu_read_unlock();
2432 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002433 }
2434
2435 return 0;
2436}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002437
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002438static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002439 .lookup = proc_task_lookup,
2440 .getattr = proc_task_getattr,
2441 .setattr = proc_setattr,
2442};
2443
Arjan van de Ven00977a52007-02-12 00:55:34 -08002444static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002445 .read = generic_read_dir,
2446 .readdir = proc_task_readdir,
2447};