Re: [PATCH] MIPS: Fix strnlen_user access check

From: Jinyang He
Date: Mon Apr 12 2021 - 02:08:01 EST


On 04/12/2021 11:02 AM, Tiezhu Yang wrote:

On 04/11/2021 07:04 PM, Jinyang He wrote:
Commit 04324f44cb69 ("MIPS: Remove get_fs/set_fs") brought a problem for
strnlen_user(). Jump out when checking access_ok() with condition that
(s + strlen(s)) < __UA_LIMIT <= (s + n). The old __strnlen_user_asm()
just checked (ua_limit & s) without checking (ua_limit & (s + n)).
Therefore, find strlen form s to __UA_LIMIT - 1 in that condition.

Signed-off-by: Jinyang He <hejinyang@xxxxxxxxxxx>
---
arch/mips/include/asm/uaccess.h | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/arch/mips/include/asm/uaccess.h b/arch/mips/include/asm/uaccess.h
index 91bc7fb..85ba0c8 100644
--- a/arch/mips/include/asm/uaccess.h
+++ b/arch/mips/include/asm/uaccess.h
@@ -630,8 +630,15 @@ static inline long strnlen_user(const char __user *s, long n)
{
long res;
- if (!access_ok(s, n))
- return -0;
+ if (unlikely(n <= 0))
+ return 0;
+
+ if (!access_ok(s, n)) {
+ if (!access_ok(s, 0))
+ return 0;
+
+ n = __UA_LIMIT - (unsigned long)s - 1;
+ }
might_fault();
__asm__ __volatile__(

The following simple changes are OK to fix this issue?

diff --git a/arch/mips/include/asm/uaccess.h b/arch/mips/include/asm/uaccess.h
index 91bc7fb..eafc99b 100644
--- a/arch/mips/include/asm/uaccess.h
+++ b/arch/mips/include/asm/uaccess.h
@@ -630,8 +630,8 @@ static inline long strnlen_user(const char __user *s, long n)
{
long res;

- if (!access_ok(s, n))
- return -0;
+ if (!access_ok(s, 1))
+ return 0;

might_fault();
__asm__ __volatile__(

Thanks,
Tiezhu

Thanks for your comment. That looks similar to other archs, but I don't
know how the access_ok() implementation in other archs.

Using access_ok(s, 0) is similar to the old strnlen_user(). Using
access_ok(s, 1) may have a problem in this extreme case,
s = __UA_LIMIT - 1, *s = 0, and we hope it returns 1. But it returns 0 by
!access_ok(s, 1). Of course, it is so extrme.

More importantly, I want to set up a maximum for strnlen_user_asm. And do
not access the part of beyond __ua_limit. As follow shows,

+-----------+
| ... |
+-----------+ <---- s + n
| 0 |
+-----------+
| s |
+-----------+
| r |
+-----------+
| e |
+-----------+
| h |
+-----------+
| t |
+-----------+
| o |
+-----------+ <---- __UA_LIMIT
| r |
+-----------+
| t |
+-----------+
| s |
+-----------+ <---- s
| ... |
+-----------+

It is dangerous to access "others", for user, only "str" is safe.

I don't know whether it would be happend, I just limited it by change `n`.
Should do other things if meet __UA_LIMIT - 1?

Thanks,
Jinyang