11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 561da177e4SLinus Torvalds #include <linux/init.h> 5716f7e0feSRandy Dunlap #include <linux/capability.h> 581da177e4SLinus Torvalds #include <linux/file.h> 591da177e4SLinus Torvalds #include <linux/string.h> 601da177e4SLinus Torvalds #include <linux/seq_file.h> 611da177e4SLinus Torvalds #include <linux/namei.h> 626b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 631da177e4SLinus Torvalds #include <linux/mm.h> 64b835996fSDipankar Sarma #include <linux/rcupdate.h> 651da177e4SLinus Torvalds #include <linux/kallsyms.h> 66d85f50d5SNeil Horman #include <linux/resource.h> 675096add8SKees Cook #include <linux/module.h> 681da177e4SLinus Torvalds #include <linux/mount.h> 691da177e4SLinus Torvalds #include <linux/security.h> 701da177e4SLinus Torvalds #include <linux/ptrace.h> 71a424316cSPaul Menage #include <linux/cgroup.h> 721da177e4SLinus Torvalds #include <linux/cpuset.h> 731da177e4SLinus Torvalds #include <linux/audit.h> 745addc5ddSAl Viro #include <linux/poll.h> 751651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 768ac773b4SAlexey Dobriyan #include <linux/oom.h> 773cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 7860347f67SPavel Emelyanov #include <linux/pid_namespace.h> 791da177e4SLinus Torvalds #include "internal.h" 801da177e4SLinus Torvalds 810f2fe20fSEric W. Biederman /* NOTE: 820f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 830f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 840f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 850f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 860f2fe20fSEric W. Biederman * 870f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 880f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 890f2fe20fSEric W. Biederman */ 900f2fe20fSEric W. Biederman 911da177e4SLinus Torvalds struct pid_entry { 921da177e4SLinus Torvalds char *name; 93c5141e6dSEric Dumazet int len; 941da177e4SLinus Torvalds mode_t mode; 95c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9600977a59SArjan van de Ven const struct file_operations *fop; 9720cdc894SEric W. Biederman union proc_op op; 981da177e4SLinus Torvalds }; 991da177e4SLinus Torvalds 10061a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10120cdc894SEric W. Biederman .name = (NAME), \ 102c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10320cdc894SEric W. Biederman .mode = MODE, \ 10420cdc894SEric W. Biederman .iop = IOP, \ 10520cdc894SEric W. Biederman .fop = FOP, \ 10620cdc894SEric W. Biederman .op = OP, \ 10720cdc894SEric W. Biederman } 10820cdc894SEric W. Biederman 10961a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11061a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11120cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11220cdc894SEric W. Biederman {} ) 11361a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11461a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11520cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11620cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11761a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11861a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 11920cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12061a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12161a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12220cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12320cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 124be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 125be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 126be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 127be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1281da177e4SLinus Torvalds 1295096add8SKees Cook int maps_protect; 1305096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1315096add8SKees Cook 1320494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct fs_struct *fs; 1350494f6ecSMiklos Szeredi task_lock(task); 1360494f6ecSMiklos Szeredi fs = task->fs; 1371da177e4SLinus Torvalds if(fs) 1381da177e4SLinus Torvalds atomic_inc(&fs->count); 1390494f6ecSMiklos Szeredi task_unlock(task); 1400494f6ecSMiklos Szeredi return fs; 1410494f6ecSMiklos Szeredi } 1420494f6ecSMiklos Szeredi 14399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14499f89551SEric W. Biederman { 14599f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14699f89551SEric W. Biederman unsigned long flags; 14799f89551SEric W. Biederman int count = 0; 14899f89551SEric W. Biederman 14999f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 15099f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 15199f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 15299f89551SEric W. Biederman } 15399f89551SEric W. Biederman return count; 15499f89551SEric W. Biederman } 15599f89551SEric W. Biederman 1563dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1570494f6ecSMiklos Szeredi { 15899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15999f89551SEric W. Biederman struct fs_struct *fs = NULL; 1600494f6ecSMiklos Szeredi int result = -ENOENT; 16199f89551SEric W. Biederman 16299f89551SEric W. Biederman if (task) { 16399f89551SEric W. Biederman fs = get_fs_struct(task); 16499f89551SEric W. Biederman put_task_struct(task); 16599f89551SEric W. Biederman } 1661da177e4SLinus Torvalds if (fs) { 1671da177e4SLinus Torvalds read_lock(&fs->lock); 1683dcd25f3SJan Blunck *path = fs->pwd; 1693dcd25f3SJan Blunck path_get(&fs->pwd); 1701da177e4SLinus Torvalds read_unlock(&fs->lock); 1711da177e4SLinus Torvalds result = 0; 1721da177e4SLinus Torvalds put_fs_struct(fs); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds return result; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1773dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1781da177e4SLinus Torvalds { 17999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 18099f89551SEric W. Biederman struct fs_struct *fs = NULL; 1811da177e4SLinus Torvalds int result = -ENOENT; 18299f89551SEric W. Biederman 18399f89551SEric W. Biederman if (task) { 18499f89551SEric W. Biederman fs = get_fs_struct(task); 18599f89551SEric W. Biederman put_task_struct(task); 18699f89551SEric W. Biederman } 1871da177e4SLinus Torvalds if (fs) { 1881da177e4SLinus Torvalds read_lock(&fs->lock); 1893dcd25f3SJan Blunck *path = fs->root; 1903dcd25f3SJan Blunck path_get(&fs->root); 1911da177e4SLinus Torvalds read_unlock(&fs->lock); 1921da177e4SLinus Torvalds result = 0; 1931da177e4SLinus Torvalds put_fs_struct(fs); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds return result; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1991da177e4SLinus Torvalds (task == current || \ 2001da177e4SLinus Torvalds (task->parent == current && \ 2011da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 2026d8982d9SMatthew Wilcox (task_is_stopped_or_traced(task)) && \ 2031da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2041da177e4SLinus Torvalds 205831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 206831830b5SAl Viro { 207831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 208831830b5SAl Viro if (!mm) 209831830b5SAl Viro return NULL; 210831830b5SAl Viro down_read(&mm->mmap_sem); 211831830b5SAl Viro task_lock(task); 212831830b5SAl Viro if (task->mm != mm) 213831830b5SAl Viro goto out; 214831830b5SAl Viro if (task->mm != current->mm && __ptrace_may_attach(task) < 0) 215831830b5SAl Viro goto out; 216831830b5SAl Viro task_unlock(task); 217831830b5SAl Viro return mm; 218831830b5SAl Viro out: 219831830b5SAl Viro task_unlock(task); 220831830b5SAl Viro up_read(&mm->mmap_sem); 221831830b5SAl Viro mmput(mm); 222831830b5SAl Viro return NULL; 223831830b5SAl Viro } 224831830b5SAl Viro 2251da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2261da177e4SLinus Torvalds { 2271da177e4SLinus Torvalds int res = 0; 2281da177e4SLinus Torvalds unsigned int len; 2291da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2301da177e4SLinus Torvalds if (!mm) 2311da177e4SLinus Torvalds goto out; 2321da177e4SLinus Torvalds if (!mm->arg_end) 2331da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (len > PAGE_SIZE) 2381da177e4SLinus Torvalds len = PAGE_SIZE; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2431da177e4SLinus Torvalds // assume application is using setproctitle(3). 2441da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2451da177e4SLinus Torvalds len = strnlen(buffer, res); 2461da177e4SLinus Torvalds if (len < res) { 2471da177e4SLinus Torvalds res = len; 2481da177e4SLinus Torvalds } else { 2491da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2501da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2511da177e4SLinus Torvalds len = PAGE_SIZE - res; 2521da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2531da177e4SLinus Torvalds res = strnlen(buffer, res); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds out_mm: 2571da177e4SLinus Torvalds mmput(mm); 2581da177e4SLinus Torvalds out: 2591da177e4SLinus Torvalds return res; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds int res = 0; 2651da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2661da177e4SLinus Torvalds if (mm) { 2671da177e4SLinus Torvalds unsigned int nwords = 0; 2681da177e4SLinus Torvalds do 2691da177e4SLinus Torvalds nwords += 2; 2701da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2711da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2721da177e4SLinus Torvalds if (res > PAGE_SIZE) 2731da177e4SLinus Torvalds res = PAGE_SIZE; 2741da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2751da177e4SLinus Torvalds mmput(mm); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds return res; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2841da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2871da177e4SLinus Torvalds { 288ffb45122SAlexey Dobriyan unsigned long wchan; 2899281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds wchan = get_wchan(task); 2921da177e4SLinus Torvalds 2939d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2941da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2959d65cb4aSAlexey Dobriyan else 2969d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3011da177e4SLinus Torvalds /* 3021da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3031da177e4SLinus Torvalds */ 3041da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3051da177e4SLinus Torvalds { 306172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3071da177e4SLinus Torvalds task->sched_info.cpu_time, 3081da177e4SLinus Torvalds task->sched_info.run_delay, 3092d72376bSIngo Molnar task->sched_info.pcount); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds #endif 3121da177e4SLinus Torvalds 3139745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3149745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3159745512cSArjan van de Ven { 3169745512cSArjan van de Ven int i; 31713d77c37SHiroshi Shimamoto struct inode *inode = m->private; 31813d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3199745512cSArjan van de Ven 32013d77c37SHiroshi Shimamoto if (!task) 32113d77c37SHiroshi Shimamoto return -ESRCH; 32213d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3239745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3249745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3259745512cSArjan van de Ven int q; 3269745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3279745512cSArjan van de Ven task->latency_record[i].count, 3289745512cSArjan van de Ven task->latency_record[i].time, 3299745512cSArjan van de Ven task->latency_record[i].max); 3309745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3319745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3329745512cSArjan van de Ven char *c; 3339745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3349745512cSArjan van de Ven break; 3359745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3369745512cSArjan van de Ven break; 3379745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3389745512cSArjan van de Ven c = strchr(sym, '+'); 3399745512cSArjan van de Ven if (c) 3409745512cSArjan van de Ven *c = 0; 3419745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3429745512cSArjan van de Ven } 3439745512cSArjan van de Ven seq_printf(m, "\n"); 3449745512cSArjan van de Ven } 3459745512cSArjan van de Ven 3469745512cSArjan van de Ven } 34713d77c37SHiroshi Shimamoto put_task_struct(task); 3489745512cSArjan van de Ven return 0; 3499745512cSArjan van de Ven } 3509745512cSArjan van de Ven 3519745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3529745512cSArjan van de Ven { 35313d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 354d6643d12SHiroshi Shimamoto } 355d6643d12SHiroshi Shimamoto 3569745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3579745512cSArjan van de Ven size_t count, loff_t *offs) 3589745512cSArjan van de Ven { 35913d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 3609745512cSArjan van de Ven 36113d77c37SHiroshi Shimamoto if (!task) 36213d77c37SHiroshi Shimamoto return -ESRCH; 3639745512cSArjan van de Ven clear_all_latency_tracing(task); 36413d77c37SHiroshi Shimamoto put_task_struct(task); 3659745512cSArjan van de Ven 3669745512cSArjan van de Ven return count; 3679745512cSArjan van de Ven } 3689745512cSArjan van de Ven 3699745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3709745512cSArjan van de Ven .open = lstats_open, 3719745512cSArjan van de Ven .read = seq_read, 3729745512cSArjan van de Ven .write = lstats_write, 3739745512cSArjan van de Ven .llseek = seq_lseek, 37413d77c37SHiroshi Shimamoto .release = single_release, 3759745512cSArjan van de Ven }; 3769745512cSArjan van de Ven 3779745512cSArjan van de Ven #endif 3789745512cSArjan van de Ven 3791da177e4SLinus Torvalds /* The badness from the OOM killer */ 3801da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 3811da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 3821da177e4SLinus Torvalds { 3831da177e4SLinus Torvalds unsigned long points; 3841da177e4SLinus Torvalds struct timespec uptime; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 38719c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3881da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 38919c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3901da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 393d85f50d5SNeil Horman struct limit_names { 394d85f50d5SNeil Horman char *name; 395d85f50d5SNeil Horman char *unit; 396d85f50d5SNeil Horman }; 397d85f50d5SNeil Horman 398d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 399d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 400d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 401d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 402d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 403d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 404d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 405d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 406d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 407d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 408d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 409d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 410d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 411d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 412d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 413d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4148808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 415d85f50d5SNeil Horman }; 416d85f50d5SNeil Horman 417d85f50d5SNeil Horman /* Display limits for a process */ 418d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 419d85f50d5SNeil Horman { 420d85f50d5SNeil Horman unsigned int i; 421d85f50d5SNeil Horman int count = 0; 422d85f50d5SNeil Horman unsigned long flags; 423d85f50d5SNeil Horman char *bufptr = buffer; 424d85f50d5SNeil Horman 425d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 426d85f50d5SNeil Horman 427d85f50d5SNeil Horman rcu_read_lock(); 428d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 429d85f50d5SNeil Horman rcu_read_unlock(); 430d85f50d5SNeil Horman return 0; 431d85f50d5SNeil Horman } 432d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 433d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 434d85f50d5SNeil Horman rcu_read_unlock(); 435d85f50d5SNeil Horman 436d85f50d5SNeil Horman /* 437d85f50d5SNeil Horman * print the file header 438d85f50d5SNeil Horman */ 439d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 440d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 441d85f50d5SNeil Horman 442d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 443d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 444d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 445d85f50d5SNeil Horman lnames[i].name, "unlimited"); 446d85f50d5SNeil Horman else 447d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 448d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 449d85f50d5SNeil Horman 450d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 451d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 452d85f50d5SNeil Horman else 453d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 454d85f50d5SNeil Horman rlim[i].rlim_max); 455d85f50d5SNeil Horman 456d85f50d5SNeil Horman if (lnames[i].unit) 457d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 458d85f50d5SNeil Horman lnames[i].unit); 459d85f50d5SNeil Horman else 460d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 461d85f50d5SNeil Horman } 462d85f50d5SNeil Horman 463d85f50d5SNeil Horman return count; 464d85f50d5SNeil Horman } 465d85f50d5SNeil Horman 4661da177e4SLinus Torvalds /************************************************************************/ 4671da177e4SLinus Torvalds /* Here the fs part begins */ 4681da177e4SLinus Torvalds /************************************************************************/ 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* permission checks */ 471778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 4721da177e4SLinus Torvalds { 473778c1144SEric W. Biederman struct task_struct *task; 474778c1144SEric W. Biederman int allowed = 0; 475df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 476df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 477df26c40eSEric W. Biederman * information. 478778c1144SEric W. Biederman */ 479778c1144SEric W. Biederman task = get_proc_task(inode); 480df26c40eSEric W. Biederman if (task) { 481778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 482778c1144SEric W. Biederman put_task_struct(task); 483df26c40eSEric W. Biederman } 484778c1144SEric W. Biederman return allowed; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4876d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 4886d76fa58SLinus Torvalds { 4896d76fa58SLinus Torvalds int error; 4906d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 4916d76fa58SLinus Torvalds 4926d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 4936d76fa58SLinus Torvalds return -EPERM; 4946d76fa58SLinus Torvalds 4956d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 4966d76fa58SLinus Torvalds if (!error) 4976d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 4986d76fa58SLinus Torvalds return error; 4996d76fa58SLinus Torvalds } 5006d76fa58SLinus Torvalds 501c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5026d76fa58SLinus Torvalds .setattr = proc_setattr, 5036d76fa58SLinus Torvalds }; 5046d76fa58SLinus Torvalds 505a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 506a1a2c409SMiklos Szeredi const struct seq_operations *op) 5071da177e4SLinus Torvalds { 50899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 509cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5106b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 511a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 512a1a2c409SMiklos Szeredi struct path root; 5135addc5ddSAl Viro struct proc_mounts *p; 5145addc5ddSAl Viro int ret = -EINVAL; 5155addc5ddSAl Viro 51699f89551SEric W. Biederman if (task) { 517cf7b708cSPavel Emelyanov rcu_read_lock(); 518cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 519cf7b708cSPavel Emelyanov if (nsp) { 520cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5216b3286edSKirill Korotaev if (ns) 5226b3286edSKirill Korotaev get_mnt_ns(ns); 523863c4702SAlexey Dobriyan } 524cf7b708cSPavel Emelyanov rcu_read_unlock(); 525a1a2c409SMiklos Szeredi if (ns) 526a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 52799f89551SEric W. Biederman put_task_struct(task); 52899f89551SEric W. Biederman } 5291da177e4SLinus Torvalds 530a1a2c409SMiklos Szeredi if (!ns) 531a1a2c409SMiklos Szeredi goto err; 532a1a2c409SMiklos Szeredi if (!fs) 533a1a2c409SMiklos Szeredi goto err_put_ns; 534a1a2c409SMiklos Szeredi 535a1a2c409SMiklos Szeredi read_lock(&fs->lock); 536a1a2c409SMiklos Szeredi root = fs->root; 537a1a2c409SMiklos Szeredi path_get(&root); 538a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 539a1a2c409SMiklos Szeredi put_fs_struct(fs); 540a1a2c409SMiklos Szeredi 5415addc5ddSAl Viro ret = -ENOMEM; 5425addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 543a1a2c409SMiklos Szeredi if (!p) 544a1a2c409SMiklos Szeredi goto err_put_path; 545a1a2c409SMiklos Szeredi 5465addc5ddSAl Viro file->private_data = &p->m; 547a1a2c409SMiklos Szeredi ret = seq_open(file, op); 548a1a2c409SMiklos Szeredi if (ret) 549a1a2c409SMiklos Szeredi goto err_free; 550a1a2c409SMiklos Szeredi 551a1a2c409SMiklos Szeredi p->m.private = p; 552a1a2c409SMiklos Szeredi p->ns = ns; 553a1a2c409SMiklos Szeredi p->root = root; 5546b3286edSKirill Korotaev p->event = ns->event; 555a1a2c409SMiklos Szeredi 5565addc5ddSAl Viro return 0; 557a1a2c409SMiklos Szeredi 558a1a2c409SMiklos Szeredi err_free: 5595addc5ddSAl Viro kfree(p); 560a1a2c409SMiklos Szeredi err_put_path: 561a1a2c409SMiklos Szeredi path_put(&root); 562a1a2c409SMiklos Szeredi err_put_ns: 5636b3286edSKirill Korotaev put_mnt_ns(ns); 564a1a2c409SMiklos Szeredi err: 5651da177e4SLinus Torvalds return ret; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 5691da177e4SLinus Torvalds { 570a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 571a1a2c409SMiklos Szeredi path_put(&p->root); 572a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 5731da177e4SLinus Torvalds return seq_release(inode, file); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5765addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 5775addc5ddSAl Viro { 5785addc5ddSAl Viro struct proc_mounts *p = file->private_data; 579a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 5805addc5ddSAl Viro unsigned res = 0; 5815addc5ddSAl Viro 5825addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 5835addc5ddSAl Viro 5845addc5ddSAl Viro spin_lock(&vfsmount_lock); 5855addc5ddSAl Viro if (p->event != ns->event) { 5865addc5ddSAl Viro p->event = ns->event; 5875addc5ddSAl Viro res = POLLERR; 5885addc5ddSAl Viro } 5895addc5ddSAl Viro spin_unlock(&vfsmount_lock); 5905addc5ddSAl Viro 5915addc5ddSAl Viro return res; 5925addc5ddSAl Viro } 5935addc5ddSAl Viro 594a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 595a1a2c409SMiklos Szeredi { 596a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 597a1a2c409SMiklos Szeredi } 598a1a2c409SMiklos Szeredi 59900977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6001da177e4SLinus Torvalds .open = mounts_open, 6011da177e4SLinus Torvalds .read = seq_read, 6021da177e4SLinus Torvalds .llseek = seq_lseek, 6031da177e4SLinus Torvalds .release = mounts_release, 6045addc5ddSAl Viro .poll = mounts_poll, 6051da177e4SLinus Torvalds }; 6061da177e4SLinus Torvalds 6072d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6082d4d4864SRam Pai { 6092d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6102d4d4864SRam Pai } 6112d4d4864SRam Pai 6122d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6132d4d4864SRam Pai .open = mountinfo_open, 6142d4d4864SRam Pai .read = seq_read, 6152d4d4864SRam Pai .llseek = seq_lseek, 6162d4d4864SRam Pai .release = mounts_release, 6172d4d4864SRam Pai .poll = mounts_poll, 6182d4d4864SRam Pai }; 6192d4d4864SRam Pai 620b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 621b4629fe2SChuck Lever { 622a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 623b4629fe2SChuck Lever } 624b4629fe2SChuck Lever 62500977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 626b4629fe2SChuck Lever .open = mountstats_open, 627b4629fe2SChuck Lever .read = seq_read, 628b4629fe2SChuck Lever .llseek = seq_lseek, 629b4629fe2SChuck Lever .release = mounts_release, 630b4629fe2SChuck Lever }; 631b4629fe2SChuck Lever 6321da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6351da177e4SLinus Torvalds size_t count, loff_t *ppos) 6361da177e4SLinus Torvalds { 6372fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6381da177e4SLinus Torvalds unsigned long page; 6391da177e4SLinus Torvalds ssize_t length; 64099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 64199f89551SEric W. Biederman 64299f89551SEric W. Biederman length = -ESRCH; 64399f89551SEric W. Biederman if (!task) 64499f89551SEric W. Biederman goto out_no_task; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6471da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 64899f89551SEric W. Biederman 64999f89551SEric W. Biederman length = -ENOMEM; 650e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 65199f89551SEric W. Biederman goto out; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds if (length >= 0) 6561da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6571da177e4SLinus Torvalds free_page(page); 65899f89551SEric W. Biederman out: 65999f89551SEric W. Biederman put_task_struct(task); 66099f89551SEric W. Biederman out_no_task: 6611da177e4SLinus Torvalds return length; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 66400977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6651da177e4SLinus Torvalds .read = proc_info_read, 6661da177e4SLinus Torvalds }; 6671da177e4SLinus Torvalds 668be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 669be614086SEric W. Biederman { 670be614086SEric W. Biederman struct inode *inode = m->private; 671be614086SEric W. Biederman struct pid_namespace *ns; 672be614086SEric W. Biederman struct pid *pid; 673be614086SEric W. Biederman struct task_struct *task; 674be614086SEric W. Biederman int ret; 675be614086SEric W. Biederman 676be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 677be614086SEric W. Biederman pid = proc_pid(inode); 678be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 679be614086SEric W. Biederman if (!task) 680be614086SEric W. Biederman return -ESRCH; 681be614086SEric W. Biederman 682be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 683be614086SEric W. Biederman 684be614086SEric W. Biederman put_task_struct(task); 685be614086SEric W. Biederman return ret; 686be614086SEric W. Biederman } 687be614086SEric W. Biederman 688be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 689be614086SEric W. Biederman { 690be614086SEric W. Biederman int ret; 691be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 692be614086SEric W. Biederman if (!ret) { 693be614086SEric W. Biederman struct seq_file *m = filp->private_data; 694be614086SEric W. Biederman 695be614086SEric W. Biederman m->private = inode; 696be614086SEric W. Biederman } 697be614086SEric W. Biederman return ret; 698be614086SEric W. Biederman } 699be614086SEric W. Biederman 700be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 701be614086SEric W. Biederman .open = proc_single_open, 702be614086SEric W. Biederman .read = seq_read, 703be614086SEric W. Biederman .llseek = seq_lseek, 704be614086SEric W. Biederman .release = single_release, 705be614086SEric W. Biederman }; 706be614086SEric W. Biederman 7071da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7081da177e4SLinus Torvalds { 7091da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7101da177e4SLinus Torvalds return 0; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7141da177e4SLinus Torvalds size_t count, loff_t *ppos) 7151da177e4SLinus Torvalds { 7162fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7171da177e4SLinus Torvalds char *page; 7181da177e4SLinus Torvalds unsigned long src = *ppos; 7191da177e4SLinus Torvalds int ret = -ESRCH; 7201da177e4SLinus Torvalds struct mm_struct *mm; 7211da177e4SLinus Torvalds 72299f89551SEric W. Biederman if (!task) 72399f89551SEric W. Biederman goto out_no_task; 72499f89551SEric W. Biederman 725ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 7261da177e4SLinus Torvalds goto out; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds ret = -ENOMEM; 729e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7301da177e4SLinus Torvalds if (!page) 7311da177e4SLinus Torvalds goto out; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds ret = 0; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds mm = get_task_mm(task); 7361da177e4SLinus Torvalds if (!mm) 7371da177e4SLinus Torvalds goto out_free; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds ret = -EIO; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7421da177e4SLinus Torvalds goto out_put; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds ret = 0; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds while (count > 0) { 7471da177e4SLinus Torvalds int this_len, retval; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7501da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 751ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 7521da177e4SLinus Torvalds if (!ret) 7531da177e4SLinus Torvalds ret = -EIO; 7541da177e4SLinus Torvalds break; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7581da177e4SLinus Torvalds ret = -EFAULT; 7591da177e4SLinus Torvalds break; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds ret += retval; 7631da177e4SLinus Torvalds src += retval; 7641da177e4SLinus Torvalds buf += retval; 7651da177e4SLinus Torvalds count -= retval; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds *ppos = src; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds out_put: 7701da177e4SLinus Torvalds mmput(mm); 7711da177e4SLinus Torvalds out_free: 7721da177e4SLinus Torvalds free_page((unsigned long) page); 7731da177e4SLinus Torvalds out: 77499f89551SEric W. Biederman put_task_struct(task); 77599f89551SEric W. Biederman out_no_task: 7761da177e4SLinus Torvalds return ret; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds #define mem_write NULL 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds #ifndef mem_write 7821da177e4SLinus Torvalds /* This is a security hazard */ 78363967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 7841da177e4SLinus Torvalds size_t count, loff_t *ppos) 7851da177e4SLinus Torvalds { 786f7ca54f4SFrederik Deweerdt int copied; 7871da177e4SLinus Torvalds char *page; 7882fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7891da177e4SLinus Torvalds unsigned long dst = *ppos; 7901da177e4SLinus Torvalds 79199f89551SEric W. Biederman copied = -ESRCH; 79299f89551SEric W. Biederman if (!task) 79399f89551SEric W. Biederman goto out_no_task; 7941da177e4SLinus Torvalds 79599f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 79699f89551SEric W. Biederman goto out; 79799f89551SEric W. Biederman 79899f89551SEric W. Biederman copied = -ENOMEM; 799e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8001da177e4SLinus Torvalds if (!page) 80199f89551SEric W. Biederman goto out; 8021da177e4SLinus Torvalds 803f7ca54f4SFrederik Deweerdt copied = 0; 8041da177e4SLinus Torvalds while (count > 0) { 8051da177e4SLinus Torvalds int this_len, retval; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8081da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8091da177e4SLinus Torvalds copied = -EFAULT; 8101da177e4SLinus Torvalds break; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8131da177e4SLinus Torvalds if (!retval) { 8141da177e4SLinus Torvalds if (!copied) 8151da177e4SLinus Torvalds copied = -EIO; 8161da177e4SLinus Torvalds break; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds copied += retval; 8191da177e4SLinus Torvalds buf += retval; 8201da177e4SLinus Torvalds dst += retval; 8211da177e4SLinus Torvalds count -= retval; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds *ppos = dst; 8241da177e4SLinus Torvalds free_page((unsigned long) page); 82599f89551SEric W. Biederman out: 82699f89551SEric W. Biederman put_task_struct(task); 82799f89551SEric W. Biederman out_no_task: 8281da177e4SLinus Torvalds return copied; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds #endif 8311da177e4SLinus Torvalds 83285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds switch (orig) { 8351da177e4SLinus Torvalds case 0: 8361da177e4SLinus Torvalds file->f_pos = offset; 8371da177e4SLinus Torvalds break; 8381da177e4SLinus Torvalds case 1: 8391da177e4SLinus Torvalds file->f_pos += offset; 8401da177e4SLinus Torvalds break; 8411da177e4SLinus Torvalds default: 8421da177e4SLinus Torvalds return -EINVAL; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds force_successful_syscall_return(); 8451da177e4SLinus Torvalds return file->f_pos; 8461da177e4SLinus Torvalds } 8471da177e4SLinus Torvalds 84800977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8491da177e4SLinus Torvalds .llseek = mem_lseek, 8501da177e4SLinus Torvalds .read = mem_read, 8511da177e4SLinus Torvalds .write = mem_write, 8521da177e4SLinus Torvalds .open = mem_open, 8531da177e4SLinus Torvalds }; 8541da177e4SLinus Torvalds 855315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 856315e28c8SJames Pearson size_t count, loff_t *ppos) 857315e28c8SJames Pearson { 858315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 859315e28c8SJames Pearson char *page; 860315e28c8SJames Pearson unsigned long src = *ppos; 861315e28c8SJames Pearson int ret = -ESRCH; 862315e28c8SJames Pearson struct mm_struct *mm; 863315e28c8SJames Pearson 864315e28c8SJames Pearson if (!task) 865315e28c8SJames Pearson goto out_no_task; 866315e28c8SJames Pearson 867315e28c8SJames Pearson if (!ptrace_may_attach(task)) 868315e28c8SJames Pearson goto out; 869315e28c8SJames Pearson 870315e28c8SJames Pearson ret = -ENOMEM; 871315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 872315e28c8SJames Pearson if (!page) 873315e28c8SJames Pearson goto out; 874315e28c8SJames Pearson 875315e28c8SJames Pearson ret = 0; 876315e28c8SJames Pearson 877315e28c8SJames Pearson mm = get_task_mm(task); 878315e28c8SJames Pearson if (!mm) 879315e28c8SJames Pearson goto out_free; 880315e28c8SJames Pearson 881315e28c8SJames Pearson while (count > 0) { 882315e28c8SJames Pearson int this_len, retval, max_len; 883315e28c8SJames Pearson 884315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 885315e28c8SJames Pearson 886315e28c8SJames Pearson if (this_len <= 0) 887315e28c8SJames Pearson break; 888315e28c8SJames Pearson 889315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 890315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 891315e28c8SJames Pearson 892315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 893315e28c8SJames Pearson page, this_len, 0); 894315e28c8SJames Pearson 895315e28c8SJames Pearson if (retval <= 0) { 896315e28c8SJames Pearson ret = retval; 897315e28c8SJames Pearson break; 898315e28c8SJames Pearson } 899315e28c8SJames Pearson 900315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 901315e28c8SJames Pearson ret = -EFAULT; 902315e28c8SJames Pearson break; 903315e28c8SJames Pearson } 904315e28c8SJames Pearson 905315e28c8SJames Pearson ret += retval; 906315e28c8SJames Pearson src += retval; 907315e28c8SJames Pearson buf += retval; 908315e28c8SJames Pearson count -= retval; 909315e28c8SJames Pearson } 910315e28c8SJames Pearson *ppos = src; 911315e28c8SJames Pearson 912315e28c8SJames Pearson mmput(mm); 913315e28c8SJames Pearson out_free: 914315e28c8SJames Pearson free_page((unsigned long) page); 915315e28c8SJames Pearson out: 916315e28c8SJames Pearson put_task_struct(task); 917315e28c8SJames Pearson out_no_task: 918315e28c8SJames Pearson return ret; 919315e28c8SJames Pearson } 920315e28c8SJames Pearson 921315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 922315e28c8SJames Pearson .read = environ_read, 923315e28c8SJames Pearson }; 924315e28c8SJames Pearson 9251da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9261da177e4SLinus Torvalds size_t count, loff_t *ppos) 9271da177e4SLinus Torvalds { 9282fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9298578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9301da177e4SLinus Torvalds size_t len; 93199f89551SEric W. Biederman int oom_adjust; 9321da177e4SLinus Torvalds 93399f89551SEric W. Biederman if (!task) 93499f89551SEric W. Biederman return -ESRCH; 93599f89551SEric W. Biederman oom_adjust = task->oomkilladj; 93699f89551SEric W. Biederman put_task_struct(task); 93799f89551SEric W. Biederman 9388578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9390c28f287SAkinobu Mita 9400c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9441da177e4SLinus Torvalds size_t count, loff_t *ppos) 9451da177e4SLinus Torvalds { 94699f89551SEric W. Biederman struct task_struct *task; 9478578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 9481da177e4SLinus Torvalds int oom_adjust; 9491da177e4SLinus Torvalds 9508578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9518578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9528578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9531da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 9541da177e4SLinus Torvalds return -EFAULT; 9551da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 9568ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 9578ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 9581da177e4SLinus Torvalds return -EINVAL; 9591da177e4SLinus Torvalds if (*end == '\n') 9601da177e4SLinus Torvalds end++; 9612fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 96299f89551SEric W. Biederman if (!task) 96399f89551SEric W. Biederman return -ESRCH; 9648fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 9658fb4fc68SGuillem Jover put_task_struct(task); 9668fb4fc68SGuillem Jover return -EACCES; 9678fb4fc68SGuillem Jover } 9681da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 96999f89551SEric W. Biederman put_task_struct(task); 9701da177e4SLinus Torvalds if (end - buffer == 0) 9711da177e4SLinus Torvalds return -EIO; 9721da177e4SLinus Torvalds return end - buffer; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 97500977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 9761da177e4SLinus Torvalds .read = oom_adjust_read, 9771da177e4SLinus Torvalds .write = oom_adjust_write, 9781da177e4SLinus Torvalds }; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 9811da177e4SLinus Torvalds #define TMPBUFLEN 21 9821da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 9831da177e4SLinus Torvalds size_t count, loff_t *ppos) 9841da177e4SLinus Torvalds { 9852fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 98699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 9871da177e4SLinus Torvalds ssize_t length; 9881da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 9891da177e4SLinus Torvalds 99099f89551SEric W. Biederman if (!task) 99199f89551SEric W. Biederman return -ESRCH; 9921da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 9930c11b942SAl Viro audit_get_loginuid(task)); 99499f89551SEric W. Biederman put_task_struct(task); 9951da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 9991da177e4SLinus Torvalds size_t count, loff_t *ppos) 10001da177e4SLinus Torvalds { 10012fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10021da177e4SLinus Torvalds char *page, *tmp; 10031da177e4SLinus Torvalds ssize_t length; 10041da177e4SLinus Torvalds uid_t loginuid; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10071da177e4SLinus Torvalds return -EPERM; 10081da177e4SLinus Torvalds 100913b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10101da177e4SLinus Torvalds return -EPERM; 10111da177e4SLinus Torvalds 1012e0182909SAl Viro if (count >= PAGE_SIZE) 1013e0182909SAl Viro count = PAGE_SIZE - 1; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds if (*ppos != 0) { 10161da177e4SLinus Torvalds /* No partial writes. */ 10171da177e4SLinus Torvalds return -EINVAL; 10181da177e4SLinus Torvalds } 1019e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10201da177e4SLinus Torvalds if (!page) 10211da177e4SLinus Torvalds return -ENOMEM; 10221da177e4SLinus Torvalds length = -EFAULT; 10231da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10241da177e4SLinus Torvalds goto out_free_page; 10251da177e4SLinus Torvalds 1026e0182909SAl Viro page[count] = '\0'; 10271da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10281da177e4SLinus Torvalds if (tmp == page) { 10291da177e4SLinus Torvalds length = -EINVAL; 10301da177e4SLinus Torvalds goto out_free_page; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds } 103399f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10341da177e4SLinus Torvalds if (likely(length == 0)) 10351da177e4SLinus Torvalds length = count; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds out_free_page: 10381da177e4SLinus Torvalds free_page((unsigned long) page); 10391da177e4SLinus Torvalds return length; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 104200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10431da177e4SLinus Torvalds .read = proc_loginuid_read, 10441da177e4SLinus Torvalds .write = proc_loginuid_write, 10451da177e4SLinus Torvalds }; 10461e0bd755SEric Paris 10471e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 10481e0bd755SEric Paris size_t count, loff_t *ppos) 10491e0bd755SEric Paris { 10501e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 10511e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 10521e0bd755SEric Paris ssize_t length; 10531e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 10541e0bd755SEric Paris 10551e0bd755SEric Paris if (!task) 10561e0bd755SEric Paris return -ESRCH; 10571e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10581e0bd755SEric Paris audit_get_sessionid(task)); 10591e0bd755SEric Paris put_task_struct(task); 10601e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10611e0bd755SEric Paris } 10621e0bd755SEric Paris 10631e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 10641e0bd755SEric Paris .read = proc_sessionid_read, 10651e0bd755SEric Paris }; 10661da177e4SLinus Torvalds #endif 10671da177e4SLinus Torvalds 1068f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1069f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1070f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1071f4f154fdSAkinobu Mita { 1072f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1073f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1074f4f154fdSAkinobu Mita size_t len; 1075f4f154fdSAkinobu Mita int make_it_fail; 1076f4f154fdSAkinobu Mita 1077f4f154fdSAkinobu Mita if (!task) 1078f4f154fdSAkinobu Mita return -ESRCH; 1079f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1080f4f154fdSAkinobu Mita put_task_struct(task); 1081f4f154fdSAkinobu Mita 1082f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 10830c28f287SAkinobu Mita 10840c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1085f4f154fdSAkinobu Mita } 1086f4f154fdSAkinobu Mita 1087f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1088f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1089f4f154fdSAkinobu Mita { 1090f4f154fdSAkinobu Mita struct task_struct *task; 1091f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1092f4f154fdSAkinobu Mita int make_it_fail; 1093f4f154fdSAkinobu Mita 1094f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1095f4f154fdSAkinobu Mita return -EPERM; 1096f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1097f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1098f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1099f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1100f4f154fdSAkinobu Mita return -EFAULT; 1101f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1102f4f154fdSAkinobu Mita if (*end == '\n') 1103f4f154fdSAkinobu Mita end++; 1104f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1105f4f154fdSAkinobu Mita if (!task) 1106f4f154fdSAkinobu Mita return -ESRCH; 1107f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1108f4f154fdSAkinobu Mita put_task_struct(task); 1109f4f154fdSAkinobu Mita if (end - buffer == 0) 1110f4f154fdSAkinobu Mita return -EIO; 1111f4f154fdSAkinobu Mita return end - buffer; 1112f4f154fdSAkinobu Mita } 1113f4f154fdSAkinobu Mita 111400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1115f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1116f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1117f4f154fdSAkinobu Mita }; 1118f4f154fdSAkinobu Mita #endif 1119f4f154fdSAkinobu Mita 11209745512cSArjan van de Ven 112143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 112243ae34cbSIngo Molnar /* 112343ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 112443ae34cbSIngo Molnar */ 112543ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 112643ae34cbSIngo Molnar { 112743ae34cbSIngo Molnar struct inode *inode = m->private; 112843ae34cbSIngo Molnar struct task_struct *p; 112943ae34cbSIngo Molnar 113043ae34cbSIngo Molnar WARN_ON(!inode); 113143ae34cbSIngo Molnar 113243ae34cbSIngo Molnar p = get_proc_task(inode); 113343ae34cbSIngo Molnar if (!p) 113443ae34cbSIngo Molnar return -ESRCH; 113543ae34cbSIngo Molnar proc_sched_show_task(p, m); 113643ae34cbSIngo Molnar 113743ae34cbSIngo Molnar put_task_struct(p); 113843ae34cbSIngo Molnar 113943ae34cbSIngo Molnar return 0; 114043ae34cbSIngo Molnar } 114143ae34cbSIngo Molnar 114243ae34cbSIngo Molnar static ssize_t 114343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 114443ae34cbSIngo Molnar size_t count, loff_t *offset) 114543ae34cbSIngo Molnar { 114643ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 114743ae34cbSIngo Molnar struct task_struct *p; 114843ae34cbSIngo Molnar 114943ae34cbSIngo Molnar WARN_ON(!inode); 115043ae34cbSIngo Molnar 115143ae34cbSIngo Molnar p = get_proc_task(inode); 115243ae34cbSIngo Molnar if (!p) 115343ae34cbSIngo Molnar return -ESRCH; 115443ae34cbSIngo Molnar proc_sched_set_task(p); 115543ae34cbSIngo Molnar 115643ae34cbSIngo Molnar put_task_struct(p); 115743ae34cbSIngo Molnar 115843ae34cbSIngo Molnar return count; 115943ae34cbSIngo Molnar } 116043ae34cbSIngo Molnar 116143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 116243ae34cbSIngo Molnar { 116343ae34cbSIngo Molnar int ret; 116443ae34cbSIngo Molnar 116543ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 116643ae34cbSIngo Molnar if (!ret) { 116743ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 116843ae34cbSIngo Molnar 116943ae34cbSIngo Molnar m->private = inode; 117043ae34cbSIngo Molnar } 117143ae34cbSIngo Molnar return ret; 117243ae34cbSIngo Molnar } 117343ae34cbSIngo Molnar 117443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 117543ae34cbSIngo Molnar .open = sched_open, 117643ae34cbSIngo Molnar .read = seq_read, 117743ae34cbSIngo Molnar .write = sched_write, 117843ae34cbSIngo Molnar .llseek = seq_lseek, 11795ea473a1SAlexey Dobriyan .release = single_release, 118043ae34cbSIngo Molnar }; 118143ae34cbSIngo Molnar 118243ae34cbSIngo Molnar #endif 118343ae34cbSIngo Molnar 1184*925d1c40SMatt Helsley /* 1185*925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1186*925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1187*925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1188*925d1c40SMatt Helsley */ 1189*925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1190*925d1c40SMatt Helsley { 1191*925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1192*925d1c40SMatt Helsley } 1193*925d1c40SMatt Helsley 1194*925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1195*925d1c40SMatt Helsley { 1196*925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1197*925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1198*925d1c40SMatt Helsley fput(mm->exe_file); 1199*925d1c40SMatt Helsley mm->exe_file = NULL; 1200*925d1c40SMatt Helsley } 1201*925d1c40SMatt Helsley 1202*925d1c40SMatt Helsley } 1203*925d1c40SMatt Helsley 1204*925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1205*925d1c40SMatt Helsley { 1206*925d1c40SMatt Helsley if (new_exe_file) 1207*925d1c40SMatt Helsley get_file(new_exe_file); 1208*925d1c40SMatt Helsley if (mm->exe_file) 1209*925d1c40SMatt Helsley fput(mm->exe_file); 1210*925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1211*925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1212*925d1c40SMatt Helsley } 1213*925d1c40SMatt Helsley 1214*925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1215*925d1c40SMatt Helsley { 1216*925d1c40SMatt Helsley struct file *exe_file; 1217*925d1c40SMatt Helsley 1218*925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1219*925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1220*925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1221*925d1c40SMatt Helsley exe_file = mm->exe_file; 1222*925d1c40SMatt Helsley if (exe_file) 1223*925d1c40SMatt Helsley get_file(exe_file); 1224*925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1225*925d1c40SMatt Helsley return exe_file; 1226*925d1c40SMatt Helsley } 1227*925d1c40SMatt Helsley 1228*925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1229*925d1c40SMatt Helsley { 1230*925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1231*925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1232*925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1233*925d1c40SMatt Helsley } 1234*925d1c40SMatt Helsley 1235*925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1236*925d1c40SMatt Helsley { 1237*925d1c40SMatt Helsley struct task_struct *task; 1238*925d1c40SMatt Helsley struct mm_struct *mm; 1239*925d1c40SMatt Helsley struct file *exe_file; 1240*925d1c40SMatt Helsley 1241*925d1c40SMatt Helsley task = get_proc_task(inode); 1242*925d1c40SMatt Helsley if (!task) 1243*925d1c40SMatt Helsley return -ENOENT; 1244*925d1c40SMatt Helsley mm = get_task_mm(task); 1245*925d1c40SMatt Helsley put_task_struct(task); 1246*925d1c40SMatt Helsley if (!mm) 1247*925d1c40SMatt Helsley return -ENOENT; 1248*925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1249*925d1c40SMatt Helsley mmput(mm); 1250*925d1c40SMatt Helsley if (exe_file) { 1251*925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1252*925d1c40SMatt Helsley path_get(&exe_file->f_path); 1253*925d1c40SMatt Helsley fput(exe_file); 1254*925d1c40SMatt Helsley return 0; 1255*925d1c40SMatt Helsley } else 1256*925d1c40SMatt Helsley return -ENOENT; 1257*925d1c40SMatt Helsley } 1258*925d1c40SMatt Helsley 1259008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 12601da177e4SLinus Torvalds { 12611da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 12621da177e4SLinus Torvalds int error = -EACCES; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 12651d957f9bSJan Blunck path_put(&nd->path); 12661da177e4SLinus Torvalds 1267778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1268778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 12691da177e4SLinus Torvalds goto out; 12701da177e4SLinus Torvalds 12713dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 12721da177e4SLinus Torvalds nd->last_type = LAST_BIND; 12731da177e4SLinus Torvalds out: 1274008b150aSAl Viro return ERR_PTR(error); 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12773dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 12781da177e4SLinus Torvalds { 1279e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 12803dcd25f3SJan Blunck char *pathname; 12811da177e4SLinus Torvalds int len; 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds if (!tmp) 12841da177e4SLinus Torvalds return -ENOMEM; 12851da177e4SLinus Torvalds 1286cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 12873dcd25f3SJan Blunck len = PTR_ERR(pathname); 12883dcd25f3SJan Blunck if (IS_ERR(pathname)) 12891da177e4SLinus Torvalds goto out; 12903dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds if (len > buflen) 12931da177e4SLinus Torvalds len = buflen; 12943dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 12951da177e4SLinus Torvalds len = -EFAULT; 12961da177e4SLinus Torvalds out: 12971da177e4SLinus Torvalds free_page((unsigned long)tmp); 12981da177e4SLinus Torvalds return len; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13021da177e4SLinus Torvalds { 13031da177e4SLinus Torvalds int error = -EACCES; 13041da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13053dcd25f3SJan Blunck struct path path; 13061da177e4SLinus Torvalds 1307778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1308778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13091da177e4SLinus Torvalds goto out; 13101da177e4SLinus Torvalds 13113dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13121da177e4SLinus Torvalds if (error) 13131da177e4SLinus Torvalds goto out; 13141da177e4SLinus Torvalds 13153dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13163dcd25f3SJan Blunck path_put(&path); 13171da177e4SLinus Torvalds out: 13181da177e4SLinus Torvalds return error; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 1321c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13221da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13236d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13246d76fa58SLinus Torvalds .setattr = proc_setattr, 13251da177e4SLinus Torvalds }; 13261da177e4SLinus Torvalds 132728a6d671SEric W. Biederman 132828a6d671SEric W. Biederman /* building an inode */ 132928a6d671SEric W. Biederman 133028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 133128a6d671SEric W. Biederman { 133228a6d671SEric W. Biederman int dumpable = 0; 133328a6d671SEric W. Biederman struct mm_struct *mm; 133428a6d671SEric W. Biederman 133528a6d671SEric W. Biederman task_lock(task); 133628a6d671SEric W. Biederman mm = task->mm; 133728a6d671SEric W. Biederman if (mm) 13386c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 133928a6d671SEric W. Biederman task_unlock(task); 134028a6d671SEric W. Biederman if(dumpable == 1) 134128a6d671SEric W. Biederman return 1; 134228a6d671SEric W. Biederman return 0; 134328a6d671SEric W. Biederman } 134428a6d671SEric W. Biederman 134528a6d671SEric W. Biederman 134661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 134728a6d671SEric W. Biederman { 134828a6d671SEric W. Biederman struct inode * inode; 134928a6d671SEric W. Biederman struct proc_inode *ei; 135028a6d671SEric W. Biederman 135128a6d671SEric W. Biederman /* We need a new inode */ 135228a6d671SEric W. Biederman 135328a6d671SEric W. Biederman inode = new_inode(sb); 135428a6d671SEric W. Biederman if (!inode) 135528a6d671SEric W. Biederman goto out; 135628a6d671SEric W. Biederman 135728a6d671SEric W. Biederman /* Common stuff */ 135828a6d671SEric W. Biederman ei = PROC_I(inode); 135928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 136028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 136128a6d671SEric W. Biederman 136228a6d671SEric W. Biederman /* 136328a6d671SEric W. Biederman * grab the reference to task. 136428a6d671SEric W. Biederman */ 13651a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 136628a6d671SEric W. Biederman if (!ei->pid) 136728a6d671SEric W. Biederman goto out_unlock; 136828a6d671SEric W. Biederman 136928a6d671SEric W. Biederman inode->i_uid = 0; 137028a6d671SEric W. Biederman inode->i_gid = 0; 137128a6d671SEric W. Biederman if (task_dumpable(task)) { 137228a6d671SEric W. Biederman inode->i_uid = task->euid; 137328a6d671SEric W. Biederman inode->i_gid = task->egid; 137428a6d671SEric W. Biederman } 137528a6d671SEric W. Biederman security_task_to_inode(task, inode); 137628a6d671SEric W. Biederman 137728a6d671SEric W. Biederman out: 137828a6d671SEric W. Biederman return inode; 137928a6d671SEric W. Biederman 138028a6d671SEric W. Biederman out_unlock: 138128a6d671SEric W. Biederman iput(inode); 138228a6d671SEric W. Biederman return NULL; 138328a6d671SEric W. Biederman } 138428a6d671SEric W. Biederman 138528a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 138628a6d671SEric W. Biederman { 138728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 138828a6d671SEric W. Biederman struct task_struct *task; 138928a6d671SEric W. Biederman generic_fillattr(inode, stat); 139028a6d671SEric W. Biederman 139128a6d671SEric W. Biederman rcu_read_lock(); 139228a6d671SEric W. Biederman stat->uid = 0; 139328a6d671SEric W. Biederman stat->gid = 0; 139428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 139528a6d671SEric W. Biederman if (task) { 139628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 139728a6d671SEric W. Biederman task_dumpable(task)) { 139828a6d671SEric W. Biederman stat->uid = task->euid; 139928a6d671SEric W. Biederman stat->gid = task->egid; 140028a6d671SEric W. Biederman } 140128a6d671SEric W. Biederman } 140228a6d671SEric W. Biederman rcu_read_unlock(); 140328a6d671SEric W. Biederman return 0; 140428a6d671SEric W. Biederman } 140528a6d671SEric W. Biederman 140628a6d671SEric W. Biederman /* dentry stuff */ 140728a6d671SEric W. Biederman 140828a6d671SEric W. Biederman /* 140928a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 141028a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 141128a6d671SEric W. Biederman * due to the way we treat inodes. 141228a6d671SEric W. Biederman * 141328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 141428a6d671SEric W. Biederman * performed a setuid(), etc. 141528a6d671SEric W. Biederman * 141628a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 141728a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 141828a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 141928a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 142028a6d671SEric W. Biederman * made this apply to all per process world readable and executable 142128a6d671SEric W. Biederman * directories. 142228a6d671SEric W. Biederman */ 142328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 142428a6d671SEric W. Biederman { 142528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 142628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 142728a6d671SEric W. Biederman if (task) { 142828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 142928a6d671SEric W. Biederman task_dumpable(task)) { 143028a6d671SEric W. Biederman inode->i_uid = task->euid; 143128a6d671SEric W. Biederman inode->i_gid = task->egid; 143228a6d671SEric W. Biederman } else { 143328a6d671SEric W. Biederman inode->i_uid = 0; 143428a6d671SEric W. Biederman inode->i_gid = 0; 143528a6d671SEric W. Biederman } 143628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 143728a6d671SEric W. Biederman security_task_to_inode(task, inode); 143828a6d671SEric W. Biederman put_task_struct(task); 143928a6d671SEric W. Biederman return 1; 144028a6d671SEric W. Biederman } 144128a6d671SEric W. Biederman d_drop(dentry); 144228a6d671SEric W. Biederman return 0; 144328a6d671SEric W. Biederman } 144428a6d671SEric W. Biederman 144528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 144628a6d671SEric W. Biederman { 144728a6d671SEric W. Biederman /* Is the task we represent dead? 144828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 144928a6d671SEric W. Biederman * kill it immediately. 145028a6d671SEric W. Biederman */ 145128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 145228a6d671SEric W. Biederman } 145328a6d671SEric W. Biederman 145428a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 145528a6d671SEric W. Biederman { 145628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 145728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 145828a6d671SEric W. Biederman }; 145928a6d671SEric W. Biederman 146028a6d671SEric W. Biederman /* Lookups */ 146128a6d671SEric W. Biederman 1462c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1463c5141e6dSEric Dumazet struct task_struct *, const void *); 146461a28784SEric W. Biederman 14651c0d04c9SEric W. Biederman /* 14661c0d04c9SEric W. Biederman * Fill a directory entry. 14671c0d04c9SEric W. Biederman * 14681c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 14691c0d04c9SEric W. Biederman * file type from dcache entry. 14701c0d04c9SEric W. Biederman * 14711c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 14721c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 14731c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 14741c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 14751c0d04c9SEric W. Biederman * by stat. 14761c0d04c9SEric W. Biederman */ 147761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 147861a28784SEric W. Biederman char *name, int len, 1479c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 148061a28784SEric W. Biederman { 14812fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 148261a28784SEric W. Biederman struct inode *inode; 148361a28784SEric W. Biederman struct qstr qname; 148461a28784SEric W. Biederman ino_t ino = 0; 148561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 148661a28784SEric W. Biederman 148761a28784SEric W. Biederman qname.name = name; 148861a28784SEric W. Biederman qname.len = len; 148961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 149061a28784SEric W. Biederman 149161a28784SEric W. Biederman child = d_lookup(dir, &qname); 149261a28784SEric W. Biederman if (!child) { 149361a28784SEric W. Biederman struct dentry *new; 149461a28784SEric W. Biederman new = d_alloc(dir, &qname); 149561a28784SEric W. Biederman if (new) { 149661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 149761a28784SEric W. Biederman if (child) 149861a28784SEric W. Biederman dput(new); 149961a28784SEric W. Biederman else 150061a28784SEric W. Biederman child = new; 150161a28784SEric W. Biederman } 150261a28784SEric W. Biederman } 150361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 150461a28784SEric W. Biederman goto end_instantiate; 150561a28784SEric W. Biederman inode = child->d_inode; 150661a28784SEric W. Biederman if (inode) { 150761a28784SEric W. Biederman ino = inode->i_ino; 150861a28784SEric W. Biederman type = inode->i_mode >> 12; 150961a28784SEric W. Biederman } 151061a28784SEric W. Biederman dput(child); 151161a28784SEric W. Biederman end_instantiate: 151261a28784SEric W. Biederman if (!ino) 151361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 151461a28784SEric W. Biederman if (!ino) 151561a28784SEric W. Biederman ino = 1; 151661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 151761a28784SEric W. Biederman } 151861a28784SEric W. Biederman 151928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 152028a6d671SEric W. Biederman { 152128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 152228a6d671SEric W. Biederman int len = dentry->d_name.len; 152328a6d671SEric W. Biederman unsigned n = 0; 152428a6d671SEric W. Biederman 152528a6d671SEric W. Biederman if (len > 1 && *name == '0') 152628a6d671SEric W. Biederman goto out; 152728a6d671SEric W. Biederman while (len-- > 0) { 152828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 152928a6d671SEric W. Biederman if (c > 9) 153028a6d671SEric W. Biederman goto out; 153128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 153228a6d671SEric W. Biederman goto out; 153328a6d671SEric W. Biederman n *= 10; 153428a6d671SEric W. Biederman n += c; 153528a6d671SEric W. Biederman } 153628a6d671SEric W. Biederman return n; 153728a6d671SEric W. Biederman out: 153828a6d671SEric W. Biederman return ~0U; 153928a6d671SEric W. Biederman } 154028a6d671SEric W. Biederman 154127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 154227932742SMiklos Szeredi 15433dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 154428a6d671SEric W. Biederman { 154528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 154628a6d671SEric W. Biederman struct files_struct *files = NULL; 154728a6d671SEric W. Biederman struct file *file; 154828a6d671SEric W. Biederman int fd = proc_fd(inode); 154928a6d671SEric W. Biederman 155028a6d671SEric W. Biederman if (task) { 155128a6d671SEric W. Biederman files = get_files_struct(task); 155228a6d671SEric W. Biederman put_task_struct(task); 155328a6d671SEric W. Biederman } 155428a6d671SEric W. Biederman if (files) { 155528a6d671SEric W. Biederman /* 155628a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 155728a6d671SEric W. Biederman * hold ->file_lock. 155828a6d671SEric W. Biederman */ 155928a6d671SEric W. Biederman spin_lock(&files->file_lock); 156028a6d671SEric W. Biederman file = fcheck_files(files, fd); 156128a6d671SEric W. Biederman if (file) { 15623dcd25f3SJan Blunck if (path) { 15633dcd25f3SJan Blunck *path = file->f_path; 15643dcd25f3SJan Blunck path_get(&file->f_path); 15653dcd25f3SJan Blunck } 156627932742SMiklos Szeredi if (info) 156727932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 156827932742SMiklos Szeredi "pos:\t%lli\n" 156927932742SMiklos Szeredi "flags:\t0%o\n", 157027932742SMiklos Szeredi (long long) file->f_pos, 157127932742SMiklos Szeredi file->f_flags); 157228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 157328a6d671SEric W. Biederman put_files_struct(files); 157428a6d671SEric W. Biederman return 0; 157528a6d671SEric W. Biederman } 157628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 157728a6d671SEric W. Biederman put_files_struct(files); 157828a6d671SEric W. Biederman } 157928a6d671SEric W. Biederman return -ENOENT; 158028a6d671SEric W. Biederman } 158128a6d671SEric W. Biederman 15823dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 158327932742SMiklos Szeredi { 15843dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 158527932742SMiklos Szeredi } 158627932742SMiklos Szeredi 158728a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 158828a6d671SEric W. Biederman { 158928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 159028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 159128a6d671SEric W. Biederman int fd = proc_fd(inode); 159228a6d671SEric W. Biederman struct files_struct *files; 159328a6d671SEric W. Biederman 159428a6d671SEric W. Biederman if (task) { 159528a6d671SEric W. Biederman files = get_files_struct(task); 159628a6d671SEric W. Biederman if (files) { 159728a6d671SEric W. Biederman rcu_read_lock(); 159828a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 159928a6d671SEric W. Biederman rcu_read_unlock(); 160028a6d671SEric W. Biederman put_files_struct(files); 160128a6d671SEric W. Biederman if (task_dumpable(task)) { 160228a6d671SEric W. Biederman inode->i_uid = task->euid; 160328a6d671SEric W. Biederman inode->i_gid = task->egid; 160428a6d671SEric W. Biederman } else { 160528a6d671SEric W. Biederman inode->i_uid = 0; 160628a6d671SEric W. Biederman inode->i_gid = 0; 160728a6d671SEric W. Biederman } 160828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 160928a6d671SEric W. Biederman security_task_to_inode(task, inode); 161028a6d671SEric W. Biederman put_task_struct(task); 161128a6d671SEric W. Biederman return 1; 161228a6d671SEric W. Biederman } 161328a6d671SEric W. Biederman rcu_read_unlock(); 161428a6d671SEric W. Biederman put_files_struct(files); 161528a6d671SEric W. Biederman } 161628a6d671SEric W. Biederman put_task_struct(task); 161728a6d671SEric W. Biederman } 161828a6d671SEric W. Biederman d_drop(dentry); 161928a6d671SEric W. Biederman return 0; 162028a6d671SEric W. Biederman } 162128a6d671SEric W. Biederman 162228a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 162328a6d671SEric W. Biederman { 162428a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 162528a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 162628a6d671SEric W. Biederman }; 162728a6d671SEric W. Biederman 1628444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1629c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 163028a6d671SEric W. Biederman { 1631c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 163228a6d671SEric W. Biederman struct file *file; 163328a6d671SEric W. Biederman struct files_struct *files; 163428a6d671SEric W. Biederman struct inode *inode; 163528a6d671SEric W. Biederman struct proc_inode *ei; 1636444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 163728a6d671SEric W. Biederman 163861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 163928a6d671SEric W. Biederman if (!inode) 164028a6d671SEric W. Biederman goto out; 164128a6d671SEric W. Biederman ei = PROC_I(inode); 164228a6d671SEric W. Biederman ei->fd = fd; 164328a6d671SEric W. Biederman files = get_files_struct(task); 164428a6d671SEric W. Biederman if (!files) 1645444ceed8SEric W. Biederman goto out_iput; 164628a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 164728a6d671SEric W. Biederman 164828a6d671SEric W. Biederman /* 164928a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 165028a6d671SEric W. Biederman * hold ->file_lock. 165128a6d671SEric W. Biederman */ 165228a6d671SEric W. Biederman spin_lock(&files->file_lock); 165328a6d671SEric W. Biederman file = fcheck_files(files, fd); 165428a6d671SEric W. Biederman if (!file) 1655444ceed8SEric W. Biederman goto out_unlock; 165628a6d671SEric W. Biederman if (file->f_mode & 1) 165728a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 165828a6d671SEric W. Biederman if (file->f_mode & 2) 165928a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 166028a6d671SEric W. Biederman spin_unlock(&files->file_lock); 166128a6d671SEric W. Biederman put_files_struct(files); 1662444ceed8SEric W. Biederman 166328a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 166428a6d671SEric W. Biederman inode->i_size = 64; 166528a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 166628a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 166728a6d671SEric W. Biederman d_add(dentry, inode); 166828a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 166928a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1670444ceed8SEric W. Biederman error = NULL; 1671444ceed8SEric W. Biederman 1672444ceed8SEric W. Biederman out: 1673444ceed8SEric W. Biederman return error; 1674444ceed8SEric W. Biederman out_unlock: 1675444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1676444ceed8SEric W. Biederman put_files_struct(files); 1677444ceed8SEric W. Biederman out_iput: 1678444ceed8SEric W. Biederman iput(inode); 1679444ceed8SEric W. Biederman goto out; 1680444ceed8SEric W. Biederman } 1681444ceed8SEric W. Biederman 168227932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 168327932742SMiklos Szeredi struct dentry *dentry, 168427932742SMiklos Szeredi instantiate_t instantiate) 1685444ceed8SEric W. Biederman { 1686444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1687444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1688444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1689444ceed8SEric W. Biederman 1690444ceed8SEric W. Biederman if (!task) 1691444ceed8SEric W. Biederman goto out_no_task; 1692444ceed8SEric W. Biederman if (fd == ~0U) 1693444ceed8SEric W. Biederman goto out; 1694444ceed8SEric W. Biederman 169527932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 169628a6d671SEric W. Biederman out: 169728a6d671SEric W. Biederman put_task_struct(task); 169828a6d671SEric W. Biederman out_no_task: 169928a6d671SEric W. Biederman return result; 170028a6d671SEric W. Biederman } 170128a6d671SEric W. Biederman 170227932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 170327932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17041da177e4SLinus Torvalds { 17052fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17065634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 170799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1708457c2510SPavel Emelyanov unsigned int fd, ino; 17091da177e4SLinus Torvalds int retval; 17101da177e4SLinus Torvalds struct files_struct * files; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds retval = -ENOENT; 171399f89551SEric W. Biederman if (!p) 171499f89551SEric W. Biederman goto out_no_task; 17151da177e4SLinus Torvalds retval = 0; 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds fd = filp->f_pos; 17181da177e4SLinus Torvalds switch (fd) { 17191da177e4SLinus Torvalds case 0: 17201da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17211da177e4SLinus Torvalds goto out; 17221da177e4SLinus Torvalds filp->f_pos++; 17231da177e4SLinus Torvalds case 1: 17245634708bSEric W. Biederman ino = parent_ino(dentry); 17251da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 17261da177e4SLinus Torvalds goto out; 17271da177e4SLinus Torvalds filp->f_pos++; 17281da177e4SLinus Torvalds default: 17291da177e4SLinus Torvalds files = get_files_struct(p); 17301da177e4SLinus Torvalds if (!files) 17311da177e4SLinus Torvalds goto out; 1732b835996fSDipankar Sarma rcu_read_lock(); 17331da177e4SLinus Torvalds for (fd = filp->f_pos-2; 17349b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 17351da177e4SLinus Torvalds fd++, filp->f_pos++) { 173627932742SMiklos Szeredi char name[PROC_NUMBUF]; 173727932742SMiklos Szeredi int len; 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 17401da177e4SLinus Torvalds continue; 1741b835996fSDipankar Sarma rcu_read_unlock(); 17421da177e4SLinus Torvalds 174327932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 174427932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 174527932742SMiklos Szeredi name, len, instantiate, 174627932742SMiklos Szeredi p, &fd) < 0) { 1747b835996fSDipankar Sarma rcu_read_lock(); 17481da177e4SLinus Torvalds break; 17491da177e4SLinus Torvalds } 1750b835996fSDipankar Sarma rcu_read_lock(); 17511da177e4SLinus Torvalds } 1752b835996fSDipankar Sarma rcu_read_unlock(); 17531da177e4SLinus Torvalds put_files_struct(files); 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds out: 175699f89551SEric W. Biederman put_task_struct(p); 175799f89551SEric W. Biederman out_no_task: 17581da177e4SLinus Torvalds return retval; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 176127932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 176227932742SMiklos Szeredi struct nameidata *nd) 176327932742SMiklos Szeredi { 176427932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 176527932742SMiklos Szeredi } 176627932742SMiklos Szeredi 176727932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 176827932742SMiklos Szeredi { 176927932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 177027932742SMiklos Szeredi } 177127932742SMiklos Szeredi 177227932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 177327932742SMiklos Szeredi size_t len, loff_t *ppos) 177427932742SMiklos Szeredi { 177527932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 17763dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 177727932742SMiklos Szeredi if (!err) 177827932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 177927932742SMiklos Szeredi return err; 178027932742SMiklos Szeredi } 178127932742SMiklos Szeredi 178227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 178327932742SMiklos Szeredi .open = nonseekable_open, 178427932742SMiklos Szeredi .read = proc_fdinfo_read, 178527932742SMiklos Szeredi }; 178627932742SMiklos Szeredi 178700977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 17881da177e4SLinus Torvalds .read = generic_read_dir, 17891da177e4SLinus Torvalds .readdir = proc_readfd, 17901da177e4SLinus Torvalds }; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds /* 17938948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 17948948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 17958948e11fSAlexey Dobriyan */ 17968948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 17978948e11fSAlexey Dobriyan struct nameidata *nd) 17988948e11fSAlexey Dobriyan { 17998948e11fSAlexey Dobriyan int rv; 18008948e11fSAlexey Dobriyan 18018948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18028948e11fSAlexey Dobriyan if (rv == 0) 18038948e11fSAlexey Dobriyan return 0; 18048948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18058948e11fSAlexey Dobriyan rv = 0; 18068948e11fSAlexey Dobriyan return rv; 18078948e11fSAlexey Dobriyan } 18088948e11fSAlexey Dobriyan 18098948e11fSAlexey Dobriyan /* 18101da177e4SLinus Torvalds * proc directories can do almost nothing.. 18111da177e4SLinus Torvalds */ 1812c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18131da177e4SLinus Torvalds .lookup = proc_lookupfd, 18148948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18156d76fa58SLinus Torvalds .setattr = proc_setattr, 18161da177e4SLinus Torvalds }; 18171da177e4SLinus Torvalds 181827932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 181927932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 182027932742SMiklos Szeredi { 182127932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 182227932742SMiklos Szeredi struct inode *inode; 182327932742SMiklos Szeredi struct proc_inode *ei; 182427932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 182527932742SMiklos Szeredi 182627932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 182727932742SMiklos Szeredi if (!inode) 182827932742SMiklos Szeredi goto out; 182927932742SMiklos Szeredi ei = PROC_I(inode); 183027932742SMiklos Szeredi ei->fd = fd; 183127932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 183227932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 183327932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 183427932742SMiklos Szeredi d_add(dentry, inode); 183527932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 183627932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 183727932742SMiklos Szeredi error = NULL; 183827932742SMiklos Szeredi 183927932742SMiklos Szeredi out: 184027932742SMiklos Szeredi return error; 184127932742SMiklos Szeredi } 184227932742SMiklos Szeredi 184327932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 184427932742SMiklos Szeredi struct dentry *dentry, 184527932742SMiklos Szeredi struct nameidata *nd) 184627932742SMiklos Szeredi { 184727932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 184827932742SMiklos Szeredi } 184927932742SMiklos Szeredi 185027932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 185127932742SMiklos Szeredi { 185227932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 185327932742SMiklos Szeredi proc_fdinfo_instantiate); 185427932742SMiklos Szeredi } 185527932742SMiklos Szeredi 185627932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 185727932742SMiklos Szeredi .read = generic_read_dir, 185827932742SMiklos Szeredi .readdir = proc_readfdinfo, 185927932742SMiklos Szeredi }; 186027932742SMiklos Szeredi 186127932742SMiklos Szeredi /* 186227932742SMiklos Szeredi * proc directories can do almost nothing.. 186327932742SMiklos Szeredi */ 186427932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 186527932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 186627932742SMiklos Szeredi .setattr = proc_setattr, 186727932742SMiklos Szeredi }; 186827932742SMiklos Szeredi 186927932742SMiklos Szeredi 1870444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1871c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1872444ceed8SEric W. Biederman { 1873c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1874444ceed8SEric W. Biederman struct inode *inode; 1875444ceed8SEric W. Biederman struct proc_inode *ei; 1876444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1877444ceed8SEric W. Biederman 187861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1879444ceed8SEric W. Biederman if (!inode) 1880444ceed8SEric W. Biederman goto out; 1881444ceed8SEric W. Biederman 1882444ceed8SEric W. Biederman ei = PROC_I(inode); 1883444ceed8SEric W. Biederman inode->i_mode = p->mode; 1884444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1885444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1886444ceed8SEric W. Biederman if (p->iop) 1887444ceed8SEric W. Biederman inode->i_op = p->iop; 1888444ceed8SEric W. Biederman if (p->fop) 1889444ceed8SEric W. Biederman inode->i_fop = p->fop; 1890444ceed8SEric W. Biederman ei->op = p->op; 1891444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1892444ceed8SEric W. Biederman d_add(dentry, inode); 1893444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1894444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1895444ceed8SEric W. Biederman error = NULL; 1896444ceed8SEric W. Biederman out: 1897444ceed8SEric W. Biederman return error; 1898444ceed8SEric W. Biederman } 1899444ceed8SEric W. Biederman 19001da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19011da177e4SLinus Torvalds struct dentry *dentry, 1902c5141e6dSEric Dumazet const struct pid_entry *ents, 19037bcd6b0eSEric W. Biederman unsigned int nents) 19041da177e4SLinus Torvalds { 19051da177e4SLinus Torvalds struct inode *inode; 1906cd6a3ce9SEric W. Biederman struct dentry *error; 190799f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1908c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19091da177e4SLinus Torvalds 1910cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19111da177e4SLinus Torvalds inode = NULL; 19121da177e4SLinus Torvalds 191399f89551SEric W. Biederman if (!task) 191499f89551SEric W. Biederman goto out_no_task; 19151da177e4SLinus Torvalds 191620cdc894SEric W. Biederman /* 191720cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 191820cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 191920cdc894SEric W. Biederman */ 19207bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19217bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19221da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19231da177e4SLinus Torvalds continue; 19241da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19251da177e4SLinus Torvalds break; 19261da177e4SLinus Torvalds } 19277bcd6b0eSEric W. Biederman if (p > last) 19281da177e4SLinus Torvalds goto out; 19291da177e4SLinus Torvalds 1930444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 19311da177e4SLinus Torvalds out: 193299f89551SEric W. Biederman put_task_struct(task); 193399f89551SEric W. Biederman out_no_task: 1934cd6a3ce9SEric W. Biederman return error; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 1937c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1938c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 193961a28784SEric W. Biederman { 194061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 194161a28784SEric W. Biederman proc_pident_instantiate, task, p); 194261a28784SEric W. Biederman } 194361a28784SEric W. Biederman 194428a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 194528a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1946c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 194728a6d671SEric W. Biederman { 194828a6d671SEric W. Biederman int i; 19492fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 195028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 195128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1952c5141e6dSEric Dumazet const struct pid_entry *p, *last; 195328a6d671SEric W. Biederman ino_t ino; 195428a6d671SEric W. Biederman int ret; 195528a6d671SEric W. Biederman 195628a6d671SEric W. Biederman ret = -ENOENT; 195728a6d671SEric W. Biederman if (!task) 195861a28784SEric W. Biederman goto out_no_task; 195928a6d671SEric W. Biederman 196028a6d671SEric W. Biederman ret = 0; 196128a6d671SEric W. Biederman i = filp->f_pos; 196228a6d671SEric W. Biederman switch (i) { 196328a6d671SEric W. Biederman case 0: 196428a6d671SEric W. Biederman ino = inode->i_ino; 196528a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 196628a6d671SEric W. Biederman goto out; 196728a6d671SEric W. Biederman i++; 196828a6d671SEric W. Biederman filp->f_pos++; 196928a6d671SEric W. Biederman /* fall through */ 197028a6d671SEric W. Biederman case 1: 197128a6d671SEric W. Biederman ino = parent_ino(dentry); 197228a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 197328a6d671SEric W. Biederman goto out; 197428a6d671SEric W. Biederman i++; 197528a6d671SEric W. Biederman filp->f_pos++; 197628a6d671SEric W. Biederman /* fall through */ 197728a6d671SEric W. Biederman default: 197828a6d671SEric W. Biederman i -= 2; 197928a6d671SEric W. Biederman if (i >= nents) { 198028a6d671SEric W. Biederman ret = 1; 198128a6d671SEric W. Biederman goto out; 198228a6d671SEric W. Biederman } 198328a6d671SEric W. Biederman p = ents + i; 19847bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19857bcd6b0eSEric W. Biederman while (p <= last) { 198661a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 198728a6d671SEric W. Biederman goto out; 198828a6d671SEric W. Biederman filp->f_pos++; 198928a6d671SEric W. Biederman p++; 199028a6d671SEric W. Biederman } 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 199328a6d671SEric W. Biederman ret = 1; 199428a6d671SEric W. Biederman out: 199561a28784SEric W. Biederman put_task_struct(task); 199661a28784SEric W. Biederman out_no_task: 199728a6d671SEric W. Biederman return ret; 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 200128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 200228a6d671SEric W. Biederman size_t count, loff_t *ppos) 200328a6d671SEric W. Biederman { 20042fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 200504ff9708SAl Viro char *p = NULL; 200628a6d671SEric W. Biederman ssize_t length; 200728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 200828a6d671SEric W. Biederman 200928a6d671SEric W. Biederman if (!task) 201004ff9708SAl Viro return -ESRCH; 201128a6d671SEric W. Biederman 201228a6d671SEric W. Biederman length = security_getprocattr(task, 20132fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 201404ff9708SAl Viro &p); 201528a6d671SEric W. Biederman put_task_struct(task); 201604ff9708SAl Viro if (length > 0) 201704ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 201804ff9708SAl Viro kfree(p); 201928a6d671SEric W. Biederman return length; 202028a6d671SEric W. Biederman } 202128a6d671SEric W. Biederman 202228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 202328a6d671SEric W. Biederman size_t count, loff_t *ppos) 202428a6d671SEric W. Biederman { 20252fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 202628a6d671SEric W. Biederman char *page; 202728a6d671SEric W. Biederman ssize_t length; 202828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 202928a6d671SEric W. Biederman 203028a6d671SEric W. Biederman length = -ESRCH; 203128a6d671SEric W. Biederman if (!task) 203228a6d671SEric W. Biederman goto out_no_task; 203328a6d671SEric W. Biederman if (count > PAGE_SIZE) 203428a6d671SEric W. Biederman count = PAGE_SIZE; 203528a6d671SEric W. Biederman 203628a6d671SEric W. Biederman /* No partial writes. */ 203728a6d671SEric W. Biederman length = -EINVAL; 203828a6d671SEric W. Biederman if (*ppos != 0) 203928a6d671SEric W. Biederman goto out; 204028a6d671SEric W. Biederman 204128a6d671SEric W. Biederman length = -ENOMEM; 2042e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 204328a6d671SEric W. Biederman if (!page) 204428a6d671SEric W. Biederman goto out; 204528a6d671SEric W. Biederman 204628a6d671SEric W. Biederman length = -EFAULT; 204728a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 204828a6d671SEric W. Biederman goto out_free; 204928a6d671SEric W. Biederman 205028a6d671SEric W. Biederman length = security_setprocattr(task, 20512fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 205228a6d671SEric W. Biederman (void*)page, count); 205328a6d671SEric W. Biederman out_free: 205428a6d671SEric W. Biederman free_page((unsigned long) page); 205528a6d671SEric W. Biederman out: 205628a6d671SEric W. Biederman put_task_struct(task); 205728a6d671SEric W. Biederman out_no_task: 205828a6d671SEric W. Biederman return length; 205928a6d671SEric W. Biederman } 206028a6d671SEric W. Biederman 206100977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 206228a6d671SEric W. Biederman .read = proc_pid_attr_read, 206328a6d671SEric W. Biederman .write = proc_pid_attr_write, 206428a6d671SEric W. Biederman }; 206528a6d671SEric W. Biederman 2066c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 206761a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 206861a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 206961a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 207061a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 207161a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 207261a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 207328a6d671SEric W. Biederman }; 207428a6d671SEric W. Biederman 207572d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 20761da177e4SLinus Torvalds void * dirent, filldir_t filldir) 20771da177e4SLinus Torvalds { 20781da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 207972d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 208200977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 20831da177e4SLinus Torvalds .read = generic_read_dir, 208472d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 20851da177e4SLinus Torvalds }; 20861da177e4SLinus Torvalds 208772d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 20881da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 20891da177e4SLinus Torvalds { 20907bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 20917bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds 2094c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 209572d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 209699f89551SEric W. Biederman .getattr = pid_getattr, 20976d76fa58SLinus Torvalds .setattr = proc_setattr, 20981da177e4SLinus Torvalds }; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds #endif 21011da177e4SLinus Torvalds 21023cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21033cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21043cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21053cb4a0bbSKawai, Hidehiro { 21063cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21073cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21083cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21093cb4a0bbSKawai, Hidehiro size_t len; 21103cb4a0bbSKawai, Hidehiro int ret; 21113cb4a0bbSKawai, Hidehiro 21123cb4a0bbSKawai, Hidehiro if (!task) 21133cb4a0bbSKawai, Hidehiro return -ESRCH; 21143cb4a0bbSKawai, Hidehiro 21153cb4a0bbSKawai, Hidehiro ret = 0; 21163cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21173cb4a0bbSKawai, Hidehiro if (mm) { 21183cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21193cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21203cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21213cb4a0bbSKawai, Hidehiro mmput(mm); 21223cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21233cb4a0bbSKawai, Hidehiro } 21243cb4a0bbSKawai, Hidehiro 21253cb4a0bbSKawai, Hidehiro put_task_struct(task); 21263cb4a0bbSKawai, Hidehiro 21273cb4a0bbSKawai, Hidehiro return ret; 21283cb4a0bbSKawai, Hidehiro } 21293cb4a0bbSKawai, Hidehiro 21303cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 21313cb4a0bbSKawai, Hidehiro const char __user *buf, 21323cb4a0bbSKawai, Hidehiro size_t count, 21333cb4a0bbSKawai, Hidehiro loff_t *ppos) 21343cb4a0bbSKawai, Hidehiro { 21353cb4a0bbSKawai, Hidehiro struct task_struct *task; 21363cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21373cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 21383cb4a0bbSKawai, Hidehiro unsigned int val; 21393cb4a0bbSKawai, Hidehiro int ret; 21403cb4a0bbSKawai, Hidehiro int i; 21413cb4a0bbSKawai, Hidehiro unsigned long mask; 21423cb4a0bbSKawai, Hidehiro 21433cb4a0bbSKawai, Hidehiro ret = -EFAULT; 21443cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 21453cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 21463cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 21473cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 21483cb4a0bbSKawai, Hidehiro goto out_no_task; 21493cb4a0bbSKawai, Hidehiro 21503cb4a0bbSKawai, Hidehiro ret = -EINVAL; 21513cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 21523cb4a0bbSKawai, Hidehiro if (*end == '\n') 21533cb4a0bbSKawai, Hidehiro end++; 21543cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 21553cb4a0bbSKawai, Hidehiro goto out_no_task; 21563cb4a0bbSKawai, Hidehiro 21573cb4a0bbSKawai, Hidehiro ret = -ESRCH; 21583cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 21593cb4a0bbSKawai, Hidehiro if (!task) 21603cb4a0bbSKawai, Hidehiro goto out_no_task; 21613cb4a0bbSKawai, Hidehiro 21623cb4a0bbSKawai, Hidehiro ret = end - buffer; 21633cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21643cb4a0bbSKawai, Hidehiro if (!mm) 21653cb4a0bbSKawai, Hidehiro goto out_no_mm; 21663cb4a0bbSKawai, Hidehiro 21673cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 21683cb4a0bbSKawai, Hidehiro if (val & mask) 21693cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 21703cb4a0bbSKawai, Hidehiro else 21713cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 21723cb4a0bbSKawai, Hidehiro } 21733cb4a0bbSKawai, Hidehiro 21743cb4a0bbSKawai, Hidehiro mmput(mm); 21753cb4a0bbSKawai, Hidehiro out_no_mm: 21763cb4a0bbSKawai, Hidehiro put_task_struct(task); 21773cb4a0bbSKawai, Hidehiro out_no_task: 21783cb4a0bbSKawai, Hidehiro return ret; 21793cb4a0bbSKawai, Hidehiro } 21803cb4a0bbSKawai, Hidehiro 21813cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 21823cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 21833cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 21843cb4a0bbSKawai, Hidehiro }; 21853cb4a0bbSKawai, Hidehiro #endif 21863cb4a0bbSKawai, Hidehiro 21871da177e4SLinus Torvalds /* 21881da177e4SLinus Torvalds * /proc/self: 21891da177e4SLinus Torvalds */ 21901da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 21911da177e4SLinus Torvalds int buflen) 21921da177e4SLinus Torvalds { 2193488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2194b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 21958578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2196b55fcb22SAndrew Morton if (!tgid) 2197488e5bc4SEric W. Biederman return -ENOENT; 2198b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 21991da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22001da177e4SLinus Torvalds } 22011da177e4SLinus Torvalds 2202008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22031da177e4SLinus Torvalds { 2204488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2205b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22068578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2207b55fcb22SAndrew Morton if (!tgid) 2208488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2209b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2210008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds 2213c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22141da177e4SLinus Torvalds .readlink = proc_self_readlink, 22151da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22161da177e4SLinus Torvalds }; 22171da177e4SLinus Torvalds 221828a6d671SEric W. Biederman /* 2219801199ceSEric W. Biederman * proc base 2220801199ceSEric W. Biederman * 2221801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2222801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2223801199ceSEric W. Biederman * describe something that is process related. 2224801199ceSEric W. Biederman */ 2225c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 222661a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2227801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2228801199ceSEric W. Biederman }; 2229801199ceSEric W. Biederman 2230801199ceSEric W. Biederman /* 2231801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2232801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2233801199ceSEric W. Biederman * due to the way we treat inodes. 2234801199ceSEric W. Biederman */ 2235801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2236801199ceSEric W. Biederman { 2237801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2238801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2239801199ceSEric W. Biederman if (task) { 2240801199ceSEric W. Biederman put_task_struct(task); 2241801199ceSEric W. Biederman return 1; 2242801199ceSEric W. Biederman } 2243801199ceSEric W. Biederman d_drop(dentry); 2244801199ceSEric W. Biederman return 0; 2245801199ceSEric W. Biederman } 2246801199ceSEric W. Biederman 2247801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2248801199ceSEric W. Biederman { 2249801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2250801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2251801199ceSEric W. Biederman }; 2252801199ceSEric W. Biederman 2253444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2254c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2255801199ceSEric W. Biederman { 2256c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2257801199ceSEric W. Biederman struct inode *inode; 2258801199ceSEric W. Biederman struct proc_inode *ei; 2259444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2260801199ceSEric W. Biederman 2261801199ceSEric W. Biederman /* Allocate the inode */ 2262801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2263801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2264801199ceSEric W. Biederman if (!inode) 2265801199ceSEric W. Biederman goto out; 2266801199ceSEric W. Biederman 2267801199ceSEric W. Biederman /* Initialize the inode */ 2268801199ceSEric W. Biederman ei = PROC_I(inode); 2269801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2270801199ceSEric W. Biederman 2271801199ceSEric W. Biederman /* 2272801199ceSEric W. Biederman * grab the reference to the task. 2273801199ceSEric W. Biederman */ 22741a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2275801199ceSEric W. Biederman if (!ei->pid) 2276801199ceSEric W. Biederman goto out_iput; 2277801199ceSEric W. Biederman 2278801199ceSEric W. Biederman inode->i_uid = 0; 2279801199ceSEric W. Biederman inode->i_gid = 0; 2280801199ceSEric W. Biederman inode->i_mode = p->mode; 2281801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2282801199ceSEric W. Biederman inode->i_nlink = 2; 2283801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2284801199ceSEric W. Biederman inode->i_size = 64; 2285801199ceSEric W. Biederman if (p->iop) 2286801199ceSEric W. Biederman inode->i_op = p->iop; 2287801199ceSEric W. Biederman if (p->fop) 2288801199ceSEric W. Biederman inode->i_fop = p->fop; 2289801199ceSEric W. Biederman ei->op = p->op; 2290801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2291801199ceSEric W. Biederman d_add(dentry, inode); 2292801199ceSEric W. Biederman error = NULL; 2293801199ceSEric W. Biederman out: 2294801199ceSEric W. Biederman return error; 2295801199ceSEric W. Biederman out_iput: 2296801199ceSEric W. Biederman iput(inode); 2297801199ceSEric W. Biederman goto out; 2298801199ceSEric W. Biederman } 2299801199ceSEric W. Biederman 2300444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2301444ceed8SEric W. Biederman { 2302444ceed8SEric W. Biederman struct dentry *error; 2303444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2304c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2305444ceed8SEric W. Biederman 2306444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2307444ceed8SEric W. Biederman 2308444ceed8SEric W. Biederman if (!task) 2309444ceed8SEric W. Biederman goto out_no_task; 2310444ceed8SEric W. Biederman 2311444ceed8SEric W. Biederman /* Lookup the directory entry */ 23127bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23137bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2314444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2315444ceed8SEric W. Biederman continue; 2316444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2317444ceed8SEric W. Biederman break; 2318444ceed8SEric W. Biederman } 23197bcd6b0eSEric W. Biederman if (p > last) 2320444ceed8SEric W. Biederman goto out; 2321444ceed8SEric W. Biederman 2322444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2323444ceed8SEric W. Biederman 2324444ceed8SEric W. Biederman out: 2325444ceed8SEric W. Biederman put_task_struct(task); 2326444ceed8SEric W. Biederman out_no_task: 2327444ceed8SEric W. Biederman return error; 2328444ceed8SEric W. Biederman } 2329444ceed8SEric W. Biederman 2330c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2331c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 233261a28784SEric W. Biederman { 233361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 233461a28784SEric W. Biederman proc_base_instantiate, task, p); 233561a28784SEric W. Biederman } 233661a28784SEric W. Biederman 2337aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2338aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2339aba76fdbSAndrew Morton { 2340aba76fdbSAndrew Morton return sprintf(buffer, 23414b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2342aba76fdbSAndrew Morton "rchar: %llu\n" 2343aba76fdbSAndrew Morton "wchar: %llu\n" 2344aba76fdbSAndrew Morton "syscr: %llu\n" 2345aba76fdbSAndrew Morton "syscw: %llu\n" 23464b98d11bSAlexey Dobriyan #endif 2347aba76fdbSAndrew Morton "read_bytes: %llu\n" 2348aba76fdbSAndrew Morton "write_bytes: %llu\n" 2349aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 23504b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2351aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2352aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2353aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2354aba76fdbSAndrew Morton (unsigned long long)task->syscw, 23554b98d11bSAlexey Dobriyan #endif 2356aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2357aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2358aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2359aba76fdbSAndrew Morton } 2360aba76fdbSAndrew Morton #endif 2361aba76fdbSAndrew Morton 2362801199ceSEric W. Biederman /* 236328a6d671SEric W. Biederman * Thread groups 236428a6d671SEric W. Biederman */ 236500977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2366c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 236720cdc894SEric W. Biederman 2368c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 236961a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 237061a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 237127932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2372b2211a36SAndrew Morton #ifdef CONFIG_NET 23734f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2374b2211a36SAndrew Morton #endif 2375315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 237661a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2377df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2378d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 237943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 238043ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 238143ae34cbSIngo Molnar #endif 238261a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2383ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2384a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 238561a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 238628a6d671SEric W. Biederman #ifdef CONFIG_NUMA 238761a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 238828a6d671SEric W. Biederman #endif 238961a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 239061a28784SEric W. Biederman LNK("cwd", cwd), 239161a28784SEric W. Biederman LNK("root", root), 239261a28784SEric W. Biederman LNK("exe", exe), 239361a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 23942d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 239561a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 23961e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2397b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 239861a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 239985863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 240028a6d671SEric W. Biederman #endif 240128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 240272d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 240328a6d671SEric W. Biederman #endif 240428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 240561a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 240628a6d671SEric W. Biederman #endif 240728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 240861a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 240928a6d671SEric W. Biederman #endif 24109745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 24119745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 24129745512cSArjan van de Ven #endif 24138793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 241461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 241528a6d671SEric W. Biederman #endif 2416a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2417a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2418a424316cSPaul Menage #endif 241961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 242061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 242128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 242261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 24231e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 242428a6d671SEric W. Biederman #endif 2425f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2426f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2427f4f154fdSAkinobu Mita #endif 24283cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 24293cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 24303cb4a0bbSKawai, Hidehiro #endif 2431aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2432aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2433aba76fdbSAndrew Morton #endif 243428a6d671SEric W. Biederman }; 243528a6d671SEric W. Biederman 243628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 243728a6d671SEric W. Biederman void * dirent, filldir_t filldir) 243828a6d671SEric W. Biederman { 243928a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 244028a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 244128a6d671SEric W. Biederman } 244228a6d671SEric W. Biederman 244300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 244428a6d671SEric W. Biederman .read = generic_read_dir, 244528a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 244628a6d671SEric W. Biederman }; 244728a6d671SEric W. Biederman 244828a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 24497bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 24507bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 245128a6d671SEric W. Biederman } 245228a6d671SEric W. Biederman 2453c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 245428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 245528a6d671SEric W. Biederman .getattr = pid_getattr, 245628a6d671SEric W. Biederman .setattr = proc_setattr, 245728a6d671SEric W. Biederman }; 245828a6d671SEric W. Biederman 245960347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 24601da177e4SLinus Torvalds { 246148e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 24628578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 246348e6484dSEric W. Biederman struct qstr name; 24641da177e4SLinus Torvalds 246548e6484dSEric W. Biederman name.name = buf; 246660347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 246760347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 246848e6484dSEric W. Biederman if (dentry) { 24697766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 247048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 247148e6484dSEric W. Biederman d_drop(dentry); 247248e6484dSEric W. Biederman dput(dentry); 24731da177e4SLinus Torvalds } 24741da177e4SLinus Torvalds 247560347f67SPavel Emelyanov if (tgid == 0) 247648e6484dSEric W. Biederman goto out; 24771da177e4SLinus Torvalds 247848e6484dSEric W. Biederman name.name = buf; 247960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 248060347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 248148e6484dSEric W. Biederman if (!leader) 248248e6484dSEric W. Biederman goto out; 248348e6484dSEric W. Biederman 248448e6484dSEric W. Biederman name.name = "task"; 248548e6484dSEric W. Biederman name.len = strlen(name.name); 248648e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 248748e6484dSEric W. Biederman if (!dir) 248848e6484dSEric W. Biederman goto out_put_leader; 248948e6484dSEric W. Biederman 249048e6484dSEric W. Biederman name.name = buf; 249160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 249248e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 249348e6484dSEric W. Biederman if (dentry) { 249448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 249548e6484dSEric W. Biederman d_drop(dentry); 249648e6484dSEric W. Biederman dput(dentry); 24971da177e4SLinus Torvalds } 249848e6484dSEric W. Biederman 249948e6484dSEric W. Biederman dput(dir); 250048e6484dSEric W. Biederman out_put_leader: 250148e6484dSEric W. Biederman dput(leader); 250248e6484dSEric W. Biederman out: 250348e6484dSEric W. Biederman return; 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25060895e91dSRandy Dunlap /** 25070895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 25080895e91dSRandy Dunlap * @task: task that should be flushed. 25090895e91dSRandy Dunlap * 25100895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 251160347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 25120895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 25130895e91dSRandy Dunlap * 25140895e91dSRandy Dunlap * Looks in the dcache for 25150895e91dSRandy Dunlap * /proc/@pid 25160895e91dSRandy Dunlap * /proc/@tgid/task/@pid 25170895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 25180895e91dSRandy Dunlap * from the dcache. 25190895e91dSRandy Dunlap * 25200895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 25210895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 25220895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 25230895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 25240895e91dSRandy Dunlap * dcache entries at process exit time. 25250895e91dSRandy Dunlap * 25260895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 25270895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 25280895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 252960347f67SPavel Emelyanov */ 253060347f67SPavel Emelyanov 253160347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 253260347f67SPavel Emelyanov { 25339fcc2d15SEric W. Biederman int i; 25349fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2535130f77ecSPavel Emelyanov struct upid *upid; 2536130f77ecSPavel Emelyanov 2537130f77ecSPavel Emelyanov pid = task_pid(task); 25389fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2539130f77ecSPavel Emelyanov tgid = task_tgid(task); 25409fcc2d15SEric W. Biederman 25419fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2542130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2543130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 25449fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2545130f77ecSPavel Emelyanov } 25466f4e6433SPavel Emelyanov 25476f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 25486f4e6433SPavel Emelyanov if (upid->nr == 1) 25496f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 255060347f67SPavel Emelyanov } 255160347f67SPavel Emelyanov 25529711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 25539711ef99SAdrian Bunk struct dentry * dentry, 2554c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2555444ceed8SEric W. Biederman { 2556444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2557444ceed8SEric W. Biederman struct inode *inode; 2558444ceed8SEric W. Biederman 255961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2560444ceed8SEric W. Biederman if (!inode) 2561444ceed8SEric W. Biederman goto out; 2562444ceed8SEric W. Biederman 2563444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2564444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2565444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2566444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 256727932742SMiklos Szeredi inode->i_nlink = 5; 2568444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2569444ceed8SEric W. Biederman inode->i_nlink += 1; 2570444ceed8SEric W. Biederman #endif 2571444ceed8SEric W. Biederman 2572444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2573444ceed8SEric W. Biederman 2574444ceed8SEric W. Biederman d_add(dentry, inode); 2575444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2576444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2577444ceed8SEric W. Biederman error = NULL; 2578444ceed8SEric W. Biederman out: 2579444ceed8SEric W. Biederman return error; 2580444ceed8SEric W. Biederman } 2581444ceed8SEric W. Biederman 25821da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 25831da177e4SLinus Torvalds { 2584cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 25851da177e4SLinus Torvalds struct task_struct *task; 25861da177e4SLinus Torvalds unsigned tgid; 2587b488893aSPavel Emelyanov struct pid_namespace *ns; 25881da177e4SLinus Torvalds 2589801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2590801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2591801199ceSEric W. Biederman goto out; 2592801199ceSEric W. Biederman 25931da177e4SLinus Torvalds tgid = name_to_int(dentry); 25941da177e4SLinus Torvalds if (tgid == ~0U) 25951da177e4SLinus Torvalds goto out; 25961da177e4SLinus Torvalds 2597b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2598de758734SEric W. Biederman rcu_read_lock(); 2599b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 26001da177e4SLinus Torvalds if (task) 26011da177e4SLinus Torvalds get_task_struct(task); 2602de758734SEric W. Biederman rcu_read_unlock(); 26031da177e4SLinus Torvalds if (!task) 26041da177e4SLinus Torvalds goto out; 26051da177e4SLinus Torvalds 2606444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 260748e6484dSEric W. Biederman put_task_struct(task); 26081da177e4SLinus Torvalds out: 2609cd6a3ce9SEric W. Biederman return result; 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /* 26130804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 26140bc58a91SEric W. Biederman * 26151da177e4SLinus Torvalds */ 261619fd4bb2SEric W. Biederman struct tgid_iter { 261719fd4bb2SEric W. Biederman unsigned int tgid; 26180804ef4bSEric W. Biederman struct task_struct *task; 261919fd4bb2SEric W. Biederman }; 262019fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 262119fd4bb2SEric W. Biederman { 26220804ef4bSEric W. Biederman struct pid *pid; 26231da177e4SLinus Torvalds 262419fd4bb2SEric W. Biederman if (iter.task) 262519fd4bb2SEric W. Biederman put_task_struct(iter.task); 26260804ef4bSEric W. Biederman rcu_read_lock(); 26270804ef4bSEric W. Biederman retry: 262819fd4bb2SEric W. Biederman iter.task = NULL; 262919fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 26300804ef4bSEric W. Biederman if (pid) { 263119fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 263219fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 26330804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 26340804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 26350804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 26360804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 26370804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 26380804ef4bSEric W. Biederman * 26390804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 26400804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 26410804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 26420804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 26430804ef4bSEric W. Biederman * As we don't care in the case of readdir. 26440bc58a91SEric W. Biederman */ 264519fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 264619fd4bb2SEric W. Biederman iter.tgid += 1; 26470804ef4bSEric W. Biederman goto retry; 264819fd4bb2SEric W. Biederman } 264919fd4bb2SEric W. Biederman get_task_struct(iter.task); 26501da177e4SLinus Torvalds } 2651454cc105SEric W. Biederman rcu_read_unlock(); 265219fd4bb2SEric W. Biederman return iter; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26557bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 26561da177e4SLinus Torvalds 265761a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 265819fd4bb2SEric W. Biederman struct tgid_iter iter) 265961a28784SEric W. Biederman { 266061a28784SEric W. Biederman char name[PROC_NUMBUF]; 266119fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 266261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 266319fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 266461a28784SEric W. Biederman } 266561a28784SEric W. Biederman 26661da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 26671da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 26681da177e4SLinus Torvalds { 26691da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 26702fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 267119fd4bb2SEric W. Biederman struct tgid_iter iter; 2672b488893aSPavel Emelyanov struct pid_namespace *ns; 26731da177e4SLinus Torvalds 267461a28784SEric W. Biederman if (!reaper) 267561a28784SEric W. Biederman goto out_no_task; 267661a28784SEric W. Biederman 26777bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2678c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 267961a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2680801199ceSEric W. Biederman goto out; 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 2683b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 268419fd4bb2SEric W. Biederman iter.task = NULL; 268519fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 268619fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 268719fd4bb2SEric W. Biederman iter.task; 268819fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 268919fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 269019fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 269119fd4bb2SEric W. Biederman put_task_struct(iter.task); 26920804ef4bSEric W. Biederman goto out; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds } 26950804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 26960804ef4bSEric W. Biederman out: 269761a28784SEric W. Biederman put_task_struct(reaper); 269861a28784SEric W. Biederman out_no_task: 26991da177e4SLinus Torvalds return 0; 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 27020bc58a91SEric W. Biederman /* 270328a6d671SEric W. Biederman * Tasks 270428a6d671SEric W. Biederman */ 2705c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 270661a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 270727932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2708315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 270961a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2710df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2711d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 271243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 271343ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 271443ae34cbSIngo Molnar #endif 271561a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2716ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2717a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 271861a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 271928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 272061a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 272128a6d671SEric W. Biederman #endif 272261a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 272361a28784SEric W. Biederman LNK("cwd", cwd), 272461a28784SEric W. Biederman LNK("root", root), 272561a28784SEric W. Biederman LNK("exe", exe), 272661a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 27272d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 27281e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2729b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 273061a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 273185863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 273228a6d671SEric W. Biederman #endif 273328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 273472d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 273528a6d671SEric W. Biederman #endif 273628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 273761a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 273828a6d671SEric W. Biederman #endif 273928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 274061a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 274128a6d671SEric W. Biederman #endif 27429745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 27439745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 27449745512cSArjan van de Ven #endif 27458793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 274661a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 274728a6d671SEric W. Biederman #endif 2748a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2749a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2750a424316cSPaul Menage #endif 275161a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 275261a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 275328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 275461a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 27551e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 275628a6d671SEric W. Biederman #endif 2757f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2758f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2759f4f154fdSAkinobu Mita #endif 276028a6d671SEric W. Biederman }; 276128a6d671SEric W. Biederman 276228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 276328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 276428a6d671SEric W. Biederman { 276528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 276628a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 276728a6d671SEric W. Biederman } 276828a6d671SEric W. Biederman 276928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 27707bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 27717bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 277228a6d671SEric W. Biederman } 277328a6d671SEric W. Biederman 277400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 277528a6d671SEric W. Biederman .read = generic_read_dir, 277628a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 277728a6d671SEric W. Biederman }; 277828a6d671SEric W. Biederman 2779c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 278028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 278128a6d671SEric W. Biederman .getattr = pid_getattr, 278228a6d671SEric W. Biederman .setattr = proc_setattr, 278328a6d671SEric W. Biederman }; 278428a6d671SEric W. Biederman 2785444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2786c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2787444ceed8SEric W. Biederman { 2788444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2789444ceed8SEric W. Biederman struct inode *inode; 279061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2791444ceed8SEric W. Biederman 2792444ceed8SEric W. Biederman if (!inode) 2793444ceed8SEric W. Biederman goto out; 2794444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2795444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2796444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2797444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 279827932742SMiklos Szeredi inode->i_nlink = 4; 2799444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2800444ceed8SEric W. Biederman inode->i_nlink += 1; 2801444ceed8SEric W. Biederman #endif 2802444ceed8SEric W. Biederman 2803444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2804444ceed8SEric W. Biederman 2805444ceed8SEric W. Biederman d_add(dentry, inode); 2806444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2807444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2808444ceed8SEric W. Biederman error = NULL; 2809444ceed8SEric W. Biederman out: 2810444ceed8SEric W. Biederman return error; 2811444ceed8SEric W. Biederman } 2812444ceed8SEric W. Biederman 281328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 281428a6d671SEric W. Biederman { 281528a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 281628a6d671SEric W. Biederman struct task_struct *task; 281728a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 281828a6d671SEric W. Biederman unsigned tid; 2819b488893aSPavel Emelyanov struct pid_namespace *ns; 282028a6d671SEric W. Biederman 282128a6d671SEric W. Biederman if (!leader) 282228a6d671SEric W. Biederman goto out_no_task; 282328a6d671SEric W. Biederman 282428a6d671SEric W. Biederman tid = name_to_int(dentry); 282528a6d671SEric W. Biederman if (tid == ~0U) 282628a6d671SEric W. Biederman goto out; 282728a6d671SEric W. Biederman 2828b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 282928a6d671SEric W. Biederman rcu_read_lock(); 2830b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 283128a6d671SEric W. Biederman if (task) 283228a6d671SEric W. Biederman get_task_struct(task); 283328a6d671SEric W. Biederman rcu_read_unlock(); 283428a6d671SEric W. Biederman if (!task) 283528a6d671SEric W. Biederman goto out; 2836bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 283728a6d671SEric W. Biederman goto out_drop_task; 283828a6d671SEric W. Biederman 2839444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 284028a6d671SEric W. Biederman out_drop_task: 284128a6d671SEric W. Biederman put_task_struct(task); 284228a6d671SEric W. Biederman out: 284328a6d671SEric W. Biederman put_task_struct(leader); 284428a6d671SEric W. Biederman out_no_task: 284528a6d671SEric W. Biederman return result; 284628a6d671SEric W. Biederman } 284728a6d671SEric W. Biederman 284828a6d671SEric W. Biederman /* 28490bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 28500bc58a91SEric W. Biederman * 28510bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 28520bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 28530bc58a91SEric W. Biederman * directory we have more work todo. 28540bc58a91SEric W. Biederman * 28550bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 28560bc58a91SEric W. Biederman * 28570bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 28580bc58a91SEric W. Biederman * threads past it. 28590bc58a91SEric W. Biederman */ 2860cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2861b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 28620bc58a91SEric W. Biederman { 2863a872ff0cSOleg Nesterov struct task_struct *pos; 28640bc58a91SEric W. Biederman 2865cc288738SEric W. Biederman rcu_read_lock(); 28660bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 28670bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2868b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2869a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2870a872ff0cSOleg Nesterov goto found; 28710bc58a91SEric W. Biederman } 28720bc58a91SEric W. Biederman 28730bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 28740bc58a91SEric W. Biederman pos = NULL; 2875a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2876a872ff0cSOleg Nesterov goto out; 2877a872ff0cSOleg Nesterov 2878a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2879a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2880a872ff0cSOleg Nesterov */ 2881a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2882a872ff0cSOleg Nesterov pos = next_thread(pos); 2883a872ff0cSOleg Nesterov if (pos == leader) { 2884a872ff0cSOleg Nesterov pos = NULL; 2885a872ff0cSOleg Nesterov goto out; 2886a872ff0cSOleg Nesterov } 2887a872ff0cSOleg Nesterov } 2888a872ff0cSOleg Nesterov found: 2889a872ff0cSOleg Nesterov get_task_struct(pos); 2890a872ff0cSOleg Nesterov out: 2891cc288738SEric W. Biederman rcu_read_unlock(); 28920bc58a91SEric W. Biederman return pos; 28930bc58a91SEric W. Biederman } 28940bc58a91SEric W. Biederman 28950bc58a91SEric W. Biederman /* 28960bc58a91SEric W. Biederman * Find the next thread in the thread list. 28970bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 28980bc58a91SEric W. Biederman * 28990bc58a91SEric W. Biederman * The reference to the input task_struct is released. 29000bc58a91SEric W. Biederman */ 29010bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 29020bc58a91SEric W. Biederman { 2903c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2904cc288738SEric W. Biederman rcu_read_lock(); 2905c1df7fb8SOleg Nesterov if (pid_alive(start)) { 29060bc58a91SEric W. Biederman pos = next_thread(start); 2907c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 29080bc58a91SEric W. Biederman pos = NULL; 2909c1df7fb8SOleg Nesterov else 2910c1df7fb8SOleg Nesterov get_task_struct(pos); 2911c1df7fb8SOleg Nesterov } 2912cc288738SEric W. Biederman rcu_read_unlock(); 29130bc58a91SEric W. Biederman put_task_struct(start); 29140bc58a91SEric W. Biederman return pos; 29150bc58a91SEric W. Biederman } 29160bc58a91SEric W. Biederman 291761a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 291861a28784SEric W. Biederman struct task_struct *task, int tid) 291961a28784SEric W. Biederman { 292061a28784SEric W. Biederman char name[PROC_NUMBUF]; 292161a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 292261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 292361a28784SEric W. Biederman proc_task_instantiate, task, NULL); 292461a28784SEric W. Biederman } 292561a28784SEric W. Biederman 29261da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 29271da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 29281da177e4SLinus Torvalds { 29292fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 29301da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 29317d895244SGuillaume Chazarain struct task_struct *leader = NULL; 29320bc58a91SEric W. Biederman struct task_struct *task; 29331da177e4SLinus Torvalds int retval = -ENOENT; 29341da177e4SLinus Torvalds ino_t ino; 29350bc58a91SEric W. Biederman int tid; 29361da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2937b488893aSPavel Emelyanov struct pid_namespace *ns; 29381da177e4SLinus Torvalds 29397d895244SGuillaume Chazarain task = get_proc_task(inode); 29407d895244SGuillaume Chazarain if (!task) 29417d895244SGuillaume Chazarain goto out_no_task; 29427d895244SGuillaume Chazarain rcu_read_lock(); 29437d895244SGuillaume Chazarain if (pid_alive(task)) { 29447d895244SGuillaume Chazarain leader = task->group_leader; 29457d895244SGuillaume Chazarain get_task_struct(leader); 29467d895244SGuillaume Chazarain } 29477d895244SGuillaume Chazarain rcu_read_unlock(); 29487d895244SGuillaume Chazarain put_task_struct(task); 294999f89551SEric W. Biederman if (!leader) 295099f89551SEric W. Biederman goto out_no_task; 29511da177e4SLinus Torvalds retval = 0; 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds switch (pos) { 29541da177e4SLinus Torvalds case 0: 29551da177e4SLinus Torvalds ino = inode->i_ino; 29561da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 29571da177e4SLinus Torvalds goto out; 29581da177e4SLinus Torvalds pos++; 29591da177e4SLinus Torvalds /* fall through */ 29601da177e4SLinus Torvalds case 1: 29611da177e4SLinus Torvalds ino = parent_ino(dentry); 29621da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 29631da177e4SLinus Torvalds goto out; 29641da177e4SLinus Torvalds pos++; 29651da177e4SLinus Torvalds /* fall through */ 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds 29680bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 29690bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 29700bc58a91SEric W. Biederman */ 2971b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 29722b47c361SMathieu Desnoyers tid = (int)filp->f_version; 29730bc58a91SEric W. Biederman filp->f_version = 0; 2974b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 29750bc58a91SEric W. Biederman task; 29760bc58a91SEric W. Biederman task = next_tid(task), pos++) { 2977b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 297861a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 29790bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 29800bc58a91SEric W. Biederman * pid for the next readir call */ 29812b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 29820bc58a91SEric W. Biederman put_task_struct(task); 29831da177e4SLinus Torvalds break; 29840bc58a91SEric W. Biederman } 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds out: 29871da177e4SLinus Torvalds filp->f_pos = pos; 298899f89551SEric W. Biederman put_task_struct(leader); 298999f89551SEric W. Biederman out_no_task: 29901da177e4SLinus Torvalds return retval; 29911da177e4SLinus Torvalds } 29926e66b52bSEric W. Biederman 29936e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 29946e66b52bSEric W. Biederman { 29956e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 299699f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 29976e66b52bSEric W. Biederman generic_fillattr(inode, stat); 29986e66b52bSEric W. Biederman 299999f89551SEric W. Biederman if (p) { 300099f89551SEric W. Biederman rcu_read_lock(); 300199f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 300299f89551SEric W. Biederman rcu_read_unlock(); 300399f89551SEric W. Biederman put_task_struct(p); 30046e66b52bSEric W. Biederman } 30056e66b52bSEric W. Biederman 30066e66b52bSEric W. Biederman return 0; 30076e66b52bSEric W. Biederman } 300828a6d671SEric W. Biederman 3009c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 301028a6d671SEric W. Biederman .lookup = proc_task_lookup, 301128a6d671SEric W. Biederman .getattr = proc_task_getattr, 301228a6d671SEric W. Biederman .setattr = proc_setattr, 301328a6d671SEric W. Biederman }; 301428a6d671SEric W. Biederman 301500977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 301628a6d671SEric W. Biederman .read = generic_read_dir, 301728a6d671SEric W. Biederman .readdir = proc_task_readdir, 301828a6d671SEric W. Biederman }; 3019