hfsplus: validate btree bitmap during mount and handle corruption gracefully
Subsystems (2)
PATCH v2 1/1 hfsplus: validate btree bitmap during mount and handle corruption gracefully
Add bitmap validation during HFS+ btree open to detect corruption where node 0 (header node) is not marked allocated. When corruption is detected, mount the filesystem read-only instead of failing the mount, allowing data recovery from corrupted images.
The bitmap validation checks the node allocation bitmap in the btree header node (record #2) and verifies that bit 7 (MSB) of the first byte is set, indicating node 0 is allocated. This is a fundamental invariant that must always hold.
Implementation details:
- Add 'btree_bitmap_corrupted' flag to 'struct hfsplus_sb_info' to track corruption at superblock level
- Create and use 'hfsplus_validate_btree_bitmap()' to return bool indicating corruption
- Check corruption flag in 'hfsplus_fill_super()' after all btree opens
- Mount read-only with consolidated warning message when corruption detected
- Preserve existing btree validation logic and error handling patterns
This prevents kernel panics from corrupted syzkaller-generated HFS+ images while enabling data recovery by mounting read-only instead of failing.
---
v2 changes:
- Fix compiler warning about comparing u16 bitmap_off with PAGE_SIZE which
can exceed u16 maximum on some architectures
- Cast bitmap_off to unsigned int for the PAGE_SIZE comparison to avoid
tautological constant-out-of-range comparison warning.
- Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
fs/hfsplus/btree.c | 69 +++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/super.c | 7 +++++ 3 files changed, 77 insertions(+)
fs/hfsplus/btree.c +69 -0
@@ -129,6 +129,68 @@ u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size,return clump_size; } +/* +* Validate that node 0 (header node) is marked allocated in the bitmap. +* This is a fundamental invariant - node 0 must always be allocated. +* Returns true if corruption is detected (node 0 bit is unset). +* Note: head must be from kmap_local_page(page) that is still mapped. +* This function accesses the page through head pointer, so it must be +* called before kunmap_local(head). +*/ +static bool hfsplus_validate_btree_bitmap (struct hfs_btree *tree, +struct hfs_btree_header_rec *head) +{ +u8 *page_base; +u16 rec_off_tbl_off; +__be16 rec_data[2 ]; +u16 bitmap_off, bitmap_len; +u8 *bitmap_ptr; +u8 first_byte; +unsigned int node_size = tree->node_size; + +/* +* Get base page pointer. head points to: +* kmap_local_page(page) + sizeof(struct hfs_bnode_desc) +*/ + page_base = (u8 *)head -sizeof (struct hfs_bnode_desc ); + +/* +* Calculate offset to record 2 entry in record offset table. +* Record offsets are at end of node: node_size - (rec_num + 2) * 2 +* Record 2: (2+2)*2 = 8 bytes from end +*/ + rec_off_tbl_off = node_size - (2 +2 ) *2 ; + +/* Only validate if record offset table is on the first page */ +if (rec_off_tbl_off +4 > node_size || rec_off_tbl_off +4 >PAGE_SIZE ) +return false;/* Skip validation if offset table not on first page */ + +/* Read record 2 offset table entry (length and offset, both u16) */ +memcpy (rec_data, page_base + rec_off_tbl_off,4 ); + bitmap_off =be16_to_cpu (rec_data[1 ]); + bitmap_len =be16_to_cpu (rec_data[0 ]) - bitmap_off; + +/* +* Validate bitmap offset is within node and after bnode_desc. +* Also ensure bitmap is on the first page. +*/ +if (bitmap_len ==0 || + bitmap_off <sizeof (struct hfs_bnode_desc ) || + bitmap_off >= node_size || + (unsigned int ) bitmap_off >=PAGE_SIZE ) +return false;/* Skip validation if bitmap not accessible */ + +/* Read first byte of bitmap */ + bitmap_ptr = page_base + bitmap_off; + first_byte = bitmap_ptr[0 ]; + +/* Check if node 0's bit (bit 7, MSB) is set */ +if (!(first_byte &0x80 )) +return true;/* Corruption detected */ + +return false; +} +/* Get a reference to a B*Tree and do some initial checks */ struct hfs_btree *hfs_btree_open (struct super_block *sb,u32 id) {
@@ -176,6 +238,13 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) tree->max_key_len =be16_to_cpu (head->max_key_len); tree->depth =be16_to_cpu (head->depth); +/* Validate bitmap: node 0 must be marked allocated */ +if (hfsplus_validate_btree_bitmap (tree, head)) { +struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb); + + sbi->btree_bitmap_corrupted = true; + } +/* Verify the tree and set the correct compare function */ switch (id) {case HFSPLUS_EXT_CNID :
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -154,6 +154,7 @@ struct hfsplus_sb_info {int part, session;unsigned long flags; +bool btree_bitmap_corrupted;/* Bitmap corruption detected during btree open */ int work_queued;/* non-zero delayed work is queued */ struct delayed_work sync_work;/* FS sync delayed work */
fs/hfsplus/super.c +7 -0
@@ -534,6 +534,13 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc) }atomic_set (&sbi->attr_tree_state,HFSPLUS_VALID_ATTR_TREE ); } + +/* Check for bitmap corruption and mount read-only if detected */ +if (sbi->btree_bitmap_corrupted) { +pr_warn ("HFS+ (device %s): btree bitmap corruption detected, mounting read-only; run fsck.hfsplus to repair\n" , + sb->s_id); + sb->s_flags |=SB_RDONLY ; + } sb->s_xattr = hfsplus_xattr_handlers; inode =hfsplus_iget (sb,HFSPLUS_ALLOC_CNID );
Signature · 1 line
2.34.1
Quoted 45 lines · Shardul Bankar
Add bitmap validation during HFS+ btree open to detect corruption where node 0 (header node) is not marked allocated. When corruption is detected, mount the filesystem read-only instead of failing the mount, allowing data recovery from corrupted images.
The bitmap validation checks the node allocation bitmap in the btree header node (record #2) and verifies that bit 7 (MSB) of the first byte is set, indicating node 0 is allocated. This is a fundamental invariant that must always hold.
Implementation details:
- Add 'btree_bitmap_corrupted' flag to 'structhfsplus_sb_info' to track corruption at superblock level
- Create and use 'hfsplus_validate_btree_bitmap()' to return bool indicating corruption
- Check corruption flag in 'hfsplus_fill_super()' after all btree opens
- Mount read-only with consolidated warning message when corruption detected
- Preserve existing btree validation logic and error handling patternsThis prevents kernel panics from corrupted syzkaller-generated HFS+ images while enabling data recovery by mounting read-only instead of failing.
Reported-by [email protected]Signed-off-by Shardul Bankar <[email protected]>---
v2 changes:
- Fix compiler warning about comparing u16bitmap_offwithPAGE_SIZEwhich
can exceed u16 maximum on some architectures
- Castbitmap_offto unsigned int for thePAGE_SIZEcomparison to avoid
tautological constant-out-of-range comparison warning.
- Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/fs/hfsplus/btree.c | 69 +++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/super.c | 7 +++++ 3 files changed, 77 insertions(+)fs/hfsplus/btree.c +0
-0
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 136
Quoted 8 lines
fs/hfsplus/btree.c +5
-0@@ -129,6 +129,68 @@ u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size,return clump_size; } +/* +* Validate that node 0 (header node) is marked allocated in the bitmap. +* This is a fundamental invariant - node 0 must always be allocated. +* Returns true if corruption is detected (node 0 bit is unset). +* Note: head must be from kmap_local_page(page) that is still mapped.
This function works with pointer on memory region. It doesn't need to explain the details of how this pointer has been prepared.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 141
Quoted 5 lines
fs/hfsplus/btree.c +5
-0+* This function accesses the page through head pointer, so it must be +* called before kunmap_local(head). +*/ +static bool hfsplus_validate_btree_bitmap (struct hfs_btree *tree, +struct hfs_btree_header_rec *head)
I don't think that we need in struct hfs_btree_header_rec here. We don't use any information from the header. We simply need to provide pointer on node itself and it can be void * or u8 * pointer.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 153
Quoted 12 lines
fs/hfsplus/btree.c +12
-0+{ +u8 *page_base; +u16 rec_off_tbl_off; +__be16 rec_data[2 ]; +u16 bitmap_off, bitmap_len; +u8 *bitmap_ptr; +u8 first_byte; +unsigned int node_size = tree->node_size; + +/* +* Get base page pointer. head points to: +* kmap_local_page(page) + sizeof(struct hfs_bnode_desc)
Forget about kmap_local_page(page).
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 155
Quoted 2 lines
fs/hfsplus/btree.c +2
-0+*/ + page_base = (u8 *)head -sizeof (struct hfs_bnode_desc );
What's the point to provide pointer on header record if you need the start of node? This computation could be source of bugs. Simply provide the pointer on node's start to this function.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 162
Quoted 7 lines
fs/hfsplus/btree.c +7
-0+ +/* +* Calculate offset to record 2 entry in record offset table. +* Record offsets are at end of node: node_size - (rec_num + 2) * 2 +* Record 2: (2+2)*2 = 8 bytes from end +*/ + rec_off_tbl_off = node_size - (2 +2 ) *2 ;
Don't invent the wheel. HFS+ code already has logic of offsets table reading.
What if the node size will be bigger that 4K (for example, 8K)?
I never accept the patch with hardcoded constants. Please, use named constants. But you need to reuse the existing logic of working with b-tree nodes.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 166
Quoted 4 lines
fs/hfsplus/btree.c +4
-0+ +/* Only validate if record offset table is on the first page */ +if (rec_off_tbl_off +4 > node_size || rec_off_tbl_off +4 >PAGE_SIZE ) +return false;/* Skip validation if offset table not on first page */
So, we skip bitmap validation if a node is bigger than 4K? I cannot accept such code.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 169
Quoted 3 lines
fs/hfsplus/btree.c +3
-0+ +/* Read record 2 offset table entry (length and offset, both u16) */ +memcpy (rec_data, page_base + rec_off_tbl_off,4 );
Ditto.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 171
Quoted 2 lines
fs/hfsplus/btree.c +2
-0+ bitmap_off =be16_to_cpu (rec_data[1 ]); + bitmap_len =be16_to_cpu (rec_data[0 ]) - bitmap_off;
I assume we have metadata structure for offsets table record. No?
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 181
Quoted 10 lines
fs/hfsplus/btree.c +10
-0+ +/* +* Validate bitmap offset is within node and after bnode_desc. +* Also ensure bitmap is on the first page. +*/ +if (bitmap_len ==0 || + bitmap_off <sizeof (struct hfs_bnode_desc ) || + bitmap_off >= node_size || + (unsigned int ) bitmap_off >=PAGE_SIZE ) +return false;/* Skip validation if bitmap not accessible */
First of all, it makes sense to introduce the static inline function for the check.
Secondly, we need to be ready to validate the bitmap for any reasonable node size.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 185
Quoted 4 lines
fs/hfsplus/btree.c +4
-0+ +/* Read first byte of bitmap */ + bitmap_ptr = page_base + bitmap_off; + first_byte = bitmap_ptr[0 ];
The bitmap_ptr is completely enough. The *bitmap_ptr gives you access to the first byte.
fs/hfsplus/btree.c u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, · line 188
Quoted 3 lines
fs/hfsplus/btree.c +3
-0+ +/* Check if node 0's bit (bit 7, MSB) is set */ +if (!(first_byte &0x80 ))
What's about test_bit(nr, addr) here?
Quoted 9 lines
fs/hfsplus/btree.c +5
-0+return true;/* Corruption detected */ + +return false; +} +/* Get a reference to a B*Tree and do some initial checks */ struct hfs_btree *hfs_btree_open (struct super_block *sb,u32 id) {
fs/hfsplus/btree.c struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) · line 245
Quoted 8 lines
fs/hfsplus/btree.c +5
-0@@ -176,6 +238,13 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) tree->max_key_len =be16_to_cpu (head->max_key_len); tree->depth =be16_to_cpu (head->depth); +/* Validate bitmap: node 0 must be marked allocated */ +if (hfsplus_validate_btree_bitmap (tree, head)) { +struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb); + + sbi->btree_bitmap_corrupted = true;
Please, see my comment about this field.
Quoted 10 lines
fs/hfsplus/btree.c +2
-0+ } +/* Verify the tree and set the correct compare function */ switch (id) {case HFSPLUS_EXT_CNID :fs/hfsplus/hfsplus_fs.h +0
-0
fs/hfsplus/hfsplus_fs.h struct hfsplus_sb_info { · line 157
Quoted 4 lines
fs/hfsplus/hfsplus_fs.h +1
-0@@ -154,6 +154,7 @@ struct hfsplus_sb_info {int part, session;unsigned long flags; +bool btree_bitmap_corrupted;/* Bitmap corruption detected during btree open */
This field is completely unnecessary. The hfs_btree_open() can return -EROFS error code and hfsplus_fill_super() can process it.
Quoted 8 lines
fs/hfsplus/hfsplus_fs.h +0
-0int work_queued;/* non-zero delayed work is queued */ struct delayed_work sync_work;/* FS sync delayed work */ fs/hfsplus/super.c +0
-0
fs/hfsplus/super.c static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc) · line 540
Quoted 7 lines
fs/hfsplus/super.c +4
-0@@ -534,6 +534,13 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc) }atomic_set (&sbi->attr_tree_state,HFSPLUS_VALID_ATTR_TREE ); } + +/* Check for bitmap corruption and mount read-only if detected */ +if (sbi->btree_bitmap_corrupted) { + pr_warn("HFS+ (device %s): btree bitmap corruption detected, mounting read-only; run fsck.hfsplus to repair\n",
We don't need to mention "HFS+" here. I prefer to have two messages: (1) information about bitmap corruption (it will be good to know which tree is corrupted), (2) recommendation of running FSCK tool.
Thanks,
Slava.
Quoted 6 lines
fs/hfsplus/super.c +3
-0+ sb->s_id); + sb->s_flags |=SB_RDONLY ; + } sb->s_xattr = hfsplus_xattr_handlers; inode =hfsplus_iget (sb,HFSPLUS_ALLOC_CNID );
Quoted 27 lines · Viacheslav Dubeyko
Quoted 11 lines · Shardul Bankar
fs/hfsplus/btree.c +0
-0@@ -176,6 +238,13 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) tree->max_key_len = be16_to_cpu(head->max_key_len); tree->depth = be16_to_cpu(head->depth);
+ /* Validate bitmap: node 0 must be marked allocated */ + if (hfsplus_validate_btree_bitmap(tree, head)) { + structhfsplus_sb_info*sbi =HFSPLUS_SB(sb); +
+ sbi->btree_bitmap_corrupted = true;Please, see my comment about this field.
Quoted 12 lines
+ }
+
/* Verify the tree and set the correct compare function */ switch (id) {
caseHFSPLUS_EXT_CNID:fs/hfsplus/hfsplus_fs.h +0
-0@@ -154,6 +154,7 @@ struct hfsplus_sb_info {
int part, session;
Quoted 8 lines
Quoted 3 lines
unsigned long flags;
+ bool btree_bitmap_corrupted; /* Bitmap corruption detected during btree open */This field is completely unnecessary. The
hfs_btree_open()can return-EROFS
error code andhfsplus_fill_super()can process it.
Hi Slava,
Thanks for the review.
Regarding the suggestion to convert hfs_btree_open() to return ERR_PTR(-EROFS):
I reviewed this, but I cannot use ERR_PTR for the corruption case because it would defeat a purpose of the patch (data recovery).
If hfs_btree_open() returns -EROFS, the caller hfsplus_fill_super() would receive the error code but would have no tree object to work with. Without the B-tree structure, we cannot mount the filesystem-even read-only-making data recovery impossible.
To support recovery, hfs_btree_open() must return a valid tree pointer even when corruption is detected.
Therefore, for v3, I plan to:
-Keep the return type as-is to avoid scope creep and ensure easy
backporting.
-Use sb->s_flags |= SB_RDONLY inside hfs_btree_open() to flag the
safety issue.
-Drop the bool flag I added in v2 (as you requested) and simply
check sb_rdonly(sb) in fill_super to print the warning.
I will, of course, address your other comments regarding named constants and pointer arithmetic in v3.
Does this sound acceptable?
Thanks,
Shardul
Quoted 46 lines · Shardul Bankar
Quoted 27 lines · Viacheslav Dubeyko
Quoted 11 lines · Shardul Bankar
fs/hfsplus/btree.c +0
-0@@ -176,6 +238,13 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) tree->max_key_len = be16_to_cpu(head->max_key_len); tree->depth = be16_to_cpu(head->depth);
+ /* Validate bitmap: node 0 must be marked allocated */ + if (hfsplus_validate_btree_bitmap(tree, head)) { + structhfsplus_sb_info*sbi =HFSPLUS_SB(sb); +
+ sbi->btree_bitmap_corrupted = true;Please, see my comment about this field.
Quoted 12 lines
+ }
+
/* Verify the tree and set the correct compare function */ switch (id) {
caseHFSPLUS_EXT_CNID:fs/hfsplus/hfsplus_fs.h +0
-0@@ -154,6 +154,7 @@ struct hfsplus_sb_info {
int part, session;Quoted 8 lines
Quoted 3 lines
unsigned long flags;
+ bool btree_bitmap_corrupted; /* Bitmap corruption detected during btree open */This field is completely unnecessary. The
hfs_btree_open()can return-EROFS
error code andhfsplus_fill_super()can process it.Hi Slava,
Thanks for the review.
Regarding the suggestion to convert
hfs_btree_open()to returnERR_PTR(-EROFS):I reviewed this, but I cannot use
ERR_PTRfor the corruption case because it would defeat a purpose of the patch (data recovery).
Quoted 7 lines
If
hfs_btree_open()returns-EROFS, the callerhfsplus_fill_super()would receive the error code but would have no tree object to work with. Without the B-tree structure, we cannot mount the filesystem-even read-only-making data recovery impossible.To support recovery,
hfs_btree_open()must return a valid tree pointer even when corruption is detected.
Yeah, I missed this point. You are correct here.
Quoted 8 lines
Therefore, for v3, I plan to:
-Keep the return type as-is to avoid scope creep and ensure easy
backporting.-Use sb->s_flags |= SB_RDONLY inside hfs_btree_open() to flag thesafety issue.
Sounds like a better option.
Quoted 3 lines
-Drop the bool flag I added in v2 (as you requested) and simply
checksb_rdonly(sb)infill_superto print the warning.
Sounds good.
Quoted 5 lines
I will, of course, address your other comments regarding named constants and pointer arithmetic in v3.
Does this sound acceptable?
Yes, it sounds reasonably well. I like your suggestions.
Thanks,
Slava.