mm/mremap: fix regression in vrm->new_addr check

Commit 3215eaceca ("mm/mremap: refactor initial parameter sanity
checks") moved the sanity check for vrm->new_addr from mremap_to() to
check_mremap_params().

However, this caused a regression as vrm->new_addr is now checked even
when MREMAP_FIXED and MREMAP_DONTUNMAP flags are not specified.  In this
case, vrm->new_addr can be garbage and create unexpected failures.

Fix this by moving the new_addr check after the vrm_implies_new_addr()
guard.  This ensures that the new_addr is only checked when the user has
specified one explicitly.

Link: https://lkml.kernel.org/r/20250828142657.770502-1-cmllamas@google.com
Fixes: 3215eaceca ("mm/mremap: refactor initial parameter sanity checks")
Signed-off-by: Carlos Llamas <cmllamas@google.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Carlos Llamas <cmllamas@google.com>
Cc: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Carlos Llamas
2025-08-28 14:26:56 +00:00
committed by Andrew Morton
parent 7989fdce69
commit 78d2d32f0b

View File

@@ -1774,15 +1774,18 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
if (!vrm->new_len) if (!vrm->new_len)
return -EINVAL; return -EINVAL;
/* Is the new length or address silly? */ /* Is the new length silly? */
if (vrm->new_len > TASK_SIZE || if (vrm->new_len > TASK_SIZE)
vrm->new_addr > TASK_SIZE - vrm->new_len)
return -EINVAL; return -EINVAL;
/* Remainder of checks are for cases with specific new_addr. */ /* Remainder of checks are for cases with specific new_addr. */
if (!vrm_implies_new_addr(vrm)) if (!vrm_implies_new_addr(vrm))
return 0; return 0;
/* Is the new address silly? */
if (vrm->new_addr > TASK_SIZE - vrm->new_len)
return -EINVAL;
/* The new address must be page-aligned. */ /* The new address must be page-aligned. */
if (offset_in_page(vrm->new_addr)) if (offset_in_page(vrm->new_addr))
return -EINVAL; return -EINVAL;