From patchwork Tue Aug 22 16:26:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 136583 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3798475vqi; Tue, 22 Aug 2023 11:04:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFeSChM/kfNl4O73NeY4Cf9hkUYeuKwsCBW48egphLczPytc7uG9Oqmyg7I2RkYLOntPXm0 X-Received: by 2002:a17:90a:d258:b0:268:5c3b:6f28 with SMTP id o24-20020a17090ad25800b002685c3b6f28mr7373296pjw.19.1692727455860; Tue, 22 Aug 2023 11:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692727455; cv=none; d=google.com; s=arc-20160816; b=y9nC+EOqicPm0YBxpC90opaN4oc8k+vhpwsBgt8d2j6vDJreKVQo4RXq0XOfFKXx7/ r7E7HxXhQu2mwLgU38Iw9K0AY6w9p26Jin314RvCaEoOHwObqFtSpQQ1elCoe87DFbXA ESnMVTcRoym0zl6t7VFVkLI2WaF7wn21dLGo2QuZUDIdxgKmS3o93cLNMP6NEeuJdcMD UKf20KxzyfcYdwstcS6RHw0fA0k41JaMoS1g8pcCkPjLgr7qCDwPJ3vsGTBHH1D6Czg3 8Zm/6bgqmu9owmDxkx1O6kSLeHy5aeYyF5g0buliGcwJXmIWPJvhpOOQXd1unBXOJ7H3 060g== 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=6rgdKHbL/YHvd1W3hP+boI40Pjj2gXhMwxpovaLKdfo=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=BPzqRQDsWoG0nQLYbAxj25Y1VHWDiuuv6iDdoYvmTd7A6AJ9Ng0KNEmURzqBKRDDOX 4FRbfLM8Yqo72aV/zoYgeG7h+3EaZv9nmy7283gDrKgkfE8qkoJDJkKKDls4GIMBU8w+ np12jUtb9lHU/hmGK19w5S2nAsSay4ElMjzDw8/m8lbrNc85G5b4AB5eLb231yGKTpRj z2P0B/d465XdAZXUgpWFCX5aM0TBhBONffD1OrJTNH+ss8xR1DmKU8DpWRHI3KRnX41U E4FwTok9mS+MZv1sFRwrPjCkt+6a9EEybqP2Wv0g7KtW2Yau5Rpv/Y5X9Btr20mFgjsg t8Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="oq/3X6LY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d20-20020a17090ad99400b0026b38330c7asi11187906pjv.179.2023.08.22.11.04.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 11:04:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="oq/3X6LY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3D1DB9ED3; Tue, 22 Aug 2023 10:19:58 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237765AbjHVQ0z (ORCPT + 99 others); Tue, 22 Aug 2023 12:26:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237735AbjHVQ0y (ORCPT ); Tue, 22 Aug 2023 12:26:54 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A502CDD; Tue, 22 Aug 2023 09:26:38 -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 C814A65A5B; Tue, 22 Aug 2023 16:26:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 39391C433C8; Tue, 22 Aug 2023 16:26:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692721597; bh=z5HZQKxxeBH9fJjmD9jGaQHV/G79OpU0dTqpNG/QDZA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oq/3X6LYWFJThLSdPdEL1gHoqZCZwmHEWwbLIlgsufVtvCw4fobzSpk+DfbaGjC05 71AMN2qkeTuShdudBf9UDlcVpvvitoN19mxRYt7RfhZRIyx6RIitE+Fp6OUesb9SaX prtIXhVk6eJvvG5BHegvUf4BtVnTBa06ub+y2d0cThDPxdpkAorpTc2ZwWyCjarlHK mJcbCMq32ihwMpltloyVUHeUociaZJUmukcUxIHAvRKmTxd+CHWIlXjpNIH3pEtywS wJovvqbYgrdIjsZQhIq+JMbL9JlIPCwZ3vSqSIBquCPxADiHZL3FXF0X0T3tsBOeOc IN5BLn02Q8alg== 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 v6 6/9] tracing/probes: Add string type check with BTF Date: Wed, 23 Aug 2023 01:26:32 +0900 Message-Id: <169272159250.160970.1881112937198526188.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169272153143.160970.15584603734373446082.stgit@devnote2> References: <169272153143.160970.15584603734373446082.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 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 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: INBOX X-GMAIL-THRID: 1774953384776910503 X-GMAIL-MSGID: 1774953384776910503 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) Acked-by: Steven Rostedt (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 7345e1af4db2..4dc74d73fc1d 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -310,6 +310,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) @@ -675,6 +746,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)) @@ -1117,8 +1195,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