]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/mremap: check remap conditions earlier
authorLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Thu, 17 Jul 2025 16:55:56 +0000 (17:55 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 25 Jul 2025 02:12:30 +0000 (19:12 -0700)
When we expand or move a VMA, this requires a number of additional checks
to be performed.

Make it really obvious under what circumstances these checks must be
performed and aggregate all the checks in one place by invoking this in
check_prep_vma().

We have to adjust the checks to account for shrink + move operations by
checking new_len <= old_len rather than new_len == old_len.

No functional change intended.

[lorenzo.stoakes@oracle.com: allow undocumented mremap() shrink behaviour]
Link: https://lkml.kernel.org/r/8fc92a38-c636-465e-9a2f-2c6ac9cb49b8@lucifer.local
Link: https://lkml.kernel.org/r/8b4161ce074901e00602a446d81f182db92b0430.1752770784.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/mremap.c

index db7e773d08840fef9aa9171b492faacf2d8dc10c..11a8321a90b8e0d6d7fc25e9d7b8e5ebc5a5d243 100644 (file)
@@ -1339,6 +1339,13 @@ static int remap_is_valid(struct vma_remap_struct *vrm)
                        (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
                return -EINVAL;
 
+       /*
+        * We permit crossing of boundaries for the range being unmapped due to
+        * a shrink.
+        */
+       if (vrm->remap_type == MREMAP_SHRINK)
+               old_len = new_len;
+
        /* We can't remap across vm area boundaries */
        if (old_len > vma->vm_end - addr)
                return -EFAULT;
@@ -1443,10 +1450,6 @@ static unsigned long mremap_to(struct vma_remap_struct *vrm)
                vrm->old_len = vrm->new_len;
        }
 
-       err = remap_is_valid(vrm);
-       if (err)
-               return err;
-
        /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
        if (vrm->flags & MREMAP_DONTUNMAP) {
                vm_flags_t vm_flags = vrm->vma->vm_flags;
@@ -1635,10 +1638,6 @@ static unsigned long expand_vma(struct vma_remap_struct *vrm)
 {
        unsigned long err;
 
-       err = remap_is_valid(vrm);
-       if (err)
-               return err;
-
        /*
         * [addr, old_len) spans precisely to the end of the VMA, so try to
         * expand it in-place.
@@ -1705,6 +1704,21 @@ static unsigned long mremap_at(struct vma_remap_struct *vrm)
        return -EINVAL;
 }
 
+/*
+ * Will this operation result in the VMA being expanded or moved and thus need
+ * to map a new portion of virtual address space?
+ */
+static bool vrm_will_map_new(struct vma_remap_struct *vrm)
+{
+       if (vrm->remap_type == MREMAP_EXPAND)
+               return true;
+
+       if (vrm_implies_new_addr(vrm))
+               return true;
+
+       return false;
+}
+
 static int check_prep_vma(struct vma_remap_struct *vrm)
 {
        struct vm_area_struct *vma = vrm->vma;
@@ -1726,6 +1740,9 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
        if (!vrm_implies_new_addr(vrm))
                vrm->new_addr = vrm->addr;
 
+       if (vrm_will_map_new(vrm))
+               return remap_is_valid(vrm);
+
        return 0;
 }