Skip to contentSkip to conversation

hfs/hfsplus: serialize B-tree close against folio release

PATCH In review Bruno Produit 2 participants 3 messages Sep 21 – Sep 22

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

PATCH v2 1/1 hfs/hfsplus: serialize B-tree close against folio release

B-tree nodes with a zero reference count remain in the node hash until folio reclaim or tree teardown frees them. The folio release callbacks remove nodes while holding hash_lock, but hfs_btree_close() walks and frees the same hash without that lock. Reclaim can therefore unhash and free a node after close has loaded its pointer, causing a use-after-free or double-free.

The following syzkaller crashes seem to be the same UAF in {hfs,hfsplus}_btree_close() (or {hfs,hfsplus}_bnode_unhash()) and follow the same pattern, but do not contain a reproducer to confirm.

Detach each node with hfs_bnode_unhash() while holding hash_lock before inspecting and freeing it. Drop the lock before hfs_bnode_free() so a large tree is not freed while holding a spinlock. Apply the same fix to the matching HFS+ implementation.

---
 fs/hfs/btree.c     | 7 +++++--
 fs/hfsplus/btree.c | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)
fs/hfs/btree.c +5 -2
index 41b4e8fc9..4f0ddc76e 100644
@@ -310,14 +310,17 @@ void hfs_btree_close(struct hfs_btree *tree)
 		return;
 
 	for (i = 0; i < NODE_HASH_SIZE; i++) {
+		spin_lock(&tree->hash_lock);
 		while ((node = tree->node_hash[i])) {
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);
 			if (atomic_read(&node->refcnt))
 				pr_err("node %d:%d still has %d user(s)!\n",
 				       node->tree->cnid, node->this,
 				       atomic_read(&node->refcnt));
 			hfs_bnode_free(node);
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);
 		}
+		spin_unlock(&tree->hash_lock);
 	}
 	iput(tree->inode);
fs/hfsplus/btree.c +5 -2
index 2ea8cd565..bd4dbdbc8 100644
@@ -417,15 +417,18 @@ void hfs_btree_close(struct hfs_btree *tree)
 		return;
 
 	for (i = 0; i < NODE_HASH_SIZE; i++) {
+		spin_lock(&tree->hash_lock);
 		while ((node = tree->node_hash[i])) {
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);
 			if (atomic_read(&node->refcnt))
 				pr_crit("node %d:%d "
 						"still has %d user(s)!\n",
 					node->tree->cnid, node->this,
 					atomic_read(&node->refcnt));
 			hfs_bnode_free(node);
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);
 		}
+		spin_unlock(&tree->hash_lock);
 	}
 	iput(tree->inode);
#1 lore raw
Quoted 24 lines · Bruno Produit

B-tree nodes with a zero reference count remain in the node hash until
folio reclaim or tree teardown frees them.  The folio release callbacks
remove nodes while holding hash_lock, but hfs_btree_close() walks and frees the same hash without that lock.  Reclaim can therefore unhash and
free a node after close has loaded its pointer, causing a use-after- free
or double-free.

The following syzkaller crashes seem to be the same UAF in {hfs,hfsplus}_btree_close() (or {hfs,hfsplus}_bnode_unhash()) and follow
the same pattern, but do not contain a reproducer to confirm.

Detach each node with hfs_bnode_unhash() while holding hash_lock before
inspecting and freeing it.  Drop the lock before hfs_bnode_free() so a
large tree is not freed while holding a spinlock.  Apply the same fix to
the matching HFS+ implementation.

Quoted 11 lines

I still don't see Cc: [email protected] here.

Quoted 59 lines
Assisted-by Codex:gpt-5.6-sol

---
 fs/hfs/btree.c     | 7 +++++--
 fs/hfsplus/btree.c | 7 +++++--

 2 files changed, 10 insertions(+), 4 deletions(-)
fs/hfs/btree.c +0 -0
index 41b4e8fc9..4f0ddc76e 100644
@@ -310,14 +310,17 @@ void hfs_btree_close(struct hfs_btree *tree)
  		return;


  for (i = 0; i < NODE_HASH_SIZE; i++) {

fs/hfs/btree.c +1 -0
+		spin_lock(&tree->hash_lock);

  while ((node = tree->node_hash[i])) {

fs/hfs/btree.c +2 -1
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);

  if (atomic_read(&node->refcnt)) pr_err("node %d:%d still has %d user(s)!\n",
         node->tree->cnid, node->this, atomic_read(&node->refcnt)); hfs_bnode_free(node);

fs/hfs/btree.c +1 -1
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);

  }

fs/hfs/btree.c +1 -0
+		spin_unlock(&tree->hash_lock);

  }
  iput(tree->inode);

fs/hfsplus/btree.c +0 -0
index 2ea8cd565..bd4dbdbc8 100644
@@ -417,15 +417,18 @@ void hfs_btree_close(struct hfs_btree *tree)
  		return;


  for (i = 0; i < NODE_HASH_SIZE; i++) {

fs/hfsplus/btree.c +1 -0
+		spin_lock(&tree->hash_lock);

  while ((node = tree->node_hash[i])) {

fs/hfsplus/btree.c +2 -1
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);

  if (atomic_read(&node->refcnt)) pr_crit("node %d:%d " "still has %d user(s)!\n",
  node->tree->cnid, node-

Quoted 1 line

this,

  atomic_read(&node->refcnt)); hfs_bnode_free(node);

fs/hfsplus/btree.c +1 -1
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);

  }

fs/hfsplus/btree.c +1 -0
+		spin_unlock(&tree->hash_lock);
Quoted 2 lines

  }
  iput(tree->inode);

I haven't any comments for the fix. But let me run xfstests for the patch (at minimum for HFS+).

Thanks,
Slava.

maintainer Reviewed-by Tested-by
#2 lore raw
Quoted 106 lines · Viacheslav Dubeyko
Quoted 37 lines · Bruno Produit

B-tree nodes with a zero reference count remain in the node hash until
folio reclaim or tree teardown frees them.  The folio release callbacks
remove nodes while holding hash_lock, but hfs_btree_close() walks and
frees the same hash without that lock.  Reclaim can therefore unhash
and
free a node after close has loaded its pointer, causing a use- after-
free
or double-free.

The following syzkaller crashes seem to be the same UAF in {hfs,hfsplus}_btree_close() (or {hfs,hfsplus}_bnode_unhash()) and follow
the same pattern, but do not contain a reproducer to confirm.

Detach each node with hfs_bnode_unhash() while holding hash_lock before
inspecting and freeing it.  Drop the lock before hfs_bnode_free() so
a
large tree is not freed while holding a spinlock.  Apply the same fix
to
the matching HFS+ implementation.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by [email protected]

Closes:
https://syzkaller.appspot.com/bug?extid=d729df28d933979e017a

Reported-by [email protected]

Closes:
https://syzkaller.appspot.com/bug?extid=2eac7d175baf21e6a5d5

Reported-by [email protected]
Quoted 8 lines

Closes:
https://syzkaller.appspot.com/bug?extid=7155b2fe09e033c91381

Reported-by [email protected]

Closes:
https://syzkaller.appspot.com/bug?extid=adeb387cede15eb11607

Reported-by [email protected]

Closes:
https://syzkaller.appspot.com/bug?extid=ae7f2423f3648100506d

I still don't see Cc: [email protected] here.

Quoted 57 lines
Assisted-by Codex:gpt-5.6-sol

---
 fs/hfs/btree.c     | 7 +++++--
 fs/hfsplus/btree.c | 7 +++++--

 2 files changed, 10 insertions(+), 4 deletions(-)
fs/hfs/btree.c +0 -0
index 41b4e8fc9..4f0ddc76e 100644
@@ -310,14 +310,17 @@ void hfs_btree_close(struct hfs_btree *tree)
  		return;


  for (i = 0; i < NODE_HASH_SIZE; i++) {

fs/hfs/btree.c +1 -0
+		spin_lock(&tree->hash_lock);

  while ((node = tree->node_hash[i])) {

fs/hfs/btree.c +2 -1
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);

  if (atomic_read(&node->refcnt)) pr_err("node %d:%d still has %d user(s)!\n",
         node->tree->cnid, node-

Quoted 1 line

this,

         atomic_read(&node-

Quoted 1 line

refcnt));

  hfs_bnode_free(node);

fs/hfs/btree.c +1 -1
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);

  }

fs/hfs/btree.c +1 -0
+		spin_unlock(&tree->hash_lock);

  }
  iput(tree->inode);

fs/hfsplus/btree.c +0 -0
index 2ea8cd565..bd4dbdbc8 100644
@@ -417,15 +417,18 @@ void hfs_btree_close(struct hfs_btree *tree)
  		return;


  for (i = 0; i < NODE_HASH_SIZE; i++) {

fs/hfsplus/btree.c +1 -0
+		spin_lock(&tree->hash_lock);

  while ((node = tree->node_hash[i])) {

fs/hfsplus/btree.c +2 -1
-			tree->node_hash[i] = node->next_hash;
+			hfs_bnode_unhash(node);
+			spin_unlock(&tree->hash_lock);

  if (atomic_read(&node->refcnt)) pr_crit("node %d:%d " "still has %d user(s)!\n",
  node->tree->cnid, node-

Quoted 1 line

this,

  atomic_read(&node-

Quoted 1 line

refcnt));

Quoted 12 lines
Quoted 7 lines

  hfs_bnode_free(node);

fs/hfsplus/btree.c +1 -1
-			tree->node_hash_cnt--;
+			spin_lock(&tree->hash_lock);

  }

fs/hfsplus/btree.c +1 -0
+		spin_unlock(&tree->hash_lock);

  }
  iput(tree->inode);

I haven't any comments for the fix. But let me run xfstests for the patch (at minimum for HFS+).

I don't see any xfstests degradation for the case of HFS+. The patch looks good.

Thanks,
Slava.