hfs: port HFS+ b-tree bitmap corruption check
Subsystems (3)
PATCH v5 1/1 hfs: port HFS+ b-tree bitmap corruption check
In HFS+ filesystems, during b-tree open (hfs_btree_open()), the code verifies that the allocation map bit for the tree header (node 0) is set. If not, it indicates a corrupted map record/bitmap and mounts the volume as read-only (SB_RDONLY) to prevent further damage.
HFS filesystems share the same b-tree structure but currently lack this corruption detection check.
Port this check to HFS, aligning its implementation with HFS+ to maintain consistent b-tree logic across both filesystems:
1. Define struct hfs_bmap_ctx, and define HFS_TREE_HEAD and the relevant map record indices in include/linux/hfs_common.h.
2. Implement static hfs_bmap_get_map_page() in fs/hfs/btree.c, and port the necessary offset and length validation helpers to fs/hfs/btree.h as static inline functions with robust null pointer checks.
3. Implement static hfs_bmap_test_bit() in fs/hfs/btree.c to inspect the B-tree bitmap using the new get_map_page helper.
4. In hfs_btree_open(), retrieve the header node via hfs_bnode_find(), test its allocation bit with hfs_bmap_test_bit(), and release it using hfs_bnode_put().
--- fs/hfs/bnode.c | 42 ------------------- fs/hfs/btree.c | 84 ++++++++++++++++++++++++++++++++++++++ fs/hfs/btree.h | 40 ++++++++++++++++++ include/linux/hfs_common.h | 13 ++++-- 4 files changed, 134 insertions(+), 45 deletions(-)
fs/hfs/bnode.c +0 -42
@@ -15,48 +15,6 @@#include "btree.h" -static inline -bool is_bnode_offset_valid (struct hfs_bnode *node,u32 off) -{ -bool is_valid = off < node->tree->node_size; - -if (!is_valid) { -pr_err ("requested invalid offset: " -"NODE: id %u, type %#x, height %u, " -"node_size %u, offset %u\n" , - node->this, node->type, node->height, - node->tree->node_size, off); - } - -return is_valid; -} - -static inline -u32 check_and_correct_requested_length (struct hfs_bnode *node,u32 off,u32 len) -{ -unsigned int node_size; - -if (!is_bnode_offset_valid (node, off)) -return 0 ; - - node_size = node->tree->node_size; - -if ((u64 )off + len > node_size) { -u32 new_len = node_size - off; - -pr_err ("requested length has been corrected: " -"NODE: id %u, type %#x, height %u, " -"node_size %u, offset %u, " -"requested_len %u, corrected_len %u\n" , - node->this, node->type, node->height, - node->tree->node_size, off, len, new_len); - -return new_len; - } - -return len; -} -void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len) {struct page *page;
fs/hfs/btree.c +84 -0
102 lines not on this page · raw message
fs/hfs/btree.h +40 -0
@@ -129,3 +129,43 @@ extern int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd);extern int hfs_brec_find (struct hfs_find_data *fd);extern int hfs_brec_read (struct hfs_find_data *fd,void *rec,u32 rec_len);extern int hfs_brec_goto (struct hfs_find_data *fd,int cnt); + +static inline bool is_bnode_offset_valid (struct hfs_bnode *node,u32 off) +{ +bool is_valid; + +if (!node || !node->tree) +return false; + + is_valid = off < node->tree->node_size; + +if (!is_valid) { +pr_err ("invalid offset: id %u, type %x, h %u, sz %u, off %u\n" , + node->this, node->type, node->height, + node->tree->node_size, off); + } + +return is_valid; +} + +static inline u32 check_and_correct_requested_length (struct hfs_bnode *node,u32 off,u32 len) +{ +unsigned int node_size; + +if (!is_bnode_offset_valid (node, off)) +return 0 ; + + node_size = node->tree->node_size; + +if ((u64 )off + len > node_size) { +u32 new_len = node_size - off; + +pr_err ("corrected len: id %u, type %x, h %u, sz %u, off %u, len %u->%u\n" , + node->this, node->type, node->height, + node_size, off, len, new_len); + +return new_len; + } + +return len; +}
include/linux/hfs_common.h +10 -3
@@ -510,14 +510,21 @@ struct hfs_btree_header_rec {b-tree but not in extents b-tree (hfsplus). */ +/* HFS BTree misc info */ +#define HFS_TREE_HEAD 0 +#define HFS_BTREE_HDR_MAP_REC_INDEX 2/* Map (bitmap) record in Header node */ +#define HFS_BTREE_MAP_NODE_REC_INDEX 0/* Map record in Map Node */ +/* HFS+ BTree misc info */ -#define HFSPLUS_TREE_HEAD 0 +#define HFSPLUS_TREE_HEAD HFS_TREE_HEAD#define HFSPLUS_NODE_MXSZ 32768#define HFSPLUS_NODE_MINSZ 512#define HFSPLUS_ATTR_TREE_NODE_SIZE 8192#define HFSPLUS_BTREE_HDR_NODE_RECS_COUNT 3 -#define HFSPLUS_BTREE_HDR_MAP_REC_INDEX 2/* Map (bitmap) record in Header node */ -#define HFSPLUS_BTREE_MAP_NODE_REC_INDEX 0/* Map record in Map Node */ +/* Map (bitmap) record in Header node */ +#define HFSPLUS_BTREE_HDR_MAP_REC_INDEX HFS_BTREE_HDR_MAP_REC_INDEX +/* Map record in Map Node */ +#define HFSPLUS_BTREE_MAP_NODE_REC_INDEX HFS_BTREE_MAP_NODE_REC_INDEX#define HFSPLUS_BTREE_HDR_USER_BYTES 128#define HFSPLUS_BTREE_MAP_NODE_RECS_COUNT 2#define HFSPLUS_BTREE_MAP_NODE_RESERVED_BYTES 2
Signature · 1 line
2.47.3
Viacheslav Dubeyko
maintainerQuoted 104 lines · Aditya Prakash Srivastava
In HFS+ filesystems, during b-tree open (
hfs_btree_open()), the code verifies that the allocation map bit for the tree header (node 0) is set. If not, it indicates a corrupted map record/bitmap and mounts the volume as read-only (SB_RDONLY) to prevent further damage.HFS filesystems share the same b-tree structure but currently lack this corruption detection check.
Port this check to HFS, aligning its implementation with HFS+ to maintain consistent b-tree logic across both filesystems:
1. Define structhfs_bmap_ctx, and defineHFS_TREE_HEADand the relevant
map record indices ininclude/linux/hfs_common.h.
2. Implement statichfs_bmap_get_map_page()infs/hfs/btree.c, and port
the necessary offset and length validation helpers tofs/hfs/btree.h
as static inline functions with robust null pointer checks.
3. Implement statichfs_bmap_test_bit()infs/hfs/btree.cto inspect the B-tree bitmap using the newget_map_pagehelper.
4. Inhfs_btree_open(), retrieve the header node viahfs_bnode_find(),
test its allocation bit withhfs_bmap_test_bit(), and release it usinghfs_bnode_put().Suggested-by Viacheslav Dubeyko <[email protected]>Link:
https://lore.kernel.org/all/[email protected]/T/#r446d0fed2a2900bd805534bbcb799d86619ae2eaSigned-off-by Aditya Prakash Srivastava <[email protected]>---
fs/hfs/bnode.c | 42 ------------------- fs/hfs/btree.c | 84
++++++++++++++++++++++++++++++++++++++
fs/hfs/btree.h | 40 ++++++++++++++++++include/linux/hfs_common.h| 13 ++++--4 files changed, 134 insertions(+), 45 deletions(-)fs/hfs/bnode.c +0
-0@@ -15,48 +15,6 @@
#include "btree.h"fs/hfs/bnode.c +0
-18-static inline -bool is_bnode_offset_valid (struct hfs_bnode *node,u32 off) -{ -bool is_valid = off < node->tree->node_size; - -if (!is_valid) { -pr_err ("requested invalid offset: " - "NODE: id %u, type %#x, height %u, " - "node_size %u, offset %u\n", - node->this, node->type, node->height, - node->tree->node_size, off); - } - -return is_valid; -} - -static inline -u32 check_and_correct_requested_length(struct hfs_bnode *node, u32off, u32 len)
fs/hfs/bnode.c +0
-24-{ -unsigned int node_size; - -if (!is_bnode_offset_valid (node, off)) -return 0 ; - - node_size = node->tree->node_size; - -if ((u64 )off + len > node_size) { -u32 new_len = node_size - off; - -pr_err ("requested length has been corrected: " - "NODE: id %u, type %#x, height %u, " - "node_size %u, offset %u, " - "requested_len %u, corrected_len %u\n", - node->this, node->type, node->height, - node->tree->node_size, off, len, new_len); - -return new_len; - } - -return len; -} -void
hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len)
{
struct page *page;fs/hfs/btree.c +0
-0@@ -15,6 +15,75 @@
#include "btree.h"fs/hfs/btree.c +4
-0+/* Context for iterating b-tree map pages +* @page_idx: The index of the page within the b-node's page array +* @off: The byte offset within the mapped page +* @len: The remaining length of the map record
fs/hfs/btree.c line 31
Quoted 10 lines
fs/hfs/btree.c +10
-0+*/ +struct hfs_bmap_ctx { +unsigned int page_idx; +unsigned int off; +u16 len; +}; + +static struct page *hfs_bmap_get_map_page (struct hfs_bnode *node, + struct hfs_bmap_ctx *ctx, + u32 byte_offset)
We have really nice comments of functions [3,4] for the case of HFS+. I believe it makes to keep the comments for the case of HFS too.
Mostly, patch looks good. However, we have one critical issue in the patch. Current code uses hfs_bmap_get_map_page() only in hfs_bmap_test_bit(). But, we have the same logic in hfs_bmap_alloc() [1, 2]. It is not good to have the same logic implemented several times in different places. Also, hfs_bmap_free() could benefit from hfs_bmap_clear_bit() function. Could you please improve your patch?
Quoted 118 lines
fs/hfs/btree.c +26
-0+{ +u16 rec_idx, off16; +unsigned int page_off; + +if (node->this ==HFS_TREE_HEAD ) { +if (node->type !=HFS_NODE_HEADER ) { +pr_err ("hfs: invalid btree header node\n" ); +return ERR_PTR (-EIO ); + } + rec_idx =HFS_BTREE_HDR_MAP_REC_INDEX ; + }else { +if (node->type !=HFS_NODE_MAP ) { +pr_err ("hfs: invalid btree map node\n" ); +return ERR_PTR (-EIO ); + } + rec_idx =HFS_BTREE_MAP_NODE_REC_INDEX ; + } + + ctx->len =hfs_brec_lenoff (node, rec_idx, &off16); +if (!ctx->len) +return ERR_PTR (-ENOENT ); + +if (!is_bnode_offset_valid (node, off16)) +return ERR_PTR (-EIO ); + + ctx->len = check_and_correct_requested_length(node, off16,
ctx->len);fs/hfs/btree.c +12
-0+ +if (byte_offset >= ctx->len) +return ERR_PTR (-EINVAL ); + + page_off = (u32 )off16 + node->page_offset + byte_offset; + ctx->page_idx = page_off >>PAGE_SHIFT ; + ctx->off = page_off & ~PAGE_MASK ; + +return node->page[ctx->page_idx]; +} + +static bool hfs_bmap_test_bit(struct hfs_bnode *node, u32
node_bit_idx)fs/hfs/btree.c +6
-0+{ +struct hfs_bmap_ctx ctx; +struct page *page; +u8 *bmap, byte, mask; + + page = hfs_bmap_get_map_page(node, &ctx, node_bit_idx /
BITS_PER_BYTE);fs/hfs/btree.c +11
-0+if (IS_ERR (page)) +return false; + + bmap =kmap_local_page (page); + byte = bmap[ctx.off]; +kunmap_local (bmap); + + mask =1 << (7 - (node_bit_idx %BITS_PER_BYTE )); +return (byte & mask) !=0 ; +} +/* Get a reference to a B*Tree and do some initial checks */ struct
hfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,btree_keycmpkeycmp)
{fs/hfs/btree.c +1
-0@@ -23,6 +92,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke struct address_space *mapping; struct folio *folio; struct buffer_head *bh; +struct hfs_bnode *node; unsigned int size; u16 dblock;
sector_tstart_block;fs/hfs/btree.c +0
-0@@ -155,6 +225,20 @@ struct hfs_btree *hfs_btree_open(struct
super_block*sb, u32 id,btree_keycmpke
kunmap_local(head);
folio_unlock(folio);
folio_put(folio);fs/hfs/btree.c +7
-0+ + node =hfs_bnode_find (tree,HFS_TREE_HEAD ); +if (IS_ERR (node)) + goto free_inode; + +if (!hfs_bmap_test_bit (node,HFS_TREE_HEAD )) { + pr_warn("(%s): %s (cnid 0x%x) bitmap corrupted,forcing rdonly\n",
fs/hfs/btree.c +1
-0+ sb->s_id, id == HFS_EXT_CNID ? "extents" :"catalog", id);
fs/hfs/btree.c +6
-0+pr_warn ("Run fsck.hfs to repair.\n" ); + sb->s_flags |=SB_RDONLY ; + } + +hfs_bnode_put (node); +return tree;
fail_folio:fs/hfs/btree.h +0
-0@@ -129,3 +129,43 @@ extern int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd); extern int hfs_brec_find(struct hfs_find_data *fd);extern int
hfs_brec_read(struct hfs_find_data *fd, void *rec, u32 rec_len);
extern inthfs_brec_goto(struct hfs_find_data *fd, int cnt);fs/hfs/btree.h +2
-0+ +static inline bool is_bnode_offset_valid(struct hfs_bnode *node, u32off)
fs/hfs/btree.h +10
-0+{ +bool is_valid; + +if (!node || !node->tree) +return false; + + is_valid = off < node->tree->node_size; + +if (!is_valid) { + pr_err("invalid offset: id %u, type %x, h %u, sz %u,
Quoted 1 line
off %u\n",
type %#x
Quoted 21 lines
fs/hfs/btree.h +8
-0+ node->this, node->type, node->height, + node->tree->node_size, off); + } + +return is_valid; +} + +static inline u32 check_and_correct_requested_length(struct
hfs_bnode*node, u32 off, u32 len)fs/hfs/btree.h +12
-0+{ +unsigned int node_size; + +if (!is_bnode_offset_valid (node, off)) +return 0 ; + + node_size = node->tree->node_size; + +if ((u64 )off + len > node_size) { +u32 new_len = node_size - off; + + pr_err("corrected len: id %u, type %x, h %u, sz %u,
Quoted 1 line
off %u, len %u->%u\n",
type %#x
Quoted 41 lines
fs/hfs/btree.h +8
-0+ node->this, node->type, node->height, + node_size, off, len, new_len); + +return new_len; + } + +return len; +}include/linux/hfs_common.h +0
-0@@ -510,14 +510,21 @@ struct hfs_btree_header_rec {b-tree but not in extents
b-tree (hfsplus). */include/linux/hfs_common.h +3
-0+/* HFS BTree misc info */ +#define HFS_TREE_HEAD 0 +#define HFS_BTREE_HDR_MAP_REC_INDEX 2/* Map (bitmap) record in Header node */
+#defineHFS_BTREE_MAP_NODE_REC_INDEX0 /* Map
record in Map Node */
+
/* HFS+ BTree misc info */include/linux/hfs_common.h +1
-1-#define HFSPLUS_TREE_HEAD 0 +#define HFSPLUS_TREE_HEAD HFS_TREE_HEAD#define
HFSPLUS_NODE_MXSZ32768
#defineHFSPLUS_NODE_MINSZ512
#defineHFSPLUS_ATTR_TREE_NODE_SIZE8192
#defineHFSPLUS_BTREE_HDR_NODE_RECS_COUNT3
-#defineHFSPLUS_BTREE_HDR_MAP_REC_INDEX2 /*
Map (bitmap) record in Header node */
-#defineHFSPLUS_BTREE_MAP_NODE_REC_INDEX0 /* Map
record in Map Node */include/linux/hfs_common.h +2
-0+/* Map (bitmap) record in Header node */ +#define
HFSPLUS_BTREE_HDR_MAP_REC_INDEXHFS_BTREE_HDR_MAP_REC_INDEXinclude/linux/hfs_common.h +2
-0+/* Map record in Map Node */ +#define
Quoted 4 lines
HFSPLUS_BTREE_MAP_NODE_REC_INDEXHFS_BTREE_MAP_NODE_REC_INDEX
#defineHFSPLUS_BTREE_HDR_USER_BYTES128
#defineHFSPLUS_BTREE_MAP_NODE_RECS_COUNT2
#defineHFSPLUS_BTREE_MAP_NODE_RESERVED_BYTES2
Thanks,
Slava.
[1]
https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfs/btree.c#L307
[2]
https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfs/btree.c#L352
[3]
https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/btree.c#L144
[4]
https://elixir.bootlin.com/linux/v7.2-rc3/source/fs/hfsplus/btree.c#L190