2 * Based on arch/arm/include/asm/uaccess.h
4 * Copyright (C) 2012 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef __ASM_UACCESS_H
19 #define __ASM_UACCESS_H
22 * User space memory access functions
24 #include <linux/kasan-checks.h>
25 #include <linux/string.h>
26 #include <linux/thread_info.h>
28 #include <asm/alternative.h>
29 #include <asm/cpufeature.h>
30 #include <asm/ptrace.h>
31 #include <asm/sysreg.h>
32 #include <asm/errno.h>
33 #include <asm/memory.h>
34 #include <asm/compiler.h>
37 #define VERIFY_WRITE 1
40 * The exception table consists of pairs of relative offsets: the first
41 * is the relative offset to an instruction that is allowed to fault,
42 * and the second is the relative offset at which the program should
43 * continue. No registers are modified, so it is entirely up to the
44 * continuation code to figure out what to do.
46 * All the routines below use bits of fixup code that are out of line
47 * with the main instruction path. This means when everything is well,
48 * we don't even have to jump over them. Further, they do not intrude
49 * on our cache or tlb entries.
52 struct exception_table_entry
57 #define ARCH_HAS_RELATIVE_EXTABLE
59 extern int fixup_exception(struct pt_regs *regs);
61 #define KERNEL_DS (-1UL)
62 #define get_ds() (KERNEL_DS)
64 #define USER_DS TASK_SIZE_64
65 #define get_fs() (current_thread_info()->addr_limit)
67 static inline void set_fs(mm_segment_t fs)
69 current_thread_info()->addr_limit = fs;
72 * Enable/disable UAO so that copy_to_user() etc can access
73 * kernel memory with the unprivileged instructions.
75 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
76 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
78 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
82 #define segment_eq(a, b) ((a) == (b))
85 * Test whether a block of memory is a valid user space address.
86 * Returns 1 if the range is valid, 0 otherwise.
88 * This is equivalent to the following test:
89 * (u65)addr + (u65)size <= current->addr_limit
91 * This needs 65-bit arithmetic.
93 #define __range_ok(addr, size) \
95 unsigned long flag, roksum; \
96 __chk_user_ptr(addr); \
97 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
98 : "=&r" (flag), "=&r" (roksum) \
99 : "1" (addr), "Ir" (size), \
100 "r" (current_thread_info()->addr_limit) \
105 #define access_ok(type, addr, size) __range_ok(addr, size)
106 #define user_addr_max get_fs
108 #define _ASM_EXTABLE(from, to) \
109 " .pushsection __ex_table, \"a\"\n" \
111 " .long (" #from " - .), (" #to " - .)\n" \
115 * The "__xxx" versions of the user access functions do not verify the address
116 * space - it must have been done previously with a separate "access_ok()"
119 * The "__xxx_error" versions set the third argument to -EFAULT if an error
120 * occurs, and leave it unchanged on success.
122 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
124 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
125 alt_instr " " reg "1, [%2]\n", feature) \
127 " .section .fixup, \"ax\"\n" \
133 _ASM_EXTABLE(1b, 3b) \
134 : "+r" (err), "=&r" (x) \
135 : "r" (addr), "i" (-EFAULT))
137 #define __get_user_err(x, ptr, err) \
139 unsigned long __gu_val; \
140 __chk_user_ptr(ptr); \
141 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
142 CONFIG_ARM64_PAN)); \
143 switch (sizeof(*(ptr))) { \
145 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
146 (err), ARM64_HAS_UAO); \
149 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
150 (err), ARM64_HAS_UAO); \
153 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
154 (err), ARM64_HAS_UAO); \
157 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
158 (err), ARM64_HAS_UAO); \
163 (x) = (__force __typeof__(*(ptr)))__gu_val; \
164 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
165 CONFIG_ARM64_PAN)); \
168 #define __get_user(x, ptr) \
171 __get_user_err((x), (ptr), __gu_err); \
175 #define __get_user_error(x, ptr, err) \
177 __get_user_err((x), (ptr), (err)); \
181 #define __get_user_unaligned __get_user
183 #define get_user(x, ptr) \
185 __typeof__(*(ptr)) __user *__p = (ptr); \
187 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
188 __get_user((x), __p) : \
189 ((x) = 0, -EFAULT); \
192 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
194 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
195 alt_instr " " reg "1, [%2]\n", feature) \
197 " .section .fixup,\"ax\"\n" \
202 _ASM_EXTABLE(1b, 3b) \
204 : "r" (x), "r" (addr), "i" (-EFAULT))
206 #define __put_user_err(x, ptr, err) \
208 __typeof__(*(ptr)) __pu_val = (x); \
209 __chk_user_ptr(ptr); \
210 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_ALT_PAN_NOT_UAO,\
211 CONFIG_ARM64_PAN)); \
212 switch (sizeof(*(ptr))) { \
214 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
215 (err), ARM64_HAS_UAO); \
218 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
219 (err), ARM64_HAS_UAO); \
222 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
223 (err), ARM64_HAS_UAO); \
226 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
227 (err), ARM64_HAS_UAO); \
232 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_ALT_PAN_NOT_UAO,\
233 CONFIG_ARM64_PAN)); \
236 #define __put_user(x, ptr) \
239 __put_user_err((x), (ptr), __pu_err); \
243 #define __put_user_error(x, ptr, err) \
245 __put_user_err((x), (ptr), (err)); \
249 #define __put_user_unaligned __put_user
251 #define put_user(x, ptr) \
253 __typeof__(*(ptr)) __user *__p = (ptr); \
255 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
256 __put_user((x), __p) : \
260 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
261 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
262 extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
263 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
265 static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
267 kasan_check_write(to, n);
268 check_object_size(to, n, false);
269 return __arch_copy_from_user(to, from, n);
272 static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
274 kasan_check_read(from, n);
275 check_object_size(from, n, true);
276 return __arch_copy_to_user(to, from, n);
279 static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
281 kasan_check_write(to, n);
283 if (access_ok(VERIFY_READ, from, n)) {
284 check_object_size(to, n, false);
285 n = __arch_copy_from_user(to, from, n);
286 } else /* security hole - plug it */
291 static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
293 kasan_check_read(from, n);
295 if (access_ok(VERIFY_WRITE, to, n)) {
296 check_object_size(from, n, true);
297 n = __arch_copy_to_user(to, from, n);
302 static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
304 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
305 n = __copy_in_user(to, from, n);
309 #define __copy_to_user_inatomic __copy_to_user
310 #define __copy_from_user_inatomic __copy_from_user
312 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
314 if (access_ok(VERIFY_WRITE, to, n))
315 n = __clear_user(to, n);
319 extern long strncpy_from_user(char *dest, const char __user *src, long count);
321 extern __must_check long strlen_user(const char __user *str);
322 extern __must_check long strnlen_user(const char __user *str, long n);
324 #endif /* __ASM_UACCESS_H */