xref: /linux/include/linux/uaccess.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c22ce143SHiro Yoshioka #ifndef __LINUX_UACCESS_H__
3c22ce143SHiro Yoshioka #define __LINUX_UACCESS_H__
4c22ce143SHiro Yoshioka 
54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h>
676d6f06cSMarco Elver #include <linux/instrumented.h>
7b296a6d5SAndy Shevchenko #include <linux/minmax.h>
88bcbde54SDavid Hildenbrand #include <linux/sched.h>
9af1d5b37SAl Viro #include <linux/thread_info.h>
105e6039d8SAl Viro 
11c22ce143SHiro Yoshioka #include <asm/uaccess.h>
12c22ce143SHiro Yoshioka 
133d13f313SChristoph Hellwig /*
14*428e106aSKirill A. Shutemov  * Architectures that support memory tagging (assigning tags to memory regions,
15*428e106aSKirill A. Shutemov  * embedding these tags into addresses that point to these memory regions, and
16*428e106aSKirill A. Shutemov  * checking that the memory and the pointer tags match on memory accesses)
17*428e106aSKirill A. Shutemov  * redefine this macro to strip tags from pointers.
18*428e106aSKirill A. Shutemov  *
19*428e106aSKirill A. Shutemov  * Passing down mm_struct allows to define untagging rules on per-process
20*428e106aSKirill A. Shutemov  * basis.
21*428e106aSKirill A. Shutemov  *
22*428e106aSKirill A. Shutemov  * It's defined as noop for architectures that don't support memory tagging.
23*428e106aSKirill A. Shutemov  */
24*428e106aSKirill A. Shutemov #ifndef untagged_addr
25*428e106aSKirill A. Shutemov #define untagged_addr(addr) (addr)
26*428e106aSKirill A. Shutemov #endif
27*428e106aSKirill A. Shutemov 
28*428e106aSKirill A. Shutemov #ifndef untagged_addr_remote
29*428e106aSKirill A. Shutemov #define untagged_addr_remote(mm, addr)	({		\
30*428e106aSKirill A. Shutemov 	mmap_assert_locked(mm);				\
31*428e106aSKirill A. Shutemov 	untagged_addr(addr);				\
32*428e106aSKirill A. Shutemov })
33*428e106aSKirill A. Shutemov #endif
34*428e106aSKirill A. Shutemov 
35*428e106aSKirill A. Shutemov /*
36d597580dSAl Viro  * Architectures should provide two primitives (raw_copy_{to,from}_user())
37701cac61SAl Viro  * and get rid of their private instances of copy_{to,from}_user() and
38701cac61SAl Viro  * __copy_{to,from}_user{,_inatomic}().
39d597580dSAl Viro  *
40d597580dSAl Viro  * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and
41d597580dSAl Viro  * return the amount left to copy.  They should assume that access_ok() has
42d597580dSAl Viro  * already been checked (and succeeded); they should *not* zero-pad anything.
43d597580dSAl Viro  * No KASAN or object size checks either - those belong here.
44d597580dSAl Viro  *
45d597580dSAl Viro  * Both of these functions should attempt to copy size bytes starting at from
46d597580dSAl Viro  * into the area starting at to.  They must not fetch or store anything
47d597580dSAl Viro  * outside of those areas.  Return value must be between 0 (everything
48d597580dSAl Viro  * copied successfully) and size (nothing copied).
49d597580dSAl Viro  *
50d597580dSAl Viro  * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting
51d597580dSAl Viro  * at to must become equal to the bytes fetched from the corresponding area
52d597580dSAl Viro  * starting at from.  All data past to + size - N must be left unmodified.
53d597580dSAl Viro  *
54d597580dSAl Viro  * If copying succeeds, the return value must be 0.  If some data cannot be
55d597580dSAl Viro  * fetched, it is permitted to copy less than had been fetched; the only
56d597580dSAl Viro  * hard requirement is that not storing anything at all (i.e. returning size)
57d597580dSAl Viro  * should happen only when nothing could be copied.  In other words, you don't
58d597580dSAl Viro  * have to squeeze as much as possible - it is allowed, but not necessary.
59d597580dSAl Viro  *
60d597580dSAl Viro  * For raw_copy_from_user() to always points to kernel memory and no faults
61d597580dSAl Viro  * on store should happen.  Interpretation of from is affected by set_fs().
62d597580dSAl Viro  * For raw_copy_to_user() it's the other way round.
63d597580dSAl Viro  *
64d597580dSAl Viro  * Both can be inlined - it's up to architectures whether it wants to bother
65d597580dSAl Viro  * with that.  They should not be used directly; they are used to implement
66d597580dSAl Viro  * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic())
67d597580dSAl Viro  * that are used instead.  Out of those, __... ones are inlined.  Plain
68d597580dSAl Viro  * copy_{to,from}_user() might or might not be inlined.  If you want them
69d597580dSAl Viro  * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER.
70d597580dSAl Viro  *
71d597580dSAl Viro  * NOTE: only copy_from_user() zero-pads the destination in case of short copy.
72d597580dSAl Viro  * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything
73d597580dSAl Viro  * at all; their callers absolutely must check the return value.
74d597580dSAl Viro  *
75d597580dSAl Viro  * Biarch ones should also provide raw_copy_in_user() - similar to the above,
76d597580dSAl Viro  * but both source and destination are __user pointers (affected by set_fs()
77d597580dSAl Viro  * as usual) and both source and destination can trigger faults.
78d597580dSAl Viro  */
79d597580dSAl Viro 
809dd819a1SKees Cook static __always_inline __must_check unsigned long
__copy_from_user_inatomic(void * to,const void __user * from,unsigned long n)81d597580dSAl Viro __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
82d597580dSAl Viro {
8333b75c1dSAlexander Potapenko 	unsigned long res;
8433b75c1dSAlexander Potapenko 
8533b75c1dSAlexander Potapenko 	instrument_copy_from_user_before(to, from, n);
86d597580dSAl Viro 	check_object_size(to, n, false);
8733b75c1dSAlexander Potapenko 	res = raw_copy_from_user(to, from, n);
8833b75c1dSAlexander Potapenko 	instrument_copy_from_user_after(to, from, n, res);
8933b75c1dSAlexander Potapenko 	return res;
90d597580dSAl Viro }
91d597580dSAl Viro 
929dd819a1SKees Cook static __always_inline __must_check unsigned long
__copy_from_user(void * to,const void __user * from,unsigned long n)93d597580dSAl Viro __copy_from_user(void *to, const void __user *from, unsigned long n)
94d597580dSAl Viro {
9533b75c1dSAlexander Potapenko 	unsigned long res;
9633b75c1dSAlexander Potapenko 
97d597580dSAl Viro 	might_fault();
9833b75c1dSAlexander Potapenko 	instrument_copy_from_user_before(to, from, n);
994d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1004d0e9df5SAlbert van der Linde 		return n;
101d597580dSAl Viro 	check_object_size(to, n, false);
10233b75c1dSAlexander Potapenko 	res = raw_copy_from_user(to, from, n);
10333b75c1dSAlexander Potapenko 	instrument_copy_from_user_after(to, from, n, res);
10433b75c1dSAlexander Potapenko 	return res;
105d597580dSAl Viro }
106d597580dSAl Viro 
107d597580dSAl Viro /**
108d597580dSAl Viro  * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
109d597580dSAl Viro  * @to:   Destination address, in user space.
110d597580dSAl Viro  * @from: Source address, in kernel space.
111d597580dSAl Viro  * @n:    Number of bytes to copy.
112d597580dSAl Viro  *
113d597580dSAl Viro  * Context: User context only.
114d597580dSAl Viro  *
115d597580dSAl Viro  * Copy data from kernel space to user space.  Caller must check
116d597580dSAl Viro  * the specified block with access_ok() before calling this function.
117d597580dSAl Viro  * The caller should also make sure he pins the user space address
118d597580dSAl Viro  * so that we don't result in page fault and sleep.
119d597580dSAl Viro  */
1209dd819a1SKees Cook static __always_inline __must_check unsigned long
__copy_to_user_inatomic(void __user * to,const void * from,unsigned long n)121d597580dSAl Viro __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
122d597580dSAl Viro {
1234d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1244d0e9df5SAlbert van der Linde 		return n;
12576d6f06cSMarco Elver 	instrument_copy_to_user(to, from, n);
126d597580dSAl Viro 	check_object_size(from, n, true);
127d597580dSAl Viro 	return raw_copy_to_user(to, from, n);
128d597580dSAl Viro }
129d597580dSAl Viro 
1309dd819a1SKees Cook static __always_inline __must_check unsigned long
__copy_to_user(void __user * to,const void * from,unsigned long n)131d597580dSAl Viro __copy_to_user(void __user *to, const void *from, unsigned long n)
132d597580dSAl Viro {
133d597580dSAl Viro 	might_fault();
1344d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1354d0e9df5SAlbert van der Linde 		return n;
13676d6f06cSMarco Elver 	instrument_copy_to_user(to, from, n);
137d597580dSAl Viro 	check_object_size(from, n, true);
138d597580dSAl Viro 	return raw_copy_to_user(to, from, n);
139d597580dSAl Viro }
140d597580dSAl Viro 
141d597580dSAl Viro #ifdef INLINE_COPY_FROM_USER
1429dd819a1SKees Cook static inline __must_check unsigned long
_copy_from_user(void * to,const void __user * from,unsigned long n)143d597580dSAl Viro _copy_from_user(void *to, const void __user *from, unsigned long n)
144d597580dSAl Viro {
145d597580dSAl Viro 	unsigned long res = n;
1469c5f6908SAl Viro 	might_fault();
1474d0e9df5SAlbert van der Linde 	if (!should_fail_usercopy() && likely(access_ok(from, n))) {
14833b75c1dSAlexander Potapenko 		instrument_copy_from_user_before(to, from, n);
149d597580dSAl Viro 		res = raw_copy_from_user(to, from, n);
15033b75c1dSAlexander Potapenko 		instrument_copy_from_user_after(to, from, n, res);
1519c5f6908SAl Viro 	}
152d597580dSAl Viro 	if (unlikely(res))
153d597580dSAl Viro 		memset(to + (n - res), 0, res);
154d597580dSAl Viro 	return res;
155d597580dSAl Viro }
156d597580dSAl Viro #else
1579dd819a1SKees Cook extern __must_check unsigned long
158d597580dSAl Viro _copy_from_user(void *, const void __user *, unsigned long);
159d597580dSAl Viro #endif
160d597580dSAl Viro 
161d597580dSAl Viro #ifdef INLINE_COPY_TO_USER
1629dd819a1SKees Cook static inline __must_check unsigned long
_copy_to_user(void __user * to,const void * from,unsigned long n)163d597580dSAl Viro _copy_to_user(void __user *to, const void *from, unsigned long n)
164d597580dSAl Viro {
1659c5f6908SAl Viro 	might_fault();
1664d0e9df5SAlbert van der Linde 	if (should_fail_usercopy())
1674d0e9df5SAlbert van der Linde 		return n;
16896d4f267SLinus Torvalds 	if (access_ok(to, n)) {
16976d6f06cSMarco Elver 		instrument_copy_to_user(to, from, n);
170d597580dSAl Viro 		n = raw_copy_to_user(to, from, n);
1719c5f6908SAl Viro 	}
172d597580dSAl Viro 	return n;
173d597580dSAl Viro }
174d597580dSAl Viro #else
1759dd819a1SKees Cook extern __must_check unsigned long
176d597580dSAl Viro _copy_to_user(void __user *, const void *, unsigned long);
177d597580dSAl Viro #endif
178d597580dSAl Viro 
179d597580dSAl Viro static __always_inline unsigned long __must_check
copy_from_user(void * to,const void __user * from,unsigned long n)180d597580dSAl Viro copy_from_user(void *to, const void __user *from, unsigned long n)
181d597580dSAl Viro {
1820e3c3b90SAl Viro 	if (check_copy_size(to, n, false))
183d597580dSAl Viro 		n = _copy_from_user(to, from, n);
184d597580dSAl Viro 	return n;
185d597580dSAl Viro }
186d597580dSAl Viro 
187d597580dSAl Viro static __always_inline unsigned long __must_check
copy_to_user(void __user * to,const void * from,unsigned long n)188d597580dSAl Viro copy_to_user(void __user *to, const void *from, unsigned long n)
189d597580dSAl Viro {
1900e3c3b90SAl Viro 	if (check_copy_size(from, n, true))
191d597580dSAl Viro 		n = _copy_to_user(to, from, n);
192d597580dSAl Viro 	return n;
193d597580dSAl Viro }
194d597580dSAl Viro 
195ec6347bbSDan Williams #ifndef copy_mc_to_kernel
196ec6347bbSDan Williams /*
197ec6347bbSDan Williams  * Without arch opt-in this generic copy_mc_to_kernel() will not handle
198ec6347bbSDan Williams  * #MC (or arch equivalent) during source read.
199ec6347bbSDan Williams  */
200ec6347bbSDan Williams static inline unsigned long __must_check
copy_mc_to_kernel(void * dst,const void * src,size_t cnt)201ec6347bbSDan Williams copy_mc_to_kernel(void *dst, const void *src, size_t cnt)
202ec6347bbSDan Williams {
203ec6347bbSDan Williams 	memcpy(dst, src, cnt);
204ec6347bbSDan Williams 	return 0;
205ec6347bbSDan Williams }
206ec6347bbSDan Williams #endif
207ec6347bbSDan Williams 
pagefault_disabled_inc(void)2088bcbde54SDavid Hildenbrand static __always_inline void pagefault_disabled_inc(void)
2098bcbde54SDavid Hildenbrand {
2108bcbde54SDavid Hildenbrand 	current->pagefault_disabled++;
2118bcbde54SDavid Hildenbrand }
2128bcbde54SDavid Hildenbrand 
pagefault_disabled_dec(void)2138bcbde54SDavid Hildenbrand static __always_inline void pagefault_disabled_dec(void)
2148bcbde54SDavid Hildenbrand {
2158bcbde54SDavid Hildenbrand 	current->pagefault_disabled--;
2168bcbde54SDavid Hildenbrand }
2178bcbde54SDavid Hildenbrand 
218a866374aSPeter Zijlstra /*
2198bcbde54SDavid Hildenbrand  * These routines enable/disable the pagefault handler. If disabled, it will
2208bcbde54SDavid Hildenbrand  * not take any locks and go straight to the fixup table.
221a866374aSPeter Zijlstra  *
2228222dbe2SDavid Hildenbrand  * User access methods will not sleep when called from a pagefault_disabled()
2238222dbe2SDavid Hildenbrand  * environment.
224a866374aSPeter Zijlstra  */
pagefault_disable(void)225a866374aSPeter Zijlstra static inline void pagefault_disable(void)
226a866374aSPeter Zijlstra {
2278bcbde54SDavid Hildenbrand 	pagefault_disabled_inc();
228a866374aSPeter Zijlstra 	/*
229a866374aSPeter Zijlstra 	 * make sure to have issued the store before a pagefault
230a866374aSPeter Zijlstra 	 * can hit.
231a866374aSPeter Zijlstra 	 */
232a866374aSPeter Zijlstra 	barrier();
233a866374aSPeter Zijlstra }
234a866374aSPeter Zijlstra 
pagefault_enable(void)235a866374aSPeter Zijlstra static inline void pagefault_enable(void)
236a866374aSPeter Zijlstra {
237a866374aSPeter Zijlstra 	/*
238a866374aSPeter Zijlstra 	 * make sure to issue those last loads/stores before enabling
239a866374aSPeter Zijlstra 	 * the pagefault handler again.
240a866374aSPeter Zijlstra 	 */
241a866374aSPeter Zijlstra 	barrier();
2428bcbde54SDavid Hildenbrand 	pagefault_disabled_dec();
243a866374aSPeter Zijlstra }
244a866374aSPeter Zijlstra 
2458bcbde54SDavid Hildenbrand /*
2468bcbde54SDavid Hildenbrand  * Is the pagefault handler disabled? If so, user access methods will not sleep.
2478bcbde54SDavid Hildenbrand  */
pagefault_disabled(void)2482d8d8facSMasami Hiramatsu static inline bool pagefault_disabled(void)
2492d8d8facSMasami Hiramatsu {
2502d8d8facSMasami Hiramatsu 	return current->pagefault_disabled != 0;
2512d8d8facSMasami Hiramatsu }
2528bcbde54SDavid Hildenbrand 
25370ffdb93SDavid Hildenbrand /*
25470ffdb93SDavid Hildenbrand  * The pagefault handler is in general disabled by pagefault_disable() or
25570ffdb93SDavid Hildenbrand  * when in irq context (via in_atomic()).
25670ffdb93SDavid Hildenbrand  *
25770ffdb93SDavid Hildenbrand  * This function should only be used by the fault handlers. Other users should
25870ffdb93SDavid Hildenbrand  * stick to pagefault_disabled().
25970ffdb93SDavid Hildenbrand  * Please NEVER use preempt_disable() to disable the fault handler. With
26070ffdb93SDavid Hildenbrand  * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
26170ffdb93SDavid Hildenbrand  * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
26270ffdb93SDavid Hildenbrand  */
26370ffdb93SDavid Hildenbrand #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
26470ffdb93SDavid Hildenbrand 
265da32b581SCatalin Marinas #ifndef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
266da32b581SCatalin Marinas 
267da32b581SCatalin Marinas /**
268da32b581SCatalin Marinas  * probe_subpage_writeable: probe the user range for write faults at sub-page
269da32b581SCatalin Marinas  *			    granularity (e.g. arm64 MTE)
270da32b581SCatalin Marinas  * @uaddr: start of address range
271da32b581SCatalin Marinas  * @size: size of address range
272da32b581SCatalin Marinas  *
273da32b581SCatalin Marinas  * Returns 0 on success, the number of bytes not probed on fault.
274da32b581SCatalin Marinas  *
275da32b581SCatalin Marinas  * It is expected that the caller checked for the write permission of each
276da32b581SCatalin Marinas  * page in the range either by put_user() or GUP. The architecture port can
277da32b581SCatalin Marinas  * implement a more efficient get_user() probing if the same sub-page faults
278da32b581SCatalin Marinas  * are triggered by either a read or a write.
279da32b581SCatalin Marinas  */
probe_subpage_writeable(char __user * uaddr,size_t size)280da32b581SCatalin Marinas static inline size_t probe_subpage_writeable(char __user *uaddr, size_t size)
281da32b581SCatalin Marinas {
282da32b581SCatalin Marinas 	return 0;
283da32b581SCatalin Marinas }
284da32b581SCatalin Marinas 
285da32b581SCatalin Marinas #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
286da32b581SCatalin Marinas 
287c22ce143SHiro Yoshioka #ifndef ARCH_HAS_NOCACHE_UACCESS
288c22ce143SHiro Yoshioka 
2899dd819a1SKees Cook static inline __must_check unsigned long
__copy_from_user_inatomic_nocache(void * to,const void __user * from,unsigned long n)2909dd819a1SKees Cook __copy_from_user_inatomic_nocache(void *to, const void __user *from,
2919dd819a1SKees Cook 				  unsigned long n)
292c22ce143SHiro Yoshioka {
293c22ce143SHiro Yoshioka 	return __copy_from_user_inatomic(to, from, n);
294c22ce143SHiro Yoshioka }
295c22ce143SHiro Yoshioka 
296c22ce143SHiro Yoshioka #endif		/* ARCH_HAS_NOCACHE_UACCESS */
297c22ce143SHiro Yoshioka 
298f5a1a536SAleksa Sarai extern __must_check int check_zeroed_user(const void __user *from, size_t size);
299f5a1a536SAleksa Sarai 
300f5a1a536SAleksa Sarai /**
301f5a1a536SAleksa Sarai  * copy_struct_from_user: copy a struct from userspace
302f5a1a536SAleksa Sarai  * @dst:   Destination address, in kernel space. This buffer must be @ksize
303f5a1a536SAleksa Sarai  *         bytes long.
304f5a1a536SAleksa Sarai  * @ksize: Size of @dst struct.
305f5a1a536SAleksa Sarai  * @src:   Source address, in userspace.
306f5a1a536SAleksa Sarai  * @usize: (Alleged) size of @src struct.
307f5a1a536SAleksa Sarai  *
308f5a1a536SAleksa Sarai  * Copies a struct from userspace to kernel space, in a way that guarantees
309f5a1a536SAleksa Sarai  * backwards-compatibility for struct syscall arguments (as long as future
310f5a1a536SAleksa Sarai  * struct extensions are made such that all new fields are *appended* to the
311f5a1a536SAleksa Sarai  * old struct, and zeroed-out new fields have the same meaning as the old
312f5a1a536SAleksa Sarai  * struct).
313f5a1a536SAleksa Sarai  *
314f5a1a536SAleksa Sarai  * @ksize is just sizeof(*dst), and @usize should've been passed by userspace.
315f5a1a536SAleksa Sarai  * The recommended usage is something like the following:
316f5a1a536SAleksa Sarai  *
317f5a1a536SAleksa Sarai  *   SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize)
318f5a1a536SAleksa Sarai  *   {
319f5a1a536SAleksa Sarai  *      int err;
320f5a1a536SAleksa Sarai  *      struct foo karg = {};
321f5a1a536SAleksa Sarai  *
322f5a1a536SAleksa Sarai  *      if (usize > PAGE_SIZE)
323f5a1a536SAleksa Sarai  *        return -E2BIG;
324f5a1a536SAleksa Sarai  *      if (usize < FOO_SIZE_VER0)
325f5a1a536SAleksa Sarai  *        return -EINVAL;
326f5a1a536SAleksa Sarai  *
327f5a1a536SAleksa Sarai  *      err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
328f5a1a536SAleksa Sarai  *      if (err)
329f5a1a536SAleksa Sarai  *        return err;
330f5a1a536SAleksa Sarai  *
331f5a1a536SAleksa Sarai  *      // ...
332f5a1a536SAleksa Sarai  *   }
333f5a1a536SAleksa Sarai  *
334f5a1a536SAleksa Sarai  * There are three cases to consider:
335f5a1a536SAleksa Sarai  *  * If @usize == @ksize, then it's copied verbatim.
336f5a1a536SAleksa Sarai  *  * If @usize < @ksize, then the userspace has passed an old struct to a
337f5a1a536SAleksa Sarai  *    newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize)
338f5a1a536SAleksa Sarai  *    are to be zero-filled.
339f5a1a536SAleksa Sarai  *  * If @usize > @ksize, then the userspace has passed a new struct to an
340f5a1a536SAleksa Sarai  *    older kernel. The trailing bytes unknown to the kernel (@usize - @ksize)
341f5a1a536SAleksa Sarai  *    are checked to ensure they are zeroed, otherwise -E2BIG is returned.
342f5a1a536SAleksa Sarai  *
343f5a1a536SAleksa Sarai  * Returns (in all cases, some data may have been copied):
344f5a1a536SAleksa Sarai  *  * -E2BIG:  (@usize > @ksize) and there are non-zero trailing bytes in @src.
345f5a1a536SAleksa Sarai  *  * -EFAULT: access to userspace failed.
346f5a1a536SAleksa Sarai  */
347f5a1a536SAleksa Sarai static __always_inline __must_check int
copy_struct_from_user(void * dst,size_t ksize,const void __user * src,size_t usize)348f5a1a536SAleksa Sarai copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
349f5a1a536SAleksa Sarai 		      size_t usize)
350f5a1a536SAleksa Sarai {
351f5a1a536SAleksa Sarai 	size_t size = min(ksize, usize);
352f5a1a536SAleksa Sarai 	size_t rest = max(ksize, usize) - size;
353f5a1a536SAleksa Sarai 
35404ffde13SKees Cook 	/* Double check if ksize is larger than a known object size. */
35504ffde13SKees Cook 	if (WARN_ON_ONCE(ksize > __builtin_object_size(dst, 1)))
35604ffde13SKees Cook 		return -E2BIG;
35704ffde13SKees Cook 
358f5a1a536SAleksa Sarai 	/* Deal with trailing bytes. */
359f5a1a536SAleksa Sarai 	if (usize < ksize) {
360f5a1a536SAleksa Sarai 		memset(dst + size, 0, rest);
361f5a1a536SAleksa Sarai 	} else if (usize > ksize) {
362f5a1a536SAleksa Sarai 		int ret = check_zeroed_user(src + size, rest);
363f5a1a536SAleksa Sarai 		if (ret <= 0)
364f5a1a536SAleksa Sarai 			return ret ?: -E2BIG;
365f5a1a536SAleksa Sarai 	}
366f5a1a536SAleksa Sarai 	/* Copy the interoperable parts of the struct. */
367f5a1a536SAleksa Sarai 	if (copy_from_user(dst, src, size))
368f5a1a536SAleksa Sarai 		return -EFAULT;
369f5a1a536SAleksa Sarai 	return 0;
370f5a1a536SAleksa Sarai }
371f5a1a536SAleksa Sarai 
372fe557319SChristoph Hellwig bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size);
373eab0c608SChristoph Hellwig 
374fe557319SChristoph Hellwig long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
375fe557319SChristoph Hellwig long notrace copy_to_kernel_nofault(void *dst, const void *src, size_t size);
376fe557319SChristoph Hellwig 
377c0ee37e8SChristoph Hellwig long copy_from_user_nofault(void *dst, const void __user *src, size_t size);
378c0ee37e8SChristoph Hellwig long notrace copy_to_user_nofault(void __user *dst, const void *src,
379fe557319SChristoph Hellwig 		size_t size);
3801d1585caSDaniel Borkmann 
381c4cb1644SChristoph Hellwig long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr,
38275a1a607SDaniel Borkmann 		long count);
383eab0c608SChristoph Hellwig 
384bd88bb5dSChristoph Hellwig long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
3853d708182SMasami Hiramatsu 		long count);
38602dddb16SChristoph Hellwig long strnlen_user_nofault(const void __user *unsafe_addr, long count);
3871a6877b9SAlexei Starovoitov 
38834737e26SArnd Bergmann #ifndef __get_kernel_nofault
38934737e26SArnd Bergmann #define __get_kernel_nofault(dst, src, type, label)	\
39034737e26SArnd Bergmann do {							\
39134737e26SArnd Bergmann 	type __user *p = (type __force __user *)(src);	\
39234737e26SArnd Bergmann 	type data;					\
39334737e26SArnd Bergmann 	if (__get_user(data, p))			\
39434737e26SArnd Bergmann 		goto label;				\
39534737e26SArnd Bergmann 	*(type *)dst = data;				\
39634737e26SArnd Bergmann } while (0)
39734737e26SArnd Bergmann 
39834737e26SArnd Bergmann #define __put_kernel_nofault(dst, src, type, label)	\
39934737e26SArnd Bergmann do {							\
40034737e26SArnd Bergmann 	type __user *p = (type __force __user *)(dst);	\
40134737e26SArnd Bergmann 	type data = *(type *)src;			\
40234737e26SArnd Bergmann 	if (__put_user(data, p))			\
40334737e26SArnd Bergmann 		goto label;				\
40434737e26SArnd Bergmann } while (0)
40534737e26SArnd Bergmann #endif
40634737e26SArnd Bergmann 
4070ab32b6fSAndrew Morton /**
40825f12ae4SChristoph Hellwig  * get_kernel_nofault(): safely attempt to read from a location
40925f12ae4SChristoph Hellwig  * @val: read into this variable
41025f12ae4SChristoph Hellwig  * @ptr: address to read from
4110ab32b6fSAndrew Morton  *
4120ab32b6fSAndrew Morton  * Returns 0 on success, or -EFAULT.
4130ab32b6fSAndrew Morton  */
4140c389d89SLinus Torvalds #define get_kernel_nofault(val, ptr) ({				\
4150c389d89SLinus Torvalds 	const typeof(val) *__gk_ptr = (ptr);			\
4160c389d89SLinus Torvalds 	copy_from_kernel_nofault(&(val), __gk_ptr, sizeof(val));\
4170c389d89SLinus Torvalds })
4180ab32b6fSAndrew Morton 
4195b24a7a2SLinus Torvalds #ifndef user_access_begin
420594cc251SLinus Torvalds #define user_access_begin(ptr,len) access_ok(ptr, len)
4215b24a7a2SLinus Torvalds #define user_access_end() do { } while (0)
422c512c691SLinus Torvalds #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
423c512c691SLinus Torvalds #define unsafe_get_user(x,p,e) unsafe_op_wrap(__get_user(x,p),e)
424c512c691SLinus Torvalds #define unsafe_put_user(x,p,e) unsafe_op_wrap(__put_user(x,p),e)
425c512c691SLinus Torvalds #define unsafe_copy_to_user(d,s,l,e) unsafe_op_wrap(__copy_to_user(d,s,l),e)
426fb05121fSChristophe Leroy #define unsafe_copy_from_user(d,s,l,e) unsafe_op_wrap(__copy_from_user(d,s,l),e)
user_access_save(void)427e74deb11SPeter Zijlstra static inline unsigned long user_access_save(void) { return 0UL; }
user_access_restore(unsigned long flags)428e74deb11SPeter Zijlstra static inline void user_access_restore(unsigned long flags) { }
4295b24a7a2SLinus Torvalds #endif
430999a2289SChristophe Leroy #ifndef user_write_access_begin
431999a2289SChristophe Leroy #define user_write_access_begin user_access_begin
432999a2289SChristophe Leroy #define user_write_access_end user_access_end
433999a2289SChristophe Leroy #endif
434999a2289SChristophe Leroy #ifndef user_read_access_begin
435999a2289SChristophe Leroy #define user_read_access_begin user_access_begin
436999a2289SChristophe Leroy #define user_read_access_end user_access_end
437999a2289SChristophe Leroy #endif
4385b24a7a2SLinus Torvalds 
439b394d468SKees Cook #ifdef CONFIG_HARDENED_USERCOPY
440b394d468SKees Cook void __noreturn usercopy_abort(const char *name, const char *detail,
441b394d468SKees Cook 			       bool to_user, unsigned long offset,
442b394d468SKees Cook 			       unsigned long len);
443b394d468SKees Cook #endif
444b394d468SKees Cook 
445c22ce143SHiro Yoshioka #endif		/* __LINUX_UACCESS_H__ */
446