Skip to content

Commit b843add

Browse files
Quinn Tranmartinkpetersen
authored andcommitted
scsi: qla2xxx: Fix mem access after free
System crash, where driver is accessing scsi layer's memory (scsi_cmnd->device->host) to search for a well known internal pointer (vha). The scsi_cmnd was released back to upper layer which could be freed, but the driver is still accessing it. 7 [ffffa8e8d2c3f8d0] page_fault at ffffffff86c010fe [exception RIP: __qla2x00_eh_wait_for_pending_commands+240] RIP: ffffffffc0642350 RSP: ffffa8e8d2c3f988 RFLAGS: 00010286 RAX: 0000000000000165 RBX: 0000000000000002 RCX: 00000000000036d8 RDX: 0000000000000000 RSI: ffff9c5c56535188 RDI: 0000000000000286 RBP: ffff9c5bf7aa4a58 R8: ffff9c589aecdb70 R9: 00000000000003d1 R10: 0000000000000001 R11: 0000000000380000 R12: ffff9c5c5392bc78 R13: ffff9c57044ff5c0 R14: ffff9c56b5a3aa00 R15: 00000000000006db ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 8 [ffffa8e8d2c3f9c8] qla2x00_eh_wait_for_pending_commands at ffffffffc0646dd5 [qla2xxx] 9 [ffffa8e8d2c3fa00] __qla2x00_async_tm_cmd at ffffffffc0658094 [qla2xxx] Remove access of freed memory. Currently the driver was checking to see if scsi_done was called by seeing if the sp->type has changed. Instead, check to see if the command has left the oustanding_cmds[] array as sign of scsi_done was called. Cc: [email protected] Signed-off-by: Quinn Tran <[email protected]> Signed-off-by: Nilesh Javali <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Himanshu Madhani <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 9ae615c commit b843add

File tree

2 files changed

+95
-73
lines changed

2 files changed

+95
-73
lines changed

drivers/scsi/qla2xxx/qla_isr.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,9 +1862,9 @@ qla2x00_process_completed_request(struct scsi_qla_host *vha,
18621862
}
18631863
}
18641864

1865-
srb_t *
1866-
qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1867-
struct req_que *req, void *iocb)
1865+
static srb_t *
1866+
qla_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1867+
struct req_que *req, void *iocb, u16 *ret_index)
18681868
{
18691869
struct qla_hw_data *ha = vha->hw;
18701870
sts_entry_t *pkt = iocb;
@@ -1899,12 +1899,25 @@ qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
18991899
return NULL;
19001900
}
19011901

1902-
req->outstanding_cmds[index] = NULL;
1903-
1902+
*ret_index = index;
19041903
qla_put_fw_resources(sp->qpair, &sp->iores);
19051904
return sp;
19061905
}
19071906

1907+
srb_t *
1908+
qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1909+
struct req_que *req, void *iocb)
1910+
{
1911+
uint16_t index;
1912+
srb_t *sp;
1913+
1914+
sp = qla_get_sp_from_handle(vha, func, req, iocb, &index);
1915+
if (sp)
1916+
req->outstanding_cmds[index] = NULL;
1917+
1918+
return sp;
1919+
}
1920+
19081921
static void
19091922
qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
19101923
struct mbx_entry *mbx)
@@ -3237,13 +3250,13 @@ qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
32373250
return;
32383251
}
32393252

3240-
req->outstanding_cmds[handle] = NULL;
32413253
cp = GET_CMD_SP(sp);
32423254
if (cp == NULL) {
32433255
ql_dbg(ql_dbg_io, vha, 0x3018,
32443256
"Command already returned (0x%x/%p).\n",
32453257
sts->handle, sp);
32463258

3259+
req->outstanding_cmds[handle] = NULL;
32473260
return;
32483261
}
32493262

@@ -3514,6 +3527,9 @@ qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
35143527

35153528
if (rsp->status_srb == NULL)
35163529
sp->done(sp, res);
3530+
3531+
/* for io's, clearing of outstanding_cmds[handle] means scsi_done was called */
3532+
req->outstanding_cmds[handle] = NULL;
35173533
}
35183534

35193535
/**
@@ -3590,6 +3606,7 @@ qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
35903606
uint16_t que = MSW(pkt->handle);
35913607
struct req_que *req = NULL;
35923608
int res = DID_ERROR << 16;
3609+
u16 index;
35933610

35943611
ql_dbg(ql_dbg_async, vha, 0x502a,
35953612
"iocb type %xh with error status %xh, handle %xh, rspq id %d\n",
@@ -3608,7 +3625,6 @@ qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
36083625

36093626
switch (pkt->entry_type) {
36103627
case NOTIFY_ACK_TYPE:
3611-
case STATUS_TYPE:
36123628
case STATUS_CONT_TYPE:
36133629
case LOGINOUT_PORT_IOCB_TYPE:
36143630
case CT_IOCB_TYPE:
@@ -3628,6 +3644,14 @@ qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
36283644
case CTIO_TYPE7:
36293645
case CTIO_CRC2:
36303646
return 1;
3647+
case STATUS_TYPE:
3648+
sp = qla_get_sp_from_handle(vha, func, req, pkt, &index);
3649+
if (sp) {
3650+
sp->done(sp, res);
3651+
req->outstanding_cmds[index] = NULL;
3652+
return 0;
3653+
}
3654+
break;
36313655
}
36323656
fatal:
36333657
ql_log(ql_log_warn, vha, 0x5030,

drivers/scsi/qla2xxx/qla_os.c

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,43 +1078,6 @@ qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
10781078
return 0;
10791079
}
10801080

1081-
/*
1082-
* qla2x00_eh_wait_on_command
1083-
* Waits for the command to be returned by the Firmware for some
1084-
* max time.
1085-
*
1086-
* Input:
1087-
* cmd = Scsi Command to wait on.
1088-
*
1089-
* Return:
1090-
* Completed in time : QLA_SUCCESS
1091-
* Did not complete in time : QLA_FUNCTION_FAILED
1092-
*/
1093-
static int
1094-
qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1095-
{
1096-
#define ABORT_POLLING_PERIOD 1000
1097-
#define ABORT_WAIT_ITER ((2 * 1000) / (ABORT_POLLING_PERIOD))
1098-
unsigned long wait_iter = ABORT_WAIT_ITER;
1099-
scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1100-
struct qla_hw_data *ha = vha->hw;
1101-
srb_t *sp = scsi_cmd_priv(cmd);
1102-
int ret = QLA_SUCCESS;
1103-
1104-
if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
1105-
ql_dbg(ql_dbg_taskm, vha, 0x8005,
1106-
"Return:eh_wait.\n");
1107-
return ret;
1108-
}
1109-
1110-
while (sp->type && wait_iter--)
1111-
msleep(ABORT_POLLING_PERIOD);
1112-
if (sp->type)
1113-
ret = QLA_FUNCTION_FAILED;
1114-
1115-
return ret;
1116-
}
1117-
11181081
/*
11191082
* qla2x00_wait_for_hba_online
11201083
* Wait till the HBA is online after going through
@@ -1365,6 +1328,9 @@ qla2xxx_eh_abort(struct scsi_cmnd *cmd)
13651328
return ret;
13661329
}
13671330

1331+
#define ABORT_POLLING_PERIOD 1000
1332+
#define ABORT_WAIT_ITER ((2 * 1000) / (ABORT_POLLING_PERIOD))
1333+
13681334
/*
13691335
* Returns: QLA_SUCCESS or QLA_FUNCTION_FAILED.
13701336
*/
@@ -1378,41 +1344,73 @@ __qla2x00_eh_wait_for_pending_commands(struct qla_qpair *qpair, unsigned int t,
13781344
struct req_que *req = qpair->req;
13791345
srb_t *sp;
13801346
struct scsi_cmnd *cmd;
1347+
unsigned long wait_iter = ABORT_WAIT_ITER;
1348+
bool found;
1349+
struct qla_hw_data *ha = vha->hw;
13811350

13821351
status = QLA_SUCCESS;
13831352

1384-
spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1385-
for (cnt = 1; status == QLA_SUCCESS &&
1386-
cnt < req->num_outstanding_cmds; cnt++) {
1387-
sp = req->outstanding_cmds[cnt];
1388-
if (!sp)
1389-
continue;
1390-
if (sp->type != SRB_SCSI_CMD)
1391-
continue;
1392-
if (vha->vp_idx != sp->vha->vp_idx)
1393-
continue;
1394-
match = 0;
1395-
cmd = GET_CMD_SP(sp);
1396-
switch (type) {
1397-
case WAIT_HOST:
1398-
match = 1;
1399-
break;
1400-
case WAIT_TARGET:
1401-
match = cmd->device->id == t;
1402-
break;
1403-
case WAIT_LUN:
1404-
match = (cmd->device->id == t &&
1405-
cmd->device->lun == l);
1406-
break;
1407-
}
1408-
if (!match)
1409-
continue;
1353+
while (wait_iter--) {
1354+
found = false;
14101355

1411-
spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1412-
status = qla2x00_eh_wait_on_command(cmd);
14131356
spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1357+
for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1358+
sp = req->outstanding_cmds[cnt];
1359+
if (!sp)
1360+
continue;
1361+
if (sp->type != SRB_SCSI_CMD)
1362+
continue;
1363+
if (vha->vp_idx != sp->vha->vp_idx)
1364+
continue;
1365+
match = 0;
1366+
cmd = GET_CMD_SP(sp);
1367+
switch (type) {
1368+
case WAIT_HOST:
1369+
match = 1;
1370+
break;
1371+
case WAIT_TARGET:
1372+
if (sp->fcport)
1373+
match = sp->fcport->d_id.b24 == t;
1374+
else
1375+
match = 0;
1376+
break;
1377+
case WAIT_LUN:
1378+
if (sp->fcport)
1379+
match = (sp->fcport->d_id.b24 == t &&
1380+
cmd->device->lun == l);
1381+
else
1382+
match = 0;
1383+
break;
1384+
}
1385+
if (!match)
1386+
continue;
1387+
1388+
spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1389+
1390+
if (unlikely(pci_channel_offline(ha->pdev)) ||
1391+
ha->flags.eeh_busy) {
1392+
ql_dbg(ql_dbg_taskm, vha, 0x8005,
1393+
"Return:eh_wait.\n");
1394+
return status;
1395+
}
1396+
1397+
/*
1398+
* SRB_SCSI_CMD is still in the outstanding_cmds array.
1399+
* it means scsi_done has not called. Wait for it to
1400+
* clear from outstanding_cmds.
1401+
*/
1402+
msleep(ABORT_POLLING_PERIOD);
1403+
spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1404+
found = true;
1405+
}
1406+
spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1407+
1408+
if (!found)
1409+
break;
14141410
}
1415-
spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1411+
1412+
if (!wait_iter && found)
1413+
status = QLA_FUNCTION_FAILED;
14161414

14171415
return status;
14181416
}

0 commit comments

Comments
 (0)