update USBMSD::memoryRead implementation

Fix protects underlaying block device from out-of-bound reads
pull/12693/head
Maciej Bocianski 2020-03-12 10:48:08 +01:00
parent ac105f5a18
commit 89e67d3136
1 changed files with 15 additions and 13 deletions

View File

@ -876,11 +876,12 @@ void USBMSD::memoryRead(void)
n = (_length > MAX_PACKET) ? MAX_PACKET : _length; n = (_length > MAX_PACKET) ? MAX_PACKET : _length;
if ((_addr + n) > _memory_size) { if (_addr > (_memory_size - n)) {
n = _memory_size - _addr; n = _addr < _memory_size ? _memory_size - _addr : 0;
_stage = ERROR; _stage = ERROR;
} }
if (n > 0) {
// we read an entire block // we read an entire block
if (!(_addr % _block_size)) { if (!(_addr % _block_size)) {
disk_read(_page, _addr / _block_size, 1); disk_read(_page, _addr / _block_size, 1);
@ -893,6 +894,7 @@ void USBMSD::memoryRead(void)
_length -= n; _length -= n;
_csw.DataResidue -= n; _csw.DataResidue -= n;
}
if (!_length || (_stage != PROCESS_CBW)) { if (!_length || (_stage != PROCESS_CBW)) {
_csw.Status = (_stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED; _csw.Status = (_stage == PROCESS_CBW) ? CSW_PASSED : CSW_FAILED;