A length within a page of ULONG_MAX wraps when mlock() and munlock()
round the range to pages. At an aligned address the length becomes 0 and
the call is a silent success; at an unaligned one it becomes a single
page, which is then locked or unlocked. An overflowing start + len is
only caught in apply_vma_lock_flags(), after the RLIMIT_MEMLOCK check, so
mlock() returns -ENOMEM without CAP_IPC_LOCK and -EINVAL with it.

Return -EINVAL, as man 2 mlock documents, when start + len overflows or
the end overflows when rounded up to a page. Doing this before
can_do_mlock() also avoids making the result depend on
RLIMIT_MEMLOCK/CAP_IPC_LOCK. Requests that get a silent success today
now fail.

Reported-by: Park Tae-sun <[email protected]>
Closes: 
https://lore.kernel.org/all/[email protected]/
Signed-off-by: Jose A. Perez de Azpillaga <[email protected]>
---
 mm/mlock.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/mm/mlock.c b/mm/mlock.c
index cc28e5d7413a..a9e2b90c058f 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -608,6 +608,7 @@ static __must_check int do_mlock(unsigned long start, 
size_t len,
 {
        unsigned long locked;
        unsigned long lock_limit;
+       unsigned long end;
        int error = -ENOMEM;

        start = untagged_addr(start);
@@ -615,12 +616,19 @@ static __must_check int do_mlock(unsigned long start, 
size_t len,
        if (!len)
                return 0;

+       /* Reject a wrapping range before can_do_mlock() and the rlimit check. 
*/
+       end = start + len;
+       if (end < start)
+               return -EINVAL;
+       end = PAGE_ALIGN(end);
+       if (!end)
+               return -EINVAL;
+       start &= PAGE_MASK;
+       len = end - start;
+
        if (!can_do_mlock())
                return -EPERM;

-       len = PAGE_ALIGN(len + (offset_in_page(start)));
-       start &= PAGE_MASK;
-
        lock_limit = rlimit(RLIMIT_MEMLOCK);
        lock_limit >>= PAGE_SHIFT;
        locked = len >> PAGE_SHIFT;
@@ -676,6 +684,7 @@ SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, 
int, flags)

 SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
 {
+       unsigned long end;
        vma_flags_t flags = EMPTY_VMA_FLAGS;
        int ret;

@@ -684,8 +693,14 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
        if (!len)
                return 0;

-       len = PAGE_ALIGN(len + (offset_in_page(start)));
+       end = start + len;
+       if (end < start)
+               return -EINVAL;
+       end = PAGE_ALIGN(end);
+       if (!end)
+               return -EINVAL;
        start &= PAGE_MASK;
+       len = end - start;

        if (mmap_write_lock_killable(current->mm))
                return -EINTR;
--
2.55.0


Reply via email to