From patchwork Tue Mar 13 20:59:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10280785 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 23E08602C2 for ; Tue, 13 Mar 2018 21:04:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 134D321327 for ; Tue, 13 Mar 2018 21:04:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 056A3284CE; Tue, 13 Mar 2018 21:04:44 +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 F281021327 for ; Tue, 13 Mar 2018 21:04:42 +0000 (UTC) Received: (qmail 21690 invoked by uid 550); 13 Mar 2018 21:01:05 -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 20435 invoked from network); 13 Mar 2018 21:00:53 -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=8Z0HoSlEr/r8ueDJF32+0PobUO6ef1mKRTWK6wqcoxI=; b=hSXI/bunmGvm6N4VL6KC8WY6QDeHH1HRshTbGl5JJhe8ugERZl1p38t+1oRsJ6wHk8 piqepawxrBqiuFNACNuyQKfwy5LbvmZoBW7yY3UjLH9IwZVcQIImfeXk3HxtjVI3hlYA idH79S+qtgIppGXVDdIz2iTjiR2dFueUlSNp1xBo4jhdrpTDYQLubxUjrLFBz5Rz+h9b xreC1HD1r3UdyHD2mjnNjLUKvDRmoPkTy3erLO1ktrMhj2UMY3x71A/dpiOeHiK0i85u TJJ88n8hhc4GXy6CykOvQL9VTVosGE+/g2gCberPmbihfYus0WzAQwLbXdSVLDjHr+vd GlfA== 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=8Z0HoSlEr/r8ueDJF32+0PobUO6ef1mKRTWK6wqcoxI=; b=XXkdADOInGJXy9OSWv4FA1O16cyJP7b5vjw1etG5BJQqwJ/3LX0CgJxG5y5Rf6QICF aS6VPg/CYJbjVlbPBtpeKfeGZXlcUVjL8plCk8Ig7xazxOlNTIti6XDMH4jsS/pBrpYW lyIyUosm+pmp7Bqp9YTRMMAiIWpoIfKTa2XdFO3ObUWd/OBM1cGyK2nPzsw8BgkPAjVG XrvYS7XZ4o5smGeKA9DrOcmc1Cg/9YOd83MDEFTlUuP6leD52e1ZpSXq60SmahRFccdq GXEYn496hKTRA7TZ4ScqTogfxt02Cm2BdxlZS2FIF317wi74JuLVu+rpdMbwRauEJ+a2 QlPw== X-Gm-Message-State: AElRT7G8+6qtImOCrE1QBzWC6VvadN8e4r8/o+lGExnYKig+/CL9BeZ2 sbVlNuXkNVZEMecgsJTalPtrsA== X-Google-Smtp-Source: AG47ELv+64jFuN1zct6gdaeKaUsiiaPumXOOe0sZC84Ouz0jwCqAqJ8KYCtnnmRIi0TBaZBs505ZPA== X-Received: by 10.101.82.198 with SMTP id z6mr1570784pgp.41.1520974840625; Tue, 13 Mar 2018 14:00:40 -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 13/27] x86/boot/64: Build head64.c as mcmodel large when PIE is enabled Date: Tue, 13 Mar 2018 13:59:31 -0700 Message-Id: <20180313205945.245105-14-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 The __startup_64 function assumes all symbols have relocated addresses instead of the current boot virtual address. PIE generated code favor relative addresses making all virtual and physical address math incorrect. If PIE is enabled, build head64.c as mcmodel large instead to ensure absolute references on all memory access. Add a global __force_order variable required when using a large model with read_cr* functions. To build head64.c as mcmodel=large, disable the retpoline gcc flags. This code is used at early boot and removed later, it doesn't need retpoline mitigation. 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/Makefile | 6 ++++++ arch/x86/kernel/head64.c | 3 +++ 2 files changed, 9 insertions(+) diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 29786c87e864..1ff6be34de66 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -22,6 +22,12 @@ CFLAGS_REMOVE_early_printk.o = -pg CFLAGS_REMOVE_head64.o = -pg endif +ifdef CONFIG_X86_PIE +# Remove PIE and retpoline flags that are incompatible with mcmodel=large +CFLAGS_REMOVE_head64.o += -fPIE -mindirect-branch=thunk-extern -mindirect-branch-register +CFLAGS_head64.o = -mcmodel=large +endif + KASAN_SANITIZE_head$(BITS).o := n KASAN_SANITIZE_dumpstack.o := n KASAN_SANITIZE_dumpstack_$(BITS).o := n diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 0c855deee165..2fe60e661227 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -64,6 +64,9 @@ EXPORT_SYMBOL(vmemmap_base); #define __head __section(.head.text) +/* Required for read_cr3 when building as PIE */ +unsigned long __force_order; + static void __head *fixup_pointer(void *ptr, unsigned long physaddr) { return ptr - (void *)_text + (void *)physaddr;