| 1 | //===---- KCFI.cpp - Implements Kernel Control-Flow Integrity (KCFI) ------===// |
| 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 | // This pass implements Kernel Control-Flow Integrity (KCFI) indirect call |
| 10 | // check lowering. For each call instruction with a cfi-type attribute, it |
| 11 | // emits an arch-specific check before the call, and bundles the check and |
| 12 | // the call to prevent unintentional modifications. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/CodeGen/KCFI.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBundle.h" |
| 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 21 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 22 | #include "llvm/CodeGen/TargetLowering.h" |
| 23 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/InitializePasses.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "kcfi" |
| 30 | #define KCFI_PASS_NAME "Insert KCFI indirect call checks" |
| 31 | |
| 32 | STATISTIC(NumKCFIChecksAdded, "Number of indirect call checks added" ); |
| 33 | |
| 34 | namespace { |
| 35 | class KCFI { |
| 36 | public: |
| 37 | bool run(MachineFunction &MF); |
| 38 | |
| 39 | private: |
| 40 | /// Machine instruction info used throughout the class. |
| 41 | const TargetInstrInfo *TII = nullptr; |
| 42 | |
| 43 | /// Target lowering for arch-specific parts. |
| 44 | const TargetLowering *TLI = nullptr; |
| 45 | |
| 46 | /// Emits a KCFI check before an indirect call. |
| 47 | /// \returns true if the check was added and false otherwise. |
| 48 | bool emitCheck(MachineBasicBlock &MBB, |
| 49 | MachineBasicBlock::instr_iterator I) const; |
| 50 | }; |
| 51 | |
| 52 | class MachineKCFILegacy : public MachineFunctionPass { |
| 53 | public: |
| 54 | static char ID; |
| 55 | |
| 56 | MachineKCFILegacy() : MachineFunctionPass(ID) {} |
| 57 | |
| 58 | StringRef getPassName() const override { return KCFI_PASS_NAME; } |
| 59 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 60 | return KCFI().run(MF); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | char MachineKCFILegacy::ID = 0; |
| 65 | } // end anonymous namespace |
| 66 | |
| 67 | INITIALIZE_PASS(MachineKCFILegacy, DEBUG_TYPE, KCFI_PASS_NAME, false, false) |
| 68 | |
| 69 | FunctionPass *llvm::createKCFIPass() { return new MachineKCFILegacy(); } |
| 70 | |
| 71 | PreservedAnalyses MachineKCFIPass::run(MachineFunction &MF, |
| 72 | MachineFunctionAnalysisManager &MFAM) { |
| 73 | if (!KCFI().run(MF)) |
| 74 | return PreservedAnalyses::all(); |
| 75 | |
| 76 | PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses(); |
| 77 | PA.preserveSet<CFGAnalyses>(); |
| 78 | return PA; |
| 79 | } |
| 80 | |
| 81 | bool KCFI::emitCheck(MachineBasicBlock &MBB, |
| 82 | MachineBasicBlock::instr_iterator MBBI) const { |
| 83 | assert(TII && "Target instruction info was not initialized" ); |
| 84 | assert(TLI && "Target lowering was not initialized" ); |
| 85 | |
| 86 | // If the call instruction is bundled, we can only emit a check safely if |
| 87 | // it's the first instruction in the bundle. |
| 88 | if (MBBI->isBundled() && !std::prev(x: MBBI)->isBundle()) |
| 89 | report_fatal_error(reason: "Cannot emit a KCFI check for a bundled call" ); |
| 90 | |
| 91 | // Emit a KCFI check for the call instruction at MBBI. The implementation |
| 92 | // must unfold memory operands if applicable. |
| 93 | MachineInstr *Check = TLI->EmitKCFICheck(MBB, MBBI, TII); |
| 94 | |
| 95 | // Clear the original call's CFI type. |
| 96 | assert(MBBI->isCall() && "Unexpected instruction type" ); |
| 97 | MBBI->setCFIType(MF&: *MBB.getParent(), Type: 0); |
| 98 | |
| 99 | // If not already bundled, bundle the check and the call to prevent |
| 100 | // further changes. |
| 101 | if (!MBBI->isBundled()) |
| 102 | finalizeBundle(MBB, FirstMI: Check->getIterator(), LastMI: std::next(x: MBBI->getIterator())); |
| 103 | |
| 104 | ++NumKCFIChecksAdded; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool KCFI::run(MachineFunction &MF) { |
| 109 | const Module *M = MF.getFunction().getParent(); |
| 110 | if (!M->getModuleFlag(Key: "kcfi" )) |
| 111 | return false; |
| 112 | |
| 113 | const auto &SubTarget = MF.getSubtarget(); |
| 114 | TII = SubTarget.getInstrInfo(); |
| 115 | TLI = SubTarget.getTargetLowering(); |
| 116 | |
| 117 | bool Changed = false; |
| 118 | for (MachineBasicBlock &MBB : MF) { |
| 119 | // Use instr_iterator because we don't want to skip bundles. |
| 120 | for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(), |
| 121 | MIE = MBB.instr_end(); |
| 122 | MII != MIE; ++MII) { |
| 123 | if (MII->isCall() && MII->getCFIType()) |
| 124 | Changed |= emitCheck(MBB, MBBI: MII); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return Changed; |
| 129 | } |
| 130 | |