blob: 87f96453403a1cfd6cc79a6ac6a8e2ec0f5e267f [file] [log] [blame]
David S. Millerfb340352009-12-10 23:05:23 -08001#include <linux/module.h>
David S. Millerff06dff2012-05-22 17:53:19 -07002#include <linux/uaccess.h>
3#include <linux/errno.h>
David S. Millerfb340352009-12-10 23:05:23 -08004#include <linux/bug.h>
5
6void copy_from_user_overflow(void)
7{
8 WARN(1, "Buffer overflow detected!\n");
9}
10EXPORT_SYMBOL(copy_from_user_overflow);
David S. Millerff06dff2012-05-22 17:53:19 -070011
12#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
13
David S. Miller4efcac32012-05-23 19:20:20 -070014static inline long find_zero(unsigned long mask)
David S. Millerff06dff2012-05-22 17:53:19 -070015{
David S. Miller4efcac32012-05-23 19:20:20 -070016 long byte = 0;
David S. Millerff06dff2012-05-22 17:53:19 -070017#ifdef CONFIG_64BIT
David S. Miller4efcac32012-05-23 19:20:20 -070018 if (mask >> 32)
19 mask >>= 32;
20 else
21 byte = 4;
David S. Millerff06dff2012-05-22 17:53:19 -070022#endif
David S. Miller4efcac32012-05-23 19:20:20 -070023 if (mask >> 16)
24 mask >>= 16;
25 else
26 byte += 2;
27 return (mask >> 8) ? byte : byte + 1;
David S. Millerff06dff2012-05-22 17:53:19 -070028}
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 */
36static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max)
37{
David S. Miller4efcac32012-05-23 19:20:20 -070038 const unsigned long high_bits = REPEAT_BYTE(0xfe) + 1;
39 const unsigned long low_bits = REPEAT_BYTE(0x7f);
David S. Millerff06dff2012-05-22 17:53:19 -070040 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. Miller4efcac32012-05-23 19:20:20 -070053 unsigned long c, v, rhs;
David S. Millerff06dff2012-05-22 17:53:19 -070054
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. Miller4efcac32012-05-23 19:20:20 -070058 rhs = c | low_bits;
59 v = (c + high_bits) & ~rhs;
David S. Millerff06dff2012-05-22 17:53:19 -070060 *(unsigned long *)(dst+res) = c;
David S. Miller4efcac32012-05-23 19:20:20 -070061 if (v) {
62 v = (c & low_bits) + low_bits;;
63 v = ~(v | rhs);
64 return res + find_zero(v);
65 }
David S. Millerff06dff2012-05-22 17:53:19 -070066 res += sizeof(unsigned long);
67 max -= sizeof(unsigned long);
68 }
69
70byte_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 */
115long 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}
132EXPORT_SYMBOL(strncpy_from_user);