Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 411439

Summary: [hardened] app-editors/emacs-23.4-r1 segfault in unexec with hardened-sources-3.3*
Product: Gentoo Linux Reporter: Nick Wallingford <nick>
Component: Current packagesAssignee: Emacs project <emacs>
Status: RESOLVED UPSTREAM    
Severity: normal CC: flameeyes, hardened-kernel+disabled, pageexec, spender
Priority: Normal    
Version: unspecified   
Hardware: AMD64   
OS: Linux   
URL: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11398
See Also: https://bugs.gentoo.org/show_bug.cgi?id=417037
Whiteboard:
Package list:
Runtime testing required: ---
Attachments: Complete build log.
emerge --info
kernel config for hardened-sources-3.3.1-r1
Diff between 3.2.12 and 3.3.1
Patch for app-editors/emacs-23.4-r1
Patch for app-editors/emacs-vcs-24.0.97 (applies to BZR trunk version too)

Description Nick Wallingford 2012-04-10 07:31:25 UTC
app-editors/emacs will no longer build on hardened ~amd64 with sys-devel/gcc-4.6.2. This package compiled correctly with this setup when emacs-23.4-r1 was recently released; during today's 'emerge -e @world' the build failed, and will no longer build.

Reproducible: Always

Steps to Reproduce:
1. 'emerge app-editors/emacs'
2.
3.
Actual Results:  
segfault during build:

Dumping under the name emacs
**************************************************
Warning: Your system has a gap between BSS and the
heap (15045480 bytes).  This usually means that exec-shield
or something similar is in effect.  The dump may
fail because of this.  See the section about
exec-shield in etc/PROBLEMS for more information.
**************************************************
make[1]: *** [bootstrap-emacs] Segmentation fault
make[1]: Leaving directory `/var/tmp/portage/app-editors/emacs-23.4-r1/work/emacs-23.4/src'
make: *** [src] Error 2

Expected Results:  
Should have compiled and installed correctly.

All app-editors/emacs USE flags are disabled via /etc/portage/package.use
Comment 1 Nick Wallingford 2012-04-10 07:32:12 UTC
Created attachment 308365 [details]
Complete build log.
Comment 2 Nick Wallingford 2012-04-10 07:33:40 UTC
Created attachment 308367 [details]
emerge --info
Comment 3 Ulrich Müller gentoo-dev 2012-04-10 08:15:19 UTC
Does it build with the recipe from bug 345001 comment #5?

Does building of the latest pretest version (emacs-vcs-24.0.95) succeed? If not, what has changed on your system (kernel version, etc.) since the last successful build of 23.4?
Comment 4 Nick Wallingford 2012-04-10 11:06:46 UTC
Succeeds with /proc/sys/kernel/randomize_va_space=0
Fails with /proc/sys/kernel/randomize_va_space=1
Fails with /proc/sys/kernel/randomize_va_space=2 (default)

app-editors/emacs-vcs-24.0.95 fails with /proc/sys/kernel/randomize_va_space=2

The current kernel is hardend-sources-3.3.1. (fails)
Fails with hardened-sources-3.3.0
Fails with hardened-sources-3.3.1-r1
Succeeds with hardened-sources-3.2.11

The kernel .config did not change in a relevant fashion between 3.3.0 and 3.2.11.
Comment 5 Ulrich Müller gentoo-dev 2012-04-10 12:03:58 UTC
(In reply to comment #4)
> The current kernel is hardend-sources-3.3.1. (fails)
> Fails with hardened-sources-3.3.0
> Fails with hardened-sources-3.3.1-r1
> Succeeds with hardened-sources-3.2.11
> 
> The kernel .config did not change in a relevant fashion between 3.3.0 and
> 3.2.11.

Thanks for testing; this may help to locate the problem. Could you attach your .config for 3.2.11 and 3.3.0?

@hardened-kernel: Any idea what could cause this?
Comment 6 Nick Wallingford 2012-04-10 13:38:57 UTC
Created attachment 308395 [details]
kernel config for hardened-sources-3.3.1-r1
Comment 7 Ulrich Müller gentoo-dev 2012-04-11 01:47:45 UTC
(In reply to comment #4)
> Fails with hardened-sources-3.3.1-r1
> Succeeds with hardened-sources-3.2.11

I can reproduce above two cases.

The problem disappears (i.e. building of Emacs succeeds) when I unset the CONFIG_PAX_RANDMMAP kernel config option for hardened-sources-3.3.1-r1.
Comment 8 Ulrich Müller gentoo-dev 2012-04-11 01:52:24 UTC
Created attachment 308469 [details, diff]
Diff between 3.2.12 and 3.3.1

The relevant change between linux-3.2.12-hardened and linux-3.3.1-hardened-r1 that causes the bug seems to be in fs/binfmt_elf.c, see attached diff.

Reverting this change fixes the problem for me.
Comment 9 Anthony Basile gentoo-dev 2012-04-29 21:36:23 UTC
(In reply to comment #8)
> Created attachment 308469 [details, diff] [details, diff]
> Diff between 3.2.12 and 3.3.1
> 
> The relevant change between linux-3.2.12-hardened and
> linux-3.3.1-hardened-r1 that causes the bug seems to be in fs/binfmt_elf.c,
> see attached diff.
> 
> Reverting this change fixes the problem for me.

@pageexec  Looks like your change to the elf_brk += code broke something.  Maybe because of the page alignment?
Comment 10 PaX Team 2012-04-30 10:17:52 UTC
can you guys try a more recent version please? i've fixed bugs in that code (just compare what's in PaX now vs. what was posted here), so i'd rather not chase ghosts ;).
Comment 11 Anthony Basile gentoo-dev 2012-04-30 11:20:35 UTC
(In reply to comment #10)
> can you guys try a more recent version please? i've fixed bugs in that code
> (just compare what's in PaX now vs. what was posted here), so i'd rather not
> chase ghosts ;).

Okay, I'll prepare the latest and put them on the tree for testing.  I'll ping you guys back when its ready.
Comment 12 Anthony Basile gentoo-dev 2012-05-01 01:43:23 UTC
(In reply to comment #11)
> (In reply to comment #10)
> > can you guys try a more recent version please? i've fixed bugs in that code
> > (just compare what's in PaX now vs. what was posted here), so i'd rather not
> > chase ghosts ;).
> 
> Okay, I'll prepare the latest and put them on the tree for testing.  I'll
> ping you guys back when its ready.

No joy.  I'm hitting the same issue on both x86 and amd64 with hardened-sources-3.3.4 (which is only on my overlay for now).  hs-3.3.4 = grsecurity-2.9-3.3.4-201204272006.  I have not tested with the 2.6.32 or 3.2.16 patches.
Comment 13 PaX Team 2012-05-02 00:32:44 UTC
i've debugged the problem and it's a bug in emacs. it wants to create a memory dump of its address space without actually looking at what memory ranges are available with what access rights. due to recentish changes in PaX the gap between the end of the main executable's data section and the start of the brk heap is mapped with PROT_NONE rights, so no access is allowed and this is where emacs fails. PaX also ignores the ADDR_NO_RANDOMIZE personality bit because it's a security bug itself, so emacs can't use it as a workaround either. the fix for this (in gentoo) can go two ways: either disable RANDMMAP on emacs (requires makefile changes i guess) or as a last resort, i can add a special case hack to the brk randomization code to use PROT_READ rights for the gap when this personality bit is set (have yet to test it, but i think it'd get emacs's memory dumper code to work). your turn ;).
Comment 14 Ulrich Müller gentoo-dev 2012-05-02 02:13:35 UTC
It's certainly not a bug in Emacs. That unexec code has worked for many years without any problem.

(In reply to comment #13)
> PaX also ignores the ADDR_NO_RANDOMIZE personality bit [...]

That's where the bug is. Emacs sets ADDR_NO_RANDOMIZE for a purpose. This is only done for dumping the final executable, not during normal operation.
Comment 15 PaX Team 2012-05-02 10:14:08 UTC
(In reply to comment #14)
> It's certainly not a bug in Emacs. That unexec code has worked for many
> years without any problem.

it worked by chance, not because it was/is correct. you know, programs with exploitable bugs in them work fine 'for many years without any problem' as well...

> (In reply to comment #13)
> > PaX also ignores the ADDR_NO_RANDOMIZE personality bit [...]
> 
> That's where the bug is. Emacs sets ADDR_NO_RANDOMIZE for a purpose. This is
> only done for dumping the final executable, not during normal operation.

FYI, ADDR_NO_RANDOMIZE was added as a workaround to fix userland bugs like what emacs has (the first bug is about assuming a particular address space layout that no standard has ever guaranteed, the second bug is that emacs doesn't use the kernel provided interface to discover its own address space layout). ignoring ADDR_NO_RANDOMIZE is the right decision because it destroys the security provided by randomization. last but not least, PaX allows the turning off of ASLR - just not at runtime. did you give it a try?
Comment 16 Ulrich Müller gentoo-dev 2012-05-02 11:13:06 UTC
(In reply to comment #15)
> PaX allows the turning off of ASLR - just not at runtime. did you give it
> a try?

As I said before, Emacs doesn't need it at runtime, only at build time. But how should the Emacs build system turn off ASLR, if ADDR_NO_RANDOMIZE isn't available?
Comment 17 PaX Team 2012-05-02 11:44:22 UTC
(In reply to comment #16)
> (In reply to comment #15)
> > PaX allows the turning off of ASLR - just not at runtime. did you give it
> > a try?
> 
> As I said before, Emacs doesn't need it at runtime, only at build time. But
> how should the Emacs build system turn off ASLR, if ADDR_NO_RANDOMIZE isn't
> available?

you misunderstood 'runtime'. i meant it as 'when the program is running' and emacs does enable this personality bit when it is running, i.e., at runtime. on the other hand the PaX approach is to not let programs do this exactly because an exploit could then do the same as well. instead, the binary file must be properly marked (chpax/paxctl/etc) so that the kernel can disable ASLR on process startup. but all that is still a workaround for what is fundamentally a bug in emacs's memory dumper code, the proper fix should be in there.
Comment 18 Ulrich Müller gentoo-dev 2012-05-17 09:43:35 UTC
Looks like dumping succeeds after doing "paxctl -r src/temacs". However, the final emacs executable would inherit the "r" flag, and looks like paxctl cannot output existing flags in a machine readable way that could be used for restoring them later. (It doesn't even have an option to suppress its chattiness and always outputs its copyright...) I've tried "paxctl -z", but that will clear out all flags, whereas the set was nonempty before.
Comment 19 PaX Team 2012-05-17 10:48:08 UTC
(In reply to comment #18)
> Looks like dumping succeeds after doing "paxctl -r src/temacs". However, the
> final emacs executable would inherit the "r" flag,

does emacs (after dumping) work with ASLR at all?

> and looks like paxctl cannot output existing flags in a machine readable way
> that could be used for restoring them later.

paxctl -vQ /bin/cat 2>/dev/null| cut -f 4 -d ' ' | sed s/-//g

> (It doesn't even have an option to suppress its chattiness and always outputs
> its copyright...)

paxctl ... 2>/dev/null

> I've tried "paxctl -z", but that will clear out all flags, whereas the set was nonempty before.

the binutils default (it's in the manpage too) is equivalent to: paxctl -zex
Comment 20 Ulrich Müller gentoo-dev 2012-05-17 14:32:14 UTC
(In reply to comment #19)
> (In reply to comment #18)
> > Looks like dumping succeeds after doing "paxctl -r src/temacs". However, the
> > final emacs executable would inherit the "r" flag,
> 
> does emacs (after dumping) work with ASLR at all?

I haven't noticed any problems, at least.

Basically, Emacs' build system first compiles the C sources to make the temacs binary. Then it executes "./temacs --batch --load loadup dump" which will load the standard lisp files and then dump the final emacs binary.

Disabling RANDMMAP for temacs ("paxctl -r temacs") before that step should be enough; restoring the flags for the final emacs binary ("paxctl -zex emacs") seems to be harmless.

> the binutils default (it's in the manpage too) is equivalent to: paxctl -zex

Is it guaranteed that this default will be stable across systems and releases?


(In reply to comment #17)
> [...] all that is still a workaround for what is fundamentally a bug in
> emacs's memory dumper code, the proper fix should be in there.

I had forwarded this to Emacs upstream <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11398>, but they immediately passed the buck back to this (downstream) bug.
Comment 21 PaX Team 2012-05-17 20:38:17 UTC
(In reply to comment #20)
> > the binutils default (it's in the manpage too) is equivalent to: paxctl -zex
> 
> Is it guaranteed that this default will be stable across systems and
> releases?

yes, it's been this way since 2004 or so ;).

> I had forwarded this to Emacs upstream
> <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11398>, but they immediately
> passed the buck back to this (downstream) bug.

what does 'Set bug forwarded-to-address to' mean in their lingo? i don't see any explanation/discussion at all, so i'm wondering what's going on there...
Comment 22 Ulrich Müller gentoo-dev 2012-05-17 21:23:09 UTC
(In reply to comment #21)
> what does 'Set bug forwarded-to-address to' mean in their lingo? i don't see
> any explanation/discussion at all, so i'm wondering what's going on there...

Sorry, that was a misunderstanding on my side. See <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11398#13> for clarification from upstream.
Comment 23 Ulrich Müller gentoo-dev 2012-05-23 19:57:31 UTC
Created attachment 312873 [details, diff]
Patch for app-editors/emacs-23.4-r1

Could you please try if attached patch for Emacs 23.4 fixes the problem?
Comment 24 Ulrich Müller gentoo-dev 2012-05-23 19:58:49 UTC
Created attachment 312875 [details, diff]
Patch for app-editors/emacs-vcs-24.0.97 (applies to BZR trunk version too)
Comment 25 PaX Team 2012-05-23 22:33:24 UTC
> [...]unexec will fail because memory regions of executables are randomized.

note that the problem is not with randomization per se but the inaccessible gap between the bss and the brk regions. it could be of a constant size (and hence brk heap addresses would not be randomized) yet it'd still trigger the emacs memory dumper bug.
Comment 26 Ulrich Müller gentoo-dev 2012-05-24 10:28:09 UTC
(In reply to comment #25)
> > [...]unexec will fail because memory regions of executables are randomized.
> 
> note that the problem is not with randomization per se but the inaccessible
> gap between the bss and the brk regions. it could be of a constant size (and
> hence brk heap addresses would not be randomized) yet it'd still trigger the
> emacs memory dumper bug.

## On grsecurity/PaX systems, unexec will fail due to a gap between
## the bss section and the heap.  This can be prevented by disabling
## memory randomization in temacs with "paxctl -r".  See bug#11398.

Better?
Comment 27 PaX Team 2012-05-25 09:08:06 UTC
(In reply to comment #26)
> ## On grsecurity/PaX systems, unexec will fail due to a gap between
> ## the bss section and the heap.  This can be prevented by disabling
> ## memory randomization in temacs with "paxctl -r".  See bug#11398.
> 
> Better?

definitely, thanks ;)
Comment 28 Ulrich Müller gentoo-dev 2012-05-26 16:15:26 UTC
Should be fixed in emacs-23.4-r2 (with USE=pax_kernel).

Please test.
Comment 29 Ulrich Müller gentoo-dev 2012-06-04 17:46:24 UTC
Patch with paxctl workaround committed upstream:
<http://bzr.savannah.gnu.org/lh/emacs/trunk/revision/108471>
Comment 30 Ulrich Müller gentoo-dev 2012-06-26 19:37:29 UTC
*** Bug 423693 has been marked as a duplicate of this bug. ***
Comment 31 Ulrich Müller gentoo-dev 2012-06-29 14:36:18 UTC
*** Bug 423693 has been marked as a duplicate of this bug. ***
Comment 32 bsod 2013-02-12 16:26:30 UTC
I have exactly the same problem with emacs-24.2 (latest stable on hardened profile) and sys-kernel/hardened-sources-3.7.4-r1 and sys-devel/gcc-4.6.3. 

**************************************************
Warning: Your system has a gap between BSS and the
heap (15854248 bytes).  This usually means that exec-shield
or something similar is in effect.  The dump may
fail because of this.  See the section about
exec-shield in etc/PROBLEMS for more information.
**************************************************

Workaround is to disable randomize_va_space for the compile step and reenable it afterwards.

echo "0" > /proc/sys/kernel/randomize_va_space 
emerge emacs
echo "1" > /proc/sys/kernel/randomize_va_space
Comment 33 Ulrich Müller gentoo-dev 2013-02-12 17:07:45 UTC
(In reply to comment #32)
> I have exactly the same problem with emacs-24.2 (latest stable on hardened
> profile) and sys-kernel/hardened-sources-3.7.4-r1 and sys-devel/gcc-4.6.3. 

Please file a new bug report, including build.log and emerge --info.
Comment 34 bsod 2013-02-12 17:49:27 UTC
(In reply to comment #33)
> (In reply to comment #32)
> > I have exactly the same problem with emacs-24.2 (latest stable on hardened
> > profile) and sys-kernel/hardened-sources-3.7.4-r1 and sys-devel/gcc-4.6.3. 
> 
> Please file a new bug report, including build.log and emerge --info.

See bug 456970.