All of lore.kernel.org
 help / color / mirror / Atom feed
From: xkernel.wang@foxmail.com
To: xiang@kernel.org, chao@kernel.org
Cc: linux-erofs@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	Xiaoke Wang <xkernel.wang@foxmail.com>
Subject: [PATCH] erofs: fix a potential NULL dereference of alloc_pages()
Date: Tue, 22 Mar 2022 16:08:12 +0800	[thread overview]
Message-ID: <tencent_010A807048A5F97F0A900866A35C648E2E07@qq.com> (raw)

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)
-- 

             reply	other threads:[~2022-03-22  8:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-22  8:08 xkernel.wang [this message]
2022-03-22  8:28 ` [PATCH] erofs: fix a potential NULL dereference of alloc_pages() Gao Xiang
2022-03-22  8:28   ` Gao Xiang
2022-03-22  8:44   ` Xiaoke Wang
2022-03-22  8:44     ` Xiaoke Wang

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=tencent_010A807048A5F97F0A900866A35C648E2E07@qq.com \
    --to=xkernel.wang@foxmail.com \
    --cc=chao@kernel.org \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xiang@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 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.