Skip to content

Commit 2832158

Browse files
amitdanielkachhapctmarinas
authored andcommitted
arm64: initialize ptrauth keys for kernel booting task
This patch uses the existing boot_init_stack_canary arch function to initialize the ptrauth keys for the booting task in the primary core. The requirement here is that it should be always inline and the caller must never return. As pointer authentication too detects a subset of stack corruption so it makes sense to place this code here. Both pointer authentication and stack canary codes are protected by their respective config option. Suggested-by: Ard Biesheuvel <[email protected]> Signed-off-by: Amit Daniel Kachhap <[email protected]> Reviewed-by: Vincenzo Frascino <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent 33e4523 commit 2832158

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

arch/arm64/include/asm/pointer_auth.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ do { \
5454
write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
5555
} while (0)
5656

57-
static inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
57+
static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
5858
{
5959
if (system_supports_address_auth())
6060
get_random_bytes(&keys->apia, sizeof(keys->apia));
6161
}
6262

63+
static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys)
64+
{
65+
if (system_supports_address_auth())
66+
__ptrauth_key_install(APIA, keys->apia);
67+
}
68+
6369
extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
6470

6571
/*
@@ -78,12 +84,15 @@ static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr)
7884
ptrauth_keys_init_user(&(tsk)->thread.keys_user)
7985
#define ptrauth_thread_init_kernel(tsk) \
8086
ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
87+
#define ptrauth_thread_switch_kernel(tsk) \
88+
ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
8189

8290
#else /* CONFIG_ARM64_PTR_AUTH */
8391
#define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
8492
#define ptrauth_strip_insn_pac(lr) (lr)
8593
#define ptrauth_thread_init_user(tsk)
8694
#define ptrauth_thread_init_kernel(tsk)
95+
#define ptrauth_thread_switch_kernel(tsk)
8796
#endif /* CONFIG_ARM64_PTR_AUTH */
8897

8998
#endif /* __ASM_POINTER_AUTH_H */

arch/arm64/include/asm/stackprotector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <linux/random.h>
1717
#include <linux/version.h>
18+
#include <asm/pointer_auth.h>
1819

1920
extern unsigned long __stack_chk_guard;
2021

@@ -26,6 +27,7 @@ extern unsigned long __stack_chk_guard;
2627
*/
2728
static __always_inline void boot_init_stack_canary(void)
2829
{
30+
#if defined(CONFIG_STACKPROTECTOR)
2931
unsigned long canary;
3032

3133
/* Try to get a semi random initial value. */
@@ -36,6 +38,9 @@ static __always_inline void boot_init_stack_canary(void)
3638
current->stack_canary = canary;
3739
if (!IS_ENABLED(CONFIG_STACKPROTECTOR_PER_TASK))
3840
__stack_chk_guard = current->stack_canary;
41+
#endif
42+
ptrauth_thread_init_kernel(current);
43+
ptrauth_thread_switch_kernel(current);
3944
}
4045

4146
#endif /* _ASM_STACKPROTECTOR_H */

include/linux/stackprotector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <linux/sched.h>
77
#include <linux/random.h>
88

9-
#ifdef CONFIG_STACKPROTECTOR
9+
#if defined(CONFIG_STACKPROTECTOR) || defined(CONFIG_ARM64_PTR_AUTH)
1010
# include <asm/stackprotector.h>
1111
#else
1212
static inline void boot_init_stack_canary(void)

0 commit comments

Comments
 (0)