Skip to contentSkip to conversation

Add overflow checks for several syscalls

PATCH Superseded Wupeng Ma 2 participants 13 messages Jan 28, 2023 – Feb 9, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wupeng Ma

author
#0 lore raw

v3 cover Add overflow checks for several syscalls

From: Ma Wupeng <[email protected]>

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 v2[2]:
- modified the way of checking overflows based on Andrew's comments

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

[1]: https://lore.kernel.org/lkml/[email protected]/T/
[2]: https://lore.kernel.org/linux-mm/[email protected]/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 | 18 ++++++++++++------
 mm/mlock.c     | 14 ++++++++++++--
 mm/msync.c     |  9 ++++++---
 3 files changed, 30 insertions(+), 11 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/4 mm/mlock: return EINVAL if len overflows for mlock/munlock

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
 mm/mlock.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +12 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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)
+		return -EINVAL;
+
 	start &= PAGE_MASK;
 
 	lock_limit = rlimit(RLIMIT_MEMLOCK);
@@ -635,7 +639,13 @@ 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)));
+	if (!len)
+		return -EINVAL;
+
 	start &= PAGE_MASK;
 
 	if (mmap_write_lock_killable(current->mm))
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 35 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
  mm/mlock.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +0 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla
  	if (!can_do_mlock())

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 10 lines
mm/mlock.c +6 -0
  		return -EPERM;
 
+	if (!len)
+		return 0;
+
  	len = PAGE_ALIGN(len + (offset_in_page(start)));
+	if (!len)
+		return -EINVAL;
+
  	start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Signature · 3 lines
Thanks,

David / dhildenb
author
#3 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
  mm/mlock.c | 14 ++++++++++++--

  1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +1 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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;
Quoted 8 lines

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 6 lines
mm/mlock.c +4 -0
+
       len = PAGE_ALIGN(len + (offset_in_page(start)));
+    if (!len)
+        return -EINVAL;
+
       start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Overflow is not checked anywhere however the ordinary return early if len == 0 is detected in apply_vma_lock_flags().

do_mlock
apply_vma_lock_flags

	end = start + len;
	if (end == start)
	  return 0;

Move the checking to the begin is easier to detect overflows and make the logic clearer and avoid burn a bunch of cpu cycles.

Quoted 1 line
#4 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 51 lines · mawupeng
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
   mm/mlock.c | 14 ++++++++++++--
   1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +1 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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;
Quoted 8 lines

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 6 lines
mm/mlock.c +4 -0
+
       len = PAGE_ALIGN(len + (offset_in_page(start)));
+    if (!len)
+        return -EINVAL;
+
       start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Quoted 2 lines

Overflow is not checked anywhere however the ordinary return early if len == 0 is detected in apply_vma_lock_flags().

I meant the

end = start + len;
if (end < start)

	return -EINVAL;

Essentially, what I wanted to double-check is that with your changes, we catch all kinds of overflows as documented in the man page, correct?

Signature · 3 lines
Thanks,

David / dhildenb
author
#5 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 55 lines · David Hildenbrand
Quoted 51 lines · mawupeng
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
   mm/mlock.c | 14 ++++++++++++--

   1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +1 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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;
Quoted 8 lines

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 6 lines
mm/mlock.c +4 -0
+
        len = PAGE_ALIGN(len + (offset_in_page(start)));
+    if (!len)
+        return -EINVAL;
+
        start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Quoted 2 lines

Overflow is not checked anywhere however the ordinary return early if len == 0 is detected in apply_vma_lock_flags().

Quoted 7 lines

I meant the

end = start + len;
if (end < start)
    return -EINVAL;

Essentially, what I wanted to double-check is that with your changes, we catch all kinds of overflows as documented in the man page, correct?

Oh i see. You are right, The "ordinary" overflows are detected for mlock/munlock in apply_vma_lock_flags().

Yes, we may need to update the man page for all these four syscalls.

Thanks,

mawupeng.

Quoted 1 line
#6 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 64 lines · mawupeng
Quoted 61 lines · David Hildenbrand
Quoted 51 lines · mawupeng
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
    mm/mlock.c | 14 ++++++++++++--
    1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +1 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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;
Quoted 8 lines

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 6 lines
mm/mlock.c +4 -0
+
        len = PAGE_ALIGN(len + (offset_in_page(start)));
+    if (!len)
+        return -EINVAL;
+
        start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Quoted 2 lines

Overflow is not checked anywhere however the ordinary return early if len == 0 is detected in apply_vma_lock_flags().

I meant the

end = start + len;
if (end < start)
    return -EINVAL;

Quoted 5 lines
Quoted 1 line

Essentially, what I wanted to double-check is that with your changes, we catch all kinds of overflows as documented in the man page, correct?

Oh i see. You are right, The "ordinary" overflows are detected for mlock/munlock in apply_vma_lock_flags().

Yes, we may need to update the man page for all these four syscalls.

E.g., mlock() already documents "EINVAL (mlock(), mlock2(), and munlock()) The result of the addition addr+len was less than addr (e.g., the addition may have resulted in an overflow)."

Just to rephrase my question what I wanted to double-check: are we now identifying all such overflows or are you aware of other corner cases?

Signature · 3 lines
Thanks,

David / dhildenb
author
#7 lore raw

Re: [PATCH v3 1/4] mm/mlock: return EINVAL if len overflows for mlock/munlock

Quoted 67 lines · David Hildenbrand
Quoted 66 lines · mawupeng
Quoted 62 lines · David Hildenbrand
Quoted 51 lines · mawupeng
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Return 0 early to avoid burn a bunch of cpu cycles if len == 0.

---
    mm/mlock.c | 14 ++++++++++++--

    1 file changed, 12 insertions(+), 2 deletions(-)
mm/mlock.c +1 -2
index 7032f6dd0ce1..eb09968ba27f 100644
@@ -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,13 @@ 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;
Quoted 8 lines

mm/mlock.c static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla · line 583

Quoted 6 lines
mm/mlock.c +4 -0
+
         len = PAGE_ALIGN(len + (offset_in_page(start)));
+    if (!len)
+        return -EINVAL;
+
         start &= PAGE_MASK;

The "ordinary" overflows are detected in apply_vma_lock_flags(), correct?

Quoted 2 lines

Overflow is not checked anywhere however the ordinary return early if len == 0 is detected in apply_vma_lock_flags().

I meant the

end = start + len;
if (end < start)
     return -EINVAL;

Essentially, what I wanted to double-check is that with your changes, we catch all kinds of overflows as documented in the man page, correct?

Quoted 7 lines
Quoted 3 lines

Oh i see. You are right, The "ordinary" overflows are detected for mlock/munlock in apply_vma_lock_flags().

Yes, we may need to update the man page for all these four syscalls.

E.g., mlock() already documents "EINVAL (mlock(),  mlock2(),  and munlock()) The result of the addition addr+len was less than addr (e.g., the addition may have resulted in an overflow)."

Just to rephrase my question what I wanted to double-check: are we now identifying all such overflows or are you aware of other corner cases?

AFAICT. There is no cornel cases now.

Pervious normal overflow can be detected via the following codes as you mentioned.

end = start + len;
if (end < start)

	return -EINVAL;

this is fine for normal overflows.

But the len may be zero in PAGE_ALIGN(len + (offset_in_page(start))).

This leed to two scenarios for len == 0:
a) user pass len as zero. this is fine, we don't need to do anything.
b) overflow scenarios. we need to return err rather than response ok as above.

Quoted 1 line
author
#8 lore raw

PATCH v3 2/4 mm/mempolicy: return EINVAL for if len overflows for set_mempolicy_home_node

In reply to Wupeng Ma

From: Ma Wupeng <[email protected]>

Check and return 0 if len == 0 at the beginning of the function. Return -EINVAL if len overflows for set_mempolicy_home_node.

---
 mm/mempolicy.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/mempolicy.c +6 -3
index 02c8a712282f..85c5d3c2503b 100644
@@ -1512,13 +1512,16 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, le
 	if (home_node >= MAX_NUMNODES || !node_online(home_node))
 		return -EINVAL;
 
+	if (!len)
+		return 0;
+
 	len = PAGE_ALIGN(len);
-	end = start + len;
+	if (!len)
+		return -EINVAL;
 
+	end = start + len;
 	if (end < start)
 		return -EINVAL;
-	if (end == start)
-		return 0;
 	mmap_write_lock(mm);
 	for_each_vma_range(vmi, vma, end) {
 		vmstart = max(start, vma->vm_start);
Signature · 1 line
2.25.1
author
#9 lore raw

PATCH v3 3/4 mm/mempolicy: return EINVAL if len overflows for mbind

In reply to Wupeng Ma

From: Ma Wupeng <[email protected]>

Check and return 0 if len == 0 at the beginning of the function. Return -EINVAL if len overflows for mbind.

---
 mm/mempolicy.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/mempolicy.c +6 -3
index 85c5d3c2503b..7791be5a2677 100644
@@ -1275,13 +1275,16 @@ static long do_mbind(unsigned long start, unsigned long len,
 	if (mode == MPOL_DEFAULT)
 		flags &= ~MPOL_MF_STRICT;
 
+	if (!len)
+		return 0;
+
 	len = PAGE_ALIGN(len);
-	end = start + len;
+	if (!len)
+		return -EINVAL;
 
+	end = start + len;
 	if (end < start)
 		return -EINVAL;
-	if (end == start)
-		return 0;
 
 	new = mpol_new(mode, mode_flags, nmask);
 	if (IS_ERR(new))
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH v3 4/4 mm/msync: return ENOMEM if len overflows for msync

In reply to Wupeng Ma

From: Ma Wupeng <[email protected]>

Check and return 0 if len == 0 at the beginning of the function. Return -ENOMEM if len overflows for msync.

---
 mm/msync.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/msync.c +6 -3
index ac4c9bfea2e7..3104c97d70d3 100644
@@ -46,13 +46,16 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)
 	if ((flags & MS_ASYNC) && (flags & MS_SYNC))
 		goto out;
 	error = -ENOMEM;
+	if (!len)
+		return 0;
+
 	len = (len + ~PAGE_MASK) & PAGE_MASK;
+	if (!len)
+		goto out;
+
 	end = start + len;
 	if (end < start)
 		goto out;
-	error = 0;
-	if (end == start)
-		goto out;
 	/*
 	 * If the interval [start,end) covers some unmapped address ranges,
 	 * just ignore them, but return -ENOMEM at the end. Besides, if the
Signature · 1 line
2.25.1
#11 lore raw
In reply to Wupeng Ma
Quoted 13 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Quoted 7 lines

Similar logic is used to fix problems with multiple syscalls.

Changelog since v2[2]:
- modified the way of checking overflows based on Andrew's comments

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

Do you have some test cases and could we add them to LTP, for example?

Signature · 3 lines
Thanks,

David / dhildenb
author
#12 lore raw
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Wupeng Ma

From: Ma Wupeng <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines

Changelog since v2[2]:
- modified the way of checking overflows based on Andrew's comments

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

Do you have some test cases and could we add them to LTP, for example?

The basic idea is setting param len to ULONG_MAX. while will overflow if PAGE_ALIGN(len).

Here are some simple examples:

mlock(addr, ULONG_MAX);
set_mempolicy_home_node(addr, ULONG_MAX, 0, 0);
mbind(addr, ULONG_MAX, MPOL_BIND, &nodemask, max_node, MPOL_MF_MOVE_ALL); msync(addr, ULONG_MAX, MS_ASYNC);

Quoted 1 line