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> 7687ebdc00SAndrew Morton #include <linux/printk.h> 77a424316cSPaul Menage #include <linux/cgroup.h> 781da177e4SLinus Torvalds #include <linux/cpuset.h> 791da177e4SLinus Torvalds #include <linux/audit.h> 805addc5ddSAl Viro #include <linux/poll.h> 811651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 828ac773b4SAlexey Dobriyan #include <linux/oom.h> 833cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8460347f67SPavel Emelyanov #include <linux/pid_namespace.h> 8522d917d8SEric W. Biederman #include <linux/user_namespace.h> 865ad4e53bSAl Viro #include <linux/fs_struct.h> 875a0e3ad6STejun Heo #include <linux/slab.h> 88640708a2SPavel Emelyanov #include <linux/flex_array.h> 8948f6a7a5SPavel Emelyanov #include <linux/posix-timers.h> 90f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 91f133eccaSChris Metcalf #include <asm/hardwall.h> 92f133eccaSChris Metcalf #endif 9343d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h> 941da177e4SLinus Torvalds #include "internal.h" 95faf60af1SCyrill Gorcunov #include "fd.h" 961da177e4SLinus Torvalds 970f2fe20fSEric W. Biederman /* NOTE: 980f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 990f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 1000f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 1010f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 1020f2fe20fSEric W. Biederman * 1030f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 1040f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 1050f2fe20fSEric W. Biederman */ 1060f2fe20fSEric W. Biederman 1071da177e4SLinus Torvalds struct pid_entry { 108cedbccabSAlexey Dobriyan const char *name; 109c5141e6dSEric Dumazet int len; 110d161a13fSAl Viro umode_t mode; 111c5ef1c42SArjan van de Ven const struct inode_operations *iop; 11200977a59SArjan van de Ven const struct file_operations *fop; 11320cdc894SEric W. Biederman union proc_op op; 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 11661a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 11720cdc894SEric W. Biederman .name = (NAME), \ 118c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11920cdc894SEric W. Biederman .mode = MODE, \ 12020cdc894SEric W. Biederman .iop = IOP, \ 12120cdc894SEric W. Biederman .fop = FOP, \ 12220cdc894SEric W. Biederman .op = OP, \ 12320cdc894SEric W. Biederman } 12420cdc894SEric W. Biederman 125631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 126631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 127631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 12861a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12920cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 130631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 131631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 132631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 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 138aed54175SVegard Nossum /* 139aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 140aed54175SVegard Nossum * and .. links. 141aed54175SVegard Nossum */ 142aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 143aed54175SVegard Nossum unsigned int n) 144aed54175SVegard Nossum { 145aed54175SVegard Nossum unsigned int i; 146aed54175SVegard Nossum unsigned int count; 147aed54175SVegard Nossum 148aed54175SVegard Nossum count = 0; 149aed54175SVegard Nossum for (i = 0; i < n; ++i) { 150aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 151aed54175SVegard Nossum ++count; 152aed54175SVegard Nossum } 153aed54175SVegard Nossum 154aed54175SVegard Nossum return count; 155aed54175SVegard Nossum } 156aed54175SVegard Nossum 157f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1581da177e4SLinus Torvalds { 1597c2c7d99SHugh Dickins int result = -ENOENT; 1607c2c7d99SHugh Dickins 1610494f6ecSMiklos Szeredi task_lock(task); 162f7ad3c6bSMiklos Szeredi if (task->fs) { 163f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1647c2c7d99SHugh Dickins result = 0; 1657c2c7d99SHugh Dickins } 1660494f6ecSMiklos Szeredi task_unlock(task); 1677c2c7d99SHugh Dickins return result; 1680494f6ecSMiklos Szeredi } 1690494f6ecSMiklos Szeredi 1707773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path) 1710494f6ecSMiklos Szeredi { 1727773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1730494f6ecSMiklos Szeredi int result = -ENOENT; 17499f89551SEric W. Biederman 17599f89551SEric W. Biederman if (task) { 176f7ad3c6bSMiklos Szeredi task_lock(task); 177f7ad3c6bSMiklos Szeredi if (task->fs) { 178f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 179f7ad3c6bSMiklos Szeredi result = 0; 180f7ad3c6bSMiklos Szeredi } 181f7ad3c6bSMiklos Szeredi task_unlock(task); 18299f89551SEric W. Biederman put_task_struct(task); 18399f89551SEric W. Biederman } 1841da177e4SLinus Torvalds return result; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1877773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path) 1881da177e4SLinus Torvalds { 1897773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1901da177e4SLinus Torvalds int result = -ENOENT; 19199f89551SEric W. Biederman 19299f89551SEric W. Biederman if (task) { 193f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 19499f89551SEric W. Biederman put_task_struct(task); 19599f89551SEric W. Biederman } 1961da177e4SLinus Torvalds return result; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1992ca66ff7SAlexey Dobriyan static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns, 2002ca66ff7SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2011da177e4SLinus Torvalds { 2022ca66ff7SAlexey Dobriyan /* 2032ca66ff7SAlexey Dobriyan * Rely on struct seq_operations::show() being called once 2042ca66ff7SAlexey Dobriyan * per internal buffer allocation. See single_open(), traverse(). 2052ca66ff7SAlexey Dobriyan */ 2062ca66ff7SAlexey Dobriyan BUG_ON(m->size < PAGE_SIZE); 2072ca66ff7SAlexey Dobriyan m->count += get_cmdline(task, m->buf, PAGE_SIZE); 2082ca66ff7SAlexey Dobriyan return 0; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 211f9ea536eSAlexey Dobriyan static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns, 212f9ea536eSAlexey Dobriyan struct pid *pid, struct task_struct *task) 2131da177e4SLinus Torvalds { 214e7dcd999SCong Wang struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ); 2152fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 2161da177e4SLinus Torvalds unsigned int nwords = 0; 217dfe6b7d9SHannes Eder do { 2181da177e4SLinus Torvalds nwords += 2; 219dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 220f9ea536eSAlexey Dobriyan seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0])); 2211da177e4SLinus Torvalds mmput(mm); 222f9ea536eSAlexey Dobriyan return 0; 223f9ea536eSAlexey Dobriyan } else 224f9ea536eSAlexey Dobriyan return PTR_ERR(mm); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2311da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2321da177e4SLinus Torvalds */ 233edfcd606SAlexey Dobriyan static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, 234edfcd606SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2351da177e4SLinus Torvalds { 236ffb45122SAlexey Dobriyan unsigned long wchan; 2379281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds wchan = get_wchan(task); 2401da177e4SLinus Torvalds 241*25ce3191SJoe Perches if (lookup_symbol_name(wchan, symname) < 0) { 242f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 243f83ce3e6SJake Edge return 0; 244*25ce3191SJoe Perches seq_printf(m, "%lu", wchan); 245*25ce3191SJoe Perches } else { 246*25ce3191SJoe Perches seq_printf(m, "%s", symname); 247*25ce3191SJoe Perches } 248*25ce3191SJoe Perches 249*25ce3191SJoe Perches return 0; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2521da177e4SLinus Torvalds 253a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 254a9712bc1SAl Viro { 255a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 256a9712bc1SAl Viro if (err) 257a9712bc1SAl Viro return err; 258a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 259a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 260a9712bc1SAl Viro return -EPERM; 261a9712bc1SAl Viro } 262a9712bc1SAl Viro return 0; 263a9712bc1SAl Viro } 264a9712bc1SAl Viro 265a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 266a9712bc1SAl Viro { 267a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 268a9712bc1SAl Viro } 269a9712bc1SAl Viro 2702ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2712ec220e2SKen Chen 2722ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 2732ec220e2SKen Chen 2742ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 2752ec220e2SKen Chen struct pid *pid, struct task_struct *task) 2762ec220e2SKen Chen { 2772ec220e2SKen Chen struct stack_trace trace; 2782ec220e2SKen Chen unsigned long *entries; 279a9712bc1SAl Viro int err; 2802ec220e2SKen Chen int i; 2812ec220e2SKen Chen 2822ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 2832ec220e2SKen Chen if (!entries) 2842ec220e2SKen Chen return -ENOMEM; 2852ec220e2SKen Chen 2862ec220e2SKen Chen trace.nr_entries = 0; 2872ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 2882ec220e2SKen Chen trace.entries = entries; 2892ec220e2SKen Chen trace.skip = 0; 290a9712bc1SAl Viro 291a9712bc1SAl Viro err = lock_trace(task); 292a9712bc1SAl Viro if (!err) { 2932ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 2942ec220e2SKen Chen 2952ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 29651e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 2972ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 2982ec220e2SKen Chen } 299a9712bc1SAl Viro unlock_trace(task); 300a9712bc1SAl Viro } 3012ec220e2SKen Chen kfree(entries); 3022ec220e2SKen Chen 303a9712bc1SAl Viro return err; 3042ec220e2SKen Chen } 3052ec220e2SKen Chen #endif 3062ec220e2SKen Chen 3071da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3081da177e4SLinus Torvalds /* 3091da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3101da177e4SLinus Torvalds */ 311f6e826caSAlexey Dobriyan static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, 312f6e826caSAlexey Dobriyan struct pid *pid, struct task_struct *task) 3131da177e4SLinus Torvalds { 314*25ce3191SJoe Perches seq_printf(m, "%llu %llu %lu\n", 315826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 316826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3172d72376bSIngo Molnar task->sched_info.pcount); 318*25ce3191SJoe Perches 319*25ce3191SJoe Perches return 0; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds #endif 3221da177e4SLinus Torvalds 3239745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3249745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3259745512cSArjan van de Ven { 3269745512cSArjan van de Ven int i; 32713d77c37SHiroshi Shimamoto struct inode *inode = m->private; 32813d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3299745512cSArjan van de Ven 33013d77c37SHiroshi Shimamoto if (!task) 33113d77c37SHiroshi Shimamoto return -ESRCH; 33213d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3339745512cSArjan van de Ven for (i = 0; i < 32; i++) { 33434e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 33534e49d4fSJoe Perches if (lr->backtrace[0]) { 3369745512cSArjan van de Ven int q; 3379745512cSArjan van de Ven seq_printf(m, "%i %li %li", 33834e49d4fSJoe Perches lr->count, lr->time, lr->max); 3399745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 34034e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 34134e49d4fSJoe Perches if (!bt) 3429745512cSArjan van de Ven break; 34334e49d4fSJoe Perches if (bt == ULONG_MAX) 3449745512cSArjan van de Ven break; 34534e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 3469745512cSArjan van de Ven } 3479d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 3489745512cSArjan van de Ven } 3499745512cSArjan van de Ven 3509745512cSArjan van de Ven } 35113d77c37SHiroshi Shimamoto put_task_struct(task); 3529745512cSArjan van de Ven return 0; 3539745512cSArjan van de Ven } 3549745512cSArjan van de Ven 3559745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3569745512cSArjan van de Ven { 35713d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 358d6643d12SHiroshi Shimamoto } 359d6643d12SHiroshi Shimamoto 3609745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3619745512cSArjan van de Ven size_t count, loff_t *offs) 3629745512cSArjan van de Ven { 363496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 3649745512cSArjan van de Ven 36513d77c37SHiroshi Shimamoto if (!task) 36613d77c37SHiroshi Shimamoto return -ESRCH; 3679745512cSArjan van de Ven clear_all_latency_tracing(task); 36813d77c37SHiroshi Shimamoto put_task_struct(task); 3699745512cSArjan van de Ven 3709745512cSArjan van de Ven return count; 3719745512cSArjan van de Ven } 3729745512cSArjan van de Ven 3739745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3749745512cSArjan van de Ven .open = lstats_open, 3759745512cSArjan van de Ven .read = seq_read, 3769745512cSArjan van de Ven .write = lstats_write, 3779745512cSArjan van de Ven .llseek = seq_lseek, 37813d77c37SHiroshi Shimamoto .release = single_release, 3799745512cSArjan van de Ven }; 3809745512cSArjan van de Ven 3819745512cSArjan van de Ven #endif 3829745512cSArjan van de Ven 3836ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, 3846ba51e37SAlexey Dobriyan struct pid *pid, struct task_struct *task) 3851da177e4SLinus Torvalds { 386a7f638f9SDavid Rientjes unsigned long totalpages = totalram_pages + total_swap_pages; 387b95c35e7SOleg Nesterov unsigned long points = 0; 3881da177e4SLinus Torvalds 38919c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 390b95c35e7SOleg Nesterov if (pid_alive(task)) 391a7f638f9SDavid Rientjes points = oom_badness(task, NULL, NULL, totalpages) * 392a7f638f9SDavid Rientjes 1000 / totalpages; 39319c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 394*25ce3191SJoe Perches seq_printf(m, "%lu\n", points); 395*25ce3191SJoe Perches 396*25ce3191SJoe Perches return 0; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 399d85f50d5SNeil Horman struct limit_names { 400cedbccabSAlexey Dobriyan const char *name; 401cedbccabSAlexey Dobriyan const char *unit; 402d85f50d5SNeil Horman }; 403d85f50d5SNeil Horman 404d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 405cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 406d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 407d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 408d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 409d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 410d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 411d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 412d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 413d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 414d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 415d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 416d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 417d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 418d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 419d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4208808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 421d85f50d5SNeil Horman }; 422d85f50d5SNeil Horman 423d85f50d5SNeil Horman /* Display limits for a process */ 4241c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, 4251c963eb1SAlexey Dobriyan struct pid *pid, struct task_struct *task) 426d85f50d5SNeil Horman { 427d85f50d5SNeil Horman unsigned int i; 428d85f50d5SNeil Horman unsigned long flags; 429d85f50d5SNeil Horman 430d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 431d85f50d5SNeil Horman 432a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 433d85f50d5SNeil Horman return 0; 434d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 435d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 436d85f50d5SNeil Horman 437d85f50d5SNeil Horman /* 438d85f50d5SNeil Horman * print the file header 439d85f50d5SNeil Horman */ 4401c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20s %-20s %-10s\n", 441d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 442d85f50d5SNeil Horman 443d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 444d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 4451c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20s ", 446d85f50d5SNeil Horman lnames[i].name, "unlimited"); 447d85f50d5SNeil Horman else 4481c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20lu ", 449d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 450d85f50d5SNeil Horman 451d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 4521c963eb1SAlexey Dobriyan seq_printf(m, "%-20s ", "unlimited"); 453d85f50d5SNeil Horman else 4541c963eb1SAlexey Dobriyan seq_printf(m, "%-20lu ", rlim[i].rlim_max); 455d85f50d5SNeil Horman 456d85f50d5SNeil Horman if (lnames[i].unit) 4571c963eb1SAlexey Dobriyan seq_printf(m, "%-10s\n", lnames[i].unit); 458d85f50d5SNeil Horman else 4591c963eb1SAlexey Dobriyan seq_putc(m, '\n'); 460d85f50d5SNeil Horman } 461d85f50d5SNeil Horman 4621c963eb1SAlexey Dobriyan return 0; 463d85f50d5SNeil Horman } 464d85f50d5SNeil Horman 465ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 46609d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, 46709d93bd6SAlexey Dobriyan struct pid *pid, struct task_struct *task) 468ebcb6734SRoland McGrath { 469ebcb6734SRoland McGrath long nr; 470ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 471*25ce3191SJoe Perches int res; 472*25ce3191SJoe Perches 473*25ce3191SJoe Perches res = lock_trace(task); 474a9712bc1SAl Viro if (res) 475a9712bc1SAl Viro return res; 476ebcb6734SRoland McGrath 477ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 47809d93bd6SAlexey Dobriyan seq_puts(m, "running\n"); 479a9712bc1SAl Viro else if (nr < 0) 48009d93bd6SAlexey Dobriyan seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 481a9712bc1SAl Viro else 48209d93bd6SAlexey Dobriyan seq_printf(m, 483ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 484ebcb6734SRoland McGrath nr, 485ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 486ebcb6734SRoland McGrath sp, pc); 487a9712bc1SAl Viro unlock_trace(task); 488*25ce3191SJoe Perches 489*25ce3191SJoe Perches return 0; 490ebcb6734SRoland McGrath } 491ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 492ebcb6734SRoland McGrath 4931da177e4SLinus Torvalds /************************************************************************/ 4941da177e4SLinus Torvalds /* Here the fs part begins */ 4951da177e4SLinus Torvalds /************************************************************************/ 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /* permission checks */ 498778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 4991da177e4SLinus Torvalds { 500778c1144SEric W. Biederman struct task_struct *task; 501778c1144SEric W. Biederman int allowed = 0; 502df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 503df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 504df26c40eSEric W. Biederman * information. 505778c1144SEric W. Biederman */ 506778c1144SEric W. Biederman task = get_proc_task(inode); 507df26c40eSEric W. Biederman if (task) { 508006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 509778c1144SEric W. Biederman put_task_struct(task); 510df26c40eSEric W. Biederman } 511778c1144SEric W. Biederman return allowed; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5146b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 5156d76fa58SLinus Torvalds { 5166d76fa58SLinus Torvalds int error; 5176d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5186d76fa58SLinus Torvalds 5196d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5206d76fa58SLinus Torvalds return -EPERM; 5216d76fa58SLinus Torvalds 5226d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5231025774cSChristoph Hellwig if (error) 5246d76fa58SLinus Torvalds return error; 5251025774cSChristoph Hellwig 5261025774cSChristoph Hellwig setattr_copy(inode, attr); 5271025774cSChristoph Hellwig mark_inode_dirty(inode); 5281025774cSChristoph Hellwig return 0; 5296d76fa58SLinus Torvalds } 5306d76fa58SLinus Torvalds 5310499680aSVasiliy Kulikov /* 5320499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1) 5330499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)? 5340499680aSVasiliy Kulikov */ 5350499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid, 5360499680aSVasiliy Kulikov struct task_struct *task, 5370499680aSVasiliy Kulikov int hide_pid_min) 5380499680aSVasiliy Kulikov { 5390499680aSVasiliy Kulikov if (pid->hide_pid < hide_pid_min) 5400499680aSVasiliy Kulikov return true; 5410499680aSVasiliy Kulikov if (in_group_p(pid->pid_gid)) 5420499680aSVasiliy Kulikov return true; 5430499680aSVasiliy Kulikov return ptrace_may_access(task, PTRACE_MODE_READ); 5440499680aSVasiliy Kulikov } 5450499680aSVasiliy Kulikov 5460499680aSVasiliy Kulikov 5470499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask) 5480499680aSVasiliy Kulikov { 5490499680aSVasiliy Kulikov struct pid_namespace *pid = inode->i_sb->s_fs_info; 5500499680aSVasiliy Kulikov struct task_struct *task; 5510499680aSVasiliy Kulikov bool has_perms; 5520499680aSVasiliy Kulikov 5530499680aSVasiliy Kulikov task = get_proc_task(inode); 554a2ef990aSXiaotian Feng if (!task) 555a2ef990aSXiaotian Feng return -ESRCH; 5560499680aSVasiliy Kulikov has_perms = has_pid_permissions(pid, task, 1); 5570499680aSVasiliy Kulikov put_task_struct(task); 5580499680aSVasiliy Kulikov 5590499680aSVasiliy Kulikov if (!has_perms) { 5600499680aSVasiliy Kulikov if (pid->hide_pid == 2) { 5610499680aSVasiliy Kulikov /* 5620499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open() 5630499680aSVasiliy Kulikov * consistent with each other. If a process 5640499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen 5650499680aSVasiliy Kulikov * in procfs at all. 5660499680aSVasiliy Kulikov */ 5670499680aSVasiliy Kulikov return -ENOENT; 5680499680aSVasiliy Kulikov } 5690499680aSVasiliy Kulikov 5700499680aSVasiliy Kulikov return -EPERM; 5710499680aSVasiliy Kulikov } 5720499680aSVasiliy Kulikov return generic_permission(inode, mask); 5730499680aSVasiliy Kulikov } 5740499680aSVasiliy Kulikov 5750499680aSVasiliy Kulikov 5760499680aSVasiliy Kulikov 577c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5786d76fa58SLinus Torvalds .setattr = proc_setattr, 5796d76fa58SLinus Torvalds }; 5806d76fa58SLinus Torvalds 581be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 582be614086SEric W. Biederman { 583be614086SEric W. Biederman struct inode *inode = m->private; 584be614086SEric W. Biederman struct pid_namespace *ns; 585be614086SEric W. Biederman struct pid *pid; 586be614086SEric W. Biederman struct task_struct *task; 587be614086SEric W. Biederman int ret; 588be614086SEric W. Biederman 589be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 590be614086SEric W. Biederman pid = proc_pid(inode); 591be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 592be614086SEric W. Biederman if (!task) 593be614086SEric W. Biederman return -ESRCH; 594be614086SEric W. Biederman 595be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 596be614086SEric W. Biederman 597be614086SEric W. Biederman put_task_struct(task); 598be614086SEric W. Biederman return ret; 599be614086SEric W. Biederman } 600be614086SEric W. Biederman 601be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 602be614086SEric W. Biederman { 603c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 604be614086SEric W. Biederman } 605be614086SEric W. Biederman 606be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 607be614086SEric W. Biederman .open = proc_single_open, 608be614086SEric W. Biederman .read = seq_read, 609be614086SEric W. Biederman .llseek = seq_lseek, 610be614086SEric W. Biederman .release = single_release, 611be614086SEric W. Biederman }; 612be614086SEric W. Biederman 6135381e169SOleg Nesterov 6145381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode) 6151da177e4SLinus Torvalds { 6165381e169SOleg Nesterov struct task_struct *task = get_proc_task(inode); 6175381e169SOleg Nesterov struct mm_struct *mm = ERR_PTR(-ESRCH); 618e268337dSLinus Torvalds 6195381e169SOleg Nesterov if (task) { 620b409e578SCong Wang mm = mm_access(task, mode); 621e268337dSLinus Torvalds put_task_struct(task); 622e268337dSLinus Torvalds 6235381e169SOleg Nesterov if (!IS_ERR_OR_NULL(mm)) { 6246d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */ 6256d08f2c7SOleg Nesterov atomic_inc(&mm->mm_count); 6266d08f2c7SOleg Nesterov /* but do not pin its memory */ 6276d08f2c7SOleg Nesterov mmput(mm); 6286d08f2c7SOleg Nesterov } 6295381e169SOleg Nesterov } 6305381e169SOleg Nesterov 6315381e169SOleg Nesterov return mm; 6325381e169SOleg Nesterov } 6335381e169SOleg Nesterov 6345381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) 6355381e169SOleg Nesterov { 6365381e169SOleg Nesterov struct mm_struct *mm = proc_mem_open(inode, mode); 6375381e169SOleg Nesterov 6385381e169SOleg Nesterov if (IS_ERR(mm)) 6395381e169SOleg Nesterov return PTR_ERR(mm); 6406d08f2c7SOleg Nesterov 641e268337dSLinus Torvalds file->private_data = mm; 6421da177e4SLinus Torvalds return 0; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 645b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file) 646b409e578SCong Wang { 647bc452b4bSDjalal Harouni int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); 648bc452b4bSDjalal Harouni 649bc452b4bSDjalal Harouni /* OK to pass negative loff_t, we can catch out-of-range */ 650bc452b4bSDjalal Harouni file->f_mode |= FMODE_UNSIGNED_OFFSET; 651bc452b4bSDjalal Harouni 652bc452b4bSDjalal Harouni return ret; 653b409e578SCong Wang } 654b409e578SCong Wang 655572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf, 656572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write) 6571da177e4SLinus Torvalds { 658e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 659572d34b9SOleg Nesterov unsigned long addr = *ppos; 660572d34b9SOleg Nesterov ssize_t copied; 6611da177e4SLinus Torvalds char *page; 6621da177e4SLinus Torvalds 663e268337dSLinus Torvalds if (!mm) 664e268337dSLinus Torvalds return 0; 6651da177e4SLinus Torvalds 666e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 6671da177e4SLinus Torvalds if (!page) 668e268337dSLinus Torvalds return -ENOMEM; 6691da177e4SLinus Torvalds 670f7ca54f4SFrederik Deweerdt copied = 0; 6716d08f2c7SOleg Nesterov if (!atomic_inc_not_zero(&mm->mm_users)) 6726d08f2c7SOleg Nesterov goto free; 6736d08f2c7SOleg Nesterov 6741da177e4SLinus Torvalds while (count > 0) { 675572d34b9SOleg Nesterov int this_len = min_t(int, count, PAGE_SIZE); 6761da177e4SLinus Torvalds 677572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) { 6781da177e4SLinus Torvalds copied = -EFAULT; 6791da177e4SLinus Torvalds break; 6801da177e4SLinus Torvalds } 681572d34b9SOleg Nesterov 682572d34b9SOleg Nesterov this_len = access_remote_vm(mm, addr, page, this_len, write); 683572d34b9SOleg Nesterov if (!this_len) { 6841da177e4SLinus Torvalds if (!copied) 6851da177e4SLinus Torvalds copied = -EIO; 6861da177e4SLinus Torvalds break; 6871da177e4SLinus Torvalds } 688572d34b9SOleg Nesterov 689572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) { 690572d34b9SOleg Nesterov copied = -EFAULT; 691572d34b9SOleg Nesterov break; 6921da177e4SLinus Torvalds } 693572d34b9SOleg Nesterov 694572d34b9SOleg Nesterov buf += this_len; 695572d34b9SOleg Nesterov addr += this_len; 696572d34b9SOleg Nesterov copied += this_len; 697572d34b9SOleg Nesterov count -= this_len; 698572d34b9SOleg Nesterov } 699572d34b9SOleg Nesterov *ppos = addr; 70030cd8903SKOSAKI Motohiro 7016d08f2c7SOleg Nesterov mmput(mm); 7026d08f2c7SOleg Nesterov free: 70330cd8903SKOSAKI Motohiro free_page((unsigned long) page); 7041da177e4SLinus Torvalds return copied; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 707572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf, 708572d34b9SOleg Nesterov size_t count, loff_t *ppos) 709572d34b9SOleg Nesterov { 710572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0); 711572d34b9SOleg Nesterov } 712572d34b9SOleg Nesterov 713572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf, 714572d34b9SOleg Nesterov size_t count, loff_t *ppos) 715572d34b9SOleg Nesterov { 716572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1); 717572d34b9SOleg Nesterov } 718572d34b9SOleg Nesterov 71985863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds switch (orig) { 7221da177e4SLinus Torvalds case 0: 7231da177e4SLinus Torvalds file->f_pos = offset; 7241da177e4SLinus Torvalds break; 7251da177e4SLinus Torvalds case 1: 7261da177e4SLinus Torvalds file->f_pos += offset; 7271da177e4SLinus Torvalds break; 7281da177e4SLinus Torvalds default: 7291da177e4SLinus Torvalds return -EINVAL; 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds force_successful_syscall_return(); 7321da177e4SLinus Torvalds return file->f_pos; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 735e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file) 736e268337dSLinus Torvalds { 737e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 73871879d3cSOleg Nesterov if (mm) 7396d08f2c7SOleg Nesterov mmdrop(mm); 740e268337dSLinus Torvalds return 0; 741e268337dSLinus Torvalds } 742e268337dSLinus Torvalds 74300977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 7441da177e4SLinus Torvalds .llseek = mem_lseek, 7451da177e4SLinus Torvalds .read = mem_read, 7461da177e4SLinus Torvalds .write = mem_write, 7471da177e4SLinus Torvalds .open = mem_open, 748e268337dSLinus Torvalds .release = mem_release, 7491da177e4SLinus Torvalds }; 7501da177e4SLinus Torvalds 751b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file) 752b409e578SCong Wang { 753b409e578SCong Wang return __mem_open(inode, file, PTRACE_MODE_READ); 754b409e578SCong Wang } 755b409e578SCong Wang 756315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 757315e28c8SJames Pearson size_t count, loff_t *ppos) 758315e28c8SJames Pearson { 759315e28c8SJames Pearson char *page; 760315e28c8SJames Pearson unsigned long src = *ppos; 761b409e578SCong Wang int ret = 0; 762b409e578SCong Wang struct mm_struct *mm = file->private_data; 763315e28c8SJames Pearson 764b409e578SCong Wang if (!mm) 765b409e578SCong Wang return 0; 766315e28c8SJames Pearson 767315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 768315e28c8SJames Pearson if (!page) 769b409e578SCong Wang return -ENOMEM; 770315e28c8SJames Pearson 771d6f64b89SAl Viro ret = 0; 772b409e578SCong Wang if (!atomic_inc_not_zero(&mm->mm_users)) 773b409e578SCong Wang goto free; 774315e28c8SJames Pearson while (count > 0) { 775e8905ec2SDjalal Harouni size_t this_len, max_len; 776e8905ec2SDjalal Harouni int retval; 777e8905ec2SDjalal Harouni 778e8905ec2SDjalal Harouni if (src >= (mm->env_end - mm->env_start)) 779e8905ec2SDjalal Harouni break; 780315e28c8SJames Pearson 781315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 782315e28c8SJames Pearson 783e8905ec2SDjalal Harouni max_len = min_t(size_t, PAGE_SIZE, count); 784e8905ec2SDjalal Harouni this_len = min(max_len, this_len); 785315e28c8SJames Pearson 786b409e578SCong Wang retval = access_remote_vm(mm, (mm->env_start + src), 787315e28c8SJames Pearson page, this_len, 0); 788315e28c8SJames Pearson 789315e28c8SJames Pearson if (retval <= 0) { 790315e28c8SJames Pearson ret = retval; 791315e28c8SJames Pearson break; 792315e28c8SJames Pearson } 793315e28c8SJames Pearson 794315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 795315e28c8SJames Pearson ret = -EFAULT; 796315e28c8SJames Pearson break; 797315e28c8SJames Pearson } 798315e28c8SJames Pearson 799315e28c8SJames Pearson ret += retval; 800315e28c8SJames Pearson src += retval; 801315e28c8SJames Pearson buf += retval; 802315e28c8SJames Pearson count -= retval; 803315e28c8SJames Pearson } 804315e28c8SJames Pearson *ppos = src; 805315e28c8SJames Pearson mmput(mm); 806b409e578SCong Wang 807b409e578SCong Wang free: 808315e28c8SJames Pearson free_page((unsigned long) page); 809315e28c8SJames Pearson return ret; 810315e28c8SJames Pearson } 811315e28c8SJames Pearson 812315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 813b409e578SCong Wang .open = environ_open, 814315e28c8SJames Pearson .read = environ_read, 81587df8424SArnd Bergmann .llseek = generic_file_llseek, 816b409e578SCong Wang .release = mem_release, 817315e28c8SJames Pearson }; 818315e28c8SJames Pearson 819fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, 820fa0cbbf1SDavid Rientjes loff_t *ppos) 821fa0cbbf1SDavid Rientjes { 822496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 823fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 824fa0cbbf1SDavid Rientjes int oom_adj = OOM_ADJUST_MIN; 825fa0cbbf1SDavid Rientjes size_t len; 826fa0cbbf1SDavid Rientjes unsigned long flags; 827fa0cbbf1SDavid Rientjes 828fa0cbbf1SDavid Rientjes if (!task) 829fa0cbbf1SDavid Rientjes return -ESRCH; 830fa0cbbf1SDavid Rientjes if (lock_task_sighand(task, &flags)) { 831fa0cbbf1SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) 832fa0cbbf1SDavid Rientjes oom_adj = OOM_ADJUST_MAX; 833fa0cbbf1SDavid Rientjes else 834fa0cbbf1SDavid Rientjes oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / 835fa0cbbf1SDavid Rientjes OOM_SCORE_ADJ_MAX; 836fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 837fa0cbbf1SDavid Rientjes } 838fa0cbbf1SDavid Rientjes put_task_struct(task); 839fa0cbbf1SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); 840fa0cbbf1SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 841fa0cbbf1SDavid Rientjes } 842fa0cbbf1SDavid Rientjes 843fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf, 844fa0cbbf1SDavid Rientjes size_t count, loff_t *ppos) 845fa0cbbf1SDavid Rientjes { 846fa0cbbf1SDavid Rientjes struct task_struct *task; 847fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 848fa0cbbf1SDavid Rientjes int oom_adj; 849fa0cbbf1SDavid Rientjes unsigned long flags; 850fa0cbbf1SDavid Rientjes int err; 851fa0cbbf1SDavid Rientjes 852fa0cbbf1SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 853fa0cbbf1SDavid Rientjes if (count > sizeof(buffer) - 1) 854fa0cbbf1SDavid Rientjes count = sizeof(buffer) - 1; 855fa0cbbf1SDavid Rientjes if (copy_from_user(buffer, buf, count)) { 856fa0cbbf1SDavid Rientjes err = -EFAULT; 857fa0cbbf1SDavid Rientjes goto out; 858fa0cbbf1SDavid Rientjes } 859fa0cbbf1SDavid Rientjes 860fa0cbbf1SDavid Rientjes err = kstrtoint(strstrip(buffer), 0, &oom_adj); 861fa0cbbf1SDavid Rientjes if (err) 862fa0cbbf1SDavid Rientjes goto out; 863fa0cbbf1SDavid Rientjes if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && 864fa0cbbf1SDavid Rientjes oom_adj != OOM_DISABLE) { 865fa0cbbf1SDavid Rientjes err = -EINVAL; 866fa0cbbf1SDavid Rientjes goto out; 867fa0cbbf1SDavid Rientjes } 868fa0cbbf1SDavid Rientjes 869496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 870fa0cbbf1SDavid Rientjes if (!task) { 871fa0cbbf1SDavid Rientjes err = -ESRCH; 872fa0cbbf1SDavid Rientjes goto out; 873fa0cbbf1SDavid Rientjes } 874fa0cbbf1SDavid Rientjes 875fa0cbbf1SDavid Rientjes task_lock(task); 876fa0cbbf1SDavid Rientjes if (!task->mm) { 877fa0cbbf1SDavid Rientjes err = -EINVAL; 878fa0cbbf1SDavid Rientjes goto err_task_lock; 879fa0cbbf1SDavid Rientjes } 880fa0cbbf1SDavid Rientjes 881fa0cbbf1SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 882fa0cbbf1SDavid Rientjes err = -ESRCH; 883fa0cbbf1SDavid Rientjes goto err_task_lock; 884fa0cbbf1SDavid Rientjes } 885fa0cbbf1SDavid Rientjes 886fa0cbbf1SDavid Rientjes /* 887fa0cbbf1SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 888fa0cbbf1SDavid Rientjes * value is always attainable. 889fa0cbbf1SDavid Rientjes */ 890fa0cbbf1SDavid Rientjes if (oom_adj == OOM_ADJUST_MAX) 891fa0cbbf1SDavid Rientjes oom_adj = OOM_SCORE_ADJ_MAX; 892fa0cbbf1SDavid Rientjes else 893fa0cbbf1SDavid Rientjes oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; 894fa0cbbf1SDavid Rientjes 895fa0cbbf1SDavid Rientjes if (oom_adj < task->signal->oom_score_adj && 896fa0cbbf1SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 897fa0cbbf1SDavid Rientjes err = -EACCES; 898fa0cbbf1SDavid Rientjes goto err_sighand; 899fa0cbbf1SDavid Rientjes } 900fa0cbbf1SDavid Rientjes 901fa0cbbf1SDavid Rientjes /* 902fa0cbbf1SDavid Rientjes * /proc/pid/oom_adj is provided for legacy purposes, ask users to use 903fa0cbbf1SDavid Rientjes * /proc/pid/oom_score_adj instead. 904fa0cbbf1SDavid Rientjes */ 90587ebdc00SAndrew Morton pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 906fa0cbbf1SDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 907fa0cbbf1SDavid Rientjes task_pid_nr(task)); 908fa0cbbf1SDavid Rientjes 909fa0cbbf1SDavid Rientjes task->signal->oom_score_adj = oom_adj; 910fa0cbbf1SDavid Rientjes trace_oom_score_adj_update(task); 911fa0cbbf1SDavid Rientjes err_sighand: 912fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 913fa0cbbf1SDavid Rientjes err_task_lock: 914fa0cbbf1SDavid Rientjes task_unlock(task); 915fa0cbbf1SDavid Rientjes put_task_struct(task); 916fa0cbbf1SDavid Rientjes out: 917fa0cbbf1SDavid Rientjes return err < 0 ? err : count; 918fa0cbbf1SDavid Rientjes } 919fa0cbbf1SDavid Rientjes 920fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = { 921fa0cbbf1SDavid Rientjes .read = oom_adj_read, 922fa0cbbf1SDavid Rientjes .write = oom_adj_write, 923fa0cbbf1SDavid Rientjes .llseek = generic_file_llseek, 924fa0cbbf1SDavid Rientjes }; 925fa0cbbf1SDavid Rientjes 926a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 927a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 928a63d83f4SDavid Rientjes { 929496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 930a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 931a9c58b90SDavid Rientjes short oom_score_adj = OOM_SCORE_ADJ_MIN; 932a63d83f4SDavid Rientjes unsigned long flags; 933a63d83f4SDavid Rientjes size_t len; 934a63d83f4SDavid Rientjes 935a63d83f4SDavid Rientjes if (!task) 936a63d83f4SDavid Rientjes return -ESRCH; 937a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 938a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 939a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 940a63d83f4SDavid Rientjes } 941a63d83f4SDavid Rientjes put_task_struct(task); 942a9c58b90SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); 943a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 944a63d83f4SDavid Rientjes } 945a63d83f4SDavid Rientjes 946a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 947a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 948a63d83f4SDavid Rientjes { 949a63d83f4SDavid Rientjes struct task_struct *task; 950a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 951a63d83f4SDavid Rientjes unsigned long flags; 9520a8cb8e3SAlexey Dobriyan int oom_score_adj; 953a63d83f4SDavid Rientjes int err; 954a63d83f4SDavid Rientjes 955a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 956a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 957a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 958723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 959723548bfSDavid Rientjes err = -EFAULT; 960723548bfSDavid Rientjes goto out; 961723548bfSDavid Rientjes } 962a63d83f4SDavid Rientjes 9630a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 964a63d83f4SDavid Rientjes if (err) 965723548bfSDavid Rientjes goto out; 966a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 967723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 968723548bfSDavid Rientjes err = -EINVAL; 969723548bfSDavid Rientjes goto out; 970723548bfSDavid Rientjes } 971a63d83f4SDavid Rientjes 972496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 973723548bfSDavid Rientjes if (!task) { 974723548bfSDavid Rientjes err = -ESRCH; 975723548bfSDavid Rientjes goto out; 976723548bfSDavid Rientjes } 977a63d83f4SDavid Rientjes 9783d5992d2SYing Han task_lock(task); 9793d5992d2SYing Han if (!task->mm) { 980723548bfSDavid Rientjes err = -EINVAL; 981723548bfSDavid Rientjes goto err_task_lock; 9823d5992d2SYing Han } 983d19d5476SDavid Rientjes 984d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 985d19d5476SDavid Rientjes err = -ESRCH; 986d19d5476SDavid Rientjes goto err_task_lock; 987d19d5476SDavid Rientjes } 988d19d5476SDavid Rientjes 989a9c58b90SDavid Rientjes if ((short)oom_score_adj < task->signal->oom_score_adj_min && 990d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 991d19d5476SDavid Rientjes err = -EACCES; 992d19d5476SDavid Rientjes goto err_sighand; 993d19d5476SDavid Rientjes } 994d19d5476SDavid Rientjes 995a9c58b90SDavid Rientjes task->signal->oom_score_adj = (short)oom_score_adj; 996dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 997a9c58b90SDavid Rientjes task->signal->oom_score_adj_min = (short)oom_score_adj; 99843d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 99901dc52ebSDavidlohr Bueso 1000723548bfSDavid Rientjes err_sighand: 1001a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1002d19d5476SDavid Rientjes err_task_lock: 1003d19d5476SDavid Rientjes task_unlock(task); 1004a63d83f4SDavid Rientjes put_task_struct(task); 1005723548bfSDavid Rientjes out: 1006723548bfSDavid Rientjes return err < 0 ? err : count; 1007a63d83f4SDavid Rientjes } 1008a63d83f4SDavid Rientjes 1009a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1010a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1011a63d83f4SDavid Rientjes .write = oom_score_adj_write, 10126038f373SArnd Bergmann .llseek = default_llseek, 1013a63d83f4SDavid Rientjes }; 1014a63d83f4SDavid Rientjes 10151da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10161da177e4SLinus Torvalds #define TMPBUFLEN 21 10171da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10181da177e4SLinus Torvalds size_t count, loff_t *ppos) 10191da177e4SLinus Torvalds { 1020496ad9aaSAl Viro struct inode * inode = file_inode(file); 102199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10221da177e4SLinus Torvalds ssize_t length; 10231da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10241da177e4SLinus Torvalds 102599f89551SEric W. Biederman if (!task) 102699f89551SEric W. Biederman return -ESRCH; 10271da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1028e1760bd5SEric W. Biederman from_kuid(file->f_cred->user_ns, 1029e1760bd5SEric W. Biederman audit_get_loginuid(task))); 103099f89551SEric W. Biederman put_task_struct(task); 10311da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10351da177e4SLinus Torvalds size_t count, loff_t *ppos) 10361da177e4SLinus Torvalds { 1037496ad9aaSAl Viro struct inode * inode = file_inode(file); 10381da177e4SLinus Torvalds char *page, *tmp; 10391da177e4SLinus Torvalds ssize_t length; 10401da177e4SLinus Torvalds uid_t loginuid; 1041e1760bd5SEric W. Biederman kuid_t kloginuid; 10421da177e4SLinus Torvalds 10437dc52157SPaul E. McKenney rcu_read_lock(); 10447dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 10457dc52157SPaul E. McKenney rcu_read_unlock(); 10461da177e4SLinus Torvalds return -EPERM; 10477dc52157SPaul E. McKenney } 10487dc52157SPaul E. McKenney rcu_read_unlock(); 10491da177e4SLinus Torvalds 1050e0182909SAl Viro if (count >= PAGE_SIZE) 1051e0182909SAl Viro count = PAGE_SIZE - 1; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds if (*ppos != 0) { 10541da177e4SLinus Torvalds /* No partial writes. */ 10551da177e4SLinus Torvalds return -EINVAL; 10561da177e4SLinus Torvalds } 1057e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10581da177e4SLinus Torvalds if (!page) 10591da177e4SLinus Torvalds return -ENOMEM; 10601da177e4SLinus Torvalds length = -EFAULT; 10611da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10621da177e4SLinus Torvalds goto out_free_page; 10631da177e4SLinus Torvalds 1064e0182909SAl Viro page[count] = '\0'; 10651da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10661da177e4SLinus Torvalds if (tmp == page) { 10671da177e4SLinus Torvalds length = -EINVAL; 10681da177e4SLinus Torvalds goto out_free_page; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds } 107181407c84SEric Paris 107281407c84SEric Paris /* is userspace tring to explicitly UNSET the loginuid? */ 107381407c84SEric Paris if (loginuid == AUDIT_UID_UNSET) { 107481407c84SEric Paris kloginuid = INVALID_UID; 107581407c84SEric Paris } else { 1076e1760bd5SEric W. Biederman kloginuid = make_kuid(file->f_cred->user_ns, loginuid); 1077e1760bd5SEric W. Biederman if (!uid_valid(kloginuid)) { 1078e1760bd5SEric W. Biederman length = -EINVAL; 1079e1760bd5SEric W. Biederman goto out_free_page; 1080e1760bd5SEric W. Biederman } 108181407c84SEric Paris } 1082e1760bd5SEric W. Biederman 1083e1760bd5SEric W. Biederman length = audit_set_loginuid(kloginuid); 10841da177e4SLinus Torvalds if (likely(length == 0)) 10851da177e4SLinus Torvalds length = count; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds out_free_page: 10881da177e4SLinus Torvalds free_page((unsigned long) page); 10891da177e4SLinus Torvalds return length; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 109200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10931da177e4SLinus Torvalds .read = proc_loginuid_read, 10941da177e4SLinus Torvalds .write = proc_loginuid_write, 109587df8424SArnd Bergmann .llseek = generic_file_llseek, 10961da177e4SLinus Torvalds }; 10971e0bd755SEric Paris 10981e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 10991e0bd755SEric Paris size_t count, loff_t *ppos) 11001e0bd755SEric Paris { 1101496ad9aaSAl Viro struct inode * inode = file_inode(file); 11021e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11031e0bd755SEric Paris ssize_t length; 11041e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11051e0bd755SEric Paris 11061e0bd755SEric Paris if (!task) 11071e0bd755SEric Paris return -ESRCH; 11081e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11091e0bd755SEric Paris audit_get_sessionid(task)); 11101e0bd755SEric Paris put_task_struct(task); 11111e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11121e0bd755SEric Paris } 11131e0bd755SEric Paris 11141e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11151e0bd755SEric Paris .read = proc_sessionid_read, 111687df8424SArnd Bergmann .llseek = generic_file_llseek, 11171e0bd755SEric Paris }; 11181da177e4SLinus Torvalds #endif 11191da177e4SLinus Torvalds 1120f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1121f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1122f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1123f4f154fdSAkinobu Mita { 1124496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 1125f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1126f4f154fdSAkinobu Mita size_t len; 1127f4f154fdSAkinobu Mita int make_it_fail; 1128f4f154fdSAkinobu Mita 1129f4f154fdSAkinobu Mita if (!task) 1130f4f154fdSAkinobu Mita return -ESRCH; 1131f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1132f4f154fdSAkinobu Mita put_task_struct(task); 1133f4f154fdSAkinobu Mita 1134f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11350c28f287SAkinobu Mita 11360c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1137f4f154fdSAkinobu Mita } 1138f4f154fdSAkinobu Mita 1139f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1140f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1141f4f154fdSAkinobu Mita { 1142f4f154fdSAkinobu Mita struct task_struct *task; 1143f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1144f4f154fdSAkinobu Mita int make_it_fail; 1145f4f154fdSAkinobu Mita 1146f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1147f4f154fdSAkinobu Mita return -EPERM; 1148f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1149f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1150f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1151f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1152f4f154fdSAkinobu Mita return -EFAULT; 1153cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1154cba8aafeSVincent Li if (*end) 1155cba8aafeSVincent Li return -EINVAL; 115616caed31SDave Jones if (make_it_fail < 0 || make_it_fail > 1) 115716caed31SDave Jones return -EINVAL; 115816caed31SDave Jones 1159496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 1160f4f154fdSAkinobu Mita if (!task) 1161f4f154fdSAkinobu Mita return -ESRCH; 1162f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1163f4f154fdSAkinobu Mita put_task_struct(task); 1164cba8aafeSVincent Li 1165cba8aafeSVincent Li return count; 1166f4f154fdSAkinobu Mita } 1167f4f154fdSAkinobu Mita 116800977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1169f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1170f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 117187df8424SArnd Bergmann .llseek = generic_file_llseek, 1172f4f154fdSAkinobu Mita }; 1173f4f154fdSAkinobu Mita #endif 1174f4f154fdSAkinobu Mita 11759745512cSArjan van de Ven 117643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 117743ae34cbSIngo Molnar /* 117843ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 117943ae34cbSIngo Molnar */ 118043ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 118143ae34cbSIngo Molnar { 118243ae34cbSIngo Molnar struct inode *inode = m->private; 118343ae34cbSIngo Molnar struct task_struct *p; 118443ae34cbSIngo Molnar 118543ae34cbSIngo Molnar p = get_proc_task(inode); 118643ae34cbSIngo Molnar if (!p) 118743ae34cbSIngo Molnar return -ESRCH; 118843ae34cbSIngo Molnar proc_sched_show_task(p, m); 118943ae34cbSIngo Molnar 119043ae34cbSIngo Molnar put_task_struct(p); 119143ae34cbSIngo Molnar 119243ae34cbSIngo Molnar return 0; 119343ae34cbSIngo Molnar } 119443ae34cbSIngo Molnar 119543ae34cbSIngo Molnar static ssize_t 119643ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 119743ae34cbSIngo Molnar size_t count, loff_t *offset) 119843ae34cbSIngo Molnar { 1199496ad9aaSAl Viro struct inode *inode = file_inode(file); 120043ae34cbSIngo Molnar struct task_struct *p; 120143ae34cbSIngo Molnar 120243ae34cbSIngo Molnar p = get_proc_task(inode); 120343ae34cbSIngo Molnar if (!p) 120443ae34cbSIngo Molnar return -ESRCH; 120543ae34cbSIngo Molnar proc_sched_set_task(p); 120643ae34cbSIngo Molnar 120743ae34cbSIngo Molnar put_task_struct(p); 120843ae34cbSIngo Molnar 120943ae34cbSIngo Molnar return count; 121043ae34cbSIngo Molnar } 121143ae34cbSIngo Molnar 121243ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 121343ae34cbSIngo Molnar { 1214c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 121543ae34cbSIngo Molnar } 121643ae34cbSIngo Molnar 121743ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 121843ae34cbSIngo Molnar .open = sched_open, 121943ae34cbSIngo Molnar .read = seq_read, 122043ae34cbSIngo Molnar .write = sched_write, 122143ae34cbSIngo Molnar .llseek = seq_lseek, 12225ea473a1SAlexey Dobriyan .release = single_release, 122343ae34cbSIngo Molnar }; 122443ae34cbSIngo Molnar 122543ae34cbSIngo Molnar #endif 122643ae34cbSIngo Molnar 12275091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 12285091faa4SMike Galbraith /* 12295091faa4SMike Galbraith * Print out autogroup related information: 12305091faa4SMike Galbraith */ 12315091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 12325091faa4SMike Galbraith { 12335091faa4SMike Galbraith struct inode *inode = m->private; 12345091faa4SMike Galbraith struct task_struct *p; 12355091faa4SMike Galbraith 12365091faa4SMike Galbraith p = get_proc_task(inode); 12375091faa4SMike Galbraith if (!p) 12385091faa4SMike Galbraith return -ESRCH; 12395091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 12405091faa4SMike Galbraith 12415091faa4SMike Galbraith put_task_struct(p); 12425091faa4SMike Galbraith 12435091faa4SMike Galbraith return 0; 12445091faa4SMike Galbraith } 12455091faa4SMike Galbraith 12465091faa4SMike Galbraith static ssize_t 12475091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 12485091faa4SMike Galbraith size_t count, loff_t *offset) 12495091faa4SMike Galbraith { 1250496ad9aaSAl Viro struct inode *inode = file_inode(file); 12515091faa4SMike Galbraith struct task_struct *p; 12525091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 12530a8cb8e3SAlexey Dobriyan int nice; 12545091faa4SMike Galbraith int err; 12555091faa4SMike Galbraith 12565091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 12575091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 12585091faa4SMike Galbraith count = sizeof(buffer) - 1; 12595091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 12605091faa4SMike Galbraith return -EFAULT; 12615091faa4SMike Galbraith 12620a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 12630a8cb8e3SAlexey Dobriyan if (err < 0) 12640a8cb8e3SAlexey Dobriyan return err; 12655091faa4SMike Galbraith 12665091faa4SMike Galbraith p = get_proc_task(inode); 12675091faa4SMike Galbraith if (!p) 12685091faa4SMike Galbraith return -ESRCH; 12695091faa4SMike Galbraith 12702e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice); 12715091faa4SMike Galbraith if (err) 12725091faa4SMike Galbraith count = err; 12735091faa4SMike Galbraith 12745091faa4SMike Galbraith put_task_struct(p); 12755091faa4SMike Galbraith 12765091faa4SMike Galbraith return count; 12775091faa4SMike Galbraith } 12785091faa4SMike Galbraith 12795091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 12805091faa4SMike Galbraith { 12815091faa4SMike Galbraith int ret; 12825091faa4SMike Galbraith 12835091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 12845091faa4SMike Galbraith if (!ret) { 12855091faa4SMike Galbraith struct seq_file *m = filp->private_data; 12865091faa4SMike Galbraith 12875091faa4SMike Galbraith m->private = inode; 12885091faa4SMike Galbraith } 12895091faa4SMike Galbraith return ret; 12905091faa4SMike Galbraith } 12915091faa4SMike Galbraith 12925091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 12935091faa4SMike Galbraith .open = sched_autogroup_open, 12945091faa4SMike Galbraith .read = seq_read, 12955091faa4SMike Galbraith .write = sched_autogroup_write, 12965091faa4SMike Galbraith .llseek = seq_lseek, 12975091faa4SMike Galbraith .release = single_release, 12985091faa4SMike Galbraith }; 12995091faa4SMike Galbraith 13005091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 13015091faa4SMike Galbraith 13024614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 13034614a696Sjohn stultz size_t count, loff_t *offset) 13044614a696Sjohn stultz { 1305496ad9aaSAl Viro struct inode *inode = file_inode(file); 13064614a696Sjohn stultz struct task_struct *p; 13074614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 1308830e0fc9SDavid Rientjes const size_t maxlen = sizeof(buffer) - 1; 13094614a696Sjohn stultz 13104614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 1311830e0fc9SDavid Rientjes if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) 13124614a696Sjohn stultz return -EFAULT; 13134614a696Sjohn stultz 13144614a696Sjohn stultz p = get_proc_task(inode); 13154614a696Sjohn stultz if (!p) 13164614a696Sjohn stultz return -ESRCH; 13174614a696Sjohn stultz 13184614a696Sjohn stultz if (same_thread_group(current, p)) 13194614a696Sjohn stultz set_task_comm(p, buffer); 13204614a696Sjohn stultz else 13214614a696Sjohn stultz count = -EINVAL; 13224614a696Sjohn stultz 13234614a696Sjohn stultz put_task_struct(p); 13244614a696Sjohn stultz 13254614a696Sjohn stultz return count; 13264614a696Sjohn stultz } 13274614a696Sjohn stultz 13284614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 13294614a696Sjohn stultz { 13304614a696Sjohn stultz struct inode *inode = m->private; 13314614a696Sjohn stultz struct task_struct *p; 13324614a696Sjohn stultz 13334614a696Sjohn stultz p = get_proc_task(inode); 13344614a696Sjohn stultz if (!p) 13354614a696Sjohn stultz return -ESRCH; 13364614a696Sjohn stultz 13374614a696Sjohn stultz task_lock(p); 13384614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 13394614a696Sjohn stultz task_unlock(p); 13404614a696Sjohn stultz 13414614a696Sjohn stultz put_task_struct(p); 13424614a696Sjohn stultz 13434614a696Sjohn stultz return 0; 13444614a696Sjohn stultz } 13454614a696Sjohn stultz 13464614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 13474614a696Sjohn stultz { 1348c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 13494614a696Sjohn stultz } 13504614a696Sjohn stultz 13514614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 13524614a696Sjohn stultz .open = comm_open, 13534614a696Sjohn stultz .read = seq_read, 13544614a696Sjohn stultz .write = comm_write, 13554614a696Sjohn stultz .llseek = seq_lseek, 13564614a696Sjohn stultz .release = single_release, 13574614a696Sjohn stultz }; 13584614a696Sjohn stultz 13597773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1360925d1c40SMatt Helsley { 1361925d1c40SMatt Helsley struct task_struct *task; 1362925d1c40SMatt Helsley struct mm_struct *mm; 1363925d1c40SMatt Helsley struct file *exe_file; 1364925d1c40SMatt Helsley 13657773fbc5SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 1366925d1c40SMatt Helsley if (!task) 1367925d1c40SMatt Helsley return -ENOENT; 1368925d1c40SMatt Helsley mm = get_task_mm(task); 1369925d1c40SMatt Helsley put_task_struct(task); 1370925d1c40SMatt Helsley if (!mm) 1371925d1c40SMatt Helsley return -ENOENT; 1372925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1373925d1c40SMatt Helsley mmput(mm); 1374925d1c40SMatt Helsley if (exe_file) { 1375925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1376925d1c40SMatt Helsley path_get(&exe_file->f_path); 1377925d1c40SMatt Helsley fput(exe_file); 1378925d1c40SMatt Helsley return 0; 1379925d1c40SMatt Helsley } else 1380925d1c40SMatt Helsley return -ENOENT; 1381925d1c40SMatt Helsley } 1382925d1c40SMatt Helsley 1383008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1386408ef013SChristoph Hellwig struct path path; 13871da177e4SLinus Torvalds int error = -EACCES; 13881da177e4SLinus Torvalds 1389778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1390778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13911da177e4SLinus Torvalds goto out; 13921da177e4SLinus Torvalds 1393408ef013SChristoph Hellwig error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1394408ef013SChristoph Hellwig if (error) 1395408ef013SChristoph Hellwig goto out; 1396408ef013SChristoph Hellwig 1397b5fb63c1SChristoph Hellwig nd_jump_link(nd, &path); 1398408ef013SChristoph Hellwig return NULL; 13991da177e4SLinus Torvalds out: 1400008b150aSAl Viro return ERR_PTR(error); 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14033dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 14041da177e4SLinus Torvalds { 1405e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 14063dcd25f3SJan Blunck char *pathname; 14071da177e4SLinus Torvalds int len; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds if (!tmp) 14101da177e4SLinus Torvalds return -ENOMEM; 14111da177e4SLinus Torvalds 14127b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 14133dcd25f3SJan Blunck len = PTR_ERR(pathname); 14143dcd25f3SJan Blunck if (IS_ERR(pathname)) 14151da177e4SLinus Torvalds goto out; 14163dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds if (len > buflen) 14191da177e4SLinus Torvalds len = buflen; 14203dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 14211da177e4SLinus Torvalds len = -EFAULT; 14221da177e4SLinus Torvalds out: 14231da177e4SLinus Torvalds free_page((unsigned long)tmp); 14241da177e4SLinus Torvalds return len; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds int error = -EACCES; 14301da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14313dcd25f3SJan Blunck struct path path; 14321da177e4SLinus Torvalds 1433778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1434778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14351da177e4SLinus Torvalds goto out; 14361da177e4SLinus Torvalds 14377773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path); 14381da177e4SLinus Torvalds if (error) 14391da177e4SLinus Torvalds goto out; 14401da177e4SLinus Torvalds 14413dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14423dcd25f3SJan Blunck path_put(&path); 14431da177e4SLinus Torvalds out: 14441da177e4SLinus Torvalds return error; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 1447faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = { 14481da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14496d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14506d76fa58SLinus Torvalds .setattr = proc_setattr, 14511da177e4SLinus Torvalds }; 14521da177e4SLinus Torvalds 145328a6d671SEric W. Biederman 145428a6d671SEric W. Biederman /* building an inode */ 145528a6d671SEric W. Biederman 14566b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 145728a6d671SEric W. Biederman { 145828a6d671SEric W. Biederman struct inode * inode; 145928a6d671SEric W. Biederman struct proc_inode *ei; 1460c69e8d9cSDavid Howells const struct cred *cred; 146128a6d671SEric W. Biederman 146228a6d671SEric W. Biederman /* We need a new inode */ 146328a6d671SEric W. Biederman 146428a6d671SEric W. Biederman inode = new_inode(sb); 146528a6d671SEric W. Biederman if (!inode) 146628a6d671SEric W. Biederman goto out; 146728a6d671SEric W. Biederman 146828a6d671SEric W. Biederman /* Common stuff */ 146928a6d671SEric W. Biederman ei = PROC_I(inode); 147085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 147128a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 147228a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 147328a6d671SEric W. Biederman 147428a6d671SEric W. Biederman /* 147528a6d671SEric W. Biederman * grab the reference to task. 147628a6d671SEric W. Biederman */ 14771a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 147828a6d671SEric W. Biederman if (!ei->pid) 147928a6d671SEric W. Biederman goto out_unlock; 148028a6d671SEric W. Biederman 148128a6d671SEric W. Biederman if (task_dumpable(task)) { 1482c69e8d9cSDavid Howells rcu_read_lock(); 1483c69e8d9cSDavid Howells cred = __task_cred(task); 1484c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1485c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1486c69e8d9cSDavid Howells rcu_read_unlock(); 148728a6d671SEric W. Biederman } 148828a6d671SEric W. Biederman security_task_to_inode(task, inode); 148928a6d671SEric W. Biederman 149028a6d671SEric W. Biederman out: 149128a6d671SEric W. Biederman return inode; 149228a6d671SEric W. Biederman 149328a6d671SEric W. Biederman out_unlock: 149428a6d671SEric W. Biederman iput(inode); 149528a6d671SEric W. Biederman return NULL; 149628a6d671SEric W. Biederman } 149728a6d671SEric W. Biederman 14986b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 149928a6d671SEric W. Biederman { 150028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 150128a6d671SEric W. Biederman struct task_struct *task; 1502c69e8d9cSDavid Howells const struct cred *cred; 15030499680aSVasiliy Kulikov struct pid_namespace *pid = dentry->d_sb->s_fs_info; 1504c69e8d9cSDavid Howells 150528a6d671SEric W. Biederman generic_fillattr(inode, stat); 150628a6d671SEric W. Biederman 150728a6d671SEric W. Biederman rcu_read_lock(); 1508dcb0f222SEric W. Biederman stat->uid = GLOBAL_ROOT_UID; 1509dcb0f222SEric W. Biederman stat->gid = GLOBAL_ROOT_GID; 151028a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 151128a6d671SEric W. Biederman if (task) { 15120499680aSVasiliy Kulikov if (!has_pid_permissions(pid, task, 2)) { 15130499680aSVasiliy Kulikov rcu_read_unlock(); 15140499680aSVasiliy Kulikov /* 15150499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists, 15160499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir(). 15170499680aSVasiliy Kulikov */ 15180499680aSVasiliy Kulikov return -ENOENT; 15190499680aSVasiliy Kulikov } 152028a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 152128a6d671SEric W. Biederman task_dumpable(task)) { 1522c69e8d9cSDavid Howells cred = __task_cred(task); 1523c69e8d9cSDavid Howells stat->uid = cred->euid; 1524c69e8d9cSDavid Howells stat->gid = cred->egid; 152528a6d671SEric W. Biederman } 152628a6d671SEric W. Biederman } 152728a6d671SEric W. Biederman rcu_read_unlock(); 152828a6d671SEric W. Biederman return 0; 152928a6d671SEric W. Biederman } 153028a6d671SEric W. Biederman 153128a6d671SEric W. Biederman /* dentry stuff */ 153228a6d671SEric W. Biederman 153328a6d671SEric W. Biederman /* 153428a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 153528a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 153628a6d671SEric W. Biederman * due to the way we treat inodes. 153728a6d671SEric W. Biederman * 153828a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 153928a6d671SEric W. Biederman * performed a setuid(), etc. 154028a6d671SEric W. Biederman * 154128a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 154228a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 154328a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 154428a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 154528a6d671SEric W. Biederman * made this apply to all per process world readable and executable 154628a6d671SEric W. Biederman * directories. 154728a6d671SEric W. Biederman */ 15480b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags) 154928a6d671SEric W. Biederman { 155034286d66SNick Piggin struct inode *inode; 155134286d66SNick Piggin struct task_struct *task; 1552c69e8d9cSDavid Howells const struct cred *cred; 1553c69e8d9cSDavid Howells 15540b728e19SAl Viro if (flags & LOOKUP_RCU) 155534286d66SNick Piggin return -ECHILD; 155634286d66SNick Piggin 155734286d66SNick Piggin inode = dentry->d_inode; 155834286d66SNick Piggin task = get_proc_task(inode); 155934286d66SNick Piggin 156028a6d671SEric W. Biederman if (task) { 156128a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 156228a6d671SEric W. Biederman task_dumpable(task)) { 1563c69e8d9cSDavid Howells rcu_read_lock(); 1564c69e8d9cSDavid Howells cred = __task_cred(task); 1565c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1566c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1567c69e8d9cSDavid Howells rcu_read_unlock(); 156828a6d671SEric W. Biederman } else { 1569dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1570dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 157128a6d671SEric W. Biederman } 157228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 157328a6d671SEric W. Biederman security_task_to_inode(task, inode); 157428a6d671SEric W. Biederman put_task_struct(task); 157528a6d671SEric W. Biederman return 1; 157628a6d671SEric W. Biederman } 157728a6d671SEric W. Biederman return 0; 157828a6d671SEric W. Biederman } 157928a6d671SEric W. Biederman 1580d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode) 1581d855a4b7SOleg Nesterov { 1582d855a4b7SOleg Nesterov return !proc_pid(inode)->tasks[PIDTYPE_PID].first; 1583d855a4b7SOleg Nesterov } 1584d855a4b7SOleg Nesterov 15851dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry) 15861dd704b6SDavid Howells { 15871dd704b6SDavid Howells /* Is the task we represent dead? 15881dd704b6SDavid Howells * If so, then don't put the dentry on the lru list, 15891dd704b6SDavid Howells * kill it immediately. 15901dd704b6SDavid Howells */ 1591d855a4b7SOleg Nesterov return proc_inode_is_dead(dentry->d_inode); 15921dd704b6SDavid Howells } 15931dd704b6SDavid Howells 15946b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 159528a6d671SEric W. Biederman { 159628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 159728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 159828a6d671SEric W. Biederman }; 159928a6d671SEric W. Biederman 160028a6d671SEric W. Biederman /* Lookups */ 160128a6d671SEric W. Biederman 16021c0d04c9SEric W. Biederman /* 16031c0d04c9SEric W. Biederman * Fill a directory entry. 16041c0d04c9SEric W. Biederman * 16051c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 16061c0d04c9SEric W. Biederman * file type from dcache entry. 16071c0d04c9SEric W. Biederman * 16081c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 16091c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 16101c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 16111c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 16121c0d04c9SEric W. Biederman * by stat. 16131c0d04c9SEric W. Biederman */ 1614f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx, 16156b4e306aSEric W. Biederman const char *name, int len, 1616c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 161761a28784SEric W. Biederman { 1618f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry; 16191df98b8bSAl Viro struct qstr qname = QSTR_INIT(name, len); 162061a28784SEric W. Biederman struct inode *inode; 16211df98b8bSAl Viro unsigned type; 16221df98b8bSAl Viro ino_t ino; 162361a28784SEric W. Biederman 16241df98b8bSAl Viro child = d_hash_and_lookup(dir, &qname); 162561a28784SEric W. Biederman if (!child) { 16261df98b8bSAl Viro child = d_alloc(dir, &qname); 16271df98b8bSAl Viro if (!child) 162861a28784SEric W. Biederman goto end_instantiate; 16291df98b8bSAl Viro if (instantiate(dir->d_inode, child, task, ptr) < 0) { 16301df98b8bSAl Viro dput(child); 16311df98b8bSAl Viro goto end_instantiate; 16321df98b8bSAl Viro } 16331df98b8bSAl Viro } 163461a28784SEric W. Biederman inode = child->d_inode; 163561a28784SEric W. Biederman ino = inode->i_ino; 163661a28784SEric W. Biederman type = inode->i_mode >> 12; 163761a28784SEric W. Biederman dput(child); 1638f0c3b509SAl Viro return dir_emit(ctx, name, len, ino, type); 16391df98b8bSAl Viro 16401df98b8bSAl Viro end_instantiate: 16411df98b8bSAl Viro return dir_emit(ctx, name, len, 1, DT_UNKNOWN); 164261a28784SEric W. Biederman } 164361a28784SEric W. Biederman 1644640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 1645640708a2SPavel Emelyanov 1646640708a2SPavel Emelyanov /* 1647640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs 1648640708a2SPavel Emelyanov * which represent vma start and end addresses. 1649640708a2SPavel Emelyanov */ 1650640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry, 1651640708a2SPavel Emelyanov unsigned long *start, unsigned long *end) 1652640708a2SPavel Emelyanov { 1653640708a2SPavel Emelyanov if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) 1654640708a2SPavel Emelyanov return -EINVAL; 1655640708a2SPavel Emelyanov 1656640708a2SPavel Emelyanov return 0; 1657640708a2SPavel Emelyanov } 1658640708a2SPavel Emelyanov 16590b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) 1660640708a2SPavel Emelyanov { 1661640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1662640708a2SPavel Emelyanov bool exact_vma_exists = false; 1663640708a2SPavel Emelyanov struct mm_struct *mm = NULL; 1664640708a2SPavel Emelyanov struct task_struct *task; 1665640708a2SPavel Emelyanov const struct cred *cred; 1666640708a2SPavel Emelyanov struct inode *inode; 1667640708a2SPavel Emelyanov int status = 0; 1668640708a2SPavel Emelyanov 16690b728e19SAl Viro if (flags & LOOKUP_RCU) 1670640708a2SPavel Emelyanov return -ECHILD; 1671640708a2SPavel Emelyanov 1672640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) { 167341735818SZhao Hongjiang status = -EPERM; 1674640708a2SPavel Emelyanov goto out_notask; 1675640708a2SPavel Emelyanov } 1676640708a2SPavel Emelyanov 1677640708a2SPavel Emelyanov inode = dentry->d_inode; 1678640708a2SPavel Emelyanov task = get_proc_task(inode); 1679640708a2SPavel Emelyanov if (!task) 1680640708a2SPavel Emelyanov goto out_notask; 1681640708a2SPavel Emelyanov 16822344bec7SCong Wang mm = mm_access(task, PTRACE_MODE_READ); 16832344bec7SCong Wang if (IS_ERR_OR_NULL(mm)) 1684640708a2SPavel Emelyanov goto out; 1685640708a2SPavel Emelyanov 1686640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 1687640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1688640708a2SPavel Emelyanov exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); 1689640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1690640708a2SPavel Emelyanov } 1691640708a2SPavel Emelyanov 1692640708a2SPavel Emelyanov mmput(mm); 1693640708a2SPavel Emelyanov 1694640708a2SPavel Emelyanov if (exact_vma_exists) { 1695640708a2SPavel Emelyanov if (task_dumpable(task)) { 1696640708a2SPavel Emelyanov rcu_read_lock(); 1697640708a2SPavel Emelyanov cred = __task_cred(task); 1698640708a2SPavel Emelyanov inode->i_uid = cred->euid; 1699640708a2SPavel Emelyanov inode->i_gid = cred->egid; 1700640708a2SPavel Emelyanov rcu_read_unlock(); 1701640708a2SPavel Emelyanov } else { 1702dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1703dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1704640708a2SPavel Emelyanov } 1705640708a2SPavel Emelyanov security_task_to_inode(task, inode); 1706640708a2SPavel Emelyanov status = 1; 1707640708a2SPavel Emelyanov } 1708640708a2SPavel Emelyanov 1709640708a2SPavel Emelyanov out: 1710640708a2SPavel Emelyanov put_task_struct(task); 1711640708a2SPavel Emelyanov 1712640708a2SPavel Emelyanov out_notask: 1713640708a2SPavel Emelyanov return status; 1714640708a2SPavel Emelyanov } 1715640708a2SPavel Emelyanov 1716640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = { 1717640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate, 1718640708a2SPavel Emelyanov .d_delete = pid_delete_dentry, 1719640708a2SPavel Emelyanov }; 1720640708a2SPavel Emelyanov 1721640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path) 1722640708a2SPavel Emelyanov { 1723640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1724640708a2SPavel Emelyanov struct vm_area_struct *vma; 1725640708a2SPavel Emelyanov struct task_struct *task; 1726640708a2SPavel Emelyanov struct mm_struct *mm; 1727640708a2SPavel Emelyanov int rc; 1728640708a2SPavel Emelyanov 1729640708a2SPavel Emelyanov rc = -ENOENT; 1730640708a2SPavel Emelyanov task = get_proc_task(dentry->d_inode); 1731640708a2SPavel Emelyanov if (!task) 1732640708a2SPavel Emelyanov goto out; 1733640708a2SPavel Emelyanov 1734640708a2SPavel Emelyanov mm = get_task_mm(task); 1735640708a2SPavel Emelyanov put_task_struct(task); 1736640708a2SPavel Emelyanov if (!mm) 1737640708a2SPavel Emelyanov goto out; 1738640708a2SPavel Emelyanov 1739640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 1740640708a2SPavel Emelyanov if (rc) 1741640708a2SPavel Emelyanov goto out_mmput; 1742640708a2SPavel Emelyanov 174370335abbSArtem Fetishev rc = -ENOENT; 1744640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1745640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1746640708a2SPavel Emelyanov if (vma && vma->vm_file) { 1747640708a2SPavel Emelyanov *path = vma->vm_file->f_path; 1748640708a2SPavel Emelyanov path_get(path); 1749640708a2SPavel Emelyanov rc = 0; 1750640708a2SPavel Emelyanov } 1751640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1752640708a2SPavel Emelyanov 1753640708a2SPavel Emelyanov out_mmput: 1754640708a2SPavel Emelyanov mmput(mm); 1755640708a2SPavel Emelyanov out: 1756640708a2SPavel Emelyanov return rc; 1757640708a2SPavel Emelyanov } 1758640708a2SPavel Emelyanov 1759640708a2SPavel Emelyanov struct map_files_info { 17607b540d06SAl Viro fmode_t mode; 1761640708a2SPavel Emelyanov unsigned long len; 1762640708a2SPavel Emelyanov unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ 1763640708a2SPavel Emelyanov }; 1764640708a2SPavel Emelyanov 1765c52a47acSAl Viro static int 1766640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 1767640708a2SPavel Emelyanov struct task_struct *task, const void *ptr) 1768640708a2SPavel Emelyanov { 17697b540d06SAl Viro fmode_t mode = (fmode_t)(unsigned long)ptr; 1770640708a2SPavel Emelyanov struct proc_inode *ei; 1771640708a2SPavel Emelyanov struct inode *inode; 1772640708a2SPavel Emelyanov 1773640708a2SPavel Emelyanov inode = proc_pid_make_inode(dir->i_sb, task); 1774640708a2SPavel Emelyanov if (!inode) 1775c52a47acSAl Viro return -ENOENT; 1776640708a2SPavel Emelyanov 1777640708a2SPavel Emelyanov ei = PROC_I(inode); 1778640708a2SPavel Emelyanov ei->op.proc_get_link = proc_map_files_get_link; 1779640708a2SPavel Emelyanov 1780640708a2SPavel Emelyanov inode->i_op = &proc_pid_link_inode_operations; 1781640708a2SPavel Emelyanov inode->i_size = 64; 1782640708a2SPavel Emelyanov inode->i_mode = S_IFLNK; 1783640708a2SPavel Emelyanov 17847b540d06SAl Viro if (mode & FMODE_READ) 1785640708a2SPavel Emelyanov inode->i_mode |= S_IRUSR; 17867b540d06SAl Viro if (mode & FMODE_WRITE) 1787640708a2SPavel Emelyanov inode->i_mode |= S_IWUSR; 1788640708a2SPavel Emelyanov 1789640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations); 1790640708a2SPavel Emelyanov d_add(dentry, inode); 1791640708a2SPavel Emelyanov 1792c52a47acSAl Viro return 0; 1793640708a2SPavel Emelyanov } 1794640708a2SPavel Emelyanov 1795640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir, 179600cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 1797640708a2SPavel Emelyanov { 1798640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1799640708a2SPavel Emelyanov struct vm_area_struct *vma; 1800640708a2SPavel Emelyanov struct task_struct *task; 1801c52a47acSAl Viro int result; 1802640708a2SPavel Emelyanov struct mm_struct *mm; 1803640708a2SPavel Emelyanov 1804c52a47acSAl Viro result = -EPERM; 1805640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1806640708a2SPavel Emelyanov goto out; 1807640708a2SPavel Emelyanov 1808c52a47acSAl Viro result = -ENOENT; 1809640708a2SPavel Emelyanov task = get_proc_task(dir); 1810640708a2SPavel Emelyanov if (!task) 1811640708a2SPavel Emelyanov goto out; 1812640708a2SPavel Emelyanov 1813c52a47acSAl Viro result = -EACCES; 1814eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1815640708a2SPavel Emelyanov goto out_put_task; 1816640708a2SPavel Emelyanov 1817c52a47acSAl Viro result = -ENOENT; 1818640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 1819eb94cd96SCyrill Gorcunov goto out_put_task; 1820640708a2SPavel Emelyanov 1821640708a2SPavel Emelyanov mm = get_task_mm(task); 1822640708a2SPavel Emelyanov if (!mm) 1823eb94cd96SCyrill Gorcunov goto out_put_task; 1824640708a2SPavel Emelyanov 1825640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1826640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1827640708a2SPavel Emelyanov if (!vma) 1828640708a2SPavel Emelyanov goto out_no_vma; 1829640708a2SPavel Emelyanov 183005f56484SStanislav Kinsbursky if (vma->vm_file) 18317b540d06SAl Viro result = proc_map_files_instantiate(dir, dentry, task, 18327b540d06SAl Viro (void *)(unsigned long)vma->vm_file->f_mode); 1833640708a2SPavel Emelyanov 1834640708a2SPavel Emelyanov out_no_vma: 1835640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1836640708a2SPavel Emelyanov mmput(mm); 1837640708a2SPavel Emelyanov out_put_task: 1838640708a2SPavel Emelyanov put_task_struct(task); 1839640708a2SPavel Emelyanov out: 1840c52a47acSAl Viro return ERR_PTR(result); 1841640708a2SPavel Emelyanov } 1842640708a2SPavel Emelyanov 1843640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = { 1844640708a2SPavel Emelyanov .lookup = proc_map_files_lookup, 1845640708a2SPavel Emelyanov .permission = proc_fd_permission, 1846640708a2SPavel Emelyanov .setattr = proc_setattr, 1847640708a2SPavel Emelyanov }; 1848640708a2SPavel Emelyanov 1849640708a2SPavel Emelyanov static int 1850f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx) 1851640708a2SPavel Emelyanov { 1852640708a2SPavel Emelyanov struct vm_area_struct *vma; 1853640708a2SPavel Emelyanov struct task_struct *task; 1854640708a2SPavel Emelyanov struct mm_struct *mm; 1855f0c3b509SAl Viro unsigned long nr_files, pos, i; 1856f0c3b509SAl Viro struct flex_array *fa = NULL; 1857f0c3b509SAl Viro struct map_files_info info; 1858f0c3b509SAl Viro struct map_files_info *p; 1859640708a2SPavel Emelyanov int ret; 1860640708a2SPavel Emelyanov 186141735818SZhao Hongjiang ret = -EPERM; 1862640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1863640708a2SPavel Emelyanov goto out; 1864640708a2SPavel Emelyanov 1865640708a2SPavel Emelyanov ret = -ENOENT; 1866f0c3b509SAl Viro task = get_proc_task(file_inode(file)); 1867640708a2SPavel Emelyanov if (!task) 1868640708a2SPavel Emelyanov goto out; 1869640708a2SPavel Emelyanov 1870640708a2SPavel Emelyanov ret = -EACCES; 1871eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1872640708a2SPavel Emelyanov goto out_put_task; 1873640708a2SPavel Emelyanov 1874640708a2SPavel Emelyanov ret = 0; 1875f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 1876eb94cd96SCyrill Gorcunov goto out_put_task; 1877640708a2SPavel Emelyanov 1878640708a2SPavel Emelyanov mm = get_task_mm(task); 1879640708a2SPavel Emelyanov if (!mm) 1880eb94cd96SCyrill Gorcunov goto out_put_task; 1881640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1882640708a2SPavel Emelyanov 1883640708a2SPavel Emelyanov nr_files = 0; 1884640708a2SPavel Emelyanov 1885640708a2SPavel Emelyanov /* 1886640708a2SPavel Emelyanov * We need two passes here: 1887640708a2SPavel Emelyanov * 1888640708a2SPavel Emelyanov * 1) Collect vmas of mapped files with mmap_sem taken 1889640708a2SPavel Emelyanov * 2) Release mmap_sem and instantiate entries 1890640708a2SPavel Emelyanov * 1891640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir() 1892640708a2SPavel Emelyanov * routine might require mmap_sem taken in might_fault(). 1893640708a2SPavel Emelyanov */ 1894640708a2SPavel Emelyanov 1895640708a2SPavel Emelyanov for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 1896f0c3b509SAl Viro if (vma->vm_file && ++pos > ctx->pos) 1897640708a2SPavel Emelyanov nr_files++; 1898640708a2SPavel Emelyanov } 1899640708a2SPavel Emelyanov 1900640708a2SPavel Emelyanov if (nr_files) { 1901640708a2SPavel Emelyanov fa = flex_array_alloc(sizeof(info), nr_files, 1902640708a2SPavel Emelyanov GFP_KERNEL); 1903640708a2SPavel Emelyanov if (!fa || flex_array_prealloc(fa, 0, nr_files, 1904640708a2SPavel Emelyanov GFP_KERNEL)) { 1905640708a2SPavel Emelyanov ret = -ENOMEM; 1906640708a2SPavel Emelyanov if (fa) 1907640708a2SPavel Emelyanov flex_array_free(fa); 1908640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1909640708a2SPavel Emelyanov mmput(mm); 1910eb94cd96SCyrill Gorcunov goto out_put_task; 1911640708a2SPavel Emelyanov } 1912640708a2SPavel Emelyanov for (i = 0, vma = mm->mmap, pos = 2; vma; 1913640708a2SPavel Emelyanov vma = vma->vm_next) { 1914640708a2SPavel Emelyanov if (!vma->vm_file) 1915640708a2SPavel Emelyanov continue; 1916f0c3b509SAl Viro if (++pos <= ctx->pos) 1917640708a2SPavel Emelyanov continue; 1918640708a2SPavel Emelyanov 19197b540d06SAl Viro info.mode = vma->vm_file->f_mode; 1920640708a2SPavel Emelyanov info.len = snprintf(info.name, 1921640708a2SPavel Emelyanov sizeof(info.name), "%lx-%lx", 1922640708a2SPavel Emelyanov vma->vm_start, vma->vm_end); 1923640708a2SPavel Emelyanov if (flex_array_put(fa, i++, &info, GFP_KERNEL)) 1924640708a2SPavel Emelyanov BUG(); 1925640708a2SPavel Emelyanov } 1926640708a2SPavel Emelyanov } 1927640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1928640708a2SPavel Emelyanov 1929640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) { 1930640708a2SPavel Emelyanov p = flex_array_get(fa, i); 1931f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, 1932640708a2SPavel Emelyanov p->name, p->len, 1933640708a2SPavel Emelyanov proc_map_files_instantiate, 19347b540d06SAl Viro task, 1935f0c3b509SAl Viro (void *)(unsigned long)p->mode)) 1936640708a2SPavel Emelyanov break; 1937f0c3b509SAl Viro ctx->pos++; 1938640708a2SPavel Emelyanov } 1939640708a2SPavel Emelyanov if (fa) 1940640708a2SPavel Emelyanov flex_array_free(fa); 1941640708a2SPavel Emelyanov mmput(mm); 1942640708a2SPavel Emelyanov 1943640708a2SPavel Emelyanov out_put_task: 1944640708a2SPavel Emelyanov put_task_struct(task); 1945640708a2SPavel Emelyanov out: 1946640708a2SPavel Emelyanov return ret; 1947640708a2SPavel Emelyanov } 1948640708a2SPavel Emelyanov 1949640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = { 1950640708a2SPavel Emelyanov .read = generic_read_dir, 1951f0c3b509SAl Viro .iterate = proc_map_files_readdir, 1952640708a2SPavel Emelyanov .llseek = default_llseek, 1953640708a2SPavel Emelyanov }; 1954640708a2SPavel Emelyanov 195548f6a7a5SPavel Emelyanov struct timers_private { 195648f6a7a5SPavel Emelyanov struct pid *pid; 195748f6a7a5SPavel Emelyanov struct task_struct *task; 195848f6a7a5SPavel Emelyanov struct sighand_struct *sighand; 195957b8015eSPavel Emelyanov struct pid_namespace *ns; 196048f6a7a5SPavel Emelyanov unsigned long flags; 196148f6a7a5SPavel Emelyanov }; 196248f6a7a5SPavel Emelyanov 196348f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos) 196448f6a7a5SPavel Emelyanov { 196548f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 196648f6a7a5SPavel Emelyanov 196748f6a7a5SPavel Emelyanov tp->task = get_pid_task(tp->pid, PIDTYPE_PID); 196848f6a7a5SPavel Emelyanov if (!tp->task) 196948f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH); 197048f6a7a5SPavel Emelyanov 197148f6a7a5SPavel Emelyanov tp->sighand = lock_task_sighand(tp->task, &tp->flags); 197248f6a7a5SPavel Emelyanov if (!tp->sighand) 197348f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH); 197448f6a7a5SPavel Emelyanov 197548f6a7a5SPavel Emelyanov return seq_list_start(&tp->task->signal->posix_timers, *pos); 197648f6a7a5SPavel Emelyanov } 197748f6a7a5SPavel Emelyanov 197848f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos) 197948f6a7a5SPavel Emelyanov { 198048f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 198148f6a7a5SPavel Emelyanov return seq_list_next(v, &tp->task->signal->posix_timers, pos); 198248f6a7a5SPavel Emelyanov } 198348f6a7a5SPavel Emelyanov 198448f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v) 198548f6a7a5SPavel Emelyanov { 198648f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 198748f6a7a5SPavel Emelyanov 198848f6a7a5SPavel Emelyanov if (tp->sighand) { 198948f6a7a5SPavel Emelyanov unlock_task_sighand(tp->task, &tp->flags); 199048f6a7a5SPavel Emelyanov tp->sighand = NULL; 199148f6a7a5SPavel Emelyanov } 199248f6a7a5SPavel Emelyanov 199348f6a7a5SPavel Emelyanov if (tp->task) { 199448f6a7a5SPavel Emelyanov put_task_struct(tp->task); 199548f6a7a5SPavel Emelyanov tp->task = NULL; 199648f6a7a5SPavel Emelyanov } 199748f6a7a5SPavel Emelyanov } 199848f6a7a5SPavel Emelyanov 199948f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v) 200048f6a7a5SPavel Emelyanov { 200148f6a7a5SPavel Emelyanov struct k_itimer *timer; 200257b8015eSPavel Emelyanov struct timers_private *tp = m->private; 200357b8015eSPavel Emelyanov int notify; 2004cedbccabSAlexey Dobriyan static const char * const nstr[] = { 200557b8015eSPavel Emelyanov [SIGEV_SIGNAL] = "signal", 200657b8015eSPavel Emelyanov [SIGEV_NONE] = "none", 200757b8015eSPavel Emelyanov [SIGEV_THREAD] = "thread", 200857b8015eSPavel Emelyanov }; 200948f6a7a5SPavel Emelyanov 201048f6a7a5SPavel Emelyanov timer = list_entry((struct list_head *)v, struct k_itimer, list); 201157b8015eSPavel Emelyanov notify = timer->it_sigev_notify; 201257b8015eSPavel Emelyanov 201348f6a7a5SPavel Emelyanov seq_printf(m, "ID: %d\n", timer->it_id); 2014*25ce3191SJoe Perches seq_printf(m, "signal: %d/%p\n", 2015*25ce3191SJoe Perches timer->sigq->info.si_signo, 201657b8015eSPavel Emelyanov timer->sigq->info.si_value.sival_ptr); 201757b8015eSPavel Emelyanov seq_printf(m, "notify: %s/%s.%d\n", 201857b8015eSPavel Emelyanov nstr[notify & ~SIGEV_THREAD_ID], 201957b8015eSPavel Emelyanov (notify & SIGEV_THREAD_ID) ? "tid" : "pid", 202057b8015eSPavel Emelyanov pid_nr_ns(timer->it_pid, tp->ns)); 202115ef0298SPavel Tikhomirov seq_printf(m, "ClockID: %d\n", timer->it_clock); 202248f6a7a5SPavel Emelyanov 202348f6a7a5SPavel Emelyanov return 0; 202448f6a7a5SPavel Emelyanov } 202548f6a7a5SPavel Emelyanov 202648f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = { 202748f6a7a5SPavel Emelyanov .start = timers_start, 202848f6a7a5SPavel Emelyanov .next = timers_next, 202948f6a7a5SPavel Emelyanov .stop = timers_stop, 203048f6a7a5SPavel Emelyanov .show = show_timer, 203148f6a7a5SPavel Emelyanov }; 203248f6a7a5SPavel Emelyanov 203348f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file) 203448f6a7a5SPavel Emelyanov { 203548f6a7a5SPavel Emelyanov struct timers_private *tp; 203648f6a7a5SPavel Emelyanov 203748f6a7a5SPavel Emelyanov tp = __seq_open_private(file, &proc_timers_seq_ops, 203848f6a7a5SPavel Emelyanov sizeof(struct timers_private)); 203948f6a7a5SPavel Emelyanov if (!tp) 204048f6a7a5SPavel Emelyanov return -ENOMEM; 204148f6a7a5SPavel Emelyanov 204248f6a7a5SPavel Emelyanov tp->pid = proc_pid(inode); 204357b8015eSPavel Emelyanov tp->ns = inode->i_sb->s_fs_info; 204448f6a7a5SPavel Emelyanov return 0; 204548f6a7a5SPavel Emelyanov } 204648f6a7a5SPavel Emelyanov 204748f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = { 204848f6a7a5SPavel Emelyanov .open = proc_timers_open, 204948f6a7a5SPavel Emelyanov .read = seq_read, 205048f6a7a5SPavel Emelyanov .llseek = seq_lseek, 205148f6a7a5SPavel Emelyanov .release = seq_release_private, 205248f6a7a5SPavel Emelyanov }; 2053640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */ 2054640708a2SPavel Emelyanov 2055c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir, 2056c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2057444ceed8SEric W. Biederman { 2058c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2059444ceed8SEric W. Biederman struct inode *inode; 2060444ceed8SEric W. Biederman struct proc_inode *ei; 2061444ceed8SEric W. Biederman 206261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2063444ceed8SEric W. Biederman if (!inode) 2064444ceed8SEric W. Biederman goto out; 2065444ceed8SEric W. Biederman 2066444ceed8SEric W. Biederman ei = PROC_I(inode); 2067444ceed8SEric W. Biederman inode->i_mode = p->mode; 2068444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2069bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2070444ceed8SEric W. Biederman if (p->iop) 2071444ceed8SEric W. Biederman inode->i_op = p->iop; 2072444ceed8SEric W. Biederman if (p->fop) 2073444ceed8SEric W. Biederman inode->i_fop = p->fop; 2074444ceed8SEric W. Biederman ei->op = p->op; 2075fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2076444ceed8SEric W. Biederman d_add(dentry, inode); 2077444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 20780b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2079c52a47acSAl Viro return 0; 2080444ceed8SEric W. Biederman out: 2081c52a47acSAl Viro return -ENOENT; 2082444ceed8SEric W. Biederman } 2083444ceed8SEric W. Biederman 20841da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 20851da177e4SLinus Torvalds struct dentry *dentry, 2086c5141e6dSEric Dumazet const struct pid_entry *ents, 20877bcd6b0eSEric W. Biederman unsigned int nents) 20881da177e4SLinus Torvalds { 2089c52a47acSAl Viro int error; 209099f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2091c5141e6dSEric Dumazet const struct pid_entry *p, *last; 20921da177e4SLinus Torvalds 2093c52a47acSAl Viro error = -ENOENT; 20941da177e4SLinus Torvalds 209599f89551SEric W. Biederman if (!task) 209699f89551SEric W. Biederman goto out_no_task; 20971da177e4SLinus Torvalds 209820cdc894SEric W. Biederman /* 209920cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 210020cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 210120cdc894SEric W. Biederman */ 21027bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 21037bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 21041da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 21051da177e4SLinus Torvalds continue; 21061da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 21071da177e4SLinus Torvalds break; 21081da177e4SLinus Torvalds } 21097bcd6b0eSEric W. Biederman if (p > last) 21101da177e4SLinus Torvalds goto out; 21111da177e4SLinus Torvalds 2112444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 21131da177e4SLinus Torvalds out: 211499f89551SEric W. Biederman put_task_struct(task); 211599f89551SEric W. Biederman out_no_task: 2116c52a47acSAl Viro return ERR_PTR(error); 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 2119f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx, 2120c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 212128a6d671SEric W. Biederman { 2122f0c3b509SAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 2123f0c3b509SAl Viro const struct pid_entry *p; 212428a6d671SEric W. Biederman 212528a6d671SEric W. Biederman if (!task) 2126f0c3b509SAl Viro return -ENOENT; 212728a6d671SEric W. Biederman 2128f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 212928a6d671SEric W. Biederman goto out; 21301da177e4SLinus Torvalds 2131f0c3b509SAl Viro if (ctx->pos >= nents + 2) 2132f0c3b509SAl Viro goto out; 2133f0c3b509SAl Viro 2134f0c3b509SAl Viro for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) { 2135f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, p->name, p->len, 2136f0c3b509SAl Viro proc_pident_instantiate, task, p)) 2137f0c3b509SAl Viro break; 2138f0c3b509SAl Viro ctx->pos++; 2139f0c3b509SAl Viro } 214028a6d671SEric W. Biederman out: 214161a28784SEric W. Biederman put_task_struct(task); 2142f0c3b509SAl Viro return 0; 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 214628a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 214728a6d671SEric W. Biederman size_t count, loff_t *ppos) 214828a6d671SEric W. Biederman { 2149496ad9aaSAl Viro struct inode * inode = file_inode(file); 215004ff9708SAl Viro char *p = NULL; 215128a6d671SEric W. Biederman ssize_t length; 215228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 215328a6d671SEric W. Biederman 215428a6d671SEric W. Biederman if (!task) 215504ff9708SAl Viro return -ESRCH; 215628a6d671SEric W. Biederman 215728a6d671SEric W. Biederman length = security_getprocattr(task, 21582fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 215904ff9708SAl Viro &p); 216028a6d671SEric W. Biederman put_task_struct(task); 216104ff9708SAl Viro if (length > 0) 216204ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 216304ff9708SAl Viro kfree(p); 216428a6d671SEric W. Biederman return length; 216528a6d671SEric W. Biederman } 216628a6d671SEric W. Biederman 216728a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 216828a6d671SEric W. Biederman size_t count, loff_t *ppos) 216928a6d671SEric W. Biederman { 2170496ad9aaSAl Viro struct inode * inode = file_inode(file); 217128a6d671SEric W. Biederman char *page; 217228a6d671SEric W. Biederman ssize_t length; 217328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 217428a6d671SEric W. Biederman 217528a6d671SEric W. Biederman length = -ESRCH; 217628a6d671SEric W. Biederman if (!task) 217728a6d671SEric W. Biederman goto out_no_task; 217828a6d671SEric W. Biederman if (count > PAGE_SIZE) 217928a6d671SEric W. Biederman count = PAGE_SIZE; 218028a6d671SEric W. Biederman 218128a6d671SEric W. Biederman /* No partial writes. */ 218228a6d671SEric W. Biederman length = -EINVAL; 218328a6d671SEric W. Biederman if (*ppos != 0) 218428a6d671SEric W. Biederman goto out; 218528a6d671SEric W. Biederman 218628a6d671SEric W. Biederman length = -ENOMEM; 2187e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 218828a6d671SEric W. Biederman if (!page) 218928a6d671SEric W. Biederman goto out; 219028a6d671SEric W. Biederman 219128a6d671SEric W. Biederman length = -EFAULT; 219228a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 219328a6d671SEric W. Biederman goto out_free; 219428a6d671SEric W. Biederman 2195107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 21969b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2197107db7c7SDavid Howells if (length < 0) 2198107db7c7SDavid Howells goto out_free; 2199107db7c7SDavid Howells 220028a6d671SEric W. Biederman length = security_setprocattr(task, 22012fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 220228a6d671SEric W. Biederman (void*)page, count); 22039b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 220428a6d671SEric W. Biederman out_free: 220528a6d671SEric W. Biederman free_page((unsigned long) page); 220628a6d671SEric W. Biederman out: 220728a6d671SEric W. Biederman put_task_struct(task); 220828a6d671SEric W. Biederman out_no_task: 220928a6d671SEric W. Biederman return length; 221028a6d671SEric W. Biederman } 221128a6d671SEric W. Biederman 221200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 221328a6d671SEric W. Biederman .read = proc_pid_attr_read, 221428a6d671SEric W. Biederman .write = proc_pid_attr_write, 221587df8424SArnd Bergmann .llseek = generic_file_llseek, 221628a6d671SEric W. Biederman }; 221728a6d671SEric W. Biederman 2218c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2219631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2220631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2221631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2222631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2223631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2224631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 222528a6d671SEric W. Biederman }; 222628a6d671SEric W. Biederman 2227f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) 22281da177e4SLinus Torvalds { 2229f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 223072d9dcfcSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22311da177e4SLinus Torvalds } 22321da177e4SLinus Torvalds 223300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 22341da177e4SLinus Torvalds .read = generic_read_dir, 2235f0c3b509SAl Viro .iterate = proc_attr_dir_readdir, 22366038f373SArnd Bergmann .llseek = default_llseek, 22371da177e4SLinus Torvalds }; 22381da177e4SLinus Torvalds 223972d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 224000cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 22411da177e4SLinus Torvalds { 22427bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 22437bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 2246c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 224772d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 224899f89551SEric W. Biederman .getattr = pid_getattr, 22496d76fa58SLinus Torvalds .setattr = proc_setattr, 22501da177e4SLinus Torvalds }; 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds #endif 22531da177e4SLinus Torvalds 2254698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 22553cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 22563cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 22573cb4a0bbSKawai, Hidehiro { 2258496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 22593cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22603cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 22613cb4a0bbSKawai, Hidehiro size_t len; 22623cb4a0bbSKawai, Hidehiro int ret; 22633cb4a0bbSKawai, Hidehiro 22643cb4a0bbSKawai, Hidehiro if (!task) 22653cb4a0bbSKawai, Hidehiro return -ESRCH; 22663cb4a0bbSKawai, Hidehiro 22673cb4a0bbSKawai, Hidehiro ret = 0; 22683cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22693cb4a0bbSKawai, Hidehiro if (mm) { 22703cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22713cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22723cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22733cb4a0bbSKawai, Hidehiro mmput(mm); 22743cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22753cb4a0bbSKawai, Hidehiro } 22763cb4a0bbSKawai, Hidehiro 22773cb4a0bbSKawai, Hidehiro put_task_struct(task); 22783cb4a0bbSKawai, Hidehiro 22793cb4a0bbSKawai, Hidehiro return ret; 22803cb4a0bbSKawai, Hidehiro } 22813cb4a0bbSKawai, Hidehiro 22823cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22833cb4a0bbSKawai, Hidehiro const char __user *buf, 22843cb4a0bbSKawai, Hidehiro size_t count, 22853cb4a0bbSKawai, Hidehiro loff_t *ppos) 22863cb4a0bbSKawai, Hidehiro { 22873cb4a0bbSKawai, Hidehiro struct task_struct *task; 22883cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22893cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22903cb4a0bbSKawai, Hidehiro unsigned int val; 22913cb4a0bbSKawai, Hidehiro int ret; 22923cb4a0bbSKawai, Hidehiro int i; 22933cb4a0bbSKawai, Hidehiro unsigned long mask; 22943cb4a0bbSKawai, Hidehiro 22953cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22963cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22973cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 22983cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 22993cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 23003cb4a0bbSKawai, Hidehiro goto out_no_task; 23013cb4a0bbSKawai, Hidehiro 23023cb4a0bbSKawai, Hidehiro ret = -EINVAL; 23033cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 23043cb4a0bbSKawai, Hidehiro if (*end == '\n') 23053cb4a0bbSKawai, Hidehiro end++; 23063cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 23073cb4a0bbSKawai, Hidehiro goto out_no_task; 23083cb4a0bbSKawai, Hidehiro 23093cb4a0bbSKawai, Hidehiro ret = -ESRCH; 2310496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 23113cb4a0bbSKawai, Hidehiro if (!task) 23123cb4a0bbSKawai, Hidehiro goto out_no_task; 23133cb4a0bbSKawai, Hidehiro 23143cb4a0bbSKawai, Hidehiro ret = end - buffer; 23153cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 23163cb4a0bbSKawai, Hidehiro if (!mm) 23173cb4a0bbSKawai, Hidehiro goto out_no_mm; 23183cb4a0bbSKawai, Hidehiro 23193cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 23203cb4a0bbSKawai, Hidehiro if (val & mask) 23213cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23223cb4a0bbSKawai, Hidehiro else 23233cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23243cb4a0bbSKawai, Hidehiro } 23253cb4a0bbSKawai, Hidehiro 23263cb4a0bbSKawai, Hidehiro mmput(mm); 23273cb4a0bbSKawai, Hidehiro out_no_mm: 23283cb4a0bbSKawai, Hidehiro put_task_struct(task); 23293cb4a0bbSKawai, Hidehiro out_no_task: 23303cb4a0bbSKawai, Hidehiro return ret; 23313cb4a0bbSKawai, Hidehiro } 23323cb4a0bbSKawai, Hidehiro 23333cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 23343cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 23353cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 233687df8424SArnd Bergmann .llseek = generic_file_llseek, 23373cb4a0bbSKawai, Hidehiro }; 23383cb4a0bbSKawai, Hidehiro #endif 23393cb4a0bbSKawai, Hidehiro 2340aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 234119aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) 2342aba76fdbSAndrew Morton { 2343940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2344297c5d92SAndrea Righi unsigned long flags; 2345293eb1e7SVasiliy Kulikov int result; 2346297c5d92SAndrea Righi 2347293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2348293eb1e7SVasiliy Kulikov if (result) 2349293eb1e7SVasiliy Kulikov return result; 2350293eb1e7SVasiliy Kulikov 2351293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2352293eb1e7SVasiliy Kulikov result = -EACCES; 2353293eb1e7SVasiliy Kulikov goto out_unlock; 2354293eb1e7SVasiliy Kulikov } 23551d1221f3SVasiliy Kulikov 23565995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2357b2d002dbSAndrea Righi struct task_struct *t = task; 2358297c5d92SAndrea Righi 23595995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 23605995477aSAndrea Righi while_each_thread(task, t) 23615995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2362297c5d92SAndrea Righi 2363297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2364297c5d92SAndrea Righi } 2365*25ce3191SJoe Perches seq_printf(m, 2366aba76fdbSAndrew Morton "rchar: %llu\n" 2367aba76fdbSAndrew Morton "wchar: %llu\n" 2368aba76fdbSAndrew Morton "syscr: %llu\n" 2369aba76fdbSAndrew Morton "syscw: %llu\n" 2370aba76fdbSAndrew Morton "read_bytes: %llu\n" 2371aba76fdbSAndrew Morton "write_bytes: %llu\n" 2372aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 23737c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 23747c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 23757c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 23767c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 23777c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 23787c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 23797c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2380*25ce3191SJoe Perches result = 0; 2381*25ce3191SJoe Perches 2382293eb1e7SVasiliy Kulikov out_unlock: 2383293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2384293eb1e7SVasiliy Kulikov return result; 2385aba76fdbSAndrew Morton } 2386297c5d92SAndrea Righi 238719aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 238819aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2389297c5d92SAndrea Righi { 239019aadc98SAlexey Dobriyan return do_io_accounting(task, m, 0); 2391297c5d92SAndrea Righi } 2392297c5d92SAndrea Righi 239319aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 239419aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2395297c5d92SAndrea Righi { 239619aadc98SAlexey Dobriyan return do_io_accounting(task, m, 1); 2397297c5d92SAndrea Righi } 2398297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2399aba76fdbSAndrew Morton 240022d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 240122d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file, 2402ccf94f1bSFabian Frederick const struct seq_operations *seq_ops) 240322d917d8SEric W. Biederman { 240422d917d8SEric W. Biederman struct user_namespace *ns = NULL; 240522d917d8SEric W. Biederman struct task_struct *task; 240622d917d8SEric W. Biederman struct seq_file *seq; 240722d917d8SEric W. Biederman int ret = -EINVAL; 240822d917d8SEric W. Biederman 240922d917d8SEric W. Biederman task = get_proc_task(inode); 241022d917d8SEric W. Biederman if (task) { 241122d917d8SEric W. Biederman rcu_read_lock(); 241222d917d8SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 241322d917d8SEric W. Biederman rcu_read_unlock(); 241422d917d8SEric W. Biederman put_task_struct(task); 241522d917d8SEric W. Biederman } 241622d917d8SEric W. Biederman if (!ns) 241722d917d8SEric W. Biederman goto err; 241822d917d8SEric W. Biederman 241922d917d8SEric W. Biederman ret = seq_open(file, seq_ops); 242022d917d8SEric W. Biederman if (ret) 242122d917d8SEric W. Biederman goto err_put_ns; 242222d917d8SEric W. Biederman 242322d917d8SEric W. Biederman seq = file->private_data; 242422d917d8SEric W. Biederman seq->private = ns; 242522d917d8SEric W. Biederman 242622d917d8SEric W. Biederman return 0; 242722d917d8SEric W. Biederman err_put_ns: 242822d917d8SEric W. Biederman put_user_ns(ns); 242922d917d8SEric W. Biederman err: 243022d917d8SEric W. Biederman return ret; 243122d917d8SEric W. Biederman } 243222d917d8SEric W. Biederman 243322d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file) 243422d917d8SEric W. Biederman { 243522d917d8SEric W. Biederman struct seq_file *seq = file->private_data; 243622d917d8SEric W. Biederman struct user_namespace *ns = seq->private; 243722d917d8SEric W. Biederman put_user_ns(ns); 243822d917d8SEric W. Biederman return seq_release(inode, file); 243922d917d8SEric W. Biederman } 244022d917d8SEric W. Biederman 244122d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file) 244222d917d8SEric W. Biederman { 244322d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_uid_seq_operations); 244422d917d8SEric W. Biederman } 244522d917d8SEric W. Biederman 244622d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file) 244722d917d8SEric W. Biederman { 244822d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_gid_seq_operations); 244922d917d8SEric W. Biederman } 245022d917d8SEric W. Biederman 2451f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file) 2452f76d207aSEric W. Biederman { 2453f76d207aSEric W. Biederman return proc_id_map_open(inode, file, &proc_projid_seq_operations); 2454f76d207aSEric W. Biederman } 2455f76d207aSEric W. Biederman 245622d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = { 245722d917d8SEric W. Biederman .open = proc_uid_map_open, 245822d917d8SEric W. Biederman .write = proc_uid_map_write, 245922d917d8SEric W. Biederman .read = seq_read, 246022d917d8SEric W. Biederman .llseek = seq_lseek, 246122d917d8SEric W. Biederman .release = proc_id_map_release, 246222d917d8SEric W. Biederman }; 246322d917d8SEric W. Biederman 246422d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = { 246522d917d8SEric W. Biederman .open = proc_gid_map_open, 246622d917d8SEric W. Biederman .write = proc_gid_map_write, 246722d917d8SEric W. Biederman .read = seq_read, 246822d917d8SEric W. Biederman .llseek = seq_lseek, 246922d917d8SEric W. Biederman .release = proc_id_map_release, 247022d917d8SEric W. Biederman }; 2471f76d207aSEric W. Biederman 2472f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = { 2473f76d207aSEric W. Biederman .open = proc_projid_map_open, 2474f76d207aSEric W. Biederman .write = proc_projid_map_write, 2475f76d207aSEric W. Biederman .read = seq_read, 2476f76d207aSEric W. Biederman .llseek = seq_lseek, 2477f76d207aSEric W. Biederman .release = proc_id_map_release, 2478f76d207aSEric W. Biederman }; 24799cc46516SEric W. Biederman 24809cc46516SEric W. Biederman static int proc_setgroups_open(struct inode *inode, struct file *file) 24819cc46516SEric W. Biederman { 24829cc46516SEric W. Biederman struct user_namespace *ns = NULL; 24839cc46516SEric W. Biederman struct task_struct *task; 24849cc46516SEric W. Biederman int ret; 24859cc46516SEric W. Biederman 24869cc46516SEric W. Biederman ret = -ESRCH; 24879cc46516SEric W. Biederman task = get_proc_task(inode); 24889cc46516SEric W. Biederman if (task) { 24899cc46516SEric W. Biederman rcu_read_lock(); 24909cc46516SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 24919cc46516SEric W. Biederman rcu_read_unlock(); 24929cc46516SEric W. Biederman put_task_struct(task); 24939cc46516SEric W. Biederman } 24949cc46516SEric W. Biederman if (!ns) 24959cc46516SEric W. Biederman goto err; 24969cc46516SEric W. Biederman 24979cc46516SEric W. Biederman if (file->f_mode & FMODE_WRITE) { 24989cc46516SEric W. Biederman ret = -EACCES; 24999cc46516SEric W. Biederman if (!ns_capable(ns, CAP_SYS_ADMIN)) 25009cc46516SEric W. Biederman goto err_put_ns; 25019cc46516SEric W. Biederman } 25029cc46516SEric W. Biederman 25039cc46516SEric W. Biederman ret = single_open(file, &proc_setgroups_show, ns); 25049cc46516SEric W. Biederman if (ret) 25059cc46516SEric W. Biederman goto err_put_ns; 25069cc46516SEric W. Biederman 25079cc46516SEric W. Biederman return 0; 25089cc46516SEric W. Biederman err_put_ns: 25099cc46516SEric W. Biederman put_user_ns(ns); 25109cc46516SEric W. Biederman err: 25119cc46516SEric W. Biederman return ret; 25129cc46516SEric W. Biederman } 25139cc46516SEric W. Biederman 25149cc46516SEric W. Biederman static int proc_setgroups_release(struct inode *inode, struct file *file) 25159cc46516SEric W. Biederman { 25169cc46516SEric W. Biederman struct seq_file *seq = file->private_data; 25179cc46516SEric W. Biederman struct user_namespace *ns = seq->private; 25189cc46516SEric W. Biederman int ret = single_release(inode, file); 25199cc46516SEric W. Biederman put_user_ns(ns); 25209cc46516SEric W. Biederman return ret; 25219cc46516SEric W. Biederman } 25229cc46516SEric W. Biederman 25239cc46516SEric W. Biederman static const struct file_operations proc_setgroups_operations = { 25249cc46516SEric W. Biederman .open = proc_setgroups_open, 25259cc46516SEric W. Biederman .write = proc_setgroups_write, 25269cc46516SEric W. Biederman .read = seq_read, 25279cc46516SEric W. Biederman .llseek = seq_lseek, 25289cc46516SEric W. Biederman .release = proc_setgroups_release, 25299cc46516SEric W. Biederman }; 253022d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */ 253122d917d8SEric W. Biederman 253247830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 253347830723SKees Cook struct pid *pid, struct task_struct *task) 253447830723SKees Cook { 2535a9712bc1SAl Viro int err = lock_trace(task); 2536a9712bc1SAl Viro if (!err) { 253747830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2538a9712bc1SAl Viro unlock_trace(task); 2539a9712bc1SAl Viro } 2540a9712bc1SAl Viro return err; 254147830723SKees Cook } 254247830723SKees Cook 2543801199ceSEric W. Biederman /* 254428a6d671SEric W. Biederman * Thread groups 254528a6d671SEric W. Biederman */ 254600977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2547c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 254820cdc894SEric W. Biederman 2549c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2550631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2551631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2552640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2553640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 2554640708a2SPavel Emelyanov #endif 2555631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 25566b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2557b2211a36SAndrew Morton #ifdef CONFIG_NET 2558631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2559b2211a36SAndrew Morton #endif 2560631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2561f9ea536eSAlexey Dobriyan ONE("auxv", S_IRUSR, proc_pid_auxv), 2562631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 256335a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality), 25641c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits), 256543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2566631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 256743ae34cbSIngo Molnar #endif 25685091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 25695091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 25705091faa4SMike Galbraith #endif 25714614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2572ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 257309d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall), 2574ebcb6734SRoland McGrath #endif 25752ca66ff7SAlexey Dobriyan ONE("cmdline", S_IRUGO, proc_pid_cmdline), 2576631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2577631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2578b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations), 257928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2580b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 258128a6d671SEric W. Biederman #endif 2582631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2583631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2584631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2585631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2586631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2587631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2588631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25891e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2590631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2591b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations), 259232ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations), 259328a6d671SEric W. Biederman #endif 259428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2595631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 259628a6d671SEric W. Biederman #endif 259728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2598edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan), 259928a6d671SEric W. Biederman #endif 26002ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 260135a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack), 260228a6d671SEric W. Biederman #endif 260328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2604f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat), 260528a6d671SEric W. Biederman #endif 26069745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2607631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 26089745512cSArjan van de Ven #endif 26098793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 261052de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show), 261128a6d671SEric W. Biederman #endif 2612a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2613006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show), 2614a424316cSPaul Menage #endif 26156ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score), 2616fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2617a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 261828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2619631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2620631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 262128a6d671SEric W. Biederman #endif 2622f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2623631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2624f4f154fdSAkinobu Mita #endif 2625698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2626631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 26273cb4a0bbSKawai, Hidehiro #endif 2628aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 262919aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tgid_io_accounting), 2630aba76fdbSAndrew Morton #endif 2631f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2632d962c144SAlexey Dobriyan ONE("hardwall", S_IRUGO, proc_pid_hardwall), 2633f133eccaSChris Metcalf #endif 263422d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 263522d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 263622d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2637f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 26389cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 263922d917d8SEric W. Biederman #endif 264048f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 264148f6a7a5SPavel Emelyanov REG("timers", S_IRUGO, proc_timers_operations), 264248f6a7a5SPavel Emelyanov #endif 264328a6d671SEric W. Biederman }; 264428a6d671SEric W. Biederman 2645f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) 264628a6d671SEric W. Biederman { 2647f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 264828a6d671SEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 264928a6d671SEric W. Biederman } 265028a6d671SEric W. Biederman 265100977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 265228a6d671SEric W. Biederman .read = generic_read_dir, 2653f0c3b509SAl Viro .iterate = proc_tgid_base_readdir, 26546038f373SArnd Bergmann .llseek = default_llseek, 265528a6d671SEric W. Biederman }; 265628a6d671SEric W. Biederman 265700cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 265800cd8dd3SAl Viro { 26597bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 26607bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 266128a6d671SEric W. Biederman } 266228a6d671SEric W. Biederman 2663c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 266428a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 266528a6d671SEric W. Biederman .getattr = pid_getattr, 266628a6d671SEric W. Biederman .setattr = proc_setattr, 26670499680aSVasiliy Kulikov .permission = proc_pid_permission, 266828a6d671SEric W. Biederman }; 266928a6d671SEric W. Biederman 267060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 26711da177e4SLinus Torvalds { 267248e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 26738578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 267448e6484dSEric W. Biederman struct qstr name; 26751da177e4SLinus Torvalds 267648e6484dSEric W. Biederman name.name = buf; 267760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 26784f522a24SAl Viro /* no ->d_hash() rejects on procfs */ 267960347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 268048e6484dSEric W. Biederman if (dentry) { 2681bbd51924SEric W. Biederman d_invalidate(dentry); 268248e6484dSEric W. Biederman dput(dentry); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 2685c35a7f18SOleg Nesterov if (pid == tgid) 2686c35a7f18SOleg Nesterov return; 2687c35a7f18SOleg Nesterov 268848e6484dSEric W. Biederman name.name = buf; 268960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 269060347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 269148e6484dSEric W. Biederman if (!leader) 269248e6484dSEric W. Biederman goto out; 269348e6484dSEric W. Biederman 269448e6484dSEric W. Biederman name.name = "task"; 269548e6484dSEric W. Biederman name.len = strlen(name.name); 269648e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 269748e6484dSEric W. Biederman if (!dir) 269848e6484dSEric W. Biederman goto out_put_leader; 269948e6484dSEric W. Biederman 270048e6484dSEric W. Biederman name.name = buf; 270160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 270248e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 270348e6484dSEric W. Biederman if (dentry) { 2704bbd51924SEric W. Biederman d_invalidate(dentry); 270548e6484dSEric W. Biederman dput(dentry); 27061da177e4SLinus Torvalds } 270748e6484dSEric W. Biederman 270848e6484dSEric W. Biederman dput(dir); 270948e6484dSEric W. Biederman out_put_leader: 271048e6484dSEric W. Biederman dput(leader); 271148e6484dSEric W. Biederman out: 271248e6484dSEric W. Biederman return; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds 27150895e91dSRandy Dunlap /** 27160895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 27170895e91dSRandy Dunlap * @task: task that should be flushed. 27180895e91dSRandy Dunlap * 27190895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 272060347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 27210895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 27220895e91dSRandy Dunlap * 27230895e91dSRandy Dunlap * Looks in the dcache for 27240895e91dSRandy Dunlap * /proc/@pid 27250895e91dSRandy Dunlap * /proc/@tgid/task/@pid 27260895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 27270895e91dSRandy Dunlap * from the dcache. 27280895e91dSRandy Dunlap * 27290895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 27300895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 27310895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 27320895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 27330895e91dSRandy Dunlap * dcache entries at process exit time. 27340895e91dSRandy Dunlap * 27350895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 27360895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 27370895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 273860347f67SPavel Emelyanov */ 273960347f67SPavel Emelyanov 274060347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 274160347f67SPavel Emelyanov { 27429fcc2d15SEric W. Biederman int i; 27439b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2744130f77ecSPavel Emelyanov struct upid *upid; 2745130f77ecSPavel Emelyanov 2746130f77ecSPavel Emelyanov pid = task_pid(task); 2747130f77ecSPavel Emelyanov tgid = task_tgid(task); 27489fcc2d15SEric W. Biederman 27499fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2750130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2751130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 27529b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 2753130f77ecSPavel Emelyanov } 275460347f67SPavel Emelyanov } 275560347f67SPavel Emelyanov 2756c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir, 27579711ef99SAdrian Bunk struct dentry * dentry, 2758c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2759444ceed8SEric W. Biederman { 2760444ceed8SEric W. Biederman struct inode *inode; 2761444ceed8SEric W. Biederman 276261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2763444ceed8SEric W. Biederman if (!inode) 2764444ceed8SEric W. Biederman goto out; 2765444ceed8SEric W. Biederman 2766444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2767444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2768444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2769444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2770aed54175SVegard Nossum 2771bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 2772bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 2773444ceed8SEric W. Biederman 2774fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2775444ceed8SEric W. Biederman 2776444ceed8SEric W. Biederman d_add(dentry, inode); 2777444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 27780b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2779c52a47acSAl Viro return 0; 2780444ceed8SEric W. Biederman out: 2781c52a47acSAl Viro return -ENOENT; 2782444ceed8SEric W. Biederman } 2783444ceed8SEric W. Biederman 278400cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 27851da177e4SLinus Torvalds { 2786335eb531SAlexey Dobriyan int result = -ENOENT; 27871da177e4SLinus Torvalds struct task_struct *task; 27881da177e4SLinus Torvalds unsigned tgid; 2789b488893aSPavel Emelyanov struct pid_namespace *ns; 27901da177e4SLinus Torvalds 2791dbcdb504SAlexey Dobriyan tgid = name_to_int(&dentry->d_name); 27921da177e4SLinus Torvalds if (tgid == ~0U) 27931da177e4SLinus Torvalds goto out; 27941da177e4SLinus Torvalds 2795b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2796de758734SEric W. Biederman rcu_read_lock(); 2797b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27981da177e4SLinus Torvalds if (task) 27991da177e4SLinus Torvalds get_task_struct(task); 2800de758734SEric W. Biederman rcu_read_unlock(); 28011da177e4SLinus Torvalds if (!task) 28021da177e4SLinus Torvalds goto out; 28031da177e4SLinus Torvalds 2804444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 280548e6484dSEric W. Biederman put_task_struct(task); 28061da177e4SLinus Torvalds out: 2807c52a47acSAl Viro return ERR_PTR(result); 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds /* 28110804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 28120bc58a91SEric W. Biederman * 28131da177e4SLinus Torvalds */ 281419fd4bb2SEric W. Biederman struct tgid_iter { 281519fd4bb2SEric W. Biederman unsigned int tgid; 28160804ef4bSEric W. Biederman struct task_struct *task; 281719fd4bb2SEric W. Biederman }; 281819fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 281919fd4bb2SEric W. Biederman { 28200804ef4bSEric W. Biederman struct pid *pid; 28211da177e4SLinus Torvalds 282219fd4bb2SEric W. Biederman if (iter.task) 282319fd4bb2SEric W. Biederman put_task_struct(iter.task); 28240804ef4bSEric W. Biederman rcu_read_lock(); 28250804ef4bSEric W. Biederman retry: 282619fd4bb2SEric W. Biederman iter.task = NULL; 282719fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 28280804ef4bSEric W. Biederman if (pid) { 282919fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 283019fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 28310804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 28320804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 28330804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 28340804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 28350804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 28360804ef4bSEric W. Biederman * 28370804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 28380804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 28390804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 28400804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 28410804ef4bSEric W. Biederman * As we don't care in the case of readdir. 28420bc58a91SEric W. Biederman */ 284319fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 284419fd4bb2SEric W. Biederman iter.tgid += 1; 28450804ef4bSEric W. Biederman goto retry; 284619fd4bb2SEric W. Biederman } 284719fd4bb2SEric W. Biederman get_task_struct(iter.task); 28481da177e4SLinus Torvalds } 2849454cc105SEric W. Biederman rcu_read_unlock(); 285019fd4bb2SEric W. Biederman return iter; 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds 28530097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 2856f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx) 28571da177e4SLinus Torvalds { 285819fd4bb2SEric W. Biederman struct tgid_iter iter; 28593aa3377fSAl Viro struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info; 2860f0c3b509SAl Viro loff_t pos = ctx->pos; 28611da177e4SLinus Torvalds 2862021ada7dSAl Viro if (pos >= PID_MAX_LIMIT + TGID_OFFSET) 2863f0c3b509SAl Viro return 0; 28641da177e4SLinus Torvalds 28650097875bSEric W. Biederman if (pos == TGID_OFFSET - 2) { 2866db963164SAl Viro struct inode *inode = ns->proc_self->d_inode; 2867db963164SAl Viro if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) 2868f0c3b509SAl Viro return 0; 28690097875bSEric W. Biederman ctx->pos = pos = pos + 1; 2870021ada7dSAl Viro } 28710097875bSEric W. Biederman if (pos == TGID_OFFSET - 1) { 28720097875bSEric W. Biederman struct inode *inode = ns->proc_thread_self->d_inode; 28730097875bSEric W. Biederman if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK)) 28740097875bSEric W. Biederman return 0; 28750097875bSEric W. Biederman ctx->pos = pos = pos + 1; 28760097875bSEric W. Biederman } 28770097875bSEric W. Biederman iter.tgid = pos - TGID_OFFSET; 287819fd4bb2SEric W. Biederman iter.task = NULL; 287919fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 288019fd4bb2SEric W. Biederman iter.task; 288119fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 2882f0c3b509SAl Viro char name[PROC_NUMBUF]; 2883f0c3b509SAl Viro int len; 2884f0c3b509SAl Viro if (!has_pid_permissions(ns, iter.task, 2)) 2885f0c3b509SAl Viro continue; 28860499680aSVasiliy Kulikov 2887f0c3b509SAl Viro len = snprintf(name, sizeof(name), "%d", iter.tgid); 2888f0c3b509SAl Viro ctx->pos = iter.tgid + TGID_OFFSET; 2889f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len, 2890f0c3b509SAl Viro proc_pid_instantiate, iter.task, NULL)) { 289119fd4bb2SEric W. Biederman put_task_struct(iter.task); 2892f0c3b509SAl Viro return 0; 28931da177e4SLinus Torvalds } 28941da177e4SLinus Torvalds } 2895f0c3b509SAl Viro ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; 28961da177e4SLinus Torvalds return 0; 28971da177e4SLinus Torvalds } 28981da177e4SLinus Torvalds 28990bc58a91SEric W. Biederman /* 290028a6d671SEric W. Biederman * Tasks 290128a6d671SEric W. Biederman */ 2902c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2903631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 29043835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 29056b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 29066ba8ed79SEric W. Biederman #ifdef CONFIG_NET 29076ba8ed79SEric W. Biederman DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 29086ba8ed79SEric W. Biederman #endif 2909631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2910f9ea536eSAlexey Dobriyan ONE("auxv", S_IRUSR, proc_pid_auxv), 2911631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 291235a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality), 29131c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits), 291443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2915631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 291643ae34cbSIngo Molnar #endif 29174614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2918ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 291909d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall), 2920ebcb6734SRoland McGrath #endif 29212ca66ff7SAlexey Dobriyan ONE("cmdline", S_IRUGO, proc_pid_cmdline), 2922631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2923631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2924b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_tid_maps_operations), 292581841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE 292681841161SCyrill Gorcunov REG("children", S_IRUGO, proc_tid_children_operations), 292781841161SCyrill Gorcunov #endif 292828a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2929b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), 293028a6d671SEric W. Biederman #endif 2931631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2932631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2933631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2934631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2935631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2936631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 29371e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2938631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2939b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_tid_smaps_operations), 294032ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations), 294128a6d671SEric W. Biederman #endif 294228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2943631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 294428a6d671SEric W. Biederman #endif 294528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2946edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan), 294728a6d671SEric W. Biederman #endif 29482ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 294935a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack), 295028a6d671SEric W. Biederman #endif 295128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2952f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat), 295328a6d671SEric W. Biederman #endif 29549745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2955631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 29569745512cSArjan van de Ven #endif 29578793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 295852de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show), 295928a6d671SEric W. Biederman #endif 2960a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2961006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show), 2962a424316cSPaul Menage #endif 29636ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score), 2964fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2965a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 296628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2967631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 296826ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 296928a6d671SEric W. Biederman #endif 2970f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2971631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2972f4f154fdSAkinobu Mita #endif 2973297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 297419aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tid_io_accounting), 2975297c5d92SAndrea Righi #endif 2976f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2977d962c144SAlexey Dobriyan ONE("hardwall", S_IRUGO, proc_pid_hardwall), 2978f133eccaSChris Metcalf #endif 297922d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 298022d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 298122d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2982f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 29839cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 298422d917d8SEric W. Biederman #endif 298528a6d671SEric W. Biederman }; 298628a6d671SEric W. Biederman 2987f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) 298828a6d671SEric W. Biederman { 2989f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 299028a6d671SEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 299128a6d671SEric W. Biederman } 299228a6d671SEric W. Biederman 299300cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 299400cd8dd3SAl Viro { 29957bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 29967bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 299728a6d671SEric W. Biederman } 299828a6d671SEric W. Biederman 299900977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 300028a6d671SEric W. Biederman .read = generic_read_dir, 3001f0c3b509SAl Viro .iterate = proc_tid_base_readdir, 30026038f373SArnd Bergmann .llseek = default_llseek, 300328a6d671SEric W. Biederman }; 300428a6d671SEric W. Biederman 3005c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 300628a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 300728a6d671SEric W. Biederman .getattr = pid_getattr, 300828a6d671SEric W. Biederman .setattr = proc_setattr, 300928a6d671SEric W. Biederman }; 301028a6d671SEric W. Biederman 3011c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir, 3012c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3013444ceed8SEric W. Biederman { 3014444ceed8SEric W. Biederman struct inode *inode; 301561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3016444ceed8SEric W. Biederman 3017444ceed8SEric W. Biederman if (!inode) 3018444ceed8SEric W. Biederman goto out; 3019444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3020444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3021444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3022444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3023aed54175SVegard Nossum 3024bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 3025bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 3026444ceed8SEric W. Biederman 3027fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3028444ceed8SEric W. Biederman 3029444ceed8SEric W. Biederman d_add(dentry, inode); 3030444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 30310b728e19SAl Viro if (pid_revalidate(dentry, 0)) 3032c52a47acSAl Viro return 0; 3033444ceed8SEric W. Biederman out: 3034c52a47acSAl Viro return -ENOENT; 3035444ceed8SEric W. Biederman } 3036444ceed8SEric W. Biederman 303700cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 303828a6d671SEric W. Biederman { 3039c52a47acSAl Viro int result = -ENOENT; 304028a6d671SEric W. Biederman struct task_struct *task; 304128a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 304228a6d671SEric W. Biederman unsigned tid; 3043b488893aSPavel Emelyanov struct pid_namespace *ns; 304428a6d671SEric W. Biederman 304528a6d671SEric W. Biederman if (!leader) 304628a6d671SEric W. Biederman goto out_no_task; 304728a6d671SEric W. Biederman 3048dbcdb504SAlexey Dobriyan tid = name_to_int(&dentry->d_name); 304928a6d671SEric W. Biederman if (tid == ~0U) 305028a6d671SEric W. Biederman goto out; 305128a6d671SEric W. Biederman 3052b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 305328a6d671SEric W. Biederman rcu_read_lock(); 3054b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 305528a6d671SEric W. Biederman if (task) 305628a6d671SEric W. Biederman get_task_struct(task); 305728a6d671SEric W. Biederman rcu_read_unlock(); 305828a6d671SEric W. Biederman if (!task) 305928a6d671SEric W. Biederman goto out; 3060bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 306128a6d671SEric W. Biederman goto out_drop_task; 306228a6d671SEric W. Biederman 3063444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 306428a6d671SEric W. Biederman out_drop_task: 306528a6d671SEric W. Biederman put_task_struct(task); 306628a6d671SEric W. Biederman out: 306728a6d671SEric W. Biederman put_task_struct(leader); 306828a6d671SEric W. Biederman out_no_task: 3069c52a47acSAl Viro return ERR_PTR(result); 307028a6d671SEric W. Biederman } 307128a6d671SEric W. Biederman 307228a6d671SEric W. Biederman /* 30730bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 30740bc58a91SEric W. Biederman * 30750bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 30760bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 30770bc58a91SEric W. Biederman * directory we have more work todo. 30780bc58a91SEric W. Biederman * 30790bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 30800bc58a91SEric W. Biederman * 30810bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 30820bc58a91SEric W. Biederman * threads past it. 30830bc58a91SEric W. Biederman */ 30849f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, 30859f6e963fSOleg Nesterov struct pid_namespace *ns) 30860bc58a91SEric W. Biederman { 3087d855a4b7SOleg Nesterov struct task_struct *pos, *task; 30889f6e963fSOleg Nesterov unsigned long nr = f_pos; 30899f6e963fSOleg Nesterov 30909f6e963fSOleg Nesterov if (nr != f_pos) /* 32bit overflow? */ 30919f6e963fSOleg Nesterov return NULL; 30920bc58a91SEric W. Biederman 3093cc288738SEric W. Biederman rcu_read_lock(); 3094d855a4b7SOleg Nesterov task = pid_task(pid, PIDTYPE_PID); 3095d855a4b7SOleg Nesterov if (!task) 3096d855a4b7SOleg Nesterov goto fail; 3097d855a4b7SOleg Nesterov 3098d855a4b7SOleg Nesterov /* Attempt to start with the tid of a thread */ 30999f6e963fSOleg Nesterov if (tid && nr) { 3100b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3101d855a4b7SOleg Nesterov if (pos && same_thread_group(pos, task)) 3102a872ff0cSOleg Nesterov goto found; 31030bc58a91SEric W. Biederman } 31040bc58a91SEric W. Biederman 31050bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 31069f6e963fSOleg Nesterov if (nr >= get_nr_threads(task)) 3107c986c14aSOleg Nesterov goto fail; 3108a872ff0cSOleg Nesterov 3109a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3110a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3111a872ff0cSOleg Nesterov */ 3112d855a4b7SOleg Nesterov pos = task = task->group_leader; 3113c986c14aSOleg Nesterov do { 31149f6e963fSOleg Nesterov if (!nr--) 3115c986c14aSOleg Nesterov goto found; 3116d855a4b7SOleg Nesterov } while_each_thread(task, pos); 3117c986c14aSOleg Nesterov fail: 3118a872ff0cSOleg Nesterov pos = NULL; 3119a872ff0cSOleg Nesterov goto out; 3120a872ff0cSOleg Nesterov found: 3121a872ff0cSOleg Nesterov get_task_struct(pos); 3122a872ff0cSOleg Nesterov out: 3123cc288738SEric W. Biederman rcu_read_unlock(); 31240bc58a91SEric W. Biederman return pos; 31250bc58a91SEric W. Biederman } 31260bc58a91SEric W. Biederman 31270bc58a91SEric W. Biederman /* 31280bc58a91SEric W. Biederman * Find the next thread in the thread list. 31290bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 31300bc58a91SEric W. Biederman * 31310bc58a91SEric W. Biederman * The reference to the input task_struct is released. 31320bc58a91SEric W. Biederman */ 31330bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 31340bc58a91SEric W. Biederman { 3135c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3136cc288738SEric W. Biederman rcu_read_lock(); 3137c1df7fb8SOleg Nesterov if (pid_alive(start)) { 31380bc58a91SEric W. Biederman pos = next_thread(start); 3139c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 31400bc58a91SEric W. Biederman pos = NULL; 3141c1df7fb8SOleg Nesterov else 3142c1df7fb8SOleg Nesterov get_task_struct(pos); 3143c1df7fb8SOleg Nesterov } 3144cc288738SEric W. Biederman rcu_read_unlock(); 31450bc58a91SEric W. Biederman put_task_struct(start); 31460bc58a91SEric W. Biederman return pos; 31470bc58a91SEric W. Biederman } 31480bc58a91SEric W. Biederman 31491da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 3150f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx) 31511da177e4SLinus Torvalds { 3152d855a4b7SOleg Nesterov struct inode *inode = file_inode(file); 3153d855a4b7SOleg Nesterov struct task_struct *task; 3154b488893aSPavel Emelyanov struct pid_namespace *ns; 3155f0c3b509SAl Viro int tid; 31561da177e4SLinus Torvalds 3157d855a4b7SOleg Nesterov if (proc_inode_is_dead(inode)) 3158f0c3b509SAl Viro return -ENOENT; 31591da177e4SLinus Torvalds 3160f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 3161d855a4b7SOleg Nesterov return 0; 31621da177e4SLinus Torvalds 31630bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 31640bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 31650bc58a91SEric W. Biederman */ 31663aa3377fSAl Viro ns = inode->i_sb->s_fs_info; 3167f0c3b509SAl Viro tid = (int)file->f_version; 3168f0c3b509SAl Viro file->f_version = 0; 3169d855a4b7SOleg Nesterov for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); 31700bc58a91SEric W. Biederman task; 3171f0c3b509SAl Viro task = next_tid(task), ctx->pos++) { 3172f0c3b509SAl Viro char name[PROC_NUMBUF]; 3173f0c3b509SAl Viro int len; 3174b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 3175f0c3b509SAl Viro len = snprintf(name, sizeof(name), "%d", tid); 3176f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len, 3177f0c3b509SAl Viro proc_task_instantiate, task, NULL)) { 31780bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 31790bc58a91SEric W. Biederman * pid for the next readir call */ 3180f0c3b509SAl Viro file->f_version = (u64)tid; 31810bc58a91SEric W. Biederman put_task_struct(task); 31821da177e4SLinus Torvalds break; 31830bc58a91SEric W. Biederman } 31841da177e4SLinus Torvalds } 3185d855a4b7SOleg Nesterov 3186f0c3b509SAl Viro return 0; 31871da177e4SLinus Torvalds } 31886e66b52bSEric W. Biederman 31896e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31906e66b52bSEric W. Biederman { 31916e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 319299f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31936e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31946e66b52bSEric W. Biederman 319599f89551SEric W. Biederman if (p) { 319699f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 319799f89551SEric W. Biederman put_task_struct(p); 31986e66b52bSEric W. Biederman } 31996e66b52bSEric W. Biederman 32006e66b52bSEric W. Biederman return 0; 32016e66b52bSEric W. Biederman } 320228a6d671SEric W. Biederman 3203c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 320428a6d671SEric W. Biederman .lookup = proc_task_lookup, 320528a6d671SEric W. Biederman .getattr = proc_task_getattr, 320628a6d671SEric W. Biederman .setattr = proc_setattr, 32070499680aSVasiliy Kulikov .permission = proc_pid_permission, 320828a6d671SEric W. Biederman }; 320928a6d671SEric W. Biederman 321000977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 321128a6d671SEric W. Biederman .read = generic_read_dir, 3212f0c3b509SAl Viro .iterate = proc_task_readdir, 32136038f373SArnd Bergmann .llseek = default_llseek, 321428a6d671SEric W. Biederman }; 3215