Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_UACCESS_H__ |
| 2 | #define __LINUX_UACCESS_H__ |
| 3 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 4 | #include <linux/sched.h> |
Al Viro | af1d5b3 | 2016-12-27 18:14:09 -0500 | [diff] [blame] | 5 | #include <linux/thread_info.h> |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 6 | #include <linux/kasan-checks.h> |
Al Viro | 5e6039d | 2016-12-27 18:00:15 -0500 | [diff] [blame] | 7 | |
| 8 | #define VERIFY_READ 0 |
| 9 | #define VERIFY_WRITE 1 |
| 10 | |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 11 | #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS) |
| 12 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
| 14 | |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 15 | /* |
| 16 | * Architectures should provide two primitives (raw_copy_{to,from}_user()) |
Al Viro | 701cac6 | 2017-04-05 19:15:53 -0400 | [diff] [blame] | 17 | * and get rid of their private instances of copy_{to,from}_user() and |
| 18 | * __copy_{to,from}_user{,_inatomic}(). |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 19 | * |
| 20 | * raw_copy_{to,from}_user(to, from, size) should copy up to size bytes and |
| 21 | * return the amount left to copy. They should assume that access_ok() has |
| 22 | * already been checked (and succeeded); they should *not* zero-pad anything. |
| 23 | * No KASAN or object size checks either - those belong here. |
| 24 | * |
| 25 | * Both of these functions should attempt to copy size bytes starting at from |
| 26 | * into the area starting at to. They must not fetch or store anything |
| 27 | * outside of those areas. Return value must be between 0 (everything |
| 28 | * copied successfully) and size (nothing copied). |
| 29 | * |
| 30 | * If raw_copy_{to,from}_user(to, from, size) returns N, size - N bytes starting |
| 31 | * at to must become equal to the bytes fetched from the corresponding area |
| 32 | * starting at from. All data past to + size - N must be left unmodified. |
| 33 | * |
| 34 | * If copying succeeds, the return value must be 0. If some data cannot be |
| 35 | * fetched, it is permitted to copy less than had been fetched; the only |
| 36 | * hard requirement is that not storing anything at all (i.e. returning size) |
| 37 | * should happen only when nothing could be copied. In other words, you don't |
| 38 | * have to squeeze as much as possible - it is allowed, but not necessary. |
| 39 | * |
| 40 | * For raw_copy_from_user() to always points to kernel memory and no faults |
| 41 | * on store should happen. Interpretation of from is affected by set_fs(). |
| 42 | * For raw_copy_to_user() it's the other way round. |
| 43 | * |
| 44 | * Both can be inlined - it's up to architectures whether it wants to bother |
| 45 | * with that. They should not be used directly; they are used to implement |
| 46 | * the 6 functions (copy_{to,from}_user(), __copy_{to,from}_user_inatomic()) |
| 47 | * that are used instead. Out of those, __... ones are inlined. Plain |
| 48 | * copy_{to,from}_user() might or might not be inlined. If you want them |
| 49 | * inlined, have asm/uaccess.h define INLINE_COPY_{TO,FROM}_USER. |
| 50 | * |
| 51 | * NOTE: only copy_from_user() zero-pads the destination in case of short copy. |
| 52 | * Neither __copy_from_user() nor __copy_from_user_inatomic() zero anything |
| 53 | * at all; their callers absolutely must check the return value. |
| 54 | * |
| 55 | * Biarch ones should also provide raw_copy_in_user() - similar to the above, |
| 56 | * but both source and destination are __user pointers (affected by set_fs() |
| 57 | * as usual) and both source and destination can trigger faults. |
| 58 | */ |
| 59 | |
| 60 | static __always_inline unsigned long |
| 61 | __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) |
| 62 | { |
| 63 | kasan_check_write(to, n); |
| 64 | check_object_size(to, n, false); |
| 65 | return raw_copy_from_user(to, from, n); |
| 66 | } |
| 67 | |
| 68 | static __always_inline unsigned long |
| 69 | __copy_from_user(void *to, const void __user *from, unsigned long n) |
| 70 | { |
| 71 | might_fault(); |
| 72 | kasan_check_write(to, n); |
| 73 | check_object_size(to, n, false); |
| 74 | return raw_copy_from_user(to, from, n); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * __copy_to_user_inatomic: - Copy a block of data into user space, with less checking. |
| 79 | * @to: Destination address, in user space. |
| 80 | * @from: Source address, in kernel space. |
| 81 | * @n: Number of bytes to copy. |
| 82 | * |
| 83 | * Context: User context only. |
| 84 | * |
| 85 | * Copy data from kernel space to user space. Caller must check |
| 86 | * the specified block with access_ok() before calling this function. |
| 87 | * The caller should also make sure he pins the user space address |
| 88 | * so that we don't result in page fault and sleep. |
| 89 | */ |
| 90 | static __always_inline unsigned long |
| 91 | __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) |
| 92 | { |
| 93 | kasan_check_read(from, n); |
| 94 | check_object_size(from, n, true); |
| 95 | return raw_copy_to_user(to, from, n); |
| 96 | } |
| 97 | |
| 98 | static __always_inline unsigned long |
| 99 | __copy_to_user(void __user *to, const void *from, unsigned long n) |
| 100 | { |
| 101 | might_fault(); |
| 102 | kasan_check_read(from, n); |
| 103 | check_object_size(from, n, true); |
| 104 | return raw_copy_to_user(to, from, n); |
| 105 | } |
| 106 | |
| 107 | #ifdef INLINE_COPY_FROM_USER |
| 108 | static inline unsigned long |
| 109 | _copy_from_user(void *to, const void __user *from, unsigned long n) |
| 110 | { |
| 111 | unsigned long res = n; |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 112 | might_fault(); |
| 113 | if (likely(access_ok(VERIFY_READ, from, n))) { |
| 114 | kasan_check_write(to, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 115 | res = raw_copy_from_user(to, from, n); |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 116 | } |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 117 | if (unlikely(res)) |
| 118 | memset(to + (n - res), 0, res); |
| 119 | return res; |
| 120 | } |
| 121 | #else |
| 122 | extern unsigned long |
| 123 | _copy_from_user(void *, const void __user *, unsigned long); |
| 124 | #endif |
| 125 | |
| 126 | #ifdef INLINE_COPY_TO_USER |
| 127 | static inline unsigned long |
| 128 | _copy_to_user(void __user *to, const void *from, unsigned long n) |
| 129 | { |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 130 | might_fault(); |
| 131 | if (access_ok(VERIFY_WRITE, to, n)) { |
| 132 | kasan_check_read(from, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 133 | n = raw_copy_to_user(to, from, n); |
Al Viro | 9c5f690 | 2017-06-29 21:39:54 -0400 | [diff] [blame] | 134 | } |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 135 | return n; |
| 136 | } |
| 137 | #else |
| 138 | extern unsigned long |
| 139 | _copy_to_user(void __user *, const void *, unsigned long); |
| 140 | #endif |
| 141 | |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 142 | static __always_inline unsigned long __must_check |
| 143 | copy_from_user(void *to, const void __user *from, unsigned long n) |
| 144 | { |
Al Viro | b0377fe | 2017-06-29 21:42:43 -0400 | [diff] [blame^] | 145 | if (likely(check_copy_size(to, n, false))) |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 146 | n = _copy_from_user(to, from, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 147 | return n; |
| 148 | } |
| 149 | |
| 150 | static __always_inline unsigned long __must_check |
| 151 | copy_to_user(void __user *to, const void *from, unsigned long n) |
| 152 | { |
Al Viro | b0377fe | 2017-06-29 21:42:43 -0400 | [diff] [blame^] | 153 | if (likely(check_copy_size(from, n, true))) |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 154 | n = _copy_to_user(to, from, n); |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 155 | return n; |
| 156 | } |
| 157 | #ifdef CONFIG_COMPAT |
| 158 | static __always_inline unsigned long __must_check |
| 159 | __copy_in_user(void __user *to, const void *from, unsigned long n) |
| 160 | { |
| 161 | might_fault(); |
| 162 | return raw_copy_in_user(to, from, n); |
| 163 | } |
| 164 | static __always_inline unsigned long __must_check |
| 165 | copy_in_user(void __user *to, const void *from, unsigned long n) |
| 166 | { |
| 167 | might_fault(); |
| 168 | if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n)) |
| 169 | n = raw_copy_in_user(to, from, n); |
| 170 | return n; |
| 171 | } |
| 172 | #endif |
Al Viro | d597580 | 2017-03-20 21:56:06 -0400 | [diff] [blame] | 173 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 174 | static __always_inline void pagefault_disabled_inc(void) |
| 175 | { |
| 176 | current->pagefault_disabled++; |
| 177 | } |
| 178 | |
| 179 | static __always_inline void pagefault_disabled_dec(void) |
| 180 | { |
| 181 | current->pagefault_disabled--; |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 184 | /* |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 185 | * These routines enable/disable the pagefault handler. If disabled, it will |
| 186 | * not take any locks and go straight to the fixup table. |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 187 | * |
David Hildenbrand | 8222dbe | 2015-05-11 17:52:20 +0200 | [diff] [blame] | 188 | * User access methods will not sleep when called from a pagefault_disabled() |
| 189 | * environment. |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 190 | */ |
| 191 | static inline void pagefault_disable(void) |
| 192 | { |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 193 | pagefault_disabled_inc(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 194 | /* |
| 195 | * make sure to have issued the store before a pagefault |
| 196 | * can hit. |
| 197 | */ |
| 198 | barrier(); |
| 199 | } |
| 200 | |
| 201 | static inline void pagefault_enable(void) |
| 202 | { |
| 203 | /* |
| 204 | * make sure to issue those last loads/stores before enabling |
| 205 | * the pagefault handler again. |
| 206 | */ |
| 207 | barrier(); |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 208 | pagefault_disabled_dec(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 209 | } |
| 210 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 211 | /* |
| 212 | * Is the pagefault handler disabled? If so, user access methods will not sleep. |
| 213 | */ |
| 214 | #define pagefault_disabled() (current->pagefault_disabled != 0) |
| 215 | |
David Hildenbrand | 70ffdb9 | 2015-05-11 17:52:11 +0200 | [diff] [blame] | 216 | /* |
| 217 | * The pagefault handler is in general disabled by pagefault_disable() or |
| 218 | * when in irq context (via in_atomic()). |
| 219 | * |
| 220 | * This function should only be used by the fault handlers. Other users should |
| 221 | * stick to pagefault_disabled(). |
| 222 | * Please NEVER use preempt_disable() to disable the fault handler. With |
| 223 | * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled. |
| 224 | * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT. |
| 225 | */ |
| 226 | #define faulthandler_disabled() (pagefault_disabled() || in_atomic()) |
| 227 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 228 | #ifndef ARCH_HAS_NOCACHE_UACCESS |
| 229 | |
| 230 | static inline unsigned long __copy_from_user_inatomic_nocache(void *to, |
| 231 | const void __user *from, unsigned long n) |
| 232 | { |
| 233 | return __copy_from_user_inatomic(to, from, n); |
| 234 | } |
| 235 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 236 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ |
| 237 | |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 238 | /* |
| 239 | * probe_kernel_read(): safely attempt to read from a location |
| 240 | * @dst: pointer to the buffer that shall take the data |
| 241 | * @src: address to read from |
| 242 | * @size: size of the data chunk |
| 243 | * |
| 244 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 245 | * happens, handle that and return -EFAULT. |
| 246 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 247 | extern long probe_kernel_read(void *dst, const void *src, size_t size); |
| 248 | extern long __probe_kernel_read(void *dst, const void *src, size_t size); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * probe_kernel_write(): safely attempt to write to a location |
| 252 | * @dst: address to write to |
| 253 | * @src: pointer to the data that shall be written |
| 254 | * @size: size of the data chunk |
| 255 | * |
| 256 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 257 | * happens, handle that and return -EFAULT. |
| 258 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 259 | extern long notrace probe_kernel_write(void *dst, const void *src, size_t size); |
| 260 | extern long notrace __probe_kernel_write(void *dst, const void *src, size_t size); |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 261 | |
Alexei Starovoitov | 1a6877b | 2015-08-28 15:56:22 -0700 | [diff] [blame] | 262 | extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count); |
| 263 | |
Andrew Morton | 0ab32b6 | 2015-11-05 18:46:03 -0800 | [diff] [blame] | 264 | /** |
| 265 | * probe_kernel_address(): safely attempt to read from a location |
| 266 | * @addr: address to read from |
| 267 | * @retval: read into this variable |
| 268 | * |
| 269 | * Returns 0 on success, or -EFAULT. |
| 270 | */ |
| 271 | #define probe_kernel_address(addr, retval) \ |
| 272 | probe_kernel_read(&retval, addr, sizeof(retval)) |
| 273 | |
Linus Torvalds | 5b24a7a | 2015-12-17 09:57:27 -0800 | [diff] [blame] | 274 | #ifndef user_access_begin |
| 275 | #define user_access_begin() do { } while (0) |
| 276 | #define user_access_end() do { } while (0) |
Linus Torvalds | 1bd4403 | 2016-08-08 13:02:01 -0700 | [diff] [blame] | 277 | #define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0) |
| 278 | #define unsafe_put_user(x, ptr, err) do { if (unlikely(__put_user(x, ptr))) goto err; } while (0) |
Linus Torvalds | 5b24a7a | 2015-12-17 09:57:27 -0800 | [diff] [blame] | 279 | #endif |
| 280 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 281 | #endif /* __LINUX_UACCESS_H__ */ |