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 <linux/types.h>
17 
18 #include "../kselftest.h"
19 
20 /* Define some kernel-like types */
21 typedef __u8	u8;
22 typedef __u16	u16;
23 typedef __u32	u32;
24 typedef __u64	u64;
25 
26 #define PTR_ERR_ENOTSUP ((void *)-ENOTSUP)
27 
28 #ifndef DEBUG_LEVEL
29 #define DEBUG_LEVEL 0
30 #endif
31 extern int dprint_in_signal;
32 
33 extern int test_nr;
34 extern int iteration_nr;
35 
36 #ifdef __GNUC__
37 __printf(1, 2)
38 #endif
sigsafe_printf(const char * format,...)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 #ifndef __maybe_unused
87 # define __maybe_unused __attribute__((__unused__))
88 #endif
89 
90 int sys_pkey_alloc(unsigned long flags, unsigned long init_val);
91 int sys_pkey_free(unsigned long pkey);
92 int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
93 		unsigned long pkey);
94 
95 /* For functions called from protection_keys.c only */
96 noinline int read_ptr(int *ptr);
97 void expected_pkey_fault(int pkey);
98 int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
99 		unsigned long pkey);
100 void record_pkey_malloc(void *ptr, long size, int prot);
101 
102 #if defined(__i386__) || defined(__x86_64__) /* arch */
103 #include "pkey-x86.h"
104 #elif defined(__powerpc64__) /* arch */
105 #include "pkey-powerpc.h"
106 #elif defined(__aarch64__) /* arch */
107 #include "pkey-arm64.h"
108 #else /* arch */
109 #error Architecture not supported
110 #endif /* arch */
111 
112 #ifndef PKEY_MASK
113 #define PKEY_MASK	(PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE)
114 #endif
115 
116 /*
117  * FIXME: Remove once the generic PKEY_UNRESTRICTED definition is merged.
118  */
119 #ifndef PKEY_UNRESTRICTED
120 #define PKEY_UNRESTRICTED 0x0
121 #endif
122 
123 #ifndef set_pkey_bits
set_pkey_bits(u64 reg,int pkey,u64 flags)124 static inline u64 set_pkey_bits(u64 reg, int pkey, u64 flags)
125 {
126 	u32 shift = pkey_bit_position(pkey);
127 	/* mask out bits from pkey in old value */
128 	reg &= ~((u64)PKEY_MASK << shift);
129 	/* OR in new bits for pkey */
130 	reg |= (flags & PKEY_MASK) << shift;
131 	return reg;
132 }
133 #endif
134 
135 #ifndef get_pkey_bits
get_pkey_bits(u64 reg,int pkey)136 static inline u64 get_pkey_bits(u64 reg, int pkey)
137 {
138 	u32 shift = pkey_bit_position(pkey);
139 	/*
140 	 * shift down the relevant bits to the lowest two, then
141 	 * mask off all the other higher bits
142 	 */
143 	return ((reg >> shift) & PKEY_MASK);
144 }
145 #endif
146 
147 extern u64 shadow_pkey_reg;
148 
_read_pkey_reg(int line)149 static inline u64 _read_pkey_reg(int line)
150 {
151 	u64 pkey_reg = __read_pkey_reg();
152 
153 	dprintf4("read_pkey_reg(line=%d) pkey_reg: %016llx"
154 			" shadow: %016llx\n",
155 			line, pkey_reg, shadow_pkey_reg);
156 	assert(pkey_reg == shadow_pkey_reg);
157 
158 	return pkey_reg;
159 }
160 
161 #define read_pkey_reg() _read_pkey_reg(__LINE__)
162 
write_pkey_reg(u64 pkey_reg)163 static inline void write_pkey_reg(u64 pkey_reg)
164 {
165 	dprintf4("%s() changing %016llx to %016llx\n", __func__,
166 			__read_pkey_reg(), pkey_reg);
167 	/* will do the shadow check for us: */
168 	read_pkey_reg();
169 	__write_pkey_reg(pkey_reg);
170 	shadow_pkey_reg = pkey_reg;
171 	dprintf4("%s(%016llx) pkey_reg: %016llx\n", __func__,
172 			pkey_reg, __read_pkey_reg());
173 }
174 
175 #define ALIGN_UP(x, align_to)	(((x) + ((align_to)-1)) & ~((align_to)-1))
176 #define ALIGN_DOWN(x, align_to) ((x) & ~((align_to)-1))
177 #define ALIGN_PTR_UP(p, ptr_align_to)	\
178 	((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to))
179 #define ALIGN_PTR_DOWN(p, ptr_align_to)	\
180 	((typeof(p))ALIGN_DOWN((unsigned long)(p), ptr_align_to))
181 #define __stringify_1(x...)     #x
182 #define __stringify(x...)       __stringify_1(x)
183 
siginfo_get_pkey_ptr(siginfo_t * si)184 static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
185 {
186 #ifdef si_pkey
187 	return &si->si_pkey;
188 #else
189 	return (u32 *)(((u8 *)si) + si_pkey_offset);
190 #endif
191 }
192 
kernel_has_pkeys(void)193 static inline int kernel_has_pkeys(void)
194 {
195 	/* try allocating a key and see if it succeeds */
196 	int ret = sys_pkey_alloc(0, 0);
197 	if (ret <= 0) {
198 		return 0;
199 	}
200 	sys_pkey_free(ret);
201 	return 1;
202 }
203 
is_pkeys_supported(void)204 static inline int is_pkeys_supported(void)
205 {
206 	/* check if the cpu supports pkeys */
207 	if (!cpu_has_pkeys()) {
208 		dprintf1("SKIP: %s: no CPU support\n", __func__);
209 		return 0;
210 	}
211 
212 	/* check if the kernel supports pkeys */
213 	if (!kernel_has_pkeys()) {
214 		dprintf1("SKIP: %s: no kernel support\n", __func__);
215 		return 0;
216 	}
217 
218 	return 1;
219 }
220 
221 #endif /* _PKEYS_HELPER_H */
222