Skip to contentSkip to conversation

hfsplus: fix duplicate bnode creation in hfs_bnode_create

PATCH In review Sainath Manda 2 participants 2 messages Jun 21 – Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hfsplus: fix duplicate bnode creation in hfs_bnode_create

Syzbot reported a WARNING in hfsplus_bnode_create. A maliciously crafted HFS+ image can request the creation of a duplicate bnode (e.g., bnode 0) that is mathematically within the tree->node_count bounds, but has already been hashed in memory. This bypasses initial boundary checks and triggers a WARN_ON(1) upon a duplicate hash collision.

This patch adds a clean state check in hfs_bnode_create. If a requested node is already hashed, the driver now safely rejects the operation with -EEXIST instead of tripping the kernel warning.

---
 fs/hfsplus/bnode.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/hfsplus/bnode.c +8 -2
index f8b5a8ae5..5946baad2 100644
@@ -626,12 +626,18 @@ struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
 	struct page **pagep;
 	int i;
 
+	if (num >= tree->node_count) {
+		pr_err("hfsplus: attempted to create invalid bnode %u (max %u)\n",
+		       num, tree->node_count);
+		return ERR_PTR(-EINVAL);
+	}
+
 	spin_lock(&tree->hash_lock);
 	node = hfs_bnode_findhash(tree, num);
 	spin_unlock(&tree->hash_lock);
 	if (node) {
-		pr_crit("new node %u already hashed?\n", num);
-		WARN_ON(1);
+		pr_err("hfsplus: attempted to create already hashed bnode %u\n",
+		       num);
 		return ERR_PTR(-EEXIST);
 	}
 	node = __hfs_bnode_create(tree, num);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 45 lines · Sainath Manda

Syzbot reported a WARNING in hfsplus_bnode_create. A maliciously crafted
HFS+ image can request the creation of a duplicate bnode (e.g., bnode
0)
that is mathematically within the tree->node_count bounds, but has already been hashed in memory. This bypasses initial boundary checks and
triggers a WARN_ON(1) upon a duplicate hash collision.

This patch adds a clean state check in hfs_bnode_create. If a requested
node is already hashed, the driver now safely rejects the operation with -EEXIST instead of tripping the kernel warning.

---
 fs/hfsplus/bnode.c | 10 ++++++++--

 1 file changed, 8 insertions(+), 2 deletions(-)
fs/hfsplus/bnode.c +0 -0
index f8b5a8ae5..5946baad2 100644
@@ -626,12 +626,18 @@ struct hfs_bnode *hfs_bnode_create(struct
 hfs_btree *tree, u32 num)
  	struct page **pagep;
  	int i;

fs/hfsplus/bnode.c +2 -0
+	if (num >= tree->node_count) {
+		pr_err("hfsplus: attempted to create invalid bnode

%u (max %u)\n",

fs/hfsplus/bnode.c +4 -0
+		       num, tree->node_count);
+		return ERR_PTR(-EINVAL);
+	}
+

  spin_lock(&tree->hash_lock);
  node = hfs_bnode_findhash(tree, num);
  spin_unlock(&tree->hash_lock);
  if (node) {

fs/hfsplus/bnode.c +1 -2
-		pr_crit("new node %u already hashed?\n", num);
-		WARN_ON(1);
+		pr_err("hfsplus: attempted to create already hashed

bnode %u\n",

fs/hfsplus/bnode.c +1 -0
+		       num);
Quoted 3 lines

  return ERR_PTR(-EEXIST);
  }
  node = __hfs_bnode_create(tree, num);

If we have corrupted HFS+ image, then we should not mount it. If I remember correctly, we already have patch that fixes this issue by checking b-tree's map during mount operation. Are you sure that you are running the latest state of the Linux kernel?

Thanks,
Slava.