Linux filesystem development
 help / color / mirror / Atom feed
* hfsplus: mount loops forever on an HFS wrapper whose embedded volume points back at itself
@ 2026-11-21 18:17 Aymen T
  0 siblings, 0 replies; only message in thread
From: Aymen T @ 2026-11-21 18:17 UTC (permalink / raw)
  To: slava, glaubitz, frank.li; +Cc: linux-fsdevel

[-- Attachment #1: Type: text/plain, Size: 1866 bytes --]

Hi,

hfsplus_read_wrapper() follows an HFS wrapper to the embedded HFS+
volume and re-reads the volume header there:

    case 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;

hfsplus_read_mdb() only checks the embedded signature, two attribute
bits and that ablk_size is a non-zero multiple of 512. With
drAlBlSt = 0 and the embedded extent's start block = 0, part_start
doesn't move, the next read returns the same wrapper MDB, and the
mount spins on that sector for as long as the device answers. There
is no iteration limit and no fatal_signal_pending() check, so the
mount(2) caller can't be killed. The default: branch can loop the
same way: hfs_parse_new_pmap() adds pmPyPartStart without checking
it, so an Apple_HFS entry with pmPyPartStart = 0 sends it back to
reread at the same place. I only reproduced the wrapper case.

On mainline (65e4fd942f72, 7.4.0-rc3) in QEMU, with the image attached
as a USB mass-storage device (qemu-xhci + usb-storage) and mounted with
flags 0x18e (NOSUID|NODEV|NOEXEC|DIRSYNC|NOSYMFOLLOW): mount never
returned, and the device saw 62,680 READs of sector 2 before the
120 s run was killed. The same image with
ablk_start = 8, embed_start = 1 fails the mount with -EINVAL after 5
reads.

Fix: refuse a wrapper whose computed part_start doesn't advance (or
limit the number of wrapper/partition hops to a small number), and
return -EINVAL.

Attached: mkhfs.py (python3 mkhfs.py wrap_loop loop.img makes the
image; wrap_once makes the control, which needs mkfs.hfsplus).

Thanks,
Aymen

[-- Attachment #2: mkhfs.py --]
[-- Type: text/x-python, Size: 2698 bytes --]

#!/usr/bin/env python3
"""HFS+ wrapper images (fs/hfsplus/wrapper.c read_wrapper + read_mdb).

wrap_loop : sector 2 carries an HFS wrapper MDB (signature 'BD') whose embedded-volume extent says
            ablk_start = 0, embed_start = 0, so hfsplus_read_wrapper recomputes part_start to the same
            value and jumps back to `reread` with identical input.
wrap_once : same MDB but ablk_start = 8 and embed_start = 1 with ablk_size 512, so part_start advances to
            sector 9, where a real HFS+ volume header (made by mkfs.hfsplus) sits. Control: one re-read, then
            the volume mounts.
"""
import os, struct, subprocess, sys

SEC = 512

def mdb(ablk_size, ablk_start, embed_start, embed_count):
    b = bytearray(SEC)
    struct.pack_into('>H', b, 0x00, 0x4244)                      # HFSP_WRAP_MAGIC
    struct.pack_into('>H', b, 0x0A, 0x8000 | 0x0200)             # SLOCK | SPARED
    struct.pack_into('>I', b, 0x14, ablk_size)                   # drAlBlkSiz
    struct.pack_into('>H', b, 0x1C, ablk_start)                  # drAlBlSt
    struct.pack_into('>H', b, 0x7C, 0x482B)                      # embedded signature H+
    struct.pack_into('>HH', b, 0x7E, embed_start, embed_count)   # embedded extent
    return b

def main():
    kind, out = sys.argv[1], sys.argv[2]
    if os.path.exists(out):
        os.unlink(out)
    open(out, 'wb').truncate(64 << 20)
    if kind == 'wrap_step':
        # Same MDB, but the extent advances part_start by 9 sectors each time, so each re-read looks at a new
        # sector; nothing there is a wrapper, so the loop ends after one extra read. Control for wrap_loop.
        with open(out, 'r+b') as f:
            f.seek(2 * SEC)
            f.write(mdb(512, 8, 1, 1024))
        print('wrap_step: MDB at sector 2 -> part_start advances to 9 each pass')
    elif kind == 'wrap_once':
        # a real HFS+ volume starting at sector 9: build one, then shift it by 9 sectors
        tmp = out + '.inner'
        open(tmp, 'wb').truncate(32 << 20)
        subprocess.check_call(['mkfs.hfsplus', '-v', 'T', tmp], stdout=subprocess.DEVNULL)
        inner = open(tmp, 'rb').read()
        os.unlink(tmp)
        with open(out, 'r+b') as f:
            f.seek(9 * SEC)
            f.write(inner)
            f.seek(2 * SEC)
            f.write(mdb(512, 8, 1, (32 << 20) // SEC))
        print('wrap_once: MDB at sector 2 -> part_start 0+8+1*1 = 9, HFS+ volume header at sector 9+2')
    else:
        with open(out, 'r+b') as f:
            f.seek(2 * SEC)
            f.write(mdb(512, 0, 0, 1024))
        print('wrap_loop: MDB at sector 2, ablk_start=0 embed_start=0 -> part_start unchanged')

if __name__ == '__main__':
    main()

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-21 18:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 18:17 hfsplus: mount loops forever on an HFS wrapper whose embedded volume points back at itself Aymen T

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox