Skip to content

Commit cff5bef

Browse files
committedAug 24, 2022
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 67504c9 with a fix for 32-bit builds. Reviewed By: nickdesaulniers, kees, joaomoreira, MaskRay Differential Revision: https://reviews.llvm.org/D119296
1 parent 03798f2 commit cff5bef

File tree

81 files changed

+1596
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1596
-49
lines changed
 

‎clang/docs/ControlFlowIntegrity.rst

+13
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ the identity of function pointers is maintained, and calls across shared
306306
library boundaries are no different from calls within a single program or
307307
shared library.
308308

309+
.. _kcfi:
310+
311+
``-fsanitize=kcfi``
312+
-------------------
313+
314+
This is an alternative indirect call control-flow integrity scheme designed
315+
for low-level system software, such as operating system kernels. Unlike
316+
``-fsanitize=cfi-icall``, it doesn't require ``-flto``, won't result in
317+
function pointers being replaced with jump table references, and never breaks
318+
cross-DSO function address equality. These properties make KCFI easier to
319+
adopt in low-level software. KCFI is limited to checking only function
320+
pointers, and isn't compatible with executable-only memory.
321+
309322
Member Function Pointer Call Checking
310323
=====================================
311324

‎clang/docs/UsersManual.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,8 @@ are listed below.
17201720
flow analysis.
17211721
- ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
17221722
checks. Requires ``-flto``.
1723+
- ``-fsanitize=kcfi``: kernel indirect call forward-edge control flow
1724+
integrity.
17231725
- ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
17241726
protection against stack-based memory corruption errors.
17251727

0 commit comments

Comments
 (0)