Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp552902lqh; Tue, 7 May 2024 07:17:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUIHbjIDkHcX0kmi8T8Fb8cIFYUFxbBCSpCb0+zk70EgE7Y6RjDkxhIc4Towv8RH6Kz7zIuctipQs6FQoDO36cqHrMdkTTk9srEYd7f6A== X-Google-Smtp-Source: AGHT+IFQQ7/hzRIrxAdSCnPcTABoJpFJukCQw+NAfYsxu06T/6+kg+GLEpCJB5opclVkUcBVyhA/ X-Received: by 2002:a9d:6942:0:b0:6ee:ca2:523 with SMTP id p2-20020a9d6942000000b006ee0ca20523mr14422296oto.26.1715091466911; Tue, 07 May 2024 07:17:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715091466; cv=pass; d=google.com; s=arc-20160816; b=G+A1XUe16Db15xjRfwBOKF3bTJkckEYTunDhyQH50/gsc2eltarTHuIO9Hk7BuWdcv sxLlg4kI0ABDqE5Rj4hKybybsC+LImUbldoJ+7m6u/xG/2ESoKp3vkOcLfGbGM9LHpK0 gZ0bIoat/yenwSBeMnNYowvhxkq+uXshGiaHH45ffESTyJDW+5Y4IAstJj+eO2W9dmuz B4AookQlsFEIvkDFuCu1nk3m/3uLwFftTkE+6BzMEAxU746I38av2OcBHQ5jxUVnXkBo 6MjkK59pV0FKzqXBYVXBf7yHLr+1GG0TVtwCuUxW7bxU9VIBqyxcXR9jt24QelaU6HyJ hhGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=Lc8I5l0hhrbFStMpUKrdAfBo7WiXQNme33wokKUZp20=; fh=z3/YlGhmWVGbDDVRrPyvN7QnpotMsOuIPt85/Od1vi8=; b=Ibwig/88pwzw/eDalGxkqAvpX1+RcvyE1xSG/lJbB/Pdj+DE9hR0KwYjstzgIgw3Hp FP8oBrw3sjvRMIbmGEMj/lXIGAXHXD/Zc4yYDOnSc7xKVvLaQxb4QGWisgjLMwaeN4Ws nkQWWkBYKNFSjWcMq8/ZQArpEcOVSojr23KhjSM/wT5hlJz4DL4W4mSYQg+6jfkehHJX k3hfUGV6bTbsu9xIfO4X/pw/mgeqUHR+m29fyAwIrT72Bn09xGgqDkrNcW8RouF7RBzA aMQZBKufatoYKlDXNJEC9//mxMIjwOcbTrsM4moSwADwzjNeSd/tx0wdeGWZZTWUhlR7 aTNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-171537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171537-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q12-20020ab06c4c000000b007ed761cee46si2146248uas.151.2024.05.07.07.17.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 07:17:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-171537-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171537-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 86EC21C23323 for ; Tue, 7 May 2024 14:17:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D75AD16DEC9; Tue, 7 May 2024 14:13:32 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 201EA16D33F; Tue, 7 May 2024 14:13:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091210; cv=none; b=Dw4pKPJ4l+r9xjCtHduNBzu241IrbY48vZsO2/sYMqU54/L5X/ZdxAqBnnG22hcpIR+XnDAbyxCQPXPWt+3l3KBEMF6Tn29DknVoy15PV52LOFRL9wTRWy7/hyySslE3hjKL+Tf7UvTGvZvhKjiKgwioPEako3HnxkSdnQ1Jqb0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715091210; c=relaxed/simple; bh=inoTy0XjeXCVYFWYhXWKSmRMDG/2+bgKHRzwgIGvl9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=duOZBVqt5uDD2GQckRnqVFmuRDZujORD09WVQkXRR6SR+0Rhv1q0fs8mgXRJSivYWFQy9cRRQOJXsxgTsnqrC7OIqzhlTvmTOuvXrWWjLuvNK0z/kN9m2/RU+jsVnZg9otd6/fYV6ziIVXEJv0cjXod8Uo7BGOFteaHThdYamVM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6E6BF153B; Tue, 7 May 2024 07:13:37 -0700 (PDT) Received: from e127643.broadband (usa-sjc-mx-foss1.foss.arm.com [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2D7BF3F6A8; Tue, 7 May 2024 07:13:09 -0700 (PDT) From: James Clark To: linux-perf-users@vger.kernel.org, atrajeev@linux.vnet.ibm.com, irogers@google.com Cc: James Clark , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , "Liang, Kan" , linux-kernel@vger.kernel.org Subject: [PATCH 4/4] perf symbols: Fix ownership of string in dso__load_vmlinux() Date: Tue, 7 May 2024 15:12:08 +0100 Message-Id: <20240507141210.195939-5-james.clark@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240507141210.195939-1-james.clark@arm.com> References: <20240507141210.195939-1-james.clark@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The linked commit updated dso__load_vmlinux() to call dso__set_long_name() before loading the symbols. Loading the symbols may not succeed but dso__set_long_name() takes ownership of the string. The two callers of this function free the string themselves on failure cases, resulting in the following error: $ perf record -- ls $ perf report free(): double free detected in tcache 2 Fix it by always taking ownership of the string, even on failure. This means the string is either freed at the very first early exit condition, or later when the dso is deleted or the long name is replaced. Now no special return value is needed to signify that the caller needs to free the string. Fixes: e59fea47f83e ("perf symbols: Fix DSO kernel load and symbol process to correctly map DSO to its long_name, type and adjust_symbols") Signed-off-by: James Clark --- tools/perf/util/symbol.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index e98dfe766da3..6a0900dcdfd3 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1977,6 +1977,10 @@ int dso__load(struct dso *dso, struct map *map) return ret; } +/* + * Always takes ownership of vmlinux when vmlinux_allocated == true, even if + * it returns an error. + */ int dso__load_vmlinux(struct dso *dso, struct map *map, const char *vmlinux, bool vmlinux_allocated) { @@ -1995,8 +1999,11 @@ int dso__load_vmlinux(struct dso *dso, struct map *map, else symtab_type = DSO_BINARY_TYPE__VMLINUX; - if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) + if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) { + if (vmlinux_allocated) + free((char *) vmlinux); return -1; + } /* * dso__load_sym() may copy 'dso' which will result in the copies having @@ -2039,7 +2046,6 @@ int dso__load_vmlinux_path(struct dso *dso, struct map *map) err = dso__load_vmlinux(dso, map, filename, true); if (err > 0) goto out; - free(filename); } out: return err; @@ -2191,7 +2197,6 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map) err = dso__load_vmlinux(dso, map, filename, true); if (err > 0) return err; - free(filename); } if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { -- 2.34.1