qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Daniele Buono <dbuono@linux.vnet.ibm.com>
Subject: [PULL 20/55] docs: Add CFI Documentation
Date: Mon, 21 Dec 2020 15:44:12 +0100	[thread overview]
Message-ID: <20201221144447.26161-21-pbonzini@redhat.com> (raw)
In-Reply-To: <20201221144447.26161-1-pbonzini@redhat.com>

From: Daniele Buono <dbuono@linux.vnet.ibm.com>

Document how to compile with CFI and how to maintain CFI-safe code

Signed-off-by: Daniele Buono <dbuono@linux.vnet.ibm.com>
Message-Id: <20201204230615.2392-6-dbuono@linux.vnet.ibm.com>
[Make build system section in index.rst and add the new file. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 docs/devel/control-flow-integrity.rst | 137 ++++++++++++++++++++++++++
 docs/devel/index.rst                  |   5 +-
 2 files changed, 140 insertions(+), 2 deletions(-)
 create mode 100644 docs/devel/control-flow-integrity.rst

diff --git a/docs/devel/control-flow-integrity.rst b/docs/devel/control-flow-integrity.rst
new file mode 100644
index 0000000000..d89d70733d
--- /dev/null
+++ b/docs/devel/control-flow-integrity.rst
@@ -0,0 +1,137 @@
+============================
+Control-Flow Integrity (CFI)
+============================
+
+This document describes the current control-flow integrity (CFI) mechanism in
+QEMU. How it can be enabled, its benefits and deficiencies, and how it affects
+new and existing code in QEMU
+
+Basics
+------
+
+CFI is a hardening technique that focusing on guaranteeing that indirect
+function calls have not been altered by an attacker.
+The type used in QEMU is a forward-edge control-flow integrity that ensures
+function calls performed through function pointers, always call a "compatible"
+function. A compatible function is a function with the same signature of the
+function pointer declared in the source code.
+
+This type of CFI is entirely compiler-based and relies on the compiler knowing
+the signature of every function and every function pointer used in the code.
+As of now, the only compiler that provides support for CFI is Clang.
+
+CFI is best used on production binaries, to protect against unknown attack
+vectors.
+
+In case of a CFI violation (i.e. call to a non-compatible function) QEMU will
+terminate abruptly, to stop the possible attack.
+
+Building with CFI
+-----------------
+
+NOTE: CFI requires the use of link-time optimization. Therefore, when CFI is
+selected, LTO will be automatically enabled.
+
+To build with CFI, the minimum requirement is Clang 6+. If you
+are planning to also enable fuzzing, then Clang 11+ is needed (more on this
+later).
+
+Given the use of LTO, a version of AR that supports LLVM IR is required.
+The easies way of doing this is by selecting the AR provided by LLVM::
+
+ AR=llvm-ar-9 CC=clang-9 CXX=lang++-9 /path/to/configure --enable-cfi
+
+CFI is enabled on every binary produced.
+
+If desired, an additional flag to increase the verbosity of the output in case
+of a CFI violation is offered (``--enable-debug-cfi``).
+
+Using QEMU built with CFI
+-------------------------
+
+A binary with CFI will work exactly like a standard binary. In case of a CFI
+violation, the binary will terminate with an illegal instruction signal.
+
+Incompatible code with CFI
+--------------------------
+
+As mentioned above, CFI is entirely compiler-based and therefore relies on
+compile-time knowledge of the code. This means that, while generally supported
+for most code, some specific use pattern can break CFI compatibility, and
+create false-positives. The two main patterns that can cause issues are:
+
+* Just-in-time compiled code: since such code is created at runtime, the jump
+  to the buffer containing JIT code will fail.
+
+* Libraries loaded dynamically, e.g. with dlopen/dlsym, since the library was
+  not known at compile time.
+
+Current areas of QEMU that are not entirely compatible with CFI are:
+
+1. TCG, since the idea of TCG is to pre-compile groups of instructions at
+   runtime to speed-up interpretation, quite similarly to a JIT compiler
+
+2. TCI, where the interpreter has to interpret the generic *call* operation
+
+3. Plugins, since a plugin is implemented as an external library
+
+4. Modules, since they are implemented as an external library
+
+5. Directly calling signal handlers from the QEMU source code, since the
+   signal handler may have been provided by an external library or even plugged
+   at runtime.
+
+Disabling CFI for a specific function
+-------------------------------------
+
+If you are working on function that is performing a call using an
+incompatible way, as described before, you can selectively disable CFI checks
+for such function by using the decorator ``QEMU_DISABLE_CFI`` at function
+definition, and add an explanation on why the function is not compatible
+with CFI. An example of the use of ``QEMU_DISABLE_CFI`` is provided here::
+
+	/*
+	 * Disable CFI checks.
+	 * TCG creates binary blobs at runtime, with the transformed code.
+	 * A TB is a blob of binary code, created at runtime and called with an
+	 * indirect function call. Since such function did not exist at compile time,
+	 * the CFI runtime has no way to verify its signature and would fail.
+	 * TCG is not considered a security-sensitive part of QEMU so this does not
+	 * affect the impact of CFI in environment with high security requirements
+	 */
+	QEMU_DISABLE_CFI
+	static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb)
+
+NOTE: CFI needs to be disabled at the **caller** function, (i.e. a compatible
+cfi function that calls a non-compatible one), since the check is performed
+when the function call is performed.
+
+CFI and fuzzing
+---------------
+
+There is generally no advantage of using CFI and fuzzing together, because
+they target different environments (production for CFI, debug for fuzzing).
+
+CFI could be used in conjunction with fuzzing to identify a broader set of
+bugs that may not end immediately in a segmentation fault or triggering
+an assertion. However, other sanitizers such as address and ub sanitizers
+can identify such bugs in a more precise way than CFI.
+
+There is, however, an interesting use case in using CFI in conjunction with
+fuzzing, that is to make sure that CFI is not triggering any false positive
+in remote-but-possible parts of the code.
+
+CFI can be enabled with fuzzing, but with some caveats:
+1. Fuzzing relies on the linker performing function wrapping at link-time.
+The standard BFD linker does not support function wrapping when LTO is
+also enabled. The workaround is to use LLVM's lld linker.
+2. Fuzzing also relies on a custom linker script, which is only supported by
+lld with version 11+.
+
+In other words, to compile with fuzzing and CFI, clang 11+ is required, and
+lld needs to be used as a linker::
+
+ AR=llvm-ar-11 CC=clang-11 CXX=lang++-11 /path/to/configure --enable-cfi \
+                           -enable-fuzzing --extra-ldflags="-fuse-ld=lld"
+
+and then, compile the fuzzers as usual.
diff --git a/docs/devel/index.rst b/docs/devel/index.rst
index f10ed77e4c..ea0e1e17ae 100644
--- a/docs/devel/index.rst
+++ b/docs/devel/index.rst
@@ -15,14 +15,15 @@ Contents:
 
    build-system
    kconfig
+   testing
+   fuzzing
+   control-flow-integrity
    loads-stores
    memory
    migration
    atomics
    stable-process
-   testing
    qtest
-   fuzzing
    decodetree
    secure-coding-practices
    tcg
-- 
2.29.2




  parent reply	other threads:[~2020-12-21 14:54 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-21 14:43 [PULL 00/55] Misc patches for 2020-12-21 Paolo Bonzini
2020-12-21 14:43 ` [PULL 01/55] gitlab: include aarch64-softmmu and ppc64-softmmu cross-system-build Paolo Bonzini
2020-12-21 14:43 ` [PULL 02/55] configure: move gettext detection to meson.build Paolo Bonzini
2020-12-21 14:43 ` [PULL 03/55] configure: add --without-default-features Paolo Bonzini
2020-12-21 14:43 ` [PULL 04/55] python: add __repr__ to ConsoleSocket to aid debugging Paolo Bonzini
2020-12-21 14:43 ` [PULL 05/55] gitlab: move --without-default-devices build from Travis Paolo Bonzini
2020-12-21 14:43 ` [PULL 06/55] gitlab: add --without-default-features build Paolo Bonzini
2020-12-21 14:43 ` [PULL 07/55] tests/tcg: build tests with -Werror Paolo Bonzini
2020-12-21 14:44 ` [PULL 08/55] gitlab-CI: Test 32-bit builds with the fedora-i386-cross container Paolo Bonzini
2020-12-21 14:44 ` [PULL 09/55] tests/docker: Remove the remainders of debian9 containers from the Makefile Paolo Bonzini
2020-12-21 14:44 ` [PULL 10/55] tests: update for rename of CentOS8 PowerTools repo Paolo Bonzini
2020-12-21 14:44 ` [PULL 11/55] configure: document --without-default-{features, devices} Paolo Bonzini
2020-12-21 14:44 ` [PULL 12/55] vl: initialize displays _after_ exiting preconfiguration Paolo Bonzini
2020-12-21 14:44 ` [PULL 13/55] test-char: Destroy chardev correctly at char_file_test_internal() Paolo Bonzini
2020-12-21 14:44 ` [PULL 14/55] qom: Assert that objects being destroyed have no parent Paolo Bonzini
2020-12-21 14:44 ` [PULL 15/55] target/i386: Check privilege level for protected mode 'int N' task gate Paolo Bonzini
2020-12-21 14:44 ` [PULL 16/55] configure,meson: add option to enable LTO Paolo Bonzini
2020-12-21 14:44 ` [PULL 17/55] cfi: Initial support for cfi-icall in QEMU Paolo Bonzini
2020-12-21 14:44 ` [PULL 18/55] check-block: enable iotests with cfi-icall Paolo Bonzini
2020-12-21 14:44 ` [PULL 19/55] configure,meson: support Control-Flow Integrity Paolo Bonzini
2020-12-21 14:44 ` Paolo Bonzini [this message]
2020-12-21 14:44 ` [PULL 21/55] build-sys: fix -static linking of libvhost-user Paolo Bonzini
2020-12-21 14:44 ` [PULL 22/55] remove TCG includes from common code Paolo Bonzini
2020-12-21 14:44 ` [PULL 23/55] util: Extract flush_icache_range to cacheflush.c Paolo Bonzini
2020-12-21 14:44 ` [PULL 24/55] trace: do not include TCG helper tracepoints in no-TCG builds Paolo Bonzini
2020-12-21 14:44 ` [PULL 25/55] Makefile: add dummy target for build.ninja dependencies Paolo Bonzini
2020-12-21 14:44 ` [PULL 26/55] meson: update submodule to 0.56.0 Paolo Bonzini
2020-12-21 14:44 ` [PULL 27/55] meson: switch minimum meson version " Paolo Bonzini
2020-12-21 14:44 ` [PULL 28/55] meson: fix detection of curses with pkgconfig Paolo Bonzini
2020-12-21 14:44 ` [PULL 29/55] meson: use pkg-config method for libudev Paolo Bonzini
2020-12-21 14:44 ` [PULL 30/55] meson: use dependency to gate block modules Paolo Bonzini
2020-12-21 14:44 ` [PULL 31/55] meson: cleanup Kconfig.host handling Paolo Bonzini
2020-12-21 14:44 ` [PULL 32/55] configure: remove useless code to check for Xen PCI passthrough Paolo Bonzini
2020-12-21 14:44 ` [PULL 33/55] configure: remove variable bogus_os Paolo Bonzini
2020-12-21 14:44 ` [PULL 34/55] configure: accept --enable-slirp Paolo Bonzini
2020-12-21 14:44 ` [PULL 35/55] configure: remove CONFIG_FILEVERSION and CONFIG_PRODUCTVERSION Paolo Bonzini
2020-12-21 14:44 ` [PULL 36/55] brlapi: convert to meson Paolo Bonzini
2020-12-21 14:44 ` [PULL 37/55] curl: remove compatibility code, require 7.29.0 Paolo Bonzini
2020-12-21 14:44 ` [PULL 38/55] curl: convert to meson Paolo Bonzini
2020-12-21 14:44 ` [PULL 39/55] glusterfs: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 40/55] bzip2: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 41/55] libiscsi: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 42/55] libnfs: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 43/55] libssh: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 44/55] rbd: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 45/55] lzo: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 46/55] snappy: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 47/55] lzfse: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 48/55] zstd: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 49/55] seccomp: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 50/55] virtfs: " Paolo Bonzini
2021-01-07 13:58   ` Bruce Rogers
2021-01-07 14:49     ` Paolo Bonzini
2021-01-07 15:31       ` Bruce Rogers
2020-12-21 14:44 ` [PULL 51/55] cap_ng: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 52/55] libattr: " Paolo Bonzini
2020-12-21 14:44 ` [PULL 53/55] meson.build: convert --with-default-devices " Paolo Bonzini
2020-12-21 14:44 ` [PULL 54/55] configure: move tests/qemu-iotests/common.env generation " Paolo Bonzini
2020-12-21 14:44 ` [PULL 55/55] win32: drop fd registration to the main-loop on setting non-block Paolo Bonzini
2021-01-01 17:05 ` [PULL 00/55] Misc patches for 2020-12-21 Peter Maydell
2021-01-01 19:59   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201221144447.26161-21-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=dbuono@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).