A zero length mlock() or munlock() at an unaligned address is rounded up to a page, so mlock(addr + 5, 0) locks a page that was never asked for, and munlock(addr + 5, 0) unlocks one the caller may still rely on. An aligned zero length is already a no-op.
Return early for a zero length, before the rounding and before can_do_mlock(). This is a uapi change: with RLIMIT_MEMLOCK=0 and no CAP_IPC_LOCK, mlock(addr, 0) returns 0 instead of -EPERM, which is what munlock(addr, 0) already returns. man 2 mlock does not say what a zero length should do, and -EINVAL would break callers that pass one today and get 0. Reported-by: Park Tae-sun <[email protected]> Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Jose A. Perez de Azpillaga <[email protected]> --- mm/mlock.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/mlock.c b/mm/mlock.c index 4235a1518fc9..cc28e5d7413a 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -612,6 +612,9 @@ static __must_check int do_mlock(unsigned long start, size_t len, start = untagged_addr(start); + if (!len) + return 0; + if (!can_do_mlock()) return -EPERM; @@ -678,6 +681,9 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) start = untagged_addr(start); + if (!len) + return 0; + len = PAGE_ALIGN(len + (offset_in_page(start))); start &= PAGE_MASK; -- 2.55.0

