Skip to contentSkip to conversation

RDMA/ucma: Fix resource leak on error path

PATCH Applied Alex Dewar 4 participants 6 messages Sep 2, 2020 – Sep 3, 2020 InfiniBand

lore
Share & tools
lore

Alex Dewar

author
#0 lore raw

PATCH 1/1 RDMA/ucma: Fix resource leak on error path

In ucma_process_join(), if the call to xa_alloc() fails, the function will return without freeing mc. Fix this by jumping to the correct line.

In the process I renamed the jump labels to something more memorable for extra clarity.

Addresses-Coverity: ("Resource leak")

Fixes 95fe51096b7a ("RDMA/ucma: Remove mc_list and rely on xarray")
---
 drivers/infiniband/core/ucma.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/infiniband/core/ucma.c +8 -7
index f2c9ef6ae481..a5595bd489b0 100644
@@ -1453,7 +1453,7 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
 	if (!mc) {
 		ret = -ENOMEM;
-		goto err1;
+		goto err_put_ctx;
 	}
 
 	mc->ctx = ctx;
@@ -1464,7 +1464,7 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 	if (xa_alloc(&multicast_table, &mc->id, NULL, xa_limit_32b,
 		     GFP_KERNEL)) {
 		ret = -ENOMEM;
-		goto err1;
+		goto err_free_mc;
 	}
 
 	mutex_lock(&ctx->mutex);
@@ -1472,13 +1472,13 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 				  join_state, mc);
 	mutex_unlock(&ctx->mutex);
 	if (ret)
-		goto err2;
+		goto err_xa_erase;
 
 	resp.id = mc->id;
 	if (copy_to_user(u64_to_user_ptr(cmd->response),
 			 &resp, sizeof(resp))) {
 		ret = -EFAULT;
-		goto err3;
+		goto err_leave_multicast;
 	}
 
 	xa_store(&multicast_table, mc->id, mc, 0);
@@ -1486,15 +1486,16 @@ static ssize_t ucma_process_join(struct ucma_file *file,
 	ucma_put_ctx(ctx);
 	return 0;
 
-err3:
+err_leave_multicast:
 	mutex_lock(&ctx->mutex);
 	rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
 	mutex_unlock(&ctx->mutex);
 	ucma_cleanup_mc_events(mc);
-err2:
+err_xa_erase:
 	xa_erase(&multicast_table, mc->id);
+err_free_mc:
 	kfree(mc);
-err1:
+err_put_ctx:
 	ucma_put_ctx(ctx);
 	return ret;
 }
Signature · 1 line
2.28.0
maintainer Applied
#1 lore raw
Quoted 9 lines · Alex Dewar

In ucma_process_join(), if the call to xa_alloc() fails, the function will return without freeing mc. Fix this by jumping to the correct line.

In the process I renamed the jump labels to something more memorable for extra clarity.

Addresses-Coverity: ("Resource leak")

Fixes 95fe51096b7a ("RDMA/ucma: Remove mc_list and rely on xarray")

Applied to for-next, thanks

Jason

#2 lore raw
In reply to Alex Dewar

Hi Alex,

Quoted 7 lines · Alex Dewar

In ucma_process_join(), if the call to xa_alloc() fails, the function will return without freeing mc. Fix this by jumping to the correct line.

In the process I renamed the jump labels to something more memorable for extra clarity.

Addresses-Coverity: ("Resource leak")

If you are using a public Coverity scan, please also include the Coverity ID. In this case ID 1496814, something like:

Addresses-Coverity-ID: 1496814 ("Resource leak")

People that don't include an CID is because they are using a private Coverity scan, so it doesn't make sense for them to add an ID because no one but them have access to that scan report.

Thanks
--
Gustavo

#3 lore raw
Quoted 9 lines · Gustavo A. R. Silva

Hi Alex,

Quoted 6 lines · Alex Dewar

In ucma_process_join(), if the call to xa_alloc() fails, the function will return without freeing mc. Fix this by jumping to the correct line.

In the process I renamed the jump labels to something more memorable for extra clarity.

Quoted 6 lines
Quoted 1 line

Addresses-Coverity: ("Resource leak")

If you are using a public Coverity scan, please also include the Coverity ID. In this case ID 1496814, something like:

Addresses-Coverity-ID: 1496814 ("Resource leak")

Thanks, I fixed it up

Jason

#4 lore raw
Quoted 9 lines · Jason Gunthorpe
Quoted 6 lines
Quoted 1 line

Addresses-Coverity: ("Resource leak")

If you are using a public Coverity scan, please also include the Coverity ID. In this case ID 1496814, something like:

Addresses-Coverity-ID: 1496814 ("Resource leak")

Thanks, I fixed it up

Awesome. :)

Thanks, Jason.
--
Gustavo

author
#5 lore raw
In reply to Gustavo A. R. Silva
Quoted 9 lines · Gustavo A. R. Silva

Hi Alex,

Quoted 6 lines · Alex Dewar

In ucma_process_join(), if the call to xa_alloc() fails, the function will return without freeing mc. Fix this by jumping to the correct line.

In the process I renamed the jump labels to something more memorable for extra clarity.

Quoted 6 lines
Quoted 1 line

Addresses-Coverity: ("Resource leak")

If you are using a public Coverity scan, please also include the Coverity ID. In this case ID 1496814, something like:

Addresses-Coverity-ID: 1496814 ("Resource leak")

Ahh ok. Thanks for the pointer :)

Quoted 9 lines

People that don't include an CID is because they are using a private Coverity scan, so it doesn't make sense for them to add an ID because no one but them have access to that scan report.

Thanks
--
Gustavo