From patchwork Thu Aug 10 17:26:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9894373 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 08BF360236 for ; Thu, 10 Aug 2017 17:30:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E379928AE0 for ; Thu, 10 Aug 2017 17:30:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D73F528B50; Thu, 10 Aug 2017 17:30:49 +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 1004928AE0 for ; Thu, 10 Aug 2017 17:30:48 +0000 (UTC) Received: (qmail 1800 invoked by uid 550); 10 Aug 2017 17:27:26 -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 1463 invoked from network); 10 Aug 2017 17:27:17 -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=gAVwiPBy3JtHEZJ5HEj8vlypNFm+yKxTx2MeZs2QXWk=; b=QliFHHRxd3MLczfzI8dgqerGf+ErK0/7UAlj+pERHJDSp4fGQjHykwwv9uoesd2inN Q5cTm4WO0fzV3qCg54Hk48V42wYpZLOJOxcScrw8FnhvZ5I7bdP000iOKC52lT8VBS3w VhZf9paMiQz4aKhjaQ94WA4NuIZzqIBFr3yq83nvn7EWacv0LrtMXqYxROZFAdxiXuO8 ewWUh46d+WpQbcccj5lHHTcYfpj9Rae/XrLYrYWG4IdKx2hSExAnO8Pch66bbIaFlsEz Se8osgRLDNWgXW888CZRzbQ824I5eDY5qBKwdzQLZh4f80QWrerRwjN4K0LRzSykfqqR gyTg== 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=gAVwiPBy3JtHEZJ5HEj8vlypNFm+yKxTx2MeZs2QXWk=; b=XxIOnCSO2kKaEOIwnN6ti/AcxZBHCNmKVY/WXfYMK/UBhnNwEJG5bdng/fm9/SWw3h rnNNBes+sySPVJUlOB73kTINQ6KzrZY/kLv4lBL5xNzHLfYeaAAyM6mTXMpSHiSgZeT2 O3J3VfAiprhNLPBsD2yWcu11M+QNltexQ+rKaKhDrIVKEhP5SeCZ5COrqc8djn3yewYC s4+M8L521C3xne3KjMUCMsj9cY7sfmB2EjZQGHtp9/2U7xqUDgiPGX42OaMhEAb6KQmW 6OWYcugwSLWHSMNSwHLeKvlwrzdIBpqO0eJX77xMZyT4OQq3x2fxo21Uq8XXhq0HBsvu xMIw== X-Gm-Message-State: AHYfb5gfmtMLnq8uv4s+IpnaXJWVY80RQbybATQaJtFt2E3VH58Ykcgt YaYYcK7w80RbHCir X-Received: by 10.84.174.1 with SMTP id q1mr14252178plb.10.1502386024733; Thu, 10 Aug 2017 10:27:04 -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 , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Tom Lendacky , Andy Lutomirski , Borislav Petkov , Brian Gerst , "Kirill A . Shutemov" , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Paul Gortmaker , Chris Metcalf , Andrew Morton , "Paul E . McKenney" , Nicolas Pitre , Christopher Li , "Rafael J . Wysocki" , Lukas Wunner , Mika Westerberg , Dou Liyang , Daniel Borkmann , Alexei Starovoitov , Masahiro Yamada , Markus Trippelsdorf , Steven Rostedt , Kees Cook , Rik van Riel , David Howells , Waiman Long , Kyle Huey , Peter Foley , Tim Chen , Catalin Marinas , Ard Biesheuvel , Michal Hocko , Matthew Wilcox , "H . J . Lu" , Paul Bolle , Rob Landley , Baoquan He , Daniel Micay Cc: x86@kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, linux-arch@vger.kernel.org, linux-sparse@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Thu, 10 Aug 2017 10:26:07 -0700 Message-Id: <20170810172615.51965-16-thgarnie@google.com> X-Mailer: git-send-email 2.14.0.434.g98096fd7a8-goog In-Reply-To: <20170810172615.51965-1-thgarnie@google.com> References: <20170810172615.51965-1-thgarnie@google.com> Subject: [kernel-hardening] [RFC v2 15/23] 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 925b2928f377..e71f27a20576 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; @@ -64,7 +70,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)