Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3586744pxb; Mon, 24 Jan 2022 12:54:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwiRaoDALVj7X/AJRMXwZKKJDqWN0puhnI4Uf3X5q8CkcdAyeIEqeV8JnDlk5FGTferkRYy X-Received: by 2002:a17:902:b687:b0:149:90e2:8687 with SMTP id c7-20020a170902b68700b0014990e28687mr16041625pls.131.1643057646725; Mon, 24 Jan 2022 12:54:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643057646; cv=none; d=google.com; s=arc-20160816; b=TqJnkop0lioEpj9euBxbarEWyoTCcaNjJQ4RtcvD8RvHlj41WBhhSGuFFqXQWOC3+i Lgh23GpUKeaQBfY02WS9yB6CpWH9IxWo8VyESunTqIOAopsHvdeAPENolXFEg//iS7Hw hhj0hV5/YFKz3mM+9yQUnkWOh1xWMonGL0tmfbJ4JJ42FwJaYzQ8CqWQsWSOd3Iv7b8U lGYE60eflNSu/7ri+tjIOOIO6a5iMxeSjMn2f+5e+FvMW6n53kK8FnULVXtaaYIrTXnU tI4hcQ1IKOrhhtbcdoOMuMbA78XkhYnX9dKgqdbQNAwYurvFjbiXfxTUqK1NUfA9wlUm 4wSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t80F6Lp1A+eSqJKxPwluHwoaG6rR0oEmEXCg5zbXEmU=; b=NQ/kYJtOnlD6hfrKQxmsgWrJ6cgqK7NsJq8Oh5sB01wvkLy8kga1NfP6N6ymRW4gWW nA+uHcDC2QiW5QyGvcczWtDBMyJcHLNtBfzx2VbRLk/6wICYjDQveeZsqgs184kq9cCG givn/H2yTXnzy21qcDtdgLsCV7sJIbsnS2ylq24FRM0eUYzx/GOQZ5Scqz7z2o5/3Syx dqb6BAWYV9cq0VsDrgK0GabdPwzb6ddx/ZmpguSvr/ygcvo7KnmtANbQvO4DbXiW9Zc9 4Cik8RnYGFaBJq7oPBRxm1ZNcH8J2UhT2/Qiz+AlAHaBaPdXU5DTBKDCH/DQ5wy7KhJ5 8FDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Aow3Ls3z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y16si13004753plr.104.2022.01.24.12.53.52; Mon, 24 Jan 2022 12:54:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Aow3Ls3z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380395AbiAXUQP (ORCPT + 99 others); Mon, 24 Jan 2022 15:16:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348125AbiAXTwN (ORCPT ); Mon, 24 Jan 2022 14:52:13 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F203C041895; Mon, 24 Jan 2022 11:24:50 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 3D24BB8122F; Mon, 24 Jan 2022 19:24:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65D43C340E7; Mon, 24 Jan 2022 19:24:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643052288; bh=oBieQZ3SwdWDNJAbWyZOdHNTR2Z5uh3/Q1LKFni/fOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Aow3Ls3z4ke5DuuIYm7IJ0FD8KBqhi3hylUkT3eQSdrO3kh5oqVGKqu740oJxQlyC E5QbDnXpYeq0hSTLtYdd0LfID10+h4GZxSkQEEKCgM4rXFem10KojAYGOaZfsOMb86 SgVPnht9LDjHJVi5Ps2zUI+cV8Nip84M4070dH1M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bedirhan KURT , Louvian Lyndal , Peter Cordes , Ammar Faizi , Willy Tarreau , "Paul E. McKenney" Subject: [PATCH 5.4 010/320] tools/nolibc: x86-64: Fix startup code bug Date: Mon, 24 Jan 2022 19:39:54 +0100 Message-Id: <20220124183954.113119436@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ammar Faizi commit 937ed91c712273131de6d2a02caafd3ee84e0c72 upstream. Before this patch, the `_start` function looks like this: ``` 0000000000001170 <_start>: 1170: pop %rdi 1171: mov %rsp,%rsi 1174: lea 0x8(%rsi,%rdi,8),%rdx 1179: and $0xfffffffffffffff0,%rsp 117d: sub $0x8,%rsp 1181: call 1000
1186: movzbq %al,%rdi 118a: mov $0x3c,%rax 1191: syscall 1193: hlt 1194: data16 cs nopw 0x0(%rax,%rax,1) 119f: nop ``` Note the "and" to %rsp with $-16, it makes the %rsp be 16-byte aligned, but then there is a "sub" with $0x8 which makes the %rsp no longer 16-byte aligned, then it calls main. That's the bug! What actually the x86-64 System V ABI mandates is that right before the "call", the %rsp must be 16-byte aligned, not after the "call". So the "sub" with $0x8 here breaks the alignment. Remove it. An example where this rule matters is when the callee needs to align its stack at 16-byte for aligned move instruction, like `movdqa` and `movaps`. If the callee can't align its stack properly, it will result in segmentation fault. x86-64 System V ABI also mandates the deepest stack frame should be zero. Just to be safe, let's zero the %rbp on startup as the content of %rbp may be unspecified when the program starts. Now it looks like this: ``` 0000000000001170 <_start>: 1170: pop %rdi 1171: mov %rsp,%rsi 1174: lea 0x8(%rsi,%rdi,8),%rdx 1179: xor %ebp,%ebp # zero the %rbp 117b: and $0xfffffffffffffff0,%rsp # align the %rsp 117f: call 1000
1184: movzbq %al,%rdi 1188: mov $0x3c,%rax 118f: syscall 1191: hlt 1192: data16 cs nopw 0x0(%rax,%rax,1) 119d: nopl (%rax) ``` Cc: Bedirhan KURT Cc: Louvian Lyndal Reported-by: Peter Cordes Signed-off-by: Ammar Faizi [wt: I did this on purpose due to a misunderstanding of the spec, other archs will thus have to be rechecked, particularly i386] Cc: stable@vger.kernel.org Signed-off-by: Willy Tarreau Signed-off-by: Paul E. McKenney Signed-off-by: Greg Kroah-Hartman --- tools/include/nolibc/nolibc.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -422,14 +422,20 @@ struct stat { }) /* startup code */ +/* + * x86-64 System V ABI mandates: + * 1) %rsp must be 16-byte aligned right before the function call. + * 2) The deepest stack frame should be zero (the %rbp). + * + */ asm(".section .text\n" ".global _start\n" "_start:\n" "pop %rdi\n" // argc (first arg, %rdi) "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) - "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned when - "sub $8, %rsp\n" // entering the callee + "xor %ebp, %ebp\n" // zero the stack frame + "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call "call main\n" // main() returns the status code, we'll exit with it. "movzb %al, %rdi\n" // retrieve exit code from 8 lower bits "mov $60, %rax\n" // NR_exit == 60