[PATCH v3] hfsplus: validate attribute key name length on every read path
From: Kyuwon Cho
Date: Sun Dec 06 2026 - 03:06:44 EST
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(). Have hfs_bnode_read_key() derive the offset from the
record index and return -EIO for an invalid key. Propagate that error from
record insertion and parent updates so detected corruption cannot be turned
into a zero-key B-tree update.
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 v3:
- Make hfs_bnode_read_key() return and propagate -EIO instead of
continuing with a zeroed key, as requested by Viacheslav Dubeyko.
- Pass only the record index and derive the record offset internally;
remove the duplicate attribute-tree check from the validator.
- Add a corrupt split-node first-key test. The v2 code continued with a
zeroed key; this version returns -EIO without modifying the key buffer.
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/
v2: https://lore.kernel.org/all/179636379461.1106205.12507366887678809732@xxxxxxxx/
Review reply: https://lore.kernel.org/all/179643157800.1720719.12188333346797756271@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.
A reproducer is available on request.
fs/hfsplus/bnode.c | 19 ++++++++---
fs/hfsplus/brec.c | 72 ++++++++++++++++++++++++++++++++---------
fs/hfsplus/hfsplus_fs.h | 6 ++--
3 files changed, 75 insertions(+), 22 deletions(-)
diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
index d91714e97b9b..c67a23565167 100644
--- a/fs/hfsplus/bnode.c
+++ b/fs/hfsplus/bnode.c
@@ -71,13 +71,21 @@ 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,
- u32 key_capacity)
+int hfs_bnode_read_key(struct hfs_bnode *node, void *key, u16 rec,
+ u32 key_capacity)
{
struct hfs_btree *tree;
+ u16 off, rec_len;
u32 key_len;
tree = node->tree;
+ rec_len = hfs_brec_lenoff(node, rec, &off);
+ if (hfs_brec_len_invalid(node, rec_len)) {
+ pr_err("hfsplus: invalid record length in node %u record %u\n",
+ node->this, rec);
+ return -EIO;
+ }
+
if (node->type == HFS_NODE_LEAF ||
tree->attributes & HFS_TREE_VARIDXKEYS ||
node->tree->cnid == HFSPLUS_ATTR_CNID)
@@ -86,12 +94,15 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
key_len = tree->max_key_len + 2;
if (key_len > key_capacity || key_len < 1) {
- memset(key, 0, key_capacity);
pr_err("hfsplus: Invalid key length: %u\n", key_len);
- return;
+ return -EIO;
}
+ if (tree->cnid == HFSPLUS_ATTR_CNID &&
+ !hfsplus_attr_key_valid(node, rec, off, key_len, rec_len))
+ return -EIO;
hfs_bnode_read(node, key, off, key_len);
+ return 0;
}
void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
index c56ca7a852b1..ead231cb900d 100644
--- a/fs/hfsplus/brec.c
+++ b/fs/hfsplus/brec.c
@@ -18,6 +18,31 @@ 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 (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 +87,12 @@ 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 (node->tree->cnid == HFSPLUS_ATTR_CNID &&
+ !hfsplus_attr_key_valid(node, rec, recoff, retval, len))
+ return U16_MAX;
}
return retval;
}
@@ -74,7 +101,7 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
{
struct hfs_btree *tree;
struct hfs_bnode *node, *new_node;
- int size, key_len, rec;
+ int size, key_len, rec, res;
int data_off, end_off;
int idx_rec_off, data_rec_off, end_rec_off;
__be32 cnid;
@@ -148,9 +175,13 @@ 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,
- fd->key_capacity);
- hfs_brec_update_parent(fd);
+ res = hfs_bnode_read_key(node, fd->search_key, 1,
+ fd->key_capacity);
+ if (res)
+ return res;
+ res = hfs_brec_update_parent(fd);
+ if (res)
+ return res;
}
if (new_node) {
@@ -167,8 +198,12 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
entry_len = sizeof(cnid);
/* get index key */
- hfs_bnode_read_key(new_node, fd->search_key,
- HFS_BNODE_FIRST_REC_OFF, fd->key_capacity);
+ res = hfs_bnode_read_key(new_node, fd->search_key, 0,
+ fd->key_capacity);
+ if (res) {
+ hfs_bnode_put(new_node);
+ return res;
+ }
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
hfs_bnode_put(new_node);
@@ -248,7 +283,7 @@ int hfs_brec_remove(struct hfs_find_data *fd)
skip:
hfs_bnode_dump(node);
if (!fd->record)
- hfs_brec_update_parent(fd);
+ return hfs_brec_update_parent(fd);
return 0;
}
@@ -467,12 +502,16 @@ 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);
+ res = hfs_bnode_read_key(new_node, fd->search_key, 0,
+ fd->key_capacity);
+ if (res)
+ return res;
cnid = cpu_to_be32(new_node->this);
__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
- hfs_brec_insert(fd, &cnid, sizeof(cnid));
+ res = hfs_brec_insert(fd, &cnid, sizeof(cnid));
+ if (res)
+ return res;
hfs_bnode_put(fd->bnode);
hfs_bnode_put(new_node);
@@ -480,9 +519,10 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
if (new_node == node)
goto out;
/* restore search_key */
- hfs_bnode_read_key(node, fd->search_key,
- HFS_BNODE_FIRST_REC_OFF,
- fd->key_capacity);
+ res = hfs_bnode_read_key(node, fd->search_key, 0,
+ fd->key_capacity);
+ if (res)
+ return res;
}
new_node = NULL;
}
diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
index 43062f056f8c..271565e9de37 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -405,8 +405,8 @@ 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,
- u32 key_capacity);
+int hfs_bnode_read_key(struct hfs_bnode *node, void *key, 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);
void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
@@ -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);
--
2.43.0