From patchwork Mon Jul 17 15:24:17 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: 121432 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1209080vqt; Mon, 17 Jul 2023 09:16:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlELPZQXQG+sKCSOaHOKRb8dccmEBN0VGttk6y8AkNIMDe5WIdLrOkb+57RNII4K+7daqxDS X-Received: by 2002:a17:906:151:b0:993:ea6b:edf6 with SMTP id 17-20020a170906015100b00993ea6bedf6mr11306006ejh.0.1689610559790; Mon, 17 Jul 2023 09:15:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689610559; cv=none; d=google.com; s=arc-20160816; b=peOcNj77NWdRJ5u2pQ1H3WBu3QsAuuR1sW1jc7E+E4EofVVxxiaQCmULmU0z/GGkSz fnSnbatC1fmJjH2DDklF7X+31OyS1u57Et/BBsKpJP1H7bWeAI89uh6WhCiGQZc9qxbO HjFO/POMSmIFECYF+8xK1qJ0uPC0pBIvjIt3+JRuk0vznwRcD00pvnpaaKhR0LLr6ojD NrK3SDmjyJG9iha0Xpe0x1dy03A/8S95sLL40IbA4nVRvHvY5QI84mD40xqJXA2Vj5Jl 5s5+8d86chE/UACsYhfahOIJWFQtacIz71pHnW2jkl44uM7ld7LAqAQeYWzdUJrAVvG4 Yaxg== 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=2Pvng8lJEdc6nKeeZWasXIGFxUdo+SBby1IstXLcNmU=; fh=raxTTJvC+dSJYbgfX5JG5gvrVjxTZXczN0HF8iDayLY=; b=sYbWhu9GCT9Lblb0jn4XYlhQR41HZMZIY3S9bhypOZ/fTwRWXoOMAwRHNaZRvbT8ji /o/180A06lTYHC6W7dALsnG0uNc1xJ+G7e1badDr+FNTKlX4rOEUkCTVSaac8gnGaopc UlqVO8/N3US2dT9gRuAfwLnFDEc83+PMU/9AzW+mOkdit3YvkseA9WfqONv4Eh5qxkFJ b6A+oWnA8Mre18td8brE78vuHBz3LuFsl0hcqWu5t3FsdT5TZAc8Rqr/GzWCiTpTe6CS /hrdLUX8Wq/HhzlTjl9lO4/nXTXXB+oqsOwmQ8kK5QEmOzl6qRdboPOVDIV5NwwS/gxn 4hOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kKx2dT1S; 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 c8-20020a170906694800b0099279b2cdddsi13585539ejs.833.2023.07.17.09.15.35; Mon, 17 Jul 2023 09:15:59 -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=kKx2dT1S; 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 S230029AbjGQPZG (ORCPT + 99 others); Mon, 17 Jul 2023 11:25:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232033AbjGQPYr (ORCPT ); Mon, 17 Jul 2023 11:24:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E07FE2127; Mon, 17 Jul 2023 08:24:22 -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 8DD346111D; Mon, 17 Jul 2023 15:24:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01D69C433C7; Mon, 17 Jul 2023 15:24:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689607462; bh=jRgqNwwWlmesVuWztk4h6xTjuyWOGZBfgSaCsZjlSG8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kKx2dT1SQ0ADmcZHSuZ5bK1IjU9ciiukuO4i4kYLOl/hFpoeEY5llLJ8XCayyHPv8 3EBEGFIm+0oqNDfyAgv8dljtUNsuEMQVjTcV1z4s7vaNhj2EqLo4tZAeijztI8+MYW WMVRSK/ue0sFGF9kIEIiyrwGgQpc9H/n1lx3xyM0fF3slIy9kkYnouiWSNPWe4uoLw liP6O8+Vo1Xc0EX9U5Y2CKdeuw4lKtzeydwNiF9wrrUIopdYDU11dTwH+/HWa76h+7 uW2hqA5z6YzKAbgjFAPkU6emEtfS87zB7DfpcgbiarGrIId05aldwSWgxjS+9ABKPz 1CdX90mJ66lug== 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 v2 6/9] tracing/probes: Add string type check with BTF Date: Tue, 18 Jul 2023 00:24:17 +0900 Message-Id: <168960745735.34107.7883639718251524290.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <168960739768.34107.15145201749042174448.stgit@devnote2> References: <168960739768.34107.15145201749042174448.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771685082293182572 X-GMAIL-MSGID: 1771685082293182572 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) --- 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 4442ff9c2728..4d1dccb8724b 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -319,6 +319,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 = traceprobe_get_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) @@ -660,6 +731,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)) @@ -1102,8 +1180,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 050909aaaa1b..604d6fb9c5ff 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -504,7 +504,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