hfs/hfsplus: fix endless loop on mount of crafted images
Subsystems (4)
Viacheslav Dubeyko
maintainercover hfs/hfsplus: fix endless loop on mount of crafted images
Both drivers look for the volume header (HFS+) or MDB (HFS) by rereading at part_start until they find the signature. An HFS wrapper or an Apple partition map moves part_start by values from disk without any check. If such a value is zero, part_start does not move, the same wrapper or partition map is found again, and the mount loops forever. The mount(2) caller cannot be killed.
The HFS+ wrapper case was reported with drAlBlSt = 0 and the embedded extent start = 0. A partition map entry with pmPyPartStart = 0 hangs both drivers in the same way. On HFS, the reread is served from the buffer cache, so the mount spins on the CPU without any I/O.
The on-disk layout allows only one order:
[partition map] -> [HFS wrapper] -> HFS+ volume header (or HFS MDB)
Patches 1 and 4 follow this order step by step instead of rereading in a loop, so the mount cannot loop, whatever the metadata are. They are the minimal fix.
Patches 2, 3 and 5 validate the metadata before following it:
- Patch 2: the wrapper's volume bitmap must follow its MDB, the first allocation block must follow the volume bitmap, and the embedded extent must be non-empty, lie within the allocation blocks and end within the wrapper. The embedded volume header must have the signature declared in drEmbedSigWord, and the embedded volume must fit in the embedded extent.
- Patches 3 and 5: a partition must not overlap the driver descriptor and the partition map, must be non-empty and must end within the device, for both the old and the new partition map.
Patch 6 fixes an older issue found while working on patch 5: for the old partition map, hfs_part_find() doesn't stop at the first matching entry, so every following "TFS1" entry adds its start to part_start again. Now it stops at the first matching entry, like the new partition map and the HFS+ code do.
Tested in QEMU (KASAN, lockdep):
- Unpatched: a self-referencing wrapper (hfsplus) and a self-referencing partition map (hfsplus and hfs) hang the mount, and SIGKILL does not stop it.
- Patched: 26 crafted images are refused, each by the check it breaks (self-referencing wrapper, volume bitmap at the MDB, allocation blocks before the volume bitmap, empty or out-of-range embedded extent, embedded volume beyond the device, nested wrapper, wrapper containing a partition map, embedded volume with another signature, missing or larger than its extent; self-referencing, overlapping, empty and out-of-range partitions in old and new partition maps, chained partition maps).
- Patched: plain HFS+, wrapped HFS+ and plain HFS, also inside old and new Apple partition maps, mount and a written file survives a remount. Wrappers made by mkfs.hfsplus -w for 4 MiB - 8 GiB volumes and 512 B - 64 KiB block sizes pass the new checks; their embedded volume size equals the embedded extent size.
- Patches 1 and 4 alone: no crafted image hangs the mount either.
Viacheslav Dubeyko (6):
hfsplus: look for the volume header in the order of the on-disk layout
hfsplus: validate the HFS wrapper before following it
hfsplus: validate the partition map entry before following it
hfs: look for the MDB in the order of the on-disk layout
hfs: validate the partition map entry before following it
hfs: stop at the first matching entry of the old partition map
fs/hfs/mdb.c | 31 ++++++-- fs/hfs/part_tbl.c | 54 +++++++++++-- fs/hfsplus/part_tbl.c | 48 ++++++++++-- fs/hfsplus/wrapper.c | 172 ++++++++++++++++++++++++++++++++++-------- 4 files changed, 256 insertions(+), 49 deletions(-)
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 1/6 hfsplus: look for the volume header in the order of the on-disk layout
hfsplus_read_wrapper() looks for the volume header by rereading at part_start until it finds an HFS+ signature. For an HFS wrapper, it moves part_start to the embedded volume, and for anything else, it looks for an Apple partition map and moves part_start to the found partition. Both offsets come from disk.
If such an offset is zero, part_start does not move, the same wrapper MDB or partition map is found again, and the mount loops forever. This was reported for a wrapper with drAlBlSt = 0 and the embedded extent start = 0, and a partition map entry with pmPyPartStart = 0 loops the same way. There is no iteration limit and no signal check, so the mount(2) caller cannot be killed.
The on-disk layout allows only one order: an optional Apple partition map at the start of the device, whose partition contains the volume, then an optional HFS wrapper, whose embedded volume is the HFS+ volume. Follow this order step by step instead of rereading in a loop: look for a partition map only if no volume is found at the start of the device, require the found partition to contain an HFS+ volume or a wrapper, and require the volume embedded in a wrapper to be the HFS+ volume. As there is no way back, the mount cannot loop, whatever the metadata are.
--- fs/hfsplus/wrapper.c | 80 +++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 20 deletions(-)
fs/hfsplus/wrapper.c +60 -20
@@ -129,6 +129,22 @@ static int hfsplus_get_last_session(struct super_block *sb,return 0 ; } +static int hfsplus_read_vhdr (struct super_block *sb,sector_t part_start) +{ +struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb); + +return hfsplus_submit_bio (sb, part_start +HFSPLUS_VOLHEAD_SECTOR , + sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, +REQ_OP_READ ); +} + +static bool is_hfsplus_volume (__be16 sig) +{ +return sig ==cpu_to_be16 (HFSPLUS_VOLHEAD_SIG ) || + sig ==cpu_to_be16 (HFSPLUS_VOLHEAD_SIGX ) || + sig ==cpu_to_be16 (HFSP_WRAP_MAGIC ); +} +/* Find the volume header and fill in some minimum bits in superblock */ /* Takes in super block, returns true if good data read */ int hfsplus_read_wrapper (struct super_block *sb)
@@ -157,39 +173,63 @@ int hfsplus_read_wrapper(struct super_block *sb)if (!sbi->s_backup_vhdr_buf) goto out_free_vhdr; -reread: - error =hfsplus_submit_bio (sb, part_start +HFSPLUS_VOLHEAD_SECTOR , - sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, -REQ_OP_READ ); +/* +* The volume header is in sector HFSPLUS_VOLHEAD_SECTOR of the +* volume. The volume can be in a partition of an Apple partition +* map, and it can be embedded in an HFS wrapper, whose MDB is in the +* same sector. So, the on-disk layout is: +* +* [partition map] -> [HFS wrapper] -> HFS+ volume header +* +* Follow it in this order, each step at most once. +*/ + error =hfsplus_read_vhdr (sb, part_start);if (error) goto out_free_backup_vhdr; error = -EINVAL ; -switch (sbi->s_vhdr->signature) { -case cpu_to_be16 (HFSPLUS_VOLHEAD_SIGX ): -set_bit (HFSPLUS_SB_HFSX , &sbi->flags); - fallthrough; -case cpu_to_be16 (HFSPLUS_VOLHEAD_SIG ): -break ; -case cpu_to_be16 (HFSP_WRAP_MAGIC ): +if (!is_hfsplus_volume (sbi->s_vhdr->signature)) { +/* +* Check for a partition block. The partition found must +* contain the volume itself. +* +* (should do this only for cdrom/loop though) +*/ +if (hfs_part_find (sb, &part_start, &part_size)) + goto out_free_backup_vhdr; + + error =hfsplus_read_vhdr (sb, part_start); +if (error) + goto out_free_backup_vhdr; + + error = -EINVAL ; +if (!is_hfsplus_volume (sbi->s_vhdr->signature)) + goto out_free_backup_vhdr; + } + +if (sbi->s_vhdr->signature ==cpu_to_be16 (HFSP_WRAP_MAGIC )) {if (!hfsplus_read_mdb (sbi->s_vhdr, &wd)) goto out_free_backup_vhdr; wd.ablk_size >>=HFSPLUS_SECTOR_SHIFT ; part_start += (sector_t )wd.ablk_start + (sector_t )wd.embed_start * wd.ablk_size; part_size = (sector_t )wd.embed_count * wd.ablk_size; - goto reread; -default : -/* -* Check for a partition block. -* -* (should do this only for cdrom/loop though) -*/ -if (hfs_part_find (sb, &part_start, &part_size)) + + error =hfsplus_read_vhdr (sb, part_start); +if (error) goto out_free_backup_vhdr; - goto reread; + error = -EINVAL ; } +/* +* Here it must be the HFS+ volume. The volume embedded in a wrapper +* cannot be a wrapper again. +*/ +if (sbi->s_vhdr->signature ==cpu_to_be16 (HFSPLUS_VOLHEAD_SIGX )) +set_bit (HFSPLUS_SB_HFSX , &sbi->flags); +else if (sbi->s_vhdr->signature !=cpu_to_be16 (HFSPLUS_VOLHEAD_SIG )) + goto out_free_backup_vhdr; + error =hfsplus_submit_bio (sb, part_start + part_size -2 , sbi->s_backup_vhdr_buf, (void **)&sbi->s_backup_vhdr,REQ_OP_READ );
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 2/6 hfsplus: validate the HFS wrapper before following it
In reply to Viacheslav DubeykoFor 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.
--- fs/hfsplus/wrapper.c | 96 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 12 deletions(-)
fs/hfsplus/wrapper.c +84 -12
@@ -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; + } }/*
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 3/6 hfsplus: validate the partition map entry before following it
In reply to Viacheslav DubeykoIf 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.
--- fs/hfsplus/part_tbl.c | 48 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-)
fs/hfsplus/part_tbl.c +42 -6
@@ -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);
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 4/6 hfs: look for the MDB in the order of the on-disk layout
In reply to Viacheslav Dubeykohfs_mdb_get() looks for the MDB by rereading at part_start until it finds the HFS signature, calling hfs_part_find() whenever it does not. hfs_part_find() moves part_start by the start of the found partition, which comes from disk.
For an Apple_HFS entry with pmPyPartStart = 0, part_start does not move, the same partition map is found again, and the mount loops forever. As the MDB sector is read from the buffer cache, the loop spins on the CPU without any I/O. There is no iteration limit and no signal check, so the mount(2) caller cannot be killed.
The on-disk layout allows only an optional Apple partition map at the start of the device, whose partition contains the volume. Follow it instead of rereading in a loop: look for a partition map only if no MDB is found at the start of the device, and require the found partition to contain the MDB. As there is no way back, the mount cannot loop, whatever the metadata are.
--- fs/hfs/mdb.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
fs/hfs/mdb.c +23 -8
@@ -142,21 +142,36 @@ int hfs_mdb_get(struct super_block *sb)if (hfs_get_last_session (sb, &part_start, &part_size))return -EINVAL ; -while (1 ) { -/* See if this is an HFS filesystem */ - bh =sb_bread512 (sb, part_start +HFS_MDB_BLK , mdb); -if (!bh) -return -EIO ; -if (mdb->drSigWord ==cpu_to_be16 (HFS_SUPER_MAGIC )) -break ; +/* +* The MDB is in sector HFS_MDB_BLK of the volume. The volume can be +* in a partition of an Apple partition map at the start of the +* device, so look for the partition map at most once. +*/ + bh =sb_bread512 (sb, part_start +HFS_MDB_BLK , mdb); +if (!bh) +return -EIO ; + +if (mdb->drSigWord !=cpu_to_be16 (HFS_SUPER_MAGIC )) {brelse (bh); -/* check for a partition block +/* +* Check for a partition block. The partition found must +* contain the volume itself. +* * (should do this only for cdrom/loop though) */ if (hfs_part_find (sb, &part_start, &part_size))return -EIO ; + + bh =sb_bread512 (sb, part_start +HFS_MDB_BLK , mdb); +if (!bh) +return -EIO ; + +if (mdb->drSigWord !=cpu_to_be16 (HFS_SUPER_MAGIC )) { +brelse (bh); +return -EINVAL ; + } }HFS_SB (sb)->alloc_blksz = size =be32_to_cpu (mdb->drAlBlkSiz);
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 5/6 hfs: validate the partition map entry before following it
In reply to Viacheslav DubeykoIf 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.
--- fs/hfs/part_tbl.c | 53 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-)
fs/hfs/part_tbl.c +48 -5
@@ -9,6 +9,8 @@* a patch contributed by Holger Schemel ([email protected]). */ +#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);
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH 6/6 hfs: stop at the first matching entry of the old partition map
In reply to Viacheslav DubeykoWhen hfs_part_find() finds a matching "TFS1" entry in the old partition map, it adds the start of the partition to *part_start and sets *part_size, but it doesn't leave the loop. It goes on checking the rest of the 42 entries, so without the part= mount option every following "TFS1" entry adds its start to *part_start again. As a result, the MDB is looked for at the sum of the partition starts, which is not the start of any partition, and the size of the last entry is used with it. Also, hfs_part_valid() checks the following entries against the already shifted *part_start.
The new partition map branch of hfs_part_find() and the hfsplus code stop at the first matching entry. Do the same for the old partition map.
--- fs/hfs/part_tbl.c | 1 + 1 file changed, 1 insertion(+)
fs/hfs/part_tbl.c +1 -0
@@ -117,6 +117,7 @@ int hfs_part_find(struct super_block *sb, *part_start += entry_start; *part_size = entry_count; res =0 ; +break ; } }break ;
Signature · 1 line
2.53.0