]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/include/asm/uaccess.h
x86: Replace assembly access_ok() with a C variant
[mirror_ubuntu-focal-kernel.git] / arch / x86 / include / asm / uaccess.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_UACCESS_H
2#define _ASM_X86_UACCESS_H
ca233862
GC
3/*
4 * User space memory access functions
5 */
6#include <linux/errno.h>
7#include <linux/compiler.h>
8#include <linux/thread_info.h>
ca233862
GC
9#include <linux/string.h>
10#include <asm/asm.h>
11#include <asm/page.h>
63bcff2a 12#include <asm/smap.h>
ca233862
GC
13
14#define VERIFY_READ 0
15#define VERIFY_WRITE 1
16
17/*
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
21 *
22 * For historical reasons, these macros are grossly misnamed.
23 */
24
25#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26
27#define KERNEL_DS MAKE_MM_SEG(-1UL)
9063c61f 28#define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX)
ca233862
GC
29
30#define get_ds() (KERNEL_DS)
31#define get_fs() (current_thread_info()->addr_limit)
32#define set_fs(x) (current_thread_info()->addr_limit = (x))
33
34#define segment_eq(a, b) ((a).seg == (b).seg)
35
4ae73f2d 36#define user_addr_max() (current_thread_info()->addr_limit.seg)
bc6ca7b3
AS
37#define __addr_ok(addr) \
38 ((unsigned long __force)(addr) < user_addr_max())
002ca169 39
ca233862
GC
40/*
41 * Test whether a block of memory is a valid user space address.
42 * Returns 0 if the range is valid, nonzero otherwise.
ca233862 43 */
c5fe5d80
LT
44static inline int __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
45{
46 /*
47 * If we have used "sizeof()" for the size,
48 * we know it won't overflow the limit (but
49 * it might overflow the 'addr', so it's
50 * important to subtract the size from the
51 * limit, not add it to the address).
52 */
53 if (__builtin_constant_p(size))
54 return addr > limit - size;
55
56 /* Arbitrary sizes? Be careful about overflow */
57 addr += size;
58 return (addr < size) || (addr > limit);
59}
ca233862 60
bc6ca7b3 61#define __range_not_ok(addr, size, limit) \
ca233862 62({ \
ca233862 63 __chk_user_ptr(addr); \
c5fe5d80 64 __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
ca233862
GC
65})
66
67/**
68 * access_ok: - Checks if a user space pointer is valid
69 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
70 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
71 * to write to a block, it is always safe to read from it.
72 * @addr: User space pointer to start of block to check
73 * @size: Size of block to check
74 *
75 * Context: User context only. This function may sleep.
76 *
77 * Checks if a pointer to a block of memory in user space is valid.
78 *
79 * Returns true (nonzero) if the memory block may be valid, false (zero)
80 * if it is definitely invalid.
81 *
82 * Note that, depending on architecture, this function probably just
83 * checks that the pointer is in the user space range - after calling
84 * this function, memory access functions may still return -EFAULT.
85 */
bc6ca7b3
AS
86#define access_ok(type, addr, size) \
87 (likely(__range_not_ok(addr, size, user_addr_max()) == 0))
ca233862
GC
88
89/*
70627654
PA
90 * The exception table consists of pairs of addresses relative to the
91 * exception table enty itself: the first is the address of an
92 * instruction that is allowed to fault, and the second is the address
93 * at which the program should continue. No registers are modified,
94 * so it is entirely up to the continuation code to figure out what to
95 * do.
ca233862
GC
96 *
97 * All the routines below use bits of fixup code that are out of line
98 * with the main instruction path. This means when everything is well,
99 * we don't even have to jump over them. Further, they do not intrude
100 * on our cache or tlb entries.
101 */
102
103struct exception_table_entry {
70627654 104 int insn, fixup;
ca233862 105};
70627654
PA
106/* This is not the generic standard exception_table_entry format */
107#define ARCH_HAS_SORT_EXTABLE
108#define ARCH_HAS_SEARCH_EXTABLE
ca233862
GC
109
110extern int fixup_exception(struct pt_regs *regs);
70627654 111extern int early_fixup_exception(unsigned long *ip);
ca233862
GC
112
113/*
114 * These are the main single-value transfer routines. They automatically
115 * use the right size if we just have the right pointer type.
116 *
117 * This gets kind of ugly. We want to return _two_ values in "get_user()"
118 * and yet we don't want to do any pointers, because that is too much
119 * of a performance impact. Thus we have a few rather ugly macros here,
120 * and hide all the ugliness from the user.
121 *
122 * The "__xxx" versions of the user access functions are versions that
123 * do not verify the address space, that must have been done previously
124 * with a separate "access_ok()" call (this is used when we do multiple
125 * accesses to the same area of user memory).
126 */
127
128extern int __get_user_1(void);
129extern int __get_user_2(void);
130extern int __get_user_4(void);
131extern int __get_user_8(void);
132extern int __get_user_bad(void);
133
3578baae
PA
134/*
135 * This is a type: either unsigned long, if the argument fits into
136 * that type, or otherwise unsigned long long.
137 */
138#define __inttype(x) \
139__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
865e5b76
GC
140
141/**
142 * get_user: - Get a simple variable from user space.
143 * @x: Variable to store result.
144 * @ptr: Source address, in user space.
145 *
146 * Context: User context only. This function may sleep.
147 *
148 * This macro copies a single simple variable from user space to kernel
149 * space. It supports simple types like char and int, but not larger
150 * data types like structures or arrays.
151 *
152 * @ptr must have pointer-to-simple-variable type, and the result of
153 * dereferencing @ptr must be assignable to @x without a cast.
154 *
155 * Returns zero on success, or -EFAULT on error.
156 * On error, the variable @x is set to zero.
ff52c3b0
PA
157 */
158/*
3578baae
PA
159 * Careful: we have to cast the result to the type of the pointer
160 * for sign reasons.
ff52c3b0 161 *
f69fa9a9 162 * The use of _ASM_DX as the register specifier is a bit of a
ff52c3b0
PA
163 * simplification, as gcc only cares about it as the starting point
164 * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits
165 * (%ecx being the next register in gcc's x86 register sequence), and
166 * %rdx on 64 bits.
f69fa9a9
PA
167 *
168 * Clang/LLVM cares about the size of the register, but still wants
169 * the base register for something that ends up being a pair.
865e5b76 170 */
865e5b76
GC
171#define get_user(x, ptr) \
172({ \
173 int __ret_gu; \
bdfc017e 174 register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
865e5b76 175 __chk_user_ptr(ptr); \
3ee1afa3 176 might_fault(); \
3578baae
PA
177 asm volatile("call __get_user_%P3" \
178 : "=a" (__ret_gu), "=r" (__val_gu) \
179 : "0" (ptr), "i" (sizeof(*(ptr)))); \
180 (x) = (__typeof__(*(ptr))) __val_gu; \
865e5b76
GC
181 __ret_gu; \
182})
183
e30a44fd
GC
184#define __put_user_x(size, x, ptr, __ret_pu) \
185 asm volatile("call __put_user_" #size : "=a" (__ret_pu) \
4d5d7838 186 : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
e30a44fd
GC
187
188
189
dc70ddf4 190#ifdef CONFIG_X86_32
18114f61 191#define __put_user_asm_u64(x, addr, err, errret) \
63bcff2a
PA
192 asm volatile(ASM_STAC "\n" \
193 "1: movl %%eax,0(%2)\n" \
dc70ddf4 194 "2: movl %%edx,4(%2)\n" \
63bcff2a 195 "3: " ASM_CLAC "\n" \
dc70ddf4
GC
196 ".section .fixup,\"ax\"\n" \
197 "4: movl %3,%0\n" \
198 " jmp 3b\n" \
199 ".previous\n" \
200 _ASM_EXTABLE(1b, 4b) \
201 _ASM_EXTABLE(2b, 4b) \
202 : "=r" (err) \
18114f61 203 : "A" (x), "r" (addr), "i" (errret), "0" (err))
e30a44fd 204
fe40c0af 205#define __put_user_asm_ex_u64(x, addr) \
63bcff2a
PA
206 asm volatile(ASM_STAC "\n" \
207 "1: movl %%eax,0(%1)\n" \
fe40c0af 208 "2: movl %%edx,4(%1)\n" \
63bcff2a 209 "3: " ASM_CLAC "\n" \
535c0c34
PA
210 _ASM_EXTABLE_EX(1b, 2b) \
211 _ASM_EXTABLE_EX(2b, 3b) \
fe40c0af
HS
212 : : "A" (x), "r" (addr))
213
e30a44fd
GC
214#define __put_user_x8(x, ptr, __ret_pu) \
215 asm volatile("call __put_user_8" : "=a" (__ret_pu) \
216 : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx")
dc70ddf4 217#else
18114f61 218#define __put_user_asm_u64(x, ptr, retval, errret) \
ebe119cd 219 __put_user_asm(x, ptr, retval, "q", "", "er", errret)
fe40c0af 220#define __put_user_asm_ex_u64(x, addr) \
ebe119cd 221 __put_user_asm_ex(x, addr, "q", "", "er")
e30a44fd 222#define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu)
dc70ddf4
GC
223#endif
224
e30a44fd
GC
225extern void __put_user_bad(void);
226
227/*
228 * Strange magic calling convention: pointer in %ecx,
229 * value in %eax(:%edx), return value in %eax. clobbers %rbx
230 */
231extern void __put_user_1(void);
232extern void __put_user_2(void);
233extern void __put_user_4(void);
234extern void __put_user_8(void);
235
e30a44fd
GC
236/**
237 * put_user: - Write a simple value into user space.
238 * @x: Value to copy to user space.
239 * @ptr: Destination address, in user space.
240 *
241 * Context: User context only. This function may sleep.
242 *
243 * This macro copies a single simple value from kernel space to user
244 * space. It supports simple types like char and int, but not larger
245 * data types like structures or arrays.
246 *
247 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
248 * to the result of dereferencing @ptr.
249 *
250 * Returns zero on success, or -EFAULT on error.
251 */
252#define put_user(x, ptr) \
253({ \
254 int __ret_pu; \
255 __typeof__(*(ptr)) __pu_val; \
256 __chk_user_ptr(ptr); \
3ee1afa3 257 might_fault(); \
e30a44fd
GC
258 __pu_val = x; \
259 switch (sizeof(*(ptr))) { \
260 case 1: \
261 __put_user_x(1, __pu_val, ptr, __ret_pu); \
262 break; \
263 case 2: \
264 __put_user_x(2, __pu_val, ptr, __ret_pu); \
265 break; \
266 case 4: \
267 __put_user_x(4, __pu_val, ptr, __ret_pu); \
268 break; \
269 case 8: \
270 __put_user_x8(__pu_val, ptr, __ret_pu); \
271 break; \
272 default: \
273 __put_user_x(X, __pu_val, ptr, __ret_pu); \
274 break; \
275 } \
276 __ret_pu; \
277})
278
dc70ddf4
GC
279#define __put_user_size(x, ptr, size, retval, errret) \
280do { \
281 retval = 0; \
282 __chk_user_ptr(ptr); \
283 switch (size) { \
284 case 1: \
285 __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \
286 break; \
287 case 2: \
288 __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \
289 break; \
290 case 4: \
4d5d7838 291 __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \
dc70ddf4
GC
292 break; \
293 case 8: \
18114f61
HS
294 __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \
295 errret); \
dc70ddf4
GC
296 break; \
297 default: \
298 __put_user_bad(); \
299 } \
300} while (0)
301
fe40c0af
HS
302#define __put_user_size_ex(x, ptr, size) \
303do { \
304 __chk_user_ptr(ptr); \
305 switch (size) { \
306 case 1: \
307 __put_user_asm_ex(x, ptr, "b", "b", "iq"); \
308 break; \
309 case 2: \
310 __put_user_asm_ex(x, ptr, "w", "w", "ir"); \
311 break; \
312 case 4: \
313 __put_user_asm_ex(x, ptr, "l", "k", "ir"); \
314 break; \
315 case 8: \
316 __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \
317 break; \
318 default: \
319 __put_user_bad(); \
320 } \
321} while (0)
322
3f168221
GC
323#ifdef CONFIG_X86_32
324#define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad()
fe40c0af 325#define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad()
3f168221
GC
326#else
327#define __get_user_asm_u64(x, ptr, retval, errret) \
328 __get_user_asm(x, ptr, retval, "q", "", "=r", errret)
fe40c0af
HS
329#define __get_user_asm_ex_u64(x, ptr) \
330 __get_user_asm_ex(x, ptr, "q", "", "=r")
3f168221
GC
331#endif
332
333#define __get_user_size(x, ptr, size, retval, errret) \
334do { \
335 retval = 0; \
336 __chk_user_ptr(ptr); \
337 switch (size) { \
338 case 1: \
339 __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \
340 break; \
341 case 2: \
342 __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \
343 break; \
344 case 4: \
345 __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \
346 break; \
347 case 8: \
348 __get_user_asm_u64(x, ptr, retval, errret); \
349 break; \
350 default: \
351 (x) = __get_user_bad(); \
352 } \
353} while (0)
354
355#define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \
63bcff2a
PA
356 asm volatile(ASM_STAC "\n" \
357 "1: mov"itype" %2,%"rtype"1\n" \
358 "2: " ASM_CLAC "\n" \
3f168221
GC
359 ".section .fixup,\"ax\"\n" \
360 "3: mov %3,%0\n" \
361 " xor"itype" %"rtype"1,%"rtype"1\n" \
362 " jmp 2b\n" \
363 ".previous\n" \
364 _ASM_EXTABLE(1b, 3b) \
365 : "=r" (err), ltype(x) \
366 : "m" (__m(addr)), "i" (errret), "0" (err))
367
fe40c0af
HS
368#define __get_user_size_ex(x, ptr, size) \
369do { \
370 __chk_user_ptr(ptr); \
371 switch (size) { \
372 case 1: \
373 __get_user_asm_ex(x, ptr, "b", "b", "=q"); \
374 break; \
375 case 2: \
376 __get_user_asm_ex(x, ptr, "w", "w", "=r"); \
377 break; \
378 case 4: \
379 __get_user_asm_ex(x, ptr, "l", "k", "=r"); \
380 break; \
381 case 8: \
382 __get_user_asm_ex_u64(x, ptr); \
383 break; \
384 default: \
385 (x) = __get_user_bad(); \
386 } \
387} while (0)
388
389#define __get_user_asm_ex(x, addr, itype, rtype, ltype) \
5e88353d
PA
390 asm volatile("1: mov"itype" %1,%"rtype"0\n" \
391 "2:\n" \
535c0c34 392 _ASM_EXTABLE_EX(1b, 2b) \
fe40c0af
HS
393 : ltype(x) : "m" (__m(addr)))
394
dc70ddf4
GC
395#define __put_user_nocheck(x, ptr, size) \
396({ \
16855f87 397 int __pu_err; \
dc70ddf4
GC
398 __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \
399 __pu_err; \
400})
401
3f168221
GC
402#define __get_user_nocheck(x, ptr, size) \
403({ \
16855f87 404 int __gu_err; \
3f168221
GC
405 unsigned long __gu_val; \
406 __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \
407 (x) = (__force __typeof__(*(ptr)))__gu_val; \
408 __gu_err; \
409})
dc70ddf4
GC
410
411/* FIXME: this hack is definitely wrong -AK */
412struct __large_struct { unsigned long buf[100]; };
413#define __m(x) (*(struct __large_struct __user *)(x))
414
415/*
416 * Tell gcc we read from memory instead of writing: this is because
417 * we do not write to any memory gcc knows about, so there are no
418 * aliasing issues.
419 */
420#define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \
63bcff2a
PA
421 asm volatile(ASM_STAC "\n" \
422 "1: mov"itype" %"rtype"1,%2\n" \
423 "2: " ASM_CLAC "\n" \
dc70ddf4
GC
424 ".section .fixup,\"ax\"\n" \
425 "3: mov %3,%0\n" \
426 " jmp 2b\n" \
427 ".previous\n" \
428 _ASM_EXTABLE(1b, 3b) \
429 : "=r"(err) \
430 : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
fe40c0af
HS
431
432#define __put_user_asm_ex(x, addr, itype, rtype, ltype) \
5e88353d
PA
433 asm volatile("1: mov"itype" %"rtype"0,%1\n" \
434 "2:\n" \
535c0c34 435 _ASM_EXTABLE_EX(1b, 2b) \
fe40c0af
HS
436 : : ltype(x), "m" (__m(addr)))
437
438/*
439 * uaccess_try and catch
440 */
441#define uaccess_try do { \
fe40c0af 442 current_thread_info()->uaccess_err = 0; \
5e88353d 443 stac(); \
fe40c0af
HS
444 barrier();
445
446#define uaccess_catch(err) \
5e88353d 447 clac(); \
4fc34901 448 (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \
fe40c0af
HS
449} while (0)
450
8cb834e9
GC
451/**
452 * __get_user: - Get a simple variable from user space, with less checking.
453 * @x: Variable to store result.
454 * @ptr: Source address, in user space.
455 *
456 * Context: User context only. This function may sleep.
457 *
458 * This macro copies a single simple variable from user space to kernel
459 * space. It supports simple types like char and int, but not larger
460 * data types like structures or arrays.
461 *
462 * @ptr must have pointer-to-simple-variable type, and the result of
463 * dereferencing @ptr must be assignable to @x without a cast.
464 *
465 * Caller must check the pointer with access_ok() before calling this
466 * function.
467 *
468 * Returns zero on success, or -EFAULT on error.
469 * On error, the variable @x is set to zero.
470 */
471
472#define __get_user(x, ptr) \
473 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
fe40c0af 474
8cb834e9
GC
475/**
476 * __put_user: - Write a simple value into user space, with less checking.
477 * @x: Value to copy to user space.
478 * @ptr: Destination address, in user space.
479 *
480 * Context: User context only. This function may sleep.
481 *
482 * This macro copies a single simple value from kernel space to user
483 * space. It supports simple types like char and int, but not larger
484 * data types like structures or arrays.
485 *
486 * @ptr must have pointer-to-simple-variable type, and @x must be assignable
487 * to the result of dereferencing @ptr.
488 *
489 * Caller must check the pointer with access_ok() before calling this
490 * function.
491 *
492 * Returns zero on success, or -EFAULT on error.
493 */
494
495#define __put_user(x, ptr) \
496 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
dc70ddf4 497
8cb834e9
GC
498#define __get_user_unaligned __get_user
499#define __put_user_unaligned __put_user
865e5b76 500
fe40c0af
HS
501/*
502 * {get|put}_user_try and catch
503 *
504 * get_user_try {
505 * get_user_ex(...);
506 * } get_user_catch(err)
507 */
508#define get_user_try uaccess_try
509#define get_user_catch(err) uaccess_catch(err)
fe40c0af
HS
510
511#define get_user_ex(x, ptr) do { \
512 unsigned long __gue_val; \
513 __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \
514 (x) = (__force __typeof__(*(ptr)))__gue_val; \
515} while (0)
516
019a1369
HS
517#define put_user_try uaccess_try
518#define put_user_catch(err) uaccess_catch(err)
519
fe40c0af
HS
520#define put_user_ex(x, ptr) \
521 __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
522
1ac2e6ca
RR
523extern unsigned long
524copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
92ae03f2
LT
525extern __must_check long
526strncpy_from_user(char *dst, const char __user *src, long count);
1ac2e6ca 527
5723aa99
LT
528extern __must_check long strlen_user(const char __user *str);
529extern __must_check long strnlen_user(const char __user *str, long n);
530
a052858f
PA
531unsigned long __must_check clear_user(void __user *mem, unsigned long len);
532unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
533
8bc7de0c
GC
534/*
535 * movsl can be slow when source and dest are not both 8-byte aligned
536 */
537#ifdef CONFIG_X86_INTEL_USERCOPY
538extern struct movsl_mask {
539 int mask;
540} ____cacheline_aligned_in_smp movsl_mask;
541#endif
542
22cac167
GC
543#define ARCH_HAS_NOCACHE_UACCESS 1
544
96a388de 545#ifdef CONFIG_X86_32
a1ce3928 546# include <asm/uaccess_32.h>
96a388de 547#else
a1ce3928 548# include <asm/uaccess_64.h>
96a388de 549#endif
ca233862 550
3df7b41a
JB
551unsigned long __must_check _copy_from_user(void *to, const void __user *from,
552 unsigned n);
7a3d9b0f
JB
553unsigned long __must_check _copy_to_user(void __user *to, const void *from,
554 unsigned n);
3df7b41a
JB
555
556#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
557# define copy_user_diag __compiletime_error
558#else
559# define copy_user_diag __compiletime_warning
560#endif
561
562extern void copy_user_diag("copy_from_user() buffer size is too small")
563copy_from_user_overflow(void);
7a3d9b0f
JB
564extern void copy_user_diag("copy_to_user() buffer size is too small")
565copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
3df7b41a
JB
566
567#undef copy_user_diag
568
569#ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
570
571extern void
572__compiletime_warning("copy_from_user() buffer size is not provably correct")
573__copy_from_user_overflow(void) __asm__("copy_from_user_overflow");
574#define __copy_from_user_overflow(size, count) __copy_from_user_overflow()
575
7a3d9b0f
JB
576extern void
577__compiletime_warning("copy_to_user() buffer size is not provably correct")
578__copy_to_user_overflow(void) __asm__("copy_from_user_overflow");
579#define __copy_to_user_overflow(size, count) __copy_to_user_overflow()
580
3df7b41a
JB
581#else
582
583static inline void
584__copy_from_user_overflow(int size, unsigned long count)
585{
586 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
587}
588
7a3d9b0f
JB
589#define __copy_to_user_overflow __copy_from_user_overflow
590
3df7b41a
JB
591#endif
592
593static inline unsigned long __must_check
594copy_from_user(void *to, const void __user *from, unsigned long n)
595{
596 int sz = __compiletime_object_size(to);
597
598 might_fault();
599
600 /*
601 * While we would like to have the compiler do the checking for us
602 * even in the non-constant size case, any false positives there are
603 * a problem (especially when DEBUG_STRICT_USER_COPY_CHECKS, but even
604 * without - the [hopefully] dangerous looking nature of the warning
605 * would make people go look at the respecitive call sites over and
606 * over again just to find that there's no problem).
607 *
608 * And there are cases where it's just not realistic for the compiler
609 * to prove the count to be in range. For example when multiple call
610 * sites of a helper function - perhaps in different source files -
611 * all doing proper range checking, yet the helper function not doing
612 * so again.
613 *
614 * Therefore limit the compile time checking to the constant size
615 * case, and do only runtime checking for non-constant sizes.
616 */
617
618 if (likely(sz < 0 || sz >= n))
619 n = _copy_from_user(to, from, n);
620 else if(__builtin_constant_p(n))
621 copy_from_user_overflow();
622 else
623 __copy_from_user_overflow(sz, n);
624
625 return n;
626}
627
7a3d9b0f
JB
628static inline unsigned long __must_check
629copy_to_user(void __user *to, const void *from, unsigned long n)
630{
631 int sz = __compiletime_object_size(from);
632
633 might_fault();
634
635 /* See the comment in copy_from_user() above. */
636 if (likely(sz < 0 || sz >= n))
637 n = _copy_to_user(to, from, n);
638 else if(__builtin_constant_p(n))
639 copy_to_user_overflow();
640 else
641 __copy_to_user_overflow(sz, n);
642
643 return n;
644}
645
3df7b41a 646#undef __copy_from_user_overflow
7a3d9b0f 647#undef __copy_to_user_overflow
3df7b41a 648
1965aae3 649#endif /* _ASM_X86_UACCESS_H */
8174c430 650