Skip to content

Commit 92ae03f

Browse files
committed
x86: merge 32/64-bit versions of 'strncpy_from_user()' and speed it up
This merges the 32- and 64-bit versions of the x86 strncpy_from_user() by just rewriting it in C rather than the ancient inline asm versions that used lodsb/stosb and had been duplicated for (trivial) differences between the 32-bit and 64-bit versions. While doing that, it also speeds them up by doing the accesses a word at a time. Finally, the new routines also properly handle the case of hitting the end of the address space, which we have never done correctly before (fs/namei.c has a hack around it for that reason). Despite all these improvements, it actually removes more lines than it adds, due to the de-duplication. Also, we no longer export (or define) the legacy __strncpy_from_user() function (that was defined to not do the user permission checks), since it's not actually used anywhere, and the user address space checks are built in to the new code. Other architecture maintainers have been notified that the old hack in fs/namei.c will be going away in the 3.5 merge window, in case they copied the x86 approach of being a bit cavalier about the end of the address space. Cc: linux-arch@vger.kernel.org Cc: Ingo Molnar <mingo@kernel.org> Cc: Peter Anvin" <hpa@zytor.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 79549c6 commit 92ae03f

6 files changed

Lines changed: 105 additions & 145 deletions

File tree

‎arch/x86/include/asm/uaccess.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@ struct __large_struct { unsigned long buf[100]; };
557557

558558
extern unsigned long
559559
copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
560+
extern __must_check long
561+
strncpy_from_user(char *dst, const char __user *src, long count);
560562

561563
/*
562564
* movsl can be slow when source and dest are not both 8-byte aligned

‎arch/x86/include/asm/uaccess_32.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ static inline unsigned long __must_check copy_from_user(void *to,
213213
return n;
214214
}
215215

216-
long __must_check strncpy_from_user(char *dst, const char __user *src,
217-
long count);
218-
long __must_check __strncpy_from_user(char *dst,
219-
const char __user *src, long count);
220-
221216
/**
222217
* strlen_user: - Get the size of a string in user space.
223218
* @str: The string to measure.

‎arch/x86/include/asm/uaccess_64.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ int __copy_in_user(void __user *dst, const void __user *src, unsigned size)
208208
}
209209
}
210210

211-
__must_check long
212-
strncpy_from_user(char *dst, const char __user *src, long count);
213-
__must_check long
214-
__strncpy_from_user(char *dst, const char __user *src, long count);
215211
__must_check long strnlen_user(const char __user *str, long n);
216212
__must_check long __strnlen_user(const char __user *str, long n);
217213
__must_check long strlen_user(const char __user *str);

‎arch/x86/lib/usercopy.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <linux/highmem.h>
88
#include <linux/module.h>
99

10+
#include <asm/word-at-a-time.h>
11+
1012
/*
1113
* best effort, GUP based copy_from_user() that is NMI-safe
1214
*/
@@ -41,3 +43,104 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
4143
return len;
4244
}
4345
EXPORT_SYMBOL_GPL(copy_from_user_nmi);
46+
47+
static inline unsigned long count_bytes(unsigned long mask)
48+
{
49+
mask = (mask - 1) & ~mask;
50+
mask >>= 7;
51+
return count_masked_bytes(mask);
52+
}
53+
54+
/*
55+
* Do a strncpy, return length of string without final '\0'.
56+
* 'count' is the user-supplied count (return 'count' if we
57+
* hit it), 'max' is the address space maximum (and we return
58+
* -EFAULT if we hit it).
59+
*/
60+
static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, long max)
61+
{
62+
long res = 0;
63+
64+
/*
65+
* Truncate 'max' to the user-specified limit, so that
66+
* we only have one limit we need to check in the loop
67+
*/
68+
if (max > count)
69+
max = count;
70+
71+
while (max >= sizeof(unsigned long)) {
72+
unsigned long c;
73+
74+
/* Fall back to byte-at-a-time if we get a page fault */
75+
if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
76+
break;
77+
/* This can write a few bytes past the NUL character, but that's ok */
78+
*(unsigned long *)(dst+res) = c;
79+
c = has_zero(c);
80+
if (c)
81+
return res + count_bytes(c);
82+
res += sizeof(unsigned long);
83+
max -= sizeof(unsigned long);
84+
}
85+
86+
while (max) {
87+
char c;
88+
89+
if (unlikely(__get_user(c,src+res)))
90+
return -EFAULT;
91+
dst[res] = c;
92+
if (!c)
93+
return res;
94+
res++;
95+
max--;
96+
}
97+
98+
/*
99+
* Uhhuh. We hit 'max'. But was that the user-specified maximum
100+
* too? If so, that's ok - we got as much as the user asked for.
101+
*/
102+
if (res >= count)
103+
return count;
104+
105+
/*
106+
* Nope: we hit the address space limit, and we still had more
107+
* characters the caller would have wanted. That's an EFAULT.
108+
*/
109+
return -EFAULT;
110+
}
111+
112+
/**
113+
* strncpy_from_user: - Copy a NUL terminated string from userspace.
114+
* @dst: Destination address, in kernel space. This buffer must be at
115+
* least @count bytes long.
116+
* @src: Source address, in user space.
117+
* @count: Maximum number of bytes to copy, including the trailing NUL.
118+
*
119+
* Copies a NUL-terminated string from userspace to kernel space.
120+
*
121+
* On success, returns the length of the string (not including the trailing
122+
* NUL).
123+
*
124+
* If access to userspace fails, returns -EFAULT (some data may have been
125+
* copied).
126+
*
127+
* If @count is smaller than the length of the string, copies @count bytes
128+
* and returns @count.
129+
*/
130+
long
131+
strncpy_from_user(char *dst, const char __user *src, long count)
132+
{
133+
unsigned long max_addr, src_addr;
134+
135+
if (unlikely(count <= 0))
136+
return 0;
137+
138+
max_addr = current_thread_info()->addr_limit.seg;
139+
src_addr = (unsigned long)src;
140+
if (likely(src_addr < max_addr)) {
141+
unsigned long max = max_addr - src_addr;
142+
return do_strncpy_from_user(dst, src, count, max);
143+
}
144+
return -EFAULT;
145+
}
146+
EXPORT_SYMBOL(strncpy_from_user);

‎arch/x86/lib/usercopy_32.c‎

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,93 +32,6 @@ static inline int __movsl_is_ok(unsigned long a1, unsigned long a2, unsigned lon
3232
#define movsl_is_ok(a1, a2, n) \
3333
__movsl_is_ok((unsigned long)(a1), (unsigned long)(a2), (n))
3434

35-
/*
36-
* Copy a null terminated string from userspace.
37-
*/
38-
39-
#define __do_strncpy_from_user(dst, src, count, res) \
40-
do { \
41-
int __d0, __d1, __d2; \
42-
might_fault(); \
43-
__asm__ __volatile__( \
44-
" testl %1,%1\n" \
45-
" jz 2f\n" \
46-
"0: lodsb\n" \
47-
" stosb\n" \
48-
" testb %%al,%%al\n" \
49-
" jz 1f\n" \
50-
" decl %1\n" \
51-
" jnz 0b\n" \
52-
"1: subl %1,%0\n" \
53-
"2:\n" \
54-
".section .fixup,\"ax\"\n" \
55-
"3: movl %5,%0\n" \
56-
" jmp 2b\n" \
57-
".previous\n" \
58-
_ASM_EXTABLE(0b,3b) \
59-
: "=&d"(res), "=&c"(count), "=&a" (__d0), "=&S" (__d1), \
60-
"=&D" (__d2) \
61-
: "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
62-
: "memory"); \
63-
} while (0)
64-
65-
/**
66-
* __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
67-
* @dst: Destination address, in kernel space. This buffer must be at
68-
* least @count bytes long.
69-
* @src: Source address, in user space.
70-
* @count: Maximum number of bytes to copy, including the trailing NUL.
71-
*
72-
* Copies a NUL-terminated string from userspace to kernel space.
73-
* Caller must check the specified block with access_ok() before calling
74-
* this function.
75-
*
76-
* On success, returns the length of the string (not including the trailing
77-
* NUL).
78-
*
79-
* If access to userspace fails, returns -EFAULT (some data may have been
80-
* copied).
81-
*
82-
* If @count is smaller than the length of the string, copies @count bytes
83-
* and returns @count.
84-
*/
85-
long
86-
__strncpy_from_user(char *dst, const char __user *src, long count)
87-
{
88-
long res;
89-
__do_strncpy_from_user(dst, src, count, res);
90-
return res;
91-
}
92-
EXPORT_SYMBOL(__strncpy_from_user);
93-
94-
/**
95-
* strncpy_from_user: - Copy a NUL terminated string from userspace.
96-
* @dst: Destination address, in kernel space. This buffer must be at
97-
* least @count bytes long.
98-
* @src: Source address, in user space.
99-
* @count: Maximum number of bytes to copy, including the trailing NUL.
100-
*
101-
* Copies a NUL-terminated string from userspace to kernel space.
102-
*
103-
* On success, returns the length of the string (not including the trailing
104-
* NUL).
105-
*
106-
* If access to userspace fails, returns -EFAULT (some data may have been
107-
* copied).
108-
*
109-
* If @count is smaller than the length of the string, copies @count bytes
110-
* and returns @count.
111-
*/
112-
long
113-
strncpy_from_user(char *dst, const char __user *src, long count)
114-
{
115-
long res = -EFAULT;
116-
if (access_ok(VERIFY_READ, src, 1))
117-
__do_strncpy_from_user(dst, src, count, res);
118-
return res;
119-
}
120-
EXPORT_SYMBOL(strncpy_from_user);
121-
12235
/*
12336
* Zero Userspace
12437
*/

‎arch/x86/lib/usercopy_64.c‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,6 @@
88
#include <linux/module.h>
99
#include <asm/uaccess.h>
1010

11-
/*
12-
* Copy a null terminated string from userspace.
13-
*/
14-
15-
#define __do_strncpy_from_user(dst,src,count,res) \
16-
do { \
17-
long __d0, __d1, __d2; \
18-
might_fault(); \
19-
__asm__ __volatile__( \
20-
" testq %1,%1\n" \
21-
" jz 2f\n" \
22-
"0: lodsb\n" \
23-
" stosb\n" \
24-
" testb %%al,%%al\n" \
25-
" jz 1f\n" \
26-
" decq %1\n" \
27-
" jnz 0b\n" \
28-
"1: subq %1,%0\n" \
29-
"2:\n" \
30-
".section .fixup,\"ax\"\n" \
31-
"3: movq %5,%0\n" \
32-
" jmp 2b\n" \
33-
".previous\n" \
34-
_ASM_EXTABLE(0b,3b) \
35-
: "=&r"(res), "=&c"(count), "=&a" (__d0), "=&S" (__d1), \
36-
"=&D" (__d2) \
37-
: "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
38-
: "memory"); \
39-
} while (0)
40-
41-
long
42-
__strncpy_from_user(char *dst, const char __user *src, long count)
43-
{
44-
long res;
45-
__do_strncpy_from_user(dst, src, count, res);
46-
return res;
47-
}
48-
EXPORT_SYMBOL(__strncpy_from_user);
49-
50-
long
51-
strncpy_from_user(char *dst, const char __user *src, long count)
52-
{
53-
long res = -EFAULT;
54-
if (access_ok(VERIFY_READ, src, 1))
55-
return __strncpy_from_user(dst, src, count);
56-
return res;
57-
}
58-
EXPORT_SYMBOL(strncpy_from_user);
59-
6011
/*
6112
* Zero Userspace
6213
*/

0 commit comments

Comments
 (0)