mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] Fix leak in drivers/target/iscsi/iscsi_target_parameters.c::iscsi_copy_param_list()
@ 2011-07-31 22:28 Jesper Juhl
  2011-07-31 22:33 ` Jesper Juhl
  2011-07-31 22:44 ` Joe Perches
  0 siblings, 2 replies; 7+ messages in thread
From: Jesper Juhl @ 2011-07-31 22:28 UTC (permalink / raw)
  To: Nicholas A. Bellinger
  Cc: Andy Grover, Roland Dreier, Christoph Hellwig, linux-scsi, linux-kernel

We leak memory if the allocations for 'new_param->name' or 'new_param->value' fail.
We also do a lot of variable assignments that are completely pointless
if the allocations fail.
So, let's move the allocations before the assignments and also make
sure that we free whatever was allocated to one if the allocation for
another fails.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
---
 drivers/target/iscsi/iscsi_target_parameters.c |   26 ++++++++++-------------
 1 files changed, 11 insertions(+), 15 deletions(-)

 compile tested only

diff --git a/drivers/target/iscsi/iscsi_target_parameters.c b/drivers/target/iscsi/iscsi_target_parameters.c
index 252e246..c708092 100644
--- a/drivers/target/iscsi/iscsi_target_parameters.c
+++ b/drivers/target/iscsi/iscsi_target_parameters.c
@@ -572,6 +572,17 @@ int iscsi_copy_param_list(
 			goto err_out;
 		}
 
+		new_param->name = kmalloc(strlen(param->name) + 1, GFP_KERNEL);
+		new_param->value = kmalloc(strlen(param->value) + 1, GFP_KERNEL);
+		if (!new_param->name || !new_param->value) {
+			kfree(new_param->value);
+			kfree(new_param->name);
+			kfree(new_param);
+			pr_err("Unable to allocate memory for parameter "
+				"name/value.\n");
+			goto err_out;
+		}
+
 		new_param->set_param = param->set_param;
 		new_param->phase = param->phase;
 		new_param->scope = param->scope;
@@ -580,21 +591,6 @@ int iscsi_copy_param_list(
 		new_param->use = param->use;
 		new_param->type_range = param->type_range;
 
-		new_param->name = kzalloc(strlen(param->name) + 1, GFP_KERNEL);
-		if (!new_param->name) {
-			pr_err("Unable to allocate memory for"
-				" parameter name.\n");
-			goto err_out;
-		}
-
-		new_param->value = kzalloc(strlen(param->value) + 1,
-				GFP_KERNEL);
-		if (!new_param->value) {
-			pr_err("Unable to allocate memory for"
-				" parameter value.\n");
-			goto err_out;
-		}
-
 		memcpy(new_param->name, param->name, strlen(param->name));
 		new_param->name[strlen(param->name)] = '\0';
 		memcpy(new_param->value, param->value, strlen(param->value));
-- 
1.7.6

-- 
Jesper Juhl <jj@chaosbits.net>       http://www.chaosbits.net/
Don't top-post http://www.catb.org/jargon/html/T/top-post.html
Plain text mails only, please.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-08-04 18:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-31 22:28 [PATCH] Fix leak in drivers/target/iscsi/iscsi_target_parameters.c::iscsi_copy_param_list() Jesper Juhl
2011-07-31 22:33 ` Jesper Juhl
2011-07-31 22:44 ` Joe Perches
2011-08-01 20:32   ` [PATCH v2] " Jesper Juhl
2011-08-01 23:32     ` Joe Perches
2011-08-02  8:26       ` [PATCH v3] " Jesper Juhl
2011-08-04 18:04         ` Nicholas A. Bellinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®