[PATCH] bcachefs: Add error handling for bch2_folio()

Wentao Liang posted 1 patch 11 months, 1 week ago
fs/bcachefs/fs-io-pagecache.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] bcachefs: Add error handling for bch2_folio()
Posted by Wentao Liang 11 months, 1 week ago
Add error handling for the case where bch2_folio() returns NULL
in __bch2_folio_set(). Return immediately to prevent null pointer
dereference.

Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 fs/bcachefs/fs-io-pagecache.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/bcachefs/fs-io-pagecache.c b/fs/bcachefs/fs-io-pagecache.c
index e072900e6a5b..1dc65fef3ff4 100644
--- a/fs/bcachefs/fs-io-pagecache.c
+++ b/fs/bcachefs/fs-io-pagecache.c
@@ -159,6 +159,9 @@ static void __bch2_folio_set(struct folio *folio,
 	struct bch_folio *s = bch2_folio(folio);
 	unsigned i, sectors = folio_sectors(folio);
 
+	if (!s)
+		return;
+
 	BUG_ON(pg_offset >= sectors);
 	BUG_ON(pg_offset + pg_len > sectors);
 
-- 
2.42.0.windows.2
Re: [PATCH] bcachefs: Add error handling for bch2_folio()
Posted by Kent Overstreet 11 months ago
On Tue, Mar 04, 2025 at 12:03:11PM +0800, Wentao Liang wrote:
> Add error handling for the case where bch2_folio() returns NULL
> in __bch2_folio_set(). Return immediately to prevent null pointer
> dereference.

There is no null ptr deref, this is only called from paths where we
preallocate bch_folio (and there's only two).


> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> ---
>  fs/bcachefs/fs-io-pagecache.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/bcachefs/fs-io-pagecache.c b/fs/bcachefs/fs-io-pagecache.c
> index e072900e6a5b..1dc65fef3ff4 100644
> --- a/fs/bcachefs/fs-io-pagecache.c
> +++ b/fs/bcachefs/fs-io-pagecache.c
> @@ -159,6 +159,9 @@ static void __bch2_folio_set(struct folio *folio,
>  	struct bch_folio *s = bch2_folio(folio);
>  	unsigned i, sectors = folio_sectors(folio);
>  
> +	if (!s)
> +		return;
> +
>  	BUG_ON(pg_offset >= sectors);
>  	BUG_ON(pg_offset + pg_len > sectors);
>  
> -- 
> 2.42.0.windows.2
>
Re: [PATCH] bcachefs: Add error handling for bch2_folio()
Posted by Markus Elfring 11 months ago
> Add error handling for the case where bch2_folio() returns NULL
> in __bch2_folio_set(). Return immediately to prevent null pointer
> dereference.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14-rc5#n145

Regards,
Markus
Re: [PATCH] bcachefs: Add error handling for bch2_folio()
Posted by Kent Overstreet 11 months ago
On Fri, Mar 07, 2025 at 07:01:03PM +0100, Markus Elfring wrote:
> > Add error handling for the case where bch2_folio() returns NULL
> > in __bch2_folio_set(). Return immediately to prevent null pointer
> > dereference.
> 
> How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
> https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14-rc5#n145

I'm only doing backports for _really_ critical fixes until the
experimental tag is lifted, so don't worry about it yet