[PATCH 4/6] hfs: look for the MDB in the order of the on-disk layout

From: Viacheslav Dubeyko

Date: Fri Dec 04 2026 - 20:31:56 EST


hfs_mdb_get() looks for the MDB by rereading at part_start until it
finds the HFS signature, calling hfs_part_find() whenever it does not.
hfs_part_find() moves part_start by the start of the found partition,
which comes from disk.

For an Apple_HFS entry with pmPyPartStart = 0, part_start does not
move, the same partition map is found again, and the mount loops
forever. As the MDB sector is read from the buffer cache, the loop
spins on the CPU without any I/O. There is no iteration limit and no
signal check, so the mount(2) caller cannot be killed.

The on-disk layout allows only an optional Apple partition map at the
start of the device, whose partition contains the volume. Follow it
instead of rereading in a loop: look for a partition map only if no
MDB is found at the start of the device, and require the found
partition to contain the MDB. As there is no way back, the mount
cannot loop, whatever the metadata are.

Reported-by: Aymen T <atijari00@xxxxxxxxx>
Link: https://lore.kernel.org/linux-fsdevel/45bfa4d7-e9e2-45f2-9e12-a794defae64a@xxxxxxxxx/
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
cc: John Paul Adrian Glaubitz <glaubitz@xxxxxxxxxxxxxxxxxxx>
cc: Yangtao Li <frank.li@xxxxxxxx>
cc: linux-fsdevel@xxxxxxxxxxxxxxx
---
fs/hfs/mdb.c | 31 +++++++++++++++++++++++--------
1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/fs/hfs/mdb.c b/fs/hfs/mdb.c
index 277de712f9d4..d17d7f9ac4f5 100644
--- a/fs/hfs/mdb.c
+++ b/fs/hfs/mdb.c
@@ -142,21 +142,36 @@ int hfs_mdb_get(struct super_block *sb)

if (hfs_get_last_session(sb, &part_start, &part_size))
return -EINVAL;
- while (1) {
- /* See if this is an HFS filesystem */
- bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
- if (!bh)
- return -EIO;

- if (mdb->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC))
- break;
+ /*
+ * The MDB is in sector HFS_MDB_BLK of the volume. The volume can be
+ * in a partition of an Apple partition map at the start of the
+ * device, so look for the partition map at most once.
+ */
+ bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
+ if (!bh)
+ return -EIO;
+
+ if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC)) {
brelse(bh);

- /* check for a partition block
+ /*
+ * 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))
return -EIO;
+
+ bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
+ if (!bh)
+ return -EIO;
+
+ if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC)) {
+ brelse(bh);
+ return -EINVAL;
+ }
}

HFS_SB(sb)->alloc_blksz = size = be32_to_cpu(mdb->drAlBlkSiz);
--
2.53.0