1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_UACCESS_H__
3 #define __LINUX_UACCESS_H__
5 #include <linux/sched.h>
6 #include <linux/thread_info.h>
7 #include <linux/kasan-checks.h>
9 #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
11 #include <asm/uaccess.h>
14 * Architectures should provide two primitives (raw_copy_{to,from}_user())
15 * and get rid of their private instances of copy_{to,from}_user() and
16 * __copy_{to,from}_user{,_inatomic}().
18 * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and
19 * return the amount left to copy. They should assume that access_ok() has
20 * already been checked (and succeeded); they should *not* zero-pad anything.
21 * No KASAN or object size checks either - those belong here.
23 * Both of these functions should attempt to copy size bytes starting at from
24 * into the area starting at to. They must not fetch or store anything
25 * outside of those areas. Return value must be between 0 (everything
26 * copied successfully) and size (nothing copied).
28 * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting
29 * at to must become equal to the bytes fetched from the corresponding area
30 * starting at from. All data past to + size - N must be left unmodified.
32 * If copying succeeds, the return value must be 0. If some data cannot be
33 * fetched, it is permitted to copy less than had been fetched; the only
34 * hard requirement is that not storing anything at all (i.e. returning size)
35 * should happen only when nothing could be copied. In other words, you don't
36 * have to squeeze as much as possible - it is allowed, but not necessary.
38 * For raw_copy_from_user() to always points to kernel memory and no faults
39 * on store should happen. Interpretation of from is affected by set_fs().
40 * For raw_copy_to_user() it's the other way round.
42 * Both can be inlined - it's up to architectures whether it wants to bother
43 * with that. They should not be used directly; they are used to implement
44 * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic())
45 * that are used instead. Out of those, __... ones are inlined. Plain
46 * copy_{to,from}_user() might or might not be inlined. If you want them
47 * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER.
49 * NOTE: only copy_from_user() zero-pads the destination in case of short copy.
50 * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything
51 * at all; their callers absolutely must check the return value.
53 * Biarch ones should also provide raw_copy_in_user() - similar to the above,
54 * but both source and destination are __user pointers (affected by set_fs()
55 * as usual) and both source and destination can trigger faults.
58 static __always_inline unsigned long
59 __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
61 kasan_check_write(to, n);
62 check_object_size(to, n, false);
63 return raw_copy_from_user(to, from, n);
66 static __always_inline unsigned long
67 __copy_from_user(void *to, const void __user *from, unsigned long n)
70 kasan_check_write(to, n);
71 check_object_size(to, n, false);
72 return raw_copy_from_user(to, from, n);
76 * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking.
77 * @to: Destination address, in user space.
78 * @from: Source address, in kernel space.
79 * @n: Number of bytes to copy.
81 * Context: User context only.
83 * Copy data from kernel space to user space. Caller must check
84 * the specified block with access_ok() before calling this function.
85 * The caller should also make sure he pins the user space address
86 * so that we don't result in page fault and sleep.
88 static __always_inline unsigned long
89 __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n)
91 kasan_check_read(from, n);
92 check_object_size(from, n, true);
93 return raw_copy_to_user(to, from, n);
96 static __always_inline unsigned long
97 __copy_to_user(void __user *to, const void *from, unsigned long n)
100 kasan_check_read(from, n);
101 check_object_size(from, n, true);
102 return raw_copy_to_user(to, from, n);
105 #ifdef INLINE_COPY_FROM_USER
106 static inline unsigned long
107 _copy_from_user(void *to, const void __user *from, unsigned long n)
109 unsigned long res = n;
111 if (likely(access_ok(from, n))) {
112 kasan_check_write(to, n);
113 res = raw_copy_from_user(to, from, n);
116 memset(to + (n - res), 0, res);
121 _copy_from_user(void *, const void __user *, unsigned long);
124 #ifdef INLINE_COPY_TO_USER
125 static inline unsigned long
126 _copy_to_user(void __user *to, const void *from, unsigned long n)
129 if (access_ok(to, n)) {
130 kasan_check_read(from, n);
131 n = raw_copy_to_user(to, from, n);
137 _copy_to_user(void __user *, const void *, unsigned long);
140 static __always_inline unsigned long __must_check
141 copy_from_user(void *to, const void __user *from, unsigned long n)
143 if (likely(check_copy_size(to, n, false)))
144 n = _copy_from_user(to, from, n);
148 static __always_inline unsigned long __must_check
149 copy_to_user(void __user *to, const void *from, unsigned long n)
151 if (likely(check_copy_size(from, n, true)))
152 n = _copy_to_user(to, from, n);
156 static __always_inline unsigned long __must_check
157 copy_in_user(void __user *to, const void __user *from, unsigned long n)
160 if (access_ok(to, n) && access_ok(from, n))
161 n = raw_copy_in_user(to, from, n);
166 static __always_inline void pagefault_disabled_inc(void)
168 current->pagefault_disabled++;
171 static __always_inline void pagefault_disabled_dec(void)
173 current->pagefault_disabled--;
177 * These routines enable/disable the pagefault handler. If disabled, it will
178 * not take any locks and go straight to the fixup table.
180 * User access methods will not sleep when called from a pagefault_disabled()
183 static inline void pagefault_disable(void)
185 pagefault_disabled_inc();
187 * make sure to have issued the store before a pagefault
193 static inline void pagefault_enable(void)
196 * make sure to issue those last loads/stores before enabling
197 * the pagefault handler again.
200 pagefault_disabled_dec();
204 * Is the pagefault handler disabled? If so, user access methods will not sleep.
206 #define pagefault_disabled() (current->pagefault_disabled != 0)
209 * The pagefault handler is in general disabled by pagefault_disable() or
210 * when in irq context (via in_atomic()).
212 * This function should only be used by the fault handlers. Other users should
213 * stick to pagefault_disabled().
214 * Please NEVER use preempt_disable() to disable the fault handler. With
215 * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled.
216 * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT.
218 #define faulthandler_disabled() (pagefault_disabled() || in_atomic())
220 #ifndef ARCH_HAS_NOCACHE_UACCESS
222 static inline unsigned long __copy_from_user_inatomic_nocache(void *to,
223 const void __user *from, unsigned long n)
225 return __copy_from_user_inatomic(to, from, n);
228 #endif /* ARCH_HAS_NOCACHE_UACCESS */
231 * probe_kernel_read(): safely attempt to read from a location
232 * @dst: pointer to the buffer that shall take the data
233 * @src: address to read from
234 * @size: size of the data chunk
236 * Safely read from address @src to the buffer at @dst. If a kernel fault
237 * happens, handle that and return -EFAULT.
239 extern long probe_kernel_read(void *dst, const void *src, size_t size);
240 extern long __probe_kernel_read(void *dst, const void *src, size_t size);
243 * probe_kernel_write(): safely attempt to write to a location
244 * @dst: address to write to
245 * @src: pointer to the data that shall be written
246 * @size: size of the data chunk
248 * Safely write to address @dst from the buffer at @src. If a kernel fault
249 * happens, handle that and return -EFAULT.
251 extern long notrace probe_kernel_write(void *dst, const void *src, size_t size);
252 extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size);
254 extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count);
257 * probe_kernel_address(): safely attempt to read from a location
258 * @addr: address to read from
259 * @retval: read into this variable
261 * Returns 0 on success, or -EFAULT.
263 #define probe_kernel_address(addr, retval) \
264 probe_kernel_read(&retval, addr, sizeof(retval))
266 #ifndef user_access_begin
267 #define user_access_begin(ptr,len) access_ok(ptr, len)
268 #define user_access_end() do { } while (0)
269 #define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0)
270 #define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0)
273 #ifdef CONFIG_HARDENED_USERCOPY
274 void usercopy_warn(const char *name, const char *detail, bool to_user,
275 unsigned long offset, unsigned long len);
276 void __noreturn usercopy_abort(const char *name, const char *detail,
277 bool to_user, unsigned long offset,
281 #endif /* __LINUX_UACCESS_H__ */