Skip to content

Commit a0e35d2

Browse files
maciejbocianskimergify-bot
authored andcommitted
update USBMSD::infoTransfer implementation
Fix protects underlaying block device from out-of-bound read/writes - prevents the host from setting block device addres larger then block device size - prevents the host from setting wrong read/write lenght (cherry picked from commit ac105f5)
1 parent 468bd41 commit a0e35d2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

drivers/source/usb/USBMSD.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -896,30 +896,37 @@ void USBMSD::memoryRead(void)
896896

897897
bool USBMSD::infoTransfer(void)
898898
{
899-
uint32_t n;
899+
uint32_t addr_block;
900900

901901
// Logical Block Address of First Block
902-
n = (_cbw.CB[2] << 24) | (_cbw.CB[3] << 16) | (_cbw.CB[4] << 8) | (_cbw.CB[5] << 0);
902+
addr_block = (_cbw.CB[2] << 24) | (_cbw.CB[3] << 16) | (_cbw.CB[4] << 8) | (_cbw.CB[5] << 0);
903+
904+
_addr = addr_block * _block_size;
903905

904-
_addr = n * _block_size;
906+
if ((addr_block >= _block_count) || (_addr >= _memory_size)) {
907+
_csw.Status = CSW_FAILED;
908+
sendCSW();
909+
return false;
910+
}
905911

912+
uint32_t length_blocks = 0;
906913
// Number of Blocks to transfer
907914
switch (_cbw.CB[0]) {
908915
case READ10:
909916
case WRITE10:
910917
case VERIFY10:
911-
n = (_cbw.CB[7] << 8) | (_cbw.CB[8] << 0);
918+
length_blocks = (_cbw.CB[7] << 8) | (_cbw.CB[8] << 0);
912919
break;
913920

914921
case READ12:
915922
case WRITE12:
916-
n = (_cbw.CB[6] << 24) | (_cbw.CB[7] << 16) | (_cbw.CB[8] << 8) | (_cbw.CB[9] << 0);
923+
length_blocks = (_cbw.CB[6] << 24) | (_cbw.CB[7] << 16) | (_cbw.CB[8] << 8) | (_cbw.CB[9] << 0);
917924
break;
918925
}
919926

920-
_length = n * _block_size;
927+
_length = length_blocks * _block_size;
921928

922-
if (!_cbw.DataLength) { // host requests no data
929+
if (!_cbw.DataLength || !length_blocks || (length_blocks > _block_count - addr_block) || (_length > _memory_size - _addr)) { // host requests no data or wrong length
923930
_csw.Status = CSW_FAILED;
924931
sendCSW();
925932
return false;

0 commit comments

Comments
 (0)