mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viacheslav Dubeyko <slava@dubeyko.com>
To: Mahmut Emin Kurhan <guvenlik@noroxi.com>, linux-fsdevel@vger.kernel.org
Cc: glaubitz@physik.fu-berlin.de, frank.li@vivo.com,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] hfsplus: free cached B-tree nodes on hfs_btree_open() error path
Date: Mon, 05 Oct 2026 12:16:37 -0700	[thread overview]
Message-ID: <0eb39e561e4c81c0fe2198a2ba352ba81f8d94b7.camel@dubeyko.com> (raw)
In-Reply-To: <20261001213807.1519745-2-guvenlik@noroxi.com>

On Thu, 2026-10-01 at 23:38 +0200, Mahmut Emin Kurhan wrote:
> hfs_btree_open() can fail after hfs_bnode_find(tree,
> HFSPLUS_TREE_HEAD) has
> already inserted the head node into tree->node_hash.
> 
> __hfs_bnode_create() inserts the new bnode into tree->node_hash
> before it
> reads the node pages; if a page read fails it sets HFS_BNODE_ERROR
> and
> returns the node still hashed. hfs_bnode_find() then takes its
> node_error
> path and calls hfs_bnode_put(), which only frees a node once its
> refcount
> reaches zero *and* HFS_BNODE_DELETED is set. For the errored head
> node that
> flag is not set, so it stays in tree->node_hash with a zero refcount.
> 
> hfs_btree_open() then sees IS_ERR(node) and jumps to free_tree:,
> doing a
> bare kfree(tree). Only hfs_btree_close() walks tree->node_hash[] and
> frees
> the cached nodes, so the head node is leaked. Mounting a crafted HFS+
> image
> whose head B-tree node fails to read leaks kernel memory on every
> attempt.
> 
> Factor the node-hash freeing out of hfs_btree_close() into a small
> helper
> hfs_bnode_hash_free() and call it from both hfs_btree_close() and the
> hfs_btree_open() error path. The helper keeps the hash_lock
> serialization
> added in commit dce0e0248205 ("hfs/hfsplus: serialize B-tree close

I think it is not good idea of mentioning this. This patch is not
upstream yet.

> against
> folio release"); on the open error path the tree has not been
> published yet,
> so the lock is uncontended but harmless.
> 
> Found via coverage-guided fuzzing (syzkaller + kmemleak) by Noroxi.

Should we use Reported-by: tag?

> 
> Signed-off-by: Mahmut Emin Kurhan <guvenlik@noroxi.com>
> ---
>  fs/hfsplus/btree.c | 41 +++++++++++++++++++++++------------------
>  1 file changed, 23 insertions(+), 18 deletions(-)
> 
> diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
> index bd4dbdbc8f..2183828d5d 100644
> --- a/fs/hfsplus/btree.c
> +++ b/fs/hfsplus/btree.c
> @@ -265,6 +265,27 @@ static const char *hfs_btree_name(u32 cnid)
>  }
>  
>  /* Get a reference to a B*Tree and do some initial checks */

Are you sure that it is correct place for the comment?

> +static void hfs_bnode_hash_free(struct hfs_btree *tree)

Maybe, hfs_btree_free_nodes() because function lives in btree.c?

> +{
> +	struct hfs_bnode *node;
> +	int i;
> +
> +	for (i = 0; i < NODE_HASH_SIZE; i++) {
> +		spin_lock(&tree->hash_lock);
> +		while ((node = tree->node_hash[i])) {
> +			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);
> +			spin_lock(&tree->hash_lock);
> +		}
> +		spin_unlock(&tree->hash_lock);
> +	}
> +}
> +
>  struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
>  {
>  	struct hfs_btree *tree;
> @@ -403,6 +424,7 @@ struct hfs_btree *hfs_btree_open(struct
> super_block *sb, u32 id)
>  	tree->inode->i_mapping->a_ops = &hfsplus_aops;
>  	iput(tree->inode);
>   free_tree:
> +	hfs_bnode_hash_free(tree);

We have inconsistency here. The hfs_btree_close() calls
hfs_bnode_hash_free() and iput() then. Here we have opposite sequence.

Thanks,
Slava.

>  	kfree(tree);
>  	return NULL;
>  }
> @@ -410,27 +432,10 @@ struct hfs_btree *hfs_btree_open(struct
> super_block *sb, u32 id)
>  /* Release resources used by a btree */
>  void hfs_btree_close(struct hfs_btree *tree)
>  {
> -	struct hfs_bnode *node;
> -	int i;
> -
>  	if (!tree)
>  		return;
>  
> -	for (i = 0; i < NODE_HASH_SIZE; i++) {
> -		spin_lock(&tree->hash_lock);
> -		while ((node = tree->node_hash[i])) {
> -			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);
> -			spin_lock(&tree->hash_lock);
> -		}
> -		spin_unlock(&tree->hash_lock);
> -	}
> +	hfs_bnode_hash_free(tree);
>  	iput(tree->inode);
>  	kfree(tree);
>  }

  reply	other threads:[~2026-10-05 19:16 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30 18:50 [PATCH] " Mahmut Emin Kurhan
2026-09-30 23:04 ` Viacheslav Dubeyko
2026-09-30 23:23   ` [PATCH v2 0/2] hfsplus, hfs: fix B-tree node leak " Mahmut Emin Kurhan
2026-09-30 23:23     ` [PATCH v2 1/2] hfsplus: free cached B-tree nodes " Mahmut Emin Kurhan
2026-10-01 20:03       ` Viacheslav Dubeyko
2026-10-01 21:38         ` [PATCH v3 0/2] hfsplus, hfs: fix B-tree node leak " Mahmut Emin Kurhan
2026-10-01 21:38           ` [PATCH v3 1/2] hfsplus: free cached B-tree nodes " Mahmut Emin Kurhan
2026-10-05 19:16             ` Viacheslav Dubeyko [this message]
2026-10-01 21:38           ` [PATCH v3 2/2] hfs: " Mahmut Emin Kurhan
2026-10-05 19:20             ` Viacheslav Dubeyko
2026-10-02 18:18           ` [PATCH v3 0/2] hfsplus, hfs: fix B-tree node leak " Viacheslav Dubeyko
2026-10-05 18:51             ` Viacheslav Dubeyko
2026-09-30 23:23     ` [PATCH v2 2/2] hfs: free cached B-tree nodes " Mahmut Emin Kurhan
2026-10-01 20:03       ` Viacheslav Dubeyko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0eb39e561e4c81c0fe2198a2ba352ba81f8d94b7.camel@dubeyko.com \
    --to=slava@dubeyko.com \
    --cc=frank.li@vivo.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=guvenlik@noroxi.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®