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