hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
v3 cover hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()
hfs_bnode_read_key() checks the on-disk key length against the size of the key union, but the buffers from hfs_find_init() are only max_key_len + 2 bytes each. A crafted extents leaf key overflows them.
Changes since v2, following Viacheslav's review:
- The literal 14 (node descriptor size) is replaced with HFS_BNODE_DESC_SIZE, sizeof(struct hfs_bnode_desc), in a new patch 1/3 with no functional change.
- "max_key_len * 2 + 4" is written with named constants:
HFS_FIND_KEY_BUFS * (max_key_len + HFSPLUS_KEY_LEN_SIZE) for hfsplus, where HFSPLUS_KEY_LEN_SIZE is the size of the __be16 key_len field, and HFS_KEY_LEN_MAX_SIZE for hfs. The allocation size is unchanged.
- struct hfs_find_data has a comment before it and one on every field.
- The new field is in the "filled by caller" section, next to search_key and key.
- key_size is renamed to key_buf_size.
Tested on 7.4-rc3 with KASAN in QEMU: the crafted images are refused with "Invalid key length: 516" and there is no KASAN report. An image made by mkfs.hfsplus survives create, rename, delete, xattrs, appends and remount, and fsck.hfsplus reports it clean. hfs is compile-tested only. The series applies to mainline and to hfs.git for-next.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
ozgun kultekin (3):
hfs/hfsplus: add HFS_BNODE_DESC_SIZE for the node descriptor size
hfsplus: pass the key buffer size to hfs_bnode_read_key()
hfs: pass the key buffer size to hfs_bnode_read_key()
fs/hfs/bfind.c | 5 +++-- fs/hfs/bnode.c | 7 ++++--- fs/hfs/brec.c | 36 ++++++++++++++++++++++++------------ fs/hfs/btree.c | 4 ++-- fs/hfs/btree.h | 31 +++++++++++++++++++++++-------- fs/hfsplus/bfind.c | 5 +++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 35 +++++++++++++++++++++++------------ fs/hfsplus/btree.c | 4 ++-- fs/hfsplus/hfsplus_fs.h | 28 ++++++++++++++++++++-------- include/linux/hfs_common.h | 3 +++ 11 files changed, 111 insertions(+), 54 deletions(-)
Signature · 1 line
2.39.5
PATCH v3 1/3 hfs/hfsplus: add HFS_BNODE_DESC_SIZE for the node descriptor size
brec.c and btree.c in both drivers use the literal 14 for the size of struct hfs_bnode_desc, which is also the offset of record 0 in a node. Add HFS_BNODE_DESC_SIZE next to the structure and use it instead.
No functional change. The next two patches use the new name in the hfs_bnode_read_key() calls they change.
---
v3: new patch, as requested by Viacheslav
fs/hfs/brec.c | 32 +++++++++++++++++++++----------- fs/hfs/btree.c | 4 ++-- fs/hfsplus/brec.c | 31 ++++++++++++++++++++----------- fs/hfsplus/btree.c | 4 ++-- include/linux/hfs_common.h | 3 +++ 5 files changed, 48 insertions(+), 26 deletions(-)
fs/hfs/brec.c +21 -11
@@ -156,7 +156,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len =sizeof (cnid);/* get index key */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, +HFS_BNODE_DESC_SIZE );__hfs_brec_find (fd->bnode, fd);hfs_bnode_put (new_node);
@@ -402,7 +403,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)/* size difference between old and new key */ if (tree->attributes &HFS_TREE_VARIDXKEYS ) - newkeylen = (hfs_bnode_read_u8 (node,14 ) |1 ) +1 ; + newkeylen = (hfs_bnode_read_u8 (node, +HFS_BNODE_DESC_SIZE ) |1 ) +1 ;else fd->keylength = newkeylen = tree->max_key_len +1 ; hfs_dbg("rec %d, keylength %d, newkeylen %d\n",
@@ -441,7 +443,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)hfs_bnode_move (parent, start_off + diff, start_off, end_off - start_off); skip: -hfs_bnode_copy (parent, fd->keyoffset, node,14 , newkeylen); +hfs_bnode_copy (parent, fd->keyoffset, node,HFS_BNODE_DESC_SIZE , + newkeylen);if (!(tree->attributes &HFS_TREE_VARIDXKEYS ))hfs_bnode_write_u8 (parent, fd->keyoffset, newkeylen -1 );hfs_bnode_dump (parent);
@@ -458,7 +461,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, +HFS_BNODE_DESC_SIZE ); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd);
@@ -470,7 +474,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)if (new_node == node) goto out;/* restore search_key */ -hfs_bnode_read_key (node, fd->search_key,14 ); +hfs_bnode_read_key (node, fd->search_key, +HFS_BNODE_DESC_SIZE ); } new_node =NULL ; }
@@ -524,28 +529,33 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)hfs_bnode_write (new_node, &node_desc,0 ,sizeof (node_desc)); rec = tree->node_size -2 ; -hfs_bnode_write_u16 (new_node, rec,14 ); +hfs_bnode_write_u16 (new_node, rec,HFS_BNODE_DESC_SIZE );if (node) {/* insert old root idx into new root */ node->parent = tree->root;if (node->type ==HFS_NODE_LEAF || tree->attributes &HFS_TREE_VARIDXKEYS ) - key_size =hfs_bnode_read_u8 (node,14 ) +1 ; + key_size =hfs_bnode_read_u8 (node, +HFS_BNODE_DESC_SIZE ) +1 ;else key_size = tree->max_key_len +1 ; -hfs_bnode_copy (new_node,14 , node,14 , key_size); +hfs_bnode_copy (new_node,HFS_BNODE_DESC_SIZE , node, +HFS_BNODE_DESC_SIZE , key_size);if (!(tree->attributes &HFS_TREE_VARIDXKEYS )) { key_size = tree->max_key_len +1 ; -hfs_bnode_write_u8 (new_node,14 , tree->max_key_len); +hfs_bnode_write_u8 (new_node,HFS_BNODE_DESC_SIZE , + tree->max_key_len); } key_size = (key_size +1 ) &-2 ; cnid =cpu_to_be32 (node->this); -hfs_bnode_write (new_node, &cnid,14 + key_size,4 ); +hfs_bnode_write (new_node, &cnid, +HFS_BNODE_DESC_SIZE + key_size,4 ); rec -=2 ; -hfs_bnode_write_u16 (new_node, rec,14 + key_size +4 ); +hfs_bnode_write_u16 (new_node, rec, +HFS_BNODE_DESC_SIZE + key_size +4 );hfs_bnode_put (node); }
fs/hfs/btree.c +2 -2
@@ -356,8 +356,8 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs =cpu_to_be16 (1 ); desc.reserved =0 ;hfs_bnode_write (node, &desc,0 ,sizeof (desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_DESC_SIZE ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 ,HFS_BNODE_DESC_SIZE );hfs_bnode_write_u16 (node, tree->node_size -4 , tree->node_size -6 );return node;
fs/hfsplus/brec.c +20 -11
@@ -166,7 +166,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len =sizeof (cnid);/* get index key */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, +HFS_BNODE_DESC_SIZE );__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);hfs_bnode_put (new_node);
@@ -409,7 +410,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)/* size difference between old and new key */ if ((tree->attributes &HFS_TREE_VARIDXKEYS ) || (tree->cnid ==HFSPLUS_ATTR_CNID )) - newkeylen =hfs_bnode_read_u16 (node,14 ) +2 ; + newkeylen =hfs_bnode_read_u16 (node,HFS_BNODE_DESC_SIZE ) +2 ;else fd->keylength = newkeylen = tree->max_key_len +2 ; hfs_dbg("rec %d, keylength %d, newkeylen %d\n",
@@ -449,7 +450,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)hfs_bnode_move (parent, start_off + diff, start_off, end_off - start_off); skip: -hfs_bnode_copy (parent, fd->keyoffset, node,14 , newkeylen); +hfs_bnode_copy (parent, fd->keyoffset, node,HFS_BNODE_DESC_SIZE , + newkeylen);hfs_bnode_dump (parent);hfs_bnode_put (node);
@@ -464,7 +466,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, +HFS_BNODE_DESC_SIZE ); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);
@@ -476,7 +479,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)if (new_node == node) goto out;/* restore search_key */ -hfs_bnode_read_key (node, fd->search_key,14 ); +hfs_bnode_read_key (node, fd->search_key, +HFS_BNODE_DESC_SIZE ); } new_node =NULL ; }
@@ -530,7 +534,7 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)hfs_bnode_write (new_node, &node_desc,0 ,sizeof (node_desc)); rec = tree->node_size -2 ; -hfs_bnode_write_u16 (new_node, rec,14 ); +hfs_bnode_write_u16 (new_node, rec,HFS_BNODE_DESC_SIZE );if (node) {/* insert old root idx into new root */
@@ -538,21 +542,26 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)if (node->type ==HFS_NODE_LEAF || tree->attributes &HFS_TREE_VARIDXKEYS || tree->cnid ==HFSPLUS_ATTR_CNID ) - key_size =hfs_bnode_read_u16 (node,14 ) +2 ; + key_size =hfs_bnode_read_u16 (node, +HFS_BNODE_DESC_SIZE ) +2 ;else key_size = tree->max_key_len +2 ; -hfs_bnode_copy (new_node,14 , node,14 , key_size); +hfs_bnode_copy (new_node,HFS_BNODE_DESC_SIZE , node, +HFS_BNODE_DESC_SIZE , key_size);if (!(tree->attributes &HFS_TREE_VARIDXKEYS ) && (tree->cnid !=HFSPLUS_ATTR_CNID )) { key_size = tree->max_key_len +2 ; -hfs_bnode_write_u16 (new_node,14 , tree->max_key_len); +hfs_bnode_write_u16 (new_node,HFS_BNODE_DESC_SIZE , + tree->max_key_len); } cnid =cpu_to_be32 (node->this); -hfs_bnode_write (new_node, &cnid,14 + key_size,4 ); +hfs_bnode_write (new_node, &cnid, +HFS_BNODE_DESC_SIZE + key_size,4 ); rec -=2 ; -hfs_bnode_write_u16 (new_node, rec,14 + key_size +4 ); +hfs_bnode_write_u16 (new_node, rec, +HFS_BNODE_DESC_SIZE + key_size +4 );hfs_bnode_put (node); }
fs/hfsplus/btree.c +2 -2
@@ -497,8 +497,8 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs =cpu_to_be16 (1 ); desc.reserved =0 ;hfs_bnode_write (node, &desc,0 ,sizeof (desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_DESC_SIZE ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 ,HFS_BNODE_DESC_SIZE );hfs_bnode_write_u16 (node, tree->node_size -4 , tree->node_size -6 );return node;
include/linux/hfs_common.h +3 -0
@@ -495,6 +495,9 @@ struct hfs_bnode_desc {u16 reserved; } __packed; +/* Size of the node descriptor, which is also the offset of record 0 */ +#define HFS_BNODE_DESC_SIZE sizeof(struct hfs_bnode_desc) +/* HFS/HFS+ BTree node types */ #define HFS_NODE_INDEX 0x00/* An internal (index) node */ #define HFS_NODE_HEADER 0x01/* The tree header node (node 0) */
Signature · 1 line
2.39.5
Viacheslav Dubeyko
maintainerRe: [PATCH v3 1/3] hfs/hfsplus: add HFS_BNODE_DESC_SIZE for the node descriptor size
Quoted 6 lines · ozgun kultekin
brec.candbtree.cin both drivers use the literal 14 for the size of structhfs_bnode_desc, which is also the offset of record 0 in a node.
AddHFS_BNODE_DESC_SIZEnext to the structure and use it instead.No functional change. The next two patches use the new name in the
Usually, mentioning "next two patches" is not good phrase. Because, it means nothing once the series is merged.
Quoted 4 lines
hfs_bnode_read_key()calls they change.Suggested-by Viacheslav Dubeyko <[email protected]>Cc [email protected] # prerequisite for the next two patches
The include/linux/hfs_common.h has been introduced pretty recently v6.19-rc1 (ec95cd103c3a). Older stable trees declare it elsewhere, so this patch won't apply there.
Probably, we need to reorder the series so the fixes come first with
Cc: stable, and this cleanup last without it, because the header only exists from v6.19.
Quoted 34 lines
Signed-off-by ozgun kultekin <[email protected]>---
v3: new patch, as requested by Viacheslavfs/hfs/brec.c | 32 +++++++++++++++++++++----------- fs/hfs/btree.c | 4 ++--
fs/hfsplus/brec.c | 31 ++++++++++++++++++++----------- fs/hfsplus/btree.c | 4 ++--include/linux/hfs_common.h| 3 +++5 files changed, 48 insertions(+), 26 deletions(-)fs/hfs/brec.c +0
-0@@ -156,7 +156,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len = sizeof(cnid);
/* get index key */fs/hfs/brec.c +2
-1-hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, + HFS_BNODE_DESC_SIZE);
__hfs_brec_find(fd->bnode, fd);
hfs_bnode_put(new_node);fs/hfs/brec.c +0
-0@@ -402,7 +403,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
/* size difference between old and new key */ if (tree->attributes&HFS_TREE_VARIDXKEYS)fs/hfs/brec.c +2
-1- newkeylen = (hfs_bnode_read_u8 (node,14 ) |1 ) +1 ; + newkeylen = (hfs_bnode_read_u8 (node, + HFS_BNODE_DESC_SIZE)
Quoted 1 line
| 1) + 1;
This looks slightly weird. Maybe:
newkeylen =
(hfs_bnode_read_u8(node, HFS_BNODE_DESC_SIZE) | 1) + 1;
Quoted 55 lines
else
fd->keylength= newkeylen =tree->max_key_len+ 1; hfs_dbg("rec %d, keylength %d, newkeylen %d\n",fs/hfs/brec.c +0
-0@@ -441,7 +443,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) hfs_bnode_move(parent, start_off + diff, start_off, end_off - start_off);skip:
fs/hfs/brec.c +1
-1-hfs_bnode_copy (parent, fd->keyoffset, node,14 , newkeylen); + hfs_bnode_copy(parent, fd->keyoffset, node,
HFS_BNODE_DESC_SIZE,fs/hfs/brec.c +1
-0+ newkeylen);if (!(tree->attributes &
HFS_TREE_VARIDXKEYS)) hfs_bnode_write_u8(parent,fd->keyoffset, newkeylen
- 1);
hfs_bnode_dump(parent);fs/hfs/brec.c +2
-1@@ -458,7 +461,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode = hfs_bnode_find(tree, new_node->parent); /* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, + HFS_BNODE_DESC_SIZE); cnid = cpu_to_be32(new_node->this);
__hfs_brec_find(fd->bnode, fd);fs/hfs/brec.c +2
-1@@ -470,7 +474,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) if (new_node == node) goto out; /* restore search_key */ -hfs_bnode_read_key (node, fd->search_key, 14); +hfs_bnode_read_key (node, fd->search_key, + HFS_BNODE_DESC_SIZE); }
new_node= NULL;
}fs/hfs/brec.c +0
-0@@ -524,28 +529,33 @@ static int hfs_btree_inc_height(struct hfs_btree *tree) hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
rec =tree->node_size- 2;fs/hfs/brec.c +1
-1-hfs_bnode_write_u16 (new_node, rec,14 ); +hfs_bnode_write_u16 (new_node, rec,HFS_BNODE_DESC_SIZE );
if (node) {
/* insert old root idx into new root */node->parent=tree->root;
if (node->type==HFS_NODE_LEAF||tree->attributes&HFS_TREE_VARIDXKEYS)fs/hfs/brec.c +2
-1- key_size =hfs_bnode_read_u8 (node,14 ) +1 ; + key_size = hfs_bnode_read_u8(node, +
Quoted 1 line
HFS_BNODE_DESC_SIZE) + 1;
Maybe:
key_size =
hfs_bnode_read_u8(node, HFS_BNODE_DESC_SIZE) + 1;
Quoted 16 lines
else
key_size=tree->max_key_len+ 1;fs/hfs/brec.c +2
-1-hfs_bnode_copy (new_node,14 , node,14 , key_size); +hfs_bnode_copy (new_node,HFS_BNODE_DESC_SIZE , node, + HFS_BNODE_DESC_SIZE, key_size);
if (!(tree->attributes &HFS_TREE_VARIDXKEYS)) {key_size=tree->max_key_len+ 1;fs/hfs/brec.c +0
-1- hfs_bnode_write_u8(new_node, 14, tree-Quoted 1 line
max_key_len);fs/hfs/brec.c +1
-0+ hfs_bnode_write_u8(new_node,
HFS_BNODE_DESC_SIZE,fs/hfs/brec.c +1
-0+ tree->max_key_len);}
key_size= (key_size+ 1) & -2;
cnid =cpu_to_be32(node->this);
fs/hfs/brec.c static int hfs_btree_inc_height(struct hfs_btree *tree) · line 554
Quoted 3 lines
fs/hfs/brec.c +2
-1-hfs_bnode_write (new_node, &cnid,14 + key_size,4 ); +hfs_bnode_write (new_node, &cnid, +HFS_BNODE_DESC_SIZE + key_size,4 );
Maybe, introduce a local variable for HFS_BNODE_DESC_SIZE + key_size?
Quoted 7 lines
rec -= 2;fs/hfs/brec.c +0
-1- hfs_bnode_write_u16(new_node, rec, 14 + key_size +4);
fs/hfs/brec.c +2
-0+ hfs_bnode_write_u16(new_node, rec, + HFS_BNODE_DESC_SIZE + key_size +4);
Ditto. For HFS_BNODE_DESC_SIZE + key_size + 4?
Yeah, we cannot solve all code issues in one patch. But 4 looks not good too. Let's fix it step by step.
Quoted 8 lines
hfs_bnode_put(node);
}fs/hfs/btree.c +0
-0
fs/hfs/btree.c static struct hfs_bnode *hfs_bmap_new_bmap(struct · line 362
Quoted 9 lines
fs/hfs/btree.c +2
-2@@ -356,8 +356,8 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs = cpu_to_be16(1); desc.reserved = 0; hfs_bnode_write(node, &desc, 0, sizeof(desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_DESC_SIZE ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 ,HFS_BNODE_DESC_SIZE );
Do we have normal alignment here?
Quoted 84 lines
hfs_bnode_write_u16(node,
tree->node_size- 4, tree-Quoted 1 line
node_size- 6);
return node;fs/hfsplus/brec.c +0
-0@@ -166,7 +166,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len = sizeof(cnid);
/* get index key */fs/hfsplus/brec.c +2
-1-hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, + HFS_BNODE_DESC_SIZE);
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
hfs_bnode_put(new_node);fs/hfsplus/brec.c +0
-0@@ -409,7 +410,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) /* size difference between old and new key */if ((
tree->attributes&HFS_TREE_VARIDXKEYS) || (tree->cnid==HFSPLUS_ATTR_CNID))fs/hfsplus/brec.c +1
-1- newkeylen =hfs_bnode_read_u16 (node,14 ) +2 ; + newkeylen = hfs_bnode_read_u16(node,
HFS_BNODE_DESC_SIZE) + 2;
else
fd->keylength= newkeylen =tree->max_key_len+ 2; hfs_dbg("rec %d, keylength %d, newkeylen %d\n",fs/hfsplus/brec.c +0
-0@@ -449,7 +450,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) hfs_bnode_move(parent, start_off + diff, start_off, end_off - start_off);skip:
fs/hfsplus/brec.c +1
-1-hfs_bnode_copy (parent, fd->keyoffset, node,14 , newkeylen); + hfs_bnode_copy(parent, fd->keyoffset, node,
HFS_BNODE_DESC_SIZE,fs/hfsplus/brec.c +1
-0+ newkeylen);
hfs_bnode_dump(parent);
hfs_bnode_put(node);fs/hfsplus/brec.c +2
-1@@ -464,7 +466,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode = hfs_bnode_find(tree, new_node->parent); /* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key, + HFS_BNODE_DESC_SIZE); cnid = cpu_to_be32(new_node->this);
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);fs/hfsplus/brec.c +2
-1@@ -476,7 +479,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) if (new_node == node) goto out; /* restore search_key */ -hfs_bnode_read_key (node, fd->search_key, 14); +hfs_bnode_read_key (node, fd->search_key, + HFS_BNODE_DESC_SIZE); }
new_node= NULL;
}fs/hfsplus/brec.c +0
-0@@ -530,7 +534,7 @@ static int hfs_btree_inc_height(struct hfs_btree *tree) hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
rec =tree->node_size- 2;fs/hfsplus/brec.c +1
-1-hfs_bnode_write_u16 (new_node, rec,14 ); +hfs_bnode_write_u16 (new_node, rec,HFS_BNODE_DESC_SIZE );
if (node) {
/* insert old root idx into new root */fs/hfsplus/brec.c +0
-0@@ -538,21 +542,26 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)if (
node->type==HFS_NODE_LEAF||tree->attributes&HFS_TREE_VARIDXKEYS||
tree->cnid==HFSPLUS_ATTR_CNID)fs/hfsplus/brec.c +2
-1- key_size =hfs_bnode_read_u16 (node,14 ) +2 ; + key_size = hfs_bnode_read_u16(node, +
Quoted 1 line
HFS_BNODE_DESC_SIZE) + 2;
Maybe,
key_size =
hfs_bnode_read_u16(node, HFS_BNODE_DESC_SIZE) + 2;
Quoted 16 lines
else
key_size=tree->max_key_len+ 2;fs/hfsplus/brec.c +2
-1-hfs_bnode_copy (new_node,14 , node,14 , key_size); +hfs_bnode_copy (new_node,HFS_BNODE_DESC_SIZE , node, + HFS_BNODE_DESC_SIZE, key_size);
if (!(tree->attributes &HFS_TREE_VARIDXKEYS) && (tree->cnid!=HFSPLUS_ATTR_CNID)) {key_size=tree->max_key_len+ 2;fs/hfsplus/brec.c +0
-1- hfs_bnode_write_u16(new_node, 14, tree-Quoted 1 line
max_key_len);fs/hfsplus/brec.c +1
-0+ hfs_bnode_write_u16(new_node,
HFS_BNODE_DESC_SIZE,fs/hfsplus/brec.c +1
-0+ tree->max_key_len);}
cnid =cpu_to_be32(node->this);
fs/hfsplus/brec.c
Quoted 3 lines
fs/hfsplus/brec.c +2
-1-hfs_bnode_write (new_node, &cnid,14 + key_size,4 ); +hfs_bnode_write (new_node, &cnid, +HFS_BNODE_DESC_SIZE + key_size,4 );
Local variable?
Quoted 7 lines
rec -= 2;fs/hfsplus/brec.c +0
-1- hfs_bnode_write_u16(new_node, rec, 14 + key_size +4);
fs/hfsplus/brec.c +2
-0+ hfs_bnode_write_u16(new_node, rec, + HFS_BNODE_DESC_SIZE + key_size +4);
Local variable?
Quoted 8 lines
hfs_bnode_put(node);
}fs/hfsplus/btree.c +0
-0
fs/hfsplus/btree.c static struct hfs_bnode *hfs_bmap_new_bmap(struct · line 503
Quoted 9 lines
fs/hfsplus/btree.c +2
-2@@ -497,8 +497,8 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs = cpu_to_be16(1); desc.reserved = 0; hfs_bnode_write(node, &desc, 0, sizeof(desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_DESC_SIZE ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 ,HFS_BNODE_DESC_SIZE );
Normal alignment?
Quoted 11 lines
hfs_bnode_write_u16(node,
tree->node_size- 4, tree-Quoted 1 line
node_size- 6);
return node;include/linux/hfs_common.h +0
-0@@ -495,6 +495,9 @@ struct hfs_bnode_desc { u16 reserved; } __packed;
Quoted 4 lines
+/* Size of the node descriptor, which is also the offset of record 0 */
+#defineHFS_BNODE_DESC_SIZEsizeof(struct hfs_bnode_desc)
HFS_BNODE_DESC_SIZE describes the size, but most call sites use it as "offset of the first record". Two names would read better: a size constant plus something like:
#define HFS_BNODE_FIRST_REC_OFF HFS_BNODE_DESC_SIZE.
The same quantity is still spelled other ways in the same files: the local size_t node_desc_size = sizeof(struct hfs_bnode_desc) in hfs_bnode_split(), plus sizeof(struct hfs_bnode_desc) in bnode.c, btree.c and hfsplus/xattr.c.
Thanks,
Slava.
Quoted 5 lines
+
/* HFS/HFS+ BTree node types */
#defineHFS_NODE_INDEX0x00/* An internal (index) node */
#defineHFS_NODE_HEADER0x01/* The tree header node
(node 0) */
PATCH v3 2/3 hfsplus: pass the key buffer size to hfs_bnode_read_key()
In reply to ozgun kultekinhfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.
hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node. A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.
Do not let hfs_bnode_read_key() assume the size of the buffer. Store the size of each key buffer in struct hfs_find_data as key_buf_size in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.
The allocation size does not change: max_key_len * 2 + 4 is now written as HFS_FIND_KEY_BUFS * (max_key_len + HFSPLUS_KEY_LEN_SIZE). Document struct hfs_find_data while adding the field.
---
v3: key_size renamed to key_buf_size and moved to the "filled by caller"
part of struct hfs_find_data, struct and fields commented, buffer size written with named constants, as requested by Viacheslav
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
fs/hfsplus/bfind.c | 5 +++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 10 ++++++---- fs/hfsplus/hfsplus_fs.h | 28 ++++++++++++++++++++-------- 4 files changed, 33 insertions(+), 17 deletions(-)
fs/hfsplus/bfind.c +3 -2
@@ -19,11 +19,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode =NULL ;hfs_find_result_init (fd); - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL ); + fd->key_buf_size = tree->max_key_len +HFSPLUS_KEY_LEN_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_buf_size,GFP_KERNEL );if (!ptr)return -ENOMEM ; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_buf_size;hfs_dbg ("cnid %d, caller %ps\n" , tree->cnid,__builtin_return_address (0 )); mutex_lock_nested(&tree->tree_lock,
fs/hfsplus/bnode.c +4 -3
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)return data; } -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size) {struct hfs_btree *tree;u32 key_len;
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else key_len = tree->max_key_len +2 ; -if (key_len >sizeof (hfsplus_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfsplus_btree_key)); +if (key_len > key_buf_size || key_len <1 ) { +memset (key,0 , key_buf_size);pr_err ("hfsplus: Invalid key length: %u\n" , key_len);return ; }
fs/hfsplus/brec.c +6 -4
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)* at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_buf_size);hfs_brec_update_parent (fd); }
@@ -167,7 +168,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)/* get index key */ hfs_bnode_read_key (new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , fd->key_buf_size);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);hfs_bnode_put (new_node);
@@ -467,7 +468,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ hfs_bnode_read_key (new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , fd->key_buf_size); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);
@@ -480,7 +481,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) goto out;/* restore search_key */ hfs_bnode_read_key (node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , + fd->key_buf_size); } new_node =NULL ; }
fs/hfsplus/hfsplus_fs.h +20 -8
@@ -257,17 +257,28 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode,mark_inode_dirty (inode); } +/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* Size of the key_len field at the start of every key */ +#define HFSPLUS_KEY_LEN_SIZE sizeof_field(hfsplus_btree_key, key_len) + +/* +* State of one b-tree search. hfs_find_init() sets up the key buffers +* and the tree, the search functions fill in the rest. +*/ struct hfs_find_data {/* filled by caller */ -hfsplus_btree_key *search_key; -hfsplus_btree_key *key; +hfsplus_btree_key *search_key;/* key to search for */ +hfsplus_btree_key *key;/* key of the found record */ +u32 key_buf_size;/* size of each of search_key and key */ /* filled by find */ -struct hfs_btree *tree; -struct hfs_bnode *bnode; +struct hfs_btree *tree;/* tree being searched */ +struct hfs_bnode *bnode;/* node with the found record */ /* filled by findrec */ -int record; -int keyoffset, keylength; -int entryoffset, entrylength; +int record;/* index of the record in bnode */ +int keyoffset, keylength;/* offset and length of its key */ +int entryoffset, entrylength;/* offset and length of its data */ };struct hfsplus_readdir_data {
@@ -372,7 +383,8 @@ void hfs_bmap_free(struct hfs_bnode *node);void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len);u16 hfs_bnode_read_u16 (struct hfs_bnode *node,u32 off);u8 hfs_bnode_read_u8 (struct hfs_bnode *node,u32 off); -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size);void hfs_bnode_write (struct hfs_bnode *node,void *buf,u32 off,u32 len);void hfs_bnode_write_u16 (struct hfs_bnode *node,u32 off,u16 data);void hfs_bnode_clear (struct hfs_bnode *node,u32 off,u32 len);
Signature · 1 line
2.39.5
Viacheslav Dubeyko
maintainerRe: [PATCH v3 2/3] hfsplus: pass the key buffer size to hfs_bnode_read_key()
Quoted 62 lines · ozgun kultekin
hfs_bnode_read_key()reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it againstsizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffershfs_find_init()allocates, eachtree->max_key_len+ 2 bytes long. For the extents treemax_key_lenis fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.
hfs_brec_insert()reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node.
A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.Do not let
hfs_bnode_read_key()assume the size of the buffer. Store the size of each key buffer in structhfs_find_dataaskey_buf_sizeinhfs_find_init(), allocate the two buffers from it, and pass it to everyhfs_bnode_read_key()call. Keys longer than the buffer are rejected as
before, and the error path clears only the buffer.The allocation size does not change:
max_key_len* 2 + 4 is now written
asHFS_FIND_KEY_BUFS* (max_key_len+HFSPLUS_KEY_LEN_SIZE). Document structhfs_find_datawhile adding the field.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in
hfs_bnode_read_key")Reported-by Guikun Yang <[email protected]>Closes:
https://lore.kernel.org/all/[email protected]/Suggested-by Viacheslav Dubeyko <[email protected]>Signed-off-by ozgun kultekin <[email protected]>---
v3:key_sizerenamed tokey_buf_sizeand moved to the "filled by caller"
part of structhfs_find_data, struct and fields commented, buffer size written with named constants, as requested by Viacheslav v2:
https://lore.kernel.org/all/[email protected]/
v1:
https://lore.kernel.org/all/[email protected]/fs/hfsplus/bfind.c | 5 +++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 10 ++++++----
fs/hfsplus/hfsplus_fs.h| 28 ++++++++++++++++++++--------4 files changed, 33 insertions(+), 17 deletions(-)fs/hfsplus/bfind.c +0
-0
fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct · line 23
Quoted 5 lines
fs/hfsplus/bfind.c +0
-1@@ -19,11 +19,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode = NULL; hfs_find_result_init(fd); - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL );
OK. It looks like we try to reserve the space for two hfsplus_btree_key (search_key + key) and for two __be16 key_len. Correct? But any key struct hfsplus_cat_key, struct hfsplus_ext_key, struct hfsplus_attr_key already include __be16 key_len:
struct hfsplus_ext_key {
__be16 key_len;
u8 fork_type;
u8 pad;
hfsplus_cnid cnid;
__be32 start_block;
} __packed;Do we really need to add this 4 or sizeof_field(hfsplus_btree_key, key_len)? I assume that tree->max_key_len should include the size of key_len. Am I right?
Quoted 20 lines
fs/hfsplus/bfind.c +3
-1+ fd->key_buf_size = tree->max_key_len +HFSPLUS_KEY_LEN_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_buf_size,GFP_KERNEL ); if (!ptr) return -ENOMEM; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_buf_size; hfs_dbg("cnid %d, caller %ps\n",
tree->cnid,__builtin_return_address(0)); mutex_lock_nested(&tree->tree_lock,fs/hfsplus/bnode.c +0
-0@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) return data; }
fs/hfsplus/bnode.c u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) · line 75
Quoted 3 lines
fs/hfsplus/bnode.c +2
-1-void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size)
The key_capacity looks better.
Quoted 84 lines
{
structhfs_btree*tree;
u32key_len;fs/hfsplus/bnode.c +0
-0@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else
key_len=tree->max_key_len+ 2;fs/hfsplus/bnode.c +2
-2-if (key_len >sizeof (hfsplus_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfsplus_btree_key)); +if (key_len > key_buf_size || key_len <1 ) { +memset (key,0 , key_buf_size);
pr_err("hfsplus: Invalid key length: %u\n", key_len);
return;
}fs/hfsplus/brec.c +2
-1@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,void *entry, u32 entry_len) * at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_buf_size);
hfs_brec_update_parent(fd);
}fs/hfsplus/brec.c +0
-0@@ -167,7 +168,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
/* get index key */ hfs_bnode_read_key(new_node,fd->search_key,fs/hfsplus/brec.c +1
-1- HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, fd-Quoted 1 line
key_buf_size);
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
hfs_bnode_put(new_node);fs/hfsplus/brec.c +1
-1@@ -467,7 +468,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) fd->bnode = hfs_bnode_find(tree, new_node->parent); /* create index key and entry */ hfs_bnode_read_key(new_node, fd->search_key, - HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, fd- >key_buf_size); cnid = cpu_to_be32(new_node->this);
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);fs/hfsplus/brec.c +2
-1@@ -480,7 +481,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) goto out; /* restore search_key */ hfs_bnode_read_key(node, fd->search_key, - HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, + fd->key_buf_size); } new_node = NULL;}
fs/hfsplus/hfsplus_fs.h +0
-0@@ -257,17 +257,28 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode, mark_inode_dirty(inode); }fs/hfsplus/hfsplus_fs.h +5
-0+/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* Size of the key_len field at the start of every key */ +#define HFSPLUS_KEY_LEN_SIZE sizeof_field(hfsplus_btree_key,
key_len)fs/hfsplus/hfsplus_fs.h +3
-0+ +/* +* State of one b-tree search. hfs_find_init() sets up the key
Quoted 3 lines
buffers
+ * and the tree, the search functions fill in the rest.
+ */
I expected to have comments here but not inside the structure declarations.
Quoted 9 lines
struct
hfs_find_data{
/* filled by caller */fs/hfsplus/hfsplus_fs.h +2
-2-hfsplus_btree_key *search_key; -hfsplus_btree_key *key; +hfsplus_btree_key *search_key;/* key to search for */ +hfsplus_btree_key *key;/* key of the found record */
fs/hfsplus/hfsplus_fs.h +1
-0+u32 key_buf_size;/* size of each of
search_keyand key */
I think key_capacity could be better name, finally.
I spent some time for analysis. I think my request to put it here was not good suggestion. Putting u32 key_buf_size after the two pointers leaves a 4-byte hole, and pahole shows struct hfs_find_data growing from 56 to 64 bytes. Also, "filled by caller" heading was already wrong (hfs_find_init() fills these fields, and tree too).
Quoted 16 lines
/* filled by find */
fs/hfsplus/hfsplus_fs.h +1
-2-struct hfs_btree *tree; -struct hfs_bnode *bnode; +struct hfs_btree *tree;/* tree being searched */
fs/hfsplus/hfsplus_fs.h +1
-0+struct hfs_bnode *bnode;/* node with the found record */
/* filled by findrec */fs/hfsplus/hfsplus_fs.h +1
-3-int record; -int keyoffset, keylength; -int entryoffset, entrylength; +int record;/* index of the record in bnode */
fs/hfsplus/hfsplus_fs.h +1
-0+int keyoffset, keylength;/* offset and length of its key */
fs/hfsplus/hfsplus_fs.h +1
-0+int entryoffset, entrylength;/* offset and length of its
Quoted 2 lines
data */
};
I am thinking about this:
struct hfs_find_data {
/* set by hfs_find_init() */
hfsplus_btree_key *search_key;
hfsplus_btree_key *key;
struct hfs_btree *tree;
/* filled by find */
struct hfs_bnode *bnode;
/* set by hfs_find_init() */
u32 key_capacity;
/* filled by findrec, reset by hfs_find_result_init() */
int record;
int keyoffset, keylength;
int entryoffset, entrylength;
};Thanks,
Slava.
Quoted 14 lines
structhfsplus_readdir_data{fs/hfsplus/hfsplus_fs.h +0
-0@@ -372,7 +383,8 @@ void hfs_bmap_free(struct hfs_bnode *node);void
hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
u16hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); u8hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);fs/hfsplus/hfsplus_fs.h +2
-1-void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size);void
hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
voidhfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); voidhfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
Re: [PATCH v3 2/3] hfsplus: pass the key buffer size to hfs_bnode_read_key()
Hi Slava,
Thanks for the review. v4 follows; point by point:
- Order: the fixes are now 1/3 (hfsplus) and 2/3 (hfs) with Cc:
stable, and the HFS_BNODE_DESC_SIZE cleanup is 3/3 without it. The "next two patches" wording is gone.
- "+ 4": it is not double counting. The header's max_key_len does not include key_len. hfs_btree_open() requires HFSPLUS_*_KEYLEN - sizeof(u16) (fs/hfsplus/btree.c:318/331/350), and hfs_bnode_read_key() reads the on-disk length + 2. The extents tree has max_key_len 10, but a full key is 12 bytes. hfs is the same with a u8 length (HFS_MAX_*_KEYLEN = sizeof - sizeof(u8)). The + 2 stays, and the comment on the constant now says why.
- Name: key_buf_size is now key_capacity.
- Layout: I used your struct, with key_capacity after bnode. pahole gives 56 bytes before and after in both drivers, with no hole. Comments are now section headings only, with nothing inside the declarations.
- extern: kept on the hfs prototype, like its neighbours.
- hfs: the same changes as hfsplus.
- 3/3: added HFS_BNODE_FIRST_REC_OFF for offsets. node_desc_size, desc_size and sizeof(struct hfs_bnode_desc) are converted too. There is a local cnid_off, sizeof(cnid) instead of 4, line breaks after "=", and aligned conditions.
1/3 and 2/3 also apply to 6.12.y and 6.6.y except for one context line in hfs_find_init(), and the fixed-up backports build.
ozgun
Quoted 275 lines · Viacheslav Dubeyko
Quoted 62 lines · ozgun kultekin
hfs_bnode_read_key()reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks
it againstsizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffershfs_find_init()allocates, eachtree->max_key_len+ 2 bytes long. For the extents treemax_key_lenis fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key
writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.
hfs_brec_insert()reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node.
A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.Do not let
hfs_bnode_read_key()assume the size of the buffer. Store the size of each key buffer in structhfs_find_dataaskey_buf_sizeinhfs_find_init(), allocate the two buffers from it, and pass it to everyhfs_bnode_read_key()call. Keys longer than the buffer are rejected as
before, and the error path clears only the buffer.The allocation size does not change:
max_key_len* 2 + 4 is now written
asHFS_FIND_KEY_BUFS* (max_key_len+HFSPLUS_KEY_LEN_SIZE). Document structhfs_find_datawhile adding the field.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in
hfs_bnode_read_key")Reported-by Guikun Yang <[email protected]>Closes:
https://lore.kernel.org/all/[email protected]/Suggested-by Viacheslav Dubeyko <[email protected]>Signed-off-by ozgun kultekin <[email protected]>---
v3:key_sizerenamed tokey_buf_sizeand moved to the "filled by caller"
part of structhfs_find_data, struct and fields commented, buffer size written with named constants, as requested by Viacheslav
v2:
https://lore.kernel.org/all/[email protected]/
v1:
https://lore.kernel.org/all/[email protected]/fs/hfsplus/bfind.c | 5 +++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 10 ++++++---- fs/hfsplus/hfsplus_fs.h | 28 ++++++++++++++++++++-------- 4 files changed, 33 insertions(+), 17 deletions(-)fs/hfsplus/bfind.c +0
-0fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct · line 23
Quoted 5 lines
fs/hfsplus/bfind.c +0
-1@@ -19,11 +19,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode =NULL ;hfs_find_result_init (fd); - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL );OK. It looks like we try to reserve the space for two
hfsplus_btree_key(search_key+ key) and for two__be16key_len. Correct? But any key structhfsplus_cat_key, structhfsplus_ext_key, structhfsplus_attr_keyalready include__be16key_len:struct hfsplus_ext_key { __be16 key_len; u8 fork_type; u8 pad; hfsplus_cnid cnid; __be32 start_block; } __packed;Do we really need to add this 4 or
sizeof_field(hfsplus_btree_key, key_len)? I assume thattree->max_key_lenshould include the size ofkey_len. Am I right?Quoted 16 lines
fs/hfsplus/bfind.c +3
-1+ fd->key_buf_size = tree->max_key_len +HFSPLUS_KEY_LEN_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_buf_size,GFP_KERNEL );if (!ptr)return -ENOMEM ; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_buf_size; hfs_dbg("cnid %d, caller %ps\n",tree->cnid, __builtin_return_address(0)); mutex_lock_nested(&tree->tree_lock,fs/hfsplus/bnode.c +0
-0fs/hfsplus/bnode.c u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 · line 76
Quoted 7 lines
fs/hfsplus/bnode.c +2
-1@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)return data; } -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size)The
key_capacitylooks better.Quoted 84 lines
fs/hfsplus/bnode.c +0
-0{struct hfs_btree *tree;u32 key_len;fs/hfsplus/bnode.c +2
-2@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off) else key_len = tree->max_key_len +2 ; -if (key_len >sizeof (hfsplus_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfsplus_btree_key)); +if (key_len > key_buf_size || key_len <1 ) { +memset (key,0 , key_buf_size);pr_err ("hfsplus: Invalid key length: %u\n" , key_len);return; }fs/hfsplus/brec.c +2
-1@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,void *entry, u32 entry_len) * at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_buf_size);hfs_brec_update_parent(fd); }fs/hfsplus/brec.c +1
-1@@ -167,7 +168,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)/* get index key */ hfs_bnode_read_key(new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); + HFS_BNODE_DESC_SIZE, fd- >key_buf_size);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);hfs_bnode_put(new_node);fs/hfsplus/brec.c +1
-1@@ -467,7 +468,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) fd->bnode = hfs_bnode_find(tree, new_node->parent);/* create index key and entry */ hfs_bnode_read_key (new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , fd- >key_buf_size); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);fs/hfsplus/brec.c +2
-1@@ -480,7 +481,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) goto out;/* restore search_key */ hfs_bnode_read_key (node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , + fd->key_buf_size); } new_node =NULL ;}fs/hfsplus/hfsplus_fs.h +8
-0@@ -257,17 +257,28 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode,mark_inode_dirty (inode); } +/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* Size of the key_len field at the start of every key */ +#define HFSPLUS_KEY_LEN_SIZE sizeof_field(hfsplus_btree_key, key_len) + +/* +* State of one b-tree search. hfs_find_init() sets up the key Quoted 3 lines
buffers
+ * and the tree, the search functions fill in the rest.
+ */I expected to have comments here but not inside the structure declarations.
Quoted 9 lines
struct hfs_find_data { /* filled by caller */fs/hfsplus/hfsplus_fs.h +2
-2-hfsplus_btree_key *search_key; -hfsplus_btree_key *key; +hfsplus_btree_key *search_key;/* key to search for */ +hfsplus_btree_key *key;/* key of the found record */
+ u32key_buf_size; /* size of each ofsearch_keyand key */I think
key_capacitycould be better name, finally.I spent some time for analysis. I think my request to put it here was not good suggestion. Putting u32
key_buf_sizeafter the two pointers leaves a 4-byte hole, and pahole shows structhfs_find_datagrowing from 56 to 64 bytes. Also, "filled by caller" heading was already wrong (hfs_find_init()fills these fields, and tree too).Quoted 12 lines
/* filled by find */fs/hfsplus/hfsplus_fs.h +1
-2-struct hfs_btree *tree; -struct hfs_bnode *bnode; +struct hfs_btree *tree;/* tree being searched */
+ structhfs_bnode*bnode; /* node with the found
record *//* filled by findrec */fs/hfsplus/hfsplus_fs.h +1
-3-int record; -int keyoffset, keylength; -int entryoffset, entrylength; +int record;/* index of the record in Quoted 6 lines
bnode */
+ int keyoffset, keylength; /* offset and length of its
key */
+ int entryoffset, entrylength; /* offset and length of its
data */
};I am thinking about this:
struct hfs_find_data { /* set by hfs_find_init() */ hfsplus_btree_key *search_key; hfsplus_btree_key *key; struct hfs_btree *tree; /* filled by find */ struct hfs_bnode *bnode; /* set by hfs_find_init() */ u32 key_capacity; /* filled by findrec, reset by hfs_find_result_init() */ int record; int keyoffset, keylength; int entryoffset, entrylength; };Thanks,
Slava.Quoted 14 lines
struct
hfsplus_readdir_data{fs/hfsplus/hfsplus_fs.h +2
-1@@ -372,7 +383,8 @@ void hfs_bmap_free(struct hfs_bnode *node);void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len);u16 hfs_bnode_read_u16 (struct hfs_bnode *node,u32 off);u8 hfs_bnode_read_u8 (struct hfs_bnode *node,u32 off); -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size);void hfs_bnode_write (struct hfs_bnode *node,void *buf,u32 off,u32 len);void
hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); voidhfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
PATCH v3 3/3 hfs: pass the key buffer size to hfs_bnode_read_key()
In reply to ozgun kultekinhfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it against sizeof(hfs_btree_key). The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long. For the extents tree max_key_len is 7, so the whole allocation is 18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clears sizeof(hfs_btree_key) bytes of the same buffer.
hfs_brec_insert() and hfs_brec_update_parent() reach this with keys that were never validated, the same way as in hfsplus.
Store the size of each key buffer in struct hfs_find_data as key_buf_size in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.
The allocation size does not change: max_key_len * 2 + 4 is now written as HFS_FIND_KEY_BUFS * (max_key_len + HFS_KEY_LEN_MAX_SIZE), where HFS_KEY_LEN_MAX_SIZE is the 16-bit key length of HFS_TREE_BIGKEYS trees. Document struct hfs_find_data while adding the field.
---
v3: same changes as in the hfsplus patch
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
fs/hfs/bfind.c | 5 +++-- fs/hfs/bnode.c | 7 ++++--- fs/hfs/brec.c | 10 ++++++---- fs/hfs/btree.h | 31 +++++++++++++++++++++++-------- 4 files changed, 36 insertions(+), 17 deletions(-)
fs/hfs/bfind.c +3 -2
@@ -21,11 +21,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode =NULL ; - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL ); + fd->key_buf_size = tree->max_key_len +HFS_KEY_LEN_MAX_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_buf_size,GFP_KERNEL );if (!ptr)return -ENOMEM ; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_buf_size;hfs_dbg ("cnid %d, caller %ps\n" , tree->cnid,__builtin_return_address (0 ));switch (tree->cnid) {
fs/hfs/bnode.c +4 -3
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)return data; } -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size) {struct hfs_btree *tree;u32 key_len;
@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else key_len = tree->max_key_len +1 ; -if (key_len >sizeof (hfs_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfs_btree_key)); +if (key_len > key_buf_size || key_len <1 ) { +memset (key,0 , key_buf_size);pr_err ("hfs: Invalid key length: %u\n" , key_len);return ; }
fs/hfs/brec.c +6 -4
@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)* at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_buf_size);hfs_brec_update_parent (fd); }
@@ -157,7 +158,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)/* get index key */ hfs_bnode_read_key (new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , fd->key_buf_size);__hfs_brec_find (fd->bnode, fd);hfs_bnode_put (new_node);
@@ -462,7 +463,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ hfs_bnode_read_key (new_node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , fd->key_buf_size); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd);
@@ -475,7 +476,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) goto out;/* restore search_key */ hfs_bnode_read_key (node, fd->search_key, -HFS_BNODE_DESC_SIZE ); +HFS_BNODE_DESC_SIZE , + fd->key_buf_size); } new_node =NULL ; }
fs/hfs/btree.h +23 -8
@@ -74,14 +74,28 @@ struct hfs_bnode {#define HFS_BNODE_NEW 1#define HFS_BNODE_DELETED 2 +/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* Largest key length field: u16 in trees with HFS_TREE_BIGKEYS */ +#define HFS_KEY_LEN_MAX_SIZE sizeof(__be16) + +/* +* State of one b-tree search. hfs_find_init() sets up the key buffers +* and the tree, the search functions fill in the rest. +*/ struct hfs_find_data { -btree_key *key; -btree_key *search_key; -struct hfs_btree *tree; -struct hfs_bnode *bnode; -int record; -int keyoffset, keylength; -int entryoffset, entrylength; +/* filled by caller */ +btree_key *key;/* key of the found record */ +btree_key *search_key;/* key to search for */ +u32 key_buf_size;/* size of each of search_key and key */ +/* filled by find */ +struct hfs_btree *tree;/* tree being searched */ +struct hfs_bnode *bnode;/* node with the found record */ +/* filled by findrec */ +int record;/* index of the record in bnode */ +int keyoffset, keylength;/* offset and length of its key */ +int entryoffset, entrylength;/* offset and length of its data */ }; static inline bool is_hfs_bnode_range_valid(struct hfs_find_data *fd,
@@ -117,7 +131,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node);extern void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len);extern u16 hfs_bnode_read_u16 (struct hfs_bnode *node,u32 off);extern u8 hfs_bnode_read_u8 (struct hfs_bnode *node,u32 off); -extern void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size);extern void hfs_bnode_write (struct hfs_bnode *node,void *buf,u32 off,u32 len);extern void hfs_bnode_write_u16 (struct hfs_bnode *node,u32 off,u16 data);extern void hfs_bnode_write_u8 (struct hfs_bnode *node,u32 off,u8 data);
Signature · 1 line
2.39.5
Viacheslav Dubeyko
maintainerRe: [PATCH v3 3/3] hfs: pass the key buffer size to hfs_bnode_read_key()
Quoted 61 lines · ozgun kultekin
hfs_bnode_read_key()reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it againstsizeof(hfs_btree_key). The destination is always one of the two key buffershfs_find_init()allocates, eachtree->max_key_len+ 2 bytes long. For the extents treemax_key_lenis 7, so the whole allocation is
18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clearssizeof(hfs_btree_key)bytes of the same
buffer.
hfs_brec_insert()andhfs_brec_update_parent()reach this with keys that were never validated, the same way as in hfsplus.Store the size of each key buffer in struct
hfs_find_dataaskey_buf_sizeinhfs_find_init(), allocate the two buffers from it, and
pass it to everyhfs_bnode_read_key()call. Keys longer than the buffer
are rejected as before, and the error path clears only the buffer.The allocation size does not change:
max_key_len* 2 + 4 is now written
asHFS_FIND_KEY_BUFS* (max_key_len+HFS_KEY_LEN_MAX_SIZE), whereHFS_KEY_LEN_MAX_SIZEis the 16-bit key length ofHFS_TREE_BIGKEYStrees. Document structhfs_find_datawhile adding the field.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Fixes bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in
hfs_bnode_read_key")Reported-by Guikun Yang <[email protected]>Closes:
https://lore.kernel.org/all/[email protected]/Suggested-by Viacheslav Dubeyko <[email protected]>Signed-off-by ozgun kultekin <[email protected]>---
v3: same changes as in the hfsplus patch
v2:
https://lore.kernel.org/all/[email protected]/
v1:
https://lore.kernel.org/all/[email protected]/fs/hfs/bfind.c | 5 +++--
fs/hfs/bnode.c | 7 ++++---
fs/hfs/brec.c | 10 ++++++----fs/hfs/btree.h| 31 +++++++++++++++++++++++--------4 files changed, 36 insertions(+), 17 deletions(-)fs/hfs/bfind.c +0
-0@@ -21,11 +21,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
fd->tree= tree;
fd->bnode= NULL;fs/hfs/bfind.c +2
-1- ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL ); + fd->key_buf_size = tree->max_key_len +HFS_KEY_LEN_MAX_SIZE ; + ptr = kzalloc(HFS_FIND_KEY_BUFS * fd->key_buf_size,
Quoted 1 line
GFP_KERNEL);
I have the same concerns here as for HFS+.
Quoted 17 lines
if (!ptr)
return-ENOMEM;
fd->search_key= ptr;fs/hfs/bfind.c +1
-1- fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_buf_size;
hfs_dbg("cnid %d, caller %ps\n", tree->cnid, __builtin_return_address(0)); switch (tree->cnid) {fs/hfs/bnode.c +0
-0@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) return data; }
fs/hfs/bnode.c u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off) · line 75
Quoted 3 lines
fs/hfs/bnode.c +2
-1-void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size)
key_capacity?
Quoted 109 lines
{
structhfs_btree*tree;
u32key_len;fs/hfs/bnode.c +0
-0@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else
key_len=tree->max_key_len+ 1;fs/hfs/bnode.c +2
-2-if (key_len >sizeof (hfs_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfs_btree_key)); +if (key_len > key_buf_size || key_len <1 ) { +memset (key,0 , key_buf_size);
pr_err("hfs: Invalid key length: %u\n", key_len); return;
}fs/hfs/brec.c +2
-1@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd,void *entry, u32 entry_len) * at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_buf_size);
hfs_brec_update_parent(fd);
}fs/hfs/brec.c +0
-0@@ -157,7 +158,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
/* get index key */ hfs_bnode_read_key(new_node,fd->search_key,fs/hfs/brec.c +1
-1- HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, fd-Quoted 1 line
key_buf_size);
__hfs_brec_find(fd->bnode, fd);
hfs_bnode_put(new_node);fs/hfs/brec.c +1
-1@@ -462,7 +463,7 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) fd->bnode = hfs_bnode_find(tree, new_node->parent); /* create index key and entry */ hfs_bnode_read_key(new_node, fd->search_key, - HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, fd- >key_buf_size); cnid = cpu_to_be32(new_node->this);
__hfs_brec_find(fd->bnode, fd);fs/hfs/brec.c +2
-1@@ -475,7 +476,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) goto out; /* restore search_key */ hfs_bnode_read_key(node, fd->search_key, - HFS_BNODE_DESC_SIZE); + HFS_BNODE_DESC_SIZE, + fd->key_buf_size); } new_node = NULL;}
fs/hfs/btree.h +0
-0@@ -74,14 +74,28 @@ struct hfs_bnode { #define HFS_BNODE_NEW 1 #define HFS_BNODE_DELETED 2fs/hfs/btree.h +8
-0+/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* Largest key length field: u16 in trees with HFS_TREE_BIGKEYS */ +#define HFS_KEY_LEN_MAX_SIZE sizeof(__be16) + +/* +* State of one b-tree search. hfs_find_init() sets up the key buffers
+ * and the tree, the search functions fill in the rest.
+ */
structhfs_find_data{fs/hfs/btree.h +2
-7-btree_key *key; -btree_key *search_key; -struct hfs_btree *tree; -struct hfs_bnode *bnode; -int record; -int keyoffset, keylength; -int entryoffset, entrylength; +/* filled by caller */ +btree_key *key;/* key of the found record */
fs/hfs/btree.h +2
-0+btree_key *search_key;/* key to search for */ +u32 key_buf_size;/* size of each of
search_keyand key */fs/hfs/btree.h +2
-0+/* filled by find */ +struct hfs_btree *tree;/* tree being searched */
fs/hfs/btree.h +1
-0+struct hfs_bnode *bnode;/* node with the found record */
fs/hfs/btree.h +2
-0+/* filled by findrec */ +int record;/* index of the record in bnode */
fs/hfs/btree.h +1
-0+int keyoffset, keylength;/* offset and length of its key */
fs/hfs/btree.h +1
-0+int entryoffset, entrylength;/* offset and length of its
Quoted 2 lines
data */
};
The same issue here. The pahole shows a 4-byte hole, and struct hfs_find_data grows from 56 to 64 bytes.
struct hfs_find_data {
btree_key *key;
btree_key *search_key;
struct hfs_btree *tree;
struct hfs_bnode *bnode;
u32 key_capacity;
int record;
int keyoffset, keylength;
int entryoffset, entrylength;
};fs/hfs/btree.h extern void hfs_bmap_free(struct hfs_bnode *node); · line 135
Quoted 13 lines
static inline bool is_hfs_bnode_range_valid(structhfs_find_data*fd,fs/hfs/btree.h +0
-0@@ -117,7 +131,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node);extern void
hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
extern u16hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); extern u8hfs_bnode_read_u8(struct hfs_bnode *node, u32 off); -extern voidhfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);fs/hfs/btree.h +2
-0+void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_buf_size);
The new prototype drops extern while every neighbour keeps it. checkpatch prefers no extern, but a mixed block looks accidental. Keep extern here for consistency, or leave a full cleanup for a separate patch.
Thanks,
Slava.
Quoted 6 lines
extern void
hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
extern voidhfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
extern voidhfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data);
v4 cover hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()
In reply to ozgun kultekinhfs_bnode_read_key() checks the on-disk key length against the size of the key union, but the buffers from hfs_find_init() are only max_key_len + 2 bytes each. A crafted extents leaf key overflows them.
Changes in v4, following Viacheslav's review of v3:
- Reordered: the two fixes are now 1/3 (hfsplus) and 2/3 (hfs) with
Cc: stable, and the cleanup is 3/3 without it, because include/linux/hfs_common.h only exists since v6.19. The fixes keep the literal 14 that 3/3 replaces. The "next two patches" wording is gone.
- The + 2 per buffer (the "+ 4" in total) is not double counting. max_key_len in the b-tree header record does not include the key_len field. hfs_btree_open() requires HFSPLUS_EXT_KEYLEN - sizeof(u16), HFSPLUS_CAT_KEYLEN - sizeof(u16) and HFSPLUS_ATTR_KEYLEN - sizeof(u16) (fs/hfsplus/btree.c:318, 331, 350), where the *_KEYLEN constants are the full struct sizes including key_len (hfs_common.h:363, 382, 627), and hfsplus_init_header_node() writes HFSPLUS_ATTR_KEYLEN - sizeof(u16) (fs/hfsplus/xattr.c:88). hfs_bnode_read_key() copies the on-disk length + 2 (fs/hfsplus/bnode.c:83, 85). So for the extents tree max_key_len is 10 and a valid key is 12 bytes. Without the + 2 every full-size key would be rejected. hfs is the same with a u8
length: HFS_MAX_EXT_KEYLEN and HFS_MAX_CAT_KEYLEN are sizeof(...) - sizeof(u8) (hfs_common.h:390-391, checked at fs/hfs/btree.c:251, 258). The comment on HFSPLUS_KEY_LEN_SIZE and HFS_KEY_LEN_MAX_SIZE now says this.
- key_buf_size is renamed to key_capacity.
- struct hfs_find_data: key_capacity now comes after bnode, where it pairs with record. pahole (x86_64, both drivers) shows 56 bytes before and after the series, with no hole; the 4 bytes of tail padding are used. The hfsplus headings are the ones you proposed ("set by hfs_find_init()", "filled by find", "filled by findrec, reset by hfs_find_result_init()"), and there are no per-field comments inside the struct. hfs keeps its struct without comments.
- hfs: the changed prototype in btree.h keeps extern like its neighbours.
- 3/3: HFS_BNODE_DESC_SIZE is used for sizes and the new HFS_BNODE_FIRST_REC_OFF for the offset of record 0. It also converts node_desc_size in hfsplus hfs_bnode_split(), the desc_size locals in hfsplus_fs.h, and sizeof(struct hfs_bnode_desc) in bnode.c, btree.c and hfsplus/xattr.c. hfs_btree_inc_height() gets a local cnid_off, and the 4 becomes sizeof(cnid). Long lines break after "=", and the conditions next to the changed lines are aligned.
Tested on mainline 9437ee8b6cf6 (7.4-rc4+) with KASAN in QEMU:
- hfsplus crafted images: without the series KASAN reports a double-free in hfsplus_find_exit() and the kernel oopses; with it they are refused with "Invalid key length: 516" and there is no KASAN report.
- hfsplus image made by mkfs.hfsplus: create, rename, delete, fill, xattrs, appends and remount work, and fsck.hfsplus reports it clean.
- hfs image made by hformat: 400 creates, 200 deletes, fragmented appends and remount work with no KASAN report, and hfsutils reads the result.
checkpatch --strict reports no errors. Each patch builds on its own with W=1. 1/3 and 2/3 also apply to 6.12.y and 6.6.y, except for one context conflict in hfs_find_init() (older hfs_dbg() format, no hfs_find_result_init()); the fixed-up backports build with W=1.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Ozgun Kultekin (3):
hfsplus: pass the key buffer size to hfs_bnode_read_key()
hfs: pass the key buffer size to hfs_bnode_read_key() hfs/hfsplus: name the node descriptor size and the first record offset
fs/hfs/bfind.c | 5 ++-- fs/hfs/bnode.c | 9 +++--- fs/hfs/brec.c | 47 +++++++++++++++++++------------ fs/hfs/btree.c | 9 +++--- fs/hfs/btree.h | 15 +++++++++- fs/hfsplus/bfind.c | 5 ++-- fs/hfsplus/bnode.c | 9 +++--- fs/hfsplus/brec.c | 57 +++++++++++++++++++++++--------------- fs/hfsplus/btree.c | 9 +++--- fs/hfsplus/hfsplus_fs.h | 29 +++++++++++++------ fs/hfsplus/xattr.c | 6 ++-- include/linux/hfs_common.h | 6 ++++ 12 files changed, 133 insertions(+), 73 deletions(-)
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
Signature · 1 line
2.39.5
PATCH v4 1/3 hfsplus: pass the key buffer size to hfs_bnode_read_key()
hfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key and attributes trees) from disk and only checks it against sizeof(hfsplus_btree_key), 518 bytes. The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes long: max_key_len in the b-tree header record does not count the __be16 key_len field, as hfs_btree_open() checks (HFSPLUS_EXT_KEYLEN - sizeof(u16) and so on). For the extents tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and a crafted leaf key writes up to 516 bytes from the image into kmalloc-32. The error path clears 518 bytes of the same buffer.
hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it inserts into, and after a split it reads the first key of the new node. A read-write mount that extends the catalog, or an append to a file whose extents spill into the extents tree, is enough.
Do not let hfs_bnode_read_key() assume the size of the buffer. Store the capacity of each key buffer in struct hfs_find_data as key_capacity in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.
The allocation size does not change: max_key_len * 2 + 4 is now written as HFS_FIND_KEY_BUFS * (max_key_len + HFSPLUS_KEY_LEN_SIZE). key_capacity goes after bnode, where it pairs with record, so struct hfs_find_data stays 56 bytes on 64-bit.
---
v4: now 1/3 so it applies to stable trees without the cleanup; uses
the literal 14 that the cleanup in 3/3 replaces. key_buf_size renamed to key_capacity and placed after bnode (no hole, 56 bytes), struct comments as section headings only, as suggested by Viacheslav. The + 2 per buffer stays: max_key_len excludes key_len.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- fs/hfsplus/bfind.c | 5 +++-- fs/hfsplus/bnode.c | 7 ++++--- fs/hfsplus/brec.c | 12 ++++++++---- fs/hfsplus/hfsplus_fs.h | 21 +++++++++++++++++---- 4 files changed, 32 insertions(+), 13 deletions(-)
fs/hfsplus/bfind.c +3 -2
@@ -19,11 +19,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode =NULL ;hfs_find_result_init (fd); - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL ); + fd->key_capacity = tree->max_key_len +HFSPLUS_KEY_LEN_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_capacity,GFP_KERNEL );if (!ptr)return -ENOMEM ; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_capacity;hfs_dbg ("cnid %d, caller %ps\n" , tree->cnid,__builtin_return_address (0 )); mutex_lock_nested(&tree->tree_lock,
fs/hfsplus/bnode.c +4 -3
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)return data; } -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_capacity) {struct hfs_btree *tree;u32 key_len;
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else key_len = tree->max_key_len +2 ; -if (key_len >sizeof (hfsplus_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfsplus_btree_key)); +if (key_len > key_capacity || key_len <1 ) { +memset (key,0 , key_capacity);pr_err ("hfsplus: Invalid key length: %u\n" , key_len);return ; }
fs/hfsplus/brec.c +8 -4
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)* at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_capacity);hfs_brec_update_parent (fd); }
@@ -166,7 +167,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len =sizeof (cnid);/* get index key */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key,14 , + fd->key_capacity);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);hfs_bnode_put (new_node);
@@ -464,7 +466,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key,14 , + fd->key_capacity); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd, hfs_find_rec_by_key);
@@ -476,7 +479,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)if (new_node == node) goto out;/* restore search_key */ -hfs_bnode_read_key (node, fd->search_key,14 ); +hfs_bnode_read_key (node, fd->search_key,14 , + fd->key_capacity); } new_node =NULL ; }
fs/hfsplus/hfsplus_fs.h +17 -4
@@ -257,14 +257,26 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode,mark_inode_dirty (inode); } +/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* +* Size of the key_len field at the start of every key. The max_key_len +* of the b-tree header record does not count it (see hfs_btree_open()), +* so a key buffer needs max_key_len + HFSPLUS_KEY_LEN_SIZE bytes. +*/ +#define HFSPLUS_KEY_LEN_SIZE sizeof_field(hfsplus_btree_key, key_len) +struct hfs_find_data { -/* filled by caller */ +/* set by hfs_find_init() */ hfsplus_btree_key *search_key;hfsplus_btree_key *key; -/* filled by find */ struct hfs_btree *tree; +/* filled by find */ struct hfs_bnode *bnode; -/* filled by findrec */ +/* set by hfs_find_init(): size of each of search_key and key */ +u32 key_capacity; +/* filled by findrec, reset by hfs_find_result_init() */ int record;int keyoffset, keylength;int entryoffset, entrylength;
@@ -372,7 +384,8 @@ void hfs_bmap_free(struct hfs_bnode *node);void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len);u16 hfs_bnode_read_u16 (struct hfs_bnode *node,u32 off);u8 hfs_bnode_read_u8 (struct hfs_bnode *node,u32 off); -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_capacity);void hfs_bnode_write (struct hfs_bnode *node,void *buf,u32 off,u32 len);void hfs_bnode_write_u16 (struct hfs_bnode *node,u32 off,u16 data);void hfs_bnode_clear (struct hfs_bnode *node,u32 off,u32 len);
Signature · 1 line
2.39.5
PATCH v4 2/3 hfs: pass the key buffer size to hfs_bnode_read_key()
In reply to Ozgun Kultekinhfs_bnode_read_key() reads the key length of leaf records (and of all records of variable-key trees) from disk and only checks it against sizeof(hfs_btree_key). The destination is always one of the two key buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes
long: max_key_len in the b-tree header record does not count the key length field, as hfs_btree_open() checks (HFS_MAX_EXT_KEYLEN is sizeof(struct hfs_ext_key) - sizeof(u8)). For the extents tree max_key_len is 7, so the whole allocation is 18 bytes and a crafted leaf key writes up to 256 bytes from the image past it. The error path clears sizeof(hfs_btree_key) bytes of the same buffer.
hfs_brec_insert() and hfs_brec_update_parent() reach this with keys that were never validated, the same way as in hfsplus.
Store the capacity of each key buffer in struct hfs_find_data as key_capacity in hfs_find_init(), allocate the two buffers from it, and pass it to every hfs_bnode_read_key() call. Keys longer than the buffer are rejected as before, and the error path clears only the buffer.
The allocation size does not change: max_key_len * 2 + 4 is now written as HFS_FIND_KEY_BUFS * (max_key_len + HFS_KEY_LEN_MAX_SIZE), where HFS_KEY_LEN_MAX_SIZE is the 16-bit key length of HFS_TREE_BIGKEYS trees. key_capacity goes after bnode, where it pairs with record, so struct hfs_find_data stays 56 bytes on 64-bit.
---
v4: now 2/3 so it applies to stable trees without the cleanup; uses
the literal 14 that 3/3 replaces. key_buf_size renamed to key_capacity and placed after bnode (no hole, 56 bytes), no comments inside the struct, extern kept on the prototype like its neighbours, as suggested by Viacheslav.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
--- fs/hfs/bfind.c | 5 +++-- fs/hfs/bnode.c | 7 ++++--- fs/hfs/brec.c | 12 ++++++++---- fs/hfs/btree.h | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 10 deletions(-)
fs/hfs/bfind.c +3 -2
@@ -21,11 +21,12 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) fd->tree = tree; fd->bnode =NULL ; - ptr =kzalloc (tree->max_key_len *2 +4 ,GFP_KERNEL ); + fd->key_capacity = tree->max_key_len +HFS_KEY_LEN_MAX_SIZE ; + ptr =kzalloc (HFS_FIND_KEY_BUFS * fd->key_capacity,GFP_KERNEL );if (!ptr)return -ENOMEM ; fd->search_key = ptr; - fd->key = ptr + tree->max_key_len +2 ; + fd->key = ptr + fd->key_capacity;hfs_dbg ("cnid %d, caller %ps\n" , tree->cnid,__builtin_return_address (0 ));switch (tree->cnid) {
fs/hfs/bnode.c +4 -3
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)return data; } -void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off) +void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_capacity) {struct hfs_btree *tree;u32 key_len;
@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)else key_len = tree->max_key_len +1 ; -if (key_len >sizeof (hfs_btree_key) || key_len <1 ) { -memset (key,0 ,sizeof (hfs_btree_key)); +if (key_len > key_capacity || key_len <1 ) { +memset (key,0 , key_capacity);pr_err ("hfs: Invalid key length: %u\n" , key_len);return ; }
fs/hfs/brec.c +8 -4
@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)* at the start of the node and it is not the new node */ if (!rec && new_node != node) { -hfs_bnode_read_key (node, fd->search_key, data_off + size); +hfs_bnode_read_key (node, fd->search_key, data_off + size, + fd->key_capacity);hfs_brec_update_parent (fd); }
@@ -156,7 +157,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len) entry_len =sizeof (cnid);/* get index key */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key,14 , + fd->key_capacity);__hfs_brec_find (fd->bnode, fd);hfs_bnode_put (new_node);
@@ -450,7 +452,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd) } fd->bnode =hfs_bnode_find (tree, new_node->parent);/* create index key and entry */ -hfs_bnode_read_key (new_node, fd->search_key,14 ); +hfs_bnode_read_key (new_node, fd->search_key,14 , + fd->key_capacity); cnid =cpu_to_be32 (new_node->this);__hfs_brec_find (fd->bnode, fd);
@@ -462,7 +465,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)if (new_node == node) goto out;/* restore search_key */ -hfs_bnode_read_key (node, fd->search_key,14 ); +hfs_bnode_read_key (node, fd->search_key,14 , + fd->key_capacity); } new_node =NULL ; }
fs/hfs/btree.h +14 -1
@@ -74,11 +74,23 @@ struct hfs_bnode {#define HFS_BNODE_NEW 1#define HFS_BNODE_DELETED 2 +/* hfs_find_init() allocates search_key and key as one buffer */ +#define HFS_FIND_KEY_BUFS 2 + +/* +* Room for the key length field at the start of every key. The +* max_key_len of the b-tree header record does not count it (see +* hfs_btree_open()). The field is a u8, or a u16 in HFS_TREE_BIGKEYS +* trees (see hfs_brec_keylen()); the buffers have room for the larger. +*/ +#define HFS_KEY_LEN_MAX_SIZE sizeof(__be16) +struct hfs_find_data {btree_key *key;btree_key *search_key;struct hfs_btree *tree;struct hfs_bnode *bnode; +u32 key_capacity;int record;int keyoffset, keylength;int entryoffset, entrylength;
@@ -117,7 +129,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node);extern void hfs_bnode_read (struct hfs_bnode *node,void *buf,u32 off,u32 len);extern u16 hfs_bnode_read_u16 (struct hfs_bnode *node,u32 off);extern u8 hfs_bnode_read_u8 (struct hfs_bnode *node,u32 off); -extern void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off); +extern void hfs_bnode_read_key (struct hfs_bnode *node,void *key,u32 off, +u32 key_capacity);extern void hfs_bnode_write (struct hfs_bnode *node,void *buf,u32 off,u32 len);extern void hfs_bnode_write_u16 (struct hfs_bnode *node,u32 off,u16 data);extern void hfs_bnode_write_u8 (struct hfs_bnode *node,u32 off,u8 data);
Signature · 1 line
2.39.5
PATCH v4 3/3 hfs/hfsplus: name the node descriptor size and the first record offset
In reply to Ozgun KultekinBoth drivers spell the size of struct hfs_bnode_desc, which is also the offset of record 0 in a node, in several ways: the literal 14 in brec.c and btree.c, a local node_desc_size in hfsplus hfs_bnode_split(), local desc_size variables in hfsplus_fs.h, and sizeof(struct hfs_bnode_desc) in bnode.c, btree.c and hfsplus/xattr.c.
Add two names next to the structure: HFS_BNODE_DESC_SIZE for the size and HFS_BNODE_FIRST_REC_OFF for the offset of record 0, and use them everywhere. In hfs_btree_inc_height() keep the offset of the child pointer in a local variable and write its length as sizeof(cnid) instead of 4. Align the conditions next to the changed lines.
No functional change.
---
v4: moved to the end of the series without Cc: stable, since
include/linux/hfs_common.h only exists since v6.19. Second name HFS_BNODE_FIRST_REC_OFF for offset uses, node_desc_size, desc_size and sizeof(struct hfs_bnode_desc) converted too, local cnid_off and sizeof(cnid) in hfs_btree_inc_height(), line breaks after "=" and condition alignment as suggested by Viacheslav.
v3: https://lore.kernel.org/all/[email protected]/
--- fs/hfs/bnode.c | 2 +- fs/hfs/brec.c | 45 ++++++++++++++++++------------- fs/hfs/btree.c | 9 ++++--- fs/hfsplus/bnode.c | 2 +- fs/hfsplus/brec.c | 55 +++++++++++++++++++++----------------- fs/hfsplus/btree.c | 9 ++++--- fs/hfsplus/hfsplus_fs.h | 8 +++--- fs/hfsplus/xattr.c | 6 ++--- include/linux/hfs_common.h | 6 +++++ 9 files changed, 82 insertions(+), 60 deletions(-)
fs/hfs/bnode.c +1 -1
@@ -426,7 +426,7 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num) rec_off = tree->node_size -2 ; off =hfs_bnode_read_u16 (node, rec_off); -if (off !=sizeof (struct hfs_bnode_desc )) +if (off !=HFS_BNODE_FIRST_REC_OFF ) goto node_error;for (i =1 ; i <= node->num_recs; off = next_off, i++) { rec_off -=2 ;
fs/hfs/brec.c +27 -18
126 lines not on this page · raw message
fs/hfs/btree.c +5 -4
@@ -229,7 +229,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke/* Load the header */ head = (struct hfs_btree_header_rec *)(kmap_local_folio (folio,0 ) + -sizeof (struct hfs_bnode_desc )); +HFS_BNODE_FIRST_REC_OFF ); tree->root =be32_to_cpu (head->root); tree->leaf_count =be32_to_cpu (head->leaf_count); tree->leaf_head =be32_to_cpu (head->leaf_head);
@@ -340,7 +340,7 @@ void hfs_btree_write(struct hfs_btree *tree)/* Load the header */ page = node->page[0 ]; head = (struct hfs_btree_header_rec *)(kmap_local_page (page) + -sizeof (struct hfs_bnode_desc )); +HFS_BNODE_FIRST_REC_OFF ); head->root =cpu_to_be32 (tree->root); head->leaf_count =cpu_to_be32 (tree->leaf_count);
@@ -384,8 +384,9 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs =cpu_to_be16 (1 ); desc.reserved =0 ;hfs_bnode_write (node, &desc,0 ,sizeof (desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_FIRST_REC_OFF ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 , +HFS_BNODE_FIRST_REC_OFF );hfs_bnode_write_u16 (node, tree->node_size -4 , tree->node_size -6 );return node;
fs/hfsplus/bnode.c +1 -1
@@ -592,7 +592,7 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num) rec_off = tree->node_size -2 ; off =hfs_bnode_read_u16 (node, rec_off); -if (off !=sizeof (struct hfs_bnode_desc )) +if (off !=HFS_BNODE_FIRST_REC_OFF ) goto node_error;for (i =1 ; i <= node->num_recs; off = next_off, i++) { rec_off -=2 ;
fs/hfsplus/brec.c +31 -24
140 lines not on this page · raw message
fs/hfsplus/btree.c +5 -4
@@ -300,7 +300,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)/* Load the header */ head = (struct hfs_btree_header_rec *)(kmap_local_page (page) + -sizeof (struct hfs_bnode_desc )); +HFS_BNODE_FIRST_REC_OFF ); tree->root =be32_to_cpu (head->root); tree->leaf_count =be32_to_cpu (head->leaf_count); tree->leaf_head =be32_to_cpu (head->leaf_head);
@@ -448,7 +448,7 @@ int hfs_btree_write(struct hfs_btree *tree)/* Load the header */ page = node->page[0 ]; head = (struct hfs_btree_header_rec *)(kmap_local_page (page) + -sizeof (struct hfs_bnode_desc )); +HFS_BNODE_FIRST_REC_OFF ); head->root =cpu_to_be32 (tree->root); head->leaf_count =cpu_to_be32 (tree->leaf_count);
@@ -491,8 +491,9 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) desc.num_recs =cpu_to_be16 (1 ); desc.reserved =0 ;hfs_bnode_write (node, &desc,0 ,sizeof (desc)); -hfs_bnode_write_u16 (node,14 ,0x8000 ); -hfs_bnode_write_u16 (node, tree->node_size -2 ,14 ); +hfs_bnode_write_u16 (node,HFS_BNODE_FIRST_REC_OFF ,0x8000 ); +hfs_bnode_write_u16 (node, tree->node_size -2 , +HFS_BNODE_FIRST_REC_OFF );hfs_bnode_write_u16 (node, tree->node_size -4 , tree->node_size -6 );return node;
fs/hfsplus/hfsplus_fs.h +3 -5
@@ -609,16 +609,15 @@ bool hfs_bnode_num_recs_invalid(struct hfs_bnode *node)u32 table_size;u32 area_size;u32 rec_size =sizeof (__be16); -u32 desc_size =sizeof (struct hfs_bnode_desc );if (!node || !node->tree)return true; node_size = node->tree->node_size; -if (node_size < desc_size) +if (node_size <HFS_BNODE_DESC_SIZE )return true; - area_size = node_size - desc_size; + area_size = node_size -HFS_BNODE_DESC_SIZE ; table_size = ((u32 )node->num_recs +1 ) * rec_size;return table_size > area_size;
@@ -641,12 +640,11 @@ bool hfs_brec_offsets_invalid(struct hfs_bnode *node, u16 off, u16 next_off)u32 table_size;u32 table_start;u32 rec_size =sizeof (__be16); -u32 desc_size =sizeof (struct hfs_bnode_desc );if (!node || !node->tree)return true; -if (off < desc_size || (off &1 )) +if (off <HFS_BNODE_FIRST_REC_OFF || (off &1 ))return true; if (next_off <= off ||
fs/hfsplus/xattr.c +3 -3
@@ -74,7 +74,7 @@ static u32 hfsplus_init_header_node(struct inode *attr_file, desc = (struct hfs_bnode_desc *)buf; desc->type =HFS_NODE_HEADER ; desc->num_recs =cpu_to_be16 (HFSPLUS_BTREE_HDR_NODE_RECS_COUNT ); - offset =sizeof (struct hfs_bnode_desc ); + offset =HFS_BNODE_FIRST_REC_OFF ; *--rec_offsets =cpu_to_be16 (offset); head = (struct hfs_btree_header_rec *)(buf + offset);
@@ -96,7 +96,7 @@ static u32 hfsplus_init_header_node(struct inode *attr_file,u32 map_node_bits; desc->next =cpu_to_be32 (be32_to_cpu (head->leaf_tail) +1 ); - map_node_bits =8 * (node_size -sizeof (struct hfs_bnode_desc ) - + map_node_bits =8 * (node_size -HFS_BNODE_DESC_SIZE - (2 *sizeof (u16)) -2 ); map_nodes = (be32_to_cpu(head->node_count) - hdr_node_map_rec_bits +
@@ -157,7 +157,7 @@ static void hfsplus_init_map_node(u8 *buf, u16 node_size, u32 next_node) rec_offsets = (__be16 *)(buf + node_size);/* record #1 */ - offset =sizeof (struct hfs_bnode_desc ); + offset =HFS_BNODE_FIRST_REC_OFF ; *--rec_offsets =cpu_to_be16 (offset);/* record #2 */
include/linux/hfs_common.h +6 -0
@@ -475,6 +475,12 @@ struct hfs_bnode_desc {u16 reserved; } __packed; +/* Size of the node descriptor at the start of every node */ +#define HFS_BNODE_DESC_SIZE sizeof(struct hfs_bnode_desc) + +/* Offset of record 0, which follows the node descriptor */ +#define HFS_BNODE_FIRST_REC_OFF HFS_BNODE_DESC_SIZE +/* HFS/HFS+ BTree node types */ #define HFS_NODE_INDEX 0x00/* An internal (index) node */ #define HFS_NODE_HEADER 0x01/* The tree header node (node 0) */
Signature · 1 line
2.39.5