scsi: fix scsi_convert_sense crash when in_buf == NULL && in_len == 0

scsi_disk_emulate_command passes in_buf == NULL when sent a REQUEST
SENSE command.  Check for in_len == 0 before dereferencing in_buf.

Fixes: f68d98b21f
Reported-by: Roman Kagan <rkagan@virtuozzo.com>
Tested-by: Roman Kagan <rkagan@virtuozzo.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2017-12-22 16:30:34 +01:00
parent d9bcd6f7f2
commit 2770c90d43

View File

@ -322,18 +322,18 @@ int scsi_convert_sense(uint8_t *in_buf, int in_len,
SCSISense sense;
bool fixed_in;
fixed_in = (in_buf[0] & 2) == 0;
if (in_len && fixed == fixed_in) {
memcpy(buf, in_buf, MIN(len, in_len));
return MIN(len, in_len);
if (in_len == 0) {
return scsi_build_sense_buf(buf, len, SENSE_CODE(NO_SENSE), fixed);
}
if (in_len == 0) {
sense = SENSE_CODE(NO_SENSE);
fixed_in = (in_buf[0] & 2) == 0;
if (fixed == fixed_in) {
memcpy(buf, in_buf, MIN(len, in_len));
return MIN(len, in_len);
} else {
sense = scsi_parse_sense_buf(in_buf, in_len);
return scsi_build_sense_buf(buf, len, sense, fixed);
}
return scsi_build_sense_buf(buf, len, sense, fixed);
}
int scsi_sense_to_errno(int key, int asc, int ascq)