From: mawupeng <mawupeng1@huawei.com>
To: <akpm@linux-foundation.org>
Cc: <mawupeng1@huawei.com>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>, <kuleshovmail@gmail.com>,
<aneesh.kumar@linux.ibm.com>
Subject: Re: [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock
Date: Tue, 17 Jan 2023 15:09:08 +0800 [thread overview]
Message-ID: <5c83fae3-8eae-6303-4656-8da40a2c12b1@huawei.com> (raw)
In-Reply-To: <20230116125126.ed715ddf00ff4ffa2952ca29@linux-foundation.org>
On 2023/1/17 4:51, Andrew Morton wrote:
> On Mon, 16 Jan 2023 19:58:10 +0800 Wupeng Ma <mawupeng1@huawei.com> wrote:
>
>> From: Ma Wupeng <mawupeng1@huawei.com>
>>
>> While testing mlock, we have a problem if the len of mlock is ULONG_MAX.
>> The return value of mlock is zero. But nothing will be locked since the
>> len in do_mlock overflows to zero due to the following code in mlock:
>>
>> len = PAGE_ALIGN(len + (offset_in_page(start)));
>>
>> The same problem happens in munlock.
>>
>> Add new check and return -EINVAL to fix this overflowing scenarios since
>> they are absolutely wrong.
>>
>> ...
>>
>> --- a/mm/mlock.c
>> +++ b/mm/mlock.c
>> @@ -569,6 +569,7 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
>> unsigned long locked;
>> unsigned long lock_limit;
>> int error = -ENOMEM;
>> + size_t old_len = len;
>
> I'm not sure that "old_len" is a good identifier. It reads to me like
> "the length of the old mlocked region" or something.
>
> I really don't like it when functions modify the values of the incoming
> argument like this. It would be better to leave `len' alone and create
> a new_len or something.
Thanks for your reviewing.
You do have a point in saying that.
>
>> start = untagged_addr(start);
>>
>> @@ -578,6 +579,9 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
>> len = PAGE_ALIGN(len + (offset_in_page(start)));
>> start &= PAGE_MASK;
>>
>> + if (old_len != 0 && len == 0)
>> + return -EINVAL;
>
> It would be clearer to do this immediately after calculating the new
> value of `len'. Before going on to play with `start'.
>
> Can we do something like this?
>
> --- a/mm/mlock.c~a
> +++ a/mm/mlock.c
> @@ -575,7 +575,12 @@ static __must_check int do_mlock(unsigne
> if (!can_do_mlock())
> return -EPERM;
>
> - len = PAGE_ALIGN(len + (offset_in_page(start)));
> + if (len) {
> + len = PAGE_ALIGN(len + (offset_in_page(start)));
> + if (len == 0) /* overflow */
> + return -EINVAL;
> + }
> +
> start &= PAGE_MASK;
>
> lock_limit = rlimit(RLIMIT_MEMLOCK);
> _
It's really more appropriate to check like this, I will use this in the next patchset.
>
> That depends on how we handle len==0. afaict, mlock(len==0) will
> presently burn a bunch of cpu cycles (not that we want to optimize this
> case), do nothing then return 0?
We can add and a new check in if len == 0, since the similar check appears in
mbind, set_mempolicy_home_node, msync.
The origin len == 0 check for mlock/munlock can be found in apply_vma_lock_flags,
We can move this check to here to avoid burn a bunch of cpu cycles.
do_mlock
apply_vma_lock_flags
end = start + len;
if (end == start)
return 0;
Can we do something like this?
diff --git a/mm/mlock.c b/mm/mlock.c
index 7032f6dd0ce1..50a33abc1a2e 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -478,8 +478,6 @@ static int apply_vma_lock_flags(unsigned long start, size_t len,
end = start + len;
if (end < start)
return -EINVAL;
- if (end == start)
- return 0;
vma = mas_walk(&mas);
if (!vma)
return -ENOMEM;
@@ -575,7 +573,12 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
if (!can_do_mlock())
return -EPERM;
+ if (!len)
+ return 0;
len = PAGE_ALIGN(len + (offset_in_page(start)));
+ if (len == 0)
+ return -EINVAL;
+
start &= PAGE_MASK;
lock_limit = rlimit(RLIMIT_MEMLOCK);
@@ -632,10 +635,14 @@ SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
{
int ret;
-
start = untagged_addr(start);
+ if (!len)
+ return 0;
len = PAGE_ALIGN(len + (offset_in_page(start)));
+ if (len == 0)
+ return -EINVAL;
+
start &= PAGE_MASK;
if (mmap_write_lock_killable(current->mm))
>
next prev parent reply other threads:[~2023-01-17 7:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-16 11:58 [PATCH v2 0/4] Add overflow checks for several syscalls Wupeng Ma
2023-01-16 11:58 ` [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock Wupeng Ma
2023-01-16 20:51 ` Andrew Morton
2023-01-17 7:09 ` mawupeng [this message]
2023-01-16 11:58 ` [PATCH v2 2/4] mm/mempolicy: return EINVAL for if len overflows for set_mempolicy_home_node Wupeng Ma
2023-01-16 11:58 ` [PATCH v2 3/4] mm/mempolicy: return EINVAL if len overflows for mbind Wupeng Ma
2023-01-16 11:58 ` [PATCH v2 4/4] mm/msync: return ENOMEM if len overflows for msync Wupeng Ma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5c83fae3-8eae-6303-4656-8da40a2c12b1@huawei.com \
--to=mawupeng1@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=aneesh.kumar@linux.ibm.com \
--cc=kuleshovmail@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®