[PATCH] bcache: Fix memory leak when cache_alloc() return failed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Wu Bo <wubo40@xxxxxxxxxx>

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@xxxxxxxxxx>
---
 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




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux ARM Kernel]     [Linux Filesystem Development]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux