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 | 2cf6ba5 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 5 | |
| 6 | #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS) |
| 7 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 8 | #include <asm/uaccess.h> |
| 9 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 10 | static __always_inline void pagefault_disabled_inc(void) |
| 11 | { |
| 12 | current->pagefault_disabled++; |
| 13 | } |
| 14 | |
| 15 | static __always_inline void pagefault_disabled_dec(void) |
| 16 | { |
| 17 | current->pagefault_disabled--; |
| 18 | WARN_ON(current->pagefault_disabled < 0); |
| 19 | } |
| 20 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 21 | /* |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 22 | * These routines enable/disable the pagefault handler. If disabled, it will |
| 23 | * not take any locks and go straight to the fixup table. |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 24 | * |
David Hildenbrand | 8222dbe | 2015-05-11 17:52:20 +0200 | [diff] [blame] | 25 | * User access methods will not sleep when called from a pagefault_disabled() |
| 26 | * environment. |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 27 | */ |
| 28 | static inline void pagefault_disable(void) |
| 29 | { |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 30 | pagefault_disabled_inc(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 31 | /* |
| 32 | * make sure to have issued the store before a pagefault |
| 33 | * can hit. |
| 34 | */ |
| 35 | barrier(); |
| 36 | } |
| 37 | |
| 38 | static inline void pagefault_enable(void) |
| 39 | { |
| 40 | /* |
| 41 | * make sure to issue those last loads/stores before enabling |
| 42 | * the pagefault handler again. |
| 43 | */ |
| 44 | barrier(); |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 45 | pagefault_disabled_dec(); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 46 | } |
| 47 | |
David Hildenbrand | 8bcbde5 | 2015-05-11 17:52:06 +0200 | [diff] [blame] | 48 | /* |
| 49 | * Is the pagefault handler disabled? If so, user access methods will not sleep. |
| 50 | */ |
| 51 | #define pagefault_disabled() (current->pagefault_disabled != 0) |
| 52 | |
David Hildenbrand | 70ffdb9 | 2015-05-11 17:52:11 +0200 | [diff] [blame] | 53 | /* |
| 54 | * The pagefault handler is in general disabled by pagefault_disable() or |
| 55 | * when in irq context (via in_atomic()). |
| 56 | * |
| 57 | * This function should only be used by the fault handlers. Other users should |
| 58 | * stick to pagefault_disabled(). |
| 59 | * Please NEVER use preempt_disable() to disable the fault handler. With |
| 60 | * !CONFIG_PREEMPT_COUNT, this is like a NOP. So the handler won't be disabled. |
| 61 | * in_atomic() will report different values based on !CONFIG_PREEMPT_COUNT. |
| 62 | */ |
| 63 | #define faulthandler_disabled() (pagefault_disabled() || in_atomic()) |
| 64 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 65 | #ifndef ARCH_HAS_NOCACHE_UACCESS |
| 66 | |
| 67 | static inline unsigned long __copy_from_user_inatomic_nocache(void *to, |
| 68 | const void __user *from, unsigned long n) |
| 69 | { |
| 70 | return __copy_from_user_inatomic(to, from, n); |
| 71 | } |
| 72 | |
| 73 | static inline unsigned long __copy_from_user_nocache(void *to, |
| 74 | const void __user *from, unsigned long n) |
| 75 | { |
| 76 | return __copy_from_user(to, from, n); |
| 77 | } |
| 78 | |
| 79 | #endif /* ARCH_HAS_NOCACHE_UACCESS */ |
| 80 | |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 81 | /* |
| 82 | * probe_kernel_read(): safely attempt to read from a location |
| 83 | * @dst: pointer to the buffer that shall take the data |
| 84 | * @src: address to read from |
| 85 | * @size: size of the data chunk |
| 86 | * |
| 87 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 88 | * happens, handle that and return -EFAULT. |
| 89 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 90 | extern long probe_kernel_read(void *dst, const void *src, size_t size); |
| 91 | 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] | 92 | |
| 93 | /* |
Masami Hiramatsu | ab6d8b2 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 94 | * probe_user_read(): safely attempt to read from a location in user space |
| 95 | * @dst: pointer to the buffer that shall take the data |
| 96 | * @src: address to read from |
| 97 | * @size: size of the data chunk |
| 98 | * |
| 99 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 100 | * happens, handle that and return -EFAULT. |
| 101 | */ |
| 102 | extern long probe_user_read(void *dst, const void __user *src, size_t size); |
| 103 | |
| 104 | /* |
Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame] | 105 | * probe_kernel_write(): safely attempt to write to a location |
| 106 | * @dst: address to write to |
| 107 | * @src: pointer to the data that shall be written |
| 108 | * @size: size of the data chunk |
| 109 | * |
| 110 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 111 | * happens, handle that and return -EFAULT. |
| 112 | */ |
Steven Rostedt | f29c504 | 2011-05-19 14:35:33 -0400 | [diff] [blame] | 113 | extern long notrace probe_kernel_write(void *dst, const void *src, size_t size); |
| 114 | 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] | 115 | |
Daniel Borkmann | 6faf75b | 2019-11-02 00:17:56 +0100 | [diff] [blame] | 116 | /* |
| 117 | * probe_user_write(): safely attempt to write to a location in user space |
| 118 | * @dst: address to write to |
| 119 | * @src: pointer to the data that shall be written |
| 120 | * @size: size of the data chunk |
| 121 | * |
| 122 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 123 | * happens, handle that and return -EFAULT. |
| 124 | */ |
| 125 | extern long notrace probe_user_write(void __user *dst, const void *src, size_t size); |
| 126 | extern long notrace __probe_user_write(void __user *dst, const void *src, size_t size); |
| 127 | |
Alexei Starovoitov | 1a6877b | 2015-08-28 15:56:22 -0700 | [diff] [blame] | 128 | extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count); |
Masami Hiramatsu | ab6d8b2 | 2019-05-15 14:38:18 +0900 | [diff] [blame] | 129 | extern long strncpy_from_unsafe_user(char *dst, const void __user *unsafe_addr, |
| 130 | long count); |
| 131 | extern long strnlen_unsafe_user(const void __user *unsafe_addr, long count); |
Alexei Starovoitov | 1a6877b | 2015-08-28 15:56:22 -0700 | [diff] [blame] | 132 | |
Andrew Morton | 0ab32b6 | 2015-11-05 18:46:03 -0800 | [diff] [blame] | 133 | /** |
| 134 | * probe_kernel_address(): safely attempt to read from a location |
| 135 | * @addr: address to read from |
| 136 | * @retval: read into this variable |
| 137 | * |
| 138 | * Returns 0 on success, or -EFAULT. |
| 139 | */ |
| 140 | #define probe_kernel_address(addr, retval) \ |
| 141 | probe_kernel_read(&retval, addr, sizeof(retval)) |
| 142 | |
Linus Torvalds | 5b24a7a | 2015-12-17 09:57:27 -0800 | [diff] [blame] | 143 | #ifndef user_access_begin |
| 144 | #define user_access_begin() do { } while (0) |
| 145 | #define user_access_end() do { } while (0) |
Linus Torvalds | 1bd4403 | 2016-08-08 13:02:01 -0700 | [diff] [blame] | 146 | #define unsafe_get_user(x, ptr, err) do { if (unlikely(__get_user(x, ptr))) goto err; } while (0) |
| 147 | #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] | 148 | #endif |
| 149 | |
Hiro Yoshioka | c22ce14 | 2006-06-23 02:04:16 -0700 | [diff] [blame] | 150 | #endif /* __LINUX_UACCESS_H__ */ |