Skip to content

Commit be12984

Browse files
kristina-martsenkoctmarinas
authored andcommitted
arm64: install user ptrauth keys at kernel exit time
As we're going to enable pointer auth within the kernel and use a different APIAKey for the kernel itself, so move the user APIAKey switch to EL0 exception return. The other 4 keys could remain switched during task switch, but are also moved to keep things consistent. Reviewed-by: Kees Cook <[email protected]> Reviewed-by: James Morse <[email protected]> Reviewed-by: Vincenzo Frascino <[email protected]> Signed-off-by: Kristina Martsenko <[email protected]> [Amit: commit msg, re-positioned the patch, comments] Signed-off-by: Amit Daniel Kachhap <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent 91a1b6c commit be12984

File tree

6 files changed

+64
-26
lines changed

6 files changed

+64
-26
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_ASM_POINTER_AUTH_H
3+
#define __ASM_ASM_POINTER_AUTH_H
4+
5+
#include <asm/alternative.h>
6+
#include <asm/asm-offsets.h>
7+
#include <asm/cpufeature.h>
8+
#include <asm/sysreg.h>
9+
10+
#ifdef CONFIG_ARM64_PTR_AUTH
11+
/*
12+
* thread.keys_user.ap* as offset exceeds the #imm offset range
13+
* so use the base value of ldp as thread.keys_user and offset as
14+
* thread.keys_user.ap*.
15+
*/
16+
.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
17+
mov \tmp1, #THREAD_KEYS_USER
18+
add \tmp1, \tsk, \tmp1
19+
alternative_if_not ARM64_HAS_ADDRESS_AUTH
20+
b .Laddr_auth_skip_\@
21+
alternative_else_nop_endif
22+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIA]
23+
msr_s SYS_APIAKEYLO_EL1, \tmp2
24+
msr_s SYS_APIAKEYHI_EL1, \tmp3
25+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIB]
26+
msr_s SYS_APIBKEYLO_EL1, \tmp2
27+
msr_s SYS_APIBKEYHI_EL1, \tmp3
28+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APDA]
29+
msr_s SYS_APDAKEYLO_EL1, \tmp2
30+
msr_s SYS_APDAKEYHI_EL1, \tmp3
31+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APDB]
32+
msr_s SYS_APDBKEYLO_EL1, \tmp2
33+
msr_s SYS_APDBKEYHI_EL1, \tmp3
34+
.Laddr_auth_skip_\@:
35+
alternative_if ARM64_HAS_GENERIC_AUTH
36+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APGA]
37+
msr_s SYS_APGAKEYLO_EL1, \tmp2
38+
msr_s SYS_APGAKEYHI_EL1, \tmp3
39+
alternative_else_nop_endif
40+
.endm
41+
42+
#else /* CONFIG_ARM64_PTR_AUTH */
43+
44+
.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
45+
.endm
46+
47+
#endif /* CONFIG_ARM64_PTR_AUTH */
48+
49+
#endif /* __ASM_ASM_POINTER_AUTH_H */

arch/arm64/include/asm/pointer_auth.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,6 @@ do { \
5050
write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
5151
} while (0)
5252

53-
static inline void ptrauth_keys_switch_user(struct ptrauth_keys_user *keys)
54-
{
55-
if (system_supports_address_auth()) {
56-
__ptrauth_key_install(APIA, keys->apia);
57-
__ptrauth_key_install(APIB, keys->apib);
58-
__ptrauth_key_install(APDA, keys->apda);
59-
__ptrauth_key_install(APDB, keys->apdb);
60-
}
61-
62-
if (system_supports_generic_auth())
63-
__ptrauth_key_install(APGA, keys->apga);
64-
}
65-
6653
extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
6754

6855
/*
@@ -78,20 +65,12 @@ static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr)
7865
}
7966

8067
#define ptrauth_thread_init_user(tsk) \
81-
do { \
82-
struct task_struct *__ptiu_tsk = (tsk); \
83-
ptrauth_keys_init_user(&__ptiu_tsk->thread.keys_user); \
84-
ptrauth_keys_switch_user(&__ptiu_tsk->thread.keys_user); \
85-
} while (0)
86-
87-
#define ptrauth_thread_switch(tsk) \
88-
ptrauth_keys_switch_user(&(tsk)->thread.keys_user)
68+
ptrauth_keys_init_user(&(tsk)->thread.keys_user)
8969

9070
#else /* CONFIG_ARM64_PTR_AUTH */
9171
#define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
9272
#define ptrauth_strip_insn_pac(lr) (lr)
9373
#define ptrauth_thread_init_user(tsk)
94-
#define ptrauth_thread_switch(tsk)
9574
#endif /* CONFIG_ARM64_PTR_AUTH */
9675

9776
#endif /* __ASM_POINTER_AUTH_H */

arch/arm64/kernel/asm-offsets.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ int main(void)
4040
#endif
4141
BLANK();
4242
DEFINE(THREAD_CPU_CONTEXT, offsetof(struct task_struct, thread.cpu_context));
43+
#ifdef CONFIG_ARM64_PTR_AUTH
44+
DEFINE(THREAD_KEYS_USER, offsetof(struct task_struct, thread.keys_user));
45+
#endif
4346
BLANK();
4447
DEFINE(S_X0, offsetof(struct pt_regs, regs[0]));
4548
DEFINE(S_X2, offsetof(struct pt_regs, regs[2]));
@@ -127,6 +130,14 @@ int main(void)
127130
#ifdef CONFIG_ARM_SDE_INTERFACE
128131
DEFINE(SDEI_EVENT_INTREGS, offsetof(struct sdei_registered_event, interrupted_regs));
129132
DEFINE(SDEI_EVENT_PRIORITY, offsetof(struct sdei_registered_event, priority));
133+
#endif
134+
#ifdef CONFIG_ARM64_PTR_AUTH
135+
DEFINE(PTRAUTH_USER_KEY_APIA, offsetof(struct ptrauth_keys_user, apia));
136+
DEFINE(PTRAUTH_USER_KEY_APIB, offsetof(struct ptrauth_keys_user, apib));
137+
DEFINE(PTRAUTH_USER_KEY_APDA, offsetof(struct ptrauth_keys_user, apda));
138+
DEFINE(PTRAUTH_USER_KEY_APDB, offsetof(struct ptrauth_keys_user, apdb));
139+
DEFINE(PTRAUTH_USER_KEY_APGA, offsetof(struct ptrauth_keys_user, apga));
140+
BLANK();
130141
#endif
131142
return 0;
132143
}

arch/arm64/kernel/entry.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <asm/alternative.h>
1515
#include <asm/assembler.h>
1616
#include <asm/asm-offsets.h>
17+
#include <asm/asm_pointer_auth.h>
1718
#include <asm/cpufeature.h>
1819
#include <asm/errno.h>
1920
#include <asm/esr.h>
@@ -341,6 +342,8 @@ alternative_else_nop_endif
341342
msr cntkctl_el1, x1
342343
4:
343344
#endif
345+
ptrauth_keys_install_user tsk, x0, x1, x2
346+
344347
apply_ssbd 0, x0, x1
345348
.endif
346349

arch/arm64/kernel/pointer_auth.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
1919

2020
if (!arg) {
2121
ptrauth_keys_init_user(keys);
22-
ptrauth_keys_switch_user(keys);
2322
return 0;
2423
}
2524

@@ -41,7 +40,5 @@ int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
4140
if (arg & PR_PAC_APGAKEY)
4241
get_random_bytes(&keys->apga, sizeof(keys->apga));
4342

44-
ptrauth_keys_switch_user(keys);
45-
4643
return 0;
4744
}

arch/arm64/kernel/process.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
512512
contextidr_thread_switch(next);
513513
entry_task_switch(next);
514514
uao_thread_switch(next);
515-
ptrauth_thread_switch(next);
516515
ssbs_thread_switch(next);
517516

518517
/*

0 commit comments

Comments
 (0)