Skip to content

Commit 381c043

Browse files
Christoph HellwigDarrick J. Wong
authored andcommitted
iomap: add a workaround for racy i_size updates on block devices
A szybot reproducer that does write I/O while truncating the size of a block device can end up in clean_bdev_aliases, which tries to clean the bdev aliases that it uses. This is because iomap_to_bh automatically sets the BH_New flag when outside of i_size. For block devices updates to i_size are racy and we can hit this case in a tiny race window, leading to the eventual clean_bdev_aliases call. Fix this by erroring out of > i_size I/O on block devices. Reported-by: syzbot+1fa947e7f09e136925b8@syzkaller.appspotmail.com Signed-off-by: Christoph Hellwig <hch@lst.de> Tested-by: syzbot+1fa947e7f09e136925b8@syzkaller.appspotmail.com Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Darrick J. Wong <djwong@kernel.org>
1 parent a5f31a5 commit 381c043

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

fs/buffer.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,8 +2058,17 @@ iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
20582058
fallthrough;
20592059
case IOMAP_MAPPED:
20602060
if ((iomap->flags & IOMAP_F_NEW) ||
2061-
offset >= i_size_read(inode))
2061+
offset >= i_size_read(inode)) {
2062+
/*
2063+
* This can happen if truncating the block device races
2064+
* with the check in the caller as i_size updates on
2065+
* block devices aren't synchronized by i_rwsem for
2066+
* block devices.
2067+
*/
2068+
if (S_ISBLK(inode->i_mode))
2069+
return -EIO;
20622070
set_buffer_new(bh);
2071+
}
20632072
bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
20642073
inode->i_blkbits;
20652074
set_buffer_mapped(bh);

0 commit comments

Comments
 (0)