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 { 1722b0143b5SDavid Howells struct task_struct *task = get_proc_task(d_inode(dentry)); 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 { 1892b0143b5SDavid Howells struct task_struct *task = get_proc_task(d_inode(dentry)); 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 24125ce3191SJoe Perches if (lookup_symbol_name(wchan, symname) < 0) { 242f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 243f83ce3e6SJake Edge return 0; 24425ce3191SJoe Perches seq_printf(m, "%lu", wchan); 24525ce3191SJoe Perches } else { 24625ce3191SJoe Perches seq_printf(m, "%s", symname); 24725ce3191SJoe Perches } 24825ce3191SJoe Perches 24925ce3191SJoe 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 307*5968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO 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*5968ceceSNaveen N. Rao if (unlikely(!sched_info_on())) 315*5968ceceSNaveen N. Rao seq_printf(m, "0 0 0\n"); 316*5968ceceSNaveen N. Rao else 31725ce3191SJoe Perches seq_printf(m, "%llu %llu %lu\n", 318826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 319826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3202d72376bSIngo Molnar task->sched_info.pcount); 32125ce3191SJoe Perches 32225ce3191SJoe Perches return 0; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds #endif 3251da177e4SLinus Torvalds 3269745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3279745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3289745512cSArjan van de Ven { 3299745512cSArjan van de Ven int i; 33013d77c37SHiroshi Shimamoto struct inode *inode = m->private; 33113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3329745512cSArjan van de Ven 33313d77c37SHiroshi Shimamoto if (!task) 33413d77c37SHiroshi Shimamoto return -ESRCH; 33513d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3369745512cSArjan van de Ven for (i = 0; i < 32; i++) { 33734e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 33834e49d4fSJoe Perches if (lr->backtrace[0]) { 3399745512cSArjan van de Ven int q; 3409745512cSArjan van de Ven seq_printf(m, "%i %li %li", 34134e49d4fSJoe Perches lr->count, lr->time, lr->max); 3429745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 34334e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 34434e49d4fSJoe Perches if (!bt) 3459745512cSArjan van de Ven break; 34634e49d4fSJoe Perches if (bt == ULONG_MAX) 3479745512cSArjan van de Ven break; 34834e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 3499745512cSArjan van de Ven } 3509d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 3519745512cSArjan van de Ven } 3529745512cSArjan van de Ven 3539745512cSArjan van de Ven } 35413d77c37SHiroshi Shimamoto put_task_struct(task); 3559745512cSArjan van de Ven return 0; 3569745512cSArjan van de Ven } 3579745512cSArjan van de Ven 3589745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3599745512cSArjan van de Ven { 36013d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 361d6643d12SHiroshi Shimamoto } 362d6643d12SHiroshi Shimamoto 3639745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3649745512cSArjan van de Ven size_t count, loff_t *offs) 3659745512cSArjan van de Ven { 366496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 3679745512cSArjan van de Ven 36813d77c37SHiroshi Shimamoto if (!task) 36913d77c37SHiroshi Shimamoto return -ESRCH; 3709745512cSArjan van de Ven clear_all_latency_tracing(task); 37113d77c37SHiroshi Shimamoto put_task_struct(task); 3729745512cSArjan van de Ven 3739745512cSArjan van de Ven return count; 3749745512cSArjan van de Ven } 3759745512cSArjan van de Ven 3769745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3779745512cSArjan van de Ven .open = lstats_open, 3789745512cSArjan van de Ven .read = seq_read, 3799745512cSArjan van de Ven .write = lstats_write, 3809745512cSArjan van de Ven .llseek = seq_lseek, 38113d77c37SHiroshi Shimamoto .release = single_release, 3829745512cSArjan van de Ven }; 3839745512cSArjan van de Ven 3849745512cSArjan van de Ven #endif 3859745512cSArjan van de Ven 3866ba51e37SAlexey Dobriyan static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, 3876ba51e37SAlexey Dobriyan struct pid *pid, struct task_struct *task) 3881da177e4SLinus Torvalds { 389a7f638f9SDavid Rientjes unsigned long totalpages = totalram_pages + total_swap_pages; 390b95c35e7SOleg Nesterov unsigned long points = 0; 3911da177e4SLinus Torvalds 39219c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 393b95c35e7SOleg Nesterov if (pid_alive(task)) 394a7f638f9SDavid Rientjes points = oom_badness(task, NULL, NULL, totalpages) * 395a7f638f9SDavid Rientjes 1000 / totalpages; 39619c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 39725ce3191SJoe Perches seq_printf(m, "%lu\n", points); 39825ce3191SJoe Perches 39925ce3191SJoe Perches return 0; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 402d85f50d5SNeil Horman struct limit_names { 403cedbccabSAlexey Dobriyan const char *name; 404cedbccabSAlexey Dobriyan const char *unit; 405d85f50d5SNeil Horman }; 406d85f50d5SNeil Horman 407d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 408cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 409d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 410d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 411d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 412d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 413d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 414d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 415d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 416d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 417d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 418d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 419d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 420d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 421d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 422d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4238808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 424d85f50d5SNeil Horman }; 425d85f50d5SNeil Horman 426d85f50d5SNeil Horman /* Display limits for a process */ 4271c963eb1SAlexey Dobriyan static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, 4281c963eb1SAlexey Dobriyan struct pid *pid, struct task_struct *task) 429d85f50d5SNeil Horman { 430d85f50d5SNeil Horman unsigned int i; 431d85f50d5SNeil Horman unsigned long flags; 432d85f50d5SNeil Horman 433d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 434d85f50d5SNeil Horman 435a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 436d85f50d5SNeil Horman return 0; 437d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 438d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 439d85f50d5SNeil Horman 440d85f50d5SNeil Horman /* 441d85f50d5SNeil Horman * print the file header 442d85f50d5SNeil Horman */ 4431c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20s %-20s %-10s\n", 444d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 445d85f50d5SNeil Horman 446d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 447d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 4481c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20s ", 449d85f50d5SNeil Horman lnames[i].name, "unlimited"); 450d85f50d5SNeil Horman else 4511c963eb1SAlexey Dobriyan seq_printf(m, "%-25s %-20lu ", 452d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 453d85f50d5SNeil Horman 454d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 4551c963eb1SAlexey Dobriyan seq_printf(m, "%-20s ", "unlimited"); 456d85f50d5SNeil Horman else 4571c963eb1SAlexey Dobriyan seq_printf(m, "%-20lu ", rlim[i].rlim_max); 458d85f50d5SNeil Horman 459d85f50d5SNeil Horman if (lnames[i].unit) 4601c963eb1SAlexey Dobriyan seq_printf(m, "%-10s\n", lnames[i].unit); 461d85f50d5SNeil Horman else 4621c963eb1SAlexey Dobriyan seq_putc(m, '\n'); 463d85f50d5SNeil Horman } 464d85f50d5SNeil Horman 4651c963eb1SAlexey Dobriyan return 0; 466d85f50d5SNeil Horman } 467d85f50d5SNeil Horman 468ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 46909d93bd6SAlexey Dobriyan static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, 47009d93bd6SAlexey Dobriyan struct pid *pid, struct task_struct *task) 471ebcb6734SRoland McGrath { 472ebcb6734SRoland McGrath long nr; 473ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 47425ce3191SJoe Perches int res; 47525ce3191SJoe Perches 47625ce3191SJoe Perches res = lock_trace(task); 477a9712bc1SAl Viro if (res) 478a9712bc1SAl Viro return res; 479ebcb6734SRoland McGrath 480ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 48109d93bd6SAlexey Dobriyan seq_puts(m, "running\n"); 482a9712bc1SAl Viro else if (nr < 0) 48309d93bd6SAlexey Dobriyan seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 484a9712bc1SAl Viro else 48509d93bd6SAlexey Dobriyan seq_printf(m, 486ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 487ebcb6734SRoland McGrath nr, 488ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 489ebcb6734SRoland McGrath sp, pc); 490a9712bc1SAl Viro unlock_trace(task); 49125ce3191SJoe Perches 49225ce3191SJoe Perches return 0; 493ebcb6734SRoland McGrath } 494ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 495ebcb6734SRoland McGrath 4961da177e4SLinus Torvalds /************************************************************************/ 4971da177e4SLinus Torvalds /* Here the fs part begins */ 4981da177e4SLinus Torvalds /************************************************************************/ 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* permission checks */ 501778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5021da177e4SLinus Torvalds { 503778c1144SEric W. Biederman struct task_struct *task; 504778c1144SEric W. Biederman int allowed = 0; 505df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 506df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 507df26c40eSEric W. Biederman * information. 508778c1144SEric W. Biederman */ 509778c1144SEric W. Biederman task = get_proc_task(inode); 510df26c40eSEric W. Biederman if (task) { 511006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 512778c1144SEric W. Biederman put_task_struct(task); 513df26c40eSEric W. Biederman } 514778c1144SEric W. Biederman return allowed; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5176b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 5186d76fa58SLinus Torvalds { 5196d76fa58SLinus Torvalds int error; 5202b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 5216d76fa58SLinus Torvalds 5226d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5236d76fa58SLinus Torvalds return -EPERM; 5246d76fa58SLinus Torvalds 5256d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5261025774cSChristoph Hellwig if (error) 5276d76fa58SLinus Torvalds return error; 5281025774cSChristoph Hellwig 5291025774cSChristoph Hellwig setattr_copy(inode, attr); 5301025774cSChristoph Hellwig mark_inode_dirty(inode); 5311025774cSChristoph Hellwig return 0; 5326d76fa58SLinus Torvalds } 5336d76fa58SLinus Torvalds 5340499680aSVasiliy Kulikov /* 5350499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1) 5360499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)? 5370499680aSVasiliy Kulikov */ 5380499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid, 5390499680aSVasiliy Kulikov struct task_struct *task, 5400499680aSVasiliy Kulikov int hide_pid_min) 5410499680aSVasiliy Kulikov { 5420499680aSVasiliy Kulikov if (pid->hide_pid < hide_pid_min) 5430499680aSVasiliy Kulikov return true; 5440499680aSVasiliy Kulikov if (in_group_p(pid->pid_gid)) 5450499680aSVasiliy Kulikov return true; 5460499680aSVasiliy Kulikov return ptrace_may_access(task, PTRACE_MODE_READ); 5470499680aSVasiliy Kulikov } 5480499680aSVasiliy Kulikov 5490499680aSVasiliy Kulikov 5500499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask) 5510499680aSVasiliy Kulikov { 5520499680aSVasiliy Kulikov struct pid_namespace *pid = inode->i_sb->s_fs_info; 5530499680aSVasiliy Kulikov struct task_struct *task; 5540499680aSVasiliy Kulikov bool has_perms; 5550499680aSVasiliy Kulikov 5560499680aSVasiliy Kulikov task = get_proc_task(inode); 557a2ef990aSXiaotian Feng if (!task) 558a2ef990aSXiaotian Feng return -ESRCH; 5590499680aSVasiliy Kulikov has_perms = has_pid_permissions(pid, task, 1); 5600499680aSVasiliy Kulikov put_task_struct(task); 5610499680aSVasiliy Kulikov 5620499680aSVasiliy Kulikov if (!has_perms) { 5630499680aSVasiliy Kulikov if (pid->hide_pid == 2) { 5640499680aSVasiliy Kulikov /* 5650499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open() 5660499680aSVasiliy Kulikov * consistent with each other. If a process 5670499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen 5680499680aSVasiliy Kulikov * in procfs at all. 5690499680aSVasiliy Kulikov */ 5700499680aSVasiliy Kulikov return -ENOENT; 5710499680aSVasiliy Kulikov } 5720499680aSVasiliy Kulikov 5730499680aSVasiliy Kulikov return -EPERM; 5740499680aSVasiliy Kulikov } 5750499680aSVasiliy Kulikov return generic_permission(inode, mask); 5760499680aSVasiliy Kulikov } 5770499680aSVasiliy Kulikov 5780499680aSVasiliy Kulikov 5790499680aSVasiliy Kulikov 580c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5816d76fa58SLinus Torvalds .setattr = proc_setattr, 5826d76fa58SLinus Torvalds }; 5836d76fa58SLinus Torvalds 584be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 585be614086SEric W. Biederman { 586be614086SEric W. Biederman struct inode *inode = m->private; 587be614086SEric W. Biederman struct pid_namespace *ns; 588be614086SEric W. Biederman struct pid *pid; 589be614086SEric W. Biederman struct task_struct *task; 590be614086SEric W. Biederman int ret; 591be614086SEric W. Biederman 592be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 593be614086SEric W. Biederman pid = proc_pid(inode); 594be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 595be614086SEric W. Biederman if (!task) 596be614086SEric W. Biederman return -ESRCH; 597be614086SEric W. Biederman 598be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 599be614086SEric W. Biederman 600be614086SEric W. Biederman put_task_struct(task); 601be614086SEric W. Biederman return ret; 602be614086SEric W. Biederman } 603be614086SEric W. Biederman 604be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 605be614086SEric W. Biederman { 606c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 607be614086SEric W. Biederman } 608be614086SEric W. Biederman 609be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 610be614086SEric W. Biederman .open = proc_single_open, 611be614086SEric W. Biederman .read = seq_read, 612be614086SEric W. Biederman .llseek = seq_lseek, 613be614086SEric W. Biederman .release = single_release, 614be614086SEric W. Biederman }; 615be614086SEric W. Biederman 6165381e169SOleg Nesterov 6175381e169SOleg Nesterov struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode) 6181da177e4SLinus Torvalds { 6195381e169SOleg Nesterov struct task_struct *task = get_proc_task(inode); 6205381e169SOleg Nesterov struct mm_struct *mm = ERR_PTR(-ESRCH); 621e268337dSLinus Torvalds 6225381e169SOleg Nesterov if (task) { 623b409e578SCong Wang mm = mm_access(task, mode); 624e268337dSLinus Torvalds put_task_struct(task); 625e268337dSLinus Torvalds 6265381e169SOleg Nesterov if (!IS_ERR_OR_NULL(mm)) { 6276d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */ 6286d08f2c7SOleg Nesterov atomic_inc(&mm->mm_count); 6296d08f2c7SOleg Nesterov /* but do not pin its memory */ 6306d08f2c7SOleg Nesterov mmput(mm); 6316d08f2c7SOleg Nesterov } 6325381e169SOleg Nesterov } 6335381e169SOleg Nesterov 6345381e169SOleg Nesterov return mm; 6355381e169SOleg Nesterov } 6365381e169SOleg Nesterov 6375381e169SOleg Nesterov static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) 6385381e169SOleg Nesterov { 6395381e169SOleg Nesterov struct mm_struct *mm = proc_mem_open(inode, mode); 6405381e169SOleg Nesterov 6415381e169SOleg Nesterov if (IS_ERR(mm)) 6425381e169SOleg Nesterov return PTR_ERR(mm); 6436d08f2c7SOleg Nesterov 644e268337dSLinus Torvalds file->private_data = mm; 6451da177e4SLinus Torvalds return 0; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 648b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file) 649b409e578SCong Wang { 650bc452b4bSDjalal Harouni int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); 651bc452b4bSDjalal Harouni 652bc452b4bSDjalal Harouni /* OK to pass negative loff_t, we can catch out-of-range */ 653bc452b4bSDjalal Harouni file->f_mode |= FMODE_UNSIGNED_OFFSET; 654bc452b4bSDjalal Harouni 655bc452b4bSDjalal Harouni return ret; 656b409e578SCong Wang } 657b409e578SCong Wang 658572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf, 659572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write) 6601da177e4SLinus Torvalds { 661e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 662572d34b9SOleg Nesterov unsigned long addr = *ppos; 663572d34b9SOleg Nesterov ssize_t copied; 6641da177e4SLinus Torvalds char *page; 6651da177e4SLinus Torvalds 666e268337dSLinus Torvalds if (!mm) 667e268337dSLinus Torvalds return 0; 6681da177e4SLinus Torvalds 669e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 6701da177e4SLinus Torvalds if (!page) 671e268337dSLinus Torvalds return -ENOMEM; 6721da177e4SLinus Torvalds 673f7ca54f4SFrederik Deweerdt copied = 0; 6746d08f2c7SOleg Nesterov if (!atomic_inc_not_zero(&mm->mm_users)) 6756d08f2c7SOleg Nesterov goto free; 6766d08f2c7SOleg Nesterov 6771da177e4SLinus Torvalds while (count > 0) { 678572d34b9SOleg Nesterov int this_len = min_t(int, count, PAGE_SIZE); 6791da177e4SLinus Torvalds 680572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) { 6811da177e4SLinus Torvalds copied = -EFAULT; 6821da177e4SLinus Torvalds break; 6831da177e4SLinus Torvalds } 684572d34b9SOleg Nesterov 685572d34b9SOleg Nesterov this_len = access_remote_vm(mm, addr, page, this_len, write); 686572d34b9SOleg Nesterov if (!this_len) { 6871da177e4SLinus Torvalds if (!copied) 6881da177e4SLinus Torvalds copied = -EIO; 6891da177e4SLinus Torvalds break; 6901da177e4SLinus Torvalds } 691572d34b9SOleg Nesterov 692572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) { 693572d34b9SOleg Nesterov copied = -EFAULT; 694572d34b9SOleg Nesterov break; 6951da177e4SLinus Torvalds } 696572d34b9SOleg Nesterov 697572d34b9SOleg Nesterov buf += this_len; 698572d34b9SOleg Nesterov addr += this_len; 699572d34b9SOleg Nesterov copied += this_len; 700572d34b9SOleg Nesterov count -= this_len; 701572d34b9SOleg Nesterov } 702572d34b9SOleg Nesterov *ppos = addr; 70330cd8903SKOSAKI Motohiro 7046d08f2c7SOleg Nesterov mmput(mm); 7056d08f2c7SOleg Nesterov free: 70630cd8903SKOSAKI Motohiro free_page((unsigned long) page); 7071da177e4SLinus Torvalds return copied; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf, 711572d34b9SOleg Nesterov size_t count, loff_t *ppos) 712572d34b9SOleg Nesterov { 713572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0); 714572d34b9SOleg Nesterov } 715572d34b9SOleg Nesterov 716572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf, 717572d34b9SOleg Nesterov size_t count, loff_t *ppos) 718572d34b9SOleg Nesterov { 719572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1); 720572d34b9SOleg Nesterov } 721572d34b9SOleg Nesterov 72285863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 7231da177e4SLinus Torvalds { 7241da177e4SLinus Torvalds switch (orig) { 7251da177e4SLinus Torvalds case 0: 7261da177e4SLinus Torvalds file->f_pos = offset; 7271da177e4SLinus Torvalds break; 7281da177e4SLinus Torvalds case 1: 7291da177e4SLinus Torvalds file->f_pos += offset; 7301da177e4SLinus Torvalds break; 7311da177e4SLinus Torvalds default: 7321da177e4SLinus Torvalds return -EINVAL; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds force_successful_syscall_return(); 7351da177e4SLinus Torvalds return file->f_pos; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 738e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file) 739e268337dSLinus Torvalds { 740e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 74171879d3cSOleg Nesterov if (mm) 7426d08f2c7SOleg Nesterov mmdrop(mm); 743e268337dSLinus Torvalds return 0; 744e268337dSLinus Torvalds } 745e268337dSLinus Torvalds 74600977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 7471da177e4SLinus Torvalds .llseek = mem_lseek, 7481da177e4SLinus Torvalds .read = mem_read, 7491da177e4SLinus Torvalds .write = mem_write, 7501da177e4SLinus Torvalds .open = mem_open, 751e268337dSLinus Torvalds .release = mem_release, 7521da177e4SLinus Torvalds }; 7531da177e4SLinus Torvalds 754b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file) 755b409e578SCong Wang { 756b409e578SCong Wang return __mem_open(inode, file, PTRACE_MODE_READ); 757b409e578SCong Wang } 758b409e578SCong Wang 759315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 760315e28c8SJames Pearson size_t count, loff_t *ppos) 761315e28c8SJames Pearson { 762315e28c8SJames Pearson char *page; 763315e28c8SJames Pearson unsigned long src = *ppos; 764b409e578SCong Wang int ret = 0; 765b409e578SCong Wang struct mm_struct *mm = file->private_data; 766315e28c8SJames Pearson 767b409e578SCong Wang if (!mm) 768b409e578SCong Wang return 0; 769315e28c8SJames Pearson 770315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 771315e28c8SJames Pearson if (!page) 772b409e578SCong Wang return -ENOMEM; 773315e28c8SJames Pearson 774d6f64b89SAl Viro ret = 0; 775b409e578SCong Wang if (!atomic_inc_not_zero(&mm->mm_users)) 776b409e578SCong Wang goto free; 777315e28c8SJames Pearson while (count > 0) { 778e8905ec2SDjalal Harouni size_t this_len, max_len; 779e8905ec2SDjalal Harouni int retval; 780e8905ec2SDjalal Harouni 781e8905ec2SDjalal Harouni if (src >= (mm->env_end - mm->env_start)) 782e8905ec2SDjalal Harouni break; 783315e28c8SJames Pearson 784315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 785315e28c8SJames Pearson 786e8905ec2SDjalal Harouni max_len = min_t(size_t, PAGE_SIZE, count); 787e8905ec2SDjalal Harouni this_len = min(max_len, this_len); 788315e28c8SJames Pearson 789b409e578SCong Wang retval = access_remote_vm(mm, (mm->env_start + src), 790315e28c8SJames Pearson page, this_len, 0); 791315e28c8SJames Pearson 792315e28c8SJames Pearson if (retval <= 0) { 793315e28c8SJames Pearson ret = retval; 794315e28c8SJames Pearson break; 795315e28c8SJames Pearson } 796315e28c8SJames Pearson 797315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 798315e28c8SJames Pearson ret = -EFAULT; 799315e28c8SJames Pearson break; 800315e28c8SJames Pearson } 801315e28c8SJames Pearson 802315e28c8SJames Pearson ret += retval; 803315e28c8SJames Pearson src += retval; 804315e28c8SJames Pearson buf += retval; 805315e28c8SJames Pearson count -= retval; 806315e28c8SJames Pearson } 807315e28c8SJames Pearson *ppos = src; 808315e28c8SJames Pearson mmput(mm); 809b409e578SCong Wang 810b409e578SCong Wang free: 811315e28c8SJames Pearson free_page((unsigned long) page); 812315e28c8SJames Pearson return ret; 813315e28c8SJames Pearson } 814315e28c8SJames Pearson 815315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 816b409e578SCong Wang .open = environ_open, 817315e28c8SJames Pearson .read = environ_read, 81887df8424SArnd Bergmann .llseek = generic_file_llseek, 819b409e578SCong Wang .release = mem_release, 820315e28c8SJames Pearson }; 821315e28c8SJames Pearson 822fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, 823fa0cbbf1SDavid Rientjes loff_t *ppos) 824fa0cbbf1SDavid Rientjes { 825496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 826fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 827fa0cbbf1SDavid Rientjes int oom_adj = OOM_ADJUST_MIN; 828fa0cbbf1SDavid Rientjes size_t len; 829fa0cbbf1SDavid Rientjes unsigned long flags; 830fa0cbbf1SDavid Rientjes 831fa0cbbf1SDavid Rientjes if (!task) 832fa0cbbf1SDavid Rientjes return -ESRCH; 833fa0cbbf1SDavid Rientjes if (lock_task_sighand(task, &flags)) { 834fa0cbbf1SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) 835fa0cbbf1SDavid Rientjes oom_adj = OOM_ADJUST_MAX; 836fa0cbbf1SDavid Rientjes else 837fa0cbbf1SDavid Rientjes oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / 838fa0cbbf1SDavid Rientjes OOM_SCORE_ADJ_MAX; 839fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 840fa0cbbf1SDavid Rientjes } 841fa0cbbf1SDavid Rientjes put_task_struct(task); 842fa0cbbf1SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); 843fa0cbbf1SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 844fa0cbbf1SDavid Rientjes } 845fa0cbbf1SDavid Rientjes 846fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf, 847fa0cbbf1SDavid Rientjes size_t count, loff_t *ppos) 848fa0cbbf1SDavid Rientjes { 849fa0cbbf1SDavid Rientjes struct task_struct *task; 850fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 851fa0cbbf1SDavid Rientjes int oom_adj; 852fa0cbbf1SDavid Rientjes unsigned long flags; 853fa0cbbf1SDavid Rientjes int err; 854fa0cbbf1SDavid Rientjes 855fa0cbbf1SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 856fa0cbbf1SDavid Rientjes if (count > sizeof(buffer) - 1) 857fa0cbbf1SDavid Rientjes count = sizeof(buffer) - 1; 858fa0cbbf1SDavid Rientjes if (copy_from_user(buffer, buf, count)) { 859fa0cbbf1SDavid Rientjes err = -EFAULT; 860fa0cbbf1SDavid Rientjes goto out; 861fa0cbbf1SDavid Rientjes } 862fa0cbbf1SDavid Rientjes 863fa0cbbf1SDavid Rientjes err = kstrtoint(strstrip(buffer), 0, &oom_adj); 864fa0cbbf1SDavid Rientjes if (err) 865fa0cbbf1SDavid Rientjes goto out; 866fa0cbbf1SDavid Rientjes if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && 867fa0cbbf1SDavid Rientjes oom_adj != OOM_DISABLE) { 868fa0cbbf1SDavid Rientjes err = -EINVAL; 869fa0cbbf1SDavid Rientjes goto out; 870fa0cbbf1SDavid Rientjes } 871fa0cbbf1SDavid Rientjes 872496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 873fa0cbbf1SDavid Rientjes if (!task) { 874fa0cbbf1SDavid Rientjes err = -ESRCH; 875fa0cbbf1SDavid Rientjes goto out; 876fa0cbbf1SDavid Rientjes } 877fa0cbbf1SDavid Rientjes 878fa0cbbf1SDavid Rientjes task_lock(task); 879fa0cbbf1SDavid Rientjes if (!task->mm) { 880fa0cbbf1SDavid Rientjes err = -EINVAL; 881fa0cbbf1SDavid Rientjes goto err_task_lock; 882fa0cbbf1SDavid Rientjes } 883fa0cbbf1SDavid Rientjes 884fa0cbbf1SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 885fa0cbbf1SDavid Rientjes err = -ESRCH; 886fa0cbbf1SDavid Rientjes goto err_task_lock; 887fa0cbbf1SDavid Rientjes } 888fa0cbbf1SDavid Rientjes 889fa0cbbf1SDavid Rientjes /* 890fa0cbbf1SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 891fa0cbbf1SDavid Rientjes * value is always attainable. 892fa0cbbf1SDavid Rientjes */ 893fa0cbbf1SDavid Rientjes if (oom_adj == OOM_ADJUST_MAX) 894fa0cbbf1SDavid Rientjes oom_adj = OOM_SCORE_ADJ_MAX; 895fa0cbbf1SDavid Rientjes else 896fa0cbbf1SDavid Rientjes oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; 897fa0cbbf1SDavid Rientjes 898fa0cbbf1SDavid Rientjes if (oom_adj < task->signal->oom_score_adj && 899fa0cbbf1SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 900fa0cbbf1SDavid Rientjes err = -EACCES; 901fa0cbbf1SDavid Rientjes goto err_sighand; 902fa0cbbf1SDavid Rientjes } 903fa0cbbf1SDavid Rientjes 904fa0cbbf1SDavid Rientjes /* 905fa0cbbf1SDavid Rientjes * /proc/pid/oom_adj is provided for legacy purposes, ask users to use 906fa0cbbf1SDavid Rientjes * /proc/pid/oom_score_adj instead. 907fa0cbbf1SDavid Rientjes */ 90887ebdc00SAndrew Morton pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 909fa0cbbf1SDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 910fa0cbbf1SDavid Rientjes task_pid_nr(task)); 911fa0cbbf1SDavid Rientjes 912fa0cbbf1SDavid Rientjes task->signal->oom_score_adj = oom_adj; 913fa0cbbf1SDavid Rientjes trace_oom_score_adj_update(task); 914fa0cbbf1SDavid Rientjes err_sighand: 915fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 916fa0cbbf1SDavid Rientjes err_task_lock: 917fa0cbbf1SDavid Rientjes task_unlock(task); 918fa0cbbf1SDavid Rientjes put_task_struct(task); 919fa0cbbf1SDavid Rientjes out: 920fa0cbbf1SDavid Rientjes return err < 0 ? err : count; 921fa0cbbf1SDavid Rientjes } 922fa0cbbf1SDavid Rientjes 923fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = { 924fa0cbbf1SDavid Rientjes .read = oom_adj_read, 925fa0cbbf1SDavid Rientjes .write = oom_adj_write, 926fa0cbbf1SDavid Rientjes .llseek = generic_file_llseek, 927fa0cbbf1SDavid Rientjes }; 928fa0cbbf1SDavid Rientjes 929a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 930a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 931a63d83f4SDavid Rientjes { 932496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 933a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 934a9c58b90SDavid Rientjes short oom_score_adj = OOM_SCORE_ADJ_MIN; 935a63d83f4SDavid Rientjes unsigned long flags; 936a63d83f4SDavid Rientjes size_t len; 937a63d83f4SDavid Rientjes 938a63d83f4SDavid Rientjes if (!task) 939a63d83f4SDavid Rientjes return -ESRCH; 940a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 941a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 942a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 943a63d83f4SDavid Rientjes } 944a63d83f4SDavid Rientjes put_task_struct(task); 945a9c58b90SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); 946a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 947a63d83f4SDavid Rientjes } 948a63d83f4SDavid Rientjes 949a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 950a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 951a63d83f4SDavid Rientjes { 952a63d83f4SDavid Rientjes struct task_struct *task; 953a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 954a63d83f4SDavid Rientjes unsigned long flags; 9550a8cb8e3SAlexey Dobriyan int oom_score_adj; 956a63d83f4SDavid Rientjes int err; 957a63d83f4SDavid Rientjes 958a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 959a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 960a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 961723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 962723548bfSDavid Rientjes err = -EFAULT; 963723548bfSDavid Rientjes goto out; 964723548bfSDavid Rientjes } 965a63d83f4SDavid Rientjes 9660a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 967a63d83f4SDavid Rientjes if (err) 968723548bfSDavid Rientjes goto out; 969a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 970723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 971723548bfSDavid Rientjes err = -EINVAL; 972723548bfSDavid Rientjes goto out; 973723548bfSDavid Rientjes } 974a63d83f4SDavid Rientjes 975496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 976723548bfSDavid Rientjes if (!task) { 977723548bfSDavid Rientjes err = -ESRCH; 978723548bfSDavid Rientjes goto out; 979723548bfSDavid Rientjes } 980a63d83f4SDavid Rientjes 9813d5992d2SYing Han task_lock(task); 9823d5992d2SYing Han if (!task->mm) { 983723548bfSDavid Rientjes err = -EINVAL; 984723548bfSDavid Rientjes goto err_task_lock; 9853d5992d2SYing Han } 986d19d5476SDavid Rientjes 987d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 988d19d5476SDavid Rientjes err = -ESRCH; 989d19d5476SDavid Rientjes goto err_task_lock; 990d19d5476SDavid Rientjes } 991d19d5476SDavid Rientjes 992a9c58b90SDavid Rientjes if ((short)oom_score_adj < task->signal->oom_score_adj_min && 993d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 994d19d5476SDavid Rientjes err = -EACCES; 995d19d5476SDavid Rientjes goto err_sighand; 996d19d5476SDavid Rientjes } 997d19d5476SDavid Rientjes 998a9c58b90SDavid Rientjes task->signal->oom_score_adj = (short)oom_score_adj; 999dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1000a9c58b90SDavid Rientjes task->signal->oom_score_adj_min = (short)oom_score_adj; 100143d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 100201dc52ebSDavidlohr Bueso 1003723548bfSDavid Rientjes err_sighand: 1004a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1005d19d5476SDavid Rientjes err_task_lock: 1006d19d5476SDavid Rientjes task_unlock(task); 1007a63d83f4SDavid Rientjes put_task_struct(task); 1008723548bfSDavid Rientjes out: 1009723548bfSDavid Rientjes return err < 0 ? err : count; 1010a63d83f4SDavid Rientjes } 1011a63d83f4SDavid Rientjes 1012a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1013a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1014a63d83f4SDavid Rientjes .write = oom_score_adj_write, 10156038f373SArnd Bergmann .llseek = default_llseek, 1016a63d83f4SDavid Rientjes }; 1017a63d83f4SDavid Rientjes 10181da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10191da177e4SLinus Torvalds #define TMPBUFLEN 21 10201da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10211da177e4SLinus Torvalds size_t count, loff_t *ppos) 10221da177e4SLinus Torvalds { 1023496ad9aaSAl Viro struct inode * inode = file_inode(file); 102499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10251da177e4SLinus Torvalds ssize_t length; 10261da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10271da177e4SLinus Torvalds 102899f89551SEric W. Biederman if (!task) 102999f89551SEric W. Biederman return -ESRCH; 10301da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1031e1760bd5SEric W. Biederman from_kuid(file->f_cred->user_ns, 1032e1760bd5SEric W. Biederman audit_get_loginuid(task))); 103399f89551SEric W. Biederman put_task_struct(task); 10341da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10381da177e4SLinus Torvalds size_t count, loff_t *ppos) 10391da177e4SLinus Torvalds { 1040496ad9aaSAl Viro struct inode * inode = file_inode(file); 10411da177e4SLinus Torvalds char *page, *tmp; 10421da177e4SLinus Torvalds ssize_t length; 10431da177e4SLinus Torvalds uid_t loginuid; 1044e1760bd5SEric W. Biederman kuid_t kloginuid; 10451da177e4SLinus Torvalds 10467dc52157SPaul E. McKenney rcu_read_lock(); 10477dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 10487dc52157SPaul E. McKenney rcu_read_unlock(); 10491da177e4SLinus Torvalds return -EPERM; 10507dc52157SPaul E. McKenney } 10517dc52157SPaul E. McKenney rcu_read_unlock(); 10521da177e4SLinus Torvalds 1053e0182909SAl Viro if (count >= PAGE_SIZE) 1054e0182909SAl Viro count = PAGE_SIZE - 1; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (*ppos != 0) { 10571da177e4SLinus Torvalds /* No partial writes. */ 10581da177e4SLinus Torvalds return -EINVAL; 10591da177e4SLinus Torvalds } 1060e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10611da177e4SLinus Torvalds if (!page) 10621da177e4SLinus Torvalds return -ENOMEM; 10631da177e4SLinus Torvalds length = -EFAULT; 10641da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10651da177e4SLinus Torvalds goto out_free_page; 10661da177e4SLinus Torvalds 1067e0182909SAl Viro page[count] = '\0'; 10681da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10691da177e4SLinus Torvalds if (tmp == page) { 10701da177e4SLinus Torvalds length = -EINVAL; 10711da177e4SLinus Torvalds goto out_free_page; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds } 107481407c84SEric Paris 107581407c84SEric Paris /* is userspace tring to explicitly UNSET the loginuid? */ 107681407c84SEric Paris if (loginuid == AUDIT_UID_UNSET) { 107781407c84SEric Paris kloginuid = INVALID_UID; 107881407c84SEric Paris } else { 1079e1760bd5SEric W. Biederman kloginuid = make_kuid(file->f_cred->user_ns, loginuid); 1080e1760bd5SEric W. Biederman if (!uid_valid(kloginuid)) { 1081e1760bd5SEric W. Biederman length = -EINVAL; 1082e1760bd5SEric W. Biederman goto out_free_page; 1083e1760bd5SEric W. Biederman } 108481407c84SEric Paris } 1085e1760bd5SEric W. Biederman 1086e1760bd5SEric W. Biederman length = audit_set_loginuid(kloginuid); 10871da177e4SLinus Torvalds if (likely(length == 0)) 10881da177e4SLinus Torvalds length = count; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds out_free_page: 10911da177e4SLinus Torvalds free_page((unsigned long) page); 10921da177e4SLinus Torvalds return length; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 109500977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10961da177e4SLinus Torvalds .read = proc_loginuid_read, 10971da177e4SLinus Torvalds .write = proc_loginuid_write, 109887df8424SArnd Bergmann .llseek = generic_file_llseek, 10991da177e4SLinus Torvalds }; 11001e0bd755SEric Paris 11011e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11021e0bd755SEric Paris size_t count, loff_t *ppos) 11031e0bd755SEric Paris { 1104496ad9aaSAl Viro struct inode * inode = file_inode(file); 11051e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11061e0bd755SEric Paris ssize_t length; 11071e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11081e0bd755SEric Paris 11091e0bd755SEric Paris if (!task) 11101e0bd755SEric Paris return -ESRCH; 11111e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11121e0bd755SEric Paris audit_get_sessionid(task)); 11131e0bd755SEric Paris put_task_struct(task); 11141e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11151e0bd755SEric Paris } 11161e0bd755SEric Paris 11171e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11181e0bd755SEric Paris .read = proc_sessionid_read, 111987df8424SArnd Bergmann .llseek = generic_file_llseek, 11201e0bd755SEric Paris }; 11211da177e4SLinus Torvalds #endif 11221da177e4SLinus Torvalds 1123f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1124f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1125f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1126f4f154fdSAkinobu Mita { 1127496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 1128f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1129f4f154fdSAkinobu Mita size_t len; 1130f4f154fdSAkinobu Mita int make_it_fail; 1131f4f154fdSAkinobu Mita 1132f4f154fdSAkinobu Mita if (!task) 1133f4f154fdSAkinobu Mita return -ESRCH; 1134f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1135f4f154fdSAkinobu Mita put_task_struct(task); 1136f4f154fdSAkinobu Mita 1137f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11380c28f287SAkinobu Mita 11390c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1140f4f154fdSAkinobu Mita } 1141f4f154fdSAkinobu Mita 1142f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1143f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1144f4f154fdSAkinobu Mita { 1145f4f154fdSAkinobu Mita struct task_struct *task; 1146f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1147f4f154fdSAkinobu Mita int make_it_fail; 1148f4f154fdSAkinobu Mita 1149f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1150f4f154fdSAkinobu Mita return -EPERM; 1151f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1152f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1153f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1154f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1155f4f154fdSAkinobu Mita return -EFAULT; 1156cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1157cba8aafeSVincent Li if (*end) 1158cba8aafeSVincent Li return -EINVAL; 115916caed31SDave Jones if (make_it_fail < 0 || make_it_fail > 1) 116016caed31SDave Jones return -EINVAL; 116116caed31SDave Jones 1162496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 1163f4f154fdSAkinobu Mita if (!task) 1164f4f154fdSAkinobu Mita return -ESRCH; 1165f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1166f4f154fdSAkinobu Mita put_task_struct(task); 1167cba8aafeSVincent Li 1168cba8aafeSVincent Li return count; 1169f4f154fdSAkinobu Mita } 1170f4f154fdSAkinobu Mita 117100977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1172f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1173f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 117487df8424SArnd Bergmann .llseek = generic_file_llseek, 1175f4f154fdSAkinobu Mita }; 1176f4f154fdSAkinobu Mita #endif 1177f4f154fdSAkinobu Mita 11789745512cSArjan van de Ven 117943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 118043ae34cbSIngo Molnar /* 118143ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 118243ae34cbSIngo Molnar */ 118343ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 118443ae34cbSIngo Molnar { 118543ae34cbSIngo Molnar struct inode *inode = m->private; 118643ae34cbSIngo Molnar struct task_struct *p; 118743ae34cbSIngo Molnar 118843ae34cbSIngo Molnar p = get_proc_task(inode); 118943ae34cbSIngo Molnar if (!p) 119043ae34cbSIngo Molnar return -ESRCH; 119143ae34cbSIngo Molnar proc_sched_show_task(p, m); 119243ae34cbSIngo Molnar 119343ae34cbSIngo Molnar put_task_struct(p); 119443ae34cbSIngo Molnar 119543ae34cbSIngo Molnar return 0; 119643ae34cbSIngo Molnar } 119743ae34cbSIngo Molnar 119843ae34cbSIngo Molnar static ssize_t 119943ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 120043ae34cbSIngo Molnar size_t count, loff_t *offset) 120143ae34cbSIngo Molnar { 1202496ad9aaSAl Viro struct inode *inode = file_inode(file); 120343ae34cbSIngo Molnar struct task_struct *p; 120443ae34cbSIngo Molnar 120543ae34cbSIngo Molnar p = get_proc_task(inode); 120643ae34cbSIngo Molnar if (!p) 120743ae34cbSIngo Molnar return -ESRCH; 120843ae34cbSIngo Molnar proc_sched_set_task(p); 120943ae34cbSIngo Molnar 121043ae34cbSIngo Molnar put_task_struct(p); 121143ae34cbSIngo Molnar 121243ae34cbSIngo Molnar return count; 121343ae34cbSIngo Molnar } 121443ae34cbSIngo Molnar 121543ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 121643ae34cbSIngo Molnar { 1217c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 121843ae34cbSIngo Molnar } 121943ae34cbSIngo Molnar 122043ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 122143ae34cbSIngo Molnar .open = sched_open, 122243ae34cbSIngo Molnar .read = seq_read, 122343ae34cbSIngo Molnar .write = sched_write, 122443ae34cbSIngo Molnar .llseek = seq_lseek, 12255ea473a1SAlexey Dobriyan .release = single_release, 122643ae34cbSIngo Molnar }; 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar #endif 122943ae34cbSIngo Molnar 12305091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 12315091faa4SMike Galbraith /* 12325091faa4SMike Galbraith * Print out autogroup related information: 12335091faa4SMike Galbraith */ 12345091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 12355091faa4SMike Galbraith { 12365091faa4SMike Galbraith struct inode *inode = m->private; 12375091faa4SMike Galbraith struct task_struct *p; 12385091faa4SMike Galbraith 12395091faa4SMike Galbraith p = get_proc_task(inode); 12405091faa4SMike Galbraith if (!p) 12415091faa4SMike Galbraith return -ESRCH; 12425091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 12435091faa4SMike Galbraith 12445091faa4SMike Galbraith put_task_struct(p); 12455091faa4SMike Galbraith 12465091faa4SMike Galbraith return 0; 12475091faa4SMike Galbraith } 12485091faa4SMike Galbraith 12495091faa4SMike Galbraith static ssize_t 12505091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 12515091faa4SMike Galbraith size_t count, loff_t *offset) 12525091faa4SMike Galbraith { 1253496ad9aaSAl Viro struct inode *inode = file_inode(file); 12545091faa4SMike Galbraith struct task_struct *p; 12555091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 12560a8cb8e3SAlexey Dobriyan int nice; 12575091faa4SMike Galbraith int err; 12585091faa4SMike Galbraith 12595091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 12605091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 12615091faa4SMike Galbraith count = sizeof(buffer) - 1; 12625091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 12635091faa4SMike Galbraith return -EFAULT; 12645091faa4SMike Galbraith 12650a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 12660a8cb8e3SAlexey Dobriyan if (err < 0) 12670a8cb8e3SAlexey Dobriyan return err; 12685091faa4SMike Galbraith 12695091faa4SMike Galbraith p = get_proc_task(inode); 12705091faa4SMike Galbraith if (!p) 12715091faa4SMike Galbraith return -ESRCH; 12725091faa4SMike Galbraith 12732e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice); 12745091faa4SMike Galbraith if (err) 12755091faa4SMike Galbraith count = err; 12765091faa4SMike Galbraith 12775091faa4SMike Galbraith put_task_struct(p); 12785091faa4SMike Galbraith 12795091faa4SMike Galbraith return count; 12805091faa4SMike Galbraith } 12815091faa4SMike Galbraith 12825091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 12835091faa4SMike Galbraith { 12845091faa4SMike Galbraith int ret; 12855091faa4SMike Galbraith 12865091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 12875091faa4SMike Galbraith if (!ret) { 12885091faa4SMike Galbraith struct seq_file *m = filp->private_data; 12895091faa4SMike Galbraith 12905091faa4SMike Galbraith m->private = inode; 12915091faa4SMike Galbraith } 12925091faa4SMike Galbraith return ret; 12935091faa4SMike Galbraith } 12945091faa4SMike Galbraith 12955091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 12965091faa4SMike Galbraith .open = sched_autogroup_open, 12975091faa4SMike Galbraith .read = seq_read, 12985091faa4SMike Galbraith .write = sched_autogroup_write, 12995091faa4SMike Galbraith .llseek = seq_lseek, 13005091faa4SMike Galbraith .release = single_release, 13015091faa4SMike Galbraith }; 13025091faa4SMike Galbraith 13035091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 13045091faa4SMike Galbraith 13054614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 13064614a696Sjohn stultz size_t count, loff_t *offset) 13074614a696Sjohn stultz { 1308496ad9aaSAl Viro struct inode *inode = file_inode(file); 13094614a696Sjohn stultz struct task_struct *p; 13104614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 1311830e0fc9SDavid Rientjes const size_t maxlen = sizeof(buffer) - 1; 13124614a696Sjohn stultz 13134614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 1314830e0fc9SDavid Rientjes if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) 13154614a696Sjohn stultz return -EFAULT; 13164614a696Sjohn stultz 13174614a696Sjohn stultz p = get_proc_task(inode); 13184614a696Sjohn stultz if (!p) 13194614a696Sjohn stultz return -ESRCH; 13204614a696Sjohn stultz 13214614a696Sjohn stultz if (same_thread_group(current, p)) 13224614a696Sjohn stultz set_task_comm(p, buffer); 13234614a696Sjohn stultz else 13244614a696Sjohn stultz count = -EINVAL; 13254614a696Sjohn stultz 13264614a696Sjohn stultz put_task_struct(p); 13274614a696Sjohn stultz 13284614a696Sjohn stultz return count; 13294614a696Sjohn stultz } 13304614a696Sjohn stultz 13314614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 13324614a696Sjohn stultz { 13334614a696Sjohn stultz struct inode *inode = m->private; 13344614a696Sjohn stultz struct task_struct *p; 13354614a696Sjohn stultz 13364614a696Sjohn stultz p = get_proc_task(inode); 13374614a696Sjohn stultz if (!p) 13384614a696Sjohn stultz return -ESRCH; 13394614a696Sjohn stultz 13404614a696Sjohn stultz task_lock(p); 13414614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 13424614a696Sjohn stultz task_unlock(p); 13434614a696Sjohn stultz 13444614a696Sjohn stultz put_task_struct(p); 13454614a696Sjohn stultz 13464614a696Sjohn stultz return 0; 13474614a696Sjohn stultz } 13484614a696Sjohn stultz 13494614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 13504614a696Sjohn stultz { 1351c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 13524614a696Sjohn stultz } 13534614a696Sjohn stultz 13544614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 13554614a696Sjohn stultz .open = comm_open, 13564614a696Sjohn stultz .read = seq_read, 13574614a696Sjohn stultz .write = comm_write, 13584614a696Sjohn stultz .llseek = seq_lseek, 13594614a696Sjohn stultz .release = single_release, 13604614a696Sjohn stultz }; 13614614a696Sjohn stultz 13627773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1363925d1c40SMatt Helsley { 1364925d1c40SMatt Helsley struct task_struct *task; 1365925d1c40SMatt Helsley struct mm_struct *mm; 1366925d1c40SMatt Helsley struct file *exe_file; 1367925d1c40SMatt Helsley 13682b0143b5SDavid Howells task = get_proc_task(d_inode(dentry)); 1369925d1c40SMatt Helsley if (!task) 1370925d1c40SMatt Helsley return -ENOENT; 1371925d1c40SMatt Helsley mm = get_task_mm(task); 1372925d1c40SMatt Helsley put_task_struct(task); 1373925d1c40SMatt Helsley if (!mm) 1374925d1c40SMatt Helsley return -ENOENT; 1375925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1376925d1c40SMatt Helsley mmput(mm); 1377925d1c40SMatt Helsley if (exe_file) { 1378925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1379925d1c40SMatt Helsley path_get(&exe_file->f_path); 1380925d1c40SMatt Helsley fput(exe_file); 1381925d1c40SMatt Helsley return 0; 1382925d1c40SMatt Helsley } else 1383925d1c40SMatt Helsley return -ENOENT; 1384925d1c40SMatt Helsley } 1385925d1c40SMatt Helsley 13866e77137bSAl Viro static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie) 13871da177e4SLinus Torvalds { 13882b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 1389408ef013SChristoph Hellwig struct path path; 13901da177e4SLinus Torvalds int error = -EACCES; 13911da177e4SLinus Torvalds 1392778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1393778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13941da177e4SLinus Torvalds goto out; 13951da177e4SLinus Torvalds 1396408ef013SChristoph Hellwig error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1397408ef013SChristoph Hellwig if (error) 1398408ef013SChristoph Hellwig goto out; 1399408ef013SChristoph Hellwig 14006e77137bSAl Viro nd_jump_link(&path); 1401408ef013SChristoph Hellwig return NULL; 14021da177e4SLinus Torvalds out: 1403008b150aSAl Viro return ERR_PTR(error); 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14063dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 14071da177e4SLinus Torvalds { 1408e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 14093dcd25f3SJan Blunck char *pathname; 14101da177e4SLinus Torvalds int len; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds if (!tmp) 14131da177e4SLinus Torvalds return -ENOMEM; 14141da177e4SLinus Torvalds 14157b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 14163dcd25f3SJan Blunck len = PTR_ERR(pathname); 14173dcd25f3SJan Blunck if (IS_ERR(pathname)) 14181da177e4SLinus Torvalds goto out; 14193dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds if (len > buflen) 14221da177e4SLinus Torvalds len = buflen; 14233dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 14241da177e4SLinus Torvalds len = -EFAULT; 14251da177e4SLinus Torvalds out: 14261da177e4SLinus Torvalds free_page((unsigned long)tmp); 14271da177e4SLinus Torvalds return len; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 14311da177e4SLinus Torvalds { 14321da177e4SLinus Torvalds int error = -EACCES; 14332b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 14343dcd25f3SJan Blunck struct path path; 14351da177e4SLinus Torvalds 1436778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1437778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds 14407773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path); 14411da177e4SLinus Torvalds if (error) 14421da177e4SLinus Torvalds goto out; 14431da177e4SLinus Torvalds 14443dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14453dcd25f3SJan Blunck path_put(&path); 14461da177e4SLinus Torvalds out: 14471da177e4SLinus Torvalds return error; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 1450faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = { 14511da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14526d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14536d76fa58SLinus Torvalds .setattr = proc_setattr, 14541da177e4SLinus Torvalds }; 14551da177e4SLinus Torvalds 145628a6d671SEric W. Biederman 145728a6d671SEric W. Biederman /* building an inode */ 145828a6d671SEric W. Biederman 14596b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 146028a6d671SEric W. Biederman { 146128a6d671SEric W. Biederman struct inode * inode; 146228a6d671SEric W. Biederman struct proc_inode *ei; 1463c69e8d9cSDavid Howells const struct cred *cred; 146428a6d671SEric W. Biederman 146528a6d671SEric W. Biederman /* We need a new inode */ 146628a6d671SEric W. Biederman 146728a6d671SEric W. Biederman inode = new_inode(sb); 146828a6d671SEric W. Biederman if (!inode) 146928a6d671SEric W. Biederman goto out; 147028a6d671SEric W. Biederman 147128a6d671SEric W. Biederman /* Common stuff */ 147228a6d671SEric W. Biederman ei = PROC_I(inode); 147385fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 147428a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 147528a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 147628a6d671SEric W. Biederman 147728a6d671SEric W. Biederman /* 147828a6d671SEric W. Biederman * grab the reference to task. 147928a6d671SEric W. Biederman */ 14801a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 148128a6d671SEric W. Biederman if (!ei->pid) 148228a6d671SEric W. Biederman goto out_unlock; 148328a6d671SEric W. Biederman 148428a6d671SEric W. Biederman if (task_dumpable(task)) { 1485c69e8d9cSDavid Howells rcu_read_lock(); 1486c69e8d9cSDavid Howells cred = __task_cred(task); 1487c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1488c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1489c69e8d9cSDavid Howells rcu_read_unlock(); 149028a6d671SEric W. Biederman } 149128a6d671SEric W. Biederman security_task_to_inode(task, inode); 149228a6d671SEric W. Biederman 149328a6d671SEric W. Biederman out: 149428a6d671SEric W. Biederman return inode; 149528a6d671SEric W. Biederman 149628a6d671SEric W. Biederman out_unlock: 149728a6d671SEric W. Biederman iput(inode); 149828a6d671SEric W. Biederman return NULL; 149928a6d671SEric W. Biederman } 150028a6d671SEric W. Biederman 15016b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 150228a6d671SEric W. Biederman { 15032b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 150428a6d671SEric W. Biederman struct task_struct *task; 1505c69e8d9cSDavid Howells const struct cred *cred; 15060499680aSVasiliy Kulikov struct pid_namespace *pid = dentry->d_sb->s_fs_info; 1507c69e8d9cSDavid Howells 150828a6d671SEric W. Biederman generic_fillattr(inode, stat); 150928a6d671SEric W. Biederman 151028a6d671SEric W. Biederman rcu_read_lock(); 1511dcb0f222SEric W. Biederman stat->uid = GLOBAL_ROOT_UID; 1512dcb0f222SEric W. Biederman stat->gid = GLOBAL_ROOT_GID; 151328a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 151428a6d671SEric W. Biederman if (task) { 15150499680aSVasiliy Kulikov if (!has_pid_permissions(pid, task, 2)) { 15160499680aSVasiliy Kulikov rcu_read_unlock(); 15170499680aSVasiliy Kulikov /* 15180499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists, 15190499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir(). 15200499680aSVasiliy Kulikov */ 15210499680aSVasiliy Kulikov return -ENOENT; 15220499680aSVasiliy Kulikov } 152328a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 152428a6d671SEric W. Biederman task_dumpable(task)) { 1525c69e8d9cSDavid Howells cred = __task_cred(task); 1526c69e8d9cSDavid Howells stat->uid = cred->euid; 1527c69e8d9cSDavid Howells stat->gid = cred->egid; 152828a6d671SEric W. Biederman } 152928a6d671SEric W. Biederman } 153028a6d671SEric W. Biederman rcu_read_unlock(); 153128a6d671SEric W. Biederman return 0; 153228a6d671SEric W. Biederman } 153328a6d671SEric W. Biederman 153428a6d671SEric W. Biederman /* dentry stuff */ 153528a6d671SEric W. Biederman 153628a6d671SEric W. Biederman /* 153728a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 153828a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 153928a6d671SEric W. Biederman * due to the way we treat inodes. 154028a6d671SEric W. Biederman * 154128a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 154228a6d671SEric W. Biederman * performed a setuid(), etc. 154328a6d671SEric W. Biederman * 154428a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 154528a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 154628a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 154728a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 154828a6d671SEric W. Biederman * made this apply to all per process world readable and executable 154928a6d671SEric W. Biederman * directories. 155028a6d671SEric W. Biederman */ 15510b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags) 155228a6d671SEric W. Biederman { 155334286d66SNick Piggin struct inode *inode; 155434286d66SNick Piggin struct task_struct *task; 1555c69e8d9cSDavid Howells const struct cred *cred; 1556c69e8d9cSDavid Howells 15570b728e19SAl Viro if (flags & LOOKUP_RCU) 155834286d66SNick Piggin return -ECHILD; 155934286d66SNick Piggin 15602b0143b5SDavid Howells inode = d_inode(dentry); 156134286d66SNick Piggin task = get_proc_task(inode); 156234286d66SNick Piggin 156328a6d671SEric W. Biederman if (task) { 156428a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 156528a6d671SEric W. Biederman task_dumpable(task)) { 1566c69e8d9cSDavid Howells rcu_read_lock(); 1567c69e8d9cSDavid Howells cred = __task_cred(task); 1568c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1569c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1570c69e8d9cSDavid Howells rcu_read_unlock(); 157128a6d671SEric W. Biederman } else { 1572dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1573dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 157428a6d671SEric W. Biederman } 157528a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 157628a6d671SEric W. Biederman security_task_to_inode(task, inode); 157728a6d671SEric W. Biederman put_task_struct(task); 157828a6d671SEric W. Biederman return 1; 157928a6d671SEric W. Biederman } 158028a6d671SEric W. Biederman return 0; 158128a6d671SEric W. Biederman } 158228a6d671SEric W. Biederman 1583d855a4b7SOleg Nesterov static inline bool proc_inode_is_dead(struct inode *inode) 1584d855a4b7SOleg Nesterov { 1585d855a4b7SOleg Nesterov return !proc_pid(inode)->tasks[PIDTYPE_PID].first; 1586d855a4b7SOleg Nesterov } 1587d855a4b7SOleg Nesterov 15881dd704b6SDavid Howells int pid_delete_dentry(const struct dentry *dentry) 15891dd704b6SDavid Howells { 15901dd704b6SDavid Howells /* Is the task we represent dead? 15911dd704b6SDavid Howells * If so, then don't put the dentry on the lru list, 15921dd704b6SDavid Howells * kill it immediately. 15931dd704b6SDavid Howells */ 15942b0143b5SDavid Howells return proc_inode_is_dead(d_inode(dentry)); 15951dd704b6SDavid Howells } 15961dd704b6SDavid Howells 15976b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 159828a6d671SEric W. Biederman { 159928a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 160028a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 160128a6d671SEric W. Biederman }; 160228a6d671SEric W. Biederman 160328a6d671SEric W. Biederman /* Lookups */ 160428a6d671SEric W. Biederman 16051c0d04c9SEric W. Biederman /* 16061c0d04c9SEric W. Biederman * Fill a directory entry. 16071c0d04c9SEric W. Biederman * 16081c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 16091c0d04c9SEric W. Biederman * file type from dcache entry. 16101c0d04c9SEric W. Biederman * 16111c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 16121c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 16131c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 16141c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 16151c0d04c9SEric W. Biederman * by stat. 16161c0d04c9SEric W. Biederman */ 1617f0c3b509SAl Viro bool proc_fill_cache(struct file *file, struct dir_context *ctx, 16186b4e306aSEric W. Biederman const char *name, int len, 1619c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 162061a28784SEric W. Biederman { 1621f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry; 16221df98b8bSAl Viro struct qstr qname = QSTR_INIT(name, len); 162361a28784SEric W. Biederman struct inode *inode; 16241df98b8bSAl Viro unsigned type; 16251df98b8bSAl Viro ino_t ino; 162661a28784SEric W. Biederman 16271df98b8bSAl Viro child = d_hash_and_lookup(dir, &qname); 162861a28784SEric W. Biederman if (!child) { 16291df98b8bSAl Viro child = d_alloc(dir, &qname); 16301df98b8bSAl Viro if (!child) 163161a28784SEric W. Biederman goto end_instantiate; 16322b0143b5SDavid Howells if (instantiate(d_inode(dir), child, task, ptr) < 0) { 16331df98b8bSAl Viro dput(child); 16341df98b8bSAl Viro goto end_instantiate; 16351df98b8bSAl Viro } 16361df98b8bSAl Viro } 16372b0143b5SDavid Howells inode = d_inode(child); 163861a28784SEric W. Biederman ino = inode->i_ino; 163961a28784SEric W. Biederman type = inode->i_mode >> 12; 164061a28784SEric W. Biederman dput(child); 1641f0c3b509SAl Viro return dir_emit(ctx, name, len, ino, type); 16421df98b8bSAl Viro 16431df98b8bSAl Viro end_instantiate: 16441df98b8bSAl Viro return dir_emit(ctx, name, len, 1, DT_UNKNOWN); 164561a28784SEric W. Biederman } 164661a28784SEric W. Biederman 1647640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 1648640708a2SPavel Emelyanov 1649640708a2SPavel Emelyanov /* 1650640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs 1651640708a2SPavel Emelyanov * which represent vma start and end addresses. 1652640708a2SPavel Emelyanov */ 1653640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry, 1654640708a2SPavel Emelyanov unsigned long *start, unsigned long *end) 1655640708a2SPavel Emelyanov { 1656640708a2SPavel Emelyanov if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) 1657640708a2SPavel Emelyanov return -EINVAL; 1658640708a2SPavel Emelyanov 1659640708a2SPavel Emelyanov return 0; 1660640708a2SPavel Emelyanov } 1661640708a2SPavel Emelyanov 16620b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) 1663640708a2SPavel Emelyanov { 1664640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1665640708a2SPavel Emelyanov bool exact_vma_exists = false; 1666640708a2SPavel Emelyanov struct mm_struct *mm = NULL; 1667640708a2SPavel Emelyanov struct task_struct *task; 1668640708a2SPavel Emelyanov const struct cred *cred; 1669640708a2SPavel Emelyanov struct inode *inode; 1670640708a2SPavel Emelyanov int status = 0; 1671640708a2SPavel Emelyanov 16720b728e19SAl Viro if (flags & LOOKUP_RCU) 1673640708a2SPavel Emelyanov return -ECHILD; 1674640708a2SPavel Emelyanov 1675640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) { 167641735818SZhao Hongjiang status = -EPERM; 1677640708a2SPavel Emelyanov goto out_notask; 1678640708a2SPavel Emelyanov } 1679640708a2SPavel Emelyanov 16802b0143b5SDavid Howells inode = d_inode(dentry); 1681640708a2SPavel Emelyanov task = get_proc_task(inode); 1682640708a2SPavel Emelyanov if (!task) 1683640708a2SPavel Emelyanov goto out_notask; 1684640708a2SPavel Emelyanov 16852344bec7SCong Wang mm = mm_access(task, PTRACE_MODE_READ); 16862344bec7SCong Wang if (IS_ERR_OR_NULL(mm)) 1687640708a2SPavel Emelyanov goto out; 1688640708a2SPavel Emelyanov 1689640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 1690640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1691640708a2SPavel Emelyanov exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); 1692640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1693640708a2SPavel Emelyanov } 1694640708a2SPavel Emelyanov 1695640708a2SPavel Emelyanov mmput(mm); 1696640708a2SPavel Emelyanov 1697640708a2SPavel Emelyanov if (exact_vma_exists) { 1698640708a2SPavel Emelyanov if (task_dumpable(task)) { 1699640708a2SPavel Emelyanov rcu_read_lock(); 1700640708a2SPavel Emelyanov cred = __task_cred(task); 1701640708a2SPavel Emelyanov inode->i_uid = cred->euid; 1702640708a2SPavel Emelyanov inode->i_gid = cred->egid; 1703640708a2SPavel Emelyanov rcu_read_unlock(); 1704640708a2SPavel Emelyanov } else { 1705dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1706dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1707640708a2SPavel Emelyanov } 1708640708a2SPavel Emelyanov security_task_to_inode(task, inode); 1709640708a2SPavel Emelyanov status = 1; 1710640708a2SPavel Emelyanov } 1711640708a2SPavel Emelyanov 1712640708a2SPavel Emelyanov out: 1713640708a2SPavel Emelyanov put_task_struct(task); 1714640708a2SPavel Emelyanov 1715640708a2SPavel Emelyanov out_notask: 1716640708a2SPavel Emelyanov return status; 1717640708a2SPavel Emelyanov } 1718640708a2SPavel Emelyanov 1719640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = { 1720640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate, 1721640708a2SPavel Emelyanov .d_delete = pid_delete_dentry, 1722640708a2SPavel Emelyanov }; 1723640708a2SPavel Emelyanov 1724640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path) 1725640708a2SPavel Emelyanov { 1726640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1727640708a2SPavel Emelyanov struct vm_area_struct *vma; 1728640708a2SPavel Emelyanov struct task_struct *task; 1729640708a2SPavel Emelyanov struct mm_struct *mm; 1730640708a2SPavel Emelyanov int rc; 1731640708a2SPavel Emelyanov 1732640708a2SPavel Emelyanov rc = -ENOENT; 17332b0143b5SDavid Howells task = get_proc_task(d_inode(dentry)); 1734640708a2SPavel Emelyanov if (!task) 1735640708a2SPavel Emelyanov goto out; 1736640708a2SPavel Emelyanov 1737640708a2SPavel Emelyanov mm = get_task_mm(task); 1738640708a2SPavel Emelyanov put_task_struct(task); 1739640708a2SPavel Emelyanov if (!mm) 1740640708a2SPavel Emelyanov goto out; 1741640708a2SPavel Emelyanov 1742640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 1743640708a2SPavel Emelyanov if (rc) 1744640708a2SPavel Emelyanov goto out_mmput; 1745640708a2SPavel Emelyanov 174670335abbSArtem Fetishev rc = -ENOENT; 1747640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1748640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1749640708a2SPavel Emelyanov if (vma && vma->vm_file) { 1750640708a2SPavel Emelyanov *path = vma->vm_file->f_path; 1751640708a2SPavel Emelyanov path_get(path); 1752640708a2SPavel Emelyanov rc = 0; 1753640708a2SPavel Emelyanov } 1754640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1755640708a2SPavel Emelyanov 1756640708a2SPavel Emelyanov out_mmput: 1757640708a2SPavel Emelyanov mmput(mm); 1758640708a2SPavel Emelyanov out: 1759640708a2SPavel Emelyanov return rc; 1760640708a2SPavel Emelyanov } 1761640708a2SPavel Emelyanov 1762640708a2SPavel Emelyanov struct map_files_info { 17637b540d06SAl Viro fmode_t mode; 1764640708a2SPavel Emelyanov unsigned long len; 1765640708a2SPavel Emelyanov unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ 1766640708a2SPavel Emelyanov }; 1767640708a2SPavel Emelyanov 1768c52a47acSAl Viro static int 1769640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 1770640708a2SPavel Emelyanov struct task_struct *task, const void *ptr) 1771640708a2SPavel Emelyanov { 17727b540d06SAl Viro fmode_t mode = (fmode_t)(unsigned long)ptr; 1773640708a2SPavel Emelyanov struct proc_inode *ei; 1774640708a2SPavel Emelyanov struct inode *inode; 1775640708a2SPavel Emelyanov 1776640708a2SPavel Emelyanov inode = proc_pid_make_inode(dir->i_sb, task); 1777640708a2SPavel Emelyanov if (!inode) 1778c52a47acSAl Viro return -ENOENT; 1779640708a2SPavel Emelyanov 1780640708a2SPavel Emelyanov ei = PROC_I(inode); 1781640708a2SPavel Emelyanov ei->op.proc_get_link = proc_map_files_get_link; 1782640708a2SPavel Emelyanov 1783640708a2SPavel Emelyanov inode->i_op = &proc_pid_link_inode_operations; 1784640708a2SPavel Emelyanov inode->i_size = 64; 1785640708a2SPavel Emelyanov inode->i_mode = S_IFLNK; 1786640708a2SPavel Emelyanov 17877b540d06SAl Viro if (mode & FMODE_READ) 1788640708a2SPavel Emelyanov inode->i_mode |= S_IRUSR; 17897b540d06SAl Viro if (mode & FMODE_WRITE) 1790640708a2SPavel Emelyanov inode->i_mode |= S_IWUSR; 1791640708a2SPavel Emelyanov 1792640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations); 1793640708a2SPavel Emelyanov d_add(dentry, inode); 1794640708a2SPavel Emelyanov 1795c52a47acSAl Viro return 0; 1796640708a2SPavel Emelyanov } 1797640708a2SPavel Emelyanov 1798640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir, 179900cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 1800640708a2SPavel Emelyanov { 1801640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1802640708a2SPavel Emelyanov struct vm_area_struct *vma; 1803640708a2SPavel Emelyanov struct task_struct *task; 1804c52a47acSAl Viro int result; 1805640708a2SPavel Emelyanov struct mm_struct *mm; 1806640708a2SPavel Emelyanov 1807c52a47acSAl Viro result = -EPERM; 1808640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1809640708a2SPavel Emelyanov goto out; 1810640708a2SPavel Emelyanov 1811c52a47acSAl Viro result = -ENOENT; 1812640708a2SPavel Emelyanov task = get_proc_task(dir); 1813640708a2SPavel Emelyanov if (!task) 1814640708a2SPavel Emelyanov goto out; 1815640708a2SPavel Emelyanov 1816c52a47acSAl Viro result = -EACCES; 1817eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1818640708a2SPavel Emelyanov goto out_put_task; 1819640708a2SPavel Emelyanov 1820c52a47acSAl Viro result = -ENOENT; 1821640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 1822eb94cd96SCyrill Gorcunov goto out_put_task; 1823640708a2SPavel Emelyanov 1824640708a2SPavel Emelyanov mm = get_task_mm(task); 1825640708a2SPavel Emelyanov if (!mm) 1826eb94cd96SCyrill Gorcunov goto out_put_task; 1827640708a2SPavel Emelyanov 1828640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1829640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1830640708a2SPavel Emelyanov if (!vma) 1831640708a2SPavel Emelyanov goto out_no_vma; 1832640708a2SPavel Emelyanov 183305f56484SStanislav Kinsbursky if (vma->vm_file) 18347b540d06SAl Viro result = proc_map_files_instantiate(dir, dentry, task, 18357b540d06SAl Viro (void *)(unsigned long)vma->vm_file->f_mode); 1836640708a2SPavel Emelyanov 1837640708a2SPavel Emelyanov out_no_vma: 1838640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1839640708a2SPavel Emelyanov mmput(mm); 1840640708a2SPavel Emelyanov out_put_task: 1841640708a2SPavel Emelyanov put_task_struct(task); 1842640708a2SPavel Emelyanov out: 1843c52a47acSAl Viro return ERR_PTR(result); 1844640708a2SPavel Emelyanov } 1845640708a2SPavel Emelyanov 1846640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = { 1847640708a2SPavel Emelyanov .lookup = proc_map_files_lookup, 1848640708a2SPavel Emelyanov .permission = proc_fd_permission, 1849640708a2SPavel Emelyanov .setattr = proc_setattr, 1850640708a2SPavel Emelyanov }; 1851640708a2SPavel Emelyanov 1852640708a2SPavel Emelyanov static int 1853f0c3b509SAl Viro proc_map_files_readdir(struct file *file, struct dir_context *ctx) 1854640708a2SPavel Emelyanov { 1855640708a2SPavel Emelyanov struct vm_area_struct *vma; 1856640708a2SPavel Emelyanov struct task_struct *task; 1857640708a2SPavel Emelyanov struct mm_struct *mm; 1858f0c3b509SAl Viro unsigned long nr_files, pos, i; 1859f0c3b509SAl Viro struct flex_array *fa = NULL; 1860f0c3b509SAl Viro struct map_files_info info; 1861f0c3b509SAl Viro struct map_files_info *p; 1862640708a2SPavel Emelyanov int ret; 1863640708a2SPavel Emelyanov 186441735818SZhao Hongjiang ret = -EPERM; 1865640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1866640708a2SPavel Emelyanov goto out; 1867640708a2SPavel Emelyanov 1868640708a2SPavel Emelyanov ret = -ENOENT; 1869f0c3b509SAl Viro task = get_proc_task(file_inode(file)); 1870640708a2SPavel Emelyanov if (!task) 1871640708a2SPavel Emelyanov goto out; 1872640708a2SPavel Emelyanov 1873640708a2SPavel Emelyanov ret = -EACCES; 1874eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1875640708a2SPavel Emelyanov goto out_put_task; 1876640708a2SPavel Emelyanov 1877640708a2SPavel Emelyanov ret = 0; 1878f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 1879eb94cd96SCyrill Gorcunov goto out_put_task; 1880640708a2SPavel Emelyanov 1881640708a2SPavel Emelyanov mm = get_task_mm(task); 1882640708a2SPavel Emelyanov if (!mm) 1883eb94cd96SCyrill Gorcunov goto out_put_task; 1884640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1885640708a2SPavel Emelyanov 1886640708a2SPavel Emelyanov nr_files = 0; 1887640708a2SPavel Emelyanov 1888640708a2SPavel Emelyanov /* 1889640708a2SPavel Emelyanov * We need two passes here: 1890640708a2SPavel Emelyanov * 1891640708a2SPavel Emelyanov * 1) Collect vmas of mapped files with mmap_sem taken 1892640708a2SPavel Emelyanov * 2) Release mmap_sem and instantiate entries 1893640708a2SPavel Emelyanov * 1894640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir() 1895640708a2SPavel Emelyanov * routine might require mmap_sem taken in might_fault(). 1896640708a2SPavel Emelyanov */ 1897640708a2SPavel Emelyanov 1898640708a2SPavel Emelyanov for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 1899f0c3b509SAl Viro if (vma->vm_file && ++pos > ctx->pos) 1900640708a2SPavel Emelyanov nr_files++; 1901640708a2SPavel Emelyanov } 1902640708a2SPavel Emelyanov 1903640708a2SPavel Emelyanov if (nr_files) { 1904640708a2SPavel Emelyanov fa = flex_array_alloc(sizeof(info), nr_files, 1905640708a2SPavel Emelyanov GFP_KERNEL); 1906640708a2SPavel Emelyanov if (!fa || flex_array_prealloc(fa, 0, nr_files, 1907640708a2SPavel Emelyanov GFP_KERNEL)) { 1908640708a2SPavel Emelyanov ret = -ENOMEM; 1909640708a2SPavel Emelyanov if (fa) 1910640708a2SPavel Emelyanov flex_array_free(fa); 1911640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1912640708a2SPavel Emelyanov mmput(mm); 1913eb94cd96SCyrill Gorcunov goto out_put_task; 1914640708a2SPavel Emelyanov } 1915640708a2SPavel Emelyanov for (i = 0, vma = mm->mmap, pos = 2; vma; 1916640708a2SPavel Emelyanov vma = vma->vm_next) { 1917640708a2SPavel Emelyanov if (!vma->vm_file) 1918640708a2SPavel Emelyanov continue; 1919f0c3b509SAl Viro if (++pos <= ctx->pos) 1920640708a2SPavel Emelyanov continue; 1921640708a2SPavel Emelyanov 19227b540d06SAl Viro info.mode = vma->vm_file->f_mode; 1923640708a2SPavel Emelyanov info.len = snprintf(info.name, 1924640708a2SPavel Emelyanov sizeof(info.name), "%lx-%lx", 1925640708a2SPavel Emelyanov vma->vm_start, vma->vm_end); 1926640708a2SPavel Emelyanov if (flex_array_put(fa, i++, &info, GFP_KERNEL)) 1927640708a2SPavel Emelyanov BUG(); 1928640708a2SPavel Emelyanov } 1929640708a2SPavel Emelyanov } 1930640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1931640708a2SPavel Emelyanov 1932640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) { 1933640708a2SPavel Emelyanov p = flex_array_get(fa, i); 1934f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, 1935640708a2SPavel Emelyanov p->name, p->len, 1936640708a2SPavel Emelyanov proc_map_files_instantiate, 19377b540d06SAl Viro task, 1938f0c3b509SAl Viro (void *)(unsigned long)p->mode)) 1939640708a2SPavel Emelyanov break; 1940f0c3b509SAl Viro ctx->pos++; 1941640708a2SPavel Emelyanov } 1942640708a2SPavel Emelyanov if (fa) 1943640708a2SPavel Emelyanov flex_array_free(fa); 1944640708a2SPavel Emelyanov mmput(mm); 1945640708a2SPavel Emelyanov 1946640708a2SPavel Emelyanov out_put_task: 1947640708a2SPavel Emelyanov put_task_struct(task); 1948640708a2SPavel Emelyanov out: 1949640708a2SPavel Emelyanov return ret; 1950640708a2SPavel Emelyanov } 1951640708a2SPavel Emelyanov 1952640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = { 1953640708a2SPavel Emelyanov .read = generic_read_dir, 1954f0c3b509SAl Viro .iterate = proc_map_files_readdir, 1955640708a2SPavel Emelyanov .llseek = default_llseek, 1956640708a2SPavel Emelyanov }; 1957640708a2SPavel Emelyanov 195848f6a7a5SPavel Emelyanov struct timers_private { 195948f6a7a5SPavel Emelyanov struct pid *pid; 196048f6a7a5SPavel Emelyanov struct task_struct *task; 196148f6a7a5SPavel Emelyanov struct sighand_struct *sighand; 196257b8015eSPavel Emelyanov struct pid_namespace *ns; 196348f6a7a5SPavel Emelyanov unsigned long flags; 196448f6a7a5SPavel Emelyanov }; 196548f6a7a5SPavel Emelyanov 196648f6a7a5SPavel Emelyanov static void *timers_start(struct seq_file *m, loff_t *pos) 196748f6a7a5SPavel Emelyanov { 196848f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 196948f6a7a5SPavel Emelyanov 197048f6a7a5SPavel Emelyanov tp->task = get_pid_task(tp->pid, PIDTYPE_PID); 197148f6a7a5SPavel Emelyanov if (!tp->task) 197248f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH); 197348f6a7a5SPavel Emelyanov 197448f6a7a5SPavel Emelyanov tp->sighand = lock_task_sighand(tp->task, &tp->flags); 197548f6a7a5SPavel Emelyanov if (!tp->sighand) 197648f6a7a5SPavel Emelyanov return ERR_PTR(-ESRCH); 197748f6a7a5SPavel Emelyanov 197848f6a7a5SPavel Emelyanov return seq_list_start(&tp->task->signal->posix_timers, *pos); 197948f6a7a5SPavel Emelyanov } 198048f6a7a5SPavel Emelyanov 198148f6a7a5SPavel Emelyanov static void *timers_next(struct seq_file *m, void *v, loff_t *pos) 198248f6a7a5SPavel Emelyanov { 198348f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 198448f6a7a5SPavel Emelyanov return seq_list_next(v, &tp->task->signal->posix_timers, pos); 198548f6a7a5SPavel Emelyanov } 198648f6a7a5SPavel Emelyanov 198748f6a7a5SPavel Emelyanov static void timers_stop(struct seq_file *m, void *v) 198848f6a7a5SPavel Emelyanov { 198948f6a7a5SPavel Emelyanov struct timers_private *tp = m->private; 199048f6a7a5SPavel Emelyanov 199148f6a7a5SPavel Emelyanov if (tp->sighand) { 199248f6a7a5SPavel Emelyanov unlock_task_sighand(tp->task, &tp->flags); 199348f6a7a5SPavel Emelyanov tp->sighand = NULL; 199448f6a7a5SPavel Emelyanov } 199548f6a7a5SPavel Emelyanov 199648f6a7a5SPavel Emelyanov if (tp->task) { 199748f6a7a5SPavel Emelyanov put_task_struct(tp->task); 199848f6a7a5SPavel Emelyanov tp->task = NULL; 199948f6a7a5SPavel Emelyanov } 200048f6a7a5SPavel Emelyanov } 200148f6a7a5SPavel Emelyanov 200248f6a7a5SPavel Emelyanov static int show_timer(struct seq_file *m, void *v) 200348f6a7a5SPavel Emelyanov { 200448f6a7a5SPavel Emelyanov struct k_itimer *timer; 200557b8015eSPavel Emelyanov struct timers_private *tp = m->private; 200657b8015eSPavel Emelyanov int notify; 2007cedbccabSAlexey Dobriyan static const char * const nstr[] = { 200857b8015eSPavel Emelyanov [SIGEV_SIGNAL] = "signal", 200957b8015eSPavel Emelyanov [SIGEV_NONE] = "none", 201057b8015eSPavel Emelyanov [SIGEV_THREAD] = "thread", 201157b8015eSPavel Emelyanov }; 201248f6a7a5SPavel Emelyanov 201348f6a7a5SPavel Emelyanov timer = list_entry((struct list_head *)v, struct k_itimer, list); 201457b8015eSPavel Emelyanov notify = timer->it_sigev_notify; 201557b8015eSPavel Emelyanov 201648f6a7a5SPavel Emelyanov seq_printf(m, "ID: %d\n", timer->it_id); 201725ce3191SJoe Perches seq_printf(m, "signal: %d/%p\n", 201825ce3191SJoe Perches timer->sigq->info.si_signo, 201957b8015eSPavel Emelyanov timer->sigq->info.si_value.sival_ptr); 202057b8015eSPavel Emelyanov seq_printf(m, "notify: %s/%s.%d\n", 202157b8015eSPavel Emelyanov nstr[notify & ~SIGEV_THREAD_ID], 202257b8015eSPavel Emelyanov (notify & SIGEV_THREAD_ID) ? "tid" : "pid", 202357b8015eSPavel Emelyanov pid_nr_ns(timer->it_pid, tp->ns)); 202415ef0298SPavel Tikhomirov seq_printf(m, "ClockID: %d\n", timer->it_clock); 202548f6a7a5SPavel Emelyanov 202648f6a7a5SPavel Emelyanov return 0; 202748f6a7a5SPavel Emelyanov } 202848f6a7a5SPavel Emelyanov 202948f6a7a5SPavel Emelyanov static const struct seq_operations proc_timers_seq_ops = { 203048f6a7a5SPavel Emelyanov .start = timers_start, 203148f6a7a5SPavel Emelyanov .next = timers_next, 203248f6a7a5SPavel Emelyanov .stop = timers_stop, 203348f6a7a5SPavel Emelyanov .show = show_timer, 203448f6a7a5SPavel Emelyanov }; 203548f6a7a5SPavel Emelyanov 203648f6a7a5SPavel Emelyanov static int proc_timers_open(struct inode *inode, struct file *file) 203748f6a7a5SPavel Emelyanov { 203848f6a7a5SPavel Emelyanov struct timers_private *tp; 203948f6a7a5SPavel Emelyanov 204048f6a7a5SPavel Emelyanov tp = __seq_open_private(file, &proc_timers_seq_ops, 204148f6a7a5SPavel Emelyanov sizeof(struct timers_private)); 204248f6a7a5SPavel Emelyanov if (!tp) 204348f6a7a5SPavel Emelyanov return -ENOMEM; 204448f6a7a5SPavel Emelyanov 204548f6a7a5SPavel Emelyanov tp->pid = proc_pid(inode); 204657b8015eSPavel Emelyanov tp->ns = inode->i_sb->s_fs_info; 204748f6a7a5SPavel Emelyanov return 0; 204848f6a7a5SPavel Emelyanov } 204948f6a7a5SPavel Emelyanov 205048f6a7a5SPavel Emelyanov static const struct file_operations proc_timers_operations = { 205148f6a7a5SPavel Emelyanov .open = proc_timers_open, 205248f6a7a5SPavel Emelyanov .read = seq_read, 205348f6a7a5SPavel Emelyanov .llseek = seq_lseek, 205448f6a7a5SPavel Emelyanov .release = seq_release_private, 205548f6a7a5SPavel Emelyanov }; 2056640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */ 2057640708a2SPavel Emelyanov 2058c52a47acSAl Viro static int proc_pident_instantiate(struct inode *dir, 2059c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2060444ceed8SEric W. Biederman { 2061c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2062444ceed8SEric W. Biederman struct inode *inode; 2063444ceed8SEric W. Biederman struct proc_inode *ei; 2064444ceed8SEric W. Biederman 206561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2066444ceed8SEric W. Biederman if (!inode) 2067444ceed8SEric W. Biederman goto out; 2068444ceed8SEric W. Biederman 2069444ceed8SEric W. Biederman ei = PROC_I(inode); 2070444ceed8SEric W. Biederman inode->i_mode = p->mode; 2071444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2072bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2073444ceed8SEric W. Biederman if (p->iop) 2074444ceed8SEric W. Biederman inode->i_op = p->iop; 2075444ceed8SEric W. Biederman if (p->fop) 2076444ceed8SEric W. Biederman inode->i_fop = p->fop; 2077444ceed8SEric W. Biederman ei->op = p->op; 2078fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2079444ceed8SEric W. Biederman d_add(dentry, inode); 2080444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 20810b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2082c52a47acSAl Viro return 0; 2083444ceed8SEric W. Biederman out: 2084c52a47acSAl Viro return -ENOENT; 2085444ceed8SEric W. Biederman } 2086444ceed8SEric W. Biederman 20871da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 20881da177e4SLinus Torvalds struct dentry *dentry, 2089c5141e6dSEric Dumazet const struct pid_entry *ents, 20907bcd6b0eSEric W. Biederman unsigned int nents) 20911da177e4SLinus Torvalds { 2092c52a47acSAl Viro int error; 209399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2094c5141e6dSEric Dumazet const struct pid_entry *p, *last; 20951da177e4SLinus Torvalds 2096c52a47acSAl Viro error = -ENOENT; 20971da177e4SLinus Torvalds 209899f89551SEric W. Biederman if (!task) 209999f89551SEric W. Biederman goto out_no_task; 21001da177e4SLinus Torvalds 210120cdc894SEric W. Biederman /* 210220cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 210320cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 210420cdc894SEric W. Biederman */ 21057bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 21067bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 21071da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 21081da177e4SLinus Torvalds continue; 21091da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 21101da177e4SLinus Torvalds break; 21111da177e4SLinus Torvalds } 21127bcd6b0eSEric W. Biederman if (p > last) 21131da177e4SLinus Torvalds goto out; 21141da177e4SLinus Torvalds 2115444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 21161da177e4SLinus Torvalds out: 211799f89551SEric W. Biederman put_task_struct(task); 211899f89551SEric W. Biederman out_no_task: 2119c52a47acSAl Viro return ERR_PTR(error); 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 2122f0c3b509SAl Viro static int proc_pident_readdir(struct file *file, struct dir_context *ctx, 2123c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 212428a6d671SEric W. Biederman { 2125f0c3b509SAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 2126f0c3b509SAl Viro const struct pid_entry *p; 212728a6d671SEric W. Biederman 212828a6d671SEric W. Biederman if (!task) 2129f0c3b509SAl Viro return -ENOENT; 213028a6d671SEric W. Biederman 2131f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 213228a6d671SEric W. Biederman goto out; 21331da177e4SLinus Torvalds 2134f0c3b509SAl Viro if (ctx->pos >= nents + 2) 2135f0c3b509SAl Viro goto out; 2136f0c3b509SAl Viro 2137f0c3b509SAl Viro for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) { 2138f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, p->name, p->len, 2139f0c3b509SAl Viro proc_pident_instantiate, task, p)) 2140f0c3b509SAl Viro break; 2141f0c3b509SAl Viro ctx->pos++; 2142f0c3b509SAl Viro } 214328a6d671SEric W. Biederman out: 214461a28784SEric W. Biederman put_task_struct(task); 2145f0c3b509SAl Viro return 0; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 214928a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 215028a6d671SEric W. Biederman size_t count, loff_t *ppos) 215128a6d671SEric W. Biederman { 2152496ad9aaSAl Viro struct inode * inode = file_inode(file); 215304ff9708SAl Viro char *p = NULL; 215428a6d671SEric W. Biederman ssize_t length; 215528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 215628a6d671SEric W. Biederman 215728a6d671SEric W. Biederman if (!task) 215804ff9708SAl Viro return -ESRCH; 215928a6d671SEric W. Biederman 216028a6d671SEric W. Biederman length = security_getprocattr(task, 21612fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 216204ff9708SAl Viro &p); 216328a6d671SEric W. Biederman put_task_struct(task); 216404ff9708SAl Viro if (length > 0) 216504ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 216604ff9708SAl Viro kfree(p); 216728a6d671SEric W. Biederman return length; 216828a6d671SEric W. Biederman } 216928a6d671SEric W. Biederman 217028a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 217128a6d671SEric W. Biederman size_t count, loff_t *ppos) 217228a6d671SEric W. Biederman { 2173496ad9aaSAl Viro struct inode * inode = file_inode(file); 217428a6d671SEric W. Biederman char *page; 217528a6d671SEric W. Biederman ssize_t length; 217628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 217728a6d671SEric W. Biederman 217828a6d671SEric W. Biederman length = -ESRCH; 217928a6d671SEric W. Biederman if (!task) 218028a6d671SEric W. Biederman goto out_no_task; 218128a6d671SEric W. Biederman if (count > PAGE_SIZE) 218228a6d671SEric W. Biederman count = PAGE_SIZE; 218328a6d671SEric W. Biederman 218428a6d671SEric W. Biederman /* No partial writes. */ 218528a6d671SEric W. Biederman length = -EINVAL; 218628a6d671SEric W. Biederman if (*ppos != 0) 218728a6d671SEric W. Biederman goto out; 218828a6d671SEric W. Biederman 218928a6d671SEric W. Biederman length = -ENOMEM; 2190e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 219128a6d671SEric W. Biederman if (!page) 219228a6d671SEric W. Biederman goto out; 219328a6d671SEric W. Biederman 219428a6d671SEric W. Biederman length = -EFAULT; 219528a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 219628a6d671SEric W. Biederman goto out_free; 219728a6d671SEric W. Biederman 2198107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 21999b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2200107db7c7SDavid Howells if (length < 0) 2201107db7c7SDavid Howells goto out_free; 2202107db7c7SDavid Howells 220328a6d671SEric W. Biederman length = security_setprocattr(task, 22042fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 220528a6d671SEric W. Biederman (void*)page, count); 22069b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 220728a6d671SEric W. Biederman out_free: 220828a6d671SEric W. Biederman free_page((unsigned long) page); 220928a6d671SEric W. Biederman out: 221028a6d671SEric W. Biederman put_task_struct(task); 221128a6d671SEric W. Biederman out_no_task: 221228a6d671SEric W. Biederman return length; 221328a6d671SEric W. Biederman } 221428a6d671SEric W. Biederman 221500977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 221628a6d671SEric W. Biederman .read = proc_pid_attr_read, 221728a6d671SEric W. Biederman .write = proc_pid_attr_write, 221887df8424SArnd Bergmann .llseek = generic_file_llseek, 221928a6d671SEric W. Biederman }; 222028a6d671SEric W. Biederman 2221c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2222631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2223631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2224631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2225631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2226631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2227631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 222828a6d671SEric W. Biederman }; 222928a6d671SEric W. Biederman 2230f0c3b509SAl Viro static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) 22311da177e4SLinus Torvalds { 2232f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 223372d9dcfcSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 223600977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 22371da177e4SLinus Torvalds .read = generic_read_dir, 2238f0c3b509SAl Viro .iterate = proc_attr_dir_readdir, 22396038f373SArnd Bergmann .llseek = default_llseek, 22401da177e4SLinus Torvalds }; 22411da177e4SLinus Torvalds 224272d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 224300cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 22441da177e4SLinus Torvalds { 22457bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 22467bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 2249c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 225072d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 225199f89551SEric W. Biederman .getattr = pid_getattr, 22526d76fa58SLinus Torvalds .setattr = proc_setattr, 22531da177e4SLinus Torvalds }; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds #endif 22561da177e4SLinus Torvalds 2257698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 22583cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 22593cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 22603cb4a0bbSKawai, Hidehiro { 2261496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 22623cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22633cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 22643cb4a0bbSKawai, Hidehiro size_t len; 22653cb4a0bbSKawai, Hidehiro int ret; 22663cb4a0bbSKawai, Hidehiro 22673cb4a0bbSKawai, Hidehiro if (!task) 22683cb4a0bbSKawai, Hidehiro return -ESRCH; 22693cb4a0bbSKawai, Hidehiro 22703cb4a0bbSKawai, Hidehiro ret = 0; 22713cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22723cb4a0bbSKawai, Hidehiro if (mm) { 22733cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22743cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22753cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22763cb4a0bbSKawai, Hidehiro mmput(mm); 22773cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22783cb4a0bbSKawai, Hidehiro } 22793cb4a0bbSKawai, Hidehiro 22803cb4a0bbSKawai, Hidehiro put_task_struct(task); 22813cb4a0bbSKawai, Hidehiro 22823cb4a0bbSKawai, Hidehiro return ret; 22833cb4a0bbSKawai, Hidehiro } 22843cb4a0bbSKawai, Hidehiro 22853cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22863cb4a0bbSKawai, Hidehiro const char __user *buf, 22873cb4a0bbSKawai, Hidehiro size_t count, 22883cb4a0bbSKawai, Hidehiro loff_t *ppos) 22893cb4a0bbSKawai, Hidehiro { 22903cb4a0bbSKawai, Hidehiro struct task_struct *task; 22913cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22923cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22933cb4a0bbSKawai, Hidehiro unsigned int val; 22943cb4a0bbSKawai, Hidehiro int ret; 22953cb4a0bbSKawai, Hidehiro int i; 22963cb4a0bbSKawai, Hidehiro unsigned long mask; 22973cb4a0bbSKawai, Hidehiro 22983cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22993cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 23003cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 23013cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 23023cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 23033cb4a0bbSKawai, Hidehiro goto out_no_task; 23043cb4a0bbSKawai, Hidehiro 23053cb4a0bbSKawai, Hidehiro ret = -EINVAL; 23063cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 23073cb4a0bbSKawai, Hidehiro if (*end == '\n') 23083cb4a0bbSKawai, Hidehiro end++; 23093cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 23103cb4a0bbSKawai, Hidehiro goto out_no_task; 23113cb4a0bbSKawai, Hidehiro 23123cb4a0bbSKawai, Hidehiro ret = -ESRCH; 2313496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 23143cb4a0bbSKawai, Hidehiro if (!task) 23153cb4a0bbSKawai, Hidehiro goto out_no_task; 23163cb4a0bbSKawai, Hidehiro 23173cb4a0bbSKawai, Hidehiro ret = end - buffer; 23183cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 23193cb4a0bbSKawai, Hidehiro if (!mm) 23203cb4a0bbSKawai, Hidehiro goto out_no_mm; 23213cb4a0bbSKawai, Hidehiro 23223cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 23233cb4a0bbSKawai, Hidehiro if (val & mask) 23243cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23253cb4a0bbSKawai, Hidehiro else 23263cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23273cb4a0bbSKawai, Hidehiro } 23283cb4a0bbSKawai, Hidehiro 23293cb4a0bbSKawai, Hidehiro mmput(mm); 23303cb4a0bbSKawai, Hidehiro out_no_mm: 23313cb4a0bbSKawai, Hidehiro put_task_struct(task); 23323cb4a0bbSKawai, Hidehiro out_no_task: 23333cb4a0bbSKawai, Hidehiro return ret; 23343cb4a0bbSKawai, Hidehiro } 23353cb4a0bbSKawai, Hidehiro 23363cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 23373cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 23383cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 233987df8424SArnd Bergmann .llseek = generic_file_llseek, 23403cb4a0bbSKawai, Hidehiro }; 23413cb4a0bbSKawai, Hidehiro #endif 23423cb4a0bbSKawai, Hidehiro 2343aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 234419aadc98SAlexey Dobriyan static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) 2345aba76fdbSAndrew Morton { 2346940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2347297c5d92SAndrea Righi unsigned long flags; 2348293eb1e7SVasiliy Kulikov int result; 2349297c5d92SAndrea Righi 2350293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2351293eb1e7SVasiliy Kulikov if (result) 2352293eb1e7SVasiliy Kulikov return result; 2353293eb1e7SVasiliy Kulikov 2354293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2355293eb1e7SVasiliy Kulikov result = -EACCES; 2356293eb1e7SVasiliy Kulikov goto out_unlock; 2357293eb1e7SVasiliy Kulikov } 23581d1221f3SVasiliy Kulikov 23595995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2360b2d002dbSAndrea Righi struct task_struct *t = task; 2361297c5d92SAndrea Righi 23625995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 23635995477aSAndrea Righi while_each_thread(task, t) 23645995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2365297c5d92SAndrea Righi 2366297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2367297c5d92SAndrea Righi } 236825ce3191SJoe Perches seq_printf(m, 2369aba76fdbSAndrew Morton "rchar: %llu\n" 2370aba76fdbSAndrew Morton "wchar: %llu\n" 2371aba76fdbSAndrew Morton "syscr: %llu\n" 2372aba76fdbSAndrew Morton "syscw: %llu\n" 2373aba76fdbSAndrew Morton "read_bytes: %llu\n" 2374aba76fdbSAndrew Morton "write_bytes: %llu\n" 2375aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 23767c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 23777c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 23787c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 23797c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 23807c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 23817c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 23827c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 238325ce3191SJoe Perches result = 0; 238425ce3191SJoe Perches 2385293eb1e7SVasiliy Kulikov out_unlock: 2386293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2387293eb1e7SVasiliy Kulikov return result; 2388aba76fdbSAndrew Morton } 2389297c5d92SAndrea Righi 239019aadc98SAlexey Dobriyan static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 239119aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2392297c5d92SAndrea Righi { 239319aadc98SAlexey Dobriyan return do_io_accounting(task, m, 0); 2394297c5d92SAndrea Righi } 2395297c5d92SAndrea Righi 239619aadc98SAlexey Dobriyan static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, 239719aadc98SAlexey Dobriyan struct pid *pid, struct task_struct *task) 2398297c5d92SAndrea Righi { 239919aadc98SAlexey Dobriyan return do_io_accounting(task, m, 1); 2400297c5d92SAndrea Righi } 2401297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2402aba76fdbSAndrew Morton 240322d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 240422d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file, 2405ccf94f1bSFabian Frederick const struct seq_operations *seq_ops) 240622d917d8SEric W. Biederman { 240722d917d8SEric W. Biederman struct user_namespace *ns = NULL; 240822d917d8SEric W. Biederman struct task_struct *task; 240922d917d8SEric W. Biederman struct seq_file *seq; 241022d917d8SEric W. Biederman int ret = -EINVAL; 241122d917d8SEric W. Biederman 241222d917d8SEric W. Biederman task = get_proc_task(inode); 241322d917d8SEric W. Biederman if (task) { 241422d917d8SEric W. Biederman rcu_read_lock(); 241522d917d8SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 241622d917d8SEric W. Biederman rcu_read_unlock(); 241722d917d8SEric W. Biederman put_task_struct(task); 241822d917d8SEric W. Biederman } 241922d917d8SEric W. Biederman if (!ns) 242022d917d8SEric W. Biederman goto err; 242122d917d8SEric W. Biederman 242222d917d8SEric W. Biederman ret = seq_open(file, seq_ops); 242322d917d8SEric W. Biederman if (ret) 242422d917d8SEric W. Biederman goto err_put_ns; 242522d917d8SEric W. Biederman 242622d917d8SEric W. Biederman seq = file->private_data; 242722d917d8SEric W. Biederman seq->private = ns; 242822d917d8SEric W. Biederman 242922d917d8SEric W. Biederman return 0; 243022d917d8SEric W. Biederman err_put_ns: 243122d917d8SEric W. Biederman put_user_ns(ns); 243222d917d8SEric W. Biederman err: 243322d917d8SEric W. Biederman return ret; 243422d917d8SEric W. Biederman } 243522d917d8SEric W. Biederman 243622d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file) 243722d917d8SEric W. Biederman { 243822d917d8SEric W. Biederman struct seq_file *seq = file->private_data; 243922d917d8SEric W. Biederman struct user_namespace *ns = seq->private; 244022d917d8SEric W. Biederman put_user_ns(ns); 244122d917d8SEric W. Biederman return seq_release(inode, file); 244222d917d8SEric W. Biederman } 244322d917d8SEric W. Biederman 244422d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file) 244522d917d8SEric W. Biederman { 244622d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_uid_seq_operations); 244722d917d8SEric W. Biederman } 244822d917d8SEric W. Biederman 244922d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file) 245022d917d8SEric W. Biederman { 245122d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_gid_seq_operations); 245222d917d8SEric W. Biederman } 245322d917d8SEric W. Biederman 2454f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file) 2455f76d207aSEric W. Biederman { 2456f76d207aSEric W. Biederman return proc_id_map_open(inode, file, &proc_projid_seq_operations); 2457f76d207aSEric W. Biederman } 2458f76d207aSEric W. Biederman 245922d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = { 246022d917d8SEric W. Biederman .open = proc_uid_map_open, 246122d917d8SEric W. Biederman .write = proc_uid_map_write, 246222d917d8SEric W. Biederman .read = seq_read, 246322d917d8SEric W. Biederman .llseek = seq_lseek, 246422d917d8SEric W. Biederman .release = proc_id_map_release, 246522d917d8SEric W. Biederman }; 246622d917d8SEric W. Biederman 246722d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = { 246822d917d8SEric W. Biederman .open = proc_gid_map_open, 246922d917d8SEric W. Biederman .write = proc_gid_map_write, 247022d917d8SEric W. Biederman .read = seq_read, 247122d917d8SEric W. Biederman .llseek = seq_lseek, 247222d917d8SEric W. Biederman .release = proc_id_map_release, 247322d917d8SEric W. Biederman }; 2474f76d207aSEric W. Biederman 2475f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = { 2476f76d207aSEric W. Biederman .open = proc_projid_map_open, 2477f76d207aSEric W. Biederman .write = proc_projid_map_write, 2478f76d207aSEric W. Biederman .read = seq_read, 2479f76d207aSEric W. Biederman .llseek = seq_lseek, 2480f76d207aSEric W. Biederman .release = proc_id_map_release, 2481f76d207aSEric W. Biederman }; 24829cc46516SEric W. Biederman 24839cc46516SEric W. Biederman static int proc_setgroups_open(struct inode *inode, struct file *file) 24849cc46516SEric W. Biederman { 24859cc46516SEric W. Biederman struct user_namespace *ns = NULL; 24869cc46516SEric W. Biederman struct task_struct *task; 24879cc46516SEric W. Biederman int ret; 24889cc46516SEric W. Biederman 24899cc46516SEric W. Biederman ret = -ESRCH; 24909cc46516SEric W. Biederman task = get_proc_task(inode); 24919cc46516SEric W. Biederman if (task) { 24929cc46516SEric W. Biederman rcu_read_lock(); 24939cc46516SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 24949cc46516SEric W. Biederman rcu_read_unlock(); 24959cc46516SEric W. Biederman put_task_struct(task); 24969cc46516SEric W. Biederman } 24979cc46516SEric W. Biederman if (!ns) 24989cc46516SEric W. Biederman goto err; 24999cc46516SEric W. Biederman 25009cc46516SEric W. Biederman if (file->f_mode & FMODE_WRITE) { 25019cc46516SEric W. Biederman ret = -EACCES; 25029cc46516SEric W. Biederman if (!ns_capable(ns, CAP_SYS_ADMIN)) 25039cc46516SEric W. Biederman goto err_put_ns; 25049cc46516SEric W. Biederman } 25059cc46516SEric W. Biederman 25069cc46516SEric W. Biederman ret = single_open(file, &proc_setgroups_show, ns); 25079cc46516SEric W. Biederman if (ret) 25089cc46516SEric W. Biederman goto err_put_ns; 25099cc46516SEric W. Biederman 25109cc46516SEric W. Biederman return 0; 25119cc46516SEric W. Biederman err_put_ns: 25129cc46516SEric W. Biederman put_user_ns(ns); 25139cc46516SEric W. Biederman err: 25149cc46516SEric W. Biederman return ret; 25159cc46516SEric W. Biederman } 25169cc46516SEric W. Biederman 25179cc46516SEric W. Biederman static int proc_setgroups_release(struct inode *inode, struct file *file) 25189cc46516SEric W. Biederman { 25199cc46516SEric W. Biederman struct seq_file *seq = file->private_data; 25209cc46516SEric W. Biederman struct user_namespace *ns = seq->private; 25219cc46516SEric W. Biederman int ret = single_release(inode, file); 25229cc46516SEric W. Biederman put_user_ns(ns); 25239cc46516SEric W. Biederman return ret; 25249cc46516SEric W. Biederman } 25259cc46516SEric W. Biederman 25269cc46516SEric W. Biederman static const struct file_operations proc_setgroups_operations = { 25279cc46516SEric W. Biederman .open = proc_setgroups_open, 25289cc46516SEric W. Biederman .write = proc_setgroups_write, 25299cc46516SEric W. Biederman .read = seq_read, 25309cc46516SEric W. Biederman .llseek = seq_lseek, 25319cc46516SEric W. Biederman .release = proc_setgroups_release, 25329cc46516SEric W. Biederman }; 253322d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */ 253422d917d8SEric W. Biederman 253547830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 253647830723SKees Cook struct pid *pid, struct task_struct *task) 253747830723SKees Cook { 2538a9712bc1SAl Viro int err = lock_trace(task); 2539a9712bc1SAl Viro if (!err) { 254047830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2541a9712bc1SAl Viro unlock_trace(task); 2542a9712bc1SAl Viro } 2543a9712bc1SAl Viro return err; 254447830723SKees Cook } 254547830723SKees Cook 2546801199ceSEric W. Biederman /* 254728a6d671SEric W. Biederman * Thread groups 254828a6d671SEric W. Biederman */ 254900977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2550c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 255120cdc894SEric W. Biederman 2552c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2553631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2554631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2555640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2556640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 2557640708a2SPavel Emelyanov #endif 2558631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 25596b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2560b2211a36SAndrew Morton #ifdef CONFIG_NET 2561631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2562b2211a36SAndrew Morton #endif 2563631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2564f9ea536eSAlexey Dobriyan ONE("auxv", S_IRUSR, proc_pid_auxv), 2565631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 256635a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality), 25671c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits), 256843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2569631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 257043ae34cbSIngo Molnar #endif 25715091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 25725091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 25735091faa4SMike Galbraith #endif 25744614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2575ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 257609d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall), 2577ebcb6734SRoland McGrath #endif 25782ca66ff7SAlexey Dobriyan ONE("cmdline", S_IRUGO, proc_pid_cmdline), 2579631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2580631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2581b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations), 258228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2583b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 258428a6d671SEric W. Biederman #endif 2585631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2586631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2587631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2588631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2589631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2590631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2591631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25921e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2593631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2594b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations), 259532ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations), 259628a6d671SEric W. Biederman #endif 259728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2598631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 259928a6d671SEric W. Biederman #endif 260028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2601edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan), 260228a6d671SEric W. Biederman #endif 26032ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 260435a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack), 260528a6d671SEric W. Biederman #endif 2606*5968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO 2607f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat), 260828a6d671SEric W. Biederman #endif 26099745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2610631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 26119745512cSArjan van de Ven #endif 26128793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 261352de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show), 261428a6d671SEric W. Biederman #endif 2615a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2616006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show), 2617a424316cSPaul Menage #endif 26186ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score), 2619fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2620a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 262128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2622631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2623631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 262428a6d671SEric W. Biederman #endif 2625f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2626631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2627f4f154fdSAkinobu Mita #endif 2628698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2629631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 26303cb4a0bbSKawai, Hidehiro #endif 2631aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 263219aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tgid_io_accounting), 2633aba76fdbSAndrew Morton #endif 2634f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2635d962c144SAlexey Dobriyan ONE("hardwall", S_IRUGO, proc_pid_hardwall), 2636f133eccaSChris Metcalf #endif 263722d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 263822d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 263922d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2640f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 26419cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 264222d917d8SEric W. Biederman #endif 264348f6a7a5SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 264448f6a7a5SPavel Emelyanov REG("timers", S_IRUGO, proc_timers_operations), 264548f6a7a5SPavel Emelyanov #endif 264628a6d671SEric W. Biederman }; 264728a6d671SEric W. Biederman 2648f0c3b509SAl Viro static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) 264928a6d671SEric W. Biederman { 2650f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 265128a6d671SEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 265228a6d671SEric W. Biederman } 265328a6d671SEric W. Biederman 265400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 265528a6d671SEric W. Biederman .read = generic_read_dir, 2656f0c3b509SAl Viro .iterate = proc_tgid_base_readdir, 26576038f373SArnd Bergmann .llseek = default_llseek, 265828a6d671SEric W. Biederman }; 265928a6d671SEric W. Biederman 266000cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 266100cd8dd3SAl Viro { 26627bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 26637bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 266428a6d671SEric W. Biederman } 266528a6d671SEric W. Biederman 2666c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 266728a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 266828a6d671SEric W. Biederman .getattr = pid_getattr, 266928a6d671SEric W. Biederman .setattr = proc_setattr, 26700499680aSVasiliy Kulikov .permission = proc_pid_permission, 267128a6d671SEric W. Biederman }; 267228a6d671SEric W. Biederman 267360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 26741da177e4SLinus Torvalds { 267548e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 26768578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 267748e6484dSEric W. Biederman struct qstr name; 26781da177e4SLinus Torvalds 267948e6484dSEric W. Biederman name.name = buf; 268060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 26814f522a24SAl Viro /* no ->d_hash() rejects on procfs */ 268260347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 268348e6484dSEric W. Biederman if (dentry) { 2684bbd51924SEric W. Biederman d_invalidate(dentry); 268548e6484dSEric W. Biederman dput(dentry); 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 2688c35a7f18SOleg Nesterov if (pid == tgid) 2689c35a7f18SOleg Nesterov return; 2690c35a7f18SOleg Nesterov 269148e6484dSEric W. Biederman name.name = buf; 269260347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 269360347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 269448e6484dSEric W. Biederman if (!leader) 269548e6484dSEric W. Biederman goto out; 269648e6484dSEric W. Biederman 269748e6484dSEric W. Biederman name.name = "task"; 269848e6484dSEric W. Biederman name.len = strlen(name.name); 269948e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 270048e6484dSEric W. Biederman if (!dir) 270148e6484dSEric W. Biederman goto out_put_leader; 270248e6484dSEric W. Biederman 270348e6484dSEric W. Biederman name.name = buf; 270460347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 270548e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 270648e6484dSEric W. Biederman if (dentry) { 2707bbd51924SEric W. Biederman d_invalidate(dentry); 270848e6484dSEric W. Biederman dput(dentry); 27091da177e4SLinus Torvalds } 271048e6484dSEric W. Biederman 271148e6484dSEric W. Biederman dput(dir); 271248e6484dSEric W. Biederman out_put_leader: 271348e6484dSEric W. Biederman dput(leader); 271448e6484dSEric W. Biederman out: 271548e6484dSEric W. Biederman return; 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27180895e91dSRandy Dunlap /** 27190895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 27200895e91dSRandy Dunlap * @task: task that should be flushed. 27210895e91dSRandy Dunlap * 27220895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 272360347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 27240895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 27250895e91dSRandy Dunlap * 27260895e91dSRandy Dunlap * Looks in the dcache for 27270895e91dSRandy Dunlap * /proc/@pid 27280895e91dSRandy Dunlap * /proc/@tgid/task/@pid 27290895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 27300895e91dSRandy Dunlap * from the dcache. 27310895e91dSRandy Dunlap * 27320895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 27330895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 27340895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 27350895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 27360895e91dSRandy Dunlap * dcache entries at process exit time. 27370895e91dSRandy Dunlap * 27380895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 27390895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 27400895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 274160347f67SPavel Emelyanov */ 274260347f67SPavel Emelyanov 274360347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 274460347f67SPavel Emelyanov { 27459fcc2d15SEric W. Biederman int i; 27469b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2747130f77ecSPavel Emelyanov struct upid *upid; 2748130f77ecSPavel Emelyanov 2749130f77ecSPavel Emelyanov pid = task_pid(task); 2750130f77ecSPavel Emelyanov tgid = task_tgid(task); 27519fcc2d15SEric W. Biederman 27529fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2753130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2754130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 27559b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 2756130f77ecSPavel Emelyanov } 275760347f67SPavel Emelyanov } 275860347f67SPavel Emelyanov 2759c52a47acSAl Viro static int proc_pid_instantiate(struct inode *dir, 27609711ef99SAdrian Bunk struct dentry * dentry, 2761c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2762444ceed8SEric W. Biederman { 2763444ceed8SEric W. Biederman struct inode *inode; 2764444ceed8SEric W. Biederman 276561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2766444ceed8SEric W. Biederman if (!inode) 2767444ceed8SEric W. Biederman goto out; 2768444ceed8SEric W. Biederman 2769444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2770444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2771444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2772444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2773aed54175SVegard Nossum 2774bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 2775bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 2776444ceed8SEric W. Biederman 2777fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2778444ceed8SEric W. Biederman 2779444ceed8SEric W. Biederman d_add(dentry, inode); 2780444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 27810b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2782c52a47acSAl Viro return 0; 2783444ceed8SEric W. Biederman out: 2784c52a47acSAl Viro return -ENOENT; 2785444ceed8SEric W. Biederman } 2786444ceed8SEric W. Biederman 278700cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 27881da177e4SLinus Torvalds { 2789335eb531SAlexey Dobriyan int result = -ENOENT; 27901da177e4SLinus Torvalds struct task_struct *task; 27911da177e4SLinus Torvalds unsigned tgid; 2792b488893aSPavel Emelyanov struct pid_namespace *ns; 27931da177e4SLinus Torvalds 2794dbcdb504SAlexey Dobriyan tgid = name_to_int(&dentry->d_name); 27951da177e4SLinus Torvalds if (tgid == ~0U) 27961da177e4SLinus Torvalds goto out; 27971da177e4SLinus Torvalds 2798b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2799de758734SEric W. Biederman rcu_read_lock(); 2800b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 28011da177e4SLinus Torvalds if (task) 28021da177e4SLinus Torvalds get_task_struct(task); 2803de758734SEric W. Biederman rcu_read_unlock(); 28041da177e4SLinus Torvalds if (!task) 28051da177e4SLinus Torvalds goto out; 28061da177e4SLinus Torvalds 2807444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 280848e6484dSEric W. Biederman put_task_struct(task); 28091da177e4SLinus Torvalds out: 2810c52a47acSAl Viro return ERR_PTR(result); 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds /* 28140804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 28150bc58a91SEric W. Biederman * 28161da177e4SLinus Torvalds */ 281719fd4bb2SEric W. Biederman struct tgid_iter { 281819fd4bb2SEric W. Biederman unsigned int tgid; 28190804ef4bSEric W. Biederman struct task_struct *task; 282019fd4bb2SEric W. Biederman }; 282119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 282219fd4bb2SEric W. Biederman { 28230804ef4bSEric W. Biederman struct pid *pid; 28241da177e4SLinus Torvalds 282519fd4bb2SEric W. Biederman if (iter.task) 282619fd4bb2SEric W. Biederman put_task_struct(iter.task); 28270804ef4bSEric W. Biederman rcu_read_lock(); 28280804ef4bSEric W. Biederman retry: 282919fd4bb2SEric W. Biederman iter.task = NULL; 283019fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 28310804ef4bSEric W. Biederman if (pid) { 283219fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 283319fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 28340804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 28350804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 28360804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 28370804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 28380804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 28390804ef4bSEric W. Biederman * 28400804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 28410804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 28420804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 28430804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 28440804ef4bSEric W. Biederman * As we don't care in the case of readdir. 28450bc58a91SEric W. Biederman */ 284619fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 284719fd4bb2SEric W. Biederman iter.tgid += 1; 28480804ef4bSEric W. Biederman goto retry; 284919fd4bb2SEric W. Biederman } 285019fd4bb2SEric W. Biederman get_task_struct(iter.task); 28511da177e4SLinus Torvalds } 2852454cc105SEric W. Biederman rcu_read_unlock(); 285319fd4bb2SEric W. Biederman return iter; 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 28560097875bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 2859f0c3b509SAl Viro int proc_pid_readdir(struct file *file, struct dir_context *ctx) 28601da177e4SLinus Torvalds { 286119fd4bb2SEric W. Biederman struct tgid_iter iter; 28623aa3377fSAl Viro struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info; 2863f0c3b509SAl Viro loff_t pos = ctx->pos; 28641da177e4SLinus Torvalds 2865021ada7dSAl Viro if (pos >= PID_MAX_LIMIT + TGID_OFFSET) 2866f0c3b509SAl Viro return 0; 28671da177e4SLinus Torvalds 28680097875bSEric W. Biederman if (pos == TGID_OFFSET - 2) { 28692b0143b5SDavid Howells struct inode *inode = d_inode(ns->proc_self); 2870db963164SAl Viro if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) 2871f0c3b509SAl Viro return 0; 28720097875bSEric W. Biederman ctx->pos = pos = pos + 1; 2873021ada7dSAl Viro } 28740097875bSEric W. Biederman if (pos == TGID_OFFSET - 1) { 28752b0143b5SDavid Howells struct inode *inode = d_inode(ns->proc_thread_self); 28760097875bSEric W. Biederman if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK)) 28770097875bSEric W. Biederman return 0; 28780097875bSEric W. Biederman ctx->pos = pos = pos + 1; 28790097875bSEric W. Biederman } 28800097875bSEric W. Biederman iter.tgid = pos - TGID_OFFSET; 288119fd4bb2SEric W. Biederman iter.task = NULL; 288219fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 288319fd4bb2SEric W. Biederman iter.task; 288419fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 2885f0c3b509SAl Viro char name[PROC_NUMBUF]; 2886f0c3b509SAl Viro int len; 2887f0c3b509SAl Viro if (!has_pid_permissions(ns, iter.task, 2)) 2888f0c3b509SAl Viro continue; 28890499680aSVasiliy Kulikov 2890f0c3b509SAl Viro len = snprintf(name, sizeof(name), "%d", iter.tgid); 2891f0c3b509SAl Viro ctx->pos = iter.tgid + TGID_OFFSET; 2892f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len, 2893f0c3b509SAl Viro proc_pid_instantiate, iter.task, NULL)) { 289419fd4bb2SEric W. Biederman put_task_struct(iter.task); 2895f0c3b509SAl Viro return 0; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds } 2898f0c3b509SAl Viro ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; 28991da177e4SLinus Torvalds return 0; 29001da177e4SLinus Torvalds } 29011da177e4SLinus Torvalds 29020bc58a91SEric W. Biederman /* 290328a6d671SEric W. Biederman * Tasks 290428a6d671SEric W. Biederman */ 2905c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2906631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 29073835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 29086b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 29096ba8ed79SEric W. Biederman #ifdef CONFIG_NET 29106ba8ed79SEric W. Biederman DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 29116ba8ed79SEric W. Biederman #endif 2912631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2913f9ea536eSAlexey Dobriyan ONE("auxv", S_IRUSR, proc_pid_auxv), 2914631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 291535a35046SDjalal Harouni ONE("personality", S_IRUSR, proc_pid_personality), 29161c963eb1SAlexey Dobriyan ONE("limits", S_IRUGO, proc_pid_limits), 291743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2918631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 291943ae34cbSIngo Molnar #endif 29204614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2921ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 292209d93bd6SAlexey Dobriyan ONE("syscall", S_IRUSR, proc_pid_syscall), 2923ebcb6734SRoland McGrath #endif 29242ca66ff7SAlexey Dobriyan ONE("cmdline", S_IRUGO, proc_pid_cmdline), 2925631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2926631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2927b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_tid_maps_operations), 292881841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE 292981841161SCyrill Gorcunov REG("children", S_IRUGO, proc_tid_children_operations), 293081841161SCyrill Gorcunov #endif 293128a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2932b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), 293328a6d671SEric W. Biederman #endif 2934631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2935631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2936631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2937631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2938631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2939631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 29401e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2941631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2942b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_tid_smaps_operations), 294332ed74a4SDjalal Harouni REG("pagemap", S_IRUSR, proc_pagemap_operations), 294428a6d671SEric W. Biederman #endif 294528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2946631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 294728a6d671SEric W. Biederman #endif 294828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2949edfcd606SAlexey Dobriyan ONE("wchan", S_IRUGO, proc_pid_wchan), 295028a6d671SEric W. Biederman #endif 29512ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 295235a35046SDjalal Harouni ONE("stack", S_IRUSR, proc_pid_stack), 295328a6d671SEric W. Biederman #endif 2954*5968ceceSNaveen N. Rao #ifdef CONFIG_SCHED_INFO 2955f6e826caSAlexey Dobriyan ONE("schedstat", S_IRUGO, proc_pid_schedstat), 295628a6d671SEric W. Biederman #endif 29579745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2958631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 29599745512cSArjan van de Ven #endif 29608793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 296152de4779SZefan Li ONE("cpuset", S_IRUGO, proc_cpuset_show), 296228a6d671SEric W. Biederman #endif 2963a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2964006f4ac4SZefan Li ONE("cgroup", S_IRUGO, proc_cgroup_show), 2965a424316cSPaul Menage #endif 29666ba51e37SAlexey Dobriyan ONE("oom_score", S_IRUGO, proc_oom_score), 2967fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2968a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 296928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2970631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 297126ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 297228a6d671SEric W. Biederman #endif 2973f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2974631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2975f4f154fdSAkinobu Mita #endif 2976297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 297719aadc98SAlexey Dobriyan ONE("io", S_IRUSR, proc_tid_io_accounting), 2978297c5d92SAndrea Righi #endif 2979f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2980d962c144SAlexey Dobriyan ONE("hardwall", S_IRUGO, proc_pid_hardwall), 2981f133eccaSChris Metcalf #endif 298222d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 298322d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 298422d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2985f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 29869cc46516SEric W. Biederman REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), 298722d917d8SEric W. Biederman #endif 298828a6d671SEric W. Biederman }; 298928a6d671SEric W. Biederman 2990f0c3b509SAl Viro static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) 299128a6d671SEric W. Biederman { 2992f0c3b509SAl Viro return proc_pident_readdir(file, ctx, 299328a6d671SEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 299428a6d671SEric W. Biederman } 299528a6d671SEric W. Biederman 299600cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 299700cd8dd3SAl Viro { 29987bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 29997bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 300028a6d671SEric W. Biederman } 300128a6d671SEric W. Biederman 300200977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 300328a6d671SEric W. Biederman .read = generic_read_dir, 3004f0c3b509SAl Viro .iterate = proc_tid_base_readdir, 30056038f373SArnd Bergmann .llseek = default_llseek, 300628a6d671SEric W. Biederman }; 300728a6d671SEric W. Biederman 3008c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 300928a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 301028a6d671SEric W. Biederman .getattr = pid_getattr, 301128a6d671SEric W. Biederman .setattr = proc_setattr, 301228a6d671SEric W. Biederman }; 301328a6d671SEric W. Biederman 3014c52a47acSAl Viro static int proc_task_instantiate(struct inode *dir, 3015c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3016444ceed8SEric W. Biederman { 3017444ceed8SEric W. Biederman struct inode *inode; 301861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3019444ceed8SEric W. Biederman 3020444ceed8SEric W. Biederman if (!inode) 3021444ceed8SEric W. Biederman goto out; 3022444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3023444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3024444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3025444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3026aed54175SVegard Nossum 3027bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 3028bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 3029444ceed8SEric W. Biederman 3030fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3031444ceed8SEric W. Biederman 3032444ceed8SEric W. Biederman d_add(dentry, inode); 3033444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 30340b728e19SAl Viro if (pid_revalidate(dentry, 0)) 3035c52a47acSAl Viro return 0; 3036444ceed8SEric W. Biederman out: 3037c52a47acSAl Viro return -ENOENT; 3038444ceed8SEric W. Biederman } 3039444ceed8SEric W. Biederman 304000cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 304128a6d671SEric W. Biederman { 3042c52a47acSAl Viro int result = -ENOENT; 304328a6d671SEric W. Biederman struct task_struct *task; 304428a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 304528a6d671SEric W. Biederman unsigned tid; 3046b488893aSPavel Emelyanov struct pid_namespace *ns; 304728a6d671SEric W. Biederman 304828a6d671SEric W. Biederman if (!leader) 304928a6d671SEric W. Biederman goto out_no_task; 305028a6d671SEric W. Biederman 3051dbcdb504SAlexey Dobriyan tid = name_to_int(&dentry->d_name); 305228a6d671SEric W. Biederman if (tid == ~0U) 305328a6d671SEric W. Biederman goto out; 305428a6d671SEric W. Biederman 3055b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 305628a6d671SEric W. Biederman rcu_read_lock(); 3057b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 305828a6d671SEric W. Biederman if (task) 305928a6d671SEric W. Biederman get_task_struct(task); 306028a6d671SEric W. Biederman rcu_read_unlock(); 306128a6d671SEric W. Biederman if (!task) 306228a6d671SEric W. Biederman goto out; 3063bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 306428a6d671SEric W. Biederman goto out_drop_task; 306528a6d671SEric W. Biederman 3066444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 306728a6d671SEric W. Biederman out_drop_task: 306828a6d671SEric W. Biederman put_task_struct(task); 306928a6d671SEric W. Biederman out: 307028a6d671SEric W. Biederman put_task_struct(leader); 307128a6d671SEric W. Biederman out_no_task: 3072c52a47acSAl Viro return ERR_PTR(result); 307328a6d671SEric W. Biederman } 307428a6d671SEric W. Biederman 307528a6d671SEric W. Biederman /* 30760bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 30770bc58a91SEric W. Biederman * 30780bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 30790bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 30800bc58a91SEric W. Biederman * directory we have more work todo. 30810bc58a91SEric W. Biederman * 30820bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 30830bc58a91SEric W. Biederman * 30840bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 30850bc58a91SEric W. Biederman * threads past it. 30860bc58a91SEric W. Biederman */ 30879f6e963fSOleg Nesterov static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, 30889f6e963fSOleg Nesterov struct pid_namespace *ns) 30890bc58a91SEric W. Biederman { 3090d855a4b7SOleg Nesterov struct task_struct *pos, *task; 30919f6e963fSOleg Nesterov unsigned long nr = f_pos; 30929f6e963fSOleg Nesterov 30939f6e963fSOleg Nesterov if (nr != f_pos) /* 32bit overflow? */ 30949f6e963fSOleg Nesterov return NULL; 30950bc58a91SEric W. Biederman 3096cc288738SEric W. Biederman rcu_read_lock(); 3097d855a4b7SOleg Nesterov task = pid_task(pid, PIDTYPE_PID); 3098d855a4b7SOleg Nesterov if (!task) 3099d855a4b7SOleg Nesterov goto fail; 3100d855a4b7SOleg Nesterov 3101d855a4b7SOleg Nesterov /* Attempt to start with the tid of a thread */ 31029f6e963fSOleg Nesterov if (tid && nr) { 3103b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3104d855a4b7SOleg Nesterov if (pos && same_thread_group(pos, task)) 3105a872ff0cSOleg Nesterov goto found; 31060bc58a91SEric W. Biederman } 31070bc58a91SEric W. Biederman 31080bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 31099f6e963fSOleg Nesterov if (nr >= get_nr_threads(task)) 3110c986c14aSOleg Nesterov goto fail; 3111a872ff0cSOleg Nesterov 3112a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3113a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3114a872ff0cSOleg Nesterov */ 3115d855a4b7SOleg Nesterov pos = task = task->group_leader; 3116c986c14aSOleg Nesterov do { 31179f6e963fSOleg Nesterov if (!nr--) 3118c986c14aSOleg Nesterov goto found; 3119d855a4b7SOleg Nesterov } while_each_thread(task, pos); 3120c986c14aSOleg Nesterov fail: 3121a872ff0cSOleg Nesterov pos = NULL; 3122a872ff0cSOleg Nesterov goto out; 3123a872ff0cSOleg Nesterov found: 3124a872ff0cSOleg Nesterov get_task_struct(pos); 3125a872ff0cSOleg Nesterov out: 3126cc288738SEric W. Biederman rcu_read_unlock(); 31270bc58a91SEric W. Biederman return pos; 31280bc58a91SEric W. Biederman } 31290bc58a91SEric W. Biederman 31300bc58a91SEric W. Biederman /* 31310bc58a91SEric W. Biederman * Find the next thread in the thread list. 31320bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 31330bc58a91SEric W. Biederman * 31340bc58a91SEric W. Biederman * The reference to the input task_struct is released. 31350bc58a91SEric W. Biederman */ 31360bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 31370bc58a91SEric W. Biederman { 3138c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3139cc288738SEric W. Biederman rcu_read_lock(); 3140c1df7fb8SOleg Nesterov if (pid_alive(start)) { 31410bc58a91SEric W. Biederman pos = next_thread(start); 3142c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 31430bc58a91SEric W. Biederman pos = NULL; 3144c1df7fb8SOleg Nesterov else 3145c1df7fb8SOleg Nesterov get_task_struct(pos); 3146c1df7fb8SOleg Nesterov } 3147cc288738SEric W. Biederman rcu_read_unlock(); 31480bc58a91SEric W. Biederman put_task_struct(start); 31490bc58a91SEric W. Biederman return pos; 31500bc58a91SEric W. Biederman } 31510bc58a91SEric W. Biederman 31521da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 3153f0c3b509SAl Viro static int proc_task_readdir(struct file *file, struct dir_context *ctx) 31541da177e4SLinus Torvalds { 3155d855a4b7SOleg Nesterov struct inode *inode = file_inode(file); 3156d855a4b7SOleg Nesterov struct task_struct *task; 3157b488893aSPavel Emelyanov struct pid_namespace *ns; 3158f0c3b509SAl Viro int tid; 31591da177e4SLinus Torvalds 3160d855a4b7SOleg Nesterov if (proc_inode_is_dead(inode)) 3161f0c3b509SAl Viro return -ENOENT; 31621da177e4SLinus Torvalds 3163f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 3164d855a4b7SOleg Nesterov return 0; 31651da177e4SLinus Torvalds 31660bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 31670bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 31680bc58a91SEric W. Biederman */ 31693aa3377fSAl Viro ns = inode->i_sb->s_fs_info; 3170f0c3b509SAl Viro tid = (int)file->f_version; 3171f0c3b509SAl Viro file->f_version = 0; 3172d855a4b7SOleg Nesterov for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); 31730bc58a91SEric W. Biederman task; 3174f0c3b509SAl Viro task = next_tid(task), ctx->pos++) { 3175f0c3b509SAl Viro char name[PROC_NUMBUF]; 3176f0c3b509SAl Viro int len; 3177b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 3178f0c3b509SAl Viro len = snprintf(name, sizeof(name), "%d", tid); 3179f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, name, len, 3180f0c3b509SAl Viro proc_task_instantiate, task, NULL)) { 31810bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 31820bc58a91SEric W. Biederman * pid for the next readir call */ 3183f0c3b509SAl Viro file->f_version = (u64)tid; 31840bc58a91SEric W. Biederman put_task_struct(task); 31851da177e4SLinus Torvalds break; 31860bc58a91SEric W. Biederman } 31871da177e4SLinus Torvalds } 3188d855a4b7SOleg Nesterov 3189f0c3b509SAl Viro return 0; 31901da177e4SLinus Torvalds } 31916e66b52bSEric W. Biederman 31926e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31936e66b52bSEric W. Biederman { 31942b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 319599f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31966e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31976e66b52bSEric W. Biederman 319899f89551SEric W. Biederman if (p) { 319999f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 320099f89551SEric W. Biederman put_task_struct(p); 32016e66b52bSEric W. Biederman } 32026e66b52bSEric W. Biederman 32036e66b52bSEric W. Biederman return 0; 32046e66b52bSEric W. Biederman } 320528a6d671SEric W. Biederman 3206c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 320728a6d671SEric W. Biederman .lookup = proc_task_lookup, 320828a6d671SEric W. Biederman .getattr = proc_task_getattr, 320928a6d671SEric W. Biederman .setattr = proc_setattr, 32100499680aSVasiliy Kulikov .permission = proc_pid_permission, 321128a6d671SEric W. Biederman }; 321228a6d671SEric W. Biederman 321300977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 321428a6d671SEric W. Biederman .read = generic_read_dir, 3215f0c3b509SAl Viro .iterate = proc_task_readdir, 32166038f373SArnd Bergmann .llseek = default_llseek, 321728a6d671SEric W. Biederman }; 3218