ocfs2-devel.oss.oracle.com archive mirror
 help / color / mirror / Atom feed
* [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios
@ 2025-05-02 15:37 Zhiyu Zhang
  2025-05-02 15:49 ` [External] : " Mark Tinguely
  0 siblings, 1 reply; 3+ messages in thread
From: Zhiyu Zhang @ 2025-05-02 15:37 UTC (permalink / raw)
  To: mark, jlbec, joseph.qi, gregkh
  Cc: ocfs2-devel, akpm, linux-fsdevel, linux-kernel, Zhiyu Zhang

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


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

* Re: [External] : [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios
  2025-05-02 15:37 [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios Zhiyu Zhang
@ 2025-05-02 15:49 ` Mark Tinguely
  2025-05-02 16:11   ` Zhiyu Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Tinguely @ 2025-05-02 15:49 UTC (permalink / raw)
  To: Zhiyu Zhang, mark, jlbec, joseph.qi, gregkh
  Cc: ocfs2-devel, akpm, linux-fsdevel, linux-kernel

On 5/2/25 10:37 AM, Zhiyu Zhang wrote:
> 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://urldefense.com/v3/__https://lore.kernel.org/linux-fsdevel/CALf2hKtnFskBvmZeigK_=mqq9Vd4TWT*YOXcwfNNt1ydOY=0Yg@mail.gmail.com/T/*u__;KyM!!ACWV5N9M2RV99hQ!O2QZFrLv_PvhBiQH1Ak3ZDOIFOQG2RgkiOSogyy_3n0ZsLL_GXVwf0aiu7ti71LagUjWpF54NlvjjQP7IfPUT2vS0yY$
> 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
> 
> 


yes, this was my bug in the folio port.
I submitted the patch with subject "ocfs2: fix panic in failed foilio 
allocation" as a solution.
In the old page allocation code, a NULL was the failed allocation. Folio 
returns an error code. the patch put the NULL back into the folio araay 
location:

  https://lore.kernel.org/ocfs2-devel/7de24670-89cc-4502-adbe-308bd5786f1d@linux.alibaba.com/T/#t


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

* Re: [External] : [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios
  2025-05-02 15:49 ` [External] : " Mark Tinguely
@ 2025-05-02 16:11   ` Zhiyu Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Zhiyu Zhang @ 2025-05-02 16:11 UTC (permalink / raw)
  To: Mark Tinguely
  Cc: mark, jlbec, joseph.qi, gregkh, ocfs2-devel, akpm, linux-fsdevel,
	linux-kernel

Hi Mark Tinguely,

Thank you for sharing the patch status.

Actually, I reported this bug on March 22nd
(https://lore.kernel.org/linux-fsdevel/CALf2hKtnFskBvmZeigK_=mqq9Vd4TWT+YOXcwfNNt1ydOY=0Yg@mail.gmail.com/T/#u),
but due to not receiving a response from the get_maintainer list and
being quite busy myself, I have not submitted the patch until today.

I am sorry to summit duplicated patch as I do not notice the change in
mainline repo. Please ignore this patch if it boters you.

Best regards,
Zhiyu

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

end of thread, other threads:[~2025-05-02 16:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-02 15:37 [PATCH] ocfs2: Fix potential ERR_PTR dereference in ocfs2_unlock_and_free_folios Zhiyu Zhang
2025-05-02 15:49 ` [External] : " Mark Tinguely
2025-05-02 16:11   ` Zhiyu Zhang

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