Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1272649pxb; Fri, 18 Feb 2022 04:28:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2qRqtnc6U0cD6M/Z7YdgvpjQDjWedkr5/WOhoHRzOfwmaeOx2KKvDp6DHwSRY2YC60rLx X-Received: by 2002:a17:902:b597:b0:14f:3396:a7a5 with SMTP id a23-20020a170902b59700b0014f3396a7a5mr7326879pls.29.1645187318152; Fri, 18 Feb 2022 04:28:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645187318; cv=none; d=google.com; s=arc-20160816; b=g13k4UA8UT+gzJglWUh+hpDOVQawrlAPERdFyy8rbTuxB3vkT912uTaPnPCumSBdVd oFgz8boSOeBR0ojjzlUTkKzeOgc1QcfWGtOrxJiq0/O/XQ2KgE6xSWAq59jm0eMdOshi BbZABA/p2rZFEwbmOffqnpKKl1UFNAUTjhkUpt1IXIDFtkL1josVzIt/jq96Rlg1s3TR 7cYLZmFzgmq8G6AGkOZbeCzim+tl7F53ZcjBAorvBFMty0zWOUNQWKwcgrmjCxohTWoz t3I8c5AnEMVoXdFn0IwJhLANyGiuskylpwQ7x6HSKKlD9r3l5HvaOeWuMp1+WrgT9Jt7 J5EA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=+6leBE5n4gvEvwXHN+5Hn0e+ZKFZ/CBaPzlj01Skar8=; b=vByiPaQcynKLEtV/4WDY8LC9EA49XIW3Ng/I3Se7TNxUMnTEm6r7sxmzm7SQPnuE+c zb18gSr5J7PQZuGfh2D0V4lkz/2DBKIj0ysXgNgyQXp06FMuMLU6TIzFD7mzxZaLO5DD g35G77c1dbDzMyfL49zVw/O+x/q1eRVsjRABt+IinYWK1vAKJKkw0i9IIKIxalQLBZkG AfPi9QQkPPwfjrgLZPmiUZIoX4DlygHzVP7Is6pP3AIYFfzErglotfl5Bee3kUAeTFPC SMoNYxbMyXoo7rApveOEtk4z1CNPec/Ij7rxQPkMhzFqxQOkewFkpknAMDIqORRyel1Q INWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="A/PiPf+E"; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m14si13708405pgu.855.2022.02.18.04.28.23; Fri, 18 Feb 2022 04:28:38 -0800 (PST) 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=@intel.com header.s=Intel header.b="A/PiPf+E"; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234518AbiBRLac (ORCPT + 99 others); Fri, 18 Feb 2022 06:30:32 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:49080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234507AbiBRLaa (ORCPT ); Fri, 18 Feb 2022 06:30:30 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9632A2AF93C for ; Fri, 18 Feb 2022 03:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1645183814; x=1676719814; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=KGo8lcJ4F56kenYAwLaTbI82s91drMBP5GcCwMezJx8=; b=A/PiPf+EIvZffJNRgRcKqWOSvOERH03meykXbNTaq4IPLyRkgPkwjdOh 6LhODio+tx/Cy6BzIcjyI0V6hxx8sq2WBTFM27DxNrpmhyG14V+6If0D9 MGFuwY5ZWb3wCL2uKHmf9GPBeR+VXiBYZJSxQe7fK8lizXyAGX+akzaag dTSAlK4QpCr9BjeWuVSHk3fCparaGm5lRWeImFPnY4sZmdXKHuSJW/8dv 25hIiDQUxRNGTjauhrgCrC8CSYG6/6XeTfY1Z7JHT+0MzBdIYEHKNOFQk 2uqTzzjhFLZ887aRJkfW0kEVZC9aegw6woZbTO8WAXyi8G8bpMEyC7UEg g==; X-IronPort-AV: E=McAfee;i="6200,9189,10261"; a="337553335" X-IronPort-AV: E=Sophos;i="5.88,378,1635231600"; d="scan'208";a="337553335" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Feb 2022 03:30:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,378,1635231600"; d="scan'208";a="775265393" Received: from nntpat99-84.inn.intel.com ([10.125.99.84]) by fmsmga006.fm.intel.com with ESMTP; 18 Feb 2022 03:30:10 -0800 From: Alexey Bayduraev To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Ingo Molnar , linux-kernel , Adrian Hunter , Alexander Antonov , Alexei Budankov Subject: [PATCH] perf data: Fix double free in perf_session__delete Date: Fri, 18 Feb 2022 14:30:08 +0300 Message-Id: <20220218113008.24416-1-alexey.v.bayduraev@linux.intel.com> X-Mailer: git-send-email 2.19.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,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 When perf_data__create_dir fails, it calls close_dir, but perf_session__delete also calls close_dir and since dir.version and dir.nr was initialized by perf_data__create_dir, a double free occurs. This patch moves the initialization of dir.version and dir.nr after successful initialization of dir.files, that prevents double freeing. This behavior is already implemented in perf_data__open_dir. The patch also adds a missing error message in case data directory creation fails. Signed-off-by: Alexey Bayduraev --- tools/perf/builtin-record.c | 4 +++- tools/perf/util/data.c | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0bc6529814b2..0306d5911de2 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -1186,8 +1186,10 @@ static int record__mmap_evlist(struct record *rec, if (record__threads_enabled(rec)) { ret = perf_data__create_dir(&rec->data, evlist->core.nr_mmaps); - if (ret) + if (ret) { + pr_err("Failed to create data directory: %s\n", strerror(errno)); return ret; + } for (i = 0; i < evlist->core.nr_mmaps; i++) { if (evlist->mmap) evlist->mmap[i].file = &rec->data.dir.files[i]; diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c index f5d260b1df4d..15a4547d608e 100644 --- a/tools/perf/util/data.c +++ b/tools/perf/util/data.c @@ -44,10 +44,6 @@ int perf_data__create_dir(struct perf_data *data, int nr) if (!files) return -ENOMEM; - data->dir.version = PERF_DIR_VERSION; - data->dir.files = files; - data->dir.nr = nr; - for (i = 0; i < nr; i++) { struct perf_data_file *file = &files[i]; @@ -62,6 +58,9 @@ int perf_data__create_dir(struct perf_data *data, int nr) file->fd = ret; } + data->dir.version = PERF_DIR_VERSION; + data->dir.files = files; + data->dir.nr = nr; return 0; out_err: -- 2.19.0