]>
Commit | Line | Data |
---|---|---|
22e6c808 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
75031975 MR |
2 | #ifndef __ASM_POINTER_AUTH_H |
3 | #define __ASM_POINTER_AUTH_H | |
4 | ||
ec6e822d | 5 | #include <linux/bitops.h> |
75031975 MR |
6 | #include <linux/random.h> |
7 | ||
8 | #include <asm/cpufeature.h> | |
ec6e822d | 9 | #include <asm/memory.h> |
75031975 MR |
10 | #include <asm/sysreg.h> |
11 | ||
12 | #ifdef CONFIG_ARM64_PTR_AUTH | |
13 | /* | |
14 | * Each key is a 128-bit quantity which is split across a pair of 64-bit | |
15 | * registers (Lo and Hi). | |
16 | */ | |
17 | struct ptrauth_key { | |
18 | unsigned long lo, hi; | |
19 | }; | |
20 | ||
21 | /* | |
22 | * We give each process its own keys, which are shared by all threads. The keys | |
23 | * are inherited upon fork(), and reinitialised upon exec*(). | |
24 | */ | |
91a1b6cc | 25 | struct ptrauth_keys_user { |
75031975 MR |
26 | struct ptrauth_key apia; |
27 | struct ptrauth_key apib; | |
28 | struct ptrauth_key apda; | |
29 | struct ptrauth_key apdb; | |
30 | struct ptrauth_key apga; | |
31 | }; | |
32 | ||
33e45234 KM |
33 | struct ptrauth_keys_kernel { |
34 | struct ptrauth_key apia; | |
35 | }; | |
36 | ||
91a1b6cc | 37 | static inline void ptrauth_keys_init_user(struct ptrauth_keys_user *keys) |
75031975 MR |
38 | { |
39 | if (system_supports_address_auth()) { | |
40 | get_random_bytes(&keys->apia, sizeof(keys->apia)); | |
41 | get_random_bytes(&keys->apib, sizeof(keys->apib)); | |
42 | get_random_bytes(&keys->apda, sizeof(keys->apda)); | |
43 | get_random_bytes(&keys->apdb, sizeof(keys->apdb)); | |
44 | } | |
45 | ||
46 | if (system_supports_generic_auth()) | |
47 | get_random_bytes(&keys->apga, sizeof(keys->apga)); | |
48 | } | |
49 | ||
3fabb438 | 50 | #define __ptrauth_key_install_nosync(k, v) \ |
75031975 MR |
51 | do { \ |
52 | struct ptrauth_key __pki_v = (v); \ | |
53 | write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \ | |
54 | write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \ | |
55 | } while (0) | |
56 | ||
28321582 | 57 | static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys) |
33e45234 KM |
58 | { |
59 | if (system_supports_address_auth()) | |
60 | get_random_bytes(&keys->apia, sizeof(keys->apia)); | |
61 | } | |
62 | ||
28321582 ADK |
63 | static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys) |
64 | { | |
3fabb438 MR |
65 | if (!system_supports_address_auth()) |
66 | return; | |
67 | ||
68 | __ptrauth_key_install_nosync(APIA, keys->apia); | |
69 | isb(); | |
28321582 ADK |
70 | } |
71 | ||
ba830885 KM |
72 | extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg); |
73 | ||
ccc43810 MR |
74 | static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr) |
75 | { | |
689eae42 | 76 | return ptrauth_clear_pac(ptr); |
ccc43810 MR |
77 | } |
78 | ||
75031975 | 79 | #define ptrauth_thread_init_user(tsk) \ |
be129842 | 80 | ptrauth_keys_init_user(&(tsk)->thread.keys_user) |
33e45234 KM |
81 | #define ptrauth_thread_init_kernel(tsk) \ |
82 | ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel) | |
28321582 ADK |
83 | #define ptrauth_thread_switch_kernel(tsk) \ |
84 | ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel) | |
75031975 MR |
85 | |
86 | #else /* CONFIG_ARM64_PTR_AUTH */ | |
ba830885 | 87 | #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL) |
ccc43810 | 88 | #define ptrauth_strip_insn_pac(lr) (lr) |
75031975 | 89 | #define ptrauth_thread_init_user(tsk) |
33e45234 | 90 | #define ptrauth_thread_init_kernel(tsk) |
28321582 | 91 | #define ptrauth_thread_switch_kernel(tsk) |
75031975 MR |
92 | #endif /* CONFIG_ARM64_PTR_AUTH */ |
93 | ||
94 | #endif /* __ASM_POINTER_AUTH_H */ |