Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

13.1 Pointer Authentication and BTI

Pointer Authentication (PAuth) and Branch Target Identification (BTI) are the two halves of Arm’s control-flow integrity story. PAuth protects backward edges, such as return addresses, and any other pointer that software chooses to sign. BTI constrains forward edges, meaning indirect branch targets. Both arrive in the HINT space wherever possible, so a binary built with -mbranch-protection=standard still runs on a v8.0 core: there, PACIASP, AUTIASP and BTI c execute as NOPs.

Keys, enables and traps

FEAT_PAuth is mandatory from v8.3, and therefore present in every Armv9-A PE. DDI0487 A2 gives the earliest version that permits it. It defines five 128-bit keys, each split into a Hi/Lo pair of system registers:

KeyRegistersUsed by
Instruction AAPIAKeyHi_EL1, APIAKeyLo_EL1PACIA*, AUTIA*, RETAA, BRAA, BLRAA, ERETAA
Instruction BAPIBKey{Hi,Lo}_EL1PACIB*, AUTIB*, … *AB forms
Data AAPDAKey{Hi,Lo}_EL1PACDA, AUTDA, LDRAA
Data BAPDBKey{Hi,Lo}_EL1PACDB, AUTDB, LDRAB
GenericAPGAKey{Hi,Lo}_EL1PACGA (a 32-bit MAC into the top half of Xd)

The keys are not banked by Exception level. There are no _EL2 or _EL3 key registers. EL2 and EL3 sign with whatever is in the _EL1 registers. This one fact drives all of the firmware work below.

Each regime enables each key separately: SCTLR_ELx.EnIA [31], EnIB [30], EnDA [27] and EnDB [13]. In SCTLR_EL1 they apply to both EL1 and EL0. When a key is disabled, the corresponding PAC and AUT instructions leave the pointer unchanged. That is how the HINT-space forms stay harmless. PACGA has no enable bit.

A higher EL can also trap the instructions and the key registers:

ControlEffect when 0EC
HCR_EL2.API [41]PAuth instructions at EL0/EL1 trap to EL20x09
HCR_EL2.APK [40]EL1 accesses to the key registers trap to EL20x18
SCR_EL3.API [17]PAuth instructions at EL0–EL2 trap to EL30x09
SCR_EL3.APK [16]Key register accesses at EL1/EL2 trap to EL30x18

The reset values are UNKNOWN, so EL3 has to write them. A forgotten SCR_EL3.API shows up as Linux dying on its first PACIASP with an EC 0x09 trap landing in BL31 (17.3). Writes to a key register, and to SCTLR_ELx.En*, are direct writes to system registers that later PAC/AUT instructions read indirectly. A context synchronization event is therefore needed between the write and the first use (7.8).

PAC and AUT

PACIA Xd, Xn computes a MAC, the PAC, over three inputs: the pointer in Xd, a 64-bit modifier in Xn, and key A. It then inserts the PAC into the pointer’s unused upper bits. Which bits are unused depends on TnSZ, TBI and bit 55. 13.3 works out the width: with a 48-bit VA it is 7 bits when TBI is on and 15 bits when TBI is off. AUTIA Xd, Xn recomputes the PAC, and if it matches it restores the canonical pointer. XPACI/XPACD strip a PAC without checking it.

The modifier is what stops a signed pointer being replayed in a different context:

FormPointerModifierTypical use
PACIASP / AUTIASPX30SPReturn address in a non-leaf function
PACIAZ / AUTIAZX30zeroLeaf functions or no-stack contexts (weaker)
PACIA1716 / AUTIA1716X17X16Signing in trampolines and PLTs
PACIA Xd, XnXdXnArbitrary: C++ vtables, function pointers with a type discriminator
RETAA, ERETAA / BRAA Xn, Xm, BLRAA Xn, XmX30, ELR_ELx / XnSP / XmCombined authenticate-and-branch
LDRAA Xt, [Xn, #imm]!Xn (DA key)zeroAuthenticated load

The problem with the SP modifier is well known. Two call sites at the same stack depth sign their return addresses with identical modifiers, so the signed LR from one can be substituted into the other. FEAT_PAuth_LR, below, addresses that.

The standard prologue and epilogue look like this:

func:
    paciasp                 // sign LR with SP; also an implicit BTI c landing pad
    stp     x29, x30, [sp, #-16]!
    mov     x29, sp
    bl      callee
    ldp     x29, x30, [sp], #16
    autiasp                 // or: retaa
    ret

What an authentication failure does

The failure behaviour changed across versions. The ID_AA64ISAR1_EL1.APA/API (and ID_AA64ISAR2_EL1.APA3) field value tells you which behaviour you have:

Field valueFeatureOn AUT failure
0b0001FEAT_PAuthAn error code is written into the PAC field’s upper bits, making the pointer non-canonical. The fault comes later, on use, as a translation fault
0b0010FEAT_EPACAUT failure as above. The change is in PAC*: signing a pointer whose extension bits are not canonical writes a PAC of zero instead of a PAC with one bit flipped
0b0011FEAT_PAuth2PAC XORs the MAC into the field instead of replacing it, and AUT XORs it back, so a failure leaves a deterministically corrupted pointer. Mandatory from v8.6
0b0100FEAT_FPACAUT* failure generates a synchronous exception, EC 0x1C, at the instruction itself
0b0101FEAT_FPACCOMBINEThe combined forms (RETAA, BRAA, LDRAA, …) also fault with EC 0x1C
0b0110FEAT_PAuth_LRAs FPACCOMBINE, plus the PC-modifier instructions

The algorithm is reported separately. APA means QARMA5, APA3 in ID_AA64ISAR2_EL1 means QARMA3 (FEAT_PACQARMA3), and API means an IMPLEMENTATION DEFINED algorithm. GPA/GPA3/GPI report the same for PACGA. QARMA3 is cheaper in latency. The choice of algorithm does not change software, but it does change the PAC values, so a test vector that passes on the FVP’s QARMA5 configuration will not match a core using an IMPLEMENTATION DEFINED algorithm.

Without FPAC, the failure point is the next dereference or RET. That surfaces as an instruction abort with a translation fault on a weird-looking address whose top bits are wrong. On a crash dump, a FAR with garbage in bits [54:48] after a RET is the signature (18.1). With FPAC, ESR_ELx.EC = 0x1C and the ISS says which key type failed. The exception is taken to the EL that would handle a synchronous exception from the current EL. An FPAC at EL1 therefore goes to EL1, not to EL3.

Security note: Without FPAC an attacker gets an oracle. A failed AUT that does not immediately fault produces a pointer the attacker may be able to observe or reuse. Combined with a speculative branch on the result, the PACMAN attack (2022) brute-forced PACs speculatively on Apple M1. PAuth is a deterministic MAC; it is not a speculation barrier. FPAC narrows the architectural oracle but not the speculative one (Part 14).

Key management at exception entry

Because keys are shared across ELs, every software layer that uses PAuth for itself must install its own keys on entry and restore the lower layer’s keys on exit:

  • Linux (EL1) gives each user task its own APIA/APIB/APDA/APDB/APGA keys and uses a separate kernel APIA key. It swaps in the kernel key on entry from EL0 (ptrauth_keys_install_kernel) and restores the task’s keys on return.
  • KVM saves the host keys and loads the guest’s on vcpu entry. Alternatively it traps the guest’s first PAuth use through HCR_EL2.API/APK = 0 and switches keys lazily.
  • TF-A BL31, when built with BRANCH_PROTECTION ≠ 0, saves all five keys of the interrupted world in the save_gp_pmcr_pauth_regs macro (lib/el3_runtime/aarch64/context.S) and loads its own APIA key from cpu_data. On exit, restore_gp_pmcr_pauth_regs restores the saved keys. The saved keys also belong to the per-world context, so the Secure world never sees Normal-world keys and vice versa.

The required ordering is: key write, then a context synchronization event, then the first PACIASP. In practice the entry path is assembly that does not itself sign anything until the keys are installed. Note also that ERET is a context synchronization event, so restored keys are in effect for the lower EL’s first instruction.

Bring-up note: Mixed-IP systems are the trap. If a little core implements an IMPLEMENTATION DEFINED algorithm and a big core implements QARMA5, a pointer signed on one core will not authenticate after migration to the other. Linux treats PAuth as a system-wide feature only if the algorithm matches on all CPUs (cpufeature sanitises APA/API/APA3 as “exact”). Check this on any heterogeneous cluster before turning it on.

FEAT_PAuth_LR

FEAT_PAuth_LR is a 2023 extension. Arm presents it with v9.5, although TF-A’s build documentation describes it as optional from v9.4, so check DDI0487 A2. It is reported by the value 0b0110 in whichever of ID_AA64ISAR1_EL1.APA/API or ID_AA64ISAR2_EL1.APA3 is non-zero. It adds the address of the signing instruction as a second modifier, which makes a signed LR bound to its function, not merely to a stack depth:

  • PACIASPPC / PACIBSPPC sign X30 with SP and the PC of the instruction itself.
  • AUTIASPPC <label> and RETAASPPC <label> authenticate with SP and a PC computed from a PC-relative immediate that points back at the signing instruction. The ...SPPCR Xm forms take that address in a register.
  • PACNBIASPPC is the variant without the implicit BTI.
  • PACIA171615 / AUTIA171615 take the pointer in X17 and two modifiers in X16 and X15.
  • PACM (HINT #39) sets PSTATE.PACM. While PACM is 1, the existing PACIASP and PACIA1716 take a second modifier (PC and X15 respectively), so a single hint retrofits old sequences. ID_AA64ISAR3_EL1.PACM reports whether this PACM behaviour is fully implemented or only trivially (see DDI0487 for what the trivial form guarantees).

The PSTATE.PACM behaviour is gated by SCTLR2_ELx.EnPACM/EnPACM0 and HCRX_EL2.PACMEn. TF-A sets SCTLR2_EL3.EnPACM and SCTLR2_EL1.EnPACM when ENABLE_FEAT_PAUTH_LR is set. SPSR_ELx has a PACM bit (bit 35 in TF-A’s definitions), so the state survives exceptions.

BTI

FEAT_BTI is permitted from v8.4, mandatory from v8.5, and reported in ID_AA64PFR1_EL1.BT. A page is guarded when its stage 1 Block or Page descriptor has the GP bit [50] set (stage 2 has no GP). In a guarded page, the first instruction executed after an indirect branch must be a compatible landing pad, or a Branch Target exception (EC 0x0D) is taken.

The mechanism is PSTATE.BTYPE, a 2-bit value that every branch sets and that the next instruction checks:

BranchBTYPE set
BR Xn / BRAA etc. from a guarded page, n = 16 or 170b01
BR Xn from a guarded page, any other register0b11
BR from a non-guarded page, any register0b01
BLR / BLRAA etc.0b10
Direct branches, RET, sequential execution0b00
Landing padAccepts BTYPE
BTI c01, 10 (call targets)
BTI j01, 11 (jump targets)
BTI jc01, 10, 11
BTI (no operand)none, so it is not a landing pad
PACIASP / PACIBSP01, 10, and 11 only when SCTLR_ELx.BT0/BT1 = 0

The X16/X17 rule exists so that linker veneers and PLT stubs, which branch with BR X16/X17, can land on a BTI c function entry. SCTLR_EL1.BT0 [35] and BT1 [36] make PACIxSP stricter for EL0 and EL1 respectively. BTYPE is saved in SPSR_ELx on exception entry and restored by ERET. An exception handler that returns to the same instruction (for example after a page fault on the landing pad) therefore preserves the check. Software that emulates a branch has to set SPSR.BTYPE itself.

Bring-up note: BTI is per page, not per image. TF-A builds its images with BTI landing pads and sets GP only on its own code regions. If a platform maps a ROM or a third-party blob as guarded and that blob was not built with -mbranch-protection, the first indirect call into it takes EC 0x0D. In the other direction, a Linux module built without BTI fails the same way only once the kernel maps it with PTE_GP.

Checkpoint

Q13.1.1 BL31 is built with BRANCH_PROTECTION=1 and runs on a FEAT_PAuth2 core without FPAC. On an SMC from Linux, BL31’s C code signs and authenticates its own return addresses. Which statement explains why BL31 must save Linux’s APIA key on entry?

  • A. SCR_EL3.APK = 1 makes EL3 use a banked copy of the key, which must be synchronised with the EL1 copy
  • B. The key must be saved only if Linux uses the B key, because EL3 cannot read the A key of a lower EL
  • C. There is a single set of _EL1 key registers shared by all ELs, so BL31 overwriting APIAKey would break every return address Linux signed before the SMC
  • D. It does not need to; ERET automatically restores the lower EL’s keys from SPSR_EL3
Answer

C. The PAuth keys are not banked by Exception level. EL3 signs with the same APIAKey{Hi,Lo}_EL1 registers that Linux uses. If BL31 installs its own key and does not restore Linux’s, then Linux’s next AUTIASP on a return address signed before the SMC fails. A is wrong because there are no banked EL3 keys; APK only controls trapping of key-register accesses. D is wrong because SPSR_EL3 holds PSTATE, not keys.

Read more: Arm ARM (DDI0487), Pointer authentication; APIAKeyHi_EL1; TF-A save_gp_pmcr_pauth_regs

Q13.1.2 A guarded page contains a function whose first instruction is BTI j. A C call through a function pointer (BLR X8) reaches it. What happens?

  • A. A Branch Target exception (EC 0x0D), because BLR sets BTYPE = 0b10 and BTI j accepts only 0b01 and 0b11
  • B. Nothing; BTI j accepts every BTYPE except 0b00
  • C. An exception only if SCTLR_EL1.BT1 = 1
  • D. A Branch Target exception only if X8 was not X16 or X17
Answer

A. BLR always sets BTYPE 0b10, a call. BTI j is a jump-only landing pad, so the check fails. The X16/X17 rule (D) applies to BR, not to BLR, and gives BTYPE 0b01 rather than avoiding the check. SCTLR_ELx.BTn (C) only affects whether PACIxSP accepts BTYPE 0b11.

Read more: Arm ARM (DDI0487), PSTATE.BTYPE; BTI instruction; Branch Target exception

Q13.1.3 Two functions, f and g, both called from main at the same stack depth, use PACIASP/AUTIASP. An attacker with a stack write primitive copies the signed LR saved by f into g’s frame. Which feature makes g’s AUTIASP-equivalent authentication fail?

  • A. FEAT_FPAC, because it adds the function’s address to the modifier
  • B. FEAT_EPAC, because it removes the error code that let the attacker detect failure
  • C. BTI, because the return lands in main without a landing pad
  • D. FEAT_PAuth_LR (PACIASPPC/RETAASPPC), because the signing instruction’s PC becomes a second modifier, so f’s signature does not verify in g
Answer

D. With SP as the only modifier, identical SP values make the two signed LRs interchangeable. FEAT_PAuth_LR adds the address of the signing instruction, which differs between f and g. FPAC (A) only changes how a failure is reported (an immediate EC 0x1C); it does not change the modifier. BTI (C) does not check RET targets.

Read more: Arm ARM (DDI0487), FEAT_PAuth_LR; PACIASPPC; RETAASPPC