Skip to content

Commit b8c9f25

Browse files
joannekoonggregkh
authored andcommitted
iomap: adjust read range correctly for non-block-aligned positions
commit 7aa6bc3 upstream. iomap_adjust_read_range() assumes that the position and length passed in are block-aligned. This is not always the case however, as shown in the syzbot generated case for erofs. This causes too many bytes to be skipped for uptodate blocks, which results in returning the incorrect position and length to read in. If all the blocks are uptodate, this underflows length and returns a position beyond the folio. Fix the calculation to also take into account the block offset when calculating how many bytes can be skipped for uptodate blocks. Signed-off-by: Joanne Koong <joannelkoong@gmail.com> Tested-by: syzbot@syzkaller.appspotmail.com Reviewed-by: Brian Foster <bfoster@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Miguel Gazquez (Schneider Electric) <miguel.gazquez@bootlin.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ef50eaa commit b8c9f25

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

fs/iomap/buffered-io.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,24 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
107107
* to avoid reading in already uptodate ranges.
108108
*/
109109
if (iop) {
110-
unsigned int i;
110+
unsigned int i, blocks_skipped;
111111

112112
/* move forward for each leading block marked uptodate */
113-
for (i = first; i <= last; i++) {
113+
for (i = first; i <= last; i++)
114114
if (!test_bit(i, iop->uptodate))
115115
break;
116-
*pos += block_size;
117-
poff += block_size;
118-
plen -= block_size;
119-
first++;
116+
117+
blocks_skipped = i - first;
118+
if (blocks_skipped) {
119+
unsigned long block_offset = *pos & (block_size - 1);
120+
unsigned bytes_skipped =
121+
(blocks_skipped << block_bits) - block_offset;
122+
123+
*pos += bytes_skipped;
124+
poff += bytes_skipped;
125+
plen -= bytes_skipped;
120126
}
127+
first = i;
121128

122129
/* truncate len if we find any trailing uptodate block(s) */
123130
for ( ; i <= last; i++) {

0 commit comments

Comments
 (0)