Index | Thread | Search

From:
Brad Smith <brad@comstyle.com>
Subject:
Fix RETGUARD on arm64 when targeting CPUs with PAuth
To:
tech@openbsd.org
Cc:
Todd Mortimer <todd@opennet.ca>
Date:
Sat, 7 Nov 2026 16:10:08 -0500

Download raw body.

Thread
  • Brad Smith:

    Fix RETGUARD on arm64 when targeting CPUs with PAuth

When compiling for ARMv8.3-A or later (e.g. -march=armv8.3-a, or a
-mcpu that implies PAuth), LLVM folds the LR authentication and
return into a single retaa. That moves the authentication after the
RETGUARD epilogue check, which then compares the cookie against the
signed LR and traps on every function return. Don't do the fold when
RETGUARD is enabled, as is already done for ShadowCallStack, so
autiasp stays ahead of the check.

For example:

  $ cat hello.c
  #include <stdio.h>

  int
  main(void)
  {
          puts("hello C");
          return 0;
  }
  $ cc -march=armv8.3-a -o hello hello.c
  $ ./hello
  hello C
  Trace/BPT trap (core dumped)


Index: llvm/lib/Target/AArch64/AArch64PointerAuth.cpp
===================================================================
RCS file: /cvs/src/gnu/llvm/llvm/lib/Target/AArch64/AArch64PointerAuth.cpp,v
retrieving revision 1.1.1.2
diff -u -p -u -p -r1.1.1.2 AArch64PointerAuth.cpp
--- llvm/lib/Target/AArch64/AArch64PointerAuth.cpp	29 May 2026 11:00:45 -0000	1.1.1.2
+++ llvm/lib/Target/AArch64/AArch64PointerAuth.cpp	7 Nov 2026 07:43:59 -0000
@@ -14,6 +14,7 @@
 #include "AArch64Subtarget.h"
 #include "llvm/CodeGen/CFIInstBuilder.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 
@@ -177,8 +178,12 @@ void AArch64PointerAuth::authenticateLR(
   // From v8.3a onwards there are optimised authenticate LR and return
   // instructions, namely RETA{A,B}, that can be used instead. In this case the
   // DW_CFA_AARCH64_negate_ra_state can't be emitted.
+  // RETGUARD places its epilogue check between PAUTH_EPILOGUE and the RET,
+  // and that check needs the authenticated LR. Folding the authentication
+  // into RETAA/RETAB would run the check against the signed LR.
   bool TerminatorIsCombinable =
-      TI != MBB.end() && TI->getOpcode() == AArch64::RET;
+      TI != MBB.end() && TI->getOpcode() == AArch64::RET &&
+      !MF.getFrameInfo().getReturnProtectorNeeded();
   MCSymbol *PACSym = MFnI->getSigningInstrLabel();
 
   if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&