mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viacheslav Dubeyko <slava@dubeyko.com>
To: glaubitz@physik.fu-berlin.de, frank.li@vivo.com
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	vdubeyko@coreweave.com, Viacheslav Dubeyko <slava@dubeyko.com>,
	Aymen T <atijari00@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH 1/6] hfsplus: look for the volume header in the order of the on-disk layout
Date: Fri,  4 Dec 2026 17:31:22 -0800	[thread overview]
Message-ID: <20261205013127.1533652-2-slava@dubeyko.com> (raw)
In-Reply-To: <20261205013127.1533652-1-slava@dubeyko.com>

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 <atijari00@gmail.com>
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 <slava@dubeyko.com>
Cc: stable@vger.kernel.org
cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
cc: Yangtao Li <frank.li@vivo.com>
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


  reply	other threads:[~2026-12-05  1:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  1:31 [PATCH 0/6] hfs/hfsplus: fix endless loop on mount of crafted images Viacheslav Dubeyko
2026-12-05  1:31 ` Viacheslav Dubeyko [this message]
2026-12-05  1:31 ` [PATCH 2/6] hfsplus: validate the HFS wrapper before following it Viacheslav Dubeyko
2026-12-05  1:31 ` [PATCH 3/6] hfsplus: validate the partition map entry " Viacheslav Dubeyko
2026-12-05  1:31 ` [PATCH 4/6] hfs: look for the MDB in the order of the on-disk layout Viacheslav Dubeyko
2026-12-05  1:31 ` [PATCH 5/6] hfs: validate the partition map entry before following it Viacheslav Dubeyko
2026-12-05  1:31 ` [PATCH 6/6] hfs: stop at the first matching entry of the old partition map Viacheslav Dubeyko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205013127.1533652-2-slava@dubeyko.com \
    --to=slava@dubeyko.com \
    --cc=atijari00@gmail.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=vdubeyko@coreweave.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®