From patchwork Wed Oct 4 21:19:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9985593 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 2F5B960586 for ; Wed, 4 Oct 2017 21:24:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FA2328B63 for ; Wed, 4 Oct 2017 21:24:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 13B8C28BCB; Wed, 4 Oct 2017 21:24:07 +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.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 1567E28B63 for ; Wed, 4 Oct 2017 21:24:05 +0000 (UTC) Received: (qmail 15861 invoked by uid 550); 4 Oct 2017 21:21:25 -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 15736 invoked from network); 4 Oct 2017 21:21:21 -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=SkOiE4XJ1I4cgeMk+K4tjeBaHCtXE2onqUAnPBH1uu0=; b=cLtw4g0iH8TyAZfPU4yp4+JKeCaOaUwzzx1zyLXmdTK6eY1z2CLEoMht22SIb2L+zS avMQuRnWMG+jqVy3T0VCtL9S0HNEIp7mndKa0pPQQdDpKWsg8KYYRhkc8iYzCuhdgC8y FueBFQXF8SzOQs7P2U9U3Bz7wLrDSxVvVdDg3n4PA99L3VI00rSF/VB5P5yckIDtbq6h 4qvAieGuuZWNQc81zUCZCgO8XSi67LVY19dcMH9mHrgfztKJ9Eb4JRldIaeMBAQEMVqT ftXGX6YoikRljBFwa7ebnq7XZ3S1Z2VpaHHTwC587e/d8XZzcqn1ZiOMvVUyHB+ko2o0 S5pQ== 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=SkOiE4XJ1I4cgeMk+K4tjeBaHCtXE2onqUAnPBH1uu0=; b=jTcuPgEARMVlmp8XeeTEQY1Pmu7Rzbwp04NVuDPAFUhy3/IVnKrrzlbTf/9uCSl2mV gLKgvRKe1ViIpYeiB7E3sBTE7NJHinxfnKx/MUIV6ImOwM6ZlOdi68HAGjaOxtF1L6tU vDtVBG3kcKjJbh6obFcr8BgCSk5ucV6Fbv9HDXZSViHF3sIlG+UWIKO6ARhtlR+9XtPe kRePmZlI8lNc0TgqOOcf+6qsx4Hfbiz7pR6bdIkxGQlmKZEMdjIU+irpA22tgfjgTPyM HZ6iBao6sIvDi+4UkrZCvV/i2We6AV7GnARGNkTIDt1rxECrss1rxrNDVe1+lK4QPVaw qbzQ== X-Gm-Message-State: AMCzsaXwV68BGXESYFud8LBREf06rdL86xnCjJ9mth1z1CRt8IZhRqiB zIS/l1K9OKibnIUip5NnKB5VnA== X-Google-Smtp-Source: AOwi7QDN0b8B+tdQhMRHDzwP7/Xr5HJAcIKLqraeOkhkG4FvxdPw1DSr+ae4hhsbU/R8AHUNbWChlQ== X-Received: by 10.98.109.69 with SMTP id i66mr8829735pfc.200.1507152068640; Wed, 04 Oct 2017 14:21:08 -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 , Kees Cook , Matthias Kaehlcke , Tom Lendacky , Andy Lutomirski , "Kirill A . Shutemov" , Borislav Petkov , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Juergen Gross , Chris Wright , Alok Kataria , Rusty Russell , Tejun Heo , Christoph Lameter , Boris Ostrovsky , Alexey Dobriyan , Andrew Morton , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Dou Liyang , "Rafael J . Wysocki" , Mika Westerberg , Lukas Wunner , Masahiro Yamada , Alexei Starovoitov , Daniel Borkmann , Markus Trippelsdorf , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Rik van Riel , David Howells , Ard Biesheuvel , Waiman Long , Kyle Huey , Andrey Ryabinin , Jonathan Corbet , Matthew Wilcox , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , Rob Landley , "H . J . Lu" , Baoquan He , =?UTF-8?q?Jan=20H=20=2E=20Sch=C3=B6nherr?= , Daniel Micay 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 Date: Wed, 4 Oct 2017 14:19:49 -0700 Message-Id: <20171004212003.28296-14-thgarnie@google.com> X-Mailer: git-send-email 2.14.2.920.gcf0c67979c-goog In-Reply-To: <20171004212003.28296-1-thgarnie@google.com> References: <20171004212003.28296-1-thgarnie@google.com> Subject: [kernel-hardening] [RFC v3 13/27] x86/boot/64: Use _text in a global for PIE support X-Virus-Scanned: ClamAV using ClamSMTP By default PIE generated code create only relative references so _text points to the temporary virtual address. Instead use a global variable so the relocation is done 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/head64.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index bab4fa579450..675f1dba3b21 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -45,8 +45,14 @@ static void __head *fixup_pointer(void *ptr, unsigned long physaddr) return ptr - (void *)_text + (void *)physaddr; } -unsigned long __head __startup_64(unsigned long physaddr, - struct boot_params *bp) +/* + * Use a global variable to properly calculate _text delta on PIE. By default + * a PIE binary do a RIP relative difference instead of the relocated address. + */ +unsigned long _text_offset = (unsigned long)(_text - __START_KERNEL_map); + +unsigned long __head notrace __startup_64(unsigned long physaddr, + struct boot_params *bp) { unsigned long load_delta, *p; unsigned long pgtable_flags; @@ -65,7 +71,7 @@ unsigned long __head __startup_64(unsigned long physaddr, * Compute the delta between the address I am compiled to run at * and the address I am actually running at. */ - load_delta = physaddr - (unsigned long)(_text - __START_KERNEL_map); + load_delta = physaddr - _text_offset; /* Is the address not 2M aligned? */ if (load_delta & ~PMD_PAGE_MASK)