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