Skip to contentSkip to conversation

userfaultfd: prevent registration of special VMAs

PATCH Superseded Mike Rapoport 4 participants 14 messages Jun 17 – Jul 7

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

PATCH 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_IO, VM_MIXEDMAP and VM_PFNMAP so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")
---
 mm/userfaultfd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/userfaultfd.c +2 -1
index 246af12bf801..b8d2d87ce8d7 100644
@@ -2111,7 +2111,8 @@ 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_IO | VM_MIXEDMAP | VM_PFNMAP |
+			     VM_SHADOW_STACK))
 		return false;
 
 	vm_flags &= __VM_UFFD_FLAGS;

base-commit: e3d8707358ea76b78bdec9928937bb9a797f2c8f

Signature · 1 line
2.53.0
#1 lore raw
Quoted 27 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_IO, VM_MIXEDMAP and VM_PFNMAP so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")
---
 mm/userfaultfd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/userfaultfd.c +0 -0
index 246af12bf801..b8d2d87ce8d7 100644

mm/userfaultfd.c static bool vma_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags, · line 2115

Quoted 6 lines
mm/userfaultfd.c +2 -1
@@ -2111,7 +2111,8 @@ 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_IO | VM_MIXEDMAP | VM_PFNMAP |
+			     VM_SHADOW_STACK))

I'm sure you considered VM_SPECIAL, which additionally includes VM_DONTEXPAND.

Would that be better, or what was the reason to allow VM_DONTEXPAND?

Signature · 3 lines
Cheers,

David
author maintainer
#2 lore raw
Quoted 31 lines · David Hildenbrand (Arm)
Quoted 30 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_IO, VM_MIXEDMAP and VM_PFNMAP so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")
---
 mm/userfaultfd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/userfaultfd.c +0 -0
index 246af12bf801..b8d2d87ce8d7 100644
@@ -2111,7 +2111,8 @@ 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);
 
Quoted 7 lines

mm/userfaultfd.c static bool vma_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags, · line 2115

Quoted 3 lines
mm/userfaultfd.c +2 -1
-	if (vma->vm_flags & VM_DROPPABLE)
+	if (vma->vm_flags & (VM_DROPPABLE | VM_IO | VM_MIXEDMAP | VM_PFNMAP |
+			     VM_SHADOW_STACK))

I'm sure you considered VM_SPECIAL, which additionally includes VM_DONTEXPAND.

Would that be better, or what was the reason to allow VM_DONTEXPAND?

By itself VM_DONTEXPAND won't matter, as uffd can't resize a VMA. But thinking more about it, it's better to make vma_can_userfault() more restrictive and just use VM_SPECIAL.

Quoted 4 lines
Signature · 3 lines
Cheers,

David
Signature · 2 lines
Sincerely yours,
Mike.
author maintainer
#3 lore raw
Quoted 38 lines · Mike Rapoport
Quoted 37 lines · David Hildenbrand (Arm)
Quoted 27 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_IO, VM_MIXEDMAP and VM_PFNMAP so that if a driver would implement vm_uffd_ops, it wouldn't be possible to register special VMAs with userfaultfd.

Fixes 54007f818206 ("mm: Introduce VM_SHADOW_STACK for shadow stack memory")
---
 mm/userfaultfd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/userfaultfd.c +0 -0
index 246af12bf801..b8d2d87ce8d7 100644

mm/userfaultfd.c static bool vma_can_userfault(struct vm_area_struct *vma, vm_flags_t vm_flags, · line 2115

Quoted 6 lines
mm/userfaultfd.c +2 -1
@@ -2111,7 +2111,8 @@ 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_IO | VM_MIXEDMAP | VM_PFNMAP |
+			     VM_SHADOW_STACK))

I'm sure you considered VM_SPECIAL, which additionally includes VM_DONTEXPAND.

Quoted 5 lines
Quoted 1 line

Would that be better, or what was the reason to allow VM_DONTEXPAND?

By itself VM_DONTEXPAND won't matter, as uffd can't resize a VMA. But thinking more about it, it's better to make vma_can_userfault() more restrictive and just use VM_SPECIAL.

Ah, hugetlb sets VM_DONTEXPAND, so it must me excluded to allow uffd with hugetlb.

Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
Quoted 5 lines · Mike Rapoport
Quoted 4 lines · Mike Rapoport
Quoted 3 lines · David Hildenbrand (Arm)

I'm sure you considered VM_SPECIAL, which additionally includes VM_DONTEXPAND.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Would that be better, or what was the reason to allow VM_DONTEXPAND?

By itself VM_DONTEXPAND won't matter, as uffd can't resize a VMA. But thinking more about it, it's better to make vma_can_userfault() more restrictive and just use VM_SPECIAL.

Ah, hugetlb sets VM_DONTEXPAND, so it must me excluded to allow uffd with hugetlb.

It would probably be cleaner to just allow hugetlb, and then check for VM_SPECIAL if not hugetlb.

Signature · 3 lines
Cheers,

David
author maintainer
#5 lore raw
Quoted 12 lines · David Hildenbrand (Arm)
Quoted 11 lines · Mike Rapoport
Quoted 9 lines · Mike Rapoport
Quoted 4 lines · David Hildenbrand (Arm)

I'm sure you considered VM_SPECIAL, which additionally includes VM_DONTEXPAND.

Would that be better, or what was the reason to allow VM_DONTEXPAND?

By itself VM_DONTEXPAND won't matter, as uffd can't resize a VMA. But thinking more about it, it's better to make vma_can_userfault() more restrictive and just use VM_SPECIAL.

Quoted 5 lines
Quoted 2 lines

Ah, hugetlb sets VM_DONTEXPAND, so it must me excluded to allow uffd with hugetlb.

It would probably be cleaner to just allow hugetlb, and then check for VM_SPECIAL if not hugetlb.

Cleaner in what sense?
Will be uglier for sure, just take a look at vma_can_userfault().

Quoted 3 lines
Signature · 2 lines
Cheers,
David
Signature · 2 lines
Sincerely yours,
Mike.
#6 lore raw
Quoted 11 lines · Mike Rapoport
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport

Ah, hugetlb sets VM_DONTEXPAND, so it must me excluded to allow uffd with hugetlb.

It would probably be cleaner to just allow hugetlb, and then check for VM_SPECIAL if not hugetlb.

Cleaner in what sense?
Will be uglier for sure, just take a look at vma_can_userfault().

I was thinking of this:

mm/userfaultfd.c +0 -0
index 180bad42fc79..8a6803618a91 100644
@@ -2029,7 +2029,10 @@ 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;
Signature · 3 lines
Cheers,

David
author maintainer
#7 lore raw
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 11 lines · Mike Rapoport
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport

Ah, hugetlb sets VM_DONTEXPAND, so it must me excluded to allow uffd with hugetlb.

It would probably be cleaner to just allow hugetlb, and then check for VM_SPECIAL if not hugetlb.

Cleaner in what sense?
Will be uglier for sure, just take a look at vma_can_userfault().

I was thinking of this:

mm/userfaultfd.c +0 -0
index 180bad42fc79..8a6803618a91 100644

mm/userfaultfd.c bool vma_can_userfault(struct vm_area_struct *vma, · line 2037

Quoted 10 lines
mm/userfaultfd.c +4 -1
@@ -2029,7 +2029,10 @@ 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;

In a way that's an extra check for hugetlb, but it will work. Will respin.

Quoted 5 lines
mm/userfaultfd.c +0 -0
        vm_flags &= __VM_UFFD_FLAGS;
Signature · 3 lines
-- 
Cheers,
David
Signature · 2 lines
Sincerely yours,
Mike.
#8 lore raw
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport

Cleaner in what sense?
Will be uglier for sure, just take a look at vma_can_userfault().

I was thinking of this:

mm/userfaultfd.c +0 -0
index 180bad42fc79..8a6803618a91 100644
@@ -2029,7 +2029,10 @@ bool vma_can_userfault(struct vm_area_struct *vma,
 vm_flags_t vm_flags,
 {
        const struct vm_uffd_ops *ops = vma_uffd_ops(vma);
 
Quoted 8 lines

mm/userfaultfd.c bool vma_can_userfault(struct vm_area_struct *vma, · line 2037

Quoted 6 lines
mm/userfaultfd.c +4 -1
-       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;

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Signature · 3 lines
Cheers,

David
#9 lore raw
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport

Cleaner in what sense?
Will be uglier for sure, just take a look at vma_can_userfault().

I was thinking of this:

mm/userfaultfd.c +0 -0
index 180bad42fc79..8a6803618a91 100644
@@ -2029,7 +2029,10 @@ bool vma_can_userfault(struct vm_area_struct *vma,
 vm_flags_t vm_flags,
 {
        const struct vm_uffd_ops *ops = vma_uffd_ops(vma);
 
Quoted 6 lines
Quoted 6 lines

mm/userfaultfd.c bool vma_can_userfault(struct vm_area_struct *vma, · line 2037

Quoted 6 lines
mm/userfaultfd.c +4 -1
-       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;

This is still pretty gross, and it's a bit odd to me that we will now enforce userfaultfd on ranges that are somehow VMA_DONTEXPAND_BIT but not otherwise 'special'.

But I think that's because we even allow that to exist as a thing and have made VMA_DONTEXPAND_BIT a little unclear in its behaviour.

But I'm fixing that, I'm working on a series that clears up the special flags, and replaces the checks in general with vma_xxx() or vma_flags_xxx() predicated, and which will ultimatately drop VM_SPECIAL/VMA_SPECIAL_FLAGS.

Quoted 5 lines
Quoted 2 lines

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Mike - you said you were respinning, it'd help my series if you respan the above quickly so I could base my change on that :).

I can send a quick patch if you're tied up also!

Quoted 6 lines

--
Cheers,

David

Thanks, Lorenzo

author maintainer
#10 lore raw
Quoted 8 lines · Lorenzo Stoakes
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 1 line

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Mike - you said you were respinning, it'd help my series if you respan the above quickly so I could base my change on that :).

I sent it already:

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

and Andrew apparently ook it:

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

Quoted 3 lines

I can send a quick patch if you're tied up also!

Thanks, Lorenzo

Signature · 2 lines
Sincerely yours,
Mike.
#11 lore raw
Quoted 10 lines · Mike Rapoport
Quoted 8 lines · Lorenzo Stoakes
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 1 line

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Mike - you said you were respinning, it'd help my series if you respan the above quickly so I could base my change on that :).

Quoted 7 lines

I sent it already:

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

and Andrew apparently ook it:

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

It's not anywhere :)

Andrew - did you take that thread as signal this should not go in?

It should definitely go in. I will clean up the mess discussed there separately.

Quoted 8 lines
Quoted 4 lines

I can send a quick patch if you're tied up also!

Thanks, Lorenzo

--
Sincerely yours,
Mike.

Thanks, Lorenzo

maintainer
#12 lore raw
Quoted 17 lines · On Fri, 3 Jul 2026 14:38:56 +0100 Lorenzo Stoakes <[email protected]> wrote:
Quoted 16 lines · Mike Rapoport
Quoted 8 lines · Lorenzo Stoakes
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 1 line

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Mike - you said you were respinning, it'd help my series if you respan the above quickly so I could base my change on that :).

I sent it already:

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

and Andrew apparently ook it:

Quoted 7 lines
Quoted 1 line

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

It's not anywhere :)

Andrew - did you take that thread as signal this should not go in?

It should definitely go in. I will clean up the mess discussed there separately.

Thanks, yes, I added it then hid it when Linus objected. I'll unhide it now.

#13 lore raw
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Fri, 3 Jul 2026 14:38:56 +0100 Lorenzo Stoakes <[email protected]> wrote:
Quoted 17 lines · Mike Rapoport
Quoted 8 lines · Lorenzo Stoakes
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 1 line

In a way that's an extra check for hugetlb, but it will work.

My point would be that we exclude all special VMAs, except hugetlb (which is special but supported ... in its special way).

Mike - you said you were respinning, it'd help my series if you respan the above quickly so I could base my change on that :).

I sent it already:

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

and Andrew apparently ook it:

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

Quoted 8 lines
Quoted 5 lines

It's not anywhere :)

Andrew - did you take that thread as signal this should not go in?

It should definitely go in. I will clean up the mess discussed there separately.

Thanks, yes, I added it then hid it when Linus objected. I'll unhide it now.

Thanks!