Skip to content

Commit b0d8d22

Browse files
committed
vfs: fix subtle use-after-free of pipe_inode_info
The pipe code was trying (and failing) to be very careful about freeing the pipe info only after the last access, with a pattern like: spin_lock(&inode->i_lock); if (!--pipe->files) { inode->i_pipe = NULL; kill = 1; } spin_unlock(&inode->i_lock); __pipe_unlock(pipe); if (kill) free_pipe_info(pipe); where the final freeing is done last. HOWEVER. The above is actually broken, because while the freeing is done at the end, if we have two racing processes releasing the pipe inode info, the one that *doesn't* free it will decrement the ->files count, and unlock the inode i_lock, but then still use the "pipe_inode_info" afterwards when it does the "__pipe_unlock(pipe)". This is *very* hard to trigger in practice, since the race window is very small, and adding debug options seems to just hide it by slowing things down. Simon originally reported this way back in July as an Oops in kmem_cache_allocate due to a single bit corruption (due to the final "spin_unlock(pipe->mutex.wait_lock)" incrementing a field in a different allocation that had re-used the free'd pipe-info), it's taken this long to figure out. Since the 'pipe->files' accesses aren't even protected by the pipe lock (we very much use the inode lock for that), the simple solution is to just drop the pipe lock early. And since there were two users of this pattern, create a helper function for it. Introduced commit ba5bb14 ("pipe: take allocation and freeing of pipe_inode_info out of ->i_mutex"). Reported-by: Simon Kirby <sim@hostway.ca> Reported-by: Ian Applegate <ia@cloudflare.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Cc: stable@kernel.org # v3.10+ Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e84a2a4 commit b0d8d22

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

‎fs/pipe.c‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,25 @@ pipe_poll(struct file *filp, poll_table *wait)
726726
return mask;
727727
}
728728

729+
static void put_pipe_info(struct inode *inode, struct pipe_inode_info *pipe)
730+
{
731+
int kill = 0;
732+
733+
spin_lock(&inode->i_lock);
734+
if (!--pipe->files) {
735+
inode->i_pipe = NULL;
736+
kill = 1;
737+
}
738+
spin_unlock(&inode->i_lock);
739+
740+
if (kill)
741+
free_pipe_info(pipe);
742+
}
743+
729744
static int
730745
pipe_release(struct inode *inode, struct file *file)
731746
{
732-
struct pipe_inode_info *pipe = inode->i_pipe;
733-
int kill = 0;
747+
struct pipe_inode_info *pipe = file->private_data;
734748

735749
__pipe_lock(pipe);
736750
if (file->f_mode & FMODE_READ)
@@ -743,17 +757,9 @@ pipe_release(struct inode *inode, struct file *file)
743757
kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
744758
kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
745759
}
746-
spin_lock(&inode->i_lock);
747-
if (!--pipe->files) {
748-
inode->i_pipe = NULL;
749-
kill = 1;
750-
}
751-
spin_unlock(&inode->i_lock);
752760
__pipe_unlock(pipe);
753761

754-
if (kill)
755-
free_pipe_info(pipe);
756-
762+
put_pipe_info(inode, pipe);
757763
return 0;
758764
}
759765

@@ -1014,7 +1020,6 @@ static int fifo_open(struct inode *inode, struct file *filp)
10141020
{
10151021
struct pipe_inode_info *pipe;
10161022
bool is_pipe = inode->i_sb->s_magic == PIPEFS_MAGIC;
1017-
int kill = 0;
10181023
int ret;
10191024

10201025
filp->f_version = 0;
@@ -1130,15 +1135,9 @@ static int fifo_open(struct inode *inode, struct file *filp)
11301135
goto err;
11311136

11321137
err:
1133-
spin_lock(&inode->i_lock);
1134-
if (!--pipe->files) {
1135-
inode->i_pipe = NULL;
1136-
kill = 1;
1137-
}
1138-
spin_unlock(&inode->i_lock);
11391138
__pipe_unlock(pipe);
1140-
if (kill)
1141-
free_pipe_info(pipe);
1139+
1140+
put_pipe_info(inode, pipe);
11421141
return ret;
11431142
}
11441143

0 commit comments

Comments
 (0)