public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 06/15] Prevent the rtld mapfile computation from dragging in __stack_chk_fail*.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
  2016-12-19 11:15 ` [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector Nix
  2016-12-19 11:15 ` [PATCH 08/15] Add a hidden __stack_chk_fail_local alias to libc.so Nix
@ 2016-12-19 11:15 ` Nix
  2016-12-19 11:25 ` [PATCH 15/15] Enable -fstack-protector=* when requested by configure Nix
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:15 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

The previous commit prevented rtld itself from being built with
-fstack-protector, but this is not quite enough.  We identify which
objects belong in rtld via a test link and analysis of the resulting
mapfile.  That link is necessarily done against objects that are
stack-protected, so drags in __stack_chk_fail_local, __stack_chk_fail,
and all the libc and libio code they use.

To stop this happening, use --defsym in the test librtld.map-production
link to force the linker to predefine these two symbols (to 0, but it
could be to anything).  (In a real link, this would of course be
catastrophic, but these object files are never used for anything else.)

v2: New.
v6: Dummy out stack_chk_fail_local too.
v7: Fix word-wrapping.

	* elf/Makefile (dummy-stack-chk-fail): New.
	($(objpfx)librtld.map): Use it.
---
 elf/Makefile | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/elf/Makefile b/elf/Makefile
index d14d48d..daf0ebd 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -369,9 +369,22 @@ $(objpfx)dl-allobjs.os: $(all-rtld-routines:%=$(objpfx)%.os)
 # are compiled with special flags, and puts these modules into rtld-libc.a
 # for us.  Then we do the real link using rtld-libc.a instead of libc_pic.a.
 
+# If the compiler can do SSP, build the mapfile with dummy __stack_chk_fail
+# and __stack_chk_fail_local symbols defined, to prevent the real things
+# being dragged into rtld even though rtld is never built with stack-
+# protection.
+
+ifeq ($(have-ssp),yes)
+dummy-stack-chk-fail := -Wl,--defsym='__stack_chk_fail=0' \
+			-Wl,--defsym='__stack_chk_fail_local=0'
+else
+dummy-stack-chk-fail :=
+endif
+
 $(objpfx)librtld.map: $(objpfx)dl-allobjs.os $(common-objpfx)libc_pic.a
 	@-rm -f $@T
-	$(reloc-link) -o $@.o '-Wl,-(' $^ -lgcc '-Wl,-)' -Wl,-Map,$@T
+	$(reloc-link) -o $@.o $(dummy-stack-chk-fail) \
+		'-Wl,-(' $^ -lgcc '-Wl,-)' -Wl,-Map,$@T
 	rm -f $@.o
 	mv -f $@T $@
 
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* --enable-stack-protector for glibc, v10
@ 2016-12-19 11:15 Nix
  2016-12-19 11:15 ` [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector Nix
                   ` (17 more replies)
  0 siblings, 18 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:15 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

Here, as promised, is version 10 of the stack-protected glibc patch,
incorporating all review comments to date (unless I missed some).

Like the last, it's against glibc head as of Sat Nov 26, bf469f0ce98.

Tested with these flag combinations on {i686,x86_64)-pc-linux-gnu (with GCC
6.2.1-20161118, binutils 2.27.0.20160920, and kernel headers for v4.7.4):

--enable-omitfp --enable-stack-protector=all
--enable-stack-protector
--enable-stack-protector=strong
--enable-stack-protector=all
--enable-stackguard-randomization --enable-stack-protector=all
--enable-omitfp --enable-stackguard-randomization --enable-stack-protector
--enable-omitfp --enable-stackguard-randomization --enable-stack-protector=strong
--enable-omitfp --enable-stackguard-randomization --enable-stack-protector=all
--disable-stack-protector
--enable-stack-protector=no

(The only skipped tests were the AVX math tests, since the test machine is not
AVX-capable.)

Tested with with these flag combinations on sparc{32,64}-pc-linux-gnu (with GCC
4.9.1-20140922 (a bit old, sorry), binutils 2.24, and kernel headers for
v4.1.12):

--enable-stack-protector
--enable-stack-protector=strong
--enable-stackguard-randomization --enable-stack-protector=strong
--enable-stackguard-randomization --enable-stack-protector=all
--disable-stack-protector
--enable-stack-protector=no

Tested with these flag combinations on armv7l-unknown-linux-gnueabihf (with GCC
4.8.5-2ubuntu1~14.04.1 (so -strong isn't available), binutils 2.24, and kernel
headers for v3.13.11):

--enable-stackguard-randomization --enable-stack-protector
--enable-stackguard-randomization --enable-stack-protector=all --enable-omitfp
--disable-stack-protector
--enable-stack-protector=no

No failures are observed that are not also observed on an unpatched glibc with
the same flag combinations, saving only the usual intermittent localedata/wcs*
tests, an intermittent failure of the assertion in stdlib/tst-makecontext on
sparc32, and a consistent failure of posix/tst-spawn2: but these are mostly
likely spurious, other than posix/tst-spawn2, but I would completely believe
that a buffer overrun there is not spurious at all. The makecontext assertion
seems likely to depend on the behaviour of the installed libgcc_s.so...)

On the copyright assignment front, I am informed that Oracle has a blanket
assignment on file for glibc work, so I don't need to do anything.  (Patch 8 is
in Adhemerval's name, but obviously there's no assignment problem there either.)

Overview of changes in this posting:

 - Add back patch #1 in the series, the configury changes, accidentally dropped
   in the previous posting: whoops!

 - Revamp the __stack_chk_fail PLT-avoidance machinery to use strong_alias, like
   everything else: much thanks to Florian, who noticed that the hack I was
   using was problematic and provided tireless guidance in getting it fixed;
   debug/libc-stack_chk_fail_local.c is gone; there is not very much left of
   Adhemerval's original patch, but I'm leaving his name on it because it seems
   presumptuous to remove it; there are no __stack_chk_fail PLT bypasses on any
   platforms I have access to any more

 - As part of this, #define STACK_PROTECTOR_LEVEL=0 in non-stack-protected files
   within an otherwise stack-protected glibc, and when glibc is not
   stack-protected at all.

 - Explicitly pass -fno-stack-protector when glibc is not compiled with
   --enable-stack-protector: this prevents compiler-inserted __stack_chk_fails
   from being generated when glibc is not set up for PLT-bypassing them (will
   need tiny revisions if --enable-stack-protector becomes the default, but that
   time is not yet)

 - Drop some more libc_cv_predef_stack_protector checks that landed since my
   last series

 - Several tests were declaring that they needed to be compiled with
   $(no-stack-protector) when this is no longer true

 - Move the tests that were in csu/ (most of which were barely related to the
   code in csu/) into misc/ instead, so the -fno-stack-protection applied to the
   static-libc csu code does not cover the tests as well

 - Compile two PIE tests (tst-quad[12]pie.c) with -fPIE, since they are being
   linked with it and when stack-protection is enabled they suddenly contain
   external symbol references for the first time, which will result in the wrong
   relocation if the compiler does not know PIE is in use

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 08/15] Add a hidden __stack_chk_fail_local alias to libc.so.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
  2016-12-19 11:15 ` [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector Nix
@ 2016-12-19 11:15 ` Nix
  2016-12-19 11:15 ` [PATCH 06/15] Prevent the rtld mapfile computation from dragging in __stack_chk_fail* Nix
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:15 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

This is required by the next commit, which routes all
__stack_chk_fail() calls in libc.so via this symbol to avoid
the PLT.

Also stop all the variants of __stack_chk_fail from being stack-
protected: this makes no sense and risks recursion.

v5: Better explanation.  Add no-stack-protection of
    __stack_chk_fail_local etc.
v6: Rework as suggested by Andreas: make a shared-only version of
    stack_chk_fail_local.c rather than linking libc_nonshared into
    libc.
v10: Drop libc-stack_chk_fail_local.c: just use a strong_alias instead.
     (Thanks to Florian Weimer.)

	* debug/stack_chk_fail.c (__stack_chk_fail): Add strong_alias to
	__stack_chk_fail_local.
	* debug/Makefile (CFLAGS-stack_chk_fail.c): Use $(no-stack-protector).
	(CFLAGS-stack_chk_fail_local.c): Likewise.
---
 debug/Makefile         | 6 ++++++
 debug/stack_chk_fail.c | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/debug/Makefile b/debug/Makefile
index 6b5f31e..2f506af 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -52,6 +52,12 @@ routines  = backtrace backtracesyms backtracesymsfd noophooks \
 	    $(static-only-routines)
 static-only-routines := warning-nop stack_chk_fail_local
 
+# Building the stack-protector failure routines with stack protection
+# makes no sense.
+
+CFLAGS-stack_chk_fail.c = $(no-stack-protector)
+CFLAGS-stack_chk_fail_local.c = $(no-stack-protector)
+
 CFLAGS-backtrace.c = -fno-omit-frame-pointer
 CFLAGS-sprintf_chk.c = $(libio-mtsafe)
 CFLAGS-snprintf_chk.c = $(libio-mtsafe)
diff --git a/debug/stack_chk_fail.c b/debug/stack_chk_fail.c
index 4d0796f..5db0886 100644
--- a/debug/stack_chk_fail.c
+++ b/debug/stack_chk_fail.c
@@ -27,3 +27,5 @@ __stack_chk_fail (void)
 {
   __fortify_fail ("stack smashing detected");
 }
+
+strong_alias (__stack_chk_fail, __stack_chk_fail_local)
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
@ 2016-12-19 11:15 ` Nix
  2016-12-21 14:18   ` Florian Weimer
  2016-12-19 11:15 ` [PATCH 08/15] Add a hidden __stack_chk_fail_local alias to libc.so Nix
                   ` (16 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:15 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

The startup code in csu/, brk() and sbrk(), and the
__pthread_initialize_tcb_internal() function we just introduced are
needed very early in initialization of a statically-linked program,
before the stack guard is initialized; TLS initialization also uses
memcpy(), which cannot overrun its own stack.  Mark all of these as
-fno-stack-protector.

We also finally introduce @libc_cv_ssp@ and @no_stack_protector@, both
substituted by the configury changes made earlier, to detect the case
when -fno-stack-protector is supported by the compiler, and
unconditionally pass it in when this is the case, whether or not
--enable-stack-protector is passed to configure.  (This means that
it'll even work when the compiler's been hacked to pass
-fstack-protector by default, unless the hackage is so broken that
it does so in a way that is impossible to override.)

(At one point we marked __libc_fatal() as non-stack-protected too,
but this was pointless: all it did was call other routines which *are*
stack-protected.  The earliest __libc_fatal() call is in the
DL_SYSDEP_OSCHECK hook on some platforms, when statically linking:
this is fine, since it is after TLS and stack-canary initialization.
I have tested invocation of programs statically and dynamically
linked against this glibc on older kernels on x86 and ARM, and they
still "work", i.e. fail with the appropriate message.)

v2: No longer mark memcpy() as -fno-stack-protector.
v3: Use $(no-stack-protector).
v4: Use inhibit_stack_protector rather than de-protecting all of nptl-init.c.
v5: Don't stack-protect brk() and sbrk() in the shared library.
v7: Add comment in misc/Makefile.  Commit message tweak.
v8: Mark memcpy() as -fstack-protector again, along with wordcpy.

	* config.make.in (have-ssp): New.
	(no-stack-protector): New.
	* csu/Makefile (CFLAGS-.o): Use it.
	(CFLAGS-.og): Likewise.
	(CFLAGS-.op): Likewise.
	(CFLAGS-.os): Likewise.
	* misc/Makefile (CFLAGS-sbrk.o): Likewise.
	(CFLAGS-sbrk.op): Likewise.
	(CFLAGS-sbrk.og): Likewise.
	(CFLAGS-brk.o): Likewise.
	(CFLAGS-brk.op): Likewise.
	(CFLAGS-brk.og): Likewise.
	* string/Makefile (CFLAGS-memcpy.c): Likewise.
	(CFLAGS-wordcopy.c): Likewise.
	* nptl/nptl-init.c [!SHARED] (__pthread_initialize_tcb_internal):
	Likewise.
---
 config.make.in   | 2 ++
 csu/Makefile     | 5 +++++
 misc/Makefile    | 9 +++++++++
 nptl/nptl-init.c | 1 +
 string/Makefile  | 4 ++++
 5 files changed, 21 insertions(+)

diff --git a/config.make.in b/config.make.in
index 04a8b3e..bfc7d39 100644
--- a/config.make.in
+++ b/config.make.in
@@ -58,7 +58,9 @@ with-fp = @with_fp@
 enable-timezone-tools = @enable_timezone_tools@
 unwind-find-fde = @libc_cv_gcc_unwind_find_fde@
 have-fpie = @libc_cv_fpie@
+have-ssp = @libc_cv_ssp@
 stack-protector = @stack_protector@
+no-stack-protector = @no_stack_protector@
 have-selinux = @have_selinux@
 have-libaudit = @have_libaudit@
 have-libcap = @have_libcap@
diff --git a/csu/Makefile b/csu/Makefile
index 31e8bb9..22afe67 100644
--- a/csu/Makefile
+++ b/csu/Makefile
@@ -45,6 +45,11 @@ before-compile += $(objpfx)version-info.h
 tests := tst-empty tst-atomic tst-atomic-long
 tests-static := tst-empty
 
+CFLAGS-.o += $(no-stack-protector)
+CFLAGS-.og += $(no-stack-protector)
+CFLAGS-.op += $(no-stack-protector)
+CFLAGS-.os += $(no-stack-protector)
+
 ifeq (yes,$(build-shared))
 extra-objs += S$(start-installed-name) gmon-start.os
 ifneq ($(start-installed-name),$(static-start-installed-name))
diff --git a/misc/Makefile b/misc/Makefile
index 3d2ebb8..c382f92 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -105,6 +105,15 @@ CFLAGS-getusershell.c = -fexceptions
 CFLAGS-err.c = -fexceptions
 CFLAGS-tst-tsearch.c = $(stack-align-test-flags)
 
+# Called during static library initialization, so turn stack-protection
+# off for non-shared builds.
+CFLAGS-sbrk.o = $(no-stack-protector)
+CFLAGS-sbrk.op = $(no-stack-protector)
+CFLAGS-sbrk.og = $(no-stack-protector)
+CFLAGS-brk.o = $(no-stack-protector)
+CFLAGS-brk.op = $(no-stack-protector)
+CFLAGS-brk.og = $(no-stack-protector)
+
 include ../Rules
 
 $(objpfx)libg.a: $(dep-dummy-lib); $(make-dummy-lib)
diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c
index dea335d..3288a85 100644
--- a/nptl/nptl-init.c
+++ b/nptl/nptl-init.c
@@ -287,6 +287,7 @@ static bool __nptl_initial_report_events __attribute_used__;
 
 #ifndef SHARED
 void
+inhibit_stack_protector
 __pthread_initialize_tcb_internal (void)
 {
   /* Unlike in the dynamically linked case the dynamic linker has not
diff --git a/string/Makefile b/string/Makefile
index 69d3f80..3e35dca 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -71,6 +71,10 @@ CFLAGS-stratcliff.c = -fno-builtin
 CFLAGS-test-ffs.c = -fno-builtin
 CFLAGS-tst-inlcall.c = -fno-builtin
 
+# Called during TLS initialization.
+CFLAGS-memcpy.c = $(no-stack-protector)
+CFLAGS-wordcopy.c = $(no-stack-protector)
+
 ifeq ($(run-built-tests),yes)
 $(objpfx)tst-svc-cmp.out: tst-svc.expect $(objpfx)tst-svc.out
 	cmp $^ > $@; \
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 15/15] Enable -fstack-protector=* when requested by configure.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (2 preceding siblings ...)
  2016-12-19 11:15 ` [PATCH 06/15] Prevent the rtld mapfile computation from dragging in __stack_chk_fail* Nix
@ 2016-12-19 11:25 ` Nix
  2016-12-19 11:25 ` [PATCH 10/15] Link a non-libc-using test with -fno-stack-protector Nix
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

This finally turns on all the machinery added in previous commits.

v3: Wrap long lines.
v5: Shuffle to the end.

	* Makeconfig (+stack-protector): New variable.
	(+cflags): Use it.
---
 Makeconfig | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/Makeconfig b/Makeconfig
index a785860..2b84d93 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -762,6 +762,11 @@ endif
 # disable any optimization that assume default rounding mode.
 +math-flags = -frounding-math
 
+# We might want to compile with some stack-protection flag.
+ifneq ($(stack-protector),)
++stack-protector=$(stack-protector)
+endif
+
 # This is the program that generates makefile dependencies from C source files.
 # The -MP flag tells GCC >= 3.2 (which we now require) to produce dummy
 # targets for headers so that removed headers don't break the build.
@@ -821,7 +826,8 @@ ifeq	"$(strip $(+cflags))" ""
 +cflags	:= $(default_cflags)
 endif	# $(+cflags) == ""
 
-+cflags += $(cflags-cpu) $(+gccwarn) $(+merge-constants) $(+math-flags)
++cflags += $(cflags-cpu) $(+gccwarn) $(+merge-constants) $(+math-flags) \
+	   $(+stack-protector)
 +gcc-nowarn := -w
 
 # Don't duplicate options if we inherited variables from the parent.
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 10/15] Link a non-libc-using test with -fno-stack-protector.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (3 preceding siblings ...)
  2016-12-19 11:25 ` [PATCH 15/15] Enable -fstack-protector=* when requested by configure Nix
@ 2016-12-19 11:25 ` Nix
  2016-12-19 11:25 ` [PATCH 11/15] Drop explicit stack-protection of pieces of the system Nix
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

This test cannot see __stack_chk_fail() because it is not linked with
libc at all.

v3: Use $(no-stack-protector).
v10: Drop a bunch of tests that don't need this option.

	* elf/Makefile (CFLAGS-filtmod1.c): Use $(no-stack-protector) for
	non-libc-linking testcase.
---
 elf/Makefile | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/elf/Makefile b/elf/Makefile
index daf0ebd..2c87a94 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -775,6 +775,9 @@ $(objpfx)filtmod1.so: $(objpfx)filtmod1.os $(objpfx)filtmod2.so
 		  $< -Wl,-F,$(objpfx)filtmod2.so
 $(objpfx)filter: $(objpfx)filtmod1.so
 
+# This does not link against libc.
+CFLAGS-filtmod1.c = $(no-stack-protector)
+
 $(objpfx)unload: $(libdl)
 $(objpfx)unload.out: $(objpfx)unloadmod.so
 
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 11/15] Drop explicit stack-protection of pieces of the system.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (4 preceding siblings ...)
  2016-12-19 11:25 ` [PATCH 10/15] Link a non-libc-using test with -fno-stack-protector Nix
@ 2016-12-19 11:25 ` Nix
  2016-12-19 11:25 ` [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE Nix
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

This is probably a bad idea: maybe we want to stack-protect some parts
of the system even when ! --enable-stack-protector.  I can easily adjust
the patch to do that (though it'll mean introducing a new variable
analogous to $(stack-protector) but not controlled by the configure
flag.)

But if we wanted to value consistency over security, and use the same
stack-protection configure flag to control everything, this is how we'd
do it!

("Always include at least one patch with something obviously wrong with
it.")

	* login/Makefile (pt_chown-cflags): Remove.
	* nscd/Makefile (CFLAGS-nscd): Likewise.
	* resolv/Makefile (CFLAGS-libresolv): Likewise.
---
 login/Makefile  | 1 -
 nscd/Makefile   | 1 -
 resolv/Makefile | 1 -
 3 files changed, 3 deletions(-)

diff --git a/login/Makefile b/login/Makefile
index 9ff36d6..1a6161c 100644
--- a/login/Makefile
+++ b/login/Makefile
@@ -58,7 +58,6 @@ CFLAGS-getpt.c = -fexceptions
 ifeq (yesyes,$(have-fpie)$(build-shared))
 pt_chown-cflags += $(pie-ccflag)
 endif
-pt_chown-cflags += $(stack-protector)
 ifeq (yes,$(have-libcap))
 libcap = -lcap
 endif
diff --git a/nscd/Makefile b/nscd/Makefile
index 50bad32..bfd72d5 100644
--- a/nscd/Makefile
+++ b/nscd/Makefile
@@ -84,7 +84,6 @@ CPPFLAGS-nscd += -D_FORTIFY_SOURCE=2
 ifeq (yesyes,$(have-fpie)$(build-shared))
 CFLAGS-nscd += $(pie-ccflag)
 endif
-CFLAGS-nscd += $(stack-protector)
 
 ifeq (yesyes,$(have-fpie)$(build-shared))
 LDFLAGS-nscd = -Wl,-z,now
diff --git a/resolv/Makefile b/resolv/Makefile
index be20368..06329e1 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -90,7 +90,6 @@ CPPFLAGS += -Dgethostbyname=res_gethostbyname \
 	    -Dgetnetbyname=res_getnetbyname \
 	    -Dgetnetbyaddr=res_getnetbyaddr
 
-CFLAGS-libresolv += $(stack-protector)
 CFLAGS-res_hconf.c = -fexceptions
 
 # The DNS NSS modules needs the resolver.
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (5 preceding siblings ...)
  2016-12-19 11:25 ` [PATCH 11/15] Drop explicit stack-protection of pieces of the system Nix
@ 2016-12-19 11:25 ` Nix
  2016-12-21 13:36   ` Florian Weimer
  2016-12-19 11:25 ` [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so Nix
                   ` (10 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

With stack protection enabled, these files have external symbol
references for the first time, so the fact that they are not compiled
with -fPIE and are then linked into a -pie binary starts to hurt.

v10: New.

	* sysdeps/x86_64/Makefile (CFLAGS-tst-quad1pie.c): Add PIE-ccflag.
	(CFLAGS-tst-quad2pie.c): Likewise.
---
 sysdeps/x86_64/Makefile | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
index 6d99284..5f25893 100644
--- a/sysdeps/x86_64/Makefile
+++ b/sysdeps/x86_64/Makefile
@@ -49,6 +49,9 @@ extra-test-objs += tst-quadmod1pie.o tst-quadmod2pie.o
 $(objpfx)tst-quad1pie: $(objpfx)tst-quadmod1pie.o
 $(objpfx)tst-quad2pie: $(objpfx)tst-quadmod2pie.o
 
+CFLAGS-tst-quad1pie.c = $(PIE-ccflag)
+CFLAGS-tst-quad2pie.c = $(PIE-ccflag)
+
 tests += tst-audit3 tst-audit4 tst-audit5 tst-audit6 tst-audit7 tst-audit10
 test-extras += tst-audit4-aux tst-audit10-aux
 extra-test-objs += tst-audit4-aux.o tst-audit10-aux.o
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (6 preceding siblings ...)
  2016-12-19 11:25 ` [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE Nix
@ 2016-12-19 11:25 ` Nix
  2016-12-21 15:05   ` Florian Weimer
  2016-12-19 11:26 ` [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector Nix
                   ` (9 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:25 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Adhemerval Zanella <adhemerval.zanella@linaro.org>

We use the same assembler-macro trick we use to de-PLTize
compiler-generated libcalls to memcpy and memset to redirect
__stack_chk_fail to __stack_chk_fail_local.

v5: New.
v6: Only do it within the shared library: with __stack_chk_fail_local
    in libc_pic.a now we don't need to worry about calls from inside
    other routines in libc_nonshared.a any more.
v8: Merge #ifdef blocks.
v10: Use STACK_PROTECTOR_LEVEL to avoid renaming in files where
     stack-protection is suppressed.

	* sysdeps/generic/symbol-hacks.h [STACK_PROTECTOR_LEVEL &&
	STACK_PROTECTOR_LEVEL > 0] (__stack_chk_fail): Add internal alias.
---
 sysdeps/generic/symbol-hacks.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/sysdeps/generic/symbol-hacks.h b/sysdeps/generic/symbol-hacks.h
index ce576c9..d614c09 100644
--- a/sysdeps/generic/symbol-hacks.h
+++ b/sysdeps/generic/symbol-hacks.h
@@ -4,4 +4,16 @@
 asm ("memmove = __GI_memmove");
 asm ("memset = __GI_memset");
 asm ("memcpy = __GI_memcpy");
+
+/* Some targets do not use __stack_chk_fail_local.  In libc.so,
+   redirect __stack_chk_fail to a hidden reference
+   __stack_chk_fail_local, to avoid the PLT reference.
+   __stack_chk_fail itself is a global symbol, exported from libc.so,
+   and cannot be made hidden.  */
+
+# if IS_IN (libc) && defined SHARED && \
+  defined STACK_PROTECTOR_LEVEL && STACK_PROTECTOR_LEVEL > 0
+asm (".hidden __stack_chk_fail_local\n"
+     "__stack_chk_fail = __stack_chk_fail_local");
+# endif
 #endif
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 01/15] Configury support for --enable-stack-protector.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (11 preceding siblings ...)
  2016-12-19 11:26 ` [PATCH 13/15] Move all tests out of csu Nix
@ 2016-12-19 11:26 ` Nix
  2016-12-19 11:27 ` [PATCH 03/15] Do not stack-protect ifunc resolvers Nix
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:26 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

This adds =all and =strong, with obvious semantics, and with a rather
arbitrarily-chosen default off, which we might well want to change to
something stronger once this patch has been tested by people other than
me.

We don't validate the value of the option yet: that's in a later patch.
Nor do we use it for anything at this stage.

We differentiate between 'the compiler understands -fstack-protector'
and 'the user wanted -fstack-protector' so that we can pass
-fno-stack-protector in appropriate places even if the user didn't want
to turn on -fstack-protector for other parts.  (This helps us overcome
another existing limitation, that glibc doesn't work with GCCs hacked
to pass in -fstack-protector by default.)

We also arrange to set the STACK_PROTECTOR_LEVEL #define to a value
appropriate for the stack-protection level in use for each file in
particular.

We might want to add another configuration option to turn on
-fstack-protector for nscd and other network-facing operations by
default, but for now I've stuck with one option to control everything.

v2: documentation in install.texi; better description of the option.
    INSTALL regenerated.
v3: Substitute in no_stack_protector.
v6: Small quoting/spacing revisions following Mike Frysinger's review.
    Add STACK_PROTECTOR_LEVEL.
v7: Quoting changes. Report --enable-stack-protector argument values
    on error.
v10: Only define STACK_PROTECTOR_LEVEL if not already defined.
     Define it to 0 in the no_stack_protector case.

	[BZ #7065]
	* configure.ac (libc_cv_ssp): Move up.
	(libc_cv_ssp_strong): Likewise.
	(libc_cv_ssp_all): New.
	(stack_protector): Augment, adding -fstack-protector-all.
	(no_stack_protector): New.
	(STACK_PROTECTOR_LEVEL): New.
	(AC_ARG_ENABLE(stack-protector)): New configure flag.
	* manual/install.texi (--enable-stack-protector): Document it.
	* config.h.in (STACK_PROTECTOR_LEVEL): New macro.
	* INSTALL: Regenerate.
---
 INSTALL             | 39 ++++++++++++++++++-----------
 config.h.in         |  6 +++++
 configure.ac        | 70 ++++++++++++++++++++++++++++++++++++++---------------
 manual/install.texi | 12 +++++++++
 4 files changed, 93 insertions(+), 34 deletions(-)

diff --git a/INSTALL b/INSTALL
index b5acedc..2b0abf9 100644
--- a/INSTALL
+++ b/INSTALL
@@ -135,20 +135,31 @@ will be used, and CFLAGS sets optimization options for the compiler.
 '--enable-lock-elision=yes'
      Enable lock elision for pthread mutexes by default.
 
-'--enable-pt_chown'
-     The file 'pt_chown' is a helper binary for 'grantpt' (*note
-     Pseudo-Terminals: Allocation.) that is installed setuid root to fix
-     up pseudo-terminal ownership.  It is not built by default because
-     systems using the Linux kernel are commonly built with the 'devpts'
-     filesystem enabled and mounted at '/dev/pts', which manages
-     pseudo-terminal ownership automatically.  By using
-     '--enable-pt_chown', you may build 'pt_chown' and install it setuid
-     and owned by 'root'.  The use of 'pt_chown' introduces additional
-     security risks to the system and you should enable it only if you
-     understand and accept those risks.
-
-'--disable-werror'
-     By default, the GNU C Library is built with '-Werror'.  If you wish
+`--enable-stack-protector'
+`--enable-stack-protector=strong'
+`--enable-stack-protector=all'
+     Compile the C library and all other parts of the glibc package
+     (including the threading and math libraries, NSS modules, and
+     transliteration modules) using the GCC `-fstack-protector',
+     `-fstack-protector-strong' or `-fstack-protector-all' options to
+     detect stack overruns.  Only the dynamic linker and a small number
+     of routines called directly from assembler are excluded from this
+     protection.
+
+`--enable-pt_chown'
+     The file `pt_chown' is a helper binary for `grantpt' (*note
+     Pseudo-Terminals: Allocation.) that is installed setuid root to
+     fix up pseudo-terminal ownership.  It is not built by default
+     because systems using the Linux kernel are commonly built with the
+     `devpts' filesystem enabled and mounted at `/dev/pts', which
+     manages pseudo-terminal ownership automatically.  By using
+     `--enable-pt_chown', you may build `pt_chown' and install it
+     setuid and owned by `root'.  The use of `pt_chown' introduces
+     additional security risks to the system and you should enable it
+     only if you understand and accept those risks.
+
+`--disable-werror'
+     By default, the GNU C Library is built with `-Werror'.  If you wish
      to build without this option (for example, if building with a newer
      version of GCC than this version of the GNU C Library was tested
      with, so new warnings cause the build with '-Werror' to fail), you
diff --git a/config.h.in b/config.h.in
index 33757bd..d96ce0f 100644
--- a/config.h.in
+++ b/config.h.in
@@ -48,6 +48,12 @@
 /* Define if compiler accepts -ftree-loop-distribute-patterns.  */
 #undef  HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
 
+/* The level of stack protection in use for glibc as a whole.
+   May be overridden on a file-by-file basis.  */
+#ifndef STACK_PROTECTOR_LEVEL
+#undef	STACK_PROTECTOR_LEVEL
+#endif
+
 /* Define if the regparm attribute shall be used for local functions
    (gcc on ix86 only).  */
 #undef	USE_REGPARMS
diff --git a/configure.ac b/configure.ac
index de0a40f..f131e97 100644
--- a/configure.ac
+++ b/configure.ac
@@ -235,6 +235,18 @@ if test "x$bindnow" = xyes; then
   AC_DEFINE(BIND_NOW)
 fi
 
+dnl Build glibc with -fstack-protector, -fstack-protector-all, or
+dnl -fstack-protector-strong.
+AC_ARG_ENABLE([stack-protector],
+	      AC_HELP_STRING([--enable-stack-protector=@<:@yes|no|all|strong@:>@],
+			     [Use -fstack-protector[-all|-strong] to detect glibc buffer overflows]),
+	      [enable_stack_protector=$enableval],
+	      [enable_stack_protector=no])
+case "$enable_stack_protector" in
+all|yes|no|strong) ;;
+*) AC_MSG_ERROR([Not a valid argument for --enable-stack-protector: \"$enable_stack_protector\"]);;
+esac
+
 dnl On some platforms we cannot use dynamic loading.  We must provide
 dnl static NSS modules.
 AC_ARG_ENABLE([static-nss],
@@ -605,6 +617,44 @@ fi
 test -n "$base_machine" || base_machine=$machine
 AC_SUBST(base_machine)
 
+AC_CACHE_CHECK(for -fstack-protector, libc_cv_ssp, [dnl
+LIBC_TRY_CC_OPTION([$CFLAGS $CPPFLAGS -Werror -fstack-protector],
+		   [libc_cv_ssp=yes],
+		   [libc_cv_ssp=no])
+])
+
+AC_CACHE_CHECK(for -fstack-protector-strong, libc_cv_ssp_strong, [dnl
+LIBC_TRY_CC_OPTION([$CFLAGS $CPPFLAGS -Werror -fstack-protector-strong],
+		   [libc_cv_ssp_strong=yes],
+		   [libc_cv_ssp_strong=no])
+])
+
+AC_CACHE_CHECK(for -fstack-protector-all, libc_cv_ssp_all, [dnl
+LIBC_TRY_CC_OPTION([$CFLAGS $CPPFLAGS -Werror -fstack-protector-all],
+		   [libc_cv_ssp_all=yes],
+		   [libc_cv_ssp_all=no])
+])
+
+stack_protector=
+no_stack_protector=
+if test "$libc_cv_ssp" = yes; then
+  no_stack_protector="-fno-stack-protector -DSTACK_PROTECTOR_LEVEL=0"
+fi
+
+if test "$enable_stack_protector" = yes && test "$libc_cv_ssp" = yes; then
+  stack_protector="-fstack-protector"
+  AC_DEFINE(STACK_PROTECTOR_LEVEL, 1)
+elif test "$enable_stack_protector" = all && test "$libc_cv_ssp_all" = yes; then
+  stack_protector="-fstack-protector-all"
+  AC_DEFINE(STACK_PROTECTOR_LEVEL, 2)
+elif test "$enable_stack_protector" = strong && test "$libc_cv_ssp_strong" = yes; then
+  stack_protector="-fstack-protector-strong"
+  AC_DEFINE(STACK_PROTECTOR_LEVEL, 3)
+fi
+AC_SUBST(libc_cv_ssp)
+AC_SUBST(stack_protector)
+AC_SUBST(no_stack_protector)
+
 # For the multi-arch option we need support in the assembler & linker.
 AC_CACHE_CHECK([for assembler and linker STT_GNU_IFUNC support],
 	       libc_cv_ld_gnu_indirect_function, [dnl
@@ -1433,26 +1483,6 @@ else
 fi
 AC_SUBST(fno_unit_at_a_time)
 
-AC_CACHE_CHECK(for -fstack-protector, libc_cv_ssp, [dnl
-LIBC_TRY_CC_OPTION([$CFLAGS $CPPFLAGS -Werror -fstack-protector],
-		   [libc_cv_ssp=yes],
-		   [libc_cv_ssp=no])
-])
-
-AC_CACHE_CHECK(for -fstack-protector-strong, libc_cv_ssp_strong, [dnl
-LIBC_TRY_CC_OPTION([$CFLAGS $CPPFLAGS -Werror -fstack-protector-strong],
-		   [libc_cv_ssp_strong=yes],
-		   [libc_cv_ssp_strong=no])
-])
-
-stack_protector=
-if test "$libc_cv_ssp_strong" = "yes"; then
-  stack_protector="-fstack-protector-strong"
-elif test "$libc_cv_ssp" = "yes"; then
-  stack_protector="-fstack-protector"
-fi
-AC_SUBST(stack_protector)
-
 AC_CACHE_CHECK([for -mtls-dialect=gnu2], libc_cv_mtls_dialect_gnu2,
 [dnl
 cat > conftest.c <<EOF
diff --git a/manual/install.texi b/manual/install.texi
index de1c203..b5be87d 100644
--- a/manual/install.texi
+++ b/manual/install.texi
@@ -164,6 +164,18 @@ time.  Consult the @file{timezone} subdirectory for more details.
 @item --enable-lock-elision=yes
 Enable lock elision for pthread mutexes by default.
 
+@item --enable-stack-protector
+@itemx --enable-stack-protector=strong
+@itemx --enable-stack-protector=all
+Compile the C library and all other parts of the glibc package
+(including the threading and math libraries, NSS modules, and
+transliteration modules) using the GCC @option{-fstack-protector},
+@option{-fstack-protector-strong} or @option{-fstack-protector-all}
+options to detect stack overruns.  Only the dynamic linker and a small
+number of routines called directly from assembler are excluded from this
+protection.
+
+
 @pindex pt_chown
 @findex grantpt
 @item --enable-pt_chown
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 13/15] Move all tests out of csu.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (10 preceding siblings ...)
  2016-12-19 11:26 ` [PATCH 12/15] Do not stack-protect sigreturn stubs Nix
@ 2016-12-19 11:26 ` Nix
  2016-12-21 13:36   ` Florian Weimer
  2016-12-19 11:26 ` [PATCH 01/15] Configury support for --enable-stack-protector Nix
                   ` (5 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:26 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

Stack-protection on .o files in csu/ must be suppressed for the sake of
library startup code.  This also suppresses stack-protection in tests
(which are also covered by CFLAGS-.o), though this is neither necessary
nor desirable.

So impose the rule that .o files in csu/ are necessarily C startup code,
and move the few tests in there into misc/ instead.

v10: New.

	* csu/Makefile (tests): Move this...
	(tests-static): ... and this...
	* misc/Makefile (tests): ... to here...
	(tests-static): ... and here.
        * csu/tst-empty.c: Move to...
        * misc/tst-empty.c: ... here.
        * csu/tst-atomic.c: Move to...
        * misc/tst-atomic.c: ... here.
        * csu/tst-atomic-long.c: Move to...
        * misc/tst-atomic-long.c: ... here.
---
 csu/Makefile                    | 3 ---
 misc/Makefile                   | 5 ++++-
 {csu => misc}/tst-atomic-long.c | 0
 {csu => misc}/tst-atomic.c      | 0
 {csu => misc}/tst-empty.c       | 0
 5 files changed, 4 insertions(+), 4 deletions(-)
 rename {csu => misc}/tst-atomic-long.c (100%)
 rename {csu => misc}/tst-atomic.c (100%)
 rename {csu => misc}/tst-empty.c (100%)

diff --git a/csu/Makefile b/csu/Makefile
index 22afe67..8e33cb1 100644
--- a/csu/Makefile
+++ b/csu/Makefile
@@ -42,9 +42,6 @@ install-lib = $(start-installed-name) g$(start-installed-name) $(csu-dummies)
 generated += version-info.h
 before-compile += $(objpfx)version-info.h
 
-tests := tst-empty tst-atomic tst-atomic-long
-tests-static := tst-empty
-
 CFLAGS-.o += $(no-stack-protector)
 CFLAGS-.og += $(no-stack-protector)
 CFLAGS-.op += $(no-stack-protector)
diff --git a/misc/Makefile b/misc/Makefile
index c382f92..a390872 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -79,7 +79,10 @@ gpl2lgpl := error.c error.h
 tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
 	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 \
 	 tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
-	 tst-preadvwritev tst-preadvwritev64 tst-makedev
+	 tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
+	 tst-atomic tst-atomic-long
+tests-static := tst-empty
+
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-error1-mem.out
 endif
diff --git a/csu/tst-atomic-long.c b/misc/tst-atomic-long.c
similarity index 100%
rename from csu/tst-atomic-long.c
rename to misc/tst-atomic-long.c
diff --git a/csu/tst-atomic.c b/misc/tst-atomic.c
similarity index 100%
rename from csu/tst-atomic.c
rename to misc/tst-atomic.c
diff --git a/csu/tst-empty.c b/misc/tst-empty.c
similarity index 100%
rename from csu/tst-empty.c
rename to misc/tst-empty.c
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 02/15] Initialize the stack guard earlier when linking statically.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (8 preceding siblings ...)
  2016-12-19 11:26 ` [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector Nix
@ 2016-12-19 11:26 ` Nix
  2016-12-21 14:16   ` Florian Weimer
  2016-12-19 11:26 ` [PATCH 12/15] Do not stack-protect sigreturn stubs Nix
                   ` (7 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:26 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

The address of the stack canary is stored in a per-thread variable,
which means that we must ensure that the TLS area is intialized before
calling any -fstack-protector'ed functions.  For dynamically linked
applications, we ensure this (in a later patch) by disabling
-fstack-protector for the whole dynamic linker, but for static
applications the AT_ENTRY address is called directly by the kernel, so
we must deal with the problem differently.

So split out the part of pthread initialization that sets up the TCB
(and, more generally, the TLS area) into a separate function (twice --
there is one implementation in libpthread.a, and another outside it for
programs that do not link with libpthread), then call it at
initialization time.  Call that, and move the stack guard initialization
above the DL_SYSDEP_OSCHECK hook, which if set will probably call
functions which are stack-protected (it does on Linux and NaCL too).
We also move apply_irel() up, so that we can still safely call functions
that require ifuncs while in __pthread_initialize_tcb_internal()
(though if stack-protection is enabled we still have to avoid calling
functions that are not stack-protected at this stage).

v2: describe why we don't move apply_irel() up, and the consequences.
v6: We can safely move apply_irel() up now.

	* nptl/nptl-init.c (__pthread_initialize_tcb_internal): New
	function, split out from...
	(__pthread_initialize_minimal_internal): ... here.
	* csu/libc-start.c (LIBC_START_MAIN): Call it.  Move stack canary
	and apply_irel() initialization up.
---
 csu/libc-start.c | 26 +++++++++++++++-----------
 csu/libc-tls.c   |  8 ++++++++
 nptl/nptl-init.c | 11 +++++++----
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/csu/libc-start.c b/csu/libc-start.c
index 99c040a..0bd4385 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -30,6 +30,7 @@ extern int __libc_multiple_libcs;
 #ifndef SHARED
 # include <dl-osinfo.h>
 extern void __pthread_initialize_minimal (void);
+extern void __pthread_initialize_tcb_internal (void);
 # ifndef THREAD_SET_STACK_GUARD
 /* Only exported for architectures that don't store the stack guard canary
    in thread local area.  */
@@ -175,6 +176,20 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
         }
     }
 
+  /* Perform IREL{,A} relocations.  */
+  apply_irel ();
+
+  /* The stack guard goes into the TCB.  */
+  __pthread_initialize_tcb_internal ();
+
+  /* Set up the stack checker's canary.  */
+  uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
+# ifdef THREAD_SET_STACK_GUARD
+  THREAD_SET_STACK_GUARD (stack_chk_guard);
+# else
+  __stack_chk_guard = stack_chk_guard;
+# endif
+
 # ifdef DL_SYSDEP_OSCHECK
   if (!__libc_multiple_libcs)
     {
@@ -184,22 +199,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
     }
 # endif
 
-  /* Perform IREL{,A} relocations.  */
-  apply_irel ();
-
   /* Initialize the thread library at least a bit since the libgcc
      functions are using thread functions if these are available and
      we need to setup errno.  */
   __pthread_initialize_minimal ();
 
-  /* Set up the stack checker's canary.  */
-  uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
-# ifdef THREAD_SET_STACK_GUARD
-  THREAD_SET_STACK_GUARD (stack_chk_guard);
-# else
-  __stack_chk_guard = stack_chk_guard;
-# endif
-
   /* Set up the pointer guard value.  */
   uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
 							 stack_chk_guard);
diff --git a/csu/libc-tls.c b/csu/libc-tls.c
index 235ac79..b92c567 100644
--- a/csu/libc-tls.c
+++ b/csu/libc-tls.c
@@ -241,5 +241,13 @@ void
 __attribute__ ((weak))
 __pthread_initialize_minimal (void)
 {
+}
+
+/* This is the minimal initialization function used when libpthread is
+   not used.  */
+void
+__attribute__ ((weak))
+__pthread_initialize_tcb_internal (void)
+{
   __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
 }
diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c
index 48fab50..dea335d 100644
--- a/nptl/nptl-init.c
+++ b/nptl/nptl-init.c
@@ -285,21 +285,24 @@ extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
 /* This can be set by the debugger before initialization is complete.  */
 static bool __nptl_initial_report_events __attribute_used__;
 
+#ifndef SHARED
 void
-__pthread_initialize_minimal_internal (void)
+__pthread_initialize_tcb_internal (void)
 {
-#ifndef SHARED
   /* Unlike in the dynamically linked case the dynamic linker has not
      taken care of initializing the TLS data structures.  */
   __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
 
-  /* We must prevent gcc from being clever and move any of the
+  /* We must prevent gcc from being clever after inlining and moving any of the
      following code ahead of the __libc_setup_tls call.  This function
      will initialize the thread register which is subsequently
      used.  */
   __asm __volatile ("");
+}
 #endif
-
+void
+__pthread_initialize_minimal_internal (void)
+{
   /* Minimal initialization of the thread descriptor.  */
   struct pthread *pd = THREAD_SELF;
   __pthread_initialize_pids (pd);
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (7 preceding siblings ...)
  2016-12-19 11:25 ` [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so Nix
@ 2016-12-19 11:26 ` Nix
  2016-12-21 14:18   ` Florian Weimer
  2016-12-19 11:26 ` [PATCH 02/15] Initialize the stack guard earlier when linking statically Nix
                   ` (8 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:26 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

Also compile corresponding routines in the static libc.a with the same
flag.

v3: Use $(no-stack-protector).
    Introduce $(elide-stack-protector) and use it to reduce redundancy.
    Bring all the elisions together textually.
v9: Adapt to no-sse adjustments in sysdeps/i386/Makefile.

	* elf/Makefile (elide-stack-protector): New.
	(CFLAGS-.os): Use it, eliding $(all-rtld-routines).
	(CFLAGS-.oX): Likewise, eliding $(elide-routines.os).
	(rtld-CFLAGS): Likewise.
	sysdeps/i386/Makefile (rtld-CFLAGS): Use +=, not =.
---
 elf/Makefile          | 13 +++++++++++++
 elf/rtld-Rules        |  2 ++
 sysdeps/i386/Makefile |  2 +-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/elf/Makefile b/elf/Makefile
index 82c7e05..d14d48d 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -50,6 +50,19 @@ CFLAGS-dl-runtime.c = -fexceptions -fasynchronous-unwind-tables
 CFLAGS-dl-lookup.c = -fexceptions -fasynchronous-unwind-tables
 CFLAGS-dl-iterate-phdr.c = $(uses-callbacks)
 
+# Compile rtld itself without stack protection.
+# Also compile all routines in the static library that are elided from
+# the shared libc because they are in ld.so the same way.
+
+define elide-stack-protector
+$(if $(filter $(@F),$(patsubst %,%$(1),$(2))), $(no-stack-protector))
+endef
+
+CFLAGS-.o += $(call elide-stack-protector,.o,$(elide-routines.os))
+CFLAGS-.op += $(call elide-stack-protector,.op,$(elide-routines.os))
+CFLAGS-.og += $(call elide-stack-protector,.og,$(elide-routines.os))
+CFLAGS-.os += $(call elide-stack-protector,.os,$(all-rtld-routines))
+
 ifeq ($(unwind-find-fde),yes)
 routines += unwind-dw2-fde-glibc
 shared-only-routines += unwind-dw2-fde-glibc
diff --git a/elf/rtld-Rules b/elf/rtld-Rules
index c1bb506..84d9387 100644
--- a/elf/rtld-Rules
+++ b/elf/rtld-Rules
@@ -144,4 +144,6 @@ cpp-srcs-left := $(rtld-modules:%.os=%)
 lib := rtld
 include $(patsubst %,$(..)cppflags-iterator.mk,$(cpp-srcs-left))
 
+rtld-CFLAGS += $(no-stack-protector)
+
 endif
diff --git a/sysdeps/i386/Makefile b/sysdeps/i386/Makefile
index e94f2cb..e30e133 100644
--- a/sysdeps/i386/Makefile
+++ b/sysdeps/i386/Makefile
@@ -88,7 +88,7 @@ endif
 # the first 3 mm/xmm/ymm/zmm registers are used to pass vector parameters
 # which must be preserved.
 # With SSE disabled, ensure -fpmath is not set to use sse either.
-rtld-CFLAGS = -mno-sse -mno-mmx -mfpmath=387
+rtld-CFLAGS += -mno-sse -mno-mmx -mfpmath=387
 ifeq ($(subdir),elf)
 CFLAGS-.os += $(if $(filter $(@F),$(patsubst %,%.os,$(all-rtld-routines))),\
 		   $(rtld-CFLAGS))
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 12/15] Do not stack-protect sigreturn stubs.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (9 preceding siblings ...)
  2016-12-19 11:26 ` [PATCH 02/15] Initialize the stack guard earlier when linking statically Nix
@ 2016-12-19 11:26 ` Nix
  2016-12-19 11:26 ` [PATCH 13/15] Move all tests out of csu Nix
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:26 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

These are called from the kernel with the stack at a carefully-
chosen location so that the stack frame can be restored: they must not
move the stack pointer lest garbage be restored into the registers.

We explicitly inhibit protection for SPARC and for signal/sigreturn.c:
other arches either define their sigreturn stubs in .S files, or (i386,
x86_64, mips) use macros expanding to top-level asm blocks and explicit
labels in the text section to mock up a "function" without telling the
compiler that one is there at all.

v2: New.
v3: Use $(no-stack-protector).
v4: Use inhibit_stack_protector.
v7: Add sigreturn.c.

	* signal/Makefile (CFLAGS-sigreturn.c): Use
	$(no-stack-protector).
	* sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c:
	(__rt_sigreturn_stub): Use inhibit_stack_protector.
	* sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c
	(__rt_sigreturn_stub): Likewise.
	(__sigreturn_stub): Likewise.
---
 signal/Makefile                                   | 2 ++
 sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c | 8 ++++++--
 sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c | 4 +++-
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/signal/Makefile b/signal/Makefile
index 9d29ff4..ccd6f51 100644
--- a/signal/Makefile
+++ b/signal/Makefile
@@ -48,3 +48,5 @@ CFLAGS-sigsuspend.c = -fexceptions -fasynchronous-unwind-tables
 CFLAGS-sigtimedwait.c = -fexceptions -fasynchronous-unwind-tables
 CFLAGS-sigwait.c = -fexceptions -fasynchronous-unwind-tables
 CFLAGS-sigwaitinfo.c = -fexceptions -fasynchronous-unwind-tables
+
+CFLAGS-sigreturn.c = $(no-stack-protector)
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c b/sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c
index 5aa3c35..b75142f 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c
+++ b/sysdeps/unix/sysv/linux/sparc/sparc32/sigaction.c
@@ -65,7 +65,9 @@ libc_hidden_def (__libc_sigaction)
 #include <nptl/sigaction.c>
 
 
-static void
+static
+inhibit_stack_protector
+void
 __rt_sigreturn_stub (void)
 {
   __asm__ ("mov %0, %%g1\n\t"
@@ -74,7 +76,9 @@ __rt_sigreturn_stub (void)
 	   : "i" (__NR_rt_sigreturn));
 }
 
-static void
+static
+inhibit_stack_protector
+void
 __sigreturn_stub (void)
 {
   __asm__ ("mov %0, %%g1\n\t"
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c b/sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c
index 50c444c..058c011 100644
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c
+++ b/sysdeps/unix/sysv/linux/sparc/sparc64/sigaction.c
@@ -66,7 +66,9 @@ libc_hidden_def (__libc_sigaction)
 #include <nptl/sigaction.c>
 
 
-static void
+static
+inhibit_stack_protector
+void
 __rt_sigreturn_stub (void)
 {
   __asm__ ("mov %0, %%g1\n\t"
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 07/15] Work even with compilers hacked to enable -fstack-protector by default.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (13 preceding siblings ...)
  2016-12-19 11:27 ` [PATCH 03/15] Do not stack-protect ifunc resolvers Nix
@ 2016-12-19 11:27 ` Nix
  2016-12-19 15:15 ` --enable-stack-protector for glibc, v10 Florian Weimer
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 11:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

With all the machinery we just added, we can easily arrange to work even
when the compiler passes in -fstack-protector automatically: all the
necessary bits of glibc are always compiled with -fno-stack-protector
now.

So tear out the check in configure, and add appropriate calls to
-fno-stack-protector in tests that need them (largely those that use
-nostdlib), since we don't yet have a __stack_chk_fail() that those
tests can rely upon.  (GCC often provides one, but we cannot rely on
this, especially not when bootstrapping.)

When stack protection is disabled, explicitly pass -fno-stack-protector
to everything, to stop a compiler hacked to enable it from inserting
calls to __stack_chk_fail via the PLT in every object file.

v2: No longer pass in -lssp to anything.
v5: Remove accidentally duplicated $(no_ssp)s.
v6: Small revisions following Mike Frysinger's review.
v9: Remove additional libc_cv_predef_stack_protector checks.
v10: Set STACK_PROTECTOR_LEVEL=0 and -fno-stack-protector when
     --disable-stack-protector.

	* configure.ac: Add check for unsupported stack-protection level.
	(libc_cv_predef_stack_protector): Remove.
	(no_ssp): New variable.
	(STACK_PROTECTOR_LEVEL): Set to zero when --disable-stack-protector.
	(stack_protector): Set to -fno-stack-protector similarly.
	(libc_cv_ld_gnu_indirect_function): Use it.
	(libc_cv_asm_set_directive): Likewise.
	(libc_cv_protected_data): Likewise.
	(libc_cv_z_combreloc): Likewise.
	(libc_cv_hashstyle): Likewise.
	(libc_cv_has_glob_dat): Likewise.
	(libc_cv_output_format): Likewise.
	(libc_cv_ehdr_start): Likewise.
	* aclocal.m4 (LIBC_TRY_LINK_STATIC): Likewise.
	(LIBC_LINKER_FEATURE): Likewise.
	(LIBC_COMPILER_BUILTIN_INLINED): Likewise.
---
 aclocal.m4   |  6 ++---
 configure.ac | 75 +++++++++++++++++++-----------------------------------------
 2 files changed, 27 insertions(+), 54 deletions(-)

diff --git a/aclocal.m4 b/aclocal.m4
index 3d64f77..6902155 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -141,7 +141,7 @@ int _start (void) { return 0; }
 int __start (void) { return 0; }
 $1
 EOF
-AS_IF([AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -o conftest
+AS_IF([AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp -o conftest
 		       conftest.c -static -nostartfiles -nostdlib
 		       1>&AS_MESSAGE_LOG_FD])],
       [$2], [$3])
@@ -226,7 +226,7 @@ if test x"$gnu_ld" = x"yes"; then
     cat > conftest.c <<EOF
 int _start (void) { return 42; }
 EOF
-    if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+    if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
 				$2 -nostdlib -nostartfiles
 				-fPIC -shared -o conftest.so conftest.c
 				1>&AS_MESSAGE_LOG_FD])
@@ -268,7 +268,7 @@ libc_compiler_builtin_inlined=no
 cat > conftest.c <<EOF
 int _start (void) { $2 return 0; }
 EOF
-if ! AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+if ! AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
 		     $3 -nostdlib -nostartfiles
 		     -S conftest.c -o - | fgrep "$1"
 		     1>&AS_MESSAGE_LOG_FD])
diff --git a/configure.ac b/configure.ac
index 2b58aa7..094bfe9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -651,11 +651,26 @@ elif test "$enable_stack_protector" = all && test "$libc_cv_ssp_all" = yes; then
 elif test "$enable_stack_protector" = strong && test "$libc_cv_ssp_strong" = yes; then
   stack_protector="-fstack-protector-strong"
   AC_DEFINE(STACK_PROTECTOR_LEVEL, 3)
+else
+  stack_protector="-fno-stack-protector"
+  AC_DEFINE(STACK_PROTECTOR_LEVEL, 0)
 fi
 AC_SUBST(libc_cv_ssp)
 AC_SUBST(stack_protector)
 AC_SUBST(no_stack_protector)
 
+if test -n "$stack_protector"; then
+  dnl Don't run configure tests with stack-protection on, to avoid problems with
+  dnl bootstrapping.
+  no_ssp=-fno-stack-protector
+else
+  no_ssp=
+
+  if test "$enable_stack_protector" != no; then
+    AC_MSG_ERROR([--enable-stack-protector=$enable_stack_protector specified, but specified level of stack protection is not supported by the compiler.])
+  fi
+fi
+
 # For the multi-arch option we need support in the assembler & linker.
 AC_CACHE_CHECK([for assembler and linker STT_GNU_IFUNC support],
 	       libc_cv_ld_gnu_indirect_function, [dnl
@@ -675,7 +690,7 @@ __start:
 EOF
 libc_cv_ld_gnu_indirect_function=no
 if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS \
-	    -nostartfiles -nostdlib \
+	    -nostartfiles -nostdlib $no_ssp \
 	    -o conftest conftest.S 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD; then
   # Do a link to see if the backend supports IFUNC relocs.
   $READELF -r conftest 1>&AS_MESSAGE_LOG_FD
@@ -1185,7 +1200,7 @@ extern int glibc_conftest_frobozz;
 void _start() { glibc_conftest_frobozz = 1; }
 EOF
 if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS \
-	    -nostartfiles -nostdlib \
+	    -nostartfiles -nostdlib $no_ssp \
 	    -o conftest conftest.s conftest1.c 1>&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD; then
   libc_cv_asm_set_directive=yes
 else
@@ -1202,12 +1217,12 @@ AC_CACHE_CHECK(linker support for protected data symbol,
 		int bar __attribute__ ((visibility ("protected"))) = 1;
 EOF
 		libc_cv_protected_data=no
-		if AC_TRY_COMMAND(${CC-cc} -nostdlib -nostartfiles -fPIC -shared conftest.c -o conftest.so); then
+		if AC_TRY_COMMAND(${CC-cc} -nostdlib -nostartfiles $no_ssp -fPIC -shared conftest.c -o conftest.so); then
 		  cat > conftest.c <<EOF
 		  extern int bar;
 		  int main (void) { return bar; }
 EOF
-		  if AC_TRY_COMMAND(${CC-cc} -nostdlib -nostartfiles conftest.c -o conftest conftest.so); then
+		  if AC_TRY_COMMAND(${CC-cc} -nostdlib -nostartfiles $no_ssp conftest.c -o conftest conftest.so); then
 		    libc_cv_protected_data=yes
 		  fi
 		fi
@@ -1329,7 +1344,7 @@ extern int mumble;
 int foo (void) { return bar (mumble); }
 EOF
 if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
-			-fPIC -shared -o conftest.so conftest.c
+			-fPIC -shared $no_ssp -o conftest.so conftest.c
 			-nostdlib -nostartfiles
 			-Wl,-z,combreloc 1>&AS_MESSAGE_LOG_FD])
 then
@@ -1367,7 +1382,7 @@ AC_CACHE_CHECK(for --hash-style option,
 cat > conftest.c <<EOF
 int _start (void) { return 42; }
 EOF
-if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
 			    -fPIC -shared -o conftest.so conftest.c
 			    -Wl,--hash-style=both -nostdlib 1>&AS_MESSAGE_LOG_FD])
 then
@@ -1439,7 +1454,7 @@ int foo (void) { return mumble; }
 EOF
 if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
 			-fPIC -shared -o conftest.so conftest.c
-			-nostdlib -nostartfiles
+			-nostdlib -nostartfiles $no_ssp
 			1>&AS_MESSAGE_LOG_FD])
 then
 dnl look for GLOB_DAT relocation.
@@ -1456,7 +1471,7 @@ AC_SUBST(libc_cv_has_glob_dat)
 
 AC_CACHE_CHECK(linker output format, libc_cv_output_format, [dnl
 if libc_cv_output_format=`
-${CC-cc} -nostartfiles -nostdlib -Wl,--print-output-format 2>&AS_MESSAGE_LOG_FD`
+${CC-cc} -nostartfiles -nostdlib $no_ssp -Wl,--print-output-format 2>&AS_MESSAGE_LOG_FD`
 then
   :
 else
@@ -1675,48 +1690,6 @@ if test $libc_cv_predef_fortify_source = yes; then
 fi
 AC_SUBST(CPPUNDEFS)
 
-dnl Check for silly hacked compilers inserting -fstack-protector.
-dnl This breaks badly for the early startup code we compile, since
-dnl the compiled code can refer to a magic machine-dependent location
-dnl for the canary value before we have sufficient setup for that to
-dnl work.  It's also questionable to build all of libc with this flag
-dnl even when you're doing that for most applications you build, since
-dnl libc's code is so heavily-used and performance-sensitive.  If we
-dnl ever really want to make that work, it should be enabled explicitly
-dnl in the libc build, not inherited from implicit compiler settings.
-AC_CACHE_CHECK([whether $CC implicitly enables -fstack-protector],
-	       libc_cv_predef_stack_protector, [
-AC_TRY_COMPILE([extern void foobar (char *);],
-	       [char large_array[2048]; foobar (large_array);], [
-libc_undefs=`$NM -u conftest.o |
-  LC_ALL=C $AWK '$1 == "U" { print $2 | "sort -u"; next } { exit(1) }' \
-    2>&AS_MESSAGE_LOG_FD` || {
-  AC_MSG_ERROR([confusing output from $NM -u])
-}
-echo >&AS_MESSAGE_LOG_FD "libc_undefs='$libc_undefs'"
-# On some architectures, there are architecture-specific undefined
-# symbols (resolved by the linker), so filter out unknown symbols.
-# This will fail to produce the correct result if the compiler
-# defaults to -fstack-protector but this produces an undefined symbol
-# other than __stack_chk_fail or __stack_chk_fail_local. However,
-# compilers like that have not been encountered in practice.
-libc_undefs=`echo "$libc_undefs" | \
-  egrep '^(foobar|__stack_chk_fail|__stack_chk_fail_local)$'`
-case "$libc_undefs" in
-foobar) libc_cv_predef_stack_protector=no ;;
-'__stack_chk_fail
-foobar'|'__stack_chk_fail_local
-foobar') libc_cv_predef_stack_protector=yes ;;
-*) AC_MSG_ERROR([unexpected symbols in test: $libc_undefs]) ;;
-esac],
-	       [AC_MSG_ERROR([test compilation failed])])
-])
-libc_extra_cflags=
-if test $libc_cv_predef_stack_protector = yes; then
-  libc_extra_cflags="$libc_extra_cflags -fno-stack-protector"
-fi
-libc_extra_cppflags=
-
 # Some linkers on some architectures support __ehdr_start but with
 # bugs.  Make sure usage of it does not create relocations in the
 # output (as the linker should resolve them all for us).
@@ -1726,7 +1699,7 @@ old_CFLAGS="$CFLAGS"
 old_LDFLAGS="$LDFLAGS"
 old_LIBS="$LIBS"
 CFLAGS="$CFLAGS -fPIC"
-LDFLAGS="$LDFLAGS -nostdlib -nostartfiles -shared"
+LDFLAGS="$LDFLAGS -nostdlib -nostartfiles -shared $no_ssp"
 LIBS=
 AC_LINK_IFELSE([AC_LANG_SOURCE([
 typedef struct {
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 03/15] Do not stack-protect ifunc resolvers.
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (12 preceding siblings ...)
  2016-12-19 11:26 ` [PATCH 01/15] Configury support for --enable-stack-protector Nix
@ 2016-12-19 11:27 ` Nix
  2016-12-21 14:17   ` Florian Weimer
  2016-12-19 11:27 ` [PATCH 07/15] Work even with compilers hacked to enable -fstack-protector by default Nix
                   ` (3 subsequent siblings)
  17 siblings, 1 reply; 35+ messages in thread
From: Nix @ 2016-12-19 11:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: fweimer

From: Nick Alcock <nick.alcock@oracle.com>

When dynamically linking, ifunc resolvers are called before TLS is
initialized, so they cannot be safely stack-protected.

We avoid disabling stack-protection on large numbers of files by
using __attribute__ ((__optimize__ ("-fno-stack-protector")))
to turn it off just for the resolvers themselves.  (We provide
the attribute even when statically linking, because we will later
use it elsewhere too.)

v4: New.
v5: Comment fix.
v6: Don't check for __attribute__((__optimize__)).
v7: Tiny context adjustments for revisions in earlier patches.
v9: Rebase atop new ifunc_resolver work.
v10: Spacing change in comment.

	* config.h.in (HAVE_CC_NO_STACK_PROTECTOR): New macro.
	* include/libc-symbols.h (inhibit_stack_protector): New macro.
	(__ifunc_resolver): Use it.
	* elf/ifuncdep2.c (foo1_ifunc): Add inhibit_stack_protector.
	(foo2_ifunc): Likewise.
	(foo3_ifunc): Likewise.
	* elf/ifuncmain6pie.c (foo_ifunc): Likewise.
	* elf/ifuncmain7.c (foo_ifunc): Likewise.
	* elf/ifuncmod1.c (foo_ifunc): Likewise.
	(foo_hidden_ifunc): Likewise.
	(foo_protected_ifunc): Likewise.
	* elf/ifuncmod5.c (foo_ifunc): Likewise.
	(foo_hidden_ifunc): Likewise.
	(foo_protected_ifunc): Likewise.
	* sysdeps/x86_64/ifuncmod8.c (foo_ifunc): Likewise.
	* sysdeps/generic/ifunc-sel.h (ifunc_sel): Likewise.
	(ifunc_one): Likewise.
	* sysdeps/nacl/nacl_interface_query.c [SHARED]
	(nacl_interface_query_ifunc): Likewise.
	* sysdeps/powerpc/ifunc-sel.h (ifunc_sel): Likewise.
	(ifunc_one): Likewise.
	* sysdeps/unix/make-syscalls.sh: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
	(gettimeofday_ifunc): Likewise.
	* sysdeps/unix/sysv/linux/x86/gettimeofday.c (gettimeofday_ifunc):
	Likewise.
	* sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c (getcpu_ifunc):
	Likewise.
---
 config.h.in                                    |  4 ++++
 configure.ac                                   |  1 +
 elf/ifuncdep2.c                                |  3 +++
 elf/ifuncmain6pie.c                            |  1 +
 elf/ifuncmain7.c                               |  1 +
 elf/ifuncmod1.c                                |  3 +++
 elf/ifuncmod5.c                                |  3 +++
 include/libc-symbols.h                         | 12 +++++++++++-
 sysdeps/generic/ifunc-sel.h                    |  2 ++
 sysdeps/nacl/nacl_interface_query.c            |  1 +
 sysdeps/powerpc/ifunc-sel.h                    |  2 ++
 sysdeps/unix/make-syscalls.sh                  |  1 +
 sysdeps/unix/sysv/linux/powerpc/gettimeofday.c |  1 +
 sysdeps/unix/sysv/linux/x86/gettimeofday.c     |  1 +
 sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c    |  1 +
 sysdeps/x86_64/ifuncmod8.c                     |  1 +
 16 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/config.h.in b/config.h.in
index d96ce0f..82f95a6 100644
--- a/config.h.in
+++ b/config.h.in
@@ -48,6 +48,10 @@
 /* Define if compiler accepts -ftree-loop-distribute-patterns.  */
 #undef  HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
 
+/* Define if compiler accepts -fno-stack-protector in an
+   __attribute__ ((__optimize__)).  */
+#undef	HAVE_CC_NO_STACK_PROTECTOR
+
 /* The level of stack protection in use for glibc as a whole.
    May be overridden on a file-by-file basis.  */
 #ifndef STACK_PROTECTOR_LEVEL
diff --git a/configure.ac b/configure.ac
index f131e97..2b58aa7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -639,6 +639,7 @@ stack_protector=
 no_stack_protector=
 if test "$libc_cv_ssp" = yes; then
   no_stack_protector="-fno-stack-protector -DSTACK_PROTECTOR_LEVEL=0"
+  AC_DEFINE(HAVE_CC_NO_STACK_PROTECTOR)
 fi
 
 if test "$enable_stack_protector" = yes && test "$libc_cv_ssp" = yes; then
diff --git a/elf/ifuncdep2.c b/elf/ifuncdep2.c
index 6e66d31..d87d61d 100644
--- a/elf/ifuncdep2.c
+++ b/elf/ifuncdep2.c
@@ -32,6 +32,7 @@ void * foo1_ifunc (void) __asm__ ("foo1");
 __asm__(".type foo1, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo1_ifunc (void)
 {
   return ifunc_sel (one, minus_one, zero);
@@ -41,6 +42,7 @@ void * foo2_ifunc (void) __asm__ ("foo2");
 __asm__(".type foo2, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo2_ifunc (void)
 {
   return ifunc_sel (minus_one, one, zero);
@@ -50,6 +52,7 @@ void * foo3_ifunc (void) __asm__ ("foo3");
 __asm__(".type foo3, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo3_ifunc (void)
 {
   return ifunc_sel (one, zero, minus_one);
diff --git a/elf/ifuncmain6pie.c b/elf/ifuncmain6pie.c
index 8478d4c..04faeb8 100644
--- a/elf/ifuncmain6pie.c
+++ b/elf/ifuncmain6pie.c
@@ -21,6 +21,7 @@ void * foo_ifunc (void) __asm__ ("foo");
 __asm__(".type foo, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_ifunc (void)
 {
   return ifunc_one (one);
diff --git a/elf/ifuncmain7.c b/elf/ifuncmain7.c
index 617a596..1e8f7ea 100644
--- a/elf/ifuncmain7.c
+++ b/elf/ifuncmain7.c
@@ -20,6 +20,7 @@ __asm__(".type foo, %gnu_indirect_function");
 
 static void *
 __attribute__ ((used))
+inhibit_stack_protector
 foo_ifunc (void)
 {
   return ifunc_one (one);
diff --git a/elf/ifuncmod1.c b/elf/ifuncmod1.c
index 0b61380..f0bf5fb 100644
--- a/elf/ifuncmod1.c
+++ b/elf/ifuncmod1.c
@@ -36,6 +36,7 @@ void * foo_ifunc (void) __asm__ ("foo");
 __asm__(".type foo, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_ifunc (void)
 {
   return ifunc_sel (one, minus_one, zero);
@@ -45,6 +46,7 @@ void * foo_hidden_ifunc (void) __asm__ ("foo_hidden");
 __asm__(".type foo_hidden, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_hidden_ifunc (void)
 {
   return ifunc_sel (minus_one, one, zero);
@@ -54,6 +56,7 @@ void * foo_protected_ifunc (void) __asm__ ("foo_protected");
 __asm__(".type foo_protected, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_protected_ifunc (void)
 {
   return ifunc_sel (one, zero, minus_one);
diff --git a/elf/ifuncmod5.c b/elf/ifuncmod5.c
index 0e65a63..5a95780 100644
--- a/elf/ifuncmod5.c
+++ b/elf/ifuncmod5.c
@@ -31,6 +31,7 @@ void * foo_ifunc (void) __asm__ ("foo");
 __asm__(".type foo, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_ifunc (void)
 {
   return ifunc_sel (one, minus_one, zero);
@@ -40,6 +41,7 @@ void * foo_hidden_ifunc (void) __asm__ ("foo_hidden");
 __asm__(".type foo_hidden, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_hidden_ifunc (void)
 {
   return ifunc_sel (minus_one, one, zero);
@@ -49,6 +51,7 @@ void * foo_protected_ifunc (void) __asm__ ("foo_protected");
 __asm__(".type foo_protected, %gnu_indirect_function");
 
 void *
+inhibit_stack_protector
 foo_protected_ifunc (void)
 {
   return ifunc_sel (one, zero, minus_one);
diff --git a/include/libc-symbols.h b/include/libc-symbols.h
index 1c91f2e..d2f3d3a 100644
--- a/include/libc-symbols.h
+++ b/include/libc-symbols.h
@@ -338,6 +338,16 @@ for linking")
 
 #define attribute_relro __attribute__ ((section (".data.rel.ro")))
 
+
+/* Used to disable stack protection in sensitive places, like ifunc
+   resolvers and early static TLS init.  */
+#ifdef HAVE_CC_NO_STACK_PROTECTOR
+# define inhibit_stack_protector \
+    __attribute__ ((__optimize__ ("-fno-stack-protector")))
+#else
+# define inhibit_stack_protector
+#endif
+
 /* The following macros are used for PLT bypassing within libc.so
    (and if needed other libraries similarly).
    First of all, you need to have the function prototyped somewhere,
@@ -739,7 +749,7 @@ for linking")
 
 /* Helper / base  macros for indirect function symbols.  */
 #define __ifunc_resolver(type_name, name, expr, arg, init, classifier)	\
-  classifier void *name##_ifunc (arg)					\
+  classifier inhibit_stack_protector void *name##_ifunc (arg)					\
   {									\
     init ();								\
     __typeof (type_name) *res = expr;					\
diff --git a/sysdeps/generic/ifunc-sel.h b/sysdeps/generic/ifunc-sel.h
index 6a27b69..1fff405 100644
--- a/sysdeps/generic/ifunc-sel.h
+++ b/sysdeps/generic/ifunc-sel.h
@@ -5,6 +5,7 @@
 extern int global;
 
 static inline void *
+inhibit_stack_protector
 ifunc_sel (int (*f1) (void), int (*f2) (void), int (*f3) (void))
 {
  switch (global)
@@ -19,6 +20,7 @@ ifunc_sel (int (*f1) (void), int (*f2) (void), int (*f3) (void))
 }
 
 static inline void *
+inhibit_stack_protector
 ifunc_one (int (*f1) (void))
 {
   return f1;
diff --git a/sysdeps/nacl/nacl_interface_query.c b/sysdeps/nacl/nacl_interface_query.c
index adf1dd4..dbaa88b 100644
--- a/sysdeps/nacl/nacl_interface_query.c
+++ b/sysdeps/nacl/nacl_interface_query.c
@@ -29,6 +29,7 @@ extern TYPE_nacl_irt_query nacl_interface_query_ifunc (void)
   asm ("nacl_interface_query");
 
 TYPE_nacl_irt_query
+inhibit_stack_protector
 nacl_interface_query_ifunc (void)
 {
   return &__nacl_irt_query;
diff --git a/sysdeps/powerpc/ifunc-sel.h b/sysdeps/powerpc/ifunc-sel.h
index ac589bd..bdb00bf 100644
--- a/sysdeps/powerpc/ifunc-sel.h
+++ b/sysdeps/powerpc/ifunc-sel.h
@@ -5,6 +5,7 @@
 extern int global;
 
 static inline void *
+inhibit_stack_protector
 ifunc_sel (int (*f1) (void), int (*f2) (void), int (*f3) (void))
 {
   register void *ret __asm__ ("r3");
@@ -32,6 +33,7 @@ ifunc_sel (int (*f1) (void), int (*f2) (void), int (*f3) (void))
 }
 
 static inline void *
+inhibit_stack_protector
 ifunc_one (int (*f1) (void))
 {
   register void *ret __asm__ ("r3");
diff --git a/sysdeps/unix/make-syscalls.sh b/sysdeps/unix/make-syscalls.sh
index 58d165e..123553c 100644
--- a/sysdeps/unix/make-syscalls.sh
+++ b/sysdeps/unix/make-syscalls.sh
@@ -287,6 +287,7 @@ while read file srcfile caller syscall args strong weak; do
 	(echo '#include <dl-vdso.h>'; \\
 	 echo 'extern void *${strong}_ifunc (void) __asm ("${strong}");'; \\
 	 echo 'void *'; \\
+	 echo 'inhibit_stack_protector'; \\
 	 echo '${strong}_ifunc (void)'; \\
 	 echo '{'; \\
 	 echo '  PREPARE_VERSION_KNOWN (symver, ${vdso_symver});'; \\
diff --git a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
index 16c00d7..46608ee 100644
--- a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
+++ b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
@@ -33,6 +33,7 @@
 #  undef __gettimeofday
 
 int
+inhibit_stack_protector
 __gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz)
 {
   return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
diff --git a/sysdeps/unix/sysv/linux/x86/gettimeofday.c b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
index c82452f..a419c4d 100644
--- a/sysdeps/unix/sysv/linux/x86/gettimeofday.c
+++ b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
@@ -24,6 +24,7 @@
 # include <errno.h>
 
 static int
+inhibit_stack_protector
 __gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
 {
   return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c b/sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c
index cbac4b3..8436f9d 100644
--- a/sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c
+++ b/sysdeps/unix/sysv/linux/x86_64/x32/getcpu.c
@@ -21,6 +21,7 @@
 void *getcpu_ifunc (void) __asm__ ("__getcpu");
 
 void *
+inhibit_stack_protector
 getcpu_ifunc (void)
 {
   PREPARE_VERSION (linux26, "LINUX_2.6", 61765110);
diff --git a/sysdeps/x86_64/ifuncmod8.c b/sysdeps/x86_64/ifuncmod8.c
index c004367..7c06562 100644
--- a/sysdeps/x86_64/ifuncmod8.c
+++ b/sysdeps/x86_64/ifuncmod8.c
@@ -28,6 +28,7 @@ foo_impl (float x)
 }
 
 void *
+inhibit_stack_protector
 foo_ifunc (void)
 {
   __m128i xmm = _mm_set1_epi32 (-1);
-- 
2.10.1.208.gbec66bc

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (14 preceding siblings ...)
  2016-12-19 11:27 ` [PATCH 07/15] Work even with compilers hacked to enable -fstack-protector by default Nix
@ 2016-12-19 15:15 ` Florian Weimer
  2016-12-19 15:18   ` Chris Metcalf
  2016-12-19 15:54   ` Nix
  2016-12-20  8:30 ` Florian Weimer
  2016-12-21 17:26 ` Florian Weimer
  17 siblings, 2 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-19 15:15 UTC (permalink / raw)
  To: Nix, libc-alpha, Chris Metcalf

On 12/19/2016 12:15 PM, Nix wrote:
> Here, as promised, is version 10 of the stack-protected glibc patch,
> incorporating all review comments to date (unless I missed some).
>
> Like the last, it's against glibc head as of Sat Nov 26, bf469f0ce98.
>
> Tested with these flag combinations on {i686,x86_64)-pc-linux-gnu (with GCC
> 6.2.1-20161118, binutils 2.27.0.20160920, and kernel headers for v4.7.4):

Thanks for posting the updated series.

I tested this with  build-many-glibcs.py, and ran into an ICE on tile*. 
Filed as:

   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78862

The good news is that ABI and PLT checking passes everywhere.  Yay!

The explicit_bzero test case (string/tst-xbzero-opt.c) now fails.  This 
is probably  test case bug, for which I will post a patch shortly.

I'll go through the updates series again, probably tomorrow.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-19 15:15 ` --enable-stack-protector for glibc, v10 Florian Weimer
@ 2016-12-19 15:18   ` Chris Metcalf
  2016-12-19 15:54   ` Nix
  1 sibling, 0 replies; 35+ messages in thread
From: Chris Metcalf @ 2016-12-19 15:18 UTC (permalink / raw)
  To: Florian Weimer, Nix, libc-alpha

On 12/19/2016 10:15 AM, Florian Weimer wrote:
> I tested this with  build-many-glibcs.py, and ran into an ICE on tile*. Filed as:
>
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78862

Thanks!  We'll take a look.

-- 
Chris Metcalf, Mellanox Technologies
http://www.mellanox.com

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-19 15:15 ` --enable-stack-protector for glibc, v10 Florian Weimer
  2016-12-19 15:18   ` Chris Metcalf
@ 2016-12-19 15:54   ` Nix
  1 sibling, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-19 15:54 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha, Chris Metcalf

On 19 Dec 2016, Florian Weimer spake thusly:
> The good news is that ABI and PLT checking passes everywhere.  Yay!

*FI*nally. :)

> The explicit_bzero test case (string/tst-xbzero-opt.c) now fails. This
> is probably test case bug, for which I will post a patch shortly.

Yeah, I decided that discretion was the better part of valour and
rebasing at this stage was unwise, so I never ran this against a tree
with the explicit_bzero stuff in it.

> I'll go through the updates series again, probably tomorrow.

Thanks!

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (15 preceding siblings ...)
  2016-12-19 15:15 ` --enable-stack-protector for glibc, v10 Florian Weimer
@ 2016-12-20  8:30 ` Florian Weimer
  2016-12-20 19:04   ` Nix
  2016-12-21 17:26 ` Florian Weimer
  17 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-20  8:30 UTC (permalink / raw)
  To: Nix, libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> Here, as promised, is version 10 of the stack-protected glibc patch,
> incorporating all review comments to date (unless I missed some).

I will start committing patches from this series shortly.

There are some open questions (more on that later), but I think the 
overall design is sound, and it will simplify matters if we reduce the 
size of the series somewhat.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-20  8:30 ` Florian Weimer
@ 2016-12-20 19:04   ` Nix
  0 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-20 19:04 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 20 Dec 2016, Florian Weimer outgrape:

> On 12/19/2016 12:15 PM, Nix wrote:
>> Here, as promised, is version 10 of the stack-protected glibc patch,
>> incorporating all review comments to date (unless I missed some).
>
> I will start committing patches from this series shortly.

Thank you! Time for a celebratory drink at this end, I think :)
(Earl Grey, naturally.)

> There are some open questions (more on that later), but I think the
> overall design is sound, and it will simplify matters if we reduce the
> size of the series somewhat.

Oh, please.

As long as you avoid committing the configury patch near the end, the
net effect of the series on generated code (so long as the result
compiles!) should be zero, so fairly risk-free. (I hope.)

-- 
NULL && (void)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE
  2016-12-19 11:25 ` [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE Nix
@ 2016-12-21 13:36   ` Florian Weimer
  0 siblings, 0 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 13:36 UTC (permalink / raw)
  To: Nix, libc-alpha

[-- Attachment #1: Type: text/plain, Size: 456 bytes --]

On 12/19/2016 12:15 PM, Nix wrote:
> From: Nick Alcock <nick.alcock@oracle.com>
>
> With stack protection enabled, these files have external symbol
> references for the first time, so the fact that they are not compiled
> with -fPIE and are then linked into a -pie binary starts to hurt.
>
> v10: New.
>
> 	* sysdeps/x86_64/Makefile (CFLAGS-tst-quad1pie.c): Add PIE-ccflag.
> 	(CFLAGS-tst-quad2pie.c): Likewise.

This is what I committed.

Thanks,
Florian

[-- Attachment #2: tst-pie.patch --]
[-- Type: text/x-patch, Size: 1033 bytes --]

x86_64: tst-quad1pie, tst-quad2pie: compile with -fPIE [BZ #7065]

With stack protection enabled, these files have external symbol
references for the first time, so the fact that they are not compiled
with -fPIE and are then linked into a -pie binary starts to hurt.

2016-12-21  Nick Alcock <nick.alcock@oracle.com>

	[BZ #7065]
	* sysdeps/x86_64/Makefile [$(subdir) = elf]
	(CFLAGS-tst-quad1pie.c, CFLAGS-tst-quad2pie.c): Add $(PIE-ccflag).

diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
index 6d99284..5f25893 100644
--- a/sysdeps/x86_64/Makefile
+++ b/sysdeps/x86_64/Makefile
@@ -49,6 +49,9 @@ extra-test-objs += tst-quadmod1pie.o tst-quadmod2pie.o
 $(objpfx)tst-quad1pie: $(objpfx)tst-quadmod1pie.o
 $(objpfx)tst-quad2pie: $(objpfx)tst-quadmod2pie.o
 
+CFLAGS-tst-quad1pie.c = $(PIE-ccflag)
+CFLAGS-tst-quad2pie.c = $(PIE-ccflag)
+
 tests += tst-audit3 tst-audit4 tst-audit5 tst-audit6 tst-audit7 tst-audit10
 test-extras += tst-audit4-aux tst-audit10-aux
 extra-test-objs += tst-audit4-aux.o tst-audit10-aux.o

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 13/15] Move all tests out of csu.
  2016-12-19 11:26 ` [PATCH 13/15] Move all tests out of csu Nix
@ 2016-12-21 13:36   ` Florian Weimer
  0 siblings, 0 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 13:36 UTC (permalink / raw)
  To: Nix, libc-alpha

[-- Attachment #1: Type: text/plain, Size: 929 bytes --]

On 12/19/2016 12:15 PM, Nix wrote:
> From: Nick Alcock <nick.alcock@oracle.com>
>
> Stack-protection on .o files in csu/ must be suppressed for the sake of
> library startup code.  This also suppresses stack-protection in tests
> (which are also covered by CFLAGS-.o), though this is neither necessary
> nor desirable.
>
> So impose the rule that .o files in csu/ are necessarily C startup code,
> and move the few tests in there into misc/ instead.
>
> v10: New.
>
> 	* csu/Makefile (tests): Move this...
> 	(tests-static): ... and this...
> 	* misc/Makefile (tests): ... to here...
> 	(tests-static): ... and here.
>         * csu/tst-empty.c: Move to...
>         * misc/tst-empty.c: ... here.
>         * csu/tst-atomic.c: Move to...
>         * misc/tst-atomic.c: ... here.
>         * csu/tst-atomic-long.c: Move to...
>         * misc/tst-atomic-long.c: ... here.

I committed this variant of the patch.

Thanks,
Florian


[-- Attachment #2: csu-tests.patch --]
[-- Type: text/x-patch, Size: 2930 bytes --]

Move all tests out of the csu subdirectory

Stack-protection on .o files in csu/ must be suppressed for the sake of
library startup code.  This also suppresses stack-protection in tests
(which are also covered by CFLAGS-.o), though this is neither necessary
nor desirable.

So impose the rule that .o files in csu/ are necessarily C startup code,
and move the few tests in there into misc/ instead.

2016-12-21  Nick Alcock <nick.alcock@oracle.com>
	    Florian Weimer  <fweimer@redhat.com>

	[BZ #7065]
	* csu/Makefile (tests): Empty assignment and document it.
	(tests-static): Remove.
	* csu/tst-empty.c: Move to...
	* misc/tst-empty.c: ... here.  Switch to new test driver.
	* csu/tst-atomic.c: Move to...
	* misc/tst-atomic.c: ... here.  Switch to new test driver.
	* csu/tst-atomic-long.c: Move to...
	* misc/tst-atomic-long.c: ... here.
	* misc/Makefile (tests): Add tst-empty, tst-atomic,
	tst-atomic-long.
	(tests-static): Add tst-empty.

diff --git a/csu/Makefile b/csu/Makefile
index 31e8bb9..3d23f13 100644
--- a/csu/Makefile
+++ b/csu/Makefile
@@ -42,8 +42,9 @@ install-lib = $(start-installed-name) g$(start-installed-name) $(csu-dummies)
 generated += version-info.h
 before-compile += $(objpfx)version-info.h
 
-tests := tst-empty tst-atomic tst-atomic-long
-tests-static := tst-empty
+# No tests are allowed in the csu/ subdirectory because the startup
+# code is compiled with special flags.
+tests =
 
 ifeq (yes,$(build-shared))
 extra-objs += S$(start-installed-name) gmon-start.os
diff --git a/misc/Makefile b/misc/Makefile
index 3d2ebb8..d241dae 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -79,7 +79,10 @@ gpl2lgpl := error.c error.h
 tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
 	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 \
 	 tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
-	 tst-preadvwritev tst-preadvwritev64 tst-makedev
+	 tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
+	 tst-atomic tst-atomic-long
+tests-static := tst-empty
+
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-error1-mem.out
 endif
diff --git a/csu/tst-atomic-long.c b/misc/tst-atomic-long.c
similarity index 100%
rename from csu/tst-atomic-long.c
rename to misc/tst-atomic-long.c
diff --git a/csu/tst-atomic.c b/misc/tst-atomic.c
diff --git a/csu/tst-atomic.c b/misc/tst-atomic.c
similarity index 99%
rename from csu/tst-atomic.c
rename to misc/tst-atomic.c
+++ b/misc/tst-atomic.c
@@ -636,5 +636,4 @@ do_test (void)
   return ret;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>
diff --git a/csu/tst-empty.c b/misc/tst-empty.c
similarity index 58%
rename from csu/tst-empty.c
rename to misc/tst-empty.c
+++ b/misc/tst-empty.c
@@ -5,5 +5,4 @@ do_test (void)
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
+#include <support/test-driver.c>

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 02/15] Initialize the stack guard earlier when linking statically.
  2016-12-19 11:26 ` [PATCH 02/15] Initialize the stack guard earlier when linking statically Nix
@ 2016-12-21 14:16   ` Florian Weimer
  2016-12-21 20:15     ` Florian Weimer
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 14:16 UTC (permalink / raw)
  To: Nix; +Cc: libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> From: Nick Alcock <nick.alcock@oracle.com>
>
> The address of the stack canary is stored in a per-thread variable,
> which means that we must ensure that the TLS area is intialized before
> calling any -fstack-protector'ed functions.  For dynamically linked
> applications, we ensure this (in a later patch) by disabling
> -fstack-protector for the whole dynamic linker, but for static
> applications the AT_ENTRY address is called directly by the kernel, so
> we must deal with the problem differently.
>
> So split out the part of pthread initialization that sets up the TCB
> (and, more generally, the TLS area) into a separate function (twice --
> there is one implementation in libpthread.a, and another outside it for
> programs that do not link with libpthread), then call it at
> initialization time.  Call that, and move the stack guard initialization
> above the DL_SYSDEP_OSCHECK hook, which if set will probably call
> functions which are stack-protected (it does on Linux and NaCL too).
> We also move apply_irel() up, so that we can still safely call functions
> that require ifuncs while in __pthread_initialize_tcb_internal()
> (though if stack-protection is enabled we still have to avoid calling
> functions that are not stack-protected at this stage).

I'm changing this to call __libc_setup_tls directly.  This functions is 
in csu/ and thus automatically exempted from stack protection.  There is 
no need to go indirectly through a definition in nptl/.

(The old approach stems from the days where TLS was optional.)

Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 03/15] Do not stack-protect ifunc resolvers.
  2016-12-19 11:27 ` [PATCH 03/15] Do not stack-protect ifunc resolvers Nix
@ 2016-12-21 14:17   ` Florian Weimer
  2016-12-22 22:40     ` Nix
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 14:17 UTC (permalink / raw)
  To: Nix, libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> diff --git a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
> index 16c00d7..46608ee 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
> +++ b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
> @@ -33,6 +33,7 @@
>  #  undef __gettimeofday
>
>  int
> +inhibit_stack_protector
>  __gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz)
>  {
>    return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
> diff --git a/sysdeps/unix/sysv/linux/x86/gettimeofday.c b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
> index c82452f..a419c4d 100644
> --- a/sysdeps/unix/sysv/linux/x86/gettimeofday.c
> +++ b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
> @@ -24,6 +24,7 @@
>  # include <errno.h>
>
>  static int
> +inhibit_stack_protector
>  __gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
>  {
>    return INLINE_SYSCALL (gettimeofday, 2, tv, tz);

These are functions *returned* by IFUNC resolvers, and therefore can be 
compiled with stack protector enabled.  I will drop those.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector.
  2016-12-19 11:26 ` [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector Nix
@ 2016-12-21 14:18   ` Florian Weimer
  2016-12-21 14:25     ` Szabolcs Nagy
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 14:18 UTC (permalink / raw)
  To: Nix, libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> +CFLAGS-.o += $(call elide-stack-protector,.o,$(elide-routines.os))
> +CFLAGS-.op += $(call elide-stack-protector,.op,$(elide-routines.os))
> +CFLAGS-.og += $(call elide-stack-protector,.og,$(elide-routines.os))
> +CFLAGS-.os += $(call elide-stack-protector,.os,$(all-rtld-routines))

.og is gone, as mentioned before.

I think .op is a shared build and thus needs to be linked the same way 
as .os.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector.
  2016-12-19 11:15 ` [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector Nix
@ 2016-12-21 14:18   ` Florian Weimer
  0 siblings, 0 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 14:18 UTC (permalink / raw)
  To: Nix, libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> +CFLAGS-.og += $(no-stack-protector)

The .og pattern is no more.  I will drop those changes from the patch.

Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector.
  2016-12-21 14:18   ` Florian Weimer
@ 2016-12-21 14:25     ` Szabolcs Nagy
  2016-12-21 14:57       ` Florian Weimer
  0 siblings, 1 reply; 35+ messages in thread
From: Szabolcs Nagy @ 2016-12-21 14:25 UTC (permalink / raw)
  To: Florian Weimer, Nix, libc-alpha; +Cc: nd

On 21/12/16 14:18, Florian Weimer wrote:
> On 12/19/2016 12:15 PM, Nix wrote:
>> +CFLAGS-.o += $(call elide-stack-protector,.o,$(elide-routines.os))
>> +CFLAGS-.op += $(call elide-stack-protector,.op,$(elide-routines.os))
>> +CFLAGS-.og += $(call elide-stack-protector,.og,$(elide-routines.os))
>> +CFLAGS-.os += $(call elide-stack-protector,.os,$(all-rtld-routines))
> 
> .og is gone, as mentioned before.
> 
> I think .op is a shared build and thus needs to be linked the same way as .os.

Makeconfig seems to set up .op like .o (other than -DPROF and -pg flags)
and unlike .os (which has -DPIC -DSHARED -fPIC)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector.
  2016-12-21 14:25     ` Szabolcs Nagy
@ 2016-12-21 14:57       ` Florian Weimer
  0 siblings, 0 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 14:57 UTC (permalink / raw)
  To: Szabolcs Nagy, Nix, libc-alpha; +Cc: nd

On 12/21/2016 03:25 PM, Szabolcs Nagy wrote:
> On 21/12/16 14:18, Florian Weimer wrote:
>> On 12/19/2016 12:15 PM, Nix wrote:
>>> +CFLAGS-.o += $(call elide-stack-protector,.o,$(elide-routines.os))
>>> +CFLAGS-.op += $(call elide-stack-protector,.op,$(elide-routines.os))
>>> +CFLAGS-.og += $(call elide-stack-protector,.og,$(elide-routines.os))
>>> +CFLAGS-.os += $(call elide-stack-protector,.os,$(all-rtld-routines))
>>
>> .og is gone, as mentioned before.
>>
>> I think .op is a shared build and thus needs to be linked the same way as .os.
>
> Makeconfig seems to set up .op like .o (other than -DPROF and -pg flags)
> and unlike .os (which has -DPIC -DSHARED -fPIC)

Right, I reverted the .op part.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so.
  2016-12-19 11:25 ` [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so Nix
@ 2016-12-21 15:05   ` Florian Weimer
  2016-12-22 22:41     ` Nix
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 15:05 UTC (permalink / raw)
  To: Nix, libc-alpha

On 12/19/2016 12:15 PM, Nix wrote:
> From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> We use the same assembler-macro trick we use to de-PLTize
> compiler-generated libcalls to memcpy and memset to redirect
> __stack_chk_fail to __stack_chk_fail_local.
>
> v5: New.
> v6: Only do it within the shared library: with __stack_chk_fail_local
>     in libc_pic.a now we don't need to worry about calls from inside
>     other routines in libc_nonshared.a any more.
> v8: Merge #ifdef blocks.
> v10: Use STACK_PROTECTOR_LEVEL to avoid renaming in files where
>      stack-protection is suppressed.
>
> 	* sysdeps/generic/symbol-hacks.h [STACK_PROTECTOR_LEVEL &&
> 	STACK_PROTECTOR_LEVEL > 0] (__stack_chk_fail): Add internal alias.

I'm going to fold this into the previous commit.

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: --enable-stack-protector for glibc, v10
  2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
                   ` (16 preceding siblings ...)
  2016-12-20  8:30 ` Florian Weimer
@ 2016-12-21 17:26 ` Florian Weimer
  17 siblings, 0 replies; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 17:26 UTC (permalink / raw)
  To: Nix, libc-alpha

I pushed my current state to the fw/stack-protector branch on Sourceware.

I added a NEWS entry to the final commit:

* Most of glibc can now be built with the stack smashing protector enabled.
   It is recommended to build glibc with --enable-stack-protector=strong.
   Implemented by Nick Alcock (Oracle).

It is basically what Nick posted the last time.  I squashed two small 
commits related to PLT avoidance together, and incorporated the 
__libc_setup_tls cleanup.

Nick, if you want to give this a final test spin, that's fine. 
Alternatively, I could push this later today.  I have tested it on 
various architectures, with and without --enable-stack-protector=strong, 
and also with compile-time-testing with build-many-glibcs.py (twice as 
well, with --enable-stack-protector=strong hacked into the script).

Thanks,
Florian

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 02/15] Initialize the stack guard earlier when linking statically.
  2016-12-21 14:16   ` Florian Weimer
@ 2016-12-21 20:15     ` Florian Weimer
  2016-12-22 22:38       ` Nix
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2016-12-21 20:15 UTC (permalink / raw)
  To: Nix; +Cc: libc-alpha

[-- Attachment #1: Type: text/plain, Size: 298 bytes --]

On 12/21/2016 03:16 PM, Florian Weimer wrote:
> I'm changing this to call __libc_setup_tls directly.  This functions is
> in csu/ and thus automatically exempted from stack protection.  There is
> no need to go indirectly through a definition in nptl/.

I should have attached the patch.

Florian


[-- Attachment #2: early-static-tls.patch --]
[-- Type: text/x-patch, Size: 7360 bytes --]

Initialize the stack guard earlier when linking statically [BZ #7065]

The address of the stack canary is stored in a per-thread variable,
which means that we must ensure that the TLS area is intialized before
calling any -fstack-protector'ed functions.  For dynamically linked
applications, we ensure this (in a later patch) by disabling
-fstack-protector for the whole dynamic linker, but for static
applications, the AT_ENTRY address is called directly by the kernel, so
we must deal with the problem differently.

In static appliations, __libc_setup_tls performs the TCB setup and TLS
initialization, so this commit arranges for it to be called early and
unconditionally.  The call (and the stack guard initialization) is
before the DL_SYSDEP_OSCHECK hook, which if set will probably call
functions which are stack-protected (it does on Linux and NaCL too).  We
also move apply_irel up, so that we can still safely call functions that
require ifuncs while in __libc_setup_tls (though if stack-protection is
enabled we still have to avoid calling functions that are not
stack-protected at this stage).

2016-12-21  Nick Alcock  <nick.alcock@oracle.com>
	    Florian Weimer  <fweimer@redhat.com>

	[BZ #7065]
	Initialize the stack guard earlier when linking statically.
	* sysdeps/generic/ldsodefs.h (__libc_setup_tls)
	(__pthread_initialize_minimal): Declare.
	* csu/libc-start.c (__pthread_initialize_minimal): Remove
	declaration.
	(LIBC_START_MAIN): Call __libc_setup_tls early and directly.  Move
	stack canary and apply_irel initialization up.  Call
	__pthread_initialize_minimal only if linked in.
	* csu/libc-tls.c (__libc_setup_tls): Replace arguments with their
	constant values.
	(__pthread_initialize_minimal): Remove.
	* nptl/nptl-init.c (__libc_setup_tls): Remove declaration.
	(__pthread_initialize_minimal_internal): Do not call
	__libc_setup_tls.

diff --git a/csu/libc-start.c b/csu/libc-start.c
index 99c040a..cc59073 100644
--- a/csu/libc-start.c
+++ b/csu/libc-start.c
@@ -29,7 +29,6 @@ extern int __libc_multiple_libcs;
 #include <tls.h>
 #ifndef SHARED
 # include <dl-osinfo.h>
-extern void __pthread_initialize_minimal (void);
 # ifndef THREAD_SET_STACK_GUARD
 /* Only exported for architectures that don't store the stack guard canary
    in thread local area.  */
@@ -175,22 +174,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
         }
     }
 
-# ifdef DL_SYSDEP_OSCHECK
-  if (!__libc_multiple_libcs)
-    {
-      /* This needs to run to initiliaze _dl_osversion before TLS
-	 setup might check it.  */
-      DL_SYSDEP_OSCHECK (__libc_fatal);
-    }
-# endif
-
   /* Perform IREL{,A} relocations.  */
   apply_irel ();
 
-  /* Initialize the thread library at least a bit since the libgcc
-     functions are using thread functions if these are available and
-     we need to setup errno.  */
-  __pthread_initialize_minimal ();
+  /* The stack guard goes into the TCB, so initialize it early.  */
+  __libc_setup_tls ();
 
   /* Set up the stack checker's canary.  */
   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
@@ -200,6 +188,19 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   __stack_chk_guard = stack_chk_guard;
 # endif
 
+# ifdef DL_SYSDEP_OSCHECK
+  if (!__libc_multiple_libcs)
+    {
+      /* This needs to run to initiliaze _dl_osversion before TLS
+	 setup might check it.  */
+      DL_SYSDEP_OSCHECK (__libc_fatal);
+    }
+# endif
+
+  /* Initialize libpthread if linked in.  */
+  if (__pthread_initialize_minimal != NULL)
+    __pthread_initialize_minimal ();
+
   /* Set up the pointer guard value.  */
   uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
 							 stack_chk_guard);
diff --git a/csu/libc-tls.c b/csu/libc-tls.c
index 8f92234..454f165 100644
--- a/csu/libc-tls.c
+++ b/csu/libc-tls.c
@@ -102,14 +102,14 @@ init_static_tls (size_t memsz, size_t align)
 }
 
 void
-__libc_setup_tls (size_t tcbsize, size_t tcbalign)
+__libc_setup_tls (void)
 {
   void *tlsblock;
   size_t memsz = 0;
   size_t filesz = 0;
   void *initimage = NULL;
   size_t align = 0;
-  size_t max_align = tcbalign;
+  size_t max_align = TCB_ALIGNMENT;
   size_t tcb_offset;
   const ElfW(Phdr) *phdr;
 
@@ -142,9 +142,9 @@ __libc_setup_tls (size_t tcbsize, size_t tcbalign)
      _dl_allocate_tls_storage (in elf/dl-tls.c) does using __libc_memalign
      and dl_tls_static_align.  */
   tcb_offset = roundup (memsz + GL(dl_tls_static_size), max_align);
-  tlsblock = __sbrk (tcb_offset + tcbsize + max_align);
+  tlsblock = __sbrk (tcb_offset + TLS_INIT_TCB_SIZE + max_align);
 #elif TLS_DTV_AT_TP
-  tcb_offset = roundup (tcbsize, align ?: 1);
+  tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1);
   tlsblock = __sbrk (tcb_offset + memsz + max_align
 		     + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
   tlsblock += TLS_PRE_TCB_SIZE;
@@ -215,12 +215,3 @@ __libc_setup_tls (size_t tcbsize, size_t tcbalign)
 
   init_static_tls (memsz, MAX (TLS_TCB_ALIGN, max_align));
 }
-
-/* This is the minimal initialization function used when libpthread is
-   not used.  */
-void
-__attribute__ ((weak))
-__pthread_initialize_minimal (void)
-{
-  __libc_setup_tls (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN);
-}
diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c
index 0fd54a0..8494b26 100644
--- a/nptl/nptl-init.c
+++ b/nptl/nptl-init.c
@@ -70,10 +70,6 @@ int __have_futex_clock_realtime;
 static const char nptl_version[] __attribute_used__ = VERSION;
 
 
-#ifndef SHARED
-extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
-#endif
-
 #ifdef SHARED
 static
 #else
@@ -288,18 +284,6 @@ static bool __nptl_initial_report_events __attribute_used__;
 void
 __pthread_initialize_minimal_internal (void)
 {
-#ifndef SHARED
-  /* Unlike in the dynamically linked case the dynamic linker has not
-     taken care of initializing the TLS data structures.  */
-  __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
-
-  /* We must prevent gcc from being clever and move any of the
-     following code ahead of the __libc_setup_tls call.  This function
-     will initialize the thread register which is subsequently
-     used.  */
-  __asm __volatile ("");
-#endif
-
   /* Minimal initialization of the thread descriptor.  */
   struct pthread *pd = THREAD_SELF;
   __pthread_initialize_pids (pd);
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 34d7ec1..bb67840 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -994,6 +994,17 @@ extern size_t _dl_count_modids (void) internal_function attribute_hidden;
 /* Calculate offset of the TLS blocks in the static TLS block.  */
 extern void _dl_determine_tlsoffset (void) internal_function attribute_hidden;
 
+#ifndef SHARED
+/* Set up the TCB for statically linked applications.  This is called
+   early during startup because we always use TLS (for errno and the
+   stack protector, among other things).  */
+void __libc_setup_tls (void);
+
+/* Initialization of libpthread for statically linked applications.
+   If libpthread is not linked in, this is an empty function.  */
+void __pthread_initialize_minimal (void) weak_function;
+#endif
+
 /* Allocate memory for static TLS block (unless MEM is nonzero) and dtv.  */
 extern void *_dl_allocate_tls (void *mem) internal_function;
 rtld_hidden_proto (_dl_allocate_tls)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 02/15] Initialize the stack guard earlier when linking statically.
  2016-12-21 20:15     ` Florian Weimer
@ 2016-12-22 22:38       ` Nix
  0 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-22 22:38 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 21 Dec 2016, Florian Weimer verbalised:

> On 12/21/2016 03:16 PM, Florian Weimer wrote:
>> I'm changing this to call __libc_setup_tls directly.  This functions is
>> in csu/ and thus automatically exempted from stack protection.  There is
>> no need to go indirectly through a definition in nptl/.

Agreed, that's much better: that indirection was always a bit nasty and
I'm glad we can do without it now. The code motion in LIBC_START_MAIN is
the delicate part, and that still looks fine.

-- 
NULL && (void)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 03/15] Do not stack-protect ifunc resolvers.
  2016-12-21 14:17   ` Florian Weimer
@ 2016-12-22 22:40     ` Nix
  0 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-22 22:40 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 21 Dec 2016, Florian Weimer outgrape:

> On 12/19/2016 12:15 PM, Nix wrote:
>> diff --git a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
>> index 16c00d7..46608ee 100644
>> --- a/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
>> +++ b/sysdeps/unix/sysv/linux/powerpc/gettimeofday.c
>> @@ -33,6 +33,7 @@
>>  #  undef __gettimeofday
>>
>>  int
>> +inhibit_stack_protector
>>  __gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz)
>>  {
>>    return INLINE_VSYSCALL (gettimeofday, 2, tv, tz);
>> diff --git a/sysdeps/unix/sysv/linux/x86/gettimeofday.c b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
>> index c82452f..a419c4d 100644
>> --- a/sysdeps/unix/sysv/linux/x86/gettimeofday.c
>> +++ b/sysdeps/unix/sysv/linux/x86/gettimeofday.c
>> @@ -24,6 +24,7 @@
>>  # include <errno.h>
>>
>>  static int
>> +inhibit_stack_protector
>>  __gettimeofday_syscall (struct timeval *tv, struct timezone *tz)
>>  {
>>    return INLINE_SYSCALL (gettimeofday, 2, tv, tz);
>
> These are functions *returned* by IFUNC resolvers, and therefore can
> be compiled with stack protector enabled. I will drop those.

I think I was a bit gettimeofday-trigger-happy when I wrote this.
If it still works on x86-32 with -fstack-protector-all, you're clearly
right :)

-- 
NULL && (void)

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so.
  2016-12-21 15:05   ` Florian Weimer
@ 2016-12-22 22:41     ` Nix
  0 siblings, 0 replies; 35+ messages in thread
From: Nix @ 2016-12-22 22:41 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 21 Dec 2016, Florian Weimer uttered the following:

> On 12/19/2016 12:15 PM, Nix wrote:
>> From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>>
>> We use the same assembler-macro trick we use to de-PLTize
>> compiler-generated libcalls to memcpy and memset to redirect
>> __stack_chk_fail to __stack_chk_fail_local.
>>
>> v5: New.
>> v6: Only do it within the shared library: with __stack_chk_fail_local
>>     in libc_pic.a now we don't need to worry about calls from inside
>>     other routines in libc_nonshared.a any more.
>> v8: Merge #ifdef blocks.
>> v10: Use STACK_PROTECTOR_LEVEL to avoid renaming in files where
>>      stack-protection is suppressed.
>>
>> 	* sysdeps/generic/symbol-hacks.h [STACK_PROTECTOR_LEVEL &&
>> 	STACK_PROTECTOR_LEVEL > 0] (__stack_chk_fail): Add internal alias.
>
> I'm going to fold this into the previous commit.

I was tempted but thought that it might be a bit presumptuous for
a newbie like me to preempt Adhemerval :)

-- 
NULL && (void)

^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2016-12-22 22:41 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-19 11:15 --enable-stack-protector for glibc, v10 Nix
2016-12-19 11:15 ` [PATCH 04/15] Mark all machinery needed in early static-link init as -fno-stack-protector Nix
2016-12-21 14:18   ` Florian Weimer
2016-12-19 11:15 ` [PATCH 08/15] Add a hidden __stack_chk_fail_local alias to libc.so Nix
2016-12-19 11:15 ` [PATCH 06/15] Prevent the rtld mapfile computation from dragging in __stack_chk_fail* Nix
2016-12-19 11:25 ` [PATCH 15/15] Enable -fstack-protector=* when requested by configure Nix
2016-12-19 11:25 ` [PATCH 10/15] Link a non-libc-using test with -fno-stack-protector Nix
2016-12-19 11:25 ` [PATCH 11/15] Drop explicit stack-protection of pieces of the system Nix
2016-12-19 11:25 ` [PATCH 14/15] tst-quad1pie, tst-quad2pie: compile with -fPIE Nix
2016-12-21 13:36   ` Florian Weimer
2016-12-19 11:25 ` [PATCH 09/15] De-PLTize __stack_chk_fail internal calls within libc.so Nix
2016-12-21 15:05   ` Florian Weimer
2016-12-22 22:41     ` Nix
2016-12-19 11:26 ` [PATCH 05/15] Compile the entire dynamic linker with -fno-stack-protector Nix
2016-12-21 14:18   ` Florian Weimer
2016-12-21 14:25     ` Szabolcs Nagy
2016-12-21 14:57       ` Florian Weimer
2016-12-19 11:26 ` [PATCH 02/15] Initialize the stack guard earlier when linking statically Nix
2016-12-21 14:16   ` Florian Weimer
2016-12-21 20:15     ` Florian Weimer
2016-12-22 22:38       ` Nix
2016-12-19 11:26 ` [PATCH 12/15] Do not stack-protect sigreturn stubs Nix
2016-12-19 11:26 ` [PATCH 13/15] Move all tests out of csu Nix
2016-12-21 13:36   ` Florian Weimer
2016-12-19 11:26 ` [PATCH 01/15] Configury support for --enable-stack-protector Nix
2016-12-19 11:27 ` [PATCH 03/15] Do not stack-protect ifunc resolvers Nix
2016-12-21 14:17   ` Florian Weimer
2016-12-22 22:40     ` Nix
2016-12-19 11:27 ` [PATCH 07/15] Work even with compilers hacked to enable -fstack-protector by default Nix
2016-12-19 15:15 ` --enable-stack-protector for glibc, v10 Florian Weimer
2016-12-19 15:18   ` Chris Metcalf
2016-12-19 15:54   ` Nix
2016-12-20  8:30 ` Florian Weimer
2016-12-20 19:04   ` Nix
2016-12-21 17:26 ` Florian Weimer

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 read-only IMAP folder(s) and NNTP newsgroup(s).