From patchwork Thu Aug 10 17:26:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9894349 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5F2E760236 for ; Thu, 10 Aug 2017 17:29:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 462F028A39 for ; Thu, 10 Aug 2017 17:29:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A17128A92; Thu, 10 Aug 2017 17:29:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C2F6A28A39 for ; Thu, 10 Aug 2017 17:29:26 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEf-0007mU-Aw; Thu, 10 Aug 2017 17:27:05 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEe-0007kb-Af for xen-devel@lists.xenproject.org; Thu, 10 Aug 2017 17:27:04 +0000 Received: from [85.158.143.35] by server-2.bemta-6.messagelabs.com id 3E/0F-27137-7679C895; Thu, 10 Aug 2017 17:27:03 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrDIsWRWlGSWpSXmKPExsVyMfTAVt206T2 RBl1XdC2+b5nM5MDocfjDFZYAxijWzLyk/IoE1ozTr/kKlotXXP15m6mBcb5wFyMnh5DADEaJ fZc5uhi5OFgEXrFIXHiznAnEkRDoZ5VY8m8ZO0iVhECcxN2Pc5kg7AqJOxcXsEN0K0ls3bCUG aRBSOAfo8SbHQ1gCTYBLYk9DfPBJokILBWUONl6jRHEYRZoYZJ48m8b2ChhAWeJuTvPMYLYLA KqElOu3WDrYuTg4BWwlOi+5w+xzUTiSesksBJOoPC0ZTOZITZbSEz9tZJpAqPAAkaGVYwaxal FZalFuobGeklFmekZJbmJmTm6hgZmermpxcWJ6ak5iUnFesn5uZsYgaHFAAQ7GL8sCzjEKMnB pCTKW/ypO1KILyk/pTIjsTgjvqg0J7X4EKMMB4eSBG/FtJ5IIcGi1PTUirTMHGCQw6QlOHiUR Hi3TgVK8xYXJOYWZ6ZDpE4xWnJcubLuCxPHlAPbgeSrCf+/MQmx5OXnpUqJ81qDzBMAacgozY MbB4vES4yyUsK8jEAHCvEUpBblZpagyr9iFOdgVBLmdQWZwpOZVwK39RXQQUxAB0X4doIcVJK IkJJqYPSd4n+qNTVNJumSA/tf8+ONRb+f5CfbeTHOPcA9N/rYPY6HB7e91PyRlNVgN7WVzzgy VMPlpqF2BYf9dM7lcp5O8tHV1XKnpPdGrvr1onunB/v7Y9q+75qDCqbaxxiLKt7k0Zsbeelae sjs6LnfdNqPGHROF4w6vyb3pGX1jz+btsoFSvx8r8RSnJFoqMVcVJwIAEC11zW/AgAA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-2.tower-21.messagelabs.com!1502386021!63453067!1 X-Originating-IP: [209.85.192.181] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 28787 invoked from network); 10 Aug 2017 17:27:02 -0000 Received: from mail-pf0-f181.google.com (HELO mail-pf0-f181.google.com) (209.85.192.181) by server-2.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 10 Aug 2017 17:27:02 -0000 Received: by mail-pf0-f181.google.com with SMTP id t86so5654834pfe.2 for ; Thu, 10 Aug 2017 10:27:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1y+SX6AQvYfTFgwSMQkmeQMRa4shpxAafat+MY0vAVY=; b=NEDDhd2TnoBfCHgNX90RJ0cqQVsKpyXDChd8xFEWrI3rSCZze0bsx6ezRXjzk5f50D 87eoSEAzH4sQgunaTGKuSfkadOG1t1jJJkh7i5NXyyQrCDmgmrw3To9feIdBNUJ2w+OT pQDwPrWJOKdZszbxVxUpUj+ieQZY/zhXkDnl9csHViUixjo0cKncZvGAdABlU8zDJt4H mmjjGYuY53B25abeNz3DHmNHOBhm0Pt52ZBGL7NDCPhd0zN/2vqfNDyBQo08RRomt2WN +H3aVIifCFX3ERAooKy8lS+HPI62CFTxqwLITCzHrPiLk7c4H1zqPJIsnf7Dc0eUbjwK jcgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1y+SX6AQvYfTFgwSMQkmeQMRa4shpxAafat+MY0vAVY=; b=UL/P7QtydirK++e+i3xMVnmTVEhLWDKnulfddDpOHdRYJYjNbBNVOzeb72RkIfCiZH caAvZRech4y3D7p61LjUgTNn6XkeT/R5aQ/h1WAAOe2Dp0U81VhxFylDVqxzhe6Yzzin AzdESL+qKoSxnCtXCMJpR0fWXmJ4ZnTzomnjxQahg/f4vWvYYjzbUPrR3Hf9rGb5E82N eBsEPw6ozWxCzh1WbI2tM0kGD4L9k9LQddyoxXm5tXRU0KBMmAlucd97qWwRhzkpH/2L WoIJLymUPKA+SAtS+OUqNZncfL1ShrKuSwy8ljDoC5Ys4t7/pdDfEhEHvu6RPbMaq6m4 yxWQ== X-Gm-Message-State: AHYfb5hdPKueAhH8jCYT+cMR6PtydxoxdTj/23M6V4c6mIXJwUm2z7UW qLVoJH+6XYCQ5zwC X-Received: by 10.84.231.198 with SMTP id g6mr5722291pln.36.1502386020638; Thu, 10 Aug 2017 10:27:00 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id p77sm11894925pfi.153.2017.08.10.10.26.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Aug 2017 10:27:00 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , Thomas Garnier , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Tom Lendacky , Andy Lutomirski , Borislav Petkov , Brian Gerst , "Kirill A . Shutemov" , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Paul Gortmaker , Chris Metcalf , Andrew Morton , "Paul E . McKenney" , Nicolas Pitre , Christopher Li , "Rafael J . Wysocki" , Lukas Wunner , Mika Westerberg , Dou Liyang , Daniel Borkmann , Alexei Starovoitov , Masahiro Yamada , Markus Trippelsdorf , Steven Rostedt , Kees Cook , Rik van Riel , David Howells , Waiman Long , Kyle Huey , Peter Foley , Tim Chen , Catalin Marinas , Ard Biesheuvel , Michal Hocko , Matthew Wilcox , "H . J . Lu" , Paul Bolle , Rob Landley , Baoquan He , Daniel Micay Date: Thu, 10 Aug 2017 10:26:04 -0700 Message-Id: <20170810172615.51965-13-thgarnie@google.com> X-Mailer: git-send-email 2.14.0.434.g98096fd7a8-goog In-Reply-To: <20170810172615.51965-1-thgarnie@google.com> References: <20170810172615.51965-1-thgarnie@google.com> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [RFC v2 12/23] x86/boot/64: Adapt assembly for PIE support X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Change the assembly code to use only relative references of symbols for the kernel to be PIE compatible. Early at boot, the kernel is mapped at a temporary address while preparing the page table. To know the changes needed for the page table with KASLR, the boot code calculate the difference between the expected address of the kernel and the one chosen by KASLR. It does not work with PIE because all symbols in code are relatives. Instead of getting the future relocated virtual address, you will get the current temporary mapping. The solution is using global variables that will be relocated as expected. Position Independent Executable (PIE) support will allow to extended the KASLR randomization range below the -2G memory limit. Signed-off-by: Thomas Garnier --- arch/x86/kernel/head_64.S | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 513cbb012ecc..09579e0714ce 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -85,8 +85,23 @@ startup_64: popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(early_top_pgt - __START_KERNEL_map), %rax + addq _early_top_pgt_offset(%rip), %rax jmp 1f + + /* + * Position Independent Code takes only relative references in code + * meaning a global variable address is relative to RIP and not its + * future virtual address. Global variables can be used instead as they + * are still relocated on the expected kernel mapping address. + */ + .align 8 +_early_top_pgt_offset: + .quad early_top_pgt - __START_KERNEL_map +_init_top_offset: + .quad init_top_pgt - __START_KERNEL_map +_va_jump: + .quad 2f + ENTRY(secondary_startup_64) /* * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0, @@ -114,7 +129,7 @@ ENTRY(secondary_startup_64) popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(init_top_pgt - __START_KERNEL_map), %rax + addq _init_top_offset(%rip), %rax 1: /* Enable PAE mode, PGE and LA57 */ @@ -129,9 +144,8 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax - jmp *%rax -1: + jmp *_va_jump(%rip) +2: /* Check if nx is implemented */ movl $0x80000001, %eax @@ -227,11 +241,12 @@ ENTRY(secondary_startup_64) * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect, * address given in m16:64. */ - pushq $.Lafter_lret # put return address on stack for unwinder + leaq .Lafter_lret(%rip), %rax + pushq %rax # put return address on stack for unwinder xorq %rbp, %rbp # clear frame pointer - movq initial_code(%rip), %rax + leaq initial_code(%rip), %rax pushq $__KERNEL_CS # set correct cs - pushq %rax # target address in negative space + pushq (%rax) # target address in negative space lretq .Lafter_lret: ENDPROC(secondary_startup_64)