Re: [PATCH] hfsplus: validate attribute key name length

From: Viacheslav Dubeyko

Date: Thu Dec 03 2026 - 18:34:12 EST


On Fri, 2026-12-04 at 01:48 +0900, Kyuwon Cho wrote:
> An attributes B-tree key carries both an outer key length and an
> embedded
> Unicode string length. hfs_brec_keylen() bounds the former, but
> hfsplus_attr_bin_cmp_key() trusts the latter. A corrupted attributes
> tree
> can therefore make hfsplus_strcmp() read beyond the key buffer.
>
> Require the embedded name length to fit the format limit and to
> describe
> the same key size as the outer length before copying the key out of
> the
> B-tree node.

Thanks for the report and the patch. The issue is real. The attributes
tree's max_key_len is fixed at HFSPLUS_ATTR_KEYLEN - 2 by
hfs_btree_open(), so the key buffer has room for only 127 Unicode
characters, while hfsplus_strcmp() clamps the length to
HFSPLUS_MAX_STRLEN (255) only.

However, I believe the fix is incomplete. Please, see my comments
below.

>
> Fixes: 3e05ca20fb57 ("hfsplus: add functionality of manipulating by
> records in attributes tree")
> Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
> ---
> The issue was found by an LLM-guided test generator that we are
> developing as a research prototype.
>
> The reproducer creates and mounts a crafted HFS+ image, then removes
> a
> file whose attribute record has an embedded name length larger than
> its
> key. Mounting the image requires root in the initial namespace.
>
> The decoded trace is:
>
>   BUG: KASAN: slab-out-of-bounds in hfsplus_strcmp
> (fs/hfsplus/unicode.c:107)
>   Read of size 2 at addr ff11000012fa4a18 by task test/116
>   Call Trace:
>    hfsplus_strcmp (fs/hfsplus/unicode.c:107)
>    hfs_find_rec_by_key (fs/hfsplus/bfind.c:90)
>    __hfsplus_brec_find (fs/hfsplus/bfind.c:129)
>    hfsplus_brec_remove (fs/hfsplus/brec.c:224)
>    __hfsplus_delete_attr (fs/hfsplus/attributes.c:339)
>    hfsplus_delete_all_attrs (fs/hfsplus/attributes.c:444)
>    hfsplus_delete_cat (fs/hfsplus/catalog.c:415)
>    hfsplus_unlink (fs/hfsplus/dir.c:400)
>
> The patch was tested on v7.4-rc5 and rebased onto the HFS tree commit
> a2c27efbbd43 ("hfsplus: fix UAF in hfsplus_cat_write_inode()").
>
>  fs/hfsplus/brec.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
> index f416e562d439..b75dbadbab9b 100644
> --- a/fs/hfsplus/brec.c
> +++ b/fs/hfsplus/brec.c
> @@ -66,6 +66,18 @@ u16 hfs_brec_keylen(struct hfs_bnode *node, u16
> rec)
>   retval);
>   retval = U16_MAX;
>   }

If the outer key length is too large, retval is already U16_MAX here,
but we still fall through into the new check and print a second error
for the same record. Could we return early (or use else if) here?

Frankly speaking, the function becomes grow. We need to refactor the
code. Even now the code looks not very clear.

> + if (node->tree->cnid == HFSPLUS_ATTR_CNID) {

We check tree CNID in multiple places. Probably, we need some helper
function(s).

> + u16 name_len = hfs_bnode_read_u16(node,
> recoff +
> + offsetof(struct hfsplus_attr_key,
> key_name.length));

The record length (len) is not checked against
offsetof(struct hfsplus_attr_key, key_name.unicode) before reading the
name length. hfs_bnode_read_u16() stays within the node, so this is not
a memory safety issue, but for a short record we read the name length
from the next record or from the offset table. I would prefer to reject
such a record explicitly instead of relying on the equality check below
failing by accident.

Frankly speaking, logic looks confusing. I think we need to make the
code more easy to understand.

> +
> + if (name_len > HFSPLUS_ATTR_MAX_STRLEN ||
> +     retval != offsetof(struct
> hfsplus_attr_key,
> +        key_name.unicode) +
> +       name_len *
> sizeof(hfsplus_unichr)) {

It looks like we need a dedicated function for this check.

retval != offsetof(struct hfsplus_attr_key,
+ key_name.unicode) +
+ name_len *
sizeof(hfsplus_unichr)

Are you sure that this is correct? This logic is complicated and
require more clean code.


> + pr_err("invalid attribute name
> length\n");

Could you please make the message more informative? For example,
the name length, key length, node ID and record index. It would help
a lot in analyzing corruption reports.

Thanks,
Slava.

> + return U16_MAX;
> + }
> + }
>   }
>   return retval;
>  }