Skip to content

Commit

Permalink
Fix status code handling, reject subpage requests
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 26, 2024
1 parent 1036a6c commit 5ec9d7f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cpp/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void Controller::Status()

// If this is a successfully terminated linked command convert the status code
GetBuffer()[0] =
linked && GetStatus() == status_code::good ? (uint8_t)status_code::intermediate : (uint8_t)status_code::good;
linked && GetStatus() == status_code::good ? (uint8_t)status_code::intermediate : (uint8_t)GetStatus();
}

void Controller::MsgIn()
Expand Down
8 changes: 4 additions & 4 deletions cpp/devices/host_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ void HostServices::ReceiveOperationResults()

int HostServices::ModeSense6(cdb_t cdb, vector<uint8_t> &buf) const
{
// Block descriptors cannot be returned
if (!(cdb[1] & 0x08)) {
// Block descriptors cannot be returned, subpages are not supported
if (cdb[3] || !(cdb[1] & 0x08)) {
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_cdb);
}

Expand All @@ -229,8 +229,8 @@ int HostServices::ModeSense6(cdb_t cdb, vector<uint8_t> &buf) const

int HostServices::ModeSense10(cdb_t cdb, vector<uint8_t> &buf) const
{
// Block descriptors cannot be returned
if (!(cdb[1] & 0x08)) {
// Block descriptors cannot be returned, subpages are not supported
if (cdb[3] || !(cdb[1] & 0x08)) {
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_cdb);
}

Expand Down
10 changes: 10 additions & 0 deletions cpp/devices/storage_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ off_t StorageDevice::GetFileSize() const

int StorageDevice::ModeSense6(cdb_t cdb, vector<uint8_t> &buf) const
{
// Subpages are not supported
if (cdb[3]) {
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_cdb);
}

const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(cdb[4])));
fill_n(buf.begin(), length, 0);

Expand Down Expand Up @@ -417,6 +422,11 @@ int StorageDevice::ModeSense6(cdb_t cdb, vector<uint8_t> &buf) const

int StorageDevice::ModeSense10(cdb_t cdb, vector<uint8_t> &buf) const
{
// Subpages are not supported
if (cdb[3]) {
throw scsi_exception(sense_key::illegal_request, asc::invalid_field_in_cdb);
}

const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(GetInt16(cdb, 7))));
fill_n(buf.begin(), length, 0);

Expand Down
6 changes: 6 additions & 0 deletions cpp/test/host_services_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ TEST(HostServicesTest, ModeSense6)
EXPECT_NO_THROW(services->Dispatch(scsi_command::mode_sense6));
buffer = controller->GetBuffer();
EXPECT_EQ(0x01, buffer[0]);

controller->SetCdbByte(3, 0x01);
EXPECT_THROW(services->Dispatch(scsi_command::mode_sense6), scsi_exception)<< "Subpages are not supported";
}

TEST(HostServicesTest, ModeSense10)
Expand Down Expand Up @@ -177,6 +180,9 @@ TEST(HostServicesTest, ModeSense10)
EXPECT_NO_THROW(services->Dispatch(scsi_command::mode_sense10));
buffer = controller->GetBuffer();
EXPECT_EQ(0x02, buffer[1]);

controller->SetCdbByte(3, 0x01);
EXPECT_THROW(services->Dispatch(scsi_command::mode_sense10), scsi_exception)<< "Subpages are not supported";
}

TEST(HostServicesTest, SetUpModePages)
Expand Down
6 changes: 6 additions & 0 deletions cpp/test/storage_device_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ TEST(StorageDeviceTest, ModeSense6)
EXPECT_NO_THROW(device->Dispatch(scsi_command::mode_sense6));
const auto &buf = controller->GetBuffer();
EXPECT_EQ(0x80, buf[2]) << "Wrong device-specific parameter";

controller->SetCdbByte(3, 0x01);
EXPECT_THROW(device->Dispatch(scsi_command::mode_sense6), scsi_exception)<< "Subpages are not supported";
}

TEST(StorageDeviceTest, ModeSense10)
Expand Down Expand Up @@ -500,6 +503,9 @@ TEST(StorageDeviceTest, ModeSense10)
EXPECT_NO_THROW(device->Dispatch(scsi_command::mode_sense10));
buf = controller->GetBuffer();
EXPECT_EQ(0x80, buf[3]) << "Wrong device-specific parameter";

controller->SetCdbByte(3, 0x01);
EXPECT_THROW(device->Dispatch(scsi_command::mode_sense10), scsi_exception)<< "Subpages are not supported";
}

TEST(StorageDeviceTest, GetStatistics)
Expand Down

0 comments on commit 5ec9d7f

Please sign in to comment.