10b57cec5SDimitry Andric //===-- AArch64BranchTargets.cpp -- Harden code using v8.5-A BTI extension -==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass inserts BTI instructions at the start of every function and basic 100b57cec5SDimitry Andric // block which could be indirectly called. The hardware will (when enabled) 110b57cec5SDimitry Andric // trap when an indirect branch or call instruction targets an instruction 120b57cec5SDimitry Andric // which is not a valid BTI instruction. This is intended to guard against 130b57cec5SDimitry Andric // control-flow hijacking attacks. Note that this does not do anything for RET 140b57cec5SDimitry Andric // instructions, as they can be more precisely protected by return address 150b57cec5SDimitry Andric // signing. 160b57cec5SDimitry Andric // 170b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 180b57cec5SDimitry Andric 19e8d8bef9SDimitry Andric #include "AArch64MachineFunctionInfo.h" 200b57cec5SDimitry Andric #include "AArch64Subtarget.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 250b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric using namespace llvm; 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric #define DEBUG_TYPE "aarch64-branch-targets" 300b57cec5SDimitry Andric #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets" 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric namespace { 330b57cec5SDimitry Andric class AArch64BranchTargets : public MachineFunctionPass { 340b57cec5SDimitry Andric public: 350b57cec5SDimitry Andric static char ID; 360b57cec5SDimitry Andric AArch64BranchTargets() : MachineFunctionPass(ID) {} 370b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override; 380b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 390b57cec5SDimitry Andric StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; } 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric private: 420b57cec5SDimitry Andric void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump); 430b57cec5SDimitry Andric }; 440b57cec5SDimitry Andric } // end anonymous namespace 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric char AArch64BranchTargets::ID = 0; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets", 490b57cec5SDimitry Andric AARCH64_BRANCH_TARGETS_NAME, false, false) 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const { 520b57cec5SDimitry Andric AU.setPreservesCFG(); 530b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric FunctionPass *llvm::createAArch64BranchTargetsPass() { 570b57cec5SDimitry Andric return new AArch64BranchTargets(); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) { 61e8d8bef9SDimitry Andric if (!MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement()) 620b57cec5SDimitry Andric return false; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric LLVM_DEBUG( 650b57cec5SDimitry Andric dbgs() << "********** AArch64 Branch Targets **********\n" 660b57cec5SDimitry Andric << "********** Function: " << MF.getName() << '\n'); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // LLVM does not consider basic blocks which are the targets of jump tables 690b57cec5SDimitry Andric // to be address-taken (the address can't escape anywhere else), but they are 700b57cec5SDimitry Andric // used for indirect branches, so need BTI instructions. 710b57cec5SDimitry Andric SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets; 720b57cec5SDimitry Andric if (auto *JTI = MF.getJumpTableInfo()) 730b57cec5SDimitry Andric for (auto &JTE : JTI->getJumpTables()) 740b57cec5SDimitry Andric for (auto *MBB : JTE.MBBs) 750b57cec5SDimitry Andric JumpTableTargets.insert(MBB); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric bool MadeChange = false; 780b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 790b57cec5SDimitry Andric bool CouldCall = false, CouldJump = false; 80*fe6060f1SDimitry Andric // Even in cases where a function has internal linkage and is only called 81*fe6060f1SDimitry Andric // directly in its translation unit, it can still be called indirectly if 82*fe6060f1SDimitry Andric // the linker decides to add a thunk to it for whatever reason (say, for 83*fe6060f1SDimitry Andric // example, if it is finally placed far from its call site and a BL is not 84*fe6060f1SDimitry Andric // long-range enough). PLT entries and tail-calls use BR, but when they are 850b57cec5SDimitry Andric // are in guarded pages should all use x16 or x17 to hold the called 860b57cec5SDimitry Andric // address, so we don't need to set CouldJump here. BR instructions in 870b57cec5SDimitry Andric // non-guarded pages (which might be non-BTI-aware code) are allowed to 880b57cec5SDimitry Andric // branch to a "BTI c" using any register. 89*fe6060f1SDimitry Andric if (&MBB == &*MF.begin()) 900b57cec5SDimitry Andric CouldCall = true; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // If the block itself is address-taken, it could be indirectly branched 930b57cec5SDimitry Andric // to, but not called. 940b57cec5SDimitry Andric if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB)) 950b57cec5SDimitry Andric CouldJump = true; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric if (CouldCall || CouldJump) { 980b57cec5SDimitry Andric addBTI(MBB, CouldCall, CouldJump); 990b57cec5SDimitry Andric MadeChange = true; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric return MadeChange; 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall, 1070b57cec5SDimitry Andric bool CouldJump) { 1080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "") 1090b57cec5SDimitry Andric << (CouldCall ? "c" : "") << " to " << MBB.getName() 1100b57cec5SDimitry Andric << "\n"); 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>( 1130b57cec5SDimitry Andric MBB.getParent()->getSubtarget().getInstrInfo()); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric unsigned HintNum = 32; 1160b57cec5SDimitry Andric if (CouldCall) 1170b57cec5SDimitry Andric HintNum |= 2; 1180b57cec5SDimitry Andric if (CouldJump) 1190b57cec5SDimitry Andric HintNum |= 4; 1200b57cec5SDimitry Andric assert(HintNum != 32 && "No target kinds!"); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric auto MBBI = MBB.begin(); 1230b57cec5SDimitry Andric 124*fe6060f1SDimitry Andric // Skip the meta instructions, those will be removed anyway. 125*fe6060f1SDimitry Andric for (; MBBI != MBB.end() && 126*fe6060f1SDimitry Andric (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY); 127*fe6060f1SDimitry Andric ++MBBI) 128e837bb5cSDimitry Andric ; 129e837bb5cSDimitry Andric 130e837bb5cSDimitry Andric // SCTLR_EL1.BT[01] is set to 0 by default which means 131e837bb5cSDimitry Andric // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there. 132e837bb5cSDimitry Andric if (MBBI != MBB.end() && HintNum == 34 && 133e837bb5cSDimitry Andric (MBBI->getOpcode() == AArch64::PACIASP || 1340b57cec5SDimitry Andric MBBI->getOpcode() == AArch64::PACIBSP)) 1350b57cec5SDimitry Andric return; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()), 1380b57cec5SDimitry Andric TII->get(AArch64::HINT)) 1390b57cec5SDimitry Andric .addImm(HintNum); 1400b57cec5SDimitry Andric } 141