1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _PKEYS_HELPER_H 3 #define _PKEYS_HELPER_H 4 #define _GNU_SOURCE 5 #include <string.h> 6 #include <stdarg.h> 7 #include <stdio.h> 8 #include <stdint.h> 9 #include <stdbool.h> 10 #include <signal.h> 11 #include <assert.h> 12 #include <stdlib.h> 13 #include <ucontext.h> 14 #include <sys/mman.h> 15 16 #include "../kselftest.h" 17 18 /* Define some kernel-like types */ 19 #define u8 __u8 20 #define u16 __u16 21 #define u32 __u32 22 #define u64 __u64 23 24 #define PTR_ERR_ENOTSUP ((void *)-ENOTSUP) 25 26 #ifndef DEBUG_LEVEL 27 #define DEBUG_LEVEL 0 28 #endif 29 #define DPRINT_IN_SIGNAL_BUF_SIZE 4096 30 extern int dprint_in_signal; 31 extern char dprint_in_signal_buffer[DPRINT_IN_SIGNAL_BUF_SIZE]; 32 33 extern int test_nr; 34 extern int iteration_nr; 35 36 #ifdef __GNUC__ 37 __printf(1, 2) 38 #endif 39 static inline void sigsafe_printf(const char *format, ...) 40 { 41 va_list ap; 42 43 if (!dprint_in_signal) { 44 va_start(ap, format); 45 vprintf(format, ap); 46 va_end(ap); 47 } else { 48 int ret; 49 /* 50 * No printf() functions are signal-safe. 51 * They deadlock easily. Write the format 52 * string to get some output, even if 53 * incomplete. 54 */ 55 ret = write(1, format, strlen(format)); 56 if (ret < 0) 57 exit(1); 58 } 59 } 60 #define dprintf_level(level, args...) do { \ 61 if (level <= DEBUG_LEVEL) \ 62 sigsafe_printf(args); \ 63 } while (0) 64 #define dprintf0(args...) dprintf_level(0, args) 65 #define dprintf1(args...) dprintf_level(1, args) 66 #define dprintf2(args...) dprintf_level(2, args) 67 #define dprintf3(args...) dprintf_level(3, args) 68 #define dprintf4(args...) dprintf_level(4, args) 69 70 extern void abort_hooks(void); 71 #define pkey_assert(condition) do { \ 72 if (!(condition)) { \ 73 dprintf0("assert() at %s::%d test_nr: %d iteration: %d\n", \ 74 __FILE__, __LINE__, \ 75 test_nr, iteration_nr); \ 76 dprintf0("errno at assert: %d", errno); \ 77 abort_hooks(); \ 78 exit(__LINE__); \ 79 } \ 80 } while (0) 81 82 #define barrier() __asm__ __volatile__("": : :"memory") 83 #ifndef noinline 84 # define noinline __attribute__((noinline)) 85 #endif 86 87 noinline int read_ptr(int *ptr) 88 { 89 /* Keep GCC from optimizing this away somehow */ 90 barrier(); 91 return *ptr; 92 } 93 94 void expected_pkey_fault(int pkey); 95 int sys_pkey_alloc(unsigned long flags, unsigned long init_val); 96 int sys_pkey_free(unsigned long pkey); 97 int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot, 98 unsigned long pkey); 99 void record_pkey_malloc(void *ptr, long size, int prot); 100 101 #if defined(__i386__) || defined(__x86_64__) /* arch */ 102 #include "pkey-x86.h" 103 #elif defined(__powerpc64__) /* arch */ 104 #include "pkey-powerpc.h" 105 #elif defined(__aarch64__) /* arch */ 106 #include "pkey-arm64.h" 107 #else /* arch */ 108 #error Architecture not supported 109 #endif /* arch */ 110 111 #ifndef PKEY_MASK 112 #define PKEY_MASK (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE) 113 #endif 114 115 #ifndef set_pkey_bits 116 static inline u64 set_pkey_bits(u64 reg, int pkey, u64 flags) 117 { 118 u32 shift = pkey_bit_position(pkey); 119 /* mask out bits from pkey in old value */ 120 reg &= ~((u64)PKEY_MASK << shift); 121 /* OR in new bits for pkey */ 122 reg |= (flags & PKEY_MASK) << shift; 123 return reg; 124 } 125 #endif 126 127 #ifndef get_pkey_bits 128 static inline u64 get_pkey_bits(u64 reg, int pkey) 129 { 130 u32 shift = pkey_bit_position(pkey); 131 /* 132 * shift down the relevant bits to the lowest two, then 133 * mask off all the other higher bits 134 */ 135 return ((reg >> shift) & PKEY_MASK); 136 } 137 #endif 138 139 extern u64 shadow_pkey_reg; 140 141 static inline u64 _read_pkey_reg(int line) 142 { 143 u64 pkey_reg = __read_pkey_reg(); 144 145 dprintf4("read_pkey_reg(line=%d) pkey_reg: %016llx" 146 " shadow: %016llx\n", 147 line, pkey_reg, shadow_pkey_reg); 148 assert(pkey_reg == shadow_pkey_reg); 149 150 return pkey_reg; 151 } 152 153 #define read_pkey_reg() _read_pkey_reg(__LINE__) 154 155 static inline void write_pkey_reg(u64 pkey_reg) 156 { 157 dprintf4("%s() changing %016llx to %016llx\n", __func__, 158 __read_pkey_reg(), pkey_reg); 159 /* will do the shadow check for us: */ 160 read_pkey_reg(); 161 __write_pkey_reg(pkey_reg); 162 shadow_pkey_reg = pkey_reg; 163 dprintf4("%s(%016llx) pkey_reg: %016llx\n", __func__, 164 pkey_reg, __read_pkey_reg()); 165 } 166 167 /* 168 * These are technically racy. since something could 169 * change PKEY register between the read and the write. 170 */ 171 static inline void __pkey_access_allow(int pkey, int do_allow) 172 { 173 u64 pkey_reg = read_pkey_reg(); 174 int bit = pkey * 2; 175 176 if (do_allow) 177 pkey_reg &= (1<<bit); 178 else 179 pkey_reg |= (1<<bit); 180 181 dprintf4("pkey_reg now: %016llx\n", read_pkey_reg()); 182 write_pkey_reg(pkey_reg); 183 } 184 185 static inline void __pkey_write_allow(int pkey, int do_allow_write) 186 { 187 u64 pkey_reg = read_pkey_reg(); 188 int bit = pkey * 2 + 1; 189 190 if (do_allow_write) 191 pkey_reg &= (1<<bit); 192 else 193 pkey_reg |= (1<<bit); 194 195 write_pkey_reg(pkey_reg); 196 dprintf4("pkey_reg now: %016llx\n", read_pkey_reg()); 197 } 198 199 #define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1)) 200 #define ALIGN_DOWN(x, align_to) ((x) & ~((align_to)-1)) 201 #define ALIGN_PTR_UP(p, ptr_align_to) \ 202 ((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to)) 203 #define ALIGN_PTR_DOWN(p, ptr_align_to) \ 204 ((typeof(p))ALIGN_DOWN((unsigned long)(p), ptr_align_to)) 205 #define __stringify_1(x...) #x 206 #define __stringify(x...) __stringify_1(x) 207 208 static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si) 209 { 210 #ifdef si_pkey 211 return &si->si_pkey; 212 #else 213 return (u32 *)(((u8 *)si) + si_pkey_offset); 214 #endif 215 } 216 217 static inline int kernel_has_pkeys(void) 218 { 219 /* try allocating a key and see if it succeeds */ 220 int ret = sys_pkey_alloc(0, 0); 221 if (ret <= 0) { 222 return 0; 223 } 224 sys_pkey_free(ret); 225 return 1; 226 } 227 228 static inline int is_pkeys_supported(void) 229 { 230 /* check if the cpu supports pkeys */ 231 if (!cpu_has_pkeys()) { 232 dprintf1("SKIP: %s: no CPU support\n", __func__); 233 return 0; 234 } 235 236 /* check if the kernel supports pkeys */ 237 if (!kernel_has_pkeys()) { 238 dprintf1("SKIP: %s: no kernel support\n", __func__); 239 return 0; 240 } 241 242 return 1; 243 } 244 245 #endif /* _PKEYS_HELPER_H */ 246