linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhiyu Zhang <zhiyuzhang999@gmail.com>
To: mark@fasheh.com, jlbec@evilplan.org, joseph.qi@linux.alibaba.com,
	gregkh@linuxfoundation.org
Cc: ocfs2-devel@lists.linux.dev, akpm@linux-foundation.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Zhiyu Zhang <zhiyuzhang999@gmail.com>
Subject: [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios
Date: Fri,  2 May 2025 23:37:42 +0800	[thread overview]
Message-ID: <20250502153742.598-1-zhiyuzhang999@gmail.com> (raw)

When allocation of a folio fails (e.g., -ENOMEM), ocfs2_write_begin_nolock()
stores an ERR_PTR in wc->w_folios[i]. ocfs2_unlock_and_free_folios() later
walks this array but only skips NULL entries, thus passing the error pointer
to folio_unlock(), which eventually dereferences it in const_folio_flags and
panics with BUG: unable to handle kernel paging request in const_folio_flags.

This patch fixes this issue by adding IS_ERR_OR_NULL() to filter both NULL
and error entries before any folio operations.

Fixes: 7e119cff9d0a ("ocfs2: convert w_pages to w_folios")
Reported-by: Zhiyu Zhang <zhiyuzhang999@gmail.com>
Closes: https://lore.kernel.org/linux-fsdevel/CALf2hKtnFskBvmZeigK_=mqq9Vd4TWT+YOXcwfNNt1ydOY=0Yg@mail.gmail.com/T/#u
Tested-by: Zhiyu Zhang <zhiyuzhang999@gmail.com>
Signed-off-by: Zhiyu Zhang <zhiyuzhang999@gmail.com>
---
 fs/ocfs2/aops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 40b6bce12951..9e500c5fee38 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -760,7 +760,7 @@ void ocfs2_unlock_and_free_folios(struct folio **folios, int num_folios)
 	int i;

 	for(i = 0; i < num_folios; i++) {
-		if (!folios[i])
+		if (IS_ERR_OR_NULL(folios[i]))
 			continue;
 		folio_unlock(folios[i]);
 		folio_mark_accessed(folios[i]);
--
2.34.1


             reply	other threads:[~2025-05-02 15:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02 15:37 Zhiyu Zhang [this message]
2025-05-02 15:49 ` [External] : [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios Mark Tinguely
2025-05-02 16:11   ` Zhiyu Zhang

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=20250502153742.598-1-zhiyuzhang999@gmail.com \
    --to=zhiyuzhang999@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jlbec@evilplan.org \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark@fasheh.com \
    --cc=ocfs2-devel@lists.linux.dev \
    /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 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).