From: "LeMay, Michael" <michael.lemay@intel.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: [RFC PATCH v3 2/2] add SafeStack build support
Date: Fri, 4 Nov 2016 20:34:39 +0000 [thread overview]
Message-ID: <390CE752059EB848A71F4F676EBAB76D3AC298A5@ORSMSX114.amr.corp.intel.com> (raw)
The SafeStack sanitizer in LLVM Clang seeks to mitigate stack memory
corruption vulnerabilities [1]. This patch enhances configure to
detect the compiler flag that enables SafeStack. It also enhances the
Makefile to specify the necessary compiler flags for specific files.
[1] http://clang.llvm.org/docs/SafeStack.html
Signed-off-by: Michael LeMay <michael.lemay@intel.com>
---
Makefile | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
configure | 10 ++++++++++
2 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 8246b78..1eb7dca 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,9 @@ CFLAGS_C99FSE = -std=c99 -ffreestanding -nostdinc
CFLAGS_ALL = $(CFLAGS_C99FSE)
CFLAGS_ALL += -D_XOPEN_SOURCE=700 -I$(srcdir)/arch/$(ARCH) -I$(srcdir)/arch/generic -Iobj/src/internal -I$(srcdir)/src/internal -Iobj/include -I$(srcdir)/include
-CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO) $(CFLAGS)
+CFLAGS_ALL += $(CPPFLAGS) $(CFLAGS_AUTO)
+# This flag is selectively re-added for certain files below.
+CFLAGS_ALL += $(filter-out -fsanitize=safe-stack,$(CFLAGS))
LDFLAGS_ALL = $(LDFLAGS_AUTO) $(LDFLAGS)
@@ -132,6 +134,53 @@ NOSSP_SRCS = $(wildcard crt/*.c) \
ldso/dlstart.c ldso/dynlink.c
$(NOSSP_SRCS:%.c=obj/%.o) $(NOSSP_SRCS:%.c=obj/%.lo): CFLAGS_ALL += $(CFLAGS_NOSSP)
+# The safestack attribute will be selectively forced within these files below.
+SAFE_STACK_OBJS = $(filter-out \
+ $(CRT_OBJS) \
+ obj/ldso/dlstart.o \
+ $(addprefix obj/src/env/__init_tls.,o lo) \
+ $(addprefix obj/src/env/__libc_start_main.,o lo) \
+ $(addprefix obj/src/internal/safe_stack.,o lo), \
+ $(ALL_OBJS))
+
+ifeq ($(SAFE_STACK),yes)
+
+CFLAGS_ALL += -DSAFE_STACK=1
+
+define FORCE_ATTR =
+ $(eval $(addprefix obj/$(1).,o lo): CFLAGS_ALL += $(foreach func,$(2),-mllvm -force-attribute=$(func):$(FORCED_ATTR)))
+endef
+
+FORCED_ATTR = noinline
+
+# The no_sanitize attribute will only take effect if there are no inlined
+# functions that lack the attribute. That is why noinline is applied to the
+# following functions that are called from functions with the no_sanitize
+# attribute.
+$(call FORCE_ATTR,ldso/dynlink, \
+ __pthread_self a_crash decode_dyn decode_vec find_sym \
+ kernel_mapped_dso load_deps load_preload make_global makefuncdescs \
+ map_library reclaim_gaps reloc_all search_vec update_tls_size)
+$(call FORCE_ATTR,src/env/__init_tls, a_crash)
+$(call FORCE_ATTR,src/env/__libc_start_main, a_crash)
+$(call FORCE_ATTR,src/internal/safe_stack, __pthread_self a_crash)
+
+FORCED_ATTR = safestack
+
+# Since __init_tp switches to a new thread control block, it is necessary to
+# avoid accessing the unsafe stack pointer from the time that switch occurs
+# until the unsafe_stack_ptr field in the new thread control block has been
+# initialized. That is why only the following function in __init_tls.c is
+# instrumented with SafeStack.
+$(call FORCE_ATTR,src/env/__init_tls, __copy_tls)
+$(call FORCE_ATTR,src/env/__libc_start_main, libc_start_init)
+$(call FORCE_ATTR,src/internal/safe_stack, \
+ __safestack_init_thread __safestack_pthread_exit)
+
+$(SAFE_STACK_OBJS) $(SAFE_STACK_OBJS:%.o=%.lo): CFLAGS_ALL += -fsanitize=safe-stack
+
+endif
+
$(CRT_OBJS): CFLAGS_ALL += -DCRT
$(LOBJS) $(LDSO_OBJS): CFLAGS_ALL += -fPIC
diff --git a/configure b/configure
index 707eb12..a70009f 100755
--- a/configure
+++ b/configure
@@ -141,6 +141,7 @@ static=yes
wrapper=auto
gcc_wrapper=no
clang_wrapper=no
+SAFE_STACK=no
for arg ; do
case "$arg" in
@@ -596,10 +597,18 @@ printf "using compiler runtime libraries: %s\n" "$LIBCC"
SUBARCH=
t="$CFLAGS_C99FSE $CPPFLAGS $CFLAGS"
+fnmatch '-fsanitize=safe-stack*|*\ -fsanitize=safe-stack*' "$CFLAGS" && SAFE_STACK=yes
+
if test "$ARCH" = "x86_64" ; then
trycppif __ILP32__ "$t" && ARCH=x32
fi
+if test "$ARCH" = "x32" -a "$SAFE_STACK" = "yes" ; then
+# x32 would change the offset of the unsafe stack pointer in the thread control
+# block.
+fail "$0: error: SafeStack unsupported for x32"
+fi
+
if test "$ARCH" = "arm" ; then
trycppif __ARMEB__ "$t" && SUBARCH=${SUBARCH}eb
trycppif __ARM_PCS_VFP "$t" && SUBARCH=${SUBARCH}hf
@@ -751,6 +760,7 @@ OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
ALL_TOOLS = $tools
TOOL_LIBS = $tool_libs
ADD_CFI = $ADD_CFI
+SAFE_STACK = $SAFE_STACK
EOF
test "x$static" = xno && echo "STATIC_LIBS ="
test "x$shared" = xno && echo "SHARED_LIBS ="
--
2.7.4
reply other threads:[~2016-11-04 20:34 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=390CE752059EB848A71F4F676EBAB76D3AC298A5@ORSMSX114.amr.corp.intel.com \
--to=michael.lemay@intel.com \
--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).