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 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. 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 Closes: https://lore.kernel.org/all/tencent_F85118A9AFDF9B230D4E8FA672510A3E2F08@qq.com/ Suggested-by: Viacheslav Dubeyko Cc: stable@vger.kernel.org Signed-off-by: Ozgun Kultekin --- 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/20261119005943.37851-1-ozgunkultekin@gmail.com/ v2: https://lore.kernel.org/all/20261118013337.67813-1-ozgunkultekin@gmail.com/ v1: https://lore.kernel.org/all/20261114043522.48764-1-ozgunkultekin@gmail.com/ --- 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(-) diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c index d56e47bdc..b37448e7b 100644 --- a/fs/hfs/bfind.c +++ b/fs/hfs/bfind.c @@ -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) { diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c index fcb5b9cd1..23ad1a565 100644 --- a/fs/hfs/bnode.c +++ b/fs/hfs/bnode.c @@ -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; } diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c index 5a2f740dd..84da30f65 100644 --- a/fs/hfs/brec.c +++ b/fs/hfs/brec.c @@ -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; } diff --git a/fs/hfs/btree.h b/fs/hfs/btree.h index 1f703f2c5..404e526f9 100644 --- a/fs/hfs/btree.h +++ b/fs/hfs/btree.h @@ -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); -- 2.39.5