143 lines · plain
1=======================================2Pointer authentication in AArch64 Linux3=======================================4 5Author: Mark Rutland <mark.rutland@arm.com>6 7Date: 2017-07-198 9This document briefly describes the provision of pointer authentication10functionality in AArch64 Linux.11 12 13Architecture overview14---------------------15 16The ARMv8.3 Pointer Authentication extension adds primitives that can be17used to mitigate certain classes of attack where an attacker can corrupt18the contents of some memory (e.g. the stack).19 20The extension uses a Pointer Authentication Code (PAC) to determine21whether pointers have been modified unexpectedly. A PAC is derived from22a pointer, another value (such as the stack pointer), and a secret key23held in system registers.24 25The extension adds instructions to insert a valid PAC into a pointer,26and to verify/remove the PAC from a pointer. The PAC occupies a number27of high-order bits of the pointer, which varies dependent on the28configured virtual address size and whether pointer tagging is in use.29 30A subset of these instructions have been allocated from the HINT31encoding space. In the absence of the extension (or when disabled),32these instructions behave as NOPs. Applications and libraries using33these instructions operate correctly regardless of the presence of the34extension.35 36The extension provides five separate keys to generate PACs - two for37instruction addresses (APIAKey, APIBKey), two for data addresses38(APDAKey, APDBKey), and one for generic authentication (APGAKey).39 40 41Basic support42-------------43 44When CONFIG_ARM64_PTR_AUTH is selected, and relevant HW support is45present, the kernel will assign random key values to each process at46exec*() time. The keys are shared by all threads within the process, and47are preserved across fork().48 49Presence of address authentication functionality is advertised via50HWCAP_PACA, and generic authentication functionality via HWCAP_PACG.51 52The number of bits that the PAC occupies in a pointer is 55 minus the53virtual address size configured by the kernel. For example, with a54virtual address size of 48, the PAC is 7 bits wide.55 56When ARM64_PTR_AUTH_KERNEL is selected, the kernel will be compiled57with HINT space pointer authentication instructions protecting58function returns. Kernels built with this option will work on hardware59with or without pointer authentication support.60 61In addition to exec(), keys can also be reinitialized to random values62using the PR_PAC_RESET_KEYS prctl. A bitmask of PR_PAC_APIAKEY,63PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY and PR_PAC_APGAKEY64specifies which keys are to be reinitialized; specifying 0 means "all65keys".66 67 68Debugging69---------70 71When CONFIG_ARM64_PTR_AUTH is selected, and HW support for address72authentication is present, the kernel will expose the position of TTBR073PAC bits in the NT_ARM_PAC_MASK regset (struct user_pac_mask), which74userspace can acquire via PTRACE_GETREGSET.75 76The regset is exposed only when HWCAP_PACA is set. Separate masks are77exposed for data pointers and instruction pointers, as the set of PAC78bits can vary between the two. Note that the masks apply to TTBR079addresses, and are not valid to apply to TTBR1 addresses (e.g. kernel80pointers).81 82Additionally, when CONFIG_CHECKPOINT_RESTORE is also set, the kernel83will expose the NT_ARM_PACA_KEYS and NT_ARM_PACG_KEYS regsets (struct84user_pac_address_keys and struct user_pac_generic_keys). These can be85used to get and set the keys for a thread.86 87 88Virtualization89--------------90 91Pointer authentication is enabled in KVM guest when each virtual cpu is92initialised by passing flags KVM_ARM_VCPU_PTRAUTH_[ADDRESS/GENERIC] and93requesting these two separate cpu features to be enabled. The current KVM94guest implementation works by enabling both features together, so both95these userspace flags are checked before enabling pointer authentication.96The separate userspace flag will allow to have no userspace ABI changes97if support is added in the future to allow these two features to be98enabled independently of one another.99 100As Arm Architecture specifies that Pointer Authentication feature is101implemented along with the VHE feature so KVM arm64 ptrauth code relies102on VHE mode to be present.103 104Additionally, when these vcpu feature flags are not set then KVM will105filter out the Pointer Authentication system key registers from106KVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID107register. Any attempt to use the Pointer Authentication instructions will108result in an UNDEFINED exception being injected into the guest.109 110 111Enabling and disabling keys112---------------------------113 114The prctl PR_PAC_SET_ENABLED_KEYS allows the user program to control which115PAC keys are enabled in a particular task. It takes two arguments, the116first being a bitmask of PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY117and PR_PAC_APDBKEY specifying which keys shall be affected by this prctl,118and the second being a bitmask of the same bits specifying whether the key119should be enabled or disabled. For example::120 121 prctl(PR_PAC_SET_ENABLED_KEYS,122 PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY,123 PR_PAC_APIBKEY, 0, 0);124 125disables all keys except the IB key.126 127The main reason why this is useful is to enable a userspace ABI that uses PAC128instructions to sign and authenticate function pointers and other pointers129exposed outside of the function, while still allowing binaries conforming to130the ABI to interoperate with legacy binaries that do not sign or authenticate131pointers.132 133The idea is that a dynamic loader or early startup code would issue this134prctl very early after establishing that a process may load legacy binaries,135but before executing any PAC instructions.136 137For compatibility with previous kernel versions, processes start up with IA,138IB, DA and DB enabled, and are reset to this state on exec(). Processes created139via fork() and clone() inherit the key enabled state from the calling process.140 141It is recommended to avoid disabling the IA key, as this has higher performance142overhead than disabling any of the other keys.143