Index: tools/llvm-cfi-verify/FileVerifier.h =================================================================== --- tools/llvm-cfi-verify/FileVerifier.h +++ tools/llvm-cfi-verify/FileVerifier.h @@ -67,6 +67,12 @@ Error printIndirectCFInstructions() const; + // Check whether the provided instruction is CFI protected in this file. + // Returns false if this instruction doesn't exist in this file, if it's not + // an indirect control flow instruction, or isn't CFI protected. Returns true + // otherwise. + bool isCFIProtected(uint64_t Address) const; + // Returns the instruction at the provided address. Returns nullptr if there // is no instruction at the provided address. const Instr *getInstruction(uint64_t Address) const; Index: tools/llvm-cfi-verify/FileVerifier.cpp =================================================================== --- tools/llvm-cfi-verify/FileVerifier.cpp +++ tools/llvm-cfi-verify/FileVerifier.cpp @@ -1,4 +1,5 @@ #include "FileVerifier.h" +#include "GraphBuilder.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/MC/MCAsmInfo.h" @@ -82,11 +83,44 @@ << "] = " << MII->getName(InstrMeta.Instruction.getOpcode()) << " "; InstrMeta.Instruction.print(outs()); outs() << "\n"; + outs() << " Protected? " << isCFIProtected(Address) << "\n"; } return Error::success(); } +bool FileVerifier::isCFIProtected(uint64_t Address) const { + const Instr *InstrMetaPtr = getInstruction(Address); + if (!InstrMetaPtr) + return false; + + const auto &InstrDesc = MII->get(InstrMetaPtr->Instruction.getOpcode()); + + if (!InstrDesc.mayAffectControlFlow(InstrMetaPtr->Instruction, *RegisterInfo)) + return false; + + bool UsesRegisterOperand = false; + for (const auto &Operand : InstrMetaPtr->Instruction) + if (Operand.isReg()) + UsesRegisterOperand = true; + + if (!UsesRegisterOperand) + return false; + + auto Flows = GraphBuilder::buildFlowGraph(*this, Address); + + if (!Flows.OrphanedNodes.empty()) + return false; + + for (const auto &BranchNode : Flows.BranchNodes) { + assert(BranchNode && "Branch node contained nullptr."); + if (!BranchNode->CFIProtection) + return false; + } + + return true; +} + const Instr * FileVerifier::getPrevInstructionSequential(const Instr &InstrMeta) const { std::map::const_iterator KV = Index: tools/llvm-cfi-verify/unittests/FileVerifier.cpp =================================================================== --- tools/llvm-cfi-verify/unittests/FileVerifier.cpp +++ tools/llvm-cfi-verify/unittests/FileVerifier.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "../FileVerifier.h" +#include "../GraphBuilder.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -473,6 +474,157 @@ EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); } +TEST_F(BasicFileVerifierTest, CFIProtectionInvalidTargets) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0x0f, 0x0b, // 1: ud2 + 0x75, 0x00, // 3: jne 5 [+0] + }, + 0xDEADBEEF, "Test"); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF)); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 1)); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 3)); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADC0DE)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionBasicFallthroughToUd2) { + Verifier.parseSectionContents( + { + 0x75, 0x02, // 0: jne 4 [+2] + 0x0f, 0x0b, // 2: ud2 + 0xff, 0x10, // 4: callq *(%rax) + }, + 0xDEADBEEF, "Test"); + EXPECT_TRUE(Verifier.isCFIProtected(0xDEADBEEF + 4)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionBasicJumpToUd2) { + Verifier.parseSectionContents( + { + 0x75, 0x02, // 0: jne 4 [+2] + 0xff, 0x10, // 2: callq *(%rax) + 0x0f, 0x0b, // 4: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_TRUE(Verifier.isCFIProtected(0xDEADBEEF + 2)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionDualPathUd2) { + Verifier.parseSectionContents( + { + 0x75, 0x03, // 0: jne 5 [+3] + 0x90, // 2: nop + 0xff, 0x10, // 3: callq *(%rax) + 0x0f, 0x0b, // 5: ud2 + 0x75, 0xf9, // 7: jne 2 [-7] + 0x0f, 0x0b, // 9: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_TRUE(Verifier.isCFIProtected(0xDEADBEEF + 3)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionDualPathSingleUd2) { + Verifier.parseSectionContents( + { + 0x75, 0x05, // 0: jne 7 [+5] + 0x90, // 2: nop + 0xff, 0x10, // 3: callq *(%rax) + 0x75, 0xfb, // 5: jne 2 [-5] + 0x0f, 0x0b, // 7: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_TRUE(Verifier.isCFIProtected(0xDEADBEEF + 3)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionDualFailLimitUpwards) { + Verifier.parseSectionContents( + { + 0x75, 0x06, // 0: jne 8 [+6] + 0x90, // 2: nop + 0x90, // 3: nop + 0x90, // 4: nop + 0x90, // 5: nop + 0xff, 0x10, // 6: callq *(%rax) + 0x0f, 0x0b, // 8: ud2 + }, + 0xDEADBEEF, "Test"); + uint64_t PrevSearchLengthForConditionalBranch = + SearchLengthForConditionalBranch; + SearchLengthForConditionalBranch = 2; + + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 6)); + + SearchLengthForConditionalBranch = PrevSearchLengthForConditionalBranch; +} + +TEST_F(BasicFileVerifierTest, CFIProtectionDualFailLimitDownwards) { + Verifier.parseSectionContents( + { + 0x75, 0x02, // 0: jne 4 [+2] + 0xff, 0x10, // 2: callq *(%rax) + 0x90, // 4: nop + 0x90, // 5: nop + 0x90, // 6: nop + 0x90, // 7: nop + 0x0f, 0x0b, // 8: ud2 + }, + 0xDEADBEEF, "Test"); + uint64_t PrevSearchLengthForUndef = SearchLengthForUndef; + SearchLengthForUndef = 2; + + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 2)); + + SearchLengthForUndef = PrevSearchLengthForUndef; +} + +TEST_F(BasicFileVerifierTest, CFIProtectionGoodAndBadPaths) { + Verifier.parseSectionContents( + { + 0xeb, 0x02, // 0: jmp 4 [+2] + 0x75, 0x02, // 2: jne 6 [+2] + 0xff, 0x10, // 4: callq *(%rax) + 0x0f, 0x0b, // 6: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 4)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionWithUnconditionalJumpInFallthrough) { + Verifier.parseSectionContents( + { + 0x75, 0x04, // 0: jne 6 [+4] + 0xeb, 0x00, // 2: jmp 4 [+0] + 0xff, 0x10, // 4: callq *(%rax) + 0x0f, 0x0b, // 6: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_TRUE(Verifier.isCFIProtected(0xDEADBEEF + 4)); +} + +TEST_F(BasicFileVerifierTest, CFIProtectionComplexExample) { + // See unittests/GraphBuilder.cpp::BuildFlowGraphComplexExample for this + // graph. + Verifier.parseSectionContents( + { + 0x75, 0x12, // 0: jne 20 [+18] + 0xeb, 0x03, // 2: jmp 7 [+3] + 0x75, 0x10, // 4: jne 22 [+16] + 0x90, // 6: nop + 0x90, // 7: nop + 0x90, // 8: nop + 0xff, 0x10, // 9: callq *(%rax) + 0xeb, 0xfc, // 11: jmp 9 [-4] + 0x75, 0xfa, // 13: jne 9 [-6] + 0xe8, 0x78, 0x56, 0x34, 0x12, // 15: callq OUTOFBOUNDS [+0x12345678] + 0x90, // 20: nop + 0x90, // 21: nop + 0x0f, 0x0b, // 22: ud2 + }, + 0xDEADBEEF, "Test"); + EXPECT_FALSE(Verifier.isCFIProtected(0xDEADBEEF + 9)); +} + } // anonymous namespace } // end namespace cfi_verify } // end namespace llvm