Protection against stack-based memory corruption errors using SafeStack
authorPeter Collingbourne <peter@pcc.me.uk>
Mon, 15 Jun 2015 21:07:11 +0000 (21:07 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Mon, 15 Jun 2015 21:07:11 +0000 (21:07 +0000)
commit7ffec838a2b72e6841d9fb993b5fe6a45f3b2a90
treeff31aa2ba509250a1c2cc0092601081159515bb3
parent3aef7761ac4b987c8e23ff62e79542057706091d
Protection against stack-based memory corruption errors using SafeStack

This patch adds the safe stack instrumentation pass to LLVM, which separates
the program stack into a safe stack, which stores return addresses, register
spills, and local variables that are statically verified to be accessed
in a safe way, and the unsafe stack, which stores everything else. Such
separation makes it much harder for an attacker to corrupt objects on the
safe stack, including function pointers stored in spilled registers and
return addresses. You can find more information about the safe stack, as
well as other parts of or control-flow hijack protection technique in our
OSDI paper on code-pointer integrity (http://dslab.epfl.ch/pubs/cpi.pdf)
and our project website (http://levee.epfl.ch).

The overhead of our implementation of the safe stack is very close to zero
(0.01% on the Phoronix benchmarks). This is lower than the overhead of
stack cookies, which are supported by LLVM and are commonly used today,
yet the security guarantees of the safe stack are strictly stronger than
stack cookies. In some cases, the safe stack improves performance due to
better cache locality.

Our current implementation of the safe stack is stable and robust, we
used it to recompile multiple projects on Linux including Chromium, and
we also recompiled the entire FreeBSD user-space system and more than 100
packages. We ran unit tests on the FreeBSD system and many of the packages
and observed no errors caused by the safe stack. The safe stack is also fully
binary compatible with non-instrumented code and can be applied to parts of
a program selectively.

This patch is our implementation of the safe stack on top of LLVM. The
patches make the following changes:

- Add the safestack function attribute, similar to the ssp, sspstrong and
  sspreq attributes.

- Add the SafeStack instrumentation pass that applies the safe stack to all
  functions that have the safestack attribute. This pass moves all unsafe local
  variables to the unsafe stack with a separate stack pointer, whereas all
  safe variables remain on the regular stack that is managed by LLVM as usual.

- Invoke the pass as the last stage before code generation (at the same time
  the existing cookie-based stack protector pass is invoked).

- Add unit tests for the safe stack.

Original patch by Volodymyr Kuznetsov and others at the Dependable Systems
Lab at EPFL; updates and upstreaming by myself.

Differential Revision: http://reviews.llvm.org/D6094

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239761 91177308-0d34-0410-b5e6-96231b3b80d8
48 files changed:
docs/LangRef.rst
include/llvm/Bitcode/LLVMBitCodes.h
include/llvm/IR/Attributes.h
include/llvm/InitializePasses.h
include/llvm/LinkAllPasses.h
include/llvm/Transforms/Instrumentation.h
lib/AsmParser/LLLexer.cpp
lib/AsmParser/LLParser.cpp
lib/AsmParser/LLToken.h
lib/Bitcode/Reader/BitcodeReader.cpp
lib/Bitcode/Writer/BitcodeWriter.cpp
lib/CodeGen/LLVMBuild.txt
lib/CodeGen/Passes.cpp
lib/IR/Attributes.cpp
lib/IR/Verifier.cpp
lib/Target/CppBackend/CPPBackend.cpp
lib/Transforms/IPO/Inliner.cpp
lib/Transforms/Instrumentation/CMakeLists.txt
lib/Transforms/Instrumentation/Instrumentation.cpp
lib/Transforms/Instrumentation/SafeStack.cpp [new file with mode: 0644]
test/Transforms/SafeStack/addr-taken.ll [new file with mode: 0644]
test/Transforms/SafeStack/array-aligned.ll [new file with mode: 0644]
test/Transforms/SafeStack/array.ll [new file with mode: 0644]
test/Transforms/SafeStack/call.ll [new file with mode: 0644]
test/Transforms/SafeStack/cast.ll [new file with mode: 0644]
test/Transforms/SafeStack/constant-gep-call.ll [new file with mode: 0644]
test/Transforms/SafeStack/constant-gep.ll [new file with mode: 0644]
test/Transforms/SafeStack/constant-geps.ll [new file with mode: 0644]
test/Transforms/SafeStack/dynamic-alloca.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-addr-pointer.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-bitcast-store.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-bitcast-store2.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-call.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-casted-pointer.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-gep-call.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-gep-invoke.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-gep-negative.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-gep-ptrtoint.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-gep-store.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-phi-call.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-select-call.ll [new file with mode: 0644]
test/Transforms/SafeStack/escape-vector.ll [new file with mode: 0644]
test/Transforms/SafeStack/invoke.ll [new file with mode: 0644]
test/Transforms/SafeStack/no-attr.ll [new file with mode: 0644]
test/Transforms/SafeStack/phi-cycle.ll [new file with mode: 0644]
test/Transforms/SafeStack/setjmp.ll [new file with mode: 0644]
test/Transforms/SafeStack/setjmp2.ll [new file with mode: 0644]
test/Transforms/SafeStack/struct.ll [new file with mode: 0644]