From patchwork Tue Mar 13 20:59:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10280777 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 534BD602C2 for ; Tue, 13 Mar 2018 21:03:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 417CD284BD for ; Tue, 13 Mar 2018 21:03:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33374284C9; Tue, 13 Mar 2018 21:03:33 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_MED 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 1C24D284BD for ; Tue, 13 Mar 2018 21:03:31 +0000 (UTC) Received: (qmail 20368 invoked by uid 550); 13 Mar 2018 21:00:48 -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 Received: (qmail 20264 invoked from network); 13 Mar 2018 21:00:44 -0000 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=eQdfHSyL7AbE7TzHHG42nf8EeTOIWyarQ4Ux7nBg5B8=; b=iiQVUdPcWwlYjNXuOgu5qG1VkaqCHOxjJLWClngZYj6Kqt/eUjb7C8kkjg2bGckfk3 Hh3tb3t8BURRYjVx2MvESosuI8sjhANXqg9bAPqw/ZkOWwTZ5PdWkEMtgKsiFLnRMBQM OcX5VZq9MGybO4KYXv346kU2MdHW+wcBbrsaiQN4dBwlpv6xrHpKEiA5WZRO0wZ3eHBD boMZ0K+cs09sYuQnnCpiuVW0ckFtfl5iVMaSyVBdCrcEUFGPY121wXaj/gKzCPQ8S2HD UxeWiGR2xrlHtN3wwDRkUiEIb4MUpAj+1kStTZNvoTQYgbNz/XUc7vn1i4SRKMHpbGkA phbA== 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=eQdfHSyL7AbE7TzHHG42nf8EeTOIWyarQ4Ux7nBg5B8=; b=kSyjHla1m810FzgLYybFcACPfGwP2cVdnL5wDOnJokOXJWSQ+AfpcEyvWVHWNpDk1Q yNwST6WztDysvcEl5RNAUi4LwUhsxiV8E9MoFURN894rJZPPGascfx3RVrqRWQ6wbNf+ BUgOmZO9feXlo6Nmro6pcAEB/KOaU/pijyCHdG9aEmQi2kG2tUBOIBat1gZ1URVWXw4o SVd4gLJVncZXx0Cnmu3uq40AE0eMuX378NXhfPLvIlwVY+mGjzX/16ZC4pqRFZAgSB4Z WmTJ7j0SJSpVcYFwVdPXIcHe4GevWSiueJcaJNeqrroF4ej8ccEObTnCFt1ZADCchGVb wWng== X-Gm-Message-State: AElRT7Hcuwcv2myUNVxh2NOzOxA9S0Kt3/jBrKAKdSpQp7eARXdDDel6 o6fRRRoakLZWecFpp6Nyz0nkVw== X-Google-Smtp-Source: AG47ELuOiyvGXklA6FKJYQ7NaM2KHPy4UF5jT2e2wQvWE83YTwJ/PZd7Lo9eVaFht4V2uAc+OQeLtA== X-Received: by 2002:a17:902:b7cb:: with SMTP id v11-v6mr1792574plz.206.1520974832063; Tue, 13 Mar 2018 14:00:32 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Greg Kroah-Hartman , Kate Stewart , Thomas Garnier , Arnd Bergmann , Philippe Ombredanne , Arnaldo Carvalho de Melo , Andrey Ryabinin , Matthias Kaehlcke , Kees Cook , Tom Lendacky , "Kirill A . Shutemov" , Andy Lutomirski , Dominik Brodowski , Borislav Petkov , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Alok Kataria , Steven Rostedt , Tejun Heo , Christoph Lameter , Dennis Zhou , Boris Ostrovsky , David Woodhouse , Alexey Dobriyan , "Paul E . McKenney" , Andrew Morton , Nicolas Pitre , Randy Dunlap , "Luis R . Rodriguez" , Christopher Li , Jason Baron , Ashish Kalra , Kyle McMartin , Dou Liyang , Lukas Wunner , Petr Mladek , Sergey Senozhatsky , Masahiro Yamada , Ingo Molnar , Nicholas Piggin , Cao jin , "H . J . Lu" , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Dave Hansen , Rik van Riel , Jia Zhang , Jiri Slaby , Kyle Huey , Jonathan Corbet , Matthew Wilcox , Michal Hocko , Rob Landley , Baoquan He , Daniel Micay , =?UTF-8?q?Jan=20H=20=2E=20Sch=C3=B6nherr?= Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, virtualization@lists.linux-foundation.org, xen-devel@lists.xenproject.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 10/27] x86/boot/64: Adapt assembly for PIE support Date: Tue, 13 Mar 2018 13:59:28 -0700 Message-Id: <20180313205945.245105-11-thgarnie@google.com> X-Mailer: git-send-email 2.16.2.660.g709887971b-goog In-Reply-To: <20180313205945.245105-1-thgarnie@google.com> References: <20180313205945.245105-1-thgarnie@google.com> 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 48385c1074a5..48652f3ec46a 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -89,8 +89,9 @@ 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 + ENTRY(secondary_startup_64) UNWIND_HINT_EMPTY /* @@ -119,7 +120,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 */ @@ -137,7 +138,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 +235,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) @@ -342,6 +344,18 @@ END(early_idt_handler_common) GLOBAL(early_recursion_flag) .long 0 + /* + * 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 + #define NEXT_PAGE(name) \ .balign PAGE_SIZE; \ GLOBAL(name)