]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mm: unconditionally close VMAs on error
authorLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Fri, 15 Nov 2024 12:36:52 +0000 (12:36 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 14 Dec 2024 18:47:40 +0000 (19:47 +0100)
[ Upstream commit 4080ef1579b2413435413988d14ac8c68e4d42c8 ]

Incorrect invocation of VMA callbacks when the VMA is no longer in a
consistent state is bug prone and risky to perform.

With regards to the important vm_ops->close() callback We have gone to
great lengths to try to track whether or not we ought to close VMAs.

Rather than doing so and risking making a mistake somewhere, instead
unconditionally close and reset vma->vm_ops to an empty dummy operations
set with a NULL .close operator.

We introduce a new function to do so - vma_close() - and simplify existing
vms logic which tracked whether we needed to close or not.

This simplifies the logic, avoids incorrect double-calling of the .close()
callback and allows us to update error paths to simply call vma_close()
unconditionally - making VMA closure idempotent.

Link: https://lkml.kernel.org/r/28e89dda96f68c505cb6f8e9fc9b57c3e9f74b42.1730224667.git.lorenzo.stoakes@oracle.com
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reported-by: Jann Horn <jannh@google.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Jann Horn <jannh@google.com>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Helge Deller <deller@gmx.de>
Cc: James E.J. Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mark Brown <broonie@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
mm/internal.h
mm/mmap.c
mm/nommu.c
mm/util.c

index e47f112a63d35c803c66e707ac9a316ea60e607a..df2b1156ef65dfbaf3740abc77b35fa02680d7a2 100644 (file)
@@ -46,6 +46,13 @@ void page_writeback_init(void);
  */
 int mmap_file(struct file *file, struct vm_area_struct *vma);
 
+/*
+ * If the VMA has a close hook then close it, and since closing it might leave
+ * it in an inconsistent state which makes the use of any hooks suspect, clear
+ * them down by installing dummy empty hooks.
+ */
+void vma_close(struct vm_area_struct *vma);
+
 vm_fault_t do_swap_page(struct vm_fault *vmf);
 
 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
index f4eac5a95d642cbba7675d38caf6dd750f00e640..ac1517a9606640f7ab8630e8d7c6019382e9b422 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -176,8 +176,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
        struct vm_area_struct *next = vma->vm_next;
 
        might_sleep();
-       if (vma->vm_ops && vma->vm_ops->close)
-               vma->vm_ops->close(vma);
+       vma_close(vma);
        if (vma->vm_file)
                fput(vma->vm_file);
        mpol_put(vma_policy(vma));
@@ -1901,8 +1900,7 @@ out:
        return addr;
 
 close_and_free_vma:
-       if (vma->vm_ops && vma->vm_ops->close)
-               vma->vm_ops->close(vma);
+       vma_close(vma);
 unmap_and_free_vma:
        vma->vm_file = NULL;
        fput(file);
@@ -2788,8 +2786,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
                return 0;
 
        /* Clean everything up if vma_adjust failed. */
-       if (new->vm_ops && new->vm_ops->close)
-               new->vm_ops->close(new);
+       vma_close(new);
        if (new->vm_file)
                fput(new->vm_file);
        unlink_anon_vmas(new);
index fdacc3d119c330ff0fa0c2d71f3dcc8a834ae2dc..f46a883e93e461d007b9498509d664f70575455d 100644 (file)
@@ -662,8 +662,7 @@ static void delete_vma_from_mm(struct vm_area_struct *vma)
  */
 static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
 {
-       if (vma->vm_ops && vma->vm_ops->close)
-               vma->vm_ops->close(vma);
+       vma_close(vma);
        if (vma->vm_file)
                fput(vma->vm_file);
        put_nommu_region(vma->vm_region);
index 8e5bd2c9f4b4ab9607e381de18059aaf3994fbdc..9e0c86555adf0d069d4383b6c3dc8c040c506678 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -1091,3 +1091,18 @@ int mmap_file(struct file *file, struct vm_area_struct *vma)
 
        return err;
 }
+
+void vma_close(struct vm_area_struct *vma)
+{
+       static const struct vm_operations_struct dummy_vm_ops = {};
+
+       if (vma->vm_ops && vma->vm_ops->close) {
+               vma->vm_ops->close(vma);
+
+               /*
+                * The mapping is in an inconsistent state, and no further hooks
+                * may be invoked upon it.
+                */
+               vma->vm_ops = &dummy_vm_ops;
+       }
+}