Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

loopback: Fix memory leak in tcm_loop_make_scsi_hba()

There is a memory leak in tcm_loop_make_scsi_hba().

If all the strstr() calls return NULL and we end up at return ERR_PTR(-EINVAL);
then we'll be leaking the memory previously allocated to tl_hba as
that variable goes out of scope.

This patch should fix the leak.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Dan Carpenter <error27@gmail.com>
Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>

authored by

Jesper Juhl and committed by
Nicholas Bellinger
a57b5d36 824cc5ff

+8 -9
+8 -9
drivers/target/loopback/tcm_loop.c
··· 1288 1288 goto check_len; 1289 1289 } 1290 1290 ptr = strstr(name, "iqn."); 1291 - if (ptr) { 1292 - tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 1293 - goto check_len; 1291 + if (!ptr) { 1292 + printk(KERN_ERR "Unable to locate prefix for emulated Target " 1293 + "Port: %s\n", name); 1294 + ret = -EINVAL; 1295 + goto out; 1294 1296 } 1295 - 1296 - printk(KERN_ERR "Unable to locate prefix for emulated Target Port:" 1297 - " %s\n", name); 1298 - return ERR_PTR(-EINVAL); 1297 + tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI; 1299 1298 1300 1299 check_len: 1301 1300 if (strlen(name) >= TL_WWN_ADDR_LEN) { 1302 1301 printk(KERN_ERR "Emulated NAA %s Address: %s, exceeds" 1303 1302 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba), 1304 1303 TL_WWN_ADDR_LEN); 1305 - kfree(tl_hba); 1306 - return ERR_PTR(-EINVAL); 1304 + ret = -EINVAL; 1305 + goto out; 1307 1306 } 1308 1307 snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]); 1309 1308