pointer-authentication.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. =======================================
  2. Pointer authentication in AArch64 Linux
  3. =======================================
  4. Author: Mark Rutland <mark.rutland@arm.com>
  5. Date: 2017-07-19
  6. This document briefly describes the provision of pointer authentication
  7. functionality in AArch64 Linux.
  8. Architecture overview
  9. ---------------------
  10. The ARMv8.3 Pointer Authentication extension adds primitives that can be
  11. used to mitigate certain classes of attack where an attacker can corrupt
  12. the contents of some memory (e.g. the stack).
  13. The extension uses a Pointer Authentication Code (PAC) to determine
  14. whether pointers have been modified unexpectedly. A PAC is derived from
  15. a pointer, another value (such as the stack pointer), and a secret key
  16. held in system registers.
  17. The extension adds instructions to insert a valid PAC into a pointer,
  18. and to verify/remove the PAC from a pointer. The PAC occupies a number
  19. of high-order bits of the pointer, which varies dependent on the
  20. configured virtual address size and whether pointer tagging is in use.
  21. A subset of these instructions have been allocated from the HINT
  22. encoding space. In the absence of the extension (or when disabled),
  23. these instructions behave as NOPs. Applications and libraries using
  24. these instructions operate correctly regardless of the presence of the
  25. extension.
  26. The extension provides five separate keys to generate PACs - two for
  27. instruction addresses (APIAKey, APIBKey), two for data addresses
  28. (APDAKey, APDBKey), and one for generic authentication (APGAKey).
  29. Basic support
  30. -------------
  31. When CONFIG_ARM64_PTR_AUTH is selected, and relevant HW support is
  32. present, the kernel will assign random key values to each process at
  33. exec*() time. The keys are shared by all threads within the process, and
  34. are preserved across fork().
  35. Presence of address authentication functionality is advertised via
  36. HWCAP_PACA, and generic authentication functionality via HWCAP_PACG.
  37. The number of bits that the PAC occupies in a pointer is 55 minus the
  38. virtual address size configured by the kernel. For example, with a
  39. virtual address size of 48, the PAC is 7 bits wide.
  40. Recent versions of GCC can compile code with APIAKey-based return
  41. address protection when passed the -msign-return-address option. This
  42. uses instructions in the HINT space (unless -march=armv8.3-a or higher
  43. is also passed), and such code can run on systems without the pointer
  44. authentication extension.
  45. In addition to exec(), keys can also be reinitialized to random values
  46. using the PR_PAC_RESET_KEYS prctl. A bitmask of PR_PAC_APIAKEY,
  47. PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY and PR_PAC_APGAKEY
  48. specifies which keys are to be reinitialized; specifying 0 means "all
  49. keys".
  50. Debugging
  51. ---------
  52. When CONFIG_ARM64_PTR_AUTH is selected, and HW support for address
  53. authentication is present, the kernel will expose the position of TTBR0
  54. PAC bits in the NT_ARM_PAC_MASK regset (struct user_pac_mask), which
  55. userspace can acquire via PTRACE_GETREGSET.
  56. The regset is exposed only when HWCAP_PACA is set. Separate masks are
  57. exposed for data pointers and instruction pointers, as the set of PAC
  58. bits can vary between the two. Note that the masks apply to TTBR0
  59. addresses, and are not valid to apply to TTBR1 addresses (e.g. kernel
  60. pointers).
  61. Additionally, when CONFIG_CHECKPOINT_RESTORE is also set, the kernel
  62. will expose the NT_ARM_PACA_KEYS and NT_ARM_PACG_KEYS regsets (struct
  63. user_pac_address_keys and struct user_pac_generic_keys). These can be
  64. used to get and set the keys for a thread.
  65. Virtualization
  66. --------------
  67. Pointer authentication is enabled in KVM guest when each virtual cpu is
  68. initialised by passing flags KVM_ARM_VCPU_PTRAUTH_[ADDRESS/GENERIC] and
  69. requesting these two separate cpu features to be enabled. The current KVM
  70. guest implementation works by enabling both features together, so both
  71. these userspace flags are checked before enabling pointer authentication.
  72. The separate userspace flag will allow to have no userspace ABI changes
  73. if support is added in the future to allow these two features to be
  74. enabled independently of one another.
  75. As Arm Architecture specifies that Pointer Authentication feature is
  76. implemented along with the VHE feature so KVM arm64 ptrauth code relies
  77. on VHE mode to be present.
  78. Additionally, when these vcpu feature flags are not set then KVM will
  79. filter out the Pointer Authentication system key registers from
  80. KVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID
  81. register. Any attempt to use the Pointer Authentication instructions will
  82. result in an UNDEFINED exception being injected into the guest.
  83. Enabling and disabling keys
  84. ---------------------------
  85. The prctl PR_PAC_SET_ENABLED_KEYS allows the user program to control which
  86. PAC keys are enabled in a particular task. It takes two arguments, the
  87. first being a bitmask of PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY
  88. and PR_PAC_APDBKEY specifying which keys shall be affected by this prctl,
  89. and the second being a bitmask of the same bits specifying whether the key
  90. should be enabled or disabled. For example::
  91. prctl(PR_PAC_SET_ENABLED_KEYS,
  92. PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY,
  93. PR_PAC_APIBKEY, 0, 0);
  94. disables all keys except the IB key.
  95. The main reason why this is useful is to enable a userspace ABI that uses PAC
  96. instructions to sign and authenticate function pointers and other pointers
  97. exposed outside of the function, while still allowing binaries conforming to
  98. the ABI to interoperate with legacy binaries that do not sign or authenticate
  99. pointers.
  100. The idea is that a dynamic loader or early startup code would issue this
  101. prctl very early after establishing that a process may load legacy binaries,
  102. but before executing any PAC instructions.
  103. For compatibility with previous kernel versions, processes start up with IA,
  104. IB, DA and DB enabled, and are reset to this state on exec(). Processes created
  105. via fork() and clone() inherit the key enabled state from the calling process.
  106. It is recommended to avoid disabling the IA key, as this has higher performance
  107. overhead than disabling any of the other keys.