[PATCH 2/6] hfsplus: validate the HFS wrapper before following it

From: Viacheslav Dubeyko

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


For an HFS wrapper, hfsplus_read_wrapper() moves part_start to the
embedded HFS+ volume by drAlBlSt + the start of the embedded extent.
hfsplus_read_mdb() only checks the embedded signature, two attribute
bits and the allocation block size, so the location of the embedded
volume is used as it is on disk, even if it points back to the wrapper
MDB itself or beyond the wrapper.

The volume bitmap of the wrapper follows its MDB, the allocation
blocks follow the volume bitmap, and the embedded volume occupies some
of the allocation blocks. Check this in hfsplus_read_mdb(): the volume
bitmap must start after the MDB, the first allocation block must be
after the volume bitmap, and the embedded extent must be non-empty,
lie within the allocation blocks and end within the wrapper. Then the
embedded volume always starts beyond the wrapper MDB.

Also check that the embedded volume is the one the wrapper declares:
its volume header must have the signature of drEmbedSigWord, and the
volume must fit in the embedded extent.

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/wrapper.c | 96 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 84 insertions(+), 12 deletions(-)

diff --git a/fs/hfsplus/wrapper.c b/fs/hfsplus/wrapper.c
index 33dcdcbe5761..71122cb2265e 100644
--- a/fs/hfsplus/wrapper.c
+++ b/fs/hfsplus/wrapper.c
@@ -17,7 +17,28 @@
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"

+/**
+ * struct hfsplus_wd - location of an HFS+ volume embedded in an HFS wrapper
+ * @embed_sig: signature of the embedded volume (drEmbedSigWord), in on-disk
+ * byte order. It must be HFSPLUS_VOLHEAD_SIG or HFSPLUS_VOLHEAD_SIGX,
+ * and the embedded volume header must carry the same signature.
+ * @ablk_size: size of an allocation block of the wrapper (drAlBlkSiz).
+ * hfsplus_read_mdb() stores it in bytes, and hfsplus_read_wrapper()
+ * converts it in place into 512-byte sectors before using it.
+ * @ablk_start: first sector of allocation block 0 of the wrapper (drAlBlSt),
+ * in 512-byte sectors from the start of the wrapper.
+ * @embed_start: first allocation block of the embedded volume
+ * (drEmbedExtent.xdrStABN).
+ * @embed_count: number of allocation blocks of the embedded volume
+ * (drEmbedExtent.xdrNumABlks).
+ *
+ * hfsplus_read_mdb() fills it in from the MDB of the wrapper. With
+ * @ablk_size in sectors, the embedded volume starts at sector
+ * @ablk_start + @embed_start * @ablk_size of the wrapper and is
+ * @embed_count * @ablk_size sectors long.
+ */
struct hfsplus_wd {
+ __be16 embed_sig;
u32 ablk_size;
u16 ablk_start;
u16 embed_start;
@@ -63,35 +84,69 @@ int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
return bdev_rw_virt(sb->s_bdev, sector, buf, io_size, opf);
}

-static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
+/*
+ * Check the HFS wrapper MDB in @bufptr and get the location of the
+ * embedded HFS+ volume from it.
+ *
+ * The volume bitmap of the wrapper follows its MDB, the allocation blocks
+ * follow the volume bitmap, and the embedded volume occupies some of the
+ * allocation blocks. So, the embedded volume starts beyond the MDB and
+ * must end within the wrapper, which is @part_size sectors long.
+ */
+static int hfsplus_read_mdb(void *bufptr, sector_t part_size,
+ struct hfsplus_wd *wd)
{
+ struct hfs_mdb *mdb = bufptr;
+ u32 nr_ablks, vbm_start, vbm_sects;
+ u64 embed_end;
u32 extent;
u16 attrib;
- __be16 sig;

- sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
- if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
- sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
+ wd->embed_sig = mdb->drEmbedSigWord;
+ if (wd->embed_sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
+ wd->embed_sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
return 0;

- attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
+ attrib = be16_to_cpu(mdb->drAtrb);
if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
!(attrib & HFSP_WRAP_ATTRIB_SPARED))
return 0;

- wd->ablk_size =
- be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
+ wd->ablk_size = be32_to_cpu(mdb->drAlBlkSiz);
if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
return 0;
if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
return 0;
- wd->ablk_start =
- be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
+ wd->ablk_start = be16_to_cpu(mdb->drAlBlSt);

- extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
+ extent = get_unaligned_be32(&mdb->drEmbedExtent);
wd->embed_start = (extent >> 16) & 0xFFFF;
wd->embed_count = extent & 0xFFFF;

+ nr_ablks = be16_to_cpu(mdb->drNmAlBlks);
+ vbm_start = be16_to_cpu(mdb->drVBMSt);
+ vbm_sects = DIV_ROUND_UP(nr_ablks, HFSPLUS_SECTOR_SIZE * BITS_PER_BYTE);
+
+ /* The MDB is in the same sector as the HFS+ volume header. */
+ if (vbm_start <= HFSPLUS_VOLHEAD_SECTOR ||
+ wd->ablk_start < vbm_start + vbm_sects) {
+ pr_err("invalid HFS wrapper layout\n");
+ return 0;
+ }
+
+ embed_end = (u64)wd->embed_start + wd->embed_count;
+ if (!wd->embed_count || embed_end > nr_ablks) {
+ pr_err("invalid HFS wrapper embedded extent\n");
+ return 0;
+ }
+
+ embed_end *= wd->ablk_size >> HFSPLUS_SECTOR_SHIFT;
+ embed_end += wd->ablk_start;
+ if (embed_end > part_size) {
+ pr_err("HFS wrapper embedded volume is beyond the wrapper\n");
+ return 0;
+ }
+
return 1;
}

@@ -208,7 +263,7 @@ int hfsplus_read_wrapper(struct super_block *sb)
}

if (sbi->s_vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) {
- if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
+ if (!hfsplus_read_mdb(sbi->s_vhdr, part_size, &wd))
goto out_free_backup_vhdr;
wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
part_start += (sector_t)wd.ablk_start +
@@ -219,6 +274,23 @@ int hfsplus_read_wrapper(struct super_block *sb)
if (error)
goto out_free_backup_vhdr;
error = -EINVAL;
+
+ /*
+ * The embedded volume must be the volume that the wrapper
+ * declares, and it must fit in the embedded extent.
+ */
+ if (sbi->s_vhdr->signature != wd.embed_sig) {
+ pr_err("HFS wrapper embedded volume has signature 0x%04x, not 0x%04x\n",
+ be16_to_cpu(sbi->s_vhdr->signature),
+ be16_to_cpu(wd.embed_sig));
+ goto out_free_backup_vhdr;
+ }
+ if ((u64)be32_to_cpu(sbi->s_vhdr->total_blocks) *
+ be32_to_cpu(sbi->s_vhdr->blocksize) >
+ (u64)part_size << HFSPLUS_SECTOR_SHIFT) {
+ pr_err("HFS wrapper embedded volume is larger than its extent\n");
+ goto out_free_backup_vhdr;
+ }
}

/*
--
2.53.0