Skip to contentSkip to conversation

erofs: fix unbalanced buf->off handling in erofs_bread()

PATCH In review Binglei Wang 2 participants 2 messages Sep 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs: fix unbalanced buf->off handling in erofs_bread()

From: Binglei Wang <[email protected]>

erofs_bread() locates the target folio with

	index = (buf->off + offset) >> PAGE_SHIFT;

but computes the in-folio offset without taking buf->off into account:

	return buf->base + (offset & ~PAGE_MASK);

If buf->off is not page-aligned, the returned pointer misses the in-page component of buf->off, so callers end up fetching data from a wrong offset.

buf->off is set to sbi->dif0.fsoff in erofs_init_metabuf(), and fsoff can be specified via the "fsoffset=" mount option, which only requires block-size alignment. Therefore, on an image with a sub-page block size (e.g. 512 bytes), a non-page-aligned fsoff (e.g. 512) triggers the issue, since 512 is a multiple of the block size but not of PAGE_SIZE.

It can be reproduced by mounting an image that is placed at a non-page-aligned offset:

mkfs.erofs -b512 -zlz4hc sub.erofs src/
# prepend 512 bytes of padding to the image mount -t erofs -o loop,fsoffset=512 padded.erofs /mnt

which fails with

erofs (device loop0): cannot find valid erofs superblock

because the on-disk superblock (at offset 1024 within the image, i.e. 1536 within the padded file) is read from a wrong in-folio offset. With this fixed, the very same image mounts successfully and its file contents match those read from the unpadded image.

Fix it by including buf->off in the in-folio offset calculation, so that it is consistent with the folio index calculation.

Fixes c36ec00d7f67 ("erofs: add 'fsoffset' mount option to specify filesystem offset")
Cc <[email protected]> # 6.16+
---
 fs/erofs/data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/erofs/data.c +1 -1
index 0885b1f2fc92..be63b89f0862 100644
@@ -48,7 +48,7 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 		return NULL;
 	if (!buf->base)
 		buf->base = kmap_local_page(buf->page);
-	return buf->base + (offset & ~PAGE_MASK);
+	return buf->base + ((buf->off + offset) & ~PAGE_MASK);
 }
 
 int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb,

--
2.33.0

maintainer
#1 lore raw
Quoted 36 lines · Binglei Wang

From: Binglei Wang <[email protected]>

erofs_bread() locates the target folio with

	index = (buf->off + offset) >> PAGE_SHIFT;

but computes the in-folio offset without taking buf->off into account:

	return buf->base + (offset & ~PAGE_MASK);

If buf->off is not page-aligned, the returned pointer misses the in-page component of buf->off, so callers end up fetching data from a wrong offset.

buf->off is set to sbi->dif0.fsoff in erofs_init_metabuf(), and fsoff can be specified via the "fsoffset=" mount option, which only requires block-size alignment. Therefore, on an image with a sub-page block size (e.g. 512 bytes), a non-page-aligned fsoff (e.g. 512) triggers the issue, since 512 is a multiple of the block size but not of PAGE_SIZE.

It can be reproduced by mounting an image that is placed at a non-page-aligned offset:

mkfs.erofs -b512 -zlz4hc sub.erofs src/
# prepend 512 bytes of padding to the image mount -t erofs -o loop,fsoffset=512 padded.erofs /mnt

which fails with

erofs (device loop0): cannot find valid erofs superblock

because the on-disk superblock (at offset 1024 within the image, i.e. 1536 within the padded file) is read from a wrong in-folio offset. With this fixed, the very same image mounts successfully and its file contents match those read from the unpadded image.

Quoted 6 lines

Fix it by including buf->off in the in-folio offset calculation, so that it is consistent with the folio index calculation.

Fixes c36ec00d7f67 ("erofs: add 'fsoffset' mount option to specify filesystem offset")
Cc <[email protected]> # 6.16+

As I said, I aiready applied a version before, please don't send it again.

Thanks,
Gao Xiang