Skip to content

Commit 97bf5a5

Browse files
Liu Bokdave
authored andcommitted
Btrfs: fix segmentation fault when doing dio read
Commit 2dabb32 ("Btrfs: Direct I/O read: Work on sectorsized blocks") introduced this bug during iterating bio pages in dio read's endio hook, and it could end up with segment fault of the dio reading task. So the reason is 'if (nr_sectors--)', and it makes the code assume that there is one more block in the same page, so page offset is increased and the bio which is created to repair the bad block then has an incorrect bvec.bv_offset, and a later access of the page content would throw a segmentation fault. This also adds ASSERT to check page offset against page size. Signed-off-by: Liu Bo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 2e949b0 commit 97bf5a5

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

fs/btrfs/inode.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7972,8 +7972,10 @@ static int __btrfs_correct_data_nocsum(struct inode *inode,
79727972

79737973
start += sectorsize;
79747974

7975-
if (nr_sectors--) {
7975+
nr_sectors--;
7976+
if (nr_sectors) {
79767977
pgoff += sectorsize;
7978+
ASSERT(pgoff < PAGE_SIZE);
79777979
goto next_block_or_try_again;
79787980
}
79797981
}
@@ -8074,8 +8076,10 @@ static int __btrfs_subio_endio_read(struct inode *inode,
80748076

80758077
ASSERT(nr_sectors);
80768078

8077-
if (--nr_sectors) {
8079+
nr_sectors--;
8080+
if (nr_sectors) {
80788081
pgoff += sectorsize;
8082+
ASSERT(pgoff < PAGE_SIZE);
80798083
goto next_block;
80808084
}
80818085
}

0 commit comments

Comments
 (0)