From patchwork Tue Dec 10 20:47:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1207277 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-107926-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="f3/thM4u"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="oaxMFW1Y"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XXDz1bh7z9sPK for ; Wed, 11 Dec 2019 07:47:31 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=F4qqbNgfl8pCE5km7S+c4Pk5ckHppcSXp+iG9joBCNnnDUYuTKM07 3PXrhVZFuct4k8sACm9jv/5bHrcK3kRcaR8u/hn5oLlamqv1be6zFlScA+umlUkE bis8olrneQ0ocmTkpxbfLxU8fy4SVgcqjiM3qP3KfXwehIOoIA1Vc8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=zi3KVZ4YeM1cdFQkDEFKGBtWxps=; b=f3/thM4uR7pf95eSCCsYHnDWJEcs WeDp8MNjb+jwRAqsVHApg2AKKSeLdtPL2LkPMIFPQpH7R+/sb0yguLHTlwm8uekN VTxMvEIibSs4Y8DGHniBPijAr3/u0bOltbTlXAkhc4D6VMtLUNmJtquSDIOXMXj2 wE6pvk3ipXhGGcg= Received: (qmail 122850 invoked by alias); 10 Dec 2019 20:47:17 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 122796 invoked by uid 89); 10 Dec 2019 20:47:17 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=unnecessarily, HContent-Transfer-Encoding:8bit X-HELO: mail-pj1-f65.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=qILRKzjt8wgkZiEaI6n3iMRY8ia0u7eDsqLsMZjWQb4=; b=oaxMFW1YxkHx9CD5TTDT5jls8gtsb8P6pEUCwXBAXdJr2uP43hGOjJ6pjUlBazoCb/ vjMlaXoJLEPh7BnN/cudrin1iEw89SjkgS1DlMRcddNEoQSls7pdD1XbSRU5S8u0Q40Q 5xryOTLYFpN0G5Ek/JEROEnzgRbpKwotIScX66dOERMdjdMySWJYCOC8zr0iEFwMgskm HkEdM253ms1cl8bkufAlIrEYVgkKiqU74IhlhY8O33qqHKK7wLQXlxEES42UZSbf+Asm tkpYKgWW1Ge8gZfDweySnmZJxmWlcLmZXjTGcaiu8nz8alnqxVgg+MaF1y4fr3wRqpwq 2fYw== From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH 1/4] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] Date: Tue, 10 Dec 2019 12:47:07 -0800 Message-Id: <20191210204710.4832-2-hjl.tools@gmail.com> In-Reply-To: <20191210204710.4832-1-hjl.tools@gmail.com> References: <20191210204710.4832-1-hjl.tools@gmail.com> MIME-Version: 1.0 On i386, since EAX, ECX and EDX are caller-saved, there are no need to save and restore EAX, ECX and EDX in getcontext, setcontext and swapcontext. They just need to clear EAX on success. The extra scratch registers are needed to enable CET. Tested on i386. --- sysdeps/unix/sysv/linux/i386/getcontext.S | 8 +------- sysdeps/unix/sysv/linux/i386/setcontext.S | 11 ++++------- sysdeps/unix/sysv/linux/i386/swapcontext.S | 17 +++++------------ 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S index 1568f9604a..85da8195f9 100644 --- a/sysdeps/unix/sysv/linux/i386/getcontext.S +++ b/sysdeps/unix/sysv/linux/i386/getcontext.S @@ -26,13 +26,7 @@ ENTRY(__getcontext) /* Load address of the context data structure. */ movl 4(%esp), %eax - /* Return value of getcontext. EAX is the only register whose - value is not preserved. */ - movl $0, oEAX(%eax) - - /* Save the 32-bit register values and the return address. */ - movl %ecx, oECX(%eax) - movl %edx, oEDX(%eax) + /* Save the preserved register values and the return address. */ movl %edi, oEDI(%eax) movl %esi, oESI(%eax) movl %ebp, oEBP(%eax) diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S index dffe1d18e7..9095584b44 100644 --- a/sysdeps/unix/sysv/linux/i386/setcontext.S +++ b/sysdeps/unix/sysv/linux/i386/setcontext.S @@ -65,22 +65,19 @@ ENTRY(__setcontext) cfi_offset (esi, oESI) cfi_offset (ebp, oEBP) cfi_offset (ebx, oEBX) - cfi_offset (edx, oEDX) - cfi_offset (ecx, oECX) movl oESP(%eax), %esp /* Push the return address on the new stack so we can return there. */ pushl %ecx - /* Load the values of all the 32-bit registers (except ESP). - Since we are loading from EAX, it must be last. */ + /* Load the values of all the preserved registers (except ESP). */ movl oEDI(%eax), %edi movl oESI(%eax), %esi movl oEBP(%eax), %ebp movl oEBX(%eax), %ebx - movl oEDX(%eax), %edx - movl oECX(%eax), %ecx - movl oEAX(%eax), %eax + + /* All done, return 0 for success. */ + xorl %eax, %eax /* End FDE here, we fall into another context. */ cfi_endproc diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S index 6fdc176535..83e5f0b2ff 100644 --- a/sysdeps/unix/sysv/linux/i386/swapcontext.S +++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S @@ -26,13 +26,7 @@ ENTRY(__swapcontext) /* Load address of the context data structure we save in. */ movl 4(%esp), %eax - /* Return value of swapcontext. EAX is the only register whose - value is not preserved. */ - movl $0, oEAX(%eax) - - /* Save the 32-bit register values and the return address. */ - movl %ecx, oECX(%eax) - movl %edx, oEDX(%eax) + /* Save the preserved register values and the return address. */ movl %edi, oEDI(%eax) movl %esi, oESI(%eax) movl %ebp, oEBP(%eax) @@ -91,15 +85,14 @@ ENTRY(__swapcontext) /* Push the return address on the new stack so we can return there. */ pushl %ecx - /* Load the values of all the 32-bit registers (except ESP). - Since we are loading from EAX, it must be last. */ + /* Load the values of all the preserved registers (except ESP). */ movl oEDI(%eax), %edi movl oESI(%eax), %esi movl oEBP(%eax), %ebp movl oEBX(%eax), %ebx - movl oEDX(%eax), %edx - movl oECX(%eax), %ecx - movl oEAX(%eax), %eax + + /* All done, return 0 for success. */ + xorl %eax, %eax /* The following 'ret' will pop the address of the code and jump to it. */ From patchwork Tue Dec 10 20:47:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1207276 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-107925-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="nNdUpxCA"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="K0ALJrdm"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XXDq38Kfz9sPK for ; Wed, 11 Dec 2019 07:47:23 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=TjlV84JX+JLcsMOmVjsINmuFjfM9mrc5ulNfZiCqxvbM3AR3p/iP5 xOa++A4X3PVTidexbP2WAwOPGtdRphYHSIzG+6kjgjIO35tEI2YIYfWzZryhREo7 mHXsutvvueLAWIa+wG6Tob7lfTEAHlcrDr6ikulUDZsosgziIh+KTY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=dm+Yo+97q/N12aXkcStvT/5h6Q8=; b=nNdUpxCANI7xCDFssaoZxpXuECrC Os+KacaKAUA++1Kfklnw3jrfmqdjLER1Tsldzt6Z/hVueKq3TqWoc9FJzN6qQ9GI 6DWP9+hII/y+ar7hXnoalZUTpPalLqO2pYQwtO/aZlIljX9dgBSqwDp3sF2tmgf5 pnVJvYLGvR/2fxg= Received: (qmail 122670 invoked by alias); 10 Dec 2019 20:47:16 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 122661 invoked by uid 89); 10 Dec 2019 20:47:16 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:536, HContent-Transfer-Encoding:8bit X-HELO: mail-pj1-f46.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=BDqY3aIzn0JRpwaCWU7CK0J+zZ2kxQ1lI+/Ud7lTXuE=; b=K0ALJrdmwbLmC7QQ1QP0giVwPz7+9zTkivXeEBFszDH8vY3GzEaepnwX/+YJOLVjew Y8nrveyb/a0bhjEuOQRKll1CVXyFA6DIQ/bXP8z+1s2i4w/lNlCPyiRJEZIfb5HGKbiR at7h25tqG9ZQjL9zb+9LvIGlIHqbIgr9G3OVsHYvpOamwlGpdvnoDNtbJgZ4F1Kth6NH NLqeqRmtFnJ9OqC/g97bGlIPIpckIDBkphWOsFrWOCZOTQ5PCfF163Esxbl65+xQFI9m ddNAyHcTqER0F4NZC+O1bBIpCbsF8NeRsZu5MBgGFE+zjdBvGMOv492a9tB2gMndyRTF BvtA== From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH 2/4] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target Date: Tue, 10 Dec 2019 12:47:08 -0800 Message-Id: <20191210204710.4832-3-hjl.tools@gmail.com> In-Reply-To: <20191210204710.4832-1-hjl.tools@gmail.com> References: <20191210204710.4832-1-hjl.tools@gmail.com> MIME-Version: 1.0 Add a missing _CET_ENDBR to indirect jump targe in sysdeps/i386/sub_n.S. --- sysdeps/i386/sub_n.S | 1 + 1 file changed, 1 insertion(+) diff --git a/sysdeps/i386/sub_n.S b/sysdeps/i386/sub_n.S index 1562cc00fd..1760beb38f 100644 --- a/sysdeps/i386/sub_n.S +++ b/sysdeps/i386/sub_n.S @@ -91,6 +91,7 @@ L(oop): movl (%esi),%eax movl 8(%esi),%eax sbbl 8(%edx),%eax movl %eax,8(%edi) + _CET_ENDBR movl 12(%esi),%eax sbbl 12(%edx),%eax movl %eax,12(%edi) From patchwork Tue Dec 10 20:47:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1207279 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-107928-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="MBvBWRD8"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OoiwM3Pn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XXFJ4jpyz9sPc for ; Wed, 11 Dec 2019 07:47:48 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=p1xgkL3peJfqQqNbDu9eTlutgUxCt5/O1hp2RwI8XIOT9QmwdIVGU i79rp9GH3tVkAQsX49MsK6ltaiMxW7LYaVTDTPh+MLWrXK3qrdD8Yh64p+t2VOBb IZxNJ+MWaBN/ANxsbCvLXIERC76Ik9JJsCv4oaSEz6ni4baiSxyA94= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=EvtDp54WbxjZC4Lvf7xYuACZDXg=; b=MBvBWRD8oWReWy7bNydwN5r4StAI j5U6vVXX7y68Z5EJLuJTx3SQ/xRItsv2hmMhy2FoaLTdZWlkR2hwRugwUMjWQTxt 6O4vQrhrLFiLSz/Bt11waFRpYtFt3u123P22IN706uMEcey6XkKimmaGudG8cH36 cGGmviZTASDXuzY= Received: (qmail 123129 invoked by alias); 10 Dec 2019 20:47:19 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123043 invoked by uid 89); 10 Dec 2019 20:47:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HContent-Transfer-Encoding:8bit X-HELO: mail-pf1-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=0B2lfqWdhswYuLMrBrb/uhP+z5fi1sKJUAIM/BVO+98=; b=OoiwM3PnvxMjRiRxJbdGulbnbCtMD1mBtzJPcJTJMYb6cumiTYMPe8HGSFOZvTZjXH mtv5WsS2j9lCB1EbInHDTMuacL3oNbJFNwNqlf1ebNwyPj6ti1ci5DTIGLtOLXmC6yNj lR1++XPkVtfrsbLMDrMPtry7g5yQhAMsSwGR3atavNhlHj/sNtOBsUqxtAbsmrCmOQRy 2wiyZnm33G8+mvNbx4OGN33T5HBbi3/MaIKq0ls+aWirE0HHNA4LZDsT1FxDwrTZtAxO R4KmdSAIJ0L9ez54k13VE483E8Pdz8QkakoqQ8QwPUyU6qIvR7ZtC0k0LSFFg5zEewS3 xqBQ== From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH 3/4] i386: Add _CET_ENDBR to assembly files without ENTRY Date: Tue, 10 Dec 2019 12:47:09 -0800 Message-Id: <20191210204710.4832-4-hjl.tools@gmail.com> In-Reply-To: <20191210204710.4832-1-hjl.tools@gmail.com> References: <20191210204710.4832-1-hjl.tools@gmail.com> MIME-Version: 1.0 Add _CET_ENDBR to i386 assembly files which don't use ENTRY to add ENDBR32 at function entries when CET is enabled. --- sysdeps/i386/i386-mcount.S | 2 ++ sysdeps/i386/nptl/pthread_spin_lock.S | 2 ++ sysdeps/i386/nptl/pthread_spin_unlock.S | 3 +++ sysdeps/i386/pthread_spin_trylock.S | 2 ++ sysdeps/unix/sysv/linux/i386/_exit.S | 1 + 5 files changed, 10 insertions(+) diff --git a/sysdeps/i386/i386-mcount.S b/sysdeps/i386/i386-mcount.S index 32b2a5f0bf..6575b84a1b 100644 --- a/sysdeps/i386/i386-mcount.S +++ b/sysdeps/i386/i386-mcount.S @@ -30,6 +30,7 @@ .type C_SYMBOL_NAME(_mcount), @function .align ALIGNARG(4) C_LABEL(_mcount) + _CET_ENDBR /* Save the caller-clobbered registers. */ pushl %eax pushl %ecx @@ -58,6 +59,7 @@ weak_alias (_mcount, mcount) .type C_SYMBOL_NAME(__fentry__), @function .align ALIGNARG(4) C_LABEL(__fentry__) + _CET_ENDBR /* Save the caller-clobbered registers. */ pushl %eax pushl %ecx diff --git a/sysdeps/i386/nptl/pthread_spin_lock.S b/sysdeps/i386/nptl/pthread_spin_lock.S index 62c72b9063..2876a8b1cf 100644 --- a/sysdeps/i386/nptl/pthread_spin_lock.S +++ b/sysdeps/i386/nptl/pthread_spin_lock.S @@ -15,12 +15,14 @@ License along with the GNU C Library; if not, see . */ +#include #include .globl pthread_spin_lock .type pthread_spin_lock,@function .align 16 pthread_spin_lock: + _CET_ENDBR mov 4(%esp), %eax 1: LOCK decl 0(%eax) diff --git a/sysdeps/i386/nptl/pthread_spin_unlock.S b/sysdeps/i386/nptl/pthread_spin_unlock.S index d190ca562d..dc412190ae 100644 --- a/sysdeps/i386/nptl/pthread_spin_unlock.S +++ b/sysdeps/i386/nptl/pthread_spin_unlock.S @@ -16,10 +16,13 @@ License along with the GNU C Library; if not, see . */ +#include + .globl pthread_spin_unlock .type pthread_spin_unlock,@function .align 16 pthread_spin_unlock: + _CET_ENDBR movl 4(%esp), %eax movl $1, (%eax) xorl %eax, %eax diff --git a/sysdeps/i386/pthread_spin_trylock.S b/sysdeps/i386/pthread_spin_trylock.S index 0ec09d8b64..c7f94375a7 100644 --- a/sysdeps/i386/pthread_spin_trylock.S +++ b/sysdeps/i386/pthread_spin_trylock.S @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include @@ -29,6 +30,7 @@ .type pthread_spin_trylock,@function .align 16 pthread_spin_trylock: + _CET_ENDBR movl 4(%esp), %edx movl $1, %eax xorl %ecx, %ecx diff --git a/sysdeps/unix/sysv/linux/i386/_exit.S b/sysdeps/unix/sysv/linux/i386/_exit.S index d59c127501..a21d5f69b3 100644 --- a/sysdeps/unix/sysv/linux/i386/_exit.S +++ b/sysdeps/unix/sysv/linux/i386/_exit.S @@ -21,6 +21,7 @@ .type _exit,@function .global _exit _exit: + _CET_ENDBR movl 4(%esp), %ebx /* Try the new syscall first. */ From patchwork Tue Dec 10 20:47:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1207280 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-107929-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="fmJRcIp7"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="PDx2enyE"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47XXFV6tyQz9sPK for ; Wed, 11 Dec 2019 07:47:58 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; q=dns; s= default; b=SVcuevAPvrvgF+iSeqX2LkIjo5dg01ve6TmjVZG5ZoD7YUJl0lNox AqBggcMGwEJL7KrfCC06qLlWgQOKb4lWT6lkfAHpxTSaa9TB6FAYeuKnD6NDGGhz hoSqULvUHRc0P7u4unrOp6+kxS8kFl8YsDugRXXBjgW5qjMnWroLr4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; s=default; bh=25c17JZREdfILuzfV8gnSPby05Q=; b=fmJRcIp70OL5ptcynpzvCrtKQoQ9 duTpiBu3dWNW6Kj+F3AifUe0aMAar4GSsI/ZC8E6jfHaoEC/GA+yJSOOsvn7CaDO Srh8RY81xBUXM68jvjP7w9wND5+FgbdJ4h/9n8qyS95RnaRB79CiuHmknpfM/J1h ozXKtSQe/4SqCG0= Received: (qmail 123379 invoked by alias); 10 Dec 2019 20:47:20 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 123325 invoked by uid 89); 10 Dec 2019 20:47:20 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=unwinding, Fetch X-HELO: mail-pl1-f194.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=wCNb/3BFV+SnTO0PIEgVvWqJdvWgG4ZEBkWHoHBVn/o=; b=PDx2enyE07hsh+Nqq5S2ItxhcDclMiMS41sUvIKNaECVBH39leMRrwBizAXveUAQ9J B4sGrqZPxYMPez6OzBUG9ZqzYwp8LFBSJUlb14Eor+IkXnK00ZLB5dep6MKsfIZyN5tO UUW96SMtgtJCl8Z9Yei8H9E0o1z8nVu/cQBHBmp+sY4pj3hUbovU6SKo3V6SP7XRp428 ay4lrwyILnQP1JsOutRuK7or92EYwIshb6OqqhjE4rt2r4DFTwxxl3M4XNjCwKZH5TmB fMZJvTb6iJYO+1101lK7iSXas4ekYKlg1GN2LHgsPakWEcg70bdPympHuA7jQgH9SeJK xCmQ== From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH 4/4] i386: Enable CET support in ucontext functions Date: Tue, 10 Dec 2019 12:47:10 -0800 Message-Id: <20191210204710.4832-5-hjl.tools@gmail.com> In-Reply-To: <20191210204710.4832-1-hjl.tools@gmail.com> References: <20191210204710.4832-1-hjl.tools@gmail.com> MIME-Version: 1.0 1. getcontext and swapcontext are updated to save the caller's shadow stack pointer and return address. 2. setcontext and swapcontext are updated to restore shadow stack and jump to new context directly. 3. makecontext is updated to allocate a new shadow stack and set the caller's return address to the helper code, L(exitcode). Since makecontext allocates a new shadow stack when making a new context and kernel allocates a new shadow stack for clone/fork/vfork syscalls, we track the current shadow stack base. In setcontext and swapcontext, if the target shadow stack base is the same as the current shadow stack base, we unwind the shadow stack. Otherwise it is a stack switch and we look for a restore token. We enable shadow stack at run-time only if program and all used shared objects, including dlopened ones, are shadow stack enabled, which means that they must be compiled with GCC 8 or above and glibc 2.28 or above. We need to save and restore shadow stack only if shadow stack is enabled. When caller of getcontext, setcontext, swapcontext and makecontext is compiled with smaller ucontext_t, shadow stack won't be enabled at run-time. We check if shadow stack is enabled before accessing the extended field in ucontext_t. Tested on i386 CET/non-CET machines. --- sysdeps/unix/sysv/linux/i386/getcontext.S | 56 ++++++++ sysdeps/unix/sysv/linux/i386/makecontext.S | 123 +++++++++++++++++ sysdeps/unix/sysv/linux/i386/setcontext.S | 101 +++++++++++++- sysdeps/unix/sysv/linux/i386/swapcontext.S | 139 ++++++++++++++++++++ sysdeps/unix/sysv/linux/i386/sysdep.h | 5 + sysdeps/unix/sysv/linux/i386/ucontext_i.sym | 1 + 6 files changed, 422 insertions(+), 3 deletions(-) diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S index 85da8195f9..0ab2808815 100644 --- a/sysdeps/unix/sysv/linux/i386/getcontext.S +++ b/sysdeps/unix/sysv/linux/i386/getcontext.S @@ -18,6 +18,7 @@ . */ #include +#include #include "ucontext_i.h" @@ -42,6 +43,61 @@ ENTRY(__getcontext) movw %fs, %dx movl %edx, oFS(%eax) +#if SHSTK_ENABLED + /* Check if shadow stack is enabled. */ + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET + jz L(no_shstk) + + /* Save EAX in EDX. */ + movl %eax, %edx + + xorl %eax, %eax + cmpl %gs:SSP_BASE_OFFSET, %eax + jnz L(shadow_stack_bound_recorded) + + /* Save EBX in the unused EAX slot. */ + movl %ebx, oEAX(%edx) + + /* Get the base address and size of the default shadow stack + which must be the current shadow stack since nothing has + been recorded yet. */ + sub $24, %esp + mov %esp, %ecx + movl $ARCH_CET_STATUS, %ebx + movl $__NR_arch_prctl, %eax + ENTER_KERNEL + testl %eax, %eax + jz L(continue_no_err) + + /* This should never happen. */ + hlt + +L(continue_no_err): + /* Restore EBX from the EAX slot. */ + movl oEAX(%edx), %ebx + + /* Record the base of the current shadow stack. */ + movl 8(%esp), %eax + movl %eax, %gs:SSP_BASE_OFFSET + add $24, %esp + +L(shadow_stack_bound_recorded): + /* Load address of the context data structure. */ + movl 4(%esp), %eax + + /* Get the current shadow stack pointer. */ + rdsspd %edx + /* NB: Save the caller's shadow stack so that we can jump back + to the caller directly. */ + addl $4, %edx + movl %edx, oSSP(%eax) + + /* Save the current shadow stack base in ucontext. */ + movl %gs:SSP_BASE_OFFSET, %edx + movl %edx, (oSSP + 4)(%eax) + +L(no_shstk): +#endif /* We have separate floating-point register content memory on the stack. We use the __fpregs_mem block in the context. Set the links up correctly. */ diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S index e2f8afc895..2d227abeef 100644 --- a/sysdeps/unix/sysv/linux/i386/makecontext.S +++ b/sysdeps/unix/sysv/linux/i386/makecontext.S @@ -18,6 +18,7 @@ . */ #include +#include #include "ucontext_i.h" @@ -68,6 +69,127 @@ ENTRY(__makecontext) jnz 1b 2: +#if SHSTK_ENABLED + /* Check if Shadow Stack is enabled. */ + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET + jz L(skip_ssp) + + /* Reload the pointer to ucontext. */ + movl 4(%esp), %eax + + /* Shadow stack is enabled. We need to allocate a new shadow + stack. */ + subl oSS_SP(%eax), %edx + shrl $STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT, %edx + + /* Align shadow stack size to 8 bytes. */ + addl $7, %edx + andl $-8, %edx + + /* Store shadow stack size in __ssp[2]. */ + movl %edx, (oSSP + 8)(%eax) + + /* Save ESI in the unused ECX slot. */ + movl %esi, oECX(%eax) + /* Save EDI in the unused EDX slot. */ + movl %edi, oEDX(%eax) + + /* Save the pointer to ucontext. */ + movl %eax, %edi + + /* Get the original shadow stack pointer. */ + rdsspd %esi + + /* Align the saved original shadow stack pointer to the next + 8 byte aligned boundary. */ + andl $-8, %esi + + /* Load the top of the new stack into EDX. */ + movl oESP(%eax), %edx + + /* We need to terminate the FDE here because the unwinder looks + at ra-1 for unwind information. */ + cfi_endproc + + /* Swap the original stack pointer with the top of the new + stack. */ + xchgl %esp, %edx + + /* Add 4 bytes since CALL will push the 4-byte return address + onto stack. */ + addl $4, %esp + + /* Allocate the new shadow stack. Save EBX in the unused EAX + slot. */ + movl %ebx, oEAX(%eax) + + /* CET syscall takes 64-bit sizes. */ + subl $16, %esp + movl (oSSP + 8)(%eax), %ecx + movl %ecx, (%esp) + movl $0, 4(%esp) + movl %ecx, 8(%esp) + movl $0, 12(%esp) + movl %esp, %ecx + + movl $ARCH_CET_ALLOC_SHSTK, %ebx + movl $__NR_arch_prctl, %eax + ENTER_KERNEL + testl %eax, %eax + jne L(hlt) /* This should never happen. */ + + /* Copy the base address of the new shadow stack to __ssp[1]. */ + movl (%esp), %eax + movl %eax, (oSSP + 4)(%edi) + + addl $16, %esp + + /* Restore EBX from the EAX slot. */ + movl oEAX(%edi), %ebx + + /* Get the size of the new shadow stack. */ + movl (oSSP + 8)(%edi), %ecx + + /* Use the restore stoken to restore the new shadow stack. */ + rstorssp -8(%eax, %ecx) + + /* Save the restore token at the next 8 byte aligned boundary + on the original shadow stack. */ + saveprevssp + + /* Push the address of "jmp exitcode" onto the new stack as + well as the new shadow stack. */ + call 1f + jmp L(exitcode) +1: + + /* Get the new shadow stack pointer. */ + rdsspd %eax + + /* Use the restore stoken to restore the original shadow stack. */ + rstorssp -8(%esi) + + /* Save the restore token on the new shadow stack. */ + saveprevssp + + /* Store the new shadow stack pointer in __ssp[0]. */ + movl %eax, oSSP(%edi) + + /* Restore the original stack. */ + mov %edx, %esp + + cfi_startproc + + /* Restore ESI from the ECX slot. */ + movl oECX(%edi), %esi + /* Restore EDI from the EDX slot. */ + movl oEDX(%edi), %edi + + ret + +L(skip_ssp): +#endif + /* If the function we call returns we must continue with the context which is given in the uc_link element. To do this set the return address for the function the user provides @@ -123,6 +245,7 @@ L(call_exit): call HIDDEN_JUMPTARGET(exit) /* The 'exit' call should never return. In case it does cause the process to terminate. */ +L(hlt): hlt cfi_startproc END(__makecontext) diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S index 9095584b44..3e55452d32 100644 --- a/sysdeps/unix/sysv/linux/i386/setcontext.S +++ b/sysdeps/unix/sysv/linux/i386/setcontext.S @@ -18,6 +18,7 @@ . */ #include +#include #include "ucontext_i.h" @@ -56,9 +57,6 @@ ENTRY(__setcontext) movl oFS(%eax), %ecx movw %cx, %fs - /* Fetch the address to return to. */ - movl oEIP(%eax), %ecx - /* Load the new stack pointer. */ cfi_def_cfa (eax, 0) cfi_offset (edi, oEDI) @@ -67,6 +65,103 @@ ENTRY(__setcontext) cfi_offset (ebx, oEBX) movl oESP(%eax), %esp +#if SHSTK_ENABLED + /* Check if Shadow Stack is enabled. */ + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET + jz L(no_shstk) + + /* If the base of the target shadow stack is the same as the + base of the current shadow stack, we unwind the shadow + stack. Otherwise it is a stack switch and we look for a + restore token. */ + movl oSSP(%eax), %esi + movl %esi, %edi + + /* Get the base of the target shadow stack. */ + movl (oSSP + 4)(%eax), %ecx + cmpl %gs:SSP_BASE_OFFSET, %ecx + je L(unwind_shadow_stack) + +L(find_restore_token_loop): + /* Align the saved original shadow stack pointer to the next + 8 byte aligned boundary. */ + andl $-8, %esi + + /* Look for a restore token. */ + movl -8(%esi), %ebx + andl $-8, %ebx + cmpl %esi, %ebx + je L(restore_shadow_stack) + + /* Try the next slot. */ + subl $8, %esi + jmp L(find_restore_token_loop) + +L(restore_shadow_stack): + /* Pop return address from the shadow stack since setcontext + will not return. */ + movl $1, %ebx + incsspd %ebx + + /* Use the restore stoken to restore the target shadow stack. */ + rstorssp -8(%esi) + + /* Save the restore token on the old shadow stack. NB: This + restore token may be checked by setcontext or swapcontext + later. */ + saveprevssp + + /* Record the new shadow stack base that was switched to. */ + movl (oSSP + 4)(%eax), %ebx + movl %ebx, %gs:SSP_BASE_OFFSET + +L(unwind_shadow_stack): + rdsspd %ebx + subl %edi, %ebx + je L(skip_unwind_shadow_stack) + negl %ebx + shrl $2, %ebx + movl $255, %esi +L(loop): + cmpl %esi, %ebx + cmovb %ebx, %esi + incsspd %esi + subl %esi, %ebx + ja L(loop) + +L(skip_unwind_shadow_stack): + + /* Load the values of all the preserved registers (except ESP). */ + movl oEDI(%eax), %edi + movl oESI(%eax), %esi + movl oEBP(%eax), %ebp + movl oEBX(%eax), %ebx + + /* Get the return address set with getcontext. */ + movl oEIP(%eax), %ecx + + /* Check if return address is valid for the case when setcontext + is invoked from L(exitcode) with linked context. */ + rdsspd %eax + cmpl (%eax), %ecx + /* Clear EAX to indicate success. NB: Don't use xorl to keep + EFLAGS for jne. */ + movl $0, %eax + jne L(jmp) + /* Return to the new context if return address valid. */ + pushl %ecx + ret + +L(jmp): + /* Jump to the new context directly. */ + jmp *%ecx + +L(no_shstk): +#endif + + /* Fetch the address to return to. */ + movl oEIP(%eax), %ecx + /* Push the return address on the new stack so we can return there. */ pushl %ecx diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S index 83e5f0b2ff..977f291294 100644 --- a/sysdeps/unix/sysv/linux/i386/swapcontext.S +++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S @@ -18,6 +18,7 @@ . */ #include +#include #include "ucontext_i.h" @@ -76,6 +77,144 @@ ENTRY(__swapcontext) movl oFS(%eax), %edx movw %dx, %fs +#if SHSTK_ENABLED + /* Check if Shadow Stack is enabled. */ + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET + jz L(no_shstk) + + xorl %eax, %eax + cmpl %gs:SSP_BASE_OFFSET, %eax + jnz L(shadow_stack_bound_recorded) + + /* Get the base address and size of the default shadow stack + which must be the current shadow stack since nothing has + been recorded yet. */ + sub $24, %esp + mov %esp, %ecx + movl $ARCH_CET_STATUS, %ebx + movl $__NR_arch_prctl, %eax + ENTER_KERNEL + testl %eax, %eax + jz L(continue_no_err) + + /* This should never happen. */ + hlt + +L(continue_no_err): + /* Record the base of the current shadow stack. */ + movl 8(%esp), %eax + movl %eax, %gs:SSP_BASE_OFFSET + add $24, %esp + +L(shadow_stack_bound_recorded): + /* Load address of the context data structure we save in. */ + movl 4(%esp), %eax + + /* Load address of the context data structure we swap in */ + movl 8(%esp), %edx + + /* If we unwind the stack, we can't undo stack unwinding. Just + save the target shadow stack pointer as the current shadow + stack pointer. */ + movl oSSP(%edx), %ecx + movl %ecx, oSSP(%eax) + + /* Save the current shadow stack base in ucontext. */ + movl %gs:SSP_BASE_OFFSET, %ecx + movl %ecx, (oSSP + 4)(%eax) + + /* If the base of the target shadow stack is the same as the + base of the current shadow stack, we unwind the shadow + stack. Otherwise it is a stack switch and we look for a + restore token. */ + movl oSSP(%edx), %esi + movl %esi, %edi + + /* Get the base of the target shadow stack. */ + movl (oSSP + 4)(%edx), %ecx + cmpl %gs:SSP_BASE_OFFSET, %ecx + je L(unwind_shadow_stack) + +L(find_restore_token_loop): + /* Align the saved original shadow stack pointer to the next + 8 byte aligned boundary. */ + andl $-8, %esi + + /* Look for a restore token. */ + movl -8(%esi), %ebx + andl $-8, %ebx + cmpl %esi, %ebx + je L(restore_shadow_stack) + + /* Try the next slot. */ + subl $8, %esi + jmp L(find_restore_token_loop) + +L(restore_shadow_stack): + /* The target shadow stack will be restored. Save the current + shadow stack pointer. */ + rdsspd %ecx + movl %ecx, oSSP(%eax) + + /* Use the restore stoken to restore the target shadow stack. */ + rstorssp -8(%esi) + + /* Save the restore token on the old shadow stack. NB: This + restore token may be checked by setcontext or swapcontext + later. */ + saveprevssp + + /* Record the new shadow stack base that was switched to. */ + movl (oSSP + 4)(%edx), %ebx + movl %ebx, %gs:SSP_BASE_OFFSET + +L(unwind_shadow_stack): + rdsspd %ebx + subl %edi, %ebx + je L(skip_unwind_shadow_stack) + negl %ebx + shrl $2, %ebx + movl $255, %esi +L(loop): + cmpl %esi, %ebx + cmovb %ebx, %esi + incsspd %esi + subl %esi, %ebx + ja L(loop) + +L(skip_unwind_shadow_stack): + + /* Load the new stack pointer. */ + movl oESP(%edx), %esp + + /* Load the values of all the preserved registers (except ESP). */ + movl oEDI(%edx), %edi + movl oESI(%edx), %esi + movl oEBP(%edx), %ebp + movl oEBX(%edx), %ebx + + /* Get the return address set with getcontext. */ + movl oEIP(%edx), %ecx + + /* Check if return address is valid for the case when setcontext + is invoked from L(exitcode) with linked context. */ + rdsspd %eax + cmpl (%eax), %ecx + /* Clear EAX to indicate success. NB: Don't use xorl to keep + EFLAGS for jne. */ + movl $0, %eax + jne L(jmp) + /* Return to the new context if return address valid. */ + pushl %ecx + ret + +L(jmp): + /* Jump to the new context directly. */ + jmp *%ecx + +L(no_shstk): +#endif + /* Fetch the address to return to. */ movl oEIP(%eax), %ecx diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h index 7066ddc214..7bb83d5617 100644 --- a/sysdeps/unix/sysv/linux/i386/sysdep.h +++ b/sysdeps/unix/sysv/linux/i386/sysdep.h @@ -659,4 +659,9 @@ struct libc_do_syscall_args # endif #endif +/* Each shadow stack slot takes 4 bytes. Assuming that each stack + frame takes 128 bytes, this is used to compute shadow stack size + from stack size. */ +#define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5 + #endif /* linux/i386/sysdep.h */ diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym index b11a5509cd..933c1924eb 100644 --- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym +++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym @@ -28,3 +28,4 @@ oEIP mreg (EIP) oFPREGS mcontext (fpregs) oSIGMASK ucontext (uc_sigmask) oFPREGSMEM ucontext (__fpregs_mem) +oSSP ucontext (__ssp)