Re: diet 0.33 segfaults

Felix von Leitner <[email protected]> Thu, 29 Mar 2018 01:56:30 +0200
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Hi Martin,

> I trying to build dietlibc 0.33 on nixos. I unpacked it, cd into it and
> ran make, but make fails here:

> bin-x86_64/diet gcc -D__dietlibc__ -Os -fstrict-aliasing
> -momit-leaf-frame-pointer -mfancy-math-387 -W -Wall -Wextra
> -Wchar-subscripts -Wmissing-prototypes -Wmissing-declarations
> -Wno-switch -Wno-unused -Wredundant-decls -o bin-x86_64/elftrunc
> contrib/elftrunc.c
> make: *** [Makefile:303: bin-x86_64/elftrunc] Segmentation fault

> Trying to run just diet has the same result.

> $ bin-x86_64/diet
> Segmentation fault

Yes, that is what triggered this error.
diet is built and linked against dietlibc during the build, so this step
ensures that the build fails if dietlibc fails to link and run a simple
program.

The instruction that crashes for you is

  4001b8:       64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
  4001bf:       00 00 

This is part of setting up thread local storage. The fs segment register
is used to point to a data structure on x86-64 that contains the stack
cookie and is preceded by the thread-local storage variables. What you
see here is code emitted by gcc that uses the stack cookie. gcc does
this if you compile with -fstack-protector.

However, the fs: register is set up using this code in stackgap.c:

  arch_prctl(ARCH_SET_FS, mainthread);

That is a syscall that shows up in strace. Your diet-i binary segfaults
on that instruction before calling this. Something is very wrong here.

Obviously you can't use that data structure before arch_prctl has been
called. That's why dietlibc compiles stackgap without -fstack-protector.
But some braindead distros change their gcc to always do
-fstack-protector.

That leads to the behavior we observe here.

To counteract this, dietlibc's Makefile contains this:

593 # WANT_SSP
594 # This facepalm brought to you by: Ubuntu!
595 $(PICODIR)/stackgap.o: EXTRACFLAGS:=-fno-stack-protector
596 $(OBJDIR)/stackgap.o: EXTRACFLAGS:=-fno-stack-protector -fno-pie -DNDEBUG

This was added early in version 0.33. You should not have seen this this
behavior. Maybe your distro vendor decided to patch gcc so it ignores
-fno-stack-protector. That would be really bad. If that is true, I see
no way to fix it on my side.

Please try the dietlibc version from CVS to see if that helps.
If it does not, then you may have to file a bug with your Linux distro
people.

Felix