Commit 4a2d046e authored by Gao Xiang's avatar Gao Xiang
Browse files

erofs: fix interlaced plain identification for encoded extents



Only plain data whose start position and on-disk physical length are
both aligned to the block size should be classified as interlaced
plain extents. Otherwise, it must be treated as shifted plain extents.

This issue was found by syzbot using a crafted compressed image
containing plain extents with unaligned physical lengths, which can
cause OOB read in z_erofs_transform_plain().

Reported-and-tested-by: default avatar <syzbot+d988dc155e740d76a331@syzkaller.appspotmail.com>
Closes: https://lore.kernel.org/r/699d5714.050a0220.cdd3c.03e7.GAE@google.com


Fixes: 1d191b4c ("erofs: implement encoded extent metadata")
Signed-off-by: default avatarGao Xiang <hsiangkao@linux.alibaba.com>
parent bf4fde7d
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -513,6 +513,7 @@ static int z_erofs_map_blocks_ext(struct inode *inode,
	unsigned int recsz = z_erofs_extent_recsize(vi->z_advise);
	erofs_off_t pos = round_up(Z_EROFS_MAP_HEADER_END(erofs_iloc(inode) +
				   vi->inode_isize + vi->xattr_isize), recsz);
	unsigned int bmask = sb->s_blocksize - 1;
	bool in_mbox = erofs_inode_in_metabox(inode);
	erofs_off_t lend = inode->i_size;
	erofs_off_t l, r, mid, pa, la, lstart;
@@ -596,17 +597,17 @@ static int z_erofs_map_blocks_ext(struct inode *inode,
			map->m_flags |= EROFS_MAP_MAPPED |
				EROFS_MAP_FULL_MAPPED | EROFS_MAP_ENCODED;
			fmt = map->m_plen >> Z_EROFS_EXTENT_PLEN_FMT_BIT;
			if (map->m_plen & Z_EROFS_EXTENT_PLEN_PARTIAL)
				map->m_flags |= EROFS_MAP_PARTIAL_REF;
			map->m_plen &= Z_EROFS_EXTENT_PLEN_MASK;
			if (fmt)
				map->m_algorithmformat = fmt - 1;
			else if (interlaced && !erofs_blkoff(sb, map->m_pa))
			else if (interlaced && !((map->m_pa | map->m_plen) & bmask))
				map->m_algorithmformat =
					Z_EROFS_COMPRESSION_INTERLACED;
			else
				map->m_algorithmformat =
					Z_EROFS_COMPRESSION_SHIFTED;
			if (map->m_plen & Z_EROFS_EXTENT_PLEN_PARTIAL)
				map->m_flags |= EROFS_MAP_PARTIAL_REF;
			map->m_plen &= Z_EROFS_EXTENT_PLEN_MASK;
		}
	}
	map->m_llen = lend - map->m_la;