[PATCH] nbd: fix possible memory leak

From: Sudip Mukherjee
Date: Tue Jan 13 2015 - 12:19:34 EST


we have already allocated memory for nbd_dev, but we were not
releasing that memory and just returning the error value.

Signed-off-by: Sudip Mukherjee <sudip@xxxxxxxxxxxxxxx>
---

Hi Paul,
after seeing your mail about maintainers, just started to see the code.
this patch is one way, another way can be to bring the kcalloc part below these checks.

drivers/block/nbd.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 4bc2a5c..13ea239 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -822,11 +822,15 @@ static int __init nbd_init(void)
max_part = (1UL << part_shift) - 1;
}

- if ((1UL << part_shift) > DISK_MAX_PARTS)
- return -EINVAL;
+ if ((1UL << part_shift) > DISK_MAX_PARTS) {
+ err = -EINVAL;
+ goto out_free;
+ }

- if (nbds_max > 1UL << (MINORBITS - part_shift))
- return -EINVAL;
+ if (nbds_max > 1UL << (MINORBITS - part_shift)) {
+ err = -EINVAL;
+ goto out_free;
+ }

for (i = 0; i < nbds_max; i++) {
struct gendisk *disk = alloc_disk(1 << part_shift);
@@ -889,6 +893,7 @@ out:
blk_cleanup_queue(nbd_dev[i].disk->queue);
put_disk(nbd_dev[i].disk);
}
+out_free:
kfree(nbd_dev);
return err;
}
--
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/