mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] hfsplus: validate attribute key name length on every read path
@ 2026-12-04  6:34 Kyuwon Cho
  2026-12-04 23:15 ` Viacheslav Dubeyko
  0 siblings, 1 reply; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-04  6:34 UTC (permalink / raw)
  To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li
  Cc: Ozgun Kultekin, linux-fsdevel, linux-kernel

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.

Fixes: 3e05ca20fb57 ("hfsplus: add functionality of manipulating by records in attributes tree")
Suggested-by: Viacheslav Dubeyko <slava@dubeyko.com>
Signed-off-by: Kyuwon Cho <kyuwon.cho@skku.edu>
---
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@skku.edu/
Base series: https://lore.kernel.org/all/20261201094246.80197-1-ozgunkultekin@gmail.com/

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.

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;
+		}
+	}
 
 	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;
+
+	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);
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05  1:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  6:34 [PATCH v2] hfsplus: validate attribute key name length on every read path Kyuwon Cho
2026-12-04 23:15 ` Viacheslav Dubeyko
2026-12-05  1:46   ` Kyuwon Cho

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®