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 5/6] hfs: validate the partition map entry before following it
Date: Fri, 4 Dec 2026 17:31:26 -0800 [thread overview]
Message-ID: <20261205013127.1533652-6-slava@dubeyko.com> (raw)
In-Reply-To: <20261205013127.1533652-1-slava@dubeyko.com>
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@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/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@valinor.owl.de).
*/
+#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
next prev 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 ` [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 ` Viacheslav Dubeyko [this message]
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-6-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®