From patchwork Wed Oct 11 20:30:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10000597 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 698316037F for ; Wed, 11 Oct 2017 20:34:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5DE8C2845C for ; Wed, 11 Oct 2017 20:34:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 508B328468; Wed, 11 Oct 2017 20:34:19 +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 534BB2845C for ; Wed, 11 Oct 2017 20:34:18 +0000 (UTC) Received: (qmail 15527 invoked by uid 550); 11 Oct 2017 20:31:42 -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 15387 invoked from network); 11 Oct 2017 20:31:37 -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=3Ft4Jh8g1gQFyUUcYublbR5eYiJL0UmIMd/NfUNtJQs=; b=Pj3Mui4uIXMR2rRzN0KyInO3hwMjpRcMrwt33LmWwxUz+CiOItvyTDGNoz9d9pGQC+ 7eBuoz3Ha69+dIiggXDQJjFhzdCYZC3937k/sCKAa0poFbvrEbJxjOwk56pz/b93zxW8 iImFwArFky6ALhCtLiKtc/hY8qhnyKRWgnIRph8M4VaKRm2DaUekoL74k1aCEPZTkJyQ u9yuXQ3r2Y2eVi/fqqcpUeZs4MyeN68Z1Gom76yIrH5Y/zj2nJAHiE1Kn+YfA9yXcQPX 70F3pj65I/uXZepfU8fRc3k/fbp3CECaDPXiTYbaDgHoJwaKF1i9eBhS08pG9YZvfD5c aGdg== 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=3Ft4Jh8g1gQFyUUcYublbR5eYiJL0UmIMd/NfUNtJQs=; b=JSfVB9PNu+i3lccLNf+mK3zL70a7Fz8u70y14MwIbA5IKXADhGX9F4dkdeQOlhKK20 vVo90XsNow7f6WVRzJ2IrpM2PJvCYh66dgjKYkUjqJLBLUGR0eiP7KH/so3SrZGDj+tN NxJKicQhqouLdh2z5M8seAPdHcj2YVohTtZepIn8Yypxr7mEKr/S6M+/zloUvyjeqdLf 7juOmtAwgeTVy/78WM88nsXeKbvXTXimA3dar00A7Bz2XeMlfqvmLGmpJkdVZ8SfdCFB LS03S01RcoI9tfEfzYybOxCJ5rSuuh3shDZ7AE1V8QwZHJkORb6DK2aK26h2RZHSYqqd Uu7g== X-Gm-Message-State: AMCzsaUtGL/0z5JKDVy3ir3jvmNZFnq5rKeKd2ZC+DqH5VW+BcSoLZ1e EHf5+TYVm3RuW+7KANE7LNSzNg== X-Google-Smtp-Source: AOwi7QBrGexlXWEm0meZycNITEryeyYk8fjcawESP+O5bt/pw0KC1uV7w+zRm4wMecXop1XRctzmBg== X-Received: by 10.84.134.164 with SMTP id 33mr161310plh.323.1507753884518; Wed, 11 Oct 2017 13:31:24 -0700 (PDT) From: Thomas Garnier To: Herbert Xu , "David S . Miller" , Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , Thomas Garnier , Kees Cook , Andrey Ryabinin , 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 , Paul Gortmaker , Andrew Morton , Alexey Dobriyan , "Paul E . McKenney" , Nicolas Pitre , Borislav Petkov , "Luis R . Rodriguez" , Greg Kroah-Hartman , Christopher Li , Steven Rostedt , Jason Baron , Mika Westerberg , Dou Liyang , "Rafael J . Wysocki" , 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 , Jonathan Corbet , Michal Hocko , Peter Foley , Paul Bolle , Jiri Kosina , "H . J . Lu" , Rob Landley , 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, 11 Oct 2017 13:30:13 -0700 Message-Id: <20171011203027.11248-14-thgarnie@google.com> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171011203027.11248-1-thgarnie@google.com> References: <20171011203027.11248-1-thgarnie@google.com> Subject: [kernel-hardening] [PATCH v1 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)