blob: 92639c9ee2bd99f777d93753203e36f4eb8b967b [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: \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800301 __put_user_asm_u64(x, ptr, retval, errret); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300302 break; \
303 default: \
304 __put_user_bad(); \
305 } \
306} while (0)
307
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800308/*
309 * This doesn't do __uaccess_begin/end - the exception handling
310 * around it must do that.
311 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800312#define __put_user_size_ex(x, ptr, size) \
313do { \
314 __chk_user_ptr(ptr); \
315 switch (size) { \
316 case 1: \
317 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
318 break; \
319 case 2: \
320 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
321 break; \
322 case 4: \
323 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
324 break; \
325 case 8: \
326 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
327 break; \
328 default: \
329 __put_user_bad(); \
330 } \
331} while (0)
332
Glauber Costa3f168222008-06-25 12:48:47 -0300333#ifdef CONFIG_X86_32
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500334#define __get_user_asm_u64(x, ptr, retval, errret) \
335({ \
336 __typeof__(ptr) __ptr = (ptr); \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700337 asm volatile("\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500338 "1: movl %2,%%eax\n" \
339 "2: movl %3,%%edx\n" \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700340 "3:\n" \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500341 ".section .fixup,\"ax\"\n" \
342 "4: mov %4,%0\n" \
343 " xorl %%eax,%%eax\n" \
344 " xorl %%edx,%%edx\n" \
345 " jmp 3b\n" \
346 ".previous\n" \
347 _ASM_EXTABLE(1b, 4b) \
348 _ASM_EXTABLE(2b, 4b) \
Linus Torvaldsae382ca2017-05-21 18:26:54 -0700349 : "=r" (retval), "=&A"(x) \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500350 : "m" (__m(__ptr)), "m" __m(((u32 *)(__ptr)) + 1), \
351 "i" (errret), "0" (retval)); \
352})
353
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800354#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
Glauber Costa3f168222008-06-25 12:48:47 -0300355#else
356#define __get_user_asm_u64(x, ptr, retval, errret) \
357 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800358#define __get_user_asm_ex_u64(x, ptr) \
359 __get_user_asm_ex(x, ptr, "q", "", "=r")
Glauber Costa3f168222008-06-25 12:48:47 -0300360#endif
361
362#define __get_user_size(x, ptr, size, retval, errret) \
363do { \
364 retval = 0; \
365 __chk_user_ptr(ptr); \
366 switch (size) { \
367 case 1: \
368 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
369 break; \
370 case 2: \
371 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
372 break; \
373 case 4: \
374 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
375 break; \
376 case 8: \
377 __get_user_asm_u64(x, ptr, retval, errret); \
378 break; \
379 default: \
380 (x) = __get_user_bad(); \
381 } \
382} while (0)
383
384#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800385 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700386 "1: mov"itype" %2,%"rtype"1\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800387 "2:\n" \
Glauber Costa3f168222008-06-25 12:48:47 -0300388 ".section .fixup,\"ax\"\n" \
389 "3: mov %3,%0\n" \
390 " xor"itype" %"rtype"1,%"rtype"1\n" \
391 " jmp 2b\n" \
392 ".previous\n" \
393 _ASM_EXTABLE(1b, 3b) \
394 : "=r" (err), ltype(x) \
395 : "m" (__m(addr)), "i" (errret), "0" (err))
396
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800397/*
398 * This doesn't do __uaccess_begin/end - the exception handling
399 * around it must do that.
400 */
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800401#define __get_user_size_ex(x, ptr, size) \
402do { \
403 __chk_user_ptr(ptr); \
404 switch (size) { \
405 case 1: \
406 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
407 break; \
408 case 2: \
409 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
410 break; \
411 case 4: \
412 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
413 break; \
414 case 8: \
415 __get_user_asm_ex_u64(x, ptr); \
416 break; \
417 default: \
418 (x) = __get_user_bad(); \
419 } \
420} while (0)
421
422#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700423 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
424 "2:\n" \
Al Viro1c109fa2016-09-15 02:35:29 +0100425 ".section .fixup,\"ax\"\n" \
426 "3:xor"itype" %"rtype"0,%"rtype"0\n" \
427 " jmp 2b\n" \
428 ".previous\n" \
429 _ASM_EXTABLE_EX(1b, 3b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800430 : ltype(x) : "m" (__m(addr)))
431
Glauber Costadc70ddf2008-06-25 11:48:29 -0300432#define __put_user_nocheck(x, ptr, size) \
433({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800434 int __pu_err; \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800435 __typeof__(*(ptr)) __pu_val; \
436 __pu_val = x; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800437 __uaccess_begin(); \
Andy Lutomirskidd15ae32019-02-22 17:17:04 -0800438 __put_user_size(__pu_val, (ptr), (size), __pu_err, -EFAULT);\
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800439 __uaccess_end(); \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700440 __builtin_expect(__pu_err, 0); \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300441})
442
Glauber Costa3f168222008-06-25 12:48:47 -0300443#define __get_user_nocheck(x, ptr, size) \
444({ \
Hiroshi Shimamoto16855f82008-12-08 19:18:38 -0800445 int __gu_err; \
Benjamin LaHaiseb2f68032016-03-09 15:05:56 -0500446 __inttype(*(ptr)) __gu_val; \
Dan Williams065eae42018-01-29 17:02:49 -0800447 __uaccess_begin_nospec(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300448 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800449 __uaccess_end(); \
Glauber Costa3f168222008-06-25 12:48:47 -0300450 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Andy Lutomirskia76cf662015-10-05 17:47:49 -0700451 __builtin_expect(__gu_err, 0); \
Glauber Costa3f168222008-06-25 12:48:47 -0300452})
Glauber Costadc70ddf2008-06-25 11:48:29 -0300453
454/* FIXME: this hack is definitely wrong -AK */
455struct __large_struct { unsigned long buf[100]; };
456#define __m(x) (*(struct __large_struct __user *)(x))
457
458/*
459 * Tell gcc we read from memory instead of writing: this is because
460 * we do not write to any memory gcc knows about, so there are no
461 * aliasing issues.
462 */
463#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800464 asm volatile("\n" \
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700465 "1: mov"itype" %"rtype"1,%2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800466 "2:\n" \
Glauber Costadc70ddf2008-06-25 11:48:29 -0300467 ".section .fixup,\"ax\"\n" \
468 "3: mov %3,%0\n" \
469 " jmp 2b\n" \
470 ".previous\n" \
471 _ASM_EXTABLE(1b, 3b) \
472 : "=r"(err) \
473 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800474
475#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
H. Peter Anvin5e883532012-09-21 12:43:15 -0700476 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
477 "2:\n" \
H. Peter Anvin535c0c32012-04-20 16:57:35 -0700478 _ASM_EXTABLE_EX(1b, 2b) \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800479 : : ltype(x), "m" (__m(addr)))
480
481/*
482 * uaccess_try and catch
483 */
484#define uaccess_try do { \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700485 current->thread.uaccess_err = 0; \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800486 __uaccess_begin(); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800487 barrier();
488
Dan Williamse06d7bf2018-01-29 17:02:39 -0800489#define uaccess_try_nospec do { \
490 current->thread.uaccess_err = 0; \
491 __uaccess_begin_nospec(); \
492
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800493#define uaccess_catch(err) \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800494 __uaccess_end(); \
Andy Lutomirskidfa9a942016-07-14 13:22:56 -0700495 (err) |= (current->thread.uaccess_err ? -EFAULT : 0); \
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800496} while (0)
497
Glauber Costa8cb834e2008-06-25 14:43:30 -0300498/**
499 * __get_user: - Get a simple variable from user space, with less checking.
500 * @x: Variable to store result.
501 * @ptr: Source address, in user space.
502 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200503 * Context: User context only. This function may sleep if pagefaults are
504 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300505 *
506 * This macro copies a single simple variable from user space to kernel
507 * space. It supports simple types like char and int, but not larger
508 * data types like structures or arrays.
509 *
510 * @ptr must have pointer-to-simple-variable type, and the result of
511 * dereferencing @ptr must be assignable to @x without a cast.
512 *
513 * Caller must check the pointer with access_ok() before calling this
514 * function.
515 *
516 * Returns zero on success, or -EFAULT on error.
517 * On error, the variable @x is set to zero.
518 */
Glauber Costadc70ddf2008-06-25 11:48:29 -0300519
Glauber Costa8cb834e2008-06-25 14:43:30 -0300520#define __get_user(x, ptr) \
521 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800522
Glauber Costa8cb834e2008-06-25 14:43:30 -0300523/**
524 * __put_user: - Write a simple value into user space, with less checking.
525 * @x: Value to copy to user space.
526 * @ptr: Destination address, in user space.
527 *
David Hildenbrandb3c395e2015-05-11 17:52:08 +0200528 * Context: User context only. This function may sleep if pagefaults are
529 * enabled.
Glauber Costa8cb834e2008-06-25 14:43:30 -0300530 *
531 * This macro copies a single simple value from kernel space to user
532 * space. It supports simple types like char and int, but not larger
533 * data types like structures or arrays.
534 *
535 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
536 * to the result of dereferencing @ptr.
537 *
538 * Caller must check the pointer with access_ok() before calling this
539 * function.
540 *
541 * Returns zero on success, or -EFAULT on error.
542 */
543
544#define __put_user(x, ptr) \
545 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
546
547#define __get_user_unaligned __get_user
548#define __put_user_unaligned __put_user
Glauber Costa865e5b72008-06-25 11:05:11 -0300549
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300550/*
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800551 * {get|put}_user_try and catch
552 *
553 * get_user_try {
554 * get_user_ex(...);
555 * } get_user_catch(err)
556 */
Dan Williams065eae42018-01-29 17:02:49 -0800557#define get_user_try uaccess_try_nospec
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800558#define get_user_catch(err) uaccess_catch(err)
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800559
560#define get_user_ex(x, ptr) do { \
561 unsigned long __gue_val; \
562 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
563 (x) = (__force __typeof__(*(ptr)))__gue_val; \
564} while (0)
565
Hiroshi Shimamoto019a1362009-01-29 11:49:18 -0800566#define put_user_try uaccess_try
567#define put_user_catch(err) uaccess_catch(err)
568
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800569#define put_user_ex(x, ptr) \
570 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
571
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200572extern unsigned long
573copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
Linus Torvalds92ae03f2012-04-06 14:32:32 -0700574extern __must_check long
575strncpy_from_user(char *dst, const char __user *src, long count);
Robert Richter1ac2e6c2011-06-07 11:49:55 +0200576
Linus Torvalds5723aa92012-05-26 11:09:53 -0700577extern __must_check long strlen_user(const char __user *str);
578extern __must_check long strnlen_user(const char __user *str, long n);
579
H. Peter Anvina0528582012-09-21 12:43:11 -0700580unsigned long __must_check clear_user(void __user *mem, unsigned long len);
581unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
582
Qiaowei Renf09174c2013-12-14 14:25:02 +0800583extern void __cmpxchg_wrong_size(void)
584 __compiletime_error("Bad argument size for cmpxchg");
585
586#define __user_atomic_cmpxchg_inatomic(uval, ptr, old, new, size) \
587({ \
588 int __ret = 0; \
589 __typeof__(ptr) __uval = (uval); \
590 __typeof__(*(ptr)) __old = (old); \
591 __typeof__(*(ptr)) __new = (new); \
Dan Williams065eae42018-01-29 17:02:49 -0800592 __uaccess_begin_nospec(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800593 switch (size) { \
594 case 1: \
595 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800596 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800597 "1:\t" LOCK_PREFIX "cmpxchgb %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800598 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800599 "\t.section .fixup, \"ax\"\n" \
600 "3:\tmov %3, %0\n" \
601 "\tjmp 2b\n" \
602 "\t.previous\n" \
603 _ASM_EXTABLE(1b, 3b) \
604 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
605 : "i" (-EFAULT), "q" (__new), "1" (__old) \
606 : "memory" \
607 ); \
608 break; \
609 } \
610 case 2: \
611 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800612 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800613 "1:\t" LOCK_PREFIX "cmpxchgw %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800614 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800615 "\t.section .fixup, \"ax\"\n" \
616 "3:\tmov %3, %0\n" \
617 "\tjmp 2b\n" \
618 "\t.previous\n" \
619 _ASM_EXTABLE(1b, 3b) \
620 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
621 : "i" (-EFAULT), "r" (__new), "1" (__old) \
622 : "memory" \
623 ); \
624 break; \
625 } \
626 case 4: \
627 { \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800628 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800629 "1:\t" LOCK_PREFIX "cmpxchgl %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800630 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800631 "\t.section .fixup, \"ax\"\n" \
632 "3:\tmov %3, %0\n" \
633 "\tjmp 2b\n" \
634 "\t.previous\n" \
635 _ASM_EXTABLE(1b, 3b) \
636 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
637 : "i" (-EFAULT), "r" (__new), "1" (__old) \
638 : "memory" \
639 ); \
640 break; \
641 } \
642 case 8: \
643 { \
644 if (!IS_ENABLED(CONFIG_X86_64)) \
645 __cmpxchg_wrong_size(); \
646 \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800647 asm volatile("\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800648 "1:\t" LOCK_PREFIX "cmpxchgq %4, %2\n" \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800649 "2:\n" \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800650 "\t.section .fixup, \"ax\"\n" \
651 "3:\tmov %3, %0\n" \
652 "\tjmp 2b\n" \
653 "\t.previous\n" \
654 _ASM_EXTABLE(1b, 3b) \
655 : "+r" (__ret), "=a" (__old), "+m" (*(ptr)) \
656 : "i" (-EFAULT), "r" (__new), "1" (__old) \
657 : "memory" \
658 ); \
659 break; \
660 } \
661 default: \
662 __cmpxchg_wrong_size(); \
663 } \
Linus Torvalds11f1a4b2015-12-17 09:45:09 -0800664 __uaccess_end(); \
Qiaowei Renf09174c2013-12-14 14:25:02 +0800665 *__uval = __old; \
666 __ret; \
667})
668
669#define user_atomic_cmpxchg_inatomic(uval, ptr, old, new) \
670({ \
671 access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) ? \
672 __user_atomic_cmpxchg_inatomic((uval), (ptr), \
673 (old), (new), sizeof(*(ptr))) : \
674 -EFAULT; \
675})
676
Hiroshi Shimamotofe40c0a2009-01-23 15:49:41 -0800677/*
Glauber Costa8bc7de0c2008-06-25 14:53:41 -0300678 * movsl can be slow when source and dest are not both 8-byte aligned
679 */
680#ifdef CONFIG_X86_INTEL_USERCOPY
681extern struct movsl_mask {
682 int mask;
683} ____cacheline_aligned_in_smp movsl_mask;
684#endif
685
Glauber Costa22cac162008-06-25 14:56:53 -0300686#define ARCH_HAS_NOCACHE_UACCESS 1
687
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200688#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100689# include <asm/uaccess_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200690#else
David Howellsa1ce3922012-10-02 18:01:25 +0100691# include <asm/uaccess_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200692#endif
Glauber Costaca233862008-06-13 14:39:25 -0300693
Jan Beulich3df7b412013-10-21 09:43:57 +0100694unsigned long __must_check _copy_from_user(void *to, const void __user *from,
695 unsigned n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100696unsigned long __must_check _copy_to_user(void __user *to, const void *from,
697 unsigned n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100698
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500699extern void __compiletime_error("usercopy buffer size is too small")
700__bad_copy_user(void);
Jan Beulich3df7b412013-10-21 09:43:57 +0100701
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500702static inline void copy_user_overflow(int size, unsigned long count)
Jan Beulich3df7b412013-10-21 09:43:57 +0100703{
704 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
705}
706
Kees Cooke6971002016-09-06 11:56:01 -0700707static __always_inline unsigned long __must_check
Jan Beulich3df7b412013-10-21 09:43:57 +0100708copy_from_user(void *to, const void __user *from, unsigned long n)
709{
710 int sz = __compiletime_object_size(to);
711
712 might_fault();
713
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700714 kasan_check_write(to, n);
715
Kees Cook5b710f32016-06-23 15:04:01 -0700716 if (likely(sz < 0 || sz >= n)) {
717 check_object_size(to, n, false);
Jan Beulich3df7b412013-10-21 09:43:57 +0100718 n = _copy_from_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500719 } else if (!__builtin_constant_p(n))
720 copy_user_overflow(sz, n);
Jan Beulich3df7b412013-10-21 09:43:57 +0100721 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500722 __bad_copy_user();
Jan Beulich3df7b412013-10-21 09:43:57 +0100723
724 return n;
725}
726
Kees Cooke6971002016-09-06 11:56:01 -0700727static __always_inline unsigned long __must_check
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100728copy_to_user(void __user *to, const void *from, unsigned long n)
729{
730 int sz = __compiletime_object_size(from);
731
Andrey Ryabinin1771c6e2016-05-20 16:59:31 -0700732 kasan_check_read(from, n);
733
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100734 might_fault();
735
Kees Cook5b710f32016-06-23 15:04:01 -0700736 if (likely(sz < 0 || sz >= n)) {
737 check_object_size(from, n, true);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100738 n = _copy_to_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500739 } else if (!__builtin_constant_p(n))
740 copy_user_overflow(sz, n);
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100741 else
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500742 __bad_copy_user();
Jan Beulich7a3d9b02013-10-21 09:44:37 +0100743
744 return n;
745}
746
Andi Kleen10013eb2015-10-22 15:07:20 -0700747/*
748 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
749 * nested NMI paths are careful to preserve CR2.
750 *
751 * Caller must use pagefault_enable/disable, or run in interrupt context,
752 * and also do a uaccess_ok() check
753 */
754#define __copy_from_user_nmi __copy_from_user_inatomic
755
Linus Torvalds404a4742016-01-21 13:02:41 -0800756/*
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800757 * The "unsafe" user accesses aren't really "unsafe", but the naming
758 * is a big fat warning: you have to not only do the access_ok()
759 * checking before using them, but you have to surround them with the
760 * user_access_begin/end() pair.
761 */
762#define user_access_begin() __uaccess_begin()
763#define user_access_end() __uaccess_end()
764
Linus Torvalds1bd44032016-08-08 13:02:01 -0700765#define unsafe_put_user(x, ptr, err_label) \
766do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800767 int __pu_err; \
768 __put_user_size((x), (ptr), sizeof(*(ptr)), __pu_err, -EFAULT); \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700769 if (unlikely(__pu_err)) goto err_label; \
770} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800771
Linus Torvalds1bd44032016-08-08 13:02:01 -0700772#define unsafe_get_user(x, ptr, err_label) \
773do { \
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800774 int __gu_err; \
775 unsigned long __gu_val; \
776 __get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err, -EFAULT); \
777 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Linus Torvalds1bd44032016-08-08 13:02:01 -0700778 if (unlikely(__gu_err)) goto err_label; \
779} while (0)
Linus Torvalds5b24a7a2015-12-17 09:57:27 -0800780
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700781#endif /* _ASM_X86_UACCESS_H */
Nick Piggin8174c432008-07-25 19:45:24 -0700782