11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 561da177e4SLinus Torvalds #include <linux/init.h> 5716f7e0feSRandy Dunlap #include <linux/capability.h> 581da177e4SLinus Torvalds #include <linux/file.h> 591da177e4SLinus Torvalds #include <linux/string.h> 601da177e4SLinus Torvalds #include <linux/seq_file.h> 611da177e4SLinus Torvalds #include <linux/namei.h> 626b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 631da177e4SLinus Torvalds #include <linux/mm.h> 64b835996fSDipankar Sarma #include <linux/rcupdate.h> 651da177e4SLinus Torvalds #include <linux/kallsyms.h> 665096add8SKees Cook #include <linux/module.h> 671da177e4SLinus Torvalds #include <linux/mount.h> 681da177e4SLinus Torvalds #include <linux/security.h> 691da177e4SLinus Torvalds #include <linux/ptrace.h> 701da177e4SLinus Torvalds #include <linux/seccomp.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> 761da177e4SLinus Torvalds #include "internal.h" 771da177e4SLinus Torvalds 780f2fe20fSEric W. Biederman /* NOTE: 790f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 800f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 810f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 820f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 830f2fe20fSEric W. Biederman * 840f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 850f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 860f2fe20fSEric W. Biederman */ 870f2fe20fSEric W. Biederman 881da177e4SLinus Torvalds 898578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 900187f879SAndrew Morton #define PROC_NUMBUF 13 918578cea7SEric W. Biederman 921da177e4SLinus Torvalds struct pid_entry { 931da177e4SLinus Torvalds char *name; 94c5141e6dSEric Dumazet int len; 951da177e4SLinus Torvalds mode_t mode; 96c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9700977a59SArjan van de Ven const struct file_operations *fop; 9820cdc894SEric W. Biederman union proc_op op; 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 10161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10220cdc894SEric W. Biederman .name = (NAME), \ 103c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10420cdc894SEric W. Biederman .mode = MODE, \ 10520cdc894SEric W. Biederman .iop = IOP, \ 10620cdc894SEric W. Biederman .fop = FOP, \ 10720cdc894SEric W. Biederman .op = OP, \ 10820cdc894SEric W. Biederman } 10920cdc894SEric W. Biederman 11061a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11161a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11220cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11320cdc894SEric W. Biederman {} ) 11461a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11561a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11620cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11720cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11861a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11961a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12020cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12161a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12420cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 1251da177e4SLinus Torvalds 1265096add8SKees Cook int maps_protect; 1275096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1285096add8SKees Cook 1290494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds struct fs_struct *fs; 1320494f6ecSMiklos Szeredi task_lock(task); 1330494f6ecSMiklos Szeredi fs = task->fs; 1341da177e4SLinus Torvalds if(fs) 1351da177e4SLinus Torvalds atomic_inc(&fs->count); 1360494f6ecSMiklos Szeredi task_unlock(task); 1370494f6ecSMiklos Szeredi return fs; 1380494f6ecSMiklos Szeredi } 1390494f6ecSMiklos Szeredi 14099f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14199f89551SEric W. Biederman { 14299f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14399f89551SEric W. Biederman unsigned long flags; 14499f89551SEric W. Biederman int count = 0; 14599f89551SEric W. Biederman 14699f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 14799f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 14899f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 14999f89551SEric W. Biederman } 15099f89551SEric W. Biederman return count; 15199f89551SEric W. Biederman } 15299f89551SEric W. Biederman 1530494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1540494f6ecSMiklos Szeredi { 15599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15699f89551SEric W. Biederman struct fs_struct *fs = NULL; 1570494f6ecSMiklos Szeredi int result = -ENOENT; 15899f89551SEric W. Biederman 15999f89551SEric W. Biederman if (task) { 16099f89551SEric W. Biederman fs = get_fs_struct(task); 16199f89551SEric W. Biederman put_task_struct(task); 16299f89551SEric W. Biederman } 1631da177e4SLinus Torvalds if (fs) { 1641da177e4SLinus Torvalds read_lock(&fs->lock); 1651da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 1661da177e4SLinus Torvalds *dentry = dget(fs->pwd); 1671da177e4SLinus Torvalds read_unlock(&fs->lock); 1681da177e4SLinus Torvalds result = 0; 1691da177e4SLinus Torvalds put_fs_struct(fs); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds return result; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1751da177e4SLinus Torvalds { 17699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17799f89551SEric W. Biederman struct fs_struct *fs = NULL; 1781da177e4SLinus Torvalds int result = -ENOENT; 17999f89551SEric W. Biederman 18099f89551SEric W. Biederman if (task) { 18199f89551SEric W. Biederman fs = get_fs_struct(task); 18299f89551SEric W. Biederman put_task_struct(task); 18399f89551SEric W. Biederman } 1841da177e4SLinus Torvalds if (fs) { 1851da177e4SLinus Torvalds read_lock(&fs->lock); 1861da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 1871da177e4SLinus Torvalds *dentry = dget(fs->root); 1881da177e4SLinus Torvalds read_unlock(&fs->lock); 1891da177e4SLinus Torvalds result = 0; 1901da177e4SLinus Torvalds put_fs_struct(fs); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds return result; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1961da177e4SLinus Torvalds (task == current || \ 1971da177e4SLinus Torvalds (task->parent == current && \ 1981da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 1991da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 2001da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static int proc_pid_environ(struct task_struct *task, char * buffer) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int res = 0; 2051da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2061da177e4SLinus Torvalds if (mm) { 2071da177e4SLinus Torvalds unsigned int len = mm->env_end - mm->env_start; 2081da177e4SLinus Torvalds if (len > PAGE_SIZE) 2091da177e4SLinus Torvalds len = PAGE_SIZE; 2101da177e4SLinus Torvalds res = access_process_vm(task, mm->env_start, buffer, len, 0); 211ab8d11beSMiklos Szeredi if (!ptrace_may_attach(task)) 2121da177e4SLinus Torvalds res = -ESRCH; 2131da177e4SLinus Torvalds mmput(mm); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds return res; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2191da177e4SLinus Torvalds { 2201da177e4SLinus Torvalds int res = 0; 2211da177e4SLinus Torvalds unsigned int len; 2221da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2231da177e4SLinus Torvalds if (!mm) 2241da177e4SLinus Torvalds goto out; 2251da177e4SLinus Torvalds if (!mm->arg_end) 2261da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (len > PAGE_SIZE) 2311da177e4SLinus Torvalds len = PAGE_SIZE; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2361da177e4SLinus Torvalds // assume application is using setproctitle(3). 2371da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2381da177e4SLinus Torvalds len = strnlen(buffer, res); 2391da177e4SLinus Torvalds if (len < res) { 2401da177e4SLinus Torvalds res = len; 2411da177e4SLinus Torvalds } else { 2421da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2431da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2441da177e4SLinus Torvalds len = PAGE_SIZE - res; 2451da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2461da177e4SLinus Torvalds res = strnlen(buffer, res); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds out_mm: 2501da177e4SLinus Torvalds mmput(mm); 2511da177e4SLinus Torvalds out: 2521da177e4SLinus Torvalds return res; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds int res = 0; 2581da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2591da177e4SLinus Torvalds if (mm) { 2601da177e4SLinus Torvalds unsigned int nwords = 0; 2611da177e4SLinus Torvalds do 2621da177e4SLinus Torvalds nwords += 2; 2631da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2641da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2651da177e4SLinus Torvalds if (res > PAGE_SIZE) 2661da177e4SLinus Torvalds res = PAGE_SIZE; 2671da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2681da177e4SLinus Torvalds mmput(mm); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds return res; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2771da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2801da177e4SLinus Torvalds { 281ffb45122SAlexey Dobriyan unsigned long wchan; 2829d65cb4aSAlexey Dobriyan char symname[KSYM_NAME_LEN+1]; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds wchan = get_wchan(task); 2851da177e4SLinus Torvalds 2869d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2871da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2889d65cb4aSAlexey Dobriyan else 2899d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2941da177e4SLinus Torvalds /* 2951da177e4SLinus Torvalds * Provides /proc/PID/schedstat 2961da177e4SLinus Torvalds */ 2971da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 2981da177e4SLinus Torvalds { 299172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3001da177e4SLinus Torvalds task->sched_info.cpu_time, 3011da177e4SLinus Torvalds task->sched_info.run_delay, 3021da177e4SLinus Torvalds task->sched_info.pcnt); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds #endif 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* The badness from the OOM killer */ 3071da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 3081da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds unsigned long points; 3111da177e4SLinus Torvalds struct timespec uptime; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 31419c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3151da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 31619c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3171da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /************************************************************************/ 3211da177e4SLinus Torvalds /* Here the fs part begins */ 3221da177e4SLinus Torvalds /************************************************************************/ 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* permission checks */ 325778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3261da177e4SLinus Torvalds { 327778c1144SEric W. Biederman struct task_struct *task; 328778c1144SEric W. Biederman int allowed = 0; 329df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 330df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 331df26c40eSEric W. Biederman * information. 332778c1144SEric W. Biederman */ 333778c1144SEric W. Biederman task = get_proc_task(inode); 334df26c40eSEric W. Biederman if (task) { 335778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 336778c1144SEric W. Biederman put_task_struct(task); 337df26c40eSEric W. Biederman } 338778c1144SEric W. Biederman return allowed; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3416d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 3426d76fa58SLinus Torvalds { 3436d76fa58SLinus Torvalds int error; 3446d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 3456d76fa58SLinus Torvalds 3466d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 3476d76fa58SLinus Torvalds return -EPERM; 3486d76fa58SLinus Torvalds 3496d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 3506d76fa58SLinus Torvalds if (!error) 3516d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 3526d76fa58SLinus Torvalds return error; 3536d76fa58SLinus Torvalds } 3546d76fa58SLinus Torvalds 355c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 3566d76fa58SLinus Torvalds .setattr = proc_setattr, 3576d76fa58SLinus Torvalds }; 3586d76fa58SLinus Torvalds 3591da177e4SLinus Torvalds extern struct seq_operations mounts_op; 3605addc5ddSAl Viro struct proc_mounts { 3615addc5ddSAl Viro struct seq_file m; 3625addc5ddSAl Viro int event; 3635addc5ddSAl Viro }; 3645addc5ddSAl Viro 3651da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 3661da177e4SLinus Torvalds { 36799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 3686b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 3695addc5ddSAl Viro struct proc_mounts *p; 3705addc5ddSAl Viro int ret = -EINVAL; 3715addc5ddSAl Viro 37299f89551SEric W. Biederman if (task) { 3731da177e4SLinus Torvalds task_lock(task); 374863c4702SAlexey Dobriyan if (task->nsproxy) { 3756b3286edSKirill Korotaev ns = task->nsproxy->mnt_ns; 3766b3286edSKirill Korotaev if (ns) 3776b3286edSKirill Korotaev get_mnt_ns(ns); 378863c4702SAlexey Dobriyan } 3791da177e4SLinus Torvalds task_unlock(task); 38099f89551SEric W. Biederman put_task_struct(task); 38199f89551SEric W. Biederman } 3821da177e4SLinus Torvalds 3836b3286edSKirill Korotaev if (ns) { 3845addc5ddSAl Viro ret = -ENOMEM; 3855addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 3865addc5ddSAl Viro if (p) { 3875addc5ddSAl Viro file->private_data = &p->m; 3885addc5ddSAl Viro ret = seq_open(file, &mounts_op); 3895addc5ddSAl Viro if (!ret) { 3906b3286edSKirill Korotaev p->m.private = ns; 3916b3286edSKirill Korotaev p->event = ns->event; 3925addc5ddSAl Viro return 0; 3931da177e4SLinus Torvalds } 3945addc5ddSAl Viro kfree(p); 3955addc5ddSAl Viro } 3966b3286edSKirill Korotaev put_mnt_ns(ns); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds return ret; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds struct seq_file *m = file->private_data; 4046b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 4056b3286edSKirill Korotaev put_mnt_ns(ns); 4061da177e4SLinus Torvalds return seq_release(inode, file); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4095addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 4105addc5ddSAl Viro { 4115addc5ddSAl Viro struct proc_mounts *p = file->private_data; 4126b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 4135addc5ddSAl Viro unsigned res = 0; 4145addc5ddSAl Viro 4155addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4165addc5ddSAl Viro 4175addc5ddSAl Viro spin_lock(&vfsmount_lock); 4185addc5ddSAl Viro if (p->event != ns->event) { 4195addc5ddSAl Viro p->event = ns->event; 4205addc5ddSAl Viro res = POLLERR; 4215addc5ddSAl Viro } 4225addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4235addc5ddSAl Viro 4245addc5ddSAl Viro return res; 4255addc5ddSAl Viro } 4265addc5ddSAl Viro 42700977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4281da177e4SLinus Torvalds .open = mounts_open, 4291da177e4SLinus Torvalds .read = seq_read, 4301da177e4SLinus Torvalds .llseek = seq_lseek, 4311da177e4SLinus Torvalds .release = mounts_release, 4325addc5ddSAl Viro .poll = mounts_poll, 4331da177e4SLinus Torvalds }; 4341da177e4SLinus Torvalds 435b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 436b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 437b4629fe2SChuck Lever { 438b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 439b4629fe2SChuck Lever 440b4629fe2SChuck Lever if (!ret) { 441b4629fe2SChuck Lever struct seq_file *m = file->private_data; 4426b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 44399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 44499f89551SEric W. Biederman 44599f89551SEric W. Biederman if (task) { 446b4629fe2SChuck Lever task_lock(task); 447701e054eSVasily Tarasov if (task->nsproxy) 4486b3286edSKirill Korotaev mnt_ns = task->nsproxy->mnt_ns; 4496b3286edSKirill Korotaev if (mnt_ns) 4506b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 451b4629fe2SChuck Lever task_unlock(task); 45299f89551SEric W. Biederman put_task_struct(task); 45399f89551SEric W. Biederman } 454b4629fe2SChuck Lever 4556b3286edSKirill Korotaev if (mnt_ns) 4566b3286edSKirill Korotaev m->private = mnt_ns; 457b4629fe2SChuck Lever else { 458b4629fe2SChuck Lever seq_release(inode, file); 459b4629fe2SChuck Lever ret = -EINVAL; 460b4629fe2SChuck Lever } 461b4629fe2SChuck Lever } 462b4629fe2SChuck Lever return ret; 463b4629fe2SChuck Lever } 464b4629fe2SChuck Lever 46500977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 466b4629fe2SChuck Lever .open = mountstats_open, 467b4629fe2SChuck Lever .read = seq_read, 468b4629fe2SChuck Lever .llseek = seq_lseek, 469b4629fe2SChuck Lever .release = mounts_release, 470b4629fe2SChuck Lever }; 471b4629fe2SChuck Lever 4721da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 4751da177e4SLinus Torvalds size_t count, loff_t *ppos) 4761da177e4SLinus Torvalds { 4772fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 4781da177e4SLinus Torvalds unsigned long page; 4791da177e4SLinus Torvalds ssize_t length; 48099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 48199f89551SEric W. Biederman 48299f89551SEric W. Biederman length = -ESRCH; 48399f89551SEric W. Biederman if (!task) 48499f89551SEric W. Biederman goto out_no_task; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 4871da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 48899f89551SEric W. Biederman 48999f89551SEric W. Biederman length = -ENOMEM; 4901da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 49199f89551SEric W. Biederman goto out; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds if (length >= 0) 4961da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 4971da177e4SLinus Torvalds free_page(page); 49899f89551SEric W. Biederman out: 49999f89551SEric W. Biederman put_task_struct(task); 50099f89551SEric W. Biederman out_no_task: 5011da177e4SLinus Torvalds return length; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 50400977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 5051da177e4SLinus Torvalds .read = proc_info_read, 5061da177e4SLinus Torvalds }; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 5111da177e4SLinus Torvalds return 0; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 5151da177e4SLinus Torvalds size_t count, loff_t *ppos) 5161da177e4SLinus Torvalds { 5172fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5181da177e4SLinus Torvalds char *page; 5191da177e4SLinus Torvalds unsigned long src = *ppos; 5201da177e4SLinus Torvalds int ret = -ESRCH; 5211da177e4SLinus Torvalds struct mm_struct *mm; 5221da177e4SLinus Torvalds 52399f89551SEric W. Biederman if (!task) 52499f89551SEric W. Biederman goto out_no_task; 52599f89551SEric W. Biederman 526ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5271da177e4SLinus Torvalds goto out; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds ret = -ENOMEM; 5301da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 5311da177e4SLinus Torvalds if (!page) 5321da177e4SLinus Torvalds goto out; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds ret = 0; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds mm = get_task_mm(task); 5371da177e4SLinus Torvalds if (!mm) 5381da177e4SLinus Torvalds goto out_free; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds ret = -EIO; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 5431da177e4SLinus Torvalds goto out_put; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds ret = 0; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds while (count > 0) { 5481da177e4SLinus Torvalds int this_len, retval; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5511da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 552ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 5531da177e4SLinus Torvalds if (!ret) 5541da177e4SLinus Torvalds ret = -EIO; 5551da177e4SLinus Torvalds break; 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 5591da177e4SLinus Torvalds ret = -EFAULT; 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds ret += retval; 5641da177e4SLinus Torvalds src += retval; 5651da177e4SLinus Torvalds buf += retval; 5661da177e4SLinus Torvalds count -= retval; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds *ppos = src; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds out_put: 5711da177e4SLinus Torvalds mmput(mm); 5721da177e4SLinus Torvalds out_free: 5731da177e4SLinus Torvalds free_page((unsigned long) page); 5741da177e4SLinus Torvalds out: 57599f89551SEric W. Biederman put_task_struct(task); 57699f89551SEric W. Biederman out_no_task: 5771da177e4SLinus Torvalds return ret; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds #define mem_write NULL 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds #ifndef mem_write 5831da177e4SLinus Torvalds /* This is a security hazard */ 58463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 5851da177e4SLinus Torvalds size_t count, loff_t *ppos) 5861da177e4SLinus Torvalds { 587f7ca54f4SFrederik Deweerdt int copied; 5881da177e4SLinus Torvalds char *page; 5892fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5901da177e4SLinus Torvalds unsigned long dst = *ppos; 5911da177e4SLinus Torvalds 59299f89551SEric W. Biederman copied = -ESRCH; 59399f89551SEric W. Biederman if (!task) 59499f89551SEric W. Biederman goto out_no_task; 5951da177e4SLinus Torvalds 59699f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 59799f89551SEric W. Biederman goto out; 59899f89551SEric W. Biederman 59999f89551SEric W. Biederman copied = -ENOMEM; 6001da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 6011da177e4SLinus Torvalds if (!page) 60299f89551SEric W. Biederman goto out; 6031da177e4SLinus Torvalds 604f7ca54f4SFrederik Deweerdt copied = 0; 6051da177e4SLinus Torvalds while (count > 0) { 6061da177e4SLinus Torvalds int this_len, retval; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 6091da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 6101da177e4SLinus Torvalds copied = -EFAULT; 6111da177e4SLinus Torvalds break; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 6141da177e4SLinus Torvalds if (!retval) { 6151da177e4SLinus Torvalds if (!copied) 6161da177e4SLinus Torvalds copied = -EIO; 6171da177e4SLinus Torvalds break; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds copied += retval; 6201da177e4SLinus Torvalds buf += retval; 6211da177e4SLinus Torvalds dst += retval; 6221da177e4SLinus Torvalds count -= retval; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds *ppos = dst; 6251da177e4SLinus Torvalds free_page((unsigned long) page); 62699f89551SEric W. Biederman out: 62799f89551SEric W. Biederman put_task_struct(task); 62899f89551SEric W. Biederman out_no_task: 6291da177e4SLinus Torvalds return copied; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds #endif 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds switch (orig) { 6361da177e4SLinus Torvalds case 0: 6371da177e4SLinus Torvalds file->f_pos = offset; 6381da177e4SLinus Torvalds break; 6391da177e4SLinus Torvalds case 1: 6401da177e4SLinus Torvalds file->f_pos += offset; 6411da177e4SLinus Torvalds break; 6421da177e4SLinus Torvalds default: 6431da177e4SLinus Torvalds return -EINVAL; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds force_successful_syscall_return(); 6461da177e4SLinus Torvalds return file->f_pos; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 64900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 6501da177e4SLinus Torvalds .llseek = mem_lseek, 6511da177e4SLinus Torvalds .read = mem_read, 6521da177e4SLinus Torvalds .write = mem_write, 6531da177e4SLinus Torvalds .open = mem_open, 6541da177e4SLinus Torvalds }; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 6571da177e4SLinus Torvalds size_t count, loff_t *ppos) 6581da177e4SLinus Torvalds { 6592fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 6608578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 6611da177e4SLinus Torvalds size_t len; 66299f89551SEric W. Biederman int oom_adjust; 6631da177e4SLinus Torvalds 66499f89551SEric W. Biederman if (!task) 66599f89551SEric W. Biederman return -ESRCH; 66699f89551SEric W. Biederman oom_adjust = task->oomkilladj; 66799f89551SEric W. Biederman put_task_struct(task); 66899f89551SEric W. Biederman 6698578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 6700c28f287SAkinobu Mita 6710c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 6751da177e4SLinus Torvalds size_t count, loff_t *ppos) 6761da177e4SLinus Torvalds { 67799f89551SEric W. Biederman struct task_struct *task; 6788578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 6791da177e4SLinus Torvalds int oom_adjust; 6801da177e4SLinus Torvalds 6818578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 6828578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 6838578cea7SEric W. Biederman count = sizeof(buffer) - 1; 6841da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 6851da177e4SLinus Torvalds return -EFAULT; 6861da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 6878ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 6888ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 6891da177e4SLinus Torvalds return -EINVAL; 6901da177e4SLinus Torvalds if (*end == '\n') 6911da177e4SLinus Torvalds end++; 6922fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 69399f89551SEric W. Biederman if (!task) 69499f89551SEric W. Biederman return -ESRCH; 6958fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 6968fb4fc68SGuillem Jover put_task_struct(task); 6978fb4fc68SGuillem Jover return -EACCES; 6988fb4fc68SGuillem Jover } 6991da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 70099f89551SEric W. Biederman put_task_struct(task); 7011da177e4SLinus Torvalds if (end - buffer == 0) 7021da177e4SLinus Torvalds return -EIO; 7031da177e4SLinus Torvalds return end - buffer; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 70600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 7071da177e4SLinus Torvalds .read = oom_adjust_read, 7081da177e4SLinus Torvalds .write = oom_adjust_write, 7091da177e4SLinus Torvalds }; 7101da177e4SLinus Torvalds 7114b8df891SDavid Rientjes #ifdef CONFIG_MMU 712b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 713b813e931SDavid Rientjes size_t count, loff_t *ppos) 714b813e931SDavid Rientjes { 715b813e931SDavid Rientjes struct task_struct *task; 716b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 717b813e931SDavid Rientjes struct mm_struct *mm; 718b813e931SDavid Rientjes 719b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 720b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 721b813e931SDavid Rientjes count = sizeof(buffer) - 1; 722b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 723b813e931SDavid Rientjes return -EFAULT; 724b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 725b813e931SDavid Rientjes return -EINVAL; 726b813e931SDavid Rientjes if (*end == '\n') 727b813e931SDavid Rientjes end++; 728b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 729b813e931SDavid Rientjes if (!task) 730b813e931SDavid Rientjes return -ESRCH; 731b813e931SDavid Rientjes mm = get_task_mm(task); 732b813e931SDavid Rientjes if (mm) { 733b813e931SDavid Rientjes clear_refs_smap(mm); 734b813e931SDavid Rientjes mmput(mm); 735b813e931SDavid Rientjes } 736b813e931SDavid Rientjes put_task_struct(task); 737b813e931SDavid Rientjes if (end - buffer == 0) 738b813e931SDavid Rientjes return -EIO; 739b813e931SDavid Rientjes return end - buffer; 740b813e931SDavid Rientjes } 741b813e931SDavid Rientjes 742b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 743b813e931SDavid Rientjes .write = clear_refs_write, 744b813e931SDavid Rientjes }; 7454b8df891SDavid Rientjes #endif 746b813e931SDavid Rientjes 7471da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 7481da177e4SLinus Torvalds #define TMPBUFLEN 21 7491da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 7501da177e4SLinus Torvalds size_t count, loff_t *ppos) 7511da177e4SLinus Torvalds { 7522fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 75399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 7541da177e4SLinus Torvalds ssize_t length; 7551da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 7561da177e4SLinus Torvalds 75799f89551SEric W. Biederman if (!task) 75899f89551SEric W. Biederman return -ESRCH; 7591da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 7601da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 76199f89551SEric W. Biederman put_task_struct(task); 7621da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 7631da177e4SLinus Torvalds } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 7661da177e4SLinus Torvalds size_t count, loff_t *ppos) 7671da177e4SLinus Torvalds { 7682fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 7691da177e4SLinus Torvalds char *page, *tmp; 7701da177e4SLinus Torvalds ssize_t length; 7711da177e4SLinus Torvalds uid_t loginuid; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 7741da177e4SLinus Torvalds return -EPERM; 7751da177e4SLinus Torvalds 77613b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 7771da177e4SLinus Torvalds return -EPERM; 7781da177e4SLinus Torvalds 779e0182909SAl Viro if (count >= PAGE_SIZE) 780e0182909SAl Viro count = PAGE_SIZE - 1; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds if (*ppos != 0) { 7831da177e4SLinus Torvalds /* No partial writes. */ 7841da177e4SLinus Torvalds return -EINVAL; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds page = (char*)__get_free_page(GFP_USER); 7871da177e4SLinus Torvalds if (!page) 7881da177e4SLinus Torvalds return -ENOMEM; 7891da177e4SLinus Torvalds length = -EFAULT; 7901da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 7911da177e4SLinus Torvalds goto out_free_page; 7921da177e4SLinus Torvalds 793e0182909SAl Viro page[count] = '\0'; 7941da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 7951da177e4SLinus Torvalds if (tmp == page) { 7961da177e4SLinus Torvalds length = -EINVAL; 7971da177e4SLinus Torvalds goto out_free_page; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds } 80099f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 8011da177e4SLinus Torvalds if (likely(length == 0)) 8021da177e4SLinus Torvalds length = count; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds out_free_page: 8051da177e4SLinus Torvalds free_page((unsigned long) page); 8061da177e4SLinus Torvalds return length; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 80900977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 8101da177e4SLinus Torvalds .read = proc_loginuid_read, 8111da177e4SLinus Torvalds .write = proc_loginuid_write, 8121da177e4SLinus Torvalds }; 8131da177e4SLinus Torvalds #endif 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 8161da177e4SLinus Torvalds static ssize_t seccomp_read(struct file *file, char __user *buf, 8171da177e4SLinus Torvalds size_t count, loff_t *ppos) 8181da177e4SLinus Torvalds { 81999f89551SEric W. Biederman struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); 8201da177e4SLinus Torvalds char __buf[20]; 8211da177e4SLinus Torvalds size_t len; 8221da177e4SLinus Torvalds 82399f89551SEric W. Biederman if (!tsk) 82499f89551SEric W. Biederman return -ESRCH; 8251da177e4SLinus Torvalds /* no need to print the trailing zero, so use only len */ 8261da177e4SLinus Torvalds len = sprintf(__buf, "%u\n", tsk->seccomp.mode); 82799f89551SEric W. Biederman put_task_struct(tsk); 8280c28f287SAkinobu Mita 8290c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, __buf, len); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds static ssize_t seccomp_write(struct file *file, const char __user *buf, 8331da177e4SLinus Torvalds size_t count, loff_t *ppos) 8341da177e4SLinus Torvalds { 83599f89551SEric W. Biederman struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); 8361da177e4SLinus Torvalds char __buf[20], *end; 8371da177e4SLinus Torvalds unsigned int seccomp_mode; 83899f89551SEric W. Biederman ssize_t result; 83999f89551SEric W. Biederman 84099f89551SEric W. Biederman result = -ESRCH; 84199f89551SEric W. Biederman if (!tsk) 84299f89551SEric W. Biederman goto out_no_task; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* can set it only once to be even more secure */ 84599f89551SEric W. Biederman result = -EPERM; 8461da177e4SLinus Torvalds if (unlikely(tsk->seccomp.mode)) 84799f89551SEric W. Biederman goto out; 8481da177e4SLinus Torvalds 84999f89551SEric W. Biederman result = -EFAULT; 8501da177e4SLinus Torvalds memset(__buf, 0, sizeof(__buf)); 8511da177e4SLinus Torvalds count = min(count, sizeof(__buf) - 1); 8521da177e4SLinus Torvalds if (copy_from_user(__buf, buf, count)) 85399f89551SEric W. Biederman goto out; 85499f89551SEric W. Biederman 8551da177e4SLinus Torvalds seccomp_mode = simple_strtoul(__buf, &end, 0); 8561da177e4SLinus Torvalds if (*end == '\n') 8571da177e4SLinus Torvalds end++; 85899f89551SEric W. Biederman result = -EINVAL; 8591da177e4SLinus Torvalds if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { 8601da177e4SLinus Torvalds tsk->seccomp.mode = seccomp_mode; 8611da177e4SLinus Torvalds set_tsk_thread_flag(tsk, TIF_SECCOMP); 8621da177e4SLinus Torvalds } else 86399f89551SEric W. Biederman goto out; 86499f89551SEric W. Biederman result = -EIO; 8651da177e4SLinus Torvalds if (unlikely(!(end - __buf))) 86699f89551SEric W. Biederman goto out; 86799f89551SEric W. Biederman result = end - __buf; 86899f89551SEric W. Biederman out: 86999f89551SEric W. Biederman put_task_struct(tsk); 87099f89551SEric W. Biederman out_no_task: 87199f89551SEric W. Biederman return result; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 87400977a59SArjan van de Ven static const struct file_operations proc_seccomp_operations = { 8751da177e4SLinus Torvalds .read = seccomp_read, 8761da177e4SLinus Torvalds .write = seccomp_write, 8771da177e4SLinus Torvalds }; 8781da177e4SLinus Torvalds #endif /* CONFIG_SECCOMP */ 8791da177e4SLinus Torvalds 880f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 881f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 882f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 883f4f154fdSAkinobu Mita { 884f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 885f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 886f4f154fdSAkinobu Mita size_t len; 887f4f154fdSAkinobu Mita int make_it_fail; 888f4f154fdSAkinobu Mita 889f4f154fdSAkinobu Mita if (!task) 890f4f154fdSAkinobu Mita return -ESRCH; 891f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 892f4f154fdSAkinobu Mita put_task_struct(task); 893f4f154fdSAkinobu Mita 894f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 8950c28f287SAkinobu Mita 8960c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 897f4f154fdSAkinobu Mita } 898f4f154fdSAkinobu Mita 899f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 900f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 901f4f154fdSAkinobu Mita { 902f4f154fdSAkinobu Mita struct task_struct *task; 903f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 904f4f154fdSAkinobu Mita int make_it_fail; 905f4f154fdSAkinobu Mita 906f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 907f4f154fdSAkinobu Mita return -EPERM; 908f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 909f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 910f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 911f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 912f4f154fdSAkinobu Mita return -EFAULT; 913f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 914f4f154fdSAkinobu Mita if (*end == '\n') 915f4f154fdSAkinobu Mita end++; 916f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 917f4f154fdSAkinobu Mita if (!task) 918f4f154fdSAkinobu Mita return -ESRCH; 919f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 920f4f154fdSAkinobu Mita put_task_struct(task); 921f4f154fdSAkinobu Mita if (end - buffer == 0) 922f4f154fdSAkinobu Mita return -EIO; 923f4f154fdSAkinobu Mita return end - buffer; 924f4f154fdSAkinobu Mita } 925f4f154fdSAkinobu Mita 92600977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 927f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 928f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 929f4f154fdSAkinobu Mita }; 930f4f154fdSAkinobu Mita #endif 931f4f154fdSAkinobu Mita 932*43ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 933*43ae34cbSIngo Molnar /* 934*43ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 935*43ae34cbSIngo Molnar */ 936*43ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 937*43ae34cbSIngo Molnar { 938*43ae34cbSIngo Molnar struct inode *inode = m->private; 939*43ae34cbSIngo Molnar struct task_struct *p; 940*43ae34cbSIngo Molnar 941*43ae34cbSIngo Molnar WARN_ON(!inode); 942*43ae34cbSIngo Molnar 943*43ae34cbSIngo Molnar p = get_proc_task(inode); 944*43ae34cbSIngo Molnar if (!p) 945*43ae34cbSIngo Molnar return -ESRCH; 946*43ae34cbSIngo Molnar proc_sched_show_task(p, m); 947*43ae34cbSIngo Molnar 948*43ae34cbSIngo Molnar put_task_struct(p); 949*43ae34cbSIngo Molnar 950*43ae34cbSIngo Molnar return 0; 951*43ae34cbSIngo Molnar } 952*43ae34cbSIngo Molnar 953*43ae34cbSIngo Molnar static ssize_t 954*43ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 955*43ae34cbSIngo Molnar size_t count, loff_t *offset) 956*43ae34cbSIngo Molnar { 957*43ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 958*43ae34cbSIngo Molnar struct task_struct *p; 959*43ae34cbSIngo Molnar 960*43ae34cbSIngo Molnar WARN_ON(!inode); 961*43ae34cbSIngo Molnar 962*43ae34cbSIngo Molnar p = get_proc_task(inode); 963*43ae34cbSIngo Molnar if (!p) 964*43ae34cbSIngo Molnar return -ESRCH; 965*43ae34cbSIngo Molnar proc_sched_set_task(p); 966*43ae34cbSIngo Molnar 967*43ae34cbSIngo Molnar put_task_struct(p); 968*43ae34cbSIngo Molnar 969*43ae34cbSIngo Molnar return count; 970*43ae34cbSIngo Molnar } 971*43ae34cbSIngo Molnar 972*43ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 973*43ae34cbSIngo Molnar { 974*43ae34cbSIngo Molnar int ret; 975*43ae34cbSIngo Molnar 976*43ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 977*43ae34cbSIngo Molnar if (!ret) { 978*43ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 979*43ae34cbSIngo Molnar 980*43ae34cbSIngo Molnar m->private = inode; 981*43ae34cbSIngo Molnar } 982*43ae34cbSIngo Molnar return ret; 983*43ae34cbSIngo Molnar } 984*43ae34cbSIngo Molnar 985*43ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 986*43ae34cbSIngo Molnar .open = sched_open, 987*43ae34cbSIngo Molnar .read = seq_read, 988*43ae34cbSIngo Molnar .write = sched_write, 989*43ae34cbSIngo Molnar .llseek = seq_lseek, 990*43ae34cbSIngo Molnar .release = seq_release, 991*43ae34cbSIngo Molnar }; 992*43ae34cbSIngo Molnar 993*43ae34cbSIngo Molnar #endif 994*43ae34cbSIngo Molnar 995008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 9961da177e4SLinus Torvalds { 9971da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9981da177e4SLinus Torvalds int error = -EACCES; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 10011da177e4SLinus Torvalds path_release(nd); 10021da177e4SLinus Torvalds 1003778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1004778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10051da177e4SLinus Torvalds goto out; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 10081da177e4SLinus Torvalds nd->last_type = LAST_BIND; 10091da177e4SLinus Torvalds out: 1010008b150aSAl Viro return ERR_PTR(error); 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10141da177e4SLinus Torvalds char __user *buffer, int buflen) 10151da177e4SLinus Torvalds { 10161da177e4SLinus Torvalds struct inode * inode; 10171da177e4SLinus Torvalds char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; 10181da177e4SLinus Torvalds int len; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (!tmp) 10211da177e4SLinus Torvalds return -ENOMEM; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds inode = dentry->d_inode; 10241da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10251da177e4SLinus Torvalds len = PTR_ERR(path); 10261da177e4SLinus Torvalds if (IS_ERR(path)) 10271da177e4SLinus Torvalds goto out; 10281da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds if (len > buflen) 10311da177e4SLinus Torvalds len = buflen; 10321da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 10331da177e4SLinus Torvalds len = -EFAULT; 10341da177e4SLinus Torvalds out: 10351da177e4SLinus Torvalds free_page((unsigned long)tmp); 10361da177e4SLinus Torvalds return len; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds int error = -EACCES; 10421da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10431da177e4SLinus Torvalds struct dentry *de; 10441da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 10451da177e4SLinus Torvalds 1046778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1047778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10481da177e4SLinus Torvalds goto out; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 10511da177e4SLinus Torvalds if (error) 10521da177e4SLinus Torvalds goto out; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 10551da177e4SLinus Torvalds dput(de); 10561da177e4SLinus Torvalds mntput(mnt); 10571da177e4SLinus Torvalds out: 10581da177e4SLinus Torvalds return error; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 1061c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 10621da177e4SLinus Torvalds .readlink = proc_pid_readlink, 10636d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 10646d76fa58SLinus Torvalds .setattr = proc_setattr, 10651da177e4SLinus Torvalds }; 10661da177e4SLinus Torvalds 106728a6d671SEric W. Biederman 106828a6d671SEric W. Biederman /* building an inode */ 106928a6d671SEric W. Biederman 107028a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 107128a6d671SEric W. Biederman { 107228a6d671SEric W. Biederman int dumpable = 0; 107328a6d671SEric W. Biederman struct mm_struct *mm; 107428a6d671SEric W. Biederman 107528a6d671SEric W. Biederman task_lock(task); 107628a6d671SEric W. Biederman mm = task->mm; 107728a6d671SEric W. Biederman if (mm) 107828a6d671SEric W. Biederman dumpable = mm->dumpable; 107928a6d671SEric W. Biederman task_unlock(task); 108028a6d671SEric W. Biederman if(dumpable == 1) 108128a6d671SEric W. Biederman return 1; 108228a6d671SEric W. Biederman return 0; 108328a6d671SEric W. Biederman } 108428a6d671SEric W. Biederman 108528a6d671SEric W. Biederman 108661a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 108728a6d671SEric W. Biederman { 108828a6d671SEric W. Biederman struct inode * inode; 108928a6d671SEric W. Biederman struct proc_inode *ei; 109028a6d671SEric W. Biederman 109128a6d671SEric W. Biederman /* We need a new inode */ 109228a6d671SEric W. Biederman 109328a6d671SEric W. Biederman inode = new_inode(sb); 109428a6d671SEric W. Biederman if (!inode) 109528a6d671SEric W. Biederman goto out; 109628a6d671SEric W. Biederman 109728a6d671SEric W. Biederman /* Common stuff */ 109828a6d671SEric W. Biederman ei = PROC_I(inode); 109928a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 110028a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 110128a6d671SEric W. Biederman 110228a6d671SEric W. Biederman /* 110328a6d671SEric W. Biederman * grab the reference to task. 110428a6d671SEric W. Biederman */ 11051a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 110628a6d671SEric W. Biederman if (!ei->pid) 110728a6d671SEric W. Biederman goto out_unlock; 110828a6d671SEric W. Biederman 110928a6d671SEric W. Biederman inode->i_uid = 0; 111028a6d671SEric W. Biederman inode->i_gid = 0; 111128a6d671SEric W. Biederman if (task_dumpable(task)) { 111228a6d671SEric W. Biederman inode->i_uid = task->euid; 111328a6d671SEric W. Biederman inode->i_gid = task->egid; 111428a6d671SEric W. Biederman } 111528a6d671SEric W. Biederman security_task_to_inode(task, inode); 111628a6d671SEric W. Biederman 111728a6d671SEric W. Biederman out: 111828a6d671SEric W. Biederman return inode; 111928a6d671SEric W. Biederman 112028a6d671SEric W. Biederman out_unlock: 112128a6d671SEric W. Biederman iput(inode); 112228a6d671SEric W. Biederman return NULL; 112328a6d671SEric W. Biederman } 112428a6d671SEric W. Biederman 112528a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 112628a6d671SEric W. Biederman { 112728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 112828a6d671SEric W. Biederman struct task_struct *task; 112928a6d671SEric W. Biederman generic_fillattr(inode, stat); 113028a6d671SEric W. Biederman 113128a6d671SEric W. Biederman rcu_read_lock(); 113228a6d671SEric W. Biederman stat->uid = 0; 113328a6d671SEric W. Biederman stat->gid = 0; 113428a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 113528a6d671SEric W. Biederman if (task) { 113628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 113728a6d671SEric W. Biederman task_dumpable(task)) { 113828a6d671SEric W. Biederman stat->uid = task->euid; 113928a6d671SEric W. Biederman stat->gid = task->egid; 114028a6d671SEric W. Biederman } 114128a6d671SEric W. Biederman } 114228a6d671SEric W. Biederman rcu_read_unlock(); 114328a6d671SEric W. Biederman return 0; 114428a6d671SEric W. Biederman } 114528a6d671SEric W. Biederman 114628a6d671SEric W. Biederman /* dentry stuff */ 114728a6d671SEric W. Biederman 114828a6d671SEric W. Biederman /* 114928a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 115028a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 115128a6d671SEric W. Biederman * due to the way we treat inodes. 115228a6d671SEric W. Biederman * 115328a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 115428a6d671SEric W. Biederman * performed a setuid(), etc. 115528a6d671SEric W. Biederman * 115628a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 115728a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 115828a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 115928a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 116028a6d671SEric W. Biederman * made this apply to all per process world readable and executable 116128a6d671SEric W. Biederman * directories. 116228a6d671SEric W. Biederman */ 116328a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 116428a6d671SEric W. Biederman { 116528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 116628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 116728a6d671SEric W. Biederman if (task) { 116828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 116928a6d671SEric W. Biederman task_dumpable(task)) { 117028a6d671SEric W. Biederman inode->i_uid = task->euid; 117128a6d671SEric W. Biederman inode->i_gid = task->egid; 117228a6d671SEric W. Biederman } else { 117328a6d671SEric W. Biederman inode->i_uid = 0; 117428a6d671SEric W. Biederman inode->i_gid = 0; 117528a6d671SEric W. Biederman } 117628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 117728a6d671SEric W. Biederman security_task_to_inode(task, inode); 117828a6d671SEric W. Biederman put_task_struct(task); 117928a6d671SEric W. Biederman return 1; 118028a6d671SEric W. Biederman } 118128a6d671SEric W. Biederman d_drop(dentry); 118228a6d671SEric W. Biederman return 0; 118328a6d671SEric W. Biederman } 118428a6d671SEric W. Biederman 118528a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 118628a6d671SEric W. Biederman { 118728a6d671SEric W. Biederman /* Is the task we represent dead? 118828a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 118928a6d671SEric W. Biederman * kill it immediately. 119028a6d671SEric W. Biederman */ 119128a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 119228a6d671SEric W. Biederman } 119328a6d671SEric W. Biederman 119428a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 119528a6d671SEric W. Biederman { 119628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 119728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 119828a6d671SEric W. Biederman }; 119928a6d671SEric W. Biederman 120028a6d671SEric W. Biederman /* Lookups */ 120128a6d671SEric W. Biederman 1202c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1203c5141e6dSEric Dumazet struct task_struct *, const void *); 120461a28784SEric W. Biederman 12051c0d04c9SEric W. Biederman /* 12061c0d04c9SEric W. Biederman * Fill a directory entry. 12071c0d04c9SEric W. Biederman * 12081c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 12091c0d04c9SEric W. Biederman * file type from dcache entry. 12101c0d04c9SEric W. Biederman * 12111c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 12121c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 12131c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 12141c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 12151c0d04c9SEric W. Biederman * by stat. 12161c0d04c9SEric W. Biederman */ 121761a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 121861a28784SEric W. Biederman char *name, int len, 1219c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 122061a28784SEric W. Biederman { 12212fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 122261a28784SEric W. Biederman struct inode *inode; 122361a28784SEric W. Biederman struct qstr qname; 122461a28784SEric W. Biederman ino_t ino = 0; 122561a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 122661a28784SEric W. Biederman 122761a28784SEric W. Biederman qname.name = name; 122861a28784SEric W. Biederman qname.len = len; 122961a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 123061a28784SEric W. Biederman 123161a28784SEric W. Biederman child = d_lookup(dir, &qname); 123261a28784SEric W. Biederman if (!child) { 123361a28784SEric W. Biederman struct dentry *new; 123461a28784SEric W. Biederman new = d_alloc(dir, &qname); 123561a28784SEric W. Biederman if (new) { 123661a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 123761a28784SEric W. Biederman if (child) 123861a28784SEric W. Biederman dput(new); 123961a28784SEric W. Biederman else 124061a28784SEric W. Biederman child = new; 124161a28784SEric W. Biederman } 124261a28784SEric W. Biederman } 124361a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 124461a28784SEric W. Biederman goto end_instantiate; 124561a28784SEric W. Biederman inode = child->d_inode; 124661a28784SEric W. Biederman if (inode) { 124761a28784SEric W. Biederman ino = inode->i_ino; 124861a28784SEric W. Biederman type = inode->i_mode >> 12; 124961a28784SEric W. Biederman } 125061a28784SEric W. Biederman dput(child); 125161a28784SEric W. Biederman end_instantiate: 125261a28784SEric W. Biederman if (!ino) 125361a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 125461a28784SEric W. Biederman if (!ino) 125561a28784SEric W. Biederman ino = 1; 125661a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 125761a28784SEric W. Biederman } 125861a28784SEric W. Biederman 125928a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 126028a6d671SEric W. Biederman { 126128a6d671SEric W. Biederman const char *name = dentry->d_name.name; 126228a6d671SEric W. Biederman int len = dentry->d_name.len; 126328a6d671SEric W. Biederman unsigned n = 0; 126428a6d671SEric W. Biederman 126528a6d671SEric W. Biederman if (len > 1 && *name == '0') 126628a6d671SEric W. Biederman goto out; 126728a6d671SEric W. Biederman while (len-- > 0) { 126828a6d671SEric W. Biederman unsigned c = *name++ - '0'; 126928a6d671SEric W. Biederman if (c > 9) 127028a6d671SEric W. Biederman goto out; 127128a6d671SEric W. Biederman if (n >= (~0U-9)/10) 127228a6d671SEric W. Biederman goto out; 127328a6d671SEric W. Biederman n *= 10; 127428a6d671SEric W. Biederman n += c; 127528a6d671SEric W. Biederman } 127628a6d671SEric W. Biederman return n; 127728a6d671SEric W. Biederman out: 127828a6d671SEric W. Biederman return ~0U; 127928a6d671SEric W. Biederman } 128028a6d671SEric W. Biederman 128127932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 128227932742SMiklos Szeredi 128327932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 128427932742SMiklos Szeredi struct vfsmount **mnt, char *info) 128528a6d671SEric W. Biederman { 128628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 128728a6d671SEric W. Biederman struct files_struct *files = NULL; 128828a6d671SEric W. Biederman struct file *file; 128928a6d671SEric W. Biederman int fd = proc_fd(inode); 129028a6d671SEric W. Biederman 129128a6d671SEric W. Biederman if (task) { 129228a6d671SEric W. Biederman files = get_files_struct(task); 129328a6d671SEric W. Biederman put_task_struct(task); 129428a6d671SEric W. Biederman } 129528a6d671SEric W. Biederman if (files) { 129628a6d671SEric W. Biederman /* 129728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 129828a6d671SEric W. Biederman * hold ->file_lock. 129928a6d671SEric W. Biederman */ 130028a6d671SEric W. Biederman spin_lock(&files->file_lock); 130128a6d671SEric W. Biederman file = fcheck_files(files, fd); 130228a6d671SEric W. Biederman if (file) { 130327932742SMiklos Szeredi if (mnt) 13042fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 130527932742SMiklos Szeredi if (dentry) 13062fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 130727932742SMiklos Szeredi if (info) 130827932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 130927932742SMiklos Szeredi "pos:\t%lli\n" 131027932742SMiklos Szeredi "flags:\t0%o\n", 131127932742SMiklos Szeredi (long long) file->f_pos, 131227932742SMiklos Szeredi file->f_flags); 131328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 131428a6d671SEric W. Biederman put_files_struct(files); 131528a6d671SEric W. Biederman return 0; 131628a6d671SEric W. Biederman } 131728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 131828a6d671SEric W. Biederman put_files_struct(files); 131928a6d671SEric W. Biederman } 132028a6d671SEric W. Biederman return -ENOENT; 132128a6d671SEric W. Biederman } 132228a6d671SEric W. Biederman 132327932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 132427932742SMiklos Szeredi struct vfsmount **mnt) 132527932742SMiklos Szeredi { 132627932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 132727932742SMiklos Szeredi } 132827932742SMiklos Szeredi 132928a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 133028a6d671SEric W. Biederman { 133128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 133228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 133328a6d671SEric W. Biederman int fd = proc_fd(inode); 133428a6d671SEric W. Biederman struct files_struct *files; 133528a6d671SEric W. Biederman 133628a6d671SEric W. Biederman if (task) { 133728a6d671SEric W. Biederman files = get_files_struct(task); 133828a6d671SEric W. Biederman if (files) { 133928a6d671SEric W. Biederman rcu_read_lock(); 134028a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 134128a6d671SEric W. Biederman rcu_read_unlock(); 134228a6d671SEric W. Biederman put_files_struct(files); 134328a6d671SEric W. Biederman if (task_dumpable(task)) { 134428a6d671SEric W. Biederman inode->i_uid = task->euid; 134528a6d671SEric W. Biederman inode->i_gid = task->egid; 134628a6d671SEric W. Biederman } else { 134728a6d671SEric W. Biederman inode->i_uid = 0; 134828a6d671SEric W. Biederman inode->i_gid = 0; 134928a6d671SEric W. Biederman } 135028a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 135128a6d671SEric W. Biederman security_task_to_inode(task, inode); 135228a6d671SEric W. Biederman put_task_struct(task); 135328a6d671SEric W. Biederman return 1; 135428a6d671SEric W. Biederman } 135528a6d671SEric W. Biederman rcu_read_unlock(); 135628a6d671SEric W. Biederman put_files_struct(files); 135728a6d671SEric W. Biederman } 135828a6d671SEric W. Biederman put_task_struct(task); 135928a6d671SEric W. Biederman } 136028a6d671SEric W. Biederman d_drop(dentry); 136128a6d671SEric W. Biederman return 0; 136228a6d671SEric W. Biederman } 136328a6d671SEric W. Biederman 136428a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 136528a6d671SEric W. Biederman { 136628a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 136728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 136828a6d671SEric W. Biederman }; 136928a6d671SEric W. Biederman 1370444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1371c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 137228a6d671SEric W. Biederman { 1373c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 137428a6d671SEric W. Biederman struct file *file; 137528a6d671SEric W. Biederman struct files_struct *files; 137628a6d671SEric W. Biederman struct inode *inode; 137728a6d671SEric W. Biederman struct proc_inode *ei; 1378444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 137928a6d671SEric W. Biederman 138061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 138128a6d671SEric W. Biederman if (!inode) 138228a6d671SEric W. Biederman goto out; 138328a6d671SEric W. Biederman ei = PROC_I(inode); 138428a6d671SEric W. Biederman ei->fd = fd; 138528a6d671SEric W. Biederman files = get_files_struct(task); 138628a6d671SEric W. Biederman if (!files) 1387444ceed8SEric W. Biederman goto out_iput; 138828a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 138928a6d671SEric W. Biederman 139028a6d671SEric W. Biederman /* 139128a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 139228a6d671SEric W. Biederman * hold ->file_lock. 139328a6d671SEric W. Biederman */ 139428a6d671SEric W. Biederman spin_lock(&files->file_lock); 139528a6d671SEric W. Biederman file = fcheck_files(files, fd); 139628a6d671SEric W. Biederman if (!file) 1397444ceed8SEric W. Biederman goto out_unlock; 139828a6d671SEric W. Biederman if (file->f_mode & 1) 139928a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 140028a6d671SEric W. Biederman if (file->f_mode & 2) 140128a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 140228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 140328a6d671SEric W. Biederman put_files_struct(files); 1404444ceed8SEric W. Biederman 140528a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 140628a6d671SEric W. Biederman inode->i_size = 64; 140728a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 140828a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 140928a6d671SEric W. Biederman d_add(dentry, inode); 141028a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 141128a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1412444ceed8SEric W. Biederman error = NULL; 1413444ceed8SEric W. Biederman 1414444ceed8SEric W. Biederman out: 1415444ceed8SEric W. Biederman return error; 1416444ceed8SEric W. Biederman out_unlock: 1417444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1418444ceed8SEric W. Biederman put_files_struct(files); 1419444ceed8SEric W. Biederman out_iput: 1420444ceed8SEric W. Biederman iput(inode); 1421444ceed8SEric W. Biederman goto out; 1422444ceed8SEric W. Biederman } 1423444ceed8SEric W. Biederman 142427932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 142527932742SMiklos Szeredi struct dentry *dentry, 142627932742SMiklos Szeredi instantiate_t instantiate) 1427444ceed8SEric W. Biederman { 1428444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1429444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1430444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1431444ceed8SEric W. Biederman 1432444ceed8SEric W. Biederman if (!task) 1433444ceed8SEric W. Biederman goto out_no_task; 1434444ceed8SEric W. Biederman if (fd == ~0U) 1435444ceed8SEric W. Biederman goto out; 1436444ceed8SEric W. Biederman 143727932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 143828a6d671SEric W. Biederman out: 143928a6d671SEric W. Biederman put_task_struct(task); 144028a6d671SEric W. Biederman out_no_task: 144128a6d671SEric W. Biederman return result; 144228a6d671SEric W. Biederman } 144328a6d671SEric W. Biederman 144427932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 144527932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 14461da177e4SLinus Torvalds { 14472fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 14485634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 144999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 14501da177e4SLinus Torvalds unsigned int fd, tid, ino; 14511da177e4SLinus Torvalds int retval; 14521da177e4SLinus Torvalds struct files_struct * files; 1453badf1662SDipankar Sarma struct fdtable *fdt; 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds retval = -ENOENT; 145699f89551SEric W. Biederman if (!p) 145799f89551SEric W. Biederman goto out_no_task; 14581da177e4SLinus Torvalds retval = 0; 14591da177e4SLinus Torvalds tid = p->pid; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds fd = filp->f_pos; 14621da177e4SLinus Torvalds switch (fd) { 14631da177e4SLinus Torvalds case 0: 14641da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 14651da177e4SLinus Torvalds goto out; 14661da177e4SLinus Torvalds filp->f_pos++; 14671da177e4SLinus Torvalds case 1: 14685634708bSEric W. Biederman ino = parent_ino(dentry); 14691da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 14701da177e4SLinus Torvalds goto out; 14711da177e4SLinus Torvalds filp->f_pos++; 14721da177e4SLinus Torvalds default: 14731da177e4SLinus Torvalds files = get_files_struct(p); 14741da177e4SLinus Torvalds if (!files) 14751da177e4SLinus Torvalds goto out; 1476b835996fSDipankar Sarma rcu_read_lock(); 1477badf1662SDipankar Sarma fdt = files_fdtable(files); 14781da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1479badf1662SDipankar Sarma fd < fdt->max_fds; 14801da177e4SLinus Torvalds fd++, filp->f_pos++) { 148127932742SMiklos Szeredi char name[PROC_NUMBUF]; 148227932742SMiklos Szeredi int len; 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 14851da177e4SLinus Torvalds continue; 1486b835996fSDipankar Sarma rcu_read_unlock(); 14871da177e4SLinus Torvalds 148827932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 148927932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 149027932742SMiklos Szeredi name, len, instantiate, 149127932742SMiklos Szeredi p, &fd) < 0) { 1492b835996fSDipankar Sarma rcu_read_lock(); 14931da177e4SLinus Torvalds break; 14941da177e4SLinus Torvalds } 1495b835996fSDipankar Sarma rcu_read_lock(); 14961da177e4SLinus Torvalds } 1497b835996fSDipankar Sarma rcu_read_unlock(); 14981da177e4SLinus Torvalds put_files_struct(files); 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds out: 150199f89551SEric W. Biederman put_task_struct(p); 150299f89551SEric W. Biederman out_no_task: 15031da177e4SLinus Torvalds return retval; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 150627932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 150727932742SMiklos Szeredi struct nameidata *nd) 150827932742SMiklos Szeredi { 150927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 151027932742SMiklos Szeredi } 151127932742SMiklos Szeredi 151227932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 151327932742SMiklos Szeredi { 151427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 151527932742SMiklos Szeredi } 151627932742SMiklos Szeredi 151727932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 151827932742SMiklos Szeredi size_t len, loff_t *ppos) 151927932742SMiklos Szeredi { 152027932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 152127932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 152227932742SMiklos Szeredi if (!err) 152327932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 152427932742SMiklos Szeredi return err; 152527932742SMiklos Szeredi } 152627932742SMiklos Szeredi 152727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 152827932742SMiklos Szeredi .open = nonseekable_open, 152927932742SMiklos Szeredi .read = proc_fdinfo_read, 153027932742SMiklos Szeredi }; 153127932742SMiklos Szeredi 153200977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 15331da177e4SLinus Torvalds .read = generic_read_dir, 15341da177e4SLinus Torvalds .readdir = proc_readfd, 15351da177e4SLinus Torvalds }; 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds /* 15388948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 15398948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 15408948e11fSAlexey Dobriyan */ 15418948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 15428948e11fSAlexey Dobriyan struct nameidata *nd) 15438948e11fSAlexey Dobriyan { 15448948e11fSAlexey Dobriyan int rv; 15458948e11fSAlexey Dobriyan 15468948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 15478948e11fSAlexey Dobriyan if (rv == 0) 15488948e11fSAlexey Dobriyan return 0; 15498948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 15508948e11fSAlexey Dobriyan rv = 0; 15518948e11fSAlexey Dobriyan return rv; 15528948e11fSAlexey Dobriyan } 15538948e11fSAlexey Dobriyan 15548948e11fSAlexey Dobriyan /* 15551da177e4SLinus Torvalds * proc directories can do almost nothing.. 15561da177e4SLinus Torvalds */ 1557c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 15581da177e4SLinus Torvalds .lookup = proc_lookupfd, 15598948e11fSAlexey Dobriyan .permission = proc_fd_permission, 15606d76fa58SLinus Torvalds .setattr = proc_setattr, 15611da177e4SLinus Torvalds }; 15621da177e4SLinus Torvalds 156327932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 156427932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 156527932742SMiklos Szeredi { 156627932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 156727932742SMiklos Szeredi struct inode *inode; 156827932742SMiklos Szeredi struct proc_inode *ei; 156927932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 157027932742SMiklos Szeredi 157127932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 157227932742SMiklos Szeredi if (!inode) 157327932742SMiklos Szeredi goto out; 157427932742SMiklos Szeredi ei = PROC_I(inode); 157527932742SMiklos Szeredi ei->fd = fd; 157627932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 157727932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 157827932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 157927932742SMiklos Szeredi d_add(dentry, inode); 158027932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 158127932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 158227932742SMiklos Szeredi error = NULL; 158327932742SMiklos Szeredi 158427932742SMiklos Szeredi out: 158527932742SMiklos Szeredi return error; 158627932742SMiklos Szeredi } 158727932742SMiklos Szeredi 158827932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 158927932742SMiklos Szeredi struct dentry *dentry, 159027932742SMiklos Szeredi struct nameidata *nd) 159127932742SMiklos Szeredi { 159227932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 159327932742SMiklos Szeredi } 159427932742SMiklos Szeredi 159527932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 159627932742SMiklos Szeredi { 159727932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 159827932742SMiklos Szeredi proc_fdinfo_instantiate); 159927932742SMiklos Szeredi } 160027932742SMiklos Szeredi 160127932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 160227932742SMiklos Szeredi .read = generic_read_dir, 160327932742SMiklos Szeredi .readdir = proc_readfdinfo, 160427932742SMiklos Szeredi }; 160527932742SMiklos Szeredi 160627932742SMiklos Szeredi /* 160727932742SMiklos Szeredi * proc directories can do almost nothing.. 160827932742SMiklos Szeredi */ 160927932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 161027932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 161127932742SMiklos Szeredi .setattr = proc_setattr, 161227932742SMiklos Szeredi }; 161327932742SMiklos Szeredi 161427932742SMiklos Szeredi 1615444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1616c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1617444ceed8SEric W. Biederman { 1618c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1619444ceed8SEric W. Biederman struct inode *inode; 1620444ceed8SEric W. Biederman struct proc_inode *ei; 1621444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1622444ceed8SEric W. Biederman 162361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1624444ceed8SEric W. Biederman if (!inode) 1625444ceed8SEric W. Biederman goto out; 1626444ceed8SEric W. Biederman 1627444ceed8SEric W. Biederman ei = PROC_I(inode); 1628444ceed8SEric W. Biederman inode->i_mode = p->mode; 1629444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1630444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1631444ceed8SEric W. Biederman if (p->iop) 1632444ceed8SEric W. Biederman inode->i_op = p->iop; 1633444ceed8SEric W. Biederman if (p->fop) 1634444ceed8SEric W. Biederman inode->i_fop = p->fop; 1635444ceed8SEric W. Biederman ei->op = p->op; 1636444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1637444ceed8SEric W. Biederman d_add(dentry, inode); 1638444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1639444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1640444ceed8SEric W. Biederman error = NULL; 1641444ceed8SEric W. Biederman out: 1642444ceed8SEric W. Biederman return error; 1643444ceed8SEric W. Biederman } 1644444ceed8SEric W. Biederman 16451da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 16461da177e4SLinus Torvalds struct dentry *dentry, 1647c5141e6dSEric Dumazet const struct pid_entry *ents, 16487bcd6b0eSEric W. Biederman unsigned int nents) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds struct inode *inode; 1651cd6a3ce9SEric W. Biederman struct dentry *error; 165299f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1653c5141e6dSEric Dumazet const struct pid_entry *p, *last; 16541da177e4SLinus Torvalds 1655cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 16561da177e4SLinus Torvalds inode = NULL; 16571da177e4SLinus Torvalds 165899f89551SEric W. Biederman if (!task) 165999f89551SEric W. Biederman goto out_no_task; 16601da177e4SLinus Torvalds 166120cdc894SEric W. Biederman /* 166220cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 166320cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 166420cdc894SEric W. Biederman */ 16657bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16667bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 16671da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16681da177e4SLinus Torvalds continue; 16691da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16701da177e4SLinus Torvalds break; 16711da177e4SLinus Torvalds } 16727bcd6b0eSEric W. Biederman if (p > last) 16731da177e4SLinus Torvalds goto out; 16741da177e4SLinus Torvalds 1675444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 16761da177e4SLinus Torvalds out: 167799f89551SEric W. Biederman put_task_struct(task); 167899f89551SEric W. Biederman out_no_task: 1679cd6a3ce9SEric W. Biederman return error; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 1682c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1683c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 168461a28784SEric W. Biederman { 168561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 168661a28784SEric W. Biederman proc_pident_instantiate, task, p); 168761a28784SEric W. Biederman } 168861a28784SEric W. Biederman 168928a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 169028a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1691c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 169228a6d671SEric W. Biederman { 169328a6d671SEric W. Biederman int i; 169428a6d671SEric W. Biederman int pid; 16952fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 169628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 169728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1698c5141e6dSEric Dumazet const struct pid_entry *p, *last; 169928a6d671SEric W. Biederman ino_t ino; 170028a6d671SEric W. Biederman int ret; 170128a6d671SEric W. Biederman 170228a6d671SEric W. Biederman ret = -ENOENT; 170328a6d671SEric W. Biederman if (!task) 170461a28784SEric W. Biederman goto out_no_task; 170528a6d671SEric W. Biederman 170628a6d671SEric W. Biederman ret = 0; 170728a6d671SEric W. Biederman pid = task->pid; 170828a6d671SEric W. Biederman i = filp->f_pos; 170928a6d671SEric W. Biederman switch (i) { 171028a6d671SEric W. Biederman case 0: 171128a6d671SEric W. Biederman ino = inode->i_ino; 171228a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 171328a6d671SEric W. Biederman goto out; 171428a6d671SEric W. Biederman i++; 171528a6d671SEric W. Biederman filp->f_pos++; 171628a6d671SEric W. Biederman /* fall through */ 171728a6d671SEric W. Biederman case 1: 171828a6d671SEric W. Biederman ino = parent_ino(dentry); 171928a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 172028a6d671SEric W. Biederman goto out; 172128a6d671SEric W. Biederman i++; 172228a6d671SEric W. Biederman filp->f_pos++; 172328a6d671SEric W. Biederman /* fall through */ 172428a6d671SEric W. Biederman default: 172528a6d671SEric W. Biederman i -= 2; 172628a6d671SEric W. Biederman if (i >= nents) { 172728a6d671SEric W. Biederman ret = 1; 172828a6d671SEric W. Biederman goto out; 172928a6d671SEric W. Biederman } 173028a6d671SEric W. Biederman p = ents + i; 17317bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 17327bcd6b0eSEric W. Biederman while (p <= last) { 173361a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 173428a6d671SEric W. Biederman goto out; 173528a6d671SEric W. Biederman filp->f_pos++; 173628a6d671SEric W. Biederman p++; 173728a6d671SEric W. Biederman } 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds 174028a6d671SEric W. Biederman ret = 1; 174128a6d671SEric W. Biederman out: 174261a28784SEric W. Biederman put_task_struct(task); 174361a28784SEric W. Biederman out_no_task: 174428a6d671SEric W. Biederman return ret; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 174828a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 174928a6d671SEric W. Biederman size_t count, loff_t *ppos) 175028a6d671SEric W. Biederman { 17512fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 175204ff9708SAl Viro char *p = NULL; 175328a6d671SEric W. Biederman ssize_t length; 175428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 175528a6d671SEric W. Biederman 175628a6d671SEric W. Biederman if (!task) 175704ff9708SAl Viro return -ESRCH; 175828a6d671SEric W. Biederman 175928a6d671SEric W. Biederman length = security_getprocattr(task, 17602fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 176104ff9708SAl Viro &p); 176228a6d671SEric W. Biederman put_task_struct(task); 176304ff9708SAl Viro if (length > 0) 176404ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 176504ff9708SAl Viro kfree(p); 176628a6d671SEric W. Biederman return length; 176728a6d671SEric W. Biederman } 176828a6d671SEric W. Biederman 176928a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 177028a6d671SEric W. Biederman size_t count, loff_t *ppos) 177128a6d671SEric W. Biederman { 17722fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 177328a6d671SEric W. Biederman char *page; 177428a6d671SEric W. Biederman ssize_t length; 177528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 177628a6d671SEric W. Biederman 177728a6d671SEric W. Biederman length = -ESRCH; 177828a6d671SEric W. Biederman if (!task) 177928a6d671SEric W. Biederman goto out_no_task; 178028a6d671SEric W. Biederman if (count > PAGE_SIZE) 178128a6d671SEric W. Biederman count = PAGE_SIZE; 178228a6d671SEric W. Biederman 178328a6d671SEric W. Biederman /* No partial writes. */ 178428a6d671SEric W. Biederman length = -EINVAL; 178528a6d671SEric W. Biederman if (*ppos != 0) 178628a6d671SEric W. Biederman goto out; 178728a6d671SEric W. Biederman 178828a6d671SEric W. Biederman length = -ENOMEM; 178928a6d671SEric W. Biederman page = (char*)__get_free_page(GFP_USER); 179028a6d671SEric W. Biederman if (!page) 179128a6d671SEric W. Biederman goto out; 179228a6d671SEric W. Biederman 179328a6d671SEric W. Biederman length = -EFAULT; 179428a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 179528a6d671SEric W. Biederman goto out_free; 179628a6d671SEric W. Biederman 179728a6d671SEric W. Biederman length = security_setprocattr(task, 17982fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 179928a6d671SEric W. Biederman (void*)page, count); 180028a6d671SEric W. Biederman out_free: 180128a6d671SEric W. Biederman free_page((unsigned long) page); 180228a6d671SEric W. Biederman out: 180328a6d671SEric W. Biederman put_task_struct(task); 180428a6d671SEric W. Biederman out_no_task: 180528a6d671SEric W. Biederman return length; 180628a6d671SEric W. Biederman } 180728a6d671SEric W. Biederman 180800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 180928a6d671SEric W. Biederman .read = proc_pid_attr_read, 181028a6d671SEric W. Biederman .write = proc_pid_attr_write, 181128a6d671SEric W. Biederman }; 181228a6d671SEric W. Biederman 1813c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 181461a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 181561a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 181661a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 181761a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 181861a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 181961a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 182028a6d671SEric W. Biederman }; 182128a6d671SEric W. Biederman 182272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 18231da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18241da177e4SLinus Torvalds { 18251da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 182672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 182900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 18301da177e4SLinus Torvalds .read = generic_read_dir, 183172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 18321da177e4SLinus Torvalds }; 18331da177e4SLinus Torvalds 183472d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 18351da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 18361da177e4SLinus Torvalds { 18377bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 18387bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 1841c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 184272d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 184399f89551SEric W. Biederman .getattr = pid_getattr, 18446d76fa58SLinus Torvalds .setattr = proc_setattr, 18451da177e4SLinus Torvalds }; 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds #endif 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds /* 18501da177e4SLinus Torvalds * /proc/self: 18511da177e4SLinus Torvalds */ 18521da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 18531da177e4SLinus Torvalds int buflen) 18541da177e4SLinus Torvalds { 18558578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 18561da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 18571da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 1860008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 18611da177e4SLinus Torvalds { 18628578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 18631da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1864008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 1867c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 18681da177e4SLinus Torvalds .readlink = proc_self_readlink, 18691da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 18701da177e4SLinus Torvalds }; 18711da177e4SLinus Torvalds 187228a6d671SEric W. Biederman /* 1873801199ceSEric W. Biederman * proc base 1874801199ceSEric W. Biederman * 1875801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 1876801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 1877801199ceSEric W. Biederman * describe something that is process related. 1878801199ceSEric W. Biederman */ 1879c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 188061a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 1881801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 1882801199ceSEric W. Biederman }; 1883801199ceSEric W. Biederman 1884801199ceSEric W. Biederman /* 1885801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 1886801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 1887801199ceSEric W. Biederman * due to the way we treat inodes. 1888801199ceSEric W. Biederman */ 1889801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 1890801199ceSEric W. Biederman { 1891801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 1892801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 1893801199ceSEric W. Biederman if (task) { 1894801199ceSEric W. Biederman put_task_struct(task); 1895801199ceSEric W. Biederman return 1; 1896801199ceSEric W. Biederman } 1897801199ceSEric W. Biederman d_drop(dentry); 1898801199ceSEric W. Biederman return 0; 1899801199ceSEric W. Biederman } 1900801199ceSEric W. Biederman 1901801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 1902801199ceSEric W. Biederman { 1903801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 1904801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 1905801199ceSEric W. Biederman }; 1906801199ceSEric W. Biederman 1907444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 1908c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1909801199ceSEric W. Biederman { 1910c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1911801199ceSEric W. Biederman struct inode *inode; 1912801199ceSEric W. Biederman struct proc_inode *ei; 1913444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1914801199ceSEric W. Biederman 1915801199ceSEric W. Biederman /* Allocate the inode */ 1916801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 1917801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 1918801199ceSEric W. Biederman if (!inode) 1919801199ceSEric W. Biederman goto out; 1920801199ceSEric W. Biederman 1921801199ceSEric W. Biederman /* Initialize the inode */ 1922801199ceSEric W. Biederman ei = PROC_I(inode); 1923801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 1924801199ceSEric W. Biederman 1925801199ceSEric W. Biederman /* 1926801199ceSEric W. Biederman * grab the reference to the task. 1927801199ceSEric W. Biederman */ 19281a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 1929801199ceSEric W. Biederman if (!ei->pid) 1930801199ceSEric W. Biederman goto out_iput; 1931801199ceSEric W. Biederman 1932801199ceSEric W. Biederman inode->i_uid = 0; 1933801199ceSEric W. Biederman inode->i_gid = 0; 1934801199ceSEric W. Biederman inode->i_mode = p->mode; 1935801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 1936801199ceSEric W. Biederman inode->i_nlink = 2; 1937801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 1938801199ceSEric W. Biederman inode->i_size = 64; 1939801199ceSEric W. Biederman if (p->iop) 1940801199ceSEric W. Biederman inode->i_op = p->iop; 1941801199ceSEric W. Biederman if (p->fop) 1942801199ceSEric W. Biederman inode->i_fop = p->fop; 1943801199ceSEric W. Biederman ei->op = p->op; 1944801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 1945801199ceSEric W. Biederman d_add(dentry, inode); 1946801199ceSEric W. Biederman error = NULL; 1947801199ceSEric W. Biederman out: 1948801199ceSEric W. Biederman return error; 1949801199ceSEric W. Biederman out_iput: 1950801199ceSEric W. Biederman iput(inode); 1951801199ceSEric W. Biederman goto out; 1952801199ceSEric W. Biederman } 1953801199ceSEric W. Biederman 1954444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 1955444ceed8SEric W. Biederman { 1956444ceed8SEric W. Biederman struct dentry *error; 1957444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1958c5141e6dSEric Dumazet const struct pid_entry *p, *last; 1959444ceed8SEric W. Biederman 1960444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 1961444ceed8SEric W. Biederman 1962444ceed8SEric W. Biederman if (!task) 1963444ceed8SEric W. Biederman goto out_no_task; 1964444ceed8SEric W. Biederman 1965444ceed8SEric W. Biederman /* Lookup the directory entry */ 19667bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 19677bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 1968444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 1969444ceed8SEric W. Biederman continue; 1970444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 1971444ceed8SEric W. Biederman break; 1972444ceed8SEric W. Biederman } 19737bcd6b0eSEric W. Biederman if (p > last) 1974444ceed8SEric W. Biederman goto out; 1975444ceed8SEric W. Biederman 1976444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 1977444ceed8SEric W. Biederman 1978444ceed8SEric W. Biederman out: 1979444ceed8SEric W. Biederman put_task_struct(task); 1980444ceed8SEric W. Biederman out_no_task: 1981444ceed8SEric W. Biederman return error; 1982444ceed8SEric W. Biederman } 1983444ceed8SEric W. Biederman 1984c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 1985c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 198661a28784SEric W. Biederman { 198761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 198861a28784SEric W. Biederman proc_base_instantiate, task, p); 198961a28784SEric W. Biederman } 199061a28784SEric W. Biederman 1991aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 1992aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 1993aba76fdbSAndrew Morton { 1994aba76fdbSAndrew Morton return sprintf(buffer, 19954b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 1996aba76fdbSAndrew Morton "rchar: %llu\n" 1997aba76fdbSAndrew Morton "wchar: %llu\n" 1998aba76fdbSAndrew Morton "syscr: %llu\n" 1999aba76fdbSAndrew Morton "syscw: %llu\n" 20004b98d11bSAlexey Dobriyan #endif 2001aba76fdbSAndrew Morton "read_bytes: %llu\n" 2002aba76fdbSAndrew Morton "write_bytes: %llu\n" 2003aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 20044b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2005aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2006aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2007aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2008aba76fdbSAndrew Morton (unsigned long long)task->syscw, 20094b98d11bSAlexey Dobriyan #endif 2010aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2011aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2012aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2013aba76fdbSAndrew Morton } 2014aba76fdbSAndrew Morton #endif 2015aba76fdbSAndrew Morton 2016801199ceSEric W. Biederman /* 201728a6d671SEric W. Biederman * Thread groups 201828a6d671SEric W. Biederman */ 201900977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2020c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 202120cdc894SEric W. Biederman 2022c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 202361a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 202461a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 202527932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 202661a28784SEric W. Biederman INF("environ", S_IRUSR, pid_environ), 202761a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 202861a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 2029*43ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2030*43ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 2031*43ae34cbSIngo Molnar #endif 203261a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 203361a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 203461a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 203561a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 203628a6d671SEric W. Biederman #ifdef CONFIG_NUMA 203761a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 203828a6d671SEric W. Biederman #endif 203961a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 204028a6d671SEric W. Biederman #ifdef CONFIG_SECCOMP 204161a28784SEric W. Biederman REG("seccomp", S_IRUSR|S_IWUSR, seccomp), 204228a6d671SEric W. Biederman #endif 204361a28784SEric W. Biederman LNK("cwd", cwd), 204461a28784SEric W. Biederman LNK("root", root), 204561a28784SEric W. Biederman LNK("exe", exe), 204661a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 204761a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 204828a6d671SEric W. Biederman #ifdef CONFIG_MMU 2049b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 205061a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 205128a6d671SEric W. Biederman #endif 205228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 205372d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 205428a6d671SEric W. Biederman #endif 205528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 205661a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 205728a6d671SEric W. Biederman #endif 205828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 205961a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 206028a6d671SEric W. Biederman #endif 206128a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 206261a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 206328a6d671SEric W. Biederman #endif 206461a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 206561a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 206628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 206761a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 206828a6d671SEric W. Biederman #endif 2069f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2070f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2071f4f154fdSAkinobu Mita #endif 2072aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2073aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2074aba76fdbSAndrew Morton #endif 207528a6d671SEric W. Biederman }; 207628a6d671SEric W. Biederman 207728a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 207828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 207928a6d671SEric W. Biederman { 208028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 208128a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 208228a6d671SEric W. Biederman } 208328a6d671SEric W. Biederman 208400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 208528a6d671SEric W. Biederman .read = generic_read_dir, 208628a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 208728a6d671SEric W. Biederman }; 208828a6d671SEric W. Biederman 208928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 20907bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 20917bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 209228a6d671SEric W. Biederman } 209328a6d671SEric W. Biederman 2094c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 209528a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 209628a6d671SEric W. Biederman .getattr = pid_getattr, 209728a6d671SEric W. Biederman .setattr = proc_setattr, 209828a6d671SEric W. Biederman }; 209928a6d671SEric W. Biederman 21001da177e4SLinus Torvalds /** 210148e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 21021da177e4SLinus Torvalds * 210348e6484dSEric W. Biederman * @task: task that should be flushed. 21041da177e4SLinus Torvalds * 210548e6484dSEric W. Biederman * Looks in the dcache for 210648e6484dSEric W. Biederman * /proc/@pid 210748e6484dSEric W. Biederman * /proc/@tgid/task/@pid 210848e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 210948e6484dSEric W. Biederman * from the dcache. 211048e6484dSEric W. Biederman * 211148e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 211248e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 211348e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 211448e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 211548e6484dSEric W. Biederman * dcache entries at process exit time. 211648e6484dSEric W. Biederman * 211748e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 211848e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 211948e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 21201da177e4SLinus Torvalds */ 212148e6484dSEric W. Biederman void proc_flush_task(struct task_struct *task) 21221da177e4SLinus Torvalds { 212348e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 21248578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 212548e6484dSEric W. Biederman struct qstr name; 21261da177e4SLinus Torvalds 212748e6484dSEric W. Biederman name.name = buf; 212848e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 212948e6484dSEric W. Biederman dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); 213048e6484dSEric W. Biederman if (dentry) { 213148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 213248e6484dSEric W. Biederman d_drop(dentry); 213348e6484dSEric W. Biederman dput(dentry); 21341da177e4SLinus Torvalds } 21351da177e4SLinus Torvalds 213648e6484dSEric W. Biederman if (thread_group_leader(task)) 213748e6484dSEric W. Biederman goto out; 21381da177e4SLinus Torvalds 213948e6484dSEric W. Biederman name.name = buf; 214048e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); 214148e6484dSEric W. Biederman leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); 214248e6484dSEric W. Biederman if (!leader) 214348e6484dSEric W. Biederman goto out; 214448e6484dSEric W. Biederman 214548e6484dSEric W. Biederman name.name = "task"; 214648e6484dSEric W. Biederman name.len = strlen(name.name); 214748e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 214848e6484dSEric W. Biederman if (!dir) 214948e6484dSEric W. Biederman goto out_put_leader; 215048e6484dSEric W. Biederman 215148e6484dSEric W. Biederman name.name = buf; 215248e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 215348e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 215448e6484dSEric W. Biederman if (dentry) { 215548e6484dSEric W. Biederman shrink_dcache_parent(dentry); 215648e6484dSEric W. Biederman d_drop(dentry); 215748e6484dSEric W. Biederman dput(dentry); 21581da177e4SLinus Torvalds } 215948e6484dSEric W. Biederman 216048e6484dSEric W. Biederman dput(dir); 216148e6484dSEric W. Biederman out_put_leader: 216248e6484dSEric W. Biederman dput(leader); 216348e6484dSEric W. Biederman out: 216448e6484dSEric W. Biederman return; 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 21679711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 21689711ef99SAdrian Bunk struct dentry * dentry, 2169c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2170444ceed8SEric W. Biederman { 2171444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2172444ceed8SEric W. Biederman struct inode *inode; 2173444ceed8SEric W. Biederman 217461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2175444ceed8SEric W. Biederman if (!inode) 2176444ceed8SEric W. Biederman goto out; 2177444ceed8SEric W. Biederman 2178444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2179444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2180444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2181444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 218227932742SMiklos Szeredi inode->i_nlink = 5; 2183444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2184444ceed8SEric W. Biederman inode->i_nlink += 1; 2185444ceed8SEric W. Biederman #endif 2186444ceed8SEric W. Biederman 2187444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2188444ceed8SEric W. Biederman 2189444ceed8SEric W. Biederman d_add(dentry, inode); 2190444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2191444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2192444ceed8SEric W. Biederman error = NULL; 2193444ceed8SEric W. Biederman out: 2194444ceed8SEric W. Biederman return error; 2195444ceed8SEric W. Biederman } 2196444ceed8SEric W. Biederman 21971da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 21981da177e4SLinus Torvalds { 2199cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 22001da177e4SLinus Torvalds struct task_struct *task; 22011da177e4SLinus Torvalds unsigned tgid; 22021da177e4SLinus Torvalds 2203801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2204801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2205801199ceSEric W. Biederman goto out; 2206801199ceSEric W. Biederman 22071da177e4SLinus Torvalds tgid = name_to_int(dentry); 22081da177e4SLinus Torvalds if (tgid == ~0U) 22091da177e4SLinus Torvalds goto out; 22101da177e4SLinus Torvalds 2211de758734SEric W. Biederman rcu_read_lock(); 22121da177e4SLinus Torvalds task = find_task_by_pid(tgid); 22131da177e4SLinus Torvalds if (task) 22141da177e4SLinus Torvalds get_task_struct(task); 2215de758734SEric W. Biederman rcu_read_unlock(); 22161da177e4SLinus Torvalds if (!task) 22171da177e4SLinus Torvalds goto out; 22181da177e4SLinus Torvalds 2219444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 222048e6484dSEric W. Biederman put_task_struct(task); 22211da177e4SLinus Torvalds out: 2222cd6a3ce9SEric W. Biederman return result; 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds /* 22260804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 22270bc58a91SEric W. Biederman * 22281da177e4SLinus Torvalds */ 22290804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 22301da177e4SLinus Torvalds { 22310804ef4bSEric W. Biederman struct task_struct *task; 22320804ef4bSEric W. Biederman struct pid *pid; 22331da177e4SLinus Torvalds 22340804ef4bSEric W. Biederman rcu_read_lock(); 22350804ef4bSEric W. Biederman retry: 22360804ef4bSEric W. Biederman task = NULL; 22370804ef4bSEric W. Biederman pid = find_ge_pid(tgid); 22380804ef4bSEric W. Biederman if (pid) { 22390804ef4bSEric W. Biederman tgid = pid->nr + 1; 22400804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 22410804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 22420804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 22430804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 22440804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 22450804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 22460804ef4bSEric W. Biederman * 22470804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 22480804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 22490804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 22500804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 22510804ef4bSEric W. Biederman * As we don't care in the case of readdir. 22520bc58a91SEric W. Biederman */ 22530804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 22540804ef4bSEric W. Biederman goto retry; 22550804ef4bSEric W. Biederman get_task_struct(task); 22561da177e4SLinus Torvalds } 2257454cc105SEric W. Biederman rcu_read_unlock(); 22580804ef4bSEric W. Biederman return task; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22617bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 22621da177e4SLinus Torvalds 226361a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 226461a28784SEric W. Biederman struct task_struct *task, int tgid) 226561a28784SEric W. Biederman { 226661a28784SEric W. Biederman char name[PROC_NUMBUF]; 226761a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 226861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 226961a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 227061a28784SEric W. Biederman } 227161a28784SEric W. Biederman 22721da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 22731da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 22741da177e4SLinus Torvalds { 22751da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 22762fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 22770bc58a91SEric W. Biederman struct task_struct *task; 22780bc58a91SEric W. Biederman int tgid; 22791da177e4SLinus Torvalds 228061a28784SEric W. Biederman if (!reaper) 228161a28784SEric W. Biederman goto out_no_task; 228261a28784SEric W. Biederman 22837bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2284c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 228561a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2286801199ceSEric W. Biederman goto out; 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22890804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 22900804ef4bSEric W. Biederman for (task = next_tgid(tgid); 22910bc58a91SEric W. Biederman task; 22920804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 22930bc58a91SEric W. Biederman tgid = task->pid; 22940804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 229561a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 22960bc58a91SEric W. Biederman put_task_struct(task); 22970804ef4bSEric W. Biederman goto out; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds } 23000804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 23010804ef4bSEric W. Biederman out: 230261a28784SEric W. Biederman put_task_struct(reaper); 230361a28784SEric W. Biederman out_no_task: 23041da177e4SLinus Torvalds return 0; 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds 23070bc58a91SEric W. Biederman /* 230828a6d671SEric W. Biederman * Tasks 230928a6d671SEric W. Biederman */ 2310c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 231161a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 231227932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 231361a28784SEric W. Biederman INF("environ", S_IRUSR, pid_environ), 231461a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 231561a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 2316*43ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2317*43ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 2318*43ae34cbSIngo Molnar #endif 231961a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 232061a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 232161a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 232261a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 232328a6d671SEric W. Biederman #ifdef CONFIG_NUMA 232461a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 232528a6d671SEric W. Biederman #endif 232661a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 232728a6d671SEric W. Biederman #ifdef CONFIG_SECCOMP 232861a28784SEric W. Biederman REG("seccomp", S_IRUSR|S_IWUSR, seccomp), 232928a6d671SEric W. Biederman #endif 233061a28784SEric W. Biederman LNK("cwd", cwd), 233161a28784SEric W. Biederman LNK("root", root), 233261a28784SEric W. Biederman LNK("exe", exe), 233361a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 233428a6d671SEric W. Biederman #ifdef CONFIG_MMU 2335b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 233661a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 233728a6d671SEric W. Biederman #endif 233828a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 233972d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 234028a6d671SEric W. Biederman #endif 234128a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 234261a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 234328a6d671SEric W. Biederman #endif 234428a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 234561a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 234628a6d671SEric W. Biederman #endif 234728a6d671SEric W. Biederman #ifdef CONFIG_CPUSETS 234861a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 234928a6d671SEric W. Biederman #endif 235061a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 235161a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 235228a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 235361a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 235428a6d671SEric W. Biederman #endif 2355f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2356f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2357f4f154fdSAkinobu Mita #endif 235828a6d671SEric W. Biederman }; 235928a6d671SEric W. Biederman 236028a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 236128a6d671SEric W. Biederman void * dirent, filldir_t filldir) 236228a6d671SEric W. Biederman { 236328a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 236428a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 236528a6d671SEric W. Biederman } 236628a6d671SEric W. Biederman 236728a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 23687bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 23697bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 237028a6d671SEric W. Biederman } 237128a6d671SEric W. Biederman 237200977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 237328a6d671SEric W. Biederman .read = generic_read_dir, 237428a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 237528a6d671SEric W. Biederman }; 237628a6d671SEric W. Biederman 2377c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 237828a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 237928a6d671SEric W. Biederman .getattr = pid_getattr, 238028a6d671SEric W. Biederman .setattr = proc_setattr, 238128a6d671SEric W. Biederman }; 238228a6d671SEric W. Biederman 2383444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2384c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2385444ceed8SEric W. Biederman { 2386444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2387444ceed8SEric W. Biederman struct inode *inode; 238861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2389444ceed8SEric W. Biederman 2390444ceed8SEric W. Biederman if (!inode) 2391444ceed8SEric W. Biederman goto out; 2392444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2393444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2394444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2395444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 239627932742SMiklos Szeredi inode->i_nlink = 4; 2397444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2398444ceed8SEric W. Biederman inode->i_nlink += 1; 2399444ceed8SEric W. Biederman #endif 2400444ceed8SEric W. Biederman 2401444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2402444ceed8SEric W. Biederman 2403444ceed8SEric W. Biederman d_add(dentry, inode); 2404444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2405444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2406444ceed8SEric W. Biederman error = NULL; 2407444ceed8SEric W. Biederman out: 2408444ceed8SEric W. Biederman return error; 2409444ceed8SEric W. Biederman } 2410444ceed8SEric W. Biederman 241128a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 241228a6d671SEric W. Biederman { 241328a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 241428a6d671SEric W. Biederman struct task_struct *task; 241528a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 241628a6d671SEric W. Biederman unsigned tid; 241728a6d671SEric W. Biederman 241828a6d671SEric W. Biederman if (!leader) 241928a6d671SEric W. Biederman goto out_no_task; 242028a6d671SEric W. Biederman 242128a6d671SEric W. Biederman tid = name_to_int(dentry); 242228a6d671SEric W. Biederman if (tid == ~0U) 242328a6d671SEric W. Biederman goto out; 242428a6d671SEric W. Biederman 242528a6d671SEric W. Biederman rcu_read_lock(); 242628a6d671SEric W. Biederman task = find_task_by_pid(tid); 242728a6d671SEric W. Biederman if (task) 242828a6d671SEric W. Biederman get_task_struct(task); 242928a6d671SEric W. Biederman rcu_read_unlock(); 243028a6d671SEric W. Biederman if (!task) 243128a6d671SEric W. Biederman goto out; 243228a6d671SEric W. Biederman if (leader->tgid != task->tgid) 243328a6d671SEric W. Biederman goto out_drop_task; 243428a6d671SEric W. Biederman 2435444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 243628a6d671SEric W. Biederman out_drop_task: 243728a6d671SEric W. Biederman put_task_struct(task); 243828a6d671SEric W. Biederman out: 243928a6d671SEric W. Biederman put_task_struct(leader); 244028a6d671SEric W. Biederman out_no_task: 244128a6d671SEric W. Biederman return result; 244228a6d671SEric W. Biederman } 244328a6d671SEric W. Biederman 244428a6d671SEric W. Biederman /* 24450bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 24460bc58a91SEric W. Biederman * 24470bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 24480bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 24490bc58a91SEric W. Biederman * directory we have more work todo. 24500bc58a91SEric W. Biederman * 24510bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 24520bc58a91SEric W. Biederman * 24530bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 24540bc58a91SEric W. Biederman * threads past it. 24550bc58a91SEric W. Biederman */ 2456cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2457cc288738SEric W. Biederman int tid, int nr) 24580bc58a91SEric W. Biederman { 2459a872ff0cSOleg Nesterov struct task_struct *pos; 24600bc58a91SEric W. Biederman 2461cc288738SEric W. Biederman rcu_read_lock(); 24620bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 24630bc58a91SEric W. Biederman if (tid && (nr > 0)) { 24640bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2465a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2466a872ff0cSOleg Nesterov goto found; 24670bc58a91SEric W. Biederman } 24680bc58a91SEric W. Biederman 24690bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 24700bc58a91SEric W. Biederman pos = NULL; 2471a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2472a872ff0cSOleg Nesterov goto out; 2473a872ff0cSOleg Nesterov 2474a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2475a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2476a872ff0cSOleg Nesterov */ 2477a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2478a872ff0cSOleg Nesterov pos = next_thread(pos); 2479a872ff0cSOleg Nesterov if (pos == leader) { 2480a872ff0cSOleg Nesterov pos = NULL; 2481a872ff0cSOleg Nesterov goto out; 2482a872ff0cSOleg Nesterov } 2483a872ff0cSOleg Nesterov } 2484a872ff0cSOleg Nesterov found: 2485a872ff0cSOleg Nesterov get_task_struct(pos); 2486a872ff0cSOleg Nesterov out: 2487cc288738SEric W. Biederman rcu_read_unlock(); 24880bc58a91SEric W. Biederman return pos; 24890bc58a91SEric W. Biederman } 24900bc58a91SEric W. Biederman 24910bc58a91SEric W. Biederman /* 24920bc58a91SEric W. Biederman * Find the next thread in the thread list. 24930bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 24940bc58a91SEric W. Biederman * 24950bc58a91SEric W. Biederman * The reference to the input task_struct is released. 24960bc58a91SEric W. Biederman */ 24970bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 24980bc58a91SEric W. Biederman { 2499c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2500cc288738SEric W. Biederman rcu_read_lock(); 2501c1df7fb8SOleg Nesterov if (pid_alive(start)) { 25020bc58a91SEric W. Biederman pos = next_thread(start); 2503c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 25040bc58a91SEric W. Biederman pos = NULL; 2505c1df7fb8SOleg Nesterov else 2506c1df7fb8SOleg Nesterov get_task_struct(pos); 2507c1df7fb8SOleg Nesterov } 2508cc288738SEric W. Biederman rcu_read_unlock(); 25090bc58a91SEric W. Biederman put_task_struct(start); 25100bc58a91SEric W. Biederman return pos; 25110bc58a91SEric W. Biederman } 25120bc58a91SEric W. Biederman 251361a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 251461a28784SEric W. Biederman struct task_struct *task, int tid) 251561a28784SEric W. Biederman { 251661a28784SEric W. Biederman char name[PROC_NUMBUF]; 251761a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 251861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 251961a28784SEric W. Biederman proc_task_instantiate, task, NULL); 252061a28784SEric W. Biederman } 252161a28784SEric W. Biederman 25221da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 25231da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 25241da177e4SLinus Torvalds { 25252fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 25261da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25277d895244SGuillaume Chazarain struct task_struct *leader = NULL; 25280bc58a91SEric W. Biederman struct task_struct *task; 25291da177e4SLinus Torvalds int retval = -ENOENT; 25301da177e4SLinus Torvalds ino_t ino; 25310bc58a91SEric W. Biederman int tid; 25321da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 25331da177e4SLinus Torvalds 25347d895244SGuillaume Chazarain task = get_proc_task(inode); 25357d895244SGuillaume Chazarain if (!task) 25367d895244SGuillaume Chazarain goto out_no_task; 25377d895244SGuillaume Chazarain rcu_read_lock(); 25387d895244SGuillaume Chazarain if (pid_alive(task)) { 25397d895244SGuillaume Chazarain leader = task->group_leader; 25407d895244SGuillaume Chazarain get_task_struct(leader); 25417d895244SGuillaume Chazarain } 25427d895244SGuillaume Chazarain rcu_read_unlock(); 25437d895244SGuillaume Chazarain put_task_struct(task); 254499f89551SEric W. Biederman if (!leader) 254599f89551SEric W. Biederman goto out_no_task; 25461da177e4SLinus Torvalds retval = 0; 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds switch (pos) { 25491da177e4SLinus Torvalds case 0: 25501da177e4SLinus Torvalds ino = inode->i_ino; 25511da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 25521da177e4SLinus Torvalds goto out; 25531da177e4SLinus Torvalds pos++; 25541da177e4SLinus Torvalds /* fall through */ 25551da177e4SLinus Torvalds case 1: 25561da177e4SLinus Torvalds ino = parent_ino(dentry); 25571da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 25581da177e4SLinus Torvalds goto out; 25591da177e4SLinus Torvalds pos++; 25601da177e4SLinus Torvalds /* fall through */ 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25630bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 25640bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 25650bc58a91SEric W. Biederman */ 25660bc58a91SEric W. Biederman tid = filp->f_version; 25670bc58a91SEric W. Biederman filp->f_version = 0; 25680bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 25690bc58a91SEric W. Biederman task; 25700bc58a91SEric W. Biederman task = next_tid(task), pos++) { 25710bc58a91SEric W. Biederman tid = task->pid; 257261a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 25730bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 25740bc58a91SEric W. Biederman * pid for the next readir call */ 25750bc58a91SEric W. Biederman filp->f_version = tid; 25760bc58a91SEric W. Biederman put_task_struct(task); 25771da177e4SLinus Torvalds break; 25780bc58a91SEric W. Biederman } 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds out: 25811da177e4SLinus Torvalds filp->f_pos = pos; 258299f89551SEric W. Biederman put_task_struct(leader); 258399f89551SEric W. Biederman out_no_task: 25841da177e4SLinus Torvalds return retval; 25851da177e4SLinus Torvalds } 25866e66b52bSEric W. Biederman 25876e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 25886e66b52bSEric W. Biederman { 25896e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 259099f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 25916e66b52bSEric W. Biederman generic_fillattr(inode, stat); 25926e66b52bSEric W. Biederman 259399f89551SEric W. Biederman if (p) { 259499f89551SEric W. Biederman rcu_read_lock(); 259599f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 259699f89551SEric W. Biederman rcu_read_unlock(); 259799f89551SEric W. Biederman put_task_struct(p); 25986e66b52bSEric W. Biederman } 25996e66b52bSEric W. Biederman 26006e66b52bSEric W. Biederman return 0; 26016e66b52bSEric W. Biederman } 260228a6d671SEric W. Biederman 2603c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 260428a6d671SEric W. Biederman .lookup = proc_task_lookup, 260528a6d671SEric W. Biederman .getattr = proc_task_getattr, 260628a6d671SEric W. Biederman .setattr = proc_setattr, 260728a6d671SEric W. Biederman }; 260828a6d671SEric W. Biederman 260900977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 261028a6d671SEric W. Biederman .read = generic_read_dir, 261128a6d671SEric W. Biederman .readdir = proc_task_readdir, 261228a6d671SEric W. Biederman }; 2613