diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-02 23:25:04 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-02 23:25:04 -0400 |
commit | aab174f0df5d72d31caccf281af5f614fa254578 (patch) | |
tree | 2a172c5009c4ac8755e858593154c258ce7709a0 /fs/proc | |
parent | ca41cc96b2813221b05af57d0355157924de5a07 (diff) | |
parent | 2bd2c1941f141ad780135ccc1cd08ca71a24f10a (diff) |
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull vfs update from Al Viro:
- big one - consolidation of descriptor-related logics; almost all of
that is moved to fs/file.c
(BTW, I'm seriously tempted to rename the result to fd.c. As it is,
we have a situation when file_table.c is about handling of struct
file and file.c is about handling of descriptor tables; the reasons
are historical - file_table.c used to be about a static array of
struct file we used to have way back).
A lot of stray ends got cleaned up and converted to saner primitives,
disgusting mess in android/binder.c is still disgusting, but at least
doesn't poke so much in descriptor table guts anymore. A bunch of
relatively minor races got fixed in process, plus an ext4 struct file
leak.
- related thing - fget_light() partially unuglified; see fdget() in
there (and yes, it generates the code as good as we used to have).
- also related - bits of Cyrill's procfs stuff that got entangled into
that work; _not_ all of it, just the initial move to fs/proc/fd.c and
switch of fdinfo to seq_file.
- Alex's fs/coredump.c spiltoff - the same story, had been easier to
take that commit than mess with conflicts. The rest is a separate
pile, this was just a mechanical code movement.
- a few misc patches all over the place. Not all for this cycle,
there'll be more (and quite a few currently sit in akpm's tree)."
Fix up trivial conflicts in the android binder driver, and some fairly
simple conflicts due to two different changes to the sock_alloc_file()
interface ("take descriptor handling from sock_alloc_file() to callers"
vs "net: Providing protocol type via system.sockprotoname xattr of
/proc/PID/fd entries" adding a dentry name to the socket)
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: (72 commits)
MAX_LFS_FILESIZE should be a loff_t
compat: fs: Generic compat_sys_sendfile implementation
fs: push rcu_barrier() from deactivate_locked_super() to filesystems
btrfs: reada_extent doesn't need kref for refcount
coredump: move core dump functionality into its own file
coredump: prevent double-free on an error path in core dumper
usb/gadget: fix misannotations
fcntl: fix misannotations
ceph: don't abuse d_delete() on failure exits
hypfs: ->d_parent is never NULL or negative
vfs: delete surplus inode NULL check
switch simple cases of fget_light to fdget
new helpers: fdget()/fdput()
switch o2hb_region_dev_write() to fget_light()
proc_map_files_readdir(): don't bother with grabbing files
make get_file() return its argument
vhost_set_vring(): turn pollstart/pollstop into bool
switch prctl_set_mm_exe_file() to fget_light()
switch xfs_find_handle() to fget_light()
switch xfs_swapext() to fget_light()
...
Diffstat (limited to 'fs/proc')
-rw-r--r-- | fs/proc/Makefile | 2 | ||||
-rw-r--r-- | fs/proc/base.c | 417 | ||||
-rw-r--r-- | fs/proc/fd.c | 367 | ||||
-rw-r--r-- | fs/proc/fd.h | 14 | ||||
-rw-r--r-- | fs/proc/internal.h | 48 |
5 files changed, 441 insertions, 407 deletions
diff --git a/fs/proc/Makefile b/fs/proc/Makefile index c1c729335924..99349efbbc2b 100644 --- a/fs/proc/Makefile +++ b/fs/proc/Makefile | |||
@@ -8,7 +8,7 @@ proc-y := nommu.o task_nommu.o | |||
8 | proc-$(CONFIG_MMU) := mmu.o task_mmu.o | 8 | proc-$(CONFIG_MMU) := mmu.o task_mmu.o |
9 | 9 | ||
10 | proc-y += inode.o root.o base.o generic.o array.o \ | 10 | proc-y += inode.o root.o base.o generic.o array.o \ |
11 | proc_tty.o | 11 | proc_tty.o fd.o |
12 | proc-y += cmdline.o | 12 | proc-y += cmdline.o |
13 | proc-y += consoles.o | 13 | proc-y += consoles.o |
14 | proc-y += cpuinfo.o | 14 | proc-y += cpuinfo.o |
diff --git a/fs/proc/base.c b/fs/proc/base.c index acd1960c28a2..d295af993677 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c | |||
@@ -90,6 +90,7 @@ | |||
90 | #endif | 90 | #endif |
91 | #include <trace/events/oom.h> | 91 | #include <trace/events/oom.h> |
92 | #include "internal.h" | 92 | #include "internal.h" |
93 | #include "fd.h" | ||
93 | 94 | ||
94 | /* NOTE: | 95 | /* NOTE: |
95 | * Implementing inode permission operations in /proc is almost | 96 | * Implementing inode permission operations in /proc is almost |
@@ -136,8 +137,6 @@ struct pid_entry { | |||
136 | NULL, &proc_single_file_operations, \ | 137 | NULL, &proc_single_file_operations, \ |
137 | { .proc_show = show } ) | 138 | { .proc_show = show } ) |
138 | 139 | ||
139 | static int proc_fd_permission(struct inode *inode, int mask); | ||
140 | |||
141 | /* | 140 | /* |
142 | * Count the number of hardlinks for the pid_entry table, excluding the . | 141 | * Count the number of hardlinks for the pid_entry table, excluding the . |
143 | * and .. links. | 142 | * and .. links. |
@@ -1500,7 +1499,7 @@ out: | |||
1500 | return error; | 1499 | return error; |
1501 | } | 1500 | } |
1502 | 1501 | ||
1503 | static const struct inode_operations proc_pid_link_inode_operations = { | 1502 | const struct inode_operations proc_pid_link_inode_operations = { |
1504 | .readlink = proc_pid_readlink, | 1503 | .readlink = proc_pid_readlink, |
1505 | .follow_link = proc_pid_follow_link, | 1504 | .follow_link = proc_pid_follow_link, |
1506 | .setattr = proc_setattr, | 1505 | .setattr = proc_setattr, |
@@ -1509,21 +1508,6 @@ static const struct inode_operations proc_pid_link_inode_operations = { | |||
1509 | 1508 | ||
1510 | /* building an inode */ | 1509 | /* building an inode */ |
1511 | 1510 | ||
1512 | static int task_dumpable(struct task_struct *task) | ||
1513 | { | ||
1514 | int dumpable = 0; | ||
1515 | struct mm_struct *mm; | ||
1516 | |||
1517 | task_lock(task); | ||
1518 | mm = task->mm; | ||
1519 | if (mm) | ||
1520 | dumpable = get_dumpable(mm); | ||
1521 | task_unlock(task); | ||
1522 | if(dumpable == 1) | ||
1523 | return 1; | ||
1524 | return 0; | ||
1525 | } | ||
1526 | |||
1527 | struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) | 1511 | struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) |
1528 | { | 1512 | { |
1529 | struct inode * inode; | 1513 | struct inode * inode; |
@@ -1649,15 +1633,6 @@ int pid_revalidate(struct dentry *dentry, unsigned int flags) | |||
1649 | return 0; | 1633 | return 0; |
1650 | } | 1634 | } |
1651 | 1635 | ||
1652 | static int pid_delete_dentry(const struct dentry * dentry) | ||
1653 | { | ||
1654 | /* Is the task we represent dead? | ||
1655 | * If so, then don't put the dentry on the lru list, | ||
1656 | * kill it immediately. | ||
1657 | */ | ||
1658 | return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; | ||
1659 | } | ||
1660 | |||
1661 | const struct dentry_operations pid_dentry_operations = | 1636 | const struct dentry_operations pid_dentry_operations = |
1662 | { | 1637 | { |
1663 | .d_revalidate = pid_revalidate, | 1638 | .d_revalidate = pid_revalidate, |
@@ -1720,289 +1695,6 @@ end_instantiate: | |||
1720 | return filldir(dirent, name, len, filp->f_pos, ino, type); | 1695 | return filldir(dirent, name, len, filp->f_pos, ino, type); |
1721 | } | 1696 | } |
1722 | 1697 | ||
1723 | static unsigned name_to_int(struct dentry *dentry) | ||
1724 | { | ||
1725 | const char *name = dentry->d_name.name; | ||
1726 | int len = dentry->d_name.len; | ||
1727 | unsigned n = 0; | ||
1728 | |||
1729 | if (len > 1 && *name == '0') | ||
1730 | goto out; | ||
1731 | while (len-- > 0) { | ||
1732 | unsigned c = *name++ - '0'; | ||
1733 | if (c > 9) | ||
1734 | goto out; | ||
1735 | if (n >= (~0U-9)/10) | ||
1736 | goto out; | ||
1737 | n *= 10; | ||
1738 | n += c; | ||
1739 | } | ||
1740 | return n; | ||
1741 | out: | ||
1742 | return ~0U; | ||
1743 | } | ||
1744 | |||
1745 | #define PROC_FDINFO_MAX 64 | ||
1746 | |||
1747 | static int proc_fd_info(struct inode *inode, struct path *path, char *info) | ||
1748 | { | ||
1749 | struct task_struct *task = get_proc_task(inode); | ||
1750 | struct files_struct *files = NULL; | ||
1751 | struct file *file; | ||
1752 | int fd = proc_fd(inode); | ||
1753 | |||
1754 | if (task) { | ||
1755 | files = get_files_struct(task); | ||
1756 | put_task_struct(task); | ||
1757 | } | ||
1758 | if (files) { | ||
1759 | /* | ||
1760 | * We are not taking a ref to the file structure, so we must | ||
1761 | * hold ->file_lock. | ||
1762 | */ | ||
1763 | spin_lock(&files->file_lock); | ||
1764 | file = fcheck_files(files, fd); | ||
1765 | if (file) { | ||
1766 | unsigned int f_flags; | ||
1767 | struct fdtable *fdt; | ||
1768 | |||
1769 | fdt = files_fdtable(files); | ||
1770 | f_flags = file->f_flags & ~O_CLOEXEC; | ||
1771 | if (close_on_exec(fd, fdt)) | ||
1772 | f_flags |= O_CLOEXEC; | ||
1773 | |||
1774 | if (path) { | ||
1775 | *path = file->f_path; | ||
1776 | path_get(&file->f_path); | ||
1777 | } | ||
1778 | if (info) | ||
1779 | snprintf(info, PROC_FDINFO_MAX, | ||
1780 | "pos:\t%lli\n" | ||
1781 | "flags:\t0%o\n", | ||
1782 | (long long) file->f_pos, | ||
1783 | f_flags); | ||
1784 | spin_unlock(&files->file_lock); | ||
1785 | put_files_struct(files); | ||
1786 | return 0; | ||
1787 | } | ||
1788 | spin_unlock(&files->file_lock); | ||
1789 | put_files_struct(files); | ||
1790 | } | ||
1791 | return -ENOENT; | ||
1792 | } | ||
1793 | |||
1794 | static int proc_fd_link(struct dentry *dentry, struct path *path) | ||
1795 | { | ||
1796 | return proc_fd_info(dentry->d_inode, path, NULL); | ||
1797 | } | ||
1798 | |||
1799 | static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) | ||
1800 | { | ||
1801 | struct inode *inode; | ||
1802 | struct task_struct *task; | ||
1803 | int fd; | ||
1804 | struct files_struct *files; | ||
1805 | const struct cred *cred; | ||
1806 | |||
1807 | if (flags & LOOKUP_RCU) | ||
1808 | return -ECHILD; | ||
1809 | |||
1810 | inode = dentry->d_inode; | ||
1811 | task = get_proc_task(inode); | ||
1812 | fd = proc_fd(inode); | ||
1813 | |||
1814 | if (task) { | ||
1815 | files = get_files_struct(task); | ||
1816 | if (files) { | ||
1817 | struct file *file; | ||
1818 | rcu_read_lock(); | ||
1819 | file = fcheck_files(files, fd); | ||
1820 | if (file) { | ||
1821 | unsigned f_mode = file->f_mode; | ||
1822 | |||
1823 | rcu_read_unlock(); | ||
1824 | put_files_struct(files); | ||
1825 | |||
1826 | if (task_dumpable(task)) { | ||
1827 | rcu_read_lock(); | ||
1828 | cred = __task_cred(task); | ||
1829 | inode->i_uid = cred->euid; | ||
1830 | inode->i_gid = cred->egid; | ||
1831 | rcu_read_unlock(); | ||
1832 | } else { | ||
1833 | inode->i_uid = GLOBAL_ROOT_UID; | ||
1834 | inode->i_gid = GLOBAL_ROOT_GID; | ||
1835 | } | ||
1836 | |||
1837 | if (S_ISLNK(inode->i_mode)) { | ||
1838 | unsigned i_mode = S_IFLNK; | ||
1839 | if (f_mode & FMODE_READ) | ||
1840 | i_mode |= S_IRUSR | S_IXUSR; | ||
1841 | if (f_mode & FMODE_WRITE) | ||
1842 | i_mode |= S_IWUSR | S_IXUSR; | ||
1843 | inode->i_mode = i_mode; | ||
1844 | } | ||
1845 | |||
1846 | security_task_to_inode(task, inode); | ||
1847 | put_task_struct(task); | ||
1848 | return 1; | ||
1849 | } | ||
1850 | rcu_read_unlock(); | ||
1851 | put_files_struct(files); | ||
1852 | } | ||
1853 | put_task_struct(task); | ||
1854 | } | ||
1855 | d_drop(dentry); | ||
1856 | return 0; | ||
1857 | } | ||
1858 | |||
1859 | static const struct dentry_operations tid_fd_dentry_operations = | ||
1860 | { | ||
1861 | .d_revalidate = tid_fd_revalidate, | ||
1862 | .d_delete = pid_delete_dentry, | ||
1863 | }; | ||
1864 | |||
1865 | static struct dentry *proc_fd_instantiate(struct inode *dir, | ||
1866 | struct dentry *dentry, struct task_struct *task, const void *ptr) | ||
1867 | { | ||
1868 | unsigned fd = (unsigned long)ptr; | ||
1869 | struct inode *inode; | ||
1870 | struct proc_inode *ei; | ||
1871 | struct dentry *error = ERR_PTR(-ENOENT); | ||
1872 | |||
1873 | inode = proc_pid_make_inode(dir->i_sb, task); | ||
1874 | if (!inode) | ||
1875 | goto out; | ||
1876 | ei = PROC_I(inode); | ||
1877 | ei->fd = fd; | ||
1878 | |||
1879 | inode->i_mode = S_IFLNK; | ||
1880 | inode->i_op = &proc_pid_link_inode_operations; | ||
1881 | inode->i_size = 64; | ||
1882 | ei->op.proc_get_link = proc_fd_link; | ||
1883 | d_set_d_op(dentry, &tid_fd_dentry_operations); | ||
1884 | d_add(dentry, inode); | ||
1885 | /* Close the race of the process dying before we return the dentry */ | ||
1886 | if (tid_fd_revalidate(dentry, 0)) | ||
1887 | error = NULL; | ||
1888 | |||
1889 | out: | ||
1890 | return error; | ||
1891 | } | ||
1892 | |||
1893 | static struct dentry *proc_lookupfd_common(struct inode *dir, | ||
1894 | struct dentry *dentry, | ||
1895 | instantiate_t instantiate) | ||
1896 | { | ||
1897 | struct task_struct *task = get_proc_task(dir); | ||
1898 | unsigned fd = name_to_int(dentry); | ||
1899 | struct dentry *result = ERR_PTR(-ENOENT); | ||
1900 | |||
1901 | if (!task) | ||
1902 | goto out_no_task; | ||
1903 | if (fd == ~0U) | ||
1904 | goto out; | ||
1905 | |||
1906 | result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); | ||
1907 | out: | ||
1908 | put_task_struct(task); | ||
1909 | out_no_task: | ||
1910 | return result; | ||
1911 | } | ||
1912 | |||
1913 | static int proc_readfd_common(struct file * filp, void * dirent, | ||
1914 | filldir_t filldir, instantiate_t instantiate) | ||
1915 | { | ||
1916 | struct dentry *dentry = filp->f_path.dentry; | ||
1917 | struct inode *inode = dentry->d_inode; | ||
1918 | struct task_struct *p = get_proc_task(inode); | ||
1919 | unsigned int fd, ino; | ||
1920 | int retval; | ||
1921 | struct files_struct * files; | ||
1922 | |||
1923 | retval = -ENOENT; | ||
1924 | if (!p) | ||
1925 | goto out_no_task; | ||
1926 | retval = 0; | ||
1927 | |||
1928 | fd = filp->f_pos; | ||
1929 | switch (fd) { | ||
1930 | case 0: | ||
1931 | if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) | ||
1932 | goto out; | ||
1933 | filp->f_pos++; | ||
1934 | case 1: | ||
1935 | ino = parent_ino(dentry); | ||
1936 | if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) | ||
1937 | goto out; | ||
1938 | filp->f_pos++; | ||
1939 | default: | ||
1940 | files = get_files_struct(p); | ||
1941 | if (!files) | ||
1942 | goto out; | ||
1943 | rcu_read_lock(); | ||
1944 | for (fd = filp->f_pos-2; | ||
1945 | fd < files_fdtable(files)->max_fds; | ||
1946 | fd++, filp->f_pos++) { | ||
1947 | char name[PROC_NUMBUF]; | ||
1948 | int len; | ||
1949 | int rv; | ||
1950 | |||
1951 | if (!fcheck_files(files, fd)) | ||
1952 | continue; | ||
1953 | rcu_read_unlock(); | ||
1954 | |||
1955 | len = snprintf(name, sizeof(name), "%d", fd); | ||
1956 | rv = proc_fill_cache(filp, dirent, filldir, | ||
1957 | name, len, instantiate, p, | ||
1958 | (void *)(unsigned long)fd); | ||
1959 | if (rv < 0) | ||
1960 | goto out_fd_loop; | ||
1961 | rcu_read_lock(); | ||
1962 | } | ||
1963 | rcu_read_unlock(); | ||
1964 | out_fd_loop: | ||
1965 | put_files_struct(files); | ||
1966 | } | ||
1967 | out: | ||
1968 | put_task_struct(p); | ||
1969 | out_no_task: | ||
1970 | return retval; | ||
1971 | } | ||
1972 | |||
1973 | static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, | ||
1974 | unsigned int flags) | ||
1975 | { | ||
1976 | return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); | ||
1977 | } | ||
1978 | |||
1979 | static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) | ||
1980 | { | ||
1981 | return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); | ||
1982 | } | ||
1983 | |||
1984 | static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, | ||
1985 | size_t len, loff_t *ppos) | ||
1986 | { | ||
1987 | char tmp[PROC_FDINFO_MAX]; | ||
1988 | int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); | ||
1989 | if (!err) | ||
1990 | err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); | ||
1991 | return err; | ||
1992 | } | ||
1993 | |||
1994 | static const struct file_operations proc_fdinfo_file_operations = { | ||
1995 | .open = nonseekable_open, | ||
1996 | .read = proc_fdinfo_read, | ||
1997 | .llseek = no_llseek, | ||
1998 | }; | ||
1999 | |||
2000 | static const struct file_operations proc_fd_operations = { | ||
2001 | .read = generic_read_dir, | ||
2002 | .readdir = proc_readfd, | ||
2003 | .llseek = default_llseek, | ||
2004 | }; | ||
2005 | |||
2006 | #ifdef CONFIG_CHECKPOINT_RESTORE | 1698 | #ifdef CONFIG_CHECKPOINT_RESTORE |
2007 | 1699 | ||
2008 | /* | 1700 | /* |
@@ -2121,7 +1813,7 @@ out: | |||
2121 | } | 1813 | } |
2122 | 1814 | ||
2123 | struct map_files_info { | 1815 | struct map_files_info { |
2124 | struct file *file; | 1816 | fmode_t mode; |
2125 | unsigned long len; | 1817 | unsigned long len; |
2126 | unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ | 1818 | unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ |
2127 | }; | 1819 | }; |
@@ -2130,13 +1822,10 @@ static struct dentry * | |||
2130 | proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, | 1822 | proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, |
2131 | struct task_struct *task, const void *ptr) | 1823 | struct task_struct *task, const void *ptr) |
2132 | { | 1824 | { |
2133 | const struct file *file = ptr; | 1825 | fmode_t mode = (fmode_t)(unsigned long)ptr; |
2134 | struct proc_inode *ei; | 1826 | struct proc_inode *ei; |
2135 | struct inode *inode; | 1827 | struct inode *inode; |
2136 | 1828 | ||
2137 | if (!file) | ||
2138 | return ERR_PTR(-ENOENT); | ||
2139 | |||
2140 | inode = proc_pid_make_inode(dir->i_sb, task); | 1829 | inode = proc_pid_make_inode(dir->i_sb, task); |
2141 | if (!inode) | 1830 | if (!inode) |
2142 | return ERR_PTR(-ENOENT); | 1831 | return ERR_PTR(-ENOENT); |
@@ -2148,9 +1837,9 @@ proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, | |||
2148 | inode->i_size = 64; | 1837 | inode->i_size = 64; |
2149 | inode->i_mode = S_IFLNK; | 1838 | inode->i_mode = S_IFLNK; |
2150 | 1839 | ||
2151 | if (file->f_mode & FMODE_READ) | 1840 | if (mode & FMODE_READ) |
2152 | inode->i_mode |= S_IRUSR; | 1841 | inode->i_mode |= S_IRUSR; |
2153 | if (file->f_mode & FMODE_WRITE) | 1842 | if (mode & FMODE_WRITE) |
2154 | inode->i_mode |= S_IWUSR; | 1843 | inode->i_mode |= S_IWUSR; |
2155 | 1844 | ||
2156 | d_set_d_op(dentry, &tid_map_files_dentry_operations); | 1845 | d_set_d_op(dentry, &tid_map_files_dentry_operations); |
@@ -2194,7 +1883,8 @@ static struct dentry *proc_map_files_lookup(struct inode *dir, | |||
2194 | if (!vma) | 1883 | if (!vma) |
2195 | goto out_no_vma; | 1884 | goto out_no_vma; |
2196 | 1885 | ||
2197 | result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file); | 1886 | result = proc_map_files_instantiate(dir, dentry, task, |
1887 | (void *)(unsigned long)vma->vm_file->f_mode); | ||
2198 | 1888 | ||
2199 | out_no_vma: | 1889 | out_no_vma: |
2200 | up_read(&mm->mmap_sem); | 1890 | up_read(&mm->mmap_sem); |
@@ -2295,8 +1985,7 @@ proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir) | |||
2295 | if (++pos <= filp->f_pos) | 1985 | if (++pos <= filp->f_pos) |
2296 | continue; | 1986 | continue; |
2297 | 1987 | ||
2298 | get_file(vma->vm_file); | 1988 | info.mode = vma->vm_file->f_mode; |
2299 | info.file = vma->vm_file; | ||
2300 | info.len = snprintf(info.name, | 1989 | info.len = snprintf(info.name, |
2301 | sizeof(info.name), "%lx-%lx", | 1990 | sizeof(info.name), "%lx-%lx", |
2302 | vma->vm_start, vma->vm_end); | 1991 | vma->vm_start, vma->vm_end); |
@@ -2311,19 +2000,11 @@ proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir) | |||
2311 | ret = proc_fill_cache(filp, dirent, filldir, | 2000 | ret = proc_fill_cache(filp, dirent, filldir, |
2312 | p->name, p->len, | 2001 | p->name, p->len, |
2313 | proc_map_files_instantiate, | 2002 | proc_map_files_instantiate, |
2314 | task, p->file); | 2003 | task, |
2004 | (void *)(unsigned long)p->mode); | ||
2315 | if (ret) | 2005 | if (ret) |
2316 | break; | 2006 | break; |
2317 | filp->f_pos++; | 2007 | filp->f_pos++; |
2318 | fput(p->file); | ||
2319 | } | ||
2320 | for (; i < nr_files; i++) { | ||
2321 | /* | ||
2322 | * In case of error don't forget | ||
2323 | * to put rest of file refs. | ||
2324 | */ | ||
2325 | p = flex_array_get(fa, i); | ||
2326 | fput(p->file); | ||
2327 | } | 2008 | } |
2328 | if (fa) | 2009 | if (fa) |
2329 | flex_array_free(fa); | 2010 | flex_array_free(fa); |
@@ -2345,82 +2026,6 @@ static const struct file_operations proc_map_files_operations = { | |||
2345 | 2026 | ||
2346 | #endif /* CONFIG_CHECKPOINT_RESTORE */ | 2027 | #endif /* CONFIG_CHECKPOINT_RESTORE */ |
2347 | 2028 | ||
2348 | /* | ||
2349 | * /proc/pid/fd needs a special permission handler so that a process can still | ||
2350 | * access /proc/self/fd after it has executed a setuid(). | ||
2351 | */ | ||
2352 | static int proc_fd_permission(struct inode *inode, int mask) | ||
2353 | { | ||
2354 | int rv = generic_permission(inode, mask); | ||
2355 | if (rv == 0) | ||
2356 | return 0; | ||
2357 | if (task_pid(current) == proc_pid(inode)) | ||
2358 | rv = 0; | ||
2359 | return rv; | ||
2360 | } | ||
2361 | |||
2362 | /* | ||
2363 | * proc directories can do almost nothing.. | ||
2364 | */ | ||
2365 | static const struct inode_operations proc_fd_inode_operations = { | ||
2366 | .lookup = proc_lookupfd, | ||
2367 | .permission = proc_fd_permission, | ||
2368 | .setattr = proc_setattr, | ||
2369 | }; | ||
2370 | |||
2371 | static struct dentry *proc_fdinfo_instantiate(struct inode *dir, | ||
2372 | struct dentry *dentry, struct task_struct *task, const void *ptr) | ||
2373 | { | ||
2374 | unsigned fd = (unsigned long)ptr; | ||
2375 | struct inode *inode; | ||
2376 | struct proc_inode *ei; | ||
2377 | struct dentry *error = ERR_PTR(-ENOENT); | ||
2378 | |||
2379 | inode = proc_pid_make_inode(dir->i_sb, task); | ||
2380 | if (!inode) | ||
2381 | goto out; | ||
2382 | ei = PROC_I(inode); | ||
2383 | ei->fd = fd; | ||
2384 | inode->i_mode = S_IFREG | S_IRUSR; | ||
2385 | inode->i_fop = &proc_fdinfo_file_operations; | ||
2386 | d_set_d_op(dentry, &tid_fd_dentry_operations); | ||
2387 | d_add(dentry, inode); | ||
2388 | /* Close the race of the process dying before we return the dentry */ | ||
2389 | if (tid_fd_revalidate(dentry, 0)) | ||
2390 | error = NULL; | ||
2391 | |||
2392 | out: | ||
2393 | return error; | ||
2394 | } | ||
2395 | |||
2396 | static struct dentry *proc_lookupfdinfo(struct inode *dir, | ||
2397 | struct dentry *dentry, | ||
2398 | unsigned int flags) | ||
2399 | { | ||
2400 | return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); | ||
2401 | } | ||
2402 | |||
2403 | static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) | ||
2404 | { | ||
2405 | return proc_readfd_common(filp, dirent, filldir, | ||
2406 | proc_fdinfo_instantiate); | ||
2407 | } | ||
2408 | |||
2409 | static const struct file_operations proc_fdinfo_operations = { | ||
2410 | .read = generic_read_dir, | ||
2411 | .readdir = proc_readfdinfo, | ||
2412 | .llseek = default_llseek, | ||
2413 | }; | ||
2414 | |||
2415 | /* | ||
2416 | * proc directories can do almost nothing.. | ||
2417 | */ | ||
2418 | static const struct inode_operations proc_fdinfo_inode_operations = { | ||
2419 | .lookup = proc_lookupfdinfo, | ||
2420 | .setattr = proc_setattr, | ||
2421 | }; | ||
2422 | |||
2423 | |||
2424 | static struct dentry *proc_pident_instantiate(struct inode *dir, | 2029 | static struct dentry *proc_pident_instantiate(struct inode *dir, |
2425 | struct dentry *dentry, struct task_struct *task, const void *ptr) | 2030 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
2426 | { | 2031 | { |
diff --git a/fs/proc/fd.c b/fs/proc/fd.c new file mode 100644 index 000000000000..f28a875f8779 --- /dev/null +++ b/fs/proc/fd.c | |||
@@ -0,0 +1,367 @@ | |||
1 | #include <linux/sched.h> | ||
2 | #include <linux/errno.h> | ||
3 | #include <linux/dcache.h> | ||
4 | #include <linux/path.h> | ||
5 | #include <linux/fdtable.h> | ||
6 | #include <linux/namei.h> | ||
7 | #include <linux/pid.h> | ||
8 | #include <linux/security.h> | ||
9 | #include <linux/file.h> | ||
10 | #include <linux/seq_file.h> | ||
11 | |||
12 | #include <linux/proc_fs.h> | ||
13 | |||
14 | #include "internal.h" | ||
15 | #include "fd.h" | ||
16 | |||
17 | static int seq_show(struct seq_file *m, void *v) | ||
18 | { | ||
19 | struct files_struct *files = NULL; | ||
20 | int f_flags = 0, ret = -ENOENT; | ||
21 | struct file *file = NULL; | ||
22 | struct task_struct *task; | ||
23 | |||
24 | task = get_proc_task(m->private); | ||
25 | if (!task) | ||
26 | return -ENOENT; | ||
27 | |||
28 | files = get_files_struct(task); | ||
29 | put_task_struct(task); | ||
30 | |||
31 | if (files) { | ||
32 | int fd = proc_fd(m->private); | ||
33 | |||
34 | spin_lock(&files->file_lock); | ||
35 | file = fcheck_files(files, fd); | ||
36 | if (file) { | ||
37 | struct fdtable *fdt = files_fdtable(files); | ||
38 | |||
39 | f_flags = file->f_flags; | ||
40 | if (close_on_exec(fd, fdt)) | ||
41 | f_flags |= O_CLOEXEC; | ||
42 | |||
43 | get_file(file); | ||
44 | ret = 0; | ||
45 | } | ||
46 | spin_unlock(&files->file_lock); | ||
47 | put_files_struct(files); | ||
48 | } | ||
49 | |||
50 | if (!ret) { | ||
51 | seq_printf(m, "pos:\t%lli\nflags:\t0%o\n", | ||
52 | (long long)file->f_pos, f_flags); | ||
53 | fput(file); | ||
54 | } | ||
55 | |||
56 | return ret; | ||
57 | } | ||
58 | |||
59 | static int seq_fdinfo_open(struct inode *inode, struct file *file) | ||
60 | { | ||
61 | return single_open(file, seq_show, inode); | ||
62 | } | ||
63 | |||
64 | static const struct file_operations proc_fdinfo_file_operations = { | ||
65 | .open = seq_fdinfo_open, | ||
66 | .read = seq_read, | ||
67 | .llseek = seq_lseek, | ||
68 | .release = single_release, | ||
69 | }; | ||
70 | |||
71 | static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) | ||
72 | { | ||
73 | struct files_struct *files; | ||
74 | struct task_struct *task; | ||
75 | const struct cred *cred; | ||
76 | struct inode *inode; | ||
77 | int fd; | ||
78 | |||
79 | if (flags & LOOKUP_RCU) | ||
80 | return -ECHILD; | ||
81 | |||
82 | inode = dentry->d_inode; | ||
83 | task = get_proc_task(inode); | ||
84 | fd = proc_fd(inode); | ||
85 | |||
86 | if (task) { | ||
87 | files = get_files_struct(task); | ||
88 | if (files) { | ||
89 | struct file *file; | ||
90 | |||
91 | rcu_read_lock(); | ||
92 | file = fcheck_files(files, fd); | ||
93 | if (file) { | ||
94 | unsigned f_mode = file->f_mode; | ||
95 | |||
96 | rcu_read_unlock(); | ||
97 | put_files_struct(files); | ||
98 | |||
99 | if (task_dumpable(task)) { | ||
100 | rcu_read_lock(); | ||
101 | cred = __task_cred(task); | ||
102 | inode->i_uid = cred->euid; | ||
103 | inode->i_gid = cred->egid; | ||
104 | rcu_read_unlock(); | ||
105 | } else { | ||
106 | inode->i_uid = GLOBAL_ROOT_UID; | ||
107 | inode->i_gid = GLOBAL_ROOT_GID; | ||
108 | } | ||
109 | |||
110 | if (S_ISLNK(inode->i_mode)) { | ||
111 | unsigned i_mode = S_IFLNK; | ||
112 | if (f_mode & FMODE_READ) | ||
113 | i_mode |= S_IRUSR | S_IXUSR; | ||
114 | if (f_mode & FMODE_WRITE) | ||
115 | i_mode |= S_IWUSR | S_IXUSR; | ||
116 | inode->i_mode = i_mode; | ||
117 | } | ||
118 | |||
119 | security_task_to_inode(task, inode); | ||
120 | put_task_struct(task); | ||
121 | return 1; | ||
122 | } | ||
123 | rcu_read_unlock(); | ||
124 | put_files_struct(files); | ||
125 | } | ||
126 | put_task_struct(task); | ||
127 | } | ||
128 | |||
129 | d_drop(dentry); | ||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | static const struct dentry_operations tid_fd_dentry_operations = { | ||
134 | .d_revalidate = tid_fd_revalidate, | ||
135 | .d_delete = pid_delete_dentry, | ||
136 | }; | ||
137 | |||
138 | static int proc_fd_link(struct dentry *dentry, struct path *path) | ||
139 | { | ||
140 | struct files_struct *files = NULL; | ||
141 | struct task_struct *task; | ||
142 | int ret = -ENOENT; | ||
143 | |||
144 | task = get_proc_task(dentry->d_inode); | ||
145 | if (task) { | ||
146 | files = get_files_struct(task); | ||
147 | put_task_struct(task); | ||
148 | } | ||
149 | |||
150 | if (files) { | ||
151 | int fd = proc_fd(dentry->d_inode); | ||
152 | struct file *fd_file; | ||
153 | |||
154 | spin_lock(&files->file_lock); | ||
155 | fd_file = fcheck_files(files, fd); | ||
156 | if (fd_file) { | ||
157 | *path = fd_file->f_path; | ||
158 | path_get(&fd_file->f_path); | ||
159 | ret = 0; | ||
160 | } | ||
161 | spin_unlock(&files->file_lock); | ||
162 | put_files_struct(files); | ||
163 | } | ||
164 | |||
165 | return ret; | ||
166 | } | ||
167 | |||
168 | static struct dentry * | ||
169 | proc_fd_instantiate(struct inode *dir, struct dentry *dentry, | ||
170 | struct task_struct *task, const void *ptr) | ||
171 | { | ||
172 | struct dentry *error = ERR_PTR(-ENOENT); | ||
173 | unsigned fd = (unsigned long)ptr; | ||
174 | struct proc_inode *ei; | ||
175 | struct inode *inode; | ||
176 | |||
177 | inode = proc_pid_make_inode(dir->i_sb, task); | ||
178 | if (!inode) | ||
179 | goto out; | ||
180 | |||
181 | ei = PROC_I(inode); | ||
182 | ei->fd = fd; | ||
183 | |||
184 | inode->i_mode = S_IFLNK; | ||
185 | inode->i_op = &proc_pid_link_inode_operations; | ||
186 | inode->i_size = 64; | ||
187 | |||
188 | ei->op.proc_get_link = proc_fd_link; | ||
189 | |||
190 | d_set_d_op(dentry, &tid_fd_dentry_operations); | ||
191 | d_add(dentry, inode); | ||
192 | |||
193 | /* Close the race of the process dying before we return the dentry */ | ||
194 | if (tid_fd_revalidate(dentry, 0)) | ||
195 | error = NULL; | ||
196 | out: | ||
197 | return error; | ||
198 | } | ||
199 | |||
200 | static struct dentry *proc_lookupfd_common(struct inode *dir, | ||
201 | struct dentry *dentry, | ||
202 | instantiate_t instantiate) | ||
203 | { | ||
204 | struct task_struct *task = get_proc_task(dir); | ||
205 | struct dentry *result = ERR_PTR(-ENOENT); | ||
206 | unsigned fd = name_to_int(dentry); | ||
207 | |||
208 | if (!task) | ||
209 | goto out_no_task; | ||
210 | if (fd == ~0U) | ||
211 | goto out; | ||
212 | |||
213 | result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); | ||
214 | out: | ||
215 | put_task_struct(task); | ||
216 | out_no_task: | ||
217 | return result; | ||
218 | } | ||
219 | |||
220 | static int proc_readfd_common(struct file * filp, void * dirent, | ||
221 | filldir_t filldir, instantiate_t instantiate) | ||
222 | { | ||
223 | struct dentry *dentry = filp->f_path.dentry; | ||
224 | struct inode *inode = dentry->d_inode; | ||
225 | struct task_struct *p = get_proc_task(inode); | ||
226 | struct files_struct *files; | ||
227 | unsigned int fd, ino; | ||
228 | int retval; | ||
229 | |||
230 | retval = -ENOENT; | ||
231 | if (!p) | ||
232 | goto out_no_task; | ||
233 | retval = 0; | ||
234 | |||
235 | fd = filp->f_pos; | ||
236 | switch (fd) { | ||
237 | case 0: | ||
238 | if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) | ||
239 | goto out; | ||
240 | filp->f_pos++; | ||
241 | case 1: | ||
242 | ino = parent_ino(dentry); | ||
243 | if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) | ||
244 | goto out; | ||
245 | filp->f_pos++; | ||
246 | default: | ||
247 | files = get_files_struct(p); | ||
248 | if (!files) | ||
249 | goto out; | ||
250 | rcu_read_lock(); | ||
251 | for (fd = filp->f_pos - 2; | ||
252 | fd < files_fdtable(files)->max_fds; | ||
253 | fd++, filp->f_pos++) { | ||
254 | char name[PROC_NUMBUF]; | ||
255 | int len; | ||
256 | int rv; | ||
257 | |||
258 | if (!fcheck_files(files, fd)) | ||
259 | continue; | ||
260 | rcu_read_unlock(); | ||
261 | |||
262 | len = snprintf(name, sizeof(name), "%d", fd); | ||
263 | rv = proc_fill_cache(filp, dirent, filldir, | ||
264 | name, len, instantiate, p, | ||
265 | (void *)(unsigned long)fd); | ||
266 | if (rv < 0) | ||
267 | goto out_fd_loop; | ||
268 | rcu_read_lock(); | ||
269 | } | ||
270 | rcu_read_unlock(); | ||
271 | out_fd_loop: | ||
272 | put_files_struct(files); | ||
273 | } | ||
274 | out: | ||
275 | put_task_struct(p); | ||
276 | out_no_task: | ||
277 | return retval; | ||
278 | } | ||
279 | |||
280 | static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) | ||
281 | { | ||
282 | return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); | ||
283 | } | ||
284 | |||
285 | const struct file_operations proc_fd_operations = { | ||
286 | .read = generic_read_dir, | ||
287 | .readdir = proc_readfd, | ||
288 | .llseek = default_llseek, | ||
289 | }; | ||
290 | |||
291 | static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, | ||
292 | unsigned int flags) | ||
293 | { | ||
294 | return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); | ||
295 | } | ||
296 | |||
297 | /* | ||
298 | * /proc/pid/fd needs a special permission handler so that a process can still | ||
299 | * access /proc/self/fd after it has executed a setuid(). | ||
300 | */ | ||
301 | int proc_fd_permission(struct inode *inode, int mask) | ||
302 | { | ||
303 | int rv = generic_permission(inode, mask); | ||
304 | if (rv == 0) | ||
305 | return 0; | ||
306 | if (task_pid(current) == proc_pid(inode)) | ||
307 | rv = 0; | ||
308 | return rv; | ||
309 | } | ||
310 | |||
311 | const struct inode_operations proc_fd_inode_operations = { | ||
312 | .lookup = proc_lookupfd, | ||
313 | .permission = proc_fd_permission, | ||
314 | .setattr = proc_setattr, | ||
315 | }; | ||
316 | |||
317 | static struct dentry * | ||
318 | proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry, | ||
319 | struct task_struct *task, const void *ptr) | ||
320 | { | ||
321 | struct dentry *error = ERR_PTR(-ENOENT); | ||
322 | unsigned fd = (unsigned long)ptr; | ||
323 | struct proc_inode *ei; | ||
324 | struct inode *inode; | ||
325 | |||
326 | inode = proc_pid_make_inode(dir->i_sb, task); | ||
327 | if (!inode) | ||
328 | goto out; | ||
329 | |||
330 | ei = PROC_I(inode); | ||
331 | ei->fd = fd; | ||
332 | |||
333 | inode->i_mode = S_IFREG | S_IRUSR; | ||
334 | inode->i_fop = &proc_fdinfo_file_operations; | ||
335 | |||
336 | d_set_d_op(dentry, &tid_fd_dentry_operations); | ||
337 | d_add(dentry, inode); | ||
338 | |||
339 | /* Close the race of the process dying before we return the dentry */ | ||
340 | if (tid_fd_revalidate(dentry, 0)) | ||
341 | error = NULL; | ||
342 | out: | ||
343 | return error; | ||
344 | } | ||
345 | |||
346 | static struct dentry * | ||
347 | proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) | ||
348 | { | ||
349 | return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); | ||
350 | } | ||
351 | |||
352 | static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) | ||
353 | { | ||
354 | return proc_readfd_common(filp, dirent, filldir, | ||
355 | proc_fdinfo_instantiate); | ||
356 | } | ||
357 | |||
358 | const struct inode_operations proc_fdinfo_inode_operations = { | ||
359 | .lookup = proc_lookupfdinfo, | ||
360 | .setattr = proc_setattr, | ||
361 | }; | ||
362 | |||
363 | const struct file_operations proc_fdinfo_operations = { | ||
364 | .read = generic_read_dir, | ||
365 | .readdir = proc_readfdinfo, | ||
366 | .llseek = default_llseek, | ||
367 | }; | ||
diff --git a/fs/proc/fd.h b/fs/proc/fd.h new file mode 100644 index 000000000000..cbb1d47deda8 --- /dev/null +++ b/fs/proc/fd.h | |||
@@ -0,0 +1,14 @@ | |||
1 | #ifndef __PROCFS_FD_H__ | ||
2 | #define __PROCFS_FD_H__ | ||
3 | |||
4 | #include <linux/fs.h> | ||
5 | |||
6 | extern const struct file_operations proc_fd_operations; | ||
7 | extern const struct inode_operations proc_fd_inode_operations; | ||
8 | |||
9 | extern const struct file_operations proc_fdinfo_operations; | ||
10 | extern const struct inode_operations proc_fdinfo_inode_operations; | ||
11 | |||
12 | extern int proc_fd_permission(struct inode *inode, int mask); | ||
13 | |||
14 | #endif /* __PROCFS_FD_H__ */ | ||
diff --git a/fs/proc/internal.h b/fs/proc/internal.h index e1167a1c9126..67925a7bd8cb 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h | |||
@@ -9,6 +9,7 @@ | |||
9 | * 2 of the License, or (at your option) any later version. | 9 | * 2 of the License, or (at your option) any later version. |
10 | */ | 10 | */ |
11 | 11 | ||
12 | #include <linux/sched.h> | ||
12 | #include <linux/proc_fs.h> | 13 | #include <linux/proc_fs.h> |
13 | struct ctl_table_header; | 14 | struct ctl_table_header; |
14 | 15 | ||
@@ -65,6 +66,7 @@ extern const struct file_operations proc_clear_refs_operations; | |||
65 | extern const struct file_operations proc_pagemap_operations; | 66 | extern const struct file_operations proc_pagemap_operations; |
66 | extern const struct file_operations proc_net_operations; | 67 | extern const struct file_operations proc_net_operations; |
67 | extern const struct inode_operations proc_net_inode_operations; | 68 | extern const struct inode_operations proc_net_inode_operations; |
69 | extern const struct inode_operations proc_pid_link_inode_operations; | ||
68 | 70 | ||
69 | struct proc_maps_private { | 71 | struct proc_maps_private { |
70 | struct pid *pid; | 72 | struct pid *pid; |
@@ -91,6 +93,52 @@ static inline int proc_fd(struct inode *inode) | |||
91 | return PROC_I(inode)->fd; | 93 | return PROC_I(inode)->fd; |
92 | } | 94 | } |
93 | 95 | ||
96 | static inline int task_dumpable(struct task_struct *task) | ||
97 | { | ||
98 | int dumpable = 0; | ||
99 | struct mm_struct *mm; | ||
100 | |||
101 | task_lock(task); | ||
102 | mm = task->mm; | ||
103 | if (mm) | ||
104 | dumpable = get_dumpable(mm); | ||
105 | task_unlock(task); | ||
106 | if(dumpable == 1) | ||
107 | return 1; | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | static inline int pid_delete_dentry(const struct dentry * dentry) | ||
112 | { | ||
113 | /* Is the task we represent dead? | ||
114 | * If so, then don't put the dentry on the lru list, | ||
115 | * kill it immediately. | ||
116 | */ | ||
117 | return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; | ||
118 | } | ||
119 | |||
120 | static inline unsigned name_to_int(struct dentry *dentry) | ||
121 | { | ||
122 | const char *name = dentry->d_name.name; | ||
123 | int len = dentry->d_name.len; | ||
124 | unsigned n = 0; | ||
125 | |||
126 | if (len > 1 && *name == '0') | ||
127 | goto out; | ||
128 | while (len-- > 0) { | ||
129 | unsigned c = *name++ - '0'; | ||
130 | if (c > 9) | ||
131 | goto out; | ||
132 | if (n >= (~0U-9)/10) | ||
133 | goto out; | ||
134 | n *= 10; | ||
135 | n += c; | ||
136 | } | ||
137 | return n; | ||
138 | out: | ||
139 | return ~0U; | ||
140 | } | ||
141 | |||
94 | struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *ino, | 142 | struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *ino, |
95 | struct dentry *dentry); | 143 | struct dentry *dentry); |
96 | int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, | 144 | int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, |