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 3/6] hfsplus: validate the partition map entry before following it
Date: Fri,  4 Dec 2026 17:31:24 -0800	[thread overview]
Message-ID: <20261205013127.1533652-4-slava@dubeyko.com> (raw)
In-Reply-To: <20261205013127.1533652-1-slava@dubeyko.com>

If hfsplus_read_wrapper() finds no volume at the start of the device,
it calls hfs_part_find() to look for an Apple partition map and reads
the volume header at the start of the found partition. The start and
the size of the partition are used as they are on disk, even if the
partition overlaps the partition map itself or goes beyond the device.

A partition cannot overlap the driver descriptor and the partition map,
which take the first sectors of the device, and must end within the
device. Check this for the found entry of both the old and the new
partition map, and fail the mount if it doesn't hold.

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/part_tbl.c | 48 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/fs/hfsplus/part_tbl.c b/fs/hfsplus/part_tbl.c
index 9ec21664eda6..4076609c51a7 100644
--- a/fs/hfsplus/part_tbl.c
+++ b/fs/hfsplus/part_tbl.c
@@ -15,6 +15,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/blkdev.h>
 #include "hfsplus_fs.h"
 
 /* offsets to various blocks */
@@ -67,6 +68,28 @@ struct old_pmap {
 	}	pdEntry[42];
 } __packed;
 
+/*
+ * Check that partition @part_index, which starts @entry_start sectors after
+ * @part_start and is @entry_count sectors long, does not overlap the driver
+ * descriptor and the partition map, which take @map_count sectors from
+ * HFS_PMAP_BLK, and ends within the device.
+ */
+static bool hfs_part_valid(struct super_block *sb, sector_t part_start,
+			   int part_index, u32 entry_start, u32 entry_count,
+			   u32 map_count)
+{
+	u64 map_end = (u64)HFS_PMAP_BLK + map_count;
+	sector_t part_end = part_start + entry_start + entry_count;
+
+	if (entry_start >= map_end && entry_count > 0 &&
+	    part_end <= bdev_nr_sectors(sb->s_bdev))
+		return true;
+
+	pr_err("invalid partition %d: entry_start %u, entry_count %u\n",
+	       part_index, entry_start, entry_count);
+	return false;
+}
+
 static int hfs_parse_old_pmap(struct super_block *sb, struct old_pmap *pm,
 		sector_t *part_start, sector_t *part_size)
 {
@@ -79,8 +102,15 @@ static int hfs_parse_old_pmap(struct super_block *sb, struct old_pmap *pm,
 		if (p->pdStart && p->pdSize &&
 		    p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
 		    (sbi->part < 0 || sbi->part == i)) {
-			*part_start += be32_to_cpu(p->pdStart);
-			*part_size = be32_to_cpu(p->pdSize);
+			u32 entry_start = be32_to_cpu(p->pdStart);
+			u32 entry_count = be32_to_cpu(p->pdSize);
+
+			/* The old partition map takes one sector. */
+			if (!hfs_part_valid(sb, *part_start, i, entry_start,
+					    entry_count, 1))
+				return -EINVAL;
+			*part_start += entry_start;
+			*part_size = entry_count;
 			return 0;
 		}
 	}
@@ -92,7 +122,7 @@ static int hfs_parse_new_pmap(struct super_block *sb, void *buf,
 		struct new_pmap *pm, sector_t *part_start, sector_t *part_size)
 {
 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
-	int size = be32_to_cpu(pm->pmMapBlkCnt);
+	int map_count = be32_to_cpu(pm->pmMapBlkCnt);
 	int buf_size = hfsplus_min_io_size(sb);
 	int res;
 	int i = 0;
@@ -100,12 +130,18 @@ static int hfs_parse_new_pmap(struct super_block *sb, void *buf,
 	do {
 		if (!memcmp(pm->pmPartType, "Apple_HFS", 9) &&
 		    (sbi->part < 0 || sbi->part == i)) {
-			*part_start += be32_to_cpu(pm->pmPyPartStart);
-			*part_size = be32_to_cpu(pm->pmPartBlkCnt);
+			u32 entry_start = be32_to_cpu(pm->pmPyPartStart);
+			u32 entry_count = be32_to_cpu(pm->pmPartBlkCnt);
+
+			if (!hfs_part_valid(sb, *part_start, i, entry_start,
+					    entry_count, map_count))
+				return -EINVAL;
+			*part_start += entry_start;
+			*part_size = entry_count;
 			return 0;
 		}
 
-		if (++i >= size)
+		if (++i >= map_count)
 			return -ENOENT;
 
 		pm = (struct new_pmap *)((u8 *)pm + HFSPLUS_SECTOR_SIZE);
-- 
2.53.0


  parent 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 ` [PATCH 1/6] hfsplus: look for the volume header in the order of the on-disk layout Viacheslav Dubeyko
2026-12-05  1:31 ` [PATCH 2/6] hfsplus: validate the HFS wrapper before following it Viacheslav Dubeyko
2026-12-05  1:31 ` Viacheslav Dubeyko [this message]
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-4-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®