From patchwork Tue Jul 18 22:33:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9850053 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 359DC60392 for ; Tue, 18 Jul 2017 22:37:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 218A9285B7 for ; Tue, 18 Jul 2017 22:37:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 15EF4285D8; Tue, 18 Jul 2017 22:37:05 +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=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, 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 A7242285B7 for ; Tue, 18 Jul 2017 22:37:04 +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 1dXb4b-0000fe-Ts; Tue, 18 Jul 2017 22:34:33 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXb4a-0000cg-6T for xen-devel@lists.xenproject.org; Tue, 18 Jul 2017 22:34:32 +0000 Received: from [85.158.143.35] by server-10.bemta-6.messagelabs.com id 45/10-03582-7FC8E695; Tue, 18 Jul 2017 22:34:31 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrFIsWRWlGSWpSXmKPExsVyMfTAat1vPXm RBk8+6ll83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBlTL/9nKngoVnFlxh+mBsbXQl2MXBxCArMY JY49/MkC4rAIvGKR6Li9gh3EkRDoZ5U4PGMSWxcjJ5ATJ/Hp9H1WCLtconHrfjBbSEBJYuuGp cwQo/4xSjy9/ZgRJMEmoCWxp2E+E0hCRGA2h8TirjNgOwQEPrJJ3NhyGKyFWaCFSeLJv21MIC 3CAg4Si+f+YgGxWQRUJQ52zwGL8wpYSfy9PxNoHwfQbmOJv1/0QUxOoPDy+zwQV1hK9PY2sEx gFFzAyLCKUaM4tagstUjXyEAvqSgzPaMkNzEzR9fQwEwvN7W4ODE9NScxqVgvOT93EyMw7BiA YAfjr2UBhxglOZiURHm3KudFCvEl5adUZiQWZ8QXleakFh9ilOHgUJLgtQaGsZBgUWp6akVaZ g4wAmDSEhw8SiK8NiBp3uKCxNzizHSI1ClGS44rV9Z9YeKYcmA7kHw14f83JiGWvPy8VClx3g XdQA0CIA0ZpXlw42BReolRVkqYlxHoQCGegtSi3MwSVPlXjOIcjErCvMdApvBk5pXAbX0FdBA T0EHCvjkgB5UkIqSkGhj3MuecExT9c90vpzrrzrXo1cbvd2sJccd+Kz051ctsj/ebf5qPnUqr ZZvNBF/EGdSahO4OFV26Iyl62quKpcaecjk//2eIxuW9rnXgsmZQKteo+bs3S+zKurBJz+Q4Q n6UpYtpGz97eeTJm0aXIp2S6Q7ywSLMdnbz3B36mDfMLnbLlsiJUmIpzkg01GIuKk4EAIqkXE XNAgAA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1500417269!67688463!1 X-Originating-IP: [209.85.192.171] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 27665 invoked from network); 18 Jul 2017 22:34:30 -0000 Received: from mail-pf0-f171.google.com (HELO mail-pf0-f171.google.com) (209.85.192.171) by server-4.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 18 Jul 2017 22:34:30 -0000 Received: by mail-pf0-f171.google.com with SMTP id e199so17769144pfh.2 for ; Tue, 18 Jul 2017 15:34:30 -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=0IOrZ5vmTnoQQnJwdlMPnvoRTi0l7sF4vc118JWoh8U=; b=UJ23AlBrp9ARBxTtccm0l+c6O6GZc6LdFjWWWxNalrWtI0qzp9mZ5wphJTkIrbRbqp T3C+rSapApff9g712xXAkZK22caNSTVzieN9ZZcIQpffPpNXHQNR/4DDI+riVJZ1XBS6 vG/NW0G+485lV88tiKoUhNxdEv7tunWdla9dMyqqVeENX4I91GNdetT1gfdh5hWpNSmA iVzDemz+kDdkCnjkwEl57BaSUNtt0mHEkWZ84yzDXoFnI86H+jR8+10mN1fwobQBSREj LP3pnQgcUrRnD4sc+/9trGFnGVinC2BegNGvoIALh7uL5nST9z6nEqpWHkGwAvtBOzMA 04pg== 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=0IOrZ5vmTnoQQnJwdlMPnvoRTi0l7sF4vc118JWoh8U=; b=iAjhYaMyeu5MPOX55Btan79Hd7jkBhja1+CJeWcsT2Ov/cEkettb7s9RXAxYIh2AL8 eDu5SDuDYLE5XZNgVeYWT2pzfgNi8R5jYccukx+XD6uOk7MtVOS+YBMU9YwMDEl222II YCmptzqh4DoIMQZJDG8sl65Tck6kmlc4XTlSzsSXCjSEv5HI6vWAPZbofmpMtZ3LHw7h URQyVZiGFSK/yoghM1yaVOmANNaOxUiy6knAg4laec4UM0ej60X07z6h2ts3hNP3hogb EZ/sWyHBGSwfLodYeL6wQOC0wfx4ibV8azTIgERsRxNeRHxYcduRelzGLV1P45TV4CcL gr4Q== X-Gm-Message-State: AIVw111mA1oufZ8wEkYsTc+AEfyIir78+Hs8pg6W6Fv/lZ1c97MXgGw3 p4XFhU4CunzxO+wy X-Received: by 10.98.210.70 with SMTP id c67mr1039313pfg.6.1500417268434; Tue, 18 Jul 2017 15:34:28 -0700 (PDT) Received: from skynet.sea.corp.google.com ([100.100.206.164]) by smtp.gmail.com with ESMTPSA id b6sm6444242pgn.67.2017.07.18.15.34.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 15:34:27 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Thomas Garnier , Arnd Bergmann , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Andy Lutomirski , Borislav Petkov , "Kirill A . Shutemov" , Brian Gerst , Borislav Petkov , Christian Borntraeger , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Kees Cook , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Andrew Morton , Christopher Li , Dou Liyang , Masahiro Yamada , Daniel Borkmann , Markus Trippelsdorf , Peter Foley , Steven Rostedt , Tim Chen , Ard Biesheuvel , Catalin Marinas , Matthew Wilcox , Michal Hocko , Rob Landley , Jiri Kosina , "H . J . Lu" , Paul Bolle , Baoquan He , Daniel Micay Date: Tue, 18 Jul 2017 15:33:23 -0700 Message-Id: <20170718223333.110371-13-thgarnie@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> References: <20170718223333.110371-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 12/22] 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 | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 6225550883df..7e4f7a83a15a 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -78,8 +78,23 @@ startup_64: call __startup_64 popq %rsi - movq $(early_top_pgt - __START_KERNEL_map), %rax + movq _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, @@ -98,7 +113,8 @@ ENTRY(secondary_startup_64) /* Sanitize CPU configuration */ call verify_cpu - movq $(init_top_pgt - __START_KERNEL_map), %rax + movq _init_top_offset(%rip), %rax + 1: /* Enable PAE mode, PGE and LA57 */ @@ -113,9 +129,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 @@ -211,11 +226,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)