Skip to content

hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()

Ozgun Kultekin linux-fsdevel stable 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 3 repliesIn review
  2. v33 patches 4 repliesSuperseded
  3. v22 patches 7 repliesSuperseded

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3hfsplus: pass the key buffer size to hfs_bnode_read_key()fs/hfsplus/bfind.c fs/hfsplus/bnode.c fs/hfsplus/brec.c +1 more+32 -131 reply
2/3hfs: pass the key buffer size to hfs_bnode_read_key()fs/hfs/bfind.c fs/hfs/bnode.c fs/hfs/brec.c +1 more+29 -101 reply
3/3hfs/hfsplus: name the node descriptor size and the first record offsetfs/hfs/bnode.c fs/hfs/brec.c fs/hfs/btree.c +6 more+82 -601 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [PATCH v4 0/3] 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 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