mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ozgun Kultekin <ozgunkultekin@gmail.com>
To: slava@dubeyko.com, glaubitz@physik.fu-berlin.de, frank.li@vivo.com
Cc: linux-fsdevel@vger.kernel.org, yangguikun19@qq.com,
	stable@vger.kernel.org, linux-kernel@vger.kernel.org,
	kovalev@altlinux.org, brauner@kernel.org,
	cengiz.can@canonical.com,
	Ozgun Kultekin <ozgunkultekin@gmail.com>
Subject: [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()
Date: Tue,  1 Dec 2026 01:42:43 -0800	[thread overview]
Message-ID: <20261201094246.80197-1-ozgunkultekin@gmail.com> (raw)
In-Reply-To: <20261127045416.1160-1-ozgunkultekin@gmail.com>

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 v5, following Viacheslav's review of v4:

- One allocation or two: two kzalloc() calls would not be measurably
  slower, since both buffers are small. But every hfs_find_init() would
  allocate twice, and the error path and hfs_find_exit() would need a
  second kfree(). I kept the single allocation so the fixes stay small
  for stable, and wrote it the way you suggested:
  kzalloc(fd->key_capacity * 2, ...) with a comment saying that
  search_key and key share it. HFS_FIND_KEY_BUFS is gone. If you would
  rather have two allocations, I can send that as a separate patch.

- HFSPLUS_KEY_LEN_SIZE is gone. hfs_find_init() now has the local
  constant you proposed:
  const size_t key_len_size = sizeof_field(hfsplus_btree_key, key_len);
  HFS_KEY_LEN_MAX_SIZE is gone from hfs as well. Its local is
  sizeof(u16), because the key length is a u16 in HFS_TREE_BIGKEYS
  trees, and a comment says so. The allocation size is unchanged in
  both drivers.

- key_len < 1: it came in with bb5e07cb9277 ("hfs/hfsplus: fix
  slab-out-of-bounds in hfs_bnode_read_key"), and neither the commit
  message nor its thread says why. As far as I can see it is never
  true. In hfsplus key_len is either hfs_bnode_read_u16() + 2 (2 to
  65537) or max_key_len + 2. In hfs it is hfs_bnode_read_u8() + 1 or
  max_key_len + 1. hfs_bnode_read() zeroes the buffer before it checks
  the offset, so even a bad offset gives a length of 2 (1 in hfs). The
  original patch printed key_len with %d, so it was probably an int at
  the time, but even then 0 and negative values could not reach the
  check. Now that key_len is a u32, a negative value is not possible at
  all. I left the check as it is so the fixes change only what they
  need to. If you agree, it can go in a separate cleanup.

- 3/3, fd->key_capacity in hfs_btree_inc_height(): no, that function
  only gets the tree, while its callers have the hfs_find_data. The
  key_size it reads from disk is used only to copy the key from the old
  root to the new root node and to place the child pointer.
  hfs_bnode_copy() and hfs_bnode_write() clamp that to the node through
  check_and_correct_requested_length(), and nothing is copied into
  search_key or key there. The tree-level bound would be
  max_key_len + 2, the same value as key_capacity. Rejecting a longer
  key there changes behaviour, so it belongs in its own fix, not in
  this no-functional-change cleanup. I can send that separately.

- 3/3, map_node_bits in hfsplus/xattr.c is now
  (node_size - HFS_BNODE_DESC_SIZE - (sizeof(u16) * 2) - 2) * 8.

- for-next: v4 did conflict with "hfs: fix orphaned b-tree node in
  hfs_bnode_split()". v5 is based on for-next (d536283b12c4). 1/3 and
  2/3 applied unchanged. In 3/3, hfs_bnode_split() in hfs now has the
  node_desc_size local from that commit, and it is converted the same
  way as the hfsplus one.

Tested on for-next d536283b12c4 (7.4-rc1) with KASAN in QEMU, with and
without the series:
- hfsplus crafted images: without the series, the mount of the first
  image fails with EINVAL after the overflow has corrupted the heap.
  KASAN then reports a use-after-free in rcu_cblist_dequeue() and the
  kernel oopses on a NULL pointer. With the series the key is rejected
  with "Invalid key length: 516", there is no KASAN report, and the two
  other images mount.
- hfsplus image made by mkfs.hfsplus: create, rename, delete, fill to
  ENOSPC, xattrs, appends and remount work, and fsck.hfsplus reports it
  clean, with and without the series.
- hfs image made by hformat: 400 creates, 200 deletes, fragmented
  appends and remount work, and hfsutils reads the result, with and
  without the series.

checkpatch --strict shows no errors. The remaining items are the
"Unknown commit id" warning for 1da177e4c3f4, which my shallow clone
does not have, and the extern CHECK on the hfs prototype, which is kept
to match its neighbours. Each patch builds on its own with W=1 and no
warnings. 1/3 and 2/3 still apply to 6.12.y and 6.6.y, except for the
same context conflict in hfs_find_init() as in v4.

v4: https://lore.kernel.org/all/20261127045416.1160-1-ozgunkultekin@gmail.com/
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/

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             | 15 ++++++++--
 fs/hfs/bnode.c             |  9 +++---
 fs/hfs/brec.c              | 49 +++++++++++++++++++-------------
 fs/hfs/btree.c             |  9 +++---
 fs/hfs/btree.h             |  4 ++-
 fs/hfsplus/bfind.c         | 11 ++++++--
 fs/hfsplus/bnode.c         |  9 +++---
 fs/hfsplus/brec.c          | 57 +++++++++++++++++++++++---------------
 fs/hfsplus/btree.c         |  9 +++---
 fs/hfsplus/hfsplus_fs.h    | 19 +++++++------
 fs/hfsplus/xattr.c         |  8 +++---
 include/linux/hfs_common.h |  6 ++++
 12 files changed, 129 insertions(+), 76 deletions(-)


base-commit: d536283b12c4597f8fc8cc65a034edb8753a9880
-- 
2.39.5


       reply	other threads:[~2026-12-01  9:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261127045416.1160-1-ozgunkultekin@gmail.com>
2026-12-01  9:42 ` Ozgun Kultekin [this message]
2026-12-01  9:42   ` [PATCH v5 1/3] hfsplus: " Ozgun Kultekin
2026-12-01  9:42   ` [PATCH v5 2/3] hfs: " Ozgun Kultekin
2026-12-01  9:42   ` [PATCH v5 3/3] hfs/hfsplus: name the node descriptor size and the first record offset Ozgun Kultekin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201094246.80197-1-ozgunkultekin@gmail.com \
    --to=ozgunkultekin@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cengiz.can@canonical.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=kovalev@altlinux.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=slava@dubeyko.com \
    --cc=stable@vger.kernel.org \
    --cc=yangguikun19@qq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®