linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] Add overflow checks for several syscalls
@ 2023-01-16 11:58 Wupeng Ma
  2023-01-16 11:58 ` [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock Wupeng Ma
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Wupeng Ma @ 2023-01-16 11:58 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, mawupeng1, kuleshovmail, aneesh.kumar

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.

Similar logic is used to fix problems with multiple syscalls.

Changelog since v1[1]:
- only check overflow rather than access_ok to keep backward-compatibility

[1]: https://lore.kernel.org/lkml/20221228141701.c64add46c4b09aa17f605baf@linux-foundation.org/T/

Ma Wupeng (4):
  mm/mlock: return EINVAL if len overflows for mlock/munlock
  mm/mempolicy: return EINVAL for if len overflows for
    set_mempolicy_home_node
  mm/mempolicy: return EINVAL if len overflows for mbind
  mm/msync: return ENOMEM if len overflows for msync

 mm/mempolicy.c | 6 ++++--
 mm/mlock.c     | 8 ++++++++
 mm/msync.c     | 3 ++-
 3 files changed, 14 insertions(+), 3 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock
  2023-01-16 11:58 [PATCH v2 0/4] Add overflow checks for several syscalls Wupeng Ma
@ 2023-01-16 11:58 ` Wupeng Ma
  2023-01-16 20:51   ` Andrew Morton
  2023-01-16 11:58 ` [PATCH v2 2/4] mm/mempolicy: return EINVAL for if len overflows for set_mempolicy_home_node Wupeng Ma
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Wupeng Ma @ 2023-01-16 11:58 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, mawupeng1, kuleshovmail, aneesh.kumar

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.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 mm/mlock.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/mlock.c b/mm/mlock.c
index 7032f6dd0ce1..5a4e767feb28 100644
--- 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;
 
 	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;
+
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
 	lock_limit >>= PAGE_SHIFT;
 	locked = len >> PAGE_SHIFT;
@@ -632,12 +636,16 @@ SYSCALL_DEFINE3(mlock2, unsigned long, start, size_t, len, int, flags)
 SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
 {
 	int ret;
+	size_t old_len = len;
 
 	start = untagged_addr(start);
 
 	len = PAGE_ALIGN(len + (offset_in_page(start)));
 	start &= PAGE_MASK;
 
+	if (old_len != 0 && len == 0)
+		return -EINVAL;
+
 	if (mmap_write_lock_killable(current->mm))
 		return -EINTR;
 	ret = apply_vma_lock_flags(start, len, 0);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/4] mm/mempolicy: return EINVAL for if len overflows for set_mempolicy_home_node
  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 11:58 ` 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
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2023-01-16 11:58 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, mawupeng1, kuleshovmail, aneesh.kumar

From: Ma Wupeng <mawupeng1@huawei.com>

Return -EINVAL if len overflows for set_mempolicy_home_node.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 mm/mempolicy.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 02c8a712282f..153fda1ebb9c 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1488,6 +1488,7 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 		unsigned long, home_node, unsigned long, flags)
 {
 	struct mm_struct *mm = current->mm;
+	unsigned long old_len = len;
 	struct vm_area_struct *vma;
 	struct mempolicy *new;
 	unsigned long vmstart;
@@ -1515,7 +1516,7 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 	len = PAGE_ALIGN(len);
 	end = start + len;
 
-	if (end < start)
+	if (end < start || (old_len != 0 && len == 0))
 		return -EINVAL;
 	if (end == start)
 		return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/4] mm/mempolicy: return EINVAL if len overflows for mbind
  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 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 ` Wupeng Ma
  2023-01-16 11:58 ` [PATCH v2 4/4] mm/msync: return ENOMEM if len overflows for msync Wupeng Ma
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2023-01-16 11:58 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, mawupeng1, kuleshovmail, aneesh.kumar

From: Ma Wupeng <mawupeng1@huawei.com>

Return -EINVAL if len overflows for mbind.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 mm/mempolicy.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 153fda1ebb9c..4cb19a6e368b 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1258,6 +1258,7 @@ static long do_mbind(unsigned long start, unsigned long len,
 		     nodemask_t *nmask, unsigned long flags)
 {
 	struct mm_struct *mm = current->mm;
+	unsigned long old_len = len;
 	struct mempolicy *new;
 	unsigned long end;
 	int err;
@@ -1278,7 +1279,7 @@ static long do_mbind(unsigned long start, unsigned long len,
 	len = PAGE_ALIGN(len);
 	end = start + len;
 
-	if (end < start)
+	if (end < start || (old_len != 0 && len == 0))
 		return -EINVAL;
 	if (end == start)
 		return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 4/4] mm/msync: return ENOMEM if len overflows for msync
  2023-01-16 11:58 [PATCH v2 0/4] Add overflow checks for several syscalls Wupeng Ma
                   ` (2 preceding siblings ...)
  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 ` Wupeng Ma
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2023-01-16 11:58 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, mawupeng1, kuleshovmail, aneesh.kumar

From: Ma Wupeng <mawupeng1@huawei.com>

Return -ENOMEM if len overflows for msync.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 mm/msync.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/msync.c b/mm/msync.c
index ac4c9bfea2e7..8ac227ec38af 100644
--- a/mm/msync.c
+++ b/mm/msync.c
@@ -36,6 +36,7 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 	struct vm_area_struct *vma;
 	int unmapped_error = 0;
 	int error = -EINVAL;
+	size_t old_len = len;
 
 	start = untagged_addr(start);
 
@@ -48,7 +49,7 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 	error = -ENOMEM;
 	len = (len + ~PAGE_MASK) & PAGE_MASK;
 	end = start + len;
-	if (end < start)
+	if (end < start || (old_len != 0 && len == 0))
 		goto out;
 	error = 0;
 	if (end == start)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2023-01-16 20:51 UTC (permalink / raw)
  To: Wupeng Ma; +Cc: linux-mm, linux-kernel, kuleshovmail, aneesh.kumar

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.

>  	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);
_

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?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock
  2023-01-16 20:51   ` Andrew Morton
@ 2023-01-17  7:09     ` mawupeng
  0 siblings, 0 replies; 7+ messages in thread
From: mawupeng @ 2023-01-17  7:09 UTC (permalink / raw)
  To: akpm; +Cc: mawupeng1, linux-mm, linux-kernel, kuleshovmail, aneesh.kumar



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))

> 

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-01-17  7:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).