mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guikun Yang <yangguikun19@qq.com>
To: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: "John Paul Adrian Glaubitz" <glaubitz@physik.fu-berlin.de>,
	"Yangtao Li" <frank.li@vivo.com>,
	"Özgün Kültekin" <ozgunkultekin@gmail.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hfsplus: bound hfs_bnode_read_key() against the per-tree key buffer size
Date: Mon, 16 Nov 2026 22:30:00 +0800	[thread overview]
Message-ID: <tencent_1CEFA39140271AD3A828251FDD28B254CE07@qq.com> (raw)
In-Reply-To: <2e910d5d5ab8dc719b4e540410a9e1024eafdfd5.camel@dubeyko.com>

Hi Slava,

The mismatch is between the declared type of fd->search_key and the
size it is actually allocated with.

hfs_find_init() allocates the key buffers from the per-tree key
length, not from the union size (fs/hfsplus/bfind.c:22):

	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
	fd->search_key = ptr;
	fd->key = ptr + tree->max_key_len + 2;

So search_key holds max_key_len + 2 bytes. For the extents tree,
max_key_len is forced to HFSPLUS_EXT_KEYLEN - sizeof(u16) = 10 at
mount time (btree.c:318), which gives search_key a 12-byte buffer.
The declared pointer type hfsplus_btree_key * suggests 518 bytes,
but only 12 bytes are actually there.

Now in hfs_bnode_read_key(), the existing bound is the union size
(bnode.c:87):

	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
		memset(key, 0, sizeof(hfsplus_btree_key));
		...
	}
	hfs_bnode_read(node, key, off, key_len);

key_len is attacker-controlled in the leaf/VARIDXKEYS/attr branch
(u16 read from the node plus 2). Anything up to 518 passes this
check, and hfs_bnode_read() writes it into the search_key of an
extents-tree hfs_find_data (a 24-byte kzalloc total, of which
search_key is the first 12). That is the slab-out-of-bounds write
from my report:

	BUG: KASAN: slab-out-of-bounds in hfsplus_bnode_read+0x30/0x5f0
	Write of size 502 at addr ... by task init/1
	 hfsplus_bnode_read -> hfs_bnode_read_key
	 hfs_brec_insert -> __hfsplus_ext_write_extent
	 ...
	Allocated by task 1:
	 hfs_find_init (kzalloc 24)

reached via hfs_brec_insert() -> hfs_bnode_read_key(new_node,
fd->search_key, 14) on an extents tree (brec.c:169); the parent-key
update path at brec.c:151 reads into the same buffer the same way.

So memset(key, 0, sizeof(hfsplus_btree_key)) is unsafe in this
function too: the 518-byte clear runs off the same 12-byte
search_key, which is why the patch bounds both the read and the
clear with the same expression.

About "what if max_key_len is corrupted": it cannot reach the tree
at all. hfs_btree_open() validates it against fixed constants per
tree id (btree.c:318/331/350):

	case HFSPLUS_EXT_CNID:
		if (tree->max_key_len != HFSPLUS_EXT_KEYLEN - sizeof(u16))
			goto fail_page;
	case HFSPLUS_CAT_CNID:
		if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16))
			goto fail_page;
	case HFSPLUS_ATTR_CNID:
		if (tree->max_key_len != HFSPLUS_ATTR_KEYLEN - sizeof(u16))
			goto fail_page;

A corrupted max_key_len fails the mount. At runtime it is always
10, 516 or 266. And the bound stays consistent even in the
hypothetical case: search_key is allocated from max_key_len itself,
so a bound derived from max_key_len can never exceed the buffer,
whatever the value is.

That is also why I would argue max_key_len + 2 is the opposite of a
hardcoded value: sizeof(hfsplus_btree_key) = 518 is the constant
that has no relation to the allocation - it matches the catalog
tree only by coincidence (516 + 2) and overshoots extents by 506
bytes and attributes by 250 bytes. max_key_len + 2 is the only
expression that tracks the kzalloc() in hfs_find_init() one to one.

Both sizes in the patch come from the same source: the check
(key_len > max_key_len + 2) and the memset use the same expression
as the allocation. If you prefer to avoid recomputing it, storing
key_buf_len = max_key_len + 2 in struct hfs_btree at mount time
would make the correspondence explicit - happy to rework in that
shape if you prefer.

Thanks,
Guikun Yang


      reply	other threads:[~2026-11-17  5:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <c44-hfsplus-extkey-oob-20261112@qq.com>
2026-11-12 14:00 ` [Bug report] hfsplus: extents-tree search_key buffer is 24 bytes but hfs_bnode_read_key() bounds key reads against 518 - slab OOB write on fsync Guikun Yang
2026-11-13 19:16   ` Viacheslav Dubeyko
2026-11-14 10:00   ` [PATCH] hfsplus: bound hfs_bnode_read_key() against the per-tree key buffer size Guikun Yang
2026-11-16 20:57     ` Viacheslav Dubeyko
2026-11-16 14:30       ` Guikun Yang [this message]

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=tencent_1CEFA39140271AD3A828251FDD28B254CE07@qq.com \
    --to=yangguikun19@qq.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ozgunkultekin@gmail.com \
    --cc=slava@dubeyko.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®