From patchwork Fri Mar 31 14:00:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Cabaj X-Patchwork-Id: 1763790 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=UvYbaCFS; dkim-atps=neutral Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Pp24p0Pjhz1yZ4 for ; Sat, 1 Apr 2023 01:00:58 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1piFJE-0002Z7-R4; Fri, 31 Mar 2023 14:00:52 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1piFJ9-0002W0-B7 for kernel-team@lists.ubuntu.com; Fri, 31 Mar 2023 14:00:47 +0000 Received: from mail-yw1-f197.google.com (mail-yw1-f197.google.com [209.85.128.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 1F7333F202 for ; Fri, 31 Mar 2023 14:00:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1680271247; bh=TxRiMshToRjzz1ag/IKKSUnWlJAB1YyYGHLofNZijsE=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UvYbaCFSyG8v9nsGkvQNUIPECa2YKK9YD5hJUtPAbzwwwNxXRjLQ4IXlDBgJNN5R2 XyLbKzscTw34fiAVcDZ0JDu0vSag0w890H/6SW18wHympvOt3XUze8r8ES6UaUW4vV ACyrzOZgMsChFLe9sZTVU3Ad5xUeVyd64y9gng6XO/e0w0ccolXyrdBIHGH/JiSxWp zxVGsVnPM2s8pbd7Q19B/RCXqrkawMbdr09L7zDWD+wT6KiiI33g1sFwajttptUYBQ 0BOA8iZYJGXUEdHlRsAnvdUp0s9POI3mgL/sJB6ZuAMgCO+dmEc/8QrwFBvIe1KtM+ S2T0q1cojDyPA== Received: by mail-yw1-f197.google.com with SMTP id 00721157ae682-54574d6204aso220026957b3.15 for ; Fri, 31 Mar 2023 07:00:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680271246; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TxRiMshToRjzz1ag/IKKSUnWlJAB1YyYGHLofNZijsE=; b=z9xaefEzpf+hU+TLzvSjl7cTmUqVDA+FWetTx0O4pmrfzOPVA1ZkxrwN8TotJV2Pog X9pq9WF4SeCJCB1rxwUUF/d58uEd20PDbwJNDsQe2mzkJuZcpu+UqdOVH6tWneDW1XtF FP3VyNa9c4eUXpKgYQ+nTTlzwe+IrdKOSlQd8TT3Lado9w4t/A8Z52oPM3i0LYe+4753 IgqeId32onrsKtGP8chm1VvnotNMhArsjZrzMS9M6Ivjq43jEcDZ/erizeQF1/VaQJtS liH50h8+O0Sszon9InYIWsc+42BgnXoVZEWH/iHAmQ/N5VVPrmh5jeqbvvGZkPJQMbw9 dcVQ== X-Gm-Message-State: AAQBX9fBUuU2RP3fwavXy4utyoWiVwZ32Yu8/X3UuMrtMeK53GFAcmNr frdgEOYSScyHE+cU+YEgcbPw/zMda0TG6MJjwjPFf5zmHP2ZQJ3OYB2/uNeExPQFROy7keBJqUN fU5XKxMiUIIk61n+WQBRJYxhhTwrnR6oqQoJxGtRbw79Ekffjmw== X-Received: by 2002:a25:340d:0:b0:a5a:5809:827e with SMTP id b13-20020a25340d000000b00a5a5809827emr22564671yba.4.1680271245680; Fri, 31 Mar 2023 07:00:45 -0700 (PDT) X-Google-Smtp-Source: AKy350ZRKh41n24rgAGS3rngM9OAk99h2xfu3StnHbDIgx8nmY2MvBlfRBebSi3X1/gd3fqV3Pll8A== X-Received: by 2002:a25:340d:0:b0:a5a:5809:827e with SMTP id b13-20020a25340d000000b00a5a5809827emr22564639yba.4.1680271245259; Fri, 31 Mar 2023 07:00:45 -0700 (PDT) Received: from smtp.gmail.com (h69-130-246-116.mdtnwi.broadband.dynamic.tds.net. [69.130.246.116]) by smtp.gmail.com with ESMTPSA id a5-20020a256605000000b00b7e0d092f91sm584336ybc.18.2023.03.31.07.00.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 07:00:44 -0700 (PDT) From: John Cabaj To: kernel-team@lists.ubuntu.com Subject: [SRU][jammy][PATCH v2 3/5] kprobes: Add kretprobe_find_ret_addr() for searching return address Date: Fri, 31 Mar 2023 09:00:39 -0500 Message-Id: <20230331140041.2112510-4-john.cabaj@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230331140041.2112510-1-john.cabaj@canonical.com> References: <20230331140041.2112510-1-john.cabaj@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Masami Hiramatsu BugLink: https://bugs.launchpad.net/bugs/2013603 (Kernel livepatch ftrace graph fix) Introduce kretprobe_find_ret_addr() and is_kretprobe_trampoline(). These APIs will be used by the ORC stack unwinder and ftrace, so that they can check whether the given address points kretprobe trampoline code and query the correct return address in that case. Link: https://lkml.kernel.org/r/163163046461.489837.1044778356430293962.stgit@devnote2 Signed-off-by: Masami Hiramatsu Tested-by: Andrii Nakryiko Signed-off-by: Steven Rostedt (VMware) (backported from commit 03bac0df2886882c43e6d0bfff9dee84a184fc7e) [John Cabaj: cleaning-up kernel panic that was relocated to conditional] Signed-off-by: John Cabaj --- include/linux/kprobes.h | 22 ++++++++ kernel/kprobes.c | 109 ++++++++++++++++++++++++++++++---------- 2 files changed, 105 insertions(+), 26 deletions(-) diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index f6b0aef35f66..9e24d8b79100 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -511,6 +511,28 @@ static inline bool is_kprobe_optinsn_slot(unsigned long addr) } #endif +#ifdef CONFIG_KRETPROBES +static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr) +{ + return (void *)addr == kretprobe_trampoline_addr(); +} + +unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp, + struct llist_node **cur); +#else +static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr) +{ + return false; +} + +static nokprobe_inline +unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp, + struct llist_node **cur) +{ + return 0; +} +#endif + /* Returns true if kprobes handled the fault */ static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs, unsigned int trap) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 7da4a5048eb7..8e06a8d6516c 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1866,45 +1866,87 @@ unsigned long __weak arch_deref_entry_point(void *entry) #ifdef CONFIG_KRETPROBES -unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, - void *frame_pointer) +/* This assumes the 'tsk' is the current task or the is not running. */ +static kprobe_opcode_t *__kretprobe_find_ret_addr(struct task_struct *tsk, + struct llist_node **cur) { - kprobe_opcode_t *correct_ret_addr = NULL; struct kretprobe_instance *ri = NULL; - struct llist_node *first, *node; - struct kretprobe *rp; + struct llist_node *node = *cur; + + if (!node) + node = tsk->kretprobe_instances.first; + else + node = node->next; - /* Find all nodes for this frame. */ - first = node = current->kretprobe_instances.first; while (node) { ri = container_of(node, struct kretprobe_instance, llist); - - BUG_ON(ri->fp != frame_pointer); - if (ri->ret_addr != kretprobe_trampoline_addr()) { - correct_ret_addr = ri->ret_addr; - /* - * This is the real return address. Any other - * instances associated with this task are for - * other calls deeper on the call stack - */ - goto found; + *cur = node; + return ri->ret_addr; } - node = node->next; } - pr_err("Oops! Kretprobe fails to find correct return address.\n"); - BUG_ON(1); + return NULL; +} +NOKPROBE_SYMBOL(__kretprobe_find_ret_addr); -found: - /* Unlink all nodes for this frame. */ - current->kretprobe_instances.first = node->next; - node->next = NULL; +/** + * kretprobe_find_ret_addr -- Find correct return address modified by kretprobe + * @tsk: Target task + * @fp: A frame pointer + * @cur: a storage of the loop cursor llist_node pointer for next call + * + * Find the correct return address modified by a kretprobe on @tsk in unsigned + * long type. If it finds the return address, this returns that address value, + * or this returns 0. + * The @tsk must be 'current' or a task which is not running. @fp is a hint + * to get the currect return address - which is compared with the + * kretprobe_instance::fp field. The @cur is a loop cursor for searching the + * kretprobe return addresses on the @tsk. The '*@cur' should be NULL at the + * first call, but '@cur' itself must NOT NULL. + */ +unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp, + struct llist_node **cur) +{ + struct kretprobe_instance *ri = NULL; + kprobe_opcode_t *ret; + + if (WARN_ON_ONCE(!cur)) + return 0; + + do { + ret = __kretprobe_find_ret_addr(tsk, cur); + if (!ret) + break; + ri = container_of(*cur, struct kretprobe_instance, llist); + } while (ri->fp != fp); - /* Run them.. */ + return (unsigned long)ret; +} +NOKPROBE_SYMBOL(kretprobe_find_ret_addr); + +unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, + void *frame_pointer) +{ + kprobe_opcode_t *correct_ret_addr = NULL; + struct kretprobe_instance *ri = NULL; + struct llist_node *first, *node = NULL; + struct kretprobe *rp; + + /* Find correct address and all nodes for this frame. */ + correct_ret_addr = __kretprobe_find_ret_addr(current, &node); + if (!correct_ret_addr) { + pr_err("kretprobe: Return address not found, not execute handler. Maybe there is a bug in the kernel.\n"); + BUG_ON(1); + } + + /* Run the user handler of the nodes. */ + first = current->kretprobe_instances.first; while (first) { ri = container_of(first, struct kretprobe_instance, llist); - first = first->next; + + if (WARN_ON_ONCE(ri->fp != frame_pointer)) + break; rp = get_kretprobe(ri); if (rp && rp->handler) { @@ -1915,6 +1957,21 @@ unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs, rp->handler(ri, regs); __this_cpu_write(current_kprobe, prev); } + if (first == node) + break; + + first = first->next; + } + + /* Unlink all nodes for this frame. */ + first = current->kretprobe_instances.first; + current->kretprobe_instances.first = node->next; + node->next = NULL; + + /* Recycle free instances. */ + while (first) { + ri = container_of(first, struct kretprobe_instance, llist); + first = first->next; recycle_rp_inst(ri); }