From patchwork Wed Oct 26 15:48:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 11316 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp349481wru; Wed, 26 Oct 2022 08:58:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5BX6tRR3M0A5/JQHLUo7Dt4oXzaVSBdgr+e/iKffnsIPHMuaLdRkqJCQ8s5kduuBVR2tzW X-Received: by 2002:a05:6a00:c89:b0:56c:3772:5f83 with SMTP id a9-20020a056a000c8900b0056c37725f83mr7156198pfv.74.1666799915885; Wed, 26 Oct 2022 08:58:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666799915; cv=none; d=google.com; s=arc-20160816; b=zsH3PJFKImeSSvInN74PXUhhSGhEhY8Wp0OXkr20A9O2MfauLYRaaEgSihbAoPWQr8 yF6p80uQoYJEdBr5YoVw6ok6EvdJ0mKR8f3C6q0OvrbHnFd3WjD+EevgMtHlR5aybJ5J SxsHwSVPSvRHsTk8rFvNnHN4PH3S2Rp5ACih7NdIPyy2WsW6udaiScCTohL5GbM6rHyn 9oFdO7MHKZApjDZydlch76H6iAXCu1tINhMhz79dJM20VEtBBx0aiPCFxPsGUExcZ4H1 NMvlQ3PWWNCbXICTrMzrM5hADXBvq61tLzwbZ8ahB1eL80JU9ZrRobbhiUFMT7vIstt3 xTLw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4p5yi8UyW9vOsOGW2+NDbbbMXd4BZ2xc9KEpNX8dmwk=; b=EngHlT3qnZkD1VI963eUsVpO0ey3cAEHQGNB1qE7FYmCj3pDfRJ42zZf61ixgMFF09 +ABh83eqt5uuYSKlBVZnhXqXLl5CQKECo2mZiat8tMCeqMxpjEmtYPbwhc9LQzM/1lzf N5rKPinr98A2A7QLbUrsb0on1wlfwyLJ7UCZTO0U6z0vSrX9sNp7XelQw6MPb3W6xeCm dgEXRGhPeuMOi4pqF2m8tHet4nltMom69X3VG92Ba20ewJnadJDaQJJm6p4ICV8vi/vb VdUpm0UxxYsHUzbD7ghubap9xMuNsakfJ7u6mEzCztN/mXmQ+cjmXmE2Zusaq9j/hcR9 04+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QbkHDSCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x14-20020a63310e000000b0045075b52a0fsi7649484pgx.534.2022.10.26.08.58.21; Wed, 26 Oct 2022 08:58:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QbkHDSCw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233318AbiJZPst (ORCPT + 99 others); Wed, 26 Oct 2022 11:48:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233663AbiJZPsn (ORCPT ); Wed, 26 Oct 2022 11:48:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40713B488F; Wed, 26 Oct 2022 08:48:42 -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 ams.source.kernel.org (Postfix) with ESMTPS id E72B9B82344; Wed, 26 Oct 2022 15:48:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F189C43470; Wed, 26 Oct 2022 15:48:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666799319; bh=sX2VmN8QIAfNUi3ehGzCa1UcNn8eoXtW++VSPOl7vVk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QbkHDSCwnYyR/TgpFq+jGc2Y6i3FFrkj4mMkK5eytAslQwuAXdJVjh2cAc6LaVrCs yVbewnkkEflINbWrv1btIkUqOnFJ5cUymjuOqfC7BHdhZxyvWgYI9QPv3Fgm+0ATdO 0ekjuIAe99BVxfkfziBzawSLqPeZYREaEVxFnMxbPGSrEg/8RJoOBHt77w1So14Tna 2YPNYb0l7MIReOi2oOc4u7iUtn8LFqa++0vZK3aOUfQXVl17Xh+jGkPyvmc6pyZIsl 5jucngEwhSgHOKRcMGArtObJvOT7R/qUKv7uhuTKTLe+Pigxymf3VsMf5qB8mLNmiY STZqgKjeMNHWA== From: "Masami Hiramatsu (Google)" To: LKML Cc: Steven Rostedt , Linux Trace Kernel , Masami Hiramatsu , Primiano Tucci Subject: [PATCH v2 2/2] tracing/probes: Reject symbol/symstr type for uprobe Date: Thu, 27 Oct 2022 00:48:36 +0900 Message-Id: <166679931679.1528100.15540755370726009882.stgit@devnote3> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog In-Reply-To: <166679929981.1528100.11309260111368557859.stgit@devnote3> References: <166679929981.1528100.11309260111368557859.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747766388692574982?= X-GMAIL-MSGID: =?utf-8?q?1747766388692574982?= From: Masami Hiramatsu (Google) Since uprobe's argument must contain the user-space data, that should not be converted to kernel symbols. Reject if user specifies these types on uprobe events. e.g. /sys/kernel/debug/tracing # echo 'p /bin/sh:10 %ax:symbol' >> uprobe_events sh: write error: Invalid argument /sys/kernel/debug/tracing # echo 'p /bin/sh:10 %ax:symstr' >> uprobe_events sh: write error: Invalid argument /sys/kernel/debug/tracing # cat error_log [ 1783.134883] trace_uprobe: error: Unknown type is specified Command: p /bin/sh:10 %ax:symbol ^ [ 1792.201120] trace_uprobe: error: Unknown type is specified Command: p /bin/sh:10 %ax:symstr ^ Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Fixes indentation. --- kernel/trace/trace_probe.c | 21 ++++++++++++-------- kernel/trace/trace_probe.h | 3 ++- kernel/trace/trace_uprobe.c | 3 ++- .../ftrace/test.d/kprobe/uprobe_syntax_errors.tc | 5 +++++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index dfec4af857b4..960bb7693a84 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -100,10 +100,15 @@ static const struct fetch_type probe_fetch_types[] = { ASSIGN_FETCH_TYPE_END }; -static const struct fetch_type *find_fetch_type(const char *type) +static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags) { int i; + /* Reject the symbol/symstr for uprobes */ + if (type && (flags & TPARG_FL_USER) && + (!strcmp(type, "symbol") || !strcmp(type, "symstr"))) + return NULL; + if (!type) type = DEFAULT_FETCH_TYPE_STR; @@ -121,13 +126,13 @@ static const struct fetch_type *find_fetch_type(const char *type) switch (bs) { case 8: - return find_fetch_type("u8"); + return find_fetch_type("u8", flags); case 16: - return find_fetch_type("u16"); + return find_fetch_type("u16", flags); case 32: - return find_fetch_type("u32"); + return find_fetch_type("u32", flags); case 64: - return find_fetch_type("u64"); + return find_fetch_type("u64", flags); default: goto fail; } @@ -480,7 +485,7 @@ parse_probe_arg(char *arg, const struct fetch_type *type, DEREF_OPEN_BRACE); return -EINVAL; } else { - const struct fetch_type *t2 = find_fetch_type(NULL); + const struct fetch_type *t2 = find_fetch_type(NULL, flags); *tmp = '\0'; ret = parse_probe_arg(arg, t2, &code, end, flags, offs); @@ -632,9 +637,9 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, /* The type of $comm must be "string", and not an array. */ if (parg->count || (t && strcmp(t, "string"))) goto out; - parg->type = find_fetch_type("string"); + parg->type = find_fetch_type("string", flags); } else - parg->type = find_fetch_type(t); + parg->type = find_fetch_type(t, flags); if (!parg->type) { trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); goto out; diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 0838b74f403b..23acfd1c3812 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -358,7 +358,8 @@ int trace_probe_create(const char *raw_command, int (*createfn)(int, const char #define TPARG_FL_KERNEL BIT(1) #define TPARG_FL_FENTRY BIT(2) #define TPARG_FL_TPOINT BIT(3) -#define TPARG_FL_MASK GENMASK(3, 0) +#define TPARG_FL_USER BIT(4) +#define TPARG_FL_MASK GENMASK(4, 0) extern int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *argv, unsigned int flags); diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index fb58e86dd117..8d64b6553aed 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -691,7 +691,8 @@ static int __trace_uprobe_create(int argc, const char **argv) for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { trace_probe_log_set_index(i + 2); ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], - is_return ? TPARG_FL_RETURN : 0); + (is_return ? TPARG_FL_RETURN : 0) | + TPARG_FL_USER); if (ret) goto error; } diff --git a/tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc b/tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc index f5e3f9e4a01f..c817158b99db 100644 --- a/tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc +++ b/tools/testing/selftests/ftrace/test.d/kprobe/uprobe_syntax_errors.tc @@ -23,4 +23,9 @@ check_error 'p /bin/sh:10^%hoge' # BAD_ADDR_SUFFIX check_error 'p /bin/sh:10(10)^%return' # BAD_REFCNT_SUFFIX fi +# symstr is not supported by uprobe +if grep -q ".*symstr.*" README; then +check_error 'p /bin/sh:10 $stack0:^symstr' # BAD_TYPE +fi + exit 0