All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
@ 2022-03-22  8:08 xkernel.wang
  2022-03-22  8:28   ` Gao Xiang
  0 siblings, 1 reply; 5+ messages in thread
From: xkernel.wang @ 2022-03-22  8:08 UTC (permalink / raw)
  To: xiang, chao; +Cc: linux-erofs, linux-kernel, Xiaoke Wang

From: Xiaoke Wang <xkernel.wang@foxmail.com>

alloc_pages() returns the page on success or NULL if allocation fails,
while set_page_private() will dereference `newpage`. So it is better to
catch the memory error in case other errors happen.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 fs/erofs/zdata.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index 11c7a1a..36a5421 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -735,11 +735,15 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
 		struct page *const newpage =
 				alloc_page(GFP_NOFS | __GFP_NOFAIL);
 
-		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
-		err = z_erofs_attach_page(clt, newpage,
-					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
-		if (!err)
-			goto retry;
+		if (!newpage) {
+			err = -ENOMEM;
+		} else {
+			set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
+			err = z_erofs_attach_page(clt, newpage,
+						Z_EROFS_PAGE_TYPE_EXCLUSIVE);
+			if (!err)
+				goto retry;
+		}
 	}
 
 	if (err)
-- 

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

* Re: [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
  2022-03-22  8:08 [PATCH] erofs: fix a potential NULL dereference of alloc_pages() xkernel.wang
@ 2022-03-22  8:28   ` Gao Xiang
  0 siblings, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2022-03-22  8:28 UTC (permalink / raw)
  To: xkernel.wang; +Cc: xiang, chao, linux-erofs, linux-kernel

On Tue, Mar 22, 2022 at 04:08:12PM +0800, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> alloc_pages() returns the page on success or NULL if allocation fails,
> while set_page_private() will dereference `newpage`. So it is better to
> catch the memory error in case other errors happen.
> 
> Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
> ---
>  fs/erofs/zdata.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
> index 11c7a1a..36a5421 100644
> --- a/fs/erofs/zdata.c
> +++ b/fs/erofs/zdata.c
> @@ -735,11 +735,15 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
>  		struct page *const newpage =
>  				alloc_page(GFP_NOFS | __GFP_NOFAIL);
>  

It's really a nofail allocation, am I missing something?

Thanks,
Gao Xiang

> -		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
> -		err = z_erofs_attach_page(clt, newpage,
> -					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
> -		if (!err)
> -			goto retry;
> +		if (!newpage) {
> +			err = -ENOMEM;
> +		} else {
> +			set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
> +			err = z_erofs_attach_page(clt, newpage,
> +						Z_EROFS_PAGE_TYPE_EXCLUSIVE);
> +			if (!err)
> +				goto retry;
> +		}
>  	}
>  
>  	if (err)
> -- 

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

* Re: [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
@ 2022-03-22  8:28   ` Gao Xiang
  0 siblings, 0 replies; 5+ messages in thread
From: Gao Xiang @ 2022-03-22  8:28 UTC (permalink / raw)
  To: xkernel.wang; +Cc: linux-erofs, linux-kernel

On Tue, Mar 22, 2022 at 04:08:12PM +0800, xkernel.wang@foxmail.com wrote:
> From: Xiaoke Wang <xkernel.wang@foxmail.com>
> 
> alloc_pages() returns the page on success or NULL if allocation fails,
> while set_page_private() will dereference `newpage`. So it is better to
> catch the memory error in case other errors happen.
> 
> Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
> ---
>  fs/erofs/zdata.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
> index 11c7a1a..36a5421 100644
> --- a/fs/erofs/zdata.c
> +++ b/fs/erofs/zdata.c
> @@ -735,11 +735,15 @@ static int z_erofs_do_read_page(struct z_erofs_decompress_frontend *fe,
>  		struct page *const newpage =
>  				alloc_page(GFP_NOFS | __GFP_NOFAIL);
>  

It's really a nofail allocation, am I missing something?

Thanks,
Gao Xiang

> -		set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
> -		err = z_erofs_attach_page(clt, newpage,
> -					  Z_EROFS_PAGE_TYPE_EXCLUSIVE);
> -		if (!err)
> -			goto retry;
> +		if (!newpage) {
> +			err = -ENOMEM;
> +		} else {
> +			set_page_private(newpage, Z_EROFS_SHORTLIVED_PAGE);
> +			err = z_erofs_attach_page(clt, newpage,
> +						Z_EROFS_PAGE_TYPE_EXCLUSIVE);
> +			if (!err)
> +				goto retry;
> +		}
>  	}
>  
>  	if (err)
> -- 

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

* Re: [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
  2022-03-22  8:28   ` Gao Xiang
@ 2022-03-22  8:44     ` Xiaoke Wang
  -1 siblings, 0 replies; 5+ messages in thread
From: Xiaoke Wang @ 2022-03-22  8:44 UTC (permalink / raw)
  To: hsiangkao; +Cc: xiang, chao, linux-erofs, linux-kernel

On Tue, 22 Mar 2022 16:28:09 +0800, hsiangkao@linux.alibaba.com wrote:

&gt; It's really a nofail allocation, am I missing something?

Sorry, I did not carefully notice the flag `__GFP_NOFAIL`.
Please ignore this patch if this does not need to deal with failure.

Regards,
Xiaoke Wang

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

* Re: [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
@ 2022-03-22  8:44     ` Xiaoke Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Xiaoke Wang @ 2022-03-22  8:44 UTC (permalink / raw)
  To: hsiangkao; +Cc: linux-erofs, linux-kernel

On Tue, 22 Mar 2022 16:28:09 +0800, hsiangkao@linux.alibaba.com wrote:

&gt; It's really a nofail allocation, am I missing something?

Sorry, I did not carefully notice the flag `__GFP_NOFAIL`.
Please ignore this patch if this does not need to deal with failure.

Regards,
Xiaoke Wang

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

end of thread, other threads:[~2022-03-22  8:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22  8:08 [PATCH] erofs: fix a potential NULL dereference of alloc_pages() xkernel.wang
2022-03-22  8:28 ` Gao Xiang
2022-03-22  8:28   ` Gao Xiang
2022-03-22  8:44   ` Xiaoke Wang
2022-03-22  8:44     ` Xiaoke Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.