x86: use generic strncpy_from_user routine
The generic strncpy_from_user() is not really optimal, since it is designed to work on both little-endian and big-endian. And on little-endian you can simplify much of the logic to find the first zero byte, since little-endian arithmetic doesn't have to worry about the carry bit propagating into earlier bytes (only later bytes, which we don't care about). But I have patches to make the generic routines use the architecture- specific <asm/word-at-a-time.h> infrastructure, so that we can regain the little-endian optimizations. But before we do that, switch over to the generic routines to make the patches each do just one well-defined thing. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
da89fb165e
commit
4ae73f2d53
|
@ -93,6 +93,7 @@ config X86
|
||||||
select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
|
select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
|
||||||
select GENERIC_TIME_VSYSCALL if X86_64
|
select GENERIC_TIME_VSYSCALL if X86_64
|
||||||
select KTIME_SCALAR if X86_32
|
select KTIME_SCALAR if X86_32
|
||||||
|
select GENERIC_STRNCPY_FROM_USER
|
||||||
|
|
||||||
config INSTRUCTION_DECODER
|
config INSTRUCTION_DECODER
|
||||||
def_bool (KPROBES || PERF_EVENTS || UPROBES)
|
def_bool (KPROBES || PERF_EVENTS || UPROBES)
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
#define segment_eq(a, b) ((a).seg == (b).seg)
|
#define segment_eq(a, b) ((a).seg == (b).seg)
|
||||||
|
|
||||||
|
#define user_addr_max() (current_thread_info()->addr_limit.seg)
|
||||||
#define __addr_ok(addr) \
|
#define __addr_ok(addr) \
|
||||||
((unsigned long __force)(addr) < \
|
((unsigned long __force)(addr) < \
|
||||||
(current_thread_info()->addr_limit.seg))
|
(current_thread_info()->addr_limit.seg))
|
||||||
|
|
|
@ -43,100 +43,3 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(copy_from_user_nmi);
|
EXPORT_SYMBOL_GPL(copy_from_user_nmi);
|
||||||
|
|
||||||
/*
|
|
||||||
* Do a strncpy, return length of string without final '\0'.
|
|
||||||
* 'count' is the user-supplied count (return 'count' if we
|
|
||||||
* hit it), 'max' is the address space maximum (and we return
|
|
||||||
* -EFAULT if we hit it).
|
|
||||||
*/
|
|
||||||
static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
|
|
||||||
{
|
|
||||||
long res = 0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Truncate 'max' to the user-specified limit, so that
|
|
||||||
* we only have one limit we need to check in the loop
|
|
||||||
*/
|
|
||||||
if (max > count)
|
|
||||||
max = count;
|
|
||||||
|
|
||||||
while (max >= sizeof(unsigned long)) {
|
|
||||||
unsigned long c, mask;
|
|
||||||
|
|
||||||
/* Fall back to byte-at-a-time if we get a page fault */
|
|
||||||
if (unlikely(__get_user(c,(unsigned long __user *)(src+res))))
|
|
||||||
break;
|
|
||||||
mask = has_zero(c);
|
|
||||||
if (mask) {
|
|
||||||
mask = (mask - 1) & ~mask;
|
|
||||||
mask >>= 7;
|
|
||||||
*(unsigned long *)(dst+res) = c & mask;
|
|
||||||
return res + count_masked_bytes(mask);
|
|
||||||
}
|
|
||||||
*(unsigned long *)(dst+res) = c;
|
|
||||||
res += sizeof(unsigned long);
|
|
||||||
max -= sizeof(unsigned long);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (max) {
|
|
||||||
char c;
|
|
||||||
|
|
||||||
if (unlikely(__get_user(c,src+res)))
|
|
||||||
return -EFAULT;
|
|
||||||
dst[res] = c;
|
|
||||||
if (!c)
|
|
||||||
return res;
|
|
||||||
res++;
|
|
||||||
max--;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Uhhuh. We hit 'max'. But was that the user-specified maximum
|
|
||||||
* too? If so, that's ok - we got as much as the user asked for.
|
|
||||||
*/
|
|
||||||
if (res >= count)
|
|
||||||
return res;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Nope: we hit the address space limit, and we still had more
|
|
||||||
* characters the caller would have wanted. That's an EFAULT.
|
|
||||||
*/
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* strncpy_from_user: - Copy a NUL terminated string from userspace.
|
|
||||||
* @dst: Destination address, in kernel space. This buffer must be at
|
|
||||||
* least @count bytes long.
|
|
||||||
* @src: Source address, in user space.
|
|
||||||
* @count: Maximum number of bytes to copy, including the trailing NUL.
|
|
||||||
*
|
|
||||||
* Copies a NUL-terminated string from userspace to kernel space.
|
|
||||||
*
|
|
||||||
* On success, returns the length of the string (not including the trailing
|
|
||||||
* NUL).
|
|
||||||
*
|
|
||||||
* If access to userspace fails, returns -EFAULT (some data may have been
|
|
||||||
* copied).
|
|
||||||
*
|
|
||||||
* If @count is smaller than the length of the string, copies @count bytes
|
|
||||||
* and returns @count.
|
|
||||||
*/
|
|
||||||
long
|
|
||||||
strncpy_from_user(char *dst, const char __user *src, long count)
|
|
||||||
{
|
|
||||||
unsigned long max_addr, src_addr;
|
|
||||||
|
|
||||||
if (unlikely(count <= 0))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
max_addr = current_thread_info()->addr_limit.seg;
|
|
||||||
src_addr = (unsigned long)src;
|
|
||||||
if (likely(src_addr < max_addr)) {
|
|
||||||
unsigned long max = max_addr - src_addr;
|
|
||||||
return do_strncpy_from_user(dst, src, count, max);
|
|
||||||
}
|
|
||||||
return -EFAULT;
|
|
||||||
}
|
|
||||||
EXPORT_SYMBOL(strncpy_from_user);
|
|
||||||
|
|
Loading…
Reference in New Issue