[PATCH 5/6] hfs: validate the partition map entry before following it

From: Viacheslav Dubeyko

Date: Fri Dec 04 2026 - 20:32:01 EST


If hfs_mdb_get() finds no MDB at the start of the device, it calls
hfs_part_find() to look for an Apple partition map and reads the MDB
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@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/part_tbl.c | 53 ++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 48 insertions(+), 5 deletions(-)

diff --git a/fs/hfs/part_tbl.c b/fs/hfs/part_tbl.c
index 36add537d153..05c4c360bd68 100644
--- a/fs/hfs/part_tbl.c
+++ b/fs/hfs/part_tbl.c
@@ -9,6 +9,8 @@
* a patch contributed by Holger Schemel (aeglos@xxxxxxxxxxxxxx).
*/

+#include <linux/blkdev.h>
+
#include "hfs_fs.h"

/*
@@ -49,6 +51,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;
+}
+
/*
* hfs_part_find()
*
@@ -80,8 +104,18 @@ int hfs_part_find(struct super_block *sb,
if (p->pdStart && p->pdSize &&
p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
(HFS_SB(sb)->part < 0 || HFS_SB(sb)->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)) {
+ res = -EINVAL;
+ break;
+ }
+ *part_start += entry_start;
+ *part_size = entry_count;
res = 0;
}
}
@@ -96,9 +130,18 @@ int hfs_part_find(struct super_block *sb,
for (i = 0; i < size;) {
if (!memcmp(pm->pmPartType,"Apple_HFS", 9) &&
(HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
- *part_start += be32_to_cpu(pm->pmPyPartStart);
- *part_size = be32_to_cpu(pm->pmPartBlkCnt);
- res = 0;
+ 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,
+ size)) {
+ *part_start += entry_start;
+ *part_size = entry_count;
+ res = 0;
+ } else {
+ res = -EINVAL;
+ }
break;
}
brelse(bh);
--
2.53.0