blob: 76a12ab47e11d58908f5c379471ad9de2dfad69e [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>
Thomas Garnierdbee7462017-06-14 18:12:01 -070011#include <linux/sched.h>
Glauber Costaca233862008-06-13 14:39:25 -030012#include <asm/asm.h>
13#include <asm/page.h>
H. Peter Anvin63bcff22012-09-21 12:43:12 -070014#include <asm/smap.h>
Al Viro45caf472016-09-05 11:32:44 -040015#include <asm/extable.h>
Glauber Costaca233862008-06-13 14:39:25 -030016
17#define VERIFY_READ 0
18#define VERIFY_WRITE 1
19
20/*
21 * The fs value determines whether argument validity checking should be
22 * performed or not. If get_fs() == USER_DS, checking is performed, with
23 * get_fs() == KERNEL_DS, checking is bypassed.
24 *
25 * For historical reasons, these macros are grossly misnamed.
26 */
27
28#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
29
30#define KERNEL_DS MAKE_MM_SEG(-1UL)
Linus Torvalds9063c612009-06-20 15:40:00 -070031#define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
Glauber Costaca233862008-06-13 14:39:25 -030032
33#define get_ds() (KERNEL_DS)
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070034#define get_fs() (current->thread.addr_limit)
Thomas Garnierdbee7462017-06-14 18:12:01 -070035static inline void set_fs(mm_segment_t fs)
36{
37 current->thread.addr_limit = fs;
38 /* On user-mode return, check fs is correct */
39 set_thread_flag(TIF_FSCHECK);
40}
Glauber Costaca233862008-06-13 14:39:25 -030041
42#define segment_eq(a, b) ((a).seg == (b).seg)
43
Andy Lutomirski13d4ea02016-07-14 13:22:57 -070044#define user_addr_max() (current->thread.addr_limit.seg)
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070045#define __addr_ok(addr) \
46 ((unsigned long __force)(addr) < user_addr_max())
Glauber Costa002ca162008-06-25 11:08:51 -030047
Glauber Costaca233862008-06-13 14:39:25 -030048/*
49 * Test whether a block of memory is a valid user space address.
50 * Returns 0 if the range is valid, nonzero otherwise.
Glauber Costaca233862008-06-13 14:39:25 -030051 */
H. Peter Anvina7405762013-12-27 16:52:47 -080052static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080053{
54 /*
55 * If we have used "sizeof()" for the size,
56 * we know it won't overflow the limit (but
57 * it might overflow the 'addr', so it's
58 * important to subtract the size from the
59 * limit, not add it to the address).
60 */
61 if (__builtin_constant_p(size))
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070062 return unlikely(addr > limit - size);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080063
64 /* Arbitrary sizes? Be careful about overflow */
65 addr += size;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070066 if (unlikely(addr < size))
H. Peter Anvina7405762013-12-27 16:52:47 -080067 return true;
Andy Lutomirski7e0f51c2015-10-05 17:47:50 -070068 return unlikely(addr > limit);
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080069}
Glauber Costaca233862008-06-13 14:39:25 -030070
Arun Sharmabc6ca7b2012-04-20 15:41:35 -070071#define __range_not_ok(addr, size, limit) \
Glauber Costaca233862008-06-13 14:39:25 -030072({ \
Glauber Costaca233862008-06-13 14:39:25 -030073 __chk_user_ptr(addr); \
Linus Torvaldsc5fe5d82013-12-27 15:30:58 -080074 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
Glauber Costaca233862008-06-13 14:39:25 -030075})
76
Peter Zijlstra2715f682016-11-22 10:57:15 +010077#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
78# define WARN_ON_IN_IRQ() WARN_ON_ONCE(!in_task())
79#else
80# define WARN_ON_IN_IRQ()
81#endif
82
Glauber Costaca233862008-06-13 14:39:25 -030083/**
84 * access_ok: - Checks if a user space pointer is valid
85 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
86 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
87 * to write to a block, it is always safe to read from it.
88 * @addr: User space pointer to start of block to check
89 * @size: Size of block to check
90 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +020091 * Context: User context only. This function may sleep if pagefaults are
92 * enabled.
Glauber Costaca233862008-06-13 14:39:25 -030093 *
94 * Checks if a pointer to a block of memory in user space is valid.
95 *
96 * Returns true (nonzero) if the memory block may be valid, false (zero)
97 * if it is definitely invalid.
98 *
99 * Note that, depending on architecture, this function probably just
100 * checks that the pointer is in the user space range - after calling
101 * this function, memory access functions may still return -EFAULT.
102 */
Peter Zijlstra2715f682016-11-22 10:57:15 +0100103#define access_ok(type, addr, size) \
104({ \
105 WARN_ON_IN_IRQ(); \
106 likely(!__range_not_ok(addr, size, user_addr_max())); \
107})
Glauber Costaca233862008-06-13 14:39:25 -0300108
109/*
Glauber Costaca233862008-06-13 14:39:25 -0300110 * These are the main single-value transfer routines. They automatically
111 * use the right size if we just have the right pointer type.
112 *
113 * This gets kind of ugly. We want to return _two_ values in "get_user()"
114 * and yet we don't want to do any pointers, because that is too much
115 * of a performance impact. Thus we have a few rather ugly macros here,
116 * and hide all the ugliness from the user.
117 *
118 * The "__xxx" versions of the user access functions are versions that
119 * do not verify the address space, that must have been done previously
120 * with a separate "access_ok()" call (this is used when we do multiple
121 * accesses to the same area of user memory).
122 */
123
124extern int __get_user_1(void);
125extern int __get_user_2(void);
126extern int __get_user_4(void);
127extern int __get_user_8(void);
128extern int __get_user_bad(void);
129
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800130#define __uaccess_begin() stac()
131#define __uaccess_end() clac()
Dan Williamse06d7bf2018-01-29 17:02:39 -0800132#define __uaccess_begin_nospec() \
133({ \
134 stac(); \
135 barrier_nospec(); \
136})
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800137
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800138/*
139 * This is a type: either unsigned long, if the argument fits into
140 * that type, or otherwise unsigned long long.
141 */
142#define __inttype(x) \
143__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
Glauber Costa865e5b72008-06-25 11:05:11 -0300144
145/**
146 * get_user: - Get a simple variable from user space.
147 * @x: Variable to store result.
148 * @ptr: Source address, in user space.
149 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200150 * Context: User context only. This function may sleep if pagefaults are
151 * enabled.
Glauber Costa865e5b72008-06-25 11:05:11 -0300152 *
153 * This macro copies a single simple variable from user space to kernel
154 * space. It supports simple types like char and int, but not larger
155 * data types like structures or arrays.
156 *
157 * @ptr must have pointer-to-simple-variable type, and the result of
158 * dereferencing @ptr must be assignable to @x without a cast.
159 *
160 * Returns zero on success, or -EFAULT on error.
161 * On error, the variable @x is set to zero.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800162 */
163/*
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800164 * Careful: we have to cast the result to the type of the pointer
165 * for sign reasons.
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800166 *
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700167 * The use of _ASM_DX as the register specifier is a bit of a
H. Peter Anvinff52c3b2013-02-12 15:37:02 -0800168 * simplification, as gcc only cares about it as the starting point
169 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
170 * (%ecx being the next register in gcc's x86 register sequence), and
171 * %rdx on 64 bits.
H. Peter Anvinf69fa9a2013-08-29 13:34:50 -0700172 *
173 * Clang/LLVM cares about the size of the register, but still wants
174 * the base register for something that ends up being a pair.
Glauber Costa865e5b72008-06-25 11:05:11 -0300175 */
Glauber Costa865e5b72008-06-25 11:05:11 -0300176#define get_user(x, ptr) \
177({ \
178 int __ret_gu; \
Jan-Simon Möllerbdfc0172013-08-29 21:13:05 +0200179 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300180 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100181 might_fault(); \
Chris J Argesf05058c2016-01-21 16:49:25 -0600182 asm volatile("call __get_user_%P4" \
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -0500183 : "=a" (__ret_gu), "=r" (__val_gu), \
184 ASM_CALL_CONSTRAINT \
H. Peter Anvin3578baa2013-02-12 11:47:31 -0800185 : "0" (ptr), "i" (sizeof(*(ptr)))); \
Michael S. Tsirkine182c572014-12-12 01:56:04 +0200186 (x) = (__force __typeof__(*(ptr))) __val_gu; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700187 __builtin_expect(__ret_gu, 0); \
Glauber Costa865e5b72008-06-25 11:05:11 -0300188})
189
Glauber Costae30a44f2008-06-25 13:17:43 -0300190#define __put_user_x(size, x, ptr, __ret_pu) \
191 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800192 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costae30a44f2008-06-25 13:17:43 -0300193
194
195
Glauber Costadc70ddf2008-06-25 11:48:29 -0300196#ifdef CONFIG_X86_32
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800197#define __put_user_asm_u64(x, addr, err, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800198 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700199 "1: movl %%eax,0(%2)\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300200 "2: movl %%edx,4(%2)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800201 "3:" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300202 ".section .fixup,\"ax\"\n" \
203 "4: movl %3,%0\n" \
204 " jmp 3b\n" \
205 ".previous\n" \
206 _ASM_EXTABLE(1b, 4b) \
207 _ASM_EXTABLE(2b, 4b) \
208 : "=r" (err) \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800209 : "A" (x), "r" (addr), "i" (errret), "0" (err))
Glauber Costae30a44f2008-06-25 13:17:43 -0300210
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800211#define __put_user_asm_ex_u64(x, addr) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800212 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700213 "1: movl %%eax,0(%1)\n" \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800214 "2: movl %%edx,4(%1)\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800215 "3:" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700216 _ASM_EXTABLE_EX(1b, 2b) \
217 _ASM_EXTABLE_EX(2b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800218 : : "A" (x), "r" (addr))
219
Glauber Costae30a44f2008-06-25 13:17:43 -0300220#define __put_user_x8(x, ptr, __ret_pu) \
221 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
222 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
Glauber Costadc70ddf2008-06-25 11:48:29 -0300223#else
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800224#define __put_user_asm_u64(x, ptr, retval, errret) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700225 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800226#define __put_user_asm_ex_u64(x, addr) \
H. Peter Anvinebe119c2009-07-20 23:27:39 -0700227 __put_user_asm_ex(x, addr, "q", "", "er")
Glauber Costae30a44f2008-06-25 13:17:43 -0300228#define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
Glauber Costadc70ddf2008-06-25 11:48:29 -0300229#endif
230
Glauber Costae30a44f2008-06-25 13:17:43 -0300231extern void __put_user_bad(void);
232
233/*
234 * Strange magic calling convention: pointer in %ecx,
235 * value in %eax(:%edx), return value in %eax. clobbers %rbx
236 */
237extern void __put_user_1(void);
238extern void __put_user_2(void);
239extern void __put_user_4(void);
240extern void __put_user_8(void);
241
Glauber Costae30a44f2008-06-25 13:17:43 -0300242/**
243 * put_user: - Write a simple value into user space.
244 * @x: Value to copy to user space.
245 * @ptr: Destination address, in user space.
246 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200247 * Context: User context only. This function may sleep if pagefaults are
248 * enabled.
Glauber Costae30a44f2008-06-25 13:17:43 -0300249 *
250 * This macro copies a single simple value from kernel space to user
251 * space. It supports simple types like char and int, but not larger
252 * data types like structures or arrays.
253 *
254 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
255 * to the result of dereferencing @ptr.
256 *
257 * Returns zero on success, or -EFAULT on error.
258 */
259#define put_user(x, ptr) \
260({ \
261 int __ret_pu; \
262 __typeof__(*(ptr)) __pu_val; \
263 __chk_user_ptr(ptr); \
Ingo Molnard1a76182008-10-28 16:54:49 +0100264 might_fault(); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300265 __pu_val = x; \
266 switch (sizeof(*(ptr))) { \
267 case 1: \
268 __put_user_x(1, __pu_val, ptr, __ret_pu); \
269 break; \
270 case 2: \
271 __put_user_x(2, __pu_val, ptr, __ret_pu); \
272 break; \
273 case 4: \
274 __put_user_x(4, __pu_val, ptr, __ret_pu); \
275 break; \
276 case 8: \
277 __put_user_x8(__pu_val, ptr, __ret_pu); \
278 break; \
279 default: \
280 __put_user_x(X, __pu_val, ptr, __ret_pu); \
281 break; \
282 } \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700283 __builtin_expect(__ret_pu, 0); \
Glauber Costae30a44f2008-06-25 13:17:43 -0300284})
285
Glauber Costadc70ddf2008-06-25 11:48:29 -0300286#define __put_user_size(x, ptr, size, retval, errret) \
287do { \
288 retval = 0; \
289 __chk_user_ptr(ptr); \
290 switch (size) { \
291 case 1: \
292 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
293 break; \
294 case 2: \
295 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
296 break; \
297 case 4: \
Hiroshi Shimamoto4d5d7832009-01-19 16:34:26 -0800298 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300299 break; \
300 case 8: \
Hiroshi Shimamoto18114f62009-01-30 18:16:46 -0800301 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
302 errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300303 break; \
304 default: \
305 __put_user_bad(); \
306 } \
307} while (0)
308
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800309/*
310 * This doesn't do __uaccess_begin/end - the exception handling
311 * around it must do that.
312 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800313#define __put_user_size_ex(x, ptr, size) \
314do { \
315 __chk_user_ptr(ptr); \
316 switch (size) { \
317 case 1: \
318 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
319 break; \
320 case 2: \
321 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
322 break; \
323 case 4: \
324 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
325 break; \
326 case 8: \
327 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
328 break; \
329 default: \
330 __put_user_bad(); \
331 } \
332} while (0)
333
Glauber Costa3f168222008-06-25 12:48:47 -0300334#ifdef CONFIG_X86_32
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500335#define __get_user_asm_u64(x, ptr, retval, errret) \
336({ \
337 __typeof__(ptr) __ptr = (ptr); \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700338 asm volatile("\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500339 "1: movl %2,%%eax\n" \
340 "2: movl %3,%%edx\n" \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700341 "3:\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500342 ".section .fixup,\"ax\"\n" \
343 "4: mov %4,%0\n" \
344 " xorl %%eax,%%eax\n" \
345 " xorl %%edx,%%edx\n" \
346 " jmp 3b\n" \
347 ".previous\n" \
348 _ASM_EXTABLE(1b, 4b) \
349 _ASM_EXTABLE(2b, 4b) \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700350 : "=r" (retval), "=&A"(x) \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500351 : "m" (__m(__ptr)), "m" __m(((u32 *)(__ptr)) + 1), \
352 "i" (errret), "0" (retval)); \
353})
354
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800355#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300356#else
357#define __get_user_asm_u64(x, ptr, retval, errret) \
358 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800359#define __get_user_asm_ex_u64(x, ptr) \
360 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300361#endif
362
363#define __get_user_size(x, ptr, size, retval, errret) \
364do { \
365 retval = 0; \
366 __chk_user_ptr(ptr); \
367 switch (size) { \
368 case 1: \
369 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
370 break; \
371 case 2: \
372 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
373 break; \
374 case 4: \
375 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
376 break; \
377 case 8: \
378 __get_user_asm_u64(x, ptr, retval, errret); \
379 break; \
380 default: \
381 (x) = __get_user_bad(); \
382 } \
383} while (0)
384
385#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800386 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700387 "1: mov"itype" %2,%"rtype"1\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800388 "2:\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300389 ".section .fixup,\"ax\"\n" \
390 "3: mov %3,%0\n" \
391 " xor"itype" %"rtype"1,%"rtype"1\n" \
392 " jmp 2b\n" \
393 ".previous\n" \
394 _ASM_EXTABLE(1b, 3b) \
395 : "=r" (err), ltype(x) \
396 : "m" (__m(addr)), "i" (errret), "0" (err))
397
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800398/*
399 * This doesn't do __uaccess_begin/end - the exception handling
400 * around it must do that.
401 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800402#define __get_user_size_ex(x, ptr, size) \
403do { \
404 __chk_user_ptr(ptr); \
405 switch (size) { \
406 case 1: \
407 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
408 break; \
409 case 2: \
410 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
411 break; \
412 case 4: \
413 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
414 break; \
415 case 8: \
416 __get_user_asm_ex_u64(x, ptr); \
417 break; \
418 default: \
419 (x) = __get_user_bad(); \
420 } \
421} while (0)
422
423#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700424 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
425 "2:\n" \
Al Viro1c109fa2016-09-15 02:35:29 +0100426 ".section .fixup,\"ax\"\n" \
427 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
428 " jmp 2b\n" \
429 ".previous\n" \
430 _ASM_EXTABLE_EX(1b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800431 : ltype(x) : "m" (__m(addr)))
432
Glauber Costadc70ddf2008-06-25 11:48:29 -0300433#define __put_user_nocheck(x, ptr, size) \
434({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800435 int __pu_err; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800436 __uaccess_begin(); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300437 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800438 __uaccess_end(); \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700439 __builtin_expect(__pu_err, 0); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300440})
441
Glauber Costa3f168222008-06-25 12:48:47 -0300442#define __get_user_nocheck(x, ptr, size) \
443({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800444 int __gu_err; \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500445 __inttype(*(ptr)) __gu_val; \
Dan Williams065eae42018-01-29 17:02:49 -0800446 __uaccess_begin_nospec(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300447 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800448 __uaccess_end(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300449 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700450 __builtin_expect(__gu_err, 0); \
Glauber Costa3f168222008-06-25 12:48:47 -0300451})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300452
453/* FIXME: this hack is definitely wrong -AK */
454struct __large_struct { unsigned long buf[100]; };
455#define __m(x) (*(struct __large_struct __user *)(x))
456
457/*
458 * Tell gcc we read from memory instead of writing: this is because
459 * we do not write to any memory gcc knows about, so there are no
460 * aliasing issues.
461 */
462#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800463 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700464 "1: mov"itype" %"rtype"1,%2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800465 "2:\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300466 ".section .fixup,\"ax\"\n" \
467 "3: mov %3,%0\n" \
468 " jmp 2b\n" \
469 ".previous\n" \
470 _ASM_EXTABLE(1b, 3b) \
471 : "=r"(err) \
472 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800473
474#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700475 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
476 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700477 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800478 : : ltype(x), "m" (__m(addr)))
479
480/*
481 * uaccess_try and catch
482 */
483#define uaccess_try do { \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700484 current->thread.uaccess_err = 0; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800485 __uaccess_begin(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800486 barrier();
487
Dan Williamse06d7bf2018-01-29 17:02:39 -0800488#define uaccess_try_nospec do { \
489 current->thread.uaccess_err = 0; \
490 __uaccess_begin_nospec(); \
491
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800492#define uaccess_catch(err) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800493 __uaccess_end(); \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700494 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800495} while (0)
496
Glauber Costa8cb834e2008-06-25 14:43:30 -0300497/**
498 * __get_user: - Get a simple variable from user space, with less checking.
499 * @x: Variable to store result.
500 * @ptr: Source address, in user space.
501 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200502 * Context: User context only. This function may sleep if pagefaults are
503 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300504 *
505 * This macro copies a single simple variable from user space to kernel
506 * space. It supports simple types like char and int, but not larger
507 * data types like structures or arrays.
508 *
509 * @ptr must have pointer-to-simple-variable type, and the result of
510 * dereferencing @ptr must be assignable to @x without a cast.
511 *
512 * Caller must check the pointer with access_ok() before calling this
513 * function.
514 *
515 * Returns zero on success, or -EFAULT on error.
516 * On error, the variable @x is set to zero.
517 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300518
Glauber Costa8cb834e2008-06-25 14:43:30 -0300519#define __get_user(x, ptr) \
520 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800521
Glauber Costa8cb834e2008-06-25 14:43:30 -0300522/**
523 * __put_user: - Write a simple value into user space, with less checking.
524 * @x: Value to copy to user space.
525 * @ptr: Destination address, in user space.
526 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200527 * Context: User context only. This function may sleep if pagefaults are
528 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300529 *
530 * This macro copies a single simple value from kernel space to user
531 * space. It supports simple types like char and int, but not larger
532 * data types like structures or arrays.
533 *
534 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
535 * to the result of dereferencing @ptr.
536 *
537 * Caller must check the pointer with access_ok() before calling this
538 * function.
539 *
540 * Returns zero on success, or -EFAULT on error.
541 */
542
543#define __put_user(x, ptr) \
544 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
545
546#define __get_user_unaligned __get_user
547#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300548
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300549/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800550 * {get|put}_user_try and catch
551 *
552 * get_user_try {
553 * get_user_ex(...);
554 * } get_user_catch(err)
555 */
Dan Williams065eae42018-01-29 17:02:49 -0800556#define get_user_try uaccess_try_nospec
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800557#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800558
559#define get_user_ex(x, ptr) do { \
560 unsigned long __gue_val; \
561 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
562 (x) = (__force __typeof__(*(ptr)))__gue_val; \
563} while (0)
564
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800565#define put_user_try uaccess_try
566#define put_user_catch(err) uaccess_catch(err)
567
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800568#define put_user_ex(x, ptr) \
569 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
570
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200571extern unsigned long
572copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700573extern __must_check long
574strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200575
Linus Torvalds5723aa92012-05-26 11:09:53 -0700576extern __must_check long strlen_user(const char __user *str);
577extern __must_check long strnlen_user(const char __user *str, long n);
578
H. Peter Anvina0528582012-09-21 12:43:11 -0700579unsigned long __must_check clear_user(void __user *mem, unsigned long len);
580unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
581
Qiaowei Renf09174c2013-12-14 14:25:02 +0800582extern void __cmpxchg_wrong_size(void)
583 __compiletime_error("Bad argument size for cmpxchg");
584
585#define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
586({ \
587 int __ret = 0; \
588 __typeof__(ptr) __uval = (uval); \
589 __typeof__(*(ptr)) __old = (old); \
590 __typeof__(*(ptr)) __new = (new); \
Dan Williams065eae42018-01-29 17:02:49 -0800591 __uaccess_begin_nospec(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800592 switch (size) { \
593 case 1: \
594 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800595 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800596 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800597 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800598 "\t.section .fixup, \"ax\"\n" \
599 "3:\tmov %3, %0\n" \
600 "\tjmp 2b\n" \
601 "\t.previous\n" \
602 _ASM_EXTABLE(1b, 3b) \
603 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
604 : "i" (-EFAULT), "q" (__new), "1" (__old) \
605 : "memory" \
606 ); \
607 break; \
608 } \
609 case 2: \
610 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800611 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800612 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800613 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800614 "\t.section .fixup, \"ax\"\n" \
615 "3:\tmov %3, %0\n" \
616 "\tjmp 2b\n" \
617 "\t.previous\n" \
618 _ASM_EXTABLE(1b, 3b) \
619 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
620 : "i" (-EFAULT), "r" (__new), "1" (__old) \
621 : "memory" \
622 ); \
623 break; \
624 } \
625 case 4: \
626 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800627 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800628 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800629 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800630 "\t.section .fixup, \"ax\"\n" \
631 "3:\tmov %3, %0\n" \
632 "\tjmp 2b\n" \
633 "\t.previous\n" \
634 _ASM_EXTABLE(1b, 3b) \
635 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
636 : "i" (-EFAULT), "r" (__new), "1" (__old) \
637 : "memory" \
638 ); \
639 break; \
640 } \
641 case 8: \
642 { \
643 if (!IS_ENABLED(CONFIG_X86_64)) \
644 __cmpxchg_wrong_size(); \
645 \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800646 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800647 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800648 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800649 "\t.section .fixup, \"ax\"\n" \
650 "3:\tmov %3, %0\n" \
651 "\tjmp 2b\n" \
652 "\t.previous\n" \
653 _ASM_EXTABLE(1b, 3b) \
654 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
655 : "i" (-EFAULT), "r" (__new), "1" (__old) \
656 : "memory" \
657 ); \
658 break; \
659 } \
660 default: \
661 __cmpxchg_wrong_size(); \
662 } \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800663 __uaccess_end(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800664 *__uval = __old; \
665 __ret; \
666})
667
668#define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
669({ \
670 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
671 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
672 (old), (new), sizeof(*(ptr))) : \
673 -EFAULT; \
674})
675
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800676/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300677 * movsl can be slow when source and dest are not both 8-byte aligned
678 */
679#ifdef CONFIG_X86_INTEL_USERCOPY
680extern struct movsl_mask {
681 int mask;
682} ____cacheline_aligned_in_smp movsl_mask;
683#endif
684
Glauber Costa22cac162008-06-25 14:56:53 -0300685#define ARCH_HAS_NOCACHE_UACCESS 1
686
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200687#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100688# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200689#else
David Howellsa1ce3922012-10-02 18:01:25 +0100690# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200691#endif
Glauber Costaca233862008-06-13 14:39:25 -0300692
Jan Beulich3df7b412013-10-21 09:43:57 +0100693unsigned long __must_check _copy_from_user(void *to, const void __user *from,
694 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100695unsigned long __must_check _copy_to_user(void __user *to, const void *from,
696 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100697
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500698extern void __compiletime_error("usercopy buffer size is too small")
699__bad_copy_user(void);
Jan Beulich3df7b412013-10-21 09:43:57 +0100700
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500701static inline void copy_user_overflow(int size, unsigned long count)
Jan Beulich3df7b412013-10-21 09:43:57 +0100702{
703 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
704}
705
Kees Cooke6971002016-09-06 11:56:01 -0700706static __always_inline unsigned long __must_check
Jan Beulich3df7b412013-10-21 09:43:57 +0100707copy_from_user(void *to, const void __user *from, unsigned long n)
708{
709 int sz = __compiletime_object_size(to);
710
711 might_fault();
712
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700713 kasan_check_write(to, n);
714
Kees Cook5b710f32016-06-23 15:04:01 -0700715 if (likely(sz < 0 || sz >= n)) {
716 check_object_size(to, n, false);
Jan Beulich3df7b412013-10-21 09:43:57 +0100717 n = _copy_from_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500718 } else if (!__builtin_constant_p(n))
719 copy_user_overflow(sz, n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100720 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500721 __bad_copy_user();
Jan Beulich3df7b412013-10-21 09:43:57 +0100722
723 return n;
724}
725
Kees Cooke6971002016-09-06 11:56:01 -0700726static __always_inline unsigned long __must_check
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100727copy_to_user(void __user *to, const void *from, unsigned long n)
728{
729 int sz = __compiletime_object_size(from);
730
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700731 kasan_check_read(from, n);
732
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100733 might_fault();
734
Kees Cook5b710f32016-06-23 15:04:01 -0700735 if (likely(sz < 0 || sz >= n)) {
736 check_object_size(from, n, true);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100737 n = _copy_to_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500738 } else if (!__builtin_constant_p(n))
739 copy_user_overflow(sz, n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100740 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500741 __bad_copy_user();
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100742
743 return n;
744}
745
Andi Kleen10013eb2015-10-22 15:07:20 -0700746/*
747 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
748 * nested NMI paths are careful to preserve CR2.
749 *
750 * Caller must use pagefault_enable/disable, or run in interrupt context,
751 * and also do a uaccess_ok() check
752 */
753#define __copy_from_user_nmi __copy_from_user_inatomic
754
Linus Torvalds404a4742016-01-21 13:02:41 -0800755/*
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800756 * The "unsafe" user accesses aren't really "unsafe", but the naming
757 * is a big fat warning: you have to not only do the access_ok()
758 * checking before using them, but you have to surround them with the
759 * user_access_begin/end() pair.
760 */
761#define user_access_begin() __uaccess_begin()
762#define user_access_end() __uaccess_end()
763
Linus Torvalds1bd44032016-08-08 13:02:01 -0700764#define unsafe_put_user(x, ptr, err_label) \
765do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800766 int __pu_err; \
767 __put_user_size((x), (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700768 if (unlikely(__pu_err)) goto err_label; \
769} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800770
Linus Torvalds1bd44032016-08-08 13:02:01 -0700771#define unsafe_get_user(x, ptr, err_label) \
772do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800773 int __gu_err; \
774 unsigned long __gu_val; \
775 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
776 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700777 if (unlikely(__gu_err)) goto err_label; \
778} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800779
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700780#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700781