Skip to content

Commit 6982934

Browse files
kristina-martsenkoctmarinas
authored andcommitted
arm64: enable ptrauth earlier
When the kernel is compiled with pointer auth instructions, the boot CPU needs to start using address auth very early, so change the cpucap to account for this. Pointer auth must be enabled before we call C functions, because it is not possible to enter a function with pointer auth disabled and exit it with pointer auth enabled. Note, mismatches between architected and IMPDEF algorithms will still be caught by the cpufeature framework (the separate *_ARCH and *_IMP_DEF cpucaps). Note the change in behavior: if the boot CPU has address auth and a late CPU does not, then the late CPU is parked by the cpufeature framework. This is possible as kernel will only have NOP space intructions for PAC so such mismatched late cpu will silently ignore those instructions in C functions. Also, if the boot CPU does not have address auth and the late CPU has then the late cpu will still boot but with ptrauth feature disabled. Leave generic authentication as a "system scope" cpucap for now, since initially the kernel will only use address authentication. Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Suzuki K Poulose <[email protected]> Reviewed-by: Vincenzo Frascino <[email protected]> Signed-off-by: Kristina Martsenko <[email protected]> [Amit: Re-worked ptrauth setup logic, comments] Signed-off-by: Amit Daniel Kachhap <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent deeaac5 commit 6982934

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

arch/arm64/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,12 @@ config ARM64_PTR_AUTH
15151515
be enabled. However, KVM guest also require VHE mode and hence
15161516
CONFIG_ARM64_VHE=y option to use this feature.
15171517

1518+
If the feature is present on the boot CPU but not on a late CPU, then
1519+
the late CPU will be parked. Also, if the boot CPU does not have
1520+
address auth and the late CPU has then the late CPU will still boot
1521+
but with the feature disabled. On such a system, this option should
1522+
not be selected.
1523+
15181524
endmenu
15191525

15201526
menu "ARMv8.5 architectural features"

arch/arm64/include/asm/cpufeature.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
291291
#define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE \
292292
(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PANIC_ON_CONFLICT)
293293

294+
/*
295+
* CPU feature used early in the boot based on the boot CPU. It is safe for a
296+
* late CPU to have this feature even though the boot CPU hasn't enabled it,
297+
* although the feature will not be used by Linux in this case. If the boot CPU
298+
* has enabled this feature already, then every late CPU must have it.
299+
*/
300+
#define ARM64_CPUCAP_BOOT_CPU_FEATURE \
301+
(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
302+
294303
struct arm64_cpu_capabilities {
295304
const char *desc;
296305
u16 capability;

arch/arm64/kernel/cpufeature.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,12 +1318,6 @@ static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
13181318
#endif /* CONFIG_ARM64_RAS_EXTN */
13191319

13201320
#ifdef CONFIG_ARM64_PTR_AUTH
1321-
static void cpu_enable_address_auth(struct arm64_cpu_capabilities const *cap)
1322-
{
1323-
sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_ENIA | SCTLR_ELx_ENIB |
1324-
SCTLR_ELx_ENDA | SCTLR_ELx_ENDB);
1325-
}
1326-
13271321
static bool has_address_auth(const struct arm64_cpu_capabilities *entry,
13281322
int __unused)
13291323
{
@@ -1627,7 +1621,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
16271621
{
16281622
.desc = "Address authentication (architected algorithm)",
16291623
.capability = ARM64_HAS_ADDRESS_AUTH_ARCH,
1630-
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
1624+
.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
16311625
.sys_reg = SYS_ID_AA64ISAR1_EL1,
16321626
.sign = FTR_UNSIGNED,
16331627
.field_pos = ID_AA64ISAR1_APA_SHIFT,
@@ -1637,7 +1631,7 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
16371631
{
16381632
.desc = "Address authentication (IMP DEF algorithm)",
16391633
.capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
1640-
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
1634+
.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
16411635
.sys_reg = SYS_ID_AA64ISAR1_EL1,
16421636
.sign = FTR_UNSIGNED,
16431637
.field_pos = ID_AA64ISAR1_API_SHIFT,
@@ -1646,9 +1640,8 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
16461640
},
16471641
{
16481642
.capability = ARM64_HAS_ADDRESS_AUTH,
1649-
.type = ARM64_CPUCAP_SYSTEM_FEATURE,
1643+
.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
16501644
.matches = has_address_auth,
1651-
.cpu_enable = cpu_enable_address_auth,
16521645
},
16531646
{
16541647
.desc = "Generic authentication (architected algorithm)",

arch/arm64/mm/proc.S

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <asm/pgtable-hwdef.h>
1717
#include <asm/cpufeature.h>
1818
#include <asm/alternative.h>
19+
#include <asm/smp.h>
1920

2021
#ifdef CONFIG_ARM64_64K_PAGES
2122
#define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K
@@ -468,9 +469,39 @@ SYM_FUNC_START(__cpu_setup)
468469
1:
469470
#endif /* CONFIG_ARM64_HW_AFDBM */
470471
msr tcr_el1, x10
472+
mov x1, x0
471473
/*
472474
* Prepare SCTLR
473475
*/
474476
mov_q x0, SCTLR_EL1_SET
477+
478+
#ifdef CONFIG_ARM64_PTR_AUTH
479+
/* No ptrauth setup for run time cpus */
480+
cmp x1, #ARM64_CPU_RUNTIME
481+
b.eq 3f
482+
483+
/* Check if the CPU supports ptrauth */
484+
mrs x2, id_aa64isar1_el1
485+
ubfx x2, x2, #ID_AA64ISAR1_APA_SHIFT, #8
486+
cbz x2, 3f
487+
488+
msr_s SYS_APIAKEYLO_EL1, xzr
489+
msr_s SYS_APIAKEYHI_EL1, xzr
490+
491+
/* Just enable ptrauth for primary cpu */
492+
cmp x1, #ARM64_CPU_BOOT_PRIMARY
493+
b.eq 2f
494+
495+
/* if !system_supports_address_auth() then skip enable */
496+
alternative_if_not ARM64_HAS_ADDRESS_AUTH
497+
b 3f
498+
alternative_else_nop_endif
499+
500+
2: /* Enable ptrauth instructions */
501+
ldr x2, =SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
502+
SCTLR_ELx_ENDA | SCTLR_ELx_ENDB
503+
orr x0, x0, x2
504+
3:
505+
#endif
475506
ret // return to head.S
476507
SYM_FUNC_END(__cpu_setup)

0 commit comments

Comments
 (0)