Skip to contentSkip to conversation

mm/frame-vec: use FOLL_LONGTERM

PATCH Jason Gunthorpe 4 participants 8 messages Oct 5, 2020 – Oct 6, 2020

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

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
---
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/frame_vector.c +4 -0
index 10f82d5643b6de..26cb20544b6c37 100644
@@ -99,6 +99,10 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
 		if (ret >= nr_frames || start < vma->vm_end)
 			break;
 		vma = find_vma_intersection(mm, start, start + 1);
+		if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
+			ret = -EINVAL;
+			goto out;
+		}
 	} while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
 out:
 	if (locked)
Signature · 1 line
2.28.0
#1 lore raw
Quoted 20 lines · Jason Gunthorpe

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
---
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/frame_vector.c +0 -0
index 10f82d5643b6de..26cb20544b6c37 100644

mm/frame_vector.c int get_vaddr_frames(unsigned long start, unsigned int nr_frames, · line 106

Quoted 8 lines
mm/frame_vector.c +4 -0
@@ -99,6 +99,10 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
 		if (ret >= nr_frames || start < vma->vm_end)
 			break;
 		vma = find_vma_intersection(mm, start, start + 1);
+		if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
+			ret = -EINVAL;
+			goto out;
+		}
 	} while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));

Hum, I fail to see how this helps. If vma has no VM_IO or VM_PFNMAP flag, we'd exit the loop (to out: label) anyway due to the loop termination condition and why not return the frames we already have? Furthermore find_vma_intersection() can return NULL which would oops in your check then. What am I missing?

Honza

Quoted 5 lines
mm/frame_vector.c +0 -0
 out:
 	if (locked)
Signature · 1 line
2.28.0
Signature · 6 lines
Jan Kara <[email protected]>
SUSE Labs, CR
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw
In reply to Jason Gunthorpe
Quoted 8 lines · Jason Gunthorpe

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Quoted 6 lines
Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
---
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)

woops, this subject got badly corrupted when I was editing the CC list, it was supposed to be:

[PATCH] mm/gpu: frame_vector: require all VMAs to be VM_PFNMAP

Andrew please let me know if you need a resend

Sorry,
Jason

#2 lore raw
In reply to Jan Kara
Quoted 25 lines · Jan Kara
Quoted 24 lines · Jason Gunthorpe

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/frame_vector.c +4 -0
index 10f82d5643b6de..26cb20544b6c37 100644
+++ b/mm/frame_vector.c
@@ -99,6 +99,10 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
 		if (ret >= nr_frames || start < vma->vm_end)
 			break;
 		vma = find_vma_intersection(mm, start, start + 1);
+		if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
+			ret = -EINVAL;
+			goto out;
Quoted 8 lines

mm/frame_vector.c int get_vaddr_frames(unsigned long start, unsigned int nr_frames, · line 106

Quoted 2 lines
mm/frame_vector.c +1 -0
+		}
 	} while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));

Hum, I fail to see how this helps. If vma has no VM_IO or VM_PFNMAP flag, we'd exit the loop (to out: label) anyway due to the loop termination condition and why not return the frames we already have? Furthermore find_vma_intersection() can return NULL which would oops in your check then. What am I missing?

Oh, nothing, you are right. It just didn't read naturally because hitting the wrong kind of VMA should be an error condition :\

Sorry again,
Jason

#3 lore raw
Quoted 36 lines · Jason Gunthorpe
Quoted 25 lines · Jan Kara
Quoted 24 lines · Jason Gunthorpe

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/frame_vector.c +4 -0
index 10f82d5643b6de..26cb20544b6c37 100644
+++ b/mm/frame_vector.c
@@ -99,6 +99,10 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
            if (ret >= nr_frames || start < vma->vm_end)
                    break;
            vma = find_vma_intersection(mm, start, start + 1);
+           if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
+                   ret = -EINVAL;
+                   goto out;
Quoted 8 lines

mm/frame_vector.c int get_vaddr_frames(unsigned long start, unsigned int nr_frames, · line 106

Quoted 2 lines
mm/frame_vector.c +1 -0
+           }
    } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));

Hum, I fail to see how this helps. If vma has no VM_IO or VM_PFNMAP flag, we'd exit the loop (to out: label) anyway due to the loop termination condition and why not return the frames we already have? Furthermore find_vma_intersection() can return NULL which would oops in your check then. What am I missing?

Quoted 2 lines

Oh, nothing, you are right. It just didn't read naturally because hitting the wrong kind of VMA should be an error condition :\

Afaik these mmio maps should all be VM_DONTEXPAND (or at least the ones in drivers/gpu are all), so not sure why we need the loop here. But maybe there's some drivers that don't set that, or have other funny things going on with userspace piecing the mmap together, and I'm not going to audit them all :-)
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
maintainer
#6 lore raw
In reply to Jason Gunthorpe
Quoted 1 line · On Mon, 5 Oct 2020 14:47:47 -0300 Jason Gunthorpe <[email protected]> wrote:

Andrew please let me know if you need a resend

Andrew is rather confused.

Can we please identify the minimal patch(es) which are needed for 5.9 and -stable?

#4 lore raw
In reply to Jason Gunthorpe
Quoted 36 lines · Jason Gunthorpe
Quoted 25 lines · Jan Kara
Quoted 24 lines · Jason Gunthorpe

When get_vaddr_frames() does its hacky follow_pfn() loop it should never be allowed to extract a struct page from a normal VMA. This could allow a serious use-after-free problem on any kernel memory.

Restrict this to only work on VMA's with one of VM_IO | VM_PFNMAP set. This limits the use-after-free problem to only IO memory, which while still serious, is an improvement.

Fixes 8025e5ddf9c1 ("[media] mm: Provide new get_vaddr_frames() helper")
 mm/frame_vector.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/frame_vector.c +4 -0
index 10f82d5643b6de..26cb20544b6c37 100644
+++ b/mm/frame_vector.c
@@ -99,6 +99,10 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,
            if (ret >= nr_frames || start < vma->vm_end)
                    break;
            vma = find_vma_intersection(mm, start, start + 1);
+           if (!(vma->vm_flags & (VM_IO | VM_PFNMAP))) {
+                   ret = -EINVAL;
+                   goto out;
Quoted 8 lines

mm/frame_vector.c int get_vaddr_frames(unsigned long start, unsigned int nr_frames, · line 106

Quoted 2 lines
mm/frame_vector.c +1 -0
+           }
    } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));

Hum, I fail to see how this helps. If vma has no VM_IO or VM_PFNMAP flag, we'd exit the loop (to out: label) anyway due to the loop termination condition and why not return the frames we already have? Furthermore find_vma_intersection() can return NULL which would oops in your check then. What am I missing?

Quoted 2 lines

Oh, nothing, you are right. It just didn't read naturally because hitting the wrong kind of VMA should be an error condition :\

Also follow_pfn checks for this same conditionat already too, so this isn't really stopping anything bad from happening.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#7 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 1 line · On Mon, 5 Oct 2020 14:47:47 -0300 Jason Gunthorpe <[email protected]> wrote:

Andrew please let me know if you need a resend

Andrew is rather confused.

Can we please identify the minimal patch(es) which are needed for 5.9 and -stable?

Please ignore this one, it was sent in haste

Sorry,
Jason