1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_POINTER_AUTH_H
3 #define __ASM_POINTER_AUTH_H
4
5 #include <linux/bitops.h>
6 #include <linux/prctl.h>
7 #include <linux/random.h>
8
9 #include <asm/cpufeature.h>
10 #include <asm/memory.h>
11 #include <asm/sysreg.h>
12
13 #define PR_PAC_ENABLED_KEYS_MASK \
14 (PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY)
15
16 #ifdef CONFIG_ARM64_PTR_AUTH
17 /*
18 * Each key is a 128-bit quantity which is split across a pair of 64-bit
19 * registers (Lo and Hi).
20 */
21 struct ptrauth_key {
22 unsigned long lo, hi;
23 };
24
25 /*
26 * We give each process its own keys, which are shared by all threads. The keys
27 * are inherited upon fork(), and reinitialised upon exec*().
28 */
29 struct ptrauth_keys_user {
30 struct ptrauth_key apia;
31 struct ptrauth_key apib;
32 struct ptrauth_key apda;
33 struct ptrauth_key apdb;
34 struct ptrauth_key apga;
35 };
36
37 struct ptrauth_keys_kernel {
38 struct ptrauth_key apia;
39 };
40
41 #define __ptrauth_key_install_nosync(k, v) \
42 do { \
43 struct ptrauth_key __pki_v = (v); \
44 write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
45 write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
46 } while (0)
47
ptrauth_keys_install_user(struct ptrauth_keys_user * keys)48 static inline void ptrauth_keys_install_user(struct ptrauth_keys_user *keys)
49 {
50 if (system_supports_address_auth()) {
51 __ptrauth_key_install_nosync(APIB, keys->apib);
52 __ptrauth_key_install_nosync(APDA, keys->apda);
53 __ptrauth_key_install_nosync(APDB, keys->apdb);
54 }
55
56 if (system_supports_generic_auth())
57 __ptrauth_key_install_nosync(APGA, keys->apga);
58 }
59
ptrauth_keys_init_user(struct ptrauth_keys_user * keys)60 static inline void ptrauth_keys_init_user(struct ptrauth_keys_user *keys)
61 {
62 if (system_supports_address_auth()) {
63 get_random_bytes(&keys->apia, sizeof(keys->apia));
64 get_random_bytes(&keys->apib, sizeof(keys->apib));
65 get_random_bytes(&keys->apda, sizeof(keys->apda));
66 get_random_bytes(&keys->apdb, sizeof(keys->apdb));
67 }
68
69 if (system_supports_generic_auth())
70 get_random_bytes(&keys->apga, sizeof(keys->apga));
71
72 ptrauth_keys_install_user(keys);
73 }
74
ptrauth_keys_init_kernel(struct ptrauth_keys_kernel * keys)75 static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
76 {
77 if (system_supports_address_auth())
78 get_random_bytes(&keys->apia, sizeof(keys->apia));
79 }
80
ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel * keys)81 static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys)
82 {
83 if (!system_supports_address_auth())
84 return;
85
86 __ptrauth_key_install_nosync(APIA, keys->apia);
87 isb();
88 }
89
90 extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
91
92 extern int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
93 unsigned long enabled);
94 extern int ptrauth_get_enabled_keys(struct task_struct *tsk);
95
ptrauth_strip_insn_pac(unsigned long ptr)96 static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr)
97 {
98 return ptrauth_clear_pac(ptr);
99 }
100
ptrauth_enable(void)101 static __always_inline void ptrauth_enable(void)
102 {
103 if (!system_supports_address_auth())
104 return;
105 sysreg_clear_set(sctlr_el1, 0, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB |
106 SCTLR_ELx_ENDA | SCTLR_ELx_ENDB));
107 isb();
108 }
109
110 #define ptrauth_suspend_exit() \
111 ptrauth_keys_install_user(¤t->thread.keys_user)
112
113 #define ptrauth_thread_init_user() \
114 do { \
115 ptrauth_keys_init_user(¤t->thread.keys_user); \
116 \
117 /* enable all keys */ \
118 if (system_supports_address_auth()) \
119 ptrauth_set_enabled_keys(current, \
120 PR_PAC_ENABLED_KEYS_MASK, \
121 PR_PAC_ENABLED_KEYS_MASK); \
122 } while (0)
123
124 #define ptrauth_thread_switch_user(tsk) \
125 ptrauth_keys_install_user(&(tsk)->thread.keys_user)
126
127 #define ptrauth_thread_init_kernel(tsk) \
128 ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
129 #define ptrauth_thread_switch_kernel(tsk) \
130 ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
131
132 #else /* CONFIG_ARM64_PTR_AUTH */
133 #define ptrauth_enable()
134 #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
135 #define ptrauth_set_enabled_keys(tsk, keys, enabled) (-EINVAL)
136 #define ptrauth_get_enabled_keys(tsk) (-EINVAL)
137 #define ptrauth_strip_insn_pac(lr) (lr)
138 #define ptrauth_suspend_exit()
139 #define ptrauth_thread_init_user()
140 #define ptrauth_thread_init_kernel(tsk)
141 #define ptrauth_thread_switch_user(tsk)
142 #define ptrauth_thread_switch_kernel(tsk)
143 #endif /* CONFIG_ARM64_PTR_AUTH */
144
145 #endif /* __ASM_POINTER_AUTH_H */
146