Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20150205052259.GI23507@brightrain.aerifal.cx>
Date: Thu, 5 Feb 2015 00:22:59 -0500
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: GNU Emacs LD_PRELOAD build hack

On Mon, Feb 02, 2015 at 10:54:07PM -0500, Rich Felker wrote:
> The right solution: As I discussed on the emacs-devel list nearly a
> year ago, the right solution is to get rid of the non-portable code in
> emacs, dumping the lisp heap and its data (rather than the whole
> program) to a file and either mmapping it at runtime (and possibly
> relocating pointers in it, if the new location it's loaded at differs)
> or converting it to a C source file that's compiled and linked and for
> which the (static or dynamic) linker can perform relocations at
> link/load time. This solution also solves a number of other serious
> issues related to the dumper, including its incompatibility with PIE
> binaries.

Apparently since the discussion last year, the emacs folks went
forward with one of their proposed fixes -- not the best possible one,
but a good one nonetheless. These changes aren't in any release and
won't be for quite a while I expect, but using emacs git master, I was
able to build successfully with the attached patch and no hacks.

> So, here's an alternate solution.
> 
> The hack: The basic trick is that we need to satisfy emacs assumptions
> about malloc, but only at build (dumping) time, not permanently. My
> first thought was to build emacs in the presence of a modified musl
> libc.so whose malloc never uses mmap (issue 1) and never reclaims gaps
> at the edge of writable mappings (issue 2), but then I realized we
> could achieve the same thing without having to build a custom libc.so
> at package-build time by exploiting LD_PRELOAD.

Unfortunately there was another invalid assumption emacs was making
that I missed, which only came up when I tried to build on 64-bit:
under some conditions, it actually passes the pre-dump objects
obtained from malloc to the post-dump realloc/free functions. This
results in horrible heap-structure corruption and I have no idea
how/why it's working on glibc since it should break there too. Anyway,
discussing this on emacs-devel led to the much better solution using
git master, but I do also have a fixed based on the previously
reported LD_PRELOAD hack; it just depends on patching emacs not to
pass these pre-dump pointers to realloc/free. I'm attaching that patch
too in case anyone is interested.

Attached files:

emacs_alloc_invalid_frees.diff is the patch to supplement the
LD_PRELOAD hack on emacs-24.x.

emacs-master-musl.diff is the (lazy) patch for emacs git master
(presently commit 4188e3cc2bc69e75d4387b369e72e89fecc46a86) to make it
build on musl. It's not acceptable for upstream at this time because
the changes made are mostly unconditional. If anyone is willing to put
this into a form where it could be submitted upstream, I would very
much appreciate it; otherwise, distros packaging emacs can just use
the patch as-is or with minor changes.

Rich

View attachment "emacs-master-musl.diff" of type "text/plain" (2295 bytes)

View attachment "emacs_alloc_invalid_frees.diff" of type "text/plain" (1259 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.