Skip to contentSkip to conversation

lpfc: fix memory leak and NULL dereference

PATCH In review Sudip Mukherjee 2 participants 3 messages Sep 23, 2015 – Oct 12, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lpfc: fix memory leak and NULL dereference

kmalloc() can return NULL and without checking we were dereferencing it. Moreover if kmalloc succeeds but the function fails in other parts then we were returning the error code but we missed freeing lcb_context. While at it fixed one related checkpatch warning.

---

I am not exactly sure if LSRJT_UNABLE_TPC is the right error code here. But that was my best guess.

 drivers/scsi/lpfc/lpfc_els.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/lpfc/lpfc_els.c +7 -1
index 36bf58b..a27efd9 100644
@@ -5209,7 +5209,6 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 		rjt_err = LSRJT_CMD_UNSUPPORTED;
 		goto rjt;
 	}
-	lcb_context = kmalloc(sizeof(struct lpfc_lcb_context), GFP_KERNEL);
 
 	if (phba->hba_flag & HBA_FCOE_MODE) {
 		rjt_err = LSRJT_CMD_UNSUPPORTED;
@@ -5240,6 +5239,12 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 		goto rjt;
 	}
 
+	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
+	if (!lcb_context) {
+		rjt_err = LSRJT_UNABLE_TPC;
+		goto rjt;
+	}
+
 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
 	lcb_context->sub_command = beacon->lcb_sub_command;
 	lcb_context->type = beacon->lcb_type;
@@ -5250,6 +5255,7 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
 		lpfc_printf_vlog(ndlp->vport, KERN_ERR,
 				 LOG_ELS, "0193 failed to send mail box");
+		kfree(lcb_context);
 		lpfc_nlp_put(ndlp);
 		rjt_err = LSRJT_UNABLE_TPC;
 		goto rjt;
Signature · 1 line
1.9.1
#1 lore raw
Quoted 7 lines · Sudip Mukherjee

kmalloc() can return NULL and without checking we were dereferencing it. Moreover if kmalloc succeeds but the function fails in other parts then we were returning the error code but we missed freeing lcb_context. While at it fixed one related checkpatch warning.

---

A gentle ping.

regards
sudip

Quoted 43 lines

I am not exactly sure if LSRJT_UNABLE_TPC is the right error code here. But that was my best guess.

 drivers/scsi/lpfc/lpfc_els.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/lpfc/lpfc_els.c +7 -1
index 36bf58b..a27efd9 100644
@@ -5209,7 +5209,6 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 		rjt_err = LSRJT_CMD_UNSUPPORTED;
 		goto rjt;
 	}
-	lcb_context = kmalloc(sizeof(struct lpfc_lcb_context), GFP_KERNEL);
 
 	if (phba->hba_flag & HBA_FCOE_MODE) {
 		rjt_err = LSRJT_CMD_UNSUPPORTED;
@@ -5240,6 +5239,12 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 		goto rjt;
 	}
 
+	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
+	if (!lcb_context) {
+		rjt_err = LSRJT_UNABLE_TPC;
+		goto rjt;
+	}
+
 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
 	lcb_context->sub_command = beacon->lcb_sub_command;
 	lcb_context->type = beacon->lcb_type;
@@ -5250,6 +5255,7 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
 		lpfc_printf_vlog(ndlp->vport, KERN_ERR,
 				 LOG_ELS, "0193 failed to send mail box");
+		kfree(lcb_context);
 		lpfc_nlp_put(ndlp);
 		rjt_err = LSRJT_UNABLE_TPC;
 		goto rjt;
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Sudip Mukherjee

Looks Good - Thank you Sudip.

-- james s

Quoted 47 lines · Sudip Mukherjee

kmalloc() can return NULL and without checking we were dereferencing it. Moreover if kmalloc succeeds but the function fails in other parts then we were returning the error code but we missed freeing lcb_context. While at it fixed one related checkpatch warning.

---

I am not exactly sure if LSRJT_UNABLE_TPC is the right error code here. But that was my best guess.

  drivers/scsi/lpfc/lpfc_els.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/lpfc/lpfc_els.c +7 -1
index 36bf58b..a27efd9 100644
@@ -5209,7 +5209,6 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
  		rjt_err = LSRJT_CMD_UNSUPPORTED;
  		goto rjt;
  	}
-	lcb_context = kmalloc(sizeof(struct lpfc_lcb_context), GFP_KERNEL);
 
  	if (phba->hba_flag & HBA_FCOE_MODE) {
  		rjt_err = LSRJT_CMD_UNSUPPORTED;
@@ -5240,6 +5239,12 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
  		goto rjt;
  	}
 
+	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
+	if (!lcb_context) {
+		rjt_err = LSRJT_UNABLE_TPC;
+		goto rjt;
+	}
+
  	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
  	lcb_context->sub_command = beacon->lcb_sub_command;
  	lcb_context->type = beacon->lcb_type;
@@ -5250,6 +5255,7 @@ lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
  	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
  		lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  				 LOG_ELS, "0193 failed to send mail box");
+		kfree(lcb_context);
  		lpfc_nlp_put(ndlp);
  		rjt_err = LSRJT_UNABLE_TPC;
  		goto rjt;