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: 9894305 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 1D2CA603B4 for ; Thu, 10 Aug 2017 17:28:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 044B5288A7 for ; Thu, 10 Aug 2017 17:28:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC5F5288ED; Thu, 10 Aug 2017 17:28:41 +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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 651BD288B0 for ; Thu, 10 Aug 2017 17:28:41 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEk-0007uW-4U; Thu, 10 Aug 2017 17:27:10 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dfrEi-0007rR-IT for xen-devel@lists.xenproject.org; Thu, 10 Aug 2017 17:27:08 +0000 Received: from [193.109.254.147] by server-6.bemta-6.messagelabs.com id B5/59-03937-B679C895; Thu, 10 Aug 2017 17:27:07 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrDIsWRWlGSWpSXmKPExsVyMfTABt3s6T2 RBtv+MFl83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBmT2z8yF0zlrTjc1s3SwNjG3cXIySEkMINR ommaQRcjFweLwCsWiavLz7KBOBIC/awS59c3MIJUSQjESdy+OIEFwi6XOHLyCStEt5LE1g1Lm UEahAT+MUrMvwWRYBPQktjTMJ8JJCEisFRQ4mTrNUYQh1mghUniyb9tTCBVwgJeEheuvwBKcA AtV5X4+zkOxOQVsJRYe5IXYpmJxJPWSWBHcAKFpy2byQyx2EJi6q+VTBMYBRYwMqxi1ChOLSp LLdI1NNVLKspMzyjJTczM0TU0MNPLTS0uTkxPzUlMKtZLzs/dxAgMLQYg2MH4bVnAIUZJDiYl Ud7iT92RQnxJ+SmVGYnFGfFFpTmpxYcYZTg4lCR4K6b1RAoJFqWmp1akZeYAgxwmLcHBoyTCu 3UqUJq3uCAxtzgzHSJ1itGS48qVdV+YOKYc2A4kX034/41JiCUvPy9VSpzXGmSeAEhDRmke3D hYJF5ilJUS5mUEOlCIpyC1KDezBFX+FaM4B6OSMK8ryBSezLwSuK2vgA5iAjoowrcT5KCSRIS UVAOjFMvNsKeNIv84d01kuCh2+kyg8LxPGV3CT7+3MOzfHP5F3748SejEMd0zu88ZHYiV7dlm 88dCfP21I0/PSe8Nnc964tVaMXs+56PVKgYPvpypmVGis8Yn9uDpKp7kiE77gy8ufQv5XCVm9 7qGL0YwZCfXYbWTG+foJHbJKZy3nSyz6uXb4PO5SizFGYmGWsxFxYkAVkkhN78CAAA= X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-5.tower-27.messagelabs.com!1502386025!105781137!1 X-Originating-IP: [209.85.192.176] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 25796 invoked from network); 10 Aug 2017 17:27:06 -0000 Received: from mail-pf0-f176.google.com (HELO mail-pf0-f176.google.com) (209.85.192.176) by server-5.tower-27.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 10 Aug 2017 17:27:06 -0000 Received: by mail-pf0-f176.google.com with SMTP id c28so5621249pfe.3 for ; Thu, 10 Aug 2017 10:27:06 -0700 (PDT) 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=XfcllTEn4wFNdRSbvgdPlRUk7kh2hiowLc8EuY1iA/crSuklYHVgV8d7KriPo+Ppt9 dRZLRmiOV1GRAHZ6uWH+h2tCpouSvvt7mDKwppgDnSfRhSXt5epW22crCo7KV/iaQnSn ky26EjRb10aIT7btL+fYzeKlR4nctNNIEofJsUr77kHDahHzGxcLAIuTsbClwo8z6RW1 e9+bkq68yAUevlM8p1OUGcfQXm0T2pWYoZNPBgCQQpCsufEx/Xc6d+69uliszepSgCwX uVGjeshjkD5qWcN18WGJ62TCUtPy5EJ7NX/md61CL35xxcu2wy7PEEnwTaiJtsZJcDSX 2N/g== X-Gm-Message-State: AHYfb5jQT89P3WOvbQhw0+AexwHo9rtxboOvJN/yfROMOxzlpYz+FCX6 jXy6wP26OrEI9Rwr X-Received: by 10.84.174.1 with SMTP id q1mr14252178plb.10.1502386024733; Thu, 10 Aug 2017 10:27:04 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id p77sm11894925pfi.153.2017.08.10.10.27.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); 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 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> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [RFC v2 15/23] x86/boot/64: Use _text in a global for PIE support X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" 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)