Skip to contentSkip to conversation

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

PATCH Superseded Carlos Llamas 4 participants 13 messages Aug 28, 2025

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

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

Commit 3215eaceca87 ("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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")
---
 mm/mremap.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
mm/mremap.c +5 -6
index e618a706aff5..692acb0f9ea2 100644
@@ -1771,18 +1771,17 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	 * for DOS-emu "duplicate shm area" thing. But
 	 * a zero new-len is nonsensical.
 	 */
-	if (!vrm->new_len)
-		return -EINVAL;
-
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	if (!vrm->new_len || vrm->new_len > TASK_SIZE)
 		return -EINVAL;
 
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		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. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
Signature · 1 line
2.51.0.268.g9569e192d0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Quoted 6 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

I assume this showed up with clang?

Quoted 35 lines
---
 mm/mremap.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
mm/mremap.c +5 -6
index e618a706aff5..692acb0f9ea2 100644
@@ -1771,18 +1771,17 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	 * for DOS-emu "duplicate shm area" thing. But
 	 * a zero new-len is nonsensical.
 	 */
-	if (!vrm->new_len)
-		return -EINVAL;
-
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	if (!vrm->new_len || vrm->new_len > TASK_SIZE)
 		return -EINVAL;
 
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		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. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
Signature · 1 line
2.51.0.268.g9569e192d0-goog
#2 lore raw
Quoted 9 lines · Liam R. Howlett
Quoted 8 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Quoted 8 lines
Quoted 6 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

I assume this showed up with clang?

Right.

The specific test that broke on our end was this:
https://android.googlesource.com/platform/bionic/+/HEAD/tests/__cxa_atexit_test.cpp
Although I'm not exactly sure how __cxa_atexit() implementation uses mremap() underneath.

#3 lore raw
Quoted 17 lines · Carlos Llamas
Quoted 16 lines · Liam R. Howlett
Quoted 14 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")
Quoted 8 lines
Quoted 1 line

I assume this showed up with clang?

Right.

The specific test that broke on our end was this:
https://android.googlesource.com/platform/bionic/+/HEAD/tests/__cxa_atexit_test.cpp
Although I'm not exactly sure how __cxa_atexit() implementation uses mremap() underneath.

Ok, here is the specific call to mremap() in the test:
https://android.googlesource.com/platform/bionic/+/HEAD/libc/bionic/atexit.cpp#197

#4 lore raw
In reply to Carlos Llamas
Quoted 7 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Yikes, sorry my mistake.

Quoted 7 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

You need a Cc: Stable.

Quoted 9 lines
---
 mm/mremap.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
mm/mremap.c +0 -0
index e618a706aff5..692acb0f9ea2 100644

mm/mremap.c static unsigned long check_mremap_params(struct vma_remap_struct *vrm) · line 1775

Quoted 5 lines
mm/mremap.c +0 -2
@@ -1771,18 +1771,17 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	 * for DOS-emu "duplicate shm area" thing. But
 	 * a zero new-len is nonsensical.
 	 */
-	if (!vrm->new_len)
-		return -EINVAL;

Please don't refactor code at the same time, especially as this is a hotfix.

This line is associated with the above comment.

mm/mremap.c static unsigned long check_mremap_params(struct vma_remap_struct *vrm) · line 1775

Quoted 6 lines
mm/mremap.c +1 -4
-
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	if (!vrm->new_len || vrm->new_len > TASK_SIZE)
 		return -EINVAL;

Yeah, it's cute but by removing the 'silly' comment you're making this associated with the comment above and... yeah.

This should be:

	/*
	 * We allow a zero old-len as a special case
	 * for DOS-emu "duplicate shm area" thing. But
	 * a zero new-len is nonsensical.
	 */
	if (!vrm->new_len)
		return -EINVAL;

	/* Is the new length silly? */
	if (vrm->new_len > TASK_SIZE)
		return -EINVAL;

mm/mremap.c static unsigned long check_mremap_params(struct vma_remap_struct *vrm) · line 1784

Quoted 9 lines
mm/mremap.c +4 -0
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		return 0;
 
+	/* Is the new address silly? */
+	if (vrm->new_addr > TASK_SIZE - vrm->new_len)
+		return -EINVAL;
+

Obviously this bit is fine :)

Sorry again, this was just a mistake on my part.

Quoted 6 lines
mm/mremap.c +0 -1
 	/* The new address must be page-aligned. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
--

2.51.0.268.g9569e192d0-goog

I'm curious why only you guys have seen it, a theory is bionic is sending random stuff to this parameter when unspecified, and glibc is not.

But obviously this fix is correct, and the original code needs fixing.

Please respin a v2 as per above.

Cheers, Lorenzo

#5 lore raw
Quoted 2 lines · Lorenzo Stoakes

I'm curious why only you guys have seen it, a theory is bionic is sending random stuff to this parameter when unspecified, and glibc is not.

I honestly don't know and it could very well be only with bionic or clang. I'll try to find out more about this, maybe glibc implementation zeros out the 'new_addres' register and bionic doesn't.

Quoted 4 lines

But obviously this fix is correct, and the original code needs fixing.

Please respin a v2 as per above.

Sounds good.

#6 lore raw
In reply to Lorenzo Stoakes
Quoted 12 lines · Lorenzo Stoakes
Quoted 7 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Yikes, sorry my mistake.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

You need a Cc: Stable.

No need as the commit being fixed is from 6.17-rc1?
But it's better to use "[PATCH mm-hotfixes]" to make sure it goes to 6.17 and not the next merge window.

#7 lore raw
Quoted 17 lines · Vlastimil Babka
Quoted 16 lines · Lorenzo Stoakes
Quoted 7 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Yikes, sorry my mistake.

Quoted 5 lines

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.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

You need a Cc: Stable.

No need as the commit being fixed is from 6.17-rc1?
But it's better to use "[PATCH mm-hotfixes]" to make sure it goes to 6.17 and not the next merge window.

Ah haha really? I'm losing track of my patches.

Yeah sure as per Vlasta then Carlos :)

#8 lore raw
Quoted 23 lines · Lorenzo Stoakes
Quoted 22 lines · Vlastimil Babka
Quoted 20 lines · Lorenzo Stoakes
Quoted 7 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Yikes, sorry my mistake.

Quoted 7 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

You need a Cc: Stable.

Quoted 8 lines
Quoted 4 lines

No need as the commit being fixed is from 6.17-rc1?
But it's better to use "[PATCH mm-hotfixes]" to make sure it goes to 6.17 and not the next merge window.

Ah haha really? I'm losing track of my patches.

Yeah sure as per Vlasta then Carlos :)

Oops, sorry my v2 raced with this.

#9 lore raw
Quoted 29 lines · Carlos Llamas
Quoted 28 lines · Lorenzo Stoakes
Quoted 26 lines · Vlastimil Babka
Quoted 12 lines · Lorenzo Stoakes
Quoted 7 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Yikes, sorry my mistake.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

You need a Cc: Stable.

No need as the commit being fixed is from 6.17-rc1?
But it's better to use "[PATCH mm-hotfixes]" to make sure it goes to 6.17 and not the next merge window.

Quoted 5 lines
Quoted 3 lines

Ah haha really? I'm losing track of my patches.

Yeah sure as per Vlasta then Carlos :)

Oops, sorry my v2 raced with this.

Nevermind, Andrew can fixup locally. Thanks :)

#10 lore raw

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

In reply to Lorenzo Stoakes

Commit 3215eaceca87 ("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.

Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

---
v2:
- split out vrm->new_len into individual checks
- cc stable, collect tags

v1:
https://lore.kernel.org/all/[email protected]/

 mm/mremap.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/mremap.c +6 -3
index e618a706aff5..35de0a7b910e 100644
@@ -1774,15 +1774,18 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	if (!vrm->new_len)
 		return -EINVAL;
 
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	/* Is the new length silly? */
+	if (vrm->new_len > TASK_SIZE)
 		return -EINVAL;
 
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		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. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
Signature · 1 line
2.51.0.268.g9569e192d0-goog
Reviewed-by on v2 1/1
#11 lore raw
Quoted 8 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Quoted 5 lines

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.

Not necessary, but for mm-hotfixes please, Andrew.

Quoted 3 lines
Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")
Quoted 37 lines

---
v2:
- split out vrm->new_len into individual checks
- cc stable, collect tags

v1:
https://lore.kernel.org/all/[email protected]/

 mm/mremap.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/mremap.c +6 -3
index e618a706aff5..35de0a7b910e 100644
@@ -1774,15 +1774,18 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	if (!vrm->new_len)
 		return -EINVAL;
 
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	/* Is the new length silly? */
+	if (vrm->new_len > TASK_SIZE)
 		return -EINVAL;
 
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		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. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
Reviewed-by on v2 1/1
#12 lore raw
In reply to Carlos Llamas

(For future reference) please send separately rather than in reply to first :)

Otherwise harder for me to find your series!

Quoted 8 lines · Carlos Llamas

Commit 3215eaceca87 ("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.

Quoted 5 lines

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.

Yeah oopsies on me suggesting this :P losing track of my own patches :)

Quoted 3 lines
Fixes 3215eaceca87 ("mm/mremap: refactor initial parameter sanity checks")

LGTM, so:

And again thanks so much for this! :)

Quoted 38 lines

---
v2:
- split out vrm->new_len into individual checks
- cc stable, collect tags

v1:
https://lore.kernel.org/all/[email protected]/

 mm/mremap.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/mremap.c +6 -4
index e618a706aff5..35de0a7b910e 100644
@@ -1774,15 +1774,18 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)
 	if (!vrm->new_len)
 		return -EINVAL;
 
-	/* Is the new length or address silly? */
-	if (vrm->new_len > TASK_SIZE ||
-	    vrm->new_addr > TASK_SIZE - vrm->new_len)
+	/* Is the new length silly? */
+	if (vrm->new_len > TASK_SIZE)
 		return -EINVAL;
 
 	/* Remainder of checks are for cases with specific new_addr. */
 	if (!vrm_implies_new_addr(vrm))
 		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. */
 	if (offset_in_page(vrm->new_addr))
 		return -EINVAL;
--
Quoted 2 lines

2.51.0.268.g9569e192d0-goog

Cheers, Lorenzo