Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1037182rwb; Wed, 26 Jul 2023 06:47:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlGXY8S9RI5CuTqfs/H943rSJkKtVAAKxN9JGyTD1SPKTwWS7pOIJA5B3xd4Jy4L9p6HIwOs X-Received: by 2002:a17:906:77db:b0:993:e9b8:90f5 with SMTP id m27-20020a17090677db00b00993e9b890f5mr1587538ejn.8.1690379255645; Wed, 26 Jul 2023 06:47:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690379255; cv=none; d=google.com; s=arc-20160816; b=gMO2bqv4zfh9d2o7clMJVJInIUVidlasfAvzyL7yQNEHeUjYdTx2aRwKaCZt74OUjm D6NCVjkT4mxKdcCldaVN4iDXVO28SEq4JMdNZVrnIaLz6TJLgizLMe2dsBLTRi58lYiL cICdh90CWtO+WUrh76MIAfjb9/CRZYCHqcoqgWSGxJWxbZzCRKeWdLBcZFr83R2Zilcj 05jarM/JEPZ0Gvy7l7Eh3xZNy4wHvS+iZ8Ra6MDpZW+O2MjIwErXi04VHTymhY6br3ix 1rPO2+uHwHgILtTwaABOHsxp5kQnW789JaH3W/dp341vY+1/GQzLSip7+epworhLmlL9 WBgw== 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=ulhp/vyPMRR1b9TnMI9V0n/t6A/gBwRTSRwq1JyEwzs=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=AvnmO5tE43jYFpZenG7J54kgfcOd95zPF9SMLkOHboW4CkFBIiRFG7/r62tQrqyEXf Hug8njkSUlqfd8Pyq3sj3pibGY98hLVQYRZit4HvDNsoXiJ980ttDK96OPNlRCMGA/Ya nW8Y8f6DTof8rYrHscTqQc8xtnlsIIfO0v7JnxjUAmVYIvOx4m45YaVef0p12/9q7yZs 0oZDbx4qFhJ3hc4Q4V6EjxZm3AqlZOerK9jOxUQjMEjisyLf9tEicIDI7vyZSW28P7Rg njYE6InQnMT+LXyfR21lU5XE86wL2wAyB2+wQCk1RLvwhEojLMXLdkN+GhbNqpWTUoTC T3kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=loCUU+aa; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n24-20020a170906841800b0099327749bddsi8974283ejx.125.2023.07.26.06.47.10; Wed, 26 Jul 2023 06:47: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=loCUU+aa; 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 S233017AbjGZNBT (ORCPT + 99 others); Wed, 26 Jul 2023 09:01:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232173AbjGZNBO (ORCPT ); Wed, 26 Jul 2023 09:01:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C8BF2681; Wed, 26 Jul 2023 06:00:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EFCC56068E; Wed, 26 Jul 2023 13:00:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78DB6C433C7; Wed, 26 Jul 2023 13:00:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690376456; bh=5xn7r6yeELI05L0FVp5FQnwWOM0qeKUD3zRDu7nxK2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=loCUU+aaXcrjN5tanPL1crn8cpmz7bluewnTQG5HbpJpzHFzbw80RmJWUhgghiQC5 zFbTYH2xC5OtdWsE0uBQaxVK921ArsPxN9bn2Mdyg5+i0yK83HGz8WTBgByUqHHmG9 MXlkhY7vtYaXw0H9PxXpzgILa1if/zxbXj63fzhoQgR/sERGRehnvCZq390ffahS0g b1DsjLeaH8RCcT0NunNIXbLUuF3GbK4DOSNeMkNYNNzyeaIz0R2xvJOn/lOMVNLNfq MC8Pc0gkzSTyzOSklZazB4WHsa1PMagbxRXTPOaDGEKA4TEekMKurti0eGTqiuBVlZ U48qKUWQXUEbA== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Steven Rostedt , mhiramat@kernel.org, Martin KaFai Lau , bpf@vger.kernel.org, Sven Schnelle , Alexei Starovoitov Subject: [PATCH v3 6/9] tracing/probes: Add string type check with BTF Date: Wed, 26 Jul 2023 22:00:52 +0900 Message-Id: <169037645210.607919.8694905190029680811.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169037639315.607919.2613476171148037242.stgit@devnote2> References: <169037639315.607919.2613476171148037242.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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,T_SCC_BODY_TEXT_LINE 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 From: Masami Hiramatsu (Google) Add a string type checking with BTF information if possible. This will check whether the given BTF argument (and field) is signed char array or pointer to signed char. If not, it reject the 'string' type. If it is pointer to signed char, it adds a dereference opration so that it can correctly fetch the string data from memory. # echo 'f getname_flags%return retval->name:string' >> dynamic_events # echo 't sched_switch next->comm:string' >> dynamic_events The above cases, 'struct filename::name' is 'char *' and 'struct task_struct::comm' is 'char []'. But in both case, user can specify ':string' to fetch the string data. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v3: - Use ctx->btf instead of traceprobe_get_btf(). --- kernel/trace/trace_probe.c | 89 +++++++++++++++++++++++++++++++++++++++++++- kernel/trace/trace_probe.h | 3 + 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index 65273fe76a9e..edd234e5b929 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -309,6 +309,77 @@ static u32 btf_type_int(const struct btf_type *t) return *(u32 *)(t + 1); } +static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type) +{ + const struct btf_type *real_type; + u32 intdata; + s32 tid; + + real_type = btf_type_skip_modifiers(btf, type->type, &tid); + if (!real_type) + return false; + + if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT) + return false; + + intdata = btf_type_int(real_type); + return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) + && BTF_INT_BITS(intdata) == 8; +} + +static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type) +{ + const struct btf_type *real_type; + const struct btf_array *array; + u32 intdata; + s32 tid; + + if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY) + return false; + + array = (const struct btf_array *)(type + 1); + + real_type = btf_type_skip_modifiers(btf, array->type, &tid); + + intdata = btf_type_int(real_type); + return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) + && BTF_INT_BITS(intdata) == 8; +} + +static int check_prepare_btf_string_fetch(char *typename, + struct fetch_insn **pcode, + struct traceprobe_parse_context *ctx) +{ + struct btf *btf = ctx->btf; + + if (!btf || !ctx->last_type) + return 0; + + /* char [] does not need any change. */ + if (btf_type_is_char_array(btf, ctx->last_type)) + return 0; + + /* char * requires dereference the pointer. */ + if (btf_type_is_char_ptr(btf, ctx->last_type)) { + struct fetch_insn *code = *pcode + 1; + + if (code->op == FETCH_OP_END) { + trace_probe_log_err(ctx->offset, TOO_MANY_OPS); + return -E2BIG; + } + if (typename[0] == 'u') + code->op = FETCH_OP_UDEREF; + else + code->op = FETCH_OP_DEREF; + code->offset = 0; + *pcode = code; + return 0; + } + /* Other types are not available for string */ + trace_probe_log_err(ctx->offset, BAD_TYPE4STR); + return -EINVAL; +} + static const char *fetch_type_from_btf_type(struct btf *btf, const struct btf_type *type, struct traceprobe_parse_context *ctx) @@ -665,6 +736,13 @@ static int parse_btf_bitfield(struct fetch_insn **pcode, #define find_fetch_type_from_btf_type(ctx) \ find_fetch_type(NULL, ctx->flags) +static int check_prepare_btf_string_fetch(char *typename, + struct fetch_insn **pcode, + struct traceprobe_parse_context *ctx) +{ + return 0; +} + #endif #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) @@ -1107,8 +1185,15 @@ static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, /* Update storing type if BTF is available */ if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && - !t && ctx->last_type) - parg->type = find_fetch_type_from_btf_type(ctx); + ctx->last_type) { + if (!t) { + parg->type = find_fetch_type_from_btf_type(ctx); + } else if (strstr(t, "string")) { + ret = check_prepare_btf_string_fetch(t, &code, ctx); + if (ret) + goto fail; + } + } ret = -EINVAL; /* Store operation */ diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 9184c84833f8..7f929482e8d4 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -513,7 +513,8 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call, C(NOSUP_DAT_ARG, "Non pointer structure/union argument is not supported."),\ C(BAD_HYPHEN, "Failed to parse single hyphen. Forgot '>'?"), \ C(NO_BTF_FIELD, "This field is not found."), \ - C(BAD_BTF_TID, "Failed to get BTF type info."), + C(BAD_BTF_TID, "Failed to get BTF type info."),\ + C(BAD_TYPE4STR, "This type does not fit for string."), #undef C #define C(a, b) TP_ERR_##a