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);
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);