From nobody Thu Nov 28 22:25:33 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