[1] 1:STAT 2:PERKS 3:OSS 4:INV 5:COMMS 6:DATA
[ --- ]

Pointer Authentication on arm64e: tightening function pointers

#writeup #cybersecurity #low-level #arm64e

Code for everything referenced here: github.com/fr4nsyz/PAC.

LLDB showing a signed function pointer, its PAC field, and the actual target address

Imagine this, the compiler loads a function pointer from memory and branches to whatever address it finds:

; plain arm64, -O0
ldr  x8, [sp, #16]   ; load the stored function pointer
blr  x8              ; branch to it; no integrity check

If an attacker can write that memory slot, they choose the branch target.

Below is a demonstrated redirect in m1_fnptr.c: overwriting o.op made the program call unexpected_handler instead of add_one. On arm64e the same call site is different:

; arm64e, -O0
ldr    x8, [sp, #16]
blraaz x8            ; authenticated indirect branch (IA key, zero modifier)

With this, the CPU will now reject a pointer whose signature does not pass verification.

Pointer Authentication (PAC) turns a pointer into a value that the CPU can verify before use. On AArch64 it is an architecture extension (PAuth, mandatory from Armv8.3-A); on Apple platforms it is deployed as the arm64e ABI.

PAC is widely described as a mitigation for control-flow hijacking, but the strength of that mitigation depends entirely on which authentication context a pointer is bound to.

The default arm64e context for C function pointers is intentionally broad:

  • one key (for different instruction / data pointers)
  • discriminator zero (acts like associated data in encryption, but this is purely for hashing)
  • no address diversity

This begs the question, how can we ensure that a function pointer is only usable in its intended authentication context, rather than relying exclusively on the default pointer authentication scheme?

The goal of my exploration into PAC is to be able to understand and find an answer to this question. Particularly, this involves how we might tighten scopes with the __ptrauth type qualifier on MacOS.

How Pointer Authentication Works

A PAC is a keyed hash. sign(pointer, modifier, key) computes a MAC (Message Authentication Code) over the pointer and a 64-bit modifier, then stores the truncated result in the pointer's spare high bits. authenticate recomputes it, compares, and removes it.

At first glance, it might seem useless to compute a long MAC that just gets truncated because that would reduce the number of possible bit combinations an attacker must try, but since we store the PAC alongside the 64 bits of the pointer itself, we need to truncate it due to size constraints.

Computing the longer MAC is still very important though because running the full-strength MAC first (even if it will be truncated) keeps the bits that ARE kept highly unpredictable.

Keys

Arm PAuth provides five 128-bit keys, stored in system registers that only privileged code can read or write:

Key Use
IA (ASIA, 0) instruction pointers
IB (ASIB, 1) instruction pointers
DA (ASDA, 2) data pointers
DB (ASDB, 3) data pointers
GA (ASGA) generic pacga signatures

Which key signs which pointer is an ABI decision which can vary across architectures.

One thing that confused me was that A and B keys do not have the same uses across operating systems.

Key usage is an ABI thing, not a hardware thing. In our generic AArch64 build with -march=armv8.3-a -mbranch-protection=pac-ret, Clang signed the return address with IA. On arm64e, it uses IB.

Clang's docs say arm64e mostly uses A keys for global stuff like function pointers and vtables, and B keys for local stuff like return addresses. A keys tend to be shared across processes, B keys tend not to be.

Something to note though is that this is entirely up to OS convention, there is no hardware guarantee of this behavior.

Modifiers / discriminators

The modifier is the extra 64-bit value mixed into the hash. Clang calls it a discriminator.

Sign, authenticate, strip

Just an FYI, throughout this writeup, I might put a * after things like PAC, AUT, or XPAC.

When I do this, I am referring to a wild card. I know with pointers * means dereference and that can get confusing in our context, so I just want to get that settled before I confuse anyone haha.

Alright, moving on:

  • PAC* signs. Observed form: pacda x8, x9 (sign the data pointer in x8, modifier in x9, DA key).
  • AUT* authenticates. Baseline Armv8.3 AUT* does not trap on failure; it leaves a corrupted pointer that faults when used.
  • XPAC* strips the code without authenticating.
  • FEAT_FPAC (Armv8.6-A, "Faulting on PAC") makes a failed AUT* raise an exception immediately.

On baseline Armv8.3, AUT* does not raise an exception. On failure it returns a corrupted pointer that would only fault when that pointer is later used.

Clang therefore adds its own check and trap around it, which I found below at -O2:

autda  x16, x8            ; authenticate (DA key), modifier in x8
mov    x17, x16
xpacd  x17                ; strip the PAC
cmp    x16, x17
b.eq   .Lauth_success
brk    #0xc472            ; trap if authentication failed
.Lauth_success:
ldr    w0, [x16]

On success the authenticated value is already canonical, so stripping it is a no-op and the compare succeeds. On failure the corrupted high bits remain in x16, so the compare fails.

The brk is not part of AUT*. It is the explicit trap Clang inserted precisely because AUT* does not trap. With FEAT_FPAC, the hardware faults at AUT* itself and this check is unnecessary.

Where the PAC lives (measured on the test host)

I probably could have just looked this up for my host architecture, but I wanted to have a little fun:

I signed 1024 data pointers with different discriminators, XOR each signed value with its raw address to cancel the address bits (uintptr_t diff = signed ^ raw), then bitwise-OR all those diff values together (mask |= diff). The only bits that ever changed would be our PAC field:

union of changed bits: 0x007f800000000000
set bit positions: 54 53 52 51 50 49 48 47    -> 8-bit field

So on this host the PAC occupies bits 47–54 (an 8-bit field).

You might be thinking now, 8 bits does not give many possible values. Could an attacker not try a blind forgery?

They could, but it's not an easy brute force. A forged pointer has a 1/256 chance of carrying the right signature for that key, modifier, and target address. A wrong guess traps right away, either from FPAC or Clang's brk, so there's no retry loop they can do in the same process and no useful feedback from failures.

The main issue PAC really does well against is substitution (copying a validly signed pointer from one field into another), which only works when both fields use the same signing schema. More on substitution prevention later.

Under arm64e's default IA,0 schema, it works. That's the A case in hardened_ops.c. Once each field has its own context, the copy fails every time.

Understanding arm64e and Its Default ABI

Terms:

  • AArch64 the instruction set.
  • PAuth / PAC the optional AArch64 extension providing PAC*/AUT*.
  • arm64e Apple's ABI: a contract specifying which pointers are signed, with which key and modifier, how signed pointers are stored, and how the linker and dynamic loader cooperate.

The default key map (arm64e)

Pointer kind Key Modifier Observed on the test host
Return address IB stack pointer on entry pacibsp … retab
C function pointer IA always 0 paciza, @AUTH(ia,0), braaz/blraaz
C++ vtable pointer DA address diversity + vtable id (runtime)
ObjC isa/super DA address diversity + 0x6AE1 / 0x25DA (runtime)
ObjC selector DB 0x57C2 for SEL ivars (runtime)
Ordinary C data pointer none — raw .quad, no signature

Note: the vtable pointer and the function entries inside it don't use the same schema.

The pointer uses DA, address diversity, and a discriminator from the mangled vtable identifier of the primary base class.

The function entries use IA, address diversity, and a discriminator from the function's mangled name.

For isa and super, both use address diversity, with 0x6AE1 for isa and 0x25DA for super.

m3_defaults.c:

; return address
pacibsp
...
retab

; C function pointer in data and its call site
_fn_ptr:
    .quad _target@AUTH(ia,0)     ; signed at load: IA key, discriminator 0
...
adrp x8, _fn_ptr@PAGE
ldr  x1, [x8, _fn_ptr@PAGEOFF]
braaz x1                          ; authenticated branch, zero modifier

; ordinary data pointer (same file)
_data_ptr:
    .quad _data_global            ; raw address, no signature

The default C function-pointer context

Key IA, discriminator 0, no address diversity.

There is effectively one function-pointer context in the WHOLE process. Any correctly signed C function pointer can authenticate in any C function-pointer slot. The ABI gives us "is this a validly signed function pointer?" but not "is it the function this field is supposed to hold?"

This is not an oversight though, because C function pointers must remain interchangeable across static libraries as well as separately compiled objects like translation units and dyld shared caches. The ABI, therefore, cannot bake a per-field discriminator into every function pointer and break compatibility.

arm64e is not "AArch64 with PAC"

The same instructions can be spent by different ABIs in very different ways. A generic AArch64 (Linux) build with -march=armv8.3-a -mbranch-protection=pac-ret produced:

caller:
    paciasp          ; return address signed with the IA key (A key by default)
    ...
    retaa

indirect:
    ldr  x2, [x0]
    br   x2          ; function pointers are NOT authenticated

versus arm64e:

caller:
    pacibsp          ; IB key
    ...
    retab

indirect:
    ldr  x1, [...]
    braaz x1         ; authenticated

There are two main differences here.

First, the return-address key choice differs: generic pac-ret defaults to the A key whereas arm64e uses the B key.

Second, the base Linux ABI does not sign function pointers at all, while arm64e signs them automatically.

What Apple added on top of Arm's extension is therefore policy and infrastructure:

  • an ABI specification (which pointer kinds are signed, with which keys and modifiers)
  • compiler, linker, and dyld integration
  • runtime support in objc4, libc++, and Swift, each with assigned discriminators
  • conventions for key usage (the A / B keys usages that I spoke of earlier in the article);

Pointer Substitution and Authentication Contexts

A pointer's authentication context has two inputs: the key and the modifier. The key is secret and the modifier is not.

To prevent substitution, the context must be different for each field we care about, so a pointer signed for one field cannot authenticate in another.

When the compiler knows both fields statically, it does the right thing on a typed assignment. It authenticates under the source schema and re-signs under the destination schema.

Discriminators and Address Diversity

Two independent ways to build a field-specific context:

  • A constant discriminator (16-bit) distinguishes categories: 0x1234 for open, 0x5678 for read, 0x9ABC for close.
  • Address diversity binds the pointer to its storage location (the modifier includes the address at which the pointer is stored)

Blending the two means replacing the top 16 bits of the storage address with the constant. The compiler generates this, then signs it with the IA key:

ldr  x1, [x0]                ; load pointer from the field
mov  x16, x0                 ; x0 = &struct (storage address)
movk x16, #4660, lsl #48     ; 4660 = 0x1234 -> top 16 bits
braa x1, x16                 ; authenticated branch, modifier in x16

; initialization for the same field:
mov  x16, x19                ; storage address
movk x16, #4660, lsl #48
pacia x8, x16                ; sign with IA key

Because the context contains both the field's address and its discriminator, a value copied from read to open carries the wrong modifier and fails authentication.

Clang also rejects direct memcpy between differently-qualified pointer types at compile time (which is pretty cool if you ask me).

Enforcing Stronger Authentication with __ptrauth

Okay FINALLY onto using __ptrauth to give your code PAC where it might not be automatic.

__ptrauth is a Clang type qualifier: __ptrauth(key, address_diversity, discriminator). It goes after the * and before the field name. Include <ptrauth.h> to use the named keys:

#include <ptrauth.h>

#define DEVICE_OP(d) __ptrauth(ptrauth_key_function_pointer, 1, d)

struct DeviceOps {
    int (*DEVICE_OP(0x1234) open)(void);
    int (*DEVICE_OP(0x5678) read)(void);
    int (*DEVICE_OP(0x9ABC) close)(void);
};

ptrauth_key_function_pointer is the IA key in this toolchain. The 1 enables address diversity. The third argument is a 16-bit constant discriminator, unique per field.

Building a Hardened Function-Pointer Table

hardened_ops.c builds both versions side by side and tests each substitution in a forked child so the failed authentication is contained:

A: raw substitution        accepted, returned 22
B: raw substitution        REJECTED (signal 10)
B: typed assignment        accepted, returned 22

Version A is the ordinary table. version B applies the qualifiers above.

Reproduce with:

clang -target arm64e-apple-macos11 -O1 hardened_ops.c -o hardened_ops
./hardened_ops

References