linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcachefs: fix uninitialised-access bug in bch2_alloc_sectors_start_trans
@ 2025-05-28 19:06 Qasim Ijaz
  2025-05-30  4:39 ` Kent Overstreet
  0 siblings, 1 reply; 2+ messages in thread
From: Qasim Ijaz @ 2025-05-28 19:06 UTC (permalink / raw)
  To: kent.overstreet; +Cc: linux-bcachefs, linux-kernel

During bch2_alloc_sectors_start_trans() if bch2_trans_relock() fails
the code jumps to label "err", skipping "cl" initialisation. Once at
the "err" label "cl" could potentially be accessed leading to a uinit
access.

Fix this by moving "cl" initialisation before bch2_trans_relock().

Fixes: c2e7fa88544d ("bcachefs: Use a loop for open_bucket_add_buckets() retries")
Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
 fs/bcachefs/alloc_foreground.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/fs/bcachefs/alloc_foreground.c b/fs/bcachefs/alloc_foreground.c
index 1410a4afb36a..ccd51bcbcec5 100644
--- a/fs/bcachefs/alloc_foreground.c
+++ b/fs/bcachefs/alloc_foreground.c
@@ -1277,6 +1277,11 @@ int bch2_alloc_sectors_start_trans(struct btree_trans *trans,
 
 	req->data_type		= req->wp->data_type;
 
+	/* If we're going to fall back to the whole fs, try nonblocking first */
+	struct closure *cl = req->target && !(flags & BCH_WRITE_only_specified_devs)
+		? _cl
+		: NULL;
+
 	ret = bch2_trans_relock(trans);
 	if (ret)
 		goto err;
@@ -1285,10 +1290,6 @@ int bch2_alloc_sectors_start_trans(struct btree_trans *trans,
 	if (req->data_type != BCH_DATA_user)
 		req->have_cache = true;
 
-	/* If we're going to fall back to the whole fs, try nonblocking first */
-	struct closure *cl = req->target && !(flags & BCH_WRITE_only_specified_devs)
-		? _cl
-		: NULL;
 	while (1) {
 		ret = open_bucket_add_buckets(trans, req, cl);
 		if (!ret ||
-- 
2.39.5


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

* Re: [PATCH] bcachefs: fix uninitialised-access bug in bch2_alloc_sectors_start_trans
  2025-05-28 19:06 [PATCH] bcachefs: fix uninitialised-access bug in bch2_alloc_sectors_start_trans Qasim Ijaz
@ 2025-05-30  4:39 ` Kent Overstreet
  0 siblings, 0 replies; 2+ messages in thread
From: Kent Overstreet @ 2025-05-30  4:39 UTC (permalink / raw)
  To: Qasim Ijaz; +Cc: linux-bcachefs, linux-kernel

On Wed, May 28, 2025 at 08:06:10PM +0100, Qasim Ijaz wrote:
> During bch2_alloc_sectors_start_trans() if bch2_trans_relock() fails
> the code jumps to label "err", skipping "cl" initialisation. Once at
> the "err" label "cl" could potentially be accessed leading to a uinit
> access.
> 
> Fix this by moving "cl" initialisation before bch2_trans_relock().
> 
> Fixes: c2e7fa88544d ("bcachefs: Use a loop for open_bucket_add_buckets() retries")
> Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>

Thanks, folded this in.

> ---
>  fs/bcachefs/alloc_foreground.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/bcachefs/alloc_foreground.c b/fs/bcachefs/alloc_foreground.c
> index 1410a4afb36a..ccd51bcbcec5 100644
> --- a/fs/bcachefs/alloc_foreground.c
> +++ b/fs/bcachefs/alloc_foreground.c
> @@ -1277,6 +1277,11 @@ int bch2_alloc_sectors_start_trans(struct btree_trans *trans,
>  
>  	req->data_type		= req->wp->data_type;
>  
> +	/* If we're going to fall back to the whole fs, try nonblocking first */
> +	struct closure *cl = req->target && !(flags & BCH_WRITE_only_specified_devs)
> +		? _cl
> +		: NULL;
> +
>  	ret = bch2_trans_relock(trans);
>  	if (ret)
>  		goto err;
> @@ -1285,10 +1290,6 @@ int bch2_alloc_sectors_start_trans(struct btree_trans *trans,
>  	if (req->data_type != BCH_DATA_user)
>  		req->have_cache = true;
>  
> -	/* If we're going to fall back to the whole fs, try nonblocking first */
> -	struct closure *cl = req->target && !(flags & BCH_WRITE_only_specified_devs)
> -		? _cl
> -		: NULL;
>  	while (1) {
>  		ret = open_bucket_add_buckets(trans, req, cl);
>  		if (!ret ||
> -- 
> 2.39.5
> 

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

end of thread, other threads:[~2025-05-30  4:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-28 19:06 [PATCH] bcachefs: fix uninitialised-access bug in bch2_alloc_sectors_start_trans Qasim Ijaz
2025-05-30  4:39 ` Kent Overstreet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).