Skip to contentSkip to conversation

scsi: hisi_sas: Fix NULL pointer dereference

PATCH Applied Gustavo A. R. Silva 3 participants 4 messages Oct 18, 2018 – Oct 20, 2018

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

PATCH v2 1/1 scsi: hisi_sas: Fix NULL pointer dereference

There is a NULL pointer dereference in case *slot* happens to be NULL at lines 1053 and 1878:

struct hisi_sas_cq *cq =

	&hisi_hba->cq[slot->dlvry_queue];

Notice that *slot* is being NULL checked at lines 1057 and 1881:
if (slot), which implies it may be NULL.

Fix this by placing the declaration and definition of variable cq, which contains the pointer dereference slot->dlvry_queue, after slot has been properly NULL checked.

Addresses-Coverity-ID: 1474515 ("Dereference before null check")
Addresses-Coverity-ID: 1474520 ("Dereference before null check")

Fixes 584f53fe5f52 ("scsi: hisi_sas: Fix the race between IO completion and timeout for SMP/internal IO")

---
Changes in v2:
- Fix another instance of the same issue at line 1053.
- Update commit log.

 drivers/scsi/hisi_sas/hisi_sas_main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_main.c +4 -4
index 2e5eaf1..b3f01d5 100644
@@ -1050,11 +1050,11 @@ static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
 				struct hisi_sas_slot *slot = task->lldd_task;
-				struct hisi_sas_cq *cq =
-					&hisi_hba->cq[slot->dlvry_queue];
 
 				dev_err(dev, "abort tmf: TMF task timeout and not done\n");
 				if (slot) {
+					struct hisi_sas_cq *cq =
+					       &hisi_hba->cq[slot->dlvry_queue];
 					/*
 					 * flush tasklet to avoid free'ing task
 					 * before using task in IO completion
@@ -1875,10 +1875,10 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
 	if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
 		if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
 			struct hisi_sas_slot *slot = task->lldd_task;
-			struct hisi_sas_cq *cq =
-				&hisi_hba->cq[slot->dlvry_queue];
 
 			if (slot) {
+				struct hisi_sas_cq *cq =
+					&hisi_hba->cq[slot->dlvry_queue];
 				/*
 				 * flush tasklet to avoid free'ing task
 				 * before using task in IO completion
Signature · 1 line
2.7.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Gustavo A. R. Silva

There is a NULL pointer dereference in case *slot* happens to be NULL at lines 1053 and 1878:

struct hisi_sas_cq *cq =

	&hisi_hba->cq[slot->dlvry_queue];

Notice that *slot* is being NULL checked at lines 1057 and 1881:
if (slot), which implies it may be NULL.

Quoted 8 lines

Fix this by placing the declaration and definition of variable cq, which contains the pointer dereference slot->dlvry_queue, after slot has been properly NULL checked.

Addresses-Coverity-ID: 1474515 ("Dereference before null check")
Addresses-Coverity-ID: 1474520 ("Dereference before null check")

Fixes 584f53fe5f52 ("scsi: hisi_sas: Fix the race between IO completion and timeout for SMP/internal IO")

Thanks!

Quoted 39 lines

---
Changes in v2:
- Fix another instance of the same issue at line 1053.
- Update commit log.

  drivers/scsi/hisi_sas/hisi_sas_main.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/scsi/hisi_sas/hisi_sas_main.c +4 -4
index 2e5eaf1..b3f01d5 100644
@@ -1050,11 +1050,11 @@ static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
  		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  				struct hisi_sas_slot *slot = task->lldd_task;
-				struct hisi_sas_cq *cq =
-					&hisi_hba->cq[slot->dlvry_queue];
 
  				dev_err(dev, "abort tmf: TMF task timeout and not done\n");
  				if (slot) {
+					struct hisi_sas_cq *cq =
+					       &hisi_hba->cq[slot->dlvry_queue];
  					/*
  					 * flush tasklet to avoid free'ing task
  					 * before using task in IO completion
@@ -1875,10 +1875,10 @@ hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  	if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  		if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  			struct hisi_sas_slot *slot = task->lldd_task;
-			struct hisi_sas_cq *cq =
-				&hisi_hba->cq[slot->dlvry_queue];
 
  			if (slot) {
+				struct hisi_sas_cq *cq =
+					&hisi_hba->cq[slot->dlvry_queue];
  				/*
  				 * flush tasklet to avoid free'ing task
  				 * before using task in IO completion
#2 lore raw
In reply to Gustavo A. R. Silva

Gustavo,

Quoted 4 lines

There is a NULL pointer dereference in case *slot* happens to be NULL at lines 1053 and 1878:

struct hisi_sas_cq *cq =

Quoted 8 lines
	&hisi_hba->cq[slot->dlvry_queue];

Notice that *slot* is being NULL checked at lines 1057 and 1881:
if (slot), which implies it may be NULL.

Fix this by placing the declaration and definition of variable cq, which contains the pointer dereference slot->dlvry_queue, after slot has been properly NULL checked.

Applied to 4.20/scsi-queue, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#3 lore raw
Quoted 9 lines · Martin K. Petersen

Gustavo,

Quoted 6 lines

There is a NULL pointer dereference in case *slot* happens to be NULL at lines 1053 and 1878:

struct hisi_sas_cq *cq =

	&hisi_hba->cq[slot->dlvry_queue];
Quoted 9 lines
Quoted 6 lines

Notice that *slot* is being NULL checked at lines 1057 and 1881:
if (slot), which implies it may be NULL.

Fix this by placing the declaration and definition of variable cq, which contains the pointer dereference slot->dlvry_queue, after slot has been properly NULL checked.

Applied to 4.20/scsi-queue, thanks!

Glad to help. :)

Thanks
--
Gustavo