]> git.dujemihanovic.xyz Git - linux.git/commitdiff
mm: avoid leaving partial pfn mappings around in error case
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 12 Sep 2024 00:11:23 +0000 (17:11 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 12 Sep 2024 19:10:00 +0000 (12:10 -0700)
As Jann points out, PFN mappings are special, because unlike normal
memory mappings, there is no lifetime information associated with the
mapping - it is just a raw mapping of PFNs with no reference counting of
a 'struct page'.

That's all very much intentional, but it does mean that it's easy to
mess up the cleanup in case of errors.  Yes, a failed mmap() will always
eventually clean up any partial mappings, but without any explicit
lifetime in the page table mapping itself, it's very easy to do the
error handling in the wrong order.

In particular, it's easy to mistakenly free the physical backing store
before the page tables are actually cleaned up and (temporarily) have
stale dangling PTE entries.

To make this situation less error-prone, just make sure that any partial
pfn mapping is torn down early, before any other error handling.

Reported-and-tested-by: Jann Horn <jannh@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Simona Vetter <simona.vetter@ffwll.ch>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/memory.c

index 3c01d68065be21a45d8b9085d7f81785bc5cbfd8..ebfc9768f801af82c25a800a0574afc5ad55e913 100644 (file)
@@ -2632,11 +2632,7 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
        return 0;
 }
 
-/*
- * Variant of remap_pfn_range that does not call track_pfn_remap.  The caller
- * must have pre-validated the caching bits of the pgprot_t.
- */
-int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
+static int remap_pfn_range_internal(struct vm_area_struct *vma, unsigned long addr,
                unsigned long pfn, unsigned long size, pgprot_t prot)
 {
        pgd_t *pgd;
@@ -2689,6 +2685,27 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
        return 0;
 }
 
+/*
+ * Variant of remap_pfn_range that does not call track_pfn_remap.  The caller
+ * must have pre-validated the caching bits of the pgprot_t.
+ */
+int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
+               unsigned long pfn, unsigned long size, pgprot_t prot)
+{
+       int error = remap_pfn_range_internal(vma, addr, pfn, size, prot);
+
+       if (!error)
+               return 0;
+
+       /*
+        * A partial pfn range mapping is dangerous: it does not
+        * maintain page reference counts, and callers may free
+        * pages due to the error. So zap it early.
+        */
+       zap_page_range_single(vma, addr, size, NULL);
+       return error;
+}
+
 /**
  * remap_pfn_range - remap kernel memory to userspace
  * @vma: user vma to map to