Received: by 10.223.148.5 with SMTP id 5csp7314784wrq; Thu, 18 Jan 2018 04:03:56 -0800 (PST) X-Google-Smtp-Source: ACJfBovCNHK1q/q/9tXJKCJs4557rTyNb4QYtZ+o4Bc7FNPyJ9cmBFp/tq7MxXmxCktIcsDdjXSP X-Received: by 10.36.80.135 with SMTP id m129mr27724642itb.134.1516277036543; Thu, 18 Jan 2018 04:03:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516277036; cv=none; d=google.com; s=arc-20160816; b=L8JTvyZd9Y9WzU0alOJnAOL0LqcK3UV8yZkhBBHwAiaNfqTiV47+LsqRmc1drZiaUm r6gfMzdb7Mx48i3kYOpZPs6PR1MjIhEpmQt/9kizGdHV8kkwJk23tZqjy6XlILms3jJb 25yODPNBzVI2GPF9uo2xHfl7gIjaB9dQmbmMcdngv2drF9VTzLntAl8HsUZzy+SYc+uE N2nV7l/t513OeC6ZhUZu8OoaHg/rlJTZlafwNEDT3xsyKfv6V+iRLDLBLEIE+8fOryCg k5dki8dHQKWwW+f9Fpg0IuCzaIkiqEvjA2+sqHrFQSG0hddiypUQPdDeksETjYW2/jNR 4eQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dmarc-filter:arc-authentication-results; bh=t8M4Y5d6DTFhYRrh9s1WFZDIyPPNMud7ME5yK7dJk9g=; b=KxCXfFHFXIJbZwGQzCFYw3vG3SKm5ejAo9lnNnqMqCAhjMwOsgeWRnRzNH6kFhYclc evsst6yw9cCp8eabBkP+uPvKSSwAZmFipWwRHw7aE03NgsCfdj2RurB/NQFfoErCxhar a7eDWaP60+rf0kz/RITE6KZS1gMba9iKrfef2fzffg4svATlDwp1bjROisq2dTj73AsC nNbrkPUf4PwRA1EqlqLQtnk/x+LeiZcSD5QbQUdje75+iQkYQWtg0XupM8fDW8GoGGhm WmHLHgOGIWs1v1J0bFldh0OJla3rCZn/7acLRVwaMiuw5d/tgrqO6TLdx3WuNCHlJhAd Hf9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si4421200ply.20.2018.01.18.04.03.42; Thu, 18 Jan 2018 04:03:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932427AbeARMCo (ORCPT + 99 others); Thu, 18 Jan 2018 07:02:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:40412 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932409AbeARMCm (ORCPT ); Thu, 18 Jan 2018 07:02:42 -0500 Received: from localhost.localdomain (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 977BB2175B; Thu, 18 Jan 2018 12:02:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 977BB2175B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=mhiramat@kernel.org From: Masami Hiramatsu To: Ingo Molnar , David Woodhouse Cc: Masami Hiramatsu , linux-kernel@vger.kernel.org, Andi Kleen , Greg Kroah-Hartman , Arjan van de Ven , Peter Zijlstra , Ananth N Mavinakayanahalli , Thomas Gleixner , "H . Peter Anvin" Subject: [PATCH v1 tip/master 3/3] kprobes/x86: Disable optimizing on the function jumps to indirect thunk Date: Thu, 18 Jan 2018 21:02:18 +0900 Message-Id: <151627693827.24682.6403436341555853012.stgit@devbox> X-Mailer: git-send-email 2.13.6 In-Reply-To: <151627684798.24682.2979775081582774869.stgit@devbox> References: <151627684798.24682.2979775081582774869.stgit@devbox> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since indirect jump instructions will be replaced by jump to __x86_indirect_thunk_*, those jmp instruction must be treated as an indirect jump. Since optprobe prohibits to optimize probes in the function which uses an indirect jump, it also needs to find out the function which jump to __x86_indirect_thunk_* and disable optimization. This adds a check that the jump target address is between the __indirect_thunk_start/end when optimizing kprobe. Signed-off-by: Masami Hiramatsu --- arch/x86/kernel/kprobes/opt.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c index e941136e24d8..203d398802a3 100644 --- a/arch/x86/kernel/kprobes/opt.c +++ b/arch/x86/kernel/kprobes/opt.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "common.h" @@ -203,7 +204,7 @@ static int copy_optimized_instructions(u8 *dest, u8 *src, u8 *real) } /* Check whether insn is indirect jump */ -static int insn_is_indirect_jump(struct insn *insn) +static int __insn_is_indirect_jump(struct insn *insn) { return ((insn->opcode.bytes[0] == 0xff && (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */ @@ -237,6 +238,26 @@ static int insn_jump_into_range(struct insn *insn, unsigned long start, int len) return (start <= target && target <= start + len); } +static int insn_is_indirect_jump(struct insn *insn) +{ + int ret = __insn_is_indirect_jump(insn); + +#ifdef CONFIG_RETPOLINE + /* + * Jump to x86_indirect_thunk_* is treated as an indirect jump. + * Note that even with CONFIG_RETPOLINE=y, the kernel compiled with + * older gcc may use indirect jump. So we add this check instead of + * replace indirect-jump check. + */ + if (!ret) + ret = insn_jump_into_range(insn, + (unsigned long)__indirect_thunk_start, + (unsigned long)__indirect_thunk_end - + (unsigned long)__indirect_thunk_start); +#endif + return ret; +} + /* Decode whole function to ensure any instructions don't jump into target */ static int can_optimize(unsigned long paddr) {