Skip to content

Commit aa6afca

Browse files
segoontorvalds
authored andcommitted
proc: fix races against execve() of /proc/PID/fd**
fd* files are restricted to the task's owner, and other users may not get direct access to them. But one may open any of these files and run any setuid program, keeping opened file descriptors. As there are permission checks on open(), but not on readdir() and read(), operations on the kept file descriptors will not be checked. It makes it possible to violate procfs permission model. Reading fdinfo/* may disclosure current fds' position and flags, reading directory contents of fdinfo/ and fd/ may disclosure the number of opened files by the target task. This information is not sensible per se, but it can reveal some private information (like length of a password stored in a file) under certain conditions. Used existing (un)lock_trace functions to check for ptrace_may_access(), but instead of using EPERM return code from it use EACCES to be consistent with existing proc_pid_follow_link()/proc_pid_readlink() return code. If they differ, attacker can guess what fds exist by analyzing stat() return code. Patched handlers: stat() for fd/*, stat() and read() for fdindo/*, readdir() and lookup() for fd/ and fdinfo/. Signed-off-by: Vasiliy Kulikov <segoon@openwall.com> Cc: Cyrill Gorcunov <gorcunov@gmail.com> Cc: <stable@kernel.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 887df07 commit aa6afca

1 file changed

Lines changed: 103 additions & 43 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 103 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,12 +1652,46 @@ static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int b
16521652
return error;
16531653
}
16541654

1655+
static int proc_pid_fd_link_getattr(struct vfsmount *mnt, struct dentry *dentry,
1656+
struct kstat *stat)
1657+
{
1658+
struct inode *inode = dentry->d_inode;
1659+
struct task_struct *task = get_proc_task(inode);
1660+
int rc;
1661+
1662+
if (task == NULL)
1663+
return -ESRCH;
1664+
1665+
rc = -EACCES;
1666+
if (lock_trace(task))
1667+
goto out_task;
1668+
1669+
generic_fillattr(inode, stat);
1670+
unlock_trace(task);
1671+
rc = 0;
1672+
out_task:
1673+
put_task_struct(task);
1674+
return rc;
1675+
}
1676+
16551677
static const struct inode_operations proc_pid_link_inode_operations = {
16561678
.readlink = proc_pid_readlink,
16571679
.follow_link = proc_pid_follow_link,
16581680
.setattr = proc_setattr,
16591681
};
16601682

1683+
static const struct inode_operations proc_fdinfo_link_inode_operations = {
1684+
.setattr = proc_setattr,
1685+
.getattr = proc_pid_fd_link_getattr,
1686+
};
1687+
1688+
static const struct inode_operations proc_fd_link_inode_operations = {
1689+
.readlink = proc_pid_readlink,
1690+
.follow_link = proc_pid_follow_link,
1691+
.setattr = proc_setattr,
1692+
.getattr = proc_pid_fd_link_getattr,
1693+
};
1694+
16611695

16621696
/* building an inode */
16631697

@@ -1889,49 +1923,61 @@ static unsigned name_to_int(struct dentry *dentry)
18891923

18901924
static int proc_fd_info(struct inode *inode, struct path *path, char *info)
18911925
{
1892-
struct task_struct *task = get_proc_task(inode);
1893-
struct files_struct *files = NULL;
1926+
struct task_struct *task;
1927+
struct files_struct *files;
18941928
struct file *file;
18951929
int fd = proc_fd(inode);
1930+
int rc;
18961931

1897-
if (task) {
1898-
files = get_files_struct(task);
1899-
put_task_struct(task);
1900-
}
1901-
if (files) {
1902-
/*
1903-
* We are not taking a ref to the file structure, so we must
1904-
* hold ->file_lock.
1905-
*/
1906-
spin_lock(&files->file_lock);
1907-
file = fcheck_files(files, fd);
1908-
if (file) {
1909-
unsigned int f_flags;
1910-
struct fdtable *fdt;
1911-
1912-
fdt = files_fdtable(files);
1913-
f_flags = file->f_flags & ~O_CLOEXEC;
1914-
if (FD_ISSET(fd, fdt->close_on_exec))
1915-
f_flags |= O_CLOEXEC;
1916-
1917-
if (path) {
1918-
*path = file->f_path;
1919-
path_get(&file->f_path);
1920-
}
1921-
if (info)
1922-
snprintf(info, PROC_FDINFO_MAX,
1923-
"pos:\t%lli\n"
1924-
"flags:\t0%o\n",
1925-
(long long) file->f_pos,
1926-
f_flags);
1927-
spin_unlock(&files->file_lock);
1928-
put_files_struct(files);
1929-
return 0;
1932+
task = get_proc_task(inode);
1933+
if (!task)
1934+
return -ENOENT;
1935+
1936+
rc = -EACCES;
1937+
if (lock_trace(task))
1938+
goto out_task;
1939+
1940+
rc = -ENOENT;
1941+
files = get_files_struct(task);
1942+
if (files == NULL)
1943+
goto out_unlock;
1944+
1945+
/*
1946+
* We are not taking a ref to the file structure, so we must
1947+
* hold ->file_lock.
1948+
*/
1949+
spin_lock(&files->file_lock);
1950+
file = fcheck_files(files, fd);
1951+
if (file) {
1952+
unsigned int f_flags;
1953+
struct fdtable *fdt;
1954+
1955+
fdt = files_fdtable(files);
1956+
f_flags = file->f_flags & ~O_CLOEXEC;
1957+
if (FD_ISSET(fd, fdt->close_on_exec))
1958+
f_flags |= O_CLOEXEC;
1959+
1960+
if (path) {
1961+
*path = file->f_path;
1962+
path_get(&file->f_path);
19301963
}
1931-
spin_unlock(&files->file_lock);
1932-
put_files_struct(files);
1933-
}
1934-
return -ENOENT;
1964+
if (info)
1965+
snprintf(info, PROC_FDINFO_MAX,
1966+
"pos:\t%lli\n"
1967+
"flags:\t0%o\n",
1968+
(long long) file->f_pos,
1969+
f_flags);
1970+
rc = 0;
1971+
} else
1972+
rc = -ENOENT;
1973+
spin_unlock(&files->file_lock);
1974+
put_files_struct(files);
1975+
1976+
out_unlock:
1977+
unlock_trace(task);
1978+
out_task:
1979+
put_task_struct(task);
1980+
return rc;
19351981
}
19361982

19371983
static int proc_fd_link(struct inode *inode, struct path *path)
@@ -2026,7 +2072,7 @@ static struct dentry *proc_fd_instantiate(struct inode *dir,
20262072
spin_unlock(&files->file_lock);
20272073
put_files_struct(files);
20282074

2029-
inode->i_op = &proc_pid_link_inode_operations;
2075+
inode->i_op = &proc_fd_link_inode_operations;
20302076
inode->i_size = 64;
20312077
ei->op.proc_get_link = proc_fd_link;
20322078
d_set_d_op(dentry, &tid_fd_dentry_operations);
@@ -2058,7 +2104,12 @@ static struct dentry *proc_lookupfd_common(struct inode *dir,
20582104
if (fd == ~0U)
20592105
goto out;
20602106

2107+
result = ERR_PTR(-EACCES);
2108+
if (lock_trace(task))
2109+
goto out;
2110+
20612111
result = instantiate(dir, dentry, task, &fd);
2112+
unlock_trace(task);
20622113
out:
20632114
put_task_struct(task);
20642115
out_no_task:
@@ -2078,23 +2129,28 @@ static int proc_readfd_common(struct file * filp, void * dirent,
20782129
retval = -ENOENT;
20792130
if (!p)
20802131
goto out_no_task;
2132+
2133+
retval = -EACCES;
2134+
if (lock_trace(p))
2135+
goto out;
2136+
20812137
retval = 0;
20822138

20832139
fd = filp->f_pos;
20842140
switch (fd) {
20852141
case 0:
20862142
if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2087-
goto out;
2143+
goto out_unlock;
20882144
filp->f_pos++;
20892145
case 1:
20902146
ino = parent_ino(dentry);
20912147
if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2092-
goto out;
2148+
goto out_unlock;
20932149
filp->f_pos++;
20942150
default:
20952151
files = get_files_struct(p);
20962152
if (!files)
2097-
goto out;
2153+
goto out_unlock;
20982154
rcu_read_lock();
20992155
for (fd = filp->f_pos-2;
21002156
fd < files_fdtable(files)->max_fds;
@@ -2118,6 +2174,9 @@ static int proc_readfd_common(struct file * filp, void * dirent,
21182174
rcu_read_unlock();
21192175
put_files_struct(files);
21202176
}
2177+
2178+
out_unlock:
2179+
unlock_trace(p);
21212180
out:
21222181
put_task_struct(p);
21232182
out_no_task:
@@ -2195,6 +2254,7 @@ static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
21952254
ei->fd = fd;
21962255
inode->i_mode = S_IFREG | S_IRUSR;
21972256
inode->i_fop = &proc_fdinfo_file_operations;
2257+
inode->i_op = &proc_fdinfo_link_inode_operations;
21982258
d_set_d_op(dentry, &tid_fd_dentry_operations);
21992259
d_add(dentry, inode);
22002260
/* Close the race of the process dying before we return the dentry */

0 commit comments

Comments
 (0)