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.2 Guarded Control Stack and Translation Hardening

PAuth makes a corrupted return address detectable with high probability, but it is probabilistic and it is vulnerable to signing gadgets. The Guarded Control Stack (GCS) makes return addresses unforgeable by keeping a second copy in memory that ordinary stores cannot write. The Translation Hardening Extension (THE) applies a similar idea to the translation tables themselves: it gives software a checked update path for descriptors, so that a kernel write primitive is not automatically a page-table write primitive.

FEAT_GCS: versions and enables

GCS was announced as part of Arm’s 2022 extensions and is an Armv9-A feature. Arm material places it as OPTIONAL from v9.3, while some vendor and TF-A commit text says v9.4, so take the version from DDI0487 A2 and the ID register, not from -march. It is reported by ID_AA64PFR1_EL1.GCS [47:44]. GCS memory is identified through stage 1 permission indirection, so in practice it needs FEAT_S1PIE (5.5).

GCS is gated in the usual pattern (17.3): SCR_EL3.GCSEn [39] for EL2 and below, HCRX_EL2.GCSEn [22] for EL1 and EL0. If these are clear, the GCS registers trap and GCS is not active at the lower ELs. TF-A sets SCR_EL3.GCSEn and context-switches the EL2 GCS registers with ENABLE_FEAT_GCS.

Registers

RegisterContent
GCSPR_EL0..GCSPR_EL3GCS pointer, PTR [63:3]. 8-byte aligned, grows down. EL0 can read GCSPR_EL0 only when GCSCRE0_EL1.nTR = 1, and can update it only through GCS instructions
GCSCR_EL1/EL2/EL3Per-EL control for the EL itself
GCSCRE0_EL1Control for EL0

The GCSCR_ELx fields are:

FieldBitMeaning
PCRSEL0Procedure call/return enable: BL/BLR push and RET pop through the GCS
RVCHKEN5Return value check: RET compares the popped value with the target in LR
EXLOCKEN6Exception-return lock (not in GCSCRE0_EL1)
PUSHMEn8Permit GCSPUSHM explicit pushes
STREn9Permit GCSSTR/GCSSTTR explicit stores to GCS memory
nTR10GCSCRE0_EL1 only. When 0, EL0 reads of GCSPR_EL0 trap; Linux always sets it to 1

How it works

When PCRSEL is set, every BL or BLR also pushes the return address: GCSPR_ELx -= 8, then a GCS store of LR at the new pointer. RET (and RETAA and friends) performs a GCS load from GCSPR_ELx, then increments the pointer. If RVCHKEN is set and the loaded value differs from the branch target, a GCS exception is taken: EC 0x2D, with ISS.ExType = 0 (data check) and ISS.IT identifying the instruction (RET, RETAA, GCSPOPM, GCSSS1, …).

GCS memory is a distinct permission. In stage 1 permission indirection there is a GCS encoding (Linux uses PIE_GCS = 0x9) under which:

  • ordinary STR, STP and atomics from any EL fault as a permission fault;
  • GCS stores (implicit pushes, GCSPUSHM, GCSSTR when STREn permits) succeed;
  • ordinary loads are allowed, which is how debuggers and unwinders read the stack.

A fault caused by a GCS access is a normal data abort, marked by ISS2.GCS (bit 8 of ISS2) in ESR_ELx. Linux reports GCS data-check failures (EC 0x2D) as SIGSEGV with SEGV_CPERR.

The remaining instructions are:

InstructionPurpose
GCSPUSHM Xt / GCSPOPM XtPush or pop an arbitrary entry (used by setjmp/longjmp unwinders)
GCSSS1 Xt / GCSSS2 XtTwo-step stack switch. The target stack must hold a valid cap token (address in [63:12] and token 0x1). Switching writes an in-progress token (0x5) to the old stack, so neither stack can be used twice
GCSSTR / GCSSTTRExplicit stores to GCS memory, gated by STREn
GCSPUSHX / GCSPOPX / GCSPOPCXPush or pop an exception return record (EL1 and above), letting a kernel protect its own nested exception returns
GCSB DSYNCGCS barrier (HINT; NOP without GCS)
CHKFEAT X16FEAT_CHK: clears bit 0 of X16 if GCS is enabled at the current EL

Exception entry does not push anything onto the GCS. The interrupted context’s GCSPR_ELx simply stays where it was, and the handler at the higher EL uses its own GCSPR.

GCSB DSYNC

GCS stores are not ordinary explicit stores, so a plain DMB does not order them. GCSB DSYNC orders the GCS memory effects of earlier instructions before later memory accesses that are ordered by a barrier or by release semantics. In the other direction, it orders acquire accesses before later GCS effects. Linux issues it on context switch whenever the previous or next task has GCS enabled. The reason given in gcs_thread_switch() is that a migrated thread’s GCS pushes must be visible to the PE that runs it next. It is also issued before reading or modifying another thread’s GCS, for example in signal delivery. Without it, a task migrated between PEs can pop a stale value and take a spurious EC 0x2D, which is a classic intermittent failure (7.5, 7.10).

PSTATE.EXLOCK

EXLOCK protects the handler’s own return state. When GCSCR_ELx.EXLOCKEN = 1, an exception taken from the same EL sets PSTATE.EXLOCK to 1. An exception from a lower EL clears it. While EXLOCK is 1, writes to ELR_ELx and SPSR_ELx at that EL generate a GCS exception with ExType = EXLOCK. An ERET that returns to the same EL with EXLOCK = 0 while EXLOCKEN = 1 is an illegal exception return. The effect is that a nested handler cannot be tricked into rewriting the return state of the frame below it unless it goes through the GCS exception-return record instructions. EXLOCK is saved in SPSR_ELx like the other PSTATE bits (2.4).

FEAT_CHK

CHKFEAT (HINT #40) exists so that runtime code can ask “is GCS on right now?” without reading a system register that might trap:

    mov     x16, #1
    chkfeat x16             // NOP on old cores: x16 stays 1
    cbz     x16, gcs_on     // bit 0 cleared => GCS enabled at this EL

Because it is a HINT, it is safe on every core. On a core without GCS the answer is always “off”. QEMU implements FEAT_CHK with GCS as its only defined bit, which reflects the current architecture.

Bring-up note: Enabling GCS for EL1 itself (a kernel shadow stack) needs a GCS region mapped with the GCS permission before GCSCR_EL1.PCRSEL is set. Otherwise the very next BL takes a permission fault on the push, and the fault handler’s own BL faults again. Set GCSPR_EL1 and the mapping first, then set PCRSEL and synchronise with an ISB. Expect the first return after enabling to fail, because that return address was never pushed. Linux documents the user-space equivalent: you cannot return from the function that enabled GCS.

FEAT_THE: Translation Hardening

FEAT_THE is a 2022 extension, the same wave as GCS. TF-A describes it as optional from v8.8, so check DDI0487 A2 for the version. It is reported by ID_AA64PFR1_EL1.THE [51:48]. The threat it addresses is an attacker with an arbitrary-write primitive in EL1 who flips descriptor bits (AP, UXN/PXN, the output address) to gain execute or write access. THE adds three things.

1. The Protected attribute. When TCR2_EL1.PnCH [0] = 1, bit [52] of stage 1 descriptors for the TTBRn_EL1 regime is reinterpreted as Protected and stops being the Contiguous bit. In the 128-bit D128 format the Protected bit is always present, at [114], independent of PnCH (sources differ on whether PnCH reads as RES0 or RES1 when TCR2_EL1.D128 = 1; see the register description) (5.7).

2. Read-Check-Write instructions. These are atomic read-modify-write instructions for descriptors, modelled on the LSE atomics (10.2):

Family64-bit128-bit pair
Compare and swapRCWCAS{A,L,AL}RCWCASP*
Bit set / clearRCWSET*, RCWCLR*RCWSETP*, RCWCLRP*
SwapRCWSWP*RCWSWPP*
“Soft” variantsRCWSCAS*, RCWSSET*, RCWSCLR*, RCWSSWP*...P forms

Each one loads the descriptor and computes the new value. It then stores the new value only if the RCW checks pass, or, for the RCWS* forms, if the RCW and RCWS checks both pass. The outcome is reported in NZCV, so software can branch on a refused update. The 64-bit forms are UNDEFINED when D128 is enabled for the regime. The architecture says these instructions are for translation-table updates, not for general use.

3. Mask registers.

  • RCWMASK_EL1 holds the bit-fields of a descriptor that an RCW instruction may change in a Protected descriptor.
  • RCWSMASK_EL1 is a software mask that the RCWS* forms also apply.

Lower-EL access to both is trapped to EL3 unless SCR_EL3.RCWMASKEn [42] = 1. TF-A sets this bit and context-switches the registers when ENABLE_FEAT_THE is set.

TCR2_EL1.PTTWI [10] (with HCRX_EL2.PTTWI) lets RCWS writes carry the Reduced Coherence property, in which the write need not be coherent with translation table walks until the usual TLB-maintenance sequence completes. It is a performance option and does not change the barrier recipe in 9.1.

The masks limit which bits change through the checked path. On their own they do not stop a plain STR to a table page. The full threat model therefore needs a component above EL1 that also:

  • keeps the table pages unwritable by ordinary stores. Stage 2 permission indirection provides “MostlyRO” (MRO, MRO-TL0/TL1) permissions in S2PIR_EL2 intended for this; see DDI0487 for the exact rules on which writes they admit (11.3);
  • prevents EL1 from widening the masks after boot, for example with fine-grained write traps on RCWMASK_EL1, or the register-mask features.

Read THE as the EL1 half of a hypervisor-assisted kernel-integrity design, not as a stand-alone defence.

Security note: In-process isolation, keeping one library’s data away from another’s within the same address space, is the job of permission overlays (FEAT_S1POE, POR_EL0) described in 5.5. POE is complementary to GCS. GCS protects control flow; POE protects data domains, with keys that user space can switch without a system call.

Checkpoint

Q13.2.1 A thread with GCS enabled is preempted on PE0, after which the kernel migrates it to PE1. Occasionally its first RET on PE1 takes an EC 0x2D data-check failure even though no attacker is present. Which missing step is the most likely cause?

  • A. An ISB after writing GCSPR_EL0 on PE1
  • B. A GCSB DSYNC on PE0’s context switch, so that the thread’s earlier GCS pushes are ordered before the release that publishes the thread to the run queue
  • C. A DSB ISH on PE1 before the ERET, because GCS loads bypass the cache
  • D. Setting GCSCR_EL1.EXLOCKEN so that the return state is locked
Answer

B. GCS stores are not ordered by a plain barrier or by the release in the scheduler’s lock. GCSB DSYNC orders the GCS memory effects before subsequent release or barrier-ordered accesses, which is what Linux’s gcs_thread_switch() does. Without it, PE1 can observe an old GCS entry. A (an ISB) handles context synchronization of the register write, not memory ordering of the pushes. C is wrong because GCS accesses are coherent memory accesses; the problem is ordering, not caching.

Read more: Arm ARM (DDI0487), The Guarded Control Stack; GCSB DSYNC

Q13.2.2 With FEAT_THE and TCR2_EL1.PnCH = 1, which statement about a stage 1 descriptor in the TTBR1_EL1 tables is correct?

  • A. Bit [52] is still the Contiguous hint, and Protected is carried in a software-reserved bit
  • B. A Protected descriptor cannot be modified by any write from EL1
  • C. RCWCAS on a Protected descriptor always succeeds; only RCWSCAS is checked
  • D. Bit [52] is the Protected bit, so these mappings cannot use the Contiguous hint, and an RCW update that would change a bit outside the permitted mask is refused, with the result reported in NZCV
Answer

D. PnCH repurposes bit [52] as Protected for the TTBRn_EL1 regime, which removes the Contiguous hint for those mappings. RCW instructions perform a conditional store and report the result in the condition flags. B overstates the mechanism: the architecture checks the RCW path, and blocking ordinary stores to table pages needs stage 2 permissions on top. C is wrong because the RCW checks apply to the non-S forms as well; RCWS adds the soft-mask check.

Read more: Arm ARM (DDI0487), TCR2_EL1.PnCH; RCWCAS; RCWMASK_EL1

Q13.2.3 A run-time library needs to know whether GCS is active for the current thread, and it must run unchanged on v8.2 cores. Which approach is architecturally safe?

  • A. mov x16, #1; chkfeat x16 and test bit 0 of X16
  • B. mrs x0, GCSCRE0_EL1 and test PCRSEL
  • C. Read ID_AA64PFR1_EL1.GCS through the kernel’s MRS emulation
  • D. mrs x0, GCSPR_EL0 and treat a non-zero value as “enabled”
Answer

A. CHKFEAT is in the HINT space, so it is a NOP on cores without FEAT_CHK, which leaves X16 = 1 (“not enabled”). Where FEAT_CHK is implemented it clears bit 0 when GCS is enabled at the current EL. B is an EL1 register, so it is UNDEFINED at EL0. C reports only that the feature is implemented, not that it is enabled for this thread. D is UNDEFINED on pre-GCS cores, and a non-zero pointer does not prove PCRSEL is set.

Read more: Arm ARM (DDI0487), CHKFEAT; FEAT_CHK