]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/uaccess.h
Merge branch 'qed-fixes'
[mirror_ubuntu-artful-kernel.git] / include / linux / uaccess.h
CommitLineData
c22ce143
HY
1#ifndef __LINUX_UACCESS_H__
2#define __LINUX_UACCESS_H__
3
8bcbde54 4#include <linux/sched.h>
af1d5b37 5#include <linux/thread_info.h>
d597580d 6#include <linux/kasan-checks.h>
5e6039d8
AV
7
8#define VERIFY_READ 0
9#define VERIFY_WRITE 1
10
db68ce10
AV
11#define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
12
c22ce143
HY
13#include <asm/uaccess.h>
14
d597580d
AV
15/*
16 * Architectures should provide two primitives (raw_copy_{to,from}_user())
701cac61
AV
17 * and get rid of their private instances of copy_{to,from}_user() and
18 * __copy_{to,from}_user{,_inatomic}().
d597580d
AV
19 *
20 * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and
21 * return the amount left to copy. They should assume that access_ok() has
22 * already been checked (and succeeded); they should *not* zero-pad anything.
23 * No KASAN or object size checks either - those belong here.
24 *
25 * Both of these functions should attempt to copy size bytes starting at from
26 * into the area starting at to. They must not fetch or store anything
27 * outside of those areas. Return value must be between 0 (everything
28 * copied successfully) and size (nothing copied).
29 *
30 * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting
31 * at to must become equal to the bytes fetched from the corresponding area
32 * starting at from. All data past to + size - N must be left unmodified.
33 *
34 * If copying succeeds, the return value must be 0. If some data cannot be
35 * fetched, it is permitted to copy less than had been fetched; the only
36 * hard requirement is that not storing anything at all (i.e. returning size)
37 * should happen only when nothing could be copied. In other words, you don't
38 * have to squeeze as much as possible - it is allowed, but not necessary.
39 *
40 * For raw_copy_from_user() to always points to kernel memory and no faults
41 * on store should happen. Interpretation of from is affected by set_fs().
42 * For raw_copy_to_user() it's the other way round.
43 *
44 * Both can be inlined - it's up to architectures whether it wants to bother
45 * with that. They should not be used directly; they are used to implement
46 * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic())
47 * that are used instead. Out of those, __... ones are inlined. Plain
48 * copy_{to,from}_user() might or might not be inlined. If you want them
49 * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER.
50 *
51 * NOTE: only copy_from_user() zero-pads the destination in case of short copy.
52 * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything
53 * at all; their callers absolutely must check the return value.
54 *
55 * Biarch ones should also provide raw_copy_in_user() - similar to the above,
56 * but both source and destination are __user pointers (affected by set_fs()
57 * as usual) and both source and destination can trigger faults.
58 */
59
60static __always_inline unsigned long
61__copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
62{
63 kasan_check_write(to, n);
64 check_object_size(to, n, false);
65 return raw_copy_from_user(to, from, n);
66}
67
68static __always_inline unsigned long
69__copy_from_user(void *to, const void __user *from, unsigned long n)
70{
71 might_fault();
72 kasan_check_write(to, n);
73 check_object_size(to, n, false);
74 return raw_copy_from_user(to, from, n);
75}
76
77/**
78 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
79 * @to: Destination address, in user space.
80 * @from: Source address, in kernel space.
81 * @n: Number of bytes to copy.
82 *
83 * Context: User context only.
84 *
85 * Copy data from kernel space to user space. Caller must check
86 * the specified block with access_ok() before calling this function.
87 * The caller should also make sure he pins the user space address
88 * so that we don't result in page fault and sleep.
89 */
90static __always_inline unsigned long
91__copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
92{
93 kasan_check_read(from, n);
94 check_object_size(from, n, true);
95 return raw_copy_to_user(to, from, n);
96}
97
98static __always_inline unsigned long
99__copy_to_user(void __user *to, const void *from, unsigned long n)
100{
101 might_fault();
102 kasan_check_read(from, n);
103 check_object_size(from, n, true);
104 return raw_copy_to_user(to, from, n);
105}
106
107#ifdef INLINE_COPY_FROM_USER
108static inline unsigned long
109_copy_from_user(void *to, const void __user *from, unsigned long n)
110{
111 unsigned long res = n;
112 if (likely(access_ok(VERIFY_READ, from, n)))
113 res = raw_copy_from_user(to, from, n);
114 if (unlikely(res))
115 memset(to + (n - res), 0, res);
116 return res;
117}
118#else
119extern unsigned long
120_copy_from_user(void *, const void __user *, unsigned long);
121#endif
122
123#ifdef INLINE_COPY_TO_USER
124static inline unsigned long
125_copy_to_user(void __user *to, const void *from, unsigned long n)
126{
127 if (access_ok(VERIFY_WRITE, to, n))
128 n = raw_copy_to_user(to, from, n);
129 return n;
130}
131#else
132extern unsigned long
133_copy_to_user(void __user *, const void *, unsigned long);
134#endif
135
136extern void __compiletime_error("usercopy buffer size is too small")
137__bad_copy_user(void);
138
139static inline void copy_user_overflow(int size, unsigned long count)
140{
141 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
142}
143
144static __always_inline unsigned long __must_check
145copy_from_user(void *to, const void __user *from, unsigned long n)
146{
147 int sz = __compiletime_object_size(to);
148
149 might_fault();
150 kasan_check_write(to, n);
151
152 if (likely(sz < 0 || sz >= n)) {
153 check_object_size(to, n, false);
154 n = _copy_from_user(to, from, n);
155 } else if (!__builtin_constant_p(n))
156 copy_user_overflow(sz, n);
157 else
158 __bad_copy_user();
159
160 return n;
161}
162
163static __always_inline unsigned long __must_check
164copy_to_user(void __user *to, const void *from, unsigned long n)
165{
166 int sz = __compiletime_object_size(from);
167
168 kasan_check_read(from, n);
169 might_fault();
170
171 if (likely(sz < 0 || sz >= n)) {
172 check_object_size(from, n, true);
173 n = _copy_to_user(to, from, n);
174 } else if (!__builtin_constant_p(n))
175 copy_user_overflow(sz, n);
176 else
177 __bad_copy_user();
178
179 return n;
180}
181#ifdef CONFIG_COMPAT
182static __always_inline unsigned long __must_check
183__copy_in_user(void __user *to, const void *from, unsigned long n)
184{
185 might_fault();
186 return raw_copy_in_user(to, from, n);
187}
188static __always_inline unsigned long __must_check
189copy_in_user(void __user *to, const void *from, unsigned long n)
190{
191 might_fault();
192 if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
193 n = raw_copy_in_user(to, from, n);
194 return n;
195}
196#endif
d597580d 197
8bcbde54
DH
198static __always_inline void pagefault_disabled_inc(void)
199{
200 current->pagefault_disabled++;
201}
202
203static __always_inline void pagefault_disabled_dec(void)
204{
205 current->pagefault_disabled--;
206 WARN_ON(current->pagefault_disabled < 0);
207}
208
a866374a 209/*
8bcbde54
DH
210 * These routines enable/disable the pagefault handler. If disabled, it will
211 * not take any locks and go straight to the fixup table.
212 *
8222dbe2
DH
213 * User access methods will not sleep when called from a pagefault_disabled()
214 * environment.
a866374a
PZ
215 */
216static inline void pagefault_disable(void)
217{
8bcbde54 218 pagefault_disabled_inc();
a866374a
PZ
219 /*
220 * make sure to have issued the store before a pagefault
221 * can hit.
222 */
223 barrier();
224}
225
226static inline void pagefault_enable(void)
227{
228 /*
229 * make sure to issue those last loads/stores before enabling
230 * the pagefault handler again.
231 */
232 barrier();
8bcbde54 233 pagefault_disabled_dec();
a866374a
PZ
234}
235
8bcbde54
DH
236/*
237 * Is the pagefault handler disabled? If so, user access methods will not sleep.
238 */
239#define pagefault_disabled() (current->pagefault_disabled != 0)
240
70ffdb93
DH
241/*
242 * The pagefault handler is in general disabled by pagefault_disable() or
243 * when in irq context (via in_atomic()).
244 *
245 * This function should only be used by the fault handlers. Other users should
246 * stick to pagefault_disabled().
247 * Please NEVER use preempt_disable() to disable the fault handler. With
248 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
249 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
250 */
251#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
252
c22ce143
HY
253#ifndef ARCH_HAS_NOCACHE_UACCESS
254
255static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
256 const void __user *from, unsigned long n)
257{
258 return __copy_from_user_inatomic(to, from, n);
259}
260
c22ce143
HY
261#endif /* ARCH_HAS_NOCACHE_UACCESS */
262
c33fa9f5
IM
263/*
264 * probe_kernel_read(): safely attempt to read from a location
265 * @dst: pointer to the buffer that shall take the data
266 * @src: address to read from
267 * @size: size of the data chunk
268 *
269 * Safely read from address @src to the buffer at @dst. If a kernel fault
270 * happens, handle that and return -EFAULT.
271 */
f29c5041
SR
272extern long probe_kernel_read(void *dst, const void *src, size_t size);
273extern long __probe_kernel_read(void *dst, const void *src, size_t size);
c33fa9f5
IM
274
275/*
276 * probe_kernel_write(): safely attempt to write to a location
277 * @dst: address to write to
278 * @src: pointer to the data that shall be written
279 * @size: size of the data chunk
280 *
281 * Safely write to address @dst from the buffer at @src. If a kernel fault
282 * happens, handle that and return -EFAULT.
283 */
f29c5041
SR
284extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
285extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
c33fa9f5 286
1a6877b9
AS
287extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
288
0ab32b6f
AM
289/**
290 * probe_kernel_address(): safely attempt to read from a location
291 * @addr: address to read from
292 * @retval: read into this variable
293 *
294 * Returns 0 on success, or -EFAULT.
295 */
296#define probe_kernel_address(addr, retval) \
297 probe_kernel_read(&retval, addr, sizeof(retval))
298
5b24a7a2
LT
299#ifndef user_access_begin
300#define user_access_begin() do { } while (0)
301#define user_access_end() do { } while (0)
1bd4403d
LT
302#define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
303#define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
5b24a7a2
LT
304#endif
305
c22ce143 306#endif /* __LINUX_UACCESS_H__ */