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: 9850055 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 AB93860392 for ; Tue, 18 Jul 2017 22:37:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 97582285CB for ; Tue, 18 Jul 2017 22:37:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8B037285D8; Tue, 18 Jul 2017 22:37:06 +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 A18B2285CB for ; Tue, 18 Jul 2017 22:37:05 +0000 (UTC) Received: (qmail 28064 invoked by uid 550); 18 Jul 2017 22:34:47 -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 27970 invoked from network); 18 Jul 2017 22:34:45 -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=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=KTpcMvIJScGlLEkSZOzeJeiHozzE8fQ/B9d9lG11TXyU/6blSwhqbkm3v3i2evmHQl RRKXlmki5PHOmFFGcgQtPZ2aaZpPVQ1OhI9+6fRGZz9yTpq+NLFQQ2+Wl0FEIbK81Hi8 vfYAH5IMXdDakbjuUfPiVbbEXA8L0c+RyWF3LRRITEL1tzNNsO25J5TBx3FBSvv5PAA4 6lNORpfiKCIRyeowS+dDSzkKy32fjTsnqKC5780h9gqOaVwW+nJpOxDToLd6PMcCvOGT AqOZ7fd9A3MOOr6S48Ied1lawnTjJZyQupJD9GZESmkSMZI1VBXQkcJYj13DOSTmRhNX VUTw== X-Gm-Message-State: AIVw112OIeq7AnmBzavM36c29x2C2waM8ASUU5M0Zx7FcBQ9uNdWirQ3 658Y9+JpLh4q8Y7A X-Received: by 10.99.96.85 with SMTP id u82mr4037659pgb.214.1500417272984; 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 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: 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> Subject: [kernel-hardening] [RFC 15/22] 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 | 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)