1*e4624435SJonathan Corbet======================================= 2*e4624435SJonathan CorbetPointer authentication in AArch64 Linux 3*e4624435SJonathan Corbet======================================= 4*e4624435SJonathan Corbet 5*e4624435SJonathan CorbetAuthor: Mark Rutland <mark.rutland@arm.com> 6*e4624435SJonathan Corbet 7*e4624435SJonathan CorbetDate: 2017-07-19 8*e4624435SJonathan Corbet 9*e4624435SJonathan CorbetThis document briefly describes the provision of pointer authentication 10*e4624435SJonathan Corbetfunctionality in AArch64 Linux. 11*e4624435SJonathan Corbet 12*e4624435SJonathan Corbet 13*e4624435SJonathan CorbetArchitecture overview 14*e4624435SJonathan Corbet--------------------- 15*e4624435SJonathan Corbet 16*e4624435SJonathan CorbetThe ARMv8.3 Pointer Authentication extension adds primitives that can be 17*e4624435SJonathan Corbetused to mitigate certain classes of attack where an attacker can corrupt 18*e4624435SJonathan Corbetthe contents of some memory (e.g. the stack). 19*e4624435SJonathan Corbet 20*e4624435SJonathan CorbetThe extension uses a Pointer Authentication Code (PAC) to determine 21*e4624435SJonathan Corbetwhether pointers have been modified unexpectedly. A PAC is derived from 22*e4624435SJonathan Corbeta pointer, another value (such as the stack pointer), and a secret key 23*e4624435SJonathan Corbetheld in system registers. 24*e4624435SJonathan Corbet 25*e4624435SJonathan CorbetThe extension adds instructions to insert a valid PAC into a pointer, 26*e4624435SJonathan Corbetand to verify/remove the PAC from a pointer. The PAC occupies a number 27*e4624435SJonathan Corbetof high-order bits of the pointer, which varies dependent on the 28*e4624435SJonathan Corbetconfigured virtual address size and whether pointer tagging is in use. 29*e4624435SJonathan Corbet 30*e4624435SJonathan CorbetA subset of these instructions have been allocated from the HINT 31*e4624435SJonathan Corbetencoding space. In the absence of the extension (or when disabled), 32*e4624435SJonathan Corbetthese instructions behave as NOPs. Applications and libraries using 33*e4624435SJonathan Corbetthese instructions operate correctly regardless of the presence of the 34*e4624435SJonathan Corbetextension. 35*e4624435SJonathan Corbet 36*e4624435SJonathan CorbetThe extension provides five separate keys to generate PACs - two for 37*e4624435SJonathan Corbetinstruction addresses (APIAKey, APIBKey), two for data addresses 38*e4624435SJonathan Corbet(APDAKey, APDBKey), and one for generic authentication (APGAKey). 39*e4624435SJonathan Corbet 40*e4624435SJonathan Corbet 41*e4624435SJonathan CorbetBasic support 42*e4624435SJonathan Corbet------------- 43*e4624435SJonathan Corbet 44*e4624435SJonathan CorbetWhen CONFIG_ARM64_PTR_AUTH is selected, and relevant HW support is 45*e4624435SJonathan Corbetpresent, the kernel will assign random key values to each process at 46*e4624435SJonathan Corbetexec*() time. The keys are shared by all threads within the process, and 47*e4624435SJonathan Corbetare preserved across fork(). 48*e4624435SJonathan Corbet 49*e4624435SJonathan CorbetPresence of address authentication functionality is advertised via 50*e4624435SJonathan CorbetHWCAP_PACA, and generic authentication functionality via HWCAP_PACG. 51*e4624435SJonathan Corbet 52*e4624435SJonathan CorbetThe number of bits that the PAC occupies in a pointer is 55 minus the 53*e4624435SJonathan Corbetvirtual address size configured by the kernel. For example, with a 54*e4624435SJonathan Corbetvirtual address size of 48, the PAC is 7 bits wide. 55*e4624435SJonathan Corbet 56*e4624435SJonathan CorbetWhen ARM64_PTR_AUTH_KERNEL is selected, the kernel will be compiled 57*e4624435SJonathan Corbetwith HINT space pointer authentication instructions protecting 58*e4624435SJonathan Corbetfunction returns. Kernels built with this option will work on hardware 59*e4624435SJonathan Corbetwith or without pointer authentication support. 60*e4624435SJonathan Corbet 61*e4624435SJonathan CorbetIn addition to exec(), keys can also be reinitialized to random values 62*e4624435SJonathan Corbetusing the PR_PAC_RESET_KEYS prctl. A bitmask of PR_PAC_APIAKEY, 63*e4624435SJonathan CorbetPR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY and PR_PAC_APGAKEY 64*e4624435SJonathan Corbetspecifies which keys are to be reinitialized; specifying 0 means "all 65*e4624435SJonathan Corbetkeys". 66*e4624435SJonathan Corbet 67*e4624435SJonathan Corbet 68*e4624435SJonathan CorbetDebugging 69*e4624435SJonathan Corbet--------- 70*e4624435SJonathan Corbet 71*e4624435SJonathan CorbetWhen CONFIG_ARM64_PTR_AUTH is selected, and HW support for address 72*e4624435SJonathan Corbetauthentication is present, the kernel will expose the position of TTBR0 73*e4624435SJonathan CorbetPAC bits in the NT_ARM_PAC_MASK regset (struct user_pac_mask), which 74*e4624435SJonathan Corbetuserspace can acquire via PTRACE_GETREGSET. 75*e4624435SJonathan Corbet 76*e4624435SJonathan CorbetThe regset is exposed only when HWCAP_PACA is set. Separate masks are 77*e4624435SJonathan Corbetexposed for data pointers and instruction pointers, as the set of PAC 78*e4624435SJonathan Corbetbits can vary between the two. Note that the masks apply to TTBR0 79*e4624435SJonathan Corbetaddresses, and are not valid to apply to TTBR1 addresses (e.g. kernel 80*e4624435SJonathan Corbetpointers). 81*e4624435SJonathan Corbet 82*e4624435SJonathan CorbetAdditionally, when CONFIG_CHECKPOINT_RESTORE is also set, the kernel 83*e4624435SJonathan Corbetwill expose the NT_ARM_PACA_KEYS and NT_ARM_PACG_KEYS regsets (struct 84*e4624435SJonathan Corbetuser_pac_address_keys and struct user_pac_generic_keys). These can be 85*e4624435SJonathan Corbetused to get and set the keys for a thread. 86*e4624435SJonathan Corbet 87*e4624435SJonathan Corbet 88*e4624435SJonathan CorbetVirtualization 89*e4624435SJonathan Corbet-------------- 90*e4624435SJonathan Corbet 91*e4624435SJonathan CorbetPointer authentication is enabled in KVM guest when each virtual cpu is 92*e4624435SJonathan Corbetinitialised by passing flags KVM_ARM_VCPU_PTRAUTH_[ADDRESS/GENERIC] and 93*e4624435SJonathan Corbetrequesting these two separate cpu features to be enabled. The current KVM 94*e4624435SJonathan Corbetguest implementation works by enabling both features together, so both 95*e4624435SJonathan Corbetthese userspace flags are checked before enabling pointer authentication. 96*e4624435SJonathan CorbetThe separate userspace flag will allow to have no userspace ABI changes 97*e4624435SJonathan Corbetif support is added in the future to allow these two features to be 98*e4624435SJonathan Corbetenabled independently of one another. 99*e4624435SJonathan Corbet 100*e4624435SJonathan CorbetAs Arm Architecture specifies that Pointer Authentication feature is 101*e4624435SJonathan Corbetimplemented along with the VHE feature so KVM arm64 ptrauth code relies 102*e4624435SJonathan Corbeton VHE mode to be present. 103*e4624435SJonathan Corbet 104*e4624435SJonathan CorbetAdditionally, when these vcpu feature flags are not set then KVM will 105*e4624435SJonathan Corbetfilter out the Pointer Authentication system key registers from 106*e4624435SJonathan CorbetKVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID 107*e4624435SJonathan Corbetregister. Any attempt to use the Pointer Authentication instructions will 108*e4624435SJonathan Corbetresult in an UNDEFINED exception being injected into the guest. 109*e4624435SJonathan Corbet 110*e4624435SJonathan Corbet 111*e4624435SJonathan CorbetEnabling and disabling keys 112*e4624435SJonathan Corbet--------------------------- 113*e4624435SJonathan Corbet 114*e4624435SJonathan CorbetThe prctl PR_PAC_SET_ENABLED_KEYS allows the user program to control which 115*e4624435SJonathan CorbetPAC keys are enabled in a particular task. It takes two arguments, the 116*e4624435SJonathan Corbetfirst being a bitmask of PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY 117*e4624435SJonathan Corbetand PR_PAC_APDBKEY specifying which keys shall be affected by this prctl, 118*e4624435SJonathan Corbetand the second being a bitmask of the same bits specifying whether the key 119*e4624435SJonathan Corbetshould be enabled or disabled. For example:: 120*e4624435SJonathan Corbet 121*e4624435SJonathan Corbet prctl(PR_PAC_SET_ENABLED_KEYS, 122*e4624435SJonathan Corbet PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY, 123*e4624435SJonathan Corbet PR_PAC_APIBKEY, 0, 0); 124*e4624435SJonathan Corbet 125*e4624435SJonathan Corbetdisables all keys except the IB key. 126*e4624435SJonathan Corbet 127*e4624435SJonathan CorbetThe main reason why this is useful is to enable a userspace ABI that uses PAC 128*e4624435SJonathan Corbetinstructions to sign and authenticate function pointers and other pointers 129*e4624435SJonathan Corbetexposed outside of the function, while still allowing binaries conforming to 130*e4624435SJonathan Corbetthe ABI to interoperate with legacy binaries that do not sign or authenticate 131*e4624435SJonathan Corbetpointers. 132*e4624435SJonathan Corbet 133*e4624435SJonathan CorbetThe idea is that a dynamic loader or early startup code would issue this 134*e4624435SJonathan Corbetprctl very early after establishing that a process may load legacy binaries, 135*e4624435SJonathan Corbetbut before executing any PAC instructions. 136*e4624435SJonathan Corbet 137*e4624435SJonathan CorbetFor compatibility with previous kernel versions, processes start up with IA, 138*e4624435SJonathan CorbetIB, DA and DB enabled, and are reset to this state on exec(). Processes created 139*e4624435SJonathan Corbetvia fork() and clone() inherit the key enabled state from the calling process. 140*e4624435SJonathan Corbet 141*e4624435SJonathan CorbetIt is recommended to avoid disabling the IA key, as this has higher performance 142*e4624435SJonathan Corbetoverhead than disabling any of the other keys. 143