1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __LINUX_UACCESS_H__
3*4882a593Smuzhiyun #define __LINUX_UACCESS_H__
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/fault-inject-usercopy.h>
6*4882a593Smuzhiyun #include <linux/instrumented.h>
7*4882a593Smuzhiyun #include <linux/minmax.h>
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun #include <linux/thread_info.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <asm/uaccess.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #ifdef CONFIG_SET_FS
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * Force the uaccess routines to be wired up for actual userspace access,
16*4882a593Smuzhiyun * overriding any possible set_fs(KERNEL_DS) still lingering around. Undone
17*4882a593Smuzhiyun * using force_uaccess_end below.
18*4882a593Smuzhiyun */
force_uaccess_begin(void)19*4882a593Smuzhiyun static inline mm_segment_t force_uaccess_begin(void)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun mm_segment_t fs = get_fs();
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun set_fs(USER_DS);
24*4882a593Smuzhiyun return fs;
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun
force_uaccess_end(mm_segment_t oldfs)27*4882a593Smuzhiyun static inline void force_uaccess_end(mm_segment_t oldfs)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun set_fs(oldfs);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun #else /* CONFIG_SET_FS */
32*4882a593Smuzhiyun typedef struct {
33*4882a593Smuzhiyun /* empty dummy */
34*4882a593Smuzhiyun } mm_segment_t;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #ifndef TASK_SIZE_MAX
37*4882a593Smuzhiyun #define TASK_SIZE_MAX TASK_SIZE
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #define uaccess_kernel() (false)
41*4882a593Smuzhiyun #define user_addr_max() (TASK_SIZE_MAX)
42*4882a593Smuzhiyun
force_uaccess_begin(void)43*4882a593Smuzhiyun static inline mm_segment_t force_uaccess_begin(void)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun return (mm_segment_t) { };
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
force_uaccess_end(mm_segment_t oldfs)48*4882a593Smuzhiyun static inline void force_uaccess_end(mm_segment_t oldfs)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun #endif /* CONFIG_SET_FS */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Architectures should provide two primitives (raw_copy_{to,from}_user())
55*4882a593Smuzhiyun * and get rid of their private instances of copy_{to,from}_user() and
56*4882a593Smuzhiyun * __copy_{to,from}_user{,_inatomic}().
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and
59*4882a593Smuzhiyun * return the amount left to copy. They should assume that access_ok() has
60*4882a593Smuzhiyun * already been checked (and succeeded); they should *not* zero-pad anything.
61*4882a593Smuzhiyun * No KASAN or object size checks either - those belong here.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Both of these functions should attempt to copy size bytes starting at from
64*4882a593Smuzhiyun * into the area starting at to. They must not fetch or store anything
65*4882a593Smuzhiyun * outside of those areas. Return value must be between 0 (everything
66*4882a593Smuzhiyun * copied successfully) and size (nothing copied).
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting
69*4882a593Smuzhiyun * at to must become equal to the bytes fetched from the corresponding area
70*4882a593Smuzhiyun * starting at from. All data past to + size - N must be left unmodified.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * If copying succeeds, the return value must be 0. If some data cannot be
73*4882a593Smuzhiyun * fetched, it is permitted to copy less than had been fetched; the only
74*4882a593Smuzhiyun * hard requirement is that not storing anything at all (i.e. returning size)
75*4882a593Smuzhiyun * should happen only when nothing could be copied. In other words, you don't
76*4882a593Smuzhiyun * have to squeeze as much as possible - it is allowed, but not necessary.
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * For raw_copy_from_user() to always points to kernel memory and no faults
79*4882a593Smuzhiyun * on store should happen. Interpretation of from is affected by set_fs().
80*4882a593Smuzhiyun * For raw_copy_to_user() it's the other way round.
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * Both can be inlined - it's up to architectures whether it wants to bother
83*4882a593Smuzhiyun * with that. They should not be used directly; they are used to implement
84*4882a593Smuzhiyun * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic())
85*4882a593Smuzhiyun * that are used instead. Out of those, __... ones are inlined. Plain
86*4882a593Smuzhiyun * copy_{to,from}_user() might or might not be inlined. If you want them
87*4882a593Smuzhiyun * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER.
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * NOTE: only copy_from_user() zero-pads the destination in case of short copy.
90*4882a593Smuzhiyun * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything
91*4882a593Smuzhiyun * at all; their callers absolutely must check the return value.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Biarch ones should also provide raw_copy_in_user() - similar to the above,
94*4882a593Smuzhiyun * but both source and destination are __user pointers (affected by set_fs()
95*4882a593Smuzhiyun * as usual) and both source and destination can trigger faults.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun static __always_inline __must_check unsigned long
__copy_from_user_inatomic(void * to,const void __user * from,unsigned long n)99*4882a593Smuzhiyun __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun instrument_copy_from_user(to, from, n);
102*4882a593Smuzhiyun check_object_size(to, n, false);
103*4882a593Smuzhiyun return raw_copy_from_user(to, from, n);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun static __always_inline __must_check unsigned long
__copy_from_user(void * to,const void __user * from,unsigned long n)107*4882a593Smuzhiyun __copy_from_user(void *to, const void __user *from, unsigned long n)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun might_fault();
110*4882a593Smuzhiyun if (should_fail_usercopy())
111*4882a593Smuzhiyun return n;
112*4882a593Smuzhiyun instrument_copy_from_user(to, from, n);
113*4882a593Smuzhiyun check_object_size(to, n, false);
114*4882a593Smuzhiyun return raw_copy_from_user(to, from, n);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /**
118*4882a593Smuzhiyun * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
119*4882a593Smuzhiyun * @to: Destination address, in user space.
120*4882a593Smuzhiyun * @from: Source address, in kernel space.
121*4882a593Smuzhiyun * @n: Number of bytes to copy.
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * Context: User context only.
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * Copy data from kernel space to user space. Caller must check
126*4882a593Smuzhiyun * the specified block with access_ok() before calling this function.
127*4882a593Smuzhiyun * The caller should also make sure he pins the user space address
128*4882a593Smuzhiyun * so that we don't result in page fault and sleep.
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun static __always_inline __must_check unsigned long
__copy_to_user_inatomic(void __user * to,const void * from,unsigned long n)131*4882a593Smuzhiyun __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun if (should_fail_usercopy())
134*4882a593Smuzhiyun return n;
135*4882a593Smuzhiyun instrument_copy_to_user(to, from, n);
136*4882a593Smuzhiyun check_object_size(from, n, true);
137*4882a593Smuzhiyun return raw_copy_to_user(to, from, n);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun static __always_inline __must_check unsigned long
__copy_to_user(void __user * to,const void * from,unsigned long n)141*4882a593Smuzhiyun __copy_to_user(void __user *to, const void *from, unsigned long n)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun might_fault();
144*4882a593Smuzhiyun if (should_fail_usercopy())
145*4882a593Smuzhiyun return n;
146*4882a593Smuzhiyun instrument_copy_to_user(to, from, n);
147*4882a593Smuzhiyun check_object_size(from, n, true);
148*4882a593Smuzhiyun return raw_copy_to_user(to, from, n);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun #ifdef INLINE_COPY_FROM_USER
152*4882a593Smuzhiyun static inline __must_check unsigned long
_copy_from_user(void * to,const void __user * from,unsigned long n)153*4882a593Smuzhiyun _copy_from_user(void *to, const void __user *from, unsigned long n)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun unsigned long res = n;
156*4882a593Smuzhiyun might_fault();
157*4882a593Smuzhiyun if (!should_fail_usercopy() && likely(access_ok(from, n))) {
158*4882a593Smuzhiyun instrument_copy_from_user(to, from, n);
159*4882a593Smuzhiyun res = raw_copy_from_user(to, from, n);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun if (unlikely(res))
162*4882a593Smuzhiyun memset(to + (n - res), 0, res);
163*4882a593Smuzhiyun return res;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun #else
166*4882a593Smuzhiyun extern __must_check unsigned long
167*4882a593Smuzhiyun _copy_from_user(void *, const void __user *, unsigned long);
168*4882a593Smuzhiyun #endif
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun #ifdef INLINE_COPY_TO_USER
171*4882a593Smuzhiyun static inline __must_check unsigned long
_copy_to_user(void __user * to,const void * from,unsigned long n)172*4882a593Smuzhiyun _copy_to_user(void __user *to, const void *from, unsigned long n)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun might_fault();
175*4882a593Smuzhiyun if (should_fail_usercopy())
176*4882a593Smuzhiyun return n;
177*4882a593Smuzhiyun if (access_ok(to, n)) {
178*4882a593Smuzhiyun instrument_copy_to_user(to, from, n);
179*4882a593Smuzhiyun n = raw_copy_to_user(to, from, n);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun return n;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun #else
184*4882a593Smuzhiyun extern __must_check unsigned long
185*4882a593Smuzhiyun _copy_to_user(void __user *, const void *, unsigned long);
186*4882a593Smuzhiyun #endif
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun static __always_inline unsigned long __must_check
copy_from_user(void * to,const void __user * from,unsigned long n)189*4882a593Smuzhiyun copy_from_user(void *to, const void __user *from, unsigned long n)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun if (likely(check_copy_size(to, n, false)))
192*4882a593Smuzhiyun n = _copy_from_user(to, from, n);
193*4882a593Smuzhiyun return n;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun static __always_inline unsigned long __must_check
copy_to_user(void __user * to,const void * from,unsigned long n)197*4882a593Smuzhiyun copy_to_user(void __user *to, const void *from, unsigned long n)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun if (likely(check_copy_size(from, n, true)))
200*4882a593Smuzhiyun n = _copy_to_user(to, from, n);
201*4882a593Smuzhiyun return n;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
204*4882a593Smuzhiyun static __always_inline unsigned long __must_check
copy_in_user(void __user * to,const void __user * from,unsigned long n)205*4882a593Smuzhiyun copy_in_user(void __user *to, const void __user *from, unsigned long n)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun might_fault();
208*4882a593Smuzhiyun if (access_ok(to, n) && access_ok(from, n))
209*4882a593Smuzhiyun n = raw_copy_in_user(to, from, n);
210*4882a593Smuzhiyun return n;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun #endif
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun #ifndef copy_mc_to_kernel
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * Without arch opt-in this generic copy_mc_to_kernel() will not handle
217*4882a593Smuzhiyun * #MC (or arch equivalent) during source read.
218*4882a593Smuzhiyun */
219*4882a593Smuzhiyun static inline unsigned long __must_check
copy_mc_to_kernel(void * dst,const void * src,size_t cnt)220*4882a593Smuzhiyun copy_mc_to_kernel(void *dst, const void *src, size_t cnt)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun memcpy(dst, src, cnt);
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun #endif
226*4882a593Smuzhiyun
pagefault_disabled_inc(void)227*4882a593Smuzhiyun static __always_inline void pagefault_disabled_inc(void)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun current->pagefault_disabled++;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
pagefault_disabled_dec(void)232*4882a593Smuzhiyun static __always_inline void pagefault_disabled_dec(void)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun current->pagefault_disabled--;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun * These routines enable/disable the pagefault handler. If disabled, it will
239*4882a593Smuzhiyun * not take any locks and go straight to the fixup table.
240*4882a593Smuzhiyun *
241*4882a593Smuzhiyun * User access methods will not sleep when called from a pagefault_disabled()
242*4882a593Smuzhiyun * environment.
243*4882a593Smuzhiyun */
pagefault_disable(void)244*4882a593Smuzhiyun static inline void pagefault_disable(void)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun pagefault_disabled_inc();
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * make sure to have issued the store before a pagefault
249*4882a593Smuzhiyun * can hit.
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun barrier();
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
pagefault_enable(void)254*4882a593Smuzhiyun static inline void pagefault_enable(void)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun /*
257*4882a593Smuzhiyun * make sure to issue those last loads/stores before enabling
258*4882a593Smuzhiyun * the pagefault handler again.
259*4882a593Smuzhiyun */
260*4882a593Smuzhiyun barrier();
261*4882a593Smuzhiyun pagefault_disabled_dec();
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /*
265*4882a593Smuzhiyun * Is the pagefault handler disabled? If so, user access methods will not sleep.
266*4882a593Smuzhiyun */
pagefault_disabled(void)267*4882a593Smuzhiyun static inline bool pagefault_disabled(void)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun return current->pagefault_disabled != 0;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun * The pagefault handler is in general disabled by pagefault_disable() or
274*4882a593Smuzhiyun * when in irq context (via in_atomic()).
275*4882a593Smuzhiyun *
276*4882a593Smuzhiyun * This function should only be used by the fault handlers. Other users should
277*4882a593Smuzhiyun * stick to pagefault_disabled().
278*4882a593Smuzhiyun * Please NEVER use preempt_disable() to disable the fault handler. With
279*4882a593Smuzhiyun * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
280*4882a593Smuzhiyun * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun #ifndef ARCH_HAS_NOCACHE_UACCESS
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun static inline __must_check unsigned long
__copy_from_user_inatomic_nocache(void * to,const void __user * from,unsigned long n)287*4882a593Smuzhiyun __copy_from_user_inatomic_nocache(void *to, const void __user *from,
288*4882a593Smuzhiyun unsigned long n)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun return __copy_from_user_inatomic(to, from, n);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun #endif /* ARCH_HAS_NOCACHE_UACCESS */
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun extern __must_check int check_zeroed_user(const void __user *from, size_t size);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /**
298*4882a593Smuzhiyun * copy_struct_from_user: copy a struct from userspace
299*4882a593Smuzhiyun * @dst: Destination address, in kernel space. This buffer must be @ksize
300*4882a593Smuzhiyun * bytes long.
301*4882a593Smuzhiyun * @ksize: Size of @dst struct.
302*4882a593Smuzhiyun * @src: Source address, in userspace.
303*4882a593Smuzhiyun * @usize: (Alleged) size of @src struct.
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * Copies a struct from userspace to kernel space, in a way that guarantees
306*4882a593Smuzhiyun * backwards-compatibility for struct syscall arguments (as long as future
307*4882a593Smuzhiyun * struct extensions are made such that all new fields are *appended* to the
308*4882a593Smuzhiyun * old struct, and zeroed-out new fields have the same meaning as the old
309*4882a593Smuzhiyun * struct).
310*4882a593Smuzhiyun *
311*4882a593Smuzhiyun * @ksize is just sizeof(*dst), and @usize should've been passed by userspace.
312*4882a593Smuzhiyun * The recommended usage is something like the following:
313*4882a593Smuzhiyun *
314*4882a593Smuzhiyun * SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize)
315*4882a593Smuzhiyun * {
316*4882a593Smuzhiyun * int err;
317*4882a593Smuzhiyun * struct foo karg = {};
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * if (usize > PAGE_SIZE)
320*4882a593Smuzhiyun * return -E2BIG;
321*4882a593Smuzhiyun * if (usize < FOO_SIZE_VER0)
322*4882a593Smuzhiyun * return -EINVAL;
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
325*4882a593Smuzhiyun * if (err)
326*4882a593Smuzhiyun * return err;
327*4882a593Smuzhiyun *
328*4882a593Smuzhiyun * // ...
329*4882a593Smuzhiyun * }
330*4882a593Smuzhiyun *
331*4882a593Smuzhiyun * There are three cases to consider:
332*4882a593Smuzhiyun * * If @usize == @ksize, then it's copied verbatim.
333*4882a593Smuzhiyun * * If @usize < @ksize, then the userspace has passed an old struct to a
334*4882a593Smuzhiyun * newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize)
335*4882a593Smuzhiyun * are to be zero-filled.
336*4882a593Smuzhiyun * * If @usize > @ksize, then the userspace has passed a new struct to an
337*4882a593Smuzhiyun * older kernel. The trailing bytes unknown to the kernel (@usize - @ksize)
338*4882a593Smuzhiyun * are checked to ensure they are zeroed, otherwise -E2BIG is returned.
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * Returns (in all cases, some data may have been copied):
341*4882a593Smuzhiyun * * -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src.
342*4882a593Smuzhiyun * * -EFAULT: access to userspace failed.
343*4882a593Smuzhiyun */
344*4882a593Smuzhiyun static __always_inline __must_check int
copy_struct_from_user(void * dst,size_t ksize,const void __user * src,size_t usize)345*4882a593Smuzhiyun copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
346*4882a593Smuzhiyun size_t usize)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun size_t size = min(ksize, usize);
349*4882a593Smuzhiyun size_t rest = max(ksize, usize) - size;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Deal with trailing bytes. */
352*4882a593Smuzhiyun if (usize < ksize) {
353*4882a593Smuzhiyun memset(dst + size, 0, rest);
354*4882a593Smuzhiyun } else if (usize > ksize) {
355*4882a593Smuzhiyun int ret = check_zeroed_user(src + size, rest);
356*4882a593Smuzhiyun if (ret <= 0)
357*4882a593Smuzhiyun return ret ?: -E2BIG;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun /* Copy the interoperable parts of the struct. */
360*4882a593Smuzhiyun if (copy_from_user(dst, src, size))
361*4882a593Smuzhiyun return -EFAULT;
362*4882a593Smuzhiyun return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
368*4882a593Smuzhiyun long notrace copy_to_kernel_nofault(void *dst, const void *src, size_t size);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun long copy_from_user_nofault(void *dst, const void __user *src, size_t size);
371*4882a593Smuzhiyun long notrace copy_to_user_nofault(void __user *dst, const void *src,
372*4882a593Smuzhiyun size_t size);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr,
375*4882a593Smuzhiyun long count);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
378*4882a593Smuzhiyun long count);
379*4882a593Smuzhiyun long strnlen_user_nofault(const void __user *unsafe_addr, long count);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun * get_kernel_nofault(): safely attempt to read from a location
383*4882a593Smuzhiyun * @val: read into this variable
384*4882a593Smuzhiyun * @ptr: address to read from
385*4882a593Smuzhiyun *
386*4882a593Smuzhiyun * Returns 0 on success, or -EFAULT.
387*4882a593Smuzhiyun */
388*4882a593Smuzhiyun #define get_kernel_nofault(val, ptr) ({ \
389*4882a593Smuzhiyun const typeof(val) *__gk_ptr = (ptr); \
390*4882a593Smuzhiyun copy_from_kernel_nofault(&(val), __gk_ptr, sizeof(val));\
391*4882a593Smuzhiyun })
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun #ifndef user_access_begin
394*4882a593Smuzhiyun #define user_access_begin(ptr,len) access_ok(ptr, len)
395*4882a593Smuzhiyun #define user_access_end() do { } while (0)
396*4882a593Smuzhiyun #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
397*4882a593Smuzhiyun #define unsafe_get_user(x,p,e) unsafe_op_wrap(__get_user(x,p),e)
398*4882a593Smuzhiyun #define unsafe_put_user(x,p,e) unsafe_op_wrap(__put_user(x,p),e)
399*4882a593Smuzhiyun #define unsafe_copy_to_user(d,s,l,e) unsafe_op_wrap(__copy_to_user(d,s,l),e)
user_access_save(void)400*4882a593Smuzhiyun static inline unsigned long user_access_save(void) { return 0UL; }
user_access_restore(unsigned long flags)401*4882a593Smuzhiyun static inline void user_access_restore(unsigned long flags) { }
402*4882a593Smuzhiyun #endif
403*4882a593Smuzhiyun #ifndef user_write_access_begin
404*4882a593Smuzhiyun #define user_write_access_begin user_access_begin
405*4882a593Smuzhiyun #define user_write_access_end user_access_end
406*4882a593Smuzhiyun #endif
407*4882a593Smuzhiyun #ifndef user_read_access_begin
408*4882a593Smuzhiyun #define user_read_access_begin user_access_begin
409*4882a593Smuzhiyun #define user_read_access_end user_access_end
410*4882a593Smuzhiyun #endif
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun #ifdef CONFIG_HARDENED_USERCOPY
413*4882a593Smuzhiyun void usercopy_warn(const char *name, const char *detail, bool to_user,
414*4882a593Smuzhiyun unsigned long offset, unsigned long len);
415*4882a593Smuzhiyun void __noreturn usercopy_abort(const char *name, const char *detail,
416*4882a593Smuzhiyun bool to_user, unsigned long offset,
417*4882a593Smuzhiyun unsigned long len);
418*4882a593Smuzhiyun #endif
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun #endif /* __LINUX_UACCESS_H__ */
421