From patchwork Wed Oct 11 20:30:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Garnier X-Patchwork-Id: 10000593 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 EB7986037F for ; Wed, 11 Oct 2017 20:33:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFF8328B37 for ; Wed, 11 Oct 2017 20:33:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D39E728B6A; Wed, 11 Oct 2017 20:33:54 +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 67B5F28B37 for ; Wed, 11 Oct 2017 20:33:54 +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 1e2NfL-0000tq-2V; Wed, 11 Oct 2017 20:31:43 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2NfJ-0000qt-Hf for xen-devel@lists.xenproject.org; Wed, 11 Oct 2017 20:31:41 +0000 Received: from [85.158.137.68] by server-13.bemta-3.messagelabs.com id 07/04-00706-CAF7ED95; Wed, 11 Oct 2017 20:31:40 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrBIsWRWlGSWpSXmKPExsVyMfTAZt1V9fc iDXZ8Urf4vmUykwOjx+EPV1gCGKNYM/OS8isSWDMW9L5jLHglW7H+4ErGBsYlkl2MXBxCAjMY JZqPH2UDcVgEXrFI9Hc1sII4EgL9rBKH2tqBHE4gJ0tiw42ZzBB2msSPDRuYIOxKid7f7xhBb CEBJYmtG5YyQ9j/GCV+TdYAsdkEtCT2NMxnAhkqInBCWGLTh9+MIA6zwBkmieN9J8E2CAv4SB w82wrWzSKgKnF391P2LkYODl4BS4lp18UglllIHDt2mAXE5gQKf994CGqZhcTKWedZJzAKLmB kWMWoXpxaVJZapGuol1SUmZ5RkpuYmaNraGCsl5taXJyYnpqTmFSsl5yfu4kRGHIMQLCDcflH p0OMkhxMSqK8D2PuRQrxJeWnVGYkFmfEF5XmpBYfYpTh4FCS4J1eB5QTLEpNT61Iy8wBBj9MW oKDR0mEtwkkzVtckJhbnJkOkTrFaMlx4c6lP0wcB/bcApIdN+/+YRJiycvPS5US5y0FaRAAac gozYMbB4vQS4yyUsK8jEAHCvEUpBblZpagyr9iFOdgVBLmPQwyhSczrwRu6yugg5iADhJNuwN yUEkiQkqqgdHsgZ/qUjtHlqsmK3Lnt+tvnKCbX5vBN7XVz7v2dIb5o7vTVVtE/9tlMd0vkb/3 osSr1VtWKovTu3Efi4qcYdXTuT2ZujqbOq94B5e5Kti/2KX6ZtGKSc9jxKX3lG5Re3idN+/Um ZfNDtNlip9MuDqlliFDPff4nOCpPDxzBJZvknG7vfRSoxJLcUaioRZzUXEiAO8zjVzLAgAA X-Env-Sender: thgarnie@google.com X-Msg-Ref: server-2.tower-31.messagelabs.com!1507753897!98178153!1 X-Originating-IP: [209.85.192.179] 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 33981 invoked from network); 11 Oct 2017 20:31:38 -0000 Received: from mail-pf0-f179.google.com (HELO mail-pf0-f179.google.com) (209.85.192.179) by server-2.tower-31.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 11 Oct 2017 20:31:38 -0000 Received: by mail-pf0-f179.google.com with SMTP id p87so1955487pfj.3 for ; Wed, 11 Oct 2017 13:31:38 -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=vMTIcJiAeIgD4jopOAXuz9bpqK+Ya/XfwK7qXpumcYI=; b=JNm6NZ5bAPpYhlR5pKc75qYfIXOHcVtle9t2Wq0FyBWtcXxX3DUAovWrQTFrIqU1MD SF3XlA3Y2XuaT5COIt89enp7vzEe4UONvjHuFdKOnO3Z3O5JaCn3dM5OjcHr0yUJ4ttQ eMFcqu5Ga0V32ad5vbeBYmg1yAtkMQtIuALXO8Y+Ylpv5BzmS0u0cDKi0EGdsiz5K38z Afr31wczWSmE5SKfk5LVMKG5iK7niPEtA8rW0FEEhuqMlZ89Q2MmCKTR+YHgj0cl2uRT +7CG2irWFjbuxKgtI0Eo2fNLN2w94oA5eoPLSVXjvDSufRBy7rdoAhRCuDPbX8OvZEY2 DFwQ== 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=vMTIcJiAeIgD4jopOAXuz9bpqK+Ya/XfwK7qXpumcYI=; b=hB1FUdbgvnJIGkTeDIKxAn/U5z+ntjTMV3kldsYCTswAr3wM/7BCBCqJoFGAWSHjl4 NQ73LdWpmUr4hQUN01CpxwT+s3xc/YQ0fCeZtsVX+YQnEraBW3lFM2It+StcxcPC+evy +3zdbDF8ru71tBxz0RBDdcVeKEa8XVQgN3+sHDw4bW9xZscnTLH8+3W3YABln6Nm09xC eNYTb5Y8KY/rRBjx/7p9TSsPQNkwYOLRrB4weQAvQdcPkmAPwdorHUnqkinwSkyD/rOf eYp6Lx56JHAVhHakJvDiwmg0NsOCCw1NVUYhTHgAhpmk7mI58fJJNDpYeNo4AyEbFNFq 9xOQ== X-Gm-Message-State: AMCzsaWBkV1RvXA/wcZKBb0vPC8GA+OOifUBvd091nAYcaYwFQcTUu6L 8mJEqSBRycC9afpMm8n6nAo1KQ== X-Google-Smtp-Source: AOwi7QArJQFiX+7qZm2zaSLW3daYMThmWNi4aoqR76HiWiBeZoXyhbXAmXzaMMZJ/ZrJ7cTlZdSFVA== X-Received: by 10.98.158.211 with SMTP id f80mr198674pfk.156.1507753896348; Wed, 11 Oct 2017 13:31:36 -0700 (PDT) Received: from skynet.sea.corp.google.com ([172.31.92.33]) by smtp.gmail.com with ESMTPSA id n12sm20691913pfb.149.2017.10.11.13.31.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 11 Oct 2017 13:31:35 -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 Date: Wed, 11 Oct 2017 13:30:20 -0700 Message-Id: <20171011203027.11248-21-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> Cc: linux-arch@vger.kernel.org, kvm@vger.kernel.org, linux-pm@vger.kernel.org, x86@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-sparse@vger.kernel.org, linux-crypto@vger.kernel.org, kernel-hardening@lists.openwall.com, xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v1 20/27] x86/ftrace: Adapt function tracing 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 When using -fPIE/PIC with function tracing, the compiler generates a call through the GOT (call *__fentry__@GOTPCREL). This instruction takes 6 bytes instead of 5 on the usual relative call. If PIE is enabled, replace the 6th byte of the GOT call by a 1-byte nop so ftrace can handle the previous 5-bytes as before. 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/include/asm/ftrace.h | 6 ++++-- arch/x86/include/asm/sections.h | 4 ++++ arch/x86/kernel/ftrace.c | 42 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index eccd0ac6bc38..183990157a5e 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -24,9 +24,11 @@ extern void __fentry__(void); static inline unsigned long ftrace_call_adjust(unsigned long addr) { /* - * addr is the address of the mcount call instruction. - * recordmcount does the necessary offset calculation. + * addr is the address of the mcount call instruction. PIE has always a + * byte added to the start of the function. */ + if (IS_ENABLED(CONFIG_X86_PIE)) + addr -= 1; return addr; } diff --git a/arch/x86/include/asm/sections.h b/arch/x86/include/asm/sections.h index 2f75f30cb2f6..6b2d496cf1aa 100644 --- a/arch/x86/include/asm/sections.h +++ b/arch/x86/include/asm/sections.h @@ -11,4 +11,8 @@ extern struct exception_table_entry __stop___ex_table[]; extern char __end_rodata_hpage_align[]; #endif +#if defined(CONFIG_X86_PIE) +extern char __start_got[], __end_got[]; +#endif + #endif /* _ASM_X86_SECTIONS_H */ diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 9bef1bbeba63..a253601e783b 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -101,7 +101,7 @@ static const unsigned char *ftrace_nop_replace(void) static int ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code, - unsigned const char *new_code) + unsigned const char *new_code) { unsigned char replaced[MCOUNT_INSN_SIZE]; @@ -134,6 +134,44 @@ ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code, return 0; } +/* Bytes before call GOT offset */ +const unsigned char got_call_preinsn[] = { 0xff, 0x15 }; + +static int +ftrace_modify_initial_code(unsigned long ip, unsigned const char *old_code, + unsigned const char *new_code) +{ + unsigned char replaced[MCOUNT_INSN_SIZE + 1]; + + ftrace_expected = old_code; + + /* + * If PIE is not enabled or no GOT call was found, default to the + * original approach to code modification. + */ + if (!IS_ENABLED(CONFIG_X86_PIE) + || probe_kernel_read(replaced, (void *)ip, sizeof(replaced)) + || memcmp(replaced, got_call_preinsn, sizeof(got_call_preinsn))) + return ftrace_modify_code_direct(ip, old_code, new_code); + + /* + * Build a nop slide with a 5-byte nop and 1-byte nop to keep the ftrace + * hooking algorithm working with the expected 5 bytes instruction. + */ + memcpy(replaced, new_code, MCOUNT_INSN_SIZE); + replaced[MCOUNT_INSN_SIZE] = ideal_nops[1][0]; + + ip = text_ip_addr(ip); + + if (probe_kernel_write((void *)ip, replaced, sizeof(replaced))) + return -EPERM; + + sync_core(); + + return 0; + +} + int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) { @@ -152,7 +190,7 @@ int ftrace_make_nop(struct module *mod, * just modify the code directly. */ if (addr == MCOUNT_ADDR) - return ftrace_modify_code_direct(rec->ip, old, new); + return ftrace_modify_initial_code(rec->ip, old, new); ftrace_expected = NULL;