From nobody Thu Nov 28 19:44:57 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A9F627733; Fri, 27 Sep 2024 21:44:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727473476; cv=none; b=sUIIZhFM/0eH4zL4ItWCESw+Jw8vvouFHeRbEdedQC86wXWPVPQupO1Mh/Wik6IQFKX8PCIzdpyLyLmWeIaL1Y3TJONpGh2eHlOkG94FnT+FQYGkUl6/S0rpbuCsyDlhF49sTkMiD7kC/Z9bLjKwm9AsQwHqcWWIYJlTgYjoja4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727473476; c=relaxed/simple; bh=K0gPUWLsnRD6/VmuB9GhQRsOndEtObPCk8eRv4AIWpo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H/sAqkfNAyUVBoVYxA6wXDG8EDcilcdbK+brG1fhYL9AP2+OHUbj4Mg7RK+aaRelwAwJxL4LenEsYoRmfSnSwtuI+Hpt9gPq4SS7kB74i4d3imF2AbSBEOR1mfHsk/tQXhPjCvRSPpmEagaBoEtWWCnpefPCK7tjRpCB5vqxwmk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=Wrr8W20w; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="Wrr8W20w" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48RKVneE031251; Fri, 27 Sep 2024 21:44:04 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=f 8WILyoQJdvgGSpm/Z9SmTvVQUBob6QojAK17v9wTMI=; b=Wrr8W20wrns8lgh5g Zdlq1rjJeVz/o8cTt+T5cYjh5MG7BQw/Mmdkd4V8k5zqbrfzB8VpGSUONpmyJ4Vv m9G5jR3xeerOmnddwbYTIlE/Jp52/6CSUkDimOy7CX/w4kbDUvWogdvk9GNEnEoY RLlFLV/0UHz3zm0l9mLOdUoPIpXmF2prsRJQSVBLOmh0rmVqjeC0oZeLLsXp09gl 6EcEeOtcpZRA4xV3woy3ChSEheI/nKwFz6uNvqflGbLMpKyf9R9GViSqAlRSL6X6 Chh5PFs8NpIgJz6N1/3+lo7qVN+wSpaPw8UHNDLMbCiyZqbJN1rbF7XU7le6YTws OY+2A== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41sn2d0wbs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 Sep 2024 21:44:04 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48RJhbnK025172; Fri, 27 Sep 2024 21:44:02 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41smke5afg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 Sep 2024 21:44:02 +0000 Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 48RLaw34030299; Fri, 27 Sep 2024 21:44:02 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 41smke5ae0-2; Fri, 27 Sep 2024 21:44:02 +0000 From: Sherry Yang To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: rostedt@goodmis.org, mingo@redhat.com, gregkh@linuxfoundation.org, sherry.yang@oracle.com Subject: [PATCH 5.4.y 1/2] tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols Date: Fri, 27 Sep 2024 14:43:58 -0700 Message-ID: <20240927214359.7611-2-sherry.yang@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240927214359.7611-1-sherry.yang@oracle.com> References: <20240927214359.7611-1-sherry.yang@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-27_06,2024-09-27_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 mlxlogscore=999 adultscore=0 mlxscore=0 bulkscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409270159 X-Proofpoint-GUID: lpnpTF7IrXSOpeGcnE5LPvTEboRIoo0w X-Proofpoint-ORIG-GUID: lpnpTF7IrXSOpeGcnE5LPvTEboRIoo0w Content-Type: text/plain; charset="utf-8" From: Francis Laniel commit b022f0c7e404887a7c5229788fc99eff9f9a80d5 upstream. 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. Link: https://lore.kernel.org/all/20231020104250.9537-2-flaniel@linux.micro= soft.com/ 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/ Acked-by: Masami Hiramatsu (Google) Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Greg Kroah-Hartman [Sherry: kselftest kprobe_non_uniq_symbol.tc failed on 5.4.y, because of mi= ssing this commit, backport it to 5.4.y. Minor conflicts due to context change, i= gnore context change] Signed-off-by: Sherry Yang --- kernel/trace/trace_kprobe.c | 74 +++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 1 + 2 files changed, 75 insertions(+) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 0b95277396fc..80a59dbdd631 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -714,6 +714,36 @@ static inline void sanitize_event_name(char *name) *name =3D '_'; } =20 +struct count_symbols_struct { + const char *func_name; + unsigned int count; +}; + +static int count_symbols(void *data, const char *name, struct module *unus= ed0, + unsigned long unused1) +{ + struct count_symbols_struct *args =3D data; + + if (strcmp(args->func_name, name)) + return 0; + + args->count++; + + return 0; +} + +static unsigned int number_of_same_symbols(char *func_name) +{ + struct count_symbols_struct args =3D { + .func_name =3D func_name, + .count =3D 0, + }; + + kallsyms_on_each_symbol(count_symbols, &args); + + return args.count; +} + static int trace_kprobe_create(int argc, const char *argv[]) { /* @@ -825,6 +855,31 @@ static int trace_kprobe_create(int argc, const char *a= rgv[]) } } =20 + if (symbol && !strchr(symbol, ':')) { + unsigned int count; + + count =3D 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 =3D -EADDRNOTAVAIL; + + goto error; + } else if (count =3D=3D 0) { + /* + * We can return ENOENT earlier than when register the + * kprobe. + */ + trace_probe_log_err(0, BAD_PROBE_ADDR); + ret =3D -ENOENT; + + goto error; + } + } + trace_probe_log_set_index(0); if (event) { ret =3D traceprobe_parse_event_name(&event, &group, buf, @@ -1596,6 +1651,7 @@ static int unregister_kprobe_event(struct trace_kprob= e *tk) } =20 #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, @@ -1605,6 +1661,24 @@ create_local_trace_kprobe(char *func, void *addr, un= signed long offs, int ret; char *event; =20 + if (func) { + unsigned int count; + + count =3D 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 =3D=3D 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 dc19d5d185d4..edbb1624061e 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -403,6 +403,7 @@ extern int traceprobe_define_arg_fields(struct trace_ev= ent_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_GROUP_NAME, "Group name is not specified"), \ C(GROUP_TOO_LONG, "Group name is too long"), \ --=20 2.46.0 From nobody Thu Nov 28 19:44:57 2024 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42DB716F909; Fri, 27 Sep 2024 21:44:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727473474; cv=none; b=pJdT41yC0wUyBPe+p0q3jnMsbZf8gfC48BwY7C6F+48XEiBvwgx74e8NjlRMOyDYiVJ1kUWhWZy8BWRp3+0nwzc4iHrJAWS8t+Q9jyo2lagAb6qIq8i++wkYKAp+kT2kXAgcx+2DOQlKR1c2U3iJiYiFKR9P3+X5Aj7jpHyQu4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727473474; c=relaxed/simple; bh=D3oLuwP6Dv6obMJ5odxVQzTAjbaZ9Z+G5xZvfXXWUcY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nPVpqXZu9YgAl6ui/68eeGDpcUwCxlAdGCf3njsPnqRvne8de/Vwo5f9gHfeqnjLpenIYZMy045LQ87cqLW7P6kXnNXoeNJEvIqkJfSDN54bUTEo50AiguAoP9nrWDA3yE6bGhUE7m5mHGy8PF9m4S/5ccwxV367ZqnD8uhBcoM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=ZgkXyV9a; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="ZgkXyV9a" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48RKVlH9025180; Fri, 27 Sep 2024 21:44:04 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=n 5Wgk42lYzokNmEIlnNyGYK3U2saSL2y5ta9ak5AIJ4=; b=ZgkXyV9absOSVzl0z Pm5ApyoOi9ytxZVv/P8RyrljssnPwfzxhxr+cDUTVIWJg/TYbjU6Ht8dgGsX5BeJ qxVNH0LMySkF91zlIMvWSyGsfP0fpCNBhdF44DTEVNdfdHHZs7SUhRnG4iG1OCpv y7xLqPzRrBtVqc7vKX/E9hyhnXdOtr6czHP/R8NLxq+o89jumkyWPlXhexRFIse9 wUQ4tc3PNDXtin3RtYEuZ84SO2JWMH91PtMHTwY5g6OkpG0O10bqazHYDPUL9vn8 p2zlSrUNAGyDCv/CoZptGIwrwbanc5bA9j6mtEnpdr3b5VOIO94JX3QWGcXg74ft XR/Jw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41snrte5kk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 Sep 2024 21:44:04 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48RK6MMc025352; Fri, 27 Sep 2024 21:44:03 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41smke5afy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 Sep 2024 21:44:03 +0000 Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 48RLaw36030299; Fri, 27 Sep 2024 21:44:03 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 41smke5ae0-3; Fri, 27 Sep 2024 21:44:03 +0000 From: Sherry Yang To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: rostedt@goodmis.org, mingo@redhat.com, gregkh@linuxfoundation.org, sherry.yang@oracle.com Subject: [PATCH 5.4.y 2/2] tracing/kprobes: Fix symbol counting logic by looking at modules as well Date: Fri, 27 Sep 2024 14:43:59 -0700 Message-ID: <20240927214359.7611-3-sherry.yang@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240927214359.7611-1-sherry.yang@oracle.com> References: <20240927214359.7611-1-sherry.yang@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-27_06,2024-09-27_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 mlxlogscore=999 adultscore=0 mlxscore=0 bulkscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409270159 X-Proofpoint-GUID: TCm0kOeeB55gQJa1Kw68urfico7sQatD X-Proofpoint-ORIG-GUID: TCm0kOeeB55gQJa1Kw68urfico7sQatD Content-Type: text/plain; charset="utf-8" From: Andrii Nakryiko commit 926fe783c8a64b33997fec405cf1af3e61aed441 upstream. Recent changes to count number of matching symbols when creating a kprobe event failed to take into account kernel modules. As such, it breaks kprobes on kernel module symbols, by assuming there is no match. Fix this my calling module_kallsyms_on_each_symbol() in addition to kallsyms_on_each_match_symbol() to perform a proper counting. Link: https://lore.kernel.org/all/20231027233126.2073148-1-andrii@kernel.or= g/ Cc: Francis Laniel Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Cc: Steven Rostedt Fixes: b022f0c7e404 ("tracing/kprobes: Return EADDRNOTAVAIL when func match= es several symbols") Signed-off-by: Andrii Nakryiko Acked-by: Song Liu Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Markus Boehme Signed-off-by: Greg Kroah-Hartman [Sherry: It's a fix for previous backport, thus backport together to 5.4.y] Signed-off-by: Sherry Yang --- kernel/trace/trace_kprobe.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 80a59dbdd631..2164abe06d84 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -741,6 +741,8 @@ static unsigned int number_of_same_symbols(char *func_n= ame) =20 kallsyms_on_each_symbol(count_symbols, &args); =20 + module_kallsyms_on_each_symbol(count_symbols, &args); + return args.count; } =20 --=20 2.46.0