From patchwork Mon Jun 25 22:38:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10488271 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 2F1DA602B3 for ; Tue, 26 Jun 2018 08:42:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18C5C28857 for ; Tue, 26 Jun 2018 08:42:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E967728A20; Tue, 26 Jun 2018 08:42:08 +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=-12.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 0057E28B06 for ; Tue, 26 Jun 2018 08:41:25 +0000 (UTC) Received: (qmail 25879 invoked by uid 550); 26 Jun 2018 08:39:18 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 11388 invoked from network); 25 Jun 2018 22:41:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:in-reply-to:message-id:references:subject:from:to :cc; bh=XXQ7C/CDeUMNvwwGw/8BYZVCFy2rI+v0Pigd08BAKSc=; b=ARK9ftcgOouSqk4fMxtyYsAfDex24/Ayzi/AtIxioDUezvChXudQkPfNOwwr+3ZiRL rVCwh5esMjB9OTMkdaqfam+0w2pfO9aYrBUYX40KkiohWcjVjigpRkTishZWQzBBP/uR 1ql786ng1WWfmkMjPO+FuSQfa1Z8UX34oKUUlsqe4cgFd2+9/+o8WiI19h/3a7pxOjwq MV/8m0KQdewBL4Q8AxKONCqHduy0eq5ZMhWTYJCpe6G/2I6BDldafG5NTvKRJKKHG4vX WhBbWMi5LPECGI+m2D0C2JNvVV08sSEH0TwU3YdQ8uhlfO7wtw83EhJ8/LD3yQCr9Jp7 060g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:in-reply-to:message-id :references:subject:from:to:cc; bh=XXQ7C/CDeUMNvwwGw/8BYZVCFy2rI+v0Pigd08BAKSc=; b=cuUkg3EmhgJNcMPNmJBkpj0qCbgjDyxesNxSSinCrfv3p0xEZt4hpmn9Yjy2NfuxKw PfBlRG4NjMuk1xqdrm5blnLu6kSWpHPtpUhbb42++GP2SBV79m/K/xlaOhft9qGOwvup 7O4z3kxJ4Q0Sm5RhjGeEPuPiEYQlStzw6oJn2ri1JAHy5nFs1XOsMcdH0Y2Zpasx2iIR v0vNvVatnPe87OM0Pno2TxsWS3q5W0lHpR3fsl2uvduvYPpB9vjcdRzT+CyZtvt1vOQn FxK2IsfY3hJ1miov+k3HWr/HSV728KDhiNJTxFxO4WZa/VaEniooF4sHBVGHcSDcMk49 ocAA== X-Gm-Message-State: APt69E0cYeeMu7yfQ+Gp4NcKDloEgB4mtGTTNC6MHANZKjt7xQiHK7HQ 3e3u9hj1exeNYPNa0Xw2vAFSGNr9gHeYUxgVDT45xMAtu2nfkDzjA7EdtTtbUBIRzCKJbZPJvey EUuQNy5/rzYujqrDTSQzw8DSzh5gghGinED/yXVQyDJ/iBu3wgICq0WBJJS84H/I3koiOYxJHdB hrs7VIQg5T X-Google-Smtp-Source: ADUXVKInHsu2npYg7XtQ4k9TVhn2iNrTC4MpiIVgLdoBMmCL8Nj9iE/HXeNOjEeLLnh2WsEB9e3brWOFXptFRg== MIME-Version: 1.0 X-Received: by 2002:a24:14ca:: with SMTP id 193-v6mr1231065itg.13.1529966485494; Mon, 25 Jun 2018 15:41:25 -0700 (PDT) Date: Mon, 25 Jun 2018 15:38:58 -0700 In-Reply-To: <20180625224014.134829-1-thgarnie@google.com> Message-Id: <20180625224014.134829-11-thgarnie@google.com> References: <20180625224014.134829-1-thgarnie@google.com> X-Mailer: git-send-email 2.18.0.rc2.346.g013aa6912e-goog Subject: [PATCH v5 10/27] x86/boot/64: Adapt assembly for PIE support From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: Thomas Garnier , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Josh Poimboeuf , "Kirill A. Shutemov" , Borislav Petkov , Dave Hansen , Tom Lendacky , linux-kernel@vger.kernel.org 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. Instructions were changed to have absolute 64-bit references. Position Independent Executable (PIE) support will allow to extend the KASLR randomization range 0xffffffff80000000. Signed-off-by: Thomas Garnier --- arch/x86/kernel/head_64.S | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index 8344dd2f310a..7fca19e1f556 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -89,8 +89,10 @@ startup_64: popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */ - addq $(early_top_pgt - __START_KERNEL_map), %rax + movabs $(early_top_pgt - __START_KERNEL_map), %rcx + addq %rcx, %rax jmp 1f + ENTRY(secondary_startup_64) UNWIND_HINT_EMPTY /* @@ -119,7 +121,8 @@ 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 + movabs $(init_top_pgt - __START_KERNEL_map), %rcx + addq %rcx, %rax 1: /* Enable PAE mode, PGE and LA57 */ @@ -137,7 +140,7 @@ ENTRY(secondary_startup_64) movq %rax, %cr3 /* Ensure I am executing from virtual addresses */ - movq $1f, %rax + movabs $1f, %rax ANNOTATE_RETPOLINE_SAFE jmp *%rax 1: @@ -234,11 +237,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 + movabs $.Lafter_lret, %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)