From patchwork Wed Oct 11 20:30:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10000535 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 D75D36037F for ; Wed, 11 Oct 2017 20:33:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CD14928B66 for ; Wed, 11 Oct 2017 20:33:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C108728B6A; Wed, 11 Oct 2017 20:33:14 +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 B462428B37 for ; Wed, 11 Oct 2017 20:33:13 +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 1e2Nf2-0000Rg-7W; Wed, 11 Oct 2017 20:31:24 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2Nf1-0000Q4-9g for xen-devel@lists.xenproject.org; Wed, 11 Oct 2017 20:31:23 +0000 Received: from [85.158.143.35] by server-7.bemta-6.messagelabs.com id D7/BF-25852-A9F7ED95; Wed, 11 Oct 2017 20:31:22 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrFIsWRWlGSWpSXmKPExsVyMfTAOt2Z9fc iDZZ+M7D4vmUykwOjx+EPV1gCGKNYM/OS8isSWDN+9T1gKVgvVrHg8jX2BsajQl2MXBxCAjMY JT62NbB3MXJysAi8YpFoeCoCkpAQ6GeV2L5iKzNIQkIgS2LNxr8sEHaaxOOmFYwQdplE66t2N hBbSEBJYuuGpcwQU/8xSjQumgJWxCagJbGnYT4TSEJE4ISwxKYPvxlBHGaBM0wSx/tOsoJUCQ u4Srzc/p8R4g5ViQ9PjoGt5hWwlFiy5CQbxDoLiWPHDoOdwQkU/77xEDPEaguJlbPOs05gFFz AyLCKUaM4tagstUjX2EgvqSgzPaMkNzEzR9fQwEwvN7W4ODE9NScxqVgvOT93EyMw7BiAYAfj 6XWBhxglOZiURHkfxtyLFOJLyk+pzEgszogvKs1JLT7EKMPBoSTBO70OKCdYlJqeWpGWmQOMA Ji0BAePkghvE0iat7ggMbc4Mx0idYrRkuPCnUt/mDgO7LkFJDtu3v3DJMSSl5+XKiXOWwrSIA DSkFGaBzcOFqWXGGWlhHkZgQ4U4ilILcrNLEGVf8UozsGoJMwbBDKFJzOvBG7rK6CDmIAOEk2 7A3JQSSJCSqqB0fPO2vcCU+TuV86NqnHxyYisP+tYk67f+MZRO1Hb9c+Kw/6Rd7veFa58t3LS sX39QZsbWl91/eJK/F/vkbfm4Ydd6wp4H9vcmajttM3zpUeLv+GE4pZZ29Iv9iaZBx+TT0otL 5qSwnii8kbfpQ9vDv0Ne/A77qmh62L+jiD2vjNVj28+enM7WYmlOCPRUIu5qDgRAEr4Dw3NAg AA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-11.tower-21.messagelabs.com!1507753880!73636801!1 X-Originating-IP: [209.85.192.174] 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 1483 invoked from network); 11 Oct 2017 20:31:21 -0000 Received: from mail-pf0-f174.google.com (HELO mail-pf0-f174.google.com) (209.85.192.174) by server-11.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 11 Oct 2017 20:31:21 -0000 Received: by mail-pf0-f174.google.com with SMTP id p87so1954598pfj.3 for ; Wed, 11 Oct 2017 13:31:21 -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=p1DhVKnroFN4+YVHCCSJ4oLO4Wt6WGbYx8SPv/nDQ7g=; b=QGOXzFfzgzT+keoR+vTgXyRl8eokhfUgeHGGNAlc+pGHsrLgFv+RdONIRJziTmouuA 0Rb8IhBzWIfiBBGBm9NnJjtWkhOxJz6uMX2KsBZ5mV44PQMMwAY3kHwvjkHS5Cxj5843 lE7LZ5078cwIVCItmiKo6KTy5KgihNHYONKuB9bgZIVx4LYSqyHR+ir1wU0u5qieG9Ku ZMRSufXOZaSRdq7UjBbFAycctRDG2FC5otFdezGhERz52y1EzQ5ALUIYdj7OmkZSXKxG pK3O9Tn7VEGrdxFXpfmGjUum6zD/ZfVmbZuGAUpawX751mrrxc79pemXztuxwWMfM4dZ Gz9A== 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=p1DhVKnroFN4+YVHCCSJ4oLO4Wt6WGbYx8SPv/nDQ7g=; b=BfbnsctCRUbk+05yaWcXhPwIiV75NklJJMJvveqqvNKU6MnwE0ousMIpnb/HDGlFGv duUkUyVKpBP9DkBD8ru23/TrMz62Qqs+x19NRpaVJW1FfxSBZThufB5GTrQ8KweYOFf2 2k7l5Kuxxj19LVX8MwLq5l16q4gD6C93gfC/+lu6Bx+xP3ARmrIcDVJyredaNfz2aOTG ggV+ZJa2KneVk3+Bh3UuzUyQTdCtecCzi6PyaSxXaP070wVW5hhgQNyhsrkBuGBBMEGh 55Z88rHaKF4guJbFpLVKDPIubYFjHO9DZCJL4lULq4jY86XdrVvImKE6pNF5YwGTHMmk njNw== X-Gm-Message-State: AMCzsaXowIJC51Epc0LgBZwTUfUYdumdUlOZfsOfIo70yrjTDHSFH5rH HT4WIqWPIC6E0r78mbCgXXYrCw== X-Google-Smtp-Source: AOwi7QDOf78RsPNS1d/9J0qZUKRUOVnsP5W2HQIlfCaKMzUff8YefEYImv+JLCFQNLYaiX1WMHkqMg== X-Received: by 10.84.231.2 with SMTP id f2mr182221plk.256.1507753879622; Wed, 11 Oct 2017 13:31:19 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id n12sm20691913pfb.149.2017.10.11.13.31.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 Oct 2017 13:31:19 -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 , Kees Cook , Andrey Ryabinin , Matthias Kaehlcke , Tom Lendacky , Andy Lutomirski , "Kirill A . Shutemov" , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Chris Wright , Alok Kataria , Rusty Russell , Tejun Heo , Christoph Lameter , Boris Ostrovsky , Paul Gortmaker , Andrew Morton , Alexey Dobriyan , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Mika Westerberg , Dou Liyang , "Rafael J . Wysocki" , Lukas Wunner , Masahiro Yamada , Alexei Starovoitov , Daniel Borkmann , Markus Trippelsdorf , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Rik van Riel , David Howells , Ard Biesheuvel , Waiman Long , Kyle Huey , Jonathan Corbet , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , "H . J . Lu" , Rob Landley , Baoquan He , =?UTF-8?q?Jan=20H=20=2E=20Sch=C3=B6nherr?= , Daniel Micay Date: Wed, 11 Oct 2017 13:30:10 -0700 Message-Id: <20171011203027.11248-11-thgarnie@google.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171011203027.11248-1-thgarnie@google.com> References: <20171011203027.11248-1-thgarnie@google.com> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v1 10/27] 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 | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 42e32c2e51bb..32d1899f48df 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -86,8 +86,21 @@ 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 + ENTRY(secondary_startup_64) UNWIND_HINT_EMPTY /* @@ -116,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 */ @@ -131,7 +144,7 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax + movabs $1f, %rax jmp *%rax 1: UNWIND_HINT_EMPTY @@ -230,11 +243,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: END(secondary_startup_64)