Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp6213238ybv; Tue, 18 Feb 2020 12:08:38 -0800 (PST) X-Google-Smtp-Source: APXvYqxiYtZXmTftQsHjhy6a1YLbWD3Y2r+hXgpbnaB9U3/4K92OPJyuVz6ottTo/SoPUoPeMuix X-Received: by 2002:a05:6830:1e72:: with SMTP id m18mr15943842otr.226.1582056518530; Tue, 18 Feb 2020 12:08:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582056518; cv=none; d=google.com; s=arc-20160816; b=ZqGwntlY+cu4luVvk4vAuf/zjJxBW24lD6kWxPSuc3Lljl72ngxmLSXcknldRsvclM pHQHo2sETFv2L3wUZjYMKhwIPLYEntmFvAk2E6e1qbjVdPZJi93N9XS7qxzWkRhFCM+4 v33BPQgF7pZULu7IVacELbVE6Jt34MYaAW07d7MDfNSegP5Jmy3TzD9JWc6N5Vxc//Zt A1tDAtBNPQxnLks+5I/2dUbqwe7PR5qzdruY8zTdTCVAuybyXM1Y5DeynU3NcMlEGTW5 5N+DC2BU0hOWtiYVO5pTSWDQg1RbJbfRJrakzfdkuT35JXVggWO0tkgBzvwf7n97ztV4 Z4pQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=93ttpJwhtDVSjEdkX1eyeVvOqXJQekMwx9uSlbpRUn0=; b=o+lmegQtqPNfWUaAn2JfXemBzL7IXWh6D3MHJKzSgfAdlUqAv/5sodRXCitMSsH2D7 h7Xky7y20/0KcSOUJGgWmfDHCJBR3X24i55Ta7hqfvA6p9kiVNo0td/JJBp/+8brp6nA x7zOTgEoqxiBEUS+otiUBSwsPTo6XnZdj2jar9vc56RqtfDx3EkBWjFuyUFqIz2xa+N2 EBt1jKYHpXwIORZkYcZgVLmbT90SBH08rZt4XiyytInXxVTQoaOVl05fqjlt4E3rMnmD VrPWATnhLSgz1uiHBPuszoJ7tMhJuM+aQK2iOMLEb0W2nlbEPSHy8pZAoP1QKKSnGTkc EjJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=I8TIB41J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l17si2208968otk.218.2020.02.18.12.08.26; Tue, 18 Feb 2020 12:08:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=I8TIB41J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727463AbgBRUIT (ORCPT + 99 others); Tue, 18 Feb 2020 15:08:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:35576 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727280AbgBRT6F (ORCPT ); Tue, 18 Feb 2020 14:58:05 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 2902A20659; Tue, 18 Feb 2020 19:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582055884; bh=oHjNPYRc6V5pjSMnY/9ywhh4+Sjp1tPVDNCkJphgWzo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I8TIB41JmU6qtIbsyR7mUY64+5j+yc4hMJmjPn1DNDf+JEqHMxqFkQuMChHWkLxS/ krgUJk1y8VAIflV/juorxwxpNwisW47haMnn9nALQSFP8sSsDQ0Ylh+oEC1o2l/fV9 lnEY1WCO3iQfGcdPfgolpzz/pm//USZw9MRcmIsg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wenwen Wang , David Sterba Subject: [PATCH 5.4 20/66] btrfs: ref-verify: fix memory leaks Date: Tue, 18 Feb 2020 20:54:47 +0100 Message-Id: <20200218190429.964168504@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200218190428.035153861@linuxfoundation.org> References: <20200218190428.035153861@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wenwen Wang commit f311ade3a7adf31658ed882aaab9f9879fdccef7 upstream. In btrfs_ref_tree_mod(), 'ref' and 'ra' are allocated through kzalloc() and kmalloc(), respectively. In the following code, if an error occurs, the execution will be redirected to 'out' or 'out_unlock' and the function will be exited. However, on some of the paths, 'ref' and 'ra' are not deallocated, leading to memory leaks. For example, if 'action' is BTRFS_ADD_DELAYED_EXTENT, add_block_entry() will be invoked. If the return value indicates an error, the execution will be redirected to 'out'. But, 'ref' is not deallocated on this path, causing a memory leak. To fix the above issues, deallocate both 'ref' and 'ra' before exiting from the function when an error is encountered. CC: stable@vger.kernel.org # 4.15+ Signed-off-by: Wenwen Wang Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ref-verify.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/fs/btrfs/ref-verify.c +++ b/fs/btrfs/ref-verify.c @@ -744,6 +744,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_i */ be = add_block_entry(fs_info, bytenr, num_bytes, ref_root); if (IS_ERR(be)) { + kfree(ref); kfree(ra); ret = PTR_ERR(be); goto out; @@ -757,6 +758,8 @@ int btrfs_ref_tree_mod(struct btrfs_fs_i "re-allocated a block that still has references to it!"); dump_block_entry(fs_info, be); dump_ref_action(fs_info, ra); + kfree(ref); + kfree(ra); goto out_unlock; } @@ -819,6 +822,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_i "dropping a ref for a existing root that doesn't have a ref on the block"); dump_block_entry(fs_info, be); dump_ref_action(fs_info, ra); + kfree(ref); kfree(ra); goto out_unlock; } @@ -834,6 +838,7 @@ int btrfs_ref_tree_mod(struct btrfs_fs_i "attempting to add another ref for an existing ref on a tree block"); dump_block_entry(fs_info, be); dump_ref_action(fs_info, ra); + kfree(ref); kfree(ra); goto out_unlock; }