[all-commits] [llvm/llvm-project] cff5be: KCFI sanitizer

Sami Tolvanen via All-commits all-commits at lists.llvm.org
Wed Aug 24 15:46:37 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: cff5bef948c91e4919de8a5fb9765e0edc13f3de
      https://github.com/llvm/llvm-project/commit/cff5bef948c91e4919de8a5fb9765e0edc13f3de
  Author: Sami Tolvanen <samitolvanen at google.com>
  Date:   2022-08-24 (Wed, 24 Aug 2022)

  Changed paths:
    M clang/docs/ControlFlowIntegrity.rst
    M clang/docs/UsersManual.rst
    M clang/include/clang/Basic/Features.def
    M clang/include/clang/Basic/Sanitizers.def
    M clang/lib/CodeGen/CGCall.cpp
    M clang/lib/CodeGen/CodeGenFunction.cpp
    M clang/lib/CodeGen/CodeGenFunction.h
    M clang/lib/CodeGen/CodeGenModule.cpp
    M clang/lib/CodeGen/CodeGenModule.h
    M clang/lib/Driver/SanitizerArgs.cpp
    M clang/lib/Driver/ToolChain.cpp
    A clang/test/CodeGen/kcfi.c
    M clang/test/Driver/fsanitize.c
    M llvm/docs/LangRef.rst
    M llvm/include/llvm/CodeGen/AsmPrinter.h
    M llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h
    M llvm/include/llvm/CodeGen/MachineFunction.h
    M llvm/include/llvm/CodeGen/MachineInstr.h
    M llvm/include/llvm/CodeGen/SelectionDAGNodes.h
    M llvm/include/llvm/CodeGen/TargetLowering.h
    M llvm/include/llvm/IR/FixedMetadataKinds.def
    M llvm/include/llvm/IR/InstrTypes.h
    M llvm/include/llvm/IR/LLVMContext.h
    M llvm/include/llvm/MC/MCObjectFileInfo.h
    M llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    M llvm/lib/CodeGen/GlobalISel/CallLowering.cpp
    M llvm/lib/CodeGen/MIRParser/MILexer.cpp
    M llvm/lib/CodeGen/MIRParser/MILexer.h
    M llvm/lib/CodeGen/MIRParser/MIParser.cpp
    M llvm/lib/CodeGen/MIRPrinter.cpp
    M llvm/lib/CodeGen/MachineFunction.cpp
    M llvm/lib/CodeGen/MachineInstr.cpp
    M llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
    M llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
    M llvm/lib/IR/Instructions.cpp
    M llvm/lib/IR/LLVMContext.cpp
    M llvm/lib/IR/Verifier.cpp
    M llvm/lib/MC/MCObjectFileInfo.cpp
    M llvm/lib/Target/AArch64/AArch64.h
    M llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
    M llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
    M llvm/lib/Target/AArch64/AArch64FastISel.cpp
    M llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
    M llvm/lib/Target/AArch64/AArch64ISelLowering.h
    M llvm/lib/Target/AArch64/AArch64InstrInfo.td
    A llvm/lib/Target/AArch64/AArch64KCFI.cpp
    M llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
    M llvm/lib/Target/AArch64/CMakeLists.txt
    M llvm/lib/Target/AArch64/GISel/AArch64CallLowering.cpp
    M llvm/lib/Target/X86/CMakeLists.txt
    M llvm/lib/Target/X86/X86.h
    M llvm/lib/Target/X86/X86AsmPrinter.cpp
    M llvm/lib/Target/X86/X86AsmPrinter.h
    M llvm/lib/Target/X86/X86ExpandPseudo.cpp
    M llvm/lib/Target/X86/X86FastISel.cpp
    M llvm/lib/Target/X86/X86ISelLowering.cpp
    M llvm/lib/Target/X86/X86ISelLowering.h
    M llvm/lib/Target/X86/X86InstrCompiler.td
    A llvm/lib/Target/X86/X86KCFI.cpp
    M llvm/lib/Target/X86/X86MCInstLower.cpp
    M llvm/lib/Target/X86/X86TargetMachine.cpp
    M llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    M llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
    M llvm/lib/Transforms/Utils/InlineFunction.cpp
    M llvm/test/Bitcode/operand-bundles-bc-analyzer.ll
    M llvm/test/CodeGen/AArch64/O0-pipeline.ll
    M llvm/test/CodeGen/AArch64/O3-pipeline.ll
    A llvm/test/CodeGen/AArch64/kcfi-bti.ll
    A llvm/test/CodeGen/AArch64/kcfi-patchable-function-prefix.ll
    A llvm/test/CodeGen/AArch64/kcfi.ll
    A llvm/test/CodeGen/MIR/X86/instr-cfi-type.mir
    M llvm/test/CodeGen/X86/O0-pipeline.ll
    A llvm/test/CodeGen/X86/kcfi-patchable-function-prefix.ll
    A llvm/test/CodeGen/X86/kcfi.ll
    M llvm/test/CodeGen/X86/opt-pipeline.ll
    A llvm/test/Transforms/InstCombine/kcfi-operand-bundles.ll
    A llvm/test/Transforms/TailCallElim/kcfi-bundle.ll
    A llvm/test/Verifier/kcfi-operand-bundles.ll
    A llvm/test/Verifier/metadata-function-kcfi-type.ll
    M llvm/utils/gn/secondary/llvm/lib/Target/AArch64/BUILD.gn
    M llvm/utils/gn/secondary/llvm/lib/Target/X86/BUILD.gn

  Log Message:
  -----------
  KCFI sanitizer

The KCFI sanitizer, enabled with `-fsanitize=kcfi`, implements a
forward-edge control flow integrity scheme for indirect calls. It
uses a !kcfi_type metadata node to attach a type identifier for each
function and injects verification code before indirect calls.

Unlike the current CFI schemes implemented in LLVM, KCFI does not
require LTO, does not alter function references to point to a jump
table, and never breaks function address equality. KCFI is intended
to be used in low-level code, such as operating system kernels,
where the existing schemes can cause undue complications because
of the aforementioned properties. However, unlike the existing
schemes, KCFI is limited to validating only function pointers and is
not compatible with executable-only memory.

KCFI does not provide runtime support, but always traps when a
type mismatch is encountered. Users of the scheme are expected
to handle the trap. With `-fsanitize=kcfi`, Clang emits a `kcfi`
operand bundle to indirect calls, and LLVM lowers this to a
known architecture-specific sequence of instructions for each
callsite to make runtime patching easier for users who require this
functionality.

A KCFI type identifier is a 32-bit constant produced by taking the
lower half of xxHash64 from a C++ mangled typename. If a program
contains indirect calls to assembly functions, they must be
manually annotated with the expected type identifiers to prevent
errors. To make this easier, Clang generates a weak SHN_ABS
`__kcfi_typeid_<function>` symbol for each address-taken function
declaration, which can be used to annotate functions in assembly
as long as at least one C translation unit linked into the program
takes the function address. For example on AArch64, we might have
the following code:

```
.c:
  int f(void);
  int (*p)(void) = f;
  p();

.s:
  .4byte __kcfi_typeid_f
  .global f
  f:
    ...
```

Note that X86 uses a different preamble format for compatibility
with Linux kernel tooling. See the comments in
`X86AsmPrinter::emitKCFITypeId` for details.

As users of KCFI may need to locate trap locations for binary
validation and error handling, LLVM can additionally emit the
locations of traps to a `.kcfi_traps` section.

Similarly to other sanitizers, KCFI checking can be disabled for a
function with a `no_sanitize("kcfi")` function attribute.

Relands 67504c95494ff05be2a613129110c9bcf17f6c13 with a fix for
32-bit builds.

Reviewed By: nickdesaulniers, kees, joaomoreira, MaskRay

Differential Revision: https://reviews.llvm.org/D119296




More information about the All-commits mailing list