Re: [patch v3] infiniband: ulp/iser, fix error retval in iser_create_ib_conn_res

Previous thread: [tip:sched/core] sched: Remove some dead code by tip-bot for Dan Carpenter on Tuesday, March 16, 2010 - 7:47 am. (1 message)

Next thread: [PATCH 1/1] DVB-USB: gp8psk, fix potential null derefernce by Jiri Slaby on Tuesday, March 16, 2010 - 7:51 am. (1 message)
From: Jiri Slaby
Date: Tuesday, March 16, 2010 - 7:49 am

Stanse foudn unreachable code in iser_create_ib_conn_res. Namely there
is switched goto and ret assignment. Switch them to make sense and
return ENOMEM in case of error.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Cc: Or Gerlitz <ogerlitz@voltaire.com>
Cc: Roland Dreier <rolandd@cisco.com>
---
 drivers/infiniband/ulp/iser/iser_verbs.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/infiniband/ulp/iser/iser_verbs.c b/drivers/infiniband/ulp/iser/iser_verbs.c
index 308d17b..6d4ba35 100644
--- a/drivers/infiniband/ulp/iser/iser_verbs.c
+++ b/drivers/infiniband/ulp/iser/iser_verbs.c
@@ -149,8 +149,8 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 
 	ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
 	if (!ib_conn->login_buf) {
-		goto alloc_err;
 		ret = -ENOMEM;
+		goto alloc_err;
 	}
 
 	ib_conn->login_dma = ib_dma_map_single(ib_conn->device->ib_device,
-- 
1.7.0.1


--

From: Roland Dreier
Date: Tuesday, March 16, 2010 - 4:52 pm

>  	ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
 >  	if (!ib_conn->login_buf) {
 > -		goto alloc_err;
 >  		ret = -ENOMEM;
 > +		goto alloc_err;
 >  	}

This looks like a valid fix but it's the same part of the code that Dan
found more extensive bugs in.  Or, can I assume you'll sort this out and
make sure a final patch is sorted out?

Thanks,
  Roland
-- 
Roland Dreier  <rolandd@cisco.com>
For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html
--

From: Dan Carpenter
Date: Wednesday, March 17, 2010 - 8:11 am

My patch is the one to take.  It addresses this problem here and fixes
some double frees.

http://marc.info/?l=linux-rdma&m=126872745710910&w=2

Sorry about that Jiri.  Btw. Julia Lawall, Daren Jenkins and I normally CC 
kernel-janitors for static checker patches.

regards,
--

From: Roland Dreier
Date: Wednesday, March 31, 2010 - 2:06 pm

So looking at merging this finally, I think I see one problem with the
proposed patch.  We have:

 > @@ -183,7 +180,7 @@  static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 >  	ib_conn->fmr_pool = ib_create_fmr_pool(device->pd, &params);
 >  	if (IS_ERR(ib_conn->fmr_pool)) {
 >  		ret = PTR_ERR(ib_conn->fmr_pool);
 > -		goto fmr_pool_err;
 > +		goto out_err;
 >  	}

and

 > @@ -209,12 +206,7 @@  static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 >  		 ib_conn->fmr_pool, ib_conn->cma_id->qp);
 >  	return ret;
 >  
 > -qp_err:
 > -	(void)ib_destroy_fmr_pool(ib_conn->fmr_pool);
 > -fmr_pool_err:
 > -	kfree(ib_conn->page_vec);
 > -	kfree(ib_conn->login_buf);
 > -alloc_err:
 > +out_err:
 >  	iser_err("unable to alloc mem or create resource, err %d\n", ret);
 >  	return ret;
 >  }

so if ib_create_fmr_pool() fails, we're left with ib_conn->fmr_pool
holding an error pointer, right?  But we're relying on
iser_free_ib_conn_res() to clean up after us, and that has:

	if (ib_conn->fmr_pool != NULL)
		ib_destroy_fmr_pool(ib_conn->fmr_pool);

so we're going to end up trying to free an error pointer, which will
probably crash.

I think.

Dan or Or, am I wrong here or do we need another iteration of this
patch? (the login_buf and page_vec changes do look correct to me, since
a failed kmalloc() will leave us with a NULL pointer that it is safe to
kfree() later)

 - R.
-- 
Roland Dreier <rolandd@cisco.com> || For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html
--

From: Dan Carpenter
Date: Thursday, April 1, 2010 - 8:38 am

You're right.  I missed that.

I will send an updated patch tomorrow.

regards,
--

From: Dan Carpenter
Date: Friday, April 2, 2010 - 4:27 am

We shouldn't free things here because we free them later.  The call 
tree looks like this:
iser_connect() => iser_cma_handler() => iser_route_handler()
        => iser_create_ib_conn_res()

                We fail here and return failures back down the tree.

iser_connect() => iser_conn_release()

                Double free.                                                                                                 

Signed-off-by: Dan Carpenter <error27@gmail.com>
---
V1 fixed unreachable code
V2 noticed that the original code had a double free
V3 Roland Dreier points out that I left a dangling ERR_PTR() in
   "ib_conn->fmr_pool" which would be freed later on.

diff --git a/drivers/infiniband/ulp/iser/iser_verbs.c b/drivers/infiniband/ulp/iser/iser_verbs.c
index 308d17b..ccab795 100644
--- a/drivers/infiniband/ulp/iser/iser_verbs.c
+++ b/drivers/infiniband/ulp/iser/iser_verbs.c
@@ -148,10 +148,8 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 	device = ib_conn->device;
 
 	ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
-	if (!ib_conn->login_buf) {
-		goto alloc_err;
-		ret = -ENOMEM;
-	}
+	if (!ib_conn->login_buf)
+		goto out_err;
 
 	ib_conn->login_dma = ib_dma_map_single(ib_conn->device->ib_device,
 				(void *)ib_conn->login_buf, ISER_RX_LOGIN_SIZE,
@@ -160,10 +158,9 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 	ib_conn->page_vec = kmalloc(sizeof(struct iser_page_vec) +
 				    (sizeof(u64) * (ISCSI_ISER_SG_TABLESIZE +1)),
 				    GFP_KERNEL);
-	if (!ib_conn->page_vec) {
-		ret = -ENOMEM;
-		goto alloc_err;
-	}
+	if (!ib_conn->page_vec)
+		goto out_err;
+
 	ib_conn->page_vec->pages = (u64 *) (ib_conn->page_vec + 1);
 
 	params.page_shift        = SHIFT_4K;
@@ -183,7 +180,8 @@ static int iser_create_ib_conn_res(struct iser_conn *ib_conn)
 	ib_conn->fmr_pool = ib_create_fmr_pool(device->pd, &params);
 	if (IS_ERR(ib_conn->fmr_pool)) {
 		ret = PTR_ERR(ib_conn->fmr_pool);
-		goto ...
From: Roland Dreier
Date: Wednesday, May 5, 2010 - 11:09 am

Or, I don't think we ever fixed this.  This patch looks correct to me,
any problem with merging this for 2.6.35?
-- 
Roland Dreier <rolandd@cisco.com> || For corporate legal information go to:
http://www.cisco.com/web/about/doing_business/legal/cri/index.html
--

From: Or Gerlitz
Date: Thursday, May 6, 2010 - 6:22 am

Roland, please use V4 below, the patch is okay and would apply before and after 
applying the multipathing patches I sent yesterday (same goes for them).

[PATCH V4] ib/iser: fix error flow in iser_create_ib_conn_res

From: Dan Carpenter <error27@gmail.com>

We shouldn't free things here because we free them later. 
The call tree looks like this: 
	iser_connect() ==> initiating the connection establishment
and later 
	iser_cma_handler() => iser_route_handler() => iser_create_ib_conn_res()
if we fail here, eventually iser_conn_release() is called, resulted in double free.
                                                                                                 
Signed-off-by: Dan Carpenter <error27@gmail.com>
Signed-off-by: Or Gerlitz <ogerlitz@voltaire.com>
---
V1 fixed unreachable code
V2 noticed that the original code had a double free
V3 Roland Dreier points out that I left a dangling ERR_PTR() in
   "ib_conn->fmr_pool" which would be freed later on.
V4 reviewed/enhanced the change-log
---
 drivers/infiniband/ulp/iser/iser_verbs.c |   25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

Index: linux-2.6.34-rc6/drivers/infiniband/ulp/iser/iser_verbs.c
===================================================================
--- linux-2.6.34-rc6.orig/drivers/infiniband/ulp/iser/iser_verbs.c
+++ linux-2.6.34-rc6/drivers/infiniband/ulp/iser/iser_verbs.c
@@ -163,10 +163,8 @@ static int iser_create_ib_conn_res(struc
 	device = ib_conn->device;
 
 	ib_conn->login_buf = kmalloc(ISER_RX_LOGIN_SIZE, GFP_KERNEL);
-	if (!ib_conn->login_buf) {
-		goto alloc_err;
-		ret = -ENOMEM;
-	}
+	if (!ib_conn->login_buf)
+		goto out_err;
 
 	ib_conn->login_dma = ib_dma_map_single(ib_conn->device->ib_device,
 				(void *)ib_conn->login_buf, ISER_RX_LOGIN_SIZE,
@@ -175,10 +173,9 @@ static int iser_create_ib_conn_res(struc
 	ib_conn->page_vec = kmalloc(sizeof(struct iser_page_vec) +
 				    (sizeof(u64) * (ISCSI_ISER_SG_TABLESIZE +1)),
 				    ...
Previous thread: [tip:sched/core] sched: Remove some dead code by tip-bot for Dan Carpenter on Tuesday, March 16, 2010 - 7:47 am. (1 message)

Next thread: [PATCH 1/1] DVB-USB: gp8psk, fix potential null derefernce by Jiri Slaby on Tuesday, March 16, 2010 - 7:51 am. (1 message)