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