Skip to contentSkip to conversation

erofs-utils: lib: validate inode offset bounds in erofs_read_inode_from_disk()

PATCH In review Utkal Singh 2 participants 4 messages Mar 4 – Mar 5 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: validate inode offset bounds in erofs_read_inode_from_disk()

A crafted EROFS image can contain an out-of-range node ID in directory entries or the superblock root_nid that causes erofs_iloc() to compute an inode offset beyond the image size. This leads to out-of-bounds reads in erofs_read_metabuf(), potentially crashing fsck.erofs, erofsfuse, or dump.erofs.

Add a bounds check at the start of erofs_read_inode_from_disk() to verify that the computed inode offset plus the minimum on-disk inode size (erofs_inode_compact) does not exceed the primary device size. Return -EFSCORRUPTED if the check fails.

This also deduplicates the erofs_iloc() computation which was previously evaluated twice.

---
 lib/namei.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
lib/namei.c +15 -2
index 896e348..8a83166 100644
@@ -25,8 +25,9 @@ static dev_t erofs_new_decode_dev(u32 dev)
 int erofs_read_inode_from_disk(struct erofs_inode *vi)
 {
 	struct erofs_sb_info *sbi = vi->sbi;
-	erofs_blk_t blkaddr = erofs_blknr(sbi, erofs_iloc(vi));
-	unsigned int ofs = erofs_blkoff(sbi, erofs_iloc(vi));
+	erofs_off_t inode_loc = erofs_iloc(vi);
+	erofs_blk_t blkaddr;
+	unsigned int ofs;
 	bool in_mbox = erofs_inode_in_metabox(vi);
 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
 	erofs_blk_t addrmask = BIT_ULL(48) - 1;
@@ -36,6 +37,18 @@ int erofs_read_inode_from_disk(struct erofs_inode *vi)
 	void *ptr;
 	int err = 0;
 
+	if (!in_mbox && sbi->primarydevice_blocks &&
+	    inode_loc + sizeof(struct erofs_inode_compact) >
+	    erofs_pos(sbi, sbi->primarydevice_blocks)) {
+		erofs_err("invalid nid %llu (inode location %llu beyond image size %llu)",
+			  vi->nid | 0ULL, inode_loc | 0ULL,
+			  erofs_pos(sbi, sbi->primarydevice_blocks) | 0ULL);
+		return -EFSCORRUPTED;
+	}
+
+	blkaddr = erofs_blknr(sbi, inode_loc);
+	ofs = erofs_blkoff(sbi, inode_loc);
+
 	ptr = erofs_read_metabuf(&buf, sbi, erofs_pos(sbi, blkaddr), in_mbox);
 	if (IS_ERR(ptr)) {
 		err = PTR_ERR(ptr);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Utkal Singh

A crafted EROFS image can contain an out-of-range node ID in directory entries or the superblock root_nid that causes erofs_iloc() to compute an inode offset beyond the image size. This leads to out-of-bounds reads in erofs_read_metabuf(), potentially crashing fsck.erofs, erofsfuse, or dump.erofs.

Do you have a reproducible image?

I think in that way, erofs_io_read or something should fail instead, we don't need such check against sbi->primarydevice_blocks.

#2 lore raw
Quoted 9 lines · Gao Xiang
Quoted 5 lines · Utkal Singh

A crafted EROFS image can contain an out-of-range node ID in directory entries or the superblock root_nid that causes erofs_iloc() to compute an inode offset beyond the image size. This leads to out-of-bounds reads in erofs_read_metabuf(), potentially crashing fsck.erofs, erofsfuse, or dump.erofs.

Quoted 5 lines

Do you have a reproducible image?

I think in that way, erofs_io_read or something should fail instead, we don't need such check against sbi->primarydevice_blocks.

It will return:
<E> erofs: erofs_read_inode_from_disk() Line[42] failed to get inode (nid: 249216) page, err -5 <E> erofs: erofsfsck_check_inode() Line[988] I/O error occurred when reading nid(249216)

I don't think such check is needed, blocks is mainly for statfs statistics, for dynamic generated EROFS, it could be 0 all the time.

#3 lore raw

Hi Gao,

Thanks for the detailed explanation. I understand now that erofs_io_read() already handles the out-of-bounds case and returns an appropriate error, so the additional check against primarydevice_blocks is unnecessary.

I also wasn't aware that primarydevice_blocks can legitimately be 0 for dynamically generated EROFS images, so my change would indeed break valid use cases.

Thanks again for reviewing and clarifying this. I’ll keep this in mind for future patches.

Best regards,
Utkal Singh

Quoted 28 lines · Gao Xiang
Quoted 9 lines · Gao Xiang
Quoted 5 lines · Utkal Singh

A crafted EROFS image can contain an out-of-range node ID in directory entries or the superblock root_nid that causes erofs_iloc() to compute an inode offset beyond the image size. This leads to out-of-bounds reads in erofs_read_metabuf(), potentially crashing fsck.erofs, erofsfuse, or dump.erofs.

Quoted 5 lines

Do you have a reproducible image?

I think in that way, erofs_io_read or something should fail instead, we don't need such check against sbi->primarydevice_blocks.

It will return:
<E> erofs: erofs_read_inode_from_disk() Line[42] failed to get inode (nid:
249216) page, err -5
<E> erofs: erofsfsck_check_inode() Line[988] I/O error occurred when reading nid(249216)

I don't think such check is needed, blocks is mainly for statfs statistics, for dynamic generated EROFS, it could be 0 all the time.