mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Charlotte Delenk <darkkirb@darkkirb.de>
To: musl@lists.openwall.com
Subject: [musl] [PATCH 2/2] Add support for LLVM's Control Flow Integrity
Date: Tue, 29 Dec 2020 12:59:12 +0100	[thread overview]
Message-ID: <b16030c7-9ae3-8847-b75c-007e7519ece6@darkkirb.de> (raw)
In-Reply-To: <104266ac-6da3-0fac-2394-54dca6675270@darkkirb.de>

Control Flow Integrity is a sanitization option found in clang which
attempts to prevent exploits and bugs that divert the control flow to an
unintended path. For more information about it, refer to clang's
documentation[1].

While there are many different schemes currently implemented, the only
one that is enabled for C code is the cfi-icall scheme, which attempts
to prevent indirect calls to function with the wrong type. In most of
musl's code this works without issues, however there are a few cases
where it does not work, or at least won't work without breaking a
considerable amount of applications.

This patch works by disabling CFI sanitization for these files:

ldso/dlstart.c
ldso/dynlink.c
src/env/__libc_start_main.c
src/exit/exit.c

These contain indirect function calls where the compiler is either
unable to find out the type of the function or where the actual function
type can be one of multiple equally valid ones.

I have checked all of the places with indirect function calls using the
output of Fangrui's clang tidy patch and only found the aforementioned
functions.

How to test: In addition to the -fsanitize=cfi flag, you also need to
pass
-flto=thin and -fvisibility=default (or hidden in a static build). The
application has to be compiled and linked with the same flags as well.
You might need to set the environment variables AR=llvm-ar and RANLIB=
llvm-ranlib for musl or the software you are compiling.

[1]: https://clang.llvm.org/docs/ControlFlowIntegrity.html
Special thanks to Fangrui Song <i@maskray.me>

This patch depends on the previous patch labelled "Fix LTO shared 
library build on GCC and Clang"

---
  Makefile | 8 ++++++++
  1 file changed, 8 insertions(+)

diff --git a/Makefile b/Makefile
index 15190fb9..9d937b21 100644
--- a/Makefile
+++ b/Makefile
@@ -134,6 +134,14 @@ $(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
  # Work around LTO compiler bugs
  lib/libc.so: CFLAGS_ALL += -u_dlstart_c -u__dls2 -u__dls2b -u__dls3 
-u__stack_chk_guard -u_start_c

+# Disable CFI for problematic source files
+ifneq (,$(findstring cfi,$(filter -fsanitize=%,$(CFLAGS))))
+obj/ldso/dlstart.lo: CFLAGS_ALL += -fno-sanitize=cfi
+obj/ldso/dynlink.lo: CFLAGS_ALL += -fno-sanitize=cfi
+obj/src/env/__libc_start_main.lo: CFLAGS_ALL += -fno-sanitize=cfi
+obj/src/exit/exit.lo: CFLAGS_ALL += -fno-sanitize=cfi
+endif
+
  CC_CMD = $(CC) $(CFLAGS_ALL) -c -o $@ $<

  # Choose invocation of assembler to be used
-- 
2.29.2



      parent reply	other threads:[~2020-12-29 11:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-27 17:53 [musl] [PATCH] " Charlotte Delenk
2020-12-27 23:05 ` Fangrui Song
2020-12-28  0:56   ` Fangrui Song
2020-12-28  9:20     ` Charlotte Delenk
2020-12-28 13:17       ` [musl] [PATCH] Add support for LLVM's Control Flow Integrity (V2) Charlotte Delenk
2020-12-28 17:01         ` Shiz
2020-12-29  1:26           ` Rich Felker
2020-12-29 10:20           ` Charlotte Delenk
2020-12-29 11:56             ` [musl] [PATCH 1/2] Fix LTO shared library build on GCC and Clang Charlotte Delenk
2020-12-29 11:59             ` Charlotte Delenk [this message]

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=b16030c7-9ae3-8847-b75c-007e7519ece6@darkkirb.de \
    --to=darkkirb@darkkirb.de \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).