[PATCH] bcachefs: fix potential NULL pointer dereference in bch2_bkey_buf_realloc

Ye Chey posted 1 patch 6 months, 3 weeks ago
fs/bcachefs/bkey_buf.h | 2 ++
1 file changed, 2 insertions(+)
[PATCH] bcachefs: fix potential NULL pointer dereference in bch2_bkey_buf_realloc
Posted by Ye Chey 6 months, 3 weeks ago
Add error checking for mempool_alloc return value to avoid potential NULL
pointer dereference when memory allocation fails.

Signed-off-by: Ye Chey <yechey@ai-sast.com>
---
 fs/bcachefs/bkey_buf.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/bcachefs/bkey_buf.h b/fs/bcachefs/bkey_buf.h
index a30c4ae8e..8272a1830 100644
--- a/fs/bcachefs/bkey_buf.h
+++ b/fs/bcachefs/bkey_buf.h
@@ -16,6 +16,8 @@ static inline void bch2_bkey_buf_realloc(struct bkey_buf *s,
 	if (s->k == (void *) s->onstack &&
 	    u64s > ARRAY_SIZE(s->onstack)) {
 		s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS);
+		if (!s->k)
+			return;
 		memcpy(s->k, s->onstack, sizeof(s->onstack));
 	}
 }
-- 
2.44.0
Re: [PATCH] bcachefs: fix potential NULL pointer dereference in bch2_bkey_buf_realloc
Posted by Kent Overstreet 6 months, 3 weeks ago
On Wed, May 21, 2025 at 10:30:33PM +0800, Ye Chey wrote:
> Add error checking for mempool_alloc return value to avoid potential NULL
> pointer dereference when memory allocation fails.

I suggest you go learn how mempools work and what they're for.


> Signed-off-by: Ye Chey <yechey@ai-sast.com>
> ---
>  fs/bcachefs/bkey_buf.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/bcachefs/bkey_buf.h b/fs/bcachefs/bkey_buf.h
> index a30c4ae8e..8272a1830 100644
> --- a/fs/bcachefs/bkey_buf.h
> +++ b/fs/bcachefs/bkey_buf.h
> @@ -16,6 +16,8 @@ static inline void bch2_bkey_buf_realloc(struct bkey_buf *s,
>  	if (s->k == (void *) s->onstack &&
>  	    u64s > ARRAY_SIZE(s->onstack)) {
>  		s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS);
> +		if (!s->k)
> +			return;
>  		memcpy(s->k, s->onstack, sizeof(s->onstack));
>  	}
>  }
> -- 
> 2.44.0
>