Skip to contentSkip to conversation

mm/msync: return -ENOMEM if page-aligned length overflows

PATCH In review Lorenzo Stoakes (ARM) 7 participants 8 messages Nov 20 – Nov 24

Subsystems (3)
lore
Share & tools
lore

Lorenzo Stoakes (ARM)

author maintainer
#0 lore raw

PATCH 1/1 mm/msync: return -ENOMEM if page-aligned length overflows

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

---
 mm/msync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
mm/msync.c +4 -6
index 90b491a27a14..ced41d3c2903 100644
@@ -46,14 +46,12 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 		goto out;
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
-	error = -ENOMEM;
+	if (!len)
+		return 0;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;
 	end = start + len;
-	if (end < start)
-		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
+	if (end <= start)
+		return -ENOMEM;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the

---
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261120-msync-enomem-fix-95f830e305dd

Cheers,

Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Lorenzo Stoakes (ARM)

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Quoted 7 lines

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Signature · 3 lines
Cheers,

David
reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Lorenzo Stoakes (ARM)

+cc linux-api

Quoted 3 lines · Lorenzo Stoakes (ARM)

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

Weird, as if it was intended, but seems to be pre-git, so who knows why.

Quoted 2 lines

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Right.

Quoted 7 lines

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Quoted 9 lines
---
 mm/msync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
mm/msync.c +0 -0
index 90b491a27a14..ced41d3c2903 100644

mm/msync.c SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags) · line 51

Quoted 7 lines
mm/msync.c +2 -1
@@ -46,14 +46,12 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 		goto out;
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
-	error = -ENOMEM;
+	if (!len)
+		return 0;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;

ALIGN(PAGE_SIZE) or something?
But fair if out of scope.

Quoted 17 lines
mm/msync.c +2 -6
 	end = start + len;
-	if (end < start)
-		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
+	if (end <= start)
+		return -ENOMEM;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the
 
---

base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261120-msync-enomem-fix-95f830e305dd

Cheers,

author maintainer
#3 lore raw
Quoted 8 lines · Vlastimil Babka (SUSE)

+cc linux-api

Quoted 3 lines · Lorenzo Stoakes (ARM)

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

Weird, as if it was intended, but seems to be pre-git, so who knows why.

Yeah, likely unintended but doesn't really matter.

I'd like us to completely centralise these kinds of checks for mm syscalls but on the laterbase...

Quoted 9 lines
Quoted 3 lines

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Right.

Quoted 3 lines

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Quoted 6 lines
Quoted 4 lines

Thanks!

Quoted 5 lines
Quoted 5 lines
---
 mm/msync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
Quoted 15 lines

mm/msync.c SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags) · line 51

Quoted 12 lines
mm/msync.c +2 -1
index 90b491a27a14..ced41d3c2903 100644
@@ -46,14 +46,12 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 		goto out;
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
-	error = -ENOMEM;
+	if (!len)
+		return 0;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;

ALIGN(PAGE_SIZE) or something?
But fair if out of scope.

Yeah can do on respin if needed, but right now I have more deletions than insertions which is rare for me so :P (maybe not the best reasoning, but still ;)

Quoted 13 lines
Quoted 13 lines
mm/msync.c +2 -5
 	end = start + len;
-	if (end < start)
-		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
+	if (end <= start)
+		return -ENOMEM;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the
Quoted 6 lines
Quoted 5 lines

---
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261120-msync-enomem-fix-95f830e305dd

Cheers,

--
Cheers, Lorenzo

Reviewed-by on v1 1/1
#4 lore raw
In reply to Lorenzo Stoakes (ARM)
Quoted 10 lines · Lorenzo Stoakes (ARM)

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Quoted 7 lines
---
 mm/msync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Thanks for your work;

Quoted 30 lines
mm/msync.c +4 -7
index 90b491a27a14..ced41d3c2903 100644
@@ -46,14 +46,12 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 		goto out;
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
-	error = -ENOMEM;
+	if (!len)
+		return 0;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;
 	end = start + len;
-	if (end < start)
-		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
+	if (end <= start)
+		return -ENOMEM;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the
 
---

base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
change-id: 20261120-msync-enomem-fix-95f830e305dd

Cheers,

Reviewed-by on v1 1/1
#5 lore raw
In reply to Lorenzo Stoakes (ARM)
Quoted 9 lines · On Fri, 20 Nov 2026 10:59:56 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Looks all correct and make sense to me.

Quoted 5 lines

Thanks,
SJ

[...]

maintainer tree notice
#6 lore raw

+ mm-msync-return-enomem-if-page-aligned-length-overflows.patch added to mm-hotfixes-unstable branch

In reply to Lorenzo Stoakes (ARM)
The patch titled Subject: mm/msync: return -ENOMEM if page-aligned length overflows has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: mm/msync: return -ENOMEM if page-aligned length overflows
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-msync-return-enomem-if-page-aligned-length-overflows.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-msync-return-enomem-if-page-aligned-length-overflows.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Subject: mm/msync: return -ENOMEM if page-aligned length overflows
Date: Fri, 20 Nov 2026 10:59:56 +0000

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

 mm/msync.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
mm/msync.c renamed from mm/msync.c~mm-msync-return-enomem-if-page-aligned-length-overflows +4 -6
@@ -46,14 +46,12 @@ SYSCALL_DEFINE3(msync, unsigned long, st
 		goto out;
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
-	error = -ENOMEM;
+	if (!len)
+		return 0;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;
 	end = start + len;
-	if (end < start)
-		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
+	if (end <= start)
+		return -ENOMEM;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the

_

Patches currently in -mm which might be from [email protected] are

mm-msync-return-enomem-if-page-aligned-length-overflows.patch mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch mm-only-assert-mmap-locks-if-config_debug_vm-or-lockdep-is-set.patch mm-only-assert-vma-locks-if-config_debug_vm-is-set.patch mm-convert-warn_on_onces-to-vm_warn_on_once-in-vma-lock-code.patch mm-madvise-zap-markers-from-guard-regions.patch

reviewer Reviewed-by on v1 1/1
#7 lore raw
In reply to Lorenzo Stoakes (ARM)
Quoted 7 lines · Lorenzo Stoakes (ARM)

Currently the msync() system call will simply return 0 if you provide a length parameter which, when page-aligned (rounding up) overflows (i.e. SIZE_MAX - n, where n is in the range [0, PAGE_SIZE - 1).)

This requires broken input, and nobody has complained about this, but for correctness this should really return -ENOMEM, so do so.

Quoted 7 lines

Also, since error is assigned within the following loop, remove the unnecessary assignments prior to it.

Signature · 1 line
Pedro