Skip to content

Commit b58b1b8

Browse files
Brian FosterKent Overstreet
authored andcommitted
bcachefs: fix iov_iter count underflow on sub-block dio read
bch2_direct_IO_read() checks the request offset and size for sector alignment and then falls through to a couple calculations to shrink the size of the request based on the inode size. The problem is that these checks round up to the fs block size, which runs the risk of underflowing iter->count if the block size happens to be large enough. This is triggered by fstest generic/361 with a 4k block size, which subsequently leads to a crash. To avoid this crash, check that the shorten length doesn't exceed the overall length of the iter. Fixes: Signed-off-by: Brian Foster <[email protected]> Reviewed-by: Su Yue <[email protected]> Signed-off-by: Kent Overstreet <[email protected]>
1 parent 204f451 commit b58b1b8

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

fs/bcachefs/fs-io-direct.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
8888
return ret;
8989

9090
shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
91+
if (shorten >= iter->count)
92+
shorten = 0;
9193
iter->count -= shorten;
9294

9395
bio = bio_alloc_bioset(NULL,

0 commit comments

Comments
 (0)