From patchwork Tue May 29 22:15:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10437297 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 A5FEA601C7 for ; Tue, 29 May 2018 22:20:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8ECE528900 for ; Tue, 29 May 2018 22:20:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 818DB2891B; Tue, 29 May 2018 22:20:30 +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 995662891F for ; Tue, 29 May 2018 22:20:29 +0000 (UTC) Received: (qmail 18025 invoked by uid 550); 29 May 2018 22:17:28 -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 17713 invoked from network); 29 May 2018 22:17:16 -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=7QHtG0Yjx1dOvc/PHEn5M0PWrJArZ+dfNCJ3mI2nlQI=; b=V5GwGRAxVHZMzLHO2Qni51ZDIrzIlLHaOrVDA7MN9ktgU9BlUEUsV2sd0cxpONFrqM MV6xLd3zs7xuTnH08HNua9Tgp4NKSSEqe0AM6gAIBt4ZIVBePkn/f+oNq55V1hr6ll1x Bo8njeM3N3Z1xYk8kPHV7F7LuOzp7sj7ErMlXn8aZxZBFqfgcPt5YGcEJhE1YlRWxTon 1T0na5vV4pCNax+pvyndOf/rL/AVDPoUKTr5tr4oktrOS9mpP4civMa/TqXdmKqP+6fu MdSDy+XEiuRnCeBCrbQhipPENosWUjRkR8KNFKdRDDv6uejkRnDRAfkhVRjBu9Axyd6P vTPw== 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=7QHtG0Yjx1dOvc/PHEn5M0PWrJArZ+dfNCJ3mI2nlQI=; b=sQGuaNNc02fqiSWWxnF+T0kI0hmvc++SMLW973Ey+6qbE7srcl5RpGKItxX0vO2C7M 5NqBGN5CkpTbIJ2ujfGIebb0NBw0RYjXumN/Oz5xFFmqUOyGRuECJ9KzPWMssodyEpko u720jG00T+VOQIvqyVkeqVelwMC32K+XPN5otF2EtsHClCVNOVYJr6tWjS8CD1pU2ieY lF2Ut0gVofYpTxzL8anBR473YJg7UO8Tr0ZieZ4/wWWjpT2EIo5EAzBWka6IFawXjH3i xxTSVSzxz44RaPQ7/daQVduMJSN/rlYPhUtD+/MpcAWWmOISnQxSe8e4s+8zwvKEn5rp X5NA== X-Gm-Message-State: ALKqPwc03UB1uoOZUmhwtR/98RIlKQgWeIj79n11u+N18EqAnM74SpkO I1AZCUIBpO7ZrVdyT0B4U2OvvZlwUrY= X-Google-Smtp-Source: ADUXVKJfngUEqPJi1QXVxMbHKzss4BVbyXZkaeFEe4FUe1/CPBKoXEsct/5g6sSEqnXfY3LRdt2QOg== X-Received: by 2002:a17:902:8604:: with SMTP id f4-v6mr249551plo.4.1527632224191; Tue, 29 May 2018 15:17:04 -0700 (PDT) From: Thomas Garnier To: kernel-hardening@lists.openwall.com Cc: Thomas Garnier , Skip Tom Lendacky , Skip Dave Hansen , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, "Kirill A. Shutemov" , Josh Poimboeuf , Juergen Gross , linux-kernel@vger.kernel.org Subject: [PATCH v4 10/27] x86/boot/64: Adapt assembly for PIE support Date: Tue, 29 May 2018 15:15:11 -0700 Message-Id: <20180529221625.33541-11-thgarnie@google.com> X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog In-Reply-To: <20180529221625.33541-1-thgarnie@google.com> References: <20180529221625.33541-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. 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)