mailing list of musl libc
 help / color / mirror / code / Atom feed
From: John Reiser <jreiser@bitwagon.com>
To: musl@lists.openwall.com
Subject: a third bug in musl clone()
Date: Tue, 2 Jan 2018 10:24:42 -0800	[thread overview]
Message-ID: <3d795d15-4808-6120-e497-d6c367c14952@bitwagon.com> (raw)
In-Reply-To: <20180102014915.GJ1627@brightrain.aerifal.cx>

In addition to the bugs in __clone for i386 with %gs and stack alignment
in the new thread, there is a third bug in musl's implementation of clone().
clone() takes optional arguments that need not be present,
yet musl/src/linux/clone.c fetches them anyway.  This can cause SIGSEGV.

===== musl/src/linux/clone.c excerpts
int clone(int (*func)(void *), void *stack, int flags, void *arg, ...)
{
    [[snip]]
         va_start(ap, arg);
         ptid = va_arg(ap, pid_t *);
         tls  = va_arg(ap, void *);
         ctid = va_arg(ap, pid_t *);
         va_end(ap);

         return __syscall_ret(__clone(func, stack, flags, arg, ptid, tls, ctid));
}
=====
The presence of ptid, tls, and ctid is indicated by bits in 'flags':
CLONE_PARENT_SETTID, CLONE_SETTLS, CLONE_CHILD_SETTID/CLONE_CHILD_CLEARTID.
If none of those bits are set, then it could be that none of the variable
arguments are present; therefore none of them should be fetched, and 0 (NULL)
should be passed to __clone() for each of ptid, tls, ctid.
[The meaning is unclear if any omitted argument is followed by an argument
that is flagged as present.  Should the implementation call the corresponding
va_arg(), or skip over it?]

How SIGSEGV can be generated: It is valid for &arg to be the address of
the last word on a hardware page: 0x...ffc on a 32-bit CPU with 4KiB pages,
with the following page unmapped.  &func would be 16-byte aligned at 0x...ff0.
Any one of the va_arg() calls would attempt to fetch from the next
page at address 0x...000 or greater, which will generate SIGSEGV.

If the implementer of clone() actually thought about this then it
is MANDATORY to insert a code comment about the situation.  Such as:
    /* Always fetch ptid,tls,ctid even though the official standard
     * says that they are va_arg.  The standard says that only
     * to allow compilation of [old] calls that omit un-flagged
     * trailing arguments.  In practice it is highly unreasonable
     * to require conditional fetching; even the meaning is unclear.
     * In the extremely unlikely case that &arg is near a "hole"
     * in the address space, then we will suffer SIGSEGV.
     */

-- 


  parent reply	other threads:[~2018-01-02 18:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-23  9:45 [PATCH] Add comments to i386 assembly source Markus Wichmann
2017-12-31  4:15 ` John Reiser
2017-12-31  6:54   ` Markus Wichmann
2017-12-31 15:49   ` Rich Felker
2018-01-01 19:52     ` Markus Wichmann
2018-01-01 22:57       ` John Reiser
2018-01-02  1:49         ` Rich Felker
2018-01-02  3:15           ` John Reiser
2018-01-02 19:49             ` Rich Felker
2018-01-02 18:24           ` John Reiser [this message]
2018-01-02 19:58             ` a third bug in musl clone() Rich Felker
2018-01-02 22:09               ` Florian Weimer
2018-01-03  2:51                 ` Rich Felker

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=3d795d15-4808-6120-e497-d6c367c14952@bitwagon.com \
    --to=jreiser@bitwagon.com \
    --cc=musl@lists.openwall.com \
    /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.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).