Skip to contentSkip to conversation

x86/mm: Fix NULL pointer dereference in kernel_page_present()

PATCH In review Tanya Agarwal 2 participants 3 messages Jan 11, 2025 – Jan 24, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/mm: Fix NULL pointer dereference in kernel_page_present()

From: Tanya Agarwal <[email protected]>

The static code analysis tool "Coverity Scan" pointed the following details out for further development considerations:
CID 1271215: Dereference null return value (NULL_RETURNS)
dereference: Dereferencing pte, which is known to be NULL.

Conclusion:
Add WARN_ON_ONCE() and NULL check for pte before dereferencing it.

Fixes 8a235efad548 ("Hibernation: Handle DEBUG_PAGEALLOC on x86")

---
V2: add WARN_ON_ONCE() as suggested by Dave

Coverity Link:
https://scan5.scan.coverity.com/#/project-view/63683/10063?selectedIssue=1271215

 arch/x86/mm/pat/set_memory.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/mm/pat/set_memory.c +3 -0
index 95bc50a8541c..8f9d418e6a8c 100644
@@ -2495,6 +2495,9 @@ bool kernel_page_present(struct page *page)
 		return false;
 
 	pte = lookup_address((unsigned long)page_address(page), &level);
+	if (WARN_ON_ONCE(!pte))
+		return false;
+
 	return (pte_val(*pte) & _PAGE_PRESENT);
 }
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 37 lines · Tanya Agarwal

From: Tanya Agarwal <[email protected]>

The static code analysis tool "Coverity Scan" pointed the following details out for further development considerations:
CID 1271215: Dereference null return value (NULL_RETURNS)
dereference: Dereferencing pte, which is known to be NULL.

Conclusion:
Add WARN_ON_ONCE() and NULL check for pte before dereferencing it.

Fixes 8a235efad548 ("Hibernation: Handle DEBUG_PAGEALLOC on x86")

---
V2: add WARN_ON_ONCE() as suggested by Dave

Coverity Link:
https://scan5.scan.coverity.com/#/project-view/63683/10063?selectedIssue=1271215

 arch/x86/mm/pat/set_memory.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/mm/pat/set_memory.c +3 -1
index 95bc50a8541c..8f9d418e6a8c 100644
@@ -2495,6 +2495,9 @@ bool kernel_page_present(struct page *page)
                return false;
 
        pte = lookup_address((unsigned long)page_address(page), &level);
+       if (WARN_ON_ONCE(!pte))
+               return false;
+
        return (pte_val(*pte) & _PAGE_PRESENT);
 }
 
--
Quoted 2 lines

2.39.5

Hi Dave,

I just wanted to check if you've had a chance to review it or need any more information.

Thanks,
Tanya

#2 lore raw
In reply to Tanya Agarwal
Quoted 6 lines · Tanya Agarwal

From: Tanya Agarwal <[email protected]>

The static code analysis tool "Coverity Scan" pointed the following details out for further development considerations:
CID 1271215: Dereference null return value (NULL_RETURNS)
dereference: Dereferencing pte, which is known to be NULL.

I think we need an actual changelog for this, as opposed to just blindly trusting Coverity. You can say that Coverity helped identify this as an issue, but we don't need the "CID" or other Coverity gunk in our changelogs. We don't care.

Could you please elaborate on where this issue might affect people? It's also entirely theoretical as far as I can tell. I can't even conjure up a contrived case where it could be triggered.

We won't do _anything_ with this patch until the merge window closes, so you've got at least a week to spruce up the changelog.