public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sstewartgallus00 at mylangara dot bc.ca" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug nptl/17214] Expose a clone variant that shares stacks instead of jumping to a new one
Date: Mon, 26 Jan 2015 22:33:00 -0000	[thread overview]
Message-ID: <bug-17214-131-ZxFzAJABnf@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-17214-131@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=17214

--- Comment #15 from Steven Stewart-Gallus <sstewartgallus00 at mylangara dot bc.ca> ---
It occurs to me that maybe clone with vfork could be exposed in a safe manner
along the lines of the following (although GLibc would probably implement it in
assembly directly).

__attribute__((noinline)) __attribute__((noclone))
__attribute__((no_sanitize_address)) static pid_t safe_vfork(
    int (*volatile f)(void *), void *volatile arg)
{
    __atomic_signal_fence(__ATOMIC_SEQ_CST);

    pid_t child = vfork();
    if (0 == child)
        _Exit(f(arg));

    return child;
}

Aside from the weirdness of vfork this should be no less safer than something
along the lines of the following:

__attribute__((noinline)) __attribute__((noclone))
__attribute__((no_sanitize_address)) static pid_t safe_vclone(
    int volatile clone_flags, int (*volatile f)(void *), void *volatile arg)
{
    long maybe_page_size = sysconf(_SC_PAGE_SIZE);
    assert(maybe_page_size >= 0);

    long maybe_stack_min_size = sysconf(_SC_THREAD_STACK_MIN);
    assert(maybe_stack_min_size >= 0);

    size_t page_size = maybe_page_size;
    size_t stack_min_size = maybe_stack_min_size;

    /* We need an extra page for signals */
    size_t stack_size = stack_min_size + page_size;

    size_t stack_and_guard_size = page_size + stack_size + page_size;
    void *child_stack = mmap(
        0, stack_and_guard_size, PROT_READ | PROT_WRITE,
        MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN | MAP_STACK, -1, 0);
    if (0 == child_stack)
        return -1;

    /* Guard pages are shared between the stacks */
    if (-1 == mprotect((char *)child_stack, page_size, PROT_NONE))
        goto on_err;

    if (-1 == mprotect((char *)child_stack + page_size + stack_size,
                       page_size, PROT_NONE))
        goto on_err;

    void *stack_start = (char *)child_stack + page_size + stack_size;

    __atomic_signal_fence(__ATOMIC_SEQ_CST);

    pid_t child =
        clone(f, stack_start, clone_flags | CLONE_VM | CLONE_VFORK, arg);
    if (-1 == child)
        goto on_err;

    munmap(child_stack, stack_and_guard_size);

    return child;

on_err:
    ;
    int errnum = errno;
    munmap(child_stack, stack_and_guard_size);
    errno = errnum;
    return -1;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.


      parent reply	other threads:[~2015-01-26 22:33 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-29 21:23 [Bug nptl/17214] New: Expose a function to reset the PID cache sstewartgallus00 at mylangara dot bc.ca
2014-07-30  4:07 ` [Bug nptl/17214] " carlos at redhat dot com
2014-07-30 17:39 ` sstewartgallus00 at mylangara dot bc.ca
2014-07-30 17:41 ` sstewartgallus00 at mylangara dot bc.ca
2014-07-31 23:25 ` carlos at redhat dot com
2014-08-01 21:59 ` sstewartgallus00 at mylangara dot bc.ca
2014-08-26  4:38 ` bugdal at aerifal dot cx
2014-08-26 18:11 ` sstewartgallus00 at mylangara dot bc.ca
2014-08-26 18:31 ` sstewartgallus00 at mylangara dot bc.ca
2014-10-30 22:01 ` rickyz at chromium dot org
2014-12-18  0:15 ` jld at mozilla dot com
2014-12-18 23:58 ` [Bug nptl/17214] Expose a clone variant that shares stacks instead of jumping to a new one sstewartgallus00 at mylangara dot bc.ca
2014-12-19  0:13 ` sstewartgallus00 at mylangara dot bc.ca
2014-12-19  1:17 ` bugdal at aerifal dot cx
2014-12-19 20:29 ` sstewartgallus00 at mylangara dot bc.ca
2014-12-19 21:01 ` bugdal at aerifal dot cx
2015-01-26 22:33 ` sstewartgallus00 at mylangara dot bc.ca [this message]

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

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

  git send-email \
    --in-reply-to=bug-17214-131-ZxFzAJABnf@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.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.
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).