11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 561da177e4SLinus Torvalds #include <linux/init.h> 5716f7e0feSRandy Dunlap #include <linux/capability.h> 581da177e4SLinus Torvalds #include <linux/file.h> 591da177e4SLinus Torvalds #include <linux/string.h> 601da177e4SLinus Torvalds #include <linux/seq_file.h> 611da177e4SLinus Torvalds #include <linux/namei.h> 621da177e4SLinus Torvalds #include <linux/namespace.h> 631da177e4SLinus Torvalds #include <linux/mm.h> 641da177e4SLinus Torvalds #include <linux/smp_lock.h> 65b835996fSDipankar Sarma #include <linux/rcupdate.h> 661da177e4SLinus Torvalds #include <linux/kallsyms.h> 671da177e4SLinus Torvalds #include <linux/mount.h> 681da177e4SLinus Torvalds #include <linux/security.h> 691da177e4SLinus Torvalds #include <linux/ptrace.h> 701da177e4SLinus Torvalds #include <linux/seccomp.h> 711da177e4SLinus Torvalds #include <linux/cpuset.h> 721da177e4SLinus Torvalds #include <linux/audit.h> 735addc5ddSAl Viro #include <linux/poll.h> 741da177e4SLinus Torvalds #include "internal.h" 751da177e4SLinus Torvalds 760f2fe20fSEric W. Biederman /* NOTE: 770f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 780f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 790f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 800f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 810f2fe20fSEric W. Biederman * 820f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 830f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 840f2fe20fSEric W. Biederman */ 850f2fe20fSEric W. Biederman 861da177e4SLinus Torvalds /* 871da177e4SLinus Torvalds * For hysterical raisins we keep the same inumbers as in the old procfs. 881da177e4SLinus Torvalds * Feel free to change the macro below - just keep the range distinct from 891da177e4SLinus Torvalds * inumbers of the rest of procfs (currently those are in 0x0000--0xffff). 901da177e4SLinus Torvalds * As soon as we'll get a separate superblock we will be able to forget 911da177e4SLinus Torvalds * about magical ranges too. 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #define fake_ino(pid,ino) (((pid)<<16)|(ino)) 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds enum pid_directory_inos { 971da177e4SLinus Torvalds PROC_TGID_INO = 2, 981da177e4SLinus Torvalds PROC_TGID_TASK, 991da177e4SLinus Torvalds PROC_TGID_STATUS, 1001da177e4SLinus Torvalds PROC_TGID_MEM, 1011da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 1021da177e4SLinus Torvalds PROC_TGID_SECCOMP, 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds PROC_TGID_CWD, 1051da177e4SLinus Torvalds PROC_TGID_ROOT, 1061da177e4SLinus Torvalds PROC_TGID_EXE, 1071da177e4SLinus Torvalds PROC_TGID_FD, 1081da177e4SLinus Torvalds PROC_TGID_ENVIRON, 1091da177e4SLinus Torvalds PROC_TGID_AUXV, 1101da177e4SLinus Torvalds PROC_TGID_CMDLINE, 1111da177e4SLinus Torvalds PROC_TGID_STAT, 1121da177e4SLinus Torvalds PROC_TGID_STATM, 1131da177e4SLinus Torvalds PROC_TGID_MAPS, 1146e21c8f1SChristoph Lameter PROC_TGID_NUMA_MAPS, 1151da177e4SLinus Torvalds PROC_TGID_MOUNTS, 116b4629fe2SChuck Lever PROC_TGID_MOUNTSTATS, 1171da177e4SLinus Torvalds PROC_TGID_WCHAN, 11863c6764cSYoshinori Sato #ifdef CONFIG_MMU 119e070ad49SMauricio Lin PROC_TGID_SMAPS, 12063c6764cSYoshinori Sato #endif 1211da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 1221da177e4SLinus Torvalds PROC_TGID_SCHEDSTAT, 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS 1251da177e4SLinus Torvalds PROC_TGID_CPUSET, 1261da177e4SLinus Torvalds #endif 1271da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 1281da177e4SLinus Torvalds PROC_TGID_ATTR, 1291da177e4SLinus Torvalds PROC_TGID_ATTR_CURRENT, 1301da177e4SLinus Torvalds PROC_TGID_ATTR_PREV, 1311da177e4SLinus Torvalds PROC_TGID_ATTR_EXEC, 1321da177e4SLinus Torvalds PROC_TGID_ATTR_FSCREATE, 1334eb582cfSMichael LeMay PROC_TGID_ATTR_KEYCREATE, 13442c3e03eSEric Paris PROC_TGID_ATTR_SOCKCREATE, 1351da177e4SLinus Torvalds #endif 1361da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1371da177e4SLinus Torvalds PROC_TGID_LOGINUID, 1381da177e4SLinus Torvalds #endif 1391da177e4SLinus Torvalds PROC_TGID_OOM_SCORE, 1401da177e4SLinus Torvalds PROC_TGID_OOM_ADJUST, 1411da177e4SLinus Torvalds PROC_TID_INO, 1421da177e4SLinus Torvalds PROC_TID_STATUS, 1431da177e4SLinus Torvalds PROC_TID_MEM, 1441da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 1451da177e4SLinus Torvalds PROC_TID_SECCOMP, 1461da177e4SLinus Torvalds #endif 1471da177e4SLinus Torvalds PROC_TID_CWD, 1481da177e4SLinus Torvalds PROC_TID_ROOT, 1491da177e4SLinus Torvalds PROC_TID_EXE, 1501da177e4SLinus Torvalds PROC_TID_FD, 1511da177e4SLinus Torvalds PROC_TID_ENVIRON, 1521da177e4SLinus Torvalds PROC_TID_AUXV, 1531da177e4SLinus Torvalds PROC_TID_CMDLINE, 1541da177e4SLinus Torvalds PROC_TID_STAT, 1551da177e4SLinus Torvalds PROC_TID_STATM, 1561da177e4SLinus Torvalds PROC_TID_MAPS, 1576e21c8f1SChristoph Lameter PROC_TID_NUMA_MAPS, 1581da177e4SLinus Torvalds PROC_TID_MOUNTS, 159b4629fe2SChuck Lever PROC_TID_MOUNTSTATS, 1601da177e4SLinus Torvalds PROC_TID_WCHAN, 16163c6764cSYoshinori Sato #ifdef CONFIG_MMU 162e070ad49SMauricio Lin PROC_TID_SMAPS, 16363c6764cSYoshinori Sato #endif 1641da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 1651da177e4SLinus Torvalds PROC_TID_SCHEDSTAT, 1661da177e4SLinus Torvalds #endif 1671da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS 1681da177e4SLinus Torvalds PROC_TID_CPUSET, 1691da177e4SLinus Torvalds #endif 1701da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 1711da177e4SLinus Torvalds PROC_TID_ATTR, 1721da177e4SLinus Torvalds PROC_TID_ATTR_CURRENT, 1731da177e4SLinus Torvalds PROC_TID_ATTR_PREV, 1741da177e4SLinus Torvalds PROC_TID_ATTR_EXEC, 1751da177e4SLinus Torvalds PROC_TID_ATTR_FSCREATE, 1764eb582cfSMichael LeMay PROC_TID_ATTR_KEYCREATE, 17742c3e03eSEric Paris PROC_TID_ATTR_SOCKCREATE, 1781da177e4SLinus Torvalds #endif 1791da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1801da177e4SLinus Torvalds PROC_TID_LOGINUID, 1811da177e4SLinus Torvalds #endif 1821da177e4SLinus Torvalds PROC_TID_OOM_SCORE, 1831da177e4SLinus Torvalds PROC_TID_OOM_ADJUST, 1845e21ccb1SMiklos Szeredi 1855e21ccb1SMiklos Szeredi /* Add new entries before this */ 1865e21ccb1SMiklos Szeredi PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */ 1871da177e4SLinus Torvalds }; 1881da177e4SLinus Torvalds 1898578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 1908578cea7SEric W. Biederman #define PROC_NUMBUF 10 1918578cea7SEric W. Biederman 1921da177e4SLinus Torvalds struct pid_entry { 1931da177e4SLinus Torvalds int type; 1941da177e4SLinus Torvalds int len; 1951da177e4SLinus Torvalds char *name; 1961da177e4SLinus Torvalds mode_t mode; 1971da177e4SLinus Torvalds }; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)} 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds static struct pid_entry tgid_base_stuff[] = { 2021da177e4SLinus Torvalds E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO), 2031da177e4SLinus Torvalds E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), 2041da177e4SLinus Torvalds E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR), 2051da177e4SLinus Torvalds E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR), 2061da177e4SLinus Torvalds E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO), 2071da177e4SLinus Torvalds E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), 2081da177e4SLinus Torvalds E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO), 2091da177e4SLinus Torvalds E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO), 2101da177e4SLinus Torvalds E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO), 2116e21c8f1SChristoph Lameter #ifdef CONFIG_NUMA 2126e21c8f1SChristoph Lameter E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), 2136e21c8f1SChristoph Lameter #endif 2141da177e4SLinus Torvalds E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), 2151da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 2161da177e4SLinus Torvalds E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), 2171da177e4SLinus Torvalds #endif 2181da177e4SLinus Torvalds E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO), 2191da177e4SLinus Torvalds E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO), 2201da177e4SLinus Torvalds E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO), 2211da177e4SLinus Torvalds E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO), 222b4629fe2SChuck Lever E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR), 22363c6764cSYoshinori Sato #ifdef CONFIG_MMU 224e070ad49SMauricio Lin E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO), 22563c6764cSYoshinori Sato #endif 2261da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 2271da177e4SLinus Torvalds E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), 2281da177e4SLinus Torvalds #endif 2291da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2301da177e4SLinus Torvalds E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO), 2311da177e4SLinus Torvalds #endif 2321da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2331da177e4SLinus Torvalds E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO), 2341da177e4SLinus Torvalds #endif 2351da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS 2361da177e4SLinus Torvalds E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO), 2371da177e4SLinus Torvalds #endif 2381da177e4SLinus Torvalds E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), 2391da177e4SLinus Torvalds E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR), 2401da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 2411da177e4SLinus Torvalds E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), 2421da177e4SLinus Torvalds #endif 2431da177e4SLinus Torvalds {0,0,NULL,0} 2441da177e4SLinus Torvalds }; 2451da177e4SLinus Torvalds static struct pid_entry tid_base_stuff[] = { 2461da177e4SLinus Torvalds E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR), 2471da177e4SLinus Torvalds E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR), 2481da177e4SLinus Torvalds E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR), 2491da177e4SLinus Torvalds E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO), 2501da177e4SLinus Torvalds E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), 2511da177e4SLinus Torvalds E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO), 2521da177e4SLinus Torvalds E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO), 2531da177e4SLinus Torvalds E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO), 2546e21c8f1SChristoph Lameter #ifdef CONFIG_NUMA 2556e21c8f1SChristoph Lameter E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO), 2566e21c8f1SChristoph Lameter #endif 2571da177e4SLinus Torvalds E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), 2581da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 2591da177e4SLinus Torvalds E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR), 2601da177e4SLinus Torvalds #endif 2611da177e4SLinus Torvalds E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO), 2621da177e4SLinus Torvalds E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO), 2631da177e4SLinus Torvalds E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO), 2641da177e4SLinus Torvalds E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO), 26563c6764cSYoshinori Sato #ifdef CONFIG_MMU 266e070ad49SMauricio Lin E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO), 26763c6764cSYoshinori Sato #endif 2681da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 2691da177e4SLinus Torvalds E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO), 2701da177e4SLinus Torvalds #endif 2711da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2721da177e4SLinus Torvalds E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO), 2731da177e4SLinus Torvalds #endif 2741da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2751da177e4SLinus Torvalds E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO), 2761da177e4SLinus Torvalds #endif 2771da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS 2781da177e4SLinus Torvalds E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO), 2791da177e4SLinus Torvalds #endif 2801da177e4SLinus Torvalds E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO), 2811da177e4SLinus Torvalds E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR), 2821da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 2831da177e4SLinus Torvalds E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO), 2841da177e4SLinus Torvalds #endif 2851da177e4SLinus Torvalds {0,0,NULL,0} 2861da177e4SLinus Torvalds }; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 2891da177e4SLinus Torvalds static struct pid_entry tgid_attr_stuff[] = { 2901da177e4SLinus Torvalds E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), 2911da177e4SLinus Torvalds E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), 2921da177e4SLinus Torvalds E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), 2931da177e4SLinus Torvalds E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), 2944eb582cfSMichael LeMay E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO), 29542c3e03eSEric Paris E(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO), 2961da177e4SLinus Torvalds {0,0,NULL,0} 2971da177e4SLinus Torvalds }; 2981da177e4SLinus Torvalds static struct pid_entry tid_attr_stuff[] = { 2991da177e4SLinus Torvalds E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO), 3001da177e4SLinus Torvalds E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO), 3011da177e4SLinus Torvalds E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO), 3021da177e4SLinus Torvalds E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO), 3034eb582cfSMichael LeMay E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO), 30442c3e03eSEric Paris E(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IFREG|S_IRUGO|S_IWUGO), 3051da177e4SLinus Torvalds {0,0,NULL,0} 3061da177e4SLinus Torvalds }; 3071da177e4SLinus Torvalds #endif 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds #undef E 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 3121da177e4SLinus Torvalds { 31399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 31499f89551SEric W. Biederman struct files_struct *files = NULL; 3151da177e4SLinus Torvalds struct file *file; 316aed7a6c4SEric W. Biederman int fd = proc_fd(inode); 3171da177e4SLinus Torvalds 31899f89551SEric W. Biederman if (task) { 3191da177e4SLinus Torvalds files = get_files_struct(task); 32099f89551SEric W. Biederman put_task_struct(task); 32199f89551SEric W. Biederman } 3221da177e4SLinus Torvalds if (files) { 323ca99c1daSDipankar Sarma /* 324ca99c1daSDipankar Sarma * We are not taking a ref to the file structure, so we must 325ca99c1daSDipankar Sarma * hold ->file_lock. 326ca99c1daSDipankar Sarma */ 327ca99c1daSDipankar Sarma spin_lock(&files->file_lock); 3281da177e4SLinus Torvalds file = fcheck_files(files, fd); 3291da177e4SLinus Torvalds if (file) { 3301da177e4SLinus Torvalds *mnt = mntget(file->f_vfsmnt); 3311da177e4SLinus Torvalds *dentry = dget(file->f_dentry); 332ca99c1daSDipankar Sarma spin_unlock(&files->file_lock); 3331da177e4SLinus Torvalds put_files_struct(files); 3341da177e4SLinus Torvalds return 0; 3351da177e4SLinus Torvalds } 336ca99c1daSDipankar Sarma spin_unlock(&files->file_lock); 3371da177e4SLinus Torvalds put_files_struct(files); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds return -ENOENT; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3420494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 3431da177e4SLinus Torvalds { 3441da177e4SLinus Torvalds struct fs_struct *fs; 3450494f6ecSMiklos Szeredi task_lock(task); 3460494f6ecSMiklos Szeredi fs = task->fs; 3471da177e4SLinus Torvalds if(fs) 3481da177e4SLinus Torvalds atomic_inc(&fs->count); 3490494f6ecSMiklos Szeredi task_unlock(task); 3500494f6ecSMiklos Szeredi return fs; 3510494f6ecSMiklos Szeredi } 3520494f6ecSMiklos Szeredi 35399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 35499f89551SEric W. Biederman { 35599f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 35699f89551SEric W. Biederman unsigned long flags; 35799f89551SEric W. Biederman int count = 0; 35899f89551SEric W. Biederman 35999f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 36099f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 36199f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 36299f89551SEric W. Biederman } 36399f89551SEric W. Biederman return count; 36499f89551SEric W. Biederman } 36599f89551SEric W. Biederman 3660494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 3670494f6ecSMiklos Szeredi { 36899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 36999f89551SEric W. Biederman struct fs_struct *fs = NULL; 3700494f6ecSMiklos Szeredi int result = -ENOENT; 37199f89551SEric W. Biederman 37299f89551SEric W. Biederman if (task) { 37399f89551SEric W. Biederman fs = get_fs_struct(task); 37499f89551SEric W. Biederman put_task_struct(task); 37599f89551SEric W. Biederman } 3761da177e4SLinus Torvalds if (fs) { 3771da177e4SLinus Torvalds read_lock(&fs->lock); 3781da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 3791da177e4SLinus Torvalds *dentry = dget(fs->pwd); 3801da177e4SLinus Torvalds read_unlock(&fs->lock); 3811da177e4SLinus Torvalds result = 0; 3821da177e4SLinus Torvalds put_fs_struct(fs); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds return result; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 3881da177e4SLinus Torvalds { 38999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 39099f89551SEric W. Biederman struct fs_struct *fs = NULL; 3911da177e4SLinus Torvalds int result = -ENOENT; 39299f89551SEric W. Biederman 39399f89551SEric W. Biederman if (task) { 39499f89551SEric W. Biederman fs = get_fs_struct(task); 39599f89551SEric W. Biederman put_task_struct(task); 39699f89551SEric W. Biederman } 3971da177e4SLinus Torvalds if (fs) { 3981da177e4SLinus Torvalds read_lock(&fs->lock); 3991da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 4001da177e4SLinus Torvalds *dentry = dget(fs->root); 4011da177e4SLinus Torvalds read_unlock(&fs->lock); 4021da177e4SLinus Torvalds result = 0; 4031da177e4SLinus Torvalds put_fs_struct(fs); 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds return result; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 4091da177e4SLinus Torvalds (task == current || \ 4101da177e4SLinus Torvalds (task->parent == current && \ 4111da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 4121da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 4131da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds static int proc_pid_environ(struct task_struct *task, char * buffer) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds int res = 0; 4181da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 4191da177e4SLinus Torvalds if (mm) { 4201da177e4SLinus Torvalds unsigned int len = mm->env_end - mm->env_start; 4211da177e4SLinus Torvalds if (len > PAGE_SIZE) 4221da177e4SLinus Torvalds len = PAGE_SIZE; 4231da177e4SLinus Torvalds res = access_process_vm(task, mm->env_start, buffer, len, 0); 424ab8d11beSMiklos Szeredi if (!ptrace_may_attach(task)) 4251da177e4SLinus Torvalds res = -ESRCH; 4261da177e4SLinus Torvalds mmput(mm); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds return res; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds int res = 0; 4341da177e4SLinus Torvalds unsigned int len; 4351da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 4361da177e4SLinus Torvalds if (!mm) 4371da177e4SLinus Torvalds goto out; 4381da177e4SLinus Torvalds if (!mm->arg_end) 4391da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds if (len > PAGE_SIZE) 4441da177e4SLinus Torvalds len = PAGE_SIZE; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 4491da177e4SLinus Torvalds // assume application is using setproctitle(3). 4501da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 4511da177e4SLinus Torvalds len = strnlen(buffer, res); 4521da177e4SLinus Torvalds if (len < res) { 4531da177e4SLinus Torvalds res = len; 4541da177e4SLinus Torvalds } else { 4551da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 4561da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 4571da177e4SLinus Torvalds len = PAGE_SIZE - res; 4581da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 4591da177e4SLinus Torvalds res = strnlen(buffer, res); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds out_mm: 4631da177e4SLinus Torvalds mmput(mm); 4641da177e4SLinus Torvalds out: 4651da177e4SLinus Torvalds return res; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds int res = 0; 4711da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 4721da177e4SLinus Torvalds if (mm) { 4731da177e4SLinus Torvalds unsigned int nwords = 0; 4741da177e4SLinus Torvalds do 4751da177e4SLinus Torvalds nwords += 2; 4761da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 4771da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 4781da177e4SLinus Torvalds if (res > PAGE_SIZE) 4791da177e4SLinus Torvalds res = PAGE_SIZE; 4801da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 4811da177e4SLinus Torvalds mmput(mm); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds return res; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 4881da177e4SLinus Torvalds /* 4891da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 4901da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 4911da177e4SLinus Torvalds */ 4921da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds char *modname; 4951da177e4SLinus Torvalds const char *sym_name; 4961da177e4SLinus Torvalds unsigned long wchan, size, offset; 4971da177e4SLinus Torvalds char namebuf[KSYM_NAME_LEN+1]; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds wchan = get_wchan(task); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf); 5021da177e4SLinus Torvalds if (sym_name) 5031da177e4SLinus Torvalds return sprintf(buffer, "%s", sym_name); 5041da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 5091da177e4SLinus Torvalds /* 5101da177e4SLinus Torvalds * Provides /proc/PID/schedstat 5111da177e4SLinus Torvalds */ 5121da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 5131da177e4SLinus Torvalds { 5141da177e4SLinus Torvalds return sprintf(buffer, "%lu %lu %lu\n", 5151da177e4SLinus Torvalds task->sched_info.cpu_time, 5161da177e4SLinus Torvalds task->sched_info.run_delay, 5171da177e4SLinus Torvalds task->sched_info.pcnt); 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds #endif 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds /* The badness from the OOM killer */ 5221da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 5231da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 5241da177e4SLinus Torvalds { 5251da177e4SLinus Torvalds unsigned long points; 5261da177e4SLinus Torvalds struct timespec uptime; 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 5291da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 5301da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /************************************************************************/ 5341da177e4SLinus Torvalds /* Here the fs part begins */ 5351da177e4SLinus Torvalds /************************************************************************/ 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* permission checks */ 538778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5391da177e4SLinus Torvalds { 540778c1144SEric W. Biederman struct task_struct *task; 541778c1144SEric W. Biederman int allowed = 0; 542df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 543df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 544df26c40eSEric W. Biederman * information. 545778c1144SEric W. Biederman */ 546778c1144SEric W. Biederman task = get_proc_task(inode); 547df26c40eSEric W. Biederman if (task) { 548778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 549778c1144SEric W. Biederman put_task_struct(task); 550df26c40eSEric W. Biederman } 551778c1144SEric W. Biederman return allowed; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5546d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5556d76fa58SLinus Torvalds { 5566d76fa58SLinus Torvalds int error; 5576d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5586d76fa58SLinus Torvalds 5596d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5606d76fa58SLinus Torvalds return -EPERM; 5616d76fa58SLinus Torvalds 5626d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5636d76fa58SLinus Torvalds if (!error) { 5646d76fa58SLinus Torvalds error = security_inode_setattr(dentry, attr); 5656d76fa58SLinus Torvalds if (!error) 5666d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5676d76fa58SLinus Torvalds } 5686d76fa58SLinus Torvalds return error; 5696d76fa58SLinus Torvalds } 5706d76fa58SLinus Torvalds 5716d76fa58SLinus Torvalds static struct inode_operations proc_def_inode_operations = { 5726d76fa58SLinus Torvalds .setattr = proc_setattr, 5736d76fa58SLinus Torvalds }; 5746d76fa58SLinus Torvalds 5751da177e4SLinus Torvalds extern struct seq_operations mounts_op; 5765addc5ddSAl Viro struct proc_mounts { 5775addc5ddSAl Viro struct seq_file m; 5785addc5ddSAl Viro int event; 5795addc5ddSAl Viro }; 5805addc5ddSAl Viro 5811da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 5821da177e4SLinus Torvalds { 58399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 58499f89551SEric W. Biederman struct namespace *namespace = NULL; 5855addc5ddSAl Viro struct proc_mounts *p; 5865addc5ddSAl Viro int ret = -EINVAL; 5875addc5ddSAl Viro 58899f89551SEric W. Biederman if (task) { 5891da177e4SLinus Torvalds task_lock(task); 5901da177e4SLinus Torvalds namespace = task->namespace; 5911da177e4SLinus Torvalds if (namespace) 5921da177e4SLinus Torvalds get_namespace(namespace); 5931da177e4SLinus Torvalds task_unlock(task); 59499f89551SEric W. Biederman put_task_struct(task); 59599f89551SEric W. Biederman } 5961da177e4SLinus Torvalds 5975addc5ddSAl Viro if (namespace) { 5985addc5ddSAl Viro ret = -ENOMEM; 5995addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 6005addc5ddSAl Viro if (p) { 6015addc5ddSAl Viro file->private_data = &p->m; 6025addc5ddSAl Viro ret = seq_open(file, &mounts_op); 6035addc5ddSAl Viro if (!ret) { 6045addc5ddSAl Viro p->m.private = namespace; 6055addc5ddSAl Viro p->event = namespace->event; 6065addc5ddSAl Viro return 0; 6071da177e4SLinus Torvalds } 6085addc5ddSAl Viro kfree(p); 6095addc5ddSAl Viro } 6105addc5ddSAl Viro put_namespace(namespace); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds return ret; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds struct seq_file *m = file->private_data; 6181da177e4SLinus Torvalds struct namespace *namespace = m->private; 6191da177e4SLinus Torvalds put_namespace(namespace); 6201da177e4SLinus Torvalds return seq_release(inode, file); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6235addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6245addc5ddSAl Viro { 6255addc5ddSAl Viro struct proc_mounts *p = file->private_data; 6265addc5ddSAl Viro struct namespace *ns = p->m.private; 6275addc5ddSAl Viro unsigned res = 0; 6285addc5ddSAl Viro 6295addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6305addc5ddSAl Viro 6315addc5ddSAl Viro spin_lock(&vfsmount_lock); 6325addc5ddSAl Viro if (p->event != ns->event) { 6335addc5ddSAl Viro p->event = ns->event; 6345addc5ddSAl Viro res = POLLERR; 6355addc5ddSAl Viro } 6365addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6375addc5ddSAl Viro 6385addc5ddSAl Viro return res; 6395addc5ddSAl Viro } 6405addc5ddSAl Viro 6411da177e4SLinus Torvalds static struct file_operations proc_mounts_operations = { 6421da177e4SLinus Torvalds .open = mounts_open, 6431da177e4SLinus Torvalds .read = seq_read, 6441da177e4SLinus Torvalds .llseek = seq_lseek, 6451da177e4SLinus Torvalds .release = mounts_release, 6465addc5ddSAl Viro .poll = mounts_poll, 6471da177e4SLinus Torvalds }; 6481da177e4SLinus Torvalds 649b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 650b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 651b4629fe2SChuck Lever { 652b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 653b4629fe2SChuck Lever 654b4629fe2SChuck Lever if (!ret) { 655b4629fe2SChuck Lever struct seq_file *m = file->private_data; 65699f89551SEric W. Biederman struct namespace *namespace = NULL; 65799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 65899f89551SEric W. Biederman 65999f89551SEric W. Biederman if (task) { 660b4629fe2SChuck Lever task_lock(task); 661b4629fe2SChuck Lever namespace = task->namespace; 662b4629fe2SChuck Lever if (namespace) 663b4629fe2SChuck Lever get_namespace(namespace); 664b4629fe2SChuck Lever task_unlock(task); 66599f89551SEric W. Biederman put_task_struct(task); 66699f89551SEric W. Biederman } 667b4629fe2SChuck Lever 668b4629fe2SChuck Lever if (namespace) 669b4629fe2SChuck Lever m->private = namespace; 670b4629fe2SChuck Lever else { 671b4629fe2SChuck Lever seq_release(inode, file); 672b4629fe2SChuck Lever ret = -EINVAL; 673b4629fe2SChuck Lever } 674b4629fe2SChuck Lever } 675b4629fe2SChuck Lever return ret; 676b4629fe2SChuck Lever } 677b4629fe2SChuck Lever 678b4629fe2SChuck Lever static struct file_operations proc_mountstats_operations = { 679b4629fe2SChuck Lever .open = mountstats_open, 680b4629fe2SChuck Lever .read = seq_read, 681b4629fe2SChuck Lever .llseek = seq_lseek, 682b4629fe2SChuck Lever .release = mounts_release, 683b4629fe2SChuck Lever }; 684b4629fe2SChuck Lever 6851da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6881da177e4SLinus Torvalds size_t count, loff_t *ppos) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds struct inode * inode = file->f_dentry->d_inode; 6911da177e4SLinus Torvalds unsigned long page; 6921da177e4SLinus Torvalds ssize_t length; 69399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 69499f89551SEric W. Biederman 69599f89551SEric W. Biederman length = -ESRCH; 69699f89551SEric W. Biederman if (!task) 69799f89551SEric W. Biederman goto out_no_task; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 7001da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 70199f89551SEric W. Biederman 70299f89551SEric W. Biederman length = -ENOMEM; 7031da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 70499f89551SEric W. Biederman goto out; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds if (length >= 0) 7091da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7101da177e4SLinus Torvalds free_page(page); 71199f89551SEric W. Biederman out: 71299f89551SEric W. Biederman put_task_struct(task); 71399f89551SEric W. Biederman out_no_task: 7141da177e4SLinus Torvalds return length; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds static struct file_operations proc_info_file_operations = { 7181da177e4SLinus Torvalds .read = proc_info_read, 7191da177e4SLinus Torvalds }; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7281da177e4SLinus Torvalds size_t count, loff_t *ppos) 7291da177e4SLinus Torvalds { 73099f89551SEric W. Biederman struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 7311da177e4SLinus Torvalds char *page; 7321da177e4SLinus Torvalds unsigned long src = *ppos; 7331da177e4SLinus Torvalds int ret = -ESRCH; 7341da177e4SLinus Torvalds struct mm_struct *mm; 7351da177e4SLinus Torvalds 73699f89551SEric W. Biederman if (!task) 73799f89551SEric W. Biederman goto out_no_task; 73899f89551SEric W. Biederman 739ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 7401da177e4SLinus Torvalds goto out; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds ret = -ENOMEM; 7431da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 7441da177e4SLinus Torvalds if (!page) 7451da177e4SLinus Torvalds goto out; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds ret = 0; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds mm = get_task_mm(task); 7501da177e4SLinus Torvalds if (!mm) 7511da177e4SLinus Torvalds goto out_free; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds ret = -EIO; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7561da177e4SLinus Torvalds goto out_put; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds ret = 0; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds while (count > 0) { 7611da177e4SLinus Torvalds int this_len, retval; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7641da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 765ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 7661da177e4SLinus Torvalds if (!ret) 7671da177e4SLinus Torvalds ret = -EIO; 7681da177e4SLinus Torvalds break; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7721da177e4SLinus Torvalds ret = -EFAULT; 7731da177e4SLinus Torvalds break; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds ret += retval; 7771da177e4SLinus Torvalds src += retval; 7781da177e4SLinus Torvalds buf += retval; 7791da177e4SLinus Torvalds count -= retval; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds *ppos = src; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds out_put: 7841da177e4SLinus Torvalds mmput(mm); 7851da177e4SLinus Torvalds out_free: 7861da177e4SLinus Torvalds free_page((unsigned long) page); 7871da177e4SLinus Torvalds out: 78899f89551SEric W. Biederman put_task_struct(task); 78999f89551SEric W. Biederman out_no_task: 7901da177e4SLinus Torvalds return ret; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds #define mem_write NULL 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds #ifndef mem_write 7961da177e4SLinus Torvalds /* This is a security hazard */ 7971da177e4SLinus Torvalds static ssize_t mem_write(struct file * file, const char * buf, 7981da177e4SLinus Torvalds size_t count, loff_t *ppos) 7991da177e4SLinus Torvalds { 800f7ca54f4SFrederik Deweerdt int copied; 8011da177e4SLinus Torvalds char *page; 80299f89551SEric W. Biederman struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 8031da177e4SLinus Torvalds unsigned long dst = *ppos; 8041da177e4SLinus Torvalds 80599f89551SEric W. Biederman copied = -ESRCH; 80699f89551SEric W. Biederman if (!task) 80799f89551SEric W. Biederman goto out_no_task; 8081da177e4SLinus Torvalds 80999f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 81099f89551SEric W. Biederman goto out; 81199f89551SEric W. Biederman 81299f89551SEric W. Biederman copied = -ENOMEM; 8131da177e4SLinus Torvalds page = (char *)__get_free_page(GFP_USER); 8141da177e4SLinus Torvalds if (!page) 81599f89551SEric W. Biederman goto out; 8161da177e4SLinus Torvalds 817f7ca54f4SFrederik Deweerdt copied = 0; 8181da177e4SLinus Torvalds while (count > 0) { 8191da177e4SLinus Torvalds int this_len, retval; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8221da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8231da177e4SLinus Torvalds copied = -EFAULT; 8241da177e4SLinus Torvalds break; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8271da177e4SLinus Torvalds if (!retval) { 8281da177e4SLinus Torvalds if (!copied) 8291da177e4SLinus Torvalds copied = -EIO; 8301da177e4SLinus Torvalds break; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds copied += retval; 8331da177e4SLinus Torvalds buf += retval; 8341da177e4SLinus Torvalds dst += retval; 8351da177e4SLinus Torvalds count -= retval; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds *ppos = dst; 8381da177e4SLinus Torvalds free_page((unsigned long) page); 83999f89551SEric W. Biederman out: 84099f89551SEric W. Biederman put_task_struct(task); 84199f89551SEric W. Biederman out_no_task: 8421da177e4SLinus Torvalds return copied; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds #endif 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds switch (orig) { 8491da177e4SLinus Torvalds case 0: 8501da177e4SLinus Torvalds file->f_pos = offset; 8511da177e4SLinus Torvalds break; 8521da177e4SLinus Torvalds case 1: 8531da177e4SLinus Torvalds file->f_pos += offset; 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds default: 8561da177e4SLinus Torvalds return -EINVAL; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds force_successful_syscall_return(); 8591da177e4SLinus Torvalds return file->f_pos; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds static struct file_operations proc_mem_operations = { 8631da177e4SLinus Torvalds .llseek = mem_lseek, 8641da177e4SLinus Torvalds .read = mem_read, 8651da177e4SLinus Torvalds .write = mem_write, 8661da177e4SLinus Torvalds .open = mem_open, 8671da177e4SLinus Torvalds }; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 8701da177e4SLinus Torvalds size_t count, loff_t *ppos) 8711da177e4SLinus Torvalds { 87299f89551SEric W. Biederman struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 8738578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 8741da177e4SLinus Torvalds size_t len; 87599f89551SEric W. Biederman int oom_adjust; 8761da177e4SLinus Torvalds loff_t __ppos = *ppos; 8771da177e4SLinus Torvalds 87899f89551SEric W. Biederman if (!task) 87999f89551SEric W. Biederman return -ESRCH; 88099f89551SEric W. Biederman oom_adjust = task->oomkilladj; 88199f89551SEric W. Biederman put_task_struct(task); 88299f89551SEric W. Biederman 8838578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 8841da177e4SLinus Torvalds if (__ppos >= len) 8851da177e4SLinus Torvalds return 0; 8861da177e4SLinus Torvalds if (count > len-__ppos) 8871da177e4SLinus Torvalds count = len-__ppos; 8881da177e4SLinus Torvalds if (copy_to_user(buf, buffer + __ppos, count)) 8891da177e4SLinus Torvalds return -EFAULT; 8901da177e4SLinus Torvalds *ppos = __ppos + count; 8911da177e4SLinus Torvalds return count; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 8951da177e4SLinus Torvalds size_t count, loff_t *ppos) 8961da177e4SLinus Torvalds { 89799f89551SEric W. Biederman struct task_struct *task; 8988578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 8991da177e4SLinus Torvalds int oom_adjust; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds if (!capable(CAP_SYS_RESOURCE)) 9021da177e4SLinus Torvalds return -EPERM; 9038578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9048578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9058578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9061da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 9071da177e4SLinus Torvalds return -EFAULT; 9081da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 90979befd0cSAndrea Arcangeli if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE) 9101da177e4SLinus Torvalds return -EINVAL; 9111da177e4SLinus Torvalds if (*end == '\n') 9121da177e4SLinus Torvalds end++; 91399f89551SEric W. Biederman task = get_proc_task(file->f_dentry->d_inode); 91499f89551SEric W. Biederman if (!task) 91599f89551SEric W. Biederman return -ESRCH; 9161da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 91799f89551SEric W. Biederman put_task_struct(task); 9181da177e4SLinus Torvalds if (end - buffer == 0) 9191da177e4SLinus Torvalds return -EIO; 9201da177e4SLinus Torvalds return end - buffer; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds static struct file_operations proc_oom_adjust_operations = { 9241da177e4SLinus Torvalds .read = oom_adjust_read, 9251da177e4SLinus Torvalds .write = oom_adjust_write, 9261da177e4SLinus Torvalds }; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 9291da177e4SLinus Torvalds #define TMPBUFLEN 21 9301da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 9311da177e4SLinus Torvalds size_t count, loff_t *ppos) 9321da177e4SLinus Torvalds { 9331da177e4SLinus Torvalds struct inode * inode = file->f_dentry->d_inode; 93499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 9351da177e4SLinus Torvalds ssize_t length; 9361da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 9371da177e4SLinus Torvalds 93899f89551SEric W. Biederman if (!task) 93999f89551SEric W. Biederman return -ESRCH; 9401da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 9411da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 94299f89551SEric W. Biederman put_task_struct(task); 9431da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 9441da177e4SLinus Torvalds } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 9471da177e4SLinus Torvalds size_t count, loff_t *ppos) 9481da177e4SLinus Torvalds { 9491da177e4SLinus Torvalds struct inode * inode = file->f_dentry->d_inode; 9501da177e4SLinus Torvalds char *page, *tmp; 9511da177e4SLinus Torvalds ssize_t length; 9521da177e4SLinus Torvalds uid_t loginuid; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 9551da177e4SLinus Torvalds return -EPERM; 9561da177e4SLinus Torvalds 95713b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 9581da177e4SLinus Torvalds return -EPERM; 9591da177e4SLinus Torvalds 960e0182909SAl Viro if (count >= PAGE_SIZE) 961e0182909SAl Viro count = PAGE_SIZE - 1; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds if (*ppos != 0) { 9641da177e4SLinus Torvalds /* No partial writes. */ 9651da177e4SLinus Torvalds return -EINVAL; 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds page = (char*)__get_free_page(GFP_USER); 9681da177e4SLinus Torvalds if (!page) 9691da177e4SLinus Torvalds return -ENOMEM; 9701da177e4SLinus Torvalds length = -EFAULT; 9711da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 9721da177e4SLinus Torvalds goto out_free_page; 9731da177e4SLinus Torvalds 974e0182909SAl Viro page[count] = '\0'; 9751da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 9761da177e4SLinus Torvalds if (tmp == page) { 9771da177e4SLinus Torvalds length = -EINVAL; 9781da177e4SLinus Torvalds goto out_free_page; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds } 98199f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 9821da177e4SLinus Torvalds if (likely(length == 0)) 9831da177e4SLinus Torvalds length = count; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds out_free_page: 9861da177e4SLinus Torvalds free_page((unsigned long) page); 9871da177e4SLinus Torvalds return length; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds static struct file_operations proc_loginuid_operations = { 9911da177e4SLinus Torvalds .read = proc_loginuid_read, 9921da177e4SLinus Torvalds .write = proc_loginuid_write, 9931da177e4SLinus Torvalds }; 9941da177e4SLinus Torvalds #endif 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 9971da177e4SLinus Torvalds static ssize_t seccomp_read(struct file *file, char __user *buf, 9981da177e4SLinus Torvalds size_t count, loff_t *ppos) 9991da177e4SLinus Torvalds { 100099f89551SEric W. Biederman struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); 10011da177e4SLinus Torvalds char __buf[20]; 10021da177e4SLinus Torvalds loff_t __ppos = *ppos; 10031da177e4SLinus Torvalds size_t len; 10041da177e4SLinus Torvalds 100599f89551SEric W. Biederman if (!tsk) 100699f89551SEric W. Biederman return -ESRCH; 10071da177e4SLinus Torvalds /* no need to print the trailing zero, so use only len */ 10081da177e4SLinus Torvalds len = sprintf(__buf, "%u\n", tsk->seccomp.mode); 100999f89551SEric W. Biederman put_task_struct(tsk); 10101da177e4SLinus Torvalds if (__ppos >= len) 10111da177e4SLinus Torvalds return 0; 10121da177e4SLinus Torvalds if (count > len - __ppos) 10131da177e4SLinus Torvalds count = len - __ppos; 10141da177e4SLinus Torvalds if (copy_to_user(buf, __buf + __ppos, count)) 10151da177e4SLinus Torvalds return -EFAULT; 10161da177e4SLinus Torvalds *ppos = __ppos + count; 10171da177e4SLinus Torvalds return count; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds static ssize_t seccomp_write(struct file *file, const char __user *buf, 10211da177e4SLinus Torvalds size_t count, loff_t *ppos) 10221da177e4SLinus Torvalds { 102399f89551SEric W. Biederman struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode); 10241da177e4SLinus Torvalds char __buf[20], *end; 10251da177e4SLinus Torvalds unsigned int seccomp_mode; 102699f89551SEric W. Biederman ssize_t result; 102799f89551SEric W. Biederman 102899f89551SEric W. Biederman result = -ESRCH; 102999f89551SEric W. Biederman if (!tsk) 103099f89551SEric W. Biederman goto out_no_task; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds /* can set it only once to be even more secure */ 103399f89551SEric W. Biederman result = -EPERM; 10341da177e4SLinus Torvalds if (unlikely(tsk->seccomp.mode)) 103599f89551SEric W. Biederman goto out; 10361da177e4SLinus Torvalds 103799f89551SEric W. Biederman result = -EFAULT; 10381da177e4SLinus Torvalds memset(__buf, 0, sizeof(__buf)); 10391da177e4SLinus Torvalds count = min(count, sizeof(__buf) - 1); 10401da177e4SLinus Torvalds if (copy_from_user(__buf, buf, count)) 104199f89551SEric W. Biederman goto out; 104299f89551SEric W. Biederman 10431da177e4SLinus Torvalds seccomp_mode = simple_strtoul(__buf, &end, 0); 10441da177e4SLinus Torvalds if (*end == '\n') 10451da177e4SLinus Torvalds end++; 104699f89551SEric W. Biederman result = -EINVAL; 10471da177e4SLinus Torvalds if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { 10481da177e4SLinus Torvalds tsk->seccomp.mode = seccomp_mode; 10491da177e4SLinus Torvalds set_tsk_thread_flag(tsk, TIF_SECCOMP); 10501da177e4SLinus Torvalds } else 105199f89551SEric W. Biederman goto out; 105299f89551SEric W. Biederman result = -EIO; 10531da177e4SLinus Torvalds if (unlikely(!(end - __buf))) 105499f89551SEric W. Biederman goto out; 105599f89551SEric W. Biederman result = end - __buf; 105699f89551SEric W. Biederman out: 105799f89551SEric W. Biederman put_task_struct(tsk); 105899f89551SEric W. Biederman out_no_task: 105999f89551SEric W. Biederman return result; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds static struct file_operations proc_seccomp_operations = { 10631da177e4SLinus Torvalds .read = seccomp_read, 10641da177e4SLinus Torvalds .write = seccomp_write, 10651da177e4SLinus Torvalds }; 10661da177e4SLinus Torvalds #endif /* CONFIG_SECCOMP */ 10671da177e4SLinus Torvalds 1068008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10711da177e4SLinus Torvalds int error = -EACCES; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 10741da177e4SLinus Torvalds path_release(nd); 10751da177e4SLinus Torvalds 1076778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1077778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10781da177e4SLinus Torvalds goto out; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 10811da177e4SLinus Torvalds nd->last_type = LAST_BIND; 10821da177e4SLinus Torvalds out: 1083008b150aSAl Viro return ERR_PTR(error); 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10871da177e4SLinus Torvalds char __user *buffer, int buflen) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct inode * inode; 10901da177e4SLinus Torvalds char *tmp = (char*)__get_free_page(GFP_KERNEL), *path; 10911da177e4SLinus Torvalds int len; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds if (!tmp) 10941da177e4SLinus Torvalds return -ENOMEM; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds inode = dentry->d_inode; 10971da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10981da177e4SLinus Torvalds len = PTR_ERR(path); 10991da177e4SLinus Torvalds if (IS_ERR(path)) 11001da177e4SLinus Torvalds goto out; 11011da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds if (len > buflen) 11041da177e4SLinus Torvalds len = buflen; 11051da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 11061da177e4SLinus Torvalds len = -EFAULT; 11071da177e4SLinus Torvalds out: 11081da177e4SLinus Torvalds free_page((unsigned long)tmp); 11091da177e4SLinus Torvalds return len; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds int error = -EACCES; 11151da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11161da177e4SLinus Torvalds struct dentry *de; 11171da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 11181da177e4SLinus Torvalds 1119778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1120778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 11211da177e4SLinus Torvalds goto out; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 11241da177e4SLinus Torvalds if (error) 11251da177e4SLinus Torvalds goto out; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 11281da177e4SLinus Torvalds dput(de); 11291da177e4SLinus Torvalds mntput(mnt); 11301da177e4SLinus Torvalds out: 11311da177e4SLinus Torvalds return error; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds static struct inode_operations proc_pid_link_inode_operations = { 11351da177e4SLinus Torvalds .readlink = proc_pid_readlink, 11366d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 11376d76fa58SLinus Torvalds .setattr = proc_setattr, 11381da177e4SLinus Torvalds }; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) 11411da177e4SLinus Torvalds { 11425634708bSEric W. Biederman struct dentry *dentry = filp->f_dentry; 11435634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 114499f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 11451da177e4SLinus Torvalds unsigned int fd, tid, ino; 11461da177e4SLinus Torvalds int retval; 11478578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 11481da177e4SLinus Torvalds struct files_struct * files; 1149badf1662SDipankar Sarma struct fdtable *fdt; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds retval = -ENOENT; 115299f89551SEric W. Biederman if (!p) 115399f89551SEric W. Biederman goto out_no_task; 11541da177e4SLinus Torvalds retval = 0; 11551da177e4SLinus Torvalds tid = p->pid; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds fd = filp->f_pos; 11581da177e4SLinus Torvalds switch (fd) { 11591da177e4SLinus Torvalds case 0: 11601da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 11611da177e4SLinus Torvalds goto out; 11621da177e4SLinus Torvalds filp->f_pos++; 11631da177e4SLinus Torvalds case 1: 11645634708bSEric W. Biederman ino = parent_ino(dentry); 11651da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 11661da177e4SLinus Torvalds goto out; 11671da177e4SLinus Torvalds filp->f_pos++; 11681da177e4SLinus Torvalds default: 11691da177e4SLinus Torvalds files = get_files_struct(p); 11701da177e4SLinus Torvalds if (!files) 11711da177e4SLinus Torvalds goto out; 1172b835996fSDipankar Sarma rcu_read_lock(); 1173badf1662SDipankar Sarma fdt = files_fdtable(files); 11741da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1175badf1662SDipankar Sarma fd < fdt->max_fds; 11761da177e4SLinus Torvalds fd++, filp->f_pos++) { 11771da177e4SLinus Torvalds unsigned int i,j; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 11801da177e4SLinus Torvalds continue; 1181b835996fSDipankar Sarma rcu_read_unlock(); 11821da177e4SLinus Torvalds 11838578cea7SEric W. Biederman j = PROC_NUMBUF; 11841da177e4SLinus Torvalds i = fd; 11851da177e4SLinus Torvalds do { 11861da177e4SLinus Torvalds j--; 11871da177e4SLinus Torvalds buf[j] = '0' + (i % 10); 11881da177e4SLinus Torvalds i /= 10; 11891da177e4SLinus Torvalds } while (i); 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds ino = fake_ino(tid, PROC_TID_FD_DIR + fd); 11928578cea7SEric W. Biederman if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) { 1193b835996fSDipankar Sarma rcu_read_lock(); 11941da177e4SLinus Torvalds break; 11951da177e4SLinus Torvalds } 1196b835996fSDipankar Sarma rcu_read_lock(); 11971da177e4SLinus Torvalds } 1198b835996fSDipankar Sarma rcu_read_unlock(); 11991da177e4SLinus Torvalds put_files_struct(files); 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds out: 120299f89551SEric W. Biederman put_task_struct(p); 120399f89551SEric W. Biederman out_no_task: 12041da177e4SLinus Torvalds return retval; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds static int proc_pident_readdir(struct file *filp, 12081da177e4SLinus Torvalds void *dirent, filldir_t filldir, 12091da177e4SLinus Torvalds struct pid_entry *ents, unsigned int nents) 12101da177e4SLinus Torvalds { 12111da177e4SLinus Torvalds int i; 12121da177e4SLinus Torvalds int pid; 12131da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 12141da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 121599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 12161da177e4SLinus Torvalds struct pid_entry *p; 12171da177e4SLinus Torvalds ino_t ino; 12181da177e4SLinus Torvalds int ret; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds ret = -ENOENT; 122199f89551SEric W. Biederman if (!task) 12221da177e4SLinus Torvalds goto out; 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds ret = 0; 122599f89551SEric W. Biederman pid = task->pid; 122699f89551SEric W. Biederman put_task_struct(task); 12271da177e4SLinus Torvalds i = filp->f_pos; 12281da177e4SLinus Torvalds switch (i) { 12291da177e4SLinus Torvalds case 0: 12301da177e4SLinus Torvalds ino = inode->i_ino; 12311da177e4SLinus Torvalds if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 12321da177e4SLinus Torvalds goto out; 12331da177e4SLinus Torvalds i++; 12341da177e4SLinus Torvalds filp->f_pos++; 12351da177e4SLinus Torvalds /* fall through */ 12361da177e4SLinus Torvalds case 1: 12371da177e4SLinus Torvalds ino = parent_ino(dentry); 12381da177e4SLinus Torvalds if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 12391da177e4SLinus Torvalds goto out; 12401da177e4SLinus Torvalds i++; 12411da177e4SLinus Torvalds filp->f_pos++; 12421da177e4SLinus Torvalds /* fall through */ 12431da177e4SLinus Torvalds default: 12441da177e4SLinus Torvalds i -= 2; 12451da177e4SLinus Torvalds if (i >= nents) { 12461da177e4SLinus Torvalds ret = 1; 12471da177e4SLinus Torvalds goto out; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds p = ents + i; 12501da177e4SLinus Torvalds while (p->name) { 12511da177e4SLinus Torvalds if (filldir(dirent, p->name, p->len, filp->f_pos, 12521da177e4SLinus Torvalds fake_ino(pid, p->type), p->mode >> 12) < 0) 12531da177e4SLinus Torvalds goto out; 12541da177e4SLinus Torvalds filp->f_pos++; 12551da177e4SLinus Torvalds p++; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds ret = 1; 12601da177e4SLinus Torvalds out: 12611da177e4SLinus Torvalds return ret; 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds static int proc_tgid_base_readdir(struct file * filp, 12651da177e4SLinus Torvalds void * dirent, filldir_t filldir) 12661da177e4SLinus Torvalds { 12671da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 12681da177e4SLinus Torvalds tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds static int proc_tid_base_readdir(struct file * filp, 12721da177e4SLinus Torvalds void * dirent, filldir_t filldir) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 12751da177e4SLinus Torvalds tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* building an inode */ 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds static int task_dumpable(struct task_struct *task) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds int dumpable = 0; 12831da177e4SLinus Torvalds struct mm_struct *mm; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds task_lock(task); 12861da177e4SLinus Torvalds mm = task->mm; 12871da177e4SLinus Torvalds if (mm) 12881da177e4SLinus Torvalds dumpable = mm->dumpable; 12891da177e4SLinus Torvalds task_unlock(task); 1290d6e71144SAlan Cox if(dumpable == 1) 1291d6e71144SAlan Cox return 1; 1292d6e71144SAlan Cox return 0; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino) 12971da177e4SLinus Torvalds { 12981da177e4SLinus Torvalds struct inode * inode; 12991da177e4SLinus Torvalds struct proc_inode *ei; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* We need a new inode */ 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds inode = new_inode(sb); 13041da177e4SLinus Torvalds if (!inode) 13051da177e4SLinus Torvalds goto out; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* Common stuff */ 13081da177e4SLinus Torvalds ei = PROC_I(inode); 13091da177e4SLinus Torvalds inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 13101da177e4SLinus Torvalds inode->i_ino = fake_ino(task->pid, ino); 13116d76fa58SLinus Torvalds inode->i_op = &proc_def_inode_operations; 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * grab the reference to task. 13151da177e4SLinus Torvalds */ 131613b41b09SEric W. Biederman ei->pid = get_pid(task->pids[PIDTYPE_PID].pid); 131713b41b09SEric W. Biederman if (!ei->pid) 131899f89551SEric W. Biederman goto out_unlock; 131999f89551SEric W. Biederman 13201da177e4SLinus Torvalds inode->i_uid = 0; 13211da177e4SLinus Torvalds inode->i_gid = 0; 132287bfbf67SEric W. Biederman if (task_dumpable(task)) { 13231da177e4SLinus Torvalds inode->i_uid = task->euid; 13241da177e4SLinus Torvalds inode->i_gid = task->egid; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds security_task_to_inode(task, inode); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds out: 13291da177e4SLinus Torvalds return inode; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds out_unlock: 13321da177e4SLinus Torvalds iput(inode); 13331da177e4SLinus Torvalds return NULL; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* dentry stuff */ 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* 13391da177e4SLinus Torvalds * Exceptional case: normally we are not allowed to unhash a busy 13401da177e4SLinus Torvalds * directory. In this case, however, we can do it - no aliasing problems 13411da177e4SLinus Torvalds * due to the way we treat inodes. 13421da177e4SLinus Torvalds * 13431da177e4SLinus Torvalds * Rewrite the inode's ownerships here because the owning task may have 13441da177e4SLinus Torvalds * performed a setuid(), etc. 134599f89551SEric W. Biederman * 134699f89551SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 134799f89551SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 134899f89551SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 134999f89551SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 135099f89551SEric W. Biederman * made this apply to all per process world readable and executable 135199f89551SEric W. Biederman * directories. 13521da177e4SLinus Torvalds */ 13531da177e4SLinus Torvalds static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 13541da177e4SLinus Torvalds { 13551da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 135699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 135799f89551SEric W. Biederman if (task) { 135899f89551SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 135999f89551SEric W. Biederman task_dumpable(task)) { 13601da177e4SLinus Torvalds inode->i_uid = task->euid; 13611da177e4SLinus Torvalds inode->i_gid = task->egid; 13621da177e4SLinus Torvalds } else { 13631da177e4SLinus Torvalds inode->i_uid = 0; 13641da177e4SLinus Torvalds inode->i_gid = 0; 13651da177e4SLinus Torvalds } 13669ee8ab9fSLinus Torvalds inode->i_mode &= ~(S_ISUID | S_ISGID); 13671da177e4SLinus Torvalds security_task_to_inode(task, inode); 136899f89551SEric W. Biederman put_task_struct(task); 13691da177e4SLinus Torvalds return 1; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds d_drop(dentry); 13721da177e4SLinus Torvalds return 0; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 137599f89551SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 137699f89551SEric W. Biederman { 137799f89551SEric W. Biederman struct inode *inode = dentry->d_inode; 137899f89551SEric W. Biederman struct task_struct *task; 137999f89551SEric W. Biederman generic_fillattr(inode, stat); 138099f89551SEric W. Biederman 138199f89551SEric W. Biederman rcu_read_lock(); 138299f89551SEric W. Biederman stat->uid = 0; 138399f89551SEric W. Biederman stat->gid = 0; 138499f89551SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 138599f89551SEric W. Biederman if (task) { 138699f89551SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 138799f89551SEric W. Biederman task_dumpable(task)) { 138899f89551SEric W. Biederman stat->uid = task->euid; 138999f89551SEric W. Biederman stat->gid = task->egid; 139099f89551SEric W. Biederman } 139199f89551SEric W. Biederman } 139299f89551SEric W. Biederman rcu_read_unlock(); 139399f89551SEric W. Biederman return 0; 139499f89551SEric W. Biederman } 139599f89551SEric W. Biederman 13961da177e4SLinus Torvalds static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 13971da177e4SLinus Torvalds { 13981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 139999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1400aed7a6c4SEric W. Biederman int fd = proc_fd(inode); 14011da177e4SLinus Torvalds struct files_struct *files; 14021da177e4SLinus Torvalds 140399f89551SEric W. Biederman if (task) { 14041da177e4SLinus Torvalds files = get_files_struct(task); 14051da177e4SLinus Torvalds if (files) { 1406b835996fSDipankar Sarma rcu_read_lock(); 14071da177e4SLinus Torvalds if (fcheck_files(files, fd)) { 1408b835996fSDipankar Sarma rcu_read_unlock(); 14091da177e4SLinus Torvalds put_files_struct(files); 14101da177e4SLinus Torvalds if (task_dumpable(task)) { 14111da177e4SLinus Torvalds inode->i_uid = task->euid; 14121da177e4SLinus Torvalds inode->i_gid = task->egid; 14131da177e4SLinus Torvalds } else { 14141da177e4SLinus Torvalds inode->i_uid = 0; 14151da177e4SLinus Torvalds inode->i_gid = 0; 14161da177e4SLinus Torvalds } 14179ee8ab9fSLinus Torvalds inode->i_mode &= ~(S_ISUID | S_ISGID); 14181da177e4SLinus Torvalds security_task_to_inode(task, inode); 141999f89551SEric W. Biederman put_task_struct(task); 14201da177e4SLinus Torvalds return 1; 14211da177e4SLinus Torvalds } 1422b835996fSDipankar Sarma rcu_read_unlock(); 14231da177e4SLinus Torvalds put_files_struct(files); 14241da177e4SLinus Torvalds } 142599f89551SEric W. Biederman put_task_struct(task); 142699f89551SEric W. Biederman } 14271da177e4SLinus Torvalds d_drop(dentry); 14281da177e4SLinus Torvalds return 0; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds static int pid_delete_dentry(struct dentry * dentry) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds /* Is the task we represent dead? 14341da177e4SLinus Torvalds * If so, then don't put the dentry on the lru list, 14351da177e4SLinus Torvalds * kill it immediately. 14361da177e4SLinus Torvalds */ 143713b41b09SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds static struct dentry_operations tid_fd_dentry_operations = 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds .d_revalidate = tid_fd_revalidate, 14431da177e4SLinus Torvalds .d_delete = pid_delete_dentry, 14441da177e4SLinus Torvalds }; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds static struct dentry_operations pid_dentry_operations = 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds .d_revalidate = pid_revalidate, 14491da177e4SLinus Torvalds .d_delete = pid_delete_dentry, 14501da177e4SLinus Torvalds }; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /* Lookups */ 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds static unsigned name_to_int(struct dentry *dentry) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds const char *name = dentry->d_name.name; 14571da177e4SLinus Torvalds int len = dentry->d_name.len; 14581da177e4SLinus Torvalds unsigned n = 0; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds if (len > 1 && *name == '0') 14611da177e4SLinus Torvalds goto out; 14621da177e4SLinus Torvalds while (len-- > 0) { 14631da177e4SLinus Torvalds unsigned c = *name++ - '0'; 14641da177e4SLinus Torvalds if (c > 9) 14651da177e4SLinus Torvalds goto out; 14661da177e4SLinus Torvalds if (n >= (~0U-9)/10) 14671da177e4SLinus Torvalds goto out; 14681da177e4SLinus Torvalds n *= 10; 14691da177e4SLinus Torvalds n += c; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds return n; 14721da177e4SLinus Torvalds out: 14731da177e4SLinus Torvalds return ~0U; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* SMP-safe */ 14771da177e4SLinus Torvalds static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd) 14781da177e4SLinus Torvalds { 147999f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 14801da177e4SLinus Torvalds unsigned fd = name_to_int(dentry); 1481cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 14821da177e4SLinus Torvalds struct file * file; 14831da177e4SLinus Torvalds struct files_struct * files; 14841da177e4SLinus Torvalds struct inode *inode; 14851da177e4SLinus Torvalds struct proc_inode *ei; 14861da177e4SLinus Torvalds 148799f89551SEric W. Biederman if (!task) 148899f89551SEric W. Biederman goto out_no_task; 14891da177e4SLinus Torvalds if (fd == ~0U) 14901da177e4SLinus Torvalds goto out; 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd); 14931da177e4SLinus Torvalds if (!inode) 14941da177e4SLinus Torvalds goto out; 14951da177e4SLinus Torvalds ei = PROC_I(inode); 1496aed7a6c4SEric W. Biederman ei->fd = fd; 14971da177e4SLinus Torvalds files = get_files_struct(task); 14981da177e4SLinus Torvalds if (!files) 14991da177e4SLinus Torvalds goto out_unlock; 15001da177e4SLinus Torvalds inode->i_mode = S_IFLNK; 1501ca99c1daSDipankar Sarma 1502ca99c1daSDipankar Sarma /* 1503ca99c1daSDipankar Sarma * We are not taking a ref to the file structure, so we must 1504ca99c1daSDipankar Sarma * hold ->file_lock. 1505ca99c1daSDipankar Sarma */ 1506ca99c1daSDipankar Sarma spin_lock(&files->file_lock); 15071da177e4SLinus Torvalds file = fcheck_files(files, fd); 15081da177e4SLinus Torvalds if (!file) 15091da177e4SLinus Torvalds goto out_unlock2; 15101da177e4SLinus Torvalds if (file->f_mode & 1) 15111da177e4SLinus Torvalds inode->i_mode |= S_IRUSR | S_IXUSR; 15121da177e4SLinus Torvalds if (file->f_mode & 2) 15131da177e4SLinus Torvalds inode->i_mode |= S_IWUSR | S_IXUSR; 1514ca99c1daSDipankar Sarma spin_unlock(&files->file_lock); 15151da177e4SLinus Torvalds put_files_struct(files); 15161da177e4SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 15171da177e4SLinus Torvalds inode->i_size = 64; 15181da177e4SLinus Torvalds ei->op.proc_get_link = proc_fd_link; 15191da177e4SLinus Torvalds dentry->d_op = &tid_fd_dentry_operations; 15201da177e4SLinus Torvalds d_add(dentry, inode); 1521cd6a3ce9SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1522cd6a3ce9SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1523cd6a3ce9SEric W. Biederman result = NULL; 1524cd6a3ce9SEric W. Biederman out: 152599f89551SEric W. Biederman put_task_struct(task); 152699f89551SEric W. Biederman out_no_task: 1527cd6a3ce9SEric W. Biederman return result; 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds out_unlock2: 1530ca99c1daSDipankar Sarma spin_unlock(&files->file_lock); 15311da177e4SLinus Torvalds put_files_struct(files); 15321da177e4SLinus Torvalds out_unlock: 15331da177e4SLinus Torvalds iput(inode); 1534cd6a3ce9SEric W. Biederman goto out; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir); 15381da177e4SLinus Torvalds static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd); 15396e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat); 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds static struct file_operations proc_fd_operations = { 15421da177e4SLinus Torvalds .read = generic_read_dir, 15431da177e4SLinus Torvalds .readdir = proc_readfd, 15441da177e4SLinus Torvalds }; 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds static struct file_operations proc_task_operations = { 15471da177e4SLinus Torvalds .read = generic_read_dir, 15481da177e4SLinus Torvalds .readdir = proc_task_readdir, 15491da177e4SLinus Torvalds }; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds /* 15521da177e4SLinus Torvalds * proc directories can do almost nothing.. 15531da177e4SLinus Torvalds */ 15541da177e4SLinus Torvalds static struct inode_operations proc_fd_inode_operations = { 15551da177e4SLinus Torvalds .lookup = proc_lookupfd, 15566d76fa58SLinus Torvalds .setattr = proc_setattr, 15571da177e4SLinus Torvalds }; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds static struct inode_operations proc_task_inode_operations = { 15601da177e4SLinus Torvalds .lookup = proc_task_lookup, 15616e66b52bSEric W. Biederman .getattr = proc_task_getattr, 15626d76fa58SLinus Torvalds .setattr = proc_setattr, 15631da177e4SLinus Torvalds }; 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 15661da177e4SLinus Torvalds static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 15671da177e4SLinus Torvalds size_t count, loff_t *ppos) 15681da177e4SLinus Torvalds { 15691da177e4SLinus Torvalds struct inode * inode = file->f_dentry->d_inode; 15701da177e4SLinus Torvalds unsigned long page; 15711da177e4SLinus Torvalds ssize_t length; 157299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 157399f89551SEric W. Biederman 157499f89551SEric W. Biederman length = -ESRCH; 157599f89551SEric W. Biederman if (!task) 157699f89551SEric W. Biederman goto out_no_task; 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds if (count > PAGE_SIZE) 15791da177e4SLinus Torvalds count = PAGE_SIZE; 158099f89551SEric W. Biederman length = -ENOMEM; 15811da177e4SLinus Torvalds if (!(page = __get_free_page(GFP_KERNEL))) 158299f89551SEric W. Biederman goto out; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds length = security_getprocattr(task, 15851da177e4SLinus Torvalds (char*)file->f_dentry->d_name.name, 15861da177e4SLinus Torvalds (void*)page, count); 15871da177e4SLinus Torvalds if (length >= 0) 15881da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 15891da177e4SLinus Torvalds free_page(page); 159099f89551SEric W. Biederman out: 159199f89551SEric W. Biederman put_task_struct(task); 159299f89551SEric W. Biederman out_no_task: 15931da177e4SLinus Torvalds return length; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 15971da177e4SLinus Torvalds size_t count, loff_t *ppos) 15981da177e4SLinus Torvalds { 15991da177e4SLinus Torvalds struct inode * inode = file->f_dentry->d_inode; 16001da177e4SLinus Torvalds char *page; 16011da177e4SLinus Torvalds ssize_t length; 160299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 16031da177e4SLinus Torvalds 160499f89551SEric W. Biederman length = -ESRCH; 160599f89551SEric W. Biederman if (!task) 160699f89551SEric W. Biederman goto out_no_task; 16071da177e4SLinus Torvalds if (count > PAGE_SIZE) 16081da177e4SLinus Torvalds count = PAGE_SIZE; 160999f89551SEric W. Biederman 16101da177e4SLinus Torvalds /* No partial writes. */ 161199f89551SEric W. Biederman length = -EINVAL; 161299f89551SEric W. Biederman if (*ppos != 0) 161399f89551SEric W. Biederman goto out; 161499f89551SEric W. Biederman 161599f89551SEric W. Biederman length = -ENOMEM; 16161da177e4SLinus Torvalds page = (char*)__get_free_page(GFP_USER); 16171da177e4SLinus Torvalds if (!page) 161899f89551SEric W. Biederman goto out; 161999f89551SEric W. Biederman 16201da177e4SLinus Torvalds length = -EFAULT; 16211da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 162299f89551SEric W. Biederman goto out_free; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds length = security_setprocattr(task, 16251da177e4SLinus Torvalds (char*)file->f_dentry->d_name.name, 16261da177e4SLinus Torvalds (void*)page, count); 162799f89551SEric W. Biederman out_free: 16281da177e4SLinus Torvalds free_page((unsigned long) page); 162999f89551SEric W. Biederman out: 163099f89551SEric W. Biederman put_task_struct(task); 163199f89551SEric W. Biederman out_no_task: 16321da177e4SLinus Torvalds return length; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds static struct file_operations proc_pid_attr_operations = { 16361da177e4SLinus Torvalds .read = proc_pid_attr_read, 16371da177e4SLinus Torvalds .write = proc_pid_attr_write, 16381da177e4SLinus Torvalds }; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds static struct file_operations proc_tid_attr_operations; 16411da177e4SLinus Torvalds static struct inode_operations proc_tid_attr_inode_operations; 16421da177e4SLinus Torvalds static struct file_operations proc_tgid_attr_operations; 16431da177e4SLinus Torvalds static struct inode_operations proc_tgid_attr_inode_operations; 16441da177e4SLinus Torvalds #endif 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* SMP-safe */ 16471da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 16481da177e4SLinus Torvalds struct dentry *dentry, 16491da177e4SLinus Torvalds struct pid_entry *ents) 16501da177e4SLinus Torvalds { 16511da177e4SLinus Torvalds struct inode *inode; 1652cd6a3ce9SEric W. Biederman struct dentry *error; 165399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 16541da177e4SLinus Torvalds struct pid_entry *p; 16551da177e4SLinus Torvalds struct proc_inode *ei; 16561da177e4SLinus Torvalds 1657cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 16581da177e4SLinus Torvalds inode = NULL; 16591da177e4SLinus Torvalds 166099f89551SEric W. Biederman if (!task) 166199f89551SEric W. Biederman goto out_no_task; 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds for (p = ents; p->name; p++) { 16641da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16651da177e4SLinus Torvalds continue; 16661da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16671da177e4SLinus Torvalds break; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds if (!p->name) 16701da177e4SLinus Torvalds goto out; 16711da177e4SLinus Torvalds 1672cd6a3ce9SEric W. Biederman error = ERR_PTR(-EINVAL); 16731da177e4SLinus Torvalds inode = proc_pid_make_inode(dir->i_sb, task, p->type); 16741da177e4SLinus Torvalds if (!inode) 16751da177e4SLinus Torvalds goto out; 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds ei = PROC_I(inode); 16781da177e4SLinus Torvalds inode->i_mode = p->mode; 16791da177e4SLinus Torvalds /* 16801da177e4SLinus Torvalds * Yes, it does not scale. And it should not. Don't add 16811da177e4SLinus Torvalds * new entries into /proc/<tgid>/ without very good reasons. 16821da177e4SLinus Torvalds */ 16831da177e4SLinus Torvalds switch(p->type) { 16841da177e4SLinus Torvalds case PROC_TGID_TASK: 16856e66b52bSEric W. Biederman inode->i_nlink = 2; 16861da177e4SLinus Torvalds inode->i_op = &proc_task_inode_operations; 16871da177e4SLinus Torvalds inode->i_fop = &proc_task_operations; 16881da177e4SLinus Torvalds break; 16891da177e4SLinus Torvalds case PROC_TID_FD: 16901da177e4SLinus Torvalds case PROC_TGID_FD: 16911da177e4SLinus Torvalds inode->i_nlink = 2; 16921da177e4SLinus Torvalds inode->i_op = &proc_fd_inode_operations; 16931da177e4SLinus Torvalds inode->i_fop = &proc_fd_operations; 16941da177e4SLinus Torvalds break; 16951da177e4SLinus Torvalds case PROC_TID_EXE: 16961da177e4SLinus Torvalds case PROC_TGID_EXE: 16971da177e4SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 16981da177e4SLinus Torvalds ei->op.proc_get_link = proc_exe_link; 16991da177e4SLinus Torvalds break; 17001da177e4SLinus Torvalds case PROC_TID_CWD: 17011da177e4SLinus Torvalds case PROC_TGID_CWD: 17021da177e4SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 17031da177e4SLinus Torvalds ei->op.proc_get_link = proc_cwd_link; 17041da177e4SLinus Torvalds break; 17051da177e4SLinus Torvalds case PROC_TID_ROOT: 17061da177e4SLinus Torvalds case PROC_TGID_ROOT: 17071da177e4SLinus Torvalds inode->i_op = &proc_pid_link_inode_operations; 17081da177e4SLinus Torvalds ei->op.proc_get_link = proc_root_link; 17091da177e4SLinus Torvalds break; 17101da177e4SLinus Torvalds case PROC_TID_ENVIRON: 17111da177e4SLinus Torvalds case PROC_TGID_ENVIRON: 17121da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17131da177e4SLinus Torvalds ei->op.proc_read = proc_pid_environ; 17141da177e4SLinus Torvalds break; 17151da177e4SLinus Torvalds case PROC_TID_AUXV: 17161da177e4SLinus Torvalds case PROC_TGID_AUXV: 17171da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17181da177e4SLinus Torvalds ei->op.proc_read = proc_pid_auxv; 17191da177e4SLinus Torvalds break; 17201da177e4SLinus Torvalds case PROC_TID_STATUS: 17211da177e4SLinus Torvalds case PROC_TGID_STATUS: 17221da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17231da177e4SLinus Torvalds ei->op.proc_read = proc_pid_status; 17241da177e4SLinus Torvalds break; 17251da177e4SLinus Torvalds case PROC_TID_STAT: 17261da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17271da177e4SLinus Torvalds ei->op.proc_read = proc_tid_stat; 17281da177e4SLinus Torvalds break; 17291da177e4SLinus Torvalds case PROC_TGID_STAT: 17301da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17311da177e4SLinus Torvalds ei->op.proc_read = proc_tgid_stat; 17321da177e4SLinus Torvalds break; 17331da177e4SLinus Torvalds case PROC_TID_CMDLINE: 17341da177e4SLinus Torvalds case PROC_TGID_CMDLINE: 17351da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17361da177e4SLinus Torvalds ei->op.proc_read = proc_pid_cmdline; 17371da177e4SLinus Torvalds break; 17381da177e4SLinus Torvalds case PROC_TID_STATM: 17391da177e4SLinus Torvalds case PROC_TGID_STATM: 17401da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 17411da177e4SLinus Torvalds ei->op.proc_read = proc_pid_statm; 17421da177e4SLinus Torvalds break; 17431da177e4SLinus Torvalds case PROC_TID_MAPS: 17441da177e4SLinus Torvalds case PROC_TGID_MAPS: 17451da177e4SLinus Torvalds inode->i_fop = &proc_maps_operations; 17461da177e4SLinus Torvalds break; 17476e21c8f1SChristoph Lameter #ifdef CONFIG_NUMA 17486e21c8f1SChristoph Lameter case PROC_TID_NUMA_MAPS: 17496e21c8f1SChristoph Lameter case PROC_TGID_NUMA_MAPS: 17506e21c8f1SChristoph Lameter inode->i_fop = &proc_numa_maps_operations; 17516e21c8f1SChristoph Lameter break; 17526e21c8f1SChristoph Lameter #endif 17531da177e4SLinus Torvalds case PROC_TID_MEM: 17541da177e4SLinus Torvalds case PROC_TGID_MEM: 17551da177e4SLinus Torvalds inode->i_fop = &proc_mem_operations; 17561da177e4SLinus Torvalds break; 17571da177e4SLinus Torvalds #ifdef CONFIG_SECCOMP 17581da177e4SLinus Torvalds case PROC_TID_SECCOMP: 17591da177e4SLinus Torvalds case PROC_TGID_SECCOMP: 17601da177e4SLinus Torvalds inode->i_fop = &proc_seccomp_operations; 17611da177e4SLinus Torvalds break; 17621da177e4SLinus Torvalds #endif /* CONFIG_SECCOMP */ 17631da177e4SLinus Torvalds case PROC_TID_MOUNTS: 17641da177e4SLinus Torvalds case PROC_TGID_MOUNTS: 17651da177e4SLinus Torvalds inode->i_fop = &proc_mounts_operations; 17661da177e4SLinus Torvalds break; 176763c6764cSYoshinori Sato #ifdef CONFIG_MMU 1768e070ad49SMauricio Lin case PROC_TID_SMAPS: 1769e070ad49SMauricio Lin case PROC_TGID_SMAPS: 1770e070ad49SMauricio Lin inode->i_fop = &proc_smaps_operations; 1771e070ad49SMauricio Lin break; 177263c6764cSYoshinori Sato #endif 1773b4629fe2SChuck Lever case PROC_TID_MOUNTSTATS: 1774b4629fe2SChuck Lever case PROC_TGID_MOUNTSTATS: 1775b4629fe2SChuck Lever inode->i_fop = &proc_mountstats_operations; 1776b4629fe2SChuck Lever break; 17771da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 17781da177e4SLinus Torvalds case PROC_TID_ATTR: 17791da177e4SLinus Torvalds inode->i_nlink = 2; 17801da177e4SLinus Torvalds inode->i_op = &proc_tid_attr_inode_operations; 17811da177e4SLinus Torvalds inode->i_fop = &proc_tid_attr_operations; 17821da177e4SLinus Torvalds break; 17831da177e4SLinus Torvalds case PROC_TGID_ATTR: 17841da177e4SLinus Torvalds inode->i_nlink = 2; 17851da177e4SLinus Torvalds inode->i_op = &proc_tgid_attr_inode_operations; 17861da177e4SLinus Torvalds inode->i_fop = &proc_tgid_attr_operations; 17871da177e4SLinus Torvalds break; 17881da177e4SLinus Torvalds case PROC_TID_ATTR_CURRENT: 17891da177e4SLinus Torvalds case PROC_TGID_ATTR_CURRENT: 17901da177e4SLinus Torvalds case PROC_TID_ATTR_PREV: 17911da177e4SLinus Torvalds case PROC_TGID_ATTR_PREV: 17921da177e4SLinus Torvalds case PROC_TID_ATTR_EXEC: 17931da177e4SLinus Torvalds case PROC_TGID_ATTR_EXEC: 17941da177e4SLinus Torvalds case PROC_TID_ATTR_FSCREATE: 17951da177e4SLinus Torvalds case PROC_TGID_ATTR_FSCREATE: 17964eb582cfSMichael LeMay case PROC_TID_ATTR_KEYCREATE: 17974eb582cfSMichael LeMay case PROC_TGID_ATTR_KEYCREATE: 179842c3e03eSEric Paris case PROC_TID_ATTR_SOCKCREATE: 179942c3e03eSEric Paris case PROC_TGID_ATTR_SOCKCREATE: 18001da177e4SLinus Torvalds inode->i_fop = &proc_pid_attr_operations; 18011da177e4SLinus Torvalds break; 18021da177e4SLinus Torvalds #endif 18031da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 18041da177e4SLinus Torvalds case PROC_TID_WCHAN: 18051da177e4SLinus Torvalds case PROC_TGID_WCHAN: 18061da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 18071da177e4SLinus Torvalds ei->op.proc_read = proc_pid_wchan; 18081da177e4SLinus Torvalds break; 18091da177e4SLinus Torvalds #endif 18101da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 18111da177e4SLinus Torvalds case PROC_TID_SCHEDSTAT: 18121da177e4SLinus Torvalds case PROC_TGID_SCHEDSTAT: 18131da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 18141da177e4SLinus Torvalds ei->op.proc_read = proc_pid_schedstat; 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds #endif 18171da177e4SLinus Torvalds #ifdef CONFIG_CPUSETS 18181da177e4SLinus Torvalds case PROC_TID_CPUSET: 18191da177e4SLinus Torvalds case PROC_TGID_CPUSET: 18201da177e4SLinus Torvalds inode->i_fop = &proc_cpuset_operations; 18211da177e4SLinus Torvalds break; 18221da177e4SLinus Torvalds #endif 18231da177e4SLinus Torvalds case PROC_TID_OOM_SCORE: 18241da177e4SLinus Torvalds case PROC_TGID_OOM_SCORE: 18251da177e4SLinus Torvalds inode->i_fop = &proc_info_file_operations; 18261da177e4SLinus Torvalds ei->op.proc_read = proc_oom_score; 18271da177e4SLinus Torvalds break; 18281da177e4SLinus Torvalds case PROC_TID_OOM_ADJUST: 18291da177e4SLinus Torvalds case PROC_TGID_OOM_ADJUST: 18301da177e4SLinus Torvalds inode->i_fop = &proc_oom_adjust_operations; 18311da177e4SLinus Torvalds break; 18321da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 18331da177e4SLinus Torvalds case PROC_TID_LOGINUID: 18341da177e4SLinus Torvalds case PROC_TGID_LOGINUID: 18351da177e4SLinus Torvalds inode->i_fop = &proc_loginuid_operations; 18361da177e4SLinus Torvalds break; 18371da177e4SLinus Torvalds #endif 18381da177e4SLinus Torvalds default: 18391da177e4SLinus Torvalds printk("procfs: impossible type (%d)",p->type); 18401da177e4SLinus Torvalds iput(inode); 1841cd6a3ce9SEric W. Biederman error = ERR_PTR(-EINVAL); 1842cd6a3ce9SEric W. Biederman goto out; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds dentry->d_op = &pid_dentry_operations; 18451da177e4SLinus Torvalds d_add(dentry, inode); 1846cd6a3ce9SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1847cd6a3ce9SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1848cd6a3ce9SEric W. Biederman error = NULL; 18491da177e4SLinus Torvalds out: 185099f89551SEric W. Biederman put_task_struct(task); 185199f89551SEric W. Biederman out_no_task: 1852cd6a3ce9SEric W. Biederman return error; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 18561da177e4SLinus Torvalds return proc_pident_lookup(dir, dentry, tgid_base_stuff); 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 18601da177e4SLinus Torvalds return proc_pident_lookup(dir, dentry, tid_base_stuff); 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds static struct file_operations proc_tgid_base_operations = { 18641da177e4SLinus Torvalds .read = generic_read_dir, 18651da177e4SLinus Torvalds .readdir = proc_tgid_base_readdir, 18661da177e4SLinus Torvalds }; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds static struct file_operations proc_tid_base_operations = { 18691da177e4SLinus Torvalds .read = generic_read_dir, 18701da177e4SLinus Torvalds .readdir = proc_tid_base_readdir, 18711da177e4SLinus Torvalds }; 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds static struct inode_operations proc_tgid_base_inode_operations = { 18741da177e4SLinus Torvalds .lookup = proc_tgid_base_lookup, 187599f89551SEric W. Biederman .getattr = pid_getattr, 18766d76fa58SLinus Torvalds .setattr = proc_setattr, 18771da177e4SLinus Torvalds }; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds static struct inode_operations proc_tid_base_inode_operations = { 18801da177e4SLinus Torvalds .lookup = proc_tid_base_lookup, 188199f89551SEric W. Biederman .getattr = pid_getattr, 18826d76fa58SLinus Torvalds .setattr = proc_setattr, 18831da177e4SLinus Torvalds }; 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 18861da177e4SLinus Torvalds static int proc_tgid_attr_readdir(struct file * filp, 18871da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 18901da177e4SLinus Torvalds tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff)); 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds static int proc_tid_attr_readdir(struct file * filp, 18941da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18951da177e4SLinus Torvalds { 18961da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 18971da177e4SLinus Torvalds tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff)); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds static struct file_operations proc_tgid_attr_operations = { 19011da177e4SLinus Torvalds .read = generic_read_dir, 19021da177e4SLinus Torvalds .readdir = proc_tgid_attr_readdir, 19031da177e4SLinus Torvalds }; 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds static struct file_operations proc_tid_attr_operations = { 19061da177e4SLinus Torvalds .read = generic_read_dir, 19071da177e4SLinus Torvalds .readdir = proc_tid_attr_readdir, 19081da177e4SLinus Torvalds }; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds static struct dentry *proc_tgid_attr_lookup(struct inode *dir, 19111da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 19121da177e4SLinus Torvalds { 19131da177e4SLinus Torvalds return proc_pident_lookup(dir, dentry, tgid_attr_stuff); 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds static struct dentry *proc_tid_attr_lookup(struct inode *dir, 19171da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 19181da177e4SLinus Torvalds { 19191da177e4SLinus Torvalds return proc_pident_lookup(dir, dentry, tid_attr_stuff); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds static struct inode_operations proc_tgid_attr_inode_operations = { 19231da177e4SLinus Torvalds .lookup = proc_tgid_attr_lookup, 192499f89551SEric W. Biederman .getattr = pid_getattr, 19256d76fa58SLinus Torvalds .setattr = proc_setattr, 19261da177e4SLinus Torvalds }; 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds static struct inode_operations proc_tid_attr_inode_operations = { 19291da177e4SLinus Torvalds .lookup = proc_tid_attr_lookup, 193099f89551SEric W. Biederman .getattr = pid_getattr, 19316d76fa58SLinus Torvalds .setattr = proc_setattr, 19321da177e4SLinus Torvalds }; 19331da177e4SLinus Torvalds #endif 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* 19361da177e4SLinus Torvalds * /proc/self: 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 19391da177e4SLinus Torvalds int buflen) 19401da177e4SLinus Torvalds { 19418578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19421da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 19431da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 1946008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 19471da177e4SLinus Torvalds { 19488578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19491da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1950008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds static struct inode_operations proc_self_inode_operations = { 19541da177e4SLinus Torvalds .readlink = proc_self_readlink, 19551da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 19561da177e4SLinus Torvalds }; 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds /** 195948e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 19601da177e4SLinus Torvalds * 196148e6484dSEric W. Biederman * @task: task that should be flushed. 19621da177e4SLinus Torvalds * 196348e6484dSEric W. Biederman * Looks in the dcache for 196448e6484dSEric W. Biederman * /proc/@pid 196548e6484dSEric W. Biederman * /proc/@tgid/task/@pid 196648e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 196748e6484dSEric W. Biederman * from the dcache. 196848e6484dSEric W. Biederman * 196948e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 197048e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 197148e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 197248e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 197348e6484dSEric W. Biederman * dcache entries at process exit time. 197448e6484dSEric W. Biederman * 197548e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 197648e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 197748e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 19781da177e4SLinus Torvalds */ 197948e6484dSEric W. Biederman void proc_flush_task(struct task_struct *task) 19801da177e4SLinus Torvalds { 198148e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 19828578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 198348e6484dSEric W. Biederman struct qstr name; 19841da177e4SLinus Torvalds 198548e6484dSEric W. Biederman name.name = buf; 198648e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 198748e6484dSEric W. Biederman dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name); 198848e6484dSEric W. Biederman if (dentry) { 198948e6484dSEric W. Biederman shrink_dcache_parent(dentry); 199048e6484dSEric W. Biederman d_drop(dentry); 199148e6484dSEric W. Biederman dput(dentry); 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 199448e6484dSEric W. Biederman if (thread_group_leader(task)) 199548e6484dSEric W. Biederman goto out; 19961da177e4SLinus Torvalds 199748e6484dSEric W. Biederman name.name = buf; 199848e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->tgid); 199948e6484dSEric W. Biederman leader = d_hash_and_lookup(proc_mnt->mnt_root, &name); 200048e6484dSEric W. Biederman if (!leader) 200148e6484dSEric W. Biederman goto out; 200248e6484dSEric W. Biederman 200348e6484dSEric W. Biederman name.name = "task"; 200448e6484dSEric W. Biederman name.len = strlen(name.name); 200548e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 200648e6484dSEric W. Biederman if (!dir) 200748e6484dSEric W. Biederman goto out_put_leader; 200848e6484dSEric W. Biederman 200948e6484dSEric W. Biederman name.name = buf; 201048e6484dSEric W. Biederman name.len = snprintf(buf, sizeof(buf), "%d", task->pid); 201148e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 201248e6484dSEric W. Biederman if (dentry) { 201348e6484dSEric W. Biederman shrink_dcache_parent(dentry); 201448e6484dSEric W. Biederman d_drop(dentry); 201548e6484dSEric W. Biederman dput(dentry); 20161da177e4SLinus Torvalds } 201748e6484dSEric W. Biederman 201848e6484dSEric W. Biederman dput(dir); 201948e6484dSEric W. Biederman out_put_leader: 202048e6484dSEric W. Biederman dput(leader); 202148e6484dSEric W. Biederman out: 202248e6484dSEric W. Biederman return; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds /* SMP-safe */ 20261da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 20271da177e4SLinus Torvalds { 2028cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 20291da177e4SLinus Torvalds struct task_struct *task; 20301da177e4SLinus Torvalds struct inode *inode; 20311da177e4SLinus Torvalds struct proc_inode *ei; 20321da177e4SLinus Torvalds unsigned tgid; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) { 20351da177e4SLinus Torvalds inode = new_inode(dir->i_sb); 20361da177e4SLinus Torvalds if (!inode) 20371da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 20381da177e4SLinus Torvalds ei = PROC_I(inode); 20391da177e4SLinus Torvalds inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 20401da177e4SLinus Torvalds inode->i_ino = fake_ino(0, PROC_TGID_INO); 20411da177e4SLinus Torvalds ei->pde = NULL; 20421da177e4SLinus Torvalds inode->i_mode = S_IFLNK|S_IRWXUGO; 20431da177e4SLinus Torvalds inode->i_uid = inode->i_gid = 0; 20441da177e4SLinus Torvalds inode->i_size = 64; 20451da177e4SLinus Torvalds inode->i_op = &proc_self_inode_operations; 20461da177e4SLinus Torvalds d_add(dentry, inode); 20471da177e4SLinus Torvalds return NULL; 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds tgid = name_to_int(dentry); 20501da177e4SLinus Torvalds if (tgid == ~0U) 20511da177e4SLinus Torvalds goto out; 20521da177e4SLinus Torvalds 2053de758734SEric W. Biederman rcu_read_lock(); 20541da177e4SLinus Torvalds task = find_task_by_pid(tgid); 20551da177e4SLinus Torvalds if (task) 20561da177e4SLinus Torvalds get_task_struct(task); 2057de758734SEric W. Biederman rcu_read_unlock(); 20581da177e4SLinus Torvalds if (!task) 20591da177e4SLinus Torvalds goto out; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO); 2062cd6a3ce9SEric W. Biederman if (!inode) 2063cd6a3ce9SEric W. Biederman goto out_put_task; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 20661da177e4SLinus Torvalds inode->i_op = &proc_tgid_base_inode_operations; 20671da177e4SLinus Torvalds inode->i_fop = &proc_tgid_base_operations; 20681da177e4SLinus Torvalds inode->i_flags|=S_IMMUTABLE; 2069bcf88e11SDaniel Drake #ifdef CONFIG_SECURITY 2070bcf88e11SDaniel Drake inode->i_nlink = 5; 2071bcf88e11SDaniel Drake #else 2072bcf88e11SDaniel Drake inode->i_nlink = 4; 2073bcf88e11SDaniel Drake #endif 20741da177e4SLinus Torvalds 207548e6484dSEric W. Biederman dentry->d_op = &pid_dentry_operations; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds d_add(dentry, inode); 2078cd6a3ce9SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2079cd6a3ce9SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2080cd6a3ce9SEric W. Biederman result = NULL; 208148e6484dSEric W. Biederman 2082cd6a3ce9SEric W. Biederman out_put_task: 208348e6484dSEric W. Biederman put_task_struct(task); 20841da177e4SLinus Torvalds out: 2085cd6a3ce9SEric W. Biederman return result; 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds /* SMP-safe */ 20891da177e4SLinus Torvalds static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 20901da177e4SLinus Torvalds { 2091cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 20921da177e4SLinus Torvalds struct task_struct *task; 209399f89551SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 20941da177e4SLinus Torvalds struct inode *inode; 20951da177e4SLinus Torvalds unsigned tid; 20961da177e4SLinus Torvalds 209799f89551SEric W. Biederman if (!leader) 209899f89551SEric W. Biederman goto out_no_task; 209999f89551SEric W. Biederman 21001da177e4SLinus Torvalds tid = name_to_int(dentry); 21011da177e4SLinus Torvalds if (tid == ~0U) 21021da177e4SLinus Torvalds goto out; 21031da177e4SLinus Torvalds 2104de758734SEric W. Biederman rcu_read_lock(); 21051da177e4SLinus Torvalds task = find_task_by_pid(tid); 21061da177e4SLinus Torvalds if (task) 21071da177e4SLinus Torvalds get_task_struct(task); 2108de758734SEric W. Biederman rcu_read_unlock(); 21091da177e4SLinus Torvalds if (!task) 21101da177e4SLinus Torvalds goto out; 21111da177e4SLinus Torvalds if (leader->tgid != task->tgid) 21121da177e4SLinus Torvalds goto out_drop_task; 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO); 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds if (!inode) 21181da177e4SLinus Torvalds goto out_drop_task; 21191da177e4SLinus Torvalds inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 21201da177e4SLinus Torvalds inode->i_op = &proc_tid_base_inode_operations; 21211da177e4SLinus Torvalds inode->i_fop = &proc_tid_base_operations; 21221da177e4SLinus Torvalds inode->i_flags|=S_IMMUTABLE; 2123bcf88e11SDaniel Drake #ifdef CONFIG_SECURITY 2124bcf88e11SDaniel Drake inode->i_nlink = 4; 2125bcf88e11SDaniel Drake #else 2126bcf88e11SDaniel Drake inode->i_nlink = 3; 2127bcf88e11SDaniel Drake #endif 21281da177e4SLinus Torvalds 212948e6484dSEric W. Biederman dentry->d_op = &pid_dentry_operations; 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds d_add(dentry, inode); 2132cd6a3ce9SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2133cd6a3ce9SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2134cd6a3ce9SEric W. Biederman result = NULL; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds out_drop_task: 21371da177e4SLinus Torvalds put_task_struct(task); 21381da177e4SLinus Torvalds out: 213999f89551SEric W. Biederman put_task_struct(leader); 214099f89551SEric W. Biederman out_no_task: 2141cd6a3ce9SEric W. Biederman return result; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* 2145*0804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 21460bc58a91SEric W. Biederman * 21471da177e4SLinus Torvalds */ 2148*0804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 21491da177e4SLinus Torvalds { 2150*0804ef4bSEric W. Biederman struct task_struct *task; 2151*0804ef4bSEric W. Biederman struct pid *pid; 21521da177e4SLinus Torvalds 2153*0804ef4bSEric W. Biederman rcu_read_lock(); 2154*0804ef4bSEric W. Biederman retry: 2155*0804ef4bSEric W. Biederman task = NULL; 2156*0804ef4bSEric W. Biederman pid = find_ge_pid(tgid); 2157*0804ef4bSEric W. Biederman if (pid) { 2158*0804ef4bSEric W. Biederman tgid = pid->nr + 1; 2159*0804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 2160*0804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 2161*0804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 2162*0804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 2163*0804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 2164*0804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 2165*0804ef4bSEric W. Biederman * 2166*0804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 2167*0804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 2168*0804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 2169*0804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 2170*0804ef4bSEric W. Biederman * As we don't care in the case of readdir. 21710bc58a91SEric W. Biederman */ 2172*0804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 2173*0804ef4bSEric W. Biederman goto retry; 2174*0804ef4bSEric W. Biederman get_task_struct(task); 21751da177e4SLinus Torvalds } 2176454cc105SEric W. Biederman rcu_read_unlock(); 2177*0804ef4bSEric W. Biederman return task; 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 2180*0804ef4bSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */)) 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 21831da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 21841da177e4SLinus Torvalds { 21851da177e4SLinus Torvalds char buf[PROC_NUMBUF]; 21861da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 21870bc58a91SEric W. Biederman struct task_struct *task; 21880bc58a91SEric W. Biederman int tgid; 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds if (!nr) { 21911da177e4SLinus Torvalds ino_t ino = fake_ino(0,PROC_TGID_INO); 21921da177e4SLinus Torvalds if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0) 21931da177e4SLinus Torvalds return 0; 21941da177e4SLinus Torvalds filp->f_pos++; 21951da177e4SLinus Torvalds nr++; 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds 2198*0804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 2199*0804ef4bSEric W. Biederman for (task = next_tgid(tgid); 22000bc58a91SEric W. Biederman task; 2201*0804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 22020bc58a91SEric W. Biederman int len; 22030bc58a91SEric W. Biederman ino_t ino; 22040bc58a91SEric W. Biederman tgid = task->pid; 2205*0804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 22060bc58a91SEric W. Biederman len = snprintf(buf, sizeof(buf), "%d", tgid); 22070bc58a91SEric W. Biederman ino = fake_ino(tgid, PROC_TGID_INO); 22080bc58a91SEric W. Biederman if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) { 22090bc58a91SEric W. Biederman put_task_struct(task); 2210*0804ef4bSEric W. Biederman goto out; 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds } 2213*0804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 2214*0804ef4bSEric W. Biederman out: 22151da177e4SLinus Torvalds return 0; 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 22180bc58a91SEric W. Biederman /* 22190bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 22200bc58a91SEric W. Biederman * 22210bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 22220bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 22230bc58a91SEric W. Biederman * directory we have more work todo. 22240bc58a91SEric W. Biederman * 22250bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 22260bc58a91SEric W. Biederman * 22270bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 22280bc58a91SEric W. Biederman * threads past it. 22290bc58a91SEric W. Biederman */ 2230cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2231cc288738SEric W. Biederman int tid, int nr) 22320bc58a91SEric W. Biederman { 2233a872ff0cSOleg Nesterov struct task_struct *pos; 22340bc58a91SEric W. Biederman 2235cc288738SEric W. Biederman rcu_read_lock(); 22360bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 22370bc58a91SEric W. Biederman if (tid && (nr > 0)) { 22380bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2239a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2240a872ff0cSOleg Nesterov goto found; 22410bc58a91SEric W. Biederman } 22420bc58a91SEric W. Biederman 22430bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 22440bc58a91SEric W. Biederman pos = NULL; 2245a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2246a872ff0cSOleg Nesterov goto out; 2247a872ff0cSOleg Nesterov 2248a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2249a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2250a872ff0cSOleg Nesterov */ 2251a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2252a872ff0cSOleg Nesterov pos = next_thread(pos); 2253a872ff0cSOleg Nesterov if (pos == leader) { 2254a872ff0cSOleg Nesterov pos = NULL; 2255a872ff0cSOleg Nesterov goto out; 2256a872ff0cSOleg Nesterov } 2257a872ff0cSOleg Nesterov } 2258a872ff0cSOleg Nesterov found: 2259a872ff0cSOleg Nesterov get_task_struct(pos); 2260a872ff0cSOleg Nesterov out: 2261cc288738SEric W. Biederman rcu_read_unlock(); 22620bc58a91SEric W. Biederman return pos; 22630bc58a91SEric W. Biederman } 22640bc58a91SEric W. Biederman 22650bc58a91SEric W. Biederman /* 22660bc58a91SEric W. Biederman * Find the next thread in the thread list. 22670bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 22680bc58a91SEric W. Biederman * 22690bc58a91SEric W. Biederman * The reference to the input task_struct is released. 22700bc58a91SEric W. Biederman */ 22710bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 22720bc58a91SEric W. Biederman { 2273c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2274cc288738SEric W. Biederman rcu_read_lock(); 2275c1df7fb8SOleg Nesterov if (pid_alive(start)) { 22760bc58a91SEric W. Biederman pos = next_thread(start); 2277c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 22780bc58a91SEric W. Biederman pos = NULL; 2279c1df7fb8SOleg Nesterov else 2280c1df7fb8SOleg Nesterov get_task_struct(pos); 2281c1df7fb8SOleg Nesterov } 2282cc288738SEric W. Biederman rcu_read_unlock(); 22830bc58a91SEric W. Biederman put_task_struct(start); 22840bc58a91SEric W. Biederman return pos; 22850bc58a91SEric W. Biederman } 22860bc58a91SEric W. Biederman 22871da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 22881da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 22891da177e4SLinus Torvalds { 22901da177e4SLinus Torvalds char buf[PROC_NUMBUF]; 22911da177e4SLinus Torvalds struct dentry *dentry = filp->f_dentry; 22921da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 229399f89551SEric W. Biederman struct task_struct *leader = get_proc_task(inode); 22940bc58a91SEric W. Biederman struct task_struct *task; 22951da177e4SLinus Torvalds int retval = -ENOENT; 22961da177e4SLinus Torvalds ino_t ino; 22970bc58a91SEric W. Biederman int tid; 22981da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 22991da177e4SLinus Torvalds 230099f89551SEric W. Biederman if (!leader) 230199f89551SEric W. Biederman goto out_no_task; 23021da177e4SLinus Torvalds retval = 0; 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds switch (pos) { 23051da177e4SLinus Torvalds case 0: 23061da177e4SLinus Torvalds ino = inode->i_ino; 23071da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 23081da177e4SLinus Torvalds goto out; 23091da177e4SLinus Torvalds pos++; 23101da177e4SLinus Torvalds /* fall through */ 23111da177e4SLinus Torvalds case 1: 23121da177e4SLinus Torvalds ino = parent_ino(dentry); 23131da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 23141da177e4SLinus Torvalds goto out; 23151da177e4SLinus Torvalds pos++; 23161da177e4SLinus Torvalds /* fall through */ 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23190bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 23200bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 23210bc58a91SEric W. Biederman */ 23220bc58a91SEric W. Biederman tid = filp->f_version; 23230bc58a91SEric W. Biederman filp->f_version = 0; 23240bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 23250bc58a91SEric W. Biederman task; 23260bc58a91SEric W. Biederman task = next_tid(task), pos++) { 23270bc58a91SEric W. Biederman int len; 23280bc58a91SEric W. Biederman tid = task->pid; 23290bc58a91SEric W. Biederman len = snprintf(buf, sizeof(buf), "%d", tid); 23301da177e4SLinus Torvalds ino = fake_ino(tid, PROC_TID_INO); 23310bc58a91SEric W. Biederman if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) { 23320bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 23330bc58a91SEric W. Biederman * pid for the next readir call */ 23340bc58a91SEric W. Biederman filp->f_version = tid; 23350bc58a91SEric W. Biederman put_task_struct(task); 23361da177e4SLinus Torvalds break; 23370bc58a91SEric W. Biederman } 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds out: 23401da177e4SLinus Torvalds filp->f_pos = pos; 234199f89551SEric W. Biederman put_task_struct(leader); 234299f89551SEric W. Biederman out_no_task: 23431da177e4SLinus Torvalds return retval; 23441da177e4SLinus Torvalds } 23456e66b52bSEric W. Biederman 23466e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 23476e66b52bSEric W. Biederman { 23486e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 234999f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 23506e66b52bSEric W. Biederman generic_fillattr(inode, stat); 23516e66b52bSEric W. Biederman 235299f89551SEric W. Biederman if (p) { 235399f89551SEric W. Biederman rcu_read_lock(); 235499f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 235599f89551SEric W. Biederman rcu_read_unlock(); 235699f89551SEric W. Biederman put_task_struct(p); 23576e66b52bSEric W. Biederman } 23586e66b52bSEric W. Biederman 23596e66b52bSEric W. Biederman return 0; 23606e66b52bSEric W. Biederman } 2361