1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_POINTER_AUTH_H
3 #define __ASM_POINTER_AUTH_H
5 #include <linux/bitops.h>
6 #include <linux/prctl.h>
7 #include <linux/random.h>
9 #include <asm/cpufeature.h>
10 #include <asm/memory.h>
11 #include <asm/sysreg.h>
14 * The EL0/EL1 pointer bits used by a pointer authentication code.
15 * This is dependent on TBI0/TBI1 being enabled, or bits 63:56 would also apply.
17 #define ptrauth_user_pac_mask() GENMASK_ULL(54, vabits_actual)
18 #define ptrauth_kernel_pac_mask() GENMASK_ULL(63, vabits_actual)
20 #define PR_PAC_ENABLED_KEYS_MASK \
21 (PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY)
23 #ifdef CONFIG_ARM64_PTR_AUTH
25 * Each key is a 128-bit quantity which is split across a pair of 64-bit
26 * registers (Lo and Hi).
33 * We give each process its own keys, which are shared by all threads. The keys
34 * are inherited upon fork(), and reinitialised upon exec*().
36 struct ptrauth_keys_user
{
37 struct ptrauth_key apia
;
38 struct ptrauth_key apib
;
39 struct ptrauth_key apda
;
40 struct ptrauth_key apdb
;
41 struct ptrauth_key apga
;
44 #define __ptrauth_key_install_nosync(k, v) \
46 struct ptrauth_key __pki_v = (v); \
47 write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
48 write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
51 #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
53 struct ptrauth_keys_kernel
{
54 struct ptrauth_key apia
;
57 static __always_inline
void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel
*keys
)
59 if (system_supports_address_auth())
60 get_random_bytes(&keys
->apia
, sizeof(keys
->apia
));
63 static __always_inline
void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel
*keys
)
65 if (!system_supports_address_auth())
68 __ptrauth_key_install_nosync(APIA
, keys
->apia
);
72 #endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
74 static inline void ptrauth_keys_install_user(struct ptrauth_keys_user
*keys
)
76 if (system_supports_address_auth()) {
77 __ptrauth_key_install_nosync(APIB
, keys
->apib
);
78 __ptrauth_key_install_nosync(APDA
, keys
->apda
);
79 __ptrauth_key_install_nosync(APDB
, keys
->apdb
);
82 if (system_supports_generic_auth())
83 __ptrauth_key_install_nosync(APGA
, keys
->apga
);
86 static inline void ptrauth_keys_init_user(struct ptrauth_keys_user
*keys
)
88 if (system_supports_address_auth()) {
89 get_random_bytes(&keys
->apia
, sizeof(keys
->apia
));
90 get_random_bytes(&keys
->apib
, sizeof(keys
->apib
));
91 get_random_bytes(&keys
->apda
, sizeof(keys
->apda
));
92 get_random_bytes(&keys
->apdb
, sizeof(keys
->apdb
));
95 if (system_supports_generic_auth())
96 get_random_bytes(&keys
->apga
, sizeof(keys
->apga
));
98 ptrauth_keys_install_user(keys
);
101 extern int ptrauth_prctl_reset_keys(struct task_struct
*tsk
, unsigned long arg
);
103 extern int ptrauth_set_enabled_keys(struct task_struct
*tsk
, unsigned long keys
,
104 unsigned long enabled
);
105 extern int ptrauth_get_enabled_keys(struct task_struct
*tsk
);
107 static __always_inline
void ptrauth_enable(void)
109 if (!system_supports_address_auth())
111 sysreg_clear_set(sctlr_el1
, 0, (SCTLR_ELx_ENIA
| SCTLR_ELx_ENIB
|
112 SCTLR_ELx_ENDA
| SCTLR_ELx_ENDB
));
116 #define ptrauth_suspend_exit() \
117 ptrauth_keys_install_user(¤t->thread.keys_user)
119 #define ptrauth_thread_init_user() \
121 ptrauth_keys_init_user(¤t->thread.keys_user); \
123 /* enable all keys */ \
124 if (system_supports_address_auth()) \
125 ptrauth_set_enabled_keys(current, \
126 PR_PAC_ENABLED_KEYS_MASK, \
127 PR_PAC_ENABLED_KEYS_MASK); \
130 #define ptrauth_thread_switch_user(tsk) \
131 ptrauth_keys_install_user(&(tsk)->thread.keys_user)
133 #else /* CONFIG_ARM64_PTR_AUTH */
134 #define ptrauth_enable()
135 #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
136 #define ptrauth_set_enabled_keys(tsk, keys, enabled) (-EINVAL)
137 #define ptrauth_get_enabled_keys(tsk) (-EINVAL)
138 #define ptrauth_suspend_exit()
139 #define ptrauth_thread_init_user()
140 #define ptrauth_thread_switch_user(tsk)
141 #endif /* CONFIG_ARM64_PTR_AUTH */
143 #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
144 #define ptrauth_thread_init_kernel(tsk) \
145 ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
146 #define ptrauth_thread_switch_kernel(tsk) \
147 ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
149 #define ptrauth_thread_init_kernel(tsk)
150 #define ptrauth_thread_switch_kernel(tsk)
151 #endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
153 #endif /* __ASM_POINTER_AUTH_H */