1d157a9bcSAndrew Paverd //===-- CFGuard.cpp - Control Flow Guard checks -----------------*- C++ -*-===//
2d157a9bcSAndrew Paverd //
3d157a9bcSAndrew Paverd // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d157a9bcSAndrew Paverd // See https://llvm.org/LICENSE.txt for license information.
5d157a9bcSAndrew Paverd // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d157a9bcSAndrew Paverd //
7d157a9bcSAndrew Paverd //===----------------------------------------------------------------------===//
8d157a9bcSAndrew Paverd ///
9d157a9bcSAndrew Paverd /// \file
10d157a9bcSAndrew Paverd /// This file contains the IR transform to add Microsoft's Control Flow Guard
11d157a9bcSAndrew Paverd /// checks on Windows targets.
12d157a9bcSAndrew Paverd ///
13d157a9bcSAndrew Paverd //===----------------------------------------------------------------------===//
14d157a9bcSAndrew Paverd
15d157a9bcSAndrew Paverd #include "llvm/Transforms/CFGuard.h"
16d157a9bcSAndrew Paverd #include "llvm/ADT/SmallVector.h"
17d157a9bcSAndrew Paverd #include "llvm/ADT/Statistic.h"
18d157a9bcSAndrew Paverd #include "llvm/ADT/Triple.h"
19d157a9bcSAndrew Paverd #include "llvm/IR/CallingConv.h"
20d157a9bcSAndrew Paverd #include "llvm/IR/IRBuilder.h"
21d157a9bcSAndrew Paverd #include "llvm/IR/Instruction.h"
22d157a9bcSAndrew Paverd #include "llvm/InitializePasses.h"
23d157a9bcSAndrew Paverd #include "llvm/Pass.h"
24d157a9bcSAndrew Paverd
25d157a9bcSAndrew Paverd using namespace llvm;
26d157a9bcSAndrew Paverd
27d157a9bcSAndrew Paverd using OperandBundleDef = OperandBundleDefT<Value *>;
28d157a9bcSAndrew Paverd
29d157a9bcSAndrew Paverd #define DEBUG_TYPE "cfguard"
30d157a9bcSAndrew Paverd
31d157a9bcSAndrew Paverd STATISTIC(CFGuardCounter, "Number of Control Flow Guard checks added");
32d157a9bcSAndrew Paverd
33d157a9bcSAndrew Paverd namespace {
34d157a9bcSAndrew Paverd
35d157a9bcSAndrew Paverd /// Adds Control Flow Guard (CFG) checks on indirect function calls/invokes.
36d157a9bcSAndrew Paverd /// These checks ensure that the target address corresponds to the start of an
37d157a9bcSAndrew Paverd /// address-taken function. X86_64 targets use the CF_Dispatch mechanism. X86,
38d157a9bcSAndrew Paverd /// ARM, and AArch64 targets use the CF_Check machanism.
39d157a9bcSAndrew Paverd class CFGuard : public FunctionPass {
40d157a9bcSAndrew Paverd public:
41d157a9bcSAndrew Paverd static char ID;
42d157a9bcSAndrew Paverd
43d157a9bcSAndrew Paverd enum Mechanism { CF_Check, CF_Dispatch };
44d157a9bcSAndrew Paverd
45d157a9bcSAndrew Paverd // Default constructor required for the INITIALIZE_PASS macro.
CFGuard()46d157a9bcSAndrew Paverd CFGuard() : FunctionPass(ID) {
47d157a9bcSAndrew Paverd initializeCFGuardPass(*PassRegistry::getPassRegistry());
48d157a9bcSAndrew Paverd // By default, use the guard check mechanism.
49d157a9bcSAndrew Paverd GuardMechanism = CF_Check;
50d157a9bcSAndrew Paverd }
51d157a9bcSAndrew Paverd
52d157a9bcSAndrew Paverd // Recommended constructor used to specify the type of guard mechanism.
CFGuard(Mechanism Var)53d157a9bcSAndrew Paverd CFGuard(Mechanism Var) : FunctionPass(ID) {
54d157a9bcSAndrew Paverd initializeCFGuardPass(*PassRegistry::getPassRegistry());
55d157a9bcSAndrew Paverd GuardMechanism = Var;
56d157a9bcSAndrew Paverd }
57d157a9bcSAndrew Paverd
58d157a9bcSAndrew Paverd /// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG
59d157a9bcSAndrew Paverd /// check mechanism. When the image is loaded, the loader puts the appropriate
60d157a9bcSAndrew Paverd /// guard check function pointer in the __guard_check_icall_fptr global
61d157a9bcSAndrew Paverd /// symbol. This checks that the target address is a valid address-taken
62d157a9bcSAndrew Paverd /// function. The address of the target function is passed to the guard check
63d157a9bcSAndrew Paverd /// function in an architecture-specific register (e.g. ECX on 32-bit X86,
64d157a9bcSAndrew Paverd /// X15 on Aarch64, and R0 on ARM). The guard check function has no return
65d157a9bcSAndrew Paverd /// value (if the target is invalid, the guard check funtion will raise an
66d157a9bcSAndrew Paverd /// error).
67d157a9bcSAndrew Paverd ///
68d157a9bcSAndrew Paverd /// For example, the following LLVM IR:
69d157a9bcSAndrew Paverd /// \code
70d157a9bcSAndrew Paverd /// %func_ptr = alloca i32 ()*, align 8
71d157a9bcSAndrew Paverd /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
72d157a9bcSAndrew Paverd /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
73d157a9bcSAndrew Paverd /// %1 = call i32 %0()
74d157a9bcSAndrew Paverd /// \endcode
75d157a9bcSAndrew Paverd ///
76d157a9bcSAndrew Paverd /// is transformed to:
77d157a9bcSAndrew Paverd /// \code
78d157a9bcSAndrew Paverd /// %func_ptr = alloca i32 ()*, align 8
79d157a9bcSAndrew Paverd /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
80d157a9bcSAndrew Paverd /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
81d157a9bcSAndrew Paverd /// %1 = load void (i8*)*, void (i8*)** @__guard_check_icall_fptr
82d157a9bcSAndrew Paverd /// %2 = bitcast i32 ()* %0 to i8*
83d157a9bcSAndrew Paverd /// call cfguard_checkcc void %1(i8* %2)
84d157a9bcSAndrew Paverd /// %3 = call i32 %0()
85d157a9bcSAndrew Paverd /// \endcode
86d157a9bcSAndrew Paverd ///
87d157a9bcSAndrew Paverd /// For example, the following X86 assembly code:
88d157a9bcSAndrew Paverd /// \code
89d157a9bcSAndrew Paverd /// movl $_target_func, %eax
90d157a9bcSAndrew Paverd /// calll *%eax
91d157a9bcSAndrew Paverd /// \endcode
92d157a9bcSAndrew Paverd ///
93d157a9bcSAndrew Paverd /// is transformed to:
94d157a9bcSAndrew Paverd /// \code
95d157a9bcSAndrew Paverd /// movl $_target_func, %ecx
96d157a9bcSAndrew Paverd /// calll *___guard_check_icall_fptr
97d157a9bcSAndrew Paverd /// calll *%ecx
98d157a9bcSAndrew Paverd /// \endcode
99d157a9bcSAndrew Paverd ///
100d157a9bcSAndrew Paverd /// \param CB indirect call to instrument.
101d157a9bcSAndrew Paverd void insertCFGuardCheck(CallBase *CB);
102d157a9bcSAndrew Paverd
103d157a9bcSAndrew Paverd /// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG
104d157a9bcSAndrew Paverd /// dispatch mechanism. When the image is loaded, the loader puts the
105d157a9bcSAndrew Paverd /// appropriate guard check function pointer in the
106d157a9bcSAndrew Paverd /// __guard_dispatch_icall_fptr global symbol. This checks that the target
107d157a9bcSAndrew Paverd /// address is a valid address-taken function and, if so, tail calls the
108d157a9bcSAndrew Paverd /// target. The target address is passed in an architecture-specific register
109d157a9bcSAndrew Paverd /// (e.g. RAX on X86_64), with all other arguments for the target function
110d157a9bcSAndrew Paverd /// passed as usual.
111d157a9bcSAndrew Paverd ///
112d157a9bcSAndrew Paverd /// For example, the following LLVM IR:
113d157a9bcSAndrew Paverd /// \code
114d157a9bcSAndrew Paverd /// %func_ptr = alloca i32 ()*, align 8
115d157a9bcSAndrew Paverd /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
116d157a9bcSAndrew Paverd /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
117d157a9bcSAndrew Paverd /// %1 = call i32 %0()
118d157a9bcSAndrew Paverd /// \endcode
119d157a9bcSAndrew Paverd ///
120d157a9bcSAndrew Paverd /// is transformed to:
121d157a9bcSAndrew Paverd /// \code
122d157a9bcSAndrew Paverd /// %func_ptr = alloca i32 ()*, align 8
123d157a9bcSAndrew Paverd /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
124d157a9bcSAndrew Paverd /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
125d157a9bcSAndrew Paverd /// %1 = load i32 ()*, i32 ()** @__guard_dispatch_icall_fptr
126d157a9bcSAndrew Paverd /// %2 = call i32 %1() [ "cfguardtarget"(i32 ()* %0) ]
127d157a9bcSAndrew Paverd /// \endcode
128d157a9bcSAndrew Paverd ///
129d157a9bcSAndrew Paverd /// For example, the following X86_64 assembly code:
130d157a9bcSAndrew Paverd /// \code
131d157a9bcSAndrew Paverd /// leaq target_func(%rip), %rax
132d157a9bcSAndrew Paverd /// callq *%rax
133d157a9bcSAndrew Paverd /// \endcode
134d157a9bcSAndrew Paverd ///
135d157a9bcSAndrew Paverd /// is transformed to:
136d157a9bcSAndrew Paverd /// \code
137d157a9bcSAndrew Paverd /// leaq target_func(%rip), %rax
138d157a9bcSAndrew Paverd /// callq *__guard_dispatch_icall_fptr(%rip)
139d157a9bcSAndrew Paverd /// \endcode
140d157a9bcSAndrew Paverd ///
141d157a9bcSAndrew Paverd /// \param CB indirect call to instrument.
142d157a9bcSAndrew Paverd void insertCFGuardDispatch(CallBase *CB);
143d157a9bcSAndrew Paverd
144d157a9bcSAndrew Paverd bool doInitialization(Module &M) override;
145d157a9bcSAndrew Paverd bool runOnFunction(Function &F) override;
146d157a9bcSAndrew Paverd
147d157a9bcSAndrew Paverd private:
148d157a9bcSAndrew Paverd // Only add checks if the module has the cfguard=2 flag.
149d157a9bcSAndrew Paverd int cfguard_module_flag = 0;
150d157a9bcSAndrew Paverd Mechanism GuardMechanism = CF_Check;
151d157a9bcSAndrew Paverd FunctionType *GuardFnType = nullptr;
152d157a9bcSAndrew Paverd PointerType *GuardFnPtrType = nullptr;
153d157a9bcSAndrew Paverd Constant *GuardFnGlobal = nullptr;
154d157a9bcSAndrew Paverd };
155d157a9bcSAndrew Paverd
156d157a9bcSAndrew Paverd } // end anonymous namespace
157d157a9bcSAndrew Paverd
insertCFGuardCheck(CallBase * CB)158d157a9bcSAndrew Paverd void CFGuard::insertCFGuardCheck(CallBase *CB) {
159d157a9bcSAndrew Paverd
160d157a9bcSAndrew Paverd assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() &&
161d157a9bcSAndrew Paverd "Only applicable for Windows targets");
162d157a9bcSAndrew Paverd assert(CB->isIndirectCall() &&
163d157a9bcSAndrew Paverd "Control Flow Guard checks can only be added to indirect calls");
164d157a9bcSAndrew Paverd
165d157a9bcSAndrew Paverd IRBuilder<> B(CB);
166d157a9bcSAndrew Paverd Value *CalledOperand = CB->getCalledOperand();
167d157a9bcSAndrew Paverd
168*181c4ba4SChoongwoo Han // If the indirect call is called within catchpad or cleanuppad,
169*181c4ba4SChoongwoo Han // we need to copy "funclet" bundle of the call.
170*181c4ba4SChoongwoo Han SmallVector<llvm::OperandBundleDef, 1> Bundles;
171*181c4ba4SChoongwoo Han if (auto Bundle = CB->getOperandBundle(LLVMContext::OB_funclet))
172*181c4ba4SChoongwoo Han Bundles.push_back(OperandBundleDef(*Bundle));
173*181c4ba4SChoongwoo Han
174d157a9bcSAndrew Paverd // Load the global symbol as a pointer to the check function.
175d157a9bcSAndrew Paverd LoadInst *GuardCheckLoad = B.CreateLoad(GuardFnPtrType, GuardFnGlobal);
176d157a9bcSAndrew Paverd
177d157a9bcSAndrew Paverd // Create new call instruction. The CFGuard check should always be a call,
178d157a9bcSAndrew Paverd // even if the original CallBase is an Invoke or CallBr instruction.
179d157a9bcSAndrew Paverd CallInst *GuardCheck =
180d157a9bcSAndrew Paverd B.CreateCall(GuardFnType, GuardCheckLoad,
181*181c4ba4SChoongwoo Han {B.CreateBitCast(CalledOperand, B.getInt8PtrTy())}, Bundles);
182d157a9bcSAndrew Paverd
183d157a9bcSAndrew Paverd // Ensure that the first argument is passed in the correct register
184d157a9bcSAndrew Paverd // (e.g. ECX on 32-bit X86 targets).
185d157a9bcSAndrew Paverd GuardCheck->setCallingConv(CallingConv::CFGuard_Check);
186d157a9bcSAndrew Paverd }
187d157a9bcSAndrew Paverd
insertCFGuardDispatch(CallBase * CB)188d157a9bcSAndrew Paverd void CFGuard::insertCFGuardDispatch(CallBase *CB) {
189d157a9bcSAndrew Paverd
190d157a9bcSAndrew Paverd assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() &&
191d157a9bcSAndrew Paverd "Only applicable for Windows targets");
192d157a9bcSAndrew Paverd assert(CB->isIndirectCall() &&
193d157a9bcSAndrew Paverd "Control Flow Guard checks can only be added to indirect calls");
194d157a9bcSAndrew Paverd
195d157a9bcSAndrew Paverd IRBuilder<> B(CB);
196d157a9bcSAndrew Paverd Value *CalledOperand = CB->getCalledOperand();
197d157a9bcSAndrew Paverd Type *CalledOperandType = CalledOperand->getType();
198d157a9bcSAndrew Paverd
199d157a9bcSAndrew Paverd // Cast the guard dispatch global to the type of the called operand.
200d157a9bcSAndrew Paverd PointerType *PTy = PointerType::get(CalledOperandType, 0);
201d157a9bcSAndrew Paverd if (GuardFnGlobal->getType() != PTy)
202d157a9bcSAndrew Paverd GuardFnGlobal = ConstantExpr::getBitCast(GuardFnGlobal, PTy);
203d157a9bcSAndrew Paverd
204d157a9bcSAndrew Paverd // Load the global as a pointer to a function of the same type.
205d157a9bcSAndrew Paverd LoadInst *GuardDispatchLoad = B.CreateLoad(CalledOperandType, GuardFnGlobal);
206d157a9bcSAndrew Paverd
207d157a9bcSAndrew Paverd // Add the original call target as a cfguardtarget operand bundle.
208d157a9bcSAndrew Paverd SmallVector<llvm::OperandBundleDef, 1> Bundles;
209d157a9bcSAndrew Paverd CB->getOperandBundlesAsDefs(Bundles);
210d157a9bcSAndrew Paverd Bundles.emplace_back("cfguardtarget", CalledOperand);
211d157a9bcSAndrew Paverd
212d157a9bcSAndrew Paverd // Create a copy of the call/invoke instruction and add the new bundle.
21369dca6efSRoman Lebedev assert((isa<CallInst>(CB) || isa<InvokeInst>(CB)) &&
21469dca6efSRoman Lebedev "Unknown indirect call type");
21569dca6efSRoman Lebedev CallBase *NewCB = CallBase::Create(CB, Bundles, CB);
216d157a9bcSAndrew Paverd
217d157a9bcSAndrew Paverd // Change the target of the call to be the guard dispatch function.
218d157a9bcSAndrew Paverd NewCB->setCalledOperand(GuardDispatchLoad);
219d157a9bcSAndrew Paverd
220d157a9bcSAndrew Paverd // Replace the original call/invoke with the new instruction.
221d157a9bcSAndrew Paverd CB->replaceAllUsesWith(NewCB);
222d157a9bcSAndrew Paverd
223d157a9bcSAndrew Paverd // Delete the original call/invoke.
224d157a9bcSAndrew Paverd CB->eraseFromParent();
225d157a9bcSAndrew Paverd }
226d157a9bcSAndrew Paverd
doInitialization(Module & M)227d157a9bcSAndrew Paverd bool CFGuard::doInitialization(Module &M) {
228d157a9bcSAndrew Paverd
229d157a9bcSAndrew Paverd // Check if this module has the cfguard flag and read its value.
230d157a9bcSAndrew Paverd if (auto *MD =
231d157a9bcSAndrew Paverd mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
232d157a9bcSAndrew Paverd cfguard_module_flag = MD->getZExtValue();
233d157a9bcSAndrew Paverd
234d157a9bcSAndrew Paverd // Skip modules for which CFGuard checks have been disabled.
235d157a9bcSAndrew Paverd if (cfguard_module_flag != 2)
236d157a9bcSAndrew Paverd return false;
237d157a9bcSAndrew Paverd
238d157a9bcSAndrew Paverd // Set up prototypes for the guard check and dispatch functions.
239d157a9bcSAndrew Paverd GuardFnType = FunctionType::get(Type::getVoidTy(M.getContext()),
240d157a9bcSAndrew Paverd {Type::getInt8PtrTy(M.getContext())}, false);
241d157a9bcSAndrew Paverd GuardFnPtrType = PointerType::get(GuardFnType, 0);
242d157a9bcSAndrew Paverd
243d157a9bcSAndrew Paverd // Get or insert the guard check or dispatch global symbols.
244d157a9bcSAndrew Paverd if (GuardMechanism == CF_Check) {
245d157a9bcSAndrew Paverd GuardFnGlobal =
246d157a9bcSAndrew Paverd M.getOrInsertGlobal("__guard_check_icall_fptr", GuardFnPtrType);
247d157a9bcSAndrew Paverd } else {
248d157a9bcSAndrew Paverd assert(GuardMechanism == CF_Dispatch && "Invalid CFGuard mechanism");
249d157a9bcSAndrew Paverd GuardFnGlobal =
250d157a9bcSAndrew Paverd M.getOrInsertGlobal("__guard_dispatch_icall_fptr", GuardFnPtrType);
251d157a9bcSAndrew Paverd }
252d157a9bcSAndrew Paverd
253d157a9bcSAndrew Paverd return true;
254d157a9bcSAndrew Paverd }
255d157a9bcSAndrew Paverd
runOnFunction(Function & F)256d157a9bcSAndrew Paverd bool CFGuard::runOnFunction(Function &F) {
257d157a9bcSAndrew Paverd
258bdd88b7eSAndrew Paverd // Skip modules for which CFGuard checks have been disabled.
259bdd88b7eSAndrew Paverd if (cfguard_module_flag != 2)
260d157a9bcSAndrew Paverd return false;
261d157a9bcSAndrew Paverd
262d157a9bcSAndrew Paverd SmallVector<CallBase *, 8> IndirectCalls;
263d157a9bcSAndrew Paverd
264d157a9bcSAndrew Paverd // Iterate over the instructions to find all indirect call/invoke/callbr
265d157a9bcSAndrew Paverd // instructions. Make a separate list of pointers to indirect
266d157a9bcSAndrew Paverd // call/invoke/callbr instructions because the original instructions will be
267d157a9bcSAndrew Paverd // deleted as the checks are added.
268d157a9bcSAndrew Paverd for (BasicBlock &BB : F.getBasicBlockList()) {
269d157a9bcSAndrew Paverd for (Instruction &I : BB.getInstList()) {
270d157a9bcSAndrew Paverd auto *CB = dyn_cast<CallBase>(&I);
271bdd88b7eSAndrew Paverd if (CB && CB->isIndirectCall() && !CB->hasFnAttr("guard_nocf")) {
272d157a9bcSAndrew Paverd IndirectCalls.push_back(CB);
273d157a9bcSAndrew Paverd CFGuardCounter++;
274d157a9bcSAndrew Paverd }
275d157a9bcSAndrew Paverd }
276d157a9bcSAndrew Paverd }
277d157a9bcSAndrew Paverd
278bdd88b7eSAndrew Paverd // If no checks are needed, return early.
279d157a9bcSAndrew Paverd if (IndirectCalls.empty()) {
280d157a9bcSAndrew Paverd return false;
281d157a9bcSAndrew Paverd }
282d157a9bcSAndrew Paverd
283d157a9bcSAndrew Paverd // For each indirect call/invoke, add the appropriate dispatch or check.
284d157a9bcSAndrew Paverd if (GuardMechanism == CF_Dispatch) {
285d157a9bcSAndrew Paverd for (CallBase *CB : IndirectCalls) {
286d157a9bcSAndrew Paverd insertCFGuardDispatch(CB);
287d157a9bcSAndrew Paverd }
288d157a9bcSAndrew Paverd } else {
289d157a9bcSAndrew Paverd for (CallBase *CB : IndirectCalls) {
290d157a9bcSAndrew Paverd insertCFGuardCheck(CB);
291d157a9bcSAndrew Paverd }
292d157a9bcSAndrew Paverd }
293d157a9bcSAndrew Paverd
294d157a9bcSAndrew Paverd return true;
295d157a9bcSAndrew Paverd }
296d157a9bcSAndrew Paverd
297d157a9bcSAndrew Paverd char CFGuard::ID = 0;
298d157a9bcSAndrew Paverd INITIALIZE_PASS(CFGuard, "CFGuard", "CFGuard", false, false)
299d157a9bcSAndrew Paverd
createCFGuardCheckPass()300d157a9bcSAndrew Paverd FunctionPass *llvm::createCFGuardCheckPass() {
301d157a9bcSAndrew Paverd return new CFGuard(CFGuard::CF_Check);
302d157a9bcSAndrew Paverd }
303d157a9bcSAndrew Paverd
createCFGuardDispatchPass()304d157a9bcSAndrew Paverd FunctionPass *llvm::createCFGuardDispatchPass() {
305d157a9bcSAndrew Paverd return new CFGuard(CFGuard::CF_Dispatch);
306d157a9bcSAndrew Paverd }
307