From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12302 Path: news.gmane.org!.POSTED!not-for-mail From: John Reiser Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] Add comments to i386 assembly source Date: Mon, 1 Jan 2018 19:15:50 -0800 Message-ID: <64245dca-3c6e-3918-701c-dcf3f8e00783@bitwagon.com> References: <20171223094545.rmx6xtmucyz5xzap@voyager> <72c68934-4445-c83d-7bbc-004953b2f9e9@bitwagon.com> <20171231154926.GG1627@brightrain.aerifal.cx> <20180101195224.tpkl5g5w66rzwzz3@voyager> <5caf910a-dd98-6836-c70f-6a98cf8a9d22@bitwagon.com> <20180102014915.GJ1627@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1514862857 20906 195.159.176.226 (2 Jan 2018 03:14:17 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 2 Jan 2018 03:14:17 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 To: musl@lists.openwall.com Original-X-From: musl-return-12318-gllmg-musl=m.gmane.org@lists.openwall.com Tue Jan 02 04:14:13 2018 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1eWD1g-0004bU-QY for gllmg-musl@m.gmane.org; Tue, 02 Jan 2018 04:14:04 +0100 Original-Received: (qmail 24470 invoked by uid 550); 2 Jan 2018 03:16:05 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 24449 invoked from network); 2 Jan 2018 03:16:05 -0000 In-Reply-To: <20180102014915.GJ1627@brightrain.aerifal.cx> Content-Language: en-US Xref: news.gmane.org gmane.linux.lib.musl.general:12302 Archived-At: On 01/01/2018 13:49 UTC, Rich Felker wrote: > On Mon, Jan 01, 2018 at 02:57:02PM -0800, John Reiser wrote: >> There's a bug. clone() is a user-level function that can be used >> independently of the musl internal implementation of threads. >> Thus when clone() in musl/src/linux/clone.c calls >> return __syscall_ret(__clone(func, stack, flags, arg, ptid, tls, ctid)); >> then the i386 implementation of __clone has no guarantee about >> the value in %gs, and it is a bug to assume that (%gs >> 3) >> fits in 8 bits. > > The ABI is that at function call or any time a signal could be > received, %gs must always be a valid segment register value reflecting > the current thread's thread pointer. If this is violated, the program > has undefined behavior. More than one segment descriptor can designate the same subset of the linear address space. Duplicate the segment descriptor to a target selector that is >= 256, and load %gs with the duplicate selector before calling clone(). > >> The code in musl/src/thread/i386/clone.s wastes up to 12 bytes >> when aligning the new stack, by aligning before [pre-]allocating >> space for the one argument to the thread function. > > I suspect the initial value happens to be aligned anyway in which case > reserving 16 bytes and aligning to 16 is the same as reserving 4 and > aligning to 16. If you think it's not, I don't mind changing if you > can do careful testing to make sure it doesn't introduce any bugs. This is another bug! Consider the valid code: void **lo_stack = malloc(5 * sizeof(void *)); /* malloc() guarantees 16-byte alignment of lo_stack */ clone(func, &lo_stack[5], ...); then __clone() does: and $-16,%ecx /* &lo_stack[4] */ sub $ 16,%ecx /* &lo_stack[0] */ ... mov %ecx,%esp /* new thread: implicit action of ___NR_clone system call */ call *%eax /* OUT-OF-BOUNDS: lo_stack[-1] = return address */ Thus, starting the thread function has scribbled outside the allocated area, even though the lo_stack[] array can accommodate the call by the code I showed: lea -NBPW(arg2),%ecx /* &lo_stack[4] */ and $-16,%ecx /* still &lo_stack[4] */ ... mov %ecx,%esp /* new thread: implicit action of __NR_clone system call */ call *%eax /* lo_stack[3] = return address */ The danger is not "new bugs", but rather revealing latent bugs that were obscured by the less-strict old code. For instance, if the thread function actually has two formal parameters, or if it uses va_arg() to reference beyond the first actual argument, then running the optimal code is more likely to notice. --