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
| Register | Content |
|---|---|
GCSPR_EL0..GCSPR_EL3 | GCS 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/EL3 | Per-EL control for the EL itself |
GCSCRE0_EL1 | Control for EL0 |
The GCSCR_ELx fields are:
| Field | Bit | Meaning |
|---|---|---|
PCRSEL | 0 | Procedure call/return enable: BL/BLR push and RET pop through the GCS |
RVCHKEN | 5 | Return value check: RET compares the popped value with the target in LR |
EXLOCKEN | 6 | Exception-return lock (not in GCSCRE0_EL1) |
PUSHMEn | 8 | Permit GCSPUSHM explicit pushes |
STREn | 9 | Permit GCSSTR/GCSSTTR explicit stores to GCS memory |
nTR | 10 | GCSCRE0_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,STPand atomics from any EL fault as a permission fault; - GCS stores (implicit pushes,
GCSPUSHM,GCSSTRwhenSTREnpermits) 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:
| Instruction | Purpose |
|---|---|
GCSPUSHM Xt / GCSPOPM Xt | Push or pop an arbitrary entry (used by setjmp/longjmp unwinders) |
GCSSS1 Xt / GCSSS2 Xt | Two-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 / GCSSTTR | Explicit stores to GCS memory, gated by STREn |
GCSPUSHX / GCSPOPX / GCSPOPCX | Push or pop an exception return record (EL1 and above), letting a kernel protect its own nested exception returns |
GCSB DSYNC | GCS barrier (HINT; NOP without GCS) |
CHKFEAT X16 | FEAT_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.PCRSELis set. Otherwise the very nextBLtakes a permission fault on the push, and the fault handler’s ownBLfaults again. SetGCSPR_EL1and the mapping first, then setPCRSELand synchronise with anISB. 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):
| Family | 64-bit | 128-bit pair |
|---|---|---|
| Compare and swap | RCWCAS{A,L,AL} | RCWCASP* |
| Bit set / clear | RCWSET*, RCWCLR* | RCWSETP*, RCWCLRP* |
| Swap | RCWSWP* | RCWSWPP* |
| “Soft” variants | RCWSCAS*, 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_EL1holds the bit-fields of a descriptor that an RCW instruction may change in a Protected descriptor.RCWSMASK_EL1is a software mask that theRCWS*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 inS2PIR_EL2intended 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
ISBafter writingGCSPR_EL0on PE1 - B. A
GCSB DSYNCon 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 ISHon PE1 before theERET, because GCS loads bypass the cache - D. Setting
GCSCR_EL1.EXLOCKENso 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.
RCWCASon a Protected descriptor always succeeds; onlyRCWSCASis 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 x16and test bit 0 of X16 - B.
mrs x0, GCSCRE0_EL1and testPCRSEL - C. Read
ID_AA64PFR1_EL1.GCSthrough the kernel’s MRS emulation - D.
mrs x0, GCSPR_EL0and 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