1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_UACCESS_H__
3 #define __LINUX_UACCESS_H__
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>
12 #include <asm/uaccess.h>
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.
20 * Passing down mm_struct allows to define untagging rules on per-process
23 * It's defined as noop for architectures that don't support memory tagging.
26 #define untagged_addr(addr) (addr)
29 #ifndef untagged_addr_remote
30 #define untagged_addr_remote(mm, addr) ({ \
31 mmap_assert_locked(mm); \
32 untagged_addr(addr); \
36 #ifdef masked_user_access_begin
37 #define can_do_masked_user_access() 1
39 #define can_do_masked_user_access() 0
40 #define masked_user_access_begin(src) NULL
41 #define mask_user_address(src) (src)
45 * Architectures should provide two primitives (raw_copy_{to,from}_user())
46 * and get rid of their private instances of copy_{to,from}_user() and
47 * __copy_{to,from}_user{,_inatomic}().
49 * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and
50 * return the amount left to copy. They should assume that access_ok() has
51 * already been checked (and succeeded); they should *not* zero-pad anything.
52 * No KASAN or object size checks either - those belong here.
54 * Both of these functions should attempt to copy size bytes starting at from
55 * into the area starting at to. They must not fetch or store anything
56 * outside of those areas. Return value must be between 0 (everything
57 * copied successfully) and size (nothing copied).
59 * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting
60 * at to must become equal to the bytes fetched from the corresponding area
61 * starting at from. All data past to + size - N must be left unmodified.
63 * If copying succeeds, the return value must be 0. If some data cannot be
64 * fetched, it is permitted to copy less than had been fetched; the only
65 * hard requirement is that not storing anything at all (i.e. returning size)
66 * should happen only when nothing could be copied. In other words, you don't
67 * have to squeeze as much as possible - it is allowed, but not necessary.
69 * For raw_copy_from_user() to always points to kernel memory and no faults
70 * on store should happen. Interpretation of from is affected by set_fs().
71 * For raw_copy_to_user() it's the other way round.
73 * Both can be inlined - it's up to architectures whether it wants to bother
74 * with that. They should not be used directly; they are used to implement
75 * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic())
76 * that are used instead. Out of those, __... ones are inlined. Plain
77 * copy_{to,from}_user() might or might not be inlined. If you want them
78 * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER.
80 * NOTE: only copy_from_user() zero-pads the destination in case of short copy.
81 * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything
82 * at all; their callers absolutely must check the return value.
84 * Biarch ones should also provide raw_copy_in_user() - similar to the above,
85 * but both source and destination are __user pointers (affected by set_fs()
86 * as usual) and both source and destination can trigger faults.
89 static __always_inline __must_check
unsigned long
90 __copy_from_user_inatomic(void *to
, const void __user
*from
, unsigned long n
)
94 instrument_copy_from_user_before(to
, from
, n
);
95 check_object_size(to
, n
, false);
96 res
= raw_copy_from_user(to
, from
, n
);
97 instrument_copy_from_user_after(to
, from
, n
, res
);
101 static __always_inline __must_check
unsigned long
102 __copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
107 instrument_copy_from_user_before(to
, from
, n
);
108 if (should_fail_usercopy())
110 check_object_size(to
, n
, false);
111 res
= raw_copy_from_user(to
, from
, n
);
112 instrument_copy_from_user_after(to
, from
, n
, res
);
117 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
118 * @to: Destination address, in user space.
119 * @from: Source address, in kernel space.
120 * @n: Number of bytes to copy.
122 * Context: User context only.
124 * Copy data from kernel space to user space. Caller must check
125 * the specified block with access_ok() before calling this function.
126 * The caller should also make sure he pins the user space address
127 * so that we don't result in page fault and sleep.
129 static __always_inline __must_check
unsigned long
130 __copy_to_user_inatomic(void __user
*to
, const void *from
, unsigned long n
)
132 if (should_fail_usercopy())
134 instrument_copy_to_user(to
, from
, n
);
135 check_object_size(from
, n
, true);
136 return raw_copy_to_user(to
, from
, n
);
139 static __always_inline __must_check
unsigned long
140 __copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
143 if (should_fail_usercopy())
145 instrument_copy_to_user(to
, from
, n
);
146 check_object_size(from
, n
, true);
147 return raw_copy_to_user(to
, from
, n
);
151 * Architectures that #define INLINE_COPY_TO_USER use this function
152 * directly in the normal copy_to/from_user(), the other ones go
153 * through an extern _copy_to/from_user(), which expands the same code
156 * Rust code always uses the extern definition.
158 static inline __must_check
unsigned long
159 _inline_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
161 unsigned long res
= n
;
163 if (should_fail_usercopy())
165 if (can_do_masked_user_access())
166 from
= mask_user_address(from
);
168 if (!access_ok(from
, n
))
171 * Ensure that bad access_ok() speculation will not
172 * lead to nasty side effects *after* the copy is
177 instrument_copy_from_user_before(to
, from
, n
);
178 res
= raw_copy_from_user(to
, from
, n
);
179 instrument_copy_from_user_after(to
, from
, n
, res
);
183 memset(to
+ (n
- res
), 0, res
);
186 extern __must_check
unsigned long
187 _copy_from_user(void *, const void __user
*, unsigned long);
189 static inline __must_check
unsigned long
190 _inline_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
193 if (should_fail_usercopy())
195 if (access_ok(to
, n
)) {
196 instrument_copy_to_user(to
, from
, n
);
197 n
= raw_copy_to_user(to
, from
, n
);
201 extern __must_check
unsigned long
202 _copy_to_user(void __user
*, const void *, unsigned long);
204 static __always_inline
unsigned long __must_check
205 copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
207 if (!check_copy_size(to
, n
, false))
209 #ifdef INLINE_COPY_FROM_USER
210 return _inline_copy_from_user(to
, from
, n
);
212 return _copy_from_user(to
, from
, n
);
216 static __always_inline
unsigned long __must_check
217 copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
219 if (!check_copy_size(from
, n
, true))
222 #ifdef INLINE_COPY_TO_USER
223 return _inline_copy_to_user(to
, from
, n
);
225 return _copy_to_user(to
, from
, n
);
229 #ifndef copy_mc_to_kernel
231 * Without arch opt-in this generic copy_mc_to_kernel() will not handle
232 * #MC (or arch equivalent) during source read.
234 static inline unsigned long __must_check
235 copy_mc_to_kernel(void *dst
, const void *src
, size_t cnt
)
237 memcpy(dst
, src
, cnt
);
242 static __always_inline
void pagefault_disabled_inc(void)
244 current
->pagefault_disabled
++;
247 static __always_inline
void pagefault_disabled_dec(void)
249 current
->pagefault_disabled
--;
253 * These routines enable/disable the pagefault handler. If disabled, it will
254 * not take any locks and go straight to the fixup table.
256 * User access methods will not sleep when called from a pagefault_disabled()
259 static inline void pagefault_disable(void)
261 pagefault_disabled_inc();
263 * make sure to have issued the store before a pagefault
269 static inline void pagefault_enable(void)
272 * make sure to issue those last loads/stores before enabling
273 * the pagefault handler again.
276 pagefault_disabled_dec();
280 * Is the pagefault handler disabled? If so, user access methods will not sleep.
282 static inline bool pagefault_disabled(void)
284 return current
->pagefault_disabled
!= 0;
288 * The pagefault handler is in general disabled by pagefault_disable() or
289 * when in irq context (via in_atomic()).
291 * This function should only be used by the fault handlers. Other users should
292 * stick to pagefault_disabled().
293 * Please NEVER use preempt_disable() to disable the fault handler. With
294 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
295 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
297 #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
299 #ifndef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
302 * probe_subpage_writeable: probe the user range for write faults at sub-page
303 * granularity (e.g. arm64 MTE)
304 * @uaddr: start of address range
305 * @size: size of address range
307 * Returns 0 on success, the number of bytes not probed on fault.
309 * It is expected that the caller checked for the write permission of each
310 * page in the range either by put_user() or GUP. The architecture port can
311 * implement a more efficient get_user() probing if the same sub-page faults
312 * are triggered by either a read or a write.
314 static inline size_t probe_subpage_writeable(char __user
*uaddr
, size_t size
)
319 #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
321 #ifndef ARCH_HAS_NOCACHE_UACCESS
323 static inline __must_check
unsigned long
324 __copy_from_user_inatomic_nocache(void *to
, const void __user
*from
,
327 return __copy_from_user_inatomic(to
, from
, n
);
330 #endif /* ARCH_HAS_NOCACHE_UACCESS */
332 extern __must_check
int check_zeroed_user(const void __user
*from
, size_t size
);
335 * copy_struct_from_user: copy a struct from userspace
336 * @dst: Destination address, in kernel space. This buffer must be @ksize
338 * @ksize: Size of @dst struct.
339 * @src: Source address, in userspace.
340 * @usize: (Alleged) size of @src struct.
342 * Copies a struct from userspace to kernel space, in a way that guarantees
343 * backwards-compatibility for struct syscall arguments (as long as future
344 * struct extensions are made such that all new fields are *appended* to the
345 * old struct, and zeroed-out new fields have the same meaning as the old
348 * @ksize is just sizeof(*dst), and @usize should've been passed by userspace.
349 * The recommended usage is something like the following:
351 * SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize)
354 * struct foo karg = {};
356 * if (usize > PAGE_SIZE)
358 * if (usize < FOO_SIZE_VER0)
361 * err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
368 * There are three cases to consider:
369 * * If @usize == @ksize, then it's copied verbatim.
370 * * If @usize < @ksize, then the userspace has passed an old struct to a
371 * newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize)
372 * are to be zero-filled.
373 * * If @usize > @ksize, then the userspace has passed a new struct to an
374 * older kernel. The trailing bytes unknown to the kernel (@usize - @ksize)
375 * are checked to ensure they are zeroed, otherwise -E2BIG is returned.
377 * Returns (in all cases, some data may have been copied):
378 * * -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src.
379 * * -EFAULT: access to userspace failed.
381 static __always_inline __must_check
int
382 copy_struct_from_user(void *dst
, size_t ksize
, const void __user
*src
,
385 size_t size
= min(ksize
, usize
);
386 size_t rest
= max(ksize
, usize
) - size
;
388 /* Double check if ksize is larger than a known object size. */
389 if (WARN_ON_ONCE(ksize
> __builtin_object_size(dst
, 1)))
392 /* Deal with trailing bytes. */
394 memset(dst
+ size
, 0, rest
);
395 } else if (usize
> ksize
) {
396 int ret
= check_zeroed_user(src
+ size
, rest
);
398 return ret
?: -E2BIG
;
400 /* Copy the interoperable parts of the struct. */
401 if (copy_from_user(dst
, src
, size
))
407 * copy_struct_to_user: copy a struct to userspace
408 * @dst: Destination address, in userspace. This buffer must be @ksize
410 * @usize: (Alleged) size of @dst struct.
411 * @src: Source address, in kernel space.
412 * @ksize: Size of @src struct.
413 * @ignored_trailing: Set to %true if there was a non-zero byte in @src that
414 * userspace cannot see because they are using an smaller struct.
416 * Copies a struct from kernel space to userspace, in a way that guarantees
417 * backwards-compatibility for struct syscall arguments (as long as future
418 * struct extensions are made such that all new fields are *appended* to the
419 * old struct, and zeroed-out new fields have the same meaning as the old
422 * Some syscalls may wish to make sure that userspace knows about everything in
423 * the struct, and if there is a non-zero value that userspce doesn't know
424 * about, they want to return an error (such as -EMSGSIZE) or have some other
425 * fallback (such as adding a "you're missing some information" flag). If
426 * @ignored_trailing is non-%NULL, it will be set to %true if there was a
427 * non-zero byte that could not be copied to userspace (ie. was past @usize).
429 * While unconditionally returning an error in this case is the simplest
430 * solution, for maximum backward compatibility you should try to only return
431 * -EMSGSIZE if the user explicitly requested the data that couldn't be copied.
432 * Note that structure sizes can change due to header changes and simple
433 * recompilations without code changes(!), so if you care about
434 * @ignored_trailing you probably want to make sure that any new field data is
435 * associated with a flag. Otherwise you might assume that a program knows
436 * about data it does not.
438 * @ksize is just sizeof(*src), and @usize should've been passed by userspace.
439 * The recommended usage is something like the following:
441 * SYSCALL_DEFINE2(foobar, struct foo __user *, uarg, size_t, usize)
444 * bool ignored_trailing;
445 * struct foo karg = {};
447 * if (usize > PAGE_SIZE)
449 * if (usize < FOO_SIZE_VER0)
452 * // ... modify karg somehow ...
454 * err = copy_struct_to_user(uarg, usize, &karg, sizeof(karg),
455 * &ignored_trailing);
458 * if (ignored_trailing)
464 * There are three cases to consider:
465 * * If @usize == @ksize, then it's copied verbatim.
466 * * If @usize < @ksize, then the kernel is trying to pass userspace a newer
467 * struct than it supports. Thus we only copy the interoperable portions
468 * (@usize) and ignore the rest (but @ignored_trailing is set to %true if
469 * any of the trailing (@ksize - @usize) bytes are non-zero).
470 * * If @usize > @ksize, then the kernel is trying to pass userspace an older
471 * struct than userspace supports. In order to make sure the
472 * unknown-to-the-kernel fields don't contain garbage values, we zero the
473 * trailing (@usize - @ksize) bytes.
475 * Returns (in all cases, some data may have been copied):
476 * * -EFAULT: access to userspace failed.
478 static __always_inline __must_check
int
479 copy_struct_to_user(void __user
*dst
, size_t usize
, const void *src
,
480 size_t ksize
, bool *ignored_trailing
)
482 size_t size
= min(ksize
, usize
);
483 size_t rest
= max(ksize
, usize
) - size
;
485 /* Double check if ksize is larger than a known object size. */
486 if (WARN_ON_ONCE(ksize
> __builtin_object_size(src
, 1)))
489 /* Deal with trailing bytes. */
491 if (clear_user(dst
+ size
, rest
))
494 if (ignored_trailing
)
495 *ignored_trailing
= ksize
< usize
&&
496 memchr_inv(src
+ size
, 0, rest
) != NULL
;
497 /* Copy the interoperable parts of the struct. */
498 if (copy_to_user(dst
, src
, size
))
503 bool copy_from_kernel_nofault_allowed(const void *unsafe_src
, size_t size
);
505 long copy_from_kernel_nofault(void *dst
, const void *src
, size_t size
);
506 long notrace
copy_to_kernel_nofault(void *dst
, const void *src
, size_t size
);
508 long copy_from_user_nofault(void *dst
, const void __user
*src
, size_t size
);
509 long notrace
copy_to_user_nofault(void __user
*dst
, const void *src
,
512 long strncpy_from_kernel_nofault(char *dst
, const void *unsafe_addr
,
515 long strncpy_from_user_nofault(char *dst
, const void __user
*unsafe_addr
,
517 long strnlen_user_nofault(const void __user
*unsafe_addr
, long count
);
519 #ifndef __get_kernel_nofault
520 #define __get_kernel_nofault(dst, src, type, label) \
522 type __user *p = (type __force __user *)(src); \
524 if (__get_user(data, p)) \
526 *(type *)dst = data; \
529 #define __put_kernel_nofault(dst, src, type, label) \
531 type __user *p = (type __force __user *)(dst); \
532 type data = *(type *)src; \
533 if (__put_user(data, p)) \
539 * get_kernel_nofault(): safely attempt to read from a location
540 * @val: read into this variable
541 * @ptr: address to read from
543 * Returns 0 on success, or -EFAULT.
545 #define get_kernel_nofault(val, ptr) ({ \
546 const typeof(val) *__gk_ptr = (ptr); \
547 copy_from_kernel_nofault(&(val), __gk_ptr, sizeof(val));\
550 #ifndef user_access_begin
551 #define user_access_begin(ptr,len) access_ok(ptr, len)
552 #define user_access_end() do { } while (0)
553 #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0)
554 #define unsafe_get_user(x,p,e) unsafe_op_wrap(__get_user(x,p),e)
555 #define unsafe_put_user(x,p,e) unsafe_op_wrap(__put_user(x,p),e)
556 #define unsafe_copy_to_user(d,s,l,e) unsafe_op_wrap(__copy_to_user(d,s,l),e)
557 #define unsafe_copy_from_user(d,s,l,e) unsafe_op_wrap(__copy_from_user(d,s,l),e)
558 static inline unsigned long user_access_save(void) { return 0UL; }
559 static inline void user_access_restore(unsigned long flags
) { }
561 #ifndef user_write_access_begin
562 #define user_write_access_begin user_access_begin
563 #define user_write_access_end user_access_end
565 #ifndef user_read_access_begin
566 #define user_read_access_begin user_access_begin
567 #define user_read_access_end user_access_end
570 #ifdef CONFIG_HARDENED_USERCOPY
571 void __noreturn
usercopy_abort(const char *name
, const char *detail
,
572 bool to_user
, unsigned long offset
,
576 #endif /* __LINUX_UACCESS_H__ */