Skip to content

Commit 2016986

Browse files
pccctmarinas
authored andcommitted
arm64: Introduce prctl(PR_PAC_{SET,GET}_ENABLED_KEYS)
This change introduces a prctl that allows the user program to control which PAC keys are enabled in a particular task. The main reason why this is useful is to enable a userspace ABI that uses PAC to sign and authenticate function pointers and other pointers exposed outside of the function, while still allowing binaries conforming to the ABI to interoperate with legacy binaries that do not sign or authenticate pointers. The idea is that a dynamic loader or early startup code would issue this prctl very early after establishing that a process may load legacy binaries, but before executing any PAC instructions. This change adds a small amount of overhead to kernel entry and exit due to additional required instruction sequences. On a DragonBoard 845c (Cortex-A75) with the powersave governor, the overhead of similar instruction sequences was measured as 4.9ns when simulating the common case where IA is left enabled, or 43.7ns when simulating the uncommon case where IA is disabled. These numbers can be seen as the worst case scenario, since in more realistic scenarios a better performing governor would be used and a newer chip would be used that would support PAC unlike Cortex-A75 and would be expected to be faster than Cortex-A75. On an Apple M1 under a hypervisor, the overhead of the entry/exit instruction sequences introduced by this patch was measured as 0.3ns in the case where IA is left enabled, and 33.0ns in the case where IA is disabled. Signed-off-by: Peter Collingbourne <pcc@google.com> Reviewed-by: Dave Martin <Dave.Martin@arm.com> Link: https://linux-review.googlesource.com/id/Ibc41a5e6a76b275efbaa126b31119dc197b927a5 Link: https://lore.kernel.org/r/d6609065f8f40397a4124654eb68c9f490b4d477.1616123271.git.pcc@google.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 2f79d2f commit 2016986

14 files changed

Lines changed: 239 additions & 13 deletions

File tree

‎Documentation/arm64/pointer-authentication.rst‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,37 @@ filter out the Pointer Authentication system key registers from
107107
KVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID
108108
register. Any attempt to use the Pointer Authentication instructions will
109109
result in an UNDEFINED exception being injected into the guest.
110+
111+
112+
Enabling and disabling keys
113+
---------------------------
114+
115+
The prctl PR_PAC_SET_ENABLED_KEYS allows the user program to control which
116+
PAC keys are enabled in a particular task. It takes two arguments, the
117+
first being a bitmask of PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY
118+
and PR_PAC_APDBKEY specifying which keys shall be affected by this prctl,
119+
and the second being a bitmask of the same bits specifying whether the key
120+
should be enabled or disabled. For example::
121+
122+
prctl(PR_PAC_SET_ENABLED_KEYS,
123+
PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY,
124+
PR_PAC_APIBKEY, 0, 0);
125+
126+
disables all keys except the IB key.
127+
128+
The main reason why this is useful is to enable a userspace ABI that uses PAC
129+
instructions to sign and authenticate function pointers and other pointers
130+
exposed outside of the function, while still allowing binaries conforming to
131+
the ABI to interoperate with legacy binaries that do not sign or authenticate
132+
pointers.
133+
134+
The idea is that a dynamic loader or early startup code would issue this
135+
prctl very early after establishing that a process may load legacy binaries,
136+
but before executing any PAC instructions.
137+
138+
For compatibility with previous kernel versions, processes start up with IA,
139+
IB, DA and DB enabled, and are reset to this state on exec(). Processes created
140+
via fork() and clone() inherit the key enabled state from the calling process.
141+
142+
It is recommended to avoid disabling the IA key, as this has higher performance
143+
overhead than disabling any of the other keys.

‎arch/arm64/include/asm/mte.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void mte_free_tag_storage(char *storage);
3939

4040
void mte_sync_tags(pte_t *ptep, pte_t pte);
4141
void mte_copy_page_tags(void *kto, const void *kfrom);
42-
void flush_mte_state(void);
42+
void mte_thread_init_user(void);
4343
void mte_thread_switch(struct task_struct *next);
4444
void mte_suspend_enter(void);
4545
void mte_suspend_exit(void);
@@ -61,7 +61,7 @@ static inline void mte_sync_tags(pte_t *ptep, pte_t pte)
6161
static inline void mte_copy_page_tags(void *kto, const void *kfrom)
6262
{
6363
}
64-
static inline void flush_mte_state(void)
64+
static inline void mte_thread_init_user(void)
6565
{
6666
}
6767
static inline void mte_thread_switch(struct task_struct *next)

‎arch/arm64/include/asm/pointer_auth.h‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define __ASM_POINTER_AUTH_H
44

55
#include <linux/bitops.h>
6+
#include <linux/prctl.h>
67
#include <linux/random.h>
78

89
#include <asm/cpufeature.h>
@@ -71,6 +72,10 @@ static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kerne
7172

7273
extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
7374

75+
extern int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
76+
unsigned long enabled);
77+
extern int ptrauth_get_enabled_keys(struct task_struct *tsk);
78+
7479
static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr)
7580
{
7681
return ptrauth_clear_pac(ptr);
@@ -85,8 +90,17 @@ static __always_inline void ptrauth_enable(void)
8590
isb();
8691
}
8792

88-
#define ptrauth_thread_init_user(tsk) \
89-
ptrauth_keys_init_user(&(tsk)->thread.keys_user)
93+
#define ptrauth_thread_init_user() \
94+
do { \
95+
ptrauth_keys_init_user(&current->thread.keys_user); \
96+
\
97+
/* enable all keys */ \
98+
if (system_supports_address_auth()) \
99+
set_task_sctlr_el1(current->thread.sctlr_user | \
100+
SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | \
101+
SCTLR_ELx_ENDA | SCTLR_ELx_ENDB); \
102+
} while (0)
103+
90104
#define ptrauth_thread_init_kernel(tsk) \
91105
ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
92106
#define ptrauth_thread_switch_kernel(tsk) \
@@ -95,10 +109,15 @@ static __always_inline void ptrauth_enable(void)
95109
#else /* CONFIG_ARM64_PTR_AUTH */
96110
#define ptrauth_enable()
97111
#define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
112+
#define ptrauth_set_enabled_keys(tsk, keys, enabled) (-EINVAL)
113+
#define ptrauth_get_enabled_keys(tsk) (-EINVAL)
98114
#define ptrauth_strip_insn_pac(lr) (lr)
99-
#define ptrauth_thread_init_user(tsk)
115+
#define ptrauth_thread_init_user()
100116
#define ptrauth_thread_init_kernel(tsk)
101117
#define ptrauth_thread_switch_kernel(tsk)
102118
#endif /* CONFIG_ARM64_PTR_AUTH */
103119

120+
#define PR_PAC_ENABLED_KEYS_MASK \
121+
(PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY)
122+
104123
#endif /* __ASM_POINTER_AUTH_H */

‎arch/arm64/include/asm/processor.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ struct thread_struct {
156156
u64 sctlr_user;
157157
};
158158

159-
#define SCTLR_USER_MASK SCTLR_EL1_TCF0_MASK
159+
#define SCTLR_USER_MASK \
160+
(SCTLR_ELx_ENIA | SCTLR_ELx_ENIB | SCTLR_ELx_ENDA | SCTLR_ELx_ENDB | \
161+
SCTLR_EL1_TCF0_MASK)
160162

161163
static inline void arch_thread_struct_whitelist(unsigned long *offset,
162164
unsigned long *size)
@@ -305,6 +307,11 @@ extern void __init minsigstksz_setup(void);
305307
/* PR_PAC_RESET_KEYS prctl */
306308
#define PAC_RESET_KEYS(tsk, arg) ptrauth_prctl_reset_keys(tsk, arg)
307309

310+
/* PR_PAC_{SET,GET}_ENABLED_KEYS prctl */
311+
#define PAC_SET_ENABLED_KEYS(tsk, keys, enabled) \
312+
ptrauth_set_enabled_keys(tsk, keys, enabled)
313+
#define PAC_GET_ENABLED_KEYS(tsk) ptrauth_get_enabled_keys(tsk)
314+
308315
#ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
309316
/* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */
310317
long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg);

‎arch/arm64/include/asm/sysreg.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,10 @@
565565
#define SCTLR_ELx_TCF_ASYNC (UL(0x2) << SCTLR_ELx_TCF_SHIFT)
566566
#define SCTLR_ELx_TCF_MASK (UL(0x3) << SCTLR_ELx_TCF_SHIFT)
567567

568+
#define SCTLR_ELx_ENIA_SHIFT 31
569+
568570
#define SCTLR_ELx_ITFSB (BIT(37))
569-
#define SCTLR_ELx_ENIA (BIT(31))
571+
#define SCTLR_ELx_ENIA (BIT(SCTLR_ELx_ENIA_SHIFT))
570572
#define SCTLR_ELx_ENIB (BIT(30))
571573
#define SCTLR_ELx_ENDA (BIT(27))
572574
#define SCTLR_ELx_EE (BIT(25))

‎arch/arm64/kernel/asm-offsets.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int main(void)
4343
#endif
4444
BLANK();
4545
DEFINE(THREAD_CPU_CONTEXT, offsetof(struct task_struct, thread.cpu_context));
46+
DEFINE(THREAD_SCTLR_USER, offsetof(struct task_struct, thread.sctlr_user));
4647
#ifdef CONFIG_ARM64_PTR_AUTH
4748
DEFINE(THREAD_KEYS_USER, offsetof(struct task_struct, thread.keys_user));
4849
DEFINE(THREAD_KEYS_KERNEL, offsetof(struct task_struct, thread.keys_kernel));

‎arch/arm64/kernel/entry.S‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,24 @@ alternative_else_nop_endif
247247
check_mte_async_tcf x19, x22
248248
apply_ssbd 1, x22, x23
249249

250-
ptrauth_keys_install_kernel tsk, x20, x22, x23
250+
ptrauth_keys_install_kernel_nosync tsk, x20, x22, x23
251+
252+
#ifdef CONFIG_ARM64_PTR_AUTH
253+
alternative_if ARM64_HAS_ADDRESS_AUTH
254+
/*
255+
* Enable IA for in-kernel PAC if the task had it disabled. Although
256+
* this could be implemented with an unconditional MRS which would avoid
257+
* a load, this was measured to be slower on Cortex-A75 and Cortex-A76.
258+
*/
259+
ldr x0, [tsk, THREAD_SCTLR_USER]
260+
tbnz x0, SCTLR_ELx_ENIA_SHIFT, 1f
261+
mrs x0, sctlr_el1
262+
orr x0, x0, SCTLR_ELx_ENIA
263+
msr sctlr_el1, x0
264+
1:
265+
isb
266+
alternative_else_nop_endif
267+
#endif
251268

252269
mte_set_kernel_gcr x22, x23
253270

@@ -351,9 +368,27 @@ alternative_else_nop_endif
351368
3:
352369
scs_save tsk, x0
353370

354-
/* No kernel C function calls after this as user keys are set. */
371+
/*
372+
* No kernel C function calls after this as user keys are set and IA may
373+
* be disabled.
374+
*/
355375
ptrauth_keys_install_user tsk, x0, x1, x2
356376

377+
#ifdef CONFIG_ARM64_PTR_AUTH
378+
alternative_if ARM64_HAS_ADDRESS_AUTH
379+
/*
380+
* IA was enabled for in-kernel PAC. Disable it now if needed.
381+
* All other per-task SCTLR bits were updated on task switch.
382+
*/
383+
ldr x0, [tsk, THREAD_SCTLR_USER]
384+
tbnz x0, SCTLR_ELx_ENIA_SHIFT, 1f
385+
mrs x0, sctlr_el1
386+
bic x0, x0, SCTLR_ELx_ENIA
387+
msr sctlr_el1, x0
388+
1:
389+
alternative_else_nop_endif
390+
#endif
391+
357392
mte_set_user_gcr tsk, x0, x1
358393

359394
apply_ssbd 0, x0, x1

‎arch/arm64/kernel/mte.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static void set_gcr_el1_excl(u64 excl)
207207
*/
208208
}
209209

210-
void flush_mte_state(void)
210+
void mte_thread_init_user(void)
211211
{
212212
if (!system_supports_mte())
213213
return;

‎arch/arm64/kernel/pointer_auth.c‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,65 @@ int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
4646

4747
return 0;
4848
}
49+
50+
static u64 arg_to_enxx_mask(unsigned long arg)
51+
{
52+
u64 sctlr_enxx_mask = 0;
53+
54+
WARN_ON(arg & ~PR_PAC_ENABLED_KEYS_MASK);
55+
if (arg & PR_PAC_APIAKEY)
56+
sctlr_enxx_mask |= SCTLR_ELx_ENIA;
57+
if (arg & PR_PAC_APIBKEY)
58+
sctlr_enxx_mask |= SCTLR_ELx_ENIB;
59+
if (arg & PR_PAC_APDAKEY)
60+
sctlr_enxx_mask |= SCTLR_ELx_ENDA;
61+
if (arg & PR_PAC_APDBKEY)
62+
sctlr_enxx_mask |= SCTLR_ELx_ENDB;
63+
return sctlr_enxx_mask;
64+
}
65+
66+
int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
67+
unsigned long enabled)
68+
{
69+
u64 sctlr = tsk->thread.sctlr_user;
70+
71+
if (!system_supports_address_auth())
72+
return -EINVAL;
73+
74+
if (is_compat_thread(task_thread_info(tsk)))
75+
return -EINVAL;
76+
77+
if ((keys & ~PR_PAC_ENABLED_KEYS_MASK) || (enabled & ~keys))
78+
return -EINVAL;
79+
80+
sctlr &= ~arg_to_enxx_mask(keys);
81+
sctlr |= arg_to_enxx_mask(enabled);
82+
if (tsk == current)
83+
set_task_sctlr_el1(sctlr);
84+
else
85+
tsk->thread.sctlr_user = sctlr;
86+
87+
return 0;
88+
}
89+
90+
int ptrauth_get_enabled_keys(struct task_struct *tsk)
91+
{
92+
int retval = 0;
93+
94+
if (!system_supports_address_auth())
95+
return -EINVAL;
96+
97+
if (is_compat_thread(task_thread_info(tsk)))
98+
return -EINVAL;
99+
100+
if (tsk->thread.sctlr_user & SCTLR_ELx_ENIA)
101+
retval |= PR_PAC_APIAKEY;
102+
if (tsk->thread.sctlr_user & SCTLR_ELx_ENIB)
103+
retval |= PR_PAC_APIBKEY;
104+
if (tsk->thread.sctlr_user & SCTLR_ELx_ENDA)
105+
retval |= PR_PAC_APDAKEY;
106+
if (tsk->thread.sctlr_user & SCTLR_ELx_ENDB)
107+
retval |= PR_PAC_APDBKEY;
108+
109+
return retval;
110+
}

‎arch/arm64/kernel/process.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ void flush_thread(void)
339339
tls_thread_flush();
340340
flush_ptrace_hw_breakpoint(current);
341341
flush_tagged_addr_state();
342-
flush_mte_state();
343342
}
344343

345344
void release_thread(struct task_struct *dead_task)
@@ -531,7 +530,11 @@ static void erratum_1418040_thread_switch(struct task_struct *prev,
531530

532531
static void update_sctlr_el1(u64 sctlr)
533532
{
534-
sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK, sctlr);
533+
/*
534+
* EnIA must not be cleared while in the kernel as this is necessary for
535+
* in-kernel PAC. It will be cleared on kernel exit if needed.
536+
*/
537+
sysreg_clear_set(sctlr_el1, SCTLR_USER_MASK & ~SCTLR_ELx_ENIA, sctlr);
535538

536539
/* ISB required for the kernel uaccess routines when setting TCF0. */
537540
isb();
@@ -632,7 +635,8 @@ void arch_setup_new_exec(void)
632635
{
633636
current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
634637

635-
ptrauth_thread_init_user(current);
638+
ptrauth_thread_init_user();
639+
mte_thread_init_user();
636640

637641
if (task_spec_ssb_noexec(current)) {
638642
arch_prctl_spec_ctrl_set(current, PR_SPEC_STORE_BYPASS,

0 commit comments

Comments
 (0)