hfsplus_read_wrapper() looks for the volume header by rereading at part_start until it finds an HFS+ signature. For an HFS wrapper, it moves part_start to the embedded volume, and for anything else, it looks for an Apple partition map and moves part_start to the found partition. Both offsets come from disk. If such an offset is zero, part_start does not move, the same wrapper MDB or partition map is found again, and the mount loops forever. This was reported for a wrapper with drAlBlSt = 0 and the embedded extent start = 0, and a partition map entry with pmPyPartStart = 0 loops the same way. There is no iteration limit and no signal check, so the mount(2) caller cannot be killed. The on-disk layout allows only one order: an optional Apple partition map at the start of the device, whose partition contains the volume, then an optional HFS wrapper, whose embedded volume is the HFS+ volume. Follow this order step by step instead of rereading in a loop: look for a partition map only if no volume is found at the start of the device, require the found partition to contain an HFS+ volume or a wrapper, and require the volume embedded in a wrapper to be the HFS+ volume. As there is no way back, the mount cannot loop, whatever the metadata are. Reported-by: Aymen T Link: https://lore.kernel.org/linux-fsdevel/45bfa4d7-e9e2-45f2-9e12-a794defae64a@gmail.com/ Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Viacheslav Dubeyko Cc: stable@vger.kernel.org cc: John Paul Adrian Glaubitz cc: Yangtao Li cc: linux-fsdevel@vger.kernel.org --- fs/hfsplus/wrapper.c | 80 +++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/fs/hfsplus/wrapper.c b/fs/hfsplus/wrapper.c index 30cf4fe78b3d..33dcdcbe5761 100644 --- a/fs/hfsplus/wrapper.c +++ b/fs/hfsplus/wrapper.c @@ -129,6 +129,22 @@ static int hfsplus_get_last_session(struct super_block *sb, return 0; } +static int hfsplus_read_vhdr(struct super_block *sb, sector_t part_start) +{ + struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); + + return hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, + sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, + REQ_OP_READ); +} + +static bool is_hfsplus_volume(__be16 sig) +{ + return sig == cpu_to_be16(HFSPLUS_VOLHEAD_SIG) || + sig == cpu_to_be16(HFSPLUS_VOLHEAD_SIGX) || + sig == cpu_to_be16(HFSP_WRAP_MAGIC); +} + /* Find the volume header and fill in some minimum bits in superblock */ /* Takes in super block, returns true if good data read */ int hfsplus_read_wrapper(struct super_block *sb) @@ -157,39 +173,63 @@ int hfsplus_read_wrapper(struct super_block *sb) if (!sbi->s_backup_vhdr_buf) goto out_free_vhdr; -reread: - error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, - sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, - REQ_OP_READ); + /* + * The volume header is in sector HFSPLUS_VOLHEAD_SECTOR of the + * volume. The volume can be in a partition of an Apple partition + * map, and it can be embedded in an HFS wrapper, whose MDB is in the + * same sector. So, the on-disk layout is: + * + * [partition map] -> [HFS wrapper] -> HFS+ volume header + * + * Follow it in this order, each step at most once. + */ + error = hfsplus_read_vhdr(sb, part_start); if (error) goto out_free_backup_vhdr; error = -EINVAL; - switch (sbi->s_vhdr->signature) { - case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX): - set_bit(HFSPLUS_SB_HFSX, &sbi->flags); - fallthrough; - case cpu_to_be16(HFSPLUS_VOLHEAD_SIG): - break; - case cpu_to_be16(HFSP_WRAP_MAGIC): + if (!is_hfsplus_volume(sbi->s_vhdr->signature)) { + /* + * Check for a partition block. The partition found must + * contain the volume itself. + * + * (should do this only for cdrom/loop though) + */ + if (hfs_part_find(sb, &part_start, &part_size)) + goto out_free_backup_vhdr; + + error = hfsplus_read_vhdr(sb, part_start); + if (error) + goto out_free_backup_vhdr; + + error = -EINVAL; + if (!is_hfsplus_volume(sbi->s_vhdr->signature)) + goto out_free_backup_vhdr; + } + + if (sbi->s_vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) { if (!hfsplus_read_mdb(sbi->s_vhdr, &wd)) goto out_free_backup_vhdr; wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; part_start += (sector_t)wd.ablk_start + (sector_t)wd.embed_start * wd.ablk_size; part_size = (sector_t)wd.embed_count * wd.ablk_size; - goto reread; - default: - /* - * Check for a partition block. - * - * (should do this only for cdrom/loop though) - */ - if (hfs_part_find(sb, &part_start, &part_size)) + + error = hfsplus_read_vhdr(sb, part_start); + if (error) goto out_free_backup_vhdr; - goto reread; + error = -EINVAL; } + /* + * Here it must be the HFS+ volume. The volume embedded in a wrapper + * cannot be a wrapper again. + */ + if (sbi->s_vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) + set_bit(HFSPLUS_SB_HFSX, &sbi->flags); + else if (sbi->s_vhdr->signature != cpu_to_be16(HFSPLUS_VOLHEAD_SIG)) + goto out_free_backup_vhdr; + error = hfsplus_submit_bio(sb, part_start + part_size - 2, sbi->s_backup_vhdr_buf, (void **)&sbi->s_backup_vhdr, REQ_OP_READ); -- 2.53.0