Re: [PATCH v2] hfsplus: validate attribute key name length on every read path
From: Viacheslav Dubeyko
Date: Fri Dec 04 2026 - 18:16:08 EST
On Fri, 2026-12-04 at 15:34 +0900, Kyuwon Cho wrote:
> Attribute keys carry an outer key length and an embedded Unicode name
> length. hfsplus_attr_bin_cmp_key() trusts the embedded length, so an
> inconsistent key can make it read beyond the key buffer.
>
> Validate the lengths in a common helper used by hfs_brec_keylen() and
> hfs_bnode_read_key(). The latter covers keys copied while inserting a
> record, updating a parent, or splitting a node. Check that the record
> is
> large enough before reading the embedded length, and include the node
> and
> record numbers in corruption messages.
Thanks a lot for the v2. It addresses all my comments on v1.
However, I still have one important concern and a few minor comments.
>
> Fixes: 3e05ca20fb57 ("hfsplus: add functionality of manipulating by
> records in attributes tree")
> Suggested-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
> Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
> ---
> Changes in v2:
> - Move attribute-key validation into a helper used by both
> hfs_brec_keylen() and hfs_bnode_read_key(), covering insert, parent
> update, and node-split key copies, as requested by Viacheslav
> Dubeyko.
> - Reject short records before reading the embedded name length,
> return
> immediately after an oversized outer key, and report the name
> length,
> key length, node ID, and record index.
> - Rebase on Ozgun Kultekin's v5 key-buffer-size series.
>
> v1:
> https://lore.kernel.org/all/179619831256.1663618.16544869010167759924@xxxxxxxx/
> Base series:
> https://lore.kernel.org/all/20261201094246.80197-1-ozgunkultekin@xxxxxxxxx/
>
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> Tested with setxattr-driven attribute-tree node splits on a valid
> HFS+
> volume; the attributes remain readable after remount.
Could you please confirm that the v1 reproducer (hfsplus_brec_remove()
path) doesn't trigger the KASAN report with v2 applied? And did you
check the split path with a corrupted key (when the invalid key ends up
as the first record of the node)?
>
> The reproducer is attached to my reply in the v1 thread.
>
> fs/hfsplus/bnode.c | 12 +++++++++++-
> fs/hfsplus/brec.c | 43 ++++++++++++++++++++++++++++++++++-----
> --
> fs/hfsplus/hfsplus_fs.h | 4 +++-
> 3 files changed, 50 insertions(+), 9 deletions(-)
>
> diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
> index d91714e97b9b..97e570b10853 100644
> --- a/fs/hfsplus/bnode.c
> +++ b/fs/hfsplus/bnode.c
> @@ -71,7 +71,7 @@ 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,
> u16 rec,
> u32 key_capacity)
> {
> struct hfs_btree *tree;
> @@ -90,6 +90,16 @@ void hfs_bnode_read_key(struct hfs_bnode *node,
> void *key, u32 off,
> pr_err("hfsplus: Invalid key length: %u\n",
> key_len);
> return;
> }
> + if (tree->cnid == HFSPLUS_ATTR_CNID) {
> + u16 rec_off;
> + u16 rec_len = hfs_brec_lenoff(node, rec, &rec_off);
> +
> + if (hfs_brec_len_invalid(node, rec_len) || rec_off
> != off ||
> + !hfsplus_attr_key_valid(node, rec, off, key_len,
> rec_len)) {
> + memset(key, 0, key_capacity);
> + return;
> + }
> + }
The main problem is that hfs_bnode_read_key() is still a void function.
If the key is invalid, then we zero the key and the caller simply
continues with it. For example, after a node split, hfs_brec_insert()
calls __hfs_brec_find() for the parent node with zeroed search key and
inserts an index record with this zeroed key. And
hfs_brec_update_parent() will search for the parent's index record by
means of zeroed key. So, we avoid the out-of-bounds read, but we
convert the detected corruption into new corruption of the b-tree, and
the operation reports success.
I think hfs_bnode_read_key() should return an error (-EIO) for an
invalid key, and hfs_brec_insert() and hfs_brec_update_parent() should
propagate it.
Also, the case of invalid record length or rec_off != off is completely
silent here. The rec_off != off case can happen only if the caller
passes a wrong record index, which is a kernel bug rather than volume
corruption. So, I think WARN_ON_ONCE() is more appropriate for it.
Moreover, both off and rec are provided to the function, but they carry
the same information. Every caller knows the record index, so the
function could take only rec and retrieve the offset by
hfs_brec_lenoff(). Then, we don't need to check the consistency of off
and rec at all.
>
> hfs_bnode_read(node, key, off, key_len);
> }
> diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
> index c56ca7a852b1..babbd51a9db6 100644
> --- a/fs/hfsplus/brec.c
> +++ b/fs/hfsplus/brec.c
> @@ -18,6 +18,34 @@ static struct hfs_bnode *hfs_bnode_split(struct
> hfs_find_data *fd);
> static int hfs_brec_update_parent(struct hfs_find_data *fd);
> static int hfs_btree_inc_height(struct hfs_btree *);
>
> +bool hfsplus_attr_key_valid(struct hfs_bnode *node, u16 rec, u32
> key_off,
> + u16 key_len, u16 rec_len)
> +{
> + const u16 min_key_len =
> + offsetof(struct hfsplus_attr_key, key_name.unicode);
> + u16 name_len;
> +
> + if (node->tree->cnid != HFSPLUS_ATTR_CNID)
> + return true;
hfs_bnode_read_key() already checks the tree ID before calling the
helper. Could we keep only one of these checks?
Thanks,
Slava.
> +
> + if (rec_len < min_key_len) {
> + pr_err("hfsplus: attribute record length %u is too
> short for key length %u in node %u record %u\n",
> + rec_len, key_len, node->this, rec);
> + return false;
> + }
> +
> + name_len = hfs_bnode_read_u16(node, key_off +
> + offsetof(struct hfsplus_attr_key,
> key_name.length));
> + if (name_len > HFSPLUS_ATTR_MAX_STRLEN || key_len > rec_len
> ||
> + key_len != min_key_len + 2 * name_len) {
> + pr_err("hfsplus: invalid attribute name length %u
> for key length %u in node %u record %u\n",
> + name_len, key_len, node->this, rec);
> + return false;
> + }
> +
> + return true;
> +}
> +
> /* Get the length and offset of the given record in the given node
> */
> u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off)
> {
> @@ -62,10 +90,11 @@ u16 hfs_brec_keylen(struct hfs_bnode *node, u16
> rec)
>
> retval = hfs_bnode_read_u16(node, recoff) + 2;
> if (retval > node->tree->max_key_len + 2) {
> - pr_err("keylen %d too large\n",
> - retval);
> - retval = U16_MAX;
> + pr_err("keylen %d too large\n", retval);
> + return U16_MAX;
> }
> + if (!hfsplus_attr_key_valid(node, rec, recoff,
> retval, len))
> + return U16_MAX;
> }
> return retval;
> }
> @@ -148,7 +177,7 @@ 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, 1,
> fd->key_capacity);
> hfs_brec_update_parent(fd);
> }
> @@ -168,7 +197,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_FIRST_REC_OFF, fd-
> >key_capacity);
> + HFS_BNODE_FIRST_REC_OFF, 0, fd-
> >key_capacity);
> __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
>
> hfs_bnode_put(new_node);
> @@ -468,7 +497,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_FIRST_REC_OFF, fd-
> >key_capacity);
> + HFS_BNODE_FIRST_REC_OFF, 0, fd-
> >key_capacity);
> cnid = cpu_to_be32(new_node->this);
>
> __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
> @@ -481,7 +510,7 @@ 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_FIRST_REC_OFF,
> + HFS_BNODE_FIRST_REC_OFF,
> 0,
> fd->key_capacity);
> }
> new_node = NULL;
> diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
> index 43062f056f8c..963c664172ca 100644
> --- a/fs/hfsplus/hfsplus_fs.h
> +++ b/fs/hfsplus/hfsplus_fs.h
> @@ -405,7 +405,7 @@ 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,
> u16 rec,
> u32 key_capacity);
> 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);
> @@ -427,6 +427,8 @@ bool hfs_bnode_need_zeroout(struct hfs_btree
> *tree);
> /* brec.c */
> u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off);
> u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec);
> +bool hfsplus_attr_key_valid(struct hfs_bnode *node, u16 rec, u32
> key_off,
> + u16 key_len, u16 rec_len);
> int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32
> entry_len);
> int hfs_brec_remove(struct hfs_find_data *fd);
>