Skip to contentSkip to conversation

userfaultfd: prevent registration of special VMAs

PATCH In review Mike Rapoport 3 participants 3 messages Jun 18 – Jul 3

Subsystems (3)
lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

PATCH v2 1/1 userfaultfd: prevent registration of special VMAs

From: "Mike Rapoport (Microsoft)" <[email protected]>

Vova Tokarev says:

userfaultfd allows registration on shadow stack VMAs. With userfaultfd access, you can register on the shadow stack, discard a page ... and inject a page with chosen return addresses via UFFDIO_COPY.

Update vma_can_userfault() to reject VM_SHADOW_STACK.

While on it, also reject VM_SPECIAL so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Since VM_SPECIAL includes VM_DONTEXPAND which is set but hugetlb, exclude hugetlb VMAs from the check for VM_SPECIAL.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")

---

v2 changes:
* reject all VM_SPECIAL except hugetlb

v1: https://lore.kernel.org/all/[email protected]

 mm/userfaultfd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/userfaultfd.c +4 -1
index 246af12bf801..c3adedaaf7d5 100644
@@ -2111,7 +2111,10 @@ static bool vma_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags,
 {
 	const struct vm_uffd_ops *ops = vma_uffd_ops(vma);
 
-	if (vma->vm_flags & VM_DROPPABLE)
+	if (vma->vm_flags & (VM_DROPPABLE | VM_SHADOW_STACK))
+		return false;
+
+	if (!is_vm_hugetlb_page(vma) && (vma->vm_flags & VM_SPECIAL))
 		return false;
 
 	vm_flags &= __VM_UFFD_FLAGS;

base-commit: e3d8707358ea76b78bdec9928937bb9a797f2c8f

Signature · 1 line
2.53.0
Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

Vova Tokarev says:

userfaultfd allows registration on shadow stack VMAs. With userfaultfd access, you can register on the shadow stack, discard a page ... and inject a page with chosen return addresses via UFFDIO_COPY.

Update vma_can_userfault() to reject VM_SHADOW_STACK.

While on it, also reject VM_SPECIAL so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Quoted 8 lines

Since VM_SPECIAL includes VM_DONTEXPAND which is set but hugetlb, exclude hugetlb VMAs from the check for VM_SPECIAL.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")

---

Thanks Mike

Signature · 3 lines
Cheers,

David
Reviewed-by on v2 1/1
#2 lore raw
In reply to Mike Rapoport
Quoted 14 lines · Mike Rapoport

From: "Mike Rapoport (Microsoft)" <[email protected]>

Vova Tokarev says:

userfaultfd allows registration on shadow stack VMAs. With userfaultfd access, you can register on the shadow stack, discard a page ... and inject a page with chosen return addresses via UFFDIO_COPY.

Update vma_can_userfault() to reject VM_SHADOW_STACK.

While on it, also reject VM_SPECIAL so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Quoted 7 lines

Since VM_SPECIAL includes VM_DONTEXPAND which is set but hugetlb, exclude hugetlb VMAs from the check for VM_SPECIAL.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")

LGTM, so:

I'm working on a series to take care of the issues discussed at [0].

[0]:https://lore.kernel.org/all/[email protected]

Quoted 27 lines

---

v2 changes:
* reject all VM_SPECIAL except hugetlb

v1: https://lore.kernel.org/all/[email protected]

 mm/userfaultfd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/userfaultfd.c +4 -1
index 246af12bf801..c3adedaaf7d5 100644
@@ -2111,7 +2111,10 @@ static bool vma_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags,
 {
 	const struct vm_uffd_ops *ops = vma_uffd_ops(vma);
 
-	if (vma->vm_flags & VM_DROPPABLE)
+	if (vma->vm_flags & (VM_DROPPABLE | VM_SHADOW_STACK))
+		return false;
+
+	if (!is_vm_hugetlb_page(vma) && (vma->vm_flags & VM_SPECIAL))
 		return false;
 
 	vm_flags &= __VM_UFFD_FLAGS;
Quoted 5 lines

base-commit: e3d8707358ea76b78bdec9928937bb9a797f2c8f --
2.53.0

Cheers, Lorenzo