[PATCH 3/6] hfsplus: validate the partition map entry before following it

From: Viacheslav Dubeyko

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


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@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/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