17fa27ce4SDimitry Andric //===---- KCFI.cpp - Implements Kernel Control-Flow Integrity (KCFI) ------===//
27fa27ce4SDimitry Andric //
37fa27ce4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47fa27ce4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
57fa27ce4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67fa27ce4SDimitry Andric //
77fa27ce4SDimitry Andric //===----------------------------------------------------------------------===//
87fa27ce4SDimitry Andric //
97fa27ce4SDimitry Andric // This pass implements Kernel Control-Flow Integrity (KCFI) indirect call
107fa27ce4SDimitry Andric // check lowering. For each call instruction with a cfi-type attribute, it
117fa27ce4SDimitry Andric // emits an arch-specific check before the call, and bundles the check and
127fa27ce4SDimitry Andric // the call to prevent unintentional modifications.
137fa27ce4SDimitry Andric //
147fa27ce4SDimitry Andric //===----------------------------------------------------------------------===//
157fa27ce4SDimitry Andric
167fa27ce4SDimitry Andric #include "llvm/ADT/Statistic.h"
177fa27ce4SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
187fa27ce4SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
197fa27ce4SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h"
207fa27ce4SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
217fa27ce4SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
227fa27ce4SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
237fa27ce4SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
24ac9a064cSDimitry Andric #include "llvm/IR/Module.h"
257fa27ce4SDimitry Andric #include "llvm/InitializePasses.h"
267fa27ce4SDimitry Andric
277fa27ce4SDimitry Andric using namespace llvm;
287fa27ce4SDimitry Andric
297fa27ce4SDimitry Andric #define DEBUG_TYPE "kcfi"
307fa27ce4SDimitry Andric #define KCFI_PASS_NAME "Insert KCFI indirect call checks"
317fa27ce4SDimitry Andric
327fa27ce4SDimitry Andric STATISTIC(NumKCFIChecksAdded, "Number of indirect call checks added");
337fa27ce4SDimitry Andric
347fa27ce4SDimitry Andric namespace {
357fa27ce4SDimitry Andric class KCFI : public MachineFunctionPass {
367fa27ce4SDimitry Andric public:
377fa27ce4SDimitry Andric static char ID;
387fa27ce4SDimitry Andric
KCFI()397fa27ce4SDimitry Andric KCFI() : MachineFunctionPass(ID) {}
407fa27ce4SDimitry Andric
getPassName() const417fa27ce4SDimitry Andric StringRef getPassName() const override { return KCFI_PASS_NAME; }
427fa27ce4SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
437fa27ce4SDimitry Andric
447fa27ce4SDimitry Andric private:
457fa27ce4SDimitry Andric /// Machine instruction info used throughout the class.
467fa27ce4SDimitry Andric const TargetInstrInfo *TII = nullptr;
477fa27ce4SDimitry Andric
487fa27ce4SDimitry Andric /// Target lowering for arch-specific parts.
497fa27ce4SDimitry Andric const TargetLowering *TLI = nullptr;
507fa27ce4SDimitry Andric
517fa27ce4SDimitry Andric /// Emits a KCFI check before an indirect call.
527fa27ce4SDimitry Andric /// \returns true if the check was added and false otherwise.
537fa27ce4SDimitry Andric bool emitCheck(MachineBasicBlock &MBB,
547fa27ce4SDimitry Andric MachineBasicBlock::instr_iterator I) const;
557fa27ce4SDimitry Andric };
567fa27ce4SDimitry Andric
577fa27ce4SDimitry Andric char KCFI::ID = 0;
587fa27ce4SDimitry Andric } // end anonymous namespace
597fa27ce4SDimitry Andric
INITIALIZE_PASS(KCFI,DEBUG_TYPE,KCFI_PASS_NAME,false,false)607fa27ce4SDimitry Andric INITIALIZE_PASS(KCFI, DEBUG_TYPE, KCFI_PASS_NAME, false, false)
617fa27ce4SDimitry Andric
627fa27ce4SDimitry Andric FunctionPass *llvm::createKCFIPass() { return new KCFI(); }
637fa27ce4SDimitry Andric
emitCheck(MachineBasicBlock & MBB,MachineBasicBlock::instr_iterator MBBI) const647fa27ce4SDimitry Andric bool KCFI::emitCheck(MachineBasicBlock &MBB,
657fa27ce4SDimitry Andric MachineBasicBlock::instr_iterator MBBI) const {
667fa27ce4SDimitry Andric assert(TII && "Target instruction info was not initialized");
677fa27ce4SDimitry Andric assert(TLI && "Target lowering was not initialized");
687fa27ce4SDimitry Andric
697fa27ce4SDimitry Andric // If the call instruction is bundled, we can only emit a check safely if
707fa27ce4SDimitry Andric // it's the first instruction in the bundle.
717fa27ce4SDimitry Andric if (MBBI->isBundled() && !std::prev(MBBI)->isBundle())
727fa27ce4SDimitry Andric report_fatal_error("Cannot emit a KCFI check for a bundled call");
737fa27ce4SDimitry Andric
747fa27ce4SDimitry Andric // Emit a KCFI check for the call instruction at MBBI. The implementation
757fa27ce4SDimitry Andric // must unfold memory operands if applicable.
767fa27ce4SDimitry Andric MachineInstr *Check = TLI->EmitKCFICheck(MBB, MBBI, TII);
777fa27ce4SDimitry Andric
787fa27ce4SDimitry Andric // Clear the original call's CFI type.
797fa27ce4SDimitry Andric assert(MBBI->isCall() && "Unexpected instruction type");
807fa27ce4SDimitry Andric MBBI->setCFIType(*MBB.getParent(), 0);
817fa27ce4SDimitry Andric
827fa27ce4SDimitry Andric // If not already bundled, bundle the check and the call to prevent
837fa27ce4SDimitry Andric // further changes.
847fa27ce4SDimitry Andric if (!MBBI->isBundled())
857fa27ce4SDimitry Andric finalizeBundle(MBB, Check->getIterator(), std::next(MBBI->getIterator()));
867fa27ce4SDimitry Andric
877fa27ce4SDimitry Andric ++NumKCFIChecksAdded;
887fa27ce4SDimitry Andric return true;
897fa27ce4SDimitry Andric }
907fa27ce4SDimitry Andric
runOnMachineFunction(MachineFunction & MF)917fa27ce4SDimitry Andric bool KCFI::runOnMachineFunction(MachineFunction &MF) {
92ac9a064cSDimitry Andric const Module *M = MF.getFunction().getParent();
937fa27ce4SDimitry Andric if (!M->getModuleFlag("kcfi"))
947fa27ce4SDimitry Andric return false;
957fa27ce4SDimitry Andric
967fa27ce4SDimitry Andric const auto &SubTarget = MF.getSubtarget();
977fa27ce4SDimitry Andric TII = SubTarget.getInstrInfo();
987fa27ce4SDimitry Andric TLI = SubTarget.getTargetLowering();
997fa27ce4SDimitry Andric
1007fa27ce4SDimitry Andric bool Changed = false;
1017fa27ce4SDimitry Andric for (MachineBasicBlock &MBB : MF) {
1027fa27ce4SDimitry Andric // Use instr_iterator because we don't want to skip bundles.
1037fa27ce4SDimitry Andric for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
1047fa27ce4SDimitry Andric MIE = MBB.instr_end();
1057fa27ce4SDimitry Andric MII != MIE; ++MII) {
1067fa27ce4SDimitry Andric if (MII->isCall() && MII->getCFIType())
1077fa27ce4SDimitry Andric Changed |= emitCheck(MBB, MII);
1087fa27ce4SDimitry Andric }
1097fa27ce4SDimitry Andric }
1107fa27ce4SDimitry Andric
1117fa27ce4SDimitry Andric return Changed;
1127fa27ce4SDimitry Andric }
113