Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix length calculation in scsi_command_util::ModeSelect #1447

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion cpp/devices/scsi_command_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ string scsi_command_util::ModeSelect(scsi_command cmd, cdb_t cdb, span<const uin
// Skip block descriptors
int offset;
if (cmd == scsi_command::eCmdModeSelect10) {
if (length < 7) {
spdlog::warn("Incomplete Mode parameter header(10)");
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_parameter_list);
}
offset = 8 + GetInt16(buf, 6);
}
else {
if (length < 4) {
spdlog::warn("Incomplete Mode parameter header(6)");
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_parameter_list);
}
offset = 4 + buf[3];
}
length -= offset;
Expand Down Expand Up @@ -67,16 +75,24 @@ string scsi_command_util::ModeSelect(scsi_command cmd, cdb_t cdb, span<const uin
// OpenVMS Alpha 7.3 uses this
has_valid_page_code = true;
}
else if ((page == 0x00) && (length == 1)) {
has_valid_page_code = true;
break; // page 0 is valid if last in list, might have no size byte following
}
else {
stringstream s;
s << "Unknown MODE SELECT page code: $" << setfill('0') << setw(2) << hex << page;
result = s.str();
}

if (length < 2) { // ensure there's a size byte before accessing it
spdlog::warn("Current MODE SELECT page has no size");
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_parameter_list);
}
// Advance to the next page
const int size = buf[offset + 1] + 2;

length -= size + 1;
length -= size;
offset += size;
}

Expand Down
3 changes: 3 additions & 0 deletions cpp/test/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ class MockSCSIHD : public SCSIHD //NOSONAR Ignore inheritance hierarchy depth in
FRIEND_TEST(ScsiHdTest, DECSpecialFunctionControlPage);
FRIEND_TEST(ScsiHdTest, GetSectorSizes);
FRIEND_TEST(ScsiHdTest, ModeSelect);
FRIEND_TEST(ScsiHdTest, PageCode1);
FRIEND_TEST(ScsiHdTest, MultiplePages);

FRIEND_TEST(PiscsiExecutorTest, SetSectorSize);

public:
Expand Down
52 changes: 52 additions & 0 deletions cpp/test/scsihd_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,55 @@ TEST(ScsiHdTest, ModeSelect)
buf[20] = 0x02;
EXPECT_NO_THROW(hd.ModeSelect(scsi_command::eCmdModeSelect10, cmd, buf, 255)) << "MODE SELECT(10) is supported";
}

// Test for the ModeSelect6, page code 1 (issued by Alpha VMS)
TEST(ScsiHdTest, PageCode1)
{
MockSCSIHD hd(0, false);
vector<int> cmd = { 0x15, 0x10, 0x00, 0x00, 0x19, 0x00 };
vector<uint8_t> buf = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00
, 0x01, 0x0a, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

EXPECT_NO_THROW(hd.ModeSelect(scsi_command::eCmdModeSelect6, cmd, buf, buf.size())) << "Page code 1 is supported";
}

// Test for the ModeSelect6, multiple pages
TEST(ScsiHdTest, MultiplePages)
{
MockSCSIHD hd(0, false);
vector<uint8_t> buf = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00
, 0x02, 0x01, 0x00 // 12
, 0x02, 0x01, 0x00 // 15
, 0x02, 0x01, 0x00 // 18
, 0x02, 0x01, 0x00 // 21
, 0x02, 0x01, 0x00 // 24
, 0x02, 0x01, 0x00 // 27
, 0x02, 0x01, 0x00 // 30
, 0x02, 0x01, 0x00 // 33
, 0x02, 0x01, 0x00 // 36
, 0x02, 0x01, 0x00 // 39
, 0x02, 0x01, 0x00 // 42
, 0x02, 0x01, 0x00 // 45
, 0x02, 0x01, 0x00 // 48
, 0x02, 0x01, 0x00 // 51
, 0x02, 0x01, 0x00 // 54
, 0x02, 0x01, 0x00 // 57
, 0x02, 0x01, 0x00 // 60
, 0x02, 0x01, 0x00 // 63
, 0x02, 0x01, 0x00 // 66
, 0x02, 0x01, 0x00 // 69
, 0x02, 0x01, 0x00 // 72
, 0x02, 0x01, 0x00 // 75
, 0x02, 0x01, 0x00 // 78
, 0x02, 0x01, 0x00 // 81
, 0x02, 0x01, 0x00 // 84
, 0x02, 0x01, 0x00 // 87
, 0x02, 0x01, 0x00 // 90
, 0x03, 0x16, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x08, 0x00, 0x00, 0x01, 0x00, 0x0b, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00
};
vector<int> cmd = { 0x15, 0x10, 0x00, 0x00, static_cast<int>(buf.size()), 0x00 };

hd.SetSectorSizeInBytes(2048); // pass the "page 3" sector_size test in scsi_command_util::ModeSelect
EXPECT_NO_THROW(hd.ModeSelect(scsi_command::eCmdModeSelect6, cmd, buf, buf.size())) << "Multiple pages are supported";
}
Loading