From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078625; b=XmJPIFfiTsxMEfm4yNj9nQUWkfhJSSIdyvhwdaveGNDIjHrHhZJwakRbcazjYOqFyzHs WK2TzEUoqkpSj5H4fIE957+SFbAwbQ+n4h/9UiPjAWM81INLYL9f54tIYu67yfX6oB4tV 04tOb0IHfvIWiLHtzc8hPMsd0ljCFTV7L476qDhevpUWbxXhpHqlQZFe+Lexo6dUX2u4o mOyv05VzTnC3LGUluzYjU7pyvtqYcjUzlMJEsd2648K6k/pZydQ8ZYFQWslKS5VLZaE4u xQ3/SeB8ZKtZWBuqvLuCbsQEKn7sSHqY/6zZlJcXjWYRXg67ncFT0dnxJmMdWc4Qopw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078625; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=I8YitrS7TqwWx7AUr68TWjT4MlBARLr8b7aAVyQ7aSBGUnIAdYS/mfy/LIV1QlOoot3J 05S1IT/CAuXYL8JaNRT6nC7dMZ/a2Cn941GDHhJiTF4Rdp8ouLP+mE1ojHAl8HLEd/vRw ZZExfGb3a9QKeYnzMYYI7/WkBMcI6zMCYtioPWki4afDBV9ygUyW5OpiPt2LulsRfWOSh eNeA1ciQCQGHTibXROVNRYxaM9FEOeq39+92kZTsh/eeYCa2oDkLB4Gm9WDyvahBERPrC KqrXie4Ab8dz63D+C2az4dv0XsJjx0lPM0PKiHXvgOGTzUxa5DPAjgHhRQPaela7VLA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EPvkXsU7; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9434B606A692 for ; Mon, 30 Nov 2026 22:43:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9434B606A692 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EPvkXsU7 Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-375bac6bd21so10546922eec.0 for ; Mon, 30 Nov 2026 14:43:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796078624; x=1796683424; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=EPvkXsU7Zb6i85YzUFDK/+AQxWN4rNqKKkF5YbgEuHUAGZR+IAkmfc1PXHFvG/X/PN WYZtZuCDGJx1Sz4ewRMn/GszvzmoLIhzVFdwHheKSYBiPVR1AzELxzAnlCP4r+HLamRf x3+q6+S6/Q3QIBydM37rltxZzDY1yjky5f8tVoM7BVli0g5CpgMcmQuaGaLCpEwsqBX+ xluYzUbtQ5rLQwxppk3TWkAag0m8ZvsiXACYexSTrf1YGAcKgqGUzxR0t57cPSxLjkX2 4VQ97WmnHeCOuUQyNZ5dbSRk/OMM04db/KjgKZl4alrSHwVOSm23sc4gIrWD5YJ26HpW 4trg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078624; x=1796683424; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=u82mHj26OVgph8a2gZ5f054PoKAsAGTJII6y60jo8LI2fJqUZy4lhJOwd1JwM5mpkb Y3u3R+cpkPlF90PyKxnAGNFr9heAfJhTeiLDf/ZMSw/Ce4nr2OceBjTjC2+8wHwWfTyl CH2APtE+P2x3MiqOSoX4RNg/QJYLxzE5PCJGN1TOabFLT77q5I7eqBQMn30BNOJ9P+1y PbkoGv3l11s89uvfZny+ZEGKk4u3rYFBh3L9dW4cIu4aIyxAA8wbJgs7Bv9WjLR/h765 K5eiY0yX1PWkdjKiZlKoAwnKrM8+qrHFtqTxnMtAstE/iBvgZbs7C2j+5VxpxKZK7HPi Mraw== X-Forwarded-Encrypted: i=1; AL+hErH1M/SUo+0WTxvBFDXJSQAxDTUbv6I/IdkPuBxGmY4iXb7vg+ovmgF5CPS9ud7JLQSultiDeIz+pBSLKac=@vger.kernel.org X-Gm-Message-State: APuU4Z80N35k8PLCZpA0aigC/US1Zbr+oIOpsksBe0hOQMaWMPrxGXhn hrWNN7qXAFjhXLzxEoR8NELfRFuhGE1v47mywPgwJd2QLR7IUm4PkZqxXrWjqRrsoKDWapQtpOZ 6XuTvvky/XA== X-Received: from dycmb5-n2.prod.google.com ([2002:a05:693c:20c5:20b0:369:fc78:c914]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:d18b:b0:386:b4f1:dab0 with SMTP id 5a478bee46e88-386b4f1e04fmr26151100eec.1.1796078623290; Mon, 30 Nov 2026 14:43:43 -0800 (PST) Date: Mon, 30 Nov 2026 14:43:31 -0800 In-Reply-To: <20261130224332.79575-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130224332.79575-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130224332.79575-4-irogers@google.com> Subject: [PATCH v2 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Jiri Olsa , Adrian Hunter , James Clark , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" In resolve_metric(), growing the pending array assigns the result of realloc() directly back to pending; if realloc() fails after earlier iterations succeeded, the existing pending allocation is overwritten with NULL and leaked. Similarly, in __add_metric(), assigning realloc() directly to root_metric->metric_refs overwrites the existing pointer with NULL on failure, preventing metric__free(root_metric) in the root caller from freeing the prior metric_refs array. Use temporary pointers for both realloc() calls, freeing pending before returning -ENOMEM in resolve_metric() and preserving root_metric->metric_refs for metric__free() in __add_metric(). Fixes: 80be6434c36f ("perf metric: Modify resolution and recursion check") Fixes: 46bdc0bf8d21 ("perf metric: Simplify metric_refs calculation") Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 779fe56fe8d0..489f13b34361 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -771,10 +771,15 @@ static int resolve_metric(struct list_head *metric_list, if (pmu_metrics_table__find_metric(table, pmu, cur->pkey, metricgroup__find_metric_callback, &pm) != PMU_METRICS__NOT_FOUND) { - pending = realloc(pending, - (pending_cnt + 1) * sizeof(struct to_resolve)); - if (!pending) + struct to_resolve *new_pending; + + new_pending = realloc(pending, + (pending_cnt + 1) * sizeof(struct to_resolve)); + if (!new_pending) { + free(pending); return -ENOMEM; + } + pending = new_pending; memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm)); pending[pending_cnt].key = cur->pkey; @@ -863,6 +868,7 @@ static int __add_metric(struct list_head *metric_list, return -ENOMEM; } else { + struct metric_ref *new_refs; int cnt = 0; /* @@ -879,10 +885,11 @@ static int __add_metric(struct list_head *metric_list, } /* Create reference. Need space for the entry and the terminator. */ - root_metric->metric_refs = realloc(root_metric->metric_refs, - (cnt + 2) * sizeof(struct metric_ref)); - if (!root_metric->metric_refs) + new_refs = realloc(root_metric->metric_refs, + (cnt + 2) * sizeof(struct metric_ref)); + if (!new_refs) return -ENOMEM; + root_metric->metric_refs = new_refs; /* * Intentionally passing just const char pointers, -- 2.56.1.839.g22a8290db9-goog