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_cmdline(struct task_struct *task, char * buffer) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int res = 0; 2051da177e4SLinus Torvalds unsigned int len; 2061da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2071da177e4SLinus Torvalds if (!mm) 2081da177e4SLinus Torvalds goto out; 2091da177e4SLinus Torvalds if (!mm->arg_end) 2101da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (len > PAGE_SIZE) 2151da177e4SLinus Torvalds len = PAGE_SIZE; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2201da177e4SLinus Torvalds // assume application is using setproctitle(3). 2211da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2221da177e4SLinus Torvalds len = strnlen(buffer, res); 2231da177e4SLinus Torvalds if (len < res) { 2241da177e4SLinus Torvalds res = len; 2251da177e4SLinus Torvalds } else { 2261da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2271da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2281da177e4SLinus Torvalds len = PAGE_SIZE - res; 2291da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2301da177e4SLinus Torvalds res = strnlen(buffer, res); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds out_mm: 2341da177e4SLinus Torvalds mmput(mm); 2351da177e4SLinus Torvalds out: 2361da177e4SLinus Torvalds return res; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds int res = 0; 2421da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2431da177e4SLinus Torvalds if (mm) { 2441da177e4SLinus Torvalds unsigned int nwords = 0; 2451da177e4SLinus Torvalds do 2461da177e4SLinus Torvalds nwords += 2; 2471da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2481da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2491da177e4SLinus Torvalds if (res > PAGE_SIZE) 2501da177e4SLinus Torvalds res = PAGE_SIZE; 2511da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2521da177e4SLinus Torvalds mmput(mm); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds return res; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2611da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2641da177e4SLinus Torvalds { 265ffb45122SAlexey Dobriyan unsigned long wchan; 2669281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds wchan = get_wchan(task); 2691da177e4SLinus Torvalds 2709d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2711da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2729d65cb4aSAlexey Dobriyan else 2739d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2781da177e4SLinus Torvalds /* 2791da177e4SLinus Torvalds * Provides /proc/PID/schedstat 2801da177e4SLinus Torvalds */ 2811da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 2821da177e4SLinus Torvalds { 283172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 2841da177e4SLinus Torvalds task->sched_info.cpu_time, 2851da177e4SLinus Torvalds task->sched_info.run_delay, 2862d72376bSIngo Molnar task->sched_info.pcount); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds #endif 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* The badness from the OOM killer */ 2911da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 2921da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds unsigned long points; 2951da177e4SLinus Torvalds struct timespec uptime; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 29819c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 2991da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 30019c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3011da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds /************************************************************************/ 3051da177e4SLinus Torvalds /* Here the fs part begins */ 3061da177e4SLinus Torvalds /************************************************************************/ 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* permission checks */ 309778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3101da177e4SLinus Torvalds { 311778c1144SEric W. Biederman struct task_struct *task; 312778c1144SEric W. Biederman int allowed = 0; 313df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 314df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 315df26c40eSEric W. Biederman * information. 316778c1144SEric W. Biederman */ 317778c1144SEric W. Biederman task = get_proc_task(inode); 318df26c40eSEric W. Biederman if (task) { 319778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 320778c1144SEric W. Biederman put_task_struct(task); 321df26c40eSEric W. Biederman } 322778c1144SEric W. Biederman return allowed; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3256d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 3266d76fa58SLinus Torvalds { 3276d76fa58SLinus Torvalds int error; 3286d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 3296d76fa58SLinus Torvalds 3306d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 3316d76fa58SLinus Torvalds return -EPERM; 3326d76fa58SLinus Torvalds 3336d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 3346d76fa58SLinus Torvalds if (!error) 3356d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 3366d76fa58SLinus Torvalds return error; 3376d76fa58SLinus Torvalds } 3386d76fa58SLinus Torvalds 339c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 3406d76fa58SLinus Torvalds .setattr = proc_setattr, 3416d76fa58SLinus Torvalds }; 3426d76fa58SLinus Torvalds 3431da177e4SLinus Torvalds extern struct seq_operations mounts_op; 3445addc5ddSAl Viro struct proc_mounts { 3455addc5ddSAl Viro struct seq_file m; 3465addc5ddSAl Viro int event; 3475addc5ddSAl Viro }; 3485addc5ddSAl Viro 3491da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 3501da177e4SLinus Torvalds { 35199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 3526b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 3535addc5ddSAl Viro struct proc_mounts *p; 3545addc5ddSAl Viro int ret = -EINVAL; 3555addc5ddSAl Viro 35699f89551SEric W. Biederman if (task) { 3571da177e4SLinus Torvalds task_lock(task); 358863c4702SAlexey Dobriyan if (task->nsproxy) { 3596b3286edSKirill Korotaev ns = task->nsproxy->mnt_ns; 3606b3286edSKirill Korotaev if (ns) 3616b3286edSKirill Korotaev get_mnt_ns(ns); 362863c4702SAlexey Dobriyan } 3631da177e4SLinus Torvalds task_unlock(task); 36499f89551SEric W. Biederman put_task_struct(task); 36599f89551SEric W. Biederman } 3661da177e4SLinus Torvalds 3676b3286edSKirill Korotaev if (ns) { 3685addc5ddSAl Viro ret = -ENOMEM; 3695addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 3705addc5ddSAl Viro if (p) { 3715addc5ddSAl Viro file->private_data = &p->m; 3725addc5ddSAl Viro ret = seq_open(file, &mounts_op); 3735addc5ddSAl Viro if (!ret) { 3746b3286edSKirill Korotaev p->m.private = ns; 3756b3286edSKirill Korotaev p->event = ns->event; 3765addc5ddSAl Viro return 0; 3771da177e4SLinus Torvalds } 3785addc5ddSAl Viro kfree(p); 3795addc5ddSAl Viro } 3806b3286edSKirill Korotaev put_mnt_ns(ns); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds return ret; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds struct seq_file *m = file->private_data; 3886b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 3896b3286edSKirill Korotaev put_mnt_ns(ns); 3901da177e4SLinus Torvalds return seq_release(inode, file); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3935addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 3945addc5ddSAl Viro { 3955addc5ddSAl Viro struct proc_mounts *p = file->private_data; 3966b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 3975addc5ddSAl Viro unsigned res = 0; 3985addc5ddSAl Viro 3995addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4005addc5ddSAl Viro 4015addc5ddSAl Viro spin_lock(&vfsmount_lock); 4025addc5ddSAl Viro if (p->event != ns->event) { 4035addc5ddSAl Viro p->event = ns->event; 4045addc5ddSAl Viro res = POLLERR; 4055addc5ddSAl Viro } 4065addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4075addc5ddSAl Viro 4085addc5ddSAl Viro return res; 4095addc5ddSAl Viro } 4105addc5ddSAl Viro 41100977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4121da177e4SLinus Torvalds .open = mounts_open, 4131da177e4SLinus Torvalds .read = seq_read, 4141da177e4SLinus Torvalds .llseek = seq_lseek, 4151da177e4SLinus Torvalds .release = mounts_release, 4165addc5ddSAl Viro .poll = mounts_poll, 4171da177e4SLinus Torvalds }; 4181da177e4SLinus Torvalds 419b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 420b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 421b4629fe2SChuck Lever { 422b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 423b4629fe2SChuck Lever 424b4629fe2SChuck Lever if (!ret) { 425b4629fe2SChuck Lever struct seq_file *m = file->private_data; 4266b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 42799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 42899f89551SEric W. Biederman 42999f89551SEric W. Biederman if (task) { 430b4629fe2SChuck Lever task_lock(task); 431701e054eSVasily Tarasov if (task->nsproxy) 4326b3286edSKirill Korotaev mnt_ns = task->nsproxy->mnt_ns; 4336b3286edSKirill Korotaev if (mnt_ns) 4346b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 435b4629fe2SChuck Lever task_unlock(task); 43699f89551SEric W. Biederman put_task_struct(task); 43799f89551SEric W. Biederman } 438b4629fe2SChuck Lever 4396b3286edSKirill Korotaev if (mnt_ns) 4406b3286edSKirill Korotaev m->private = mnt_ns; 441b4629fe2SChuck Lever else { 442b4629fe2SChuck Lever seq_release(inode, file); 443b4629fe2SChuck Lever ret = -EINVAL; 444b4629fe2SChuck Lever } 445b4629fe2SChuck Lever } 446b4629fe2SChuck Lever return ret; 447b4629fe2SChuck Lever } 448b4629fe2SChuck Lever 44900977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 450b4629fe2SChuck Lever .open = mountstats_open, 451b4629fe2SChuck Lever .read = seq_read, 452b4629fe2SChuck Lever .llseek = seq_lseek, 453b4629fe2SChuck Lever .release = mounts_release, 454b4629fe2SChuck Lever }; 455b4629fe2SChuck Lever 4561da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 4591da177e4SLinus Torvalds size_t count, loff_t *ppos) 4601da177e4SLinus Torvalds { 4612fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 4621da177e4SLinus Torvalds unsigned long page; 4631da177e4SLinus Torvalds ssize_t length; 46499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 46599f89551SEric W. Biederman 46699f89551SEric W. Biederman length = -ESRCH; 46799f89551SEric W. Biederman if (!task) 46899f89551SEric W. Biederman goto out_no_task; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 4711da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 47299f89551SEric W. Biederman 47399f89551SEric W. Biederman length = -ENOMEM; 474e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 47599f89551SEric W. Biederman goto out; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds if (length >= 0) 4801da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 4811da177e4SLinus Torvalds free_page(page); 48299f89551SEric W. Biederman out: 48399f89551SEric W. Biederman put_task_struct(task); 48499f89551SEric W. Biederman out_no_task: 4851da177e4SLinus Torvalds return length; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 48800977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 4891da177e4SLinus Torvalds .read = proc_info_read, 4901da177e4SLinus Torvalds }; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 4951da177e4SLinus Torvalds return 0; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 4991da177e4SLinus Torvalds size_t count, loff_t *ppos) 5001da177e4SLinus Torvalds { 5012fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5021da177e4SLinus Torvalds char *page; 5031da177e4SLinus Torvalds unsigned long src = *ppos; 5041da177e4SLinus Torvalds int ret = -ESRCH; 5051da177e4SLinus Torvalds struct mm_struct *mm; 5061da177e4SLinus Torvalds 50799f89551SEric W. Biederman if (!task) 50899f89551SEric W. Biederman goto out_no_task; 50999f89551SEric W. Biederman 510ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5111da177e4SLinus Torvalds goto out; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds ret = -ENOMEM; 514e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5151da177e4SLinus Torvalds if (!page) 5161da177e4SLinus Torvalds goto out; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds ret = 0; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds mm = get_task_mm(task); 5211da177e4SLinus Torvalds if (!mm) 5221da177e4SLinus Torvalds goto out_free; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds ret = -EIO; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 5271da177e4SLinus Torvalds goto out_put; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds ret = 0; 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds while (count > 0) { 5321da177e4SLinus Torvalds int this_len, retval; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5351da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 536ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 5371da177e4SLinus Torvalds if (!ret) 5381da177e4SLinus Torvalds ret = -EIO; 5391da177e4SLinus Torvalds break; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 5431da177e4SLinus Torvalds ret = -EFAULT; 5441da177e4SLinus Torvalds break; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds ret += retval; 5481da177e4SLinus Torvalds src += retval; 5491da177e4SLinus Torvalds buf += retval; 5501da177e4SLinus Torvalds count -= retval; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds *ppos = src; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds out_put: 5551da177e4SLinus Torvalds mmput(mm); 5561da177e4SLinus Torvalds out_free: 5571da177e4SLinus Torvalds free_page((unsigned long) page); 5581da177e4SLinus Torvalds out: 55999f89551SEric W. Biederman put_task_struct(task); 56099f89551SEric W. Biederman out_no_task: 5611da177e4SLinus Torvalds return ret; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds #define mem_write NULL 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds #ifndef mem_write 5671da177e4SLinus Torvalds /* This is a security hazard */ 56863967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 5691da177e4SLinus Torvalds size_t count, loff_t *ppos) 5701da177e4SLinus Torvalds { 571f7ca54f4SFrederik Deweerdt int copied; 5721da177e4SLinus Torvalds char *page; 5732fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5741da177e4SLinus Torvalds unsigned long dst = *ppos; 5751da177e4SLinus Torvalds 57699f89551SEric W. Biederman copied = -ESRCH; 57799f89551SEric W. Biederman if (!task) 57899f89551SEric W. Biederman goto out_no_task; 5791da177e4SLinus Torvalds 58099f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 58199f89551SEric W. Biederman goto out; 58299f89551SEric W. Biederman 58399f89551SEric W. Biederman copied = -ENOMEM; 584e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5851da177e4SLinus Torvalds if (!page) 58699f89551SEric W. Biederman goto out; 5871da177e4SLinus Torvalds 588f7ca54f4SFrederik Deweerdt copied = 0; 5891da177e4SLinus Torvalds while (count > 0) { 5901da177e4SLinus Torvalds int this_len, retval; 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5931da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 5941da177e4SLinus Torvalds copied = -EFAULT; 5951da177e4SLinus Torvalds break; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 5981da177e4SLinus Torvalds if (!retval) { 5991da177e4SLinus Torvalds if (!copied) 6001da177e4SLinus Torvalds copied = -EIO; 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds copied += retval; 6041da177e4SLinus Torvalds buf += retval; 6051da177e4SLinus Torvalds dst += retval; 6061da177e4SLinus Torvalds count -= retval; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds *ppos = dst; 6091da177e4SLinus Torvalds free_page((unsigned long) page); 61099f89551SEric W. Biederman out: 61199f89551SEric W. Biederman put_task_struct(task); 61299f89551SEric W. Biederman out_no_task: 6131da177e4SLinus Torvalds return copied; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds #endif 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 6181da177e4SLinus Torvalds { 6191da177e4SLinus Torvalds switch (orig) { 6201da177e4SLinus Torvalds case 0: 6211da177e4SLinus Torvalds file->f_pos = offset; 6221da177e4SLinus Torvalds break; 6231da177e4SLinus Torvalds case 1: 6241da177e4SLinus Torvalds file->f_pos += offset; 6251da177e4SLinus Torvalds break; 6261da177e4SLinus Torvalds default: 6271da177e4SLinus Torvalds return -EINVAL; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds force_successful_syscall_return(); 6301da177e4SLinus Torvalds return file->f_pos; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 63300977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 6341da177e4SLinus Torvalds .llseek = mem_lseek, 6351da177e4SLinus Torvalds .read = mem_read, 6361da177e4SLinus Torvalds .write = mem_write, 6371da177e4SLinus Torvalds .open = mem_open, 6381da177e4SLinus Torvalds }; 6391da177e4SLinus Torvalds 640*315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 641*315e28c8SJames Pearson size_t count, loff_t *ppos) 642*315e28c8SJames Pearson { 643*315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 644*315e28c8SJames Pearson char *page; 645*315e28c8SJames Pearson unsigned long src = *ppos; 646*315e28c8SJames Pearson int ret = -ESRCH; 647*315e28c8SJames Pearson struct mm_struct *mm; 648*315e28c8SJames Pearson 649*315e28c8SJames Pearson if (!task) 650*315e28c8SJames Pearson goto out_no_task; 651*315e28c8SJames Pearson 652*315e28c8SJames Pearson if (!ptrace_may_attach(task)) 653*315e28c8SJames Pearson goto out; 654*315e28c8SJames Pearson 655*315e28c8SJames Pearson ret = -ENOMEM; 656*315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 657*315e28c8SJames Pearson if (!page) 658*315e28c8SJames Pearson goto out; 659*315e28c8SJames Pearson 660*315e28c8SJames Pearson ret = 0; 661*315e28c8SJames Pearson 662*315e28c8SJames Pearson mm = get_task_mm(task); 663*315e28c8SJames Pearson if (!mm) 664*315e28c8SJames Pearson goto out_free; 665*315e28c8SJames Pearson 666*315e28c8SJames Pearson while (count > 0) { 667*315e28c8SJames Pearson int this_len, retval, max_len; 668*315e28c8SJames Pearson 669*315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 670*315e28c8SJames Pearson 671*315e28c8SJames Pearson if (this_len <= 0) 672*315e28c8SJames Pearson break; 673*315e28c8SJames Pearson 674*315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 675*315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 676*315e28c8SJames Pearson 677*315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 678*315e28c8SJames Pearson page, this_len, 0); 679*315e28c8SJames Pearson 680*315e28c8SJames Pearson if (retval <= 0) { 681*315e28c8SJames Pearson ret = retval; 682*315e28c8SJames Pearson break; 683*315e28c8SJames Pearson } 684*315e28c8SJames Pearson 685*315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 686*315e28c8SJames Pearson ret = -EFAULT; 687*315e28c8SJames Pearson break; 688*315e28c8SJames Pearson } 689*315e28c8SJames Pearson 690*315e28c8SJames Pearson ret += retval; 691*315e28c8SJames Pearson src += retval; 692*315e28c8SJames Pearson buf += retval; 693*315e28c8SJames Pearson count -= retval; 694*315e28c8SJames Pearson } 695*315e28c8SJames Pearson *ppos = src; 696*315e28c8SJames Pearson 697*315e28c8SJames Pearson mmput(mm); 698*315e28c8SJames Pearson out_free: 699*315e28c8SJames Pearson free_page((unsigned long) page); 700*315e28c8SJames Pearson out: 701*315e28c8SJames Pearson put_task_struct(task); 702*315e28c8SJames Pearson out_no_task: 703*315e28c8SJames Pearson return ret; 704*315e28c8SJames Pearson } 705*315e28c8SJames Pearson 706*315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 707*315e28c8SJames Pearson .read = environ_read, 708*315e28c8SJames Pearson }; 709*315e28c8SJames Pearson 7101da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 7111da177e4SLinus Torvalds size_t count, loff_t *ppos) 7121da177e4SLinus Torvalds { 7132fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7148578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 7151da177e4SLinus Torvalds size_t len; 71699f89551SEric W. Biederman int oom_adjust; 7171da177e4SLinus Torvalds 71899f89551SEric W. Biederman if (!task) 71999f89551SEric W. Biederman return -ESRCH; 72099f89551SEric W. Biederman oom_adjust = task->oomkilladj; 72199f89551SEric W. Biederman put_task_struct(task); 72299f89551SEric W. Biederman 7238578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 7240c28f287SAkinobu Mita 7250c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 7291da177e4SLinus Torvalds size_t count, loff_t *ppos) 7301da177e4SLinus Torvalds { 73199f89551SEric W. Biederman struct task_struct *task; 7328578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 7331da177e4SLinus Torvalds int oom_adjust; 7341da177e4SLinus Torvalds 7358578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 7368578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 7378578cea7SEric W. Biederman count = sizeof(buffer) - 1; 7381da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 7391da177e4SLinus Torvalds return -EFAULT; 7401da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 7418ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 7428ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 7431da177e4SLinus Torvalds return -EINVAL; 7441da177e4SLinus Torvalds if (*end == '\n') 7451da177e4SLinus Torvalds end++; 7462fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 74799f89551SEric W. Biederman if (!task) 74899f89551SEric W. Biederman return -ESRCH; 7498fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 7508fb4fc68SGuillem Jover put_task_struct(task); 7518fb4fc68SGuillem Jover return -EACCES; 7528fb4fc68SGuillem Jover } 7531da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 75499f89551SEric W. Biederman put_task_struct(task); 7551da177e4SLinus Torvalds if (end - buffer == 0) 7561da177e4SLinus Torvalds return -EIO; 7571da177e4SLinus Torvalds return end - buffer; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 76000977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 7611da177e4SLinus Torvalds .read = oom_adjust_read, 7621da177e4SLinus Torvalds .write = oom_adjust_write, 7631da177e4SLinus Torvalds }; 7641da177e4SLinus Torvalds 7654b8df891SDavid Rientjes #ifdef CONFIG_MMU 766b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 767b813e931SDavid Rientjes size_t count, loff_t *ppos) 768b813e931SDavid Rientjes { 769b813e931SDavid Rientjes struct task_struct *task; 770b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 771b813e931SDavid Rientjes struct mm_struct *mm; 772b813e931SDavid Rientjes 773b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 774b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 775b813e931SDavid Rientjes count = sizeof(buffer) - 1; 776b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 777b813e931SDavid Rientjes return -EFAULT; 778b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 779b813e931SDavid Rientjes return -EINVAL; 780b813e931SDavid Rientjes if (*end == '\n') 781b813e931SDavid Rientjes end++; 782b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 783b813e931SDavid Rientjes if (!task) 784b813e931SDavid Rientjes return -ESRCH; 785b813e931SDavid Rientjes mm = get_task_mm(task); 786b813e931SDavid Rientjes if (mm) { 787b813e931SDavid Rientjes clear_refs_smap(mm); 788b813e931SDavid Rientjes mmput(mm); 789b813e931SDavid Rientjes } 790b813e931SDavid Rientjes put_task_struct(task); 791b813e931SDavid Rientjes if (end - buffer == 0) 792b813e931SDavid Rientjes return -EIO; 793b813e931SDavid Rientjes return end - buffer; 794b813e931SDavid Rientjes } 795b813e931SDavid Rientjes 796b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 797b813e931SDavid Rientjes .write = clear_refs_write, 798b813e931SDavid Rientjes }; 7994b8df891SDavid Rientjes #endif 800b813e931SDavid Rientjes 8011da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 8021da177e4SLinus Torvalds #define TMPBUFLEN 21 8031da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 8041da177e4SLinus Torvalds size_t count, loff_t *ppos) 8051da177e4SLinus Torvalds { 8062fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 80799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 8081da177e4SLinus Torvalds ssize_t length; 8091da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 8101da177e4SLinus Torvalds 81199f89551SEric W. Biederman if (!task) 81299f89551SEric W. Biederman return -ESRCH; 8131da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 8141da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 81599f89551SEric W. Biederman put_task_struct(task); 8161da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 8201da177e4SLinus Torvalds size_t count, loff_t *ppos) 8211da177e4SLinus Torvalds { 8222fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 8231da177e4SLinus Torvalds char *page, *tmp; 8241da177e4SLinus Torvalds ssize_t length; 8251da177e4SLinus Torvalds uid_t loginuid; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 8281da177e4SLinus Torvalds return -EPERM; 8291da177e4SLinus Torvalds 83013b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 8311da177e4SLinus Torvalds return -EPERM; 8321da177e4SLinus Torvalds 833e0182909SAl Viro if (count >= PAGE_SIZE) 834e0182909SAl Viro count = PAGE_SIZE - 1; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (*ppos != 0) { 8371da177e4SLinus Torvalds /* No partial writes. */ 8381da177e4SLinus Torvalds return -EINVAL; 8391da177e4SLinus Torvalds } 840e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 8411da177e4SLinus Torvalds if (!page) 8421da177e4SLinus Torvalds return -ENOMEM; 8431da177e4SLinus Torvalds length = -EFAULT; 8441da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 8451da177e4SLinus Torvalds goto out_free_page; 8461da177e4SLinus Torvalds 847e0182909SAl Viro page[count] = '\0'; 8481da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 8491da177e4SLinus Torvalds if (tmp == page) { 8501da177e4SLinus Torvalds length = -EINVAL; 8511da177e4SLinus Torvalds goto out_free_page; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds } 85499f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 8551da177e4SLinus Torvalds if (likely(length == 0)) 8561da177e4SLinus Torvalds length = count; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds out_free_page: 8591da177e4SLinus Torvalds free_page((unsigned long) page); 8601da177e4SLinus Torvalds return length; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 86300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 8641da177e4SLinus Torvalds .read = proc_loginuid_read, 8651da177e4SLinus Torvalds .write = proc_loginuid_write, 8661da177e4SLinus Torvalds }; 8671da177e4SLinus Torvalds #endif 8681da177e4SLinus Torvalds 869f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 870f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 871f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 872f4f154fdSAkinobu Mita { 873f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 874f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 875f4f154fdSAkinobu Mita size_t len; 876f4f154fdSAkinobu Mita int make_it_fail; 877f4f154fdSAkinobu Mita 878f4f154fdSAkinobu Mita if (!task) 879f4f154fdSAkinobu Mita return -ESRCH; 880f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 881f4f154fdSAkinobu Mita put_task_struct(task); 882f4f154fdSAkinobu Mita 883f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 8840c28f287SAkinobu Mita 8850c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 886f4f154fdSAkinobu Mita } 887f4f154fdSAkinobu Mita 888f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 889f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 890f4f154fdSAkinobu Mita { 891f4f154fdSAkinobu Mita struct task_struct *task; 892f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 893f4f154fdSAkinobu Mita int make_it_fail; 894f4f154fdSAkinobu Mita 895f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 896f4f154fdSAkinobu Mita return -EPERM; 897f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 898f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 899f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 900f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 901f4f154fdSAkinobu Mita return -EFAULT; 902f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 903f4f154fdSAkinobu Mita if (*end == '\n') 904f4f154fdSAkinobu Mita end++; 905f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 906f4f154fdSAkinobu Mita if (!task) 907f4f154fdSAkinobu Mita return -ESRCH; 908f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 909f4f154fdSAkinobu Mita put_task_struct(task); 910f4f154fdSAkinobu Mita if (end - buffer == 0) 911f4f154fdSAkinobu Mita return -EIO; 912f4f154fdSAkinobu Mita return end - buffer; 913f4f154fdSAkinobu Mita } 914f4f154fdSAkinobu Mita 91500977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 916f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 917f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 918f4f154fdSAkinobu Mita }; 919f4f154fdSAkinobu Mita #endif 920f4f154fdSAkinobu Mita 92143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 92243ae34cbSIngo Molnar /* 92343ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 92443ae34cbSIngo Molnar */ 92543ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 92643ae34cbSIngo Molnar { 92743ae34cbSIngo Molnar struct inode *inode = m->private; 92843ae34cbSIngo Molnar struct task_struct *p; 92943ae34cbSIngo Molnar 93043ae34cbSIngo Molnar WARN_ON(!inode); 93143ae34cbSIngo Molnar 93243ae34cbSIngo Molnar p = get_proc_task(inode); 93343ae34cbSIngo Molnar if (!p) 93443ae34cbSIngo Molnar return -ESRCH; 93543ae34cbSIngo Molnar proc_sched_show_task(p, m); 93643ae34cbSIngo Molnar 93743ae34cbSIngo Molnar put_task_struct(p); 93843ae34cbSIngo Molnar 93943ae34cbSIngo Molnar return 0; 94043ae34cbSIngo Molnar } 94143ae34cbSIngo Molnar 94243ae34cbSIngo Molnar static ssize_t 94343ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 94443ae34cbSIngo Molnar size_t count, loff_t *offset) 94543ae34cbSIngo Molnar { 94643ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 94743ae34cbSIngo Molnar struct task_struct *p; 94843ae34cbSIngo Molnar 94943ae34cbSIngo Molnar WARN_ON(!inode); 95043ae34cbSIngo Molnar 95143ae34cbSIngo Molnar p = get_proc_task(inode); 95243ae34cbSIngo Molnar if (!p) 95343ae34cbSIngo Molnar return -ESRCH; 95443ae34cbSIngo Molnar proc_sched_set_task(p); 95543ae34cbSIngo Molnar 95643ae34cbSIngo Molnar put_task_struct(p); 95743ae34cbSIngo Molnar 95843ae34cbSIngo Molnar return count; 95943ae34cbSIngo Molnar } 96043ae34cbSIngo Molnar 96143ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 96243ae34cbSIngo Molnar { 96343ae34cbSIngo Molnar int ret; 96443ae34cbSIngo Molnar 96543ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 96643ae34cbSIngo Molnar if (!ret) { 96743ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 96843ae34cbSIngo Molnar 96943ae34cbSIngo Molnar m->private = inode; 97043ae34cbSIngo Molnar } 97143ae34cbSIngo Molnar return ret; 97243ae34cbSIngo Molnar } 97343ae34cbSIngo Molnar 97443ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 97543ae34cbSIngo Molnar .open = sched_open, 97643ae34cbSIngo Molnar .read = seq_read, 97743ae34cbSIngo Molnar .write = sched_write, 97843ae34cbSIngo Molnar .llseek = seq_lseek, 9795ea473a1SAlexey Dobriyan .release = single_release, 98043ae34cbSIngo Molnar }; 98143ae34cbSIngo Molnar 98243ae34cbSIngo Molnar #endif 98343ae34cbSIngo Molnar 984008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 9851da177e4SLinus Torvalds { 9861da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9871da177e4SLinus Torvalds int error = -EACCES; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 9901da177e4SLinus Torvalds path_release(nd); 9911da177e4SLinus Torvalds 992778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 993778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 9941da177e4SLinus Torvalds goto out; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 9971da177e4SLinus Torvalds nd->last_type = LAST_BIND; 9981da177e4SLinus Torvalds out: 999008b150aSAl Viro return ERR_PTR(error); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10031da177e4SLinus Torvalds char __user *buffer, int buflen) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds struct inode * inode; 1006e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 1007e12ba74dSMel Gorman char *path; 10081da177e4SLinus Torvalds int len; 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds if (!tmp) 10111da177e4SLinus Torvalds return -ENOMEM; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds inode = dentry->d_inode; 10141da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10151da177e4SLinus Torvalds len = PTR_ERR(path); 10161da177e4SLinus Torvalds if (IS_ERR(path)) 10171da177e4SLinus Torvalds goto out; 10181da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (len > buflen) 10211da177e4SLinus Torvalds len = buflen; 10221da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 10231da177e4SLinus Torvalds len = -EFAULT; 10241da177e4SLinus Torvalds out: 10251da177e4SLinus Torvalds free_page((unsigned long)tmp); 10261da177e4SLinus Torvalds return len; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds int error = -EACCES; 10321da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10331da177e4SLinus Torvalds struct dentry *de; 10341da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 10351da177e4SLinus Torvalds 1036778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1037778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10381da177e4SLinus Torvalds goto out; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 10411da177e4SLinus Torvalds if (error) 10421da177e4SLinus Torvalds goto out; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 10451da177e4SLinus Torvalds dput(de); 10461da177e4SLinus Torvalds mntput(mnt); 10471da177e4SLinus Torvalds out: 10481da177e4SLinus Torvalds return error; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 1051c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 10521da177e4SLinus Torvalds .readlink = proc_pid_readlink, 10536d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 10546d76fa58SLinus Torvalds .setattr = proc_setattr, 10551da177e4SLinus Torvalds }; 10561da177e4SLinus Torvalds 105728a6d671SEric W. Biederman 105828a6d671SEric W. Biederman /* building an inode */ 105928a6d671SEric W. Biederman 106028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 106128a6d671SEric W. Biederman { 106228a6d671SEric W. Biederman int dumpable = 0; 106328a6d671SEric W. Biederman struct mm_struct *mm; 106428a6d671SEric W. Biederman 106528a6d671SEric W. Biederman task_lock(task); 106628a6d671SEric W. Biederman mm = task->mm; 106728a6d671SEric W. Biederman if (mm) 10686c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 106928a6d671SEric W. Biederman task_unlock(task); 107028a6d671SEric W. Biederman if(dumpable == 1) 107128a6d671SEric W. Biederman return 1; 107228a6d671SEric W. Biederman return 0; 107328a6d671SEric W. Biederman } 107428a6d671SEric W. Biederman 107528a6d671SEric W. Biederman 107661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 107728a6d671SEric W. Biederman { 107828a6d671SEric W. Biederman struct inode * inode; 107928a6d671SEric W. Biederman struct proc_inode *ei; 108028a6d671SEric W. Biederman 108128a6d671SEric W. Biederman /* We need a new inode */ 108228a6d671SEric W. Biederman 108328a6d671SEric W. Biederman inode = new_inode(sb); 108428a6d671SEric W. Biederman if (!inode) 108528a6d671SEric W. Biederman goto out; 108628a6d671SEric W. Biederman 108728a6d671SEric W. Biederman /* Common stuff */ 108828a6d671SEric W. Biederman ei = PROC_I(inode); 108928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 109028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 109128a6d671SEric W. Biederman 109228a6d671SEric W. Biederman /* 109328a6d671SEric W. Biederman * grab the reference to task. 109428a6d671SEric W. Biederman */ 10951a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 109628a6d671SEric W. Biederman if (!ei->pid) 109728a6d671SEric W. Biederman goto out_unlock; 109828a6d671SEric W. Biederman 109928a6d671SEric W. Biederman inode->i_uid = 0; 110028a6d671SEric W. Biederman inode->i_gid = 0; 110128a6d671SEric W. Biederman if (task_dumpable(task)) { 110228a6d671SEric W. Biederman inode->i_uid = task->euid; 110328a6d671SEric W. Biederman inode->i_gid = task->egid; 110428a6d671SEric W. Biederman } 110528a6d671SEric W. Biederman security_task_to_inode(task, inode); 110628a6d671SEric W. Biederman 110728a6d671SEric W. Biederman out: 110828a6d671SEric W. Biederman return inode; 110928a6d671SEric W. Biederman 111028a6d671SEric W. Biederman out_unlock: 111128a6d671SEric W. Biederman iput(inode); 111228a6d671SEric W. Biederman return NULL; 111328a6d671SEric W. Biederman } 111428a6d671SEric W. Biederman 111528a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 111628a6d671SEric W. Biederman { 111728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 111828a6d671SEric W. Biederman struct task_struct *task; 111928a6d671SEric W. Biederman generic_fillattr(inode, stat); 112028a6d671SEric W. Biederman 112128a6d671SEric W. Biederman rcu_read_lock(); 112228a6d671SEric W. Biederman stat->uid = 0; 112328a6d671SEric W. Biederman stat->gid = 0; 112428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 112528a6d671SEric W. Biederman if (task) { 112628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 112728a6d671SEric W. Biederman task_dumpable(task)) { 112828a6d671SEric W. Biederman stat->uid = task->euid; 112928a6d671SEric W. Biederman stat->gid = task->egid; 113028a6d671SEric W. Biederman } 113128a6d671SEric W. Biederman } 113228a6d671SEric W. Biederman rcu_read_unlock(); 113328a6d671SEric W. Biederman return 0; 113428a6d671SEric W. Biederman } 113528a6d671SEric W. Biederman 113628a6d671SEric W. Biederman /* dentry stuff */ 113728a6d671SEric W. Biederman 113828a6d671SEric W. Biederman /* 113928a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 114028a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 114128a6d671SEric W. Biederman * due to the way we treat inodes. 114228a6d671SEric W. Biederman * 114328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 114428a6d671SEric W. Biederman * performed a setuid(), etc. 114528a6d671SEric W. Biederman * 114628a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 114728a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 114828a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 114928a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 115028a6d671SEric W. Biederman * made this apply to all per process world readable and executable 115128a6d671SEric W. Biederman * directories. 115228a6d671SEric W. Biederman */ 115328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 115428a6d671SEric W. Biederman { 115528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 115628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 115728a6d671SEric W. Biederman if (task) { 115828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 115928a6d671SEric W. Biederman task_dumpable(task)) { 116028a6d671SEric W. Biederman inode->i_uid = task->euid; 116128a6d671SEric W. Biederman inode->i_gid = task->egid; 116228a6d671SEric W. Biederman } else { 116328a6d671SEric W. Biederman inode->i_uid = 0; 116428a6d671SEric W. Biederman inode->i_gid = 0; 116528a6d671SEric W. Biederman } 116628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 116728a6d671SEric W. Biederman security_task_to_inode(task, inode); 116828a6d671SEric W. Biederman put_task_struct(task); 116928a6d671SEric W. Biederman return 1; 117028a6d671SEric W. Biederman } 117128a6d671SEric W. Biederman d_drop(dentry); 117228a6d671SEric W. Biederman return 0; 117328a6d671SEric W. Biederman } 117428a6d671SEric W. Biederman 117528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 117628a6d671SEric W. Biederman { 117728a6d671SEric W. Biederman /* Is the task we represent dead? 117828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 117928a6d671SEric W. Biederman * kill it immediately. 118028a6d671SEric W. Biederman */ 118128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 118228a6d671SEric W. Biederman } 118328a6d671SEric W. Biederman 118428a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 118528a6d671SEric W. Biederman { 118628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 118728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 118828a6d671SEric W. Biederman }; 118928a6d671SEric W. Biederman 119028a6d671SEric W. Biederman /* Lookups */ 119128a6d671SEric W. Biederman 1192c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1193c5141e6dSEric Dumazet struct task_struct *, const void *); 119461a28784SEric W. Biederman 11951c0d04c9SEric W. Biederman /* 11961c0d04c9SEric W. Biederman * Fill a directory entry. 11971c0d04c9SEric W. Biederman * 11981c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 11991c0d04c9SEric W. Biederman * file type from dcache entry. 12001c0d04c9SEric W. Biederman * 12011c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 12021c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 12031c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 12041c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 12051c0d04c9SEric W. Biederman * by stat. 12061c0d04c9SEric W. Biederman */ 120761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 120861a28784SEric W. Biederman char *name, int len, 1209c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 121061a28784SEric W. Biederman { 12112fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 121261a28784SEric W. Biederman struct inode *inode; 121361a28784SEric W. Biederman struct qstr qname; 121461a28784SEric W. Biederman ino_t ino = 0; 121561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 121661a28784SEric W. Biederman 121761a28784SEric W. Biederman qname.name = name; 121861a28784SEric W. Biederman qname.len = len; 121961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 122061a28784SEric W. Biederman 122161a28784SEric W. Biederman child = d_lookup(dir, &qname); 122261a28784SEric W. Biederman if (!child) { 122361a28784SEric W. Biederman struct dentry *new; 122461a28784SEric W. Biederman new = d_alloc(dir, &qname); 122561a28784SEric W. Biederman if (new) { 122661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 122761a28784SEric W. Biederman if (child) 122861a28784SEric W. Biederman dput(new); 122961a28784SEric W. Biederman else 123061a28784SEric W. Biederman child = new; 123161a28784SEric W. Biederman } 123261a28784SEric W. Biederman } 123361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 123461a28784SEric W. Biederman goto end_instantiate; 123561a28784SEric W. Biederman inode = child->d_inode; 123661a28784SEric W. Biederman if (inode) { 123761a28784SEric W. Biederman ino = inode->i_ino; 123861a28784SEric W. Biederman type = inode->i_mode >> 12; 123961a28784SEric W. Biederman } 124061a28784SEric W. Biederman dput(child); 124161a28784SEric W. Biederman end_instantiate: 124261a28784SEric W. Biederman if (!ino) 124361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 124461a28784SEric W. Biederman if (!ino) 124561a28784SEric W. Biederman ino = 1; 124661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 124761a28784SEric W. Biederman } 124861a28784SEric W. Biederman 124928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 125028a6d671SEric W. Biederman { 125128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 125228a6d671SEric W. Biederman int len = dentry->d_name.len; 125328a6d671SEric W. Biederman unsigned n = 0; 125428a6d671SEric W. Biederman 125528a6d671SEric W. Biederman if (len > 1 && *name == '0') 125628a6d671SEric W. Biederman goto out; 125728a6d671SEric W. Biederman while (len-- > 0) { 125828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 125928a6d671SEric W. Biederman if (c > 9) 126028a6d671SEric W. Biederman goto out; 126128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 126228a6d671SEric W. Biederman goto out; 126328a6d671SEric W. Biederman n *= 10; 126428a6d671SEric W. Biederman n += c; 126528a6d671SEric W. Biederman } 126628a6d671SEric W. Biederman return n; 126728a6d671SEric W. Biederman out: 126828a6d671SEric W. Biederman return ~0U; 126928a6d671SEric W. Biederman } 127028a6d671SEric W. Biederman 127127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 127227932742SMiklos Szeredi 127327932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 127427932742SMiklos Szeredi struct vfsmount **mnt, char *info) 127528a6d671SEric W. Biederman { 127628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 127728a6d671SEric W. Biederman struct files_struct *files = NULL; 127828a6d671SEric W. Biederman struct file *file; 127928a6d671SEric W. Biederman int fd = proc_fd(inode); 128028a6d671SEric W. Biederman 128128a6d671SEric W. Biederman if (task) { 128228a6d671SEric W. Biederman files = get_files_struct(task); 128328a6d671SEric W. Biederman put_task_struct(task); 128428a6d671SEric W. Biederman } 128528a6d671SEric W. Biederman if (files) { 128628a6d671SEric W. Biederman /* 128728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 128828a6d671SEric W. Biederman * hold ->file_lock. 128928a6d671SEric W. Biederman */ 129028a6d671SEric W. Biederman spin_lock(&files->file_lock); 129128a6d671SEric W. Biederman file = fcheck_files(files, fd); 129228a6d671SEric W. Biederman if (file) { 129327932742SMiklos Szeredi if (mnt) 12942fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 129527932742SMiklos Szeredi if (dentry) 12962fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 129727932742SMiklos Szeredi if (info) 129827932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 129927932742SMiklos Szeredi "pos:\t%lli\n" 130027932742SMiklos Szeredi "flags:\t0%o\n", 130127932742SMiklos Szeredi (long long) file->f_pos, 130227932742SMiklos Szeredi file->f_flags); 130328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 130428a6d671SEric W. Biederman put_files_struct(files); 130528a6d671SEric W. Biederman return 0; 130628a6d671SEric W. Biederman } 130728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 130828a6d671SEric W. Biederman put_files_struct(files); 130928a6d671SEric W. Biederman } 131028a6d671SEric W. Biederman return -ENOENT; 131128a6d671SEric W. Biederman } 131228a6d671SEric W. Biederman 131327932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 131427932742SMiklos Szeredi struct vfsmount **mnt) 131527932742SMiklos Szeredi { 131627932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 131727932742SMiklos Szeredi } 131827932742SMiklos Szeredi 131928a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 132028a6d671SEric W. Biederman { 132128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 132228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 132328a6d671SEric W. Biederman int fd = proc_fd(inode); 132428a6d671SEric W. Biederman struct files_struct *files; 132528a6d671SEric W. Biederman 132628a6d671SEric W. Biederman if (task) { 132728a6d671SEric W. Biederman files = get_files_struct(task); 132828a6d671SEric W. Biederman if (files) { 132928a6d671SEric W. Biederman rcu_read_lock(); 133028a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 133128a6d671SEric W. Biederman rcu_read_unlock(); 133228a6d671SEric W. Biederman put_files_struct(files); 133328a6d671SEric W. Biederman if (task_dumpable(task)) { 133428a6d671SEric W. Biederman inode->i_uid = task->euid; 133528a6d671SEric W. Biederman inode->i_gid = task->egid; 133628a6d671SEric W. Biederman } else { 133728a6d671SEric W. Biederman inode->i_uid = 0; 133828a6d671SEric W. Biederman inode->i_gid = 0; 133928a6d671SEric W. Biederman } 134028a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 134128a6d671SEric W. Biederman security_task_to_inode(task, inode); 134228a6d671SEric W. Biederman put_task_struct(task); 134328a6d671SEric W. Biederman return 1; 134428a6d671SEric W. Biederman } 134528a6d671SEric W. Biederman rcu_read_unlock(); 134628a6d671SEric W. Biederman put_files_struct(files); 134728a6d671SEric W. Biederman } 134828a6d671SEric W. Biederman put_task_struct(task); 134928a6d671SEric W. Biederman } 135028a6d671SEric W. Biederman d_drop(dentry); 135128a6d671SEric W. Biederman return 0; 135228a6d671SEric W. Biederman } 135328a6d671SEric W. Biederman 135428a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 135528a6d671SEric W. Biederman { 135628a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 135728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 135828a6d671SEric W. Biederman }; 135928a6d671SEric W. Biederman 1360444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1361c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 136228a6d671SEric W. Biederman { 1363c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 136428a6d671SEric W. Biederman struct file *file; 136528a6d671SEric W. Biederman struct files_struct *files; 136628a6d671SEric W. Biederman struct inode *inode; 136728a6d671SEric W. Biederman struct proc_inode *ei; 1368444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 136928a6d671SEric W. Biederman 137061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 137128a6d671SEric W. Biederman if (!inode) 137228a6d671SEric W. Biederman goto out; 137328a6d671SEric W. Biederman ei = PROC_I(inode); 137428a6d671SEric W. Biederman ei->fd = fd; 137528a6d671SEric W. Biederman files = get_files_struct(task); 137628a6d671SEric W. Biederman if (!files) 1377444ceed8SEric W. Biederman goto out_iput; 137828a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 137928a6d671SEric W. Biederman 138028a6d671SEric W. Biederman /* 138128a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 138228a6d671SEric W. Biederman * hold ->file_lock. 138328a6d671SEric W. Biederman */ 138428a6d671SEric W. Biederman spin_lock(&files->file_lock); 138528a6d671SEric W. Biederman file = fcheck_files(files, fd); 138628a6d671SEric W. Biederman if (!file) 1387444ceed8SEric W. Biederman goto out_unlock; 138828a6d671SEric W. Biederman if (file->f_mode & 1) 138928a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 139028a6d671SEric W. Biederman if (file->f_mode & 2) 139128a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 139228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 139328a6d671SEric W. Biederman put_files_struct(files); 1394444ceed8SEric W. Biederman 139528a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 139628a6d671SEric W. Biederman inode->i_size = 64; 139728a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 139828a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 139928a6d671SEric W. Biederman d_add(dentry, inode); 140028a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 140128a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1402444ceed8SEric W. Biederman error = NULL; 1403444ceed8SEric W. Biederman 1404444ceed8SEric W. Biederman out: 1405444ceed8SEric W. Biederman return error; 1406444ceed8SEric W. Biederman out_unlock: 1407444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1408444ceed8SEric W. Biederman put_files_struct(files); 1409444ceed8SEric W. Biederman out_iput: 1410444ceed8SEric W. Biederman iput(inode); 1411444ceed8SEric W. Biederman goto out; 1412444ceed8SEric W. Biederman } 1413444ceed8SEric W. Biederman 141427932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 141527932742SMiklos Szeredi struct dentry *dentry, 141627932742SMiklos Szeredi instantiate_t instantiate) 1417444ceed8SEric W. Biederman { 1418444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1419444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1420444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1421444ceed8SEric W. Biederman 1422444ceed8SEric W. Biederman if (!task) 1423444ceed8SEric W. Biederman goto out_no_task; 1424444ceed8SEric W. Biederman if (fd == ~0U) 1425444ceed8SEric W. Biederman goto out; 1426444ceed8SEric W. Biederman 142727932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 142828a6d671SEric W. Biederman out: 142928a6d671SEric W. Biederman put_task_struct(task); 143028a6d671SEric W. Biederman out_no_task: 143128a6d671SEric W. Biederman return result; 143228a6d671SEric W. Biederman } 143328a6d671SEric W. Biederman 143427932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 143527932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 14361da177e4SLinus Torvalds { 14372fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 14385634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 143999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 14401da177e4SLinus Torvalds unsigned int fd, tid, ino; 14411da177e4SLinus Torvalds int retval; 14421da177e4SLinus Torvalds struct files_struct * files; 1443badf1662SDipankar Sarma struct fdtable *fdt; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds retval = -ENOENT; 144699f89551SEric W. Biederman if (!p) 144799f89551SEric W. Biederman goto out_no_task; 14481da177e4SLinus Torvalds retval = 0; 14491da177e4SLinus Torvalds tid = p->pid; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds fd = filp->f_pos; 14521da177e4SLinus Torvalds switch (fd) { 14531da177e4SLinus Torvalds case 0: 14541da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 14551da177e4SLinus Torvalds goto out; 14561da177e4SLinus Torvalds filp->f_pos++; 14571da177e4SLinus Torvalds case 1: 14585634708bSEric W. Biederman ino = parent_ino(dentry); 14591da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 14601da177e4SLinus Torvalds goto out; 14611da177e4SLinus Torvalds filp->f_pos++; 14621da177e4SLinus Torvalds default: 14631da177e4SLinus Torvalds files = get_files_struct(p); 14641da177e4SLinus Torvalds if (!files) 14651da177e4SLinus Torvalds goto out; 1466b835996fSDipankar Sarma rcu_read_lock(); 1467badf1662SDipankar Sarma fdt = files_fdtable(files); 14681da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1469badf1662SDipankar Sarma fd < fdt->max_fds; 14701da177e4SLinus Torvalds fd++, filp->f_pos++) { 147127932742SMiklos Szeredi char name[PROC_NUMBUF]; 147227932742SMiklos Szeredi int len; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 14751da177e4SLinus Torvalds continue; 1476b835996fSDipankar Sarma rcu_read_unlock(); 14771da177e4SLinus Torvalds 147827932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 147927932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 148027932742SMiklos Szeredi name, len, instantiate, 148127932742SMiklos Szeredi p, &fd) < 0) { 1482b835996fSDipankar Sarma rcu_read_lock(); 14831da177e4SLinus Torvalds break; 14841da177e4SLinus Torvalds } 1485b835996fSDipankar Sarma rcu_read_lock(); 14861da177e4SLinus Torvalds } 1487b835996fSDipankar Sarma rcu_read_unlock(); 14881da177e4SLinus Torvalds put_files_struct(files); 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds out: 149199f89551SEric W. Biederman put_task_struct(p); 149299f89551SEric W. Biederman out_no_task: 14931da177e4SLinus Torvalds return retval; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 149627932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 149727932742SMiklos Szeredi struct nameidata *nd) 149827932742SMiklos Szeredi { 149927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 150027932742SMiklos Szeredi } 150127932742SMiklos Szeredi 150227932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 150327932742SMiklos Szeredi { 150427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 150527932742SMiklos Szeredi } 150627932742SMiklos Szeredi 150727932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 150827932742SMiklos Szeredi size_t len, loff_t *ppos) 150927932742SMiklos Szeredi { 151027932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 151127932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 151227932742SMiklos Szeredi if (!err) 151327932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 151427932742SMiklos Szeredi return err; 151527932742SMiklos Szeredi } 151627932742SMiklos Szeredi 151727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 151827932742SMiklos Szeredi .open = nonseekable_open, 151927932742SMiklos Szeredi .read = proc_fdinfo_read, 152027932742SMiklos Szeredi }; 152127932742SMiklos Szeredi 152200977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 15231da177e4SLinus Torvalds .read = generic_read_dir, 15241da177e4SLinus Torvalds .readdir = proc_readfd, 15251da177e4SLinus Torvalds }; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* 15288948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 15298948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 15308948e11fSAlexey Dobriyan */ 15318948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 15328948e11fSAlexey Dobriyan struct nameidata *nd) 15338948e11fSAlexey Dobriyan { 15348948e11fSAlexey Dobriyan int rv; 15358948e11fSAlexey Dobriyan 15368948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 15378948e11fSAlexey Dobriyan if (rv == 0) 15388948e11fSAlexey Dobriyan return 0; 15398948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 15408948e11fSAlexey Dobriyan rv = 0; 15418948e11fSAlexey Dobriyan return rv; 15428948e11fSAlexey Dobriyan } 15438948e11fSAlexey Dobriyan 15448948e11fSAlexey Dobriyan /* 15451da177e4SLinus Torvalds * proc directories can do almost nothing.. 15461da177e4SLinus Torvalds */ 1547c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 15481da177e4SLinus Torvalds .lookup = proc_lookupfd, 15498948e11fSAlexey Dobriyan .permission = proc_fd_permission, 15506d76fa58SLinus Torvalds .setattr = proc_setattr, 15511da177e4SLinus Torvalds }; 15521da177e4SLinus Torvalds 155327932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 155427932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 155527932742SMiklos Szeredi { 155627932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 155727932742SMiklos Szeredi struct inode *inode; 155827932742SMiklos Szeredi struct proc_inode *ei; 155927932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 156027932742SMiklos Szeredi 156127932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 156227932742SMiklos Szeredi if (!inode) 156327932742SMiklos Szeredi goto out; 156427932742SMiklos Szeredi ei = PROC_I(inode); 156527932742SMiklos Szeredi ei->fd = fd; 156627932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 156727932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 156827932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 156927932742SMiklos Szeredi d_add(dentry, inode); 157027932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 157127932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 157227932742SMiklos Szeredi error = NULL; 157327932742SMiklos Szeredi 157427932742SMiklos Szeredi out: 157527932742SMiklos Szeredi return error; 157627932742SMiklos Szeredi } 157727932742SMiklos Szeredi 157827932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 157927932742SMiklos Szeredi struct dentry *dentry, 158027932742SMiklos Szeredi struct nameidata *nd) 158127932742SMiklos Szeredi { 158227932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 158327932742SMiklos Szeredi } 158427932742SMiklos Szeredi 158527932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 158627932742SMiklos Szeredi { 158727932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 158827932742SMiklos Szeredi proc_fdinfo_instantiate); 158927932742SMiklos Szeredi } 159027932742SMiklos Szeredi 159127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 159227932742SMiklos Szeredi .read = generic_read_dir, 159327932742SMiklos Szeredi .readdir = proc_readfdinfo, 159427932742SMiklos Szeredi }; 159527932742SMiklos Szeredi 159627932742SMiklos Szeredi /* 159727932742SMiklos Szeredi * proc directories can do almost nothing.. 159827932742SMiklos Szeredi */ 159927932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 160027932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 160127932742SMiklos Szeredi .setattr = proc_setattr, 160227932742SMiklos Szeredi }; 160327932742SMiklos Szeredi 160427932742SMiklos Szeredi 1605444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1606c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1607444ceed8SEric W. Biederman { 1608c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1609444ceed8SEric W. Biederman struct inode *inode; 1610444ceed8SEric W. Biederman struct proc_inode *ei; 1611444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1612444ceed8SEric W. Biederman 161361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1614444ceed8SEric W. Biederman if (!inode) 1615444ceed8SEric W. Biederman goto out; 1616444ceed8SEric W. Biederman 1617444ceed8SEric W. Biederman ei = PROC_I(inode); 1618444ceed8SEric W. Biederman inode->i_mode = p->mode; 1619444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1620444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1621444ceed8SEric W. Biederman if (p->iop) 1622444ceed8SEric W. Biederman inode->i_op = p->iop; 1623444ceed8SEric W. Biederman if (p->fop) 1624444ceed8SEric W. Biederman inode->i_fop = p->fop; 1625444ceed8SEric W. Biederman ei->op = p->op; 1626444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1627444ceed8SEric W. Biederman d_add(dentry, inode); 1628444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1629444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1630444ceed8SEric W. Biederman error = NULL; 1631444ceed8SEric W. Biederman out: 1632444ceed8SEric W. Biederman return error; 1633444ceed8SEric W. Biederman } 1634444ceed8SEric W. Biederman 16351da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 16361da177e4SLinus Torvalds struct dentry *dentry, 1637c5141e6dSEric Dumazet const struct pid_entry *ents, 16387bcd6b0eSEric W. Biederman unsigned int nents) 16391da177e4SLinus Torvalds { 16401da177e4SLinus Torvalds struct inode *inode; 1641cd6a3ce9SEric W. Biederman struct dentry *error; 164299f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1643c5141e6dSEric Dumazet const struct pid_entry *p, *last; 16441da177e4SLinus Torvalds 1645cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 16461da177e4SLinus Torvalds inode = NULL; 16471da177e4SLinus Torvalds 164899f89551SEric W. Biederman if (!task) 164999f89551SEric W. Biederman goto out_no_task; 16501da177e4SLinus Torvalds 165120cdc894SEric W. Biederman /* 165220cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 165320cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 165420cdc894SEric W. Biederman */ 16557bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16567bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 16571da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16581da177e4SLinus Torvalds continue; 16591da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16601da177e4SLinus Torvalds break; 16611da177e4SLinus Torvalds } 16627bcd6b0eSEric W. Biederman if (p > last) 16631da177e4SLinus Torvalds goto out; 16641da177e4SLinus Torvalds 1665444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 16661da177e4SLinus Torvalds out: 166799f89551SEric W. Biederman put_task_struct(task); 166899f89551SEric W. Biederman out_no_task: 1669cd6a3ce9SEric W. Biederman return error; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 1672c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1673c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 167461a28784SEric W. Biederman { 167561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 167661a28784SEric W. Biederman proc_pident_instantiate, task, p); 167761a28784SEric W. Biederman } 167861a28784SEric W. Biederman 167928a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 168028a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1681c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 168228a6d671SEric W. Biederman { 168328a6d671SEric W. Biederman int i; 168428a6d671SEric W. Biederman int pid; 16852fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 168628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 168728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1688c5141e6dSEric Dumazet const struct pid_entry *p, *last; 168928a6d671SEric W. Biederman ino_t ino; 169028a6d671SEric W. Biederman int ret; 169128a6d671SEric W. Biederman 169228a6d671SEric W. Biederman ret = -ENOENT; 169328a6d671SEric W. Biederman if (!task) 169461a28784SEric W. Biederman goto out_no_task; 169528a6d671SEric W. Biederman 169628a6d671SEric W. Biederman ret = 0; 169728a6d671SEric W. Biederman pid = task->pid; 169828a6d671SEric W. Biederman i = filp->f_pos; 169928a6d671SEric W. Biederman switch (i) { 170028a6d671SEric W. Biederman case 0: 170128a6d671SEric W. Biederman ino = inode->i_ino; 170228a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 170328a6d671SEric W. Biederman goto out; 170428a6d671SEric W. Biederman i++; 170528a6d671SEric W. Biederman filp->f_pos++; 170628a6d671SEric W. Biederman /* fall through */ 170728a6d671SEric W. Biederman case 1: 170828a6d671SEric W. Biederman ino = parent_ino(dentry); 170928a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 171028a6d671SEric W. Biederman goto out; 171128a6d671SEric W. Biederman i++; 171228a6d671SEric W. Biederman filp->f_pos++; 171328a6d671SEric W. Biederman /* fall through */ 171428a6d671SEric W. Biederman default: 171528a6d671SEric W. Biederman i -= 2; 171628a6d671SEric W. Biederman if (i >= nents) { 171728a6d671SEric W. Biederman ret = 1; 171828a6d671SEric W. Biederman goto out; 171928a6d671SEric W. Biederman } 172028a6d671SEric W. Biederman p = ents + i; 17217bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 17227bcd6b0eSEric W. Biederman while (p <= last) { 172361a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 172428a6d671SEric W. Biederman goto out; 172528a6d671SEric W. Biederman filp->f_pos++; 172628a6d671SEric W. Biederman p++; 172728a6d671SEric W. Biederman } 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 173028a6d671SEric W. Biederman ret = 1; 173128a6d671SEric W. Biederman out: 173261a28784SEric W. Biederman put_task_struct(task); 173361a28784SEric W. Biederman out_no_task: 173428a6d671SEric W. Biederman return ret; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 173828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 173928a6d671SEric W. Biederman size_t count, loff_t *ppos) 174028a6d671SEric W. Biederman { 17412fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 174204ff9708SAl Viro char *p = NULL; 174328a6d671SEric W. Biederman ssize_t length; 174428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 174528a6d671SEric W. Biederman 174628a6d671SEric W. Biederman if (!task) 174704ff9708SAl Viro return -ESRCH; 174828a6d671SEric W. Biederman 174928a6d671SEric W. Biederman length = security_getprocattr(task, 17502fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 175104ff9708SAl Viro &p); 175228a6d671SEric W. Biederman put_task_struct(task); 175304ff9708SAl Viro if (length > 0) 175404ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 175504ff9708SAl Viro kfree(p); 175628a6d671SEric W. Biederman return length; 175728a6d671SEric W. Biederman } 175828a6d671SEric W. Biederman 175928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 176028a6d671SEric W. Biederman size_t count, loff_t *ppos) 176128a6d671SEric W. Biederman { 17622fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 176328a6d671SEric W. Biederman char *page; 176428a6d671SEric W. Biederman ssize_t length; 176528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 176628a6d671SEric W. Biederman 176728a6d671SEric W. Biederman length = -ESRCH; 176828a6d671SEric W. Biederman if (!task) 176928a6d671SEric W. Biederman goto out_no_task; 177028a6d671SEric W. Biederman if (count > PAGE_SIZE) 177128a6d671SEric W. Biederman count = PAGE_SIZE; 177228a6d671SEric W. Biederman 177328a6d671SEric W. Biederman /* No partial writes. */ 177428a6d671SEric W. Biederman length = -EINVAL; 177528a6d671SEric W. Biederman if (*ppos != 0) 177628a6d671SEric W. Biederman goto out; 177728a6d671SEric W. Biederman 177828a6d671SEric W. Biederman length = -ENOMEM; 1779e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 178028a6d671SEric W. Biederman if (!page) 178128a6d671SEric W. Biederman goto out; 178228a6d671SEric W. Biederman 178328a6d671SEric W. Biederman length = -EFAULT; 178428a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 178528a6d671SEric W. Biederman goto out_free; 178628a6d671SEric W. Biederman 178728a6d671SEric W. Biederman length = security_setprocattr(task, 17882fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 178928a6d671SEric W. Biederman (void*)page, count); 179028a6d671SEric W. Biederman out_free: 179128a6d671SEric W. Biederman free_page((unsigned long) page); 179228a6d671SEric W. Biederman out: 179328a6d671SEric W. Biederman put_task_struct(task); 179428a6d671SEric W. Biederman out_no_task: 179528a6d671SEric W. Biederman return length; 179628a6d671SEric W. Biederman } 179728a6d671SEric W. Biederman 179800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 179928a6d671SEric W. Biederman .read = proc_pid_attr_read, 180028a6d671SEric W. Biederman .write = proc_pid_attr_write, 180128a6d671SEric W. Biederman }; 180228a6d671SEric W. Biederman 1803c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 180461a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 180561a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 180661a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 180761a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 180861a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 180961a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 181028a6d671SEric W. Biederman }; 181128a6d671SEric W. Biederman 181272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 18131da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18141da177e4SLinus Torvalds { 18151da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 181672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 181900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 18201da177e4SLinus Torvalds .read = generic_read_dir, 182172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 18221da177e4SLinus Torvalds }; 18231da177e4SLinus Torvalds 182472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 18251da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 18261da177e4SLinus Torvalds { 18277bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 18287bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 1831c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 183272d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 183399f89551SEric W. Biederman .getattr = pid_getattr, 18346d76fa58SLinus Torvalds .setattr = proc_setattr, 18351da177e4SLinus Torvalds }; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds #endif 18381da177e4SLinus Torvalds 18393cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 18403cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 18413cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 18423cb4a0bbSKawai, Hidehiro { 18433cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 18443cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18453cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 18463cb4a0bbSKawai, Hidehiro size_t len; 18473cb4a0bbSKawai, Hidehiro int ret; 18483cb4a0bbSKawai, Hidehiro 18493cb4a0bbSKawai, Hidehiro if (!task) 18503cb4a0bbSKawai, Hidehiro return -ESRCH; 18513cb4a0bbSKawai, Hidehiro 18523cb4a0bbSKawai, Hidehiro ret = 0; 18533cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 18543cb4a0bbSKawai, Hidehiro if (mm) { 18553cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 18563cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 18573cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 18583cb4a0bbSKawai, Hidehiro mmput(mm); 18593cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 18603cb4a0bbSKawai, Hidehiro } 18613cb4a0bbSKawai, Hidehiro 18623cb4a0bbSKawai, Hidehiro put_task_struct(task); 18633cb4a0bbSKawai, Hidehiro 18643cb4a0bbSKawai, Hidehiro return ret; 18653cb4a0bbSKawai, Hidehiro } 18663cb4a0bbSKawai, Hidehiro 18673cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 18683cb4a0bbSKawai, Hidehiro const char __user *buf, 18693cb4a0bbSKawai, Hidehiro size_t count, 18703cb4a0bbSKawai, Hidehiro loff_t *ppos) 18713cb4a0bbSKawai, Hidehiro { 18723cb4a0bbSKawai, Hidehiro struct task_struct *task; 18733cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18743cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 18753cb4a0bbSKawai, Hidehiro unsigned int val; 18763cb4a0bbSKawai, Hidehiro int ret; 18773cb4a0bbSKawai, Hidehiro int i; 18783cb4a0bbSKawai, Hidehiro unsigned long mask; 18793cb4a0bbSKawai, Hidehiro 18803cb4a0bbSKawai, Hidehiro ret = -EFAULT; 18813cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 18823cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 18833cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 18843cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 18853cb4a0bbSKawai, Hidehiro goto out_no_task; 18863cb4a0bbSKawai, Hidehiro 18873cb4a0bbSKawai, Hidehiro ret = -EINVAL; 18883cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 18893cb4a0bbSKawai, Hidehiro if (*end == '\n') 18903cb4a0bbSKawai, Hidehiro end++; 18913cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 18923cb4a0bbSKawai, Hidehiro goto out_no_task; 18933cb4a0bbSKawai, Hidehiro 18943cb4a0bbSKawai, Hidehiro ret = -ESRCH; 18953cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 18963cb4a0bbSKawai, Hidehiro if (!task) 18973cb4a0bbSKawai, Hidehiro goto out_no_task; 18983cb4a0bbSKawai, Hidehiro 18993cb4a0bbSKawai, Hidehiro ret = end - buffer; 19003cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 19013cb4a0bbSKawai, Hidehiro if (!mm) 19023cb4a0bbSKawai, Hidehiro goto out_no_mm; 19033cb4a0bbSKawai, Hidehiro 19043cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 19053cb4a0bbSKawai, Hidehiro if (val & mask) 19063cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19073cb4a0bbSKawai, Hidehiro else 19083cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19093cb4a0bbSKawai, Hidehiro } 19103cb4a0bbSKawai, Hidehiro 19113cb4a0bbSKawai, Hidehiro mmput(mm); 19123cb4a0bbSKawai, Hidehiro out_no_mm: 19133cb4a0bbSKawai, Hidehiro put_task_struct(task); 19143cb4a0bbSKawai, Hidehiro out_no_task: 19153cb4a0bbSKawai, Hidehiro return ret; 19163cb4a0bbSKawai, Hidehiro } 19173cb4a0bbSKawai, Hidehiro 19183cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 19193cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 19203cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 19213cb4a0bbSKawai, Hidehiro }; 19223cb4a0bbSKawai, Hidehiro #endif 19233cb4a0bbSKawai, Hidehiro 19241da177e4SLinus Torvalds /* 19251da177e4SLinus Torvalds * /proc/self: 19261da177e4SLinus Torvalds */ 19271da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 19281da177e4SLinus Torvalds int buflen) 19291da177e4SLinus Torvalds { 19308578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19311da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 19321da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 1935008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 19361da177e4SLinus Torvalds { 19378578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19381da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1939008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 1942c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 19431da177e4SLinus Torvalds .readlink = proc_self_readlink, 19441da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 19451da177e4SLinus Torvalds }; 19461da177e4SLinus Torvalds 194728a6d671SEric W. Biederman /* 1948801199ceSEric W. Biederman * proc base 1949801199ceSEric W. Biederman * 1950801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 1951801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 1952801199ceSEric W. Biederman * describe something that is process related. 1953801199ceSEric W. Biederman */ 1954c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 195561a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 1956801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 1957801199ceSEric W. Biederman }; 1958801199ceSEric W. Biederman 1959801199ceSEric W. Biederman /* 1960801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 1961801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 1962801199ceSEric W. Biederman * due to the way we treat inodes. 1963801199ceSEric W. Biederman */ 1964801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 1965801199ceSEric W. Biederman { 1966801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 1967801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 1968801199ceSEric W. Biederman if (task) { 1969801199ceSEric W. Biederman put_task_struct(task); 1970801199ceSEric W. Biederman return 1; 1971801199ceSEric W. Biederman } 1972801199ceSEric W. Biederman d_drop(dentry); 1973801199ceSEric W. Biederman return 0; 1974801199ceSEric W. Biederman } 1975801199ceSEric W. Biederman 1976801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 1977801199ceSEric W. Biederman { 1978801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 1979801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 1980801199ceSEric W. Biederman }; 1981801199ceSEric W. Biederman 1982444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 1983c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1984801199ceSEric W. Biederman { 1985c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1986801199ceSEric W. Biederman struct inode *inode; 1987801199ceSEric W. Biederman struct proc_inode *ei; 1988444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1989801199ceSEric W. Biederman 1990801199ceSEric W. Biederman /* Allocate the inode */ 1991801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 1992801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 1993801199ceSEric W. Biederman if (!inode) 1994801199ceSEric W. Biederman goto out; 1995801199ceSEric W. Biederman 1996801199ceSEric W. Biederman /* Initialize the inode */ 1997801199ceSEric W. Biederman ei = PROC_I(inode); 1998801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1999801199ceSEric W. Biederman 2000801199ceSEric W. Biederman /* 2001801199ceSEric W. Biederman * grab the reference to the task. 2002801199ceSEric W. Biederman */ 20031a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2004801199ceSEric W. Biederman if (!ei->pid) 2005801199ceSEric W. Biederman goto out_iput; 2006801199ceSEric W. Biederman 2007801199ceSEric W. Biederman inode->i_uid = 0; 2008801199ceSEric W. Biederman inode->i_gid = 0; 2009801199ceSEric W. Biederman inode->i_mode = p->mode; 2010801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2011801199ceSEric W. Biederman inode->i_nlink = 2; 2012801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2013801199ceSEric W. Biederman inode->i_size = 64; 2014801199ceSEric W. Biederman if (p->iop) 2015801199ceSEric W. Biederman inode->i_op = p->iop; 2016801199ceSEric W. Biederman if (p->fop) 2017801199ceSEric W. Biederman inode->i_fop = p->fop; 2018801199ceSEric W. Biederman ei->op = p->op; 2019801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2020801199ceSEric W. Biederman d_add(dentry, inode); 2021801199ceSEric W. Biederman error = NULL; 2022801199ceSEric W. Biederman out: 2023801199ceSEric W. Biederman return error; 2024801199ceSEric W. Biederman out_iput: 2025801199ceSEric W. Biederman iput(inode); 2026801199ceSEric W. Biederman goto out; 2027801199ceSEric W. Biederman } 2028801199ceSEric W. Biederman 2029444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2030444ceed8SEric W. Biederman { 2031444ceed8SEric W. Biederman struct dentry *error; 2032444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2033c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2034444ceed8SEric W. Biederman 2035444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2036444ceed8SEric W. Biederman 2037444ceed8SEric W. Biederman if (!task) 2038444ceed8SEric W. Biederman goto out_no_task; 2039444ceed8SEric W. Biederman 2040444ceed8SEric W. Biederman /* Lookup the directory entry */ 20417bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 20427bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2043444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2044444ceed8SEric W. Biederman continue; 2045444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2046444ceed8SEric W. Biederman break; 2047444ceed8SEric W. Biederman } 20487bcd6b0eSEric W. Biederman if (p > last) 2049444ceed8SEric W. Biederman goto out; 2050444ceed8SEric W. Biederman 2051444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2052444ceed8SEric W. Biederman 2053444ceed8SEric W. Biederman out: 2054444ceed8SEric W. Biederman put_task_struct(task); 2055444ceed8SEric W. Biederman out_no_task: 2056444ceed8SEric W. Biederman return error; 2057444ceed8SEric W. Biederman } 2058444ceed8SEric W. Biederman 2059c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2060c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 206161a28784SEric W. Biederman { 206261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 206361a28784SEric W. Biederman proc_base_instantiate, task, p); 206461a28784SEric W. Biederman } 206561a28784SEric W. Biederman 2066aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2067aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2068aba76fdbSAndrew Morton { 2069aba76fdbSAndrew Morton return sprintf(buffer, 20704b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2071aba76fdbSAndrew Morton "rchar: %llu\n" 2072aba76fdbSAndrew Morton "wchar: %llu\n" 2073aba76fdbSAndrew Morton "syscr: %llu\n" 2074aba76fdbSAndrew Morton "syscw: %llu\n" 20754b98d11bSAlexey Dobriyan #endif 2076aba76fdbSAndrew Morton "read_bytes: %llu\n" 2077aba76fdbSAndrew Morton "write_bytes: %llu\n" 2078aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 20794b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2080aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2081aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2082aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2083aba76fdbSAndrew Morton (unsigned long long)task->syscw, 20844b98d11bSAlexey Dobriyan #endif 2085aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2086aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2087aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2088aba76fdbSAndrew Morton } 2089aba76fdbSAndrew Morton #endif 2090aba76fdbSAndrew Morton 2091801199ceSEric W. Biederman /* 209228a6d671SEric W. Biederman * Thread groups 209328a6d671SEric W. Biederman */ 209400977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2095c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 209620cdc894SEric W. Biederman 2097c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 209861a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 209961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 210027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2101*315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 210261a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 210361a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 210443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 210543ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 210643ae34cbSIngo Molnar #endif 210761a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 210861a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 210961a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 211061a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 211128a6d671SEric W. Biederman #ifdef CONFIG_NUMA 211261a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 211328a6d671SEric W. Biederman #endif 211461a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 211561a28784SEric W. Biederman LNK("cwd", cwd), 211661a28784SEric W. Biederman LNK("root", root), 211761a28784SEric W. Biederman LNK("exe", exe), 211861a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 211961a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 212028a6d671SEric W. Biederman #ifdef CONFIG_MMU 2121b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 212261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 212328a6d671SEric W. Biederman #endif 212428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 212572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 212628a6d671SEric W. Biederman #endif 212728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 212861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 212928a6d671SEric W. Biederman #endif 213028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 213161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 213228a6d671SEric W. Biederman #endif 213328a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 213461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 213528a6d671SEric W. Biederman #endif 213661a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 213761a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 213828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 213961a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 214028a6d671SEric W. Biederman #endif 2141f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2142f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2143f4f154fdSAkinobu Mita #endif 21443cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21453cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 21463cb4a0bbSKawai, Hidehiro #endif 2147aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2148aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2149aba76fdbSAndrew Morton #endif 215028a6d671SEric W. Biederman }; 215128a6d671SEric W. Biederman 215228a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 215328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 215428a6d671SEric W. Biederman { 215528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 215628a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 215728a6d671SEric W. Biederman } 215828a6d671SEric W. Biederman 215900977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 216028a6d671SEric W. Biederman .read = generic_read_dir, 216128a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 216228a6d671SEric W. Biederman }; 216328a6d671SEric W. Biederman 216428a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 21657bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21667bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 216728a6d671SEric W. Biederman } 216828a6d671SEric W. Biederman 2169c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 217028a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 217128a6d671SEric W. Biederman .getattr = pid_getattr, 217228a6d671SEric W. Biederman .setattr = proc_setattr, 217328a6d671SEric W. Biederman }; 217428a6d671SEric W. Biederman 21751da177e4SLinus Torvalds /** 217648e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 21771da177e4SLinus Torvalds * 217848e6484dSEric W. Biederman * @task: task that should be flushed. 21791da177e4SLinus Torvalds * 218048e6484dSEric W. Biederman * Looks in the dcache for 218148e6484dSEric W. Biederman * /proc/@pid 218248e6484dSEric W. Biederman * /proc/@tgid/task/@pid 218348e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 218448e6484dSEric W. Biederman * from the dcache. 218548e6484dSEric W. Biederman * 218648e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 218748e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 218848e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 218948e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 219048e6484dSEric W. Biederman * dcache entries at process exit time. 219148e6484dSEric W. Biederman * 219248e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 219348e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 219448e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 21951da177e4SLinus Torvalds */ 219648e6484dSEric W. Biederman void proc_flush_task(struct task_struct *task) 21971da177e4SLinus Torvalds { 219848e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 21998578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 220048e6484dSEric W. Biederman struct qstr name; 22011da177e4SLinus Torvalds 220248e6484dSEric W. Biederman name.name = buf; 220348e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 220448e6484dSEric W. Biederman dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); 220548e6484dSEric W. Biederman if (dentry) { 220648e6484dSEric W. Biederman shrink_dcache_parent(dentry); 220748e6484dSEric W. Biederman d_drop(dentry); 220848e6484dSEric W. Biederman dput(dentry); 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 221148e6484dSEric W. Biederman if (thread_group_leader(task)) 221248e6484dSEric W. Biederman goto out; 22131da177e4SLinus Torvalds 221448e6484dSEric W. Biederman name.name = buf; 221548e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); 221648e6484dSEric W. Biederman leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); 221748e6484dSEric W. Biederman if (!leader) 221848e6484dSEric W. Biederman goto out; 221948e6484dSEric W. Biederman 222048e6484dSEric W. Biederman name.name = "task"; 222148e6484dSEric W. Biederman name.len = strlen(name.name); 222248e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 222348e6484dSEric W. Biederman if (!dir) 222448e6484dSEric W. Biederman goto out_put_leader; 222548e6484dSEric W. Biederman 222648e6484dSEric W. Biederman name.name = buf; 222748e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 222848e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 222948e6484dSEric W. Biederman if (dentry) { 223048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 223148e6484dSEric W. Biederman d_drop(dentry); 223248e6484dSEric W. Biederman dput(dentry); 22331da177e4SLinus Torvalds } 223448e6484dSEric W. Biederman 223548e6484dSEric W. Biederman dput(dir); 223648e6484dSEric W. Biederman out_put_leader: 223748e6484dSEric W. Biederman dput(leader); 223848e6484dSEric W. Biederman out: 223948e6484dSEric W. Biederman return; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22429711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 22439711ef99SAdrian Bunk struct dentry * dentry, 2244c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2245444ceed8SEric W. Biederman { 2246444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2247444ceed8SEric W. Biederman struct inode *inode; 2248444ceed8SEric W. Biederman 224961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2250444ceed8SEric W. Biederman if (!inode) 2251444ceed8SEric W. Biederman goto out; 2252444ceed8SEric W. Biederman 2253444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2254444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2255444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2256444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 225727932742SMiklos Szeredi inode->i_nlink = 5; 2258444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2259444ceed8SEric W. Biederman inode->i_nlink += 1; 2260444ceed8SEric W. Biederman #endif 2261444ceed8SEric W. Biederman 2262444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2263444ceed8SEric W. Biederman 2264444ceed8SEric W. Biederman d_add(dentry, inode); 2265444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2266444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2267444ceed8SEric W. Biederman error = NULL; 2268444ceed8SEric W. Biederman out: 2269444ceed8SEric W. Biederman return error; 2270444ceed8SEric W. Biederman } 2271444ceed8SEric W. Biederman 22721da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 22731da177e4SLinus Torvalds { 2274cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 22751da177e4SLinus Torvalds struct task_struct *task; 22761da177e4SLinus Torvalds unsigned tgid; 22771da177e4SLinus Torvalds 2278801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2279801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2280801199ceSEric W. Biederman goto out; 2281801199ceSEric W. Biederman 22821da177e4SLinus Torvalds tgid = name_to_int(dentry); 22831da177e4SLinus Torvalds if (tgid == ~0U) 22841da177e4SLinus Torvalds goto out; 22851da177e4SLinus Torvalds 2286de758734SEric W. Biederman rcu_read_lock(); 22871da177e4SLinus Torvalds task = find_task_by_pid(tgid); 22881da177e4SLinus Torvalds if (task) 22891da177e4SLinus Torvalds get_task_struct(task); 2290de758734SEric W. Biederman rcu_read_unlock(); 22911da177e4SLinus Torvalds if (!task) 22921da177e4SLinus Torvalds goto out; 22931da177e4SLinus Torvalds 2294444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 229548e6484dSEric W. Biederman put_task_struct(task); 22961da177e4SLinus Torvalds out: 2297cd6a3ce9SEric W. Biederman return result; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds /* 23010804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 23020bc58a91SEric W. Biederman * 23031da177e4SLinus Torvalds */ 23040804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 23051da177e4SLinus Torvalds { 23060804ef4bSEric W. Biederman struct task_struct *task; 23070804ef4bSEric W. Biederman struct pid *pid; 23081da177e4SLinus Torvalds 23090804ef4bSEric W. Biederman rcu_read_lock(); 23100804ef4bSEric W. Biederman retry: 23110804ef4bSEric W. Biederman task = NULL; 23120804ef4bSEric W. Biederman pid = find_ge_pid(tgid); 23130804ef4bSEric W. Biederman if (pid) { 23140804ef4bSEric W. Biederman tgid = pid->nr + 1; 23150804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 23160804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 23170804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 23180804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 23190804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 23200804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 23210804ef4bSEric W. Biederman * 23220804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 23230804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 23240804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 23250804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 23260804ef4bSEric W. Biederman * As we don't care in the case of readdir. 23270bc58a91SEric W. Biederman */ 23280804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 23290804ef4bSEric W. Biederman goto retry; 23300804ef4bSEric W. Biederman get_task_struct(task); 23311da177e4SLinus Torvalds } 2332454cc105SEric W. Biederman rcu_read_unlock(); 23330804ef4bSEric W. Biederman return task; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 23367bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 23371da177e4SLinus Torvalds 233861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 233961a28784SEric W. Biederman struct task_struct *task, int tgid) 234061a28784SEric W. Biederman { 234161a28784SEric W. Biederman char name[PROC_NUMBUF]; 234261a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 234361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 234461a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 234561a28784SEric W. Biederman } 234661a28784SEric W. Biederman 23471da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 23481da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 23491da177e4SLinus Torvalds { 23501da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 23512fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 23520bc58a91SEric W. Biederman struct task_struct *task; 23530bc58a91SEric W. Biederman int tgid; 23541da177e4SLinus Torvalds 235561a28784SEric W. Biederman if (!reaper) 235661a28784SEric W. Biederman goto out_no_task; 235761a28784SEric W. Biederman 23587bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2359c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 236061a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2361801199ceSEric W. Biederman goto out; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23640804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 23650804ef4bSEric W. Biederman for (task = next_tgid(tgid); 23660bc58a91SEric W. Biederman task; 23670804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 23680bc58a91SEric W. Biederman tgid = task->pid; 23690804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 237061a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 23710bc58a91SEric W. Biederman put_task_struct(task); 23720804ef4bSEric W. Biederman goto out; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds } 23750804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 23760804ef4bSEric W. Biederman out: 237761a28784SEric W. Biederman put_task_struct(reaper); 237861a28784SEric W. Biederman out_no_task: 23791da177e4SLinus Torvalds return 0; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23820bc58a91SEric W. Biederman /* 238328a6d671SEric W. Biederman * Tasks 238428a6d671SEric W. Biederman */ 2385c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 238661a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 238727932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2388*315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 238961a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 239061a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 239143ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 239243ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 239343ae34cbSIngo Molnar #endif 239461a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 239561a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 239661a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 239761a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 239828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 239961a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 240028a6d671SEric W. Biederman #endif 240161a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 240261a28784SEric W. Biederman LNK("cwd", cwd), 240361a28784SEric W. Biederman LNK("root", root), 240461a28784SEric W. Biederman LNK("exe", exe), 240561a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 240628a6d671SEric W. Biederman #ifdef CONFIG_MMU 2407b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 240861a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 240928a6d671SEric W. Biederman #endif 241028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 241172d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 241228a6d671SEric W. Biederman #endif 241328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 241461a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 241528a6d671SEric W. Biederman #endif 241628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 241761a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 241828a6d671SEric W. Biederman #endif 241928a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 242061a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 242128a6d671SEric W. Biederman #endif 242261a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 242361a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 242428a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 242561a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 242628a6d671SEric W. Biederman #endif 2427f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2428f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2429f4f154fdSAkinobu Mita #endif 243028a6d671SEric W. Biederman }; 243128a6d671SEric W. Biederman 243228a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 243328a6d671SEric W. Biederman void * dirent, filldir_t filldir) 243428a6d671SEric W. Biederman { 243528a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 243628a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 243728a6d671SEric W. Biederman } 243828a6d671SEric W. Biederman 243928a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 24407bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 24417bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 244228a6d671SEric W. Biederman } 244328a6d671SEric W. Biederman 244400977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 244528a6d671SEric W. Biederman .read = generic_read_dir, 244628a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 244728a6d671SEric W. Biederman }; 244828a6d671SEric W. Biederman 2449c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 245028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 245128a6d671SEric W. Biederman .getattr = pid_getattr, 245228a6d671SEric W. Biederman .setattr = proc_setattr, 245328a6d671SEric W. Biederman }; 245428a6d671SEric W. Biederman 2455444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2456c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2457444ceed8SEric W. Biederman { 2458444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2459444ceed8SEric W. Biederman struct inode *inode; 246061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2461444ceed8SEric W. Biederman 2462444ceed8SEric W. Biederman if (!inode) 2463444ceed8SEric W. Biederman goto out; 2464444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2465444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2466444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2467444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 246827932742SMiklos Szeredi inode->i_nlink = 4; 2469444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2470444ceed8SEric W. Biederman inode->i_nlink += 1; 2471444ceed8SEric W. Biederman #endif 2472444ceed8SEric W. Biederman 2473444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2474444ceed8SEric W. Biederman 2475444ceed8SEric W. Biederman d_add(dentry, inode); 2476444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2477444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2478444ceed8SEric W. Biederman error = NULL; 2479444ceed8SEric W. Biederman out: 2480444ceed8SEric W. Biederman return error; 2481444ceed8SEric W. Biederman } 2482444ceed8SEric W. Biederman 248328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 248428a6d671SEric W. Biederman { 248528a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 248628a6d671SEric W. Biederman struct task_struct *task; 248728a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 248828a6d671SEric W. Biederman unsigned tid; 248928a6d671SEric W. Biederman 249028a6d671SEric W. Biederman if (!leader) 249128a6d671SEric W. Biederman goto out_no_task; 249228a6d671SEric W. Biederman 249328a6d671SEric W. Biederman tid = name_to_int(dentry); 249428a6d671SEric W. Biederman if (tid == ~0U) 249528a6d671SEric W. Biederman goto out; 249628a6d671SEric W. Biederman 249728a6d671SEric W. Biederman rcu_read_lock(); 249828a6d671SEric W. Biederman task = find_task_by_pid(tid); 249928a6d671SEric W. Biederman if (task) 250028a6d671SEric W. Biederman get_task_struct(task); 250128a6d671SEric W. Biederman rcu_read_unlock(); 250228a6d671SEric W. Biederman if (!task) 250328a6d671SEric W. Biederman goto out; 250428a6d671SEric W. Biederman if (leader->tgid != task->tgid) 250528a6d671SEric W. Biederman goto out_drop_task; 250628a6d671SEric W. Biederman 2507444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 250828a6d671SEric W. Biederman out_drop_task: 250928a6d671SEric W. Biederman put_task_struct(task); 251028a6d671SEric W. Biederman out: 251128a6d671SEric W. Biederman put_task_struct(leader); 251228a6d671SEric W. Biederman out_no_task: 251328a6d671SEric W. Biederman return result; 251428a6d671SEric W. Biederman } 251528a6d671SEric W. Biederman 251628a6d671SEric W. Biederman /* 25170bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 25180bc58a91SEric W. Biederman * 25190bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 25200bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 25210bc58a91SEric W. Biederman * directory we have more work todo. 25220bc58a91SEric W. Biederman * 25230bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 25240bc58a91SEric W. Biederman * 25250bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 25260bc58a91SEric W. Biederman * threads past it. 25270bc58a91SEric W. Biederman */ 2528cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2529cc288738SEric W. Biederman int tid, int nr) 25300bc58a91SEric W. Biederman { 2531a872ff0cSOleg Nesterov struct task_struct *pos; 25320bc58a91SEric W. Biederman 2533cc288738SEric W. Biederman rcu_read_lock(); 25340bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 25350bc58a91SEric W. Biederman if (tid && (nr > 0)) { 25360bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2537a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2538a872ff0cSOleg Nesterov goto found; 25390bc58a91SEric W. Biederman } 25400bc58a91SEric W. Biederman 25410bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 25420bc58a91SEric W. Biederman pos = NULL; 2543a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2544a872ff0cSOleg Nesterov goto out; 2545a872ff0cSOleg Nesterov 2546a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2547a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2548a872ff0cSOleg Nesterov */ 2549a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2550a872ff0cSOleg Nesterov pos = next_thread(pos); 2551a872ff0cSOleg Nesterov if (pos == leader) { 2552a872ff0cSOleg Nesterov pos = NULL; 2553a872ff0cSOleg Nesterov goto out; 2554a872ff0cSOleg Nesterov } 2555a872ff0cSOleg Nesterov } 2556a872ff0cSOleg Nesterov found: 2557a872ff0cSOleg Nesterov get_task_struct(pos); 2558a872ff0cSOleg Nesterov out: 2559cc288738SEric W. Biederman rcu_read_unlock(); 25600bc58a91SEric W. Biederman return pos; 25610bc58a91SEric W. Biederman } 25620bc58a91SEric W. Biederman 25630bc58a91SEric W. Biederman /* 25640bc58a91SEric W. Biederman * Find the next thread in the thread list. 25650bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 25660bc58a91SEric W. Biederman * 25670bc58a91SEric W. Biederman * The reference to the input task_struct is released. 25680bc58a91SEric W. Biederman */ 25690bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 25700bc58a91SEric W. Biederman { 2571c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2572cc288738SEric W. Biederman rcu_read_lock(); 2573c1df7fb8SOleg Nesterov if (pid_alive(start)) { 25740bc58a91SEric W. Biederman pos = next_thread(start); 2575c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 25760bc58a91SEric W. Biederman pos = NULL; 2577c1df7fb8SOleg Nesterov else 2578c1df7fb8SOleg Nesterov get_task_struct(pos); 2579c1df7fb8SOleg Nesterov } 2580cc288738SEric W. Biederman rcu_read_unlock(); 25810bc58a91SEric W. Biederman put_task_struct(start); 25820bc58a91SEric W. Biederman return pos; 25830bc58a91SEric W. Biederman } 25840bc58a91SEric W. Biederman 258561a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 258661a28784SEric W. Biederman struct task_struct *task, int tid) 258761a28784SEric W. Biederman { 258861a28784SEric W. Biederman char name[PROC_NUMBUF]; 258961a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 259061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 259161a28784SEric W. Biederman proc_task_instantiate, task, NULL); 259261a28784SEric W. Biederman } 259361a28784SEric W. Biederman 25941da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 25951da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 25961da177e4SLinus Torvalds { 25972fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 25981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25997d895244SGuillaume Chazarain struct task_struct *leader = NULL; 26000bc58a91SEric W. Biederman struct task_struct *task; 26011da177e4SLinus Torvalds int retval = -ENOENT; 26021da177e4SLinus Torvalds ino_t ino; 26030bc58a91SEric W. Biederman int tid; 26041da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 26051da177e4SLinus Torvalds 26067d895244SGuillaume Chazarain task = get_proc_task(inode); 26077d895244SGuillaume Chazarain if (!task) 26087d895244SGuillaume Chazarain goto out_no_task; 26097d895244SGuillaume Chazarain rcu_read_lock(); 26107d895244SGuillaume Chazarain if (pid_alive(task)) { 26117d895244SGuillaume Chazarain leader = task->group_leader; 26127d895244SGuillaume Chazarain get_task_struct(leader); 26137d895244SGuillaume Chazarain } 26147d895244SGuillaume Chazarain rcu_read_unlock(); 26157d895244SGuillaume Chazarain put_task_struct(task); 261699f89551SEric W. Biederman if (!leader) 261799f89551SEric W. Biederman goto out_no_task; 26181da177e4SLinus Torvalds retval = 0; 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds switch (pos) { 26211da177e4SLinus Torvalds case 0: 26221da177e4SLinus Torvalds ino = inode->i_ino; 26231da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 26241da177e4SLinus Torvalds goto out; 26251da177e4SLinus Torvalds pos++; 26261da177e4SLinus Torvalds /* fall through */ 26271da177e4SLinus Torvalds case 1: 26281da177e4SLinus Torvalds ino = parent_ino(dentry); 26291da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 26301da177e4SLinus Torvalds goto out; 26311da177e4SLinus Torvalds pos++; 26321da177e4SLinus Torvalds /* fall through */ 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds 26350bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 26360bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 26370bc58a91SEric W. Biederman */ 26382b47c361SMathieu Desnoyers tid = (int)filp->f_version; 26390bc58a91SEric W. Biederman filp->f_version = 0; 26400bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 26410bc58a91SEric W. Biederman task; 26420bc58a91SEric W. Biederman task = next_tid(task), pos++) { 26430bc58a91SEric W. Biederman tid = task->pid; 264461a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 26450bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 26460bc58a91SEric W. Biederman * pid for the next readir call */ 26472b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 26480bc58a91SEric W. Biederman put_task_struct(task); 26491da177e4SLinus Torvalds break; 26500bc58a91SEric W. Biederman } 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds out: 26531da177e4SLinus Torvalds filp->f_pos = pos; 265499f89551SEric W. Biederman put_task_struct(leader); 265599f89551SEric W. Biederman out_no_task: 26561da177e4SLinus Torvalds return retval; 26571da177e4SLinus Torvalds } 26586e66b52bSEric W. Biederman 26596e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 26606e66b52bSEric W. Biederman { 26616e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 266299f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 26636e66b52bSEric W. Biederman generic_fillattr(inode, stat); 26646e66b52bSEric W. Biederman 266599f89551SEric W. Biederman if (p) { 266699f89551SEric W. Biederman rcu_read_lock(); 266799f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 266899f89551SEric W. Biederman rcu_read_unlock(); 266999f89551SEric W. Biederman put_task_struct(p); 26706e66b52bSEric W. Biederman } 26716e66b52bSEric W. Biederman 26726e66b52bSEric W. Biederman return 0; 26736e66b52bSEric W. Biederman } 267428a6d671SEric W. Biederman 2675c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 267628a6d671SEric W. Biederman .lookup = proc_task_lookup, 267728a6d671SEric W. Biederman .getattr = proc_task_getattr, 267828a6d671SEric W. Biederman .setattr = proc_setattr, 267928a6d671SEric W. Biederman }; 268028a6d671SEric W. Biederman 268100977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 268228a6d671SEric W. Biederman .read = generic_read_dir, 268328a6d671SEric W. Biederman .readdir = proc_task_readdir, 268428a6d671SEric W. Biederman }; 2685