[PATCH] slab: removes local_irq_save()/local_irq_restore() pair
[deliverable/linux.git] / fs / proc / base.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
14 */
15
16#include <asm/uaccess.h>
17
18#include <linux/config.h>
19#include <linux/errno.h>
20#include <linux/time.h>
21#include <linux/proc_fs.h>
22#include <linux/stat.h>
23#include <linux/init.h>
24#include <linux/file.h>
25#include <linux/string.h>
26#include <linux/seq_file.h>
27#include <linux/namei.h>
28#include <linux/namespace.h>
29#include <linux/mm.h>
30#include <linux/smp_lock.h>
31#include <linux/kallsyms.h>
32#include <linux/mount.h>
33#include <linux/security.h>
34#include <linux/ptrace.h>
35#include <linux/seccomp.h>
36#include <linux/cpuset.h>
37#include <linux/audit.h>
38#include "internal.h"
39
40/*
41 * For hysterical raisins we keep the same inumbers as in the old procfs.
42 * Feel free to change the macro below - just keep the range distinct from
43 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
44 * As soon as we'll get a separate superblock we will be able to forget
45 * about magical ranges too.
46 */
47
48#define fake_ino(pid,ino) (((pid)<<16)|(ino))
49
50enum pid_directory_inos {
51 PROC_TGID_INO = 2,
52 PROC_TGID_TASK,
53 PROC_TGID_STATUS,
54 PROC_TGID_MEM,
55#ifdef CONFIG_SECCOMP
56 PROC_TGID_SECCOMP,
57#endif
58 PROC_TGID_CWD,
59 PROC_TGID_ROOT,
60 PROC_TGID_EXE,
61 PROC_TGID_FD,
62 PROC_TGID_ENVIRON,
63 PROC_TGID_AUXV,
64 PROC_TGID_CMDLINE,
65 PROC_TGID_STAT,
66 PROC_TGID_STATM,
67 PROC_TGID_MAPS,
6e21c8f1 68 PROC_TGID_NUMA_MAPS,
1da177e4
LT
69 PROC_TGID_MOUNTS,
70 PROC_TGID_WCHAN,
71#ifdef CONFIG_SCHEDSTATS
72 PROC_TGID_SCHEDSTAT,
73#endif
74#ifdef CONFIG_CPUSETS
75 PROC_TGID_CPUSET,
76#endif
77#ifdef CONFIG_SECURITY
78 PROC_TGID_ATTR,
79 PROC_TGID_ATTR_CURRENT,
80 PROC_TGID_ATTR_PREV,
81 PROC_TGID_ATTR_EXEC,
82 PROC_TGID_ATTR_FSCREATE,
83#endif
84#ifdef CONFIG_AUDITSYSCALL
85 PROC_TGID_LOGINUID,
86#endif
87 PROC_TGID_FD_DIR,
88 PROC_TGID_OOM_SCORE,
89 PROC_TGID_OOM_ADJUST,
90 PROC_TID_INO,
91 PROC_TID_STATUS,
92 PROC_TID_MEM,
93#ifdef CONFIG_SECCOMP
94 PROC_TID_SECCOMP,
95#endif
96 PROC_TID_CWD,
97 PROC_TID_ROOT,
98 PROC_TID_EXE,
99 PROC_TID_FD,
100 PROC_TID_ENVIRON,
101 PROC_TID_AUXV,
102 PROC_TID_CMDLINE,
103 PROC_TID_STAT,
104 PROC_TID_STATM,
105 PROC_TID_MAPS,
6e21c8f1 106 PROC_TID_NUMA_MAPS,
1da177e4
LT
107 PROC_TID_MOUNTS,
108 PROC_TID_WCHAN,
109#ifdef CONFIG_SCHEDSTATS
110 PROC_TID_SCHEDSTAT,
111#endif
112#ifdef CONFIG_CPUSETS
113 PROC_TID_CPUSET,
114#endif
115#ifdef CONFIG_SECURITY
116 PROC_TID_ATTR,
117 PROC_TID_ATTR_CURRENT,
118 PROC_TID_ATTR_PREV,
119 PROC_TID_ATTR_EXEC,
120 PROC_TID_ATTR_FSCREATE,
121#endif
122#ifdef CONFIG_AUDITSYSCALL
123 PROC_TID_LOGINUID,
124#endif
125 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
126 PROC_TID_OOM_SCORE,
127 PROC_TID_OOM_ADJUST,
128};
129
130struct pid_entry {
131 int type;
132 int len;
133 char *name;
134 mode_t mode;
135};
136
137#define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
138
139static struct pid_entry tgid_base_stuff[] = {
140 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
141 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
142 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
143 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
144 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
145 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
146 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
147 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
148 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
6e21c8f1
CL
149#ifdef CONFIG_NUMA
150 E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
151#endif
1da177e4
LT
152 E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
153#ifdef CONFIG_SECCOMP
154 E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
155#endif
156 E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
157 E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
158 E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
159 E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
160#ifdef CONFIG_SECURITY
161 E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
162#endif
163#ifdef CONFIG_KALLSYMS
164 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
165#endif
166#ifdef CONFIG_SCHEDSTATS
167 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
168#endif
169#ifdef CONFIG_CPUSETS
170 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
171#endif
172 E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
173 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
174#ifdef CONFIG_AUDITSYSCALL
175 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
176#endif
177 {0,0,NULL,0}
178};
179static struct pid_entry tid_base_stuff[] = {
180 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
181 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
182 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
183 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
184 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
185 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
186 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
187 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
6e21c8f1
CL
188#ifdef CONFIG_NUMA
189 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
190#endif
1da177e4
LT
191 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
192#ifdef CONFIG_SECCOMP
193 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
194#endif
195 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
196 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
197 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
198 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
199#ifdef CONFIG_SECURITY
200 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
201#endif
202#ifdef CONFIG_KALLSYMS
203 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
204#endif
205#ifdef CONFIG_SCHEDSTATS
206 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
207#endif
208#ifdef CONFIG_CPUSETS
209 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
210#endif
211 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
212 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
213#ifdef CONFIG_AUDITSYSCALL
214 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
215#endif
216 {0,0,NULL,0}
217};
218
219#ifdef CONFIG_SECURITY
220static struct pid_entry tgid_attr_stuff[] = {
221 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
222 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
223 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
224 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
225 {0,0,NULL,0}
226};
227static struct pid_entry tid_attr_stuff[] = {
228 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
229 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
230 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
231 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
232 {0,0,NULL,0}
233};
234#endif
235
236#undef E
237
238static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
239{
240 struct task_struct *task = proc_task(inode);
241 struct files_struct *files;
242 struct file *file;
243 int fd = proc_type(inode) - PROC_TID_FD_DIR;
244
245 files = get_files_struct(task);
246 if (files) {
247 spin_lock(&files->file_lock);
248 file = fcheck_files(files, fd);
249 if (file) {
250 *mnt = mntget(file->f_vfsmnt);
251 *dentry = dget(file->f_dentry);
252 spin_unlock(&files->file_lock);
253 put_files_struct(files);
254 return 0;
255 }
256 spin_unlock(&files->file_lock);
257 put_files_struct(files);
258 }
259 return -ENOENT;
260}
261
262static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
263{
264 struct fs_struct *fs;
265 int result = -ENOENT;
266 task_lock(proc_task(inode));
267 fs = proc_task(inode)->fs;
268 if(fs)
269 atomic_inc(&fs->count);
270 task_unlock(proc_task(inode));
271 if (fs) {
272 read_lock(&fs->lock);
273 *mnt = mntget(fs->pwdmnt);
274 *dentry = dget(fs->pwd);
275 read_unlock(&fs->lock);
276 result = 0;
277 put_fs_struct(fs);
278 }
279 return result;
280}
281
282static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
283{
284 struct fs_struct *fs;
285 int result = -ENOENT;
286 task_lock(proc_task(inode));
287 fs = proc_task(inode)->fs;
288 if(fs)
289 atomic_inc(&fs->count);
290 task_unlock(proc_task(inode));
291 if (fs) {
292 read_lock(&fs->lock);
293 *mnt = mntget(fs->rootmnt);
294 *dentry = dget(fs->root);
295 read_unlock(&fs->lock);
296 result = 0;
297 put_fs_struct(fs);
298 }
299 return result;
300}
301
302#define MAY_PTRACE(task) \
303 (task == current || \
304 (task->parent == current && \
305 (task->ptrace & PT_PTRACED) && \
306 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
307 security_ptrace(current,task) == 0))
308
309static int may_ptrace_attach(struct task_struct *task)
310{
311 int retval = 0;
312
313 task_lock(task);
314
315 if (!task->mm)
316 goto out;
317 if (((current->uid != task->euid) ||
318 (current->uid != task->suid) ||
319 (current->uid != task->uid) ||
320 (current->gid != task->egid) ||
321 (current->gid != task->sgid) ||
322 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
323 goto out;
324 rmb();
d6e71144 325 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
1da177e4
LT
326 goto out;
327 if (security_ptrace(current, task))
328 goto out;
329
330 retval = 1;
331out:
332 task_unlock(task);
333 return retval;
334}
335
336static int proc_pid_environ(struct task_struct *task, char * buffer)
337{
338 int res = 0;
339 struct mm_struct *mm = get_task_mm(task);
340 if (mm) {
341 unsigned int len = mm->env_end - mm->env_start;
342 if (len > PAGE_SIZE)
343 len = PAGE_SIZE;
344 res = access_process_vm(task, mm->env_start, buffer, len, 0);
345 if (!may_ptrace_attach(task))
346 res = -ESRCH;
347 mmput(mm);
348 }
349 return res;
350}
351
352static int proc_pid_cmdline(struct task_struct *task, char * buffer)
353{
354 int res = 0;
355 unsigned int len;
356 struct mm_struct *mm = get_task_mm(task);
357 if (!mm)
358 goto out;
359 if (!mm->arg_end)
360 goto out_mm; /* Shh! No looking before we're done */
361
362 len = mm->arg_end - mm->arg_start;
363
364 if (len > PAGE_SIZE)
365 len = PAGE_SIZE;
366
367 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
368
369 // If the nul at the end of args has been overwritten, then
370 // assume application is using setproctitle(3).
371 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
372 len = strnlen(buffer, res);
373 if (len < res) {
374 res = len;
375 } else {
376 len = mm->env_end - mm->env_start;
377 if (len > PAGE_SIZE - res)
378 len = PAGE_SIZE - res;
379 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
380 res = strnlen(buffer, res);
381 }
382 }
383out_mm:
384 mmput(mm);
385out:
386 return res;
387}
388
389static int proc_pid_auxv(struct task_struct *task, char *buffer)
390{
391 int res = 0;
392 struct mm_struct *mm = get_task_mm(task);
393 if (mm) {
394 unsigned int nwords = 0;
395 do
396 nwords += 2;
397 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
398 res = nwords * sizeof(mm->saved_auxv[0]);
399 if (res > PAGE_SIZE)
400 res = PAGE_SIZE;
401 memcpy(buffer, mm->saved_auxv, res);
402 mmput(mm);
403 }
404 return res;
405}
406
407
408#ifdef CONFIG_KALLSYMS
409/*
410 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
411 * Returns the resolved symbol. If that fails, simply return the address.
412 */
413static int proc_pid_wchan(struct task_struct *task, char *buffer)
414{
415 char *modname;
416 const char *sym_name;
417 unsigned long wchan, size, offset;
418 char namebuf[KSYM_NAME_LEN+1];
419
420 wchan = get_wchan(task);
421
422 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
423 if (sym_name)
424 return sprintf(buffer, "%s", sym_name);
425 return sprintf(buffer, "%lu", wchan);
426}
427#endif /* CONFIG_KALLSYMS */
428
429#ifdef CONFIG_SCHEDSTATS
430/*
431 * Provides /proc/PID/schedstat
432 */
433static int proc_pid_schedstat(struct task_struct *task, char *buffer)
434{
435 return sprintf(buffer, "%lu %lu %lu\n",
436 task->sched_info.cpu_time,
437 task->sched_info.run_delay,
438 task->sched_info.pcnt);
439}
440#endif
441
442/* The badness from the OOM killer */
443unsigned long badness(struct task_struct *p, unsigned long uptime);
444static int proc_oom_score(struct task_struct *task, char *buffer)
445{
446 unsigned long points;
447 struct timespec uptime;
448
449 do_posix_clock_monotonic_gettime(&uptime);
450 points = badness(task, uptime.tv_sec);
451 return sprintf(buffer, "%lu\n", points);
452}
453
454/************************************************************************/
455/* Here the fs part begins */
456/************************************************************************/
457
458/* permission checks */
459
460static int proc_check_root(struct inode *inode)
461{
462 struct dentry *de, *base, *root;
463 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
464 int res = 0;
465
466 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
467 return -ENOENT;
468 read_lock(&current->fs->lock);
469 our_vfsmnt = mntget(current->fs->rootmnt);
470 base = dget(current->fs->root);
471 read_unlock(&current->fs->lock);
472
473 spin_lock(&vfsmount_lock);
474 de = root;
475 mnt = vfsmnt;
476
477 while (vfsmnt != our_vfsmnt) {
478 if (vfsmnt == vfsmnt->mnt_parent)
479 goto out;
480 de = vfsmnt->mnt_mountpoint;
481 vfsmnt = vfsmnt->mnt_parent;
482 }
483
484 if (!is_subdir(de, base))
485 goto out;
486 spin_unlock(&vfsmount_lock);
487
488exit:
489 dput(base);
490 mntput(our_vfsmnt);
491 dput(root);
492 mntput(mnt);
493 return res;
494out:
495 spin_unlock(&vfsmount_lock);
496 res = -EACCES;
497 goto exit;
498}
499
500static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
501{
502 if (generic_permission(inode, mask, NULL) != 0)
503 return -EACCES;
504 return proc_check_root(inode);
505}
506
507extern struct seq_operations proc_pid_maps_op;
508static int maps_open(struct inode *inode, struct file *file)
509{
510 struct task_struct *task = proc_task(inode);
511 int ret = seq_open(file, &proc_pid_maps_op);
512 if (!ret) {
513 struct seq_file *m = file->private_data;
514 m->private = task;
515 }
516 return ret;
517}
518
519static struct file_operations proc_maps_operations = {
520 .open = maps_open,
521 .read = seq_read,
522 .llseek = seq_lseek,
523 .release = seq_release,
524};
525
6e21c8f1
CL
526#ifdef CONFIG_NUMA
527extern struct seq_operations proc_pid_numa_maps_op;
528static int numa_maps_open(struct inode *inode, struct file *file)
529{
530 struct task_struct *task = proc_task(inode);
531 int ret = seq_open(file, &proc_pid_numa_maps_op);
532 if (!ret) {
533 struct seq_file *m = file->private_data;
534 m->private = task;
535 }
536 return ret;
537}
538
539static struct file_operations proc_numa_maps_operations = {
540 .open = numa_maps_open,
541 .read = seq_read,
542 .llseek = seq_lseek,
543 .release = seq_release,
544};
545#endif
546
1da177e4
LT
547extern struct seq_operations mounts_op;
548static int mounts_open(struct inode *inode, struct file *file)
549{
550 struct task_struct *task = proc_task(inode);
551 int ret = seq_open(file, &mounts_op);
552
553 if (!ret) {
554 struct seq_file *m = file->private_data;
555 struct namespace *namespace;
556 task_lock(task);
557 namespace = task->namespace;
558 if (namespace)
559 get_namespace(namespace);
560 task_unlock(task);
561
562 if (namespace)
563 m->private = namespace;
564 else {
565 seq_release(inode, file);
566 ret = -EINVAL;
567 }
568 }
569 return ret;
570}
571
572static int mounts_release(struct inode *inode, struct file *file)
573{
574 struct seq_file *m = file->private_data;
575 struct namespace *namespace = m->private;
576 put_namespace(namespace);
577 return seq_release(inode, file);
578}
579
580static struct file_operations proc_mounts_operations = {
581 .open = mounts_open,
582 .read = seq_read,
583 .llseek = seq_lseek,
584 .release = mounts_release,
585};
586
587#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
588
589static ssize_t proc_info_read(struct file * file, char __user * buf,
590 size_t count, loff_t *ppos)
591{
592 struct inode * inode = file->f_dentry->d_inode;
593 unsigned long page;
594 ssize_t length;
595 struct task_struct *task = proc_task(inode);
596
597 if (count > PROC_BLOCK_SIZE)
598 count = PROC_BLOCK_SIZE;
599 if (!(page = __get_free_page(GFP_KERNEL)))
600 return -ENOMEM;
601
602 length = PROC_I(inode)->op.proc_read(task, (char*)page);
603
604 if (length >= 0)
605 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
606 free_page(page);
607 return length;
608}
609
610static struct file_operations proc_info_file_operations = {
611 .read = proc_info_read,
612};
613
614static int mem_open(struct inode* inode, struct file* file)
615{
616 file->private_data = (void*)((long)current->self_exec_id);
617 return 0;
618}
619
620static ssize_t mem_read(struct file * file, char __user * buf,
621 size_t count, loff_t *ppos)
622{
623 struct task_struct *task = proc_task(file->f_dentry->d_inode);
624 char *page;
625 unsigned long src = *ppos;
626 int ret = -ESRCH;
627 struct mm_struct *mm;
628
629 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
630 goto out;
631
632 ret = -ENOMEM;
633 page = (char *)__get_free_page(GFP_USER);
634 if (!page)
635 goto out;
636
637 ret = 0;
638
639 mm = get_task_mm(task);
640 if (!mm)
641 goto out_free;
642
643 ret = -EIO;
644
645 if (file->private_data != (void*)((long)current->self_exec_id))
646 goto out_put;
647
648 ret = 0;
649
650 while (count > 0) {
651 int this_len, retval;
652
653 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
654 retval = access_process_vm(task, src, page, this_len, 0);
655 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
656 if (!ret)
657 ret = -EIO;
658 break;
659 }
660
661 if (copy_to_user(buf, page, retval)) {
662 ret = -EFAULT;
663 break;
664 }
665
666 ret += retval;
667 src += retval;
668 buf += retval;
669 count -= retval;
670 }
671 *ppos = src;
672
673out_put:
674 mmput(mm);
675out_free:
676 free_page((unsigned long) page);
677out:
678 return ret;
679}
680
681#define mem_write NULL
682
683#ifndef mem_write
684/* This is a security hazard */
685static ssize_t mem_write(struct file * file, const char * buf,
686 size_t count, loff_t *ppos)
687{
688 int copied = 0;
689 char *page;
690 struct task_struct *task = proc_task(file->f_dentry->d_inode);
691 unsigned long dst = *ppos;
692
693 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
694 return -ESRCH;
695
696 page = (char *)__get_free_page(GFP_USER);
697 if (!page)
698 return -ENOMEM;
699
700 while (count > 0) {
701 int this_len, retval;
702
703 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
704 if (copy_from_user(page, buf, this_len)) {
705 copied = -EFAULT;
706 break;
707 }
708 retval = access_process_vm(task, dst, page, this_len, 1);
709 if (!retval) {
710 if (!copied)
711 copied = -EIO;
712 break;
713 }
714 copied += retval;
715 buf += retval;
716 dst += retval;
717 count -= retval;
718 }
719 *ppos = dst;
720 free_page((unsigned long) page);
721 return copied;
722}
723#endif
724
725static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
726{
727 switch (orig) {
728 case 0:
729 file->f_pos = offset;
730 break;
731 case 1:
732 file->f_pos += offset;
733 break;
734 default:
735 return -EINVAL;
736 }
737 force_successful_syscall_return();
738 return file->f_pos;
739}
740
741static struct file_operations proc_mem_operations = {
742 .llseek = mem_lseek,
743 .read = mem_read,
744 .write = mem_write,
745 .open = mem_open,
746};
747
748static ssize_t oom_adjust_read(struct file *file, char __user *buf,
749 size_t count, loff_t *ppos)
750{
751 struct task_struct *task = proc_task(file->f_dentry->d_inode);
752 char buffer[8];
753 size_t len;
754 int oom_adjust = task->oomkilladj;
755 loff_t __ppos = *ppos;
756
757 len = sprintf(buffer, "%i\n", oom_adjust);
758 if (__ppos >= len)
759 return 0;
760 if (count > len-__ppos)
761 count = len-__ppos;
762 if (copy_to_user(buf, buffer + __ppos, count))
763 return -EFAULT;
764 *ppos = __ppos + count;
765 return count;
766}
767
768static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
769 size_t count, loff_t *ppos)
770{
771 struct task_struct *task = proc_task(file->f_dentry->d_inode);
772 char buffer[8], *end;
773 int oom_adjust;
774
775 if (!capable(CAP_SYS_RESOURCE))
776 return -EPERM;
777 memset(buffer, 0, 8);
778 if (count > 6)
779 count = 6;
780 if (copy_from_user(buffer, buf, count))
781 return -EFAULT;
782 oom_adjust = simple_strtol(buffer, &end, 0);
79befd0c 783 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
1da177e4
LT
784 return -EINVAL;
785 if (*end == '\n')
786 end++;
787 task->oomkilladj = oom_adjust;
788 if (end - buffer == 0)
789 return -EIO;
790 return end - buffer;
791}
792
793static struct file_operations proc_oom_adjust_operations = {
794 .read = oom_adjust_read,
795 .write = oom_adjust_write,
796};
797
798static struct inode_operations proc_mem_inode_operations = {
799 .permission = proc_permission,
800};
801
802#ifdef CONFIG_AUDITSYSCALL
803#define TMPBUFLEN 21
804static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
805 size_t count, loff_t *ppos)
806{
807 struct inode * inode = file->f_dentry->d_inode;
808 struct task_struct *task = proc_task(inode);
809 ssize_t length;
810 char tmpbuf[TMPBUFLEN];
811
812 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
813 audit_get_loginuid(task->audit_context));
814 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
815}
816
817static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
818 size_t count, loff_t *ppos)
819{
820 struct inode * inode = file->f_dentry->d_inode;
821 char *page, *tmp;
822 ssize_t length;
823 struct task_struct *task = proc_task(inode);
824 uid_t loginuid;
825
826 if (!capable(CAP_AUDIT_CONTROL))
827 return -EPERM;
828
829 if (current != task)
830 return -EPERM;
831
832 if (count > PAGE_SIZE)
833 count = PAGE_SIZE;
834
835 if (*ppos != 0) {
836 /* No partial writes. */
837 return -EINVAL;
838 }
839 page = (char*)__get_free_page(GFP_USER);
840 if (!page)
841 return -ENOMEM;
842 length = -EFAULT;
843 if (copy_from_user(page, buf, count))
844 goto out_free_page;
845
846 loginuid = simple_strtoul(page, &tmp, 10);
847 if (tmp == page) {
848 length = -EINVAL;
849 goto out_free_page;
850
851 }
456be6cd 852 length = audit_set_loginuid(task, loginuid);
1da177e4
LT
853 if (likely(length == 0))
854 length = count;
855
856out_free_page:
857 free_page((unsigned long) page);
858 return length;
859}
860
861static struct file_operations proc_loginuid_operations = {
862 .read = proc_loginuid_read,
863 .write = proc_loginuid_write,
864};
865#endif
866
867#ifdef CONFIG_SECCOMP
868static ssize_t seccomp_read(struct file *file, char __user *buf,
869 size_t count, loff_t *ppos)
870{
871 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
872 char __buf[20];
873 loff_t __ppos = *ppos;
874 size_t len;
875
876 /* no need to print the trailing zero, so use only len */
877 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
878 if (__ppos >= len)
879 return 0;
880 if (count > len - __ppos)
881 count = len - __ppos;
882 if (copy_to_user(buf, __buf + __ppos, count))
883 return -EFAULT;
884 *ppos = __ppos + count;
885 return count;
886}
887
888static ssize_t seccomp_write(struct file *file, const char __user *buf,
889 size_t count, loff_t *ppos)
890{
891 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
892 char __buf[20], *end;
893 unsigned int seccomp_mode;
894
895 /* can set it only once to be even more secure */
896 if (unlikely(tsk->seccomp.mode))
897 return -EPERM;
898
899 memset(__buf, 0, sizeof(__buf));
900 count = min(count, sizeof(__buf) - 1);
901 if (copy_from_user(__buf, buf, count))
902 return -EFAULT;
903 seccomp_mode = simple_strtoul(__buf, &end, 0);
904 if (*end == '\n')
905 end++;
906 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
907 tsk->seccomp.mode = seccomp_mode;
908 set_tsk_thread_flag(tsk, TIF_SECCOMP);
909 } else
910 return -EINVAL;
911 if (unlikely(!(end - __buf)))
912 return -EIO;
913 return end - __buf;
914}
915
916static struct file_operations proc_seccomp_operations = {
917 .read = seccomp_read,
918 .write = seccomp_write,
919};
920#endif /* CONFIG_SECCOMP */
921
008b150a 922static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
923{
924 struct inode *inode = dentry->d_inode;
925 int error = -EACCES;
926
927 /* We don't need a base pointer in the /proc filesystem */
928 path_release(nd);
929
930 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
931 goto out;
932 error = proc_check_root(inode);
933 if (error)
934 goto out;
935
936 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
937 nd->last_type = LAST_BIND;
938out:
008b150a 939 return ERR_PTR(error);
1da177e4
LT
940}
941
942static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
943 char __user *buffer, int buflen)
944{
945 struct inode * inode;
946 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
947 int len;
948
949 if (!tmp)
950 return -ENOMEM;
951
952 inode = dentry->d_inode;
953 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
954 len = PTR_ERR(path);
955 if (IS_ERR(path))
956 goto out;
957 len = tmp + PAGE_SIZE - 1 - path;
958
959 if (len > buflen)
960 len = buflen;
961 if (copy_to_user(buffer, path, len))
962 len = -EFAULT;
963 out:
964 free_page((unsigned long)tmp);
965 return len;
966}
967
968static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
969{
970 int error = -EACCES;
971 struct inode *inode = dentry->d_inode;
972 struct dentry *de;
973 struct vfsmount *mnt = NULL;
974
975 lock_kernel();
976
977 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
978 goto out;
979 error = proc_check_root(inode);
980 if (error)
981 goto out;
982
983 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
984 if (error)
985 goto out;
986
987 error = do_proc_readlink(de, mnt, buffer, buflen);
988 dput(de);
989 mntput(mnt);
990out:
991 unlock_kernel();
992 return error;
993}
994
995static struct inode_operations proc_pid_link_inode_operations = {
996 .readlink = proc_pid_readlink,
997 .follow_link = proc_pid_follow_link
998};
999
1000#define NUMBUF 10
1001
1002static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1003{
1004 struct inode *inode = filp->f_dentry->d_inode;
1005 struct task_struct *p = proc_task(inode);
1006 unsigned int fd, tid, ino;
1007 int retval;
1008 char buf[NUMBUF];
1009 struct files_struct * files;
1010
1011 retval = -ENOENT;
1012 if (!pid_alive(p))
1013 goto out;
1014 retval = 0;
1015 tid = p->pid;
1016
1017 fd = filp->f_pos;
1018 switch (fd) {
1019 case 0:
1020 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1021 goto out;
1022 filp->f_pos++;
1023 case 1:
1024 ino = fake_ino(tid, PROC_TID_INO);
1025 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1026 goto out;
1027 filp->f_pos++;
1028 default:
1029 files = get_files_struct(p);
1030 if (!files)
1031 goto out;
1032 spin_lock(&files->file_lock);
1033 for (fd = filp->f_pos-2;
1034 fd < files->max_fds;
1035 fd++, filp->f_pos++) {
1036 unsigned int i,j;
1037
1038 if (!fcheck_files(files, fd))
1039 continue;
1040 spin_unlock(&files->file_lock);
1041
1042 j = NUMBUF;
1043 i = fd;
1044 do {
1045 j--;
1046 buf[j] = '0' + (i % 10);
1047 i /= 10;
1048 } while (i);
1049
1050 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1051 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1052 spin_lock(&files->file_lock);
1053 break;
1054 }
1055 spin_lock(&files->file_lock);
1056 }
1057 spin_unlock(&files->file_lock);
1058 put_files_struct(files);
1059 }
1060out:
1061 return retval;
1062}
1063
1064static int proc_pident_readdir(struct file *filp,
1065 void *dirent, filldir_t filldir,
1066 struct pid_entry *ents, unsigned int nents)
1067{
1068 int i;
1069 int pid;
1070 struct dentry *dentry = filp->f_dentry;
1071 struct inode *inode = dentry->d_inode;
1072 struct pid_entry *p;
1073 ino_t ino;
1074 int ret;
1075
1076 ret = -ENOENT;
1077 if (!pid_alive(proc_task(inode)))
1078 goto out;
1079
1080 ret = 0;
1081 pid = proc_task(inode)->pid;
1082 i = filp->f_pos;
1083 switch (i) {
1084 case 0:
1085 ino = inode->i_ino;
1086 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1087 goto out;
1088 i++;
1089 filp->f_pos++;
1090 /* fall through */
1091 case 1:
1092 ino = parent_ino(dentry);
1093 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1094 goto out;
1095 i++;
1096 filp->f_pos++;
1097 /* fall through */
1098 default:
1099 i -= 2;
1100 if (i >= nents) {
1101 ret = 1;
1102 goto out;
1103 }
1104 p = ents + i;
1105 while (p->name) {
1106 if (filldir(dirent, p->name, p->len, filp->f_pos,
1107 fake_ino(pid, p->type), p->mode >> 12) < 0)
1108 goto out;
1109 filp->f_pos++;
1110 p++;
1111 }
1112 }
1113
1114 ret = 1;
1115out:
1116 return ret;
1117}
1118
1119static int proc_tgid_base_readdir(struct file * filp,
1120 void * dirent, filldir_t filldir)
1121{
1122 return proc_pident_readdir(filp,dirent,filldir,
1123 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1124}
1125
1126static int proc_tid_base_readdir(struct file * filp,
1127 void * dirent, filldir_t filldir)
1128{
1129 return proc_pident_readdir(filp,dirent,filldir,
1130 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1131}
1132
1133/* building an inode */
1134
1135static int task_dumpable(struct task_struct *task)
1136{
1137 int dumpable = 0;
1138 struct mm_struct *mm;
1139
1140 task_lock(task);
1141 mm = task->mm;
1142 if (mm)
1143 dumpable = mm->dumpable;
1144 task_unlock(task);
d6e71144
AC
1145 if(dumpable == 1)
1146 return 1;
1147 return 0;
1da177e4
LT
1148}
1149
1150
1151static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1152{
1153 struct inode * inode;
1154 struct proc_inode *ei;
1155
1156 /* We need a new inode */
1157
1158 inode = new_inode(sb);
1159 if (!inode)
1160 goto out;
1161
1162 /* Common stuff */
1163 ei = PROC_I(inode);
1164 ei->task = NULL;
1165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1166 inode->i_ino = fake_ino(task->pid, ino);
1167
1168 if (!pid_alive(task))
1169 goto out_unlock;
1170
1171 /*
1172 * grab the reference to task.
1173 */
1174 get_task_struct(task);
1175 ei->task = task;
1176 ei->type = ino;
1177 inode->i_uid = 0;
1178 inode->i_gid = 0;
1179 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1180 inode->i_uid = task->euid;
1181 inode->i_gid = task->egid;
1182 }
1183 security_task_to_inode(task, inode);
1184
1185out:
1186 return inode;
1187
1188out_unlock:
1189 ei->pde = NULL;
1190 iput(inode);
1191 return NULL;
1192}
1193
1194/* dentry stuff */
1195
1196/*
1197 * Exceptional case: normally we are not allowed to unhash a busy
1198 * directory. In this case, however, we can do it - no aliasing problems
1199 * due to the way we treat inodes.
1200 *
1201 * Rewrite the inode's ownerships here because the owning task may have
1202 * performed a setuid(), etc.
1203 */
1204static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1205{
1206 struct inode *inode = dentry->d_inode;
1207 struct task_struct *task = proc_task(inode);
1208 if (pid_alive(task)) {
1209 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1210 inode->i_uid = task->euid;
1211 inode->i_gid = task->egid;
1212 } else {
1213 inode->i_uid = 0;
1214 inode->i_gid = 0;
1215 }
1216 security_task_to_inode(task, inode);
1217 return 1;
1218 }
1219 d_drop(dentry);
1220 return 0;
1221}
1222
1223static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1224{
1225 struct inode *inode = dentry->d_inode;
1226 struct task_struct *task = proc_task(inode);
1227 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1228 struct files_struct *files;
1229
1230 files = get_files_struct(task);
1231 if (files) {
1232 spin_lock(&files->file_lock);
1233 if (fcheck_files(files, fd)) {
1234 spin_unlock(&files->file_lock);
1235 put_files_struct(files);
1236 if (task_dumpable(task)) {
1237 inode->i_uid = task->euid;
1238 inode->i_gid = task->egid;
1239 } else {
1240 inode->i_uid = 0;
1241 inode->i_gid = 0;
1242 }
1243 security_task_to_inode(task, inode);
1244 return 1;
1245 }
1246 spin_unlock(&files->file_lock);
1247 put_files_struct(files);
1248 }
1249 d_drop(dentry);
1250 return 0;
1251}
1252
1253static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1254{
1255 struct task_struct *task = proc_task(inode);
1256 spin_lock(&task->proc_lock);
1257 if (task->proc_dentry == dentry)
1258 task->proc_dentry = NULL;
1259 spin_unlock(&task->proc_lock);
1260 iput(inode);
1261}
1262
1263static int pid_delete_dentry(struct dentry * dentry)
1264{
1265 /* Is the task we represent dead?
1266 * If so, then don't put the dentry on the lru list,
1267 * kill it immediately.
1268 */
1269 return !pid_alive(proc_task(dentry->d_inode));
1270}
1271
1272static struct dentry_operations tid_fd_dentry_operations =
1273{
1274 .d_revalidate = tid_fd_revalidate,
1275 .d_delete = pid_delete_dentry,
1276};
1277
1278static struct dentry_operations pid_dentry_operations =
1279{
1280 .d_revalidate = pid_revalidate,
1281 .d_delete = pid_delete_dentry,
1282};
1283
1284static struct dentry_operations pid_base_dentry_operations =
1285{
1286 .d_revalidate = pid_revalidate,
1287 .d_iput = pid_base_iput,
1288 .d_delete = pid_delete_dentry,
1289};
1290
1291/* Lookups */
1292
1293static unsigned name_to_int(struct dentry *dentry)
1294{
1295 const char *name = dentry->d_name.name;
1296 int len = dentry->d_name.len;
1297 unsigned n = 0;
1298
1299 if (len > 1 && *name == '0')
1300 goto out;
1301 while (len-- > 0) {
1302 unsigned c = *name++ - '0';
1303 if (c > 9)
1304 goto out;
1305 if (n >= (~0U-9)/10)
1306 goto out;
1307 n *= 10;
1308 n += c;
1309 }
1310 return n;
1311out:
1312 return ~0U;
1313}
1314
1315/* SMP-safe */
1316static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1317{
1318 struct task_struct *task = proc_task(dir);
1319 unsigned fd = name_to_int(dentry);
1320 struct file * file;
1321 struct files_struct * files;
1322 struct inode *inode;
1323 struct proc_inode *ei;
1324
1325 if (fd == ~0U)
1326 goto out;
1327 if (!pid_alive(task))
1328 goto out;
1329
1330 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1331 if (!inode)
1332 goto out;
1333 ei = PROC_I(inode);
1334 files = get_files_struct(task);
1335 if (!files)
1336 goto out_unlock;
1337 inode->i_mode = S_IFLNK;
1338 spin_lock(&files->file_lock);
1339 file = fcheck_files(files, fd);
1340 if (!file)
1341 goto out_unlock2;
1342 if (file->f_mode & 1)
1343 inode->i_mode |= S_IRUSR | S_IXUSR;
1344 if (file->f_mode & 2)
1345 inode->i_mode |= S_IWUSR | S_IXUSR;
1346 spin_unlock(&files->file_lock);
1347 put_files_struct(files);
1348 inode->i_op = &proc_pid_link_inode_operations;
1349 inode->i_size = 64;
1350 ei->op.proc_get_link = proc_fd_link;
1351 dentry->d_op = &tid_fd_dentry_operations;
1352 d_add(dentry, inode);
1353 return NULL;
1354
1355out_unlock2:
1356 spin_unlock(&files->file_lock);
1357 put_files_struct(files);
1358out_unlock:
1359 iput(inode);
1360out:
1361 return ERR_PTR(-ENOENT);
1362}
1363
1364static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1365static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1366
1367static struct file_operations proc_fd_operations = {
1368 .read = generic_read_dir,
1369 .readdir = proc_readfd,
1370};
1371
1372static struct file_operations proc_task_operations = {
1373 .read = generic_read_dir,
1374 .readdir = proc_task_readdir,
1375};
1376
1377/*
1378 * proc directories can do almost nothing..
1379 */
1380static struct inode_operations proc_fd_inode_operations = {
1381 .lookup = proc_lookupfd,
1382 .permission = proc_permission,
1383};
1384
1385static struct inode_operations proc_task_inode_operations = {
1386 .lookup = proc_task_lookup,
1387 .permission = proc_permission,
1388};
1389
1390#ifdef CONFIG_SECURITY
1391static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1392 size_t count, loff_t *ppos)
1393{
1394 struct inode * inode = file->f_dentry->d_inode;
1395 unsigned long page;
1396 ssize_t length;
1397 struct task_struct *task = proc_task(inode);
1398
1399 if (count > PAGE_SIZE)
1400 count = PAGE_SIZE;
1401 if (!(page = __get_free_page(GFP_KERNEL)))
1402 return -ENOMEM;
1403
1404 length = security_getprocattr(task,
1405 (char*)file->f_dentry->d_name.name,
1406 (void*)page, count);
1407 if (length >= 0)
1408 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1409 free_page(page);
1410 return length;
1411}
1412
1413static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1414 size_t count, loff_t *ppos)
1415{
1416 struct inode * inode = file->f_dentry->d_inode;
1417 char *page;
1418 ssize_t length;
1419 struct task_struct *task = proc_task(inode);
1420
1421 if (count > PAGE_SIZE)
1422 count = PAGE_SIZE;
1423 if (*ppos != 0) {
1424 /* No partial writes. */
1425 return -EINVAL;
1426 }
1427 page = (char*)__get_free_page(GFP_USER);
1428 if (!page)
1429 return -ENOMEM;
1430 length = -EFAULT;
1431 if (copy_from_user(page, buf, count))
1432 goto out;
1433
1434 length = security_setprocattr(task,
1435 (char*)file->f_dentry->d_name.name,
1436 (void*)page, count);
1437out:
1438 free_page((unsigned long) page);
1439 return length;
1440}
1441
1442static struct file_operations proc_pid_attr_operations = {
1443 .read = proc_pid_attr_read,
1444 .write = proc_pid_attr_write,
1445};
1446
1447static struct file_operations proc_tid_attr_operations;
1448static struct inode_operations proc_tid_attr_inode_operations;
1449static struct file_operations proc_tgid_attr_operations;
1450static struct inode_operations proc_tgid_attr_inode_operations;
1451#endif
1452
f246315e
DD
1453static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1454
1da177e4
LT
1455/* SMP-safe */
1456static struct dentry *proc_pident_lookup(struct inode *dir,
1457 struct dentry *dentry,
1458 struct pid_entry *ents)
1459{
1460 struct inode *inode;
1461 int error;
1462 struct task_struct *task = proc_task(dir);
1463 struct pid_entry *p;
1464 struct proc_inode *ei;
1465
1466 error = -ENOENT;
1467 inode = NULL;
1468
1469 if (!pid_alive(task))
1470 goto out;
1471
1472 for (p = ents; p->name; p++) {
1473 if (p->len != dentry->d_name.len)
1474 continue;
1475 if (!memcmp(dentry->d_name.name, p->name, p->len))
1476 break;
1477 }
1478 if (!p->name)
1479 goto out;
1480
1481 error = -EINVAL;
1482 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1483 if (!inode)
1484 goto out;
1485
1486 ei = PROC_I(inode);
1487 inode->i_mode = p->mode;
1488 /*
1489 * Yes, it does not scale. And it should not. Don't add
1490 * new entries into /proc/<tgid>/ without very good reasons.
1491 */
1492 switch(p->type) {
1493 case PROC_TGID_TASK:
f246315e 1494 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1da177e4
LT
1495 inode->i_op = &proc_task_inode_operations;
1496 inode->i_fop = &proc_task_operations;
1497 break;
1498 case PROC_TID_FD:
1499 case PROC_TGID_FD:
1500 inode->i_nlink = 2;
1501 inode->i_op = &proc_fd_inode_operations;
1502 inode->i_fop = &proc_fd_operations;
1503 break;
1504 case PROC_TID_EXE:
1505 case PROC_TGID_EXE:
1506 inode->i_op = &proc_pid_link_inode_operations;
1507 ei->op.proc_get_link = proc_exe_link;
1508 break;
1509 case PROC_TID_CWD:
1510 case PROC_TGID_CWD:
1511 inode->i_op = &proc_pid_link_inode_operations;
1512 ei->op.proc_get_link = proc_cwd_link;
1513 break;
1514 case PROC_TID_ROOT:
1515 case PROC_TGID_ROOT:
1516 inode->i_op = &proc_pid_link_inode_operations;
1517 ei->op.proc_get_link = proc_root_link;
1518 break;
1519 case PROC_TID_ENVIRON:
1520 case PROC_TGID_ENVIRON:
1521 inode->i_fop = &proc_info_file_operations;
1522 ei->op.proc_read = proc_pid_environ;
1523 break;
1524 case PROC_TID_AUXV:
1525 case PROC_TGID_AUXV:
1526 inode->i_fop = &proc_info_file_operations;
1527 ei->op.proc_read = proc_pid_auxv;
1528 break;
1529 case PROC_TID_STATUS:
1530 case PROC_TGID_STATUS:
1531 inode->i_fop = &proc_info_file_operations;
1532 ei->op.proc_read = proc_pid_status;
1533 break;
1534 case PROC_TID_STAT:
1535 inode->i_fop = &proc_info_file_operations;
1536 ei->op.proc_read = proc_tid_stat;
1537 break;
1538 case PROC_TGID_STAT:
1539 inode->i_fop = &proc_info_file_operations;
1540 ei->op.proc_read = proc_tgid_stat;
1541 break;
1542 case PROC_TID_CMDLINE:
1543 case PROC_TGID_CMDLINE:
1544 inode->i_fop = &proc_info_file_operations;
1545 ei->op.proc_read = proc_pid_cmdline;
1546 break;
1547 case PROC_TID_STATM:
1548 case PROC_TGID_STATM:
1549 inode->i_fop = &proc_info_file_operations;
1550 ei->op.proc_read = proc_pid_statm;
1551 break;
1552 case PROC_TID_MAPS:
1553 case PROC_TGID_MAPS:
1554 inode->i_fop = &proc_maps_operations;
1555 break;
6e21c8f1
CL
1556#ifdef CONFIG_NUMA
1557 case PROC_TID_NUMA_MAPS:
1558 case PROC_TGID_NUMA_MAPS:
1559 inode->i_fop = &proc_numa_maps_operations;
1560 break;
1561#endif
1da177e4
LT
1562 case PROC_TID_MEM:
1563 case PROC_TGID_MEM:
1564 inode->i_op = &proc_mem_inode_operations;
1565 inode->i_fop = &proc_mem_operations;
1566 break;
1567#ifdef CONFIG_SECCOMP
1568 case PROC_TID_SECCOMP:
1569 case PROC_TGID_SECCOMP:
1570 inode->i_fop = &proc_seccomp_operations;
1571 break;
1572#endif /* CONFIG_SECCOMP */
1573 case PROC_TID_MOUNTS:
1574 case PROC_TGID_MOUNTS:
1575 inode->i_fop = &proc_mounts_operations;
1576 break;
1577#ifdef CONFIG_SECURITY
1578 case PROC_TID_ATTR:
1579 inode->i_nlink = 2;
1580 inode->i_op = &proc_tid_attr_inode_operations;
1581 inode->i_fop = &proc_tid_attr_operations;
1582 break;
1583 case PROC_TGID_ATTR:
1584 inode->i_nlink = 2;
1585 inode->i_op = &proc_tgid_attr_inode_operations;
1586 inode->i_fop = &proc_tgid_attr_operations;
1587 break;
1588 case PROC_TID_ATTR_CURRENT:
1589 case PROC_TGID_ATTR_CURRENT:
1590 case PROC_TID_ATTR_PREV:
1591 case PROC_TGID_ATTR_PREV:
1592 case PROC_TID_ATTR_EXEC:
1593 case PROC_TGID_ATTR_EXEC:
1594 case PROC_TID_ATTR_FSCREATE:
1595 case PROC_TGID_ATTR_FSCREATE:
1596 inode->i_fop = &proc_pid_attr_operations;
1597 break;
1598#endif
1599#ifdef CONFIG_KALLSYMS
1600 case PROC_TID_WCHAN:
1601 case PROC_TGID_WCHAN:
1602 inode->i_fop = &proc_info_file_operations;
1603 ei->op.proc_read = proc_pid_wchan;
1604 break;
1605#endif
1606#ifdef CONFIG_SCHEDSTATS
1607 case PROC_TID_SCHEDSTAT:
1608 case PROC_TGID_SCHEDSTAT:
1609 inode->i_fop = &proc_info_file_operations;
1610 ei->op.proc_read = proc_pid_schedstat;
1611 break;
1612#endif
1613#ifdef CONFIG_CPUSETS
1614 case PROC_TID_CPUSET:
1615 case PROC_TGID_CPUSET:
1616 inode->i_fop = &proc_cpuset_operations;
1617 break;
1618#endif
1619 case PROC_TID_OOM_SCORE:
1620 case PROC_TGID_OOM_SCORE:
1621 inode->i_fop = &proc_info_file_operations;
1622 ei->op.proc_read = proc_oom_score;
1623 break;
1624 case PROC_TID_OOM_ADJUST:
1625 case PROC_TGID_OOM_ADJUST:
1626 inode->i_fop = &proc_oom_adjust_operations;
1627 break;
1628#ifdef CONFIG_AUDITSYSCALL
1629 case PROC_TID_LOGINUID:
1630 case PROC_TGID_LOGINUID:
1631 inode->i_fop = &proc_loginuid_operations;
1632 break;
1633#endif
1634 default:
1635 printk("procfs: impossible type (%d)",p->type);
1636 iput(inode);
1637 return ERR_PTR(-EINVAL);
1638 }
1639 dentry->d_op = &pid_dentry_operations;
1640 d_add(dentry, inode);
1641 return NULL;
1642
1643out:
1644 return ERR_PTR(error);
1645}
1646
1647static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1648 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1649}
1650
1651static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1652 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1653}
1654
1655static struct file_operations proc_tgid_base_operations = {
1656 .read = generic_read_dir,
1657 .readdir = proc_tgid_base_readdir,
1658};
1659
1660static struct file_operations proc_tid_base_operations = {
1661 .read = generic_read_dir,
1662 .readdir = proc_tid_base_readdir,
1663};
1664
1665static struct inode_operations proc_tgid_base_inode_operations = {
1666 .lookup = proc_tgid_base_lookup,
1667};
1668
1669static struct inode_operations proc_tid_base_inode_operations = {
1670 .lookup = proc_tid_base_lookup,
1671};
1672
1673#ifdef CONFIG_SECURITY
1674static int proc_tgid_attr_readdir(struct file * filp,
1675 void * dirent, filldir_t filldir)
1676{
1677 return proc_pident_readdir(filp,dirent,filldir,
1678 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1679}
1680
1681static int proc_tid_attr_readdir(struct file * filp,
1682 void * dirent, filldir_t filldir)
1683{
1684 return proc_pident_readdir(filp,dirent,filldir,
1685 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1686}
1687
1688static struct file_operations proc_tgid_attr_operations = {
1689 .read = generic_read_dir,
1690 .readdir = proc_tgid_attr_readdir,
1691};
1692
1693static struct file_operations proc_tid_attr_operations = {
1694 .read = generic_read_dir,
1695 .readdir = proc_tid_attr_readdir,
1696};
1697
1698static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1699 struct dentry *dentry, struct nameidata *nd)
1700{
1701 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1702}
1703
1704static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1705 struct dentry *dentry, struct nameidata *nd)
1706{
1707 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1708}
1709
1710static struct inode_operations proc_tgid_attr_inode_operations = {
1711 .lookup = proc_tgid_attr_lookup,
1712};
1713
1714static struct inode_operations proc_tid_attr_inode_operations = {
1715 .lookup = proc_tid_attr_lookup,
1716};
1717#endif
1718
1719/*
1720 * /proc/self:
1721 */
1722static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1723 int buflen)
1724{
1725 char tmp[30];
1726 sprintf(tmp, "%d", current->tgid);
1727 return vfs_readlink(dentry,buffer,buflen,tmp);
1728}
1729
008b150a 1730static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1731{
1732 char tmp[30];
1733 sprintf(tmp, "%d", current->tgid);
008b150a 1734 return ERR_PTR(vfs_follow_link(nd,tmp));
1da177e4
LT
1735}
1736
1737static struct inode_operations proc_self_inode_operations = {
1738 .readlink = proc_self_readlink,
1739 .follow_link = proc_self_follow_link,
1740};
1741
1742/**
4dc3b16b 1743 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1da177e4
LT
1744 * @p: task that should be flushed.
1745 *
4dc3b16b 1746 * Drops the /proc/@pid dcache entry from the hash chains.
1da177e4 1747 *
4dc3b16b
PP
1748 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1749 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1da177e4
LT
1750 * if the pid value is immediately reused. This is enforced by
1751 * - caller must acquire spin_lock(p->proc_lock)
1752 * - must be called before detach_pid()
1753 * - proc_pid_lookup acquires proc_lock, and checks that
1754 * the target is not dead by looking at the attach count
1755 * of PIDTYPE_PID.
1756 */
1757
1758struct dentry *proc_pid_unhash(struct task_struct *p)
1759{
1760 struct dentry *proc_dentry;
1761
1762 proc_dentry = p->proc_dentry;
1763 if (proc_dentry != NULL) {
1764
1765 spin_lock(&dcache_lock);
1766 spin_lock(&proc_dentry->d_lock);
1767 if (!d_unhashed(proc_dentry)) {
1768 dget_locked(proc_dentry);
1769 __d_drop(proc_dentry);
1770 spin_unlock(&proc_dentry->d_lock);
1771 } else {
1772 spin_unlock(&proc_dentry->d_lock);
1773 proc_dentry = NULL;
1774 }
1775 spin_unlock(&dcache_lock);
1776 }
1777 return proc_dentry;
1778}
1779
1780/**
4dc3b16b 1781 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
67be2dd1 1782 * @proc_dentry: directoy to prune.
1da177e4
LT
1783 *
1784 * Shrink the /proc directory that was used by the just killed thread.
1785 */
1786
1787void proc_pid_flush(struct dentry *proc_dentry)
1788{
1789 might_sleep();
1790 if(proc_dentry != NULL) {
1791 shrink_dcache_parent(proc_dentry);
1792 dput(proc_dentry);
1793 }
1794}
1795
1796/* SMP-safe */
1797struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1798{
1799 struct task_struct *task;
1800 struct inode *inode;
1801 struct proc_inode *ei;
1802 unsigned tgid;
1803 int died;
1804
1805 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1806 inode = new_inode(dir->i_sb);
1807 if (!inode)
1808 return ERR_PTR(-ENOMEM);
1809 ei = PROC_I(inode);
1810 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1811 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1812 ei->pde = NULL;
1813 inode->i_mode = S_IFLNK|S_IRWXUGO;
1814 inode->i_uid = inode->i_gid = 0;
1815 inode->i_size = 64;
1816 inode->i_op = &proc_self_inode_operations;
1817 d_add(dentry, inode);
1818 return NULL;
1819 }
1820 tgid = name_to_int(dentry);
1821 if (tgid == ~0U)
1822 goto out;
1823
1824 read_lock(&tasklist_lock);
1825 task = find_task_by_pid(tgid);
1826 if (task)
1827 get_task_struct(task);
1828 read_unlock(&tasklist_lock);
1829 if (!task)
1830 goto out;
1831
1832 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1833
1834
1835 if (!inode) {
1836 put_task_struct(task);
1837 goto out;
1838 }
1839 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1840 inode->i_op = &proc_tgid_base_inode_operations;
1841 inode->i_fop = &proc_tgid_base_operations;
1da177e4 1842 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
1843#ifdef CONFIG_SECURITY
1844 inode->i_nlink = 5;
1845#else
1846 inode->i_nlink = 4;
1847#endif
1da177e4
LT
1848
1849 dentry->d_op = &pid_base_dentry_operations;
1850
1851 died = 0;
1852 d_add(dentry, inode);
1853 spin_lock(&task->proc_lock);
1854 task->proc_dentry = dentry;
1855 if (!pid_alive(task)) {
1856 dentry = proc_pid_unhash(task);
1857 died = 1;
1858 }
1859 spin_unlock(&task->proc_lock);
1860
1861 put_task_struct(task);
1862 if (died) {
1863 proc_pid_flush(dentry);
1864 goto out;
1865 }
1866 return NULL;
1867out:
1868 return ERR_PTR(-ENOENT);
1869}
1870
1871/* SMP-safe */
1872static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1873{
1874 struct task_struct *task;
1875 struct task_struct *leader = proc_task(dir);
1876 struct inode *inode;
1877 unsigned tid;
1878
1879 tid = name_to_int(dentry);
1880 if (tid == ~0U)
1881 goto out;
1882
1883 read_lock(&tasklist_lock);
1884 task = find_task_by_pid(tid);
1885 if (task)
1886 get_task_struct(task);
1887 read_unlock(&tasklist_lock);
1888 if (!task)
1889 goto out;
1890 if (leader->tgid != task->tgid)
1891 goto out_drop_task;
1892
1893 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1894
1895
1896 if (!inode)
1897 goto out_drop_task;
1898 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1899 inode->i_op = &proc_tid_base_inode_operations;
1900 inode->i_fop = &proc_tid_base_operations;
1da177e4 1901 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
1902#ifdef CONFIG_SECURITY
1903 inode->i_nlink = 4;
1904#else
1905 inode->i_nlink = 3;
1906#endif
1da177e4
LT
1907
1908 dentry->d_op = &pid_base_dentry_operations;
1909
1910 d_add(dentry, inode);
1911
1912 put_task_struct(task);
1913 return NULL;
1914out_drop_task:
1915 put_task_struct(task);
1916out:
1917 return ERR_PTR(-ENOENT);
1918}
1919
1920#define PROC_NUMBUF 10
1921#define PROC_MAXPIDS 20
1922
1923/*
1924 * Get a few tgid's to return for filldir - we need to hold the
1925 * tasklist lock while doing this, and we must release it before
1926 * we actually do the filldir itself, so we use a temp buffer..
1927 */
1928static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1929{
1930 struct task_struct *p;
1931 int nr_tgids = 0;
1932
1933 index--;
1934 read_lock(&tasklist_lock);
1935 p = NULL;
1936 if (version) {
1937 p = find_task_by_pid(version);
1938 if (p && !thread_group_leader(p))
1939 p = NULL;
1940 }
1941
1942 if (p)
1943 index = 0;
1944 else
1945 p = next_task(&init_task);
1946
1947 for ( ; p != &init_task; p = next_task(p)) {
1948 int tgid = p->pid;
1949 if (!pid_alive(p))
1950 continue;
1951 if (--index >= 0)
1952 continue;
1953 tgids[nr_tgids] = tgid;
1954 nr_tgids++;
1955 if (nr_tgids >= PROC_MAXPIDS)
1956 break;
1957 }
1958 read_unlock(&tasklist_lock);
1959 return nr_tgids;
1960}
1961
1962/*
1963 * Get a few tid's to return for filldir - we need to hold the
1964 * tasklist lock while doing this, and we must release it before
1965 * we actually do the filldir itself, so we use a temp buffer..
1966 */
1967static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1968{
1969 struct task_struct *leader_task = proc_task(dir);
1970 struct task_struct *task = leader_task;
1971 int nr_tids = 0;
1972
1973 index -= 2;
1974 read_lock(&tasklist_lock);
1975 /*
1976 * The starting point task (leader_task) might be an already
1977 * unlinked task, which cannot be used to access the task-list
1978 * via next_thread().
1979 */
1980 if (pid_alive(task)) do {
1981 int tid = task->pid;
1982
1983 if (--index >= 0)
1984 continue;
f246315e
DD
1985 if (tids != NULL)
1986 tids[nr_tids] = tid;
1da177e4
LT
1987 nr_tids++;
1988 if (nr_tids >= PROC_MAXPIDS)
1989 break;
1990 } while ((task = next_thread(task)) != leader_task);
1991 read_unlock(&tasklist_lock);
1992 return nr_tids;
1993}
1994
1995/* for the /proc/ directory itself, after non-process stuff has been done */
1996int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1997{
1998 unsigned int tgid_array[PROC_MAXPIDS];
1999 char buf[PROC_NUMBUF];
2000 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2001 unsigned int nr_tgids, i;
2002 int next_tgid;
2003
2004 if (!nr) {
2005 ino_t ino = fake_ino(0,PROC_TGID_INO);
2006 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2007 return 0;
2008 filp->f_pos++;
2009 nr++;
2010 }
2011
2012 /* f_version caches the tgid value that the last readdir call couldn't
2013 * return. lseek aka telldir automagically resets f_version to 0.
2014 */
2015 next_tgid = filp->f_version;
2016 filp->f_version = 0;
2017 for (;;) {
2018 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2019 if (!nr_tgids) {
2020 /* no more entries ! */
2021 break;
2022 }
2023 next_tgid = 0;
2024
2025 /* do not use the last found pid, reserve it for next_tgid */
2026 if (nr_tgids == PROC_MAXPIDS) {
2027 nr_tgids--;
2028 next_tgid = tgid_array[nr_tgids];
2029 }
2030
2031 for (i=0;i<nr_tgids;i++) {
2032 int tgid = tgid_array[i];
2033 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2034 unsigned long j = PROC_NUMBUF;
2035
2036 do
2037 buf[--j] = '0' + (tgid % 10);
2038 while ((tgid /= 10) != 0);
2039
2040 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2041 /* returning this tgid failed, save it as the first
2042 * pid for the next readir call */
2043 filp->f_version = tgid_array[i];
2044 goto out;
2045 }
2046 filp->f_pos++;
2047 nr++;
2048 }
2049 }
2050out:
2051 return 0;
2052}
2053
2054/* for the /proc/TGID/task/ directories */
2055static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2056{
2057 unsigned int tid_array[PROC_MAXPIDS];
2058 char buf[PROC_NUMBUF];
2059 unsigned int nr_tids, i;
2060 struct dentry *dentry = filp->f_dentry;
2061 struct inode *inode = dentry->d_inode;
2062 int retval = -ENOENT;
2063 ino_t ino;
2064 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2065
2066 if (!pid_alive(proc_task(inode)))
2067 goto out;
2068 retval = 0;
2069
2070 switch (pos) {
2071 case 0:
2072 ino = inode->i_ino;
2073 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2074 goto out;
2075 pos++;
2076 /* fall through */
2077 case 1:
2078 ino = parent_ino(dentry);
2079 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2080 goto out;
2081 pos++;
2082 /* fall through */
2083 }
2084
2085 nr_tids = get_tid_list(pos, tid_array, inode);
f246315e 2086 inode->i_nlink = pos + nr_tids;
1da177e4
LT
2087
2088 for (i = 0; i < nr_tids; i++) {
2089 unsigned long j = PROC_NUMBUF;
2090 int tid = tid_array[i];
2091
2092 ino = fake_ino(tid,PROC_TID_INO);
2093
2094 do
2095 buf[--j] = '0' + (tid % 10);
2096 while ((tid /= 10) != 0);
2097
2098 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2099 break;
2100 pos++;
2101 }
2102out:
2103 filp->f_pos = pos;
2104 return retval;
2105}
This page took 0.135674 seconds and 5 git commands to generate.