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> 565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66a63d83f4SDavid Rientjes #include <linux/swap.h> 67b835996fSDipankar Sarma #include <linux/rcupdate.h> 681da177e4SLinus Torvalds #include <linux/kallsyms.h> 692ec220e2SKen Chen #include <linux/stacktrace.h> 70d85f50d5SNeil Horman #include <linux/resource.h> 715096add8SKees Cook #include <linux/module.h> 721da177e4SLinus Torvalds #include <linux/mount.h> 731da177e4SLinus Torvalds #include <linux/security.h> 741da177e4SLinus Torvalds #include <linux/ptrace.h> 750d094efeSRoland McGrath #include <linux/tracehook.h> 76a424316cSPaul Menage #include <linux/cgroup.h> 771da177e4SLinus Torvalds #include <linux/cpuset.h> 781da177e4SLinus Torvalds #include <linux/audit.h> 795addc5ddSAl Viro #include <linux/poll.h> 801651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 818ac773b4SAlexey Dobriyan #include <linux/oom.h> 823cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8360347f67SPavel Emelyanov #include <linux/pid_namespace.h> 845ad4e53bSAl Viro #include <linux/fs_struct.h> 855a0e3ad6STejun Heo #include <linux/slab.h> 86640708a2SPavel Emelyanov #include <linux/flex_array.h> 87f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 88f133eccaSChris Metcalf #include <asm/hardwall.h> 89f133eccaSChris Metcalf #endif 9043d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h> 911da177e4SLinus Torvalds #include "internal.h" 921da177e4SLinus Torvalds 930f2fe20fSEric W. Biederman /* NOTE: 940f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 950f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 960f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 970f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 980f2fe20fSEric W. Biederman * 990f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 1000f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 1010f2fe20fSEric W. Biederman */ 1020f2fe20fSEric W. Biederman 1031da177e4SLinus Torvalds struct pid_entry { 1041da177e4SLinus Torvalds char *name; 105c5141e6dSEric Dumazet int len; 106d161a13fSAl Viro umode_t mode; 107c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10800977a59SArjan van de Ven const struct file_operations *fop; 10920cdc894SEric W. Biederman union proc_op op; 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 11261a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 11320cdc894SEric W. Biederman .name = (NAME), \ 114c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11520cdc894SEric W. Biederman .mode = MODE, \ 11620cdc894SEric W. Biederman .iop = IOP, \ 11720cdc894SEric W. Biederman .fop = FOP, \ 11820cdc894SEric W. Biederman .op = OP, \ 11920cdc894SEric W. Biederman } 12020cdc894SEric W. Biederman 121631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 122631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 123631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 12461a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12520cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 126631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 127631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 128631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 129631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 13061a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 13120cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 132631f9c18SAlexey Dobriyan { .proc_read = read } ) 133631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 134be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 135be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 136631f9c18SAlexey Dobriyan { .proc_show = show } ) 1371da177e4SLinus Torvalds 138640708a2SPavel Emelyanov static int proc_fd_permission(struct inode *inode, int mask); 139640708a2SPavel Emelyanov 140aed54175SVegard Nossum /* 141aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 142aed54175SVegard Nossum * and .. links. 143aed54175SVegard Nossum */ 144aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 145aed54175SVegard Nossum unsigned int n) 146aed54175SVegard Nossum { 147aed54175SVegard Nossum unsigned int i; 148aed54175SVegard Nossum unsigned int count; 149aed54175SVegard Nossum 150aed54175SVegard Nossum count = 0; 151aed54175SVegard Nossum for (i = 0; i < n; ++i) { 152aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 153aed54175SVegard Nossum ++count; 154aed54175SVegard Nossum } 155aed54175SVegard Nossum 156aed54175SVegard Nossum return count; 157aed54175SVegard Nossum } 158aed54175SVegard Nossum 159f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1601da177e4SLinus Torvalds { 1617c2c7d99SHugh Dickins int result = -ENOENT; 1627c2c7d99SHugh Dickins 1630494f6ecSMiklos Szeredi task_lock(task); 164f7ad3c6bSMiklos Szeredi if (task->fs) { 165f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1667c2c7d99SHugh Dickins result = 0; 1677c2c7d99SHugh Dickins } 1680494f6ecSMiklos Szeredi task_unlock(task); 1697c2c7d99SHugh Dickins return result; 1700494f6ecSMiklos Szeredi } 1710494f6ecSMiklos Szeredi 1727773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path) 1730494f6ecSMiklos Szeredi { 1747773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1750494f6ecSMiklos Szeredi int result = -ENOENT; 17699f89551SEric W. Biederman 17799f89551SEric W. Biederman if (task) { 178f7ad3c6bSMiklos Szeredi task_lock(task); 179f7ad3c6bSMiklos Szeredi if (task->fs) { 180f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 181f7ad3c6bSMiklos Szeredi result = 0; 182f7ad3c6bSMiklos Szeredi } 183f7ad3c6bSMiklos Szeredi task_unlock(task); 18499f89551SEric W. Biederman put_task_struct(task); 18599f89551SEric W. Biederman } 1861da177e4SLinus Torvalds return result; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1897773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path) 1901da177e4SLinus Torvalds { 1917773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1921da177e4SLinus Torvalds int result = -ENOENT; 19399f89551SEric W. Biederman 19499f89551SEric W. Biederman if (task) { 195f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 19699f89551SEric W. Biederman put_task_struct(task); 19799f89551SEric W. Biederman } 1981da177e4SLinus Torvalds return result; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 201e268337dSLinus Torvalds struct mm_struct *mm_for_maps(struct task_struct *task) 202e268337dSLinus Torvalds { 203e268337dSLinus Torvalds return mm_access(task, PTRACE_MODE_READ); 204e268337dSLinus Torvalds } 205e268337dSLinus Torvalds 2061da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds int res = 0; 2091da177e4SLinus Torvalds unsigned int len; 2101da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2111da177e4SLinus Torvalds if (!mm) 2121da177e4SLinus Torvalds goto out; 2131da177e4SLinus Torvalds if (!mm->arg_end) 2141da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds if (len > PAGE_SIZE) 2191da177e4SLinus Torvalds len = PAGE_SIZE; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2241da177e4SLinus Torvalds // assume application is using setproctitle(3). 2251da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2261da177e4SLinus Torvalds len = strnlen(buffer, res); 2271da177e4SLinus Torvalds if (len < res) { 2281da177e4SLinus Torvalds res = len; 2291da177e4SLinus Torvalds } else { 2301da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2311da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2321da177e4SLinus Torvalds len = PAGE_SIZE - res; 2331da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2341da177e4SLinus Torvalds res = strnlen(buffer, res); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds out_mm: 2381da177e4SLinus Torvalds mmput(mm); 2391da177e4SLinus Torvalds out: 2401da177e4SLinus Torvalds return res; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2441da177e4SLinus Torvalds { 2452fadaef4SAl Viro struct mm_struct *mm = mm_for_maps(task); 2462fadaef4SAl Viro int res = PTR_ERR(mm); 2472fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 2481da177e4SLinus Torvalds unsigned int nwords = 0; 249dfe6b7d9SHannes Eder do { 2501da177e4SLinus Torvalds nwords += 2; 251dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2521da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2531da177e4SLinus Torvalds if (res > PAGE_SIZE) 2541da177e4SLinus Torvalds res = PAGE_SIZE; 2551da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2561da177e4SLinus Torvalds mmput(mm); 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds return res; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2631da177e4SLinus Torvalds /* 2641da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2651da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2681da177e4SLinus Torvalds { 269ffb45122SAlexey Dobriyan unsigned long wchan; 2709281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds wchan = get_wchan(task); 2731da177e4SLinus Torvalds 2749d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 275f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 276f83ce3e6SJake Edge return 0; 277f83ce3e6SJake Edge else 2781da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2799d65cb4aSAlexey Dobriyan else 2809d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2831da177e4SLinus Torvalds 284a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 285a9712bc1SAl Viro { 286a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 287a9712bc1SAl Viro if (err) 288a9712bc1SAl Viro return err; 289a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 290a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 291a9712bc1SAl Viro return -EPERM; 292a9712bc1SAl Viro } 293a9712bc1SAl Viro return 0; 294a9712bc1SAl Viro } 295a9712bc1SAl Viro 296a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 297a9712bc1SAl Viro { 298a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 299a9712bc1SAl Viro } 300a9712bc1SAl Viro 3012ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3022ec220e2SKen Chen 3032ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3042ec220e2SKen Chen 3052ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3062ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3072ec220e2SKen Chen { 3082ec220e2SKen Chen struct stack_trace trace; 3092ec220e2SKen Chen unsigned long *entries; 310a9712bc1SAl Viro int err; 3112ec220e2SKen Chen int i; 3122ec220e2SKen Chen 3132ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3142ec220e2SKen Chen if (!entries) 3152ec220e2SKen Chen return -ENOMEM; 3162ec220e2SKen Chen 3172ec220e2SKen Chen trace.nr_entries = 0; 3182ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3192ec220e2SKen Chen trace.entries = entries; 3202ec220e2SKen Chen trace.skip = 0; 321a9712bc1SAl Viro 322a9712bc1SAl Viro err = lock_trace(task); 323a9712bc1SAl Viro if (!err) { 3242ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3252ec220e2SKen Chen 3262ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 32751e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 3282ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3292ec220e2SKen Chen } 330a9712bc1SAl Viro unlock_trace(task); 331a9712bc1SAl Viro } 3322ec220e2SKen Chen kfree(entries); 3332ec220e2SKen Chen 334a9712bc1SAl Viro return err; 3352ec220e2SKen Chen } 3362ec220e2SKen Chen #endif 3372ec220e2SKen Chen 3381da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3431da177e4SLinus Torvalds { 344172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 345826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 346826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3472d72376bSIngo Molnar task->sched_info.pcount); 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds #endif 3501da177e4SLinus Torvalds 3519745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3529745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3539745512cSArjan van de Ven { 3549745512cSArjan van de Ven int i; 35513d77c37SHiroshi Shimamoto struct inode *inode = m->private; 35613d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3579745512cSArjan van de Ven 35813d77c37SHiroshi Shimamoto if (!task) 35913d77c37SHiroshi Shimamoto return -ESRCH; 36013d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3619745512cSArjan van de Ven for (i = 0; i < 32; i++) { 36234e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 36334e49d4fSJoe Perches if (lr->backtrace[0]) { 3649745512cSArjan van de Ven int q; 3659745512cSArjan van de Ven seq_printf(m, "%i %li %li", 36634e49d4fSJoe Perches lr->count, lr->time, lr->max); 3679745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 36834e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 36934e49d4fSJoe Perches if (!bt) 3709745512cSArjan van de Ven break; 37134e49d4fSJoe Perches if (bt == ULONG_MAX) 3729745512cSArjan van de Ven break; 37334e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 3749745512cSArjan van de Ven } 3759d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 3769745512cSArjan van de Ven } 3779745512cSArjan van de Ven 3789745512cSArjan van de Ven } 37913d77c37SHiroshi Shimamoto put_task_struct(task); 3809745512cSArjan van de Ven return 0; 3819745512cSArjan van de Ven } 3829745512cSArjan van de Ven 3839745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3849745512cSArjan van de Ven { 38513d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 386d6643d12SHiroshi Shimamoto } 387d6643d12SHiroshi Shimamoto 3889745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3899745512cSArjan van de Ven size_t count, loff_t *offs) 3909745512cSArjan van de Ven { 39113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 3929745512cSArjan van de Ven 39313d77c37SHiroshi Shimamoto if (!task) 39413d77c37SHiroshi Shimamoto return -ESRCH; 3959745512cSArjan van de Ven clear_all_latency_tracing(task); 39613d77c37SHiroshi Shimamoto put_task_struct(task); 3979745512cSArjan van de Ven 3989745512cSArjan van de Ven return count; 3999745512cSArjan van de Ven } 4009745512cSArjan van de Ven 4019745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4029745512cSArjan van de Ven .open = lstats_open, 4039745512cSArjan van de Ven .read = seq_read, 4049745512cSArjan van de Ven .write = lstats_write, 4059745512cSArjan van de Ven .llseek = seq_lseek, 40613d77c37SHiroshi Shimamoto .release = single_release, 4079745512cSArjan van de Ven }; 4089745512cSArjan van de Ven 4099745512cSArjan van de Ven #endif 4109745512cSArjan van de Ven 4111da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4121da177e4SLinus Torvalds { 413b95c35e7SOleg Nesterov unsigned long points = 0; 4141da177e4SLinus Torvalds 41519c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 416b95c35e7SOleg Nesterov if (pid_alive(task)) 417a63d83f4SDavid Rientjes points = oom_badness(task, NULL, NULL, 418a63d83f4SDavid Rientjes totalram_pages + total_swap_pages); 41919c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4201da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 423d85f50d5SNeil Horman struct limit_names { 424d85f50d5SNeil Horman char *name; 425d85f50d5SNeil Horman char *unit; 426d85f50d5SNeil Horman }; 427d85f50d5SNeil Horman 428d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 429cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 430d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 431d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 432d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 433d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 434d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 435d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 436d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 437d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 438d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 439d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 440d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 441d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 442d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 443d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4448808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 445d85f50d5SNeil Horman }; 446d85f50d5SNeil Horman 447d85f50d5SNeil Horman /* Display limits for a process */ 448d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 449d85f50d5SNeil Horman { 450d85f50d5SNeil Horman unsigned int i; 451d85f50d5SNeil Horman int count = 0; 452d85f50d5SNeil Horman unsigned long flags; 453d85f50d5SNeil Horman char *bufptr = buffer; 454d85f50d5SNeil Horman 455d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 456d85f50d5SNeil Horman 457a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 458d85f50d5SNeil Horman return 0; 459d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 460d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 461d85f50d5SNeil Horman 462d85f50d5SNeil Horman /* 463d85f50d5SNeil Horman * print the file header 464d85f50d5SNeil Horman */ 465d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 466d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 467d85f50d5SNeil Horman 468d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 469d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 470d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 471d85f50d5SNeil Horman lnames[i].name, "unlimited"); 472d85f50d5SNeil Horman else 473d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 474d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 475d85f50d5SNeil Horman 476d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 477d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 478d85f50d5SNeil Horman else 479d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 480d85f50d5SNeil Horman rlim[i].rlim_max); 481d85f50d5SNeil Horman 482d85f50d5SNeil Horman if (lnames[i].unit) 483d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 484d85f50d5SNeil Horman lnames[i].unit); 485d85f50d5SNeil Horman else 486d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 487d85f50d5SNeil Horman } 488d85f50d5SNeil Horman 489d85f50d5SNeil Horman return count; 490d85f50d5SNeil Horman } 491d85f50d5SNeil Horman 492ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 493ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 494ebcb6734SRoland McGrath { 495ebcb6734SRoland McGrath long nr; 496ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 497a9712bc1SAl Viro int res = lock_trace(task); 498a9712bc1SAl Viro if (res) 499a9712bc1SAl Viro return res; 500ebcb6734SRoland McGrath 501ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 502a9712bc1SAl Viro res = sprintf(buffer, "running\n"); 503a9712bc1SAl Viro else if (nr < 0) 504a9712bc1SAl Viro res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 505a9712bc1SAl Viro else 506a9712bc1SAl Viro res = sprintf(buffer, 507ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 508ebcb6734SRoland McGrath nr, 509ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 510ebcb6734SRoland McGrath sp, pc); 511a9712bc1SAl Viro unlock_trace(task); 512a9712bc1SAl Viro return res; 513ebcb6734SRoland McGrath } 514ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 515ebcb6734SRoland McGrath 5161da177e4SLinus Torvalds /************************************************************************/ 5171da177e4SLinus Torvalds /* Here the fs part begins */ 5181da177e4SLinus Torvalds /************************************************************************/ 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* permission checks */ 521778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5221da177e4SLinus Torvalds { 523778c1144SEric W. Biederman struct task_struct *task; 524778c1144SEric W. Biederman int allowed = 0; 525df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 526df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 527df26c40eSEric W. Biederman * information. 528778c1144SEric W. Biederman */ 529778c1144SEric W. Biederman task = get_proc_task(inode); 530df26c40eSEric W. Biederman if (task) { 531006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 532778c1144SEric W. Biederman put_task_struct(task); 533df26c40eSEric W. Biederman } 534778c1144SEric W. Biederman return allowed; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5376b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 5386d76fa58SLinus Torvalds { 5396d76fa58SLinus Torvalds int error; 5406d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5416d76fa58SLinus Torvalds 5426d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5436d76fa58SLinus Torvalds return -EPERM; 5446d76fa58SLinus Torvalds 5456d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5461025774cSChristoph Hellwig if (error) 5476d76fa58SLinus Torvalds return error; 5481025774cSChristoph Hellwig 5491025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 5501025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 5511025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 5521025774cSChristoph Hellwig if (error) 5531025774cSChristoph Hellwig return error; 5541025774cSChristoph Hellwig } 5551025774cSChristoph Hellwig 5561025774cSChristoph Hellwig setattr_copy(inode, attr); 5571025774cSChristoph Hellwig mark_inode_dirty(inode); 5581025774cSChristoph Hellwig return 0; 5596d76fa58SLinus Torvalds } 5606d76fa58SLinus Torvalds 5610499680aSVasiliy Kulikov /* 5620499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1) 5630499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)? 5640499680aSVasiliy Kulikov */ 5650499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid, 5660499680aSVasiliy Kulikov struct task_struct *task, 5670499680aSVasiliy Kulikov int hide_pid_min) 5680499680aSVasiliy Kulikov { 5690499680aSVasiliy Kulikov if (pid->hide_pid < hide_pid_min) 5700499680aSVasiliy Kulikov return true; 5710499680aSVasiliy Kulikov if (in_group_p(pid->pid_gid)) 5720499680aSVasiliy Kulikov return true; 5730499680aSVasiliy Kulikov return ptrace_may_access(task, PTRACE_MODE_READ); 5740499680aSVasiliy Kulikov } 5750499680aSVasiliy Kulikov 5760499680aSVasiliy Kulikov 5770499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask) 5780499680aSVasiliy Kulikov { 5790499680aSVasiliy Kulikov struct pid_namespace *pid = inode->i_sb->s_fs_info; 5800499680aSVasiliy Kulikov struct task_struct *task; 5810499680aSVasiliy Kulikov bool has_perms; 5820499680aSVasiliy Kulikov 5830499680aSVasiliy Kulikov task = get_proc_task(inode); 584a2ef990aSXiaotian Feng if (!task) 585a2ef990aSXiaotian Feng return -ESRCH; 5860499680aSVasiliy Kulikov has_perms = has_pid_permissions(pid, task, 1); 5870499680aSVasiliy Kulikov put_task_struct(task); 5880499680aSVasiliy Kulikov 5890499680aSVasiliy Kulikov if (!has_perms) { 5900499680aSVasiliy Kulikov if (pid->hide_pid == 2) { 5910499680aSVasiliy Kulikov /* 5920499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open() 5930499680aSVasiliy Kulikov * consistent with each other. If a process 5940499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen 5950499680aSVasiliy Kulikov * in procfs at all. 5960499680aSVasiliy Kulikov */ 5970499680aSVasiliy Kulikov return -ENOENT; 5980499680aSVasiliy Kulikov } 5990499680aSVasiliy Kulikov 6000499680aSVasiliy Kulikov return -EPERM; 6010499680aSVasiliy Kulikov } 6020499680aSVasiliy Kulikov return generic_permission(inode, mask); 6030499680aSVasiliy Kulikov } 6040499680aSVasiliy Kulikov 6050499680aSVasiliy Kulikov 6060499680aSVasiliy Kulikov 607c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 6086d76fa58SLinus Torvalds .setattr = proc_setattr, 6096d76fa58SLinus Torvalds }; 6106d76fa58SLinus Torvalds 6111da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6141da177e4SLinus Torvalds size_t count, loff_t *ppos) 6151da177e4SLinus Torvalds { 6162fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6171da177e4SLinus Torvalds unsigned long page; 6181da177e4SLinus Torvalds ssize_t length; 61999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 62099f89551SEric W. Biederman 62199f89551SEric W. Biederman length = -ESRCH; 62299f89551SEric W. Biederman if (!task) 62399f89551SEric W. Biederman goto out_no_task; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6261da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 62799f89551SEric W. Biederman 62899f89551SEric W. Biederman length = -ENOMEM; 629e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 63099f89551SEric W. Biederman goto out; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds if (length >= 0) 6351da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6361da177e4SLinus Torvalds free_page(page); 63799f89551SEric W. Biederman out: 63899f89551SEric W. Biederman put_task_struct(task); 63999f89551SEric W. Biederman out_no_task: 6401da177e4SLinus Torvalds return length; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 64300977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6441da177e4SLinus Torvalds .read = proc_info_read, 64587df8424SArnd Bergmann .llseek = generic_file_llseek, 6461da177e4SLinus Torvalds }; 6471da177e4SLinus Torvalds 648be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 649be614086SEric W. Biederman { 650be614086SEric W. Biederman struct inode *inode = m->private; 651be614086SEric W. Biederman struct pid_namespace *ns; 652be614086SEric W. Biederman struct pid *pid; 653be614086SEric W. Biederman struct task_struct *task; 654be614086SEric W. Biederman int ret; 655be614086SEric W. Biederman 656be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 657be614086SEric W. Biederman pid = proc_pid(inode); 658be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 659be614086SEric W. Biederman if (!task) 660be614086SEric W. Biederman return -ESRCH; 661be614086SEric W. Biederman 662be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 663be614086SEric W. Biederman 664be614086SEric W. Biederman put_task_struct(task); 665be614086SEric W. Biederman return ret; 666be614086SEric W. Biederman } 667be614086SEric W. Biederman 668be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 669be614086SEric W. Biederman { 670c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 671be614086SEric W. Biederman } 672be614086SEric W. Biederman 673be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 674be614086SEric W. Biederman .open = proc_single_open, 675be614086SEric W. Biederman .read = seq_read, 676be614086SEric W. Biederman .llseek = seq_lseek, 677be614086SEric W. Biederman .release = single_release, 678be614086SEric W. Biederman }; 679be614086SEric W. Biederman 6801da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 6811da177e4SLinus Torvalds { 682e268337dSLinus Torvalds struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 683e268337dSLinus Torvalds struct mm_struct *mm; 684e268337dSLinus Torvalds 685e268337dSLinus Torvalds if (!task) 686e268337dSLinus Torvalds return -ESRCH; 687e268337dSLinus Torvalds 688e268337dSLinus Torvalds mm = mm_access(task, PTRACE_MODE_ATTACH); 689e268337dSLinus Torvalds put_task_struct(task); 690e268337dSLinus Torvalds 691e268337dSLinus Torvalds if (IS_ERR(mm)) 692e268337dSLinus Torvalds return PTR_ERR(mm); 693e268337dSLinus Torvalds 6946d08f2c7SOleg Nesterov if (mm) { 6956d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */ 6966d08f2c7SOleg Nesterov atomic_inc(&mm->mm_count); 6976d08f2c7SOleg Nesterov /* but do not pin its memory */ 6986d08f2c7SOleg Nesterov mmput(mm); 6996d08f2c7SOleg Nesterov } 7006d08f2c7SOleg Nesterov 7014a3956c7SKAMEZAWA Hiroyuki /* OK to pass negative loff_t, we can catch out-of-range */ 7024a3956c7SKAMEZAWA Hiroyuki file->f_mode |= FMODE_UNSIGNED_OFFSET; 703e268337dSLinus Torvalds file->private_data = mm; 704e268337dSLinus Torvalds 7051da177e4SLinus Torvalds return 0; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 708572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf, 709572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write) 7101da177e4SLinus Torvalds { 711e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 712572d34b9SOleg Nesterov unsigned long addr = *ppos; 713572d34b9SOleg Nesterov ssize_t copied; 7141da177e4SLinus Torvalds char *page; 7151da177e4SLinus Torvalds 716e268337dSLinus Torvalds if (!mm) 717e268337dSLinus Torvalds return 0; 7181da177e4SLinus Torvalds 719e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7201da177e4SLinus Torvalds if (!page) 721e268337dSLinus Torvalds return -ENOMEM; 7221da177e4SLinus Torvalds 723f7ca54f4SFrederik Deweerdt copied = 0; 7246d08f2c7SOleg Nesterov if (!atomic_inc_not_zero(&mm->mm_users)) 7256d08f2c7SOleg Nesterov goto free; 7266d08f2c7SOleg Nesterov 7271da177e4SLinus Torvalds while (count > 0) { 728572d34b9SOleg Nesterov int this_len = min_t(int, count, PAGE_SIZE); 7291da177e4SLinus Torvalds 730572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) { 7311da177e4SLinus Torvalds copied = -EFAULT; 7321da177e4SLinus Torvalds break; 7331da177e4SLinus Torvalds } 734572d34b9SOleg Nesterov 735572d34b9SOleg Nesterov this_len = access_remote_vm(mm, addr, page, this_len, write); 736572d34b9SOleg Nesterov if (!this_len) { 7371da177e4SLinus Torvalds if (!copied) 7381da177e4SLinus Torvalds copied = -EIO; 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds } 741572d34b9SOleg Nesterov 742572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) { 743572d34b9SOleg Nesterov copied = -EFAULT; 744572d34b9SOleg Nesterov break; 7451da177e4SLinus Torvalds } 746572d34b9SOleg Nesterov 747572d34b9SOleg Nesterov buf += this_len; 748572d34b9SOleg Nesterov addr += this_len; 749572d34b9SOleg Nesterov copied += this_len; 750572d34b9SOleg Nesterov count -= this_len; 751572d34b9SOleg Nesterov } 752572d34b9SOleg Nesterov *ppos = addr; 75330cd8903SKOSAKI Motohiro 7546d08f2c7SOleg Nesterov mmput(mm); 7556d08f2c7SOleg Nesterov free: 75630cd8903SKOSAKI Motohiro free_page((unsigned long) page); 7571da177e4SLinus Torvalds return copied; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 760572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf, 761572d34b9SOleg Nesterov size_t count, loff_t *ppos) 762572d34b9SOleg Nesterov { 763572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0); 764572d34b9SOleg Nesterov } 765572d34b9SOleg Nesterov 766572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf, 767572d34b9SOleg Nesterov size_t count, loff_t *ppos) 768572d34b9SOleg Nesterov { 769572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1); 770572d34b9SOleg Nesterov } 771572d34b9SOleg Nesterov 77285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 7731da177e4SLinus Torvalds { 7741da177e4SLinus Torvalds switch (orig) { 7751da177e4SLinus Torvalds case 0: 7761da177e4SLinus Torvalds file->f_pos = offset; 7771da177e4SLinus Torvalds break; 7781da177e4SLinus Torvalds case 1: 7791da177e4SLinus Torvalds file->f_pos += offset; 7801da177e4SLinus Torvalds break; 7811da177e4SLinus Torvalds default: 7821da177e4SLinus Torvalds return -EINVAL; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds force_successful_syscall_return(); 7851da177e4SLinus Torvalds return file->f_pos; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 788e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file) 789e268337dSLinus Torvalds { 790e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 79171879d3cSOleg Nesterov if (mm) 7926d08f2c7SOleg Nesterov mmdrop(mm); 793e268337dSLinus Torvalds return 0; 794e268337dSLinus Torvalds } 795e268337dSLinus Torvalds 79600977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 7971da177e4SLinus Torvalds .llseek = mem_lseek, 7981da177e4SLinus Torvalds .read = mem_read, 7991da177e4SLinus Torvalds .write = mem_write, 8001da177e4SLinus Torvalds .open = mem_open, 801e268337dSLinus Torvalds .release = mem_release, 8021da177e4SLinus Torvalds }; 8031da177e4SLinus Torvalds 804315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 805315e28c8SJames Pearson size_t count, loff_t *ppos) 806315e28c8SJames Pearson { 807315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 808315e28c8SJames Pearson char *page; 809315e28c8SJames Pearson unsigned long src = *ppos; 810315e28c8SJames Pearson int ret = -ESRCH; 811315e28c8SJames Pearson struct mm_struct *mm; 812315e28c8SJames Pearson 813315e28c8SJames Pearson if (!task) 814315e28c8SJames Pearson goto out_no_task; 815315e28c8SJames Pearson 816315e28c8SJames Pearson ret = -ENOMEM; 817315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 818315e28c8SJames Pearson if (!page) 819315e28c8SJames Pearson goto out; 820315e28c8SJames Pearson 821315e28c8SJames Pearson 822d6f64b89SAl Viro mm = mm_for_maps(task); 823d6f64b89SAl Viro ret = PTR_ERR(mm); 824d6f64b89SAl Viro if (!mm || IS_ERR(mm)) 825315e28c8SJames Pearson goto out_free; 826315e28c8SJames Pearson 827d6f64b89SAl Viro ret = 0; 828315e28c8SJames Pearson while (count > 0) { 829315e28c8SJames Pearson int this_len, retval, max_len; 830315e28c8SJames Pearson 831315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 832315e28c8SJames Pearson 833315e28c8SJames Pearson if (this_len <= 0) 834315e28c8SJames Pearson break; 835315e28c8SJames Pearson 836315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 837315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 838315e28c8SJames Pearson 839315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 840315e28c8SJames Pearson page, this_len, 0); 841315e28c8SJames Pearson 842315e28c8SJames Pearson if (retval <= 0) { 843315e28c8SJames Pearson ret = retval; 844315e28c8SJames Pearson break; 845315e28c8SJames Pearson } 846315e28c8SJames Pearson 847315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 848315e28c8SJames Pearson ret = -EFAULT; 849315e28c8SJames Pearson break; 850315e28c8SJames Pearson } 851315e28c8SJames Pearson 852315e28c8SJames Pearson ret += retval; 853315e28c8SJames Pearson src += retval; 854315e28c8SJames Pearson buf += retval; 855315e28c8SJames Pearson count -= retval; 856315e28c8SJames Pearson } 857315e28c8SJames Pearson *ppos = src; 858315e28c8SJames Pearson 859315e28c8SJames Pearson mmput(mm); 860315e28c8SJames Pearson out_free: 861315e28c8SJames Pearson free_page((unsigned long) page); 862315e28c8SJames Pearson out: 863315e28c8SJames Pearson put_task_struct(task); 864315e28c8SJames Pearson out_no_task: 865315e28c8SJames Pearson return ret; 866315e28c8SJames Pearson } 867315e28c8SJames Pearson 868315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 869315e28c8SJames Pearson .read = environ_read, 87087df8424SArnd Bergmann .llseek = generic_file_llseek, 871315e28c8SJames Pearson }; 872315e28c8SJames Pearson 8731da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 8741da177e4SLinus Torvalds size_t count, loff_t *ppos) 8751da177e4SLinus Torvalds { 8762fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8778578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 8781da177e4SLinus Torvalds size_t len; 87928b83c51SKOSAKI Motohiro int oom_adjust = OOM_DISABLE; 88028b83c51SKOSAKI Motohiro unsigned long flags; 8811da177e4SLinus Torvalds 88299f89551SEric W. Biederman if (!task) 88399f89551SEric W. Biederman return -ESRCH; 88428b83c51SKOSAKI Motohiro 88528b83c51SKOSAKI Motohiro if (lock_task_sighand(task, &flags)) { 88628b83c51SKOSAKI Motohiro oom_adjust = task->signal->oom_adj; 88728b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 88828b83c51SKOSAKI Motohiro } 88928b83c51SKOSAKI Motohiro 89099f89551SEric W. Biederman put_task_struct(task); 89199f89551SEric W. Biederman 8928578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 8930c28f287SAkinobu Mita 8940c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 8981da177e4SLinus Torvalds size_t count, loff_t *ppos) 8991da177e4SLinus Torvalds { 90099f89551SEric W. Biederman struct task_struct *task; 9015d863b89SKOSAKI Motohiro char buffer[PROC_NUMBUF]; 9020a8cb8e3SAlexey Dobriyan int oom_adjust; 90328b83c51SKOSAKI Motohiro unsigned long flags; 9045d863b89SKOSAKI Motohiro int err; 9051da177e4SLinus Torvalds 9068578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9078578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9088578cea7SEric W. Biederman count = sizeof(buffer) - 1; 909723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 910723548bfSDavid Rientjes err = -EFAULT; 911723548bfSDavid Rientjes goto out; 912723548bfSDavid Rientjes } 9135d863b89SKOSAKI Motohiro 9140a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_adjust); 9155d863b89SKOSAKI Motohiro if (err) 916723548bfSDavid Rientjes goto out; 9178ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 918723548bfSDavid Rientjes oom_adjust != OOM_DISABLE) { 919723548bfSDavid Rientjes err = -EINVAL; 920723548bfSDavid Rientjes goto out; 921723548bfSDavid Rientjes } 9225d863b89SKOSAKI Motohiro 9232fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 924723548bfSDavid Rientjes if (!task) { 925723548bfSDavid Rientjes err = -ESRCH; 926723548bfSDavid Rientjes goto out; 927723548bfSDavid Rientjes } 92828b83c51SKOSAKI Motohiro 9293d5992d2SYing Han task_lock(task); 9303d5992d2SYing Han if (!task->mm) { 931723548bfSDavid Rientjes err = -EINVAL; 932723548bfSDavid Rientjes goto err_task_lock; 9333d5992d2SYing Han } 9343d5992d2SYing Han 935d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 936d19d5476SDavid Rientjes err = -ESRCH; 937d19d5476SDavid Rientjes goto err_task_lock; 938d19d5476SDavid Rientjes } 939d19d5476SDavid Rientjes 940d19d5476SDavid Rientjes if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) { 941d19d5476SDavid Rientjes err = -EACCES; 942d19d5476SDavid Rientjes goto err_sighand; 943d19d5476SDavid Rientjes } 944d19d5476SDavid Rientjes 94551b1bd2aSDavid Rientjes /* 94651b1bd2aSDavid Rientjes * Warn that /proc/pid/oom_adj is deprecated, see 94751b1bd2aSDavid Rientjes * Documentation/feature-removal-schedule.txt. 94851b1bd2aSDavid Rientjes */ 949c2142704SLinus Torvalds printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 950be8f684dSDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 951be8f684dSDavid Rientjes task_pid_nr(task)); 95228b83c51SKOSAKI Motohiro task->signal->oom_adj = oom_adjust; 953a63d83f4SDavid Rientjes /* 954a63d83f4SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 955a63d83f4SDavid Rientjes * value is always attainable. 956a63d83f4SDavid Rientjes */ 957a63d83f4SDavid Rientjes if (task->signal->oom_adj == OOM_ADJUST_MAX) 958a63d83f4SDavid Rientjes task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX; 959a63d83f4SDavid Rientjes else 960a63d83f4SDavid Rientjes task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / 961a63d83f4SDavid Rientjes -OOM_DISABLE; 96243d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 963723548bfSDavid Rientjes err_sighand: 96428b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 965d19d5476SDavid Rientjes err_task_lock: 966d19d5476SDavid Rientjes task_unlock(task); 96799f89551SEric W. Biederman put_task_struct(task); 968723548bfSDavid Rientjes out: 969723548bfSDavid Rientjes return err < 0 ? err : count; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 97200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 9731da177e4SLinus Torvalds .read = oom_adjust_read, 9741da177e4SLinus Torvalds .write = oom_adjust_write, 97587df8424SArnd Bergmann .llseek = generic_file_llseek, 9761da177e4SLinus Torvalds }; 9771da177e4SLinus Torvalds 978a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 979a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 980a63d83f4SDavid Rientjes { 981a63d83f4SDavid Rientjes struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 982a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 983a63d83f4SDavid Rientjes int oom_score_adj = OOM_SCORE_ADJ_MIN; 984a63d83f4SDavid Rientjes unsigned long flags; 985a63d83f4SDavid Rientjes size_t len; 986a63d83f4SDavid Rientjes 987a63d83f4SDavid Rientjes if (!task) 988a63d83f4SDavid Rientjes return -ESRCH; 989a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 990a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 991a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 992a63d83f4SDavid Rientjes } 993a63d83f4SDavid Rientjes put_task_struct(task); 994a63d83f4SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj); 995a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 996a63d83f4SDavid Rientjes } 997a63d83f4SDavid Rientjes 998a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 999a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1000a63d83f4SDavid Rientjes { 1001a63d83f4SDavid Rientjes struct task_struct *task; 1002a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1003a63d83f4SDavid Rientjes unsigned long flags; 10040a8cb8e3SAlexey Dobriyan int oom_score_adj; 1005a63d83f4SDavid Rientjes int err; 1006a63d83f4SDavid Rientjes 1007a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 1008a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 1009a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 1010723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1011723548bfSDavid Rientjes err = -EFAULT; 1012723548bfSDavid Rientjes goto out; 1013723548bfSDavid Rientjes } 1014a63d83f4SDavid Rientjes 10150a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 1016a63d83f4SDavid Rientjes if (err) 1017723548bfSDavid Rientjes goto out; 1018a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1019723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 1020723548bfSDavid Rientjes err = -EINVAL; 1021723548bfSDavid Rientjes goto out; 1022723548bfSDavid Rientjes } 1023a63d83f4SDavid Rientjes 1024a63d83f4SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 1025723548bfSDavid Rientjes if (!task) { 1026723548bfSDavid Rientjes err = -ESRCH; 1027723548bfSDavid Rientjes goto out; 1028723548bfSDavid Rientjes } 1029a63d83f4SDavid Rientjes 10303d5992d2SYing Han task_lock(task); 10313d5992d2SYing Han if (!task->mm) { 1032723548bfSDavid Rientjes err = -EINVAL; 1033723548bfSDavid Rientjes goto err_task_lock; 10343d5992d2SYing Han } 1035d19d5476SDavid Rientjes 1036d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1037d19d5476SDavid Rientjes err = -ESRCH; 1038d19d5476SDavid Rientjes goto err_task_lock; 1039d19d5476SDavid Rientjes } 1040d19d5476SDavid Rientjes 1041dabb16f6SMandeep Singh Baines if (oom_score_adj < task->signal->oom_score_adj_min && 1042d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 1043d19d5476SDavid Rientjes err = -EACCES; 1044d19d5476SDavid Rientjes goto err_sighand; 1045d19d5476SDavid Rientjes } 1046d19d5476SDavid Rientjes 1047a63d83f4SDavid Rientjes task->signal->oom_score_adj = oom_score_adj; 1048dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1049dabb16f6SMandeep Singh Baines task->signal->oom_score_adj_min = oom_score_adj; 105043d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 1051a63d83f4SDavid Rientjes /* 1052a63d83f4SDavid Rientjes * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is 1053a63d83f4SDavid Rientjes * always attainable. 1054a63d83f4SDavid Rientjes */ 1055a63d83f4SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) 1056a63d83f4SDavid Rientjes task->signal->oom_adj = OOM_DISABLE; 1057a63d83f4SDavid Rientjes else 1058a63d83f4SDavid Rientjes task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / 1059a63d83f4SDavid Rientjes OOM_SCORE_ADJ_MAX; 1060723548bfSDavid Rientjes err_sighand: 1061a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1062d19d5476SDavid Rientjes err_task_lock: 1063d19d5476SDavid Rientjes task_unlock(task); 1064a63d83f4SDavid Rientjes put_task_struct(task); 1065723548bfSDavid Rientjes out: 1066723548bfSDavid Rientjes return err < 0 ? err : count; 1067a63d83f4SDavid Rientjes } 1068a63d83f4SDavid Rientjes 1069a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1070a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1071a63d83f4SDavid Rientjes .write = oom_score_adj_write, 10726038f373SArnd Bergmann .llseek = default_llseek, 1073a63d83f4SDavid Rientjes }; 1074a63d83f4SDavid Rientjes 10751da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10761da177e4SLinus Torvalds #define TMPBUFLEN 21 10771da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10781da177e4SLinus Torvalds size_t count, loff_t *ppos) 10791da177e4SLinus Torvalds { 10802fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 108199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10821da177e4SLinus Torvalds ssize_t length; 10831da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10841da177e4SLinus Torvalds 108599f89551SEric W. Biederman if (!task) 108699f89551SEric W. Biederman return -ESRCH; 10871da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10880c11b942SAl Viro audit_get_loginuid(task)); 108999f89551SEric W. Biederman put_task_struct(task); 10901da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10941da177e4SLinus Torvalds size_t count, loff_t *ppos) 10951da177e4SLinus Torvalds { 10962fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10971da177e4SLinus Torvalds char *page, *tmp; 10981da177e4SLinus Torvalds ssize_t length; 10991da177e4SLinus Torvalds uid_t loginuid; 11001da177e4SLinus Torvalds 11017dc52157SPaul E. McKenney rcu_read_lock(); 11027dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 11037dc52157SPaul E. McKenney rcu_read_unlock(); 11041da177e4SLinus Torvalds return -EPERM; 11057dc52157SPaul E. McKenney } 11067dc52157SPaul E. McKenney rcu_read_unlock(); 11071da177e4SLinus Torvalds 1108e0182909SAl Viro if (count >= PAGE_SIZE) 1109e0182909SAl Viro count = PAGE_SIZE - 1; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds if (*ppos != 0) { 11121da177e4SLinus Torvalds /* No partial writes. */ 11131da177e4SLinus Torvalds return -EINVAL; 11141da177e4SLinus Torvalds } 1115e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 11161da177e4SLinus Torvalds if (!page) 11171da177e4SLinus Torvalds return -ENOMEM; 11181da177e4SLinus Torvalds length = -EFAULT; 11191da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 11201da177e4SLinus Torvalds goto out_free_page; 11211da177e4SLinus Torvalds 1122e0182909SAl Viro page[count] = '\0'; 11231da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11241da177e4SLinus Torvalds if (tmp == page) { 11251da177e4SLinus Torvalds length = -EINVAL; 11261da177e4SLinus Torvalds goto out_free_page; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds } 11290a300be6SEric Paris length = audit_set_loginuid(loginuid); 11301da177e4SLinus Torvalds if (likely(length == 0)) 11311da177e4SLinus Torvalds length = count; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds out_free_page: 11341da177e4SLinus Torvalds free_page((unsigned long) page); 11351da177e4SLinus Torvalds return length; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 113800977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11391da177e4SLinus Torvalds .read = proc_loginuid_read, 11401da177e4SLinus Torvalds .write = proc_loginuid_write, 114187df8424SArnd Bergmann .llseek = generic_file_llseek, 11421da177e4SLinus Torvalds }; 11431e0bd755SEric Paris 11441e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11451e0bd755SEric Paris size_t count, loff_t *ppos) 11461e0bd755SEric Paris { 11471e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11481e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11491e0bd755SEric Paris ssize_t length; 11501e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11511e0bd755SEric Paris 11521e0bd755SEric Paris if (!task) 11531e0bd755SEric Paris return -ESRCH; 11541e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11551e0bd755SEric Paris audit_get_sessionid(task)); 11561e0bd755SEric Paris put_task_struct(task); 11571e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11581e0bd755SEric Paris } 11591e0bd755SEric Paris 11601e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11611e0bd755SEric Paris .read = proc_sessionid_read, 116287df8424SArnd Bergmann .llseek = generic_file_llseek, 11631e0bd755SEric Paris }; 11641da177e4SLinus Torvalds #endif 11651da177e4SLinus Torvalds 1166f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1167f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1168f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1169f4f154fdSAkinobu Mita { 1170f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1171f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1172f4f154fdSAkinobu Mita size_t len; 1173f4f154fdSAkinobu Mita int make_it_fail; 1174f4f154fdSAkinobu Mita 1175f4f154fdSAkinobu Mita if (!task) 1176f4f154fdSAkinobu Mita return -ESRCH; 1177f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1178f4f154fdSAkinobu Mita put_task_struct(task); 1179f4f154fdSAkinobu Mita 1180f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11810c28f287SAkinobu Mita 11820c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1183f4f154fdSAkinobu Mita } 1184f4f154fdSAkinobu Mita 1185f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1186f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1187f4f154fdSAkinobu Mita { 1188f4f154fdSAkinobu Mita struct task_struct *task; 1189f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1190f4f154fdSAkinobu Mita int make_it_fail; 1191f4f154fdSAkinobu Mita 1192f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1193f4f154fdSAkinobu Mita return -EPERM; 1194f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1195f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1196f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1197f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1198f4f154fdSAkinobu Mita return -EFAULT; 1199cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1200cba8aafeSVincent Li if (*end) 1201cba8aafeSVincent Li return -EINVAL; 1202f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1203f4f154fdSAkinobu Mita if (!task) 1204f4f154fdSAkinobu Mita return -ESRCH; 1205f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1206f4f154fdSAkinobu Mita put_task_struct(task); 1207cba8aafeSVincent Li 1208cba8aafeSVincent Li return count; 1209f4f154fdSAkinobu Mita } 1210f4f154fdSAkinobu Mita 121100977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1212f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1213f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 121487df8424SArnd Bergmann .llseek = generic_file_llseek, 1215f4f154fdSAkinobu Mita }; 1216f4f154fdSAkinobu Mita #endif 1217f4f154fdSAkinobu Mita 12189745512cSArjan van de Ven 121943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 122043ae34cbSIngo Molnar /* 122143ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 122243ae34cbSIngo Molnar */ 122343ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 122443ae34cbSIngo Molnar { 122543ae34cbSIngo Molnar struct inode *inode = m->private; 122643ae34cbSIngo Molnar struct task_struct *p; 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar p = get_proc_task(inode); 122943ae34cbSIngo Molnar if (!p) 123043ae34cbSIngo Molnar return -ESRCH; 123143ae34cbSIngo Molnar proc_sched_show_task(p, m); 123243ae34cbSIngo Molnar 123343ae34cbSIngo Molnar put_task_struct(p); 123443ae34cbSIngo Molnar 123543ae34cbSIngo Molnar return 0; 123643ae34cbSIngo Molnar } 123743ae34cbSIngo Molnar 123843ae34cbSIngo Molnar static ssize_t 123943ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 124043ae34cbSIngo Molnar size_t count, loff_t *offset) 124143ae34cbSIngo Molnar { 124243ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 124343ae34cbSIngo Molnar struct task_struct *p; 124443ae34cbSIngo Molnar 124543ae34cbSIngo Molnar p = get_proc_task(inode); 124643ae34cbSIngo Molnar if (!p) 124743ae34cbSIngo Molnar return -ESRCH; 124843ae34cbSIngo Molnar proc_sched_set_task(p); 124943ae34cbSIngo Molnar 125043ae34cbSIngo Molnar put_task_struct(p); 125143ae34cbSIngo Molnar 125243ae34cbSIngo Molnar return count; 125343ae34cbSIngo Molnar } 125443ae34cbSIngo Molnar 125543ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 125643ae34cbSIngo Molnar { 1257c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 125843ae34cbSIngo Molnar } 125943ae34cbSIngo Molnar 126043ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 126143ae34cbSIngo Molnar .open = sched_open, 126243ae34cbSIngo Molnar .read = seq_read, 126343ae34cbSIngo Molnar .write = sched_write, 126443ae34cbSIngo Molnar .llseek = seq_lseek, 12655ea473a1SAlexey Dobriyan .release = single_release, 126643ae34cbSIngo Molnar }; 126743ae34cbSIngo Molnar 126843ae34cbSIngo Molnar #endif 126943ae34cbSIngo Molnar 12705091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 12715091faa4SMike Galbraith /* 12725091faa4SMike Galbraith * Print out autogroup related information: 12735091faa4SMike Galbraith */ 12745091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 12755091faa4SMike Galbraith { 12765091faa4SMike Galbraith struct inode *inode = m->private; 12775091faa4SMike Galbraith struct task_struct *p; 12785091faa4SMike Galbraith 12795091faa4SMike Galbraith p = get_proc_task(inode); 12805091faa4SMike Galbraith if (!p) 12815091faa4SMike Galbraith return -ESRCH; 12825091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 12835091faa4SMike Galbraith 12845091faa4SMike Galbraith put_task_struct(p); 12855091faa4SMike Galbraith 12865091faa4SMike Galbraith return 0; 12875091faa4SMike Galbraith } 12885091faa4SMike Galbraith 12895091faa4SMike Galbraith static ssize_t 12905091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 12915091faa4SMike Galbraith size_t count, loff_t *offset) 12925091faa4SMike Galbraith { 12935091faa4SMike Galbraith struct inode *inode = file->f_path.dentry->d_inode; 12945091faa4SMike Galbraith struct task_struct *p; 12955091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 12960a8cb8e3SAlexey Dobriyan int nice; 12975091faa4SMike Galbraith int err; 12985091faa4SMike Galbraith 12995091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 13005091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 13015091faa4SMike Galbraith count = sizeof(buffer) - 1; 13025091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 13035091faa4SMike Galbraith return -EFAULT; 13045091faa4SMike Galbraith 13050a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 13060a8cb8e3SAlexey Dobriyan if (err < 0) 13070a8cb8e3SAlexey Dobriyan return err; 13085091faa4SMike Galbraith 13095091faa4SMike Galbraith p = get_proc_task(inode); 13105091faa4SMike Galbraith if (!p) 13115091faa4SMike Galbraith return -ESRCH; 13125091faa4SMike Galbraith 13132e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice); 13145091faa4SMike Galbraith if (err) 13155091faa4SMike Galbraith count = err; 13165091faa4SMike Galbraith 13175091faa4SMike Galbraith put_task_struct(p); 13185091faa4SMike Galbraith 13195091faa4SMike Galbraith return count; 13205091faa4SMike Galbraith } 13215091faa4SMike Galbraith 13225091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 13235091faa4SMike Galbraith { 13245091faa4SMike Galbraith int ret; 13255091faa4SMike Galbraith 13265091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 13275091faa4SMike Galbraith if (!ret) { 13285091faa4SMike Galbraith struct seq_file *m = filp->private_data; 13295091faa4SMike Galbraith 13305091faa4SMike Galbraith m->private = inode; 13315091faa4SMike Galbraith } 13325091faa4SMike Galbraith return ret; 13335091faa4SMike Galbraith } 13345091faa4SMike Galbraith 13355091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 13365091faa4SMike Galbraith .open = sched_autogroup_open, 13375091faa4SMike Galbraith .read = seq_read, 13385091faa4SMike Galbraith .write = sched_autogroup_write, 13395091faa4SMike Galbraith .llseek = seq_lseek, 13405091faa4SMike Galbraith .release = single_release, 13415091faa4SMike Galbraith }; 13425091faa4SMike Galbraith 13435091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 13445091faa4SMike Galbraith 13454614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 13464614a696Sjohn stultz size_t count, loff_t *offset) 13474614a696Sjohn stultz { 13484614a696Sjohn stultz struct inode *inode = file->f_path.dentry->d_inode; 13494614a696Sjohn stultz struct task_struct *p; 13504614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 13514614a696Sjohn stultz 13524614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 13534614a696Sjohn stultz if (count > sizeof(buffer) - 1) 13544614a696Sjohn stultz count = sizeof(buffer) - 1; 13554614a696Sjohn stultz if (copy_from_user(buffer, buf, count)) 13564614a696Sjohn stultz return -EFAULT; 13574614a696Sjohn stultz 13584614a696Sjohn stultz p = get_proc_task(inode); 13594614a696Sjohn stultz if (!p) 13604614a696Sjohn stultz return -ESRCH; 13614614a696Sjohn stultz 13624614a696Sjohn stultz if (same_thread_group(current, p)) 13634614a696Sjohn stultz set_task_comm(p, buffer); 13644614a696Sjohn stultz else 13654614a696Sjohn stultz count = -EINVAL; 13664614a696Sjohn stultz 13674614a696Sjohn stultz put_task_struct(p); 13684614a696Sjohn stultz 13694614a696Sjohn stultz return count; 13704614a696Sjohn stultz } 13714614a696Sjohn stultz 13724614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 13734614a696Sjohn stultz { 13744614a696Sjohn stultz struct inode *inode = m->private; 13754614a696Sjohn stultz struct task_struct *p; 13764614a696Sjohn stultz 13774614a696Sjohn stultz p = get_proc_task(inode); 13784614a696Sjohn stultz if (!p) 13794614a696Sjohn stultz return -ESRCH; 13804614a696Sjohn stultz 13814614a696Sjohn stultz task_lock(p); 13824614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 13834614a696Sjohn stultz task_unlock(p); 13844614a696Sjohn stultz 13854614a696Sjohn stultz put_task_struct(p); 13864614a696Sjohn stultz 13874614a696Sjohn stultz return 0; 13884614a696Sjohn stultz } 13894614a696Sjohn stultz 13904614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 13914614a696Sjohn stultz { 1392c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 13934614a696Sjohn stultz } 13944614a696Sjohn stultz 13954614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 13964614a696Sjohn stultz .open = comm_open, 13974614a696Sjohn stultz .read = seq_read, 13984614a696Sjohn stultz .write = comm_write, 13994614a696Sjohn stultz .llseek = seq_lseek, 14004614a696Sjohn stultz .release = single_release, 14014614a696Sjohn stultz }; 14024614a696Sjohn stultz 14037773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1404925d1c40SMatt Helsley { 1405925d1c40SMatt Helsley struct task_struct *task; 1406925d1c40SMatt Helsley struct mm_struct *mm; 1407925d1c40SMatt Helsley struct file *exe_file; 1408925d1c40SMatt Helsley 14097773fbc5SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 1410925d1c40SMatt Helsley if (!task) 1411925d1c40SMatt Helsley return -ENOENT; 1412925d1c40SMatt Helsley mm = get_task_mm(task); 1413925d1c40SMatt Helsley put_task_struct(task); 1414925d1c40SMatt Helsley if (!mm) 1415925d1c40SMatt Helsley return -ENOENT; 1416925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1417925d1c40SMatt Helsley mmput(mm); 1418925d1c40SMatt Helsley if (exe_file) { 1419925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1420925d1c40SMatt Helsley path_get(&exe_file->f_path); 1421925d1c40SMatt Helsley fput(exe_file); 1422925d1c40SMatt Helsley return 0; 1423925d1c40SMatt Helsley } else 1424925d1c40SMatt Helsley return -ENOENT; 1425925d1c40SMatt Helsley } 1426925d1c40SMatt Helsley 1427008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14301da177e4SLinus Torvalds int error = -EACCES; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 14331d957f9bSJan Blunck path_put(&nd->path); 14341da177e4SLinus Torvalds 1435778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1436778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14371da177e4SLinus Torvalds goto out; 14381da177e4SLinus Torvalds 14397773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path); 14401da177e4SLinus Torvalds out: 1441008b150aSAl Viro return ERR_PTR(error); 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 14443dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 14451da177e4SLinus Torvalds { 1446e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 14473dcd25f3SJan Blunck char *pathname; 14481da177e4SLinus Torvalds int len; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds if (!tmp) 14511da177e4SLinus Torvalds return -ENOMEM; 14521da177e4SLinus Torvalds 14537b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 14543dcd25f3SJan Blunck len = PTR_ERR(pathname); 14553dcd25f3SJan Blunck if (IS_ERR(pathname)) 14561da177e4SLinus Torvalds goto out; 14573dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds if (len > buflen) 14601da177e4SLinus Torvalds len = buflen; 14613dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 14621da177e4SLinus Torvalds len = -EFAULT; 14631da177e4SLinus Torvalds out: 14641da177e4SLinus Torvalds free_page((unsigned long)tmp); 14651da177e4SLinus Torvalds return len; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 14691da177e4SLinus Torvalds { 14701da177e4SLinus Torvalds int error = -EACCES; 14711da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14723dcd25f3SJan Blunck struct path path; 14731da177e4SLinus Torvalds 1474778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1475778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14761da177e4SLinus Torvalds goto out; 14771da177e4SLinus Torvalds 14787773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path); 14791da177e4SLinus Torvalds if (error) 14801da177e4SLinus Torvalds goto out; 14811da177e4SLinus Torvalds 14823dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14833dcd25f3SJan Blunck path_put(&path); 14841da177e4SLinus Torvalds out: 14851da177e4SLinus Torvalds return error; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 1488c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 14891da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14906d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14916d76fa58SLinus Torvalds .setattr = proc_setattr, 14921da177e4SLinus Torvalds }; 14931da177e4SLinus Torvalds 149428a6d671SEric W. Biederman 149528a6d671SEric W. Biederman /* building an inode */ 149628a6d671SEric W. Biederman 149728a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 149828a6d671SEric W. Biederman { 149928a6d671SEric W. Biederman int dumpable = 0; 150028a6d671SEric W. Biederman struct mm_struct *mm; 150128a6d671SEric W. Biederman 150228a6d671SEric W. Biederman task_lock(task); 150328a6d671SEric W. Biederman mm = task->mm; 150428a6d671SEric W. Biederman if (mm) 15056c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 150628a6d671SEric W. Biederman task_unlock(task); 150728a6d671SEric W. Biederman if(dumpable == 1) 150828a6d671SEric W. Biederman return 1; 150928a6d671SEric W. Biederman return 0; 151028a6d671SEric W. Biederman } 151128a6d671SEric W. Biederman 15126b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 151328a6d671SEric W. Biederman { 151428a6d671SEric W. Biederman struct inode * inode; 151528a6d671SEric W. Biederman struct proc_inode *ei; 1516c69e8d9cSDavid Howells const struct cred *cred; 151728a6d671SEric W. Biederman 151828a6d671SEric W. Biederman /* We need a new inode */ 151928a6d671SEric W. Biederman 152028a6d671SEric W. Biederman inode = new_inode(sb); 152128a6d671SEric W. Biederman if (!inode) 152228a6d671SEric W. Biederman goto out; 152328a6d671SEric W. Biederman 152428a6d671SEric W. Biederman /* Common stuff */ 152528a6d671SEric W. Biederman ei = PROC_I(inode); 152685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 152728a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 152828a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 152928a6d671SEric W. Biederman 153028a6d671SEric W. Biederman /* 153128a6d671SEric W. Biederman * grab the reference to task. 153228a6d671SEric W. Biederman */ 15331a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 153428a6d671SEric W. Biederman if (!ei->pid) 153528a6d671SEric W. Biederman goto out_unlock; 153628a6d671SEric W. Biederman 153728a6d671SEric W. Biederman if (task_dumpable(task)) { 1538c69e8d9cSDavid Howells rcu_read_lock(); 1539c69e8d9cSDavid Howells cred = __task_cred(task); 1540c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1541c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1542c69e8d9cSDavid Howells rcu_read_unlock(); 154328a6d671SEric W. Biederman } 154428a6d671SEric W. Biederman security_task_to_inode(task, inode); 154528a6d671SEric W. Biederman 154628a6d671SEric W. Biederman out: 154728a6d671SEric W. Biederman return inode; 154828a6d671SEric W. Biederman 154928a6d671SEric W. Biederman out_unlock: 155028a6d671SEric W. Biederman iput(inode); 155128a6d671SEric W. Biederman return NULL; 155228a6d671SEric W. Biederman } 155328a6d671SEric W. Biederman 15546b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 155528a6d671SEric W. Biederman { 155628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 155728a6d671SEric W. Biederman struct task_struct *task; 1558c69e8d9cSDavid Howells const struct cred *cred; 15590499680aSVasiliy Kulikov struct pid_namespace *pid = dentry->d_sb->s_fs_info; 1560c69e8d9cSDavid Howells 156128a6d671SEric W. Biederman generic_fillattr(inode, stat); 156228a6d671SEric W. Biederman 156328a6d671SEric W. Biederman rcu_read_lock(); 156428a6d671SEric W. Biederman stat->uid = 0; 156528a6d671SEric W. Biederman stat->gid = 0; 156628a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 156728a6d671SEric W. Biederman if (task) { 15680499680aSVasiliy Kulikov if (!has_pid_permissions(pid, task, 2)) { 15690499680aSVasiliy Kulikov rcu_read_unlock(); 15700499680aSVasiliy Kulikov /* 15710499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists, 15720499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir(). 15730499680aSVasiliy Kulikov */ 15740499680aSVasiliy Kulikov return -ENOENT; 15750499680aSVasiliy Kulikov } 157628a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 157728a6d671SEric W. Biederman task_dumpable(task)) { 1578c69e8d9cSDavid Howells cred = __task_cred(task); 1579c69e8d9cSDavid Howells stat->uid = cred->euid; 1580c69e8d9cSDavid Howells stat->gid = cred->egid; 158128a6d671SEric W. Biederman } 158228a6d671SEric W. Biederman } 158328a6d671SEric W. Biederman rcu_read_unlock(); 158428a6d671SEric W. Biederman return 0; 158528a6d671SEric W. Biederman } 158628a6d671SEric W. Biederman 158728a6d671SEric W. Biederman /* dentry stuff */ 158828a6d671SEric W. Biederman 158928a6d671SEric W. Biederman /* 159028a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 159128a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 159228a6d671SEric W. Biederman * due to the way we treat inodes. 159328a6d671SEric W. Biederman * 159428a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 159528a6d671SEric W. Biederman * performed a setuid(), etc. 159628a6d671SEric W. Biederman * 159728a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 159828a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 159928a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 160028a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 160128a6d671SEric W. Biederman * made this apply to all per process world readable and executable 160228a6d671SEric W. Biederman * directories. 160328a6d671SEric W. Biederman */ 16046b4e306aSEric W. Biederman int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 160528a6d671SEric W. Biederman { 160634286d66SNick Piggin struct inode *inode; 160734286d66SNick Piggin struct task_struct *task; 1608c69e8d9cSDavid Howells const struct cred *cred; 1609c69e8d9cSDavid Howells 161034286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 161134286d66SNick Piggin return -ECHILD; 161234286d66SNick Piggin 161334286d66SNick Piggin inode = dentry->d_inode; 161434286d66SNick Piggin task = get_proc_task(inode); 161534286d66SNick Piggin 161628a6d671SEric W. Biederman if (task) { 161728a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 161828a6d671SEric W. Biederman task_dumpable(task)) { 1619c69e8d9cSDavid Howells rcu_read_lock(); 1620c69e8d9cSDavid Howells cred = __task_cred(task); 1621c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1622c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1623c69e8d9cSDavid Howells rcu_read_unlock(); 162428a6d671SEric W. Biederman } else { 162528a6d671SEric W. Biederman inode->i_uid = 0; 162628a6d671SEric W. Biederman inode->i_gid = 0; 162728a6d671SEric W. Biederman } 162828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 162928a6d671SEric W. Biederman security_task_to_inode(task, inode); 163028a6d671SEric W. Biederman put_task_struct(task); 163128a6d671SEric W. Biederman return 1; 163228a6d671SEric W. Biederman } 163328a6d671SEric W. Biederman d_drop(dentry); 163428a6d671SEric W. Biederman return 0; 163528a6d671SEric W. Biederman } 163628a6d671SEric W. Biederman 1637fe15ce44SNick Piggin static int pid_delete_dentry(const struct dentry * dentry) 163828a6d671SEric W. Biederman { 163928a6d671SEric W. Biederman /* Is the task we represent dead? 164028a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 164128a6d671SEric W. Biederman * kill it immediately. 164228a6d671SEric W. Biederman */ 164328a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 164428a6d671SEric W. Biederman } 164528a6d671SEric W. Biederman 16466b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 164728a6d671SEric W. Biederman { 164828a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 164928a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 165028a6d671SEric W. Biederman }; 165128a6d671SEric W. Biederman 165228a6d671SEric W. Biederman /* Lookups */ 165328a6d671SEric W. Biederman 16541c0d04c9SEric W. Biederman /* 16551c0d04c9SEric W. Biederman * Fill a directory entry. 16561c0d04c9SEric W. Biederman * 16571c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 16581c0d04c9SEric W. Biederman * file type from dcache entry. 16591c0d04c9SEric W. Biederman * 16601c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 16611c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 16621c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 16631c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 16641c0d04c9SEric W. Biederman * by stat. 16651c0d04c9SEric W. Biederman */ 16666b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 16676b4e306aSEric W. Biederman const char *name, int len, 1668c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 166961a28784SEric W. Biederman { 16702fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 167161a28784SEric W. Biederman struct inode *inode; 167261a28784SEric W. Biederman struct qstr qname; 167361a28784SEric W. Biederman ino_t ino = 0; 167461a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 167561a28784SEric W. Biederman 167661a28784SEric W. Biederman qname.name = name; 167761a28784SEric W. Biederman qname.len = len; 167861a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 167961a28784SEric W. Biederman 168061a28784SEric W. Biederman child = d_lookup(dir, &qname); 168161a28784SEric W. Biederman if (!child) { 168261a28784SEric W. Biederman struct dentry *new; 168361a28784SEric W. Biederman new = d_alloc(dir, &qname); 168461a28784SEric W. Biederman if (new) { 168561a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 168661a28784SEric W. Biederman if (child) 168761a28784SEric W. Biederman dput(new); 168861a28784SEric W. Biederman else 168961a28784SEric W. Biederman child = new; 169061a28784SEric W. Biederman } 169161a28784SEric W. Biederman } 169261a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 169361a28784SEric W. Biederman goto end_instantiate; 169461a28784SEric W. Biederman inode = child->d_inode; 169561a28784SEric W. Biederman if (inode) { 169661a28784SEric W. Biederman ino = inode->i_ino; 169761a28784SEric W. Biederman type = inode->i_mode >> 12; 169861a28784SEric W. Biederman } 169961a28784SEric W. Biederman dput(child); 170061a28784SEric W. Biederman end_instantiate: 170161a28784SEric W. Biederman if (!ino) 170261a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 170361a28784SEric W. Biederman if (!ino) 170461a28784SEric W. Biederman ino = 1; 170561a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 170661a28784SEric W. Biederman } 170761a28784SEric W. Biederman 170828a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 170928a6d671SEric W. Biederman { 171028a6d671SEric W. Biederman const char *name = dentry->d_name.name; 171128a6d671SEric W. Biederman int len = dentry->d_name.len; 171228a6d671SEric W. Biederman unsigned n = 0; 171328a6d671SEric W. Biederman 171428a6d671SEric W. Biederman if (len > 1 && *name == '0') 171528a6d671SEric W. Biederman goto out; 171628a6d671SEric W. Biederman while (len-- > 0) { 171728a6d671SEric W. Biederman unsigned c = *name++ - '0'; 171828a6d671SEric W. Biederman if (c > 9) 171928a6d671SEric W. Biederman goto out; 172028a6d671SEric W. Biederman if (n >= (~0U-9)/10) 172128a6d671SEric W. Biederman goto out; 172228a6d671SEric W. Biederman n *= 10; 172328a6d671SEric W. Biederman n += c; 172428a6d671SEric W. Biederman } 172528a6d671SEric W. Biederman return n; 172628a6d671SEric W. Biederman out: 172728a6d671SEric W. Biederman return ~0U; 172828a6d671SEric W. Biederman } 172928a6d671SEric W. Biederman 173027932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 173127932742SMiklos Szeredi 17323dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 173328a6d671SEric W. Biederman { 17345e442a49SLinus Torvalds struct task_struct *task = get_proc_task(inode); 17355e442a49SLinus Torvalds struct files_struct *files = NULL; 173628a6d671SEric W. Biederman struct file *file; 173728a6d671SEric W. Biederman int fd = proc_fd(inode); 173828a6d671SEric W. Biederman 17395e442a49SLinus Torvalds if (task) { 174028a6d671SEric W. Biederman files = get_files_struct(task); 17415e442a49SLinus Torvalds put_task_struct(task); 17425e442a49SLinus Torvalds } 17435e442a49SLinus Torvalds if (files) { 174428a6d671SEric W. Biederman /* 174528a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 174628a6d671SEric W. Biederman * hold ->file_lock. 174728a6d671SEric W. Biederman */ 174828a6d671SEric W. Biederman spin_lock(&files->file_lock); 174928a6d671SEric W. Biederman file = fcheck_files(files, fd); 175028a6d671SEric W. Biederman if (file) { 17511117f72eSLinus Torvalds unsigned int f_flags; 17521117f72eSLinus Torvalds struct fdtable *fdt; 17531117f72eSLinus Torvalds 17541117f72eSLinus Torvalds fdt = files_fdtable(files); 17551117f72eSLinus Torvalds f_flags = file->f_flags & ~O_CLOEXEC; 17561dce27c5SDavid Howells if (close_on_exec(fd, fdt)) 17571117f72eSLinus Torvalds f_flags |= O_CLOEXEC; 17581117f72eSLinus Torvalds 17593dcd25f3SJan Blunck if (path) { 17603dcd25f3SJan Blunck *path = file->f_path; 17613dcd25f3SJan Blunck path_get(&file->f_path); 17623dcd25f3SJan Blunck } 176327932742SMiklos Szeredi if (info) 176427932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 176527932742SMiklos Szeredi "pos:\t%lli\n" 176627932742SMiklos Szeredi "flags:\t0%o\n", 176727932742SMiklos Szeredi (long long) file->f_pos, 17681117f72eSLinus Torvalds f_flags); 176928a6d671SEric W. Biederman spin_unlock(&files->file_lock); 177028a6d671SEric W. Biederman put_files_struct(files); 17715e442a49SLinus Torvalds return 0; 17725e442a49SLinus Torvalds } 17735e442a49SLinus Torvalds spin_unlock(&files->file_lock); 17745e442a49SLinus Torvalds put_files_struct(files); 17755e442a49SLinus Torvalds } 17765e442a49SLinus Torvalds return -ENOENT; 177728a6d671SEric W. Biederman } 177828a6d671SEric W. Biederman 17797773fbc5SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path) 178027932742SMiklos Szeredi { 17817773fbc5SCyrill Gorcunov return proc_fd_info(dentry->d_inode, path, NULL); 178227932742SMiklos Szeredi } 178327932742SMiklos Szeredi 178428a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 178528a6d671SEric W. Biederman { 178634286d66SNick Piggin struct inode *inode; 178734286d66SNick Piggin struct task_struct *task; 178834286d66SNick Piggin int fd; 178928a6d671SEric W. Biederman struct files_struct *files; 1790c69e8d9cSDavid Howells const struct cred *cred; 179128a6d671SEric W. Biederman 179234286d66SNick Piggin if (nd && nd->flags & LOOKUP_RCU) 179334286d66SNick Piggin return -ECHILD; 179434286d66SNick Piggin 179534286d66SNick Piggin inode = dentry->d_inode; 179634286d66SNick Piggin task = get_proc_task(inode); 179734286d66SNick Piggin fd = proc_fd(inode); 179834286d66SNick Piggin 179928a6d671SEric W. Biederman if (task) { 180028a6d671SEric W. Biederman files = get_files_struct(task); 180128a6d671SEric W. Biederman if (files) { 180228a6d671SEric W. Biederman rcu_read_lock(); 180328a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 180428a6d671SEric W. Biederman rcu_read_unlock(); 180528a6d671SEric W. Biederman put_files_struct(files); 180628a6d671SEric W. Biederman if (task_dumpable(task)) { 1807c69e8d9cSDavid Howells rcu_read_lock(); 1808c69e8d9cSDavid Howells cred = __task_cred(task); 1809c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1810c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1811c69e8d9cSDavid Howells rcu_read_unlock(); 181228a6d671SEric W. Biederman } else { 181328a6d671SEric W. Biederman inode->i_uid = 0; 181428a6d671SEric W. Biederman inode->i_gid = 0; 181528a6d671SEric W. Biederman } 181628a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 181728a6d671SEric W. Biederman security_task_to_inode(task, inode); 181828a6d671SEric W. Biederman put_task_struct(task); 181928a6d671SEric W. Biederman return 1; 182028a6d671SEric W. Biederman } 182128a6d671SEric W. Biederman rcu_read_unlock(); 182228a6d671SEric W. Biederman put_files_struct(files); 182328a6d671SEric W. Biederman } 182428a6d671SEric W. Biederman put_task_struct(task); 182528a6d671SEric W. Biederman } 182628a6d671SEric W. Biederman d_drop(dentry); 182728a6d671SEric W. Biederman return 0; 182828a6d671SEric W. Biederman } 182928a6d671SEric W. Biederman 1830d72f71ebSAl Viro static const struct dentry_operations tid_fd_dentry_operations = 183128a6d671SEric W. Biederman { 183228a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 183328a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 183428a6d671SEric W. Biederman }; 183528a6d671SEric W. Biederman 1836444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1837c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 183828a6d671SEric W. Biederman { 1839c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 184028a6d671SEric W. Biederman struct file *file; 184128a6d671SEric W. Biederman struct files_struct *files; 184228a6d671SEric W. Biederman struct inode *inode; 184328a6d671SEric W. Biederman struct proc_inode *ei; 1844444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 184528a6d671SEric W. Biederman 184661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 184728a6d671SEric W. Biederman if (!inode) 184828a6d671SEric W. Biederman goto out; 184928a6d671SEric W. Biederman ei = PROC_I(inode); 185028a6d671SEric W. Biederman ei->fd = fd; 185128a6d671SEric W. Biederman files = get_files_struct(task); 185228a6d671SEric W. Biederman if (!files) 1853444ceed8SEric W. Biederman goto out_iput; 185428a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 185528a6d671SEric W. Biederman 185628a6d671SEric W. Biederman /* 185728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 185828a6d671SEric W. Biederman * hold ->file_lock. 185928a6d671SEric W. Biederman */ 186028a6d671SEric W. Biederman spin_lock(&files->file_lock); 186128a6d671SEric W. Biederman file = fcheck_files(files, fd); 186228a6d671SEric W. Biederman if (!file) 1863444ceed8SEric W. Biederman goto out_unlock; 1864aeb5d727SAl Viro if (file->f_mode & FMODE_READ) 186528a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 1866aeb5d727SAl Viro if (file->f_mode & FMODE_WRITE) 186728a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 186828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 186928a6d671SEric W. Biederman put_files_struct(files); 1870444ceed8SEric W. Biederman 18715e442a49SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 187228a6d671SEric W. Biederman inode->i_size = 64; 187328a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 1874fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 187528a6d671SEric W. Biederman d_add(dentry, inode); 187628a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 187728a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1878444ceed8SEric W. Biederman error = NULL; 1879444ceed8SEric W. Biederman 1880444ceed8SEric W. Biederman out: 1881444ceed8SEric W. Biederman return error; 1882444ceed8SEric W. Biederman out_unlock: 1883444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1884444ceed8SEric W. Biederman put_files_struct(files); 1885444ceed8SEric W. Biederman out_iput: 1886444ceed8SEric W. Biederman iput(inode); 1887444ceed8SEric W. Biederman goto out; 1888444ceed8SEric W. Biederman } 1889444ceed8SEric W. Biederman 189027932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 189127932742SMiklos Szeredi struct dentry *dentry, 189227932742SMiklos Szeredi instantiate_t instantiate) 1893444ceed8SEric W. Biederman { 1894444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1895444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1896444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1897444ceed8SEric W. Biederman 1898444ceed8SEric W. Biederman if (!task) 1899444ceed8SEric W. Biederman goto out_no_task; 1900444ceed8SEric W. Biederman if (fd == ~0U) 1901444ceed8SEric W. Biederman goto out; 1902444ceed8SEric W. Biederman 190327932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 190428a6d671SEric W. Biederman out: 190528a6d671SEric W. Biederman put_task_struct(task); 190628a6d671SEric W. Biederman out_no_task: 190728a6d671SEric W. Biederman return result; 190828a6d671SEric W. Biederman } 190928a6d671SEric W. Biederman 191027932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 191127932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 19121da177e4SLinus Torvalds { 19132fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 19145634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 191599f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1916457c2510SPavel Emelyanov unsigned int fd, ino; 19171da177e4SLinus Torvalds int retval; 19181da177e4SLinus Torvalds struct files_struct * files; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds retval = -ENOENT; 192199f89551SEric W. Biederman if (!p) 192299f89551SEric W. Biederman goto out_no_task; 19231da177e4SLinus Torvalds retval = 0; 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds fd = filp->f_pos; 19261da177e4SLinus Torvalds switch (fd) { 19271da177e4SLinus Torvalds case 0: 19281da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 19295e442a49SLinus Torvalds goto out; 19301da177e4SLinus Torvalds filp->f_pos++; 19311da177e4SLinus Torvalds case 1: 19325634708bSEric W. Biederman ino = parent_ino(dentry); 19331da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 19345e442a49SLinus Torvalds goto out; 19351da177e4SLinus Torvalds filp->f_pos++; 19361da177e4SLinus Torvalds default: 19371da177e4SLinus Torvalds files = get_files_struct(p); 19381da177e4SLinus Torvalds if (!files) 19395e442a49SLinus Torvalds goto out; 1940b835996fSDipankar Sarma rcu_read_lock(); 19411da177e4SLinus Torvalds for (fd = filp->f_pos-2; 19429b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 19431da177e4SLinus Torvalds fd++, filp->f_pos++) { 194427932742SMiklos Szeredi char name[PROC_NUMBUF]; 194527932742SMiklos Szeredi int len; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 19481da177e4SLinus Torvalds continue; 1949b835996fSDipankar Sarma rcu_read_unlock(); 19501da177e4SLinus Torvalds 195127932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 195227932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 195327932742SMiklos Szeredi name, len, instantiate, 195427932742SMiklos Szeredi p, &fd) < 0) { 1955b835996fSDipankar Sarma rcu_read_lock(); 19561da177e4SLinus Torvalds break; 19571da177e4SLinus Torvalds } 1958b835996fSDipankar Sarma rcu_read_lock(); 19591da177e4SLinus Torvalds } 1960b835996fSDipankar Sarma rcu_read_unlock(); 19611da177e4SLinus Torvalds put_files_struct(files); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds out: 196499f89551SEric W. Biederman put_task_struct(p); 196599f89551SEric W. Biederman out_no_task: 19661da177e4SLinus Torvalds return retval; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 196927932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 197027932742SMiklos Szeredi struct nameidata *nd) 197127932742SMiklos Szeredi { 197227932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 197327932742SMiklos Szeredi } 197427932742SMiklos Szeredi 197527932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 197627932742SMiklos Szeredi { 197727932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 197827932742SMiklos Szeredi } 197927932742SMiklos Szeredi 198027932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 198127932742SMiklos Szeredi size_t len, loff_t *ppos) 198227932742SMiklos Szeredi { 198327932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 19843dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 198527932742SMiklos Szeredi if (!err) 198627932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 198727932742SMiklos Szeredi return err; 198827932742SMiklos Szeredi } 198927932742SMiklos Szeredi 199027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 199127932742SMiklos Szeredi .open = nonseekable_open, 199227932742SMiklos Szeredi .read = proc_fdinfo_read, 19936038f373SArnd Bergmann .llseek = no_llseek, 199427932742SMiklos Szeredi }; 199527932742SMiklos Szeredi 199600977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 19971da177e4SLinus Torvalds .read = generic_read_dir, 19981da177e4SLinus Torvalds .readdir = proc_readfd, 19996038f373SArnd Bergmann .llseek = default_llseek, 20001da177e4SLinus Torvalds }; 20011da177e4SLinus Torvalds 2002640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2003640708a2SPavel Emelyanov 2004640708a2SPavel Emelyanov /* 2005640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs 2006640708a2SPavel Emelyanov * which represent vma start and end addresses. 2007640708a2SPavel Emelyanov */ 2008640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry, 2009640708a2SPavel Emelyanov unsigned long *start, unsigned long *end) 2010640708a2SPavel Emelyanov { 2011640708a2SPavel Emelyanov if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) 2012640708a2SPavel Emelyanov return -EINVAL; 2013640708a2SPavel Emelyanov 2014640708a2SPavel Emelyanov return 0; 2015640708a2SPavel Emelyanov } 2016640708a2SPavel Emelyanov 2017640708a2SPavel Emelyanov static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd) 2018640708a2SPavel Emelyanov { 2019640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 2020640708a2SPavel Emelyanov bool exact_vma_exists = false; 2021640708a2SPavel Emelyanov struct mm_struct *mm = NULL; 2022640708a2SPavel Emelyanov struct task_struct *task; 2023640708a2SPavel Emelyanov const struct cred *cred; 2024640708a2SPavel Emelyanov struct inode *inode; 2025640708a2SPavel Emelyanov int status = 0; 2026640708a2SPavel Emelyanov 2027640708a2SPavel Emelyanov if (nd && nd->flags & LOOKUP_RCU) 2028640708a2SPavel Emelyanov return -ECHILD; 2029640708a2SPavel Emelyanov 2030640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) { 2031640708a2SPavel Emelyanov status = -EACCES; 2032640708a2SPavel Emelyanov goto out_notask; 2033640708a2SPavel Emelyanov } 2034640708a2SPavel Emelyanov 2035640708a2SPavel Emelyanov inode = dentry->d_inode; 2036640708a2SPavel Emelyanov task = get_proc_task(inode); 2037640708a2SPavel Emelyanov if (!task) 2038640708a2SPavel Emelyanov goto out_notask; 2039640708a2SPavel Emelyanov 2040640708a2SPavel Emelyanov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 2041640708a2SPavel Emelyanov goto out; 2042640708a2SPavel Emelyanov 2043640708a2SPavel Emelyanov mm = get_task_mm(task); 2044640708a2SPavel Emelyanov if (!mm) 2045640708a2SPavel Emelyanov goto out; 2046640708a2SPavel Emelyanov 2047640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 2048640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 2049640708a2SPavel Emelyanov exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); 2050640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 2051640708a2SPavel Emelyanov } 2052640708a2SPavel Emelyanov 2053640708a2SPavel Emelyanov mmput(mm); 2054640708a2SPavel Emelyanov 2055640708a2SPavel Emelyanov if (exact_vma_exists) { 2056640708a2SPavel Emelyanov if (task_dumpable(task)) { 2057640708a2SPavel Emelyanov rcu_read_lock(); 2058640708a2SPavel Emelyanov cred = __task_cred(task); 2059640708a2SPavel Emelyanov inode->i_uid = cred->euid; 2060640708a2SPavel Emelyanov inode->i_gid = cred->egid; 2061640708a2SPavel Emelyanov rcu_read_unlock(); 2062640708a2SPavel Emelyanov } else { 2063640708a2SPavel Emelyanov inode->i_uid = 0; 2064640708a2SPavel Emelyanov inode->i_gid = 0; 2065640708a2SPavel Emelyanov } 2066640708a2SPavel Emelyanov security_task_to_inode(task, inode); 2067640708a2SPavel Emelyanov status = 1; 2068640708a2SPavel Emelyanov } 2069640708a2SPavel Emelyanov 2070640708a2SPavel Emelyanov out: 2071640708a2SPavel Emelyanov put_task_struct(task); 2072640708a2SPavel Emelyanov 2073640708a2SPavel Emelyanov out_notask: 2074640708a2SPavel Emelyanov if (status <= 0) 2075640708a2SPavel Emelyanov d_drop(dentry); 2076640708a2SPavel Emelyanov 2077640708a2SPavel Emelyanov return status; 2078640708a2SPavel Emelyanov } 2079640708a2SPavel Emelyanov 2080640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = { 2081640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate, 2082640708a2SPavel Emelyanov .d_delete = pid_delete_dentry, 2083640708a2SPavel Emelyanov }; 2084640708a2SPavel Emelyanov 2085640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path) 2086640708a2SPavel Emelyanov { 2087640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 2088640708a2SPavel Emelyanov struct vm_area_struct *vma; 2089640708a2SPavel Emelyanov struct task_struct *task; 2090640708a2SPavel Emelyanov struct mm_struct *mm; 2091640708a2SPavel Emelyanov int rc; 2092640708a2SPavel Emelyanov 2093640708a2SPavel Emelyanov rc = -ENOENT; 2094640708a2SPavel Emelyanov task = get_proc_task(dentry->d_inode); 2095640708a2SPavel Emelyanov if (!task) 2096640708a2SPavel Emelyanov goto out; 2097640708a2SPavel Emelyanov 2098640708a2SPavel Emelyanov mm = get_task_mm(task); 2099640708a2SPavel Emelyanov put_task_struct(task); 2100640708a2SPavel Emelyanov if (!mm) 2101640708a2SPavel Emelyanov goto out; 2102640708a2SPavel Emelyanov 2103640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 2104640708a2SPavel Emelyanov if (rc) 2105640708a2SPavel Emelyanov goto out_mmput; 2106640708a2SPavel Emelyanov 2107640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 2108640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 2109640708a2SPavel Emelyanov if (vma && vma->vm_file) { 2110640708a2SPavel Emelyanov *path = vma->vm_file->f_path; 2111640708a2SPavel Emelyanov path_get(path); 2112640708a2SPavel Emelyanov rc = 0; 2113640708a2SPavel Emelyanov } 2114640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 2115640708a2SPavel Emelyanov 2116640708a2SPavel Emelyanov out_mmput: 2117640708a2SPavel Emelyanov mmput(mm); 2118640708a2SPavel Emelyanov out: 2119640708a2SPavel Emelyanov return rc; 2120640708a2SPavel Emelyanov } 2121640708a2SPavel Emelyanov 2122640708a2SPavel Emelyanov struct map_files_info { 2123640708a2SPavel Emelyanov struct file *file; 2124640708a2SPavel Emelyanov unsigned long len; 2125640708a2SPavel Emelyanov unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ 2126640708a2SPavel Emelyanov }; 2127640708a2SPavel Emelyanov 2128640708a2SPavel Emelyanov static struct dentry * 2129640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 2130640708a2SPavel Emelyanov struct task_struct *task, const void *ptr) 2131640708a2SPavel Emelyanov { 2132640708a2SPavel Emelyanov const struct file *file = ptr; 2133640708a2SPavel Emelyanov struct proc_inode *ei; 2134640708a2SPavel Emelyanov struct inode *inode; 2135640708a2SPavel Emelyanov 2136640708a2SPavel Emelyanov if (!file) 2137640708a2SPavel Emelyanov return ERR_PTR(-ENOENT); 2138640708a2SPavel Emelyanov 2139640708a2SPavel Emelyanov inode = proc_pid_make_inode(dir->i_sb, task); 2140640708a2SPavel Emelyanov if (!inode) 2141640708a2SPavel Emelyanov return ERR_PTR(-ENOENT); 2142640708a2SPavel Emelyanov 2143640708a2SPavel Emelyanov ei = PROC_I(inode); 2144640708a2SPavel Emelyanov ei->op.proc_get_link = proc_map_files_get_link; 2145640708a2SPavel Emelyanov 2146640708a2SPavel Emelyanov inode->i_op = &proc_pid_link_inode_operations; 2147640708a2SPavel Emelyanov inode->i_size = 64; 2148640708a2SPavel Emelyanov inode->i_mode = S_IFLNK; 2149640708a2SPavel Emelyanov 2150640708a2SPavel Emelyanov if (file->f_mode & FMODE_READ) 2151640708a2SPavel Emelyanov inode->i_mode |= S_IRUSR; 2152640708a2SPavel Emelyanov if (file->f_mode & FMODE_WRITE) 2153640708a2SPavel Emelyanov inode->i_mode |= S_IWUSR; 2154640708a2SPavel Emelyanov 2155640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations); 2156640708a2SPavel Emelyanov d_add(dentry, inode); 2157640708a2SPavel Emelyanov 2158640708a2SPavel Emelyanov return NULL; 2159640708a2SPavel Emelyanov } 2160640708a2SPavel Emelyanov 2161640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir, 2162640708a2SPavel Emelyanov struct dentry *dentry, struct nameidata *nd) 2163640708a2SPavel Emelyanov { 2164640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 2165640708a2SPavel Emelyanov struct vm_area_struct *vma; 2166640708a2SPavel Emelyanov struct task_struct *task; 2167640708a2SPavel Emelyanov struct dentry *result; 2168640708a2SPavel Emelyanov struct mm_struct *mm; 2169640708a2SPavel Emelyanov 2170640708a2SPavel Emelyanov result = ERR_PTR(-EACCES); 2171640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 2172640708a2SPavel Emelyanov goto out; 2173640708a2SPavel Emelyanov 2174640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 2175640708a2SPavel Emelyanov task = get_proc_task(dir); 2176640708a2SPavel Emelyanov if (!task) 2177640708a2SPavel Emelyanov goto out; 2178640708a2SPavel Emelyanov 2179640708a2SPavel Emelyanov result = ERR_PTR(-EACCES); 2180*eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 2181640708a2SPavel Emelyanov goto out_put_task; 2182640708a2SPavel Emelyanov 2183640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 2184640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 2185*eb94cd96SCyrill Gorcunov goto out_put_task; 2186640708a2SPavel Emelyanov 2187640708a2SPavel Emelyanov mm = get_task_mm(task); 2188640708a2SPavel Emelyanov if (!mm) 2189*eb94cd96SCyrill Gorcunov goto out_put_task; 2190640708a2SPavel Emelyanov 2191640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 2192640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 2193640708a2SPavel Emelyanov if (!vma) 2194640708a2SPavel Emelyanov goto out_no_vma; 2195640708a2SPavel Emelyanov 2196640708a2SPavel Emelyanov result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file); 2197640708a2SPavel Emelyanov 2198640708a2SPavel Emelyanov out_no_vma: 2199640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 2200640708a2SPavel Emelyanov mmput(mm); 2201640708a2SPavel Emelyanov out_put_task: 2202640708a2SPavel Emelyanov put_task_struct(task); 2203640708a2SPavel Emelyanov out: 2204640708a2SPavel Emelyanov return result; 2205640708a2SPavel Emelyanov } 2206640708a2SPavel Emelyanov 2207640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = { 2208640708a2SPavel Emelyanov .lookup = proc_map_files_lookup, 2209640708a2SPavel Emelyanov .permission = proc_fd_permission, 2210640708a2SPavel Emelyanov .setattr = proc_setattr, 2211640708a2SPavel Emelyanov }; 2212640708a2SPavel Emelyanov 2213640708a2SPavel Emelyanov static int 2214640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir) 2215640708a2SPavel Emelyanov { 2216640708a2SPavel Emelyanov struct dentry *dentry = filp->f_path.dentry; 2217640708a2SPavel Emelyanov struct inode *inode = dentry->d_inode; 2218640708a2SPavel Emelyanov struct vm_area_struct *vma; 2219640708a2SPavel Emelyanov struct task_struct *task; 2220640708a2SPavel Emelyanov struct mm_struct *mm; 2221640708a2SPavel Emelyanov ino_t ino; 2222640708a2SPavel Emelyanov int ret; 2223640708a2SPavel Emelyanov 2224640708a2SPavel Emelyanov ret = -EACCES; 2225640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 2226640708a2SPavel Emelyanov goto out; 2227640708a2SPavel Emelyanov 2228640708a2SPavel Emelyanov ret = -ENOENT; 2229640708a2SPavel Emelyanov task = get_proc_task(inode); 2230640708a2SPavel Emelyanov if (!task) 2231640708a2SPavel Emelyanov goto out; 2232640708a2SPavel Emelyanov 2233640708a2SPavel Emelyanov ret = -EACCES; 2234*eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 2235640708a2SPavel Emelyanov goto out_put_task; 2236640708a2SPavel Emelyanov 2237640708a2SPavel Emelyanov ret = 0; 2238640708a2SPavel Emelyanov switch (filp->f_pos) { 2239640708a2SPavel Emelyanov case 0: 2240640708a2SPavel Emelyanov ino = inode->i_ino; 2241640708a2SPavel Emelyanov if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0) 2242*eb94cd96SCyrill Gorcunov goto out_put_task; 2243640708a2SPavel Emelyanov filp->f_pos++; 2244640708a2SPavel Emelyanov case 1: 2245640708a2SPavel Emelyanov ino = parent_ino(dentry); 2246640708a2SPavel Emelyanov if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 2247*eb94cd96SCyrill Gorcunov goto out_put_task; 2248640708a2SPavel Emelyanov filp->f_pos++; 2249640708a2SPavel Emelyanov default: 2250640708a2SPavel Emelyanov { 2251640708a2SPavel Emelyanov unsigned long nr_files, pos, i; 2252640708a2SPavel Emelyanov struct flex_array *fa = NULL; 2253640708a2SPavel Emelyanov struct map_files_info info; 2254640708a2SPavel Emelyanov struct map_files_info *p; 2255640708a2SPavel Emelyanov 2256640708a2SPavel Emelyanov mm = get_task_mm(task); 2257640708a2SPavel Emelyanov if (!mm) 2258*eb94cd96SCyrill Gorcunov goto out_put_task; 2259640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 2260640708a2SPavel Emelyanov 2261640708a2SPavel Emelyanov nr_files = 0; 2262640708a2SPavel Emelyanov 2263640708a2SPavel Emelyanov /* 2264640708a2SPavel Emelyanov * We need two passes here: 2265640708a2SPavel Emelyanov * 2266640708a2SPavel Emelyanov * 1) Collect vmas of mapped files with mmap_sem taken 2267640708a2SPavel Emelyanov * 2) Release mmap_sem and instantiate entries 2268640708a2SPavel Emelyanov * 2269640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir() 2270640708a2SPavel Emelyanov * routine might require mmap_sem taken in might_fault(). 2271640708a2SPavel Emelyanov */ 2272640708a2SPavel Emelyanov 2273640708a2SPavel Emelyanov for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 2274640708a2SPavel Emelyanov if (vma->vm_file && ++pos > filp->f_pos) 2275640708a2SPavel Emelyanov nr_files++; 2276640708a2SPavel Emelyanov } 2277640708a2SPavel Emelyanov 2278640708a2SPavel Emelyanov if (nr_files) { 2279640708a2SPavel Emelyanov fa = flex_array_alloc(sizeof(info), nr_files, 2280640708a2SPavel Emelyanov GFP_KERNEL); 2281640708a2SPavel Emelyanov if (!fa || flex_array_prealloc(fa, 0, nr_files, 2282640708a2SPavel Emelyanov GFP_KERNEL)) { 2283640708a2SPavel Emelyanov ret = -ENOMEM; 2284640708a2SPavel Emelyanov if (fa) 2285640708a2SPavel Emelyanov flex_array_free(fa); 2286640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 2287640708a2SPavel Emelyanov mmput(mm); 2288*eb94cd96SCyrill Gorcunov goto out_put_task; 2289640708a2SPavel Emelyanov } 2290640708a2SPavel Emelyanov for (i = 0, vma = mm->mmap, pos = 2; vma; 2291640708a2SPavel Emelyanov vma = vma->vm_next) { 2292640708a2SPavel Emelyanov if (!vma->vm_file) 2293640708a2SPavel Emelyanov continue; 2294640708a2SPavel Emelyanov if (++pos <= filp->f_pos) 2295640708a2SPavel Emelyanov continue; 2296640708a2SPavel Emelyanov 2297640708a2SPavel Emelyanov get_file(vma->vm_file); 2298640708a2SPavel Emelyanov info.file = vma->vm_file; 2299640708a2SPavel Emelyanov info.len = snprintf(info.name, 2300640708a2SPavel Emelyanov sizeof(info.name), "%lx-%lx", 2301640708a2SPavel Emelyanov vma->vm_start, vma->vm_end); 2302640708a2SPavel Emelyanov if (flex_array_put(fa, i++, &info, GFP_KERNEL)) 2303640708a2SPavel Emelyanov BUG(); 2304640708a2SPavel Emelyanov } 2305640708a2SPavel Emelyanov } 2306640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 2307640708a2SPavel Emelyanov 2308640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) { 2309640708a2SPavel Emelyanov p = flex_array_get(fa, i); 2310640708a2SPavel Emelyanov ret = proc_fill_cache(filp, dirent, filldir, 2311640708a2SPavel Emelyanov p->name, p->len, 2312640708a2SPavel Emelyanov proc_map_files_instantiate, 2313640708a2SPavel Emelyanov task, p->file); 2314640708a2SPavel Emelyanov if (ret) 2315640708a2SPavel Emelyanov break; 2316640708a2SPavel Emelyanov filp->f_pos++; 2317640708a2SPavel Emelyanov fput(p->file); 2318640708a2SPavel Emelyanov } 2319640708a2SPavel Emelyanov for (; i < nr_files; i++) { 2320640708a2SPavel Emelyanov /* 2321640708a2SPavel Emelyanov * In case of error don't forget 2322640708a2SPavel Emelyanov * to put rest of file refs. 2323640708a2SPavel Emelyanov */ 2324640708a2SPavel Emelyanov p = flex_array_get(fa, i); 2325640708a2SPavel Emelyanov fput(p->file); 2326640708a2SPavel Emelyanov } 2327640708a2SPavel Emelyanov if (fa) 2328640708a2SPavel Emelyanov flex_array_free(fa); 2329640708a2SPavel Emelyanov mmput(mm); 2330640708a2SPavel Emelyanov } 2331640708a2SPavel Emelyanov } 2332640708a2SPavel Emelyanov 2333640708a2SPavel Emelyanov out_put_task: 2334640708a2SPavel Emelyanov put_task_struct(task); 2335640708a2SPavel Emelyanov out: 2336640708a2SPavel Emelyanov return ret; 2337640708a2SPavel Emelyanov } 2338640708a2SPavel Emelyanov 2339640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = { 2340640708a2SPavel Emelyanov .read = generic_read_dir, 2341640708a2SPavel Emelyanov .readdir = proc_map_files_readdir, 2342640708a2SPavel Emelyanov .llseek = default_llseek, 2343640708a2SPavel Emelyanov }; 2344640708a2SPavel Emelyanov 2345640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */ 2346640708a2SPavel Emelyanov 23471da177e4SLinus Torvalds /* 23488948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 23498948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 23508948e11fSAlexey Dobriyan */ 235110556cb2SAl Viro static int proc_fd_permission(struct inode *inode, int mask) 23528948e11fSAlexey Dobriyan { 23532830ba7fSAl Viro int rv = generic_permission(inode, mask); 23548948e11fSAlexey Dobriyan if (rv == 0) 23558948e11fSAlexey Dobriyan return 0; 23568948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 23578948e11fSAlexey Dobriyan rv = 0; 23588948e11fSAlexey Dobriyan return rv; 23598948e11fSAlexey Dobriyan } 23608948e11fSAlexey Dobriyan 23618948e11fSAlexey Dobriyan /* 23621da177e4SLinus Torvalds * proc directories can do almost nothing.. 23631da177e4SLinus Torvalds */ 2364c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 23651da177e4SLinus Torvalds .lookup = proc_lookupfd, 23668948e11fSAlexey Dobriyan .permission = proc_fd_permission, 23676d76fa58SLinus Torvalds .setattr = proc_setattr, 23681da177e4SLinus Torvalds }; 23691da177e4SLinus Torvalds 237027932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 237127932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 237227932742SMiklos Szeredi { 237327932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 237427932742SMiklos Szeredi struct inode *inode; 237527932742SMiklos Szeredi struct proc_inode *ei; 237627932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 237727932742SMiklos Szeredi 237827932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 237927932742SMiklos Szeredi if (!inode) 238027932742SMiklos Szeredi goto out; 238127932742SMiklos Szeredi ei = PROC_I(inode); 238227932742SMiklos Szeredi ei->fd = fd; 238327932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 238427932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 2385fb045adbSNick Piggin d_set_d_op(dentry, &tid_fd_dentry_operations); 238627932742SMiklos Szeredi d_add(dentry, inode); 238727932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 238827932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 238927932742SMiklos Szeredi error = NULL; 239027932742SMiklos Szeredi 239127932742SMiklos Szeredi out: 239227932742SMiklos Szeredi return error; 239327932742SMiklos Szeredi } 239427932742SMiklos Szeredi 239527932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 239627932742SMiklos Szeredi struct dentry *dentry, 239727932742SMiklos Szeredi struct nameidata *nd) 239827932742SMiklos Szeredi { 239927932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 240027932742SMiklos Szeredi } 240127932742SMiklos Szeredi 240227932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 240327932742SMiklos Szeredi { 240427932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 240527932742SMiklos Szeredi proc_fdinfo_instantiate); 240627932742SMiklos Szeredi } 240727932742SMiklos Szeredi 240827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 240927932742SMiklos Szeredi .read = generic_read_dir, 241027932742SMiklos Szeredi .readdir = proc_readfdinfo, 24116038f373SArnd Bergmann .llseek = default_llseek, 241227932742SMiklos Szeredi }; 241327932742SMiklos Szeredi 241427932742SMiklos Szeredi /* 241527932742SMiklos Szeredi * proc directories can do almost nothing.. 241627932742SMiklos Szeredi */ 241727932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 241827932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 241927932742SMiklos Szeredi .setattr = proc_setattr, 242027932742SMiklos Szeredi }; 242127932742SMiklos Szeredi 242227932742SMiklos Szeredi 2423444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 2424c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2425444ceed8SEric W. Biederman { 2426c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2427444ceed8SEric W. Biederman struct inode *inode; 2428444ceed8SEric W. Biederman struct proc_inode *ei; 2429bd6daba9SKOSAKI Motohiro struct dentry *error = ERR_PTR(-ENOENT); 2430444ceed8SEric W. Biederman 243161a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2432444ceed8SEric W. Biederman if (!inode) 2433444ceed8SEric W. Biederman goto out; 2434444ceed8SEric W. Biederman 2435444ceed8SEric W. Biederman ei = PROC_I(inode); 2436444ceed8SEric W. Biederman inode->i_mode = p->mode; 2437444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2438bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2439444ceed8SEric W. Biederman if (p->iop) 2440444ceed8SEric W. Biederman inode->i_op = p->iop; 2441444ceed8SEric W. Biederman if (p->fop) 2442444ceed8SEric W. Biederman inode->i_fop = p->fop; 2443444ceed8SEric W. Biederman ei->op = p->op; 2444fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2445444ceed8SEric W. Biederman d_add(dentry, inode); 2446444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2447444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2448444ceed8SEric W. Biederman error = NULL; 2449444ceed8SEric W. Biederman out: 2450444ceed8SEric W. Biederman return error; 2451444ceed8SEric W. Biederman } 2452444ceed8SEric W. Biederman 24531da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 24541da177e4SLinus Torvalds struct dentry *dentry, 2455c5141e6dSEric Dumazet const struct pid_entry *ents, 24567bcd6b0eSEric W. Biederman unsigned int nents) 24571da177e4SLinus Torvalds { 2458cd6a3ce9SEric W. Biederman struct dentry *error; 245999f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2460c5141e6dSEric Dumazet const struct pid_entry *p, *last; 24611da177e4SLinus Torvalds 2462cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 24631da177e4SLinus Torvalds 246499f89551SEric W. Biederman if (!task) 246599f89551SEric W. Biederman goto out_no_task; 24661da177e4SLinus Torvalds 246720cdc894SEric W. Biederman /* 246820cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 246920cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 247020cdc894SEric W. Biederman */ 24717bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 24727bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 24731da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 24741da177e4SLinus Torvalds continue; 24751da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 24761da177e4SLinus Torvalds break; 24771da177e4SLinus Torvalds } 24787bcd6b0eSEric W. Biederman if (p > last) 24791da177e4SLinus Torvalds goto out; 24801da177e4SLinus Torvalds 2481444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 24821da177e4SLinus Torvalds out: 248399f89551SEric W. Biederman put_task_struct(task); 248499f89551SEric W. Biederman out_no_task: 2485cd6a3ce9SEric W. Biederman return error; 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds 2488c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2489c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 249061a28784SEric W. Biederman { 249161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 249261a28784SEric W. Biederman proc_pident_instantiate, task, p); 249361a28784SEric W. Biederman } 249461a28784SEric W. Biederman 249528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 249628a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2497c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 249828a6d671SEric W. Biederman { 249928a6d671SEric W. Biederman int i; 25002fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 250128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 250228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2503c5141e6dSEric Dumazet const struct pid_entry *p, *last; 250428a6d671SEric W. Biederman ino_t ino; 250528a6d671SEric W. Biederman int ret; 250628a6d671SEric W. Biederman 250728a6d671SEric W. Biederman ret = -ENOENT; 250828a6d671SEric W. Biederman if (!task) 250961a28784SEric W. Biederman goto out_no_task; 251028a6d671SEric W. Biederman 251128a6d671SEric W. Biederman ret = 0; 251228a6d671SEric W. Biederman i = filp->f_pos; 251328a6d671SEric W. Biederman switch (i) { 251428a6d671SEric W. Biederman case 0: 251528a6d671SEric W. Biederman ino = inode->i_ino; 251628a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 251728a6d671SEric W. Biederman goto out; 251828a6d671SEric W. Biederman i++; 251928a6d671SEric W. Biederman filp->f_pos++; 252028a6d671SEric W. Biederman /* fall through */ 252128a6d671SEric W. Biederman case 1: 252228a6d671SEric W. Biederman ino = parent_ino(dentry); 252328a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 252428a6d671SEric W. Biederman goto out; 252528a6d671SEric W. Biederman i++; 252628a6d671SEric W. Biederman filp->f_pos++; 252728a6d671SEric W. Biederman /* fall through */ 252828a6d671SEric W. Biederman default: 252928a6d671SEric W. Biederman i -= 2; 253028a6d671SEric W. Biederman if (i >= nents) { 253128a6d671SEric W. Biederman ret = 1; 253228a6d671SEric W. Biederman goto out; 253328a6d671SEric W. Biederman } 253428a6d671SEric W. Biederman p = ents + i; 25357bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 25367bcd6b0eSEric W. Biederman while (p <= last) { 253761a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 253828a6d671SEric W. Biederman goto out; 253928a6d671SEric W. Biederman filp->f_pos++; 254028a6d671SEric W. Biederman p++; 254128a6d671SEric W. Biederman } 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 254428a6d671SEric W. Biederman ret = 1; 254528a6d671SEric W. Biederman out: 254661a28784SEric W. Biederman put_task_struct(task); 254761a28784SEric W. Biederman out_no_task: 254828a6d671SEric W. Biederman return ret; 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 255228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 255328a6d671SEric W. Biederman size_t count, loff_t *ppos) 255428a6d671SEric W. Biederman { 25552fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 255604ff9708SAl Viro char *p = NULL; 255728a6d671SEric W. Biederman ssize_t length; 255828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 255928a6d671SEric W. Biederman 256028a6d671SEric W. Biederman if (!task) 256104ff9708SAl Viro return -ESRCH; 256228a6d671SEric W. Biederman 256328a6d671SEric W. Biederman length = security_getprocattr(task, 25642fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 256504ff9708SAl Viro &p); 256628a6d671SEric W. Biederman put_task_struct(task); 256704ff9708SAl Viro if (length > 0) 256804ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 256904ff9708SAl Viro kfree(p); 257028a6d671SEric W. Biederman return length; 257128a6d671SEric W. Biederman } 257228a6d671SEric W. Biederman 257328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 257428a6d671SEric W. Biederman size_t count, loff_t *ppos) 257528a6d671SEric W. Biederman { 25762fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 257728a6d671SEric W. Biederman char *page; 257828a6d671SEric W. Biederman ssize_t length; 257928a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 258028a6d671SEric W. Biederman 258128a6d671SEric W. Biederman length = -ESRCH; 258228a6d671SEric W. Biederman if (!task) 258328a6d671SEric W. Biederman goto out_no_task; 258428a6d671SEric W. Biederman if (count > PAGE_SIZE) 258528a6d671SEric W. Biederman count = PAGE_SIZE; 258628a6d671SEric W. Biederman 258728a6d671SEric W. Biederman /* No partial writes. */ 258828a6d671SEric W. Biederman length = -EINVAL; 258928a6d671SEric W. Biederman if (*ppos != 0) 259028a6d671SEric W. Biederman goto out; 259128a6d671SEric W. Biederman 259228a6d671SEric W. Biederman length = -ENOMEM; 2593e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 259428a6d671SEric W. Biederman if (!page) 259528a6d671SEric W. Biederman goto out; 259628a6d671SEric W. Biederman 259728a6d671SEric W. Biederman length = -EFAULT; 259828a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 259928a6d671SEric W. Biederman goto out_free; 260028a6d671SEric W. Biederman 2601107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 26029b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2603107db7c7SDavid Howells if (length < 0) 2604107db7c7SDavid Howells goto out_free; 2605107db7c7SDavid Howells 260628a6d671SEric W. Biederman length = security_setprocattr(task, 26072fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 260828a6d671SEric W. Biederman (void*)page, count); 26099b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 261028a6d671SEric W. Biederman out_free: 261128a6d671SEric W. Biederman free_page((unsigned long) page); 261228a6d671SEric W. Biederman out: 261328a6d671SEric W. Biederman put_task_struct(task); 261428a6d671SEric W. Biederman out_no_task: 261528a6d671SEric W. Biederman return length; 261628a6d671SEric W. Biederman } 261728a6d671SEric W. Biederman 261800977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 261928a6d671SEric W. Biederman .read = proc_pid_attr_read, 262028a6d671SEric W. Biederman .write = proc_pid_attr_write, 262187df8424SArnd Bergmann .llseek = generic_file_llseek, 262228a6d671SEric W. Biederman }; 262328a6d671SEric W. Biederman 2624c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2625631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2626631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2627631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2628631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2629631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2630631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 263128a6d671SEric W. Biederman }; 263228a6d671SEric W. Biederman 263372d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 26341da177e4SLinus Torvalds void * dirent, filldir_t filldir) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 263772d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds 264000977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 26411da177e4SLinus Torvalds .read = generic_read_dir, 264272d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 26436038f373SArnd Bergmann .llseek = default_llseek, 26441da177e4SLinus Torvalds }; 26451da177e4SLinus Torvalds 264672d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 26471da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 26481da177e4SLinus Torvalds { 26497bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 26507bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 2653c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 265472d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 265599f89551SEric W. Biederman .getattr = pid_getattr, 26566d76fa58SLinus Torvalds .setattr = proc_setattr, 26571da177e4SLinus Torvalds }; 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds #endif 26601da177e4SLinus Torvalds 2661698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 26623cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 26633cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 26643cb4a0bbSKawai, Hidehiro { 26653cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 26663cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 26673cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 26683cb4a0bbSKawai, Hidehiro size_t len; 26693cb4a0bbSKawai, Hidehiro int ret; 26703cb4a0bbSKawai, Hidehiro 26713cb4a0bbSKawai, Hidehiro if (!task) 26723cb4a0bbSKawai, Hidehiro return -ESRCH; 26733cb4a0bbSKawai, Hidehiro 26743cb4a0bbSKawai, Hidehiro ret = 0; 26753cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 26763cb4a0bbSKawai, Hidehiro if (mm) { 26773cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 26783cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 26793cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 26803cb4a0bbSKawai, Hidehiro mmput(mm); 26813cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 26823cb4a0bbSKawai, Hidehiro } 26833cb4a0bbSKawai, Hidehiro 26843cb4a0bbSKawai, Hidehiro put_task_struct(task); 26853cb4a0bbSKawai, Hidehiro 26863cb4a0bbSKawai, Hidehiro return ret; 26873cb4a0bbSKawai, Hidehiro } 26883cb4a0bbSKawai, Hidehiro 26893cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 26903cb4a0bbSKawai, Hidehiro const char __user *buf, 26913cb4a0bbSKawai, Hidehiro size_t count, 26923cb4a0bbSKawai, Hidehiro loff_t *ppos) 26933cb4a0bbSKawai, Hidehiro { 26943cb4a0bbSKawai, Hidehiro struct task_struct *task; 26953cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 26963cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 26973cb4a0bbSKawai, Hidehiro unsigned int val; 26983cb4a0bbSKawai, Hidehiro int ret; 26993cb4a0bbSKawai, Hidehiro int i; 27003cb4a0bbSKawai, Hidehiro unsigned long mask; 27013cb4a0bbSKawai, Hidehiro 27023cb4a0bbSKawai, Hidehiro ret = -EFAULT; 27033cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 27043cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 27053cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 27063cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 27073cb4a0bbSKawai, Hidehiro goto out_no_task; 27083cb4a0bbSKawai, Hidehiro 27093cb4a0bbSKawai, Hidehiro ret = -EINVAL; 27103cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 27113cb4a0bbSKawai, Hidehiro if (*end == '\n') 27123cb4a0bbSKawai, Hidehiro end++; 27133cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 27143cb4a0bbSKawai, Hidehiro goto out_no_task; 27153cb4a0bbSKawai, Hidehiro 27163cb4a0bbSKawai, Hidehiro ret = -ESRCH; 27173cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 27183cb4a0bbSKawai, Hidehiro if (!task) 27193cb4a0bbSKawai, Hidehiro goto out_no_task; 27203cb4a0bbSKawai, Hidehiro 27213cb4a0bbSKawai, Hidehiro ret = end - buffer; 27223cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 27233cb4a0bbSKawai, Hidehiro if (!mm) 27243cb4a0bbSKawai, Hidehiro goto out_no_mm; 27253cb4a0bbSKawai, Hidehiro 27263cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 27273cb4a0bbSKawai, Hidehiro if (val & mask) 27283cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 27293cb4a0bbSKawai, Hidehiro else 27303cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 27313cb4a0bbSKawai, Hidehiro } 27323cb4a0bbSKawai, Hidehiro 27333cb4a0bbSKawai, Hidehiro mmput(mm); 27343cb4a0bbSKawai, Hidehiro out_no_mm: 27353cb4a0bbSKawai, Hidehiro put_task_struct(task); 27363cb4a0bbSKawai, Hidehiro out_no_task: 27373cb4a0bbSKawai, Hidehiro return ret; 27383cb4a0bbSKawai, Hidehiro } 27393cb4a0bbSKawai, Hidehiro 27403cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 27413cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 27423cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 274387df8424SArnd Bergmann .llseek = generic_file_llseek, 27443cb4a0bbSKawai, Hidehiro }; 27453cb4a0bbSKawai, Hidehiro #endif 27463cb4a0bbSKawai, Hidehiro 27471da177e4SLinus Torvalds /* 27481da177e4SLinus Torvalds * /proc/self: 27491da177e4SLinus Torvalds */ 27501da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 27511da177e4SLinus Torvalds int buflen) 27521da177e4SLinus Torvalds { 2753488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2754b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 27558578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2756b55fcb22SAndrew Morton if (!tgid) 2757488e5bc4SEric W. Biederman return -ENOENT; 2758b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 27591da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 2762008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 27631da177e4SLinus Torvalds { 2764488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2765b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 27667fee4868SAl Viro char *name = ERR_PTR(-ENOENT); 27677fee4868SAl Viro if (tgid) { 27687fee4868SAl Viro name = __getname(); 27697fee4868SAl Viro if (!name) 27707fee4868SAl Viro name = ERR_PTR(-ENOMEM); 27717fee4868SAl Viro else 27727fee4868SAl Viro sprintf(name, "%d", tgid); 27737fee4868SAl Viro } 27747fee4868SAl Viro nd_set_link(nd, name); 27757fee4868SAl Viro return NULL; 27767fee4868SAl Viro } 27777fee4868SAl Viro 27787fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd, 27797fee4868SAl Viro void *cookie) 27807fee4868SAl Viro { 27817fee4868SAl Viro char *s = nd_get_link(nd); 27827fee4868SAl Viro if (!IS_ERR(s)) 27837fee4868SAl Viro __putname(s); 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 2786c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 27871da177e4SLinus Torvalds .readlink = proc_self_readlink, 27881da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 27897fee4868SAl Viro .put_link = proc_self_put_link, 27901da177e4SLinus Torvalds }; 27911da177e4SLinus Torvalds 279228a6d671SEric W. Biederman /* 2793801199ceSEric W. Biederman * proc base 2794801199ceSEric W. Biederman * 2795801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2796801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2797801199ceSEric W. Biederman * describe something that is process related. 2798801199ceSEric W. Biederman */ 2799c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 280061a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2801801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2802801199ceSEric W. Biederman }; 2803801199ceSEric W. Biederman 2804444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2805c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2806801199ceSEric W. Biederman { 2807c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2808801199ceSEric W. Biederman struct inode *inode; 2809801199ceSEric W. Biederman struct proc_inode *ei; 281073d36460SDan Carpenter struct dentry *error; 2811801199ceSEric W. Biederman 2812801199ceSEric W. Biederman /* Allocate the inode */ 2813801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2814801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2815801199ceSEric W. Biederman if (!inode) 2816801199ceSEric W. Biederman goto out; 2817801199ceSEric W. Biederman 2818801199ceSEric W. Biederman /* Initialize the inode */ 2819801199ceSEric W. Biederman ei = PROC_I(inode); 282085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2821801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2822801199ceSEric W. Biederman 2823801199ceSEric W. Biederman /* 2824801199ceSEric W. Biederman * grab the reference to the task. 2825801199ceSEric W. Biederman */ 28261a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2827801199ceSEric W. Biederman if (!ei->pid) 2828801199ceSEric W. Biederman goto out_iput; 2829801199ceSEric W. Biederman 2830801199ceSEric W. Biederman inode->i_mode = p->mode; 2831801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2832bfe86848SMiklos Szeredi set_nlink(inode, 2); 2833801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2834801199ceSEric W. Biederman inode->i_size = 64; 2835801199ceSEric W. Biederman if (p->iop) 2836801199ceSEric W. Biederman inode->i_op = p->iop; 2837801199ceSEric W. Biederman if (p->fop) 2838801199ceSEric W. Biederman inode->i_fop = p->fop; 2839801199ceSEric W. Biederman ei->op = p->op; 2840801199ceSEric W. Biederman d_add(dentry, inode); 2841801199ceSEric W. Biederman error = NULL; 2842801199ceSEric W. Biederman out: 2843801199ceSEric W. Biederman return error; 2844801199ceSEric W. Biederman out_iput: 2845801199ceSEric W. Biederman iput(inode); 2846801199ceSEric W. Biederman goto out; 2847801199ceSEric W. Biederman } 2848801199ceSEric W. Biederman 2849444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2850444ceed8SEric W. Biederman { 2851444ceed8SEric W. Biederman struct dentry *error; 2852444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2853c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2854444ceed8SEric W. Biederman 2855444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2856444ceed8SEric W. Biederman 2857444ceed8SEric W. Biederman if (!task) 2858444ceed8SEric W. Biederman goto out_no_task; 2859444ceed8SEric W. Biederman 2860444ceed8SEric W. Biederman /* Lookup the directory entry */ 28617bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 28627bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2863444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2864444ceed8SEric W. Biederman continue; 2865444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2866444ceed8SEric W. Biederman break; 2867444ceed8SEric W. Biederman } 28687bcd6b0eSEric W. Biederman if (p > last) 2869444ceed8SEric W. Biederman goto out; 2870444ceed8SEric W. Biederman 2871444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2872444ceed8SEric W. Biederman 2873444ceed8SEric W. Biederman out: 2874444ceed8SEric W. Biederman put_task_struct(task); 2875444ceed8SEric W. Biederman out_no_task: 2876444ceed8SEric W. Biederman return error; 2877444ceed8SEric W. Biederman } 2878444ceed8SEric W. Biederman 2879c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2880c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 288161a28784SEric W. Biederman { 288261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 288361a28784SEric W. Biederman proc_base_instantiate, task, p); 288461a28784SEric W. Biederman } 288561a28784SEric W. Biederman 2886aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2887297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2888aba76fdbSAndrew Morton { 2889940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2890297c5d92SAndrea Righi unsigned long flags; 2891293eb1e7SVasiliy Kulikov int result; 2892297c5d92SAndrea Righi 2893293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2894293eb1e7SVasiliy Kulikov if (result) 2895293eb1e7SVasiliy Kulikov return result; 2896293eb1e7SVasiliy Kulikov 2897293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2898293eb1e7SVasiliy Kulikov result = -EACCES; 2899293eb1e7SVasiliy Kulikov goto out_unlock; 2900293eb1e7SVasiliy Kulikov } 29011d1221f3SVasiliy Kulikov 29025995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2903b2d002dbSAndrea Righi struct task_struct *t = task; 2904297c5d92SAndrea Righi 29055995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 29065995477aSAndrea Righi while_each_thread(task, t) 29075995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2908297c5d92SAndrea Righi 2909297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2910297c5d92SAndrea Righi } 2911293eb1e7SVasiliy Kulikov result = sprintf(buffer, 2912aba76fdbSAndrew Morton "rchar: %llu\n" 2913aba76fdbSAndrew Morton "wchar: %llu\n" 2914aba76fdbSAndrew Morton "syscr: %llu\n" 2915aba76fdbSAndrew Morton "syscw: %llu\n" 2916aba76fdbSAndrew Morton "read_bytes: %llu\n" 2917aba76fdbSAndrew Morton "write_bytes: %llu\n" 2918aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 29197c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 29207c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 29217c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 29227c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 29237c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 29247c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 29257c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2926293eb1e7SVasiliy Kulikov out_unlock: 2927293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2928293eb1e7SVasiliy Kulikov return result; 2929aba76fdbSAndrew Morton } 2930297c5d92SAndrea Righi 2931297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2932297c5d92SAndrea Righi { 2933297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2934297c5d92SAndrea Righi } 2935297c5d92SAndrea Righi 2936297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2937297c5d92SAndrea Righi { 2938297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2939297c5d92SAndrea Righi } 2940297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2941aba76fdbSAndrew Morton 294247830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 294347830723SKees Cook struct pid *pid, struct task_struct *task) 294447830723SKees Cook { 2945a9712bc1SAl Viro int err = lock_trace(task); 2946a9712bc1SAl Viro if (!err) { 294747830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2948a9712bc1SAl Viro unlock_trace(task); 2949a9712bc1SAl Viro } 2950a9712bc1SAl Viro return err; 295147830723SKees Cook } 295247830723SKees Cook 2953801199ceSEric W. Biederman /* 295428a6d671SEric W. Biederman * Thread groups 295528a6d671SEric W. Biederman */ 295600977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2957c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 295820cdc894SEric W. Biederman 2959c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2960631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2961631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2962640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2963640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 2964640708a2SPavel Emelyanov #endif 2965631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 29666b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2967b2211a36SAndrew Morton #ifdef CONFIG_NET 2968631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2969b2211a36SAndrew Morton #endif 2970631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2971631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2972631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2973a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 29743036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 297543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2976631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 297743ae34cbSIngo Molnar #endif 29785091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 29795091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 29805091faa4SMike Galbraith #endif 29814614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2982ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2983a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2984ebcb6734SRoland McGrath #endif 2985631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2986631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2987631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2988b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations), 298928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2990b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 299128a6d671SEric W. Biederman #endif 2992631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2993631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2994631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2995631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2996631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2997631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2998631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 29991e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 3000631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3001b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations), 3002ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 300328a6d671SEric W. Biederman #endif 300428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 3005631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 300628a6d671SEric W. Biederman #endif 300728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 3008631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 300928a6d671SEric W. Biederman #endif 30102ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3011a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 301228a6d671SEric W. Biederman #endif 301328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 3014631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 301528a6d671SEric W. Biederman #endif 30169745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3017631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 30189745512cSArjan van de Ven #endif 30198793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 3020631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 302128a6d671SEric W. Biederman #endif 3022a424316cSPaul Menage #ifdef CONFIG_CGROUPS 3023631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 3024a424316cSPaul Menage #endif 3025631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 3026631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 3027a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 302828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 3029631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 3030631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 303128a6d671SEric W. Biederman #endif 3032f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 3033631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3034f4f154fdSAkinobu Mita #endif 3035698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 3036631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 30373cb4a0bbSKawai, Hidehiro #endif 3038aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 30391d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tgid_io_accounting), 3040aba76fdbSAndrew Morton #endif 3041f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 3042f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 3043f133eccaSChris Metcalf #endif 304428a6d671SEric W. Biederman }; 304528a6d671SEric W. Biederman 304628a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 304728a6d671SEric W. Biederman void * dirent, filldir_t filldir) 304828a6d671SEric W. Biederman { 304928a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 305028a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 305128a6d671SEric W. Biederman } 305228a6d671SEric W. Biederman 305300977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 305428a6d671SEric W. Biederman .read = generic_read_dir, 305528a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 30566038f373SArnd Bergmann .llseek = default_llseek, 305728a6d671SEric W. Biederman }; 305828a6d671SEric W. Biederman 305928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 30607bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 30617bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 306228a6d671SEric W. Biederman } 306328a6d671SEric W. Biederman 3064c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 306528a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 306628a6d671SEric W. Biederman .getattr = pid_getattr, 306728a6d671SEric W. Biederman .setattr = proc_setattr, 30680499680aSVasiliy Kulikov .permission = proc_pid_permission, 306928a6d671SEric W. Biederman }; 307028a6d671SEric W. Biederman 307160347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 30721da177e4SLinus Torvalds { 307348e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 30748578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 307548e6484dSEric W. Biederman struct qstr name; 30761da177e4SLinus Torvalds 307748e6484dSEric W. Biederman name.name = buf; 307860347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 307960347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 308048e6484dSEric W. Biederman if (dentry) { 308148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 308248e6484dSEric W. Biederman d_drop(dentry); 308348e6484dSEric W. Biederman dput(dentry); 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 308648e6484dSEric W. Biederman name.name = buf; 308760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 308860347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 308948e6484dSEric W. Biederman if (!leader) 309048e6484dSEric W. Biederman goto out; 309148e6484dSEric W. Biederman 309248e6484dSEric W. Biederman name.name = "task"; 309348e6484dSEric W. Biederman name.len = strlen(name.name); 309448e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 309548e6484dSEric W. Biederman if (!dir) 309648e6484dSEric W. Biederman goto out_put_leader; 309748e6484dSEric W. Biederman 309848e6484dSEric W. Biederman name.name = buf; 309960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 310048e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 310148e6484dSEric W. Biederman if (dentry) { 310248e6484dSEric W. Biederman shrink_dcache_parent(dentry); 310348e6484dSEric W. Biederman d_drop(dentry); 310448e6484dSEric W. Biederman dput(dentry); 31051da177e4SLinus Torvalds } 310648e6484dSEric W. Biederman 310748e6484dSEric W. Biederman dput(dir); 310848e6484dSEric W. Biederman out_put_leader: 310948e6484dSEric W. Biederman dput(leader); 311048e6484dSEric W. Biederman out: 311148e6484dSEric W. Biederman return; 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 31140895e91dSRandy Dunlap /** 31150895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 31160895e91dSRandy Dunlap * @task: task that should be flushed. 31170895e91dSRandy Dunlap * 31180895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 311960347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 31200895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 31210895e91dSRandy Dunlap * 31220895e91dSRandy Dunlap * Looks in the dcache for 31230895e91dSRandy Dunlap * /proc/@pid 31240895e91dSRandy Dunlap * /proc/@tgid/task/@pid 31250895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 31260895e91dSRandy Dunlap * from the dcache. 31270895e91dSRandy Dunlap * 31280895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 31290895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 31300895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 31310895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 31320895e91dSRandy Dunlap * dcache entries at process exit time. 31330895e91dSRandy Dunlap * 31340895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 31350895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 31360895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 313760347f67SPavel Emelyanov */ 313860347f67SPavel Emelyanov 313960347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 314060347f67SPavel Emelyanov { 31419fcc2d15SEric W. Biederman int i; 31429b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 3143130f77ecSPavel Emelyanov struct upid *upid; 3144130f77ecSPavel Emelyanov 3145130f77ecSPavel Emelyanov pid = task_pid(task); 3146130f77ecSPavel Emelyanov tgid = task_tgid(task); 31479fcc2d15SEric W. Biederman 31489fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 3149130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 3150130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 31519b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 3152130f77ecSPavel Emelyanov } 31536f4e6433SPavel Emelyanov 31546f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 31556f4e6433SPavel Emelyanov if (upid->nr == 1) 31566f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 315760347f67SPavel Emelyanov } 315860347f67SPavel Emelyanov 31599711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 31609711ef99SAdrian Bunk struct dentry * dentry, 3161c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 3162444ceed8SEric W. Biederman { 3163444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3164444ceed8SEric W. Biederman struct inode *inode; 3165444ceed8SEric W. Biederman 316661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3167444ceed8SEric W. Biederman if (!inode) 3168444ceed8SEric W. Biederman goto out; 3169444ceed8SEric W. Biederman 3170444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3171444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 3172444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 3173444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3174aed54175SVegard Nossum 3175bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 3176bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 3177444ceed8SEric W. Biederman 3178fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3179444ceed8SEric W. Biederman 3180444ceed8SEric W. Biederman d_add(dentry, inode); 3181444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 3182444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 3183444ceed8SEric W. Biederman error = NULL; 3184444ceed8SEric W. Biederman out: 3185444ceed8SEric W. Biederman return error; 3186444ceed8SEric W. Biederman } 3187444ceed8SEric W. Biederman 31881da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 31891da177e4SLinus Torvalds { 319073d36460SDan Carpenter struct dentry *result; 31911da177e4SLinus Torvalds struct task_struct *task; 31921da177e4SLinus Torvalds unsigned tgid; 3193b488893aSPavel Emelyanov struct pid_namespace *ns; 31941da177e4SLinus Torvalds 3195801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 3196801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 3197801199ceSEric W. Biederman goto out; 3198801199ceSEric W. Biederman 31991da177e4SLinus Torvalds tgid = name_to_int(dentry); 32001da177e4SLinus Torvalds if (tgid == ~0U) 32011da177e4SLinus Torvalds goto out; 32021da177e4SLinus Torvalds 3203b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 3204de758734SEric W. Biederman rcu_read_lock(); 3205b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 32061da177e4SLinus Torvalds if (task) 32071da177e4SLinus Torvalds get_task_struct(task); 3208de758734SEric W. Biederman rcu_read_unlock(); 32091da177e4SLinus Torvalds if (!task) 32101da177e4SLinus Torvalds goto out; 32111da177e4SLinus Torvalds 3212444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 321348e6484dSEric W. Biederman put_task_struct(task); 32141da177e4SLinus Torvalds out: 3215cd6a3ce9SEric W. Biederman return result; 32161da177e4SLinus Torvalds } 32171da177e4SLinus Torvalds 32181da177e4SLinus Torvalds /* 32190804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 32200bc58a91SEric W. Biederman * 32211da177e4SLinus Torvalds */ 322219fd4bb2SEric W. Biederman struct tgid_iter { 322319fd4bb2SEric W. Biederman unsigned int tgid; 32240804ef4bSEric W. Biederman struct task_struct *task; 322519fd4bb2SEric W. Biederman }; 322619fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 322719fd4bb2SEric W. Biederman { 32280804ef4bSEric W. Biederman struct pid *pid; 32291da177e4SLinus Torvalds 323019fd4bb2SEric W. Biederman if (iter.task) 323119fd4bb2SEric W. Biederman put_task_struct(iter.task); 32320804ef4bSEric W. Biederman rcu_read_lock(); 32330804ef4bSEric W. Biederman retry: 323419fd4bb2SEric W. Biederman iter.task = NULL; 323519fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 32360804ef4bSEric W. Biederman if (pid) { 323719fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 323819fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 32390804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 32400804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 32410804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 32420804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 32430804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 32440804ef4bSEric W. Biederman * 32450804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 32460804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 32470804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 32480804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 32490804ef4bSEric W. Biederman * As we don't care in the case of readdir. 32500bc58a91SEric W. Biederman */ 325119fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 325219fd4bb2SEric W. Biederman iter.tgid += 1; 32530804ef4bSEric W. Biederman goto retry; 325419fd4bb2SEric W. Biederman } 325519fd4bb2SEric W. Biederman get_task_struct(iter.task); 32561da177e4SLinus Torvalds } 3257454cc105SEric W. Biederman rcu_read_unlock(); 325819fd4bb2SEric W. Biederman return iter; 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 32617bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 32621da177e4SLinus Torvalds 326361a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 326419fd4bb2SEric W. Biederman struct tgid_iter iter) 326561a28784SEric W. Biederman { 326661a28784SEric W. Biederman char name[PROC_NUMBUF]; 326719fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 326861a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 326919fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 327061a28784SEric W. Biederman } 327161a28784SEric W. Biederman 32720499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen, 32730499680aSVasiliy Kulikov loff_t offset, u64 ino, unsigned d_type) 32740499680aSVasiliy Kulikov { 32750499680aSVasiliy Kulikov return 0; 32760499680aSVasiliy Kulikov } 32770499680aSVasiliy Kulikov 32781da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 32791da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 32801da177e4SLinus Torvalds { 3281d8bdc59fSLinus Torvalds unsigned int nr; 3282d8bdc59fSLinus Torvalds struct task_struct *reaper; 328319fd4bb2SEric W. Biederman struct tgid_iter iter; 3284b488893aSPavel Emelyanov struct pid_namespace *ns; 32850499680aSVasiliy Kulikov filldir_t __filldir; 32861da177e4SLinus Torvalds 3287d8bdc59fSLinus Torvalds if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET) 3288d8bdc59fSLinus Torvalds goto out_no_task; 3289d8bdc59fSLinus Torvalds nr = filp->f_pos - FIRST_PROCESS_ENTRY; 3290d8bdc59fSLinus Torvalds 3291d8bdc59fSLinus Torvalds reaper = get_proc_task(filp->f_path.dentry->d_inode); 329261a28784SEric W. Biederman if (!reaper) 329361a28784SEric W. Biederman goto out_no_task; 329461a28784SEric W. Biederman 32957bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 3296c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 329761a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 3298801199ceSEric W. Biederman goto out; 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 3301b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 330219fd4bb2SEric W. Biederman iter.task = NULL; 330319fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 330419fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 330519fd4bb2SEric W. Biederman iter.task; 330619fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 33070499680aSVasiliy Kulikov if (has_pid_permissions(ns, iter.task, 2)) 33080499680aSVasiliy Kulikov __filldir = filldir; 33090499680aSVasiliy Kulikov else 33100499680aSVasiliy Kulikov __filldir = fake_filldir; 33110499680aSVasiliy Kulikov 331219fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 33130499680aSVasiliy Kulikov if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) { 331419fd4bb2SEric W. Biederman put_task_struct(iter.task); 33150804ef4bSEric W. Biederman goto out; 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds } 33180804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 33190804ef4bSEric W. Biederman out: 332061a28784SEric W. Biederman put_task_struct(reaper); 332161a28784SEric W. Biederman out_no_task: 33221da177e4SLinus Torvalds return 0; 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds 33250bc58a91SEric W. Biederman /* 332628a6d671SEric W. Biederman * Tasks 332728a6d671SEric W. Biederman */ 3328c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 3329631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 33303835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 33316b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3332631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 3333631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 3334631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 3335a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 33363036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 333743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 3338631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 333943ae34cbSIngo Molnar #endif 33404614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 3341ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3342a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 3343ebcb6734SRoland McGrath #endif 3344631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 3345631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 3346631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 3347b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_tid_maps_operations), 334828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 3349b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), 335028a6d671SEric W. Biederman #endif 3351631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3352631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 3353631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 3354631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 3355631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 3356631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 33571e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 3358631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3359b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_tid_smaps_operations), 3360ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 336128a6d671SEric W. Biederman #endif 336228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 3363631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 336428a6d671SEric W. Biederman #endif 336528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 3366631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 336728a6d671SEric W. Biederman #endif 33682ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3369a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 337028a6d671SEric W. Biederman #endif 337128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 3372631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 337328a6d671SEric W. Biederman #endif 33749745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3375631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 33769745512cSArjan van de Ven #endif 33778793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 3378631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 337928a6d671SEric W. Biederman #endif 3380a424316cSPaul Menage #ifdef CONFIG_CGROUPS 3381631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 3382a424316cSPaul Menage #endif 3383631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 3384631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 3385a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 338628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 3387631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 338826ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 338928a6d671SEric W. Biederman #endif 3390f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 3391631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3392f4f154fdSAkinobu Mita #endif 3393297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 33941d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tid_io_accounting), 3395297c5d92SAndrea Righi #endif 3396f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 3397f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 3398f133eccaSChris Metcalf #endif 339928a6d671SEric W. Biederman }; 340028a6d671SEric W. Biederman 340128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 340228a6d671SEric W. Biederman void * dirent, filldir_t filldir) 340328a6d671SEric W. Biederman { 340428a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 340528a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 340628a6d671SEric W. Biederman } 340728a6d671SEric W. Biederman 340828a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 34097bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 34107bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 341128a6d671SEric W. Biederman } 341228a6d671SEric W. Biederman 341300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 341428a6d671SEric W. Biederman .read = generic_read_dir, 341528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 34166038f373SArnd Bergmann .llseek = default_llseek, 341728a6d671SEric W. Biederman }; 341828a6d671SEric W. Biederman 3419c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 342028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 342128a6d671SEric W. Biederman .getattr = pid_getattr, 342228a6d671SEric W. Biederman .setattr = proc_setattr, 342328a6d671SEric W. Biederman }; 342428a6d671SEric W. Biederman 3425444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 3426c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3427444ceed8SEric W. Biederman { 3428444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3429444ceed8SEric W. Biederman struct inode *inode; 343061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3431444ceed8SEric W. Biederman 3432444ceed8SEric W. Biederman if (!inode) 3433444ceed8SEric W. Biederman goto out; 3434444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3435444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3436444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3437444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3438aed54175SVegard Nossum 3439bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 3440bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 3441444ceed8SEric W. Biederman 3442fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3443444ceed8SEric W. Biederman 3444444ceed8SEric W. Biederman d_add(dentry, inode); 3445444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 3446444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 3447444ceed8SEric W. Biederman error = NULL; 3448444ceed8SEric W. Biederman out: 3449444ceed8SEric W. Biederman return error; 3450444ceed8SEric W. Biederman } 3451444ceed8SEric W. Biederman 345228a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 345328a6d671SEric W. Biederman { 345428a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 345528a6d671SEric W. Biederman struct task_struct *task; 345628a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 345728a6d671SEric W. Biederman unsigned tid; 3458b488893aSPavel Emelyanov struct pid_namespace *ns; 345928a6d671SEric W. Biederman 346028a6d671SEric W. Biederman if (!leader) 346128a6d671SEric W. Biederman goto out_no_task; 346228a6d671SEric W. Biederman 346328a6d671SEric W. Biederman tid = name_to_int(dentry); 346428a6d671SEric W. Biederman if (tid == ~0U) 346528a6d671SEric W. Biederman goto out; 346628a6d671SEric W. Biederman 3467b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 346828a6d671SEric W. Biederman rcu_read_lock(); 3469b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 347028a6d671SEric W. Biederman if (task) 347128a6d671SEric W. Biederman get_task_struct(task); 347228a6d671SEric W. Biederman rcu_read_unlock(); 347328a6d671SEric W. Biederman if (!task) 347428a6d671SEric W. Biederman goto out; 3475bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 347628a6d671SEric W. Biederman goto out_drop_task; 347728a6d671SEric W. Biederman 3478444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 347928a6d671SEric W. Biederman out_drop_task: 348028a6d671SEric W. Biederman put_task_struct(task); 348128a6d671SEric W. Biederman out: 348228a6d671SEric W. Biederman put_task_struct(leader); 348328a6d671SEric W. Biederman out_no_task: 348428a6d671SEric W. Biederman return result; 348528a6d671SEric W. Biederman } 348628a6d671SEric W. Biederman 348728a6d671SEric W. Biederman /* 34880bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 34890bc58a91SEric W. Biederman * 34900bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 34910bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 34920bc58a91SEric W. Biederman * directory we have more work todo. 34930bc58a91SEric W. Biederman * 34940bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 34950bc58a91SEric W. Biederman * 34960bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 34970bc58a91SEric W. Biederman * threads past it. 34980bc58a91SEric W. Biederman */ 3499cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3500b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 35010bc58a91SEric W. Biederman { 3502a872ff0cSOleg Nesterov struct task_struct *pos; 35030bc58a91SEric W. Biederman 3504cc288738SEric W. Biederman rcu_read_lock(); 35050bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 35060bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3507b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3508a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3509a872ff0cSOleg Nesterov goto found; 35100bc58a91SEric W. Biederman } 35110bc58a91SEric W. Biederman 35120bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 35130bc58a91SEric W. Biederman pos = NULL; 3514a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3515a872ff0cSOleg Nesterov goto out; 3516a872ff0cSOleg Nesterov 3517a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3518a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3519a872ff0cSOleg Nesterov */ 3520a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3521a872ff0cSOleg Nesterov pos = next_thread(pos); 3522a872ff0cSOleg Nesterov if (pos == leader) { 3523a872ff0cSOleg Nesterov pos = NULL; 3524a872ff0cSOleg Nesterov goto out; 3525a872ff0cSOleg Nesterov } 3526a872ff0cSOleg Nesterov } 3527a872ff0cSOleg Nesterov found: 3528a872ff0cSOleg Nesterov get_task_struct(pos); 3529a872ff0cSOleg Nesterov out: 3530cc288738SEric W. Biederman rcu_read_unlock(); 35310bc58a91SEric W. Biederman return pos; 35320bc58a91SEric W. Biederman } 35330bc58a91SEric W. Biederman 35340bc58a91SEric W. Biederman /* 35350bc58a91SEric W. Biederman * Find the next thread in the thread list. 35360bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 35370bc58a91SEric W. Biederman * 35380bc58a91SEric W. Biederman * The reference to the input task_struct is released. 35390bc58a91SEric W. Biederman */ 35400bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 35410bc58a91SEric W. Biederman { 3542c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3543cc288738SEric W. Biederman rcu_read_lock(); 3544c1df7fb8SOleg Nesterov if (pid_alive(start)) { 35450bc58a91SEric W. Biederman pos = next_thread(start); 3546c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 35470bc58a91SEric W. Biederman pos = NULL; 3548c1df7fb8SOleg Nesterov else 3549c1df7fb8SOleg Nesterov get_task_struct(pos); 3550c1df7fb8SOleg Nesterov } 3551cc288738SEric W. Biederman rcu_read_unlock(); 35520bc58a91SEric W. Biederman put_task_struct(start); 35530bc58a91SEric W. Biederman return pos; 35540bc58a91SEric W. Biederman } 35550bc58a91SEric W. Biederman 355661a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 355761a28784SEric W. Biederman struct task_struct *task, int tid) 355861a28784SEric W. Biederman { 355961a28784SEric W. Biederman char name[PROC_NUMBUF]; 356061a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 356161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 356261a28784SEric W. Biederman proc_task_instantiate, task, NULL); 356361a28784SEric W. Biederman } 356461a28784SEric W. Biederman 35651da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 35661da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 35671da177e4SLinus Torvalds { 35682fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 35691da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 35707d895244SGuillaume Chazarain struct task_struct *leader = NULL; 35710bc58a91SEric W. Biederman struct task_struct *task; 35721da177e4SLinus Torvalds int retval = -ENOENT; 35731da177e4SLinus Torvalds ino_t ino; 35740bc58a91SEric W. Biederman int tid; 3575b488893aSPavel Emelyanov struct pid_namespace *ns; 35761da177e4SLinus Torvalds 35777d895244SGuillaume Chazarain task = get_proc_task(inode); 35787d895244SGuillaume Chazarain if (!task) 35797d895244SGuillaume Chazarain goto out_no_task; 35807d895244SGuillaume Chazarain rcu_read_lock(); 35817d895244SGuillaume Chazarain if (pid_alive(task)) { 35827d895244SGuillaume Chazarain leader = task->group_leader; 35837d895244SGuillaume Chazarain get_task_struct(leader); 35847d895244SGuillaume Chazarain } 35857d895244SGuillaume Chazarain rcu_read_unlock(); 35867d895244SGuillaume Chazarain put_task_struct(task); 358799f89551SEric W. Biederman if (!leader) 358899f89551SEric W. Biederman goto out_no_task; 35891da177e4SLinus Torvalds retval = 0; 35901da177e4SLinus Torvalds 3591ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 35921da177e4SLinus Torvalds case 0: 35931da177e4SLinus Torvalds ino = inode->i_ino; 3594ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 35951da177e4SLinus Torvalds goto out; 3596ee6f779bSZhang Le filp->f_pos++; 35971da177e4SLinus Torvalds /* fall through */ 35981da177e4SLinus Torvalds case 1: 35991da177e4SLinus Torvalds ino = parent_ino(dentry); 3600ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 36011da177e4SLinus Torvalds goto out; 3602ee6f779bSZhang Le filp->f_pos++; 36031da177e4SLinus Torvalds /* fall through */ 36041da177e4SLinus Torvalds } 36051da177e4SLinus Torvalds 36060bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 36070bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 36080bc58a91SEric W. Biederman */ 3609b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 36102b47c361SMathieu Desnoyers tid = (int)filp->f_version; 36110bc58a91SEric W. Biederman filp->f_version = 0; 3612ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 36130bc58a91SEric W. Biederman task; 3614ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3615b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 361661a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 36170bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 36180bc58a91SEric W. Biederman * pid for the next readir call */ 36192b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 36200bc58a91SEric W. Biederman put_task_struct(task); 36211da177e4SLinus Torvalds break; 36220bc58a91SEric W. Biederman } 36231da177e4SLinus Torvalds } 36241da177e4SLinus Torvalds out: 362599f89551SEric W. Biederman put_task_struct(leader); 362699f89551SEric W. Biederman out_no_task: 36271da177e4SLinus Torvalds return retval; 36281da177e4SLinus Torvalds } 36296e66b52bSEric W. Biederman 36306e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 36316e66b52bSEric W. Biederman { 36326e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 363399f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 36346e66b52bSEric W. Biederman generic_fillattr(inode, stat); 36356e66b52bSEric W. Biederman 363699f89551SEric W. Biederman if (p) { 363799f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 363899f89551SEric W. Biederman put_task_struct(p); 36396e66b52bSEric W. Biederman } 36406e66b52bSEric W. Biederman 36416e66b52bSEric W. Biederman return 0; 36426e66b52bSEric W. Biederman } 364328a6d671SEric W. Biederman 3644c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 364528a6d671SEric W. Biederman .lookup = proc_task_lookup, 364628a6d671SEric W. Biederman .getattr = proc_task_getattr, 364728a6d671SEric W. Biederman .setattr = proc_setattr, 36480499680aSVasiliy Kulikov .permission = proc_pid_permission, 364928a6d671SEric W. Biederman }; 365028a6d671SEric W. Biederman 365100977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 365228a6d671SEric W. Biederman .read = generic_read_dir, 365328a6d671SEric W. Biederman .readdir = proc_task_readdir, 36546038f373SArnd Bergmann .llseek = default_llseek, 365528a6d671SEric W. Biederman }; 3656