1
2#ifndef __LINUX_UACCESS_H__
3#define __LINUX_UACCESS_H__
4
5#include <linux/sched.h>
6#include <linux/thread_info.h>
7#include <linux/kasan-checks.h>
8
9#define VERIFY_READ 0
10#define VERIFY_WRITE 1
11
12#define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
13
14#include <asm/uaccess.h>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61static __always_inline unsigned long
62__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
63{
64 kasan_check_write(to, n);
65 check_object_size(to, n, false);
66 return raw_copy_from_user(to, from, n);
67}
68
69static __always_inline unsigned long
70__copy_from_user(void *to, const void __user *from, unsigned long n)
71{
72 might_fault();
73 kasan_check_write(to, n);
74 check_object_size(to, n, false);
75 return raw_copy_from_user(to, from, n);
76}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91static __always_inline unsigned long
92__copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
93{
94 kasan_check_read(from, n);
95 check_object_size(from, n, true);
96 return raw_copy_to_user(to, from, n);
97}
98
99static __always_inline unsigned long
100__copy_to_user(void __user *to, const void *from, unsigned long n)
101{
102 might_fault();
103 kasan_check_read(from, n);
104 check_object_size(from, n, true);
105 return raw_copy_to_user(to, from, n);
106}
107
108#ifdef INLINE_COPY_FROM_USER
109static inline unsigned long
110_copy_from_user(void *to, const void __user *from, unsigned long n)
111{
112 unsigned long res = n;
113 might_fault();
114 if (likely(access_ok(VERIFY_READ, from, n))) {
115 kasan_check_write(to, n);
116 res = raw_copy_from_user(to, from, n);
117 }
118 if (unlikely(res))
119 memset(to + (n - res), 0, res);
120 return res;
121}
122#else
123extern unsigned long
124_copy_from_user(void *, const void __user *, unsigned long);
125#endif
126
127#ifdef INLINE_COPY_TO_USER
128static inline unsigned long
129_copy_to_user(void __user *to, const void *from, unsigned long n)
130{
131 might_fault();
132 if (access_ok(VERIFY_WRITE, to, n)) {
133 kasan_check_read(from, n);
134 n = raw_copy_to_user(to, from, n);
135 }
136 return n;
137}
138#else
139extern unsigned long
140_copy_to_user(void __user *, const void *, unsigned long);
141#endif
142
143static __always_inline unsigned long __must_check
144copy_from_user(void *to, const void __user *from, unsigned long n)
145{
146 if (likely(check_copy_size(to, n, false)))
147 n = _copy_from_user(to, from, n);
148 return n;
149}
150
151static __always_inline unsigned long __must_check
152copy_to_user(void __user *to, const void *from, unsigned long n)
153{
154 if (likely(check_copy_size(from, n, true)))
155 n = _copy_to_user(to, from, n);
156 return n;
157}
158#ifdef CONFIG_COMPAT
159static __always_inline unsigned long __must_check
160copy_in_user(void __user *to, const void __user *from, unsigned long n)
161{
162 might_fault();
163 if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
164 n = raw_copy_in_user(to, from, n);
165 return n;
166}
167#endif
168
169static __always_inline void pagefault_disabled_inc(void)
170{
171 current->pagefault_disabled++;
172}
173
174static __always_inline void pagefault_disabled_dec(void)
175{
176 current->pagefault_disabled--;
177}
178
179
180
181
182
183
184
185
186static inline void pagefault_disable(void)
187{
188 pagefault_disabled_inc();
189
190
191
192
193 barrier();
194}
195
196static inline void pagefault_enable(void)
197{
198
199
200
201
202 barrier();
203 pagefault_disabled_dec();
204}
205
206
207
208
209#define pagefault_disabled() (current->pagefault_disabled != 0)
210
211
212
213
214
215
216
217
218
219
220
221#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
222
223#ifndef ARCH_HAS_NOCACHE_UACCESS
224
225static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
226 const void __user *from, unsigned long n)
227{
228 return __copy_from_user_inatomic(to, from, n);
229}
230
231#endif
232
233
234
235
236
237
238
239
240
241
242extern long probe_kernel_read(void *dst, const void *src, size_t size);
243extern long __probe_kernel_read(void *dst, const void *src, size_t size);
244
245
246
247
248
249
250
251
252
253
254extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
255extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
256
257extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
258
259
260
261
262
263
264
265
266#define probe_kernel_address(addr, retval) \
267 probe_kernel_read(&retval, addr, sizeof(retval))
268
269#ifndef user_access_begin
270#define user_access_begin() do { } while (0)
271#define user_access_end() do { } while (0)
272#define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
273#define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
274#endif
275
276#ifdef CONFIG_HARDENED_USERCOPY
277void usercopy_warn(const char *name, const char *detail, bool to_user,
278 unsigned long offset, unsigned long len);
279void __noreturn usercopy_abort(const char *name, const char *detail,
280 bool to_user, unsigned long offset,
281 unsigned long len);
282#endif
283
284#endif
285