@@ -173,10 +173,24 @@ class TrackedRegisters {
173173// / X30 is safe-to-dereference - the state computed for sub- and
174174// / super-registers is not inspected.
175175struct State {
176- // / A BitVector containing the registers that are either safe at function
177- // / entry and were not clobbered yet, or those not clobbered since being
178- // / authenticated.
176+ // / A BitVector containing the registers that are either authenticated
177+ // / (assuming failed authentication is permitted to produce an invalid
178+ // / address, provided it generates an error on memory access) or whose
179+ // / value is known not to be attacker-controlled under Pointer Authentication
180+ // / threat model. The registers in this set are either
181+ // / * not clobbered since being authenticated, or
182+ // / * trusted at function entry and were not clobbered yet, or
183+ // / * contain a safely materialized address.
179184 BitVector SafeToDerefRegs;
185+ // / A BitVector containing the registers that are either authenticated
186+ // / *successfully* or whose value is known not to be attacker-controlled
187+ // / under Pointer Authentication threat model.
188+ // / The registers in this set are either
189+ // / * authenticated and then checked to be authenticated successfully
190+ // / (and not clobbered since then), or
191+ // / * trusted at function entry and were not clobbered yet, or
192+ // / * contain a safely materialized address.
193+ BitVector TrustedRegs;
180194 // / A vector of sets, only used in the second data flow run.
181195 // / Each element in the vector represents one of the registers for which we
182196 // / track the set of last instructions that wrote to this register. For
@@ -189,7 +203,8 @@ struct State {
189203 State () {}
190204
191205 State (unsigned NumRegs, unsigned NumRegsToTrack)
192- : SafeToDerefRegs(NumRegs), LastInstWritingReg(NumRegsToTrack) {}
206+ : SafeToDerefRegs(NumRegs), TrustedRegs(NumRegs),
207+ LastInstWritingReg (NumRegsToTrack) {}
193208
194209 State &merge (const State &StateIn) {
195210 if (StateIn.empty ())
@@ -198,6 +213,7 @@ struct State {
198213 return (*this = StateIn);
199214
200215 SafeToDerefRegs &= StateIn.SafeToDerefRegs ;
216+ TrustedRegs &= StateIn.TrustedRegs ;
201217 for (unsigned I = 0 ; I < LastInstWritingReg.size (); ++I)
202218 for (const MCInst *J : StateIn.LastInstWritingReg [I])
203219 LastInstWritingReg[I].insert (J);
@@ -210,6 +226,7 @@ struct State {
210226
211227 bool operator ==(const State &RHS) const {
212228 return SafeToDerefRegs == RHS.SafeToDerefRegs &&
229+ TrustedRegs == RHS.TrustedRegs &&
213230 LastInstWritingReg == RHS.LastInstWritingReg ;
214231 }
215232 bool operator !=(const State &RHS) const { return !((*this ) == RHS); }
@@ -234,6 +251,7 @@ raw_ostream &operator<<(raw_ostream &OS, const State &S) {
234251 OS << " empty" ;
235252 } else {
236253 OS << " SafeToDerefRegs: " << S.SafeToDerefRegs << " , " ;
254+ OS << " TrustedRegs: " << S.TrustedRegs << " , " ;
237255 printLastInsts (OS, S.LastInstWritingReg );
238256 }
239257 OS << " >" ;
@@ -254,18 +272,22 @@ void PacStatePrinter::print(raw_ostream &OS, const State &S) const {
254272 OS << " pacret-state<" ;
255273 if (S.empty ()) {
256274 assert (S.SafeToDerefRegs .empty ());
275+ assert (S.TrustedRegs .empty ());
257276 assert (S.LastInstWritingReg .empty ());
258277 OS << " empty" ;
259278 } else {
260279 OS << " SafeToDerefRegs: " ;
261280 RegStatePrinter.print (OS, S.SafeToDerefRegs );
281+ OS << " , TrustedRegs: " ;
282+ RegStatePrinter.print (OS, S.TrustedRegs );
262283 OS << " , " ;
263284 printLastInsts (OS, S.LastInstWritingReg );
264285 }
265286 OS << " >" ;
266287}
267288
268- // / Computes which registers are safe to be used by control flow instructions.
289+ // / Computes which registers are safe to be used by control flow and signing
290+ // / instructions.
269291// /
270292// / This is the base class for two implementations: a dataflow-based analysis
271293// / which is intended to be used for most functions and a simplified CFG-unaware
@@ -292,6 +314,17 @@ class RegisterSafetyAnalysis {
292314 // / RegToTrackInstsFor is the set of registers for which the dataflow analysis
293315 // / must compute which the last set of instructions writing to it are.
294316 const TrackedRegisters RegsToTrackInstsFor;
317+ // / Stores information about the detected instruction sequences emitted to
318+ // / check an authenticated pointer. Specifically, if such sequence is detected
319+ // / in a basic block, it maps the last instruction of that basic block to
320+ // / (CheckedRegister, FirstInstOfTheSequence) pair, see the description of
321+ // / MCPlusBuilder::getAuthCheckedReg(BB) method.
322+ // /
323+ // / As the detection of such sequences requires iterating over the adjacent
324+ // / instructions, it should be done before calling computeNext(), which
325+ // / operates on separate instructions.
326+ DenseMap<const MCInst *, std::pair<MCPhysReg, const MCInst *>>
327+ CheckerSequenceInfo;
295328
296329 SmallPtrSet<const MCInst *, 4 > &lastWritingInsts (State &S,
297330 MCPhysReg Reg) const {
@@ -306,8 +339,10 @@ class RegisterSafetyAnalysis {
306339
307340 State createEntryState () {
308341 State S (NumRegs, RegsToTrackInstsFor.getNumTrackedRegisters ());
309- for (MCPhysReg Reg : BC.MIB ->getTrustedLiveInRegs ())
310- S.SafeToDerefRegs |= BC.MIB ->getAliases (Reg, /* OnlySmaller=*/ true );
342+ for (MCPhysReg Reg : BC.MIB ->getTrustedLiveInRegs ()) {
343+ S.TrustedRegs |= BC.MIB ->getAliases (Reg, /* OnlySmaller=*/ true );
344+ S.SafeToDerefRegs = S.TrustedRegs ;
345+ }
311346 return S;
312347 }
313348
@@ -354,6 +389,45 @@ class RegisterSafetyAnalysis {
354389 return Regs;
355390 }
356391
392+ // Returns all registers made trusted by this instruction.
393+ SmallVector<MCPhysReg> getRegsMadeTrusted (const MCInst &Point,
394+ const State &Cur) const {
395+ SmallVector<MCPhysReg> Regs;
396+ const MCPhysReg NoReg = BC.MIB ->getNoRegister ();
397+
398+ // An authenticated pointer can be checked, or
399+ MCPhysReg CheckedReg =
400+ BC.MIB ->getAuthCheckedReg (Point, /* MayOverwrite=*/ false );
401+ if (CheckedReg != NoReg && Cur.SafeToDerefRegs [CheckedReg])
402+ Regs.push_back (CheckedReg);
403+
404+ if (CheckerSequenceInfo.contains (&Point)) {
405+ MCPhysReg CheckedReg;
406+ const MCInst *FirstCheckerInst;
407+ std::tie (CheckedReg, FirstCheckerInst) = CheckerSequenceInfo.at (&Point);
408+
409+ // FirstCheckerInst should belong to the same basic block, meaning
410+ // it was deterministically processed a few steps before this instruction.
411+ const State &StateBeforeChecker = getStateBefore (*FirstCheckerInst).get ();
412+ if (StateBeforeChecker.SafeToDerefRegs [CheckedReg])
413+ Regs.push_back (CheckedReg);
414+ }
415+
416+ // ... a safe address can be materialized, or
417+ MCPhysReg NewAddrReg = BC.MIB ->getMaterializedAddressRegForPtrAuth (Point);
418+ if (NewAddrReg != NoReg)
419+ Regs.push_back (NewAddrReg);
420+
421+ // ... an address can be updated in a safe manner, producing the result
422+ // which is as trusted as the input address.
423+ if (auto DstAndSrc = BC.MIB ->analyzeAddressArithmeticsForPtrAuth (Point)) {
424+ if (Cur.TrustedRegs [DstAndSrc->second ])
425+ Regs.push_back (DstAndSrc->first );
426+ }
427+
428+ return Regs;
429+ }
430+
357431 State computeNext (const MCInst &Point, const State &Cur) {
358432 PacStatePrinter P (BC);
359433 LLVM_DEBUG ({
@@ -380,11 +454,34 @@ class RegisterSafetyAnalysis {
380454 BitVector Clobbered = getClobberedRegs (Point);
381455 SmallVector<MCPhysReg> NewSafeToDerefRegs =
382456 getRegsMadeSafeToDeref (Point, Cur);
457+ SmallVector<MCPhysReg> NewTrustedRegs = getRegsMadeTrusted (Point, Cur);
458+
459+ // Ideally, being trusted is a strictly stronger property than being
460+ // safe-to-dereference. To simplify the computation of Next state, enforce
461+ // this for NewSafeToDerefRegs and NewTrustedRegs. Additionally, this
462+ // fixes the properly for "cumulative" register states in tricky cases
463+ // like the following:
464+ //
465+ // ; LR is safe to dereference here
466+ // mov x16, x30 ; start of the sequence, LR is s-t-d right before
467+ // xpaclri ; clobbers LR, LR is not safe anymore
468+ // cmp x30, x16
469+ // b.eq 1f ; end of the sequence: LR is marked as trusted
470+ // brk 0x1234
471+ // 1:
472+ // ; at this point LR would be marked as trusted,
473+ // ; but not safe-to-dereference
474+ //
475+ for (auto TrustedReg : NewTrustedRegs) {
476+ if (!is_contained (NewSafeToDerefRegs, TrustedReg))
477+ NewSafeToDerefRegs.push_back (TrustedReg);
478+ }
383479
384480 // Then, compute the state after this instruction is executed.
385481 State Next = Cur;
386482
387483 Next.SafeToDerefRegs .reset (Clobbered);
484+ Next.TrustedRegs .reset (Clobbered);
388485 // Keep track of this instruction if it writes to any of the registers we
389486 // need to track that for:
390487 for (MCPhysReg Reg : RegsToTrackInstsFor.getRegisters ())
@@ -405,6 +502,10 @@ class RegisterSafetyAnalysis {
405502 lastWritingInsts (Next, Reg).clear ();
406503 }
407504
505+ // Process new trusted registers.
506+ for (MCPhysReg TrustedReg : NewTrustedRegs)
507+ Next.TrustedRegs |= BC.MIB ->getAliases (TrustedReg, /* OnlySmaller=*/ true );
508+
408509 LLVM_DEBUG ({
409510 dbgs () << " .. result: (" ;
410511 P.print (dbgs (), Next);
@@ -462,7 +563,22 @@ class DataflowRegisterSafetyAnalysis
462563 return DFParent::getStateBefore (Inst);
463564 }
464565
465- void run () override { DFParent::run (); }
566+ void run () override {
567+ for (BinaryBasicBlock &BB : Func) {
568+ if (auto CheckerInfo = BC.MIB ->getAuthCheckedReg (BB)) {
569+ MCInst *LastInstOfChecker = BB.getLastNonPseudoInstr ();
570+ LLVM_DEBUG ({
571+ dbgs () << " Found pointer checking sequence in " << BB.getName ()
572+ << " :\n " ;
573+ traceReg (BC, " Checked register" , CheckerInfo->first );
574+ traceInst (BC, " First instruction" , *CheckerInfo->second );
575+ traceInst (BC, " Last instruction" , *LastInstOfChecker);
576+ });
577+ CheckerSequenceInfo[LastInstOfChecker] = *CheckerInfo;
578+ }
579+ }
580+ DFParent::run ();
581+ }
466582
467583protected:
468584 void preflight () {}
@@ -658,6 +774,26 @@ shouldReportCallGadget(const BinaryContext &BC, const MCInstReference &Inst,
658774 return std::make_shared<GadgetReport>(CallKind, Inst, DestReg);
659775}
660776
777+ static std::shared_ptr<Report>
778+ shouldReportSigningOracle (const BinaryContext &BC, const MCInstReference &Inst,
779+ const State &S) {
780+ static const GadgetKind SigningOracleKind (" signing oracle found" );
781+
782+ MCPhysReg SignedReg = BC.MIB ->getSignedReg (Inst);
783+ if (SignedReg == BC.MIB ->getNoRegister ())
784+ return nullptr ;
785+
786+ LLVM_DEBUG ({
787+ traceInst (BC, " Found sign inst" , Inst);
788+ traceReg (BC, " Signed reg" , SignedReg);
789+ traceRegMask (BC, " TrustedRegs" , S.TrustedRegs );
790+ });
791+ if (S.TrustedRegs [SignedReg])
792+ return nullptr ;
793+
794+ return std::make_shared<GadgetReport>(SigningOracleKind, Inst, SignedReg);
795+ }
796+
661797template <typename T> static void iterateOverInstrs (BinaryFunction &BF, T Fn) {
662798 if (BF.hasCFG ()) {
663799 for (BinaryBasicBlock &BB : BF)
@@ -702,6 +838,8 @@ Analysis::findGadgets(BinaryFunction &BF,
702838
703839 if (auto Report = shouldReportCallGadget (BC, Inst, S))
704840 Result.Diagnostics .push_back (Report);
841+ if (auto Report = shouldReportSigningOracle (BC, Inst, S))
842+ Result.Diagnostics .push_back (Report);
705843 });
706844 return Result;
707845}
0 commit comments