Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp4364798pxb; Tue, 25 Jan 2022 08:51:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJw1RWMRlm58pcwVYJMh6NLqNJzI2XlJkka55+6I6VsPeRgD81pSV8Gjp1pIZRSGT0BoL/tb X-Received: by 2002:a17:906:9b91:: with SMTP id dd17mr1379190ejc.193.1643129514344; Tue, 25 Jan 2022 08:51:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643129514; cv=none; d=google.com; s=arc-20160816; b=Y5sFiWtZI7Kb16NyJzzrTPUDNEe6tzIywW8YizkZA2Sd9MISjj88GoDgnvSLx5KbVp Yyi/0BcBSOUZ3KJCkvBGbeup8o9PKM5b5TIrMMY/65HdDe+/5cggq84zIjG21VY3wdHf hnVyBNwYFVHhYCst3BByCRHm+JLl9ku6IopK6Rd4f4yTixyYhbYhQQIbSEamMP4By6hR 8QZMm50C8a8xSujQdt0LeaEZE4MkIMTiz6Y3EUQZ/lI4VEny3WXpWomaW6SFmR+1Pqs2 9TYdGL38wXh9rhrVKGn6WH7SrvCuKD4UEVU69PNnOHwh8i0JwYdtMKUgmz6p71SoOGaL UDww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bUJcVyDcp4XDzl3/WN4YJwCdjxi1bnre9KExg1oGySw=; b=asCoyk7GUaFcJd4W7WZmjGg9Oq1jkr6PNsfcjToEImFJT0UBST9rMWTpSwlrFiKwzS xLGLI//M2F5LoEzMmS0qpYGNZObE7K2qOvS5Y0yzDgG2VCknuwoZKQEy2VfEl40rgqfD rKd+XGFZjJv9pRrRLdd5Z/I8Qz/r5ba2uc2GLyvzsSg24vyvGzTDhHchxRuh9E0o1+vU AGv6EkmmCI2A2RX1GvSrsMG5kI558/AAHxvBIyZ9zPzxoxJZcJatqneJMgpaJpEX1brz 0I2m9mN6YFdTbe68dPhSW5/LeaaW0dIce5MtePtyg1bGJSpSbcA5OGTLwjUiNdi6CKvb kdog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tVf6tJAc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e21si10422508edj.110.2022.01.25.08.51.29; Tue, 25 Jan 2022 08:51:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tVf6tJAc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381633AbiAYMQ6 (ORCPT + 99 others); Tue, 25 Jan 2022 07:16:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354551AbiAYMM2 (ORCPT ); Tue, 25 Jan 2022 07:12:28 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6527C061760; Tue, 25 Jan 2022 04:12:27 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 868DC61045; Tue, 25 Jan 2022 12:12:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7BD7EC340E0; Tue, 25 Jan 2022 12:12:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643112747; bh=ZM7uIiZg0njk6Wlbh5s0Fneqca+sDPI3//GwpeW5J+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tVf6tJAcQ/4y2AkIzpx2sPZyOknzR0dw1GbgrB5nHrEtMwcL1Mwl52Paw8Dwdo3x+ IhNRiPyS6D/VSX3MERhSBprZWfIMr6C61WKDS1CfiWhCe+HYcasY1Uz4BLrM/C5FlM +sNTCbS5PlcKZisdtwdyliq00JVnx8JZWXV3BO5Vt2JdBKNt+QxL3GzG3KCfsvj5Jc fUwGoiM1/Ge3LD9LPq8yzu+KcL1r/AAhHAuVa2nwLZGX4I0mx3hdUFk1teXGHThEDf 5tORsHSs6yfq2TKnY0M/fKviukSUTXWpuawVurkhqiFJLIDxVFIRV50UZ2lM8jR6EA CRp5aljadsLew== From: Masami Hiramatsu To: Jiri Olsa , Alexei Starovoitov Cc: Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" Subject: [PATCH v5 4/9] rethook: x86: Add rethook x86 implementation Date: Tue, 25 Jan 2022 21:12:21 +0900 Message-Id: <164311274101.1933078.1333348456650471950.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164311269435.1933078.6963769885544050138.stgit@devnote2> References: <164311269435.1933078.6963769885544050138.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add rethook for x86 implementation. Most of the code has been copied from kretprobes on x86. Signed-off-by: Masami Hiramatsu --- Changes in v5: - Fix a build error if !CONFIG_KRETPROBES and !CONFIG_RETHOOK. Changes in v4: - fix stack backtrace as same as kretprobe does. --- arch/x86/Kconfig | 1 arch/x86/include/asm/unwind.h | 8 ++- arch/x86/kernel/Makefile | 1 arch/x86/kernel/kprobes/common.h | 1 arch/x86/kernel/rethook.c | 115 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/rethook.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 5c2ccb85f2ef..0a7d48a63787 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -219,6 +219,7 @@ config X86 select HAVE_KPROBES_ON_FTRACE select HAVE_FUNCTION_ERROR_INJECTION select HAVE_KRETPROBES + select HAVE_RETHOOK select HAVE_KVM select HAVE_LIVEPATCH if X86_64 select HAVE_MIXED_BREAKPOINTS_REGS diff --git a/arch/x86/include/asm/unwind.h b/arch/x86/include/asm/unwind.h index 2a1f8734416d..192df5b2094d 100644 --- a/arch/x86/include/asm/unwind.h +++ b/arch/x86/include/asm/unwind.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -16,7 +17,7 @@ struct unwind_state { unsigned long stack_mask; struct task_struct *task; int graph_idx; -#ifdef CONFIG_KRETPROBES +#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK) struct llist_node *kr_cur; #endif bool error; @@ -107,6 +108,11 @@ static inline unsigned long unwind_recover_kretprobe(struct unwind_state *state, unsigned long addr, unsigned long *addr_p) { +#ifdef CONFIG_RETHOOK + if (is_rethook_trampoline(addr)) + return rethook_find_ret_addr(state->task, (unsigned long)addr_p, + &state->kr_cur); +#endif #ifdef CONFIG_KRETPROBES return is_kretprobe_trampoline(addr) ? kretprobe_find_ret_addr(state->task, addr_p, &state->kr_cur) : diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 2ff3e600f426..66593d8c4d74 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -106,6 +106,7 @@ obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o obj-$(CONFIG_X86_TSC) += trace_clock.o obj-$(CONFIG_TRACING) += trace.o +obj-$(CONFIG_RETHOOK) += rethook.o obj-$(CONFIG_CRASH_CORE) += crash_core_$(BITS).o obj-$(CONFIG_KEXEC_CORE) += machine_kexec_$(BITS).o obj-$(CONFIG_KEXEC_CORE) += relocate_kernel_$(BITS).o crash.o diff --git a/arch/x86/kernel/kprobes/common.h b/arch/x86/kernel/kprobes/common.h index 7d3a2e2daf01..c993521d4933 100644 --- a/arch/x86/kernel/kprobes/common.h +++ b/arch/x86/kernel/kprobes/common.h @@ -6,6 +6,7 @@ #include #include +#include #ifdef CONFIG_X86_64 diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c new file mode 100644 index 000000000000..f2f3b9526e43 --- /dev/null +++ b/arch/x86/kernel/rethook.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c. + */ +#include +#include +#include + +#include "kprobes/common.h" + +/* + * Called from arch_rethook_trampoline + */ +__used __visible void arch_rethook_trampoline_callback(struct pt_regs *regs) +{ + unsigned long *frame_pointer; + + /* fixup registers */ + regs->cs = __KERNEL_CS; +#ifdef CONFIG_X86_32 + regs->gs = 0; +#endif + regs->ip = (unsigned long)&arch_rethook_trampoline; + regs->orig_ax = ~0UL; + regs->sp += sizeof(long); + frame_pointer = ®s->sp + 1; + + /* + * The return address at 'frame_pointer' is recovered by the + * arch_rethook_fixup_return() which called from this + * rethook_trampoline_handler(). + */ + rethook_trampoline_handler(regs, (unsigned long)frame_pointer); + + /* + * Copy FLAGS to 'pt_regs::sp' so that arch_rethook_trapmoline() + * can do RET right after POPF. + */ + regs->sp = regs->flags; +} +NOKPROBE_SYMBOL(arch_rethook_trampoline_callback); + +/* + * When a target function returns, this code saves registers and calls + * arch_rethook_trampoline_callback(), which calls the rethook handler. + */ +asm( + ".text\n" + ".global arch_rethook_trampoline\n" + ".type arch_rethook_trampoline, @function\n" + "arch_rethook_trampoline:\n" +#ifdef CONFIG_X86_64 + /* Push a fake return address to tell the unwinder it's a kretprobe. */ + " pushq $arch_rethook_trampoline\n" + UNWIND_HINT_FUNC + /* Save the 'sp - 8', this will be fixed later. */ + " pushq %rsp\n" + " pushfq\n" + SAVE_REGS_STRING + " movq %rsp, %rdi\n" + " call arch_rethook_trampoline_callback\n" + RESTORE_REGS_STRING + /* In the callback function, 'regs->flags' is copied to 'regs->sp'. */ + " addq $8, %rsp\n" + " popfq\n" +#else + /* Push a fake return address to tell the unwinder it's a kretprobe. */ + " pushl $arch_rethook_trampoline\n" + UNWIND_HINT_FUNC + /* Save the 'sp - 4', this will be fixed later. */ + " pushl %esp\n" + " pushfl\n" + SAVE_REGS_STRING + " movl %esp, %eax\n" + " call arch_rethook_trampoline_callback\n" + RESTORE_REGS_STRING + /* In the callback function, 'regs->flags' is copied to 'regs->sp'. */ + " addl $4, %esp\n" + " popfl\n" +#endif + " ret\n" + ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n" +); +NOKPROBE_SYMBOL(arch_rethook_trampoline); +/* + * arch_rethook_trampoline() skips updating frame pointer. The frame pointer + * saved in arch_rethook_trampoline_callback() points to the real caller + * function's frame pointer. Thus the arch_rethook_trampoline() doesn't have + * a standard stack frame with CONFIG_FRAME_POINTER=y. + * Let's mark it non-standard function. Anyway, FP unwinder can correctly + * unwind without the hint. + */ +STACK_FRAME_NON_STANDARD_FP(arch_rethook_trampoline); + +/* This is called from rethook_trampoline_handler(). */ +void arch_rethook_fixup_return(struct pt_regs *regs, + unsigned long correct_ret_addr) +{ + unsigned long *frame_pointer = ®s->sp + 1; + + /* Replace fake return address with real one. */ + *frame_pointer = correct_ret_addr; +} + +void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs) +{ + unsigned long *stack = (unsigned long *)regs->sp; + + rh->ret_addr = stack[0]; + rh->frame = regs->sp; + + /* Replace the return addr with trampoline addr */ + stack[0] = (unsigned long) arch_rethook_trampoline; +} +NOKPROBE_SYMBOL(arch_rethook_prepare);