blob: d3312f0fcdfc646f3f86ce1e8edc2878e1c8199d [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_UACCESS_H
2#define _ASM_X86_UACCESS_H
Glauber Costaca233862008-06-13 14:39:25 -03003/*
4 * User space memory access functions
5 */
6#include <linux/errno.h>
7#include <linux/compiler.h>
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -07008#include <linux/kasan-checks.h>
Glauber Costaca233862008-06-13 14:39:25 -03009#include <linux/thread_info.h>
Glauber Costaca233862008-06-13 14:39:25 -030010#include <linux/string.h>
11#include <asm/asm.h>
12#include <asm/page.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070013#include <asm/smap.h>
Glauber Costaca233862008-06-13 14:39:25 -030014
15#define VERIFY_READ 0
16#define VERIFY_WRITE 1
17
18/*
19 * The fs value determines whether argument validity checking should be
20 * performed or not. If get_fs() == USER_DS, checking is performed, with
21 * get_fs() == KERNEL_DS, checking is bypassed.
22 *
23 * For historical reasons, these macros are grossly misnamed.
24 */
25
26#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
27
28#define KERNEL_DS MAKE_MM_SEG(-1UL)
Linus Torvalds9063c612009-06-20 15:40:00 -070029#define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
Glauber Costaca233862008-06-13 14:39:25 -030030
31#define get_ds() (KERNEL_DS)
32#define get_fs() (current_thread_info()->addr_limit)
33#define set_fs(x) (current_thread_info()->addr_limit = (x))
34
35#define segment_eq(a, b) ((a).seg == (b).seg)
36
Linus Torvalds4ae73f22012-05-26 10:14:39 -070037#define user_addr_max() (current_thread_info()->addr_limit.seg)
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070038#define __addr_ok(addr) \
39 ((unsigned long __force)(addr) < user_addr_max())
Glauber Costa002ca162008-06-25 11:08:51 -030040
Glauber Costaca233862008-06-13 14:39:25 -030041/*
42 * Test whether a block of memory is a valid user space address.
43 * Returns 0 if the range is valid, nonzero otherwise.
Glauber Costaca233862008-06-13 14:39:25 -030044 */
H. Peter Anvina7405762013-12-27 16:52:47 -080045static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080046{
47 /*
48 * If we have used "sizeof()" for the size,
49 * we know it won't overflow the limit (but
50 * it might overflow the 'addr', so it's
51 * important to subtract the size from the
52 * limit, not add it to the address).
53 */
54 if (__builtin_constant_p(size))
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070055 return unlikely(addr > limit - size);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080056
57 /* Arbitrary sizes? Be careful about overflow */
58 addr += size;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070059 if (unlikely(addr < size))
H. Peter Anvina7405762013-12-27 16:52:47 -080060 return true;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070061 return unlikely(addr > limit);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080062}
Glauber Costaca233862008-06-13 14:39:25 -030063
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070064#define __range_not_ok(addr, size, limit) \
Glauber Costaca233862008-06-13 14:39:25 -030065({ \
Glauber Costaca233862008-06-13 14:39:25 -030066 __chk_user_ptr(addr); \
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080067 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
Glauber Costaca233862008-06-13 14:39:25 -030068})
69
70/**
71 * access_ok: - Checks if a user space pointer is valid
72 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
73 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
74 * to write to a block, it is always safe to read from it.
75 * @addr: User space pointer to start of block to check
76 * @size: Size of block to check
77 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +020078 * Context: User context only. This function may sleep if pagefaults are
79 * enabled.
Glauber Costaca233862008-06-13 14:39:25 -030080 *
81 * Checks if a pointer to a block of memory in user space is valid.
82 *
83 * Returns true (nonzero) if the memory block may be valid, false (zero)
84 * if it is definitely invalid.
85 *
86 * Note that, depending on architecture, this function probably just
87 * checks that the pointer is in the user space range - after calling
88 * this function, memory access functions may still return -EFAULT.
89 */
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070090#define access_ok(type, addr, size) \
H. Peter Anvina7405762013-12-27 16:52:47 -080091 likely(!__range_not_ok(addr, size, user_addr_max()))
Glauber Costaca233862008-06-13 14:39:25 -030092
93/*
Tony Luck548acf12016-02-17 10:20:12 -080094 * The exception table consists of triples of addresses relative to the
95 * exception table entry itself. The first address is of an instruction
96 * that is allowed to fault, the second is the target at which the program
97 * should continue. The third is a handler function to deal with the fault
98 * caused by the instruction in the first field.
Glauber Costaca233862008-06-13 14:39:25 -030099 *
100 * All the routines below use bits of fixup code that are out of line
101 * with the main instruction path. This means when everything is well,
102 * we don't even have to jump over them. Further, they do not intrude
103 * on our cache or tlb entries.
104 */
105
106struct exception_table_entry {
Tony Luck548acf12016-02-17 10:20:12 -0800107 int insn, fixup, handler;
Glauber Costaca233862008-06-13 14:39:25 -0300108};
Ard Biesheuvel29934b02016-03-22 14:28:17 -0700109
110#define ARCH_HAS_RELATIVE_EXTABLE
Glauber Costaca233862008-06-13 14:39:25 -0300111
Mathias Krause50c73892016-05-10 23:07:02 +0200112#define swap_ex_entry_fixup(a, b, tmp, delta) \
113 do { \
114 (a)->fixup = (b)->fixup + (delta); \
115 (b)->fixup = (tmp).fixup - (delta); \
116 (a)->handler = (b)->handler + (delta); \
117 (b)->handler = (tmp).handler - (delta); \
118 } while (0)
119
Tony Luck548acf12016-02-17 10:20:12 -0800120extern int fixup_exception(struct pt_regs *regs, int trapnr);
121extern bool ex_has_fault_handler(unsigned long ip);
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700122extern void early_fixup_exception(struct pt_regs *regs, int trapnr);
Glauber Costaca233862008-06-13 14:39:25 -0300123
124/*
125 * These are the main single-value transfer routines. They automatically
126 * use the right size if we just have the right pointer type.
127 *
128 * This gets kind of ugly. We want to return _two_ values in "get_user()"
129 * and yet we don't want to do any pointers, because that is too much
130 * of a performance impact. Thus we have a few rather ugly macros here,
131 * and hide all the ugliness from the user.
132 *
133 * The "__xxx" versions of the user access functions are versions that
134 * do not verify the address space, that must have been done previously
135 * with a separate "access_ok()" call (this is used when we do multiple
136 * accesses to the same area of user memory).
137 */
138
139extern int __get_user_1(void);
140extern int __get_user_2(void);
141extern int __get_user_4(void);
142extern int __get_user_8(void);
143extern int __get_user_bad(void);
144
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800145#define __uaccess_begin() stac()
146#define __uaccess_end() clac()
147
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800148/*
149 * This is a type: either unsigned long, if the argument fits into
150 * that type, or otherwise unsigned long long.
151 */
152#define __inttype(x) \
153__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
Glauber Costa865e5b72008-06-25 11:05:11 -0300154
155/**
156 * get_user: - Get a simple variable from user space.
157 * @x: Variable to store result.
158 * @ptr: Source address, in user space.
159 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200160 * Context: User context only. This function may sleep if pagefaults are
161 * enabled.
Glauber Costa865e5b72008-06-25 11:05:11 -0300162 *
163 * This macro copies a single simple variable from user space to kernel
164 * space. It supports simple types like char and int, but not larger
165 * data types like structures or arrays.
166 *
167 * @ptr must have pointer-to-simple-variable type, and the result of
168 * dereferencing @ptr must be assignable to @x without a cast.
169 *
170 * Returns zero on success, or -EFAULT on error.
171 * On error, the variable @x is set to zero.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800172 */
173/*
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800174 * Careful: we have to cast the result to the type of the pointer
175 * for sign reasons.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800176 *
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700177 * The use of _ASM_DX as the register specifier is a bit of a
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800178 * simplification, as gcc only cares about it as the starting point
179 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
180 * (%ecx being the next register in gcc's x86 register sequence), and
181 * %rdx on 64 bits.
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700182 *
183 * Clang/LLVM cares about the size of the register, but still wants
184 * the base register for something that ends up being a pair.
Glauber Costa865e5b72008-06-25 11:05:11 -0300185 */
Glauber Costa865e5b72008-06-25 11:05:11 -0300186#define get_user(x, ptr) \
187({ \
188 int __ret_gu; \
Jan-Simon Möllerbdfc0172013-08-29 21:13:05 +0200189 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600190 register void *__sp asm(_ASM_SP); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300191 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100192 might_fault(); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600193 asm volatile("call __get_user_%P4" \
194 : "=a" (__ret_gu), "=r" (__val_gu), "+r" (__sp) \
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800195 : "0" (ptr), "i" (sizeof(*(ptr)))); \
Michael S. Tsirkine182c572014-12-12 01:56:04 +0200196 (x) = (__force __typeof__(*(ptr))) __val_gu; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700197 __builtin_expect(__ret_gu, 0); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300198})
199
Glauber Costae30a44f2008-06-25 13:17:43 -0300200#define __put_user_x(size, x, ptr, __ret_pu) \
201 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800202 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costae30a44f2008-06-25 13:17:43 -0300203
204
205
Glauber Costadc70ddf2008-06-25 11:48:29 -0300206#ifdef CONFIG_X86_32
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800207#define __put_user_asm_u64(x, addr, err, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800208 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700209 "1: movl %%eax,0(%2)\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300210 "2: movl %%edx,4(%2)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800211 "3:" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300212 ".section .fixup,\"ax\"\n" \
213 "4: movl %3,%0\n" \
214 " jmp 3b\n" \
215 ".previous\n" \
216 _ASM_EXTABLE(1b, 4b) \
217 _ASM_EXTABLE(2b, 4b) \
218 : "=r" (err) \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800219 : "A" (x), "r" (addr), "i" (errret), "0" (err))
Glauber Costae30a44f2008-06-25 13:17:43 -0300220
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800221#define __put_user_asm_ex_u64(x, addr) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800222 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700223 "1: movl %%eax,0(%1)\n" \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800224 "2: movl %%edx,4(%1)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800225 "3:" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700226 _ASM_EXTABLE_EX(1b, 2b) \
227 _ASM_EXTABLE_EX(2b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800228 : : "A" (x), "r" (addr))
229
Glauber Costae30a44f2008-06-25 13:17:43 -0300230#define __put_user_x8(x, ptr, __ret_pu) \
231 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
232 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costadc70ddf2008-06-25 11:48:29 -0300233#else
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800234#define __put_user_asm_u64(x, ptr, retval, errret) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700235 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800236#define __put_user_asm_ex_u64(x, addr) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700237 __put_user_asm_ex(x, addr, "q", "", "er")
Glauber Costae30a44f2008-06-25 13:17:43 -0300238#define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
Glauber Costadc70ddf2008-06-25 11:48:29 -0300239#endif
240
Glauber Costae30a44f2008-06-25 13:17:43 -0300241extern void __put_user_bad(void);
242
243/*
244 * Strange magic calling convention: pointer in %ecx,
245 * value in %eax(:%edx), return value in %eax. clobbers %rbx
246 */
247extern void __put_user_1(void);
248extern void __put_user_2(void);
249extern void __put_user_4(void);
250extern void __put_user_8(void);
251
Glauber Costae30a44f2008-06-25 13:17:43 -0300252/**
253 * put_user: - Write a simple value into user space.
254 * @x: Value to copy to user space.
255 * @ptr: Destination address, in user space.
256 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200257 * Context: User context only. This function may sleep if pagefaults are
258 * enabled.
Glauber Costae30a44f2008-06-25 13:17:43 -0300259 *
260 * This macro copies a single simple value from kernel space to user
261 * space. It supports simple types like char and int, but not larger
262 * data types like structures or arrays.
263 *
264 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
265 * to the result of dereferencing @ptr.
266 *
267 * Returns zero on success, or -EFAULT on error.
268 */
269#define put_user(x, ptr) \
270({ \
271 int __ret_pu; \
272 __typeof__(*(ptr)) __pu_val; \
273 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100274 might_fault(); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300275 __pu_val = x; \
276 switch (sizeof(*(ptr))) { \
277 case 1: \
278 __put_user_x(1, __pu_val, ptr, __ret_pu); \
279 break; \
280 case 2: \
281 __put_user_x(2, __pu_val, ptr, __ret_pu); \
282 break; \
283 case 4: \
284 __put_user_x(4, __pu_val, ptr, __ret_pu); \
285 break; \
286 case 8: \
287 __put_user_x8(__pu_val, ptr, __ret_pu); \
288 break; \
289 default: \
290 __put_user_x(X, __pu_val, ptr, __ret_pu); \
291 break; \
292 } \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700293 __builtin_expect(__ret_pu, 0); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300294})
295
Glauber Costadc70ddf2008-06-25 11:48:29 -0300296#define __put_user_size(x, ptr, size, retval, errret) \
297do { \
298 retval = 0; \
299 __chk_user_ptr(ptr); \
300 switch (size) { \
301 case 1: \
302 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
303 break; \
304 case 2: \
305 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
306 break; \
307 case 4: \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800308 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300309 break; \
310 case 8: \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800311 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
312 errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300313 break; \
314 default: \
315 __put_user_bad(); \
316 } \
317} while (0)
318
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800319/*
320 * This doesn't do __uaccess_begin/end - the exception handling
321 * around it must do that.
322 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800323#define __put_user_size_ex(x, ptr, size) \
324do { \
325 __chk_user_ptr(ptr); \
326 switch (size) { \
327 case 1: \
328 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
329 break; \
330 case 2: \
331 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
332 break; \
333 case 4: \
334 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
335 break; \
336 case 8: \
337 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
338 break; \
339 default: \
340 __put_user_bad(); \
341 } \
342} while (0)
343
Glauber Costa3f168222008-06-25 12:48:47 -0300344#ifdef CONFIG_X86_32
345#define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800346#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300347#else
348#define __get_user_asm_u64(x, ptr, retval, errret) \
349 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800350#define __get_user_asm_ex_u64(x, ptr) \
351 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300352#endif
353
354#define __get_user_size(x, ptr, size, retval, errret) \
355do { \
356 retval = 0; \
357 __chk_user_ptr(ptr); \
358 switch (size) { \
359 case 1: \
360 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
361 break; \
362 case 2: \
363 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
364 break; \
365 case 4: \
366 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
367 break; \
368 case 8: \
369 __get_user_asm_u64(x, ptr, retval, errret); \
370 break; \
371 default: \
372 (x) = __get_user_bad(); \
373 } \
374} while (0)
375
376#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800377 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700378 "1: mov"itype" %2,%"rtype"1\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800379 "2:\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300380 ".section .fixup,\"ax\"\n" \
381 "3: mov %3,%0\n" \
382 " xor"itype" %"rtype"1,%"rtype"1\n" \
383 " jmp 2b\n" \
384 ".previous\n" \
385 _ASM_EXTABLE(1b, 3b) \
386 : "=r" (err), ltype(x) \
387 : "m" (__m(addr)), "i" (errret), "0" (err))
388
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800389/*
390 * This doesn't do __uaccess_begin/end - the exception handling
391 * around it must do that.
392 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800393#define __get_user_size_ex(x, ptr, size) \
394do { \
395 __chk_user_ptr(ptr); \
396 switch (size) { \
397 case 1: \
398 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
399 break; \
400 case 2: \
401 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
402 break; \
403 case 4: \
404 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
405 break; \
406 case 8: \
407 __get_user_asm_ex_u64(x, ptr); \
408 break; \
409 default: \
410 (x) = __get_user_bad(); \
411 } \
412} while (0)
413
414#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700415 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
416 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700417 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800418 : ltype(x) : "m" (__m(addr)))
419
Glauber Costadc70ddf2008-06-25 11:48:29 -0300420#define __put_user_nocheck(x, ptr, size) \
421({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800422 int __pu_err; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800423 __uaccess_begin(); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300424 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800425 __uaccess_end(); \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700426 __builtin_expect(__pu_err, 0); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300427})
428
Glauber Costa3f168222008-06-25 12:48:47 -0300429#define __get_user_nocheck(x, ptr, size) \
430({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800431 int __gu_err; \
Glauber Costa3f168222008-06-25 12:48:47 -0300432 unsigned long __gu_val; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800433 __uaccess_begin(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300434 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800435 __uaccess_end(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300436 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700437 __builtin_expect(__gu_err, 0); \
Glauber Costa3f168222008-06-25 12:48:47 -0300438})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300439
440/* FIXME: this hack is definitely wrong -AK */
441struct __large_struct { unsigned long buf[100]; };
442#define __m(x) (*(struct __large_struct __user *)(x))
443
444/*
445 * Tell gcc we read from memory instead of writing: this is because
446 * we do not write to any memory gcc knows about, so there are no
447 * aliasing issues.
448 */
449#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800450 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700451 "1: mov"itype" %"rtype"1,%2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800452 "2:\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300453 ".section .fixup,\"ax\"\n" \
454 "3: mov %3,%0\n" \
455 " jmp 2b\n" \
456 ".previous\n" \
457 _ASM_EXTABLE(1b, 3b) \
458 : "=r"(err) \
459 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800460
461#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700462 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
463 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700464 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800465 : : ltype(x), "m" (__m(addr)))
466
467/*
468 * uaccess_try and catch
469 */
470#define uaccess_try do { \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800471 current_thread_info()->uaccess_err = 0; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800472 __uaccess_begin(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800473 barrier();
474
475#define uaccess_catch(err) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800476 __uaccess_end(); \
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800477 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800478} while (0)
479
Glauber Costa8cb834e2008-06-25 14:43:30 -0300480/**
481 * __get_user: - Get a simple variable from user space, with less checking.
482 * @x: Variable to store result.
483 * @ptr: Source address, in user space.
484 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200485 * Context: User context only. This function may sleep if pagefaults are
486 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300487 *
488 * This macro copies a single simple variable from user space to kernel
489 * space. It supports simple types like char and int, but not larger
490 * data types like structures or arrays.
491 *
492 * @ptr must have pointer-to-simple-variable type, and the result of
493 * dereferencing @ptr must be assignable to @x without a cast.
494 *
495 * Caller must check the pointer with access_ok() before calling this
496 * function.
497 *
498 * Returns zero on success, or -EFAULT on error.
499 * On error, the variable @x is set to zero.
500 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300501
Glauber Costa8cb834e2008-06-25 14:43:30 -0300502#define __get_user(x, ptr) \
503 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800504
Glauber Costa8cb834e2008-06-25 14:43:30 -0300505/**
506 * __put_user: - Write a simple value into user space, with less checking.
507 * @x: Value to copy to user space.
508 * @ptr: Destination address, in user space.
509 *
David Hildenbrandb3c395ef2015-05-11 17:52:08 +0200510 * Context: User context only. This function may sleep if pagefaults are
511 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300512 *
513 * This macro copies a single simple value from kernel space to user
514 * space. It supports simple types like char and int, but not larger
515 * data types like structures or arrays.
516 *
517 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
518 * to the result of dereferencing @ptr.
519 *
520 * Caller must check the pointer with access_ok() before calling this
521 * function.
522 *
523 * Returns zero on success, or -EFAULT on error.
524 */
525
526#define __put_user(x, ptr) \
527 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
528
529#define __get_user_unaligned __get_user
530#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300531
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300532/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800533 * {get|put}_user_try and catch
534 *
535 * get_user_try {
536 * get_user_ex(...);
537 * } get_user_catch(err)
538 */
539#define get_user_try uaccess_try
540#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800541
542#define get_user_ex(x, ptr) do { \
543 unsigned long __gue_val; \
544 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
545 (x) = (__force __typeof__(*(ptr)))__gue_val; \
546} while (0)
547
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800548#define put_user_try uaccess_try
549#define put_user_catch(err) uaccess_catch(err)
550
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800551#define put_user_ex(x, ptr) \
552 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
553
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200554extern unsigned long
555copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700556extern __must_check long
557strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200558
Linus Torvalds5723aa92012-05-26 11:09:53 -0700559extern __must_check long strlen_user(const char __user *str);
560extern __must_check long strnlen_user(const char __user *str, long n);
561
H. Peter Anvina0528582012-09-21 12:43:11 -0700562unsigned long __must_check clear_user(void __user *mem, unsigned long len);
563unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
564
Qiaowei Renf09174c2013-12-14 14:25:02 +0800565extern void __cmpxchg_wrong_size(void)
566 __compiletime_error("Bad argument size for cmpxchg");
567
568#define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
569({ \
570 int __ret = 0; \
571 __typeof__(ptr) __uval = (uval); \
572 __typeof__(*(ptr)) __old = (old); \
573 __typeof__(*(ptr)) __new = (new); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800574 __uaccess_begin(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800575 switch (size) { \
576 case 1: \
577 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800578 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800579 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800580 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800581 "\t.section .fixup, \"ax\"\n" \
582 "3:\tmov %3, %0\n" \
583 "\tjmp 2b\n" \
584 "\t.previous\n" \
585 _ASM_EXTABLE(1b, 3b) \
586 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
587 : "i" (-EFAULT), "q" (__new), "1" (__old) \
588 : "memory" \
589 ); \
590 break; \
591 } \
592 case 2: \
593 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800594 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800595 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800596 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800597 "\t.section .fixup, \"ax\"\n" \
598 "3:\tmov %3, %0\n" \
599 "\tjmp 2b\n" \
600 "\t.previous\n" \
601 _ASM_EXTABLE(1b, 3b) \
602 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
603 : "i" (-EFAULT), "r" (__new), "1" (__old) \
604 : "memory" \
605 ); \
606 break; \
607 } \
608 case 4: \
609 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800610 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800611 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800612 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800613 "\t.section .fixup, \"ax\"\n" \
614 "3:\tmov %3, %0\n" \
615 "\tjmp 2b\n" \
616 "\t.previous\n" \
617 _ASM_EXTABLE(1b, 3b) \
618 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
619 : "i" (-EFAULT), "r" (__new), "1" (__old) \
620 : "memory" \
621 ); \
622 break; \
623 } \
624 case 8: \
625 { \
626 if (!IS_ENABLED(CONFIG_X86_64)) \
627 __cmpxchg_wrong_size(); \
628 \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800629 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800630 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800631 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800632 "\t.section .fixup, \"ax\"\n" \
633 "3:\tmov %3, %0\n" \
634 "\tjmp 2b\n" \
635 "\t.previous\n" \
636 _ASM_EXTABLE(1b, 3b) \
637 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
638 : "i" (-EFAULT), "r" (__new), "1" (__old) \
639 : "memory" \
640 ); \
641 break; \
642 } \
643 default: \
644 __cmpxchg_wrong_size(); \
645 } \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800646 __uaccess_end(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800647 *__uval = __old; \
648 __ret; \
649})
650
651#define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
652({ \
653 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
654 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
655 (old), (new), sizeof(*(ptr))) : \
656 -EFAULT; \
657})
658
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800659/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300660 * movsl can be slow when source and dest are not both 8-byte aligned
661 */
662#ifdef CONFIG_X86_INTEL_USERCOPY
663extern struct movsl_mask {
664 int mask;
665} ____cacheline_aligned_in_smp movsl_mask;
666#endif
667
Glauber Costa22cac162008-06-25 14:56:53 -0300668#define ARCH_HAS_NOCACHE_UACCESS 1
669
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200670#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100671# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200672#else
David Howellsa1ce3922012-10-02 18:01:25 +0100673# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200674#endif
Glauber Costaca233862008-06-13 14:39:25 -0300675
Jan Beulich3df7b412013-10-21 09:43:57 +0100676unsigned long __must_check _copy_from_user(void *to, const void __user *from,
677 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100678unsigned long __must_check _copy_to_user(void __user *to, const void *from,
679 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100680
681#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
682# define copy_user_diag __compiletime_error
683#else
684# define copy_user_diag __compiletime_warning
685#endif
686
687extern void copy_user_diag("copy_from_user() buffer size is too small")
688copy_from_user_overflow(void);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100689extern void copy_user_diag("copy_to_user() buffer size is too small")
690copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
Jan Beulich3df7b412013-10-21 09:43:57 +0100691
692#undef copy_user_diag
693
694#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
695
696extern void
697__compiletime_warning("copy_from_user() buffer size is not provably correct")
698__copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
699#define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
700
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100701extern void
702__compiletime_warning("copy_to_user() buffer size is not provably correct")
703__copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
704#define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
705
Jan Beulich3df7b412013-10-21 09:43:57 +0100706#else
707
708static inline void
709__copy_from_user_overflow(int size, unsigned long count)
710{
711 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
712}
713
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100714#define __copy_to_user_overflow __copy_from_user_overflow
715
Jan Beulich3df7b412013-10-21 09:43:57 +0100716#endif
717
718static inline unsigned long __must_check
719copy_from_user(void *to, const void __user *from, unsigned long n)
720{
721 int sz = __compiletime_object_size(to);
722
723 might_fault();
724
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700725 kasan_check_write(to, n);
726
Jan Beulich3df7b412013-10-21 09:43:57 +0100727 /*
728 * While we would like to have the compiler do the checking for us
729 * even in the non-constant size case, any false positives there are
730 * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
731 * without - the [hopefully] dangerous looking nature of the warning
732 * would make people go look at the respecitive call sites over and
733 * over again just to find that there's no problem).
734 *
735 * And there are cases where it's just not realistic for the compiler
736 * to prove the count to be in range. For example when multiple call
737 * sites of a helper function - perhaps in different source files -
738 * all doing proper range checking, yet the helper function not doing
739 * so again.
740 *
741 * Therefore limit the compile time checking to the constant size
742 * case, and do only runtime checking for non-constant sizes.
743 */
744
Kees Cook5b710f32016-06-23 15:04:01 -0700745 if (likely(sz < 0 || sz >= n)) {
746 check_object_size(to, n, false);
Jan Beulich3df7b412013-10-21 09:43:57 +0100747 n = _copy_from_user(to, from, n);
Kees Cook5b710f32016-06-23 15:04:01 -0700748 } else if (__builtin_constant_p(n))
Jan Beulich3df7b412013-10-21 09:43:57 +0100749 copy_from_user_overflow();
750 else
751 __copy_from_user_overflow(sz, n);
752
753 return n;
754}
755
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100756static inline unsigned long __must_check
757copy_to_user(void __user *to, const void *from, unsigned long n)
758{
759 int sz = __compiletime_object_size(from);
760
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700761 kasan_check_read(from, n);
762
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100763 might_fault();
764
765 /* See the comment in copy_from_user() above. */
Kees Cook5b710f32016-06-23 15:04:01 -0700766 if (likely(sz < 0 || sz >= n)) {
767 check_object_size(from, n, true);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100768 n = _copy_to_user(to, from, n);
Kees Cook5b710f32016-06-23 15:04:01 -0700769 } else if (__builtin_constant_p(n))
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100770 copy_to_user_overflow();
771 else
772 __copy_to_user_overflow(sz, n);
773
774 return n;
775}
776
Jan Beulich3df7b412013-10-21 09:43:57 +0100777#undef __copy_from_user_overflow
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100778#undef __copy_to_user_overflow
Jan Beulich3df7b412013-10-21 09:43:57 +0100779
Andi Kleen10013eb2015-10-22 15:07:20 -0700780/*
781 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
782 * nested NMI paths are careful to preserve CR2.
783 *
784 * Caller must use pagefault_enable/disable, or run in interrupt context,
785 * and also do a uaccess_ok() check
786 */
787#define __copy_from_user_nmi __copy_from_user_inatomic
788
Linus Torvalds404a4742016-01-21 13:02:41 -0800789/*
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800790 * The "unsafe" user accesses aren't really "unsafe", but the naming
791 * is a big fat warning: you have to not only do the access_ok()
792 * checking before using them, but you have to surround them with the
793 * user_access_begin/end() pair.
794 */
795#define user_access_begin() __uaccess_begin()
796#define user_access_end() __uaccess_end()
797
798#define unsafe_put_user(x, ptr) \
799({ \
800 int __pu_err; \
801 __put_user_size((x), (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
802 __builtin_expect(__pu_err, 0); \
803})
804
805#define unsafe_get_user(x, ptr) \
806({ \
807 int __gu_err; \
808 unsigned long __gu_val; \
809 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
810 (x) = (__force __typeof__(*(ptr)))__gu_val; \
811 __builtin_expect(__gu_err, 0); \
812})
813
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700814#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700815