aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc/base.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-10-02 23:25:04 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-02 23:25:04 -0400
commitaab174f0df5d72d31caccf281af5f614fa254578 (patch)
tree2a172c5009c4ac8755e858593154c258ce7709a0 /fs/proc/base.c
parentca41cc96b2813221b05af57d0355157924de5a07 (diff)
parent2bd2c1941f141ad780135ccc1cd08ca71a24f10a (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/base.c')
-rw-r--r--fs/proc/base.c417
1 files changed, 11 insertions, 406 deletions
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
139static 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
1503static const struct inode_operations proc_pid_link_inode_operations = { 1502const 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
1512static 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
1527struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 1511struct 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
1652static 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
1661const struct dentry_operations pid_dentry_operations = 1636const 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
1723static 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;
1741out:
1742 return ~0U;
1743}
1744
1745#define PROC_FDINFO_MAX 64
1746
1747static 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
1794static int proc_fd_link(struct dentry *dentry, struct path *path)
1795{
1796 return proc_fd_info(dentry->d_inode, path, NULL);
1797}
1798
1799static 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
1859static const struct dentry_operations tid_fd_dentry_operations =
1860{
1861 .d_revalidate = tid_fd_revalidate,
1862 .d_delete = pid_delete_dentry,
1863};
1864
1865static 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
1893static 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);
1907out:
1908 put_task_struct(task);
1909out_no_task:
1910 return result;
1911}
1912
1913static 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();
1964out_fd_loop:
1965 put_files_struct(files);
1966 }
1967out:
1968 put_task_struct(p);
1969out_no_task:
1970 return retval;
1971}
1972
1973static 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
1979static 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
1984static 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
1994static const struct file_operations proc_fdinfo_file_operations = {
1995 .open = nonseekable_open,
1996 .read = proc_fdinfo_read,
1997 .llseek = no_llseek,
1998};
1999
2000static 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
2123struct map_files_info { 1815struct 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 *
2130proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 1822proc_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
2199out_no_vma: 1889out_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 */
2352static 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 */
2365static const struct inode_operations proc_fd_inode_operations = {
2366 .lookup = proc_lookupfd,
2367 .permission = proc_fd_permission,
2368 .setattr = proc_setattr,
2369};
2370
2371static 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
2396static 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
2403static 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
2409static 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 */
2418static const struct inode_operations proc_fdinfo_inode_operations = {
2419 .lookup = proc_lookupfdinfo,
2420 .setattr = proc_setattr,
2421};
2422
2423
2424static struct dentry *proc_pident_instantiate(struct inode *dir, 2029static 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{