Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp926061rdb; Fri, 20 Oct 2023 03:43:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3fQroJBpnWGd77WhaaFcYF4UVRHRtwGrPs5wNJJQhP+AGiMzjVv16eq92aDdzLLy5T6jC X-Received: by 2002:a17:90b:4012:b0:276:e14a:4991 with SMTP id ie18-20020a17090b401200b00276e14a4991mr1414332pjb.2.1697798613556; Fri, 20 Oct 2023 03:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697798613; cv=none; d=google.com; s=arc-20160816; b=yvsY+PHVvf8u658O946fIgOUkaj1HcpmPYNR79Ni/NyCCkzkCittfKzeIQYKPFc25v /Xf5U357OlUezJWnw99xU388+FovOTvXY0TlCJ5UDXAOO5hUXC8RvOmOkpVTMkuTBfdZ aaZVXPmkXC0/B9wQJF+RZ9aW9nn30KtYp3GdSjB7zj/N0RiacVIPCq4CfZFbduAgkuxk 7oFmL81DXntYAqifpbR3UV57i0MdWDDUZYhABL9A6rzps3VaONRoxHzICXe5i1PV6zT+ uxFtgwhYt+La23LrnxyoaViBSlQy9Ck0TFJF0bRlngFvyjPNRJIzEdZ6g25+UHzjuxu9 OlfA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=9VCxaeebanUQ/Qf2MXAfmGic0SqHTRKJ/14/HCr2fXM=; fh=eKzZdbva5vlPxC/2sk7ok5HTMakDSIbH6IugOtffm4M=; b=Wn0yLXqAQWY6Y4nrLXns3mwufJyfcYqLK4A7O/7MP4V7QWdw4wuFcKVV/U/FvumHfX GhoZVAm7zUeiChGUG0/Fw5+zMwzZCMrKc3dZhNp4tjES4rKfQRLoWSXLyifKvQZmEbQs CHhD3gK7zmeHWYHYyXHJd3PX1HkF72GNfUhvDlWpLpZdfFCP5l2E8xS9Lw0Sai0uCajl i47vAaXjD7Hq4/IkULJE+zrLyG8Im21t94VOUSl5myD1fT07o9fDQlVKK7vkXjepjnt1 aWu/8rfVj+qyl5pf2LAGrgeJbTKOiwziOVkROedbp/8sLPJM7An65KblrkoVkfIIWr7q Gnzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=dqkKKU5M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id kk2-20020a17090b4a0200b0026b51ae4574si4687609pjb.36.2023.10.20.03.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 03:43:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=dqkKKU5M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6E9088348D8D; Fri, 20 Oct 2023 03:43:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376959AbjJTKnT (ORCPT + 99 others); Fri, 20 Oct 2023 06:43:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376699AbjJTKnS (ORCPT ); Fri, 20 Oct 2023 06:43:18 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5CD3C114; Fri, 20 Oct 2023 03:43:16 -0700 (PDT) Received: from pwmachine.numericable.fr (unknown [188.24.154.80]) by linux.microsoft.com (Postfix) with ESMTPSA id A88F020B74C1; Fri, 20 Oct 2023 03:43:13 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A88F020B74C1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1697798595; bh=9VCxaeebanUQ/Qf2MXAfmGic0SqHTRKJ/14/HCr2fXM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dqkKKU5MpbDhpDEo8R7b8r+QLcxwpUm9Sne3TIlfIMKTHnvCpTFFbRWvp0iqUik4d 74Rs/+3i7npIqE7Jz7mSvHSCfD8UV0FtB/pUE+nJsWHmvX0RhA2Hy0Wfps+ukYsnSU Xs6FJuuAPh/empUA36bf1/L7t96s5VhlQMtAtFMw= From: Francis Laniel To: linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Francis Laniel , stable@vger.kernel.org, Steven Rostedt , Ananth N Mavinakayanahalli , Frederic Weisbecker , linux-kernel@vger.kernel.org Subject: [PATCH v6 1/2] tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols Date: Fri, 20 Oct 2023 13:42:49 +0300 Message-Id: <20231020104250.9537-2-flaniel@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020104250.9537-1-flaniel@linux.microsoft.com> References: <20231020104250.9537-1-flaniel@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Fri, 20 Oct 2023 03:43:28 -0700 (PDT) When a kprobe is attached to a function that's name is not unique (is static and shares the name with other functions in the kernel), the kprobe is attached to the first function it finds. This is a bug as the function that it is attaching to is not necessarily the one that the user wants to attach to. Instead of blindly picking a function to attach to what is ambiguous, error with EADDRNOTAVAIL to let the user know that this function is not unique, and that the user must use another unique function with an address offset to get to the function they want to attach to. Cc: stable@vger.kernel.org Fixes: 413d37d1eb69 ("tracing: Add kprobe-based event tracer") Suggested-by: Masami Hiramatsu Signed-off-by: Francis Laniel Link: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kernel.org/ --- kernel/trace/trace_kprobe.c | 63 +++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 1 + 2 files changed, 64 insertions(+) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 3d7a180a8427..a8fef6ab0872 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -705,6 +705,25 @@ static struct notifier_block trace_kprobe_module_nb = { .priority = 1 /* Invoked after kprobe module callback */ }; +static int count_symbols(void *data, unsigned long unused) +{ + unsigned int *count = data; + + (*count)++; + + return 0; +} + +static unsigned int number_of_same_symbols(char *func_name) +{ + unsigned int count; + + count = 0; + kallsyms_on_each_match_symbol(count_symbols, func_name, &count); + + return count; +} + static int __trace_kprobe_create(int argc, const char *argv[]) { /* @@ -836,6 +855,31 @@ static int __trace_kprobe_create(int argc, const char *argv[]) } } + if (symbol && !strchr(symbol, ':')) { + unsigned int count; + + count = number_of_same_symbols(symbol); + if (count > 1) { + /* + * Users should use ADDR to remove the ambiguity of + * using KSYM only. + */ + trace_probe_log_err(0, NON_UNIQ_SYMBOL); + ret = -EADDRNOTAVAIL; + + goto error; + } else if (count == 0) { + /* + * We can return ENOENT earlier than when register the + * kprobe. + */ + trace_probe_log_err(0, BAD_PROBE_ADDR); + ret = -ENOENT; + + goto error; + } + } + trace_probe_log_set_index(0); if (event) { ret = traceprobe_parse_event_name(&event, &group, gbuf, @@ -1695,6 +1739,7 @@ static int unregister_kprobe_event(struct trace_kprobe *tk) } #ifdef CONFIG_PERF_EVENTS + /* create a trace_kprobe, but don't add it to global lists */ struct trace_event_call * create_local_trace_kprobe(char *func, void *addr, unsigned long offs, @@ -1705,6 +1750,24 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs, int ret; char *event; + if (func) { + unsigned int count; + + count = number_of_same_symbols(func); + if (count > 1) + /* + * Users should use addr to remove the ambiguity of + * using func only. + */ + return ERR_PTR(-EADDRNOTAVAIL); + else if (count == 0) + /* + * We can return ENOENT earlier than when register the + * kprobe. + */ + return ERR_PTR(-ENOENT); + } + /* * local trace_kprobes are not added to dyn_event, so they are never * searched in find_trace_kprobe(). Therefore, there is no concern of diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 02b432ae7513..850d9ecb6765 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -450,6 +450,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call, C(BAD_MAXACT, "Invalid maxactive number"), \ C(MAXACT_TOO_BIG, "Maxactive is too big"), \ C(BAD_PROBE_ADDR, "Invalid probed address or symbol"), \ + C(NON_UNIQ_SYMBOL, "The symbol is not unique"), \ C(BAD_RETPROBE, "Retprobe address must be an function entry"), \ C(NO_TRACEPOINT, "Tracepoint is not found"), \ C(BAD_ADDR_SUFFIX, "Invalid probed address suffix"), \ -- 2.34.1