From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D87CC4332F for ; Tue, 5 Apr 2022 09:36:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=l+oDOBY+QEJYmBd6aEPsK/vW8+Tl0eusHg58+dFQ0J0=; b=d/xm9sKsBqjT95 5sPRu4RK2Mjo+7+NofFaZkZ55txWQGDr3Eyx/agvH1CB45npFeKIEwXfFqSXE2AHcJq1yNdbebn9U ykGIdYpYcskZSY+r5GRygjYo5c/hgxh7kwhBTJe49ZBHQ8NEaGSDS1bLAI/ehOssdLgPPPxMzEI1M 5Gb7NwsFu3yUDrositz4VfEnzS+3pNUy7dKI9KHRpvIPW6eF8fAicnpAhw9xo/ZZzoPYXa6QKQrb5 QZmTr4/2Tb8P0fJxBMfcaRCUd7Bob0xWKugO1JPqyEL3360j2hdxS5ItrW2TbdW1B+qTDcWOLZDEJ kyW+Z3zHNMAFV8pJh2lQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nbfai-000SE6-9y; Tue, 05 Apr 2022 09:35:12 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nbfZg-000Rae-4o for linux-arm-kernel@lists.infradead.org; Tue, 05 Apr 2022 09:34:10 +0000 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 ams.source.kernel.org (Postfix) with ESMTPS id BD23CB81C85; Tue, 5 Apr 2022 09:34:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A706C385A2; Tue, 5 Apr 2022 09:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649151245; bh=qU7NQO8a/GNTqpF3kK+hxQlG2bDuw47+HzlGrFENsk0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N77R/4na/+s2mEBMETZddTv/xM3MLgIvIy050lDk/AhaphAO0eWTunjvlgZm8fOC5 WebyxBQ0r8tyIFtxrMKghV1OhO94uWoZBfWAszOOMesbTJsGUYZk2N903xLDSL2F+G d4OasRXoX+XHMKSEhhymIalNknUyVRlGzbeHsuF42qJk/BAwfOkzEqtujJHKSQAzFF J71kaKMXg2UmOZ4Bw+dCL+31g+eun6BLFnKlFJ431SgXt10/xUNmUhCRwTArerMb42 cohDMpn2/Tq2lFdFpK0oNIu6gecLy7WrptY3JZ0C0btnVz3EtBtUK8BrJxAHddNF1w stIFOb9r3nlFQ== From: Masami Hiramatsu To: Alexei Starovoitov , Alexei Starovoitov Cc: Daniel Borkmann , Shubham Bansal , Andrii Nakryiko , Masami Hiramatsu , bpf@vger.kernel.org, kernel-team@fb.com, Jiri Olsa , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" , linux-kernel@vger.kernel.org, Mark Rutland , Will Deacon , Ard Biesheuvel , Russell King , Catalin Marinas , linux-arm-kernel@lists.infradead.org Subject: [PATCH bpf 2/4] rethook, fprobe, kprobes: Check a failure in the rethook_hook() Date: Tue, 5 Apr 2022 18:33:59 +0900 Message-Id: <164915123885.982637.5653959785968470135.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164915121498.982637.12787715964983738566.stgit@devnote2> References: <164915121498.982637.12787715964983738566.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220405_023408_602170_BFB5C0AB X-CRM114-Status: GOOD ( 19.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Since there are possible to fail to hook the function return (depends on archtecutre implememtation), rethook_hook() should return the error in that case and caller must check it. Signed-off-by: Masami Hiramatsu --- arch/x86/kernel/rethook.c | 4 +++- include/linux/rethook.h | 4 ++-- kernel/kprobes.c | 8 +++++--- kernel/trace/fprobe.c | 5 ++++- kernel/trace/rethook.c | 12 ++++++++++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c index 8a1c0111ae79..c92b4875e3b9 100644 --- a/arch/x86/kernel/rethook.c +++ b/arch/x86/kernel/rethook.c @@ -114,7 +114,7 @@ void arch_rethook_fixup_return(struct pt_regs *regs, } NOKPROBE_SYMBOL(arch_rethook_fixup_return); -void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount) +int arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount) { unsigned long *stack = (unsigned long *)regs->sp; @@ -123,5 +123,7 @@ void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mc /* Replace the return addr with trampoline addr */ stack[0] = (unsigned long) arch_rethook_trampoline; + + return 0; } NOKPROBE_SYMBOL(arch_rethook_prepare); diff --git a/include/linux/rethook.h b/include/linux/rethook.h index c8ac1e5afcd1..07b9c6663b8e 100644 --- a/include/linux/rethook.h +++ b/include/linux/rethook.h @@ -63,12 +63,12 @@ void rethook_free(struct rethook *rh); void rethook_add_node(struct rethook *rh, struct rethook_node *node); struct rethook_node *rethook_try_get(struct rethook *rh); void rethook_recycle(struct rethook_node *node); -void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount); +int rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount); unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame, struct llist_node **cur); /* Arch dependent code must implement arch_* and trampoline code */ -void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount); +int arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount); void arch_rethook_trampoline(void); /** diff --git a/kernel/kprobes.c b/kernel/kprobes.c index dbe57df2e199..7fd7f1195bde 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2109,10 +2109,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) ri = container_of(rhn, struct kretprobe_instance, node); - if (rp->entry_handler && rp->entry_handler(ri, regs)) + if (rp->entry_handler && rp->entry_handler(ri, regs)) { rethook_recycle(rhn); - else - rethook_hook(rhn, regs, kprobe_ftrace(p)); + } else if (rethook_hook(rhn, regs, kprobe_ftrace(p)) < 0) { + rethook_recycle(rhn); + rp->nmissed++; + } return 0; } diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 89d9f994ebb0..d3b13294d545 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -48,7 +48,10 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, } fpr = container_of(rh, struct fprobe_rethook_node, node); fpr->entry_ip = ip; - rethook_hook(rh, ftrace_get_regs(fregs), true); + if (rethook_hook(rh, ftrace_get_regs(fregs), true) < 0) { + rethook_recycle(rh); + fp->nmissed++; + } } out: diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index b56833700d23..e7db83438e45 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -174,11 +174,19 @@ NOKPROBE_SYMBOL(rethook_try_get); * from ftrace (mcount) callback, @mcount must be set true. If this is called * from the real function entry (e.g. kprobes) @mcount must be set false. * This is because the way to hook the function return depends on the context. + * This returns 0 if succeeded to hook the function return, or -errno if + * failed. */ -void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount) +int rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount) { - arch_rethook_prepare(node, regs, mcount); + int ret; + + ret = arch_rethook_prepare(node, regs, mcount); + if (ret < 0) + return ret; + __llist_add(&node->llist, ¤t->rethooks); + return 0; } NOKPROBE_SYMBOL(rethook_hook); _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD600C3527B for ; Tue, 5 Apr 2022 21:37:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380881AbiDEVha (ORCPT ); Tue, 5 Apr 2022 17:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348521AbiDEJrz (ORCPT ); Tue, 5 Apr 2022 05:47:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 989289FDD; Tue, 5 Apr 2022 02:34:06 -0700 (PDT) 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 1CBBC616D9; Tue, 5 Apr 2022 09:34:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A706C385A2; Tue, 5 Apr 2022 09:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649151245; bh=qU7NQO8a/GNTqpF3kK+hxQlG2bDuw47+HzlGrFENsk0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N77R/4na/+s2mEBMETZddTv/xM3MLgIvIy050lDk/AhaphAO0eWTunjvlgZm8fOC5 WebyxBQ0r8tyIFtxrMKghV1OhO94uWoZBfWAszOOMesbTJsGUYZk2N903xLDSL2F+G d4OasRXoX+XHMKSEhhymIalNknUyVRlGzbeHsuF42qJk/BAwfOkzEqtujJHKSQAzFF J71kaKMXg2UmOZ4Bw+dCL+31g+eun6BLFnKlFJ431SgXt10/xUNmUhCRwTArerMb42 cohDMpn2/Tq2lFdFpK0oNIu6gecLy7WrptY3JZ0C0btnVz3EtBtUK8BrJxAHddNF1w stIFOb9r3nlFQ== From: Masami Hiramatsu To: Alexei Starovoitov , Alexei Starovoitov Cc: Daniel Borkmann , Shubham Bansal , Andrii Nakryiko , Masami Hiramatsu , bpf@vger.kernel.org, kernel-team@fb.com, Jiri Olsa , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" , linux-kernel@vger.kernel.org, Mark Rutland , Will Deacon , Ard Biesheuvel , Russell King , Catalin Marinas , linux-arm-kernel@lists.infradead.org Subject: [PATCH bpf 2/4] rethook,fprobe,kprobes: Check a failure in the rethook_hook() Date: Tue, 5 Apr 2022 18:33:59 +0900 Message-Id: <164915123885.982637.5653959785968470135.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164915121498.982637.12787715964983738566.stgit@devnote2> References: <164915121498.982637.12787715964983738566.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 Since there are possible to fail to hook the function return (depends on archtecutre implememtation), rethook_hook() should return the error in that case and caller must check it. Signed-off-by: Masami Hiramatsu --- arch/x86/kernel/rethook.c | 4 +++- include/linux/rethook.h | 4 ++-- kernel/kprobes.c | 8 +++++--- kernel/trace/fprobe.c | 5 ++++- kernel/trace/rethook.c | 12 ++++++++++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c index 8a1c0111ae79..c92b4875e3b9 100644 --- a/arch/x86/kernel/rethook.c +++ b/arch/x86/kernel/rethook.c @@ -114,7 +114,7 @@ void arch_rethook_fixup_return(struct pt_regs *regs, } NOKPROBE_SYMBOL(arch_rethook_fixup_return); -void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount) +int arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount) { unsigned long *stack = (unsigned long *)regs->sp; @@ -123,5 +123,7 @@ void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mc /* Replace the return addr with trampoline addr */ stack[0] = (unsigned long) arch_rethook_trampoline; + + return 0; } NOKPROBE_SYMBOL(arch_rethook_prepare); diff --git a/include/linux/rethook.h b/include/linux/rethook.h index c8ac1e5afcd1..07b9c6663b8e 100644 --- a/include/linux/rethook.h +++ b/include/linux/rethook.h @@ -63,12 +63,12 @@ void rethook_free(struct rethook *rh); void rethook_add_node(struct rethook *rh, struct rethook_node *node); struct rethook_node *rethook_try_get(struct rethook *rh); void rethook_recycle(struct rethook_node *node); -void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount); +int rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount); unsigned long rethook_find_ret_addr(struct task_struct *tsk, unsigned long frame, struct llist_node **cur); /* Arch dependent code must implement arch_* and trampoline code */ -void arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount); +int arch_rethook_prepare(struct rethook_node *node, struct pt_regs *regs, bool mcount); void arch_rethook_trampoline(void); /** diff --git a/kernel/kprobes.c b/kernel/kprobes.c index dbe57df2e199..7fd7f1195bde 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2109,10 +2109,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) ri = container_of(rhn, struct kretprobe_instance, node); - if (rp->entry_handler && rp->entry_handler(ri, regs)) + if (rp->entry_handler && rp->entry_handler(ri, regs)) { rethook_recycle(rhn); - else - rethook_hook(rhn, regs, kprobe_ftrace(p)); + } else if (rethook_hook(rhn, regs, kprobe_ftrace(p)) < 0) { + rethook_recycle(rhn); + rp->nmissed++; + } return 0; } diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 89d9f994ebb0..d3b13294d545 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -48,7 +48,10 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, } fpr = container_of(rh, struct fprobe_rethook_node, node); fpr->entry_ip = ip; - rethook_hook(rh, ftrace_get_regs(fregs), true); + if (rethook_hook(rh, ftrace_get_regs(fregs), true) < 0) { + rethook_recycle(rh); + fp->nmissed++; + } } out: diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index b56833700d23..e7db83438e45 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -174,11 +174,19 @@ NOKPROBE_SYMBOL(rethook_try_get); * from ftrace (mcount) callback, @mcount must be set true. If this is called * from the real function entry (e.g. kprobes) @mcount must be set false. * This is because the way to hook the function return depends on the context. + * This returns 0 if succeeded to hook the function return, or -errno if + * failed. */ -void rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount) +int rethook_hook(struct rethook_node *node, struct pt_regs *regs, bool mcount) { - arch_rethook_prepare(node, regs, mcount); + int ret; + + ret = arch_rethook_prepare(node, regs, mcount); + if (ret < 0) + return ret; + __llist_add(&node->llist, ¤t->rethooks); + return 0; } NOKPROBE_SYMBOL(rethook_hook);