Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3199798pxb; Mon, 9 Nov 2020 05:26:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvEGmoaunNaPL+JWv6ybTGpUYcXPmF8yMmPnDUA42pPZyhY8qTWWU0KOdAIsAeR2exZHyR X-Received: by 2002:aa7:d94c:: with SMTP id l12mr15735921eds.355.1604928373666; Mon, 09 Nov 2020 05:26:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604928373; cv=none; d=google.com; s=arc-20160816; b=yPmGnSay+in9iCyr5HG1x3req0B1ufDUXbzeGRMNhCWKDnFhImBMW2AJVwi/roAaFm V/2l2j0TFA5pSHQeT0Jbe2S/cBu91+F4pqMwe1g3wXxXYfCroWbZFSywavkeN3kMelbd Rly2sSL8OT/59YCtJkydDASkGti2GuLaOB6MjCmOVS4BJ3dA1OweNSIpLm/BEMQ8QCvo VlKOc9ykfQ0kfFaB/guiGcJcBxH/iLVUnvVmryzIiVzdPJ/mU81FzxlRTEaH1jt07Ma8 8ehmo51lljBXajyj4u2YuyGWydrwnFCLrvtwA1ubnkKNtKL/JccqPLYWrg26v1wnP5Ar LpVg== 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=vCBASRNnR2KnXM3imGJuz2D3bulBOLYpBAR2j613odU=; b=Dy63qK/97ICwMqBpIftKlHiM8ix8CiEHP/4iXRJLdlOY6VgAcHWrJqiWvqDT2tatwB yG1Z5P5cCFARlZXTxB2nPRfGVCY+jq+Vl4k+OqrL74fyFk6JxbvAIF5llROXdXrPkebc IQxxurTqVjuEUdBbOHC84KBDuJ0ITL6HCTgD6W+zE/jg8qz7Ltwiqpm1xt7fq1cOfuNl DsmkvCVOTAflLbJtaKT+VzeMVHRMxJCrsuXa3hreH/X15gMAlO1tGJ7y7EWeGRG3vVia uFxkV/0N4sEJ8Ri8/BUAa5GyGYKHb303otLOxNtAUKGwVrK7kcvgto1gT/v82GDa85WI dHJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gQGsQ9JT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c91si6651615edf.313.2020.11.09.05.25.49; Mon, 09 Nov 2020 05:26:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gQGsQ9JT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732704AbgKINWa (ORCPT + 99 others); Mon, 9 Nov 2020 08:22:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:50292 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732579AbgKINWT (ORCPT ); Mon, 9 Nov 2020 08:22:19 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F235B2076E; Mon, 9 Nov 2020 13:22:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604928138; bh=WKesmw1EB5KG72XqKogbKm0+CFZevf0raON6CsIXZqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gQGsQ9JTTqKLp9Z5H1Qpuk6nZQ0zBXt90vcEN33BmboFukMjyCtnO6fFZXQ8G2vf8 JrAd4tXpbbVIGjuArxQhqrPbE2fig0syT1wn/MYHpycolLAJMHP7YCwIxjyXa64+2Y nZfmXUcP3trprj3EjJb8u1885WBBAp2fcBgjlqu8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?kiyin ?= , Dan Carpenter , Ingo Molnar , "Srivatsa S. Bhat" , Anthony Liguori Subject: [PATCH 5.9 133/133] perf/core: Fix a memory leak in perf_event_parse_addr_filter() Date: Mon, 9 Nov 2020 13:56:35 +0100 Message-Id: <20201109125037.090717654@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125030.706496283@linuxfoundation.org> References: <20201109125030.706496283@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: kiyin(尹亮) commit 7bdb157cdebbf95a1cd94ed2e01b338714075d00 upstream. As shown through runtime testing, the "filename" allocation is not always freed in perf_event_parse_addr_filter(). There are three possible ways that this could happen: - It could be allocated twice on subsequent iterations through the loop, - or leaked on the success path, - or on the failure path. Clean up the code flow to make it obvious that 'filename' is always freed in the reallocation path and in the two return paths as well. We rely on the fact that kfree(NULL) is NOP and filename is initialized with NULL. This fixes the leak. No other side effects expected. [ Dan Carpenter: cleaned up the code flow & added a changelog. ] [ Ingo Molnar: updated the changelog some more. ] Fixes: 375637bc5249 ("perf/core: Introduce address range filtering") Signed-off-by: "kiyin(尹亮)" Signed-off-by: Dan Carpenter Signed-off-by: Ingo Molnar Cc: "Srivatsa S. Bhat" Cc: Anthony Liguori -- kernel/events/core.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) Signed-off-by: Greg Kroah-Hartman --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10058,6 +10058,7 @@ perf_event_parse_addr_filter(struct perf if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { int fpos = token == IF_SRC_FILE ? 2 : 1; + kfree(filename); filename = match_strdup(&args[fpos]); if (!filename) { ret = -ENOMEM; @@ -10104,16 +10105,13 @@ perf_event_parse_addr_filter(struct perf */ ret = -EOPNOTSUPP; if (!event->ctx->task) - goto fail_free_name; + goto fail; /* look up the path and grab its inode */ ret = kern_path(filename, LOOKUP_FOLLOW, &filter->path); if (ret) - goto fail_free_name; - - kfree(filename); - filename = NULL; + goto fail; ret = -EINVAL; if (!filter->path.dentry || @@ -10133,13 +10131,13 @@ perf_event_parse_addr_filter(struct perf if (state != IF_STATE_ACTION) goto fail; + kfree(filename); kfree(orig); return 0; -fail_free_name: - kfree(filename); fail: + kfree(filename); free_filters_list(filters); kfree(orig);