From patchwork Tue Jul 18 22:33:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 9850073 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 690A760393 for ; Tue, 18 Jul 2017 22:37:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 54D67285CD for ; Tue, 18 Jul 2017 22:37:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48F15285E4; Tue, 18 Jul 2017 22:37: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=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, 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 9FC7F285CD for ; Tue, 18 Jul 2017 22:37:16 +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 1dXb4g-0000n9-OM; Tue, 18 Jul 2017 22:34:38 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXb4f-0000lO-LH for xen-devel@lists.xenproject.org; Tue, 18 Jul 2017 22:34:37 +0000 Received: from [85.158.143.35] by server-5.bemta-6.messagelabs.com id 05/56-03368-CFC8E695; Tue, 18 Jul 2017 22:34:36 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrOIsWRWlGSWpSXmKPExsVyMfTAat3fPXm RBt/7OCy+b5nM5MDocfjDFZYAxijWzLyk/IoE1oz725YzFqziqfj++R5jA+Mmri5GLg4hgZmM Ej9+TWQBcVgEXrFInNzRzQbiSAj0s0r83bYLKMMB5MRJzN0j2cXICWRWSJzuuMIEYgsJKEls3 bCUGcL+xygx/aAfiM0moCWxp2E+E8gcEYHZHBKLu86AbRAQ+MgmcWPLYWYQh1mghUniyb9tYK OEBdwlZrw8wQJiswioSry8ehDM5hWwktg88RQrxBXGEn+/6IOYnEDh5fd5IBZbSvT2NrBMYBR cwMiwilG9OLWoLLVI11AvqSgzPaMkNzEzR9fQwEwvN7W4ODE9NScxqVgvOT93EyMw4BiAYAfj zudOhxglOZiURHm3KudFCvEl5adUZiQWZ8QXleakFh9ilOHgUJLg9e0GygkWpaanVqRl5gBDH yYtwcGjJMLrA5LmLS5IzC3OTIdInWK05LhyZd0XJo4pB7YDyVcT/n9jEmLJy89LlRLnXQDSIA DSkFGaBzcOFp+XGGWlhHkZgQ4U4ilILcrNLEGVf8UozsGoJMzbDzKFJzOvBG7rK6CDmIAOEvb NATmoJBEhJdXAaKLlVft38YfVNwzjeFfpXpm34uDkDy0Pn1YYuv41ke5/tnf62vb1OzO/SLyt PjCl5pyESdva6rKZwmVWfp/StfrOTvyUybFpa8nh+s0dX/lkHj6VM1+XrKuw90jHHs4/qele3 yvD9XPsT57zO22e+M//a27bBNu/mYF96/y3WRlMKK+qv3hkpxJLcUaioRZzUXEiAK9RUrLKAg AA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-3.tower-21.messagelabs.com!1500417273!70393763!1 X-Originating-IP: [209.85.192.171] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 24589 invoked from network); 18 Jul 2017 22:34:34 -0000 Received: from mail-pf0-f171.google.com (HELO mail-pf0-f171.google.com) (209.85.192.171) by server-3.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 18 Jul 2017 22:34:34 -0000 Received: by mail-pf0-f171.google.com with SMTP id q85so17856763pfq.1 for ; Tue, 18 Jul 2017 15:34:34 -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=GviMZAj/3F+TQ+CyTFs3u9oHFIdsobEUmPqsoLTxLlo=; b=oI616StmBVHCLzC1z5VfFvlOubWtx0eaCbpu8eJserRD8zwP078lx3Gub644B1Y32q yy7jonPvhpqERjjXTPbt5Fn71O6FoqQbIAi9N6xrBcpMRzDL8XAfSTDDBiDkxkuUZIPk M08DlirGRp3GLfu9dn13vNFcN3I0aQAH0aAYZ/EpVIdd84gY3AWRnJQs6Q9zxZzwfGu0 kkpUu2QNk4elqVdfaYdyTcDzc2Z5p8XJTl6duXPK0ykEN+Ow5vBFJYsyS+BNSkaoGGTA AHo8XISYz4y2CHur9mXuS/seazKn7oVMU9B+miRymAzJ9B7HGLnnntd0wMz5gubI61v+ dlpw== 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=GviMZAj/3F+TQ+CyTFs3u9oHFIdsobEUmPqsoLTxLlo=; b=EAHApiTAOGFSARgbF0ahEqZKd2szPZf8v6fWtwSCg3nlPr7w1u1cKZboNRQX8GNL0A 8BA/zH9c0F7CkpLleLAoYzY+jxs0SevmhVKsnYaS/QL2RQF/vNEOQlhahEqqU6Up4yfH QGQ5oQAblFQFQQe7c3zBv3kYQmKFcN/OP7GH3I6GTfqY/Xf4uDbH7oCqL+4PK00Xir01 km7q5zEafwc3BELzT4pBLXUqjAf3IE2GxVQilV8f9m6hS82H8RFJ3B/CtuDQy+VZUZ7o W6xre3EL7GcE2zkDUesSaVhZ8/z7K/VVcwtxM7rrxPIFAgPpjCtSfKGnfpMavkj/aKBQ zpKQ== X-Gm-Message-State: AIVw1101BzoAV30kiP1tUbPQeOGfWE8ELtWcPDIRufWyrzBwI8c/Zsxs fanufP54rQH4Poq/ X-Received: by 10.99.96.85 with SMTP id u82mr4037659pgb.214.1500417272984; Tue, 18 Jul 2017 15:34:32 -0700 (PDT) Received: from skynet.sea.corp.google.com ([100.100.206.164]) by smtp.gmail.com with ESMTPSA id b6sm6444242pgn.67.2017.07.18.15.34.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 15:34:32 -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 , Matthias Kaehlcke , Boris Ostrovsky , Juergen Gross , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Joerg Roedel , Andy Lutomirski , Borislav Petkov , "Kirill A . Shutemov" , Brian Gerst , Borislav Petkov , Christian Borntraeger , "Rafael J . Wysocki" , Len Brown , Pavel Machek , Tejun Heo , Christoph Lameter , Kees Cook , Paul Gortmaker , Chris Metcalf , "Paul E . McKenney" , Andrew Morton , Christopher Li , Dou Liyang , Masahiro Yamada , Daniel Borkmann , Markus Trippelsdorf , Peter Foley , Steven Rostedt , Tim Chen , Ard Biesheuvel , Catalin Marinas , Matthew Wilcox , Michal Hocko , Rob Landley , Jiri Kosina , "H . J . Lu" , Paul Bolle , Baoquan He , Daniel Micay Date: Tue, 18 Jul 2017 15:33:26 -0700 Message-Id: <20170718223333.110371-16-thgarnie@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170718223333.110371-1-thgarnie@google.com> References: <20170718223333.110371-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 15/22] 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 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 46c3c73e7f43..4103e90ff128 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -45,7 +45,13 @@ static void __head *fixup_pointer(void *ptr, unsigned long physaddr) return ptr - (void *)_text + (void *)physaddr; } -void __head __startup_64(unsigned long physaddr) +/* + * 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); + +void __head notrace __startup_64(unsigned long physaddr) { unsigned long load_delta, *p; pgdval_t *pgd; @@ -62,7 +68,7 @@ void __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)