Skip to contentSkip to conversation

mm/huge_memory: check VMA end bounds in insert_pmd() and insert_pud()

PATCH In review Elkin Cruz 2 participants 4 messages Nov 25 – Nov 26

Subsystems (3)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 mm/huge_memory: check VMA end bounds in insert_pmd() and insert_pud()

insert_pmd() and insert_pud() only check that addr lies within [vma->vm_start, vma->vm_end) without verifying that the full PMD or PUD mapping fits below vma->vm_end. On a VMA whose end is not PMD- or PUD-aligned, inserting a huge mapping at the tail straddles past vma->vm_end.

Require vma->vm_end - addr >= HPAGE_PMD_SIZE in insert_pmd() and vma->vm_end - addr >= HPAGE_PUD_SIZE in insert_pud().

Fixes 5cad465d7fa6 ("mm: add vmf_insert_pfn_pmd()")
Fixes a00cc7d9dd93 ("mm, x86: add support for PUD-sized transparent hugepages")

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 mm/huge_memory.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
mm/huge_memory.c +4 -2
index e167c9ea4609..683f0c68d6b2 100644
@@ -1630,7 +1630,8 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr,
 	spinlock_t *ptl;
 	pmd_t entry;
 
-	if (addr < vma->vm_start || addr >= vma->vm_end)
+	if (addr < vma->vm_start || addr >= vma->vm_end ||
+	    vma->vm_end - addr < HPAGE_PMD_SIZE)
 		return VM_FAULT_SIGBUS;
 
 	if (arch_needs_pgtable_deposit()) {
@@ -1771,7 +1772,8 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr,
 	spinlock_t *ptl;
 	pud_t entry;
 
-	if (addr < vma->vm_start || addr >= vma->vm_end)
+	if (addr < vma->vm_start || addr >= vma->vm_end ||
+	    vma->vm_end - addr < HPAGE_PUD_SIZE)
 		return VM_FAULT_SIGBUS;
 
 	ptl = pud_lock(mm, pud);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw
Quoted 11 lines · Elkin Cruz

insert_pmd() and insert_pud() only check that addr lies within [vma->vm_start, vma->vm_end) without verifying that the full PMD or PUD mapping fits below vma->vm_end. On a VMA whose end is not PMD- or PUD-aligned, inserting a huge mapping at the tail straddles past vma->vm_end.

Require vma->vm_end - addr >= HPAGE_PMD_SIZE in insert_pmd() and vma->vm_end - addr >= HPAGE_PUD_SIZE in insert_pud().

Fixes 5cad465d7fa6 ("mm: add vmf_insert_pfn_pmd()")
Fixes a00cc7d9dd93 ("mm, x86: add support for PUD-sized transparent hugepages")

Which users in the tree trigger this?

Signature · 3 lines
Cheers,

David
author
#2 lore raw
Quoted 1 line · David Hildenbrand (Arm)

Which users in the tree trigger this?

Very fair point. After a quick check, there are indeed no users in the tree that can trigger this.
Back to the whiteboard. The linter is still too noisy. I will hold off sending any more patches :).

Please ignore/drop this patch.

Thank you so much for your very quick reply!
Elkin

#3 lore raw
Quoted 11 lines · Elkin Cruz
Quoted 1 line · David Hildenbrand (Arm)

Which users in the tree trigger this?

Very fair point. After a quick check, there are indeed no users in the tree that can trigger this.
Back to the whiteboard. The linter is still too noisy. I will hold off sending any more patches :).

Please ignore/drop this patch.

In general, making code more robust is good. But then, please don't use Fixes:
or CC stable because that makes people think that there is an actual problem.

For the code here, couldn't we do something like

	if (addr < vma->vm_start ||
	    addr + HPAGE_PMD_SIZE >= vma->vm_end )

... not sure if we'd have to care about overflows.

Signature · 3 lines
Cheers,

David