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> 70a424316cSPaul Menage #include <linux/cgroup.h> 711da177e4SLinus Torvalds #include <linux/cpuset.h> 721da177e4SLinus Torvalds #include <linux/audit.h> 735addc5ddSAl Viro #include <linux/poll.h> 741651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 758ac773b4SAlexey Dobriyan #include <linux/oom.h> 763cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 771da177e4SLinus Torvalds #include "internal.h" 781da177e4SLinus Torvalds 790f2fe20fSEric W. Biederman /* NOTE: 800f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 810f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 820f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 830f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 840f2fe20fSEric W. Biederman * 850f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 860f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 870f2fe20fSEric W. Biederman */ 880f2fe20fSEric W. Biederman 891da177e4SLinus Torvalds 908578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 910187f879SAndrew Morton #define PROC_NUMBUF 13 928578cea7SEric W. Biederman 931da177e4SLinus Torvalds struct pid_entry { 941da177e4SLinus Torvalds char *name; 95c5141e6dSEric Dumazet int len; 961da177e4SLinus Torvalds mode_t mode; 97c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9800977a59SArjan van de Ven const struct file_operations *fop; 9920cdc894SEric W. Biederman union proc_op op; 1001da177e4SLinus Torvalds }; 1011da177e4SLinus Torvalds 10261a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10320cdc894SEric W. Biederman .name = (NAME), \ 104c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10520cdc894SEric W. Biederman .mode = MODE, \ 10620cdc894SEric W. Biederman .iop = IOP, \ 10720cdc894SEric W. Biederman .fop = FOP, \ 10820cdc894SEric W. Biederman .op = OP, \ 10920cdc894SEric W. Biederman } 11020cdc894SEric W. Biederman 11161a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11261a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11320cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11420cdc894SEric W. Biederman {} ) 11561a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11661a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11720cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11820cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11961a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12061a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12120cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12261a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12361a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12420cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12520cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 1261da177e4SLinus Torvalds 1275096add8SKees Cook int maps_protect; 1285096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1295096add8SKees Cook 1300494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds struct fs_struct *fs; 1330494f6ecSMiklos Szeredi task_lock(task); 1340494f6ecSMiklos Szeredi fs = task->fs; 1351da177e4SLinus Torvalds if(fs) 1361da177e4SLinus Torvalds atomic_inc(&fs->count); 1370494f6ecSMiklos Szeredi task_unlock(task); 1380494f6ecSMiklos Szeredi return fs; 1390494f6ecSMiklos Szeredi } 1400494f6ecSMiklos Szeredi 14199f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14299f89551SEric W. Biederman { 14399f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14499f89551SEric W. Biederman unsigned long flags; 14599f89551SEric W. Biederman int count = 0; 14699f89551SEric W. Biederman 14799f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 14899f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 14999f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 15099f89551SEric W. Biederman } 15199f89551SEric W. Biederman return count; 15299f89551SEric W. Biederman } 15399f89551SEric W. Biederman 1540494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1550494f6ecSMiklos Szeredi { 15699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15799f89551SEric W. Biederman struct fs_struct *fs = NULL; 1580494f6ecSMiklos Szeredi int result = -ENOENT; 15999f89551SEric W. Biederman 16099f89551SEric W. Biederman if (task) { 16199f89551SEric W. Biederman fs = get_fs_struct(task); 16299f89551SEric W. Biederman put_task_struct(task); 16399f89551SEric W. Biederman } 1641da177e4SLinus Torvalds if (fs) { 1651da177e4SLinus Torvalds read_lock(&fs->lock); 1661da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 1671da177e4SLinus Torvalds *dentry = dget(fs->pwd); 1681da177e4SLinus Torvalds read_unlock(&fs->lock); 1691da177e4SLinus Torvalds result = 0; 1701da177e4SLinus Torvalds put_fs_struct(fs); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds return result; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1761da177e4SLinus Torvalds { 17799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17899f89551SEric W. Biederman struct fs_struct *fs = NULL; 1791da177e4SLinus Torvalds int result = -ENOENT; 18099f89551SEric W. Biederman 18199f89551SEric W. Biederman if (task) { 18299f89551SEric W. Biederman fs = get_fs_struct(task); 18399f89551SEric W. Biederman put_task_struct(task); 18499f89551SEric W. Biederman } 1851da177e4SLinus Torvalds if (fs) { 1861da177e4SLinus Torvalds read_lock(&fs->lock); 1871da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 1881da177e4SLinus Torvalds *dentry = dget(fs->root); 1891da177e4SLinus Torvalds read_unlock(&fs->lock); 1901da177e4SLinus Torvalds result = 0; 1911da177e4SLinus Torvalds put_fs_struct(fs); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds return result; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1971da177e4SLinus Torvalds (task == current || \ 1981da177e4SLinus Torvalds (task->parent == current && \ 1991da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 2001da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 2011da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds int res = 0; 2061da177e4SLinus Torvalds unsigned int len; 2071da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2081da177e4SLinus Torvalds if (!mm) 2091da177e4SLinus Torvalds goto out; 2101da177e4SLinus Torvalds if (!mm->arg_end) 2111da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds if (len > PAGE_SIZE) 2161da177e4SLinus Torvalds len = PAGE_SIZE; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2211da177e4SLinus Torvalds // assume application is using setproctitle(3). 2221da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2231da177e4SLinus Torvalds len = strnlen(buffer, res); 2241da177e4SLinus Torvalds if (len < res) { 2251da177e4SLinus Torvalds res = len; 2261da177e4SLinus Torvalds } else { 2271da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2281da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2291da177e4SLinus Torvalds len = PAGE_SIZE - res; 2301da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2311da177e4SLinus Torvalds res = strnlen(buffer, res); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds out_mm: 2351da177e4SLinus Torvalds mmput(mm); 2361da177e4SLinus Torvalds out: 2371da177e4SLinus Torvalds return res; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds int res = 0; 2431da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2441da177e4SLinus Torvalds if (mm) { 2451da177e4SLinus Torvalds unsigned int nwords = 0; 2461da177e4SLinus Torvalds do 2471da177e4SLinus Torvalds nwords += 2; 2481da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2491da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2501da177e4SLinus Torvalds if (res > PAGE_SIZE) 2511da177e4SLinus Torvalds res = PAGE_SIZE; 2521da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2531da177e4SLinus Torvalds mmput(mm); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds return res; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2601da177e4SLinus Torvalds /* 2611da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2621da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2651da177e4SLinus Torvalds { 266ffb45122SAlexey Dobriyan unsigned long wchan; 2679281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds wchan = get_wchan(task); 2701da177e4SLinus Torvalds 2719d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2721da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2739d65cb4aSAlexey Dobriyan else 2749d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2791da177e4SLinus Torvalds /* 2801da177e4SLinus Torvalds * Provides /proc/PID/schedstat 2811da177e4SLinus Torvalds */ 2821da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 2831da177e4SLinus Torvalds { 284172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 2851da177e4SLinus Torvalds task->sched_info.cpu_time, 2861da177e4SLinus Torvalds task->sched_info.run_delay, 2872d72376bSIngo Molnar task->sched_info.pcount); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds #endif 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* The badness from the OOM killer */ 2921da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 2931da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds unsigned long points; 2961da177e4SLinus Torvalds struct timespec uptime; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 29919c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3001da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 30119c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3021da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /************************************************************************/ 3061da177e4SLinus Torvalds /* Here the fs part begins */ 3071da177e4SLinus Torvalds /************************************************************************/ 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* permission checks */ 310778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3111da177e4SLinus Torvalds { 312778c1144SEric W. Biederman struct task_struct *task; 313778c1144SEric W. Biederman int allowed = 0; 314df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 315df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 316df26c40eSEric W. Biederman * information. 317778c1144SEric W. Biederman */ 318778c1144SEric W. Biederman task = get_proc_task(inode); 319df26c40eSEric W. Biederman if (task) { 320778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 321778c1144SEric W. Biederman put_task_struct(task); 322df26c40eSEric W. Biederman } 323778c1144SEric W. Biederman return allowed; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3266d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 3276d76fa58SLinus Torvalds { 3286d76fa58SLinus Torvalds int error; 3296d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 3306d76fa58SLinus Torvalds 3316d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 3326d76fa58SLinus Torvalds return -EPERM; 3336d76fa58SLinus Torvalds 3346d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 3356d76fa58SLinus Torvalds if (!error) 3366d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 3376d76fa58SLinus Torvalds return error; 3386d76fa58SLinus Torvalds } 3396d76fa58SLinus Torvalds 340c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 3416d76fa58SLinus Torvalds .setattr = proc_setattr, 3426d76fa58SLinus Torvalds }; 3436d76fa58SLinus Torvalds 3441da177e4SLinus Torvalds extern struct seq_operations mounts_op; 3455addc5ddSAl Viro struct proc_mounts { 3465addc5ddSAl Viro struct seq_file m; 3475addc5ddSAl Viro int event; 3485addc5ddSAl Viro }; 3495addc5ddSAl Viro 3501da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 3511da177e4SLinus Torvalds { 35299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 3536b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 3545addc5ddSAl Viro struct proc_mounts *p; 3555addc5ddSAl Viro int ret = -EINVAL; 3565addc5ddSAl Viro 35799f89551SEric W. Biederman if (task) { 3581da177e4SLinus Torvalds task_lock(task); 359863c4702SAlexey Dobriyan if (task->nsproxy) { 3606b3286edSKirill Korotaev ns = task->nsproxy->mnt_ns; 3616b3286edSKirill Korotaev if (ns) 3626b3286edSKirill Korotaev get_mnt_ns(ns); 363863c4702SAlexey Dobriyan } 3641da177e4SLinus Torvalds task_unlock(task); 36599f89551SEric W. Biederman put_task_struct(task); 36699f89551SEric W. Biederman } 3671da177e4SLinus Torvalds 3686b3286edSKirill Korotaev if (ns) { 3695addc5ddSAl Viro ret = -ENOMEM; 3705addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 3715addc5ddSAl Viro if (p) { 3725addc5ddSAl Viro file->private_data = &p->m; 3735addc5ddSAl Viro ret = seq_open(file, &mounts_op); 3745addc5ddSAl Viro if (!ret) { 3756b3286edSKirill Korotaev p->m.private = ns; 3766b3286edSKirill Korotaev p->event = ns->event; 3775addc5ddSAl Viro return 0; 3781da177e4SLinus Torvalds } 3795addc5ddSAl Viro kfree(p); 3805addc5ddSAl Viro } 3816b3286edSKirill Korotaev put_mnt_ns(ns); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds return ret; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct seq_file *m = file->private_data; 3896b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 3906b3286edSKirill Korotaev put_mnt_ns(ns); 3911da177e4SLinus Torvalds return seq_release(inode, file); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3945addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 3955addc5ddSAl Viro { 3965addc5ddSAl Viro struct proc_mounts *p = file->private_data; 3976b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 3985addc5ddSAl Viro unsigned res = 0; 3995addc5ddSAl Viro 4005addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4015addc5ddSAl Viro 4025addc5ddSAl Viro spin_lock(&vfsmount_lock); 4035addc5ddSAl Viro if (p->event != ns->event) { 4045addc5ddSAl Viro p->event = ns->event; 4055addc5ddSAl Viro res = POLLERR; 4065addc5ddSAl Viro } 4075addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4085addc5ddSAl Viro 4095addc5ddSAl Viro return res; 4105addc5ddSAl Viro } 4115addc5ddSAl Viro 41200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4131da177e4SLinus Torvalds .open = mounts_open, 4141da177e4SLinus Torvalds .read = seq_read, 4151da177e4SLinus Torvalds .llseek = seq_lseek, 4161da177e4SLinus Torvalds .release = mounts_release, 4175addc5ddSAl Viro .poll = mounts_poll, 4181da177e4SLinus Torvalds }; 4191da177e4SLinus Torvalds 420b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 421b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 422b4629fe2SChuck Lever { 423b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 424b4629fe2SChuck Lever 425b4629fe2SChuck Lever if (!ret) { 426b4629fe2SChuck Lever struct seq_file *m = file->private_data; 4276b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 42899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 42999f89551SEric W. Biederman 43099f89551SEric W. Biederman if (task) { 431b4629fe2SChuck Lever task_lock(task); 432701e054eSVasily Tarasov if (task->nsproxy) 4336b3286edSKirill Korotaev mnt_ns = task->nsproxy->mnt_ns; 4346b3286edSKirill Korotaev if (mnt_ns) 4356b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 436b4629fe2SChuck Lever task_unlock(task); 43799f89551SEric W. Biederman put_task_struct(task); 43899f89551SEric W. Biederman } 439b4629fe2SChuck Lever 4406b3286edSKirill Korotaev if (mnt_ns) 4416b3286edSKirill Korotaev m->private = mnt_ns; 442b4629fe2SChuck Lever else { 443b4629fe2SChuck Lever seq_release(inode, file); 444b4629fe2SChuck Lever ret = -EINVAL; 445b4629fe2SChuck Lever } 446b4629fe2SChuck Lever } 447b4629fe2SChuck Lever return ret; 448b4629fe2SChuck Lever } 449b4629fe2SChuck Lever 45000977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 451b4629fe2SChuck Lever .open = mountstats_open, 452b4629fe2SChuck Lever .read = seq_read, 453b4629fe2SChuck Lever .llseek = seq_lseek, 454b4629fe2SChuck Lever .release = mounts_release, 455b4629fe2SChuck Lever }; 456b4629fe2SChuck Lever 4571da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 4601da177e4SLinus Torvalds size_t count, loff_t *ppos) 4611da177e4SLinus Torvalds { 4622fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 4631da177e4SLinus Torvalds unsigned long page; 4641da177e4SLinus Torvalds ssize_t length; 46599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 46699f89551SEric W. Biederman 46799f89551SEric W. Biederman length = -ESRCH; 46899f89551SEric W. Biederman if (!task) 46999f89551SEric W. Biederman goto out_no_task; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 4721da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 47399f89551SEric W. Biederman 47499f89551SEric W. Biederman length = -ENOMEM; 475e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 47699f89551SEric W. Biederman goto out; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds if (length >= 0) 4811da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 4821da177e4SLinus Torvalds free_page(page); 48399f89551SEric W. Biederman out: 48499f89551SEric W. Biederman put_task_struct(task); 48599f89551SEric W. Biederman out_no_task: 4861da177e4SLinus Torvalds return length; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 48900977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 4901da177e4SLinus Torvalds .read = proc_info_read, 4911da177e4SLinus Torvalds }; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 4941da177e4SLinus Torvalds { 4951da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 4961da177e4SLinus Torvalds return 0; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 5001da177e4SLinus Torvalds size_t count, loff_t *ppos) 5011da177e4SLinus Torvalds { 5022fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5031da177e4SLinus Torvalds char *page; 5041da177e4SLinus Torvalds unsigned long src = *ppos; 5051da177e4SLinus Torvalds int ret = -ESRCH; 5061da177e4SLinus Torvalds struct mm_struct *mm; 5071da177e4SLinus Torvalds 50899f89551SEric W. Biederman if (!task) 50999f89551SEric W. Biederman goto out_no_task; 51099f89551SEric W. Biederman 511ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5121da177e4SLinus Torvalds goto out; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds ret = -ENOMEM; 515e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5161da177e4SLinus Torvalds if (!page) 5171da177e4SLinus Torvalds goto out; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds ret = 0; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds mm = get_task_mm(task); 5221da177e4SLinus Torvalds if (!mm) 5231da177e4SLinus Torvalds goto out_free; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds ret = -EIO; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 5281da177e4SLinus Torvalds goto out_put; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds ret = 0; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds while (count > 0) { 5331da177e4SLinus Torvalds int this_len, retval; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5361da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 537ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 5381da177e4SLinus Torvalds if (!ret) 5391da177e4SLinus Torvalds ret = -EIO; 5401da177e4SLinus Torvalds break; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 5441da177e4SLinus Torvalds ret = -EFAULT; 5451da177e4SLinus Torvalds break; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds ret += retval; 5491da177e4SLinus Torvalds src += retval; 5501da177e4SLinus Torvalds buf += retval; 5511da177e4SLinus Torvalds count -= retval; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds *ppos = src; 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds out_put: 5561da177e4SLinus Torvalds mmput(mm); 5571da177e4SLinus Torvalds out_free: 5581da177e4SLinus Torvalds free_page((unsigned long) page); 5591da177e4SLinus Torvalds out: 56099f89551SEric W. Biederman put_task_struct(task); 56199f89551SEric W. Biederman out_no_task: 5621da177e4SLinus Torvalds return ret; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds #define mem_write NULL 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds #ifndef mem_write 5681da177e4SLinus Torvalds /* This is a security hazard */ 56963967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 5701da177e4SLinus Torvalds size_t count, loff_t *ppos) 5711da177e4SLinus Torvalds { 572f7ca54f4SFrederik Deweerdt int copied; 5731da177e4SLinus Torvalds char *page; 5742fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5751da177e4SLinus Torvalds unsigned long dst = *ppos; 5761da177e4SLinus Torvalds 57799f89551SEric W. Biederman copied = -ESRCH; 57899f89551SEric W. Biederman if (!task) 57999f89551SEric W. Biederman goto out_no_task; 5801da177e4SLinus Torvalds 58199f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 58299f89551SEric W. Biederman goto out; 58399f89551SEric W. Biederman 58499f89551SEric W. Biederman copied = -ENOMEM; 585e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5861da177e4SLinus Torvalds if (!page) 58799f89551SEric W. Biederman goto out; 5881da177e4SLinus Torvalds 589f7ca54f4SFrederik Deweerdt copied = 0; 5901da177e4SLinus Torvalds while (count > 0) { 5911da177e4SLinus Torvalds int this_len, retval; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5941da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 5951da177e4SLinus Torvalds copied = -EFAULT; 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 5991da177e4SLinus Torvalds if (!retval) { 6001da177e4SLinus Torvalds if (!copied) 6011da177e4SLinus Torvalds copied = -EIO; 6021da177e4SLinus Torvalds break; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds copied += retval; 6051da177e4SLinus Torvalds buf += retval; 6061da177e4SLinus Torvalds dst += retval; 6071da177e4SLinus Torvalds count -= retval; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds *ppos = dst; 6101da177e4SLinus Torvalds free_page((unsigned long) page); 61199f89551SEric W. Biederman out: 61299f89551SEric W. Biederman put_task_struct(task); 61399f89551SEric W. Biederman out_no_task: 6141da177e4SLinus Torvalds return copied; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds #endif 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds switch (orig) { 6211da177e4SLinus Torvalds case 0: 6221da177e4SLinus Torvalds file->f_pos = offset; 6231da177e4SLinus Torvalds break; 6241da177e4SLinus Torvalds case 1: 6251da177e4SLinus Torvalds file->f_pos += offset; 6261da177e4SLinus Torvalds break; 6271da177e4SLinus Torvalds default: 6281da177e4SLinus Torvalds return -EINVAL; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds force_successful_syscall_return(); 6311da177e4SLinus Torvalds return file->f_pos; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 63400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 6351da177e4SLinus Torvalds .llseek = mem_lseek, 6361da177e4SLinus Torvalds .read = mem_read, 6371da177e4SLinus Torvalds .write = mem_write, 6381da177e4SLinus Torvalds .open = mem_open, 6391da177e4SLinus Torvalds }; 6401da177e4SLinus Torvalds 641315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 642315e28c8SJames Pearson size_t count, loff_t *ppos) 643315e28c8SJames Pearson { 644315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 645315e28c8SJames Pearson char *page; 646315e28c8SJames Pearson unsigned long src = *ppos; 647315e28c8SJames Pearson int ret = -ESRCH; 648315e28c8SJames Pearson struct mm_struct *mm; 649315e28c8SJames Pearson 650315e28c8SJames Pearson if (!task) 651315e28c8SJames Pearson goto out_no_task; 652315e28c8SJames Pearson 653315e28c8SJames Pearson if (!ptrace_may_attach(task)) 654315e28c8SJames Pearson goto out; 655315e28c8SJames Pearson 656315e28c8SJames Pearson ret = -ENOMEM; 657315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 658315e28c8SJames Pearson if (!page) 659315e28c8SJames Pearson goto out; 660315e28c8SJames Pearson 661315e28c8SJames Pearson ret = 0; 662315e28c8SJames Pearson 663315e28c8SJames Pearson mm = get_task_mm(task); 664315e28c8SJames Pearson if (!mm) 665315e28c8SJames Pearson goto out_free; 666315e28c8SJames Pearson 667315e28c8SJames Pearson while (count > 0) { 668315e28c8SJames Pearson int this_len, retval, max_len; 669315e28c8SJames Pearson 670315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 671315e28c8SJames Pearson 672315e28c8SJames Pearson if (this_len <= 0) 673315e28c8SJames Pearson break; 674315e28c8SJames Pearson 675315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 676315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 677315e28c8SJames Pearson 678315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 679315e28c8SJames Pearson page, this_len, 0); 680315e28c8SJames Pearson 681315e28c8SJames Pearson if (retval <= 0) { 682315e28c8SJames Pearson ret = retval; 683315e28c8SJames Pearson break; 684315e28c8SJames Pearson } 685315e28c8SJames Pearson 686315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 687315e28c8SJames Pearson ret = -EFAULT; 688315e28c8SJames Pearson break; 689315e28c8SJames Pearson } 690315e28c8SJames Pearson 691315e28c8SJames Pearson ret += retval; 692315e28c8SJames Pearson src += retval; 693315e28c8SJames Pearson buf += retval; 694315e28c8SJames Pearson count -= retval; 695315e28c8SJames Pearson } 696315e28c8SJames Pearson *ppos = src; 697315e28c8SJames Pearson 698315e28c8SJames Pearson mmput(mm); 699315e28c8SJames Pearson out_free: 700315e28c8SJames Pearson free_page((unsigned long) page); 701315e28c8SJames Pearson out: 702315e28c8SJames Pearson put_task_struct(task); 703315e28c8SJames Pearson out_no_task: 704315e28c8SJames Pearson return ret; 705315e28c8SJames Pearson } 706315e28c8SJames Pearson 707315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 708315e28c8SJames Pearson .read = environ_read, 709315e28c8SJames Pearson }; 710315e28c8SJames Pearson 7111da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 7121da177e4SLinus Torvalds size_t count, loff_t *ppos) 7131da177e4SLinus Torvalds { 7142fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7158578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 7161da177e4SLinus Torvalds size_t len; 71799f89551SEric W. Biederman int oom_adjust; 7181da177e4SLinus Torvalds 71999f89551SEric W. Biederman if (!task) 72099f89551SEric W. Biederman return -ESRCH; 72199f89551SEric W. Biederman oom_adjust = task->oomkilladj; 72299f89551SEric W. Biederman put_task_struct(task); 72399f89551SEric W. Biederman 7248578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 7250c28f287SAkinobu Mita 7260c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 7301da177e4SLinus Torvalds size_t count, loff_t *ppos) 7311da177e4SLinus Torvalds { 73299f89551SEric W. Biederman struct task_struct *task; 7338578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 7341da177e4SLinus Torvalds int oom_adjust; 7351da177e4SLinus Torvalds 7368578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 7378578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 7388578cea7SEric W. Biederman count = sizeof(buffer) - 1; 7391da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 7401da177e4SLinus Torvalds return -EFAULT; 7411da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 7428ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 7438ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 7441da177e4SLinus Torvalds return -EINVAL; 7451da177e4SLinus Torvalds if (*end == '\n') 7461da177e4SLinus Torvalds end++; 7472fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 74899f89551SEric W. Biederman if (!task) 74999f89551SEric W. Biederman return -ESRCH; 7508fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 7518fb4fc68SGuillem Jover put_task_struct(task); 7528fb4fc68SGuillem Jover return -EACCES; 7538fb4fc68SGuillem Jover } 7541da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 75599f89551SEric W. Biederman put_task_struct(task); 7561da177e4SLinus Torvalds if (end - buffer == 0) 7571da177e4SLinus Torvalds return -EIO; 7581da177e4SLinus Torvalds return end - buffer; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 76100977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 7621da177e4SLinus Torvalds .read = oom_adjust_read, 7631da177e4SLinus Torvalds .write = oom_adjust_write, 7641da177e4SLinus Torvalds }; 7651da177e4SLinus Torvalds 7664b8df891SDavid Rientjes #ifdef CONFIG_MMU 767b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 768b813e931SDavid Rientjes size_t count, loff_t *ppos) 769b813e931SDavid Rientjes { 770b813e931SDavid Rientjes struct task_struct *task; 771b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 772b813e931SDavid Rientjes struct mm_struct *mm; 773b813e931SDavid Rientjes 774b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 775b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 776b813e931SDavid Rientjes count = sizeof(buffer) - 1; 777b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 778b813e931SDavid Rientjes return -EFAULT; 779b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 780b813e931SDavid Rientjes return -EINVAL; 781b813e931SDavid Rientjes if (*end == '\n') 782b813e931SDavid Rientjes end++; 783b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 784b813e931SDavid Rientjes if (!task) 785b813e931SDavid Rientjes return -ESRCH; 786b813e931SDavid Rientjes mm = get_task_mm(task); 787b813e931SDavid Rientjes if (mm) { 788b813e931SDavid Rientjes clear_refs_smap(mm); 789b813e931SDavid Rientjes mmput(mm); 790b813e931SDavid Rientjes } 791b813e931SDavid Rientjes put_task_struct(task); 792b813e931SDavid Rientjes if (end - buffer == 0) 793b813e931SDavid Rientjes return -EIO; 794b813e931SDavid Rientjes return end - buffer; 795b813e931SDavid Rientjes } 796b813e931SDavid Rientjes 797b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 798b813e931SDavid Rientjes .write = clear_refs_write, 799b813e931SDavid Rientjes }; 8004b8df891SDavid Rientjes #endif 801b813e931SDavid Rientjes 8021da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 8031da177e4SLinus Torvalds #define TMPBUFLEN 21 8041da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 8051da177e4SLinus Torvalds size_t count, loff_t *ppos) 8061da177e4SLinus Torvalds { 8072fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 80899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 8091da177e4SLinus Torvalds ssize_t length; 8101da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 8111da177e4SLinus Torvalds 81299f89551SEric W. Biederman if (!task) 81399f89551SEric W. Biederman return -ESRCH; 8141da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 8151da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 81699f89551SEric W. Biederman put_task_struct(task); 8171da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 8211da177e4SLinus Torvalds size_t count, loff_t *ppos) 8221da177e4SLinus Torvalds { 8232fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 8241da177e4SLinus Torvalds char *page, *tmp; 8251da177e4SLinus Torvalds ssize_t length; 8261da177e4SLinus Torvalds uid_t loginuid; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 8291da177e4SLinus Torvalds return -EPERM; 8301da177e4SLinus Torvalds 83113b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 8321da177e4SLinus Torvalds return -EPERM; 8331da177e4SLinus Torvalds 834e0182909SAl Viro if (count >= PAGE_SIZE) 835e0182909SAl Viro count = PAGE_SIZE - 1; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds if (*ppos != 0) { 8381da177e4SLinus Torvalds /* No partial writes. */ 8391da177e4SLinus Torvalds return -EINVAL; 8401da177e4SLinus Torvalds } 841e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 8421da177e4SLinus Torvalds if (!page) 8431da177e4SLinus Torvalds return -ENOMEM; 8441da177e4SLinus Torvalds length = -EFAULT; 8451da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 8461da177e4SLinus Torvalds goto out_free_page; 8471da177e4SLinus Torvalds 848e0182909SAl Viro page[count] = '\0'; 8491da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 8501da177e4SLinus Torvalds if (tmp == page) { 8511da177e4SLinus Torvalds length = -EINVAL; 8521da177e4SLinus Torvalds goto out_free_page; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds } 85599f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 8561da177e4SLinus Torvalds if (likely(length == 0)) 8571da177e4SLinus Torvalds length = count; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds out_free_page: 8601da177e4SLinus Torvalds free_page((unsigned long) page); 8611da177e4SLinus Torvalds return length; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 86400977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 8651da177e4SLinus Torvalds .read = proc_loginuid_read, 8661da177e4SLinus Torvalds .write = proc_loginuid_write, 8671da177e4SLinus Torvalds }; 8681da177e4SLinus Torvalds #endif 8691da177e4SLinus Torvalds 870f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 871f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 872f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 873f4f154fdSAkinobu Mita { 874f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 875f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 876f4f154fdSAkinobu Mita size_t len; 877f4f154fdSAkinobu Mita int make_it_fail; 878f4f154fdSAkinobu Mita 879f4f154fdSAkinobu Mita if (!task) 880f4f154fdSAkinobu Mita return -ESRCH; 881f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 882f4f154fdSAkinobu Mita put_task_struct(task); 883f4f154fdSAkinobu Mita 884f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 8850c28f287SAkinobu Mita 8860c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 887f4f154fdSAkinobu Mita } 888f4f154fdSAkinobu Mita 889f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 890f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 891f4f154fdSAkinobu Mita { 892f4f154fdSAkinobu Mita struct task_struct *task; 893f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 894f4f154fdSAkinobu Mita int make_it_fail; 895f4f154fdSAkinobu Mita 896f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 897f4f154fdSAkinobu Mita return -EPERM; 898f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 899f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 900f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 901f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 902f4f154fdSAkinobu Mita return -EFAULT; 903f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 904f4f154fdSAkinobu Mita if (*end == '\n') 905f4f154fdSAkinobu Mita end++; 906f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 907f4f154fdSAkinobu Mita if (!task) 908f4f154fdSAkinobu Mita return -ESRCH; 909f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 910f4f154fdSAkinobu Mita put_task_struct(task); 911f4f154fdSAkinobu Mita if (end - buffer == 0) 912f4f154fdSAkinobu Mita return -EIO; 913f4f154fdSAkinobu Mita return end - buffer; 914f4f154fdSAkinobu Mita } 915f4f154fdSAkinobu Mita 91600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 917f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 918f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 919f4f154fdSAkinobu Mita }; 920f4f154fdSAkinobu Mita #endif 921f4f154fdSAkinobu Mita 92243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 92343ae34cbSIngo Molnar /* 92443ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 92543ae34cbSIngo Molnar */ 92643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 92743ae34cbSIngo Molnar { 92843ae34cbSIngo Molnar struct inode *inode = m->private; 92943ae34cbSIngo Molnar struct task_struct *p; 93043ae34cbSIngo Molnar 93143ae34cbSIngo Molnar WARN_ON(!inode); 93243ae34cbSIngo Molnar 93343ae34cbSIngo Molnar p = get_proc_task(inode); 93443ae34cbSIngo Molnar if (!p) 93543ae34cbSIngo Molnar return -ESRCH; 93643ae34cbSIngo Molnar proc_sched_show_task(p, m); 93743ae34cbSIngo Molnar 93843ae34cbSIngo Molnar put_task_struct(p); 93943ae34cbSIngo Molnar 94043ae34cbSIngo Molnar return 0; 94143ae34cbSIngo Molnar } 94243ae34cbSIngo Molnar 94343ae34cbSIngo Molnar static ssize_t 94443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 94543ae34cbSIngo Molnar size_t count, loff_t *offset) 94643ae34cbSIngo Molnar { 94743ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 94843ae34cbSIngo Molnar struct task_struct *p; 94943ae34cbSIngo Molnar 95043ae34cbSIngo Molnar WARN_ON(!inode); 95143ae34cbSIngo Molnar 95243ae34cbSIngo Molnar p = get_proc_task(inode); 95343ae34cbSIngo Molnar if (!p) 95443ae34cbSIngo Molnar return -ESRCH; 95543ae34cbSIngo Molnar proc_sched_set_task(p); 95643ae34cbSIngo Molnar 95743ae34cbSIngo Molnar put_task_struct(p); 95843ae34cbSIngo Molnar 95943ae34cbSIngo Molnar return count; 96043ae34cbSIngo Molnar } 96143ae34cbSIngo Molnar 96243ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 96343ae34cbSIngo Molnar { 96443ae34cbSIngo Molnar int ret; 96543ae34cbSIngo Molnar 96643ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 96743ae34cbSIngo Molnar if (!ret) { 96843ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 96943ae34cbSIngo Molnar 97043ae34cbSIngo Molnar m->private = inode; 97143ae34cbSIngo Molnar } 97243ae34cbSIngo Molnar return ret; 97343ae34cbSIngo Molnar } 97443ae34cbSIngo Molnar 97543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 97643ae34cbSIngo Molnar .open = sched_open, 97743ae34cbSIngo Molnar .read = seq_read, 97843ae34cbSIngo Molnar .write = sched_write, 97943ae34cbSIngo Molnar .llseek = seq_lseek, 9805ea473a1SAlexey Dobriyan .release = single_release, 98143ae34cbSIngo Molnar }; 98243ae34cbSIngo Molnar 98343ae34cbSIngo Molnar #endif 98443ae34cbSIngo Molnar 985008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9881da177e4SLinus Torvalds int error = -EACCES; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 9911da177e4SLinus Torvalds path_release(nd); 9921da177e4SLinus Torvalds 993778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 994778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 9951da177e4SLinus Torvalds goto out; 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 9981da177e4SLinus Torvalds nd->last_type = LAST_BIND; 9991da177e4SLinus Torvalds out: 1000008b150aSAl Viro return ERR_PTR(error); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10041da177e4SLinus Torvalds char __user *buffer, int buflen) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds struct inode * inode; 1007e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 1008e12ba74dSMel Gorman char *path; 10091da177e4SLinus Torvalds int len; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (!tmp) 10121da177e4SLinus Torvalds return -ENOMEM; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds inode = dentry->d_inode; 10151da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10161da177e4SLinus Torvalds len = PTR_ERR(path); 10171da177e4SLinus Torvalds if (IS_ERR(path)) 10181da177e4SLinus Torvalds goto out; 10191da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds if (len > buflen) 10221da177e4SLinus Torvalds len = buflen; 10231da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 10241da177e4SLinus Torvalds len = -EFAULT; 10251da177e4SLinus Torvalds out: 10261da177e4SLinus Torvalds free_page((unsigned long)tmp); 10271da177e4SLinus Torvalds return len; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 10311da177e4SLinus Torvalds { 10321da177e4SLinus Torvalds int error = -EACCES; 10331da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10341da177e4SLinus Torvalds struct dentry *de; 10351da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 10361da177e4SLinus Torvalds 1037778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1038778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10391da177e4SLinus Torvalds goto out; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 10421da177e4SLinus Torvalds if (error) 10431da177e4SLinus Torvalds goto out; 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 10461da177e4SLinus Torvalds dput(de); 10471da177e4SLinus Torvalds mntput(mnt); 10481da177e4SLinus Torvalds out: 10491da177e4SLinus Torvalds return error; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 1052c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 10531da177e4SLinus Torvalds .readlink = proc_pid_readlink, 10546d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 10556d76fa58SLinus Torvalds .setattr = proc_setattr, 10561da177e4SLinus Torvalds }; 10571da177e4SLinus Torvalds 105828a6d671SEric W. Biederman 105928a6d671SEric W. Biederman /* building an inode */ 106028a6d671SEric W. Biederman 106128a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 106228a6d671SEric W. Biederman { 106328a6d671SEric W. Biederman int dumpable = 0; 106428a6d671SEric W. Biederman struct mm_struct *mm; 106528a6d671SEric W. Biederman 106628a6d671SEric W. Biederman task_lock(task); 106728a6d671SEric W. Biederman mm = task->mm; 106828a6d671SEric W. Biederman if (mm) 10696c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 107028a6d671SEric W. Biederman task_unlock(task); 107128a6d671SEric W. Biederman if(dumpable == 1) 107228a6d671SEric W. Biederman return 1; 107328a6d671SEric W. Biederman return 0; 107428a6d671SEric W. Biederman } 107528a6d671SEric W. Biederman 107628a6d671SEric W. Biederman 107761a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 107828a6d671SEric W. Biederman { 107928a6d671SEric W. Biederman struct inode * inode; 108028a6d671SEric W. Biederman struct proc_inode *ei; 108128a6d671SEric W. Biederman 108228a6d671SEric W. Biederman /* We need a new inode */ 108328a6d671SEric W. Biederman 108428a6d671SEric W. Biederman inode = new_inode(sb); 108528a6d671SEric W. Biederman if (!inode) 108628a6d671SEric W. Biederman goto out; 108728a6d671SEric W. Biederman 108828a6d671SEric W. Biederman /* Common stuff */ 108928a6d671SEric W. Biederman ei = PROC_I(inode); 109028a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 109128a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 109228a6d671SEric W. Biederman 109328a6d671SEric W. Biederman /* 109428a6d671SEric W. Biederman * grab the reference to task. 109528a6d671SEric W. Biederman */ 10961a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 109728a6d671SEric W. Biederman if (!ei->pid) 109828a6d671SEric W. Biederman goto out_unlock; 109928a6d671SEric W. Biederman 110028a6d671SEric W. Biederman inode->i_uid = 0; 110128a6d671SEric W. Biederman inode->i_gid = 0; 110228a6d671SEric W. Biederman if (task_dumpable(task)) { 110328a6d671SEric W. Biederman inode->i_uid = task->euid; 110428a6d671SEric W. Biederman inode->i_gid = task->egid; 110528a6d671SEric W. Biederman } 110628a6d671SEric W. Biederman security_task_to_inode(task, inode); 110728a6d671SEric W. Biederman 110828a6d671SEric W. Biederman out: 110928a6d671SEric W. Biederman return inode; 111028a6d671SEric W. Biederman 111128a6d671SEric W. Biederman out_unlock: 111228a6d671SEric W. Biederman iput(inode); 111328a6d671SEric W. Biederman return NULL; 111428a6d671SEric W. Biederman } 111528a6d671SEric W. Biederman 111628a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 111728a6d671SEric W. Biederman { 111828a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 111928a6d671SEric W. Biederman struct task_struct *task; 112028a6d671SEric W. Biederman generic_fillattr(inode, stat); 112128a6d671SEric W. Biederman 112228a6d671SEric W. Biederman rcu_read_lock(); 112328a6d671SEric W. Biederman stat->uid = 0; 112428a6d671SEric W. Biederman stat->gid = 0; 112528a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 112628a6d671SEric W. Biederman if (task) { 112728a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 112828a6d671SEric W. Biederman task_dumpable(task)) { 112928a6d671SEric W. Biederman stat->uid = task->euid; 113028a6d671SEric W. Biederman stat->gid = task->egid; 113128a6d671SEric W. Biederman } 113228a6d671SEric W. Biederman } 113328a6d671SEric W. Biederman rcu_read_unlock(); 113428a6d671SEric W. Biederman return 0; 113528a6d671SEric W. Biederman } 113628a6d671SEric W. Biederman 113728a6d671SEric W. Biederman /* dentry stuff */ 113828a6d671SEric W. Biederman 113928a6d671SEric W. Biederman /* 114028a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 114128a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 114228a6d671SEric W. Biederman * due to the way we treat inodes. 114328a6d671SEric W. Biederman * 114428a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 114528a6d671SEric W. Biederman * performed a setuid(), etc. 114628a6d671SEric W. Biederman * 114728a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 114828a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 114928a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 115028a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 115128a6d671SEric W. Biederman * made this apply to all per process world readable and executable 115228a6d671SEric W. Biederman * directories. 115328a6d671SEric W. Biederman */ 115428a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 115528a6d671SEric W. Biederman { 115628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 115728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 115828a6d671SEric W. Biederman if (task) { 115928a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 116028a6d671SEric W. Biederman task_dumpable(task)) { 116128a6d671SEric W. Biederman inode->i_uid = task->euid; 116228a6d671SEric W. Biederman inode->i_gid = task->egid; 116328a6d671SEric W. Biederman } else { 116428a6d671SEric W. Biederman inode->i_uid = 0; 116528a6d671SEric W. Biederman inode->i_gid = 0; 116628a6d671SEric W. Biederman } 116728a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 116828a6d671SEric W. Biederman security_task_to_inode(task, inode); 116928a6d671SEric W. Biederman put_task_struct(task); 117028a6d671SEric W. Biederman return 1; 117128a6d671SEric W. Biederman } 117228a6d671SEric W. Biederman d_drop(dentry); 117328a6d671SEric W. Biederman return 0; 117428a6d671SEric W. Biederman } 117528a6d671SEric W. Biederman 117628a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 117728a6d671SEric W. Biederman { 117828a6d671SEric W. Biederman /* Is the task we represent dead? 117928a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 118028a6d671SEric W. Biederman * kill it immediately. 118128a6d671SEric W. Biederman */ 118228a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 118328a6d671SEric W. Biederman } 118428a6d671SEric W. Biederman 118528a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 118628a6d671SEric W. Biederman { 118728a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 118828a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 118928a6d671SEric W. Biederman }; 119028a6d671SEric W. Biederman 119128a6d671SEric W. Biederman /* Lookups */ 119228a6d671SEric W. Biederman 1193c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1194c5141e6dSEric Dumazet struct task_struct *, const void *); 119561a28784SEric W. Biederman 11961c0d04c9SEric W. Biederman /* 11971c0d04c9SEric W. Biederman * Fill a directory entry. 11981c0d04c9SEric W. Biederman * 11991c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 12001c0d04c9SEric W. Biederman * file type from dcache entry. 12011c0d04c9SEric W. Biederman * 12021c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 12031c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 12041c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 12051c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 12061c0d04c9SEric W. Biederman * by stat. 12071c0d04c9SEric W. Biederman */ 120861a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 120961a28784SEric W. Biederman char *name, int len, 1210c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 121161a28784SEric W. Biederman { 12122fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 121361a28784SEric W. Biederman struct inode *inode; 121461a28784SEric W. Biederman struct qstr qname; 121561a28784SEric W. Biederman ino_t ino = 0; 121661a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 121761a28784SEric W. Biederman 121861a28784SEric W. Biederman qname.name = name; 121961a28784SEric W. Biederman qname.len = len; 122061a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 122161a28784SEric W. Biederman 122261a28784SEric W. Biederman child = d_lookup(dir, &qname); 122361a28784SEric W. Biederman if (!child) { 122461a28784SEric W. Biederman struct dentry *new; 122561a28784SEric W. Biederman new = d_alloc(dir, &qname); 122661a28784SEric W. Biederman if (new) { 122761a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 122861a28784SEric W. Biederman if (child) 122961a28784SEric W. Biederman dput(new); 123061a28784SEric W. Biederman else 123161a28784SEric W. Biederman child = new; 123261a28784SEric W. Biederman } 123361a28784SEric W. Biederman } 123461a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 123561a28784SEric W. Biederman goto end_instantiate; 123661a28784SEric W. Biederman inode = child->d_inode; 123761a28784SEric W. Biederman if (inode) { 123861a28784SEric W. Biederman ino = inode->i_ino; 123961a28784SEric W. Biederman type = inode->i_mode >> 12; 124061a28784SEric W. Biederman } 124161a28784SEric W. Biederman dput(child); 124261a28784SEric W. Biederman end_instantiate: 124361a28784SEric W. Biederman if (!ino) 124461a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 124561a28784SEric W. Biederman if (!ino) 124661a28784SEric W. Biederman ino = 1; 124761a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 124861a28784SEric W. Biederman } 124961a28784SEric W. Biederman 125028a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 125128a6d671SEric W. Biederman { 125228a6d671SEric W. Biederman const char *name = dentry->d_name.name; 125328a6d671SEric W. Biederman int len = dentry->d_name.len; 125428a6d671SEric W. Biederman unsigned n = 0; 125528a6d671SEric W. Biederman 125628a6d671SEric W. Biederman if (len > 1 && *name == '0') 125728a6d671SEric W. Biederman goto out; 125828a6d671SEric W. Biederman while (len-- > 0) { 125928a6d671SEric W. Biederman unsigned c = *name++ - '0'; 126028a6d671SEric W. Biederman if (c > 9) 126128a6d671SEric W. Biederman goto out; 126228a6d671SEric W. Biederman if (n >= (~0U-9)/10) 126328a6d671SEric W. Biederman goto out; 126428a6d671SEric W. Biederman n *= 10; 126528a6d671SEric W. Biederman n += c; 126628a6d671SEric W. Biederman } 126728a6d671SEric W. Biederman return n; 126828a6d671SEric W. Biederman out: 126928a6d671SEric W. Biederman return ~0U; 127028a6d671SEric W. Biederman } 127128a6d671SEric W. Biederman 127227932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 127327932742SMiklos Szeredi 127427932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 127527932742SMiklos Szeredi struct vfsmount **mnt, char *info) 127628a6d671SEric W. Biederman { 127728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 127828a6d671SEric W. Biederman struct files_struct *files = NULL; 127928a6d671SEric W. Biederman struct file *file; 128028a6d671SEric W. Biederman int fd = proc_fd(inode); 128128a6d671SEric W. Biederman 128228a6d671SEric W. Biederman if (task) { 128328a6d671SEric W. Biederman files = get_files_struct(task); 128428a6d671SEric W. Biederman put_task_struct(task); 128528a6d671SEric W. Biederman } 128628a6d671SEric W. Biederman if (files) { 128728a6d671SEric W. Biederman /* 128828a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 128928a6d671SEric W. Biederman * hold ->file_lock. 129028a6d671SEric W. Biederman */ 129128a6d671SEric W. Biederman spin_lock(&files->file_lock); 129228a6d671SEric W. Biederman file = fcheck_files(files, fd); 129328a6d671SEric W. Biederman if (file) { 129427932742SMiklos Szeredi if (mnt) 12952fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 129627932742SMiklos Szeredi if (dentry) 12972fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 129827932742SMiklos Szeredi if (info) 129927932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 130027932742SMiklos Szeredi "pos:\t%lli\n" 130127932742SMiklos Szeredi "flags:\t0%o\n", 130227932742SMiklos Szeredi (long long) file->f_pos, 130327932742SMiklos Szeredi file->f_flags); 130428a6d671SEric W. Biederman spin_unlock(&files->file_lock); 130528a6d671SEric W. Biederman put_files_struct(files); 130628a6d671SEric W. Biederman return 0; 130728a6d671SEric W. Biederman } 130828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 130928a6d671SEric W. Biederman put_files_struct(files); 131028a6d671SEric W. Biederman } 131128a6d671SEric W. Biederman return -ENOENT; 131228a6d671SEric W. Biederman } 131328a6d671SEric W. Biederman 131427932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 131527932742SMiklos Szeredi struct vfsmount **mnt) 131627932742SMiklos Szeredi { 131727932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 131827932742SMiklos Szeredi } 131927932742SMiklos Szeredi 132028a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 132128a6d671SEric W. Biederman { 132228a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 132328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 132428a6d671SEric W. Biederman int fd = proc_fd(inode); 132528a6d671SEric W. Biederman struct files_struct *files; 132628a6d671SEric W. Biederman 132728a6d671SEric W. Biederman if (task) { 132828a6d671SEric W. Biederman files = get_files_struct(task); 132928a6d671SEric W. Biederman if (files) { 133028a6d671SEric W. Biederman rcu_read_lock(); 133128a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 133228a6d671SEric W. Biederman rcu_read_unlock(); 133328a6d671SEric W. Biederman put_files_struct(files); 133428a6d671SEric W. Biederman if (task_dumpable(task)) { 133528a6d671SEric W. Biederman inode->i_uid = task->euid; 133628a6d671SEric W. Biederman inode->i_gid = task->egid; 133728a6d671SEric W. Biederman } else { 133828a6d671SEric W. Biederman inode->i_uid = 0; 133928a6d671SEric W. Biederman inode->i_gid = 0; 134028a6d671SEric W. Biederman } 134128a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 134228a6d671SEric W. Biederman security_task_to_inode(task, inode); 134328a6d671SEric W. Biederman put_task_struct(task); 134428a6d671SEric W. Biederman return 1; 134528a6d671SEric W. Biederman } 134628a6d671SEric W. Biederman rcu_read_unlock(); 134728a6d671SEric W. Biederman put_files_struct(files); 134828a6d671SEric W. Biederman } 134928a6d671SEric W. Biederman put_task_struct(task); 135028a6d671SEric W. Biederman } 135128a6d671SEric W. Biederman d_drop(dentry); 135228a6d671SEric W. Biederman return 0; 135328a6d671SEric W. Biederman } 135428a6d671SEric W. Biederman 135528a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 135628a6d671SEric W. Biederman { 135728a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 135828a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 135928a6d671SEric W. Biederman }; 136028a6d671SEric W. Biederman 1361444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1362c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 136328a6d671SEric W. Biederman { 1364c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 136528a6d671SEric W. Biederman struct file *file; 136628a6d671SEric W. Biederman struct files_struct *files; 136728a6d671SEric W. Biederman struct inode *inode; 136828a6d671SEric W. Biederman struct proc_inode *ei; 1369444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 137028a6d671SEric W. Biederman 137161a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 137228a6d671SEric W. Biederman if (!inode) 137328a6d671SEric W. Biederman goto out; 137428a6d671SEric W. Biederman ei = PROC_I(inode); 137528a6d671SEric W. Biederman ei->fd = fd; 137628a6d671SEric W. Biederman files = get_files_struct(task); 137728a6d671SEric W. Biederman if (!files) 1378444ceed8SEric W. Biederman goto out_iput; 137928a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 138028a6d671SEric W. Biederman 138128a6d671SEric W. Biederman /* 138228a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 138328a6d671SEric W. Biederman * hold ->file_lock. 138428a6d671SEric W. Biederman */ 138528a6d671SEric W. Biederman spin_lock(&files->file_lock); 138628a6d671SEric W. Biederman file = fcheck_files(files, fd); 138728a6d671SEric W. Biederman if (!file) 1388444ceed8SEric W. Biederman goto out_unlock; 138928a6d671SEric W. Biederman if (file->f_mode & 1) 139028a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 139128a6d671SEric W. Biederman if (file->f_mode & 2) 139228a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 139328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 139428a6d671SEric W. Biederman put_files_struct(files); 1395444ceed8SEric W. Biederman 139628a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 139728a6d671SEric W. Biederman inode->i_size = 64; 139828a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 139928a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 140028a6d671SEric W. Biederman d_add(dentry, inode); 140128a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 140228a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1403444ceed8SEric W. Biederman error = NULL; 1404444ceed8SEric W. Biederman 1405444ceed8SEric W. Biederman out: 1406444ceed8SEric W. Biederman return error; 1407444ceed8SEric W. Biederman out_unlock: 1408444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1409444ceed8SEric W. Biederman put_files_struct(files); 1410444ceed8SEric W. Biederman out_iput: 1411444ceed8SEric W. Biederman iput(inode); 1412444ceed8SEric W. Biederman goto out; 1413444ceed8SEric W. Biederman } 1414444ceed8SEric W. Biederman 141527932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 141627932742SMiklos Szeredi struct dentry *dentry, 141727932742SMiklos Szeredi instantiate_t instantiate) 1418444ceed8SEric W. Biederman { 1419444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1420444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1421444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1422444ceed8SEric W. Biederman 1423444ceed8SEric W. Biederman if (!task) 1424444ceed8SEric W. Biederman goto out_no_task; 1425444ceed8SEric W. Biederman if (fd == ~0U) 1426444ceed8SEric W. Biederman goto out; 1427444ceed8SEric W. Biederman 142827932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 142928a6d671SEric W. Biederman out: 143028a6d671SEric W. Biederman put_task_struct(task); 143128a6d671SEric W. Biederman out_no_task: 143228a6d671SEric W. Biederman return result; 143328a6d671SEric W. Biederman } 143428a6d671SEric W. Biederman 143527932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 143627932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 14371da177e4SLinus Torvalds { 14382fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 14395634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 144099f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 14411da177e4SLinus Torvalds unsigned int fd, tid, ino; 14421da177e4SLinus Torvalds int retval; 14431da177e4SLinus Torvalds struct files_struct * files; 1444badf1662SDipankar Sarma struct fdtable *fdt; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds retval = -ENOENT; 144799f89551SEric W. Biederman if (!p) 144899f89551SEric W. Biederman goto out_no_task; 14491da177e4SLinus Torvalds retval = 0; 14501da177e4SLinus Torvalds tid = p->pid; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds fd = filp->f_pos; 14531da177e4SLinus Torvalds switch (fd) { 14541da177e4SLinus Torvalds case 0: 14551da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 14561da177e4SLinus Torvalds goto out; 14571da177e4SLinus Torvalds filp->f_pos++; 14581da177e4SLinus Torvalds case 1: 14595634708bSEric W. Biederman ino = parent_ino(dentry); 14601da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 14611da177e4SLinus Torvalds goto out; 14621da177e4SLinus Torvalds filp->f_pos++; 14631da177e4SLinus Torvalds default: 14641da177e4SLinus Torvalds files = get_files_struct(p); 14651da177e4SLinus Torvalds if (!files) 14661da177e4SLinus Torvalds goto out; 1467b835996fSDipankar Sarma rcu_read_lock(); 1468badf1662SDipankar Sarma fdt = files_fdtable(files); 14691da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1470badf1662SDipankar Sarma fd < fdt->max_fds; 14711da177e4SLinus Torvalds fd++, filp->f_pos++) { 147227932742SMiklos Szeredi char name[PROC_NUMBUF]; 147327932742SMiklos Szeredi int len; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 14761da177e4SLinus Torvalds continue; 1477b835996fSDipankar Sarma rcu_read_unlock(); 14781da177e4SLinus Torvalds 147927932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 148027932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 148127932742SMiklos Szeredi name, len, instantiate, 148227932742SMiklos Szeredi p, &fd) < 0) { 1483b835996fSDipankar Sarma rcu_read_lock(); 14841da177e4SLinus Torvalds break; 14851da177e4SLinus Torvalds } 1486b835996fSDipankar Sarma rcu_read_lock(); 14871da177e4SLinus Torvalds } 1488b835996fSDipankar Sarma rcu_read_unlock(); 14891da177e4SLinus Torvalds put_files_struct(files); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds out: 149299f89551SEric W. Biederman put_task_struct(p); 149399f89551SEric W. Biederman out_no_task: 14941da177e4SLinus Torvalds return retval; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 149727932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 149827932742SMiklos Szeredi struct nameidata *nd) 149927932742SMiklos Szeredi { 150027932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 150127932742SMiklos Szeredi } 150227932742SMiklos Szeredi 150327932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 150427932742SMiklos Szeredi { 150527932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 150627932742SMiklos Szeredi } 150727932742SMiklos Szeredi 150827932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 150927932742SMiklos Szeredi size_t len, loff_t *ppos) 151027932742SMiklos Szeredi { 151127932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 151227932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 151327932742SMiklos Szeredi if (!err) 151427932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 151527932742SMiklos Szeredi return err; 151627932742SMiklos Szeredi } 151727932742SMiklos Szeredi 151827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 151927932742SMiklos Szeredi .open = nonseekable_open, 152027932742SMiklos Szeredi .read = proc_fdinfo_read, 152127932742SMiklos Szeredi }; 152227932742SMiklos Szeredi 152300977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 15241da177e4SLinus Torvalds .read = generic_read_dir, 15251da177e4SLinus Torvalds .readdir = proc_readfd, 15261da177e4SLinus Torvalds }; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds /* 15298948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 15308948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 15318948e11fSAlexey Dobriyan */ 15328948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 15338948e11fSAlexey Dobriyan struct nameidata *nd) 15348948e11fSAlexey Dobriyan { 15358948e11fSAlexey Dobriyan int rv; 15368948e11fSAlexey Dobriyan 15378948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 15388948e11fSAlexey Dobriyan if (rv == 0) 15398948e11fSAlexey Dobriyan return 0; 15408948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 15418948e11fSAlexey Dobriyan rv = 0; 15428948e11fSAlexey Dobriyan return rv; 15438948e11fSAlexey Dobriyan } 15448948e11fSAlexey Dobriyan 15458948e11fSAlexey Dobriyan /* 15461da177e4SLinus Torvalds * proc directories can do almost nothing.. 15471da177e4SLinus Torvalds */ 1548c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 15491da177e4SLinus Torvalds .lookup = proc_lookupfd, 15508948e11fSAlexey Dobriyan .permission = proc_fd_permission, 15516d76fa58SLinus Torvalds .setattr = proc_setattr, 15521da177e4SLinus Torvalds }; 15531da177e4SLinus Torvalds 155427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 155527932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 155627932742SMiklos Szeredi { 155727932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 155827932742SMiklos Szeredi struct inode *inode; 155927932742SMiklos Szeredi struct proc_inode *ei; 156027932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 156127932742SMiklos Szeredi 156227932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 156327932742SMiklos Szeredi if (!inode) 156427932742SMiklos Szeredi goto out; 156527932742SMiklos Szeredi ei = PROC_I(inode); 156627932742SMiklos Szeredi ei->fd = fd; 156727932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 156827932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 156927932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 157027932742SMiklos Szeredi d_add(dentry, inode); 157127932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 157227932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 157327932742SMiklos Szeredi error = NULL; 157427932742SMiklos Szeredi 157527932742SMiklos Szeredi out: 157627932742SMiklos Szeredi return error; 157727932742SMiklos Szeredi } 157827932742SMiklos Szeredi 157927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 158027932742SMiklos Szeredi struct dentry *dentry, 158127932742SMiklos Szeredi struct nameidata *nd) 158227932742SMiklos Szeredi { 158327932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 158427932742SMiklos Szeredi } 158527932742SMiklos Szeredi 158627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 158727932742SMiklos Szeredi { 158827932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 158927932742SMiklos Szeredi proc_fdinfo_instantiate); 159027932742SMiklos Szeredi } 159127932742SMiklos Szeredi 159227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 159327932742SMiklos Szeredi .read = generic_read_dir, 159427932742SMiklos Szeredi .readdir = proc_readfdinfo, 159527932742SMiklos Szeredi }; 159627932742SMiklos Szeredi 159727932742SMiklos Szeredi /* 159827932742SMiklos Szeredi * proc directories can do almost nothing.. 159927932742SMiklos Szeredi */ 160027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 160127932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 160227932742SMiklos Szeredi .setattr = proc_setattr, 160327932742SMiklos Szeredi }; 160427932742SMiklos Szeredi 160527932742SMiklos Szeredi 1606444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1607c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1608444ceed8SEric W. Biederman { 1609c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1610444ceed8SEric W. Biederman struct inode *inode; 1611444ceed8SEric W. Biederman struct proc_inode *ei; 1612444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1613444ceed8SEric W. Biederman 161461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1615444ceed8SEric W. Biederman if (!inode) 1616444ceed8SEric W. Biederman goto out; 1617444ceed8SEric W. Biederman 1618444ceed8SEric W. Biederman ei = PROC_I(inode); 1619444ceed8SEric W. Biederman inode->i_mode = p->mode; 1620444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1621444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1622444ceed8SEric W. Biederman if (p->iop) 1623444ceed8SEric W. Biederman inode->i_op = p->iop; 1624444ceed8SEric W. Biederman if (p->fop) 1625444ceed8SEric W. Biederman inode->i_fop = p->fop; 1626444ceed8SEric W. Biederman ei->op = p->op; 1627444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1628444ceed8SEric W. Biederman d_add(dentry, inode); 1629444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1630444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1631444ceed8SEric W. Biederman error = NULL; 1632444ceed8SEric W. Biederman out: 1633444ceed8SEric W. Biederman return error; 1634444ceed8SEric W. Biederman } 1635444ceed8SEric W. Biederman 16361da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 16371da177e4SLinus Torvalds struct dentry *dentry, 1638c5141e6dSEric Dumazet const struct pid_entry *ents, 16397bcd6b0eSEric W. Biederman unsigned int nents) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds struct inode *inode; 1642cd6a3ce9SEric W. Biederman struct dentry *error; 164399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1644c5141e6dSEric Dumazet const struct pid_entry *p, *last; 16451da177e4SLinus Torvalds 1646cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 16471da177e4SLinus Torvalds inode = NULL; 16481da177e4SLinus Torvalds 164999f89551SEric W. Biederman if (!task) 165099f89551SEric W. Biederman goto out_no_task; 16511da177e4SLinus Torvalds 165220cdc894SEric W. Biederman /* 165320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 165420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 165520cdc894SEric W. Biederman */ 16567bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16577bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 16581da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16591da177e4SLinus Torvalds continue; 16601da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16611da177e4SLinus Torvalds break; 16621da177e4SLinus Torvalds } 16637bcd6b0eSEric W. Biederman if (p > last) 16641da177e4SLinus Torvalds goto out; 16651da177e4SLinus Torvalds 1666444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 16671da177e4SLinus Torvalds out: 166899f89551SEric W. Biederman put_task_struct(task); 166999f89551SEric W. Biederman out_no_task: 1670cd6a3ce9SEric W. Biederman return error; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 1673c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1674c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 167561a28784SEric W. Biederman { 167661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 167761a28784SEric W. Biederman proc_pident_instantiate, task, p); 167861a28784SEric W. Biederman } 167961a28784SEric W. Biederman 168028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 168128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1682c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 168328a6d671SEric W. Biederman { 168428a6d671SEric W. Biederman int i; 168528a6d671SEric W. Biederman int pid; 16862fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 168728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 168828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1689c5141e6dSEric Dumazet const struct pid_entry *p, *last; 169028a6d671SEric W. Biederman ino_t ino; 169128a6d671SEric W. Biederman int ret; 169228a6d671SEric W. Biederman 169328a6d671SEric W. Biederman ret = -ENOENT; 169428a6d671SEric W. Biederman if (!task) 169561a28784SEric W. Biederman goto out_no_task; 169628a6d671SEric W. Biederman 169728a6d671SEric W. Biederman ret = 0; 169828a6d671SEric W. Biederman pid = task->pid; 169928a6d671SEric W. Biederman i = filp->f_pos; 170028a6d671SEric W. Biederman switch (i) { 170128a6d671SEric W. Biederman case 0: 170228a6d671SEric W. Biederman ino = inode->i_ino; 170328a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 170428a6d671SEric W. Biederman goto out; 170528a6d671SEric W. Biederman i++; 170628a6d671SEric W. Biederman filp->f_pos++; 170728a6d671SEric W. Biederman /* fall through */ 170828a6d671SEric W. Biederman case 1: 170928a6d671SEric W. Biederman ino = parent_ino(dentry); 171028a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 171128a6d671SEric W. Biederman goto out; 171228a6d671SEric W. Biederman i++; 171328a6d671SEric W. Biederman filp->f_pos++; 171428a6d671SEric W. Biederman /* fall through */ 171528a6d671SEric W. Biederman default: 171628a6d671SEric W. Biederman i -= 2; 171728a6d671SEric W. Biederman if (i >= nents) { 171828a6d671SEric W. Biederman ret = 1; 171928a6d671SEric W. Biederman goto out; 172028a6d671SEric W. Biederman } 172128a6d671SEric W. Biederman p = ents + i; 17227bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 17237bcd6b0eSEric W. Biederman while (p <= last) { 172461a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 172528a6d671SEric W. Biederman goto out; 172628a6d671SEric W. Biederman filp->f_pos++; 172728a6d671SEric W. Biederman p++; 172828a6d671SEric W. Biederman } 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 173128a6d671SEric W. Biederman ret = 1; 173228a6d671SEric W. Biederman out: 173361a28784SEric W. Biederman put_task_struct(task); 173461a28784SEric W. Biederman out_no_task: 173528a6d671SEric W. Biederman return ret; 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 173928a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 174028a6d671SEric W. Biederman size_t count, loff_t *ppos) 174128a6d671SEric W. Biederman { 17422fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 174304ff9708SAl Viro char *p = NULL; 174428a6d671SEric W. Biederman ssize_t length; 174528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 174628a6d671SEric W. Biederman 174728a6d671SEric W. Biederman if (!task) 174804ff9708SAl Viro return -ESRCH; 174928a6d671SEric W. Biederman 175028a6d671SEric W. Biederman length = security_getprocattr(task, 17512fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 175204ff9708SAl Viro &p); 175328a6d671SEric W. Biederman put_task_struct(task); 175404ff9708SAl Viro if (length > 0) 175504ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 175604ff9708SAl Viro kfree(p); 175728a6d671SEric W. Biederman return length; 175828a6d671SEric W. Biederman } 175928a6d671SEric W. Biederman 176028a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 176128a6d671SEric W. Biederman size_t count, loff_t *ppos) 176228a6d671SEric W. Biederman { 17632fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 176428a6d671SEric W. Biederman char *page; 176528a6d671SEric W. Biederman ssize_t length; 176628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 176728a6d671SEric W. Biederman 176828a6d671SEric W. Biederman length = -ESRCH; 176928a6d671SEric W. Biederman if (!task) 177028a6d671SEric W. Biederman goto out_no_task; 177128a6d671SEric W. Biederman if (count > PAGE_SIZE) 177228a6d671SEric W. Biederman count = PAGE_SIZE; 177328a6d671SEric W. Biederman 177428a6d671SEric W. Biederman /* No partial writes. */ 177528a6d671SEric W. Biederman length = -EINVAL; 177628a6d671SEric W. Biederman if (*ppos != 0) 177728a6d671SEric W. Biederman goto out; 177828a6d671SEric W. Biederman 177928a6d671SEric W. Biederman length = -ENOMEM; 1780e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 178128a6d671SEric W. Biederman if (!page) 178228a6d671SEric W. Biederman goto out; 178328a6d671SEric W. Biederman 178428a6d671SEric W. Biederman length = -EFAULT; 178528a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 178628a6d671SEric W. Biederman goto out_free; 178728a6d671SEric W. Biederman 178828a6d671SEric W. Biederman length = security_setprocattr(task, 17892fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 179028a6d671SEric W. Biederman (void*)page, count); 179128a6d671SEric W. Biederman out_free: 179228a6d671SEric W. Biederman free_page((unsigned long) page); 179328a6d671SEric W. Biederman out: 179428a6d671SEric W. Biederman put_task_struct(task); 179528a6d671SEric W. Biederman out_no_task: 179628a6d671SEric W. Biederman return length; 179728a6d671SEric W. Biederman } 179828a6d671SEric W. Biederman 179900977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 180028a6d671SEric W. Biederman .read = proc_pid_attr_read, 180128a6d671SEric W. Biederman .write = proc_pid_attr_write, 180228a6d671SEric W. Biederman }; 180328a6d671SEric W. Biederman 1804c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 180561a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 180661a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 180761a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 180861a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 180961a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 181061a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 181128a6d671SEric W. Biederman }; 181228a6d671SEric W. Biederman 181372d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 18141da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18151da177e4SLinus Torvalds { 18161da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 181772d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds 182000977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 18211da177e4SLinus Torvalds .read = generic_read_dir, 182272d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 18231da177e4SLinus Torvalds }; 18241da177e4SLinus Torvalds 182572d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 18261da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 18271da177e4SLinus Torvalds { 18287bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 18297bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 1832c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 183372d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 183499f89551SEric W. Biederman .getattr = pid_getattr, 18356d76fa58SLinus Torvalds .setattr = proc_setattr, 18361da177e4SLinus Torvalds }; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds #endif 18391da177e4SLinus Torvalds 18403cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 18413cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 18423cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 18433cb4a0bbSKawai, Hidehiro { 18443cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 18453cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18463cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 18473cb4a0bbSKawai, Hidehiro size_t len; 18483cb4a0bbSKawai, Hidehiro int ret; 18493cb4a0bbSKawai, Hidehiro 18503cb4a0bbSKawai, Hidehiro if (!task) 18513cb4a0bbSKawai, Hidehiro return -ESRCH; 18523cb4a0bbSKawai, Hidehiro 18533cb4a0bbSKawai, Hidehiro ret = 0; 18543cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 18553cb4a0bbSKawai, Hidehiro if (mm) { 18563cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 18573cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 18583cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 18593cb4a0bbSKawai, Hidehiro mmput(mm); 18603cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 18613cb4a0bbSKawai, Hidehiro } 18623cb4a0bbSKawai, Hidehiro 18633cb4a0bbSKawai, Hidehiro put_task_struct(task); 18643cb4a0bbSKawai, Hidehiro 18653cb4a0bbSKawai, Hidehiro return ret; 18663cb4a0bbSKawai, Hidehiro } 18673cb4a0bbSKawai, Hidehiro 18683cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 18693cb4a0bbSKawai, Hidehiro const char __user *buf, 18703cb4a0bbSKawai, Hidehiro size_t count, 18713cb4a0bbSKawai, Hidehiro loff_t *ppos) 18723cb4a0bbSKawai, Hidehiro { 18733cb4a0bbSKawai, Hidehiro struct task_struct *task; 18743cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18753cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 18763cb4a0bbSKawai, Hidehiro unsigned int val; 18773cb4a0bbSKawai, Hidehiro int ret; 18783cb4a0bbSKawai, Hidehiro int i; 18793cb4a0bbSKawai, Hidehiro unsigned long mask; 18803cb4a0bbSKawai, Hidehiro 18813cb4a0bbSKawai, Hidehiro ret = -EFAULT; 18823cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 18833cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 18843cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 18853cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 18863cb4a0bbSKawai, Hidehiro goto out_no_task; 18873cb4a0bbSKawai, Hidehiro 18883cb4a0bbSKawai, Hidehiro ret = -EINVAL; 18893cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 18903cb4a0bbSKawai, Hidehiro if (*end == '\n') 18913cb4a0bbSKawai, Hidehiro end++; 18923cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 18933cb4a0bbSKawai, Hidehiro goto out_no_task; 18943cb4a0bbSKawai, Hidehiro 18953cb4a0bbSKawai, Hidehiro ret = -ESRCH; 18963cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 18973cb4a0bbSKawai, Hidehiro if (!task) 18983cb4a0bbSKawai, Hidehiro goto out_no_task; 18993cb4a0bbSKawai, Hidehiro 19003cb4a0bbSKawai, Hidehiro ret = end - buffer; 19013cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 19023cb4a0bbSKawai, Hidehiro if (!mm) 19033cb4a0bbSKawai, Hidehiro goto out_no_mm; 19043cb4a0bbSKawai, Hidehiro 19053cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 19063cb4a0bbSKawai, Hidehiro if (val & mask) 19073cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19083cb4a0bbSKawai, Hidehiro else 19093cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19103cb4a0bbSKawai, Hidehiro } 19113cb4a0bbSKawai, Hidehiro 19123cb4a0bbSKawai, Hidehiro mmput(mm); 19133cb4a0bbSKawai, Hidehiro out_no_mm: 19143cb4a0bbSKawai, Hidehiro put_task_struct(task); 19153cb4a0bbSKawai, Hidehiro out_no_task: 19163cb4a0bbSKawai, Hidehiro return ret; 19173cb4a0bbSKawai, Hidehiro } 19183cb4a0bbSKawai, Hidehiro 19193cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 19203cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 19213cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 19223cb4a0bbSKawai, Hidehiro }; 19233cb4a0bbSKawai, Hidehiro #endif 19243cb4a0bbSKawai, Hidehiro 19251da177e4SLinus Torvalds /* 19261da177e4SLinus Torvalds * /proc/self: 19271da177e4SLinus Torvalds */ 19281da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 19291da177e4SLinus Torvalds int buflen) 19301da177e4SLinus Torvalds { 19318578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19321da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 19331da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 1936008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 19371da177e4SLinus Torvalds { 19388578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19391da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1940008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 1943c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 19441da177e4SLinus Torvalds .readlink = proc_self_readlink, 19451da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 19461da177e4SLinus Torvalds }; 19471da177e4SLinus Torvalds 194828a6d671SEric W. Biederman /* 1949801199ceSEric W. Biederman * proc base 1950801199ceSEric W. Biederman * 1951801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 1952801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 1953801199ceSEric W. Biederman * describe something that is process related. 1954801199ceSEric W. Biederman */ 1955c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 195661a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 1957801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 1958801199ceSEric W. Biederman }; 1959801199ceSEric W. Biederman 1960801199ceSEric W. Biederman /* 1961801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 1962801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 1963801199ceSEric W. Biederman * due to the way we treat inodes. 1964801199ceSEric W. Biederman */ 1965801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 1966801199ceSEric W. Biederman { 1967801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 1968801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 1969801199ceSEric W. Biederman if (task) { 1970801199ceSEric W. Biederman put_task_struct(task); 1971801199ceSEric W. Biederman return 1; 1972801199ceSEric W. Biederman } 1973801199ceSEric W. Biederman d_drop(dentry); 1974801199ceSEric W. Biederman return 0; 1975801199ceSEric W. Biederman } 1976801199ceSEric W. Biederman 1977801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 1978801199ceSEric W. Biederman { 1979801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 1980801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 1981801199ceSEric W. Biederman }; 1982801199ceSEric W. Biederman 1983444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 1984c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1985801199ceSEric W. Biederman { 1986c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1987801199ceSEric W. Biederman struct inode *inode; 1988801199ceSEric W. Biederman struct proc_inode *ei; 1989444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1990801199ceSEric W. Biederman 1991801199ceSEric W. Biederman /* Allocate the inode */ 1992801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 1993801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 1994801199ceSEric W. Biederman if (!inode) 1995801199ceSEric W. Biederman goto out; 1996801199ceSEric W. Biederman 1997801199ceSEric W. Biederman /* Initialize the inode */ 1998801199ceSEric W. Biederman ei = PROC_I(inode); 1999801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2000801199ceSEric W. Biederman 2001801199ceSEric W. Biederman /* 2002801199ceSEric W. Biederman * grab the reference to the task. 2003801199ceSEric W. Biederman */ 20041a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2005801199ceSEric W. Biederman if (!ei->pid) 2006801199ceSEric W. Biederman goto out_iput; 2007801199ceSEric W. Biederman 2008801199ceSEric W. Biederman inode->i_uid = 0; 2009801199ceSEric W. Biederman inode->i_gid = 0; 2010801199ceSEric W. Biederman inode->i_mode = p->mode; 2011801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2012801199ceSEric W. Biederman inode->i_nlink = 2; 2013801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2014801199ceSEric W. Biederman inode->i_size = 64; 2015801199ceSEric W. Biederman if (p->iop) 2016801199ceSEric W. Biederman inode->i_op = p->iop; 2017801199ceSEric W. Biederman if (p->fop) 2018801199ceSEric W. Biederman inode->i_fop = p->fop; 2019801199ceSEric W. Biederman ei->op = p->op; 2020801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2021801199ceSEric W. Biederman d_add(dentry, inode); 2022801199ceSEric W. Biederman error = NULL; 2023801199ceSEric W. Biederman out: 2024801199ceSEric W. Biederman return error; 2025801199ceSEric W. Biederman out_iput: 2026801199ceSEric W. Biederman iput(inode); 2027801199ceSEric W. Biederman goto out; 2028801199ceSEric W. Biederman } 2029801199ceSEric W. Biederman 2030444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2031444ceed8SEric W. Biederman { 2032444ceed8SEric W. Biederman struct dentry *error; 2033444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2034c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2035444ceed8SEric W. Biederman 2036444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2037444ceed8SEric W. Biederman 2038444ceed8SEric W. Biederman if (!task) 2039444ceed8SEric W. Biederman goto out_no_task; 2040444ceed8SEric W. Biederman 2041444ceed8SEric W. Biederman /* Lookup the directory entry */ 20427bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 20437bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2044444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2045444ceed8SEric W. Biederman continue; 2046444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2047444ceed8SEric W. Biederman break; 2048444ceed8SEric W. Biederman } 20497bcd6b0eSEric W. Biederman if (p > last) 2050444ceed8SEric W. Biederman goto out; 2051444ceed8SEric W. Biederman 2052444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2053444ceed8SEric W. Biederman 2054444ceed8SEric W. Biederman out: 2055444ceed8SEric W. Biederman put_task_struct(task); 2056444ceed8SEric W. Biederman out_no_task: 2057444ceed8SEric W. Biederman return error; 2058444ceed8SEric W. Biederman } 2059444ceed8SEric W. Biederman 2060c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2061c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 206261a28784SEric W. Biederman { 206361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 206461a28784SEric W. Biederman proc_base_instantiate, task, p); 206561a28784SEric W. Biederman } 206661a28784SEric W. Biederman 2067aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2068aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2069aba76fdbSAndrew Morton { 2070aba76fdbSAndrew Morton return sprintf(buffer, 20714b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2072aba76fdbSAndrew Morton "rchar: %llu\n" 2073aba76fdbSAndrew Morton "wchar: %llu\n" 2074aba76fdbSAndrew Morton "syscr: %llu\n" 2075aba76fdbSAndrew Morton "syscw: %llu\n" 20764b98d11bSAlexey Dobriyan #endif 2077aba76fdbSAndrew Morton "read_bytes: %llu\n" 2078aba76fdbSAndrew Morton "write_bytes: %llu\n" 2079aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 20804b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2081aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2082aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2083aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2084aba76fdbSAndrew Morton (unsigned long long)task->syscw, 20854b98d11bSAlexey Dobriyan #endif 2086aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2087aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2088aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2089aba76fdbSAndrew Morton } 2090aba76fdbSAndrew Morton #endif 2091aba76fdbSAndrew Morton 2092801199ceSEric W. Biederman /* 209328a6d671SEric W. Biederman * Thread groups 209428a6d671SEric W. Biederman */ 209500977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2096c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 209720cdc894SEric W. Biederman 2098c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 209961a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 210061a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 210127932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2102315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 210361a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 210461a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 210543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 210643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 210743ae34cbSIngo Molnar #endif 210861a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 210961a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 211061a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 211161a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 211228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 211361a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 211428a6d671SEric W. Biederman #endif 211561a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 211661a28784SEric W. Biederman LNK("cwd", cwd), 211761a28784SEric W. Biederman LNK("root", root), 211861a28784SEric W. Biederman LNK("exe", exe), 211961a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 212061a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 212128a6d671SEric W. Biederman #ifdef CONFIG_MMU 2122b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 212361a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 212428a6d671SEric W. Biederman #endif 212528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 212672d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 212728a6d671SEric W. Biederman #endif 212828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 212961a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 213028a6d671SEric W. Biederman #endif 213128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 213261a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 213328a6d671SEric W. Biederman #endif 2134*8793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 213561a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 213628a6d671SEric W. Biederman #endif 2137a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2138a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2139a424316cSPaul Menage #endif 214061a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 214161a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 214228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 214361a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 214428a6d671SEric W. Biederman #endif 2145f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2146f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2147f4f154fdSAkinobu Mita #endif 21483cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21493cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 21503cb4a0bbSKawai, Hidehiro #endif 2151aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2152aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2153aba76fdbSAndrew Morton #endif 215428a6d671SEric W. Biederman }; 215528a6d671SEric W. Biederman 215628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 215728a6d671SEric W. Biederman void * dirent, filldir_t filldir) 215828a6d671SEric W. Biederman { 215928a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 216028a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 216128a6d671SEric W. Biederman } 216228a6d671SEric W. Biederman 216300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 216428a6d671SEric W. Biederman .read = generic_read_dir, 216528a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 216628a6d671SEric W. Biederman }; 216728a6d671SEric W. Biederman 216828a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 21697bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21707bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 217128a6d671SEric W. Biederman } 217228a6d671SEric W. Biederman 2173c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 217428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 217528a6d671SEric W. Biederman .getattr = pid_getattr, 217628a6d671SEric W. Biederman .setattr = proc_setattr, 217728a6d671SEric W. Biederman }; 217828a6d671SEric W. Biederman 21791da177e4SLinus Torvalds /** 218048e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 21811da177e4SLinus Torvalds * 218248e6484dSEric W. Biederman * @task: task that should be flushed. 21831da177e4SLinus Torvalds * 218448e6484dSEric W. Biederman * Looks in the dcache for 218548e6484dSEric W. Biederman * /proc/@pid 218648e6484dSEric W. Biederman * /proc/@tgid/task/@pid 218748e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 218848e6484dSEric W. Biederman * from the dcache. 218948e6484dSEric W. Biederman * 219048e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 219148e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 219248e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 219348e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 219448e6484dSEric W. Biederman * dcache entries at process exit time. 219548e6484dSEric W. Biederman * 219648e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 219748e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 219848e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 21991da177e4SLinus Torvalds */ 220048e6484dSEric W. Biederman void proc_flush_task(struct task_struct *task) 22011da177e4SLinus Torvalds { 220248e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 22038578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 220448e6484dSEric W. Biederman struct qstr name; 22051da177e4SLinus Torvalds 220648e6484dSEric W. Biederman name.name = buf; 220748e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 220848e6484dSEric W. Biederman dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); 220948e6484dSEric W. Biederman if (dentry) { 221048e6484dSEric W. Biederman shrink_dcache_parent(dentry); 221148e6484dSEric W. Biederman d_drop(dentry); 221248e6484dSEric W. Biederman dput(dentry); 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 221548e6484dSEric W. Biederman if (thread_group_leader(task)) 221648e6484dSEric W. Biederman goto out; 22171da177e4SLinus Torvalds 221848e6484dSEric W. Biederman name.name = buf; 221948e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); 222048e6484dSEric W. Biederman leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); 222148e6484dSEric W. Biederman if (!leader) 222248e6484dSEric W. Biederman goto out; 222348e6484dSEric W. Biederman 222448e6484dSEric W. Biederman name.name = "task"; 222548e6484dSEric W. Biederman name.len = strlen(name.name); 222648e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 222748e6484dSEric W. Biederman if (!dir) 222848e6484dSEric W. Biederman goto out_put_leader; 222948e6484dSEric W. Biederman 223048e6484dSEric W. Biederman name.name = buf; 223148e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 223248e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 223348e6484dSEric W. Biederman if (dentry) { 223448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 223548e6484dSEric W. Biederman d_drop(dentry); 223648e6484dSEric W. Biederman dput(dentry); 22371da177e4SLinus Torvalds } 223848e6484dSEric W. Biederman 223948e6484dSEric W. Biederman dput(dir); 224048e6484dSEric W. Biederman out_put_leader: 224148e6484dSEric W. Biederman dput(leader); 224248e6484dSEric W. Biederman out: 224348e6484dSEric W. Biederman return; 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 22469711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 22479711ef99SAdrian Bunk struct dentry * dentry, 2248c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2249444ceed8SEric W. Biederman { 2250444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2251444ceed8SEric W. Biederman struct inode *inode; 2252444ceed8SEric W. Biederman 225361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2254444ceed8SEric W. Biederman if (!inode) 2255444ceed8SEric W. Biederman goto out; 2256444ceed8SEric W. Biederman 2257444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2258444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2259444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2260444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 226127932742SMiklos Szeredi inode->i_nlink = 5; 2262444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2263444ceed8SEric W. Biederman inode->i_nlink += 1; 2264444ceed8SEric W. Biederman #endif 2265444ceed8SEric W. Biederman 2266444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2267444ceed8SEric W. Biederman 2268444ceed8SEric W. Biederman d_add(dentry, inode); 2269444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2270444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2271444ceed8SEric W. Biederman error = NULL; 2272444ceed8SEric W. Biederman out: 2273444ceed8SEric W. Biederman return error; 2274444ceed8SEric W. Biederman } 2275444ceed8SEric W. Biederman 22761da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 22771da177e4SLinus Torvalds { 2278cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 22791da177e4SLinus Torvalds struct task_struct *task; 22801da177e4SLinus Torvalds unsigned tgid; 22811da177e4SLinus Torvalds 2282801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2283801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2284801199ceSEric W. Biederman goto out; 2285801199ceSEric W. Biederman 22861da177e4SLinus Torvalds tgid = name_to_int(dentry); 22871da177e4SLinus Torvalds if (tgid == ~0U) 22881da177e4SLinus Torvalds goto out; 22891da177e4SLinus Torvalds 2290de758734SEric W. Biederman rcu_read_lock(); 22911da177e4SLinus Torvalds task = find_task_by_pid(tgid); 22921da177e4SLinus Torvalds if (task) 22931da177e4SLinus Torvalds get_task_struct(task); 2294de758734SEric W. Biederman rcu_read_unlock(); 22951da177e4SLinus Torvalds if (!task) 22961da177e4SLinus Torvalds goto out; 22971da177e4SLinus Torvalds 2298444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 229948e6484dSEric W. Biederman put_task_struct(task); 23001da177e4SLinus Torvalds out: 2301cd6a3ce9SEric W. Biederman return result; 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds /* 23050804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 23060bc58a91SEric W. Biederman * 23071da177e4SLinus Torvalds */ 23080804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 23091da177e4SLinus Torvalds { 23100804ef4bSEric W. Biederman struct task_struct *task; 23110804ef4bSEric W. Biederman struct pid *pid; 23121da177e4SLinus Torvalds 23130804ef4bSEric W. Biederman rcu_read_lock(); 23140804ef4bSEric W. Biederman retry: 23150804ef4bSEric W. Biederman task = NULL; 23160804ef4bSEric W. Biederman pid = find_ge_pid(tgid); 23170804ef4bSEric W. Biederman if (pid) { 23180804ef4bSEric W. Biederman tgid = pid->nr + 1; 23190804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 23200804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 23210804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 23220804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 23230804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 23240804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 23250804ef4bSEric W. Biederman * 23260804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 23270804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 23280804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 23290804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 23300804ef4bSEric W. Biederman * As we don't care in the case of readdir. 23310bc58a91SEric W. Biederman */ 23320804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 23330804ef4bSEric W. Biederman goto retry; 23340804ef4bSEric W. Biederman get_task_struct(task); 23351da177e4SLinus Torvalds } 2336454cc105SEric W. Biederman rcu_read_unlock(); 23370804ef4bSEric W. Biederman return task; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds 23407bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 23411da177e4SLinus Torvalds 234261a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 234361a28784SEric W. Biederman struct task_struct *task, int tgid) 234461a28784SEric W. Biederman { 234561a28784SEric W. Biederman char name[PROC_NUMBUF]; 234661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 234761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 234861a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 234961a28784SEric W. Biederman } 235061a28784SEric W. Biederman 23511da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 23521da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 23531da177e4SLinus Torvalds { 23541da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 23552fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 23560bc58a91SEric W. Biederman struct task_struct *task; 23570bc58a91SEric W. Biederman int tgid; 23581da177e4SLinus Torvalds 235961a28784SEric W. Biederman if (!reaper) 236061a28784SEric W. Biederman goto out_no_task; 236161a28784SEric W. Biederman 23627bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2363c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 236461a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2365801199ceSEric W. Biederman goto out; 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23680804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 23690804ef4bSEric W. Biederman for (task = next_tgid(tgid); 23700bc58a91SEric W. Biederman task; 23710804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 23720bc58a91SEric W. Biederman tgid = task->pid; 23730804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 237461a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 23750bc58a91SEric W. Biederman put_task_struct(task); 23760804ef4bSEric W. Biederman goto out; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds } 23790804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 23800804ef4bSEric W. Biederman out: 238161a28784SEric W. Biederman put_task_struct(reaper); 238261a28784SEric W. Biederman out_no_task: 23831da177e4SLinus Torvalds return 0; 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 23860bc58a91SEric W. Biederman /* 238728a6d671SEric W. Biederman * Tasks 238828a6d671SEric W. Biederman */ 2389c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 239061a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 239127932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2392315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 239361a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 239461a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 239543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 239643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 239743ae34cbSIngo Molnar #endif 239861a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 239961a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 240061a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 240161a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 240228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 240361a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 240428a6d671SEric W. Biederman #endif 240561a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 240661a28784SEric W. Biederman LNK("cwd", cwd), 240761a28784SEric W. Biederman LNK("root", root), 240861a28784SEric W. Biederman LNK("exe", exe), 240961a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 241028a6d671SEric W. Biederman #ifdef CONFIG_MMU 2411b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 241261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 241328a6d671SEric W. Biederman #endif 241428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 241572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 241628a6d671SEric W. Biederman #endif 241728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 241861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 241928a6d671SEric W. Biederman #endif 242028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 242161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 242228a6d671SEric W. Biederman #endif 2423*8793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 242461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 242528a6d671SEric W. Biederman #endif 2426a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2427a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2428a424316cSPaul Menage #endif 242961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 243061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 243128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 243261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 243328a6d671SEric W. Biederman #endif 2434f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2435f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2436f4f154fdSAkinobu Mita #endif 243728a6d671SEric W. Biederman }; 243828a6d671SEric W. Biederman 243928a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 244028a6d671SEric W. Biederman void * dirent, filldir_t filldir) 244128a6d671SEric W. Biederman { 244228a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 244328a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 244428a6d671SEric W. Biederman } 244528a6d671SEric W. Biederman 244628a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 24477bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 24487bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 244928a6d671SEric W. Biederman } 245028a6d671SEric W. Biederman 245100977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 245228a6d671SEric W. Biederman .read = generic_read_dir, 245328a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 245428a6d671SEric W. Biederman }; 245528a6d671SEric W. Biederman 2456c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 245728a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 245828a6d671SEric W. Biederman .getattr = pid_getattr, 245928a6d671SEric W. Biederman .setattr = proc_setattr, 246028a6d671SEric W. Biederman }; 246128a6d671SEric W. Biederman 2462444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2463c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2464444ceed8SEric W. Biederman { 2465444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2466444ceed8SEric W. Biederman struct inode *inode; 246761a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2468444ceed8SEric W. Biederman 2469444ceed8SEric W. Biederman if (!inode) 2470444ceed8SEric W. Biederman goto out; 2471444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2472444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2473444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2474444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 247527932742SMiklos Szeredi inode->i_nlink = 4; 2476444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2477444ceed8SEric W. Biederman inode->i_nlink += 1; 2478444ceed8SEric W. Biederman #endif 2479444ceed8SEric W. Biederman 2480444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2481444ceed8SEric W. Biederman 2482444ceed8SEric W. Biederman d_add(dentry, inode); 2483444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2484444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2485444ceed8SEric W. Biederman error = NULL; 2486444ceed8SEric W. Biederman out: 2487444ceed8SEric W. Biederman return error; 2488444ceed8SEric W. Biederman } 2489444ceed8SEric W. Biederman 249028a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 249128a6d671SEric W. Biederman { 249228a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 249328a6d671SEric W. Biederman struct task_struct *task; 249428a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 249528a6d671SEric W. Biederman unsigned tid; 249628a6d671SEric W. Biederman 249728a6d671SEric W. Biederman if (!leader) 249828a6d671SEric W. Biederman goto out_no_task; 249928a6d671SEric W. Biederman 250028a6d671SEric W. Biederman tid = name_to_int(dentry); 250128a6d671SEric W. Biederman if (tid == ~0U) 250228a6d671SEric W. Biederman goto out; 250328a6d671SEric W. Biederman 250428a6d671SEric W. Biederman rcu_read_lock(); 250528a6d671SEric W. Biederman task = find_task_by_pid(tid); 250628a6d671SEric W. Biederman if (task) 250728a6d671SEric W. Biederman get_task_struct(task); 250828a6d671SEric W. Biederman rcu_read_unlock(); 250928a6d671SEric W. Biederman if (!task) 251028a6d671SEric W. Biederman goto out; 251128a6d671SEric W. Biederman if (leader->tgid != task->tgid) 251228a6d671SEric W. Biederman goto out_drop_task; 251328a6d671SEric W. Biederman 2514444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 251528a6d671SEric W. Biederman out_drop_task: 251628a6d671SEric W. Biederman put_task_struct(task); 251728a6d671SEric W. Biederman out: 251828a6d671SEric W. Biederman put_task_struct(leader); 251928a6d671SEric W. Biederman out_no_task: 252028a6d671SEric W. Biederman return result; 252128a6d671SEric W. Biederman } 252228a6d671SEric W. Biederman 252328a6d671SEric W. Biederman /* 25240bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 25250bc58a91SEric W. Biederman * 25260bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 25270bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 25280bc58a91SEric W. Biederman * directory we have more work todo. 25290bc58a91SEric W. Biederman * 25300bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 25310bc58a91SEric W. Biederman * 25320bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 25330bc58a91SEric W. Biederman * threads past it. 25340bc58a91SEric W. Biederman */ 2535cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2536cc288738SEric W. Biederman int tid, int nr) 25370bc58a91SEric W. Biederman { 2538a872ff0cSOleg Nesterov struct task_struct *pos; 25390bc58a91SEric W. Biederman 2540cc288738SEric W. Biederman rcu_read_lock(); 25410bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 25420bc58a91SEric W. Biederman if (tid && (nr > 0)) { 25430bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2544a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2545a872ff0cSOleg Nesterov goto found; 25460bc58a91SEric W. Biederman } 25470bc58a91SEric W. Biederman 25480bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 25490bc58a91SEric W. Biederman pos = NULL; 2550a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2551a872ff0cSOleg Nesterov goto out; 2552a872ff0cSOleg Nesterov 2553a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2554a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2555a872ff0cSOleg Nesterov */ 2556a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2557a872ff0cSOleg Nesterov pos = next_thread(pos); 2558a872ff0cSOleg Nesterov if (pos == leader) { 2559a872ff0cSOleg Nesterov pos = NULL; 2560a872ff0cSOleg Nesterov goto out; 2561a872ff0cSOleg Nesterov } 2562a872ff0cSOleg Nesterov } 2563a872ff0cSOleg Nesterov found: 2564a872ff0cSOleg Nesterov get_task_struct(pos); 2565a872ff0cSOleg Nesterov out: 2566cc288738SEric W. Biederman rcu_read_unlock(); 25670bc58a91SEric W. Biederman return pos; 25680bc58a91SEric W. Biederman } 25690bc58a91SEric W. Biederman 25700bc58a91SEric W. Biederman /* 25710bc58a91SEric W. Biederman * Find the next thread in the thread list. 25720bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 25730bc58a91SEric W. Biederman * 25740bc58a91SEric W. Biederman * The reference to the input task_struct is released. 25750bc58a91SEric W. Biederman */ 25760bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 25770bc58a91SEric W. Biederman { 2578c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2579cc288738SEric W. Biederman rcu_read_lock(); 2580c1df7fb8SOleg Nesterov if (pid_alive(start)) { 25810bc58a91SEric W. Biederman pos = next_thread(start); 2582c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 25830bc58a91SEric W. Biederman pos = NULL; 2584c1df7fb8SOleg Nesterov else 2585c1df7fb8SOleg Nesterov get_task_struct(pos); 2586c1df7fb8SOleg Nesterov } 2587cc288738SEric W. Biederman rcu_read_unlock(); 25880bc58a91SEric W. Biederman put_task_struct(start); 25890bc58a91SEric W. Biederman return pos; 25900bc58a91SEric W. Biederman } 25910bc58a91SEric W. Biederman 259261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 259361a28784SEric W. Biederman struct task_struct *task, int tid) 259461a28784SEric W. Biederman { 259561a28784SEric W. Biederman char name[PROC_NUMBUF]; 259661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 259761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 259861a28784SEric W. Biederman proc_task_instantiate, task, NULL); 259961a28784SEric W. Biederman } 260061a28784SEric W. Biederman 26011da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 26021da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 26031da177e4SLinus Torvalds { 26042fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 26051da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26067d895244SGuillaume Chazarain struct task_struct *leader = NULL; 26070bc58a91SEric W. Biederman struct task_struct *task; 26081da177e4SLinus Torvalds int retval = -ENOENT; 26091da177e4SLinus Torvalds ino_t ino; 26100bc58a91SEric W. Biederman int tid; 26111da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 26121da177e4SLinus Torvalds 26137d895244SGuillaume Chazarain task = get_proc_task(inode); 26147d895244SGuillaume Chazarain if (!task) 26157d895244SGuillaume Chazarain goto out_no_task; 26167d895244SGuillaume Chazarain rcu_read_lock(); 26177d895244SGuillaume Chazarain if (pid_alive(task)) { 26187d895244SGuillaume Chazarain leader = task->group_leader; 26197d895244SGuillaume Chazarain get_task_struct(leader); 26207d895244SGuillaume Chazarain } 26217d895244SGuillaume Chazarain rcu_read_unlock(); 26227d895244SGuillaume Chazarain put_task_struct(task); 262399f89551SEric W. Biederman if (!leader) 262499f89551SEric W. Biederman goto out_no_task; 26251da177e4SLinus Torvalds retval = 0; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds switch (pos) { 26281da177e4SLinus Torvalds case 0: 26291da177e4SLinus Torvalds ino = inode->i_ino; 26301da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 26311da177e4SLinus Torvalds goto out; 26321da177e4SLinus Torvalds pos++; 26331da177e4SLinus Torvalds /* fall through */ 26341da177e4SLinus Torvalds case 1: 26351da177e4SLinus Torvalds ino = parent_ino(dentry); 26361da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 26371da177e4SLinus Torvalds goto out; 26381da177e4SLinus Torvalds pos++; 26391da177e4SLinus Torvalds /* fall through */ 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 26420bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 26430bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 26440bc58a91SEric W. Biederman */ 26452b47c361SMathieu Desnoyers tid = (int)filp->f_version; 26460bc58a91SEric W. Biederman filp->f_version = 0; 26470bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 26480bc58a91SEric W. Biederman task; 26490bc58a91SEric W. Biederman task = next_tid(task), pos++) { 26500bc58a91SEric W. Biederman tid = task->pid; 265161a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 26520bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 26530bc58a91SEric W. Biederman * pid for the next readir call */ 26542b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 26550bc58a91SEric W. Biederman put_task_struct(task); 26561da177e4SLinus Torvalds break; 26570bc58a91SEric W. Biederman } 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds out: 26601da177e4SLinus Torvalds filp->f_pos = pos; 266199f89551SEric W. Biederman put_task_struct(leader); 266299f89551SEric W. Biederman out_no_task: 26631da177e4SLinus Torvalds return retval; 26641da177e4SLinus Torvalds } 26656e66b52bSEric W. Biederman 26666e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 26676e66b52bSEric W. Biederman { 26686e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 266999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 26706e66b52bSEric W. Biederman generic_fillattr(inode, stat); 26716e66b52bSEric W. Biederman 267299f89551SEric W. Biederman if (p) { 267399f89551SEric W. Biederman rcu_read_lock(); 267499f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 267599f89551SEric W. Biederman rcu_read_unlock(); 267699f89551SEric W. Biederman put_task_struct(p); 26776e66b52bSEric W. Biederman } 26786e66b52bSEric W. Biederman 26796e66b52bSEric W. Biederman return 0; 26806e66b52bSEric W. Biederman } 268128a6d671SEric W. Biederman 2682c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 268328a6d671SEric W. Biederman .lookup = proc_task_lookup, 268428a6d671SEric W. Biederman .getattr = proc_task_getattr, 268528a6d671SEric W. Biederman .setattr = proc_setattr, 268628a6d671SEric W. Biederman }; 268728a6d671SEric W. Biederman 268800977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 268928a6d671SEric W. Biederman .read = generic_read_dir, 269028a6d671SEric W. Biederman .readdir = proc_task_readdir, 269128a6d671SEric W. Biederman }; 2692