David S. Miller | fb34035 | 2009-12-10 23:05:23 -0800 | [diff] [blame] | 1 | #include <linux/module.h> |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 2 | #include <linux/uaccess.h> |
| 3 | #include <linux/errno.h> |
David S. Miller | fb34035 | 2009-12-10 23:05:23 -0800 | [diff] [blame] | 4 | #include <linux/bug.h> |
| 5 | |
| 6 | void copy_from_user_overflow(void) |
| 7 | { |
| 8 | WARN(1, "Buffer overflow detected!\n"); |
| 9 | } |
| 10 | EXPORT_SYMBOL(copy_from_user_overflow); |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 11 | |
| 12 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) |
| 13 | |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 14 | static inline long find_zero(unsigned long mask) |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 15 | { |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 16 | long byte = 0; |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 17 | #ifdef CONFIG_64BIT |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 18 | if (mask >> 32) |
| 19 | mask >>= 32; |
| 20 | else |
| 21 | byte = 4; |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 22 | #endif |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 23 | if (mask >> 16) |
| 24 | mask >>= 16; |
| 25 | else |
| 26 | byte += 2; |
| 27 | return (mask >> 8) ? byte : byte + 1; |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Do a strncpy, return length of string without final '\0'. |
| 32 | * 'count' is the user-supplied count (return 'count' if we |
| 33 | * hit it), 'max' is the address space maximum (and we return |
| 34 | * -EFAULT if we hit it). |
| 35 | */ |
| 36 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) |
| 37 | { |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 38 | const unsigned long high_bits = REPEAT_BYTE(0xfe) + 1; |
| 39 | const unsigned long low_bits = REPEAT_BYTE(0x7f); |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 40 | long res = 0; |
| 41 | |
| 42 | /* |
| 43 | * Truncate 'max' to the user-specified limit, so that |
| 44 | * we only have one limit we need to check in the loop |
| 45 | */ |
| 46 | if (max > count) |
| 47 | max = count; |
| 48 | |
| 49 | if (((long) dst | (long) src) & (sizeof(long) - 1)) |
| 50 | goto byte_at_a_time; |
| 51 | |
| 52 | while (max >= sizeof(unsigned long)) { |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 53 | unsigned long c, v, rhs; |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 54 | |
| 55 | /* Fall back to byte-at-a-time if we get a page fault */ |
| 56 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) |
| 57 | break; |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 58 | rhs = c | low_bits; |
| 59 | v = (c + high_bits) & ~rhs; |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 60 | *(unsigned long *)(dst+res) = c; |
David S. Miller | 4efcac3 | 2012-05-23 19:20:20 -0700 | [diff] [blame^] | 61 | if (v) { |
| 62 | v = (c & low_bits) + low_bits;; |
| 63 | v = ~(v | rhs); |
| 64 | return res + find_zero(v); |
| 65 | } |
David S. Miller | ff06dff | 2012-05-22 17:53:19 -0700 | [diff] [blame] | 66 | res += sizeof(unsigned long); |
| 67 | max -= sizeof(unsigned long); |
| 68 | } |
| 69 | |
| 70 | byte_at_a_time: |
| 71 | while (max) { |
| 72 | char c; |
| 73 | |
| 74 | if (unlikely(__get_user(c,src+res))) |
| 75 | return -EFAULT; |
| 76 | dst[res] = c; |
| 77 | if (!c) |
| 78 | return res; |
| 79 | res++; |
| 80 | max--; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Uhhuh. We hit 'max'. But was that the user-specified maximum |
| 85 | * too? If so, that's ok - we got as much as the user asked for. |
| 86 | */ |
| 87 | if (res >= count) |
| 88 | return res; |
| 89 | |
| 90 | /* |
| 91 | * Nope: we hit the address space limit, and we still had more |
| 92 | * characters the caller would have wanted. That's an EFAULT. |
| 93 | */ |
| 94 | return -EFAULT; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * strncpy_from_user: - Copy a NUL terminated string from userspace. |
| 99 | * @dst: Destination address, in kernel space. This buffer must be at |
| 100 | * least @count bytes long. |
| 101 | * @src: Source address, in user space. |
| 102 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 103 | * |
| 104 | * Copies a NUL-terminated string from userspace to kernel space. |
| 105 | * |
| 106 | * On success, returns the length of the string (not including the trailing |
| 107 | * NUL). |
| 108 | * |
| 109 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 110 | * copied). |
| 111 | * |
| 112 | * If @count is smaller than the length of the string, copies @count bytes |
| 113 | * and returns @count. |
| 114 | */ |
| 115 | long strncpy_from_user(char *dst, const char __user *src, long count) |
| 116 | { |
| 117 | unsigned long max_addr, src_addr; |
| 118 | |
| 119 | if (unlikely(count <= 0)) |
| 120 | return 0; |
| 121 | |
| 122 | max_addr = ~0UL; |
| 123 | if (likely(segment_eq(get_fs(), USER_DS))) |
| 124 | max_addr = STACK_TOP; |
| 125 | src_addr = (unsigned long)src; |
| 126 | if (likely(src_addr < max_addr)) { |
| 127 | unsigned long max = max_addr - src_addr; |
| 128 | return do_strncpy_from_user(dst, src, count, max); |
| 129 | } |
| 130 | return -EFAULT; |
| 131 | } |
| 132 | EXPORT_SYMBOL(strncpy_from_user); |