linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ksm: Fix potential NULL pointer dereference
@ 2025-09-25  3:13 Fushuai Wang
  2025-09-25  7:50 ` David Hildenbrand
  0 siblings, 1 reply; 3+ messages in thread
From: Fushuai Wang @ 2025-09-25  3:13 UTC (permalink / raw)
  To: akpm, david, xu.xin16, chengming.zhou
  Cc: linux-mm, linux-kernel, cuigaosheng1, Fushuai Wang

The stable_tree_search() function may return an error pointer
(NULL or ERR_PTR(...)). The current code does not check for
these cases before dereferencing the returned value.

Gaosheng once added the necessary check in commit 98c3ca0015b8
("ksm: convert cmp_and_merge_page() to use a folio"), but it seems
the check was lost during the process of being merged into the
mainline.

Fixes: 98c3ca0015b8 ("ksm: convert cmp_and_merge_page() to use a folio")
Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
---
 mm/ksm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 160787bb121c..7aa9b2829a7b 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2272,7 +2272,8 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
 
 	/* Start by searching for the folio in the stable tree */
 	kfolio = stable_tree_search(page);
-	if (&kfolio->page == page && rmap_item->head == stable_node) {
+	if (!IS_ERR_OR_NULL(kfolio) && &kfolio->page == page &&
+	    rmap_item->head == stable_node) {
 		folio_put(kfolio);
 		return;
 	}
-- 
2.36.1



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

* Re: [PATCH] ksm: Fix potential NULL pointer dereference
  2025-09-25  3:13 [PATCH] ksm: Fix potential NULL pointer dereference Fushuai Wang
@ 2025-09-25  7:50 ` David Hildenbrand
  0 siblings, 0 replies; 3+ messages in thread
From: David Hildenbrand @ 2025-09-25  7:50 UTC (permalink / raw)
  To: Fushuai Wang, akpm, xu.xin16, chengming.zhou
  Cc: linux-mm, linux-kernel, cuigaosheng1

On 25.09.25 05:13, Fushuai Wang wrote:
> The stable_tree_search() function may return an error pointer
> (NULL or ERR_PTR(...)). The current code does not check for
> these cases before dereferencing the returned value.

We discussed this in [1] where Willy clarified that there is no 
de-referencing happening [2].

So no, this is not required.

Consider "&kfolio->page" currently being a "(struct page *) kfolio", 
which is just a pointer cast and no de-referencing.

[1] 
https://lore.kernel.org/linux-mm/20241024032300.2501949-1-cuigaosheng1@huawei.com/
[2] https://lore.kernel.org/linux-mm/Zzdrnod2zcTeI-Nt@casper.infradead.org/

-- 
Cheers

David / dhildenb



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

* Re: [PATCH] ksm: Fix potential NULL pointer dereference
  2024-11-15 15:41 [PATCH -next] mm/ksm: Add missing IS_ERR_OR_NULL check for stable_tree_search() Matthew Wilcox
@ 2025-09-25  8:02 ` Fushuai Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Fushuai Wang @ 2025-09-25  8:02 UTC (permalink / raw)
  To: willy; +Cc: akpm, cuigaosheng1, david, linux-mm, wangweiyang2

>>  The stable_tree_search() function may return an error pointer
>>  (NULL or ERR_PTR(...)). The current code does not check for
>>  these cases before dereferencing the returned value.
> 
> We discussed this in [1] where Willy clarified that there is no 
> de-referencing happening [2].
> 
> So no, this is not required.
> 
> Consider "&kfolio->page" currently being a "(struct page *) kfolio", 
> which is just a pointer cast and no de-referencing.
> 
> [1] 
> https://lore.kernel.org/linux-mm/20241024032300.2501949-1-cuigaosheng1@huawei.com/
> [2] https://lore.kernel.org/linux-mm/Zzdrnod2zcTeI-Nt@casper.infradead.org/

Thanks!

Pease ignore this.
I'll be more careful in next time.

Regards,
Wang.


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

end of thread, other threads:[~2025-09-25  8:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-25  3:13 [PATCH] ksm: Fix potential NULL pointer dereference Fushuai Wang
2025-09-25  7:50 ` David Hildenbrand
  -- strict thread matches above, loose matches on Subject: below --
2024-11-15 15:41 [PATCH -next] mm/ksm: Add missing IS_ERR_OR_NULL check for stable_tree_search() Matthew Wilcox
2025-09-25  8:02 ` [PATCH] ksm: Fix potential NULL pointer dereference Fushuai Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox