0001 =======================================
0002 Pointer authentication in AArch64 Linux
0003 =======================================
0004
0005 Author: Mark Rutland <mark.rutland@arm.com>
0006
0007 Date: 2017-07-19
0008
0009 This document briefly describes the provision of pointer authentication
0010 functionality in AArch64 Linux.
0011
0012
0013 Architecture overview
0014 ---------------------
0015
0016 The ARMv8.3 Pointer Authentication extension adds primitives that can be
0017 used to mitigate certain classes of attack where an attacker can corrupt
0018 the contents of some memory (e.g. the stack).
0019
0020 The extension uses a Pointer Authentication Code (PAC) to determine
0021 whether pointers have been modified unexpectedly. A PAC is derived from
0022 a pointer, another value (such as the stack pointer), and a secret key
0023 held in system registers.
0024
0025 The extension adds instructions to insert a valid PAC into a pointer,
0026 and to verify/remove the PAC from a pointer. The PAC occupies a number
0027 of high-order bits of the pointer, which varies dependent on the
0028 configured virtual address size and whether pointer tagging is in use.
0029
0030 A subset of these instructions have been allocated from the HINT
0031 encoding space. In the absence of the extension (or when disabled),
0032 these instructions behave as NOPs. Applications and libraries using
0033 these instructions operate correctly regardless of the presence of the
0034 extension.
0035
0036 The extension provides five separate keys to generate PACs - two for
0037 instruction addresses (APIAKey, APIBKey), two for data addresses
0038 (APDAKey, APDBKey), and one for generic authentication (APGAKey).
0039
0040
0041 Basic support
0042 -------------
0043
0044 When CONFIG_ARM64_PTR_AUTH is selected, and relevant HW support is
0045 present, the kernel will assign random key values to each process at
0046 exec*() time. The keys are shared by all threads within the process, and
0047 are preserved across fork().
0048
0049 Presence of address authentication functionality is advertised via
0050 HWCAP_PACA, and generic authentication functionality via HWCAP_PACG.
0051
0052 The number of bits that the PAC occupies in a pointer is 55 minus the
0053 virtual address size configured by the kernel. For example, with a
0054 virtual address size of 48, the PAC is 7 bits wide.
0055
0056 When ARM64_PTR_AUTH_KERNEL is selected, the kernel will be compiled
0057 with HINT space pointer authentication instructions protecting
0058 function returns. Kernels built with this option will work on hardware
0059 with or without pointer authentication support.
0060
0061 In addition to exec(), keys can also be reinitialized to random values
0062 using the PR_PAC_RESET_KEYS prctl. A bitmask of PR_PAC_APIAKEY,
0063 PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY and PR_PAC_APGAKEY
0064 specifies which keys are to be reinitialized; specifying 0 means "all
0065 keys".
0066
0067
0068 Debugging
0069 ---------
0070
0071 When CONFIG_ARM64_PTR_AUTH is selected, and HW support for address
0072 authentication is present, the kernel will expose the position of TTBR0
0073 PAC bits in the NT_ARM_PAC_MASK regset (struct user_pac_mask), which
0074 userspace can acquire via PTRACE_GETREGSET.
0075
0076 The regset is exposed only when HWCAP_PACA is set. Separate masks are
0077 exposed for data pointers and instruction pointers, as the set of PAC
0078 bits can vary between the two. Note that the masks apply to TTBR0
0079 addresses, and are not valid to apply to TTBR1 addresses (e.g. kernel
0080 pointers).
0081
0082 Additionally, when CONFIG_CHECKPOINT_RESTORE is also set, the kernel
0083 will expose the NT_ARM_PACA_KEYS and NT_ARM_PACG_KEYS regsets (struct
0084 user_pac_address_keys and struct user_pac_generic_keys). These can be
0085 used to get and set the keys for a thread.
0086
0087
0088 Virtualization
0089 --------------
0090
0091 Pointer authentication is enabled in KVM guest when each virtual cpu is
0092 initialised by passing flags KVM_ARM_VCPU_PTRAUTH_[ADDRESS/GENERIC] and
0093 requesting these two separate cpu features to be enabled. The current KVM
0094 guest implementation works by enabling both features together, so both
0095 these userspace flags are checked before enabling pointer authentication.
0096 The separate userspace flag will allow to have no userspace ABI changes
0097 if support is added in the future to allow these two features to be
0098 enabled independently of one another.
0099
0100 As Arm Architecture specifies that Pointer Authentication feature is
0101 implemented along with the VHE feature so KVM arm64 ptrauth code relies
0102 on VHE mode to be present.
0103
0104 Additionally, when these vcpu feature flags are not set then KVM will
0105 filter out the Pointer Authentication system key registers from
0106 KVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID
0107 register. Any attempt to use the Pointer Authentication instructions will
0108 result in an UNDEFINED exception being injected into the guest.
0109
0110
0111 Enabling and disabling keys
0112 ---------------------------
0113
0114 The prctl PR_PAC_SET_ENABLED_KEYS allows the user program to control which
0115 PAC keys are enabled in a particular task. It takes two arguments, the
0116 first being a bitmask of PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY
0117 and PR_PAC_APDBKEY specifying which keys shall be affected by this prctl,
0118 and the second being a bitmask of the same bits specifying whether the key
0119 should be enabled or disabled. For example::
0120
0121 prctl(PR_PAC_SET_ENABLED_KEYS,
0122 PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY,
0123 PR_PAC_APIBKEY, 0, 0);
0124
0125 disables all keys except the IB key.
0126
0127 The main reason why this is useful is to enable a userspace ABI that uses PAC
0128 instructions to sign and authenticate function pointers and other pointers
0129 exposed outside of the function, while still allowing binaries conforming to
0130 the ABI to interoperate with legacy binaries that do not sign or authenticate
0131 pointers.
0132
0133 The idea is that a dynamic loader or early startup code would issue this
0134 prctl very early after establishing that a process may load legacy binaries,
0135 but before executing any PAC instructions.
0136
0137 For compatibility with previous kernel versions, processes start up with IA,
0138 IB, DA and DB enabled, and are reset to this state on exec(). Processes created
0139 via fork() and clone() inherit the key enabled state from the calling process.
0140
0141 It is recommended to avoid disabling the IA key, as this has higher performance
0142 overhead than disabling any of the other keys.