[SCSI] hpsa: fix potential overrun while memcpy'ing sense data

This memcpy:

memcpy(cmd->sense_buffer, ei->SenseInfo,
ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
SCSI_SENSE_BUFFERSIZE :
ei->SenseLen);

The ei->SenseLen field is filled in by the Smart Array. For requests to
logical drives, it will not exceed 32 bytes, so should be ok, but for physical
requests it depends on the target device, not the Smart Array. It's conceivable
that this could exceed the 32 byte size of ei->SenseInfo. In that case, the memcpy
would read past the end of ei->SenseInfo, copying data from the next command,
as if it were sense data, or, if it happened to be the very last command in the
block of allocated commands, could fall off the end of the allocated area and
crash. I'm not aware of anyone ever encountering this behavior, but it could
conceivably happen. This bug was found by Coverity.

Signed-off-by: Stephen M. Cameron <scameron@beardog.cce.hp.com>
Signed-off-by: James Bottomley <JBottomley@Parallels.com>

authored by Stephen M. Cameron and committed by James Bottomley db111e18 c2dd32e0

Changed files
+9 -4
drivers
scsi
+9 -4
drivers/scsi/hpsa.c
··· 1037 1037 unsigned char sense_key; 1038 1038 unsigned char asc; /* additional sense code */ 1039 1039 unsigned char ascq; /* additional sense code qualifier */ 1040 + unsigned long sense_data_size; 1040 1041 1041 1042 ei = cp->err_info; 1042 1043 cmd = (struct scsi_cmnd *) cp->scsi_cmd; ··· 1052 1051 cmd->result |= ei->ScsiStatus; 1053 1052 1054 1053 /* copy the sense data whether we need to or not. */ 1055 - memcpy(cmd->sense_buffer, ei->SenseInfo, 1056 - ei->SenseLen > SCSI_SENSE_BUFFERSIZE ? 1057 - SCSI_SENSE_BUFFERSIZE : 1058 - ei->SenseLen); 1054 + if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo)) 1055 + sense_data_size = SCSI_SENSE_BUFFERSIZE; 1056 + else 1057 + sense_data_size = sizeof(ei->SenseInfo); 1058 + if (ei->SenseLen < sense_data_size) 1059 + sense_data_size = ei->SenseLen; 1060 + 1061 + memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size); 1059 1062 scsi_set_resid(cmd, ei->ResidualCnt); 1060 1063 1061 1064 if (ei->CommandStatus == 0) {