lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <1631347734-9950-1-git-send-email-wubo40@huawei.com>
Date:   Sat, 11 Sep 2021 16:08:54 +0800
From:   Wu Bo <wubo40@...wei.com>
To:     <colyli@...e.de>, <kent.overstreet@...il.com>
CC:     <linux-bcache@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
        <linfeilong@...wei.com>, <wubo40@...wei.com>
Subject: [PATCH] bcache: Fix memory leak when cache_alloc() return failed

From: Wu Bo <wubo40@...wei.com>

If cache_alloc() get error when register a cache device,
the ca->kobj is not initialized, the bch_cache_release() no chance 
to be called. So "ca" object will not be released.

In addition, if register_cache_set() return failed 
when register a cache device, kobject_put(&ca->kobj) will be called 
and "ca" objects will be released in bch_cache_release() function. 
But pr_notice() called after kobject_put(&ca->kobj),
the "ca->cache_dev_name" access memory that has been freed.

Signed-off-by: Wu Bo <wubo40@...wei.com>
---
 drivers/md/bcache/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f2874c7..30569f4 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2366,13 +2366,17 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
 		 * explicitly call blkdev_put() here.
 		 */
 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
+		if (ca->sb_disk)
+			put_page(virt_to_page(ca->sb_disk));
 		if (ret == -ENOMEM)
 			err = "cache_alloc(): -ENOMEM";
 		else if (ret == -EPERM)
 			err = "cache_alloc(): cache device is too small";
 		else
 			err = "cache_alloc(): unknown error";
-		goto err;
+		pr_notice("error %s: %s\n", ca->cache_dev_name, err);
+		kfree(ca);
+		return ret;
 	}
 
 	if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) {
@@ -2393,11 +2397,9 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
 	pr_info("registered cache device %s\n", ca->cache_dev_name);
 
 out:
-	kobject_put(&ca->kobj);
-
-err:
 	if (err)
 		pr_notice("error %s: %s\n", ca->cache_dev_name, err);
+	kobject_put(&ca->kobj);
 
 	return ret;
 }
-- 
1.8.3.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ