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> 665096add8SKees Cook #include <linux/module.h> 671da177e4SLinus Torvalds #include <linux/mount.h> 681da177e4SLinus Torvalds #include <linux/security.h> 691da177e4SLinus Torvalds #include <linux/ptrace.h> 701da177e4SLinus Torvalds #include <linux/cpuset.h> 711da177e4SLinus Torvalds #include <linux/audit.h> 725addc5ddSAl Viro #include <linux/poll.h> 731651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 748ac773b4SAlexey Dobriyan #include <linux/oom.h> 753cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 761da177e4SLinus Torvalds #include "internal.h" 771da177e4SLinus Torvalds 780f2fe20fSEric W. Biederman /* NOTE: 790f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 800f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 810f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 820f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 830f2fe20fSEric W. Biederman * 840f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 850f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 860f2fe20fSEric W. Biederman */ 870f2fe20fSEric W. Biederman 881da177e4SLinus Torvalds 898578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 900187f879SAndrew Morton #define PROC_NUMBUF 13 918578cea7SEric W. Biederman 921da177e4SLinus Torvalds struct pid_entry { 931da177e4SLinus Torvalds char *name; 94c5141e6dSEric Dumazet int len; 951da177e4SLinus Torvalds mode_t mode; 96c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9700977a59SArjan van de Ven const struct file_operations *fop; 9820cdc894SEric W. Biederman union proc_op op; 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 10161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10220cdc894SEric W. Biederman .name = (NAME), \ 103c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10420cdc894SEric W. Biederman .mode = MODE, \ 10520cdc894SEric W. Biederman .iop = IOP, \ 10620cdc894SEric W. Biederman .fop = FOP, \ 10720cdc894SEric W. Biederman .op = OP, \ 10820cdc894SEric W. Biederman } 10920cdc894SEric W. Biederman 11061a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11161a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11220cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11320cdc894SEric W. Biederman {} ) 11461a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11561a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11620cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11720cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11861a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11961a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12020cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12161a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12420cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 1251da177e4SLinus Torvalds 1265096add8SKees Cook int maps_protect; 1275096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1285096add8SKees Cook 1290494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds struct fs_struct *fs; 1320494f6ecSMiklos Szeredi task_lock(task); 1330494f6ecSMiklos Szeredi fs = task->fs; 1341da177e4SLinus Torvalds if(fs) 1351da177e4SLinus Torvalds atomic_inc(&fs->count); 1360494f6ecSMiklos Szeredi task_unlock(task); 1370494f6ecSMiklos Szeredi return fs; 1380494f6ecSMiklos Szeredi } 1390494f6ecSMiklos Szeredi 14099f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14199f89551SEric W. Biederman { 14299f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14399f89551SEric W. Biederman unsigned long flags; 14499f89551SEric W. Biederman int count = 0; 14599f89551SEric W. Biederman 14699f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 14799f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 14899f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 14999f89551SEric W. Biederman } 15099f89551SEric W. Biederman return count; 15199f89551SEric W. Biederman } 15299f89551SEric W. Biederman 1530494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1540494f6ecSMiklos Szeredi { 15599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15699f89551SEric W. Biederman struct fs_struct *fs = NULL; 1570494f6ecSMiklos Szeredi int result = -ENOENT; 15899f89551SEric W. Biederman 15999f89551SEric W. Biederman if (task) { 16099f89551SEric W. Biederman fs = get_fs_struct(task); 16199f89551SEric W. Biederman put_task_struct(task); 16299f89551SEric W. Biederman } 1631da177e4SLinus Torvalds if (fs) { 1641da177e4SLinus Torvalds read_lock(&fs->lock); 1651da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 1661da177e4SLinus Torvalds *dentry = dget(fs->pwd); 1671da177e4SLinus Torvalds read_unlock(&fs->lock); 1681da177e4SLinus Torvalds result = 0; 1691da177e4SLinus Torvalds put_fs_struct(fs); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds return result; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1751da177e4SLinus Torvalds { 17699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17799f89551SEric W. Biederman struct fs_struct *fs = NULL; 1781da177e4SLinus Torvalds int result = -ENOENT; 17999f89551SEric W. Biederman 18099f89551SEric W. Biederman if (task) { 18199f89551SEric W. Biederman fs = get_fs_struct(task); 18299f89551SEric W. Biederman put_task_struct(task); 18399f89551SEric W. Biederman } 1841da177e4SLinus Torvalds if (fs) { 1851da177e4SLinus Torvalds read_lock(&fs->lock); 1861da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 1871da177e4SLinus Torvalds *dentry = dget(fs->root); 1881da177e4SLinus Torvalds read_unlock(&fs->lock); 1891da177e4SLinus Torvalds result = 0; 1901da177e4SLinus Torvalds put_fs_struct(fs); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds return result; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1961da177e4SLinus Torvalds (task == current || \ 1971da177e4SLinus Torvalds (task->parent == current && \ 1981da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 1991da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 2001da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static int proc_pid_environ(struct task_struct *task, char * buffer) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int res = 0; 2051da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2061da177e4SLinus Torvalds if (mm) { 207da58a161SAlexey Dobriyan unsigned int len; 208da58a161SAlexey Dobriyan 209da58a161SAlexey Dobriyan res = -ESRCH; 210da58a161SAlexey Dobriyan if (!ptrace_may_attach(task)) 211da58a161SAlexey Dobriyan goto out; 212da58a161SAlexey Dobriyan 213da58a161SAlexey Dobriyan len = mm->env_end - mm->env_start; 2141da177e4SLinus Torvalds if (len > PAGE_SIZE) 2151da177e4SLinus Torvalds len = PAGE_SIZE; 2161da177e4SLinus Torvalds res = access_process_vm(task, mm->env_start, buffer, len, 0); 217da58a161SAlexey Dobriyan out: 2181da177e4SLinus Torvalds mmput(mm); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds return res; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds int res = 0; 2261da177e4SLinus Torvalds unsigned int len; 2271da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2281da177e4SLinus Torvalds if (!mm) 2291da177e4SLinus Torvalds goto out; 2301da177e4SLinus Torvalds if (!mm->arg_end) 2311da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds if (len > PAGE_SIZE) 2361da177e4SLinus Torvalds len = PAGE_SIZE; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2411da177e4SLinus Torvalds // assume application is using setproctitle(3). 2421da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2431da177e4SLinus Torvalds len = strnlen(buffer, res); 2441da177e4SLinus Torvalds if (len < res) { 2451da177e4SLinus Torvalds res = len; 2461da177e4SLinus Torvalds } else { 2471da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2481da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2491da177e4SLinus Torvalds len = PAGE_SIZE - res; 2501da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2511da177e4SLinus Torvalds res = strnlen(buffer, res); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds out_mm: 2551da177e4SLinus Torvalds mmput(mm); 2561da177e4SLinus Torvalds out: 2571da177e4SLinus Torvalds return res; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds int res = 0; 2631da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2641da177e4SLinus Torvalds if (mm) { 2651da177e4SLinus Torvalds unsigned int nwords = 0; 2661da177e4SLinus Torvalds do 2671da177e4SLinus Torvalds nwords += 2; 2681da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2691da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2701da177e4SLinus Torvalds if (res > PAGE_SIZE) 2711da177e4SLinus Torvalds res = PAGE_SIZE; 2721da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2731da177e4SLinus Torvalds mmput(mm); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds return res; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2821da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2831da177e4SLinus Torvalds */ 2841da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2851da177e4SLinus Torvalds { 286ffb45122SAlexey Dobriyan unsigned long wchan; 2879281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds wchan = get_wchan(task); 2901da177e4SLinus Torvalds 2919d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2921da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2939d65cb4aSAlexey Dobriyan else 2949d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2991da177e4SLinus Torvalds /* 3001da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3011da177e4SLinus Torvalds */ 3021da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3031da177e4SLinus Torvalds { 304172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3051da177e4SLinus Torvalds task->sched_info.cpu_time, 3061da177e4SLinus Torvalds task->sched_info.run_delay, 307*2d72376bSIngo Molnar task->sched_info.pcount); 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds #endif 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* The badness from the OOM killer */ 3121da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 3131da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds unsigned long points; 3161da177e4SLinus Torvalds struct timespec uptime; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 31919c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3201da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 32119c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3221da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /************************************************************************/ 3261da177e4SLinus Torvalds /* Here the fs part begins */ 3271da177e4SLinus Torvalds /************************************************************************/ 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* permission checks */ 330778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3311da177e4SLinus Torvalds { 332778c1144SEric W. Biederman struct task_struct *task; 333778c1144SEric W. Biederman int allowed = 0; 334df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 335df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 336df26c40eSEric W. Biederman * information. 337778c1144SEric W. Biederman */ 338778c1144SEric W. Biederman task = get_proc_task(inode); 339df26c40eSEric W. Biederman if (task) { 340778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 341778c1144SEric W. Biederman put_task_struct(task); 342df26c40eSEric W. Biederman } 343778c1144SEric W. Biederman return allowed; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3466d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 3476d76fa58SLinus Torvalds { 3486d76fa58SLinus Torvalds int error; 3496d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 3506d76fa58SLinus Torvalds 3516d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 3526d76fa58SLinus Torvalds return -EPERM; 3536d76fa58SLinus Torvalds 3546d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 3556d76fa58SLinus Torvalds if (!error) 3566d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 3576d76fa58SLinus Torvalds return error; 3586d76fa58SLinus Torvalds } 3596d76fa58SLinus Torvalds 360c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 3616d76fa58SLinus Torvalds .setattr = proc_setattr, 3626d76fa58SLinus Torvalds }; 3636d76fa58SLinus Torvalds 3641da177e4SLinus Torvalds extern struct seq_operations mounts_op; 3655addc5ddSAl Viro struct proc_mounts { 3665addc5ddSAl Viro struct seq_file m; 3675addc5ddSAl Viro int event; 3685addc5ddSAl Viro }; 3695addc5ddSAl Viro 3701da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 3711da177e4SLinus Torvalds { 37299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 3736b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 3745addc5ddSAl Viro struct proc_mounts *p; 3755addc5ddSAl Viro int ret = -EINVAL; 3765addc5ddSAl Viro 37799f89551SEric W. Biederman if (task) { 3781da177e4SLinus Torvalds task_lock(task); 379863c4702SAlexey Dobriyan if (task->nsproxy) { 3806b3286edSKirill Korotaev ns = task->nsproxy->mnt_ns; 3816b3286edSKirill Korotaev if (ns) 3826b3286edSKirill Korotaev get_mnt_ns(ns); 383863c4702SAlexey Dobriyan } 3841da177e4SLinus Torvalds task_unlock(task); 38599f89551SEric W. Biederman put_task_struct(task); 38699f89551SEric W. Biederman } 3871da177e4SLinus Torvalds 3886b3286edSKirill Korotaev if (ns) { 3895addc5ddSAl Viro ret = -ENOMEM; 3905addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 3915addc5ddSAl Viro if (p) { 3925addc5ddSAl Viro file->private_data = &p->m; 3935addc5ddSAl Viro ret = seq_open(file, &mounts_op); 3945addc5ddSAl Viro if (!ret) { 3956b3286edSKirill Korotaev p->m.private = ns; 3966b3286edSKirill Korotaev p->event = ns->event; 3975addc5ddSAl Viro return 0; 3981da177e4SLinus Torvalds } 3995addc5ddSAl Viro kfree(p); 4005addc5ddSAl Viro } 4016b3286edSKirill Korotaev put_mnt_ns(ns); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds return ret; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds struct seq_file *m = file->private_data; 4096b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 4106b3286edSKirill Korotaev put_mnt_ns(ns); 4111da177e4SLinus Torvalds return seq_release(inode, file); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4145addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 4155addc5ddSAl Viro { 4165addc5ddSAl Viro struct proc_mounts *p = file->private_data; 4176b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 4185addc5ddSAl Viro unsigned res = 0; 4195addc5ddSAl Viro 4205addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4215addc5ddSAl Viro 4225addc5ddSAl Viro spin_lock(&vfsmount_lock); 4235addc5ddSAl Viro if (p->event != ns->event) { 4245addc5ddSAl Viro p->event = ns->event; 4255addc5ddSAl Viro res = POLLERR; 4265addc5ddSAl Viro } 4275addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4285addc5ddSAl Viro 4295addc5ddSAl Viro return res; 4305addc5ddSAl Viro } 4315addc5ddSAl Viro 43200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4331da177e4SLinus Torvalds .open = mounts_open, 4341da177e4SLinus Torvalds .read = seq_read, 4351da177e4SLinus Torvalds .llseek = seq_lseek, 4361da177e4SLinus Torvalds .release = mounts_release, 4375addc5ddSAl Viro .poll = mounts_poll, 4381da177e4SLinus Torvalds }; 4391da177e4SLinus Torvalds 440b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 441b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 442b4629fe2SChuck Lever { 443b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 444b4629fe2SChuck Lever 445b4629fe2SChuck Lever if (!ret) { 446b4629fe2SChuck Lever struct seq_file *m = file->private_data; 4476b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 44899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 44999f89551SEric W. Biederman 45099f89551SEric W. Biederman if (task) { 451b4629fe2SChuck Lever task_lock(task); 452701e054eSVasily Tarasov if (task->nsproxy) 4536b3286edSKirill Korotaev mnt_ns = task->nsproxy->mnt_ns; 4546b3286edSKirill Korotaev if (mnt_ns) 4556b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 456b4629fe2SChuck Lever task_unlock(task); 45799f89551SEric W. Biederman put_task_struct(task); 45899f89551SEric W. Biederman } 459b4629fe2SChuck Lever 4606b3286edSKirill Korotaev if (mnt_ns) 4616b3286edSKirill Korotaev m->private = mnt_ns; 462b4629fe2SChuck Lever else { 463b4629fe2SChuck Lever seq_release(inode, file); 464b4629fe2SChuck Lever ret = -EINVAL; 465b4629fe2SChuck Lever } 466b4629fe2SChuck Lever } 467b4629fe2SChuck Lever return ret; 468b4629fe2SChuck Lever } 469b4629fe2SChuck Lever 47000977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 471b4629fe2SChuck Lever .open = mountstats_open, 472b4629fe2SChuck Lever .read = seq_read, 473b4629fe2SChuck Lever .llseek = seq_lseek, 474b4629fe2SChuck Lever .release = mounts_release, 475b4629fe2SChuck Lever }; 476b4629fe2SChuck Lever 4771da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 4801da177e4SLinus Torvalds size_t count, loff_t *ppos) 4811da177e4SLinus Torvalds { 4822fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 4831da177e4SLinus Torvalds unsigned long page; 4841da177e4SLinus Torvalds ssize_t length; 48599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 48699f89551SEric W. Biederman 48799f89551SEric W. Biederman length = -ESRCH; 48899f89551SEric W. Biederman if (!task) 48999f89551SEric W. Biederman goto out_no_task; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 4921da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 49399f89551SEric W. Biederman 49499f89551SEric W. Biederman length = -ENOMEM; 4951da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 49699f89551SEric W. Biederman goto out; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (length >= 0) 5011da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 5021da177e4SLinus Torvalds free_page(page); 50399f89551SEric W. Biederman out: 50499f89551SEric W. Biederman put_task_struct(task); 50599f89551SEric W. Biederman out_no_task: 5061da177e4SLinus Torvalds return length; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 50900977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 5101da177e4SLinus Torvalds .read = proc_info_read, 5111da177e4SLinus Torvalds }; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 5161da177e4SLinus Torvalds return 0; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 5201da177e4SLinus Torvalds size_t count, loff_t *ppos) 5211da177e4SLinus Torvalds { 5222fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5231da177e4SLinus Torvalds char *page; 5241da177e4SLinus Torvalds unsigned long src = *ppos; 5251da177e4SLinus Torvalds int ret = -ESRCH; 5261da177e4SLinus Torvalds struct mm_struct *mm; 5271da177e4SLinus Torvalds 52899f89551SEric W. Biederman if (!task) 52999f89551SEric W. Biederman goto out_no_task; 53099f89551SEric W. Biederman 531ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5321da177e4SLinus Torvalds goto out; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds ret = -ENOMEM; 5351da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 5361da177e4SLinus Torvalds if (!page) 5371da177e4SLinus Torvalds goto out; 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds ret = 0; 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds mm = get_task_mm(task); 5421da177e4SLinus Torvalds if (!mm) 5431da177e4SLinus Torvalds goto out_free; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds ret = -EIO; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 5481da177e4SLinus Torvalds goto out_put; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds ret = 0; 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds while (count > 0) { 5531da177e4SLinus Torvalds int this_len, retval; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5561da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 557ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 5581da177e4SLinus Torvalds if (!ret) 5591da177e4SLinus Torvalds ret = -EIO; 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 5641da177e4SLinus Torvalds ret = -EFAULT; 5651da177e4SLinus Torvalds break; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds ret += retval; 5691da177e4SLinus Torvalds src += retval; 5701da177e4SLinus Torvalds buf += retval; 5711da177e4SLinus Torvalds count -= retval; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds *ppos = src; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds out_put: 5761da177e4SLinus Torvalds mmput(mm); 5771da177e4SLinus Torvalds out_free: 5781da177e4SLinus Torvalds free_page((unsigned long) page); 5791da177e4SLinus Torvalds out: 58099f89551SEric W. Biederman put_task_struct(task); 58199f89551SEric W. Biederman out_no_task: 5821da177e4SLinus Torvalds return ret; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds #define mem_write NULL 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds #ifndef mem_write 5881da177e4SLinus Torvalds /* This is a security hazard */ 58963967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 5901da177e4SLinus Torvalds size_t count, loff_t *ppos) 5911da177e4SLinus Torvalds { 592f7ca54f4SFrederik Deweerdt int copied; 5931da177e4SLinus Torvalds char *page; 5942fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5951da177e4SLinus Torvalds unsigned long dst = *ppos; 5961da177e4SLinus Torvalds 59799f89551SEric W. Biederman copied = -ESRCH; 59899f89551SEric W. Biederman if (!task) 59999f89551SEric W. Biederman goto out_no_task; 6001da177e4SLinus Torvalds 60199f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 60299f89551SEric W. Biederman goto out; 60399f89551SEric W. Biederman 60499f89551SEric W. Biederman copied = -ENOMEM; 6051da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 6061da177e4SLinus Torvalds if (!page) 60799f89551SEric W. Biederman goto out; 6081da177e4SLinus Torvalds 609f7ca54f4SFrederik Deweerdt copied = 0; 6101da177e4SLinus Torvalds while (count > 0) { 6111da177e4SLinus Torvalds int this_len, retval; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 6141da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 6151da177e4SLinus Torvalds copied = -EFAULT; 6161da177e4SLinus Torvalds break; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 6191da177e4SLinus Torvalds if (!retval) { 6201da177e4SLinus Torvalds if (!copied) 6211da177e4SLinus Torvalds copied = -EIO; 6221da177e4SLinus Torvalds break; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds copied += retval; 6251da177e4SLinus Torvalds buf += retval; 6261da177e4SLinus Torvalds dst += retval; 6271da177e4SLinus Torvalds count -= retval; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds *ppos = dst; 6301da177e4SLinus Torvalds free_page((unsigned long) page); 63199f89551SEric W. Biederman out: 63299f89551SEric W. Biederman put_task_struct(task); 63399f89551SEric W. Biederman out_no_task: 6341da177e4SLinus Torvalds return copied; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds #endif 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds switch (orig) { 6411da177e4SLinus Torvalds case 0: 6421da177e4SLinus Torvalds file->f_pos = offset; 6431da177e4SLinus Torvalds break; 6441da177e4SLinus Torvalds case 1: 6451da177e4SLinus Torvalds file->f_pos += offset; 6461da177e4SLinus Torvalds break; 6471da177e4SLinus Torvalds default: 6481da177e4SLinus Torvalds return -EINVAL; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds force_successful_syscall_return(); 6511da177e4SLinus Torvalds return file->f_pos; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 65400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 6551da177e4SLinus Torvalds .llseek = mem_lseek, 6561da177e4SLinus Torvalds .read = mem_read, 6571da177e4SLinus Torvalds .write = mem_write, 6581da177e4SLinus Torvalds .open = mem_open, 6591da177e4SLinus Torvalds }; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 6621da177e4SLinus Torvalds size_t count, loff_t *ppos) 6631da177e4SLinus Torvalds { 6642fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 6658578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 6661da177e4SLinus Torvalds size_t len; 66799f89551SEric W. Biederman int oom_adjust; 6681da177e4SLinus Torvalds 66999f89551SEric W. Biederman if (!task) 67099f89551SEric W. Biederman return -ESRCH; 67199f89551SEric W. Biederman oom_adjust = task->oomkilladj; 67299f89551SEric W. Biederman put_task_struct(task); 67399f89551SEric W. Biederman 6748578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 6750c28f287SAkinobu Mita 6760c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 6801da177e4SLinus Torvalds size_t count, loff_t *ppos) 6811da177e4SLinus Torvalds { 68299f89551SEric W. Biederman struct task_struct *task; 6838578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 6841da177e4SLinus Torvalds int oom_adjust; 6851da177e4SLinus Torvalds 6868578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 6878578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 6888578cea7SEric W. Biederman count = sizeof(buffer) - 1; 6891da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 6901da177e4SLinus Torvalds return -EFAULT; 6911da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 6928ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 6938ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 6941da177e4SLinus Torvalds return -EINVAL; 6951da177e4SLinus Torvalds if (*end == '\n') 6961da177e4SLinus Torvalds end++; 6972fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 69899f89551SEric W. Biederman if (!task) 69999f89551SEric W. Biederman return -ESRCH; 7008fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 7018fb4fc68SGuillem Jover put_task_struct(task); 7028fb4fc68SGuillem Jover return -EACCES; 7038fb4fc68SGuillem Jover } 7041da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 70599f89551SEric W. Biederman put_task_struct(task); 7061da177e4SLinus Torvalds if (end - buffer == 0) 7071da177e4SLinus Torvalds return -EIO; 7081da177e4SLinus Torvalds return end - buffer; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 71100977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 7121da177e4SLinus Torvalds .read = oom_adjust_read, 7131da177e4SLinus Torvalds .write = oom_adjust_write, 7141da177e4SLinus Torvalds }; 7151da177e4SLinus Torvalds 7164b8df891SDavid Rientjes #ifdef CONFIG_MMU 717b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 718b813e931SDavid Rientjes size_t count, loff_t *ppos) 719b813e931SDavid Rientjes { 720b813e931SDavid Rientjes struct task_struct *task; 721b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 722b813e931SDavid Rientjes struct mm_struct *mm; 723b813e931SDavid Rientjes 724b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 725b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 726b813e931SDavid Rientjes count = sizeof(buffer) - 1; 727b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 728b813e931SDavid Rientjes return -EFAULT; 729b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 730b813e931SDavid Rientjes return -EINVAL; 731b813e931SDavid Rientjes if (*end == '\n') 732b813e931SDavid Rientjes end++; 733b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 734b813e931SDavid Rientjes if (!task) 735b813e931SDavid Rientjes return -ESRCH; 736b813e931SDavid Rientjes mm = get_task_mm(task); 737b813e931SDavid Rientjes if (mm) { 738b813e931SDavid Rientjes clear_refs_smap(mm); 739b813e931SDavid Rientjes mmput(mm); 740b813e931SDavid Rientjes } 741b813e931SDavid Rientjes put_task_struct(task); 742b813e931SDavid Rientjes if (end - buffer == 0) 743b813e931SDavid Rientjes return -EIO; 744b813e931SDavid Rientjes return end - buffer; 745b813e931SDavid Rientjes } 746b813e931SDavid Rientjes 747b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 748b813e931SDavid Rientjes .write = clear_refs_write, 749b813e931SDavid Rientjes }; 7504b8df891SDavid Rientjes #endif 751b813e931SDavid Rientjes 7521da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 7531da177e4SLinus Torvalds #define TMPBUFLEN 21 7541da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 7551da177e4SLinus Torvalds size_t count, loff_t *ppos) 7561da177e4SLinus Torvalds { 7572fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 75899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 7591da177e4SLinus Torvalds ssize_t length; 7601da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7611da177e4SLinus Torvalds 76299f89551SEric W. Biederman if (!task) 76399f89551SEric W. Biederman return -ESRCH; 7641da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 7651da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 76699f89551SEric W. Biederman put_task_struct(task); 7671da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 7711da177e4SLinus Torvalds size_t count, loff_t *ppos) 7721da177e4SLinus Torvalds { 7732fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7741da177e4SLinus Torvalds char *page, *tmp; 7751da177e4SLinus Torvalds ssize_t length; 7761da177e4SLinus Torvalds uid_t loginuid; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 7791da177e4SLinus Torvalds return -EPERM; 7801da177e4SLinus Torvalds 78113b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 7821da177e4SLinus Torvalds return -EPERM; 7831da177e4SLinus Torvalds 784e0182909SAl Viro if (count >= PAGE_SIZE) 785e0182909SAl Viro count = PAGE_SIZE - 1; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds if (*ppos != 0) { 7881da177e4SLinus Torvalds /* No partial writes. */ 7891da177e4SLinus Torvalds return -EINVAL; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds page = (char*)__get_free_page(GFP_USER); 7921da177e4SLinus Torvalds if (!page) 7931da177e4SLinus Torvalds return -ENOMEM; 7941da177e4SLinus Torvalds length = -EFAULT; 7951da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 7961da177e4SLinus Torvalds goto out_free_page; 7971da177e4SLinus Torvalds 798e0182909SAl Viro page[count] = '\0'; 7991da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 8001da177e4SLinus Torvalds if (tmp == page) { 8011da177e4SLinus Torvalds length = -EINVAL; 8021da177e4SLinus Torvalds goto out_free_page; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds } 80599f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 8061da177e4SLinus Torvalds if (likely(length == 0)) 8071da177e4SLinus Torvalds length = count; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds out_free_page: 8101da177e4SLinus Torvalds free_page((unsigned long) page); 8111da177e4SLinus Torvalds return length; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 81400977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 8151da177e4SLinus Torvalds .read = proc_loginuid_read, 8161da177e4SLinus Torvalds .write = proc_loginuid_write, 8171da177e4SLinus Torvalds }; 8181da177e4SLinus Torvalds #endif 8191da177e4SLinus Torvalds 820f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 821f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 822f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 823f4f154fdSAkinobu Mita { 824f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 825f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 826f4f154fdSAkinobu Mita size_t len; 827f4f154fdSAkinobu Mita int make_it_fail; 828f4f154fdSAkinobu Mita 829f4f154fdSAkinobu Mita if (!task) 830f4f154fdSAkinobu Mita return -ESRCH; 831f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 832f4f154fdSAkinobu Mita put_task_struct(task); 833f4f154fdSAkinobu Mita 834f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 8350c28f287SAkinobu Mita 8360c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 837f4f154fdSAkinobu Mita } 838f4f154fdSAkinobu Mita 839f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 840f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 841f4f154fdSAkinobu Mita { 842f4f154fdSAkinobu Mita struct task_struct *task; 843f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 844f4f154fdSAkinobu Mita int make_it_fail; 845f4f154fdSAkinobu Mita 846f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 847f4f154fdSAkinobu Mita return -EPERM; 848f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 849f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 850f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 851f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 852f4f154fdSAkinobu Mita return -EFAULT; 853f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 854f4f154fdSAkinobu Mita if (*end == '\n') 855f4f154fdSAkinobu Mita end++; 856f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 857f4f154fdSAkinobu Mita if (!task) 858f4f154fdSAkinobu Mita return -ESRCH; 859f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 860f4f154fdSAkinobu Mita put_task_struct(task); 861f4f154fdSAkinobu Mita if (end - buffer == 0) 862f4f154fdSAkinobu Mita return -EIO; 863f4f154fdSAkinobu Mita return end - buffer; 864f4f154fdSAkinobu Mita } 865f4f154fdSAkinobu Mita 86600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 867f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 868f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 869f4f154fdSAkinobu Mita }; 870f4f154fdSAkinobu Mita #endif 871f4f154fdSAkinobu Mita 87243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 87343ae34cbSIngo Molnar /* 87443ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 87543ae34cbSIngo Molnar */ 87643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 87743ae34cbSIngo Molnar { 87843ae34cbSIngo Molnar struct inode *inode = m->private; 87943ae34cbSIngo Molnar struct task_struct *p; 88043ae34cbSIngo Molnar 88143ae34cbSIngo Molnar WARN_ON(!inode); 88243ae34cbSIngo Molnar 88343ae34cbSIngo Molnar p = get_proc_task(inode); 88443ae34cbSIngo Molnar if (!p) 88543ae34cbSIngo Molnar return -ESRCH; 88643ae34cbSIngo Molnar proc_sched_show_task(p, m); 88743ae34cbSIngo Molnar 88843ae34cbSIngo Molnar put_task_struct(p); 88943ae34cbSIngo Molnar 89043ae34cbSIngo Molnar return 0; 89143ae34cbSIngo Molnar } 89243ae34cbSIngo Molnar 89343ae34cbSIngo Molnar static ssize_t 89443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 89543ae34cbSIngo Molnar size_t count, loff_t *offset) 89643ae34cbSIngo Molnar { 89743ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 89843ae34cbSIngo Molnar struct task_struct *p; 89943ae34cbSIngo Molnar 90043ae34cbSIngo Molnar WARN_ON(!inode); 90143ae34cbSIngo Molnar 90243ae34cbSIngo Molnar p = get_proc_task(inode); 90343ae34cbSIngo Molnar if (!p) 90443ae34cbSIngo Molnar return -ESRCH; 90543ae34cbSIngo Molnar proc_sched_set_task(p); 90643ae34cbSIngo Molnar 90743ae34cbSIngo Molnar put_task_struct(p); 90843ae34cbSIngo Molnar 90943ae34cbSIngo Molnar return count; 91043ae34cbSIngo Molnar } 91143ae34cbSIngo Molnar 91243ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 91343ae34cbSIngo Molnar { 91443ae34cbSIngo Molnar int ret; 91543ae34cbSIngo Molnar 91643ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 91743ae34cbSIngo Molnar if (!ret) { 91843ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 91943ae34cbSIngo Molnar 92043ae34cbSIngo Molnar m->private = inode; 92143ae34cbSIngo Molnar } 92243ae34cbSIngo Molnar return ret; 92343ae34cbSIngo Molnar } 92443ae34cbSIngo Molnar 92543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 92643ae34cbSIngo Molnar .open = sched_open, 92743ae34cbSIngo Molnar .read = seq_read, 92843ae34cbSIngo Molnar .write = sched_write, 92943ae34cbSIngo Molnar .llseek = seq_lseek, 9305ea473a1SAlexey Dobriyan .release = single_release, 93143ae34cbSIngo Molnar }; 93243ae34cbSIngo Molnar 93343ae34cbSIngo Molnar #endif 93443ae34cbSIngo Molnar 935008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 9361da177e4SLinus Torvalds { 9371da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9381da177e4SLinus Torvalds int error = -EACCES; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 9411da177e4SLinus Torvalds path_release(nd); 9421da177e4SLinus Torvalds 943778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 944778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 9451da177e4SLinus Torvalds goto out; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 9481da177e4SLinus Torvalds nd->last_type = LAST_BIND; 9491da177e4SLinus Torvalds out: 950008b150aSAl Viro return ERR_PTR(error); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 9541da177e4SLinus Torvalds char __user *buffer, int buflen) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds struct inode * inode; 9571da177e4SLinus Torvalds char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; 9581da177e4SLinus Torvalds int len; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds if (!tmp) 9611da177e4SLinus Torvalds return -ENOMEM; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds inode = dentry->d_inode; 9641da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 9651da177e4SLinus Torvalds len = PTR_ERR(path); 9661da177e4SLinus Torvalds if (IS_ERR(path)) 9671da177e4SLinus Torvalds goto out; 9681da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (len > buflen) 9711da177e4SLinus Torvalds len = buflen; 9721da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 9731da177e4SLinus Torvalds len = -EFAULT; 9741da177e4SLinus Torvalds out: 9751da177e4SLinus Torvalds free_page((unsigned long)tmp); 9761da177e4SLinus Torvalds return len; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 9801da177e4SLinus Torvalds { 9811da177e4SLinus Torvalds int error = -EACCES; 9821da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9831da177e4SLinus Torvalds struct dentry *de; 9841da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 9851da177e4SLinus Torvalds 986778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 987778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 9881da177e4SLinus Torvalds goto out; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 9911da177e4SLinus Torvalds if (error) 9921da177e4SLinus Torvalds goto out; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 9951da177e4SLinus Torvalds dput(de); 9961da177e4SLinus Torvalds mntput(mnt); 9971da177e4SLinus Torvalds out: 9981da177e4SLinus Torvalds return error; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 1001c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 10021da177e4SLinus Torvalds .readlink = proc_pid_readlink, 10036d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 10046d76fa58SLinus Torvalds .setattr = proc_setattr, 10051da177e4SLinus Torvalds }; 10061da177e4SLinus Torvalds 100728a6d671SEric W. Biederman 100828a6d671SEric W. Biederman /* building an inode */ 100928a6d671SEric W. Biederman 101028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 101128a6d671SEric W. Biederman { 101228a6d671SEric W. Biederman int dumpable = 0; 101328a6d671SEric W. Biederman struct mm_struct *mm; 101428a6d671SEric W. Biederman 101528a6d671SEric W. Biederman task_lock(task); 101628a6d671SEric W. Biederman mm = task->mm; 101728a6d671SEric W. Biederman if (mm) 10186c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 101928a6d671SEric W. Biederman task_unlock(task); 102028a6d671SEric W. Biederman if(dumpable == 1) 102128a6d671SEric W. Biederman return 1; 102228a6d671SEric W. Biederman return 0; 102328a6d671SEric W. Biederman } 102428a6d671SEric W. Biederman 102528a6d671SEric W. Biederman 102661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 102728a6d671SEric W. Biederman { 102828a6d671SEric W. Biederman struct inode * inode; 102928a6d671SEric W. Biederman struct proc_inode *ei; 103028a6d671SEric W. Biederman 103128a6d671SEric W. Biederman /* We need a new inode */ 103228a6d671SEric W. Biederman 103328a6d671SEric W. Biederman inode = new_inode(sb); 103428a6d671SEric W. Biederman if (!inode) 103528a6d671SEric W. Biederman goto out; 103628a6d671SEric W. Biederman 103728a6d671SEric W. Biederman /* Common stuff */ 103828a6d671SEric W. Biederman ei = PROC_I(inode); 103928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 104028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 104128a6d671SEric W. Biederman 104228a6d671SEric W. Biederman /* 104328a6d671SEric W. Biederman * grab the reference to task. 104428a6d671SEric W. Biederman */ 10451a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 104628a6d671SEric W. Biederman if (!ei->pid) 104728a6d671SEric W. Biederman goto out_unlock; 104828a6d671SEric W. Biederman 104928a6d671SEric W. Biederman inode->i_uid = 0; 105028a6d671SEric W. Biederman inode->i_gid = 0; 105128a6d671SEric W. Biederman if (task_dumpable(task)) { 105228a6d671SEric W. Biederman inode->i_uid = task->euid; 105328a6d671SEric W. Biederman inode->i_gid = task->egid; 105428a6d671SEric W. Biederman } 105528a6d671SEric W. Biederman security_task_to_inode(task, inode); 105628a6d671SEric W. Biederman 105728a6d671SEric W. Biederman out: 105828a6d671SEric W. Biederman return inode; 105928a6d671SEric W. Biederman 106028a6d671SEric W. Biederman out_unlock: 106128a6d671SEric W. Biederman iput(inode); 106228a6d671SEric W. Biederman return NULL; 106328a6d671SEric W. Biederman } 106428a6d671SEric W. Biederman 106528a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 106628a6d671SEric W. Biederman { 106728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 106828a6d671SEric W. Biederman struct task_struct *task; 106928a6d671SEric W. Biederman generic_fillattr(inode, stat); 107028a6d671SEric W. Biederman 107128a6d671SEric W. Biederman rcu_read_lock(); 107228a6d671SEric W. Biederman stat->uid = 0; 107328a6d671SEric W. Biederman stat->gid = 0; 107428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 107528a6d671SEric W. Biederman if (task) { 107628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 107728a6d671SEric W. Biederman task_dumpable(task)) { 107828a6d671SEric W. Biederman stat->uid = task->euid; 107928a6d671SEric W. Biederman stat->gid = task->egid; 108028a6d671SEric W. Biederman } 108128a6d671SEric W. Biederman } 108228a6d671SEric W. Biederman rcu_read_unlock(); 108328a6d671SEric W. Biederman return 0; 108428a6d671SEric W. Biederman } 108528a6d671SEric W. Biederman 108628a6d671SEric W. Biederman /* dentry stuff */ 108728a6d671SEric W. Biederman 108828a6d671SEric W. Biederman /* 108928a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 109028a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 109128a6d671SEric W. Biederman * due to the way we treat inodes. 109228a6d671SEric W. Biederman * 109328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 109428a6d671SEric W. Biederman * performed a setuid(), etc. 109528a6d671SEric W. Biederman * 109628a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 109728a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 109828a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 109928a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 110028a6d671SEric W. Biederman * made this apply to all per process world readable and executable 110128a6d671SEric W. Biederman * directories. 110228a6d671SEric W. Biederman */ 110328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 110428a6d671SEric W. Biederman { 110528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 110628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 110728a6d671SEric W. Biederman if (task) { 110828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 110928a6d671SEric W. Biederman task_dumpable(task)) { 111028a6d671SEric W. Biederman inode->i_uid = task->euid; 111128a6d671SEric W. Biederman inode->i_gid = task->egid; 111228a6d671SEric W. Biederman } else { 111328a6d671SEric W. Biederman inode->i_uid = 0; 111428a6d671SEric W. Biederman inode->i_gid = 0; 111528a6d671SEric W. Biederman } 111628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 111728a6d671SEric W. Biederman security_task_to_inode(task, inode); 111828a6d671SEric W. Biederman put_task_struct(task); 111928a6d671SEric W. Biederman return 1; 112028a6d671SEric W. Biederman } 112128a6d671SEric W. Biederman d_drop(dentry); 112228a6d671SEric W. Biederman return 0; 112328a6d671SEric W. Biederman } 112428a6d671SEric W. Biederman 112528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 112628a6d671SEric W. Biederman { 112728a6d671SEric W. Biederman /* Is the task we represent dead? 112828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 112928a6d671SEric W. Biederman * kill it immediately. 113028a6d671SEric W. Biederman */ 113128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 113228a6d671SEric W. Biederman } 113328a6d671SEric W. Biederman 113428a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 113528a6d671SEric W. Biederman { 113628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 113728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 113828a6d671SEric W. Biederman }; 113928a6d671SEric W. Biederman 114028a6d671SEric W. Biederman /* Lookups */ 114128a6d671SEric W. Biederman 1142c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1143c5141e6dSEric Dumazet struct task_struct *, const void *); 114461a28784SEric W. Biederman 11451c0d04c9SEric W. Biederman /* 11461c0d04c9SEric W. Biederman * Fill a directory entry. 11471c0d04c9SEric W. Biederman * 11481c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 11491c0d04c9SEric W. Biederman * file type from dcache entry. 11501c0d04c9SEric W. Biederman * 11511c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 11521c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 11531c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 11541c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 11551c0d04c9SEric W. Biederman * by stat. 11561c0d04c9SEric W. Biederman */ 115761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 115861a28784SEric W. Biederman char *name, int len, 1159c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 116061a28784SEric W. Biederman { 11612fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 116261a28784SEric W. Biederman struct inode *inode; 116361a28784SEric W. Biederman struct qstr qname; 116461a28784SEric W. Biederman ino_t ino = 0; 116561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 116661a28784SEric W. Biederman 116761a28784SEric W. Biederman qname.name = name; 116861a28784SEric W. Biederman qname.len = len; 116961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 117061a28784SEric W. Biederman 117161a28784SEric W. Biederman child = d_lookup(dir, &qname); 117261a28784SEric W. Biederman if (!child) { 117361a28784SEric W. Biederman struct dentry *new; 117461a28784SEric W. Biederman new = d_alloc(dir, &qname); 117561a28784SEric W. Biederman if (new) { 117661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 117761a28784SEric W. Biederman if (child) 117861a28784SEric W. Biederman dput(new); 117961a28784SEric W. Biederman else 118061a28784SEric W. Biederman child = new; 118161a28784SEric W. Biederman } 118261a28784SEric W. Biederman } 118361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 118461a28784SEric W. Biederman goto end_instantiate; 118561a28784SEric W. Biederman inode = child->d_inode; 118661a28784SEric W. Biederman if (inode) { 118761a28784SEric W. Biederman ino = inode->i_ino; 118861a28784SEric W. Biederman type = inode->i_mode >> 12; 118961a28784SEric W. Biederman } 119061a28784SEric W. Biederman dput(child); 119161a28784SEric W. Biederman end_instantiate: 119261a28784SEric W. Biederman if (!ino) 119361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 119461a28784SEric W. Biederman if (!ino) 119561a28784SEric W. Biederman ino = 1; 119661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 119761a28784SEric W. Biederman } 119861a28784SEric W. Biederman 119928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 120028a6d671SEric W. Biederman { 120128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 120228a6d671SEric W. Biederman int len = dentry->d_name.len; 120328a6d671SEric W. Biederman unsigned n = 0; 120428a6d671SEric W. Biederman 120528a6d671SEric W. Biederman if (len > 1 && *name == '0') 120628a6d671SEric W. Biederman goto out; 120728a6d671SEric W. Biederman while (len-- > 0) { 120828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 120928a6d671SEric W. Biederman if (c > 9) 121028a6d671SEric W. Biederman goto out; 121128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 121228a6d671SEric W. Biederman goto out; 121328a6d671SEric W. Biederman n *= 10; 121428a6d671SEric W. Biederman n += c; 121528a6d671SEric W. Biederman } 121628a6d671SEric W. Biederman return n; 121728a6d671SEric W. Biederman out: 121828a6d671SEric W. Biederman return ~0U; 121928a6d671SEric W. Biederman } 122028a6d671SEric W. Biederman 122127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 122227932742SMiklos Szeredi 122327932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 122427932742SMiklos Szeredi struct vfsmount **mnt, char *info) 122528a6d671SEric W. Biederman { 122628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 122728a6d671SEric W. Biederman struct files_struct *files = NULL; 122828a6d671SEric W. Biederman struct file *file; 122928a6d671SEric W. Biederman int fd = proc_fd(inode); 123028a6d671SEric W. Biederman 123128a6d671SEric W. Biederman if (task) { 123228a6d671SEric W. Biederman files = get_files_struct(task); 123328a6d671SEric W. Biederman put_task_struct(task); 123428a6d671SEric W. Biederman } 123528a6d671SEric W. Biederman if (files) { 123628a6d671SEric W. Biederman /* 123728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 123828a6d671SEric W. Biederman * hold ->file_lock. 123928a6d671SEric W. Biederman */ 124028a6d671SEric W. Biederman spin_lock(&files->file_lock); 124128a6d671SEric W. Biederman file = fcheck_files(files, fd); 124228a6d671SEric W. Biederman if (file) { 124327932742SMiklos Szeredi if (mnt) 12442fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 124527932742SMiklos Szeredi if (dentry) 12462fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 124727932742SMiklos Szeredi if (info) 124827932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 124927932742SMiklos Szeredi "pos:\t%lli\n" 125027932742SMiklos Szeredi "flags:\t0%o\n", 125127932742SMiklos Szeredi (long long) file->f_pos, 125227932742SMiklos Szeredi file->f_flags); 125328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 125428a6d671SEric W. Biederman put_files_struct(files); 125528a6d671SEric W. Biederman return 0; 125628a6d671SEric W. Biederman } 125728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 125828a6d671SEric W. Biederman put_files_struct(files); 125928a6d671SEric W. Biederman } 126028a6d671SEric W. Biederman return -ENOENT; 126128a6d671SEric W. Biederman } 126228a6d671SEric W. Biederman 126327932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 126427932742SMiklos Szeredi struct vfsmount **mnt) 126527932742SMiklos Szeredi { 126627932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 126727932742SMiklos Szeredi } 126827932742SMiklos Szeredi 126928a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 127028a6d671SEric W. Biederman { 127128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 127228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 127328a6d671SEric W. Biederman int fd = proc_fd(inode); 127428a6d671SEric W. Biederman struct files_struct *files; 127528a6d671SEric W. Biederman 127628a6d671SEric W. Biederman if (task) { 127728a6d671SEric W. Biederman files = get_files_struct(task); 127828a6d671SEric W. Biederman if (files) { 127928a6d671SEric W. Biederman rcu_read_lock(); 128028a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 128128a6d671SEric W. Biederman rcu_read_unlock(); 128228a6d671SEric W. Biederman put_files_struct(files); 128328a6d671SEric W. Biederman if (task_dumpable(task)) { 128428a6d671SEric W. Biederman inode->i_uid = task->euid; 128528a6d671SEric W. Biederman inode->i_gid = task->egid; 128628a6d671SEric W. Biederman } else { 128728a6d671SEric W. Biederman inode->i_uid = 0; 128828a6d671SEric W. Biederman inode->i_gid = 0; 128928a6d671SEric W. Biederman } 129028a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 129128a6d671SEric W. Biederman security_task_to_inode(task, inode); 129228a6d671SEric W. Biederman put_task_struct(task); 129328a6d671SEric W. Biederman return 1; 129428a6d671SEric W. Biederman } 129528a6d671SEric W. Biederman rcu_read_unlock(); 129628a6d671SEric W. Biederman put_files_struct(files); 129728a6d671SEric W. Biederman } 129828a6d671SEric W. Biederman put_task_struct(task); 129928a6d671SEric W. Biederman } 130028a6d671SEric W. Biederman d_drop(dentry); 130128a6d671SEric W. Biederman return 0; 130228a6d671SEric W. Biederman } 130328a6d671SEric W. Biederman 130428a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 130528a6d671SEric W. Biederman { 130628a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 130728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 130828a6d671SEric W. Biederman }; 130928a6d671SEric W. Biederman 1310444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1311c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 131228a6d671SEric W. Biederman { 1313c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 131428a6d671SEric W. Biederman struct file *file; 131528a6d671SEric W. Biederman struct files_struct *files; 131628a6d671SEric W. Biederman struct inode *inode; 131728a6d671SEric W. Biederman struct proc_inode *ei; 1318444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 131928a6d671SEric W. Biederman 132061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 132128a6d671SEric W. Biederman if (!inode) 132228a6d671SEric W. Biederman goto out; 132328a6d671SEric W. Biederman ei = PROC_I(inode); 132428a6d671SEric W. Biederman ei->fd = fd; 132528a6d671SEric W. Biederman files = get_files_struct(task); 132628a6d671SEric W. Biederman if (!files) 1327444ceed8SEric W. Biederman goto out_iput; 132828a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 132928a6d671SEric W. Biederman 133028a6d671SEric W. Biederman /* 133128a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 133228a6d671SEric W. Biederman * hold ->file_lock. 133328a6d671SEric W. Biederman */ 133428a6d671SEric W. Biederman spin_lock(&files->file_lock); 133528a6d671SEric W. Biederman file = fcheck_files(files, fd); 133628a6d671SEric W. Biederman if (!file) 1337444ceed8SEric W. Biederman goto out_unlock; 133828a6d671SEric W. Biederman if (file->f_mode & 1) 133928a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 134028a6d671SEric W. Biederman if (file->f_mode & 2) 134128a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 134228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 134328a6d671SEric W. Biederman put_files_struct(files); 1344444ceed8SEric W. Biederman 134528a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 134628a6d671SEric W. Biederman inode->i_size = 64; 134728a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 134828a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 134928a6d671SEric W. Biederman d_add(dentry, inode); 135028a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 135128a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1352444ceed8SEric W. Biederman error = NULL; 1353444ceed8SEric W. Biederman 1354444ceed8SEric W. Biederman out: 1355444ceed8SEric W. Biederman return error; 1356444ceed8SEric W. Biederman out_unlock: 1357444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1358444ceed8SEric W. Biederman put_files_struct(files); 1359444ceed8SEric W. Biederman out_iput: 1360444ceed8SEric W. Biederman iput(inode); 1361444ceed8SEric W. Biederman goto out; 1362444ceed8SEric W. Biederman } 1363444ceed8SEric W. Biederman 136427932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 136527932742SMiklos Szeredi struct dentry *dentry, 136627932742SMiklos Szeredi instantiate_t instantiate) 1367444ceed8SEric W. Biederman { 1368444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1369444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1370444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1371444ceed8SEric W. Biederman 1372444ceed8SEric W. Biederman if (!task) 1373444ceed8SEric W. Biederman goto out_no_task; 1374444ceed8SEric W. Biederman if (fd == ~0U) 1375444ceed8SEric W. Biederman goto out; 1376444ceed8SEric W. Biederman 137727932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 137828a6d671SEric W. Biederman out: 137928a6d671SEric W. Biederman put_task_struct(task); 138028a6d671SEric W. Biederman out_no_task: 138128a6d671SEric W. Biederman return result; 138228a6d671SEric W. Biederman } 138328a6d671SEric W. Biederman 138427932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 138527932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 13861da177e4SLinus Torvalds { 13872fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 13885634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 138999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 13901da177e4SLinus Torvalds unsigned int fd, tid, ino; 13911da177e4SLinus Torvalds int retval; 13921da177e4SLinus Torvalds struct files_struct * files; 1393badf1662SDipankar Sarma struct fdtable *fdt; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds retval = -ENOENT; 139699f89551SEric W. Biederman if (!p) 139799f89551SEric W. Biederman goto out_no_task; 13981da177e4SLinus Torvalds retval = 0; 13991da177e4SLinus Torvalds tid = p->pid; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds fd = filp->f_pos; 14021da177e4SLinus Torvalds switch (fd) { 14031da177e4SLinus Torvalds case 0: 14041da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 14051da177e4SLinus Torvalds goto out; 14061da177e4SLinus Torvalds filp->f_pos++; 14071da177e4SLinus Torvalds case 1: 14085634708bSEric W. Biederman ino = parent_ino(dentry); 14091da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 14101da177e4SLinus Torvalds goto out; 14111da177e4SLinus Torvalds filp->f_pos++; 14121da177e4SLinus Torvalds default: 14131da177e4SLinus Torvalds files = get_files_struct(p); 14141da177e4SLinus Torvalds if (!files) 14151da177e4SLinus Torvalds goto out; 1416b835996fSDipankar Sarma rcu_read_lock(); 1417badf1662SDipankar Sarma fdt = files_fdtable(files); 14181da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1419badf1662SDipankar Sarma fd < fdt->max_fds; 14201da177e4SLinus Torvalds fd++, filp->f_pos++) { 142127932742SMiklos Szeredi char name[PROC_NUMBUF]; 142227932742SMiklos Szeredi int len; 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 14251da177e4SLinus Torvalds continue; 1426b835996fSDipankar Sarma rcu_read_unlock(); 14271da177e4SLinus Torvalds 142827932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 142927932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 143027932742SMiklos Szeredi name, len, instantiate, 143127932742SMiklos Szeredi p, &fd) < 0) { 1432b835996fSDipankar Sarma rcu_read_lock(); 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds } 1435b835996fSDipankar Sarma rcu_read_lock(); 14361da177e4SLinus Torvalds } 1437b835996fSDipankar Sarma rcu_read_unlock(); 14381da177e4SLinus Torvalds put_files_struct(files); 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds out: 144199f89551SEric W. Biederman put_task_struct(p); 144299f89551SEric W. Biederman out_no_task: 14431da177e4SLinus Torvalds return retval; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 144627932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 144727932742SMiklos Szeredi struct nameidata *nd) 144827932742SMiklos Szeredi { 144927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 145027932742SMiklos Szeredi } 145127932742SMiklos Szeredi 145227932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 145327932742SMiklos Szeredi { 145427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 145527932742SMiklos Szeredi } 145627932742SMiklos Szeredi 145727932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 145827932742SMiklos Szeredi size_t len, loff_t *ppos) 145927932742SMiklos Szeredi { 146027932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 146127932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 146227932742SMiklos Szeredi if (!err) 146327932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 146427932742SMiklos Szeredi return err; 146527932742SMiklos Szeredi } 146627932742SMiklos Szeredi 146727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 146827932742SMiklos Szeredi .open = nonseekable_open, 146927932742SMiklos Szeredi .read = proc_fdinfo_read, 147027932742SMiklos Szeredi }; 147127932742SMiklos Szeredi 147200977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 14731da177e4SLinus Torvalds .read = generic_read_dir, 14741da177e4SLinus Torvalds .readdir = proc_readfd, 14751da177e4SLinus Torvalds }; 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds /* 14788948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 14798948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 14808948e11fSAlexey Dobriyan */ 14818948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 14828948e11fSAlexey Dobriyan struct nameidata *nd) 14838948e11fSAlexey Dobriyan { 14848948e11fSAlexey Dobriyan int rv; 14858948e11fSAlexey Dobriyan 14868948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 14878948e11fSAlexey Dobriyan if (rv == 0) 14888948e11fSAlexey Dobriyan return 0; 14898948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 14908948e11fSAlexey Dobriyan rv = 0; 14918948e11fSAlexey Dobriyan return rv; 14928948e11fSAlexey Dobriyan } 14938948e11fSAlexey Dobriyan 14948948e11fSAlexey Dobriyan /* 14951da177e4SLinus Torvalds * proc directories can do almost nothing.. 14961da177e4SLinus Torvalds */ 1497c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 14981da177e4SLinus Torvalds .lookup = proc_lookupfd, 14998948e11fSAlexey Dobriyan .permission = proc_fd_permission, 15006d76fa58SLinus Torvalds .setattr = proc_setattr, 15011da177e4SLinus Torvalds }; 15021da177e4SLinus Torvalds 150327932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 150427932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 150527932742SMiklos Szeredi { 150627932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 150727932742SMiklos Szeredi struct inode *inode; 150827932742SMiklos Szeredi struct proc_inode *ei; 150927932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 151027932742SMiklos Szeredi 151127932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 151227932742SMiklos Szeredi if (!inode) 151327932742SMiklos Szeredi goto out; 151427932742SMiklos Szeredi ei = PROC_I(inode); 151527932742SMiklos Szeredi ei->fd = fd; 151627932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 151727932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 151827932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 151927932742SMiklos Szeredi d_add(dentry, inode); 152027932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 152127932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 152227932742SMiklos Szeredi error = NULL; 152327932742SMiklos Szeredi 152427932742SMiklos Szeredi out: 152527932742SMiklos Szeredi return error; 152627932742SMiklos Szeredi } 152727932742SMiklos Szeredi 152827932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 152927932742SMiklos Szeredi struct dentry *dentry, 153027932742SMiklos Szeredi struct nameidata *nd) 153127932742SMiklos Szeredi { 153227932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 153327932742SMiklos Szeredi } 153427932742SMiklos Szeredi 153527932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 153627932742SMiklos Szeredi { 153727932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 153827932742SMiklos Szeredi proc_fdinfo_instantiate); 153927932742SMiklos Szeredi } 154027932742SMiklos Szeredi 154127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 154227932742SMiklos Szeredi .read = generic_read_dir, 154327932742SMiklos Szeredi .readdir = proc_readfdinfo, 154427932742SMiklos Szeredi }; 154527932742SMiklos Szeredi 154627932742SMiklos Szeredi /* 154727932742SMiklos Szeredi * proc directories can do almost nothing.. 154827932742SMiklos Szeredi */ 154927932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 155027932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 155127932742SMiklos Szeredi .setattr = proc_setattr, 155227932742SMiklos Szeredi }; 155327932742SMiklos Szeredi 155427932742SMiklos Szeredi 1555444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1556c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1557444ceed8SEric W. Biederman { 1558c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1559444ceed8SEric W. Biederman struct inode *inode; 1560444ceed8SEric W. Biederman struct proc_inode *ei; 1561444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1562444ceed8SEric W. Biederman 156361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1564444ceed8SEric W. Biederman if (!inode) 1565444ceed8SEric W. Biederman goto out; 1566444ceed8SEric W. Biederman 1567444ceed8SEric W. Biederman ei = PROC_I(inode); 1568444ceed8SEric W. Biederman inode->i_mode = p->mode; 1569444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1570444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1571444ceed8SEric W. Biederman if (p->iop) 1572444ceed8SEric W. Biederman inode->i_op = p->iop; 1573444ceed8SEric W. Biederman if (p->fop) 1574444ceed8SEric W. Biederman inode->i_fop = p->fop; 1575444ceed8SEric W. Biederman ei->op = p->op; 1576444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1577444ceed8SEric W. Biederman d_add(dentry, inode); 1578444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1579444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1580444ceed8SEric W. Biederman error = NULL; 1581444ceed8SEric W. Biederman out: 1582444ceed8SEric W. Biederman return error; 1583444ceed8SEric W. Biederman } 1584444ceed8SEric W. Biederman 15851da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 15861da177e4SLinus Torvalds struct dentry *dentry, 1587c5141e6dSEric Dumazet const struct pid_entry *ents, 15887bcd6b0eSEric W. Biederman unsigned int nents) 15891da177e4SLinus Torvalds { 15901da177e4SLinus Torvalds struct inode *inode; 1591cd6a3ce9SEric W. Biederman struct dentry *error; 159299f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1593c5141e6dSEric Dumazet const struct pid_entry *p, *last; 15941da177e4SLinus Torvalds 1595cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 15961da177e4SLinus Torvalds inode = NULL; 15971da177e4SLinus Torvalds 159899f89551SEric W. Biederman if (!task) 159999f89551SEric W. Biederman goto out_no_task; 16001da177e4SLinus Torvalds 160120cdc894SEric W. Biederman /* 160220cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 160320cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 160420cdc894SEric W. Biederman */ 16057bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16067bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 16071da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16081da177e4SLinus Torvalds continue; 16091da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16101da177e4SLinus Torvalds break; 16111da177e4SLinus Torvalds } 16127bcd6b0eSEric W. Biederman if (p > last) 16131da177e4SLinus Torvalds goto out; 16141da177e4SLinus Torvalds 1615444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 16161da177e4SLinus Torvalds out: 161799f89551SEric W. Biederman put_task_struct(task); 161899f89551SEric W. Biederman out_no_task: 1619cd6a3ce9SEric W. Biederman return error; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 1622c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1623c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 162461a28784SEric W. Biederman { 162561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 162661a28784SEric W. Biederman proc_pident_instantiate, task, p); 162761a28784SEric W. Biederman } 162861a28784SEric W. Biederman 162928a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 163028a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1631c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 163228a6d671SEric W. Biederman { 163328a6d671SEric W. Biederman int i; 163428a6d671SEric W. Biederman int pid; 16352fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 163628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 163728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1638c5141e6dSEric Dumazet const struct pid_entry *p, *last; 163928a6d671SEric W. Biederman ino_t ino; 164028a6d671SEric W. Biederman int ret; 164128a6d671SEric W. Biederman 164228a6d671SEric W. Biederman ret = -ENOENT; 164328a6d671SEric W. Biederman if (!task) 164461a28784SEric W. Biederman goto out_no_task; 164528a6d671SEric W. Biederman 164628a6d671SEric W. Biederman ret = 0; 164728a6d671SEric W. Biederman pid = task->pid; 164828a6d671SEric W. Biederman i = filp->f_pos; 164928a6d671SEric W. Biederman switch (i) { 165028a6d671SEric W. Biederman case 0: 165128a6d671SEric W. Biederman ino = inode->i_ino; 165228a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 165328a6d671SEric W. Biederman goto out; 165428a6d671SEric W. Biederman i++; 165528a6d671SEric W. Biederman filp->f_pos++; 165628a6d671SEric W. Biederman /* fall through */ 165728a6d671SEric W. Biederman case 1: 165828a6d671SEric W. Biederman ino = parent_ino(dentry); 165928a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 166028a6d671SEric W. Biederman goto out; 166128a6d671SEric W. Biederman i++; 166228a6d671SEric W. Biederman filp->f_pos++; 166328a6d671SEric W. Biederman /* fall through */ 166428a6d671SEric W. Biederman default: 166528a6d671SEric W. Biederman i -= 2; 166628a6d671SEric W. Biederman if (i >= nents) { 166728a6d671SEric W. Biederman ret = 1; 166828a6d671SEric W. Biederman goto out; 166928a6d671SEric W. Biederman } 167028a6d671SEric W. Biederman p = ents + i; 16717bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16727bcd6b0eSEric W. Biederman while (p <= last) { 167361a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 167428a6d671SEric W. Biederman goto out; 167528a6d671SEric W. Biederman filp->f_pos++; 167628a6d671SEric W. Biederman p++; 167728a6d671SEric W. Biederman } 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 168028a6d671SEric W. Biederman ret = 1; 168128a6d671SEric W. Biederman out: 168261a28784SEric W. Biederman put_task_struct(task); 168361a28784SEric W. Biederman out_no_task: 168428a6d671SEric W. Biederman return ret; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 168828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 168928a6d671SEric W. Biederman size_t count, loff_t *ppos) 169028a6d671SEric W. Biederman { 16912fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 169204ff9708SAl Viro char *p = NULL; 169328a6d671SEric W. Biederman ssize_t length; 169428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 169528a6d671SEric W. Biederman 169628a6d671SEric W. Biederman if (!task) 169704ff9708SAl Viro return -ESRCH; 169828a6d671SEric W. Biederman 169928a6d671SEric W. Biederman length = security_getprocattr(task, 17002fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 170104ff9708SAl Viro &p); 170228a6d671SEric W. Biederman put_task_struct(task); 170304ff9708SAl Viro if (length > 0) 170404ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 170504ff9708SAl Viro kfree(p); 170628a6d671SEric W. Biederman return length; 170728a6d671SEric W. Biederman } 170828a6d671SEric W. Biederman 170928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 171028a6d671SEric W. Biederman size_t count, loff_t *ppos) 171128a6d671SEric W. Biederman { 17122fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 171328a6d671SEric W. Biederman char *page; 171428a6d671SEric W. Biederman ssize_t length; 171528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 171628a6d671SEric W. Biederman 171728a6d671SEric W. Biederman length = -ESRCH; 171828a6d671SEric W. Biederman if (!task) 171928a6d671SEric W. Biederman goto out_no_task; 172028a6d671SEric W. Biederman if (count > PAGE_SIZE) 172128a6d671SEric W. Biederman count = PAGE_SIZE; 172228a6d671SEric W. Biederman 172328a6d671SEric W. Biederman /* No partial writes. */ 172428a6d671SEric W. Biederman length = -EINVAL; 172528a6d671SEric W. Biederman if (*ppos != 0) 172628a6d671SEric W. Biederman goto out; 172728a6d671SEric W. Biederman 172828a6d671SEric W. Biederman length = -ENOMEM; 172928a6d671SEric W. Biederman page = (char*)__get_free_page(GFP_USER); 173028a6d671SEric W. Biederman if (!page) 173128a6d671SEric W. Biederman goto out; 173228a6d671SEric W. Biederman 173328a6d671SEric W. Biederman length = -EFAULT; 173428a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 173528a6d671SEric W. Biederman goto out_free; 173628a6d671SEric W. Biederman 173728a6d671SEric W. Biederman length = security_setprocattr(task, 17382fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 173928a6d671SEric W. Biederman (void*)page, count); 174028a6d671SEric W. Biederman out_free: 174128a6d671SEric W. Biederman free_page((unsigned long) page); 174228a6d671SEric W. Biederman out: 174328a6d671SEric W. Biederman put_task_struct(task); 174428a6d671SEric W. Biederman out_no_task: 174528a6d671SEric W. Biederman return length; 174628a6d671SEric W. Biederman } 174728a6d671SEric W. Biederman 174800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 174928a6d671SEric W. Biederman .read = proc_pid_attr_read, 175028a6d671SEric W. Biederman .write = proc_pid_attr_write, 175128a6d671SEric W. Biederman }; 175228a6d671SEric W. Biederman 1753c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 175461a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 175561a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 175661a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 175761a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 175861a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 175961a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 176028a6d671SEric W. Biederman }; 176128a6d671SEric W. Biederman 176272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 17631da177e4SLinus Torvalds void * dirent, filldir_t filldir) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 176672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 176900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 17701da177e4SLinus Torvalds .read = generic_read_dir, 177172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 17721da177e4SLinus Torvalds }; 17731da177e4SLinus Torvalds 177472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 17751da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 17761da177e4SLinus Torvalds { 17777bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 17787bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 1781c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 178272d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 178399f89551SEric W. Biederman .getattr = pid_getattr, 17846d76fa58SLinus Torvalds .setattr = proc_setattr, 17851da177e4SLinus Torvalds }; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds #endif 17881da177e4SLinus Torvalds 17893cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 17903cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 17913cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 17923cb4a0bbSKawai, Hidehiro { 17933cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 17943cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 17953cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 17963cb4a0bbSKawai, Hidehiro size_t len; 17973cb4a0bbSKawai, Hidehiro int ret; 17983cb4a0bbSKawai, Hidehiro 17993cb4a0bbSKawai, Hidehiro if (!task) 18003cb4a0bbSKawai, Hidehiro return -ESRCH; 18013cb4a0bbSKawai, Hidehiro 18023cb4a0bbSKawai, Hidehiro ret = 0; 18033cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 18043cb4a0bbSKawai, Hidehiro if (mm) { 18053cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 18063cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 18073cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 18083cb4a0bbSKawai, Hidehiro mmput(mm); 18093cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 18103cb4a0bbSKawai, Hidehiro } 18113cb4a0bbSKawai, Hidehiro 18123cb4a0bbSKawai, Hidehiro put_task_struct(task); 18133cb4a0bbSKawai, Hidehiro 18143cb4a0bbSKawai, Hidehiro return ret; 18153cb4a0bbSKawai, Hidehiro } 18163cb4a0bbSKawai, Hidehiro 18173cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 18183cb4a0bbSKawai, Hidehiro const char __user *buf, 18193cb4a0bbSKawai, Hidehiro size_t count, 18203cb4a0bbSKawai, Hidehiro loff_t *ppos) 18213cb4a0bbSKawai, Hidehiro { 18223cb4a0bbSKawai, Hidehiro struct task_struct *task; 18233cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18243cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 18253cb4a0bbSKawai, Hidehiro unsigned int val; 18263cb4a0bbSKawai, Hidehiro int ret; 18273cb4a0bbSKawai, Hidehiro int i; 18283cb4a0bbSKawai, Hidehiro unsigned long mask; 18293cb4a0bbSKawai, Hidehiro 18303cb4a0bbSKawai, Hidehiro ret = -EFAULT; 18313cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 18323cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 18333cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 18343cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 18353cb4a0bbSKawai, Hidehiro goto out_no_task; 18363cb4a0bbSKawai, Hidehiro 18373cb4a0bbSKawai, Hidehiro ret = -EINVAL; 18383cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 18393cb4a0bbSKawai, Hidehiro if (*end == '\n') 18403cb4a0bbSKawai, Hidehiro end++; 18413cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 18423cb4a0bbSKawai, Hidehiro goto out_no_task; 18433cb4a0bbSKawai, Hidehiro 18443cb4a0bbSKawai, Hidehiro ret = -ESRCH; 18453cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 18463cb4a0bbSKawai, Hidehiro if (!task) 18473cb4a0bbSKawai, Hidehiro goto out_no_task; 18483cb4a0bbSKawai, Hidehiro 18493cb4a0bbSKawai, Hidehiro ret = end - buffer; 18503cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 18513cb4a0bbSKawai, Hidehiro if (!mm) 18523cb4a0bbSKawai, Hidehiro goto out_no_mm; 18533cb4a0bbSKawai, Hidehiro 18543cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 18553cb4a0bbSKawai, Hidehiro if (val & mask) 18563cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 18573cb4a0bbSKawai, Hidehiro else 18583cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 18593cb4a0bbSKawai, Hidehiro } 18603cb4a0bbSKawai, Hidehiro 18613cb4a0bbSKawai, Hidehiro mmput(mm); 18623cb4a0bbSKawai, Hidehiro out_no_mm: 18633cb4a0bbSKawai, Hidehiro put_task_struct(task); 18643cb4a0bbSKawai, Hidehiro out_no_task: 18653cb4a0bbSKawai, Hidehiro return ret; 18663cb4a0bbSKawai, Hidehiro } 18673cb4a0bbSKawai, Hidehiro 18683cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 18693cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 18703cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 18713cb4a0bbSKawai, Hidehiro }; 18723cb4a0bbSKawai, Hidehiro #endif 18733cb4a0bbSKawai, Hidehiro 18741da177e4SLinus Torvalds /* 18751da177e4SLinus Torvalds * /proc/self: 18761da177e4SLinus Torvalds */ 18771da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 18781da177e4SLinus Torvalds int buflen) 18791da177e4SLinus Torvalds { 18808578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 18811da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 18821da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 1885008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 18861da177e4SLinus Torvalds { 18878578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 18881da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1889008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 1892c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 18931da177e4SLinus Torvalds .readlink = proc_self_readlink, 18941da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 18951da177e4SLinus Torvalds }; 18961da177e4SLinus Torvalds 189728a6d671SEric W. Biederman /* 1898801199ceSEric W. Biederman * proc base 1899801199ceSEric W. Biederman * 1900801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 1901801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 1902801199ceSEric W. Biederman * describe something that is process related. 1903801199ceSEric W. Biederman */ 1904c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 190561a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 1906801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 1907801199ceSEric W. Biederman }; 1908801199ceSEric W. Biederman 1909801199ceSEric W. Biederman /* 1910801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 1911801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 1912801199ceSEric W. Biederman * due to the way we treat inodes. 1913801199ceSEric W. Biederman */ 1914801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 1915801199ceSEric W. Biederman { 1916801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 1917801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 1918801199ceSEric W. Biederman if (task) { 1919801199ceSEric W. Biederman put_task_struct(task); 1920801199ceSEric W. Biederman return 1; 1921801199ceSEric W. Biederman } 1922801199ceSEric W. Biederman d_drop(dentry); 1923801199ceSEric W. Biederman return 0; 1924801199ceSEric W. Biederman } 1925801199ceSEric W. Biederman 1926801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 1927801199ceSEric W. Biederman { 1928801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 1929801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 1930801199ceSEric W. Biederman }; 1931801199ceSEric W. Biederman 1932444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 1933c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1934801199ceSEric W. Biederman { 1935c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1936801199ceSEric W. Biederman struct inode *inode; 1937801199ceSEric W. Biederman struct proc_inode *ei; 1938444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1939801199ceSEric W. Biederman 1940801199ceSEric W. Biederman /* Allocate the inode */ 1941801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 1942801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 1943801199ceSEric W. Biederman if (!inode) 1944801199ceSEric W. Biederman goto out; 1945801199ceSEric W. Biederman 1946801199ceSEric W. Biederman /* Initialize the inode */ 1947801199ceSEric W. Biederman ei = PROC_I(inode); 1948801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1949801199ceSEric W. Biederman 1950801199ceSEric W. Biederman /* 1951801199ceSEric W. Biederman * grab the reference to the task. 1952801199ceSEric W. Biederman */ 19531a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 1954801199ceSEric W. Biederman if (!ei->pid) 1955801199ceSEric W. Biederman goto out_iput; 1956801199ceSEric W. Biederman 1957801199ceSEric W. Biederman inode->i_uid = 0; 1958801199ceSEric W. Biederman inode->i_gid = 0; 1959801199ceSEric W. Biederman inode->i_mode = p->mode; 1960801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 1961801199ceSEric W. Biederman inode->i_nlink = 2; 1962801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 1963801199ceSEric W. Biederman inode->i_size = 64; 1964801199ceSEric W. Biederman if (p->iop) 1965801199ceSEric W. Biederman inode->i_op = p->iop; 1966801199ceSEric W. Biederman if (p->fop) 1967801199ceSEric W. Biederman inode->i_fop = p->fop; 1968801199ceSEric W. Biederman ei->op = p->op; 1969801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 1970801199ceSEric W. Biederman d_add(dentry, inode); 1971801199ceSEric W. Biederman error = NULL; 1972801199ceSEric W. Biederman out: 1973801199ceSEric W. Biederman return error; 1974801199ceSEric W. Biederman out_iput: 1975801199ceSEric W. Biederman iput(inode); 1976801199ceSEric W. Biederman goto out; 1977801199ceSEric W. Biederman } 1978801199ceSEric W. Biederman 1979444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 1980444ceed8SEric W. Biederman { 1981444ceed8SEric W. Biederman struct dentry *error; 1982444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1983c5141e6dSEric Dumazet const struct pid_entry *p, *last; 1984444ceed8SEric W. Biederman 1985444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 1986444ceed8SEric W. Biederman 1987444ceed8SEric W. Biederman if (!task) 1988444ceed8SEric W. Biederman goto out_no_task; 1989444ceed8SEric W. Biederman 1990444ceed8SEric W. Biederman /* Lookup the directory entry */ 19917bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 19927bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 1993444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 1994444ceed8SEric W. Biederman continue; 1995444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 1996444ceed8SEric W. Biederman break; 1997444ceed8SEric W. Biederman } 19987bcd6b0eSEric W. Biederman if (p > last) 1999444ceed8SEric W. Biederman goto out; 2000444ceed8SEric W. Biederman 2001444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2002444ceed8SEric W. Biederman 2003444ceed8SEric W. Biederman out: 2004444ceed8SEric W. Biederman put_task_struct(task); 2005444ceed8SEric W. Biederman out_no_task: 2006444ceed8SEric W. Biederman return error; 2007444ceed8SEric W. Biederman } 2008444ceed8SEric W. Biederman 2009c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2010c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 201161a28784SEric W. Biederman { 201261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 201361a28784SEric W. Biederman proc_base_instantiate, task, p); 201461a28784SEric W. Biederman } 201561a28784SEric W. Biederman 2016aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2017aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2018aba76fdbSAndrew Morton { 2019aba76fdbSAndrew Morton return sprintf(buffer, 20204b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2021aba76fdbSAndrew Morton "rchar: %llu\n" 2022aba76fdbSAndrew Morton "wchar: %llu\n" 2023aba76fdbSAndrew Morton "syscr: %llu\n" 2024aba76fdbSAndrew Morton "syscw: %llu\n" 20254b98d11bSAlexey Dobriyan #endif 2026aba76fdbSAndrew Morton "read_bytes: %llu\n" 2027aba76fdbSAndrew Morton "write_bytes: %llu\n" 2028aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 20294b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2030aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2031aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2032aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2033aba76fdbSAndrew Morton (unsigned long long)task->syscw, 20344b98d11bSAlexey Dobriyan #endif 2035aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2036aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2037aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2038aba76fdbSAndrew Morton } 2039aba76fdbSAndrew Morton #endif 2040aba76fdbSAndrew Morton 2041801199ceSEric W. Biederman /* 204228a6d671SEric W. Biederman * Thread groups 204328a6d671SEric W. Biederman */ 204400977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2045c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 204620cdc894SEric W. Biederman 2047c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 204861a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 204961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 205027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 205161a28784SEric W. Biederman INF("environ", S_IRUSR, pid_environ), 205261a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 205361a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 205443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 205543ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 205643ae34cbSIngo Molnar #endif 205761a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 205861a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 205961a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 206061a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 206128a6d671SEric W. Biederman #ifdef CONFIG_NUMA 206261a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 206328a6d671SEric W. Biederman #endif 206461a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 206561a28784SEric W. Biederman LNK("cwd", cwd), 206661a28784SEric W. Biederman LNK("root", root), 206761a28784SEric W. Biederman LNK("exe", exe), 206861a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 206961a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 207028a6d671SEric W. Biederman #ifdef CONFIG_MMU 2071b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 207261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 207328a6d671SEric W. Biederman #endif 207428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 207572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 207628a6d671SEric W. Biederman #endif 207728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 207861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 207928a6d671SEric W. Biederman #endif 208028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 208161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 208228a6d671SEric W. Biederman #endif 208328a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 208461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 208528a6d671SEric W. Biederman #endif 208661a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 208761a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 208828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 208961a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 209028a6d671SEric W. Biederman #endif 2091f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2092f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2093f4f154fdSAkinobu Mita #endif 20943cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 20953cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 20963cb4a0bbSKawai, Hidehiro #endif 2097aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2098aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2099aba76fdbSAndrew Morton #endif 210028a6d671SEric W. Biederman }; 210128a6d671SEric W. Biederman 210228a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 210328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 210428a6d671SEric W. Biederman { 210528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 210628a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 210728a6d671SEric W. Biederman } 210828a6d671SEric W. Biederman 210900977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 211028a6d671SEric W. Biederman .read = generic_read_dir, 211128a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 211228a6d671SEric W. Biederman }; 211328a6d671SEric W. Biederman 211428a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 21157bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21167bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 211728a6d671SEric W. Biederman } 211828a6d671SEric W. Biederman 2119c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 212028a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 212128a6d671SEric W. Biederman .getattr = pid_getattr, 212228a6d671SEric W. Biederman .setattr = proc_setattr, 212328a6d671SEric W. Biederman }; 212428a6d671SEric W. Biederman 21251da177e4SLinus Torvalds /** 212648e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 21271da177e4SLinus Torvalds * 212848e6484dSEric W. Biederman * @task: task that should be flushed. 21291da177e4SLinus Torvalds * 213048e6484dSEric W. Biederman * Looks in the dcache for 213148e6484dSEric W. Biederman * /proc/@pid 213248e6484dSEric W. Biederman * /proc/@tgid/task/@pid 213348e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 213448e6484dSEric W. Biederman * from the dcache. 213548e6484dSEric W. Biederman * 213648e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 213748e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 213848e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 213948e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 214048e6484dSEric W. Biederman * dcache entries at process exit time. 214148e6484dSEric W. Biederman * 214248e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 214348e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 214448e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 21451da177e4SLinus Torvalds */ 214648e6484dSEric W. Biederman void proc_flush_task(struct task_struct *task) 21471da177e4SLinus Torvalds { 214848e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 21498578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 215048e6484dSEric W. Biederman struct qstr name; 21511da177e4SLinus Torvalds 215248e6484dSEric W. Biederman name.name = buf; 215348e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 215448e6484dSEric W. Biederman dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); 215548e6484dSEric W. Biederman if (dentry) { 215648e6484dSEric W. Biederman shrink_dcache_parent(dentry); 215748e6484dSEric W. Biederman d_drop(dentry); 215848e6484dSEric W. Biederman dput(dentry); 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 216148e6484dSEric W. Biederman if (thread_group_leader(task)) 216248e6484dSEric W. Biederman goto out; 21631da177e4SLinus Torvalds 216448e6484dSEric W. Biederman name.name = buf; 216548e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); 216648e6484dSEric W. Biederman leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); 216748e6484dSEric W. Biederman if (!leader) 216848e6484dSEric W. Biederman goto out; 216948e6484dSEric W. Biederman 217048e6484dSEric W. Biederman name.name = "task"; 217148e6484dSEric W. Biederman name.len = strlen(name.name); 217248e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 217348e6484dSEric W. Biederman if (!dir) 217448e6484dSEric W. Biederman goto out_put_leader; 217548e6484dSEric W. Biederman 217648e6484dSEric W. Biederman name.name = buf; 217748e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 217848e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 217948e6484dSEric W. Biederman if (dentry) { 218048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 218148e6484dSEric W. Biederman d_drop(dentry); 218248e6484dSEric W. Biederman dput(dentry); 21831da177e4SLinus Torvalds } 218448e6484dSEric W. Biederman 218548e6484dSEric W. Biederman dput(dir); 218648e6484dSEric W. Biederman out_put_leader: 218748e6484dSEric W. Biederman dput(leader); 218848e6484dSEric W. Biederman out: 218948e6484dSEric W. Biederman return; 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 21929711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 21939711ef99SAdrian Bunk struct dentry * dentry, 2194c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2195444ceed8SEric W. Biederman { 2196444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2197444ceed8SEric W. Biederman struct inode *inode; 2198444ceed8SEric W. Biederman 219961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2200444ceed8SEric W. Biederman if (!inode) 2201444ceed8SEric W. Biederman goto out; 2202444ceed8SEric W. Biederman 2203444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2204444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2205444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2206444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 220727932742SMiklos Szeredi inode->i_nlink = 5; 2208444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2209444ceed8SEric W. Biederman inode->i_nlink += 1; 2210444ceed8SEric W. Biederman #endif 2211444ceed8SEric W. Biederman 2212444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2213444ceed8SEric W. Biederman 2214444ceed8SEric W. Biederman d_add(dentry, inode); 2215444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2216444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2217444ceed8SEric W. Biederman error = NULL; 2218444ceed8SEric W. Biederman out: 2219444ceed8SEric W. Biederman return error; 2220444ceed8SEric W. Biederman } 2221444ceed8SEric W. Biederman 22221da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 22231da177e4SLinus Torvalds { 2224cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 22251da177e4SLinus Torvalds struct task_struct *task; 22261da177e4SLinus Torvalds unsigned tgid; 22271da177e4SLinus Torvalds 2228801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2229801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2230801199ceSEric W. Biederman goto out; 2231801199ceSEric W. Biederman 22321da177e4SLinus Torvalds tgid = name_to_int(dentry); 22331da177e4SLinus Torvalds if (tgid == ~0U) 22341da177e4SLinus Torvalds goto out; 22351da177e4SLinus Torvalds 2236de758734SEric W. Biederman rcu_read_lock(); 22371da177e4SLinus Torvalds task = find_task_by_pid(tgid); 22381da177e4SLinus Torvalds if (task) 22391da177e4SLinus Torvalds get_task_struct(task); 2240de758734SEric W. Biederman rcu_read_unlock(); 22411da177e4SLinus Torvalds if (!task) 22421da177e4SLinus Torvalds goto out; 22431da177e4SLinus Torvalds 2244444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 224548e6484dSEric W. Biederman put_task_struct(task); 22461da177e4SLinus Torvalds out: 2247cd6a3ce9SEric W. Biederman return result; 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /* 22510804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 22520bc58a91SEric W. Biederman * 22531da177e4SLinus Torvalds */ 22540804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 22551da177e4SLinus Torvalds { 22560804ef4bSEric W. Biederman struct task_struct *task; 22570804ef4bSEric W. Biederman struct pid *pid; 22581da177e4SLinus Torvalds 22590804ef4bSEric W. Biederman rcu_read_lock(); 22600804ef4bSEric W. Biederman retry: 22610804ef4bSEric W. Biederman task = NULL; 22620804ef4bSEric W. Biederman pid = find_ge_pid(tgid); 22630804ef4bSEric W. Biederman if (pid) { 22640804ef4bSEric W. Biederman tgid = pid->nr + 1; 22650804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 22660804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 22670804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 22680804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 22690804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 22700804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 22710804ef4bSEric W. Biederman * 22720804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 22730804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 22740804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 22750804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 22760804ef4bSEric W. Biederman * As we don't care in the case of readdir. 22770bc58a91SEric W. Biederman */ 22780804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 22790804ef4bSEric W. Biederman goto retry; 22800804ef4bSEric W. Biederman get_task_struct(task); 22811da177e4SLinus Torvalds } 2282454cc105SEric W. Biederman rcu_read_unlock(); 22830804ef4bSEric W. Biederman return task; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22867bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 22871da177e4SLinus Torvalds 228861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 228961a28784SEric W. Biederman struct task_struct *task, int tgid) 229061a28784SEric W. Biederman { 229161a28784SEric W. Biederman char name[PROC_NUMBUF]; 229261a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 229361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 229461a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 229561a28784SEric W. Biederman } 229661a28784SEric W. Biederman 22971da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 22981da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 22991da177e4SLinus Torvalds { 23001da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 23012fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 23020bc58a91SEric W. Biederman struct task_struct *task; 23030bc58a91SEric W. Biederman int tgid; 23041da177e4SLinus Torvalds 230561a28784SEric W. Biederman if (!reaper) 230661a28784SEric W. Biederman goto out_no_task; 230761a28784SEric W. Biederman 23087bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2309c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 231061a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2311801199ceSEric W. Biederman goto out; 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23140804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 23150804ef4bSEric W. Biederman for (task = next_tgid(tgid); 23160bc58a91SEric W. Biederman task; 23170804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 23180bc58a91SEric W. Biederman tgid = task->pid; 23190804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 232061a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 23210bc58a91SEric W. Biederman put_task_struct(task); 23220804ef4bSEric W. Biederman goto out; 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds } 23250804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 23260804ef4bSEric W. Biederman out: 232761a28784SEric W. Biederman put_task_struct(reaper); 232861a28784SEric W. Biederman out_no_task: 23291da177e4SLinus Torvalds return 0; 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23320bc58a91SEric W. Biederman /* 233328a6d671SEric W. Biederman * Tasks 233428a6d671SEric W. Biederman */ 2335c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 233661a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 233727932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 233861a28784SEric W. Biederman INF("environ", S_IRUSR, pid_environ), 233961a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 234061a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 234143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 234243ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 234343ae34cbSIngo Molnar #endif 234461a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 234561a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 234661a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 234761a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 234828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 234961a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 235028a6d671SEric W. Biederman #endif 235161a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 235261a28784SEric W. Biederman LNK("cwd", cwd), 235361a28784SEric W. Biederman LNK("root", root), 235461a28784SEric W. Biederman LNK("exe", exe), 235561a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 235628a6d671SEric W. Biederman #ifdef CONFIG_MMU 2357b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 235861a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 235928a6d671SEric W. Biederman #endif 236028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 236172d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 236228a6d671SEric W. Biederman #endif 236328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 236461a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 236528a6d671SEric W. Biederman #endif 236628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 236761a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 236828a6d671SEric W. Biederman #endif 236928a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 237061a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 237128a6d671SEric W. Biederman #endif 237261a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 237361a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 237428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 237561a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 237628a6d671SEric W. Biederman #endif 2377f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2378f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2379f4f154fdSAkinobu Mita #endif 238028a6d671SEric W. Biederman }; 238128a6d671SEric W. Biederman 238228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 238328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 238428a6d671SEric W. Biederman { 238528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 238628a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 238728a6d671SEric W. Biederman } 238828a6d671SEric W. Biederman 238928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 23907bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 23917bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 239228a6d671SEric W. Biederman } 239328a6d671SEric W. Biederman 239400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 239528a6d671SEric W. Biederman .read = generic_read_dir, 239628a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 239728a6d671SEric W. Biederman }; 239828a6d671SEric W. Biederman 2399c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 240028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 240128a6d671SEric W. Biederman .getattr = pid_getattr, 240228a6d671SEric W. Biederman .setattr = proc_setattr, 240328a6d671SEric W. Biederman }; 240428a6d671SEric W. Biederman 2405444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2406c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2407444ceed8SEric W. Biederman { 2408444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2409444ceed8SEric W. Biederman struct inode *inode; 241061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2411444ceed8SEric W. Biederman 2412444ceed8SEric W. Biederman if (!inode) 2413444ceed8SEric W. Biederman goto out; 2414444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2415444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2416444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2417444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 241827932742SMiklos Szeredi inode->i_nlink = 4; 2419444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2420444ceed8SEric W. Biederman inode->i_nlink += 1; 2421444ceed8SEric W. Biederman #endif 2422444ceed8SEric W. Biederman 2423444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2424444ceed8SEric W. Biederman 2425444ceed8SEric W. Biederman d_add(dentry, inode); 2426444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2427444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2428444ceed8SEric W. Biederman error = NULL; 2429444ceed8SEric W. Biederman out: 2430444ceed8SEric W. Biederman return error; 2431444ceed8SEric W. Biederman } 2432444ceed8SEric W. Biederman 243328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 243428a6d671SEric W. Biederman { 243528a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 243628a6d671SEric W. Biederman struct task_struct *task; 243728a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 243828a6d671SEric W. Biederman unsigned tid; 243928a6d671SEric W. Biederman 244028a6d671SEric W. Biederman if (!leader) 244128a6d671SEric W. Biederman goto out_no_task; 244228a6d671SEric W. Biederman 244328a6d671SEric W. Biederman tid = name_to_int(dentry); 244428a6d671SEric W. Biederman if (tid == ~0U) 244528a6d671SEric W. Biederman goto out; 244628a6d671SEric W. Biederman 244728a6d671SEric W. Biederman rcu_read_lock(); 244828a6d671SEric W. Biederman task = find_task_by_pid(tid); 244928a6d671SEric W. Biederman if (task) 245028a6d671SEric W. Biederman get_task_struct(task); 245128a6d671SEric W. Biederman rcu_read_unlock(); 245228a6d671SEric W. Biederman if (!task) 245328a6d671SEric W. Biederman goto out; 245428a6d671SEric W. Biederman if (leader->tgid != task->tgid) 245528a6d671SEric W. Biederman goto out_drop_task; 245628a6d671SEric W. Biederman 2457444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 245828a6d671SEric W. Biederman out_drop_task: 245928a6d671SEric W. Biederman put_task_struct(task); 246028a6d671SEric W. Biederman out: 246128a6d671SEric W. Biederman put_task_struct(leader); 246228a6d671SEric W. Biederman out_no_task: 246328a6d671SEric W. Biederman return result; 246428a6d671SEric W. Biederman } 246528a6d671SEric W. Biederman 246628a6d671SEric W. Biederman /* 24670bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 24680bc58a91SEric W. Biederman * 24690bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 24700bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 24710bc58a91SEric W. Biederman * directory we have more work todo. 24720bc58a91SEric W. Biederman * 24730bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 24740bc58a91SEric W. Biederman * 24750bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 24760bc58a91SEric W. Biederman * threads past it. 24770bc58a91SEric W. Biederman */ 2478cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2479cc288738SEric W. Biederman int tid, int nr) 24800bc58a91SEric W. Biederman { 2481a872ff0cSOleg Nesterov struct task_struct *pos; 24820bc58a91SEric W. Biederman 2483cc288738SEric W. Biederman rcu_read_lock(); 24840bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 24850bc58a91SEric W. Biederman if (tid && (nr > 0)) { 24860bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2487a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2488a872ff0cSOleg Nesterov goto found; 24890bc58a91SEric W. Biederman } 24900bc58a91SEric W. Biederman 24910bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 24920bc58a91SEric W. Biederman pos = NULL; 2493a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2494a872ff0cSOleg Nesterov goto out; 2495a872ff0cSOleg Nesterov 2496a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2497a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2498a872ff0cSOleg Nesterov */ 2499a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2500a872ff0cSOleg Nesterov pos = next_thread(pos); 2501a872ff0cSOleg Nesterov if (pos == leader) { 2502a872ff0cSOleg Nesterov pos = NULL; 2503a872ff0cSOleg Nesterov goto out; 2504a872ff0cSOleg Nesterov } 2505a872ff0cSOleg Nesterov } 2506a872ff0cSOleg Nesterov found: 2507a872ff0cSOleg Nesterov get_task_struct(pos); 2508a872ff0cSOleg Nesterov out: 2509cc288738SEric W. Biederman rcu_read_unlock(); 25100bc58a91SEric W. Biederman return pos; 25110bc58a91SEric W. Biederman } 25120bc58a91SEric W. Biederman 25130bc58a91SEric W. Biederman /* 25140bc58a91SEric W. Biederman * Find the next thread in the thread list. 25150bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 25160bc58a91SEric W. Biederman * 25170bc58a91SEric W. Biederman * The reference to the input task_struct is released. 25180bc58a91SEric W. Biederman */ 25190bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 25200bc58a91SEric W. Biederman { 2521c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2522cc288738SEric W. Biederman rcu_read_lock(); 2523c1df7fb8SOleg Nesterov if (pid_alive(start)) { 25240bc58a91SEric W. Biederman pos = next_thread(start); 2525c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 25260bc58a91SEric W. Biederman pos = NULL; 2527c1df7fb8SOleg Nesterov else 2528c1df7fb8SOleg Nesterov get_task_struct(pos); 2529c1df7fb8SOleg Nesterov } 2530cc288738SEric W. Biederman rcu_read_unlock(); 25310bc58a91SEric W. Biederman put_task_struct(start); 25320bc58a91SEric W. Biederman return pos; 25330bc58a91SEric W. Biederman } 25340bc58a91SEric W. Biederman 253561a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 253661a28784SEric W. Biederman struct task_struct *task, int tid) 253761a28784SEric W. Biederman { 253861a28784SEric W. Biederman char name[PROC_NUMBUF]; 253961a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 254061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 254161a28784SEric W. Biederman proc_task_instantiate, task, NULL); 254261a28784SEric W. Biederman } 254361a28784SEric W. Biederman 25441da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 25451da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 25461da177e4SLinus Torvalds { 25472fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 25481da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25497d895244SGuillaume Chazarain struct task_struct *leader = NULL; 25500bc58a91SEric W. Biederman struct task_struct *task; 25511da177e4SLinus Torvalds int retval = -ENOENT; 25521da177e4SLinus Torvalds ino_t ino; 25530bc58a91SEric W. Biederman int tid; 25541da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 25551da177e4SLinus Torvalds 25567d895244SGuillaume Chazarain task = get_proc_task(inode); 25577d895244SGuillaume Chazarain if (!task) 25587d895244SGuillaume Chazarain goto out_no_task; 25597d895244SGuillaume Chazarain rcu_read_lock(); 25607d895244SGuillaume Chazarain if (pid_alive(task)) { 25617d895244SGuillaume Chazarain leader = task->group_leader; 25627d895244SGuillaume Chazarain get_task_struct(leader); 25637d895244SGuillaume Chazarain } 25647d895244SGuillaume Chazarain rcu_read_unlock(); 25657d895244SGuillaume Chazarain put_task_struct(task); 256699f89551SEric W. Biederman if (!leader) 256799f89551SEric W. Biederman goto out_no_task; 25681da177e4SLinus Torvalds retval = 0; 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds switch (pos) { 25711da177e4SLinus Torvalds case 0: 25721da177e4SLinus Torvalds ino = inode->i_ino; 25731da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 25741da177e4SLinus Torvalds goto out; 25751da177e4SLinus Torvalds pos++; 25761da177e4SLinus Torvalds /* fall through */ 25771da177e4SLinus Torvalds case 1: 25781da177e4SLinus Torvalds ino = parent_ino(dentry); 25791da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 25801da177e4SLinus Torvalds goto out; 25811da177e4SLinus Torvalds pos++; 25821da177e4SLinus Torvalds /* fall through */ 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25850bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 25860bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 25870bc58a91SEric W. Biederman */ 25880bc58a91SEric W. Biederman tid = filp->f_version; 25890bc58a91SEric W. Biederman filp->f_version = 0; 25900bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 25910bc58a91SEric W. Biederman task; 25920bc58a91SEric W. Biederman task = next_tid(task), pos++) { 25930bc58a91SEric W. Biederman tid = task->pid; 259461a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 25950bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 25960bc58a91SEric W. Biederman * pid for the next readir call */ 25970bc58a91SEric W. Biederman filp->f_version = tid; 25980bc58a91SEric W. Biederman put_task_struct(task); 25991da177e4SLinus Torvalds break; 26000bc58a91SEric W. Biederman } 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds out: 26031da177e4SLinus Torvalds filp->f_pos = pos; 260499f89551SEric W. Biederman put_task_struct(leader); 260599f89551SEric W. Biederman out_no_task: 26061da177e4SLinus Torvalds return retval; 26071da177e4SLinus Torvalds } 26086e66b52bSEric W. Biederman 26096e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 26106e66b52bSEric W. Biederman { 26116e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 261299f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 26136e66b52bSEric W. Biederman generic_fillattr(inode, stat); 26146e66b52bSEric W. Biederman 261599f89551SEric W. Biederman if (p) { 261699f89551SEric W. Biederman rcu_read_lock(); 261799f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 261899f89551SEric W. Biederman rcu_read_unlock(); 261999f89551SEric W. Biederman put_task_struct(p); 26206e66b52bSEric W. Biederman } 26216e66b52bSEric W. Biederman 26226e66b52bSEric W. Biederman return 0; 26236e66b52bSEric W. Biederman } 262428a6d671SEric W. Biederman 2625c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 262628a6d671SEric W. Biederman .lookup = proc_task_lookup, 262728a6d671SEric W. Biederman .getattr = proc_task_getattr, 262828a6d671SEric W. Biederman .setattr = proc_setattr, 262928a6d671SEric W. Biederman }; 263028a6d671SEric W. Biederman 263100977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 263228a6d671SEric W. Biederman .read = generic_read_dir, 263328a6d671SEric W. Biederman .readdir = proc_task_readdir, 263428a6d671SEric W. Biederman }; 2635