Commit | Line | Data |
---|---|---|
12700c17 AB |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef __ASM_GENERIC_ACCESS_OK_H__ | |
3 | #define __ASM_GENERIC_ACCESS_OK_H__ | |
4 | ||
5 | /* | |
6 | * Checking whether a pointer is valid for user space access. | |
7 | * These definitions work on most architectures, but overrides can | |
8 | * be used where necessary. | |
9 | */ | |
10 | ||
11 | /* | |
12 | * architectures with compat tasks have a variable TASK_SIZE and should | |
13 | * override this to a constant. | |
14 | */ | |
15 | #ifndef TASK_SIZE_MAX | |
16 | #define TASK_SIZE_MAX TASK_SIZE | |
17 | #endif | |
18 | ||
19 | #ifndef uaccess_kernel | |
20 | #ifdef CONFIG_SET_FS | |
21 | #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg) | |
22 | #else | |
23 | #define uaccess_kernel() (0) | |
24 | #endif | |
25 | #endif | |
26 | ||
27 | #ifndef user_addr_max | |
28 | #define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE_MAX) | |
29 | #endif | |
30 | ||
31 | #ifndef __access_ok | |
32 | /* | |
33 | * 'size' is a compile-time constant for most callers, so optimize for | |
34 | * this case to turn the check into a single comparison against a constant | |
35 | * limit and catch all possible overflows. | |
36 | * On architectures with separate user address space (m68k, s390, parisc, | |
37 | * sparc64) or those without an MMU, this should always return true. | |
38 | * | |
39 | * This version was originally contributed by Jonas Bonn for the | |
40 | * OpenRISC architecture, and was found to be the most efficient | |
41 | * for constant 'size' and 'limit' values. | |
42 | */ | |
43 | static inline int __access_ok(const void __user *ptr, unsigned long size) | |
44 | { | |
45 | unsigned long limit = user_addr_max(); | |
46 | unsigned long addr = (unsigned long)ptr; | |
47 | ||
48 | if (IS_ENABLED(CONFIG_ALTERNATE_USER_ADDRESS_SPACE) || | |
49 | !IS_ENABLED(CONFIG_MMU)) | |
50 | return true; | |
51 | ||
52 | return (size <= limit) && (addr <= (limit - size)); | |
53 | } | |
54 | #endif | |
55 | ||
56 | #ifndef access_ok | |
57 | #define access_ok(addr, size) likely(__access_ok(addr, size)) | |
58 | #endif | |
59 | ||
60 | #endif |