Skip to content

Commit bd23fdb

Browse files
committed
Merge branch 'for-next/ptrauth' into for-next/core
Allow Pointer Authentication to be configured independently for kernel and userspace. * for-next/ptrauth: arm64: Conditionally configure PTR_AUTH key of the kernel. arm64: Add ARM64_PTR_AUTH_KERNEL config option
2 parents 2e5d34d + d053e71 commit bd23fdb

7 files changed

Lines changed: 91 additions & 64 deletions

File tree

arch/arm64/Kconfig

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,12 +1481,6 @@ menu "ARMv8.3 architectural features"
14811481
config ARM64_PTR_AUTH
14821482
bool "Enable support for pointer authentication"
14831483
default y
1484-
depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_PAC
1485-
# Modern compilers insert a .note.gnu.property section note for PAC
1486-
# which is only understood by binutils starting with version 2.33.1.
1487-
depends on LD_IS_LLD || LD_VERSION >= 23301 || (CC_IS_GCC && GCC_VERSION < 90100)
1488-
depends on !CC_IS_CLANG || AS_HAS_CFI_NEGATE_RA_STATE
1489-
depends on (!FUNCTION_GRAPH_TRACER || DYNAMIC_FTRACE_WITH_REGS)
14901484
help
14911485
Pointer authentication (part of the ARMv8.3 Extensions) provides
14921486
instructions for signing and authenticating pointers against secret
@@ -1498,13 +1492,6 @@ config ARM64_PTR_AUTH
14981492
for each process at exec() time, with these keys being
14991493
context-switched along with the process.
15001494

1501-
If the compiler supports the -mbranch-protection or
1502-
-msign-return-address flag (e.g. GCC 7 or later), then this option
1503-
will also cause the kernel itself to be compiled with return address
1504-
protection. In this case, and if the target hardware is known to
1505-
support pointer authentication, then CONFIG_STACKPROTECTOR can be
1506-
disabled with minimal loss of protection.
1507-
15081495
The feature is detected at runtime. If the feature is not present in
15091496
hardware it will not be advertised to userspace/KVM guest nor will it
15101497
be enabled.
@@ -1515,6 +1502,24 @@ config ARM64_PTR_AUTH
15151502
but with the feature disabled. On such a system, this option should
15161503
not be selected.
15171504

1505+
config ARM64_PTR_AUTH_KERNEL
1506+
bool "Use pointer authentication for kernel"
1507+
default y
1508+
depends on ARM64_PTR_AUTH
1509+
depends on (CC_HAS_SIGN_RETURN_ADDRESS || CC_HAS_BRANCH_PROT_PAC_RET) && AS_HAS_PAC
1510+
# Modern compilers insert a .note.gnu.property section note for PAC
1511+
# which is only understood by binutils starting with version 2.33.1.
1512+
depends on LD_IS_LLD || LD_VERSION >= 23301 || (CC_IS_GCC && GCC_VERSION < 90100)
1513+
depends on !CC_IS_CLANG || AS_HAS_CFI_NEGATE_RA_STATE
1514+
depends on (!FUNCTION_GRAPH_TRACER || DYNAMIC_FTRACE_WITH_REGS)
1515+
help
1516+
If the compiler supports the -mbranch-protection or
1517+
-msign-return-address flag (e.g. GCC 7 or later), then this option
1518+
will cause the kernel itself to be compiled with return address
1519+
protection. In this case, and if the target hardware is known to
1520+
support pointer authentication, then CONFIG_STACKPROTECTOR can be
1521+
disabled with minimal loss of protection.
1522+
15181523
This feature works with FUNCTION_GRAPH_TRACER option only if
15191524
DYNAMIC_FTRACE_WITH_REGS is enabled.
15201525

@@ -1606,7 +1611,7 @@ config ARM64_BTI_KERNEL
16061611
bool "Use Branch Target Identification for kernel"
16071612
default y
16081613
depends on ARM64_BTI
1609-
depends on ARM64_PTR_AUTH
1614+
depends on ARM64_PTR_AUTH_KERNEL
16101615
depends on CC_HAS_BRANCH_PROT_PAC_RET_BTI
16111616
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94697
16121617
depends on !CC_IS_GCC || GCC_VERSION >= 100100

arch/arm64/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ endif
7070
# off, this will be overridden if we are using branch protection.
7171
branch-prot-flags-y += $(call cc-option,-mbranch-protection=none)
7272

73-
ifeq ($(CONFIG_ARM64_PTR_AUTH),y)
73+
ifeq ($(CONFIG_ARM64_PTR_AUTH_KERNEL),y)
7474
branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all
7575
# We enable additional protection for leaf functions as there is some
7676
# narrow potential for ROP protection benefits and no substantial

arch/arm64/include/asm/asm_pointer_auth.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,7 @@
77
#include <asm/cpufeature.h>
88
#include <asm/sysreg.h>
99

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-
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIA]
20-
msr_s SYS_APIAKEYLO_EL1, \tmp2
21-
msr_s SYS_APIAKEYHI_EL1, \tmp3
22-
.endm
10+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
2311

2412
.macro __ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
2513
mov \tmp1, #THREAD_KEYS_KERNEL
@@ -42,6 +30,33 @@ alternative_if ARM64_HAS_ADDRESS_AUTH
4230
alternative_else_nop_endif
4331
.endm
4432

33+
#else /* CONFIG_ARM64_PTR_AUTH_KERNEL */
34+
35+
.macro __ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
36+
.endm
37+
38+
.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
39+
.endm
40+
41+
.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
42+
.endm
43+
44+
#endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
45+
46+
#ifdef CONFIG_ARM64_PTR_AUTH
47+
/*
48+
* thread.keys_user.ap* as offset exceeds the #imm offset range
49+
* so use the base value of ldp as thread.keys_user and offset as
50+
* thread.keys_user.ap*.
51+
*/
52+
.macro __ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
53+
mov \tmp1, #THREAD_KEYS_USER
54+
add \tmp1, \tsk, \tmp1
55+
ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_USER_KEY_APIA]
56+
msr_s SYS_APIAKEYLO_EL1, \tmp2
57+
msr_s SYS_APIAKEYHI_EL1, \tmp3
58+
.endm
59+
4560
.macro __ptrauth_keys_init_cpu tsk, tmp1, tmp2, tmp3
4661
mrs \tmp1, id_aa64isar1_el1
4762
ubfx \tmp1, \tmp1, #ID_AA64ISAR1_APA_SHIFT, #8
@@ -64,17 +79,11 @@ alternative_else_nop_endif
6479
.Lno_addr_auth\@:
6580
.endm
6681

67-
#else /* CONFIG_ARM64_PTR_AUTH */
82+
#else /* !CONFIG_ARM64_PTR_AUTH */
6883

6984
.macro ptrauth_keys_install_user tsk, tmp1, tmp2, tmp3
7085
.endm
7186

72-
.macro ptrauth_keys_install_kernel_nosync tsk, tmp1, tmp2, tmp3
73-
.endm
74-
75-
.macro ptrauth_keys_install_kernel tsk, tmp1, tmp2, tmp3
76-
.endm
77-
7887
#endif /* CONFIG_ARM64_PTR_AUTH */
7988

8089
#endif /* __ASM_ASM_POINTER_AUTH_H */

arch/arm64/include/asm/pointer_auth.h

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,36 @@ struct ptrauth_keys_user {
3131
struct ptrauth_key apga;
3232
};
3333

34-
struct ptrauth_keys_kernel {
35-
struct ptrauth_key apia;
36-
};
37-
3834
#define __ptrauth_key_install_nosync(k, v) \
3935
do { \
4036
struct ptrauth_key __pki_v = (v); \
4137
write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
4238
write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
4339
} while (0)
4440

41+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
42+
43+
struct ptrauth_keys_kernel {
44+
struct ptrauth_key apia;
45+
};
46+
47+
static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
48+
{
49+
if (system_supports_address_auth())
50+
get_random_bytes(&keys->apia, sizeof(keys->apia));
51+
}
52+
53+
static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys)
54+
{
55+
if (!system_supports_address_auth())
56+
return;
57+
58+
__ptrauth_key_install_nosync(APIA, keys->apia);
59+
isb();
60+
}
61+
62+
#endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
63+
4564
static inline void ptrauth_keys_install_user(struct ptrauth_keys_user *keys)
4665
{
4766
if (system_supports_address_auth()) {
@@ -69,21 +88,6 @@ static inline void ptrauth_keys_init_user(struct ptrauth_keys_user *keys)
6988
ptrauth_keys_install_user(keys);
7089
}
7190

72-
static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
73-
{
74-
if (system_supports_address_auth())
75-
get_random_bytes(&keys->apia, sizeof(keys->apia));
76-
}
77-
78-
static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys)
79-
{
80-
if (!system_supports_address_auth())
81-
return;
82-
83-
__ptrauth_key_install_nosync(APIA, keys->apia);
84-
isb();
85-
}
86-
8791
extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
8892

8993
extern int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
@@ -121,11 +125,6 @@ static __always_inline void ptrauth_enable(void)
121125
#define ptrauth_thread_switch_user(tsk) \
122126
ptrauth_keys_install_user(&(tsk)->thread.keys_user)
123127

124-
#define ptrauth_thread_init_kernel(tsk) \
125-
ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
126-
#define ptrauth_thread_switch_kernel(tsk) \
127-
ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
128-
129128
#else /* CONFIG_ARM64_PTR_AUTH */
130129
#define ptrauth_enable()
131130
#define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
@@ -134,11 +133,19 @@ static __always_inline void ptrauth_enable(void)
134133
#define ptrauth_strip_insn_pac(lr) (lr)
135134
#define ptrauth_suspend_exit()
136135
#define ptrauth_thread_init_user()
137-
#define ptrauth_thread_init_kernel(tsk)
138136
#define ptrauth_thread_switch_user(tsk)
139-
#define ptrauth_thread_switch_kernel(tsk)
140137
#endif /* CONFIG_ARM64_PTR_AUTH */
141138

139+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
140+
#define ptrauth_thread_init_kernel(tsk) \
141+
ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
142+
#define ptrauth_thread_switch_kernel(tsk) \
143+
ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
144+
#else
145+
#define ptrauth_thread_init_kernel(tsk)
146+
#define ptrauth_thread_switch_kernel(tsk)
147+
#endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
148+
142149
#define PR_PAC_ENABLED_KEYS_MASK \
143150
(PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY)
144151

arch/arm64/include/asm/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ struct thread_struct {
148148
struct debug_info debug; /* debugging */
149149
#ifdef CONFIG_ARM64_PTR_AUTH
150150
struct ptrauth_keys_user keys_user;
151+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
151152
struct ptrauth_keys_kernel keys_kernel;
152153
#endif
154+
#endif
153155
#ifdef CONFIG_ARM64_MTE
154156
u64 gcr_user_excl;
155157
#endif

arch/arm64/kernel/asm-offsets.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ int main(void)
4747
DEFINE(THREAD_SCTLR_USER, offsetof(struct task_struct, thread.sctlr_user));
4848
#ifdef CONFIG_ARM64_PTR_AUTH
4949
DEFINE(THREAD_KEYS_USER, offsetof(struct task_struct, thread.keys_user));
50+
#endif
51+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
5052
DEFINE(THREAD_KEYS_KERNEL, offsetof(struct task_struct, thread.keys_kernel));
5153
#endif
5254
#ifdef CONFIG_ARM64_MTE
@@ -153,7 +155,9 @@ int main(void)
153155
#endif
154156
#ifdef CONFIG_ARM64_PTR_AUTH
155157
DEFINE(PTRAUTH_USER_KEY_APIA, offsetof(struct ptrauth_keys_user, apia));
158+
#ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
156159
DEFINE(PTRAUTH_KERNEL_KEY_APIA, offsetof(struct ptrauth_keys_kernel, apia));
160+
#endif
157161
BLANK();
158162
#endif
159163
return 0;

drivers/misc/lkdtm/bugs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ void lkdtm_DOUBLE_FAULT(void)
463463
#ifdef CONFIG_ARM64
464464
static noinline void change_pac_parameters(void)
465465
{
466-
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) {
466+
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
467467
/* Reset the keys of current task */
468468
ptrauth_thread_init_kernel(current);
469469
ptrauth_thread_switch_kernel(current);
@@ -477,8 +477,8 @@ noinline void lkdtm_CORRUPT_PAC(void)
477477
#define CORRUPT_PAC_ITERATE 10
478478
int i;
479479

480-
if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH))
481-
pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH\n");
480+
if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
481+
pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
482482

483483
if (!system_supports_address_auth()) {
484484
pr_err("FAIL: CPU lacks pointer authentication feature\n");

0 commit comments

Comments
 (0)