unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Rich Felker <dalias@libc.org>
Cc: dmantipov@yandex.ru, emacs-devel@gnu.org
Subject: Re: Dumper problems and a possible solutions
Date: Wed, 25 Jun 2014 22:18:16 +0300	[thread overview]
Message-ID: <83vbropzlz.fsf@gnu.org> (raw)
In-Reply-To: <20140625190333.GZ179@brightrain.aerifal.cx>

> Date: Wed, 25 Jun 2014 15:03:33 -0400
> From: Rich Felker <dalias@libc.org>
> Cc: dmantipov@yandex.ru, emacs-devel@gnu.org
> 
> > > > Is it possible to provide our own implementation of sbrk that
> > > > allocates memory from some large static array?
> > > 
> > > That's exactly the hack I described which I'm using right now. But
> > > since I didn't implement a free-like operation and since
> > > load_charset_map_from_file allocates >700k every time it's called, I
> > > had to make the static array 400MB.
> > 
> > That's not a problem, because those 700K are free'd before the next
> > one is allocated.  And in any case, they are all free'd before we call
> > unexec.  Just implement sbrk for negative increment.  The Windows port
> 
> But load_charset_map_from_file doesn't call an sbrk-like interface; it
> calls (indirectly) xmalloc and xfree. So there's at least some
> nontrivial glue that goes in between.

Sorry, you are right.  What you need is non-toy malloc implementation
that is willing to work with your replacement sbrk.  Then support for
negative increments in sbrk isn't required.

But I still don't understand how you get to 400MB.  It's not that we
allocate hundreds of those 700K tables for charsets.  Do you have an
explanation for this?

> > If supporting deallocation in such an sbrk isn't feasible, how about
> > using gmalloc, as an malloc replacement before dumping?
> 
> I suspect it's a lot of work to wire up gmalloc to (1) avoid
> interposing on the malloc/free/etc. names, (2) use the static mini-brk
> buffer, (3) only allocate from the mini-brk buffer before dumping
> (otherwise pass to real malloc), but still check realloc/free calls
> after dumping and handle the case where the old memory was in the
> mini-brk.

Sorry, I don't see the difficulty.  Just make malloc/realloc/free be
pointers that point to gmalloc's implementation before dumping, and to
the libc implementation after it.  You may need some #define to rename
malloc to some other symbol, to avoid name clashes.  Am I missing
something?

> What seems easier, and what I tried, is writing a completely naive
> malloc with a single freelist that's linear-searched on malloc and
> which does not support coalescing free chunks. But I think my
> implementation has some bugs still, because it's not working.

The advantage of gmalloc is that it works out of the box.

> > > I would be reasonably happy with this solution (at least it would fix
> > > the problems I'm experiencing), but I don't think it's as elegant as
> > > fixing the portability problem completely by getting rid of the need
> > > to dump executable binary files and instead dumping a C array.
> > 
> > But it's conceptually much simpler and reliable.  That's "elegant" in
> > my book, when such hairy stuff is concerned.
> 
> No, it's less reliable. See my other posts in the thread about what
> happens if you have other libraries linked and they do nontrivial
> things prior to dumping (e.g. from static ctors).

But in those other posts I thought we agreed that whatever those ctors
do is irrelevant, as the dumped Emacs cannot possibly use what they
allocate, and those ctors will be invoked again in the dumped Emacs.

> As an example (I think I mentioned this earlier), if you static
> link, musl libc is remembering the clock_gettime vdso pointer from
> the pre-dump state and attempting to use it later (which is not
> valid because the kernel maps it at a random address).

You need to fix your libc to detect that it is re-exec'ed, and
re-initialize this stuff.

> > > And it doesn't fix the fact that you can't build a PIE emacs.
> > 
> > Why is that important?
> 
> Since emacs is processing lots of potentially untrusted data, PIE
> hardening may be beneficial for hardening against vulnerabilities
> where an attacker would otherwise be able to perform arbitrary code
> execution as the user running emacs. I'm not aware of such
> vulnerabilities, but being that I found things that look suspiciously
> like use-after-free while reading the allocator-related code, I
> wouldn't be surprised if they exist.

Sounds like a secondary requirement to me.  We should first make the
dumping work along these lines, and worry about bonuses later.



  reply	other threads:[~2014-06-25 19:18 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-24 17:19 Dumper problems and a possible solutions Rich Felker
2014-06-24 19:27 ` Stefan Monnier
2014-06-24 19:40   ` Rich Felker
2014-06-24 20:24     ` Stefan Monnier
2014-06-24 21:15       ` Rich Felker
2014-06-24 21:37         ` Stefan Monnier
2014-06-25 18:03 ` Dmitry Antipov
2014-06-25 18:08   ` Rich Felker
2014-06-25 18:30     ` Dmitry Antipov
2014-06-25 18:36       ` Rich Felker
2014-06-25 18:36       ` Eli Zaretskii
2014-06-25 18:41     ` Eli Zaretskii
2014-06-26  0:16     ` Stephen J. Turnbull
2014-06-25 18:20   ` Eli Zaretskii
2014-06-25 18:32     ` Rich Felker
2014-06-25 18:49       ` Eli Zaretskii
2014-06-25 19:03         ` Rich Felker
2014-06-25 19:18           ` Eli Zaretskii [this message]
2014-06-25 19:57             ` Rich Felker
2014-06-25 20:15               ` Eli Zaretskii
2014-06-25 20:34                 ` Rich Felker
2014-06-26  2:44                   ` Eli Zaretskii
2014-06-26  4:28                     ` Rich Felker
2014-06-26 15:02                       ` Eli Zaretskii
2014-06-25 20:11             ` Stefan Monnier
2014-06-25 20:06           ` Stefan Monnier
2014-06-25 20:24             ` Rich Felker
2014-06-25 21:43               ` Stefan Monnier
2014-06-25 22:07                 ` Rich Felker
2014-06-25 23:04                   ` Paul Eggert
2014-06-25 23:21                     ` Rich Felker
2014-06-25 23:05                   ` Stefan Monnier
2014-06-25 23:19                     ` Rich Felker
2014-06-26  3:02                   ` Dmitry Antipov
2014-06-26  4:14                     ` Rich Felker
2014-06-26  4:32                       ` Dmitry Antipov
2014-06-26 11:49                         ` Rich Felker
2014-06-26 15:03                         ` Eli Zaretskii
2014-06-26 15:10                           ` Rich Felker
2014-06-25 22:33               ` Andreas Schwab
2014-06-25 20:53       ` Samuel Bronson
2014-06-25 21:24         ` Rich Felker
2014-06-25 18:38   ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83vbropzlz.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=dalias@libc.org \
    --cc=dmantipov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).