[PATCH 0/1] arm64: gcs: bind signal GCS restore to the live GCSPR_EL0

黄华成 posted 1 patch 4 hours ago
Only 0 patches received!
[PATCH 0/1] arm64: gcs: bind signal GCS restore to the live GCSPR_EL0
Posted by 黄华成 4 hours ago
Hello,
We audited the arm64 GCS signal-restore protocol against x86 CET and the RISC-V usercfi implementation and found that restore_gcs_context() accepts any gcspr from the user signal frame (the in-tree comment states "We let userspace set GCSPR_EL0 to anything here, we will validate later"), and gcs_restore_signal() then validates only a self-referential value relation at that address:cap == GCS_SIGNAL_CAP(gcspr).  A still-live signal cap belonging to a different, never-restored signal context satisfies this by construction.

Consequence: with a memory-corruption primitive sufficient to corrupt a sigframe (the threat model shadow stacks exist for), a nested handler can point its own frame's gcspr at the outer handler's live cap.  The kernel accepts it, zeroes the outer context's cap, and restores GCSPR_EL0 to the attacker-chosen position + 8 - a kernel-mediated rewind to a historical depth.  We demonstrated the full chain on QEMU TCG FEAT_GCS with this kernel tree (9-arm probe, 5 runs each, fully
deterministic):

  A1/A3C  normal + nested legitimate round-trips            : PASS
  A2      frame.gcspr +8 to a capless address               : killed
          (the value check exists and works)
  A3/A5   cross-context stale-cap rewrite (A5 locates its own
          frame by stack scan only - no privileged ucontext
          pointer, the realistic attacker model)            : GCSPR
          rewound, control resumes at the attacker depth, 5/5
  A4      the stolen-from outer handler's later sigreturn   : killed
          (its cap was consumed by the thief)
  A6      full CFI bypass: after the rewind, a deliberate
          ldr x30/<RA>; ret pops the rewound slot and is
          ACCEPTED, returning at a historical return address
          and skipping the rest of the anchor function - 5/5,
          while the identical ret without the rewind is killed.

x86 CET does not have this shape (its sigframe carries no SSP field); RISC-V usercfi had the same shape and an equivalent six-line binding has been proposed there in parallel.

The patch binds the frame's gcspr to the live GCSPR_EL0 (equality; pinned empirically at zero delta for every legitimate restore at every nesting depth - the handlr's return pops its trampoline record before rt_sigreturn).  Userspace cannot move the live GCSPR_EL0 without
executing GCS instructions, which are themselves policed.

Compatibility evidence (the question we expect first):

  R1-R3  normal / nested / sigaltstack round-trips           : PASS both
  R4-R6  raw (non-GCS-aware) longjmp/setcontext/swapcontext  : killed
         on BOTH unpatched and patched kernels - architectural
         GCS behavior (raw jumps that skip sigreturn are exactly
         what GCS rejects); the patch adds zero behavioral delta
  R7     GCSPOPM userspace unwind - the primitive glibc's GCS
         series actually uses for __longjmp (v9, Khrustalev:
         MRS_GCSPR save + GCSPOPM pop loop + GCSSS1/GCSSS2
         cross-stack switch, no rt_sigreturn dependency): PASS on
         both kernels; ordinary longjmp is orthogonal to this patch
  R8     GCSPOPM attempted across a signal cap: killed on both -
         the architecture forbids userspace cap-crossing, so no
         libc can bypass sigreturn via GCSPOPM; the only sanctioned
         restore path is the kernel's, whose legitimate forms all
         satisfy the binding

One semantic boundary worth naming: a siglongjmp that abandons a still-pending outer handler frame leaves live GCSPR inconsistent with that outer frame; with this patch such a frame's later sigreturn fails with -EINVAL instead of silently rewinding to an arbitrary position. We believe reject-by-default is the correct side of that trade, but flag it for discussion; a per-signal-depth token stack would also close the attack and avoid the question at higher bookkeeping cost.

Reproducer (probe, scripts, pinned kernel-image hashes, logs) available on request or via a branch.

Thanks,
Looking forward to your reply!