From patchwork Thu Apr 29 04:01:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Tzvetomir Stoyanov (VMware)" X-Patchwork-Id: 12230411 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F400C433ED for ; Thu, 29 Apr 2021 04:01:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 048FD61450 for ; Thu, 29 Apr 2021 04:01:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230103AbhD2ECN (ORCPT ); Thu, 29 Apr 2021 00:02:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbhD2ECL (ORCPT ); Thu, 29 Apr 2021 00:02:11 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1BB3C06138B for ; Wed, 28 Apr 2021 21:01:24 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id i3so51150129edt.1 for ; Wed, 28 Apr 2021 21:01:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zVFwLv+PTToBTggoexzPUquS2fS8UZ66ZfmMfMUkv8g=; b=Ig1tVEiHfIsCJ9ughsk0JkD0wCXXb+gBQEgTXtl4d8UkcXVGZTOzIqSV0IwN/bIwRg AZFPD7jCPK9GicIVKwaAKJyCAmSygppopGJ4yw9jt8wVTjj/skAYbFtsuH6JWL2ggNUt 4pSqzfLmpzNe4VZFKRGMeCAVGJDvR80qvIZvouyUjnjWS2Uh0wcZnX1zCWgSUaMQy1c5 fRMuL1PxoQLf3RnxmEuOKgowsnSlZUTtDTfzo38a57GfF/09zcoxNm4U7Q3MItO4c8F1 qLXxnzmKcSRDEq9MVEPgdicZWa/98GvtVj9xjfafKv/odfohTzKq/8tuZADU33/zRPix A0Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zVFwLv+PTToBTggoexzPUquS2fS8UZ66ZfmMfMUkv8g=; b=auFBqubPJpTN5Avh7f1N+jdpcxsCjoAhPY+2A2vLGhLiNC0sBPu3CnMO3KUzyN7y+E M8bERd0EHbwmeHbLPzSQJdTRn1v3Epr0Jg3SFVHtBR6yt5KfRaleC2vCBIAf7m0QMwst cU9JnTUg4/M7JI25739yuw2qO1DY3j5mN5xM6XmW6DXJTMfUG0PL73Fg7X2TNMlcUygl gt7XbJTv5FcbM31nqRHHhA65hqEKDrEWV9I7STl7v/glgliFIfMFbbqH0jWz2wLDwFm1 WDvg4UkFfYpaVoEJkhfVfwwVE5KA2lJ/t8eB8UoFyoG0ihETeGtkoGfL6lh41nJoGNq/ 1Zfg== X-Gm-Message-State: AOAM532rRzGIFE0eQ1okG3EWTNja5JDyvXz4WexeJD9P/ZfR+uQ4JBSJ wtpj1vPmbM5rVKGtWA9RrAE= X-Google-Smtp-Source: ABdhPJxapQDhBxCDTSXS4/HkpKWXWA0AjpRGlJo5GHDpJreHKCVxiUEeG3tTNPW/HDlcC0y27N9MpQ== X-Received: by 2002:a05:6402:351:: with SMTP id r17mr15650303edw.186.1619668883540; Wed, 28 Apr 2021 21:01:23 -0700 (PDT) Received: from oberon.zico.biz ([83.222.187.186]) by smtp.gmail.com with ESMTPSA id z26sm1040740ejl.38.2021.04.28.21.01.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Apr 2021 21:01:22 -0700 (PDT) From: "Tzvetomir Stoyanov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org Subject: [PATCH v2 2/7] trace-cmd: Check if file version is supported Date: Thu, 29 Apr 2021 07:01:14 +0300 Message-Id: <20210429040119.843617-3-tz.stoyanov@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210429040119.843617-1-tz.stoyanov@gmail.com> References: <20210429040119.843617-1-tz.stoyanov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org When reading a trace file, version of the file is ignored. This could case problems when bumping the version number because of changes in in the structure of the file. The old code should detect unsupported file version and should not try to read it. A new trace-cmd library API is added to check if version is supported: tracecmd_is_version_supported() Checks are added in the code to ensure not trying to read trace file with unsupported version. Signed-off-by: Tzvetomir Stoyanov (VMware) --- lib/trace-cmd/include/private/trace-cmd-private.h | 2 ++ lib/trace-cmd/trace-input.c | 10 ++++++++++ lib/trace-cmd/trace-util.c | 7 +++++++ tracecmd/trace-dump.c | 7 +++++++ 4 files changed, 26 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 8ec33ea2..cbd8dc1f 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -42,6 +42,8 @@ void tracecmd_record_ref(struct tep_record *record); void tracecmd_set_debug(bool set_debug); bool tracecmd_get_debug(void); +bool tracecmd_is_version_supported(unsigned int version); + struct tracecmd_output; struct tracecmd_recorder; struct hook_list; diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c index b17b36e0..a00fa982 100644 --- a/lib/trace-cmd/trace-input.c +++ b/lib/trace-cmd/trace-input.c @@ -130,6 +130,7 @@ struct tracecmd_input { bool use_trace_clock; bool read_page; bool use_pipe; + int file_version; struct cpu_data *cpu_data; long long ts_offset; struct tsc2nsec tsc_calc; @@ -3257,6 +3258,7 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags) unsigned int page_size; char *version; char buf[BUFSIZ]; + unsigned long ver; handle = malloc(sizeof(*handle)); if (!handle) @@ -3281,6 +3283,14 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags) if (!version) goto failed_read; pr_stat("version = %s\n", version); + ver = strtol(version, NULL, 10); + if (!ver && errno) + goto failed_read; + if (!tracecmd_is_version_supported(ver)) { + tracecmd_warning("Unsupported file version %lu", ver); + goto failed_read; + } + handle->file_version = ver; free(version); if (do_read_check(handle, buf, 1)) diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 2d3bc741..a6fc64c8 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -582,3 +582,10 @@ unsigned long long tracecmd_generate_traceid(void) free(str); return hash; } + +bool tracecmd_is_version_supported(unsigned int version) +{ + if (version <= FILE_VERSION) + return true; + return false; +} diff --git a/tracecmd/trace-dump.c b/tracecmd/trace-dump.c index 3f56f65a..aa48eaad 100644 --- a/tracecmd/trace-dump.c +++ b/tracecmd/trace-dump.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "trace-local.h" @@ -145,6 +146,7 @@ static void dump_initial_format(int fd) char magic[] = TRACECMD_MAGIC; char buf[DUMP_SIZE]; int val4; + unsigned long ver; do_print(SUMMARY, "\t[Initial format]\n"); @@ -166,6 +168,11 @@ static void dump_initial_format(int fd) die("no version string"); do_print(SUMMARY, "\t\t%s\t[Version]\n", buf); + ver = strtol(buf, NULL, 10); + if (!ver && errno) + die("Invalid file version string %s", buf); + if (!tracecmd_is_version_supported(ver)) + die("Unsupported file version %lu", ver); /* get file endianness*/ if (read_file_bytes(fd, buf, 1))