Skip to contentSkip to conversation

posix: Fix error checks when copying user strings

PATCH In review Jan Kiszka 2 participants 10 messages Mar 17, 2009

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 posix: Fix error checks when copying user strings

Obviously a conversion error while switching to __xn_safe*.

---

 ksrc/skins/posix/syscall.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
ksrc/skins/posix/syscall.c +4 -4
index 16303b3..0eca3d0 100644
@@ -1663,7 +1663,7 @@ static int __mq_open(struct pt_regs *regs)
 	len = __xn_safe_strncpy_from_user(name,
 					  (const char __user *)__xn_reg_arg1(regs),
 					  sizeof(name));
-	if (len <= 0)
+	if (len < 0)
 		return -EFAULT;
 
 	if (len >= sizeof(name))
@@ -1736,7 +1736,7 @@ static int __mq_unlink(struct pt_regs *regs)
 	len = __xn_safe_strncpy_from_user(name,
 					  (const char __user *)__xn_reg_arg1(regs),
 					  sizeof(name));
-	if (len <= 0)
+	if (len < 0)
 		return -EFAULT;
 
 	if (len >= sizeof(name))
@@ -2440,7 +2440,7 @@ static int __shm_open(struct pt_regs *regs)
 	len = __xn_safe_strncpy_from_user(name,
 					  (const char __user *)__xn_reg_arg1(regs),
 					  sizeof(name));
-	if (len <= 0)
+	if (len < 0)
 		return -EFAULT;
 
 	if (len >= sizeof(name))
@@ -2487,7 +2487,7 @@ static int __shm_unlink(struct pt_regs *regs)
 	len = __xn_safe_strncpy_from_user(name,
 					  (const char __user *)__xn_reg_arg1(regs),
 					  sizeof(name));
-	if (len <= 0)
+	if (len < 0)
 		return -EFAULT;
 
 	if (len >= sizeof(name))
#1 lore raw
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Signature · 1 line
                                                 Gilles.
author
#2 lore raw
Quoted 8 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#3 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

Signature · 1 line
                                                 Gilles.
author
#4 lore raw
Quoted 5 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka
Quoted 3 lines · Gilles Chanteperdrix
Quoted 2 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#5 lore raw
Quoted 10 lines · Jan Kiszka
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

So, what should I call if I care about the return value ?

Signature · 1 line
                                                 Gilles.
author
#6 lore raw
Quoted 11 lines · Gilles Chanteperdrix
Quoted 10 lines · Jan Kiszka
Quoted 9 lines · Gilles Chanteperdrix
Quoted 8 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

So, what should I call if I care about the return value ?

The old combo of access_rok() and __xn_strncpy_from_user() - ah, I see the issue: POSIX requires the length to report overflows to the users. Hmm, then back to the old code, just adding the missing address range check.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#7 lore raw
Quoted 14 lines · Jan Kiszka
Quoted 13 lines · Gilles Chanteperdrix
Quoted 12 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 9 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

Quoted 9 lines
Quoted 4 lines
Quoted 3 lines

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

So, what should I call if I care about the return value ?

The old combo of access_rok() and __xn_strncpy_from_user() - ah, I see the issue: POSIX requires the length to report overflows to the users. Hmm, then back to the old code, just adding the missing address range check.

Forget that, now I should have looked into our code: The "safe" version transparently calls __xn_strncpy_from_user, no need to go by foot here.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
author
#8 lore raw
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Gilles Chanteperdrix
Quoted 15 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 9 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

So, what should I call if I care about the return value ?

The old combo of access_rok() and __xn_strncpy_from_user() - ah, I see the issue: POSIX requires the length to report overflows to the users. Hmm, then back to the old code, just adding the missing address range check.

Forget that, now I should have looked into our code: The "safe" version transparently calls __xn_strncpy_from_user, no need to go by foot here.

Here is the real bug:

Quoted 11 lines
        long res;
        __do_strncpy_from_user(dst, src, count, res);
ffffffff804505cc:       48 85 c9                test   %rcx,%rcx
ffffffff804505cf:       74 0e                   je     ffffffff804505df <rthal_strncpy_from_user+0x1f>
ffffffff804505d1:       ac                      lods   %ds:(%rsi),%al
ffffffff804505d2:       aa                      stos   %al,%es:(%rdi)
ffffffff804505d3:       84 c0                   test   %al,%al
ffffffff804505d5:       74 05                   je     ffffffff804505dc <rthal_strncpy_from_user+0x1c>
ffffffff804505d7:       48 ff c9                dec    %rcx
ffffffff804505da:       75 f5                   jne    ffffffff804505d1 <rthal_strncpy_from_user+0x11>
ffffffff804505dc:       48 29 c9                sub    %rcx,%rcx
                                                         ^^^^^^^^^
Quoted 5 lines
        return res;
}
ffffffff804505df:       48 89 c8                mov    %rcx,%rax
ffffffff804505e2:       c9                      leaveq
ffffffff804505e3:       c3                      retq

And that is due to us lacking kernel commit
e0a96129db574d6365e3439d16d88517c437ab33.

Nevertheless, the existing checks against 0 in the POSIX layer weren't correct either. We need to fix both, but less ad-hoc than I tried.

Jan

#9 lore raw
Quoted 49 lines · Jan Kiszka
Quoted 18 lines · Jan Kiszka
Quoted 17 lines · Jan Kiszka
Quoted 16 lines · Gilles Chanteperdrix
Quoted 15 lines · Jan Kiszka
Quoted 11 lines · Gilles Chanteperdrix
Quoted 9 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 3 lines · Jan Kiszka

Obviously a conversion error while switching to __xn_safe*.

Well, I have just checked the kernel code, and 0 as a return value of strncpy_from_user is treated as a value in most places, even if not -EFAULT.

Better check our code: :) __xn_safe_strncpy_from_user works differently.

Then I would tend to consider that xn_safe_strncpy is broken.

No, because it not a derivate of strncpy_from_user, but an internal service optimized for the most common use cases (where you don't care about the precise return value).

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

So, what should I call if I care about the return value ?

The old combo of access_rok() and __xn_strncpy_from_user() - ah, I see the issue: POSIX requires the length to report overflows to the users. Hmm, then back to the old code, just adding the missing address range check.

Forget that, now I should have looked into our code: The "safe" version transparently calls __xn_strncpy_from_user, no need to go by foot here.

Here is the real bug:

Quoted 11 lines
        long res;
        __do_strncpy_from_user(dst, src, count, res);
ffffffff804505cc:       48 85 c9                test   %rcx,%rcx
ffffffff804505cf:       74 0e                   je     ffffffff804505df <rthal_strncpy_from_user+0x1f>
ffffffff804505d1:       ac                      lods   %ds:(%rsi),%al
ffffffff804505d2:       aa                      stos   %al,%es:(%rdi)
ffffffff804505d3:       84 c0                   test   %al,%al
ffffffff804505d5:       74 05                   je     ffffffff804505dc <rthal_strncpy_from_user+0x1c>
ffffffff804505d7:       48 ff c9                dec    %rcx
ffffffff804505da:       75 f5                   jne    ffffffff804505d1 <rthal_strncpy_from_user+0x11>
ffffffff804505dc:       48 29 c9                sub    %rcx,%rcx
                                                         ^^^^^^^^^
Quoted 5 lines
        return res;
}
ffffffff804505df:       48 89 c8                mov    %rcx,%rax
ffffffff804505e2:       c9                      leaveq
ffffffff804505e3:       c3                      retq
Quoted 5 lines

And that is due to us lacking kernel commit
e0a96129db574d6365e3439d16d88517c437ab33.

Nevertheless, the existing checks against 0 in the POSIX layer weren't correct either. We need to fix both, but less ad-hoc than I tried.

IMO, sem_open(""), mq_open(""), shm_open("") should return an error. I agree that -EFAULT is a bit harsh and that -EINVAL would be more apropriate, but I do not want to remove that check.

Signature · 1 line
					    Gilles.